2006-12-16 Manuel Lopez-Ibanez <manu@gcc.gnu.org>
[official-gcc.git] / gcc / tree-complex.c
blobc3f4e0453ad96ce1a466161a5b042c9b08fe6aa3
1 /* Lower complex number operations to scalar operations.
2 Copyright (C) 2004, 2005 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 2, 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 COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301, USA. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "real.h"
28 #include "flags.h"
29 #include "tree-flow.h"
30 #include "tree-gimple.h"
31 #include "tree-iterator.h"
32 #include "tree-pass.h"
33 #include "tree-ssa-propagate.h"
34 #include "diagnostic.h"
37 /* For each complex ssa name, a lattice value. We're interested in finding
38 out whether a complex number is degenerate in some way, having only real
39 or only complex parts. */
41 typedef enum
43 UNINITIALIZED = 0,
44 ONLY_REAL = 1,
45 ONLY_IMAG = 2,
46 VARYING = 3
47 } complex_lattice_t;
49 #define PAIR(a, b) ((a) << 2 | (b))
51 DEF_VEC_I(complex_lattice_t);
52 DEF_VEC_ALLOC_I(complex_lattice_t, heap);
54 static VEC(complex_lattice_t, heap) *complex_lattice_values;
56 /* For each complex variable, a pair of variables for the components exists in
57 the hashtable. */
58 static htab_t complex_variable_components;
60 /* For each complex SSA_NAME, a pair of ssa names for the components. */
61 static VEC(tree, heap) *complex_ssa_name_components;
63 /* Lookup UID in the complex_variable_components hashtable and return the
64 associated tree. */
65 static tree
66 cvc_lookup (unsigned int uid)
68 struct int_tree_map *h, in;
69 in.uid = uid;
70 h = htab_find_with_hash (complex_variable_components, &in, uid);
71 return h ? h->to : NULL;
74 /* Insert the pair UID, TO into the complex_variable_components hashtable. */
76 static void
77 cvc_insert (unsigned int uid, tree to)
79 struct int_tree_map *h;
80 void **loc;
82 h = XNEW (struct int_tree_map);
83 h->uid = uid;
84 h->to = to;
85 loc = htab_find_slot_with_hash (complex_variable_components, h,
86 uid, INSERT);
87 *(struct int_tree_map **) loc = h;
90 /* Return true if T is not a zero constant. In the case of real values,
91 we're only interested in +0.0. */
93 static int
94 some_nonzerop (tree t)
96 int zerop = false;
98 if (TREE_CODE (t) == REAL_CST)
99 zerop = REAL_VALUES_IDENTICAL (TREE_REAL_CST (t), dconst0);
100 else if (TREE_CODE (t) == INTEGER_CST)
101 zerop = integer_zerop (t);
103 return !zerop;
106 /* Compute a lattice value from T. It may be a gimple_val, or, as a
107 special exception, a COMPLEX_EXPR. */
109 static complex_lattice_t
110 find_lattice_value (tree t)
112 tree real, imag;
113 int r, i;
114 complex_lattice_t ret;
116 switch (TREE_CODE (t))
118 case SSA_NAME:
119 return VEC_index (complex_lattice_t, complex_lattice_values,
120 SSA_NAME_VERSION (t));
122 case COMPLEX_CST:
123 real = TREE_REALPART (t);
124 imag = TREE_IMAGPART (t);
125 break;
127 case COMPLEX_EXPR:
128 real = TREE_OPERAND (t, 0);
129 imag = TREE_OPERAND (t, 1);
130 break;
132 default:
133 gcc_unreachable ();
136 r = some_nonzerop (real);
137 i = some_nonzerop (imag);
138 ret = r*ONLY_REAL + i*ONLY_IMAG;
140 /* ??? On occasion we could do better than mapping 0+0i to real, but we
141 certainly don't want to leave it UNINITIALIZED, which eventually gets
142 mapped to VARYING. */
143 if (ret == UNINITIALIZED)
144 ret = ONLY_REAL;
146 return ret;
149 /* Determine if LHS is something for which we're interested in seeing
150 simulation results. */
152 static bool
153 is_complex_reg (tree lhs)
155 return TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE && is_gimple_reg (lhs);
158 /* Mark the incoming parameters to the function as VARYING. */
160 static void
161 init_parameter_lattice_values (void)
163 tree parm;
165 for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = TREE_CHAIN (parm))
166 if (is_complex_reg (parm) && var_ann (parm) != NULL)
168 tree ssa_name = gimple_default_def (cfun, parm);
169 VEC_replace (complex_lattice_t, complex_lattice_values,
170 SSA_NAME_VERSION (ssa_name), VARYING);
174 /* Initialize DONT_SIMULATE_AGAIN for each stmt and phi. Return false if
175 we found no statements we want to simulate, and thus there's nothing for
176 the entire pass to do. */
178 static bool
179 init_dont_simulate_again (void)
181 basic_block bb;
182 block_stmt_iterator bsi;
183 tree phi;
184 bool saw_a_complex_op = false;
186 FOR_EACH_BB (bb)
188 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
189 DONT_SIMULATE_AGAIN (phi) = !is_complex_reg (PHI_RESULT (phi));
191 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
193 tree orig_stmt, stmt, rhs = NULL;
194 bool dsa;
196 orig_stmt = stmt = bsi_stmt (bsi);
198 /* Most control-altering statements must be initially
199 simulated, else we won't cover the entire cfg. */
200 dsa = !stmt_ends_bb_p (stmt);
202 switch (TREE_CODE (stmt))
204 case RETURN_EXPR:
205 /* We don't care what the lattice value of <retval> is,
206 since it's never used as an input to another computation. */
207 dsa = true;
208 stmt = TREE_OPERAND (stmt, 0);
209 if (!stmt || TREE_CODE (stmt) != GIMPLE_MODIFY_STMT)
210 break;
211 /* FALLTHRU */
213 case GIMPLE_MODIFY_STMT:
214 dsa = !is_complex_reg (GIMPLE_STMT_OPERAND (stmt, 0));
215 rhs = GIMPLE_STMT_OPERAND (stmt, 1);
216 break;
218 case COND_EXPR:
219 rhs = TREE_OPERAND (stmt, 0);
220 break;
222 default:
223 break;
226 if (rhs)
227 switch (TREE_CODE (rhs))
229 case EQ_EXPR:
230 case NE_EXPR:
231 rhs = TREE_OPERAND (rhs, 0);
232 /* FALLTHRU */
234 case PLUS_EXPR:
235 case MINUS_EXPR:
236 case MULT_EXPR:
237 case TRUNC_DIV_EXPR:
238 case CEIL_DIV_EXPR:
239 case FLOOR_DIV_EXPR:
240 case ROUND_DIV_EXPR:
241 case RDIV_EXPR:
242 case NEGATE_EXPR:
243 case CONJ_EXPR:
244 if (TREE_CODE (TREE_TYPE (rhs)) == COMPLEX_TYPE)
245 saw_a_complex_op = true;
246 break;
248 default:
249 break;
252 DONT_SIMULATE_AGAIN (orig_stmt) = dsa;
256 return saw_a_complex_op;
260 /* Evaluate statement STMT against the complex lattice defined above. */
262 static enum ssa_prop_result
263 complex_visit_stmt (tree stmt, edge *taken_edge_p ATTRIBUTE_UNUSED,
264 tree *result_p)
266 complex_lattice_t new_l, old_l, op1_l, op2_l;
267 unsigned int ver;
268 tree lhs, rhs;
270 if (TREE_CODE (stmt) != GIMPLE_MODIFY_STMT)
271 return SSA_PROP_VARYING;
273 lhs = GIMPLE_STMT_OPERAND (stmt, 0);
274 rhs = GIMPLE_STMT_OPERAND (stmt, 1);
276 /* These conditions should be satisfied due to the initial filter
277 set up in init_dont_simulate_again. */
278 gcc_assert (TREE_CODE (lhs) == SSA_NAME);
279 gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE);
281 *result_p = lhs;
282 ver = SSA_NAME_VERSION (lhs);
283 old_l = VEC_index (complex_lattice_t, complex_lattice_values, ver);
285 switch (TREE_CODE (rhs))
287 case SSA_NAME:
288 case COMPLEX_EXPR:
289 case COMPLEX_CST:
290 new_l = find_lattice_value (rhs);
291 break;
293 case PLUS_EXPR:
294 case MINUS_EXPR:
295 op1_l = find_lattice_value (TREE_OPERAND (rhs, 0));
296 op2_l = find_lattice_value (TREE_OPERAND (rhs, 1));
298 /* We've set up the lattice values such that IOR neatly
299 models addition. */
300 new_l = op1_l | op2_l;
301 break;
303 case MULT_EXPR:
304 case RDIV_EXPR:
305 case TRUNC_DIV_EXPR:
306 case CEIL_DIV_EXPR:
307 case FLOOR_DIV_EXPR:
308 case ROUND_DIV_EXPR:
309 op1_l = find_lattice_value (TREE_OPERAND (rhs, 0));
310 op2_l = find_lattice_value (TREE_OPERAND (rhs, 1));
312 /* Obviously, if either varies, so does the result. */
313 if (op1_l == VARYING || op2_l == VARYING)
314 new_l = VARYING;
315 /* Don't prematurely promote variables if we've not yet seen
316 their inputs. */
317 else if (op1_l == UNINITIALIZED)
318 new_l = op2_l;
319 else if (op2_l == UNINITIALIZED)
320 new_l = op1_l;
321 else
323 /* At this point both numbers have only one component. If the
324 numbers are of opposite kind, the result is imaginary,
325 otherwise the result is real. The add/subtract translates
326 the real/imag from/to 0/1; the ^ performs the comparison. */
327 new_l = ((op1_l - ONLY_REAL) ^ (op2_l - ONLY_REAL)) + ONLY_REAL;
329 /* Don't allow the lattice value to flip-flop indefinitely. */
330 new_l |= old_l;
332 break;
334 case NEGATE_EXPR:
335 case CONJ_EXPR:
336 new_l = find_lattice_value (TREE_OPERAND (rhs, 0));
337 break;
339 default:
340 new_l = VARYING;
341 break;
344 /* If nothing changed this round, let the propagator know. */
345 if (new_l == old_l)
346 return SSA_PROP_NOT_INTERESTING;
348 VEC_replace (complex_lattice_t, complex_lattice_values, ver, new_l);
349 return new_l == VARYING ? SSA_PROP_VARYING : SSA_PROP_INTERESTING;
352 /* Evaluate a PHI node against the complex lattice defined above. */
354 static enum ssa_prop_result
355 complex_visit_phi (tree phi)
357 complex_lattice_t new_l, old_l;
358 unsigned int ver;
359 tree lhs;
360 int i;
362 lhs = PHI_RESULT (phi);
364 /* This condition should be satisfied due to the initial filter
365 set up in init_dont_simulate_again. */
366 gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE);
368 /* We've set up the lattice values such that IOR neatly models PHI meet. */
369 new_l = UNINITIALIZED;
370 for (i = PHI_NUM_ARGS (phi) - 1; i >= 0; --i)
371 new_l |= find_lattice_value (PHI_ARG_DEF (phi, i));
373 ver = SSA_NAME_VERSION (lhs);
374 old_l = VEC_index (complex_lattice_t, complex_lattice_values, ver);
376 if (new_l == old_l)
377 return SSA_PROP_NOT_INTERESTING;
379 VEC_replace (complex_lattice_t, complex_lattice_values, ver, new_l);
380 return new_l == VARYING ? SSA_PROP_VARYING : SSA_PROP_INTERESTING;
383 /* Create one backing variable for a complex component of ORIG. */
385 static tree
386 create_one_component_var (tree type, tree orig, const char *prefix,
387 const char *suffix, enum tree_code code)
389 tree r = create_tmp_var (type, prefix);
390 add_referenced_var (r);
392 DECL_SOURCE_LOCATION (r) = DECL_SOURCE_LOCATION (orig);
393 DECL_ARTIFICIAL (r) = 1;
395 if (DECL_NAME (orig) && !DECL_IGNORED_P (orig))
397 const char *name = IDENTIFIER_POINTER (DECL_NAME (orig));
398 tree inner_type;
400 DECL_NAME (r) = get_identifier (ACONCAT ((name, suffix, NULL)));
402 inner_type = TREE_TYPE (TREE_TYPE (orig));
403 SET_DECL_DEBUG_EXPR (r, build1 (code, type, orig));
404 DECL_DEBUG_EXPR_IS_FROM (r) = 1;
405 DECL_IGNORED_P (r) = 0;
406 TREE_NO_WARNING (r) = TREE_NO_WARNING (orig);
408 else
410 DECL_IGNORED_P (r) = 1;
411 TREE_NO_WARNING (r) = 1;
414 return r;
417 /* Retrieve a value for a complex component of VAR. */
419 static tree
420 get_component_var (tree var, bool imag_p)
422 size_t decl_index = DECL_UID (var) * 2 + imag_p;
423 tree ret = cvc_lookup (decl_index);
425 if (ret == NULL)
427 ret = create_one_component_var (TREE_TYPE (TREE_TYPE (var)), var,
428 imag_p ? "CI" : "CR",
429 imag_p ? "$imag" : "$real",
430 imag_p ? IMAGPART_EXPR : REALPART_EXPR);
431 cvc_insert (decl_index, ret);
434 return ret;
437 /* Retrieve a value for a complex component of SSA_NAME. */
439 static tree
440 get_component_ssa_name (tree ssa_name, bool imag_p)
442 complex_lattice_t lattice = find_lattice_value (ssa_name);
443 size_t ssa_name_index;
444 tree ret;
446 if (lattice == (imag_p ? ONLY_REAL : ONLY_IMAG))
448 tree inner_type = TREE_TYPE (TREE_TYPE (ssa_name));
449 if (SCALAR_FLOAT_TYPE_P (inner_type))
450 return build_real (inner_type, dconst0);
451 else
452 return build_int_cst (inner_type, 0);
455 ssa_name_index = SSA_NAME_VERSION (ssa_name) * 2 + imag_p;
456 ret = VEC_index (tree, complex_ssa_name_components, ssa_name_index);
457 if (ret == NULL)
459 ret = get_component_var (SSA_NAME_VAR (ssa_name), imag_p);
460 ret = make_ssa_name (ret, NULL);
462 /* Copy some properties from the original. In particular, whether it
463 is used in an abnormal phi, and whether it's uninitialized. */
464 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ret)
465 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name);
466 if (TREE_CODE (SSA_NAME_VAR (ssa_name)) == VAR_DECL
467 && IS_EMPTY_STMT (SSA_NAME_DEF_STMT (ssa_name)))
469 SSA_NAME_DEF_STMT (ret) = SSA_NAME_DEF_STMT (ssa_name);
470 set_default_def (SSA_NAME_VAR (ret), ret);
473 VEC_replace (tree, complex_ssa_name_components, ssa_name_index, ret);
476 return ret;
479 /* Set a value for a complex component of SSA_NAME, return a STMT_LIST of
480 stuff that needs doing. */
482 static tree
483 set_component_ssa_name (tree ssa_name, bool imag_p, tree value)
485 complex_lattice_t lattice = find_lattice_value (ssa_name);
486 size_t ssa_name_index;
487 tree comp, list, last;
489 /* We know the value must be zero, else there's a bug in our lattice
490 analysis. But the value may well be a variable known to contain
491 zero. We should be safe ignoring it. */
492 if (lattice == (imag_p ? ONLY_REAL : ONLY_IMAG))
493 return NULL;
495 /* If we've already assigned an SSA_NAME to this component, then this
496 means that our walk of the basic blocks found a use before the set.
497 This is fine. Now we should create an initialization for the value
498 we created earlier. */
499 ssa_name_index = SSA_NAME_VERSION (ssa_name) * 2 + imag_p;
500 comp = VEC_index (tree, complex_ssa_name_components, ssa_name_index);
501 if (comp)
504 /* If we've nothing assigned, and the value we're given is already stable,
505 then install that as the value for this SSA_NAME. This preemptively
506 copy-propagates the value, which avoids unnecessary memory allocation. */
507 else if (is_gimple_min_invariant (value))
509 VEC_replace (tree, complex_ssa_name_components, ssa_name_index, value);
510 return NULL;
512 else if (TREE_CODE (value) == SSA_NAME
513 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name))
515 /* Replace an anonymous base value with the variable from cvc_lookup.
516 This should result in better debug info. */
517 if (DECL_IGNORED_P (SSA_NAME_VAR (value))
518 && !DECL_IGNORED_P (SSA_NAME_VAR (ssa_name)))
520 comp = get_component_var (SSA_NAME_VAR (ssa_name), imag_p);
521 replace_ssa_name_symbol (value, comp);
524 VEC_replace (tree, complex_ssa_name_components, ssa_name_index, value);
525 return NULL;
528 /* Finally, we need to stabilize the result by installing the value into
529 a new ssa name. */
530 else
531 comp = get_component_ssa_name (ssa_name, imag_p);
533 /* Do all the work to assign VALUE to COMP. */
534 value = force_gimple_operand (value, &list, false, NULL);
535 last = build2_gimple (GIMPLE_MODIFY_STMT, comp, value);
536 append_to_statement_list (last, &list);
538 gcc_assert (SSA_NAME_DEF_STMT (comp) == NULL);
539 SSA_NAME_DEF_STMT (comp) = last;
541 return list;
544 /* Extract the real or imaginary part of a complex variable or constant.
545 Make sure that it's a proper gimple_val and gimplify it if not.
546 Emit any new code before BSI. */
548 static tree
549 extract_component (block_stmt_iterator *bsi, tree t, bool imagpart_p,
550 bool gimple_p)
552 switch (TREE_CODE (t))
554 case COMPLEX_CST:
555 return imagpart_p ? TREE_IMAGPART (t) : TREE_REALPART (t);
557 case COMPLEX_EXPR:
558 return TREE_OPERAND (t, imagpart_p);
560 case VAR_DECL:
561 case RESULT_DECL:
562 case PARM_DECL:
563 case INDIRECT_REF:
564 case COMPONENT_REF:
565 case ARRAY_REF:
567 tree inner_type = TREE_TYPE (TREE_TYPE (t));
569 t = build1 ((imagpart_p ? IMAGPART_EXPR : REALPART_EXPR),
570 inner_type, unshare_expr (t));
572 if (gimple_p)
573 t = gimplify_val (bsi, inner_type, t);
575 return t;
578 case SSA_NAME:
579 return get_component_ssa_name (t, imagpart_p);
581 default:
582 gcc_unreachable ();
586 /* Update the complex components of the ssa name on the lhs of STMT. */
588 static void
589 update_complex_components (block_stmt_iterator *bsi, tree stmt, tree r, tree i)
591 tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
592 tree list;
594 list = set_component_ssa_name (lhs, false, r);
595 if (list)
596 bsi_insert_after (bsi, list, BSI_CONTINUE_LINKING);
598 list = set_component_ssa_name (lhs, true, i);
599 if (list)
600 bsi_insert_after (bsi, list, BSI_CONTINUE_LINKING);
603 static void
604 update_complex_components_on_edge (edge e, tree lhs, tree r, tree i)
606 tree list;
608 list = set_component_ssa_name (lhs, false, r);
609 if (list)
610 bsi_insert_on_edge (e, list);
612 list = set_component_ssa_name (lhs, true, i);
613 if (list)
614 bsi_insert_on_edge (e, list);
617 /* Update an assignment to a complex variable in place. */
619 static void
620 update_complex_assignment (block_stmt_iterator *bsi, tree r, tree i)
622 tree stmt, mod;
623 tree type;
625 mod = stmt = bsi_stmt (*bsi);
626 if (TREE_CODE (stmt) == RETURN_EXPR)
627 mod = TREE_OPERAND (mod, 0);
628 else if (gimple_in_ssa_p (cfun))
629 update_complex_components (bsi, stmt, r, i);
631 type = TREE_TYPE (GIMPLE_STMT_OPERAND (mod, 1));
632 GIMPLE_STMT_OPERAND (mod, 1) = build2 (COMPLEX_EXPR, type, r, i);
633 update_stmt (stmt);
636 /* Generate code at the entry point of the function to initialize the
637 component variables for a complex parameter. */
639 static void
640 update_parameter_components (void)
642 edge entry_edge = single_succ_edge (ENTRY_BLOCK_PTR);
643 tree parm;
645 for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = TREE_CHAIN (parm))
647 tree type = TREE_TYPE (parm);
648 tree ssa_name, r, i;
650 if (TREE_CODE (type) != COMPLEX_TYPE || !is_gimple_reg (parm))
651 continue;
653 type = TREE_TYPE (type);
654 ssa_name = gimple_default_def (cfun, parm);
655 if (!ssa_name)
656 continue;
658 r = build1 (REALPART_EXPR, type, ssa_name);
659 i = build1 (IMAGPART_EXPR, type, ssa_name);
660 update_complex_components_on_edge (entry_edge, ssa_name, r, i);
664 /* Generate code to set the component variables of a complex variable
665 to match the PHI statements in block BB. */
667 static void
668 update_phi_components (basic_block bb)
670 tree phi;
672 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
673 if (is_complex_reg (PHI_RESULT (phi)))
675 tree lr, li, pr = NULL, pi = NULL;
676 unsigned int i, n;
678 lr = get_component_ssa_name (PHI_RESULT (phi), false);
679 if (TREE_CODE (lr) == SSA_NAME)
681 pr = create_phi_node (lr, bb);
682 SSA_NAME_DEF_STMT (lr) = pr;
685 li = get_component_ssa_name (PHI_RESULT (phi), true);
686 if (TREE_CODE (li) == SSA_NAME)
688 pi = create_phi_node (li, bb);
689 SSA_NAME_DEF_STMT (li) = pi;
692 for (i = 0, n = PHI_NUM_ARGS (phi); i < n; ++i)
694 tree comp, arg = PHI_ARG_DEF (phi, i);
695 if (pr)
697 comp = extract_component (NULL, arg, false, false);
698 SET_PHI_ARG_DEF (pr, i, comp);
700 if (pi)
702 comp = extract_component (NULL, arg, true, false);
703 SET_PHI_ARG_DEF (pi, i, comp);
709 /* Mark each virtual op in STMT for ssa update. */
711 static void
712 update_all_vops (tree stmt)
714 ssa_op_iter iter;
715 tree sym;
717 FOR_EACH_SSA_TREE_OPERAND (sym, stmt, iter, SSA_OP_ALL_VIRTUALS)
719 if (TREE_CODE (sym) == SSA_NAME)
720 sym = SSA_NAME_VAR (sym);
721 mark_sym_for_renaming (sym);
725 /* Expand a complex move to scalars. */
727 static void
728 expand_complex_move (block_stmt_iterator *bsi, tree stmt, tree type,
729 tree lhs, tree rhs)
731 tree inner_type = TREE_TYPE (type);
732 tree r, i;
734 if (TREE_CODE (lhs) == SSA_NAME)
736 if (is_ctrl_altering_stmt (bsi_stmt (*bsi)))
738 edge_iterator ei;
739 edge e;
741 /* The value is not assigned on the exception edges, so we need not
742 concern ourselves there. We do need to update on the fallthru
743 edge. Find it. */
744 FOR_EACH_EDGE (e, ei, bsi->bb->succs)
745 if (e->flags & EDGE_FALLTHRU)
746 goto found_fallthru;
747 gcc_unreachable ();
748 found_fallthru:
750 r = build1 (REALPART_EXPR, inner_type, lhs);
751 i = build1 (IMAGPART_EXPR, inner_type, lhs);
752 update_complex_components_on_edge (e, lhs, r, i);
754 else if (TREE_CODE (rhs) == CALL_EXPR || TREE_SIDE_EFFECTS (rhs))
756 r = build1 (REALPART_EXPR, inner_type, lhs);
757 i = build1 (IMAGPART_EXPR, inner_type, lhs);
758 update_complex_components (bsi, stmt, r, i);
760 else
762 update_all_vops (bsi_stmt (*bsi));
763 r = extract_component (bsi, rhs, 0, true);
764 i = extract_component (bsi, rhs, 1, true);
765 update_complex_assignment (bsi, r, i);
768 else if (TREE_CODE (rhs) == SSA_NAME && !TREE_SIDE_EFFECTS (lhs))
770 tree x;
772 r = extract_component (bsi, rhs, 0, false);
773 i = extract_component (bsi, rhs, 1, false);
775 x = build1 (REALPART_EXPR, inner_type, unshare_expr (lhs));
776 x = build2_gimple (GIMPLE_MODIFY_STMT, x, r);
777 bsi_insert_before (bsi, x, BSI_SAME_STMT);
779 if (stmt == bsi_stmt (*bsi))
781 x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
782 GIMPLE_STMT_OPERAND (stmt, 0) = x;
783 GIMPLE_STMT_OPERAND (stmt, 1) = i;
785 else
787 x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
788 x = build2_gimple (GIMPLE_MODIFY_STMT, x, i);
789 bsi_insert_before (bsi, x, BSI_SAME_STMT);
791 stmt = bsi_stmt (*bsi);
792 gcc_assert (TREE_CODE (stmt) == RETURN_EXPR);
793 GIMPLE_STMT_OPERAND (stmt, 0) = lhs;
796 update_all_vops (stmt);
797 update_stmt (stmt);
801 /* Expand complex addition to scalars:
802 a + b = (ar + br) + i(ai + bi)
803 a - b = (ar - br) + i(ai + bi)
806 static void
807 expand_complex_addition (block_stmt_iterator *bsi, tree inner_type,
808 tree ar, tree ai, tree br, tree bi,
809 enum tree_code code,
810 complex_lattice_t al, complex_lattice_t bl)
812 tree rr, ri;
814 switch (PAIR (al, bl))
816 case PAIR (ONLY_REAL, ONLY_REAL):
817 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
818 ri = ai;
819 break;
821 case PAIR (ONLY_REAL, ONLY_IMAG):
822 rr = ar;
823 if (code == MINUS_EXPR)
824 ri = gimplify_build2 (bsi, MINUS_EXPR, inner_type, ai, bi);
825 else
826 ri = bi;
827 break;
829 case PAIR (ONLY_IMAG, ONLY_REAL):
830 if (code == MINUS_EXPR)
831 rr = gimplify_build2 (bsi, MINUS_EXPR, inner_type, ar, br);
832 else
833 rr = br;
834 ri = ai;
835 break;
837 case PAIR (ONLY_IMAG, ONLY_IMAG):
838 rr = ar;
839 ri = gimplify_build2 (bsi, code, inner_type, ai, bi);
840 break;
842 case PAIR (VARYING, ONLY_REAL):
843 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
844 ri = ai;
845 break;
847 case PAIR (VARYING, ONLY_IMAG):
848 rr = ar;
849 ri = gimplify_build2 (bsi, code, inner_type, ai, bi);
850 break;
852 case PAIR (ONLY_REAL, VARYING):
853 if (code == MINUS_EXPR)
854 goto general;
855 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
856 ri = bi;
857 break;
859 case PAIR (ONLY_IMAG, VARYING):
860 if (code == MINUS_EXPR)
861 goto general;
862 rr = br;
863 ri = gimplify_build2 (bsi, code, inner_type, ai, bi);
864 break;
866 case PAIR (VARYING, VARYING):
867 general:
868 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
869 ri = gimplify_build2 (bsi, code, inner_type, ai, bi);
870 break;
872 default:
873 gcc_unreachable ();
876 update_complex_assignment (bsi, rr, ri);
879 /* Expand a complex multiplication or division to a libcall to the c99
880 compliant routines. */
882 static void
883 expand_complex_libcall (block_stmt_iterator *bsi, tree ar, tree ai,
884 tree br, tree bi, enum tree_code code)
886 enum machine_mode mode;
887 enum built_in_function bcode;
888 tree args, fn, stmt, type;
890 args = tree_cons (NULL, bi, NULL);
891 args = tree_cons (NULL, br, args);
892 args = tree_cons (NULL, ai, args);
893 args = tree_cons (NULL, ar, args);
895 stmt = bsi_stmt (*bsi);
896 type = TREE_TYPE (GIMPLE_STMT_OPERAND (stmt, 1));
898 mode = TYPE_MODE (type);
899 gcc_assert (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT);
900 if (code == MULT_EXPR)
901 bcode = BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT;
902 else if (code == RDIV_EXPR)
903 bcode = BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT;
904 else
905 gcc_unreachable ();
906 fn = built_in_decls[bcode];
908 GIMPLE_STMT_OPERAND (stmt, 1)
909 = build3 (CALL_EXPR, type, build_fold_addr_expr (fn), args, NULL);
910 update_stmt (stmt);
912 if (gimple_in_ssa_p (cfun))
914 tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
915 type = TREE_TYPE (type);
916 update_complex_components (bsi, stmt,
917 build1 (REALPART_EXPR, type, lhs),
918 build1 (IMAGPART_EXPR, type, lhs));
922 /* Expand complex multiplication to scalars:
923 a * b = (ar*br - ai*bi) + i(ar*bi + br*ai)
926 static void
927 expand_complex_multiplication (block_stmt_iterator *bsi, tree inner_type,
928 tree ar, tree ai, tree br, tree bi,
929 complex_lattice_t al, complex_lattice_t bl)
931 tree rr, ri;
933 if (al < bl)
935 complex_lattice_t tl;
936 rr = ar, ar = br, br = rr;
937 ri = ai, ai = bi, bi = ri;
938 tl = al, al = bl, bl = tl;
941 switch (PAIR (al, bl))
943 case PAIR (ONLY_REAL, ONLY_REAL):
944 rr = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, br);
945 ri = ai;
946 break;
948 case PAIR (ONLY_IMAG, ONLY_REAL):
949 rr = ar;
950 if (TREE_CODE (ai) == REAL_CST
951 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (ai), dconst1))
952 ri = br;
953 else
954 ri = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, br);
955 break;
957 case PAIR (ONLY_IMAG, ONLY_IMAG):
958 rr = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, bi);
959 rr = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, rr);
960 ri = ar;
961 break;
963 case PAIR (VARYING, ONLY_REAL):
964 rr = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, br);
965 ri = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, br);
966 break;
968 case PAIR (VARYING, ONLY_IMAG):
969 rr = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, bi);
970 rr = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, rr);
971 ri = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, bi);
972 break;
974 case PAIR (VARYING, VARYING):
975 if (flag_complex_method == 2 && SCALAR_FLOAT_TYPE_P (inner_type))
977 expand_complex_libcall (bsi, ar, ai, br, bi, MULT_EXPR);
978 return;
980 else
982 tree t1, t2, t3, t4;
984 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, br);
985 t2 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, bi);
986 t3 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, bi);
988 /* Avoid expanding redundant multiplication for the common
989 case of squaring a complex number. */
990 if (ar == br && ai == bi)
991 t4 = t3;
992 else
993 t4 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, br);
995 rr = gimplify_build2 (bsi, MINUS_EXPR, inner_type, t1, t2);
996 ri = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t3, t4);
998 break;
1000 default:
1001 gcc_unreachable ();
1004 update_complex_assignment (bsi, rr, ri);
1007 /* Expand complex division to scalars, straightforward algorithm.
1008 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1009 t = br*br + bi*bi
1012 static void
1013 expand_complex_div_straight (block_stmt_iterator *bsi, tree inner_type,
1014 tree ar, tree ai, tree br, tree bi,
1015 enum tree_code code)
1017 tree rr, ri, div, t1, t2, t3;
1019 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, br, br);
1020 t2 = gimplify_build2 (bsi, MULT_EXPR, inner_type, bi, bi);
1021 div = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, t2);
1023 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, br);
1024 t2 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, bi);
1025 t3 = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, t2);
1026 rr = gimplify_build2 (bsi, code, inner_type, t3, div);
1028 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, br);
1029 t2 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, bi);
1030 t3 = gimplify_build2 (bsi, MINUS_EXPR, inner_type, t1, t2);
1031 ri = gimplify_build2 (bsi, code, inner_type, t3, div);
1033 update_complex_assignment (bsi, rr, ri);
1036 /* Expand complex division to scalars, modified algorithm to minimize
1037 overflow with wide input ranges. */
1039 static void
1040 expand_complex_div_wide (block_stmt_iterator *bsi, tree inner_type,
1041 tree ar, tree ai, tree br, tree bi,
1042 enum tree_code code)
1044 tree rr, ri, ratio, div, t1, t2, tr, ti, cond;
1045 basic_block bb_cond, bb_true, bb_false, bb_join;
1047 /* Examine |br| < |bi|, and branch. */
1048 t1 = gimplify_build1 (bsi, ABS_EXPR, inner_type, br);
1049 t2 = gimplify_build1 (bsi, ABS_EXPR, inner_type, bi);
1050 cond = fold_build2 (LT_EXPR, boolean_type_node, t1, t2);
1051 STRIP_NOPS (cond);
1053 bb_cond = bb_true = bb_false = bb_join = NULL;
1054 rr = ri = tr = ti = NULL;
1055 if (!TREE_CONSTANT (cond))
1057 edge e;
1059 cond = build3 (COND_EXPR, void_type_node, cond, NULL_TREE, NULL_TREE);
1060 bsi_insert_before (bsi, cond, BSI_SAME_STMT);
1062 /* Split the original block, and create the TRUE and FALSE blocks. */
1063 e = split_block (bsi->bb, cond);
1064 bb_cond = e->src;
1065 bb_join = e->dest;
1066 bb_true = create_empty_bb (bb_cond);
1067 bb_false = create_empty_bb (bb_true);
1069 t1 = build1 (GOTO_EXPR, void_type_node, tree_block_label (bb_true));
1070 t2 = build1 (GOTO_EXPR, void_type_node, tree_block_label (bb_false));
1071 COND_EXPR_THEN (cond) = t1;
1072 COND_EXPR_ELSE (cond) = t2;
1074 /* Wire the blocks together. */
1075 e->flags = EDGE_TRUE_VALUE;
1076 redirect_edge_succ (e, bb_true);
1077 make_edge (bb_cond, bb_false, EDGE_FALSE_VALUE);
1078 make_edge (bb_true, bb_join, EDGE_FALLTHRU);
1079 make_edge (bb_false, bb_join, EDGE_FALLTHRU);
1081 /* Update dominance info. Note that bb_join's data was
1082 updated by split_block. */
1083 if (dom_info_available_p (CDI_DOMINATORS))
1085 set_immediate_dominator (CDI_DOMINATORS, bb_true, bb_cond);
1086 set_immediate_dominator (CDI_DOMINATORS, bb_false, bb_cond);
1089 rr = make_rename_temp (inner_type, NULL);
1090 ri = make_rename_temp (inner_type, NULL);
1093 /* In the TRUE branch, we compute
1094 ratio = br/bi;
1095 div = (br * ratio) + bi;
1096 tr = (ar * ratio) + ai;
1097 ti = (ai * ratio) - ar;
1098 tr = tr / div;
1099 ti = ti / div; */
1100 if (bb_true || integer_nonzerop (cond))
1102 if (bb_true)
1104 *bsi = bsi_last (bb_true);
1105 bsi_insert_after (bsi, build_empty_stmt (), BSI_NEW_STMT);
1108 ratio = gimplify_build2 (bsi, code, inner_type, br, bi);
1110 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, br, ratio);
1111 div = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, bi);
1113 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, ratio);
1114 tr = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, ai);
1116 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, ratio);
1117 ti = gimplify_build2 (bsi, MINUS_EXPR, inner_type, t1, ar);
1119 tr = gimplify_build2 (bsi, code, inner_type, tr, div);
1120 ti = gimplify_build2 (bsi, code, inner_type, ti, div);
1122 if (bb_true)
1124 t1 = build2_gimple (GIMPLE_MODIFY_STMT, rr, tr);
1125 bsi_insert_before (bsi, t1, BSI_SAME_STMT);
1126 t1 = build2_gimple (GIMPLE_MODIFY_STMT, ri, ti);
1127 bsi_insert_before (bsi, t1, BSI_SAME_STMT);
1128 bsi_remove (bsi, true);
1132 /* In the FALSE branch, we compute
1133 ratio = d/c;
1134 divisor = (d * ratio) + c;
1135 tr = (b * ratio) + a;
1136 ti = b - (a * ratio);
1137 tr = tr / div;
1138 ti = ti / div; */
1139 if (bb_false || integer_zerop (cond))
1141 if (bb_false)
1143 *bsi = bsi_last (bb_false);
1144 bsi_insert_after (bsi, build_empty_stmt (), BSI_NEW_STMT);
1147 ratio = gimplify_build2 (bsi, code, inner_type, bi, br);
1149 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, bi, ratio);
1150 div = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, br);
1152 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, ratio);
1153 tr = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, ar);
1155 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, ratio);
1156 ti = gimplify_build2 (bsi, MINUS_EXPR, inner_type, ai, t1);
1158 tr = gimplify_build2 (bsi, code, inner_type, tr, div);
1159 ti = gimplify_build2 (bsi, code, inner_type, ti, div);
1161 if (bb_false)
1163 t1 = build2_gimple (GIMPLE_MODIFY_STMT, rr, tr);
1164 bsi_insert_before (bsi, t1, BSI_SAME_STMT);
1165 t1 = build2_gimple (GIMPLE_MODIFY_STMT, ri, ti);
1166 bsi_insert_before (bsi, t1, BSI_SAME_STMT);
1167 bsi_remove (bsi, true);
1171 if (bb_join)
1172 *bsi = bsi_start (bb_join);
1173 else
1174 rr = tr, ri = ti;
1176 update_complex_assignment (bsi, rr, ri);
1179 /* Expand complex division to scalars. */
1181 static void
1182 expand_complex_division (block_stmt_iterator *bsi, tree inner_type,
1183 tree ar, tree ai, tree br, tree bi,
1184 enum tree_code code,
1185 complex_lattice_t al, complex_lattice_t bl)
1187 tree rr, ri;
1189 switch (PAIR (al, bl))
1191 case PAIR (ONLY_REAL, ONLY_REAL):
1192 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
1193 ri = ai;
1194 break;
1196 case PAIR (ONLY_REAL, ONLY_IMAG):
1197 rr = ai;
1198 ri = gimplify_build2 (bsi, code, inner_type, ar, bi);
1199 ri = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, ri);
1200 break;
1202 case PAIR (ONLY_IMAG, ONLY_REAL):
1203 rr = ar;
1204 ri = gimplify_build2 (bsi, code, inner_type, ai, br);
1205 break;
1207 case PAIR (ONLY_IMAG, ONLY_IMAG):
1208 rr = gimplify_build2 (bsi, code, inner_type, ai, bi);
1209 ri = ar;
1210 break;
1212 case PAIR (VARYING, ONLY_REAL):
1213 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
1214 ri = gimplify_build2 (bsi, code, inner_type, ai, br);
1215 break;
1217 case PAIR (VARYING, ONLY_IMAG):
1218 rr = gimplify_build2 (bsi, code, inner_type, ai, bi);
1219 ri = gimplify_build2 (bsi, code, inner_type, ar, bi);
1220 ri = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, ri);
1222 case PAIR (ONLY_REAL, VARYING):
1223 case PAIR (ONLY_IMAG, VARYING):
1224 case PAIR (VARYING, VARYING):
1225 switch (flag_complex_method)
1227 case 0:
1228 /* straightforward implementation of complex divide acceptable. */
1229 expand_complex_div_straight (bsi, inner_type, ar, ai, br, bi, code);
1230 break;
1232 case 2:
1233 if (SCALAR_FLOAT_TYPE_P (inner_type))
1235 expand_complex_libcall (bsi, ar, ai, br, bi, code);
1236 break;
1238 /* FALLTHRU */
1240 case 1:
1241 /* wide ranges of inputs must work for complex divide. */
1242 expand_complex_div_wide (bsi, inner_type, ar, ai, br, bi, code);
1243 break;
1245 default:
1246 gcc_unreachable ();
1248 return;
1250 default:
1251 gcc_unreachable ();
1254 update_complex_assignment (bsi, rr, ri);
1257 /* Expand complex negation to scalars:
1258 -a = (-ar) + i(-ai)
1261 static void
1262 expand_complex_negation (block_stmt_iterator *bsi, tree inner_type,
1263 tree ar, tree ai)
1265 tree rr, ri;
1267 rr = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, ar);
1268 ri = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, ai);
1270 update_complex_assignment (bsi, rr, ri);
1273 /* Expand complex conjugate to scalars:
1274 ~a = (ar) + i(-ai)
1277 static void
1278 expand_complex_conjugate (block_stmt_iterator *bsi, tree inner_type,
1279 tree ar, tree ai)
1281 tree ri;
1283 ri = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, ai);
1285 update_complex_assignment (bsi, ar, ri);
1288 /* Expand complex comparison (EQ or NE only). */
1290 static void
1291 expand_complex_comparison (block_stmt_iterator *bsi, tree ar, tree ai,
1292 tree br, tree bi, enum tree_code code)
1294 tree cr, ci, cc, stmt, expr, type;
1296 cr = gimplify_build2 (bsi, code, boolean_type_node, ar, br);
1297 ci = gimplify_build2 (bsi, code, boolean_type_node, ai, bi);
1298 cc = gimplify_build2 (bsi,
1299 (code == EQ_EXPR ? TRUTH_AND_EXPR : TRUTH_OR_EXPR),
1300 boolean_type_node, cr, ci);
1302 stmt = expr = bsi_stmt (*bsi);
1304 switch (TREE_CODE (stmt))
1306 case RETURN_EXPR:
1307 expr = TREE_OPERAND (stmt, 0);
1308 /* FALLTHRU */
1309 case GIMPLE_MODIFY_STMT:
1310 type = TREE_TYPE (GIMPLE_STMT_OPERAND (expr, 1));
1311 GIMPLE_STMT_OPERAND (expr, 1) = fold_convert (type, cc);
1312 break;
1313 case COND_EXPR:
1314 TREE_OPERAND (stmt, 0) = cc;
1315 break;
1316 default:
1317 gcc_unreachable ();
1320 update_stmt (stmt);
1323 /* Process one statement. If we identify a complex operation, expand it. */
1325 static void
1326 expand_complex_operations_1 (block_stmt_iterator *bsi)
1328 tree stmt = bsi_stmt (*bsi);
1329 tree rhs, type, inner_type;
1330 tree ac, ar, ai, bc, br, bi;
1331 complex_lattice_t al, bl;
1332 enum tree_code code;
1334 switch (TREE_CODE (stmt))
1336 case RETURN_EXPR:
1337 stmt = TREE_OPERAND (stmt, 0);
1338 if (!stmt)
1339 return;
1340 if (TREE_CODE (stmt) != GIMPLE_MODIFY_STMT)
1341 return;
1342 /* FALLTHRU */
1344 case GIMPLE_MODIFY_STMT:
1345 rhs = GIMPLE_STMT_OPERAND (stmt, 1);
1346 break;
1348 case COND_EXPR:
1349 rhs = TREE_OPERAND (stmt, 0);
1350 break;
1352 default:
1353 return;
1356 type = TREE_TYPE (rhs);
1357 code = TREE_CODE (rhs);
1359 /* Initial filter for operations we handle. */
1360 switch (code)
1362 case PLUS_EXPR:
1363 case MINUS_EXPR:
1364 case MULT_EXPR:
1365 case TRUNC_DIV_EXPR:
1366 case CEIL_DIV_EXPR:
1367 case FLOOR_DIV_EXPR:
1368 case ROUND_DIV_EXPR:
1369 case RDIV_EXPR:
1370 case NEGATE_EXPR:
1371 case CONJ_EXPR:
1372 if (TREE_CODE (type) != COMPLEX_TYPE)
1373 return;
1374 inner_type = TREE_TYPE (type);
1375 break;
1377 case EQ_EXPR:
1378 case NE_EXPR:
1379 inner_type = TREE_TYPE (TREE_OPERAND (rhs, 1));
1380 if (TREE_CODE (inner_type) != COMPLEX_TYPE)
1381 return;
1382 break;
1384 default:
1386 tree lhs = GENERIC_TREE_OPERAND (stmt, 0);
1387 tree rhs = GENERIC_TREE_OPERAND (stmt, 1);
1389 if (TREE_CODE (type) == COMPLEX_TYPE)
1390 expand_complex_move (bsi, stmt, type, lhs, rhs);
1391 else if ((TREE_CODE (rhs) == REALPART_EXPR
1392 || TREE_CODE (rhs) == IMAGPART_EXPR)
1393 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
1395 GENERIC_TREE_OPERAND (stmt, 1)
1396 = extract_component (bsi, TREE_OPERAND (rhs, 0),
1397 TREE_CODE (rhs) == IMAGPART_EXPR, false);
1398 update_stmt (stmt);
1401 return;
1404 /* Extract the components of the two complex values. Make sure and
1405 handle the common case of the same value used twice specially. */
1406 ac = TREE_OPERAND (rhs, 0);
1407 ar = extract_component (bsi, ac, 0, true);
1408 ai = extract_component (bsi, ac, 1, true);
1410 if (TREE_CODE_CLASS (code) == tcc_unary)
1411 bc = br = bi = NULL;
1412 else
1414 bc = TREE_OPERAND (rhs, 1);
1415 if (ac == bc)
1416 br = ar, bi = ai;
1417 else
1419 br = extract_component (bsi, bc, 0, true);
1420 bi = extract_component (bsi, bc, 1, true);
1424 if (gimple_in_ssa_p (cfun))
1426 al = find_lattice_value (ac);
1427 if (al == UNINITIALIZED)
1428 al = VARYING;
1430 if (TREE_CODE_CLASS (code) == tcc_unary)
1431 bl = UNINITIALIZED;
1432 else if (ac == bc)
1433 bl = al;
1434 else
1436 bl = find_lattice_value (bc);
1437 if (bl == UNINITIALIZED)
1438 bl = VARYING;
1441 else
1442 al = bl = VARYING;
1444 switch (code)
1446 case PLUS_EXPR:
1447 case MINUS_EXPR:
1448 expand_complex_addition (bsi, inner_type, ar, ai, br, bi, code, al, bl);
1449 break;
1451 case MULT_EXPR:
1452 expand_complex_multiplication (bsi, inner_type, ar, ai, br, bi, al, bl);
1453 break;
1455 case TRUNC_DIV_EXPR:
1456 case CEIL_DIV_EXPR:
1457 case FLOOR_DIV_EXPR:
1458 case ROUND_DIV_EXPR:
1459 case RDIV_EXPR:
1460 expand_complex_division (bsi, inner_type, ar, ai, br, bi, code, al, bl);
1461 break;
1463 case NEGATE_EXPR:
1464 expand_complex_negation (bsi, inner_type, ar, ai);
1465 break;
1467 case CONJ_EXPR:
1468 expand_complex_conjugate (bsi, inner_type, ar, ai);
1469 break;
1471 case EQ_EXPR:
1472 case NE_EXPR:
1473 expand_complex_comparison (bsi, ar, ai, br, bi, code);
1474 break;
1476 default:
1477 gcc_unreachable ();
1482 /* Entry point for complex operation lowering during optimization. */
1484 static unsigned int
1485 tree_lower_complex (void)
1487 int old_last_basic_block;
1488 block_stmt_iterator bsi;
1489 basic_block bb;
1491 if (!init_dont_simulate_again ())
1492 return 0;
1494 complex_lattice_values = VEC_alloc (complex_lattice_t, heap, num_ssa_names);
1495 VEC_safe_grow (complex_lattice_t, heap,
1496 complex_lattice_values, num_ssa_names);
1497 memset (VEC_address (complex_lattice_t, complex_lattice_values), 0,
1498 num_ssa_names * sizeof(complex_lattice_t));
1500 init_parameter_lattice_values ();
1501 ssa_propagate (complex_visit_stmt, complex_visit_phi);
1503 complex_variable_components = htab_create (10, int_tree_map_hash,
1504 int_tree_map_eq, free);
1506 complex_ssa_name_components = VEC_alloc (tree, heap, 2*num_ssa_names);
1507 VEC_safe_grow (tree, heap, complex_ssa_name_components, 2*num_ssa_names);
1508 memset (VEC_address (tree, complex_ssa_name_components), 0,
1509 2 * num_ssa_names * sizeof(tree));
1511 update_parameter_components ();
1513 /* ??? Ideally we'd traverse the blocks in breadth-first order. */
1514 old_last_basic_block = last_basic_block;
1515 FOR_EACH_BB (bb)
1517 if (bb->index >= old_last_basic_block)
1518 continue;
1519 update_phi_components (bb);
1520 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1521 expand_complex_operations_1 (&bsi);
1524 bsi_commit_edge_inserts ();
1526 htab_delete (complex_variable_components);
1527 VEC_free (tree, heap, complex_ssa_name_components);
1528 VEC_free (complex_lattice_t, heap, complex_lattice_values);
1529 return 0;
1532 struct tree_opt_pass pass_lower_complex =
1534 "cplxlower", /* name */
1535 0, /* gate */
1536 tree_lower_complex, /* execute */
1537 NULL, /* sub */
1538 NULL, /* next */
1539 0, /* static_pass_number */
1540 0, /* tv_id */
1541 PROP_ssa, /* properties_required */
1542 0, /* properties_provided */
1543 0, /* properties_destroyed */
1544 0, /* todo_flags_start */
1545 TODO_dump_func | TODO_ggc_collect
1546 | TODO_update_smt_usage
1547 | TODO_update_ssa
1548 | TODO_verify_stmts, /* todo_flags_finish */
1549 0 /* letter */
1553 /* Entry point for complex operation lowering without optimization. */
1555 static unsigned int
1556 tree_lower_complex_O0 (void)
1558 int old_last_basic_block = last_basic_block;
1559 block_stmt_iterator bsi;
1560 basic_block bb;
1562 FOR_EACH_BB (bb)
1564 if (bb->index >= old_last_basic_block)
1565 continue;
1566 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1567 expand_complex_operations_1 (&bsi);
1569 return 0;
1572 static bool
1573 gate_no_optimization (void)
1575 /* With errors, normal optimization passes are not run. If we don't
1576 lower complex operations at all, rtl expansion will abort. */
1577 return optimize == 0 || sorrycount || errorcount;
1580 struct tree_opt_pass pass_lower_complex_O0 =
1582 "cplxlower0", /* name */
1583 gate_no_optimization, /* gate */
1584 tree_lower_complex_O0, /* execute */
1585 NULL, /* sub */
1586 NULL, /* next */
1587 0, /* static_pass_number */
1588 0, /* tv_id */
1589 PROP_cfg, /* properties_required */
1590 0, /* properties_provided */
1591 0, /* properties_destroyed */
1592 0, /* todo_flags_start */
1593 TODO_dump_func | TODO_ggc_collect
1594 | TODO_verify_stmts, /* todo_flags_finish */
1595 0 /* letter */