PR c++/30897
[official-gcc.git] / gcc / tree-complex.c
blob693c8c99347b388591b878828949e6c334554ca7
1 /* Lower complex number operations to scalar operations.
2 Copyright (C) 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "rtl.h"
26 #include "real.h"
27 #include "flags.h"
28 #include "tree-flow.h"
29 #include "tree-gimple.h"
30 #include "tree-iterator.h"
31 #include "tree-pass.h"
32 #include "tree-ssa-propagate.h"
33 #include "diagnostic.h"
36 /* For each complex ssa name, a lattice value. We're interested in finding
37 out whether a complex number is degenerate in some way, having only real
38 or only complex parts. */
40 typedef enum
42 UNINITIALIZED = 0,
43 ONLY_REAL = 1,
44 ONLY_IMAG = 2,
45 VARYING = 3
46 } complex_lattice_t;
48 #define PAIR(a, b) ((a) << 2 | (b))
50 DEF_VEC_I(complex_lattice_t);
51 DEF_VEC_ALLOC_I(complex_lattice_t, heap);
53 static VEC(complex_lattice_t, heap) *complex_lattice_values;
55 /* For each complex variable, a pair of variables for the components exists in
56 the hashtable. */
57 static htab_t complex_variable_components;
59 /* For each complex SSA_NAME, a pair of ssa names for the components. */
60 static VEC(tree, heap) *complex_ssa_name_components;
62 /* Lookup UID in the complex_variable_components hashtable and return the
63 associated tree. */
64 static tree
65 cvc_lookup (unsigned int uid)
67 struct int_tree_map *h, in;
68 in.uid = uid;
69 h = htab_find_with_hash (complex_variable_components, &in, uid);
70 return h ? h->to : NULL;
73 /* Insert the pair UID, TO into the complex_variable_components hashtable. */
75 static void
76 cvc_insert (unsigned int uid, tree to)
78 struct int_tree_map *h;
79 void **loc;
81 h = XNEW (struct int_tree_map);
82 h->uid = uid;
83 h->to = to;
84 loc = htab_find_slot_with_hash (complex_variable_components, h,
85 uid, INSERT);
86 *(struct int_tree_map **) loc = h;
89 /* Return true if T is not a zero constant. In the case of real values,
90 we're only interested in +0.0. */
92 static int
93 some_nonzerop (tree t)
95 int zerop = false;
97 if (TREE_CODE (t) == REAL_CST)
98 zerop = REAL_VALUES_IDENTICAL (TREE_REAL_CST (t), dconst0);
99 else if (TREE_CODE (t) == FIXED_CST)
100 zerop = fixed_zerop (t);
101 else if (TREE_CODE (t) == INTEGER_CST)
102 zerop = integer_zerop (t);
104 return !zerop;
107 /* Compute a lattice value from T. It may be a gimple_val, or, as a
108 special exception, a COMPLEX_EXPR. */
110 static complex_lattice_t
111 find_lattice_value (tree t)
113 tree real, imag;
114 int r, i;
115 complex_lattice_t ret;
117 switch (TREE_CODE (t))
119 case SSA_NAME:
120 return VEC_index (complex_lattice_t, complex_lattice_values,
121 SSA_NAME_VERSION (t));
123 case COMPLEX_CST:
124 real = TREE_REALPART (t);
125 imag = TREE_IMAGPART (t);
126 break;
128 case COMPLEX_EXPR:
129 real = TREE_OPERAND (t, 0);
130 imag = TREE_OPERAND (t, 1);
131 break;
133 default:
134 gcc_unreachable ();
137 r = some_nonzerop (real);
138 i = some_nonzerop (imag);
139 ret = r*ONLY_REAL + i*ONLY_IMAG;
141 /* ??? On occasion we could do better than mapping 0+0i to real, but we
142 certainly don't want to leave it UNINITIALIZED, which eventually gets
143 mapped to VARYING. */
144 if (ret == UNINITIALIZED)
145 ret = ONLY_REAL;
147 return ret;
150 /* Determine if LHS is something for which we're interested in seeing
151 simulation results. */
153 static bool
154 is_complex_reg (tree lhs)
156 return TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE && is_gimple_reg (lhs);
159 /* Mark the incoming parameters to the function as VARYING. */
161 static void
162 init_parameter_lattice_values (void)
164 tree parm;
166 for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = TREE_CHAIN (parm))
167 if (is_complex_reg (parm) && var_ann (parm) != NULL)
169 tree ssa_name = gimple_default_def (cfun, parm);
170 VEC_replace (complex_lattice_t, complex_lattice_values,
171 SSA_NAME_VERSION (ssa_name), VARYING);
175 /* Initialize DONT_SIMULATE_AGAIN for each stmt and phi. Return false if
176 we found no statements we want to simulate, and thus there's nothing for
177 the entire pass to do. */
179 static bool
180 init_dont_simulate_again (void)
182 basic_block bb;
183 block_stmt_iterator bsi;
184 tree phi;
185 bool saw_a_complex_op = false;
187 FOR_EACH_BB (bb)
189 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
190 DONT_SIMULATE_AGAIN (phi) = !is_complex_reg (PHI_RESULT (phi));
192 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
194 tree orig_stmt, stmt, rhs = NULL;
195 bool dsa;
197 orig_stmt = stmt = bsi_stmt (bsi);
199 /* Most control-altering statements must be initially
200 simulated, else we won't cover the entire cfg. */
201 dsa = !stmt_ends_bb_p (stmt);
203 switch (TREE_CODE (stmt))
205 case RETURN_EXPR:
206 /* We don't care what the lattice value of <retval> is,
207 since it's never used as an input to another computation. */
208 dsa = true;
209 stmt = TREE_OPERAND (stmt, 0);
210 if (!stmt || TREE_CODE (stmt) != GIMPLE_MODIFY_STMT)
211 break;
212 /* FALLTHRU */
214 case GIMPLE_MODIFY_STMT:
215 dsa = !is_complex_reg (GIMPLE_STMT_OPERAND (stmt, 0));
216 rhs = GIMPLE_STMT_OPERAND (stmt, 1);
217 break;
219 case COND_EXPR:
220 rhs = TREE_OPERAND (stmt, 0);
221 break;
223 default:
224 break;
227 if (rhs)
228 switch (TREE_CODE (rhs))
230 case EQ_EXPR:
231 case NE_EXPR:
232 rhs = TREE_OPERAND (rhs, 0);
233 /* FALLTHRU */
235 case PLUS_EXPR:
236 case MINUS_EXPR:
237 case MULT_EXPR:
238 case TRUNC_DIV_EXPR:
239 case CEIL_DIV_EXPR:
240 case FLOOR_DIV_EXPR:
241 case ROUND_DIV_EXPR:
242 case RDIV_EXPR:
243 case NEGATE_EXPR:
244 case CONJ_EXPR:
245 if (TREE_CODE (TREE_TYPE (rhs)) == COMPLEX_TYPE)
246 saw_a_complex_op = true;
247 break;
249 default:
250 break;
253 DONT_SIMULATE_AGAIN (orig_stmt) = dsa;
257 return saw_a_complex_op;
261 /* Evaluate statement STMT against the complex lattice defined above. */
263 static enum ssa_prop_result
264 complex_visit_stmt (tree stmt, edge *taken_edge_p ATTRIBUTE_UNUSED,
265 tree *result_p)
267 complex_lattice_t new_l, old_l, op1_l, op2_l;
268 unsigned int ver;
269 tree lhs, rhs;
271 if (TREE_CODE (stmt) != GIMPLE_MODIFY_STMT)
272 return SSA_PROP_VARYING;
274 lhs = GIMPLE_STMT_OPERAND (stmt, 0);
275 rhs = GIMPLE_STMT_OPERAND (stmt, 1);
277 /* These conditions should be satisfied due to the initial filter
278 set up in init_dont_simulate_again. */
279 gcc_assert (TREE_CODE (lhs) == SSA_NAME);
280 gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE);
282 *result_p = lhs;
283 ver = SSA_NAME_VERSION (lhs);
284 old_l = VEC_index (complex_lattice_t, complex_lattice_values, ver);
286 switch (TREE_CODE (rhs))
288 case SSA_NAME:
289 case COMPLEX_EXPR:
290 case COMPLEX_CST:
291 new_l = find_lattice_value (rhs);
292 break;
294 case PLUS_EXPR:
295 case MINUS_EXPR:
296 op1_l = find_lattice_value (TREE_OPERAND (rhs, 0));
297 op2_l = find_lattice_value (TREE_OPERAND (rhs, 1));
299 /* We've set up the lattice values such that IOR neatly
300 models addition. */
301 new_l = op1_l | op2_l;
302 break;
304 case MULT_EXPR:
305 case RDIV_EXPR:
306 case TRUNC_DIV_EXPR:
307 case CEIL_DIV_EXPR:
308 case FLOOR_DIV_EXPR:
309 case ROUND_DIV_EXPR:
310 op1_l = find_lattice_value (TREE_OPERAND (rhs, 0));
311 op2_l = find_lattice_value (TREE_OPERAND (rhs, 1));
313 /* Obviously, if either varies, so does the result. */
314 if (op1_l == VARYING || op2_l == VARYING)
315 new_l = VARYING;
316 /* Don't prematurely promote variables if we've not yet seen
317 their inputs. */
318 else if (op1_l == UNINITIALIZED)
319 new_l = op2_l;
320 else if (op2_l == UNINITIALIZED)
321 new_l = op1_l;
322 else
324 /* At this point both numbers have only one component. If the
325 numbers are of opposite kind, the result is imaginary,
326 otherwise the result is real. The add/subtract translates
327 the real/imag from/to 0/1; the ^ performs the comparison. */
328 new_l = ((op1_l - ONLY_REAL) ^ (op2_l - ONLY_REAL)) + ONLY_REAL;
330 /* Don't allow the lattice value to flip-flop indefinitely. */
331 new_l |= old_l;
333 break;
335 case NEGATE_EXPR:
336 case CONJ_EXPR:
337 new_l = find_lattice_value (TREE_OPERAND (rhs, 0));
338 break;
340 default:
341 new_l = VARYING;
342 break;
345 /* If nothing changed this round, let the propagator know. */
346 if (new_l == old_l)
347 return SSA_PROP_NOT_INTERESTING;
349 VEC_replace (complex_lattice_t, complex_lattice_values, ver, new_l);
350 return new_l == VARYING ? SSA_PROP_VARYING : SSA_PROP_INTERESTING;
353 /* Evaluate a PHI node against the complex lattice defined above. */
355 static enum ssa_prop_result
356 complex_visit_phi (tree phi)
358 complex_lattice_t new_l, old_l;
359 unsigned int ver;
360 tree lhs;
361 int i;
363 lhs = PHI_RESULT (phi);
365 /* This condition should be satisfied due to the initial filter
366 set up in init_dont_simulate_again. */
367 gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE);
369 /* We've set up the lattice values such that IOR neatly models PHI meet. */
370 new_l = UNINITIALIZED;
371 for (i = PHI_NUM_ARGS (phi) - 1; i >= 0; --i)
372 new_l |= find_lattice_value (PHI_ARG_DEF (phi, i));
374 ver = SSA_NAME_VERSION (lhs);
375 old_l = VEC_index (complex_lattice_t, complex_lattice_values, ver);
377 if (new_l == old_l)
378 return SSA_PROP_NOT_INTERESTING;
380 VEC_replace (complex_lattice_t, complex_lattice_values, ver, new_l);
381 return new_l == VARYING ? SSA_PROP_VARYING : SSA_PROP_INTERESTING;
384 /* Create one backing variable for a complex component of ORIG. */
386 static tree
387 create_one_component_var (tree type, tree orig, const char *prefix,
388 const char *suffix, enum tree_code code)
390 tree r = create_tmp_var (type, prefix);
391 add_referenced_var (r);
393 DECL_SOURCE_LOCATION (r) = DECL_SOURCE_LOCATION (orig);
394 DECL_ARTIFICIAL (r) = 1;
396 if (DECL_NAME (orig) && !DECL_IGNORED_P (orig))
398 const char *name = IDENTIFIER_POINTER (DECL_NAME (orig));
399 tree inner_type;
401 DECL_NAME (r) = get_identifier (ACONCAT ((name, suffix, NULL)));
403 inner_type = TREE_TYPE (TREE_TYPE (orig));
404 SET_DECL_DEBUG_EXPR (r, build1 (code, type, orig));
405 DECL_DEBUG_EXPR_IS_FROM (r) = 1;
406 DECL_IGNORED_P (r) = 0;
407 TREE_NO_WARNING (r) = TREE_NO_WARNING (orig);
409 else
411 DECL_IGNORED_P (r) = 1;
412 TREE_NO_WARNING (r) = 1;
415 return r;
418 /* Retrieve a value for a complex component of VAR. */
420 static tree
421 get_component_var (tree var, bool imag_p)
423 size_t decl_index = DECL_UID (var) * 2 + imag_p;
424 tree ret = cvc_lookup (decl_index);
426 if (ret == NULL)
428 ret = create_one_component_var (TREE_TYPE (TREE_TYPE (var)), var,
429 imag_p ? "CI" : "CR",
430 imag_p ? "$imag" : "$real",
431 imag_p ? IMAGPART_EXPR : REALPART_EXPR);
432 cvc_insert (decl_index, ret);
435 return ret;
438 /* Retrieve a value for a complex component of SSA_NAME. */
440 static tree
441 get_component_ssa_name (tree ssa_name, bool imag_p)
443 complex_lattice_t lattice = find_lattice_value (ssa_name);
444 size_t ssa_name_index;
445 tree ret;
447 if (lattice == (imag_p ? ONLY_REAL : ONLY_IMAG))
449 tree inner_type = TREE_TYPE (TREE_TYPE (ssa_name));
450 if (SCALAR_FLOAT_TYPE_P (inner_type))
451 return build_real (inner_type, dconst0);
452 else
453 return build_int_cst (inner_type, 0);
456 ssa_name_index = SSA_NAME_VERSION (ssa_name) * 2 + imag_p;
457 ret = VEC_index (tree, complex_ssa_name_components, ssa_name_index);
458 if (ret == NULL)
460 ret = get_component_var (SSA_NAME_VAR (ssa_name), imag_p);
461 ret = make_ssa_name (ret, NULL);
463 /* Copy some properties from the original. In particular, whether it
464 is used in an abnormal phi, and whether it's uninitialized. */
465 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ret)
466 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name);
467 if (TREE_CODE (SSA_NAME_VAR (ssa_name)) == VAR_DECL
468 && IS_EMPTY_STMT (SSA_NAME_DEF_STMT (ssa_name)))
470 SSA_NAME_DEF_STMT (ret) = SSA_NAME_DEF_STMT (ssa_name);
471 set_default_def (SSA_NAME_VAR (ret), ret);
474 VEC_replace (tree, complex_ssa_name_components, ssa_name_index, ret);
477 return ret;
480 /* Set a value for a complex component of SSA_NAME, return a STMT_LIST of
481 stuff that needs doing. */
483 static tree
484 set_component_ssa_name (tree ssa_name, bool imag_p, tree value)
486 complex_lattice_t lattice = find_lattice_value (ssa_name);
487 size_t ssa_name_index;
488 tree comp, list, last;
490 /* We know the value must be zero, else there's a bug in our lattice
491 analysis. But the value may well be a variable known to contain
492 zero. We should be safe ignoring it. */
493 if (lattice == (imag_p ? ONLY_REAL : ONLY_IMAG))
494 return NULL;
496 /* If we've already assigned an SSA_NAME to this component, then this
497 means that our walk of the basic blocks found a use before the set.
498 This is fine. Now we should create an initialization for the value
499 we created earlier. */
500 ssa_name_index = SSA_NAME_VERSION (ssa_name) * 2 + imag_p;
501 comp = VEC_index (tree, complex_ssa_name_components, ssa_name_index);
502 if (comp)
505 /* If we've nothing assigned, and the value we're given is already stable,
506 then install that as the value for this SSA_NAME. This preemptively
507 copy-propagates the value, which avoids unnecessary memory allocation. */
508 else if (is_gimple_min_invariant (value))
510 VEC_replace (tree, complex_ssa_name_components, ssa_name_index, value);
511 return NULL;
513 else if (TREE_CODE (value) == SSA_NAME
514 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name))
516 /* Replace an anonymous base value with the variable from cvc_lookup.
517 This should result in better debug info. */
518 if (DECL_IGNORED_P (SSA_NAME_VAR (value))
519 && !DECL_IGNORED_P (SSA_NAME_VAR (ssa_name)))
521 comp = get_component_var (SSA_NAME_VAR (ssa_name), imag_p);
522 replace_ssa_name_symbol (value, comp);
525 VEC_replace (tree, complex_ssa_name_components, ssa_name_index, value);
526 return NULL;
529 /* Finally, we need to stabilize the result by installing the value into
530 a new ssa name. */
531 else
532 comp = get_component_ssa_name (ssa_name, imag_p);
534 /* Do all the work to assign VALUE to COMP. */
535 value = force_gimple_operand (value, &list, false, NULL);
536 last = build_gimple_modify_stmt (comp, value);
537 append_to_statement_list (last, &list);
539 gcc_assert (SSA_NAME_DEF_STMT (comp) == NULL);
540 SSA_NAME_DEF_STMT (comp) = last;
542 return list;
545 /* Extract the real or imaginary part of a complex variable or constant.
546 Make sure that it's a proper gimple_val and gimplify it if not.
547 Emit any new code before BSI. */
549 static tree
550 extract_component (block_stmt_iterator *bsi, tree t, bool imagpart_p,
551 bool gimple_p)
553 switch (TREE_CODE (t))
555 case COMPLEX_CST:
556 return imagpart_p ? TREE_IMAGPART (t) : TREE_REALPART (t);
558 case COMPLEX_EXPR:
559 return TREE_OPERAND (t, imagpart_p);
561 case VAR_DECL:
562 case RESULT_DECL:
563 case PARM_DECL:
564 case INDIRECT_REF:
565 case COMPONENT_REF:
566 case ARRAY_REF:
568 tree inner_type = TREE_TYPE (TREE_TYPE (t));
570 t = build1 ((imagpart_p ? IMAGPART_EXPR : REALPART_EXPR),
571 inner_type, unshare_expr (t));
573 if (gimple_p)
574 t = gimplify_val (bsi, inner_type, t);
576 return t;
579 case SSA_NAME:
580 return get_component_ssa_name (t, imagpart_p);
582 default:
583 gcc_unreachable ();
587 /* Update the complex components of the ssa name on the lhs of STMT. */
589 static void
590 update_complex_components (block_stmt_iterator *bsi, tree stmt, tree r, tree i)
592 tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
593 tree list;
595 list = set_component_ssa_name (lhs, false, r);
596 if (list)
597 bsi_insert_after (bsi, list, BSI_CONTINUE_LINKING);
599 list = set_component_ssa_name (lhs, true, i);
600 if (list)
601 bsi_insert_after (bsi, list, BSI_CONTINUE_LINKING);
604 static void
605 update_complex_components_on_edge (edge e, tree lhs, tree r, tree i)
607 tree list;
609 list = set_component_ssa_name (lhs, false, r);
610 if (list)
611 bsi_insert_on_edge (e, list);
613 list = set_component_ssa_name (lhs, true, i);
614 if (list)
615 bsi_insert_on_edge (e, list);
618 /* Update an assignment to a complex variable in place. */
620 static void
621 update_complex_assignment (block_stmt_iterator *bsi, tree r, tree i)
623 tree stmt, mod;
624 tree type;
626 mod = stmt = bsi_stmt (*bsi);
627 if (TREE_CODE (stmt) == RETURN_EXPR)
628 mod = TREE_OPERAND (mod, 0);
629 else if (gimple_in_ssa_p (cfun))
630 update_complex_components (bsi, stmt, r, i);
632 type = TREE_TYPE (GIMPLE_STMT_OPERAND (mod, 1));
633 GIMPLE_STMT_OPERAND (mod, 1) = build2 (COMPLEX_EXPR, type, r, i);
634 update_stmt (stmt);
637 /* Generate code at the entry point of the function to initialize the
638 component variables for a complex parameter. */
640 static void
641 update_parameter_components (void)
643 edge entry_edge = single_succ_edge (ENTRY_BLOCK_PTR);
644 tree parm;
646 for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = TREE_CHAIN (parm))
648 tree type = TREE_TYPE (parm);
649 tree ssa_name, r, i;
651 if (TREE_CODE (type) != COMPLEX_TYPE || !is_gimple_reg (parm))
652 continue;
654 type = TREE_TYPE (type);
655 ssa_name = gimple_default_def (cfun, parm);
656 if (!ssa_name)
657 continue;
659 r = build1 (REALPART_EXPR, type, ssa_name);
660 i = build1 (IMAGPART_EXPR, type, ssa_name);
661 update_complex_components_on_edge (entry_edge, ssa_name, r, i);
665 /* Generate code to set the component variables of a complex variable
666 to match the PHI statements in block BB. */
668 static void
669 update_phi_components (basic_block bb)
671 tree phi;
673 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
674 if (is_complex_reg (PHI_RESULT (phi)))
676 tree lr, li, pr = NULL, pi = NULL;
677 unsigned int i, n;
679 lr = get_component_ssa_name (PHI_RESULT (phi), false);
680 if (TREE_CODE (lr) == SSA_NAME)
682 pr = create_phi_node (lr, bb);
683 SSA_NAME_DEF_STMT (lr) = pr;
686 li = get_component_ssa_name (PHI_RESULT (phi), true);
687 if (TREE_CODE (li) == SSA_NAME)
689 pi = create_phi_node (li, bb);
690 SSA_NAME_DEF_STMT (li) = pi;
693 for (i = 0, n = PHI_NUM_ARGS (phi); i < n; ++i)
695 tree comp, arg = PHI_ARG_DEF (phi, i);
696 if (pr)
698 comp = extract_component (NULL, arg, false, false);
699 SET_PHI_ARG_DEF (pr, i, comp);
701 if (pi)
703 comp = extract_component (NULL, arg, true, false);
704 SET_PHI_ARG_DEF (pi, i, comp);
710 /* Mark each virtual op in STMT for ssa update. */
712 static void
713 update_all_vops (tree stmt)
715 ssa_op_iter iter;
716 tree sym;
718 FOR_EACH_SSA_TREE_OPERAND (sym, stmt, iter, SSA_OP_ALL_VIRTUALS)
720 if (TREE_CODE (sym) == SSA_NAME)
721 sym = SSA_NAME_VAR (sym);
722 mark_sym_for_renaming (sym);
726 /* Expand a complex move to scalars. */
728 static void
729 expand_complex_move (block_stmt_iterator *bsi, tree stmt, tree type,
730 tree lhs, tree rhs)
732 tree inner_type = TREE_TYPE (type);
733 tree r, i;
735 if (TREE_CODE (lhs) == SSA_NAME)
737 if (is_ctrl_altering_stmt (bsi_stmt (*bsi)))
739 edge_iterator ei;
740 edge e;
742 /* The value is not assigned on the exception edges, so we need not
743 concern ourselves there. We do need to update on the fallthru
744 edge. Find it. */
745 FOR_EACH_EDGE (e, ei, bsi->bb->succs)
746 if (e->flags & EDGE_FALLTHRU)
747 goto found_fallthru;
748 gcc_unreachable ();
749 found_fallthru:
751 r = build1 (REALPART_EXPR, inner_type, lhs);
752 i = build1 (IMAGPART_EXPR, inner_type, lhs);
753 update_complex_components_on_edge (e, lhs, r, i);
755 else if (TREE_CODE (rhs) == CALL_EXPR || TREE_SIDE_EFFECTS (rhs))
757 r = build1 (REALPART_EXPR, inner_type, lhs);
758 i = build1 (IMAGPART_EXPR, inner_type, lhs);
759 update_complex_components (bsi, stmt, r, i);
761 else
763 update_all_vops (bsi_stmt (*bsi));
764 r = extract_component (bsi, rhs, 0, true);
765 i = extract_component (bsi, rhs, 1, true);
766 update_complex_assignment (bsi, r, i);
769 else if (TREE_CODE (rhs) == SSA_NAME && !TREE_SIDE_EFFECTS (lhs))
771 tree x;
773 r = extract_component (bsi, rhs, 0, false);
774 i = extract_component (bsi, rhs, 1, false);
776 x = build1 (REALPART_EXPR, inner_type, unshare_expr (lhs));
777 x = build_gimple_modify_stmt (x, r);
778 bsi_insert_before (bsi, x, BSI_SAME_STMT);
780 if (stmt == bsi_stmt (*bsi))
782 x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
783 GIMPLE_STMT_OPERAND (stmt, 0) = x;
784 GIMPLE_STMT_OPERAND (stmt, 1) = i;
786 else
788 x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
789 x = build_gimple_modify_stmt (x, i);
790 bsi_insert_before (bsi, x, BSI_SAME_STMT);
792 stmt = bsi_stmt (*bsi);
793 gcc_assert (TREE_CODE (stmt) == RETURN_EXPR);
794 GIMPLE_STMT_OPERAND (stmt, 0) = lhs;
797 update_all_vops (stmt);
798 update_stmt (stmt);
802 /* Expand complex addition to scalars:
803 a + b = (ar + br) + i(ai + bi)
804 a - b = (ar - br) + i(ai + bi)
807 static void
808 expand_complex_addition (block_stmt_iterator *bsi, tree inner_type,
809 tree ar, tree ai, tree br, tree bi,
810 enum tree_code code,
811 complex_lattice_t al, complex_lattice_t bl)
813 tree rr, ri;
815 switch (PAIR (al, bl))
817 case PAIR (ONLY_REAL, ONLY_REAL):
818 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
819 ri = ai;
820 break;
822 case PAIR (ONLY_REAL, ONLY_IMAG):
823 rr = ar;
824 if (code == MINUS_EXPR)
825 ri = gimplify_build2 (bsi, MINUS_EXPR, inner_type, ai, bi);
826 else
827 ri = bi;
828 break;
830 case PAIR (ONLY_IMAG, ONLY_REAL):
831 if (code == MINUS_EXPR)
832 rr = gimplify_build2 (bsi, MINUS_EXPR, inner_type, ar, br);
833 else
834 rr = br;
835 ri = ai;
836 break;
838 case PAIR (ONLY_IMAG, ONLY_IMAG):
839 rr = ar;
840 ri = gimplify_build2 (bsi, code, inner_type, ai, bi);
841 break;
843 case PAIR (VARYING, ONLY_REAL):
844 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
845 ri = ai;
846 break;
848 case PAIR (VARYING, ONLY_IMAG):
849 rr = ar;
850 ri = gimplify_build2 (bsi, code, inner_type, ai, bi);
851 break;
853 case PAIR (ONLY_REAL, VARYING):
854 if (code == MINUS_EXPR)
855 goto general;
856 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
857 ri = bi;
858 break;
860 case PAIR (ONLY_IMAG, VARYING):
861 if (code == MINUS_EXPR)
862 goto general;
863 rr = br;
864 ri = gimplify_build2 (bsi, code, inner_type, ai, bi);
865 break;
867 case PAIR (VARYING, VARYING):
868 general:
869 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
870 ri = gimplify_build2 (bsi, code, inner_type, ai, bi);
871 break;
873 default:
874 gcc_unreachable ();
877 update_complex_assignment (bsi, rr, ri);
880 /* Expand a complex multiplication or division to a libcall to the c99
881 compliant routines. */
883 static void
884 expand_complex_libcall (block_stmt_iterator *bsi, tree ar, tree ai,
885 tree br, tree bi, enum tree_code code)
887 enum machine_mode mode;
888 enum built_in_function bcode;
889 tree fn, stmt, type;
891 stmt = bsi_stmt (*bsi);
892 type = TREE_TYPE (GIMPLE_STMT_OPERAND (stmt, 1));
894 mode = TYPE_MODE (type);
895 gcc_assert (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT);
896 if (code == MULT_EXPR)
897 bcode = BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT;
898 else if (code == RDIV_EXPR)
899 bcode = BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT;
900 else
901 gcc_unreachable ();
902 fn = built_in_decls[bcode];
904 GIMPLE_STMT_OPERAND (stmt, 1) = build_call_expr (fn, 4, ar, ai, br, bi);
905 update_stmt (stmt);
907 if (gimple_in_ssa_p (cfun))
909 tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
910 type = TREE_TYPE (type);
911 update_complex_components (bsi, stmt,
912 build1 (REALPART_EXPR, type, lhs),
913 build1 (IMAGPART_EXPR, type, lhs));
917 /* Expand complex multiplication to scalars:
918 a * b = (ar*br - ai*bi) + i(ar*bi + br*ai)
921 static void
922 expand_complex_multiplication (block_stmt_iterator *bsi, tree inner_type,
923 tree ar, tree ai, tree br, tree bi,
924 complex_lattice_t al, complex_lattice_t bl)
926 tree rr, ri;
928 if (al < bl)
930 complex_lattice_t tl;
931 rr = ar, ar = br, br = rr;
932 ri = ai, ai = bi, bi = ri;
933 tl = al, al = bl, bl = tl;
936 switch (PAIR (al, bl))
938 case PAIR (ONLY_REAL, ONLY_REAL):
939 rr = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, br);
940 ri = ai;
941 break;
943 case PAIR (ONLY_IMAG, ONLY_REAL):
944 rr = ar;
945 if (TREE_CODE (ai) == REAL_CST
946 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (ai), dconst1))
947 ri = br;
948 else
949 ri = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, br);
950 break;
952 case PAIR (ONLY_IMAG, ONLY_IMAG):
953 rr = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, bi);
954 rr = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, rr);
955 ri = ar;
956 break;
958 case PAIR (VARYING, ONLY_REAL):
959 rr = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, br);
960 ri = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, br);
961 break;
963 case PAIR (VARYING, ONLY_IMAG):
964 rr = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, bi);
965 rr = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, rr);
966 ri = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, bi);
967 break;
969 case PAIR (VARYING, VARYING):
970 if (flag_complex_method == 2 && SCALAR_FLOAT_TYPE_P (inner_type))
972 expand_complex_libcall (bsi, ar, ai, br, bi, MULT_EXPR);
973 return;
975 else
977 tree t1, t2, t3, t4;
979 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, br);
980 t2 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, bi);
981 t3 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, bi);
983 /* Avoid expanding redundant multiplication for the common
984 case of squaring a complex number. */
985 if (ar == br && ai == bi)
986 t4 = t3;
987 else
988 t4 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, br);
990 rr = gimplify_build2 (bsi, MINUS_EXPR, inner_type, t1, t2);
991 ri = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t3, t4);
993 break;
995 default:
996 gcc_unreachable ();
999 update_complex_assignment (bsi, rr, ri);
1002 /* Expand complex division to scalars, straightforward algorithm.
1003 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1004 t = br*br + bi*bi
1007 static void
1008 expand_complex_div_straight (block_stmt_iterator *bsi, tree inner_type,
1009 tree ar, tree ai, tree br, tree bi,
1010 enum tree_code code)
1012 tree rr, ri, div, t1, t2, t3;
1014 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, br, br);
1015 t2 = gimplify_build2 (bsi, MULT_EXPR, inner_type, bi, bi);
1016 div = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, t2);
1018 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, br);
1019 t2 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, bi);
1020 t3 = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, t2);
1021 rr = gimplify_build2 (bsi, code, inner_type, t3, div);
1023 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, br);
1024 t2 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, bi);
1025 t3 = gimplify_build2 (bsi, MINUS_EXPR, inner_type, t1, t2);
1026 ri = gimplify_build2 (bsi, code, inner_type, t3, div);
1028 update_complex_assignment (bsi, rr, ri);
1031 /* Expand complex division to scalars, modified algorithm to minimize
1032 overflow with wide input ranges. */
1034 static void
1035 expand_complex_div_wide (block_stmt_iterator *bsi, tree inner_type,
1036 tree ar, tree ai, tree br, tree bi,
1037 enum tree_code code)
1039 tree rr, ri, ratio, div, t1, t2, tr, ti, cond;
1040 basic_block bb_cond, bb_true, bb_false, bb_join;
1042 /* Examine |br| < |bi|, and branch. */
1043 t1 = gimplify_build1 (bsi, ABS_EXPR, inner_type, br);
1044 t2 = gimplify_build1 (bsi, ABS_EXPR, inner_type, bi);
1045 cond = fold_build2 (LT_EXPR, boolean_type_node, t1, t2);
1046 STRIP_NOPS (cond);
1048 bb_cond = bb_true = bb_false = bb_join = NULL;
1049 rr = ri = tr = ti = NULL;
1050 if (!TREE_CONSTANT (cond))
1052 edge e;
1054 cond = build3 (COND_EXPR, void_type_node, cond, NULL_TREE, NULL_TREE);
1055 bsi_insert_before (bsi, cond, BSI_SAME_STMT);
1057 /* Split the original block, and create the TRUE and FALSE blocks. */
1058 e = split_block (bsi->bb, cond);
1059 bb_cond = e->src;
1060 bb_join = e->dest;
1061 bb_true = create_empty_bb (bb_cond);
1062 bb_false = create_empty_bb (bb_true);
1064 /* Wire the blocks together. */
1065 e->flags = EDGE_TRUE_VALUE;
1066 redirect_edge_succ (e, bb_true);
1067 make_edge (bb_cond, bb_false, EDGE_FALSE_VALUE);
1068 make_edge (bb_true, bb_join, EDGE_FALLTHRU);
1069 make_edge (bb_false, bb_join, EDGE_FALLTHRU);
1071 /* Update dominance info. Note that bb_join's data was
1072 updated by split_block. */
1073 if (dom_info_available_p (CDI_DOMINATORS))
1075 set_immediate_dominator (CDI_DOMINATORS, bb_true, bb_cond);
1076 set_immediate_dominator (CDI_DOMINATORS, bb_false, bb_cond);
1079 rr = make_rename_temp (inner_type, NULL);
1080 ri = make_rename_temp (inner_type, NULL);
1083 /* In the TRUE branch, we compute
1084 ratio = br/bi;
1085 div = (br * ratio) + bi;
1086 tr = (ar * ratio) + ai;
1087 ti = (ai * ratio) - ar;
1088 tr = tr / div;
1089 ti = ti / div; */
1090 if (bb_true || integer_nonzerop (cond))
1092 if (bb_true)
1094 *bsi = bsi_last (bb_true);
1095 bsi_insert_after (bsi, build_empty_stmt (), BSI_NEW_STMT);
1098 ratio = gimplify_build2 (bsi, code, inner_type, br, bi);
1100 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, br, ratio);
1101 div = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, bi);
1103 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, ratio);
1104 tr = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, ai);
1106 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, ratio);
1107 ti = gimplify_build2 (bsi, MINUS_EXPR, inner_type, t1, ar);
1109 tr = gimplify_build2 (bsi, code, inner_type, tr, div);
1110 ti = gimplify_build2 (bsi, code, inner_type, ti, div);
1112 if (bb_true)
1114 t1 = build_gimple_modify_stmt (rr, tr);
1115 bsi_insert_before (bsi, t1, BSI_SAME_STMT);
1116 t1 = build_gimple_modify_stmt (ri, ti);
1117 bsi_insert_before (bsi, t1, BSI_SAME_STMT);
1118 bsi_remove (bsi, true);
1122 /* In the FALSE branch, we compute
1123 ratio = d/c;
1124 divisor = (d * ratio) + c;
1125 tr = (b * ratio) + a;
1126 ti = b - (a * ratio);
1127 tr = tr / div;
1128 ti = ti / div; */
1129 if (bb_false || integer_zerop (cond))
1131 if (bb_false)
1133 *bsi = bsi_last (bb_false);
1134 bsi_insert_after (bsi, build_empty_stmt (), BSI_NEW_STMT);
1137 ratio = gimplify_build2 (bsi, code, inner_type, bi, br);
1139 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, bi, ratio);
1140 div = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, br);
1142 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, ratio);
1143 tr = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, ar);
1145 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, ratio);
1146 ti = gimplify_build2 (bsi, MINUS_EXPR, inner_type, ai, t1);
1148 tr = gimplify_build2 (bsi, code, inner_type, tr, div);
1149 ti = gimplify_build2 (bsi, code, inner_type, ti, div);
1151 if (bb_false)
1153 t1 = build_gimple_modify_stmt (rr, tr);
1154 bsi_insert_before (bsi, t1, BSI_SAME_STMT);
1155 t1 = build_gimple_modify_stmt (ri, ti);
1156 bsi_insert_before (bsi, t1, BSI_SAME_STMT);
1157 bsi_remove (bsi, true);
1161 if (bb_join)
1162 *bsi = bsi_start (bb_join);
1163 else
1164 rr = tr, ri = ti;
1166 update_complex_assignment (bsi, rr, ri);
1169 /* Expand complex division to scalars. */
1171 static void
1172 expand_complex_division (block_stmt_iterator *bsi, tree inner_type,
1173 tree ar, tree ai, tree br, tree bi,
1174 enum tree_code code,
1175 complex_lattice_t al, complex_lattice_t bl)
1177 tree rr, ri;
1179 switch (PAIR (al, bl))
1181 case PAIR (ONLY_REAL, ONLY_REAL):
1182 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
1183 ri = ai;
1184 break;
1186 case PAIR (ONLY_REAL, ONLY_IMAG):
1187 rr = ai;
1188 ri = gimplify_build2 (bsi, code, inner_type, ar, bi);
1189 ri = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, ri);
1190 break;
1192 case PAIR (ONLY_IMAG, ONLY_REAL):
1193 rr = ar;
1194 ri = gimplify_build2 (bsi, code, inner_type, ai, br);
1195 break;
1197 case PAIR (ONLY_IMAG, ONLY_IMAG):
1198 rr = gimplify_build2 (bsi, code, inner_type, ai, bi);
1199 ri = ar;
1200 break;
1202 case PAIR (VARYING, ONLY_REAL):
1203 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
1204 ri = gimplify_build2 (bsi, code, inner_type, ai, br);
1205 break;
1207 case PAIR (VARYING, ONLY_IMAG):
1208 rr = gimplify_build2 (bsi, code, inner_type, ai, bi);
1209 ri = gimplify_build2 (bsi, code, inner_type, ar, bi);
1210 ri = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, ri);
1212 case PAIR (ONLY_REAL, VARYING):
1213 case PAIR (ONLY_IMAG, VARYING):
1214 case PAIR (VARYING, VARYING):
1215 switch (flag_complex_method)
1217 case 0:
1218 /* straightforward implementation of complex divide acceptable. */
1219 expand_complex_div_straight (bsi, inner_type, ar, ai, br, bi, code);
1220 break;
1222 case 2:
1223 if (SCALAR_FLOAT_TYPE_P (inner_type))
1225 expand_complex_libcall (bsi, ar, ai, br, bi, code);
1226 break;
1228 /* FALLTHRU */
1230 case 1:
1231 /* wide ranges of inputs must work for complex divide. */
1232 expand_complex_div_wide (bsi, inner_type, ar, ai, br, bi, code);
1233 break;
1235 default:
1236 gcc_unreachable ();
1238 return;
1240 default:
1241 gcc_unreachable ();
1244 update_complex_assignment (bsi, rr, ri);
1247 /* Expand complex negation to scalars:
1248 -a = (-ar) + i(-ai)
1251 static void
1252 expand_complex_negation (block_stmt_iterator *bsi, tree inner_type,
1253 tree ar, tree ai)
1255 tree rr, ri;
1257 rr = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, ar);
1258 ri = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, ai);
1260 update_complex_assignment (bsi, rr, ri);
1263 /* Expand complex conjugate to scalars:
1264 ~a = (ar) + i(-ai)
1267 static void
1268 expand_complex_conjugate (block_stmt_iterator *bsi, tree inner_type,
1269 tree ar, tree ai)
1271 tree ri;
1273 ri = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, ai);
1275 update_complex_assignment (bsi, ar, ri);
1278 /* Expand complex comparison (EQ or NE only). */
1280 static void
1281 expand_complex_comparison (block_stmt_iterator *bsi, tree ar, tree ai,
1282 tree br, tree bi, enum tree_code code)
1284 tree cr, ci, cc, stmt, expr, type;
1286 cr = gimplify_build2 (bsi, code, boolean_type_node, ar, br);
1287 ci = gimplify_build2 (bsi, code, boolean_type_node, ai, bi);
1288 cc = gimplify_build2 (bsi,
1289 (code == EQ_EXPR ? TRUTH_AND_EXPR : TRUTH_OR_EXPR),
1290 boolean_type_node, cr, ci);
1292 stmt = expr = bsi_stmt (*bsi);
1294 switch (TREE_CODE (stmt))
1296 case RETURN_EXPR:
1297 expr = TREE_OPERAND (stmt, 0);
1298 /* FALLTHRU */
1299 case GIMPLE_MODIFY_STMT:
1300 type = TREE_TYPE (GIMPLE_STMT_OPERAND (expr, 1));
1301 GIMPLE_STMT_OPERAND (expr, 1) = fold_convert (type, cc);
1302 break;
1303 case COND_EXPR:
1304 TREE_OPERAND (stmt, 0) = cc;
1305 break;
1306 default:
1307 gcc_unreachable ();
1310 update_stmt (stmt);
1313 /* Process one statement. If we identify a complex operation, expand it. */
1315 static void
1316 expand_complex_operations_1 (block_stmt_iterator *bsi)
1318 tree stmt = bsi_stmt (*bsi);
1319 tree rhs, type, inner_type;
1320 tree ac, ar, ai, bc, br, bi;
1321 complex_lattice_t al, bl;
1322 enum tree_code code;
1324 switch (TREE_CODE (stmt))
1326 case RETURN_EXPR:
1327 stmt = TREE_OPERAND (stmt, 0);
1328 if (!stmt)
1329 return;
1330 if (TREE_CODE (stmt) != GIMPLE_MODIFY_STMT)
1331 return;
1332 /* FALLTHRU */
1334 case GIMPLE_MODIFY_STMT:
1335 rhs = GIMPLE_STMT_OPERAND (stmt, 1);
1336 break;
1338 case COND_EXPR:
1339 rhs = TREE_OPERAND (stmt, 0);
1340 break;
1342 default:
1343 return;
1346 type = TREE_TYPE (rhs);
1347 code = TREE_CODE (rhs);
1349 /* Initial filter for operations we handle. */
1350 switch (code)
1352 case PLUS_EXPR:
1353 case MINUS_EXPR:
1354 case MULT_EXPR:
1355 case TRUNC_DIV_EXPR:
1356 case CEIL_DIV_EXPR:
1357 case FLOOR_DIV_EXPR:
1358 case ROUND_DIV_EXPR:
1359 case RDIV_EXPR:
1360 case NEGATE_EXPR:
1361 case CONJ_EXPR:
1362 if (TREE_CODE (type) != COMPLEX_TYPE)
1363 return;
1364 inner_type = TREE_TYPE (type);
1365 break;
1367 case EQ_EXPR:
1368 case NE_EXPR:
1369 inner_type = TREE_TYPE (TREE_OPERAND (rhs, 1));
1370 if (TREE_CODE (inner_type) != COMPLEX_TYPE)
1371 return;
1372 break;
1374 default:
1376 tree lhs, rhs;
1378 /* COND_EXPR may also fallthru here, but we do not need to do anything
1379 with it. */
1380 if (TREE_CODE (stmt) != GIMPLE_MODIFY_STMT)
1381 return;
1383 lhs = GIMPLE_STMT_OPERAND (stmt, 0);
1384 rhs = GIMPLE_STMT_OPERAND (stmt, 1);
1386 if (TREE_CODE (type) == COMPLEX_TYPE)
1387 expand_complex_move (bsi, stmt, type, lhs, rhs);
1388 else if ((TREE_CODE (rhs) == REALPART_EXPR
1389 || TREE_CODE (rhs) == IMAGPART_EXPR)
1390 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
1392 GENERIC_TREE_OPERAND (stmt, 1)
1393 = extract_component (bsi, TREE_OPERAND (rhs, 0),
1394 TREE_CODE (rhs) == IMAGPART_EXPR, false);
1395 update_stmt (stmt);
1398 return;
1401 /* Extract the components of the two complex values. Make sure and
1402 handle the common case of the same value used twice specially. */
1403 ac = TREE_OPERAND (rhs, 0);
1404 ar = extract_component (bsi, ac, 0, true);
1405 ai = extract_component (bsi, ac, 1, true);
1407 if (TREE_CODE_CLASS (code) == tcc_unary)
1408 bc = br = bi = NULL;
1409 else
1411 bc = TREE_OPERAND (rhs, 1);
1412 if (ac == bc)
1413 br = ar, bi = ai;
1414 else
1416 br = extract_component (bsi, bc, 0, true);
1417 bi = extract_component (bsi, bc, 1, true);
1421 if (gimple_in_ssa_p (cfun))
1423 al = find_lattice_value (ac);
1424 if (al == UNINITIALIZED)
1425 al = VARYING;
1427 if (TREE_CODE_CLASS (code) == tcc_unary)
1428 bl = UNINITIALIZED;
1429 else if (ac == bc)
1430 bl = al;
1431 else
1433 bl = find_lattice_value (bc);
1434 if (bl == UNINITIALIZED)
1435 bl = VARYING;
1438 else
1439 al = bl = VARYING;
1441 switch (code)
1443 case PLUS_EXPR:
1444 case MINUS_EXPR:
1445 expand_complex_addition (bsi, inner_type, ar, ai, br, bi, code, al, bl);
1446 break;
1448 case MULT_EXPR:
1449 expand_complex_multiplication (bsi, inner_type, ar, ai, br, bi, al, bl);
1450 break;
1452 case TRUNC_DIV_EXPR:
1453 case CEIL_DIV_EXPR:
1454 case FLOOR_DIV_EXPR:
1455 case ROUND_DIV_EXPR:
1456 case RDIV_EXPR:
1457 expand_complex_division (bsi, inner_type, ar, ai, br, bi, code, al, bl);
1458 break;
1460 case NEGATE_EXPR:
1461 expand_complex_negation (bsi, inner_type, ar, ai);
1462 break;
1464 case CONJ_EXPR:
1465 expand_complex_conjugate (bsi, inner_type, ar, ai);
1466 break;
1468 case EQ_EXPR:
1469 case NE_EXPR:
1470 expand_complex_comparison (bsi, ar, ai, br, bi, code);
1471 break;
1473 default:
1474 gcc_unreachable ();
1479 /* Entry point for complex operation lowering during optimization. */
1481 static unsigned int
1482 tree_lower_complex (void)
1484 int old_last_basic_block;
1485 block_stmt_iterator bsi;
1486 basic_block bb;
1488 if (!init_dont_simulate_again ())
1489 return 0;
1491 complex_lattice_values = VEC_alloc (complex_lattice_t, heap, num_ssa_names);
1492 VEC_safe_grow_cleared (complex_lattice_t, heap,
1493 complex_lattice_values, num_ssa_names);
1495 init_parameter_lattice_values ();
1496 ssa_propagate (complex_visit_stmt, complex_visit_phi);
1498 complex_variable_components = htab_create (10, int_tree_map_hash,
1499 int_tree_map_eq, free);
1501 complex_ssa_name_components = VEC_alloc (tree, heap, 2*num_ssa_names);
1502 VEC_safe_grow_cleared (tree, heap, complex_ssa_name_components,
1503 2 * num_ssa_names);
1505 update_parameter_components ();
1507 /* ??? Ideally we'd traverse the blocks in breadth-first order. */
1508 old_last_basic_block = last_basic_block;
1509 FOR_EACH_BB (bb)
1511 if (bb->index >= old_last_basic_block)
1512 continue;
1513 update_phi_components (bb);
1514 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1515 expand_complex_operations_1 (&bsi);
1518 bsi_commit_edge_inserts ();
1520 htab_delete (complex_variable_components);
1521 VEC_free (tree, heap, complex_ssa_name_components);
1522 VEC_free (complex_lattice_t, heap, complex_lattice_values);
1523 return 0;
1526 struct tree_opt_pass pass_lower_complex =
1528 "cplxlower", /* name */
1529 0, /* gate */
1530 tree_lower_complex, /* execute */
1531 NULL, /* sub */
1532 NULL, /* next */
1533 0, /* static_pass_number */
1534 0, /* tv_id */
1535 PROP_ssa, /* properties_required */
1536 0, /* properties_provided */
1537 0, /* properties_destroyed */
1538 0, /* todo_flags_start */
1539 TODO_dump_func
1540 | TODO_ggc_collect
1541 | TODO_update_ssa
1542 | TODO_verify_stmts, /* todo_flags_finish */
1543 0 /* letter */
1547 /* Entry point for complex operation lowering without optimization. */
1549 static unsigned int
1550 tree_lower_complex_O0 (void)
1552 int old_last_basic_block = last_basic_block;
1553 block_stmt_iterator bsi;
1554 basic_block bb;
1556 FOR_EACH_BB (bb)
1558 if (bb->index >= old_last_basic_block)
1559 continue;
1560 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1561 expand_complex_operations_1 (&bsi);
1563 return 0;
1566 static bool
1567 gate_no_optimization (void)
1569 /* With errors, normal optimization passes are not run. If we don't
1570 lower complex operations at all, rtl expansion will abort. */
1571 return optimize == 0 || sorrycount || errorcount;
1574 struct tree_opt_pass pass_lower_complex_O0 =
1576 "cplxlower0", /* name */
1577 gate_no_optimization, /* gate */
1578 tree_lower_complex_O0, /* execute */
1579 NULL, /* sub */
1580 NULL, /* next */
1581 0, /* static_pass_number */
1582 0, /* tv_id */
1583 PROP_cfg, /* properties_required */
1584 0, /* properties_provided */
1585 0, /* properties_destroyed */
1586 0, /* todo_flags_start */
1587 TODO_dump_func | TODO_ggc_collect
1588 | TODO_verify_stmts, /* todo_flags_finish */
1589 0 /* letter */