Check in tree-dce enh to trunk
[official-gcc.git] / gcc / tree-complex.c
blob8d680dfa6bf02205d55714459a524f7972780370
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, ssa_name;
166 for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = TREE_CHAIN (parm))
167 if (is_complex_reg (parm)
168 && var_ann (parm) != NULL
169 && (ssa_name = gimple_default_def (cfun, parm)) != NULL_TREE)
170 VEC_replace (complex_lattice_t, complex_lattice_values,
171 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 case REALPART_EXPR:
249 case IMAGPART_EXPR:
250 /* The total store transformation performed during
251 gimplification creates such uninitialized loads
252 and we need to lower the statement to be able
253 to fix things up. */
254 if (TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME
255 && ssa_undefined_value_p (TREE_OPERAND (rhs, 0)))
256 saw_a_complex_op = true;
257 break;
259 default:
260 break;
263 DONT_SIMULATE_AGAIN (orig_stmt) = dsa;
267 return saw_a_complex_op;
271 /* Evaluate statement STMT against the complex lattice defined above. */
273 static enum ssa_prop_result
274 complex_visit_stmt (tree stmt, edge *taken_edge_p ATTRIBUTE_UNUSED,
275 tree *result_p)
277 complex_lattice_t new_l, old_l, op1_l, op2_l;
278 unsigned int ver;
279 tree lhs, rhs;
281 if (TREE_CODE (stmt) != GIMPLE_MODIFY_STMT)
282 return SSA_PROP_VARYING;
284 lhs = GIMPLE_STMT_OPERAND (stmt, 0);
285 rhs = GIMPLE_STMT_OPERAND (stmt, 1);
287 /* These conditions should be satisfied due to the initial filter
288 set up in init_dont_simulate_again. */
289 gcc_assert (TREE_CODE (lhs) == SSA_NAME);
290 gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE);
292 *result_p = lhs;
293 ver = SSA_NAME_VERSION (lhs);
294 old_l = VEC_index (complex_lattice_t, complex_lattice_values, ver);
296 switch (TREE_CODE (rhs))
298 case SSA_NAME:
299 case COMPLEX_EXPR:
300 case COMPLEX_CST:
301 new_l = find_lattice_value (rhs);
302 break;
304 case PLUS_EXPR:
305 case MINUS_EXPR:
306 op1_l = find_lattice_value (TREE_OPERAND (rhs, 0));
307 op2_l = find_lattice_value (TREE_OPERAND (rhs, 1));
309 /* We've set up the lattice values such that IOR neatly
310 models addition. */
311 new_l = op1_l | op2_l;
312 break;
314 case MULT_EXPR:
315 case RDIV_EXPR:
316 case TRUNC_DIV_EXPR:
317 case CEIL_DIV_EXPR:
318 case FLOOR_DIV_EXPR:
319 case ROUND_DIV_EXPR:
320 op1_l = find_lattice_value (TREE_OPERAND (rhs, 0));
321 op2_l = find_lattice_value (TREE_OPERAND (rhs, 1));
323 /* Obviously, if either varies, so does the result. */
324 if (op1_l == VARYING || op2_l == VARYING)
325 new_l = VARYING;
326 /* Don't prematurely promote variables if we've not yet seen
327 their inputs. */
328 else if (op1_l == UNINITIALIZED)
329 new_l = op2_l;
330 else if (op2_l == UNINITIALIZED)
331 new_l = op1_l;
332 else
334 /* At this point both numbers have only one component. If the
335 numbers are of opposite kind, the result is imaginary,
336 otherwise the result is real. The add/subtract translates
337 the real/imag from/to 0/1; the ^ performs the comparison. */
338 new_l = ((op1_l - ONLY_REAL) ^ (op2_l - ONLY_REAL)) + ONLY_REAL;
340 /* Don't allow the lattice value to flip-flop indefinitely. */
341 new_l |= old_l;
343 break;
345 case NEGATE_EXPR:
346 case CONJ_EXPR:
347 new_l = find_lattice_value (TREE_OPERAND (rhs, 0));
348 break;
350 default:
351 new_l = VARYING;
352 break;
355 /* If nothing changed this round, let the propagator know. */
356 if (new_l == old_l)
357 return SSA_PROP_NOT_INTERESTING;
359 VEC_replace (complex_lattice_t, complex_lattice_values, ver, new_l);
360 return new_l == VARYING ? SSA_PROP_VARYING : SSA_PROP_INTERESTING;
363 /* Evaluate a PHI node against the complex lattice defined above. */
365 static enum ssa_prop_result
366 complex_visit_phi (tree phi)
368 complex_lattice_t new_l, old_l;
369 unsigned int ver;
370 tree lhs;
371 int i;
373 lhs = PHI_RESULT (phi);
375 /* This condition should be satisfied due to the initial filter
376 set up in init_dont_simulate_again. */
377 gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE);
379 /* We've set up the lattice values such that IOR neatly models PHI meet. */
380 new_l = UNINITIALIZED;
381 for (i = PHI_NUM_ARGS (phi) - 1; i >= 0; --i)
382 new_l |= find_lattice_value (PHI_ARG_DEF (phi, i));
384 ver = SSA_NAME_VERSION (lhs);
385 old_l = VEC_index (complex_lattice_t, complex_lattice_values, ver);
387 if (new_l == old_l)
388 return SSA_PROP_NOT_INTERESTING;
390 VEC_replace (complex_lattice_t, complex_lattice_values, ver, new_l);
391 return new_l == VARYING ? SSA_PROP_VARYING : SSA_PROP_INTERESTING;
394 /* Create one backing variable for a complex component of ORIG. */
396 static tree
397 create_one_component_var (tree type, tree orig, const char *prefix,
398 const char *suffix, enum tree_code code)
400 tree r = create_tmp_var (type, prefix);
401 add_referenced_var (r);
403 DECL_SOURCE_LOCATION (r) = DECL_SOURCE_LOCATION (orig);
404 DECL_ARTIFICIAL (r) = 1;
406 if (DECL_NAME (orig) && !DECL_IGNORED_P (orig))
408 const char *name = IDENTIFIER_POINTER (DECL_NAME (orig));
409 tree inner_type;
411 DECL_NAME (r) = get_identifier (ACONCAT ((name, suffix, NULL)));
413 inner_type = TREE_TYPE (TREE_TYPE (orig));
414 SET_DECL_DEBUG_EXPR (r, build1 (code, type, orig));
415 DECL_DEBUG_EXPR_IS_FROM (r) = 1;
416 DECL_IGNORED_P (r) = 0;
417 TREE_NO_WARNING (r) = TREE_NO_WARNING (orig);
419 else
421 DECL_IGNORED_P (r) = 1;
422 TREE_NO_WARNING (r) = 1;
425 return r;
428 /* Retrieve a value for a complex component of VAR. */
430 static tree
431 get_component_var (tree var, bool imag_p)
433 size_t decl_index = DECL_UID (var) * 2 + imag_p;
434 tree ret = cvc_lookup (decl_index);
436 if (ret == NULL)
438 ret = create_one_component_var (TREE_TYPE (TREE_TYPE (var)), var,
439 imag_p ? "CI" : "CR",
440 imag_p ? "$imag" : "$real",
441 imag_p ? IMAGPART_EXPR : REALPART_EXPR);
442 cvc_insert (decl_index, ret);
445 return ret;
448 /* Retrieve a value for a complex component of SSA_NAME. */
450 static tree
451 get_component_ssa_name (tree ssa_name, bool imag_p)
453 complex_lattice_t lattice = find_lattice_value (ssa_name);
454 size_t ssa_name_index;
455 tree ret;
457 if (lattice == (imag_p ? ONLY_REAL : ONLY_IMAG))
459 tree inner_type = TREE_TYPE (TREE_TYPE (ssa_name));
460 if (SCALAR_FLOAT_TYPE_P (inner_type))
461 return build_real (inner_type, dconst0);
462 else
463 return build_int_cst (inner_type, 0);
466 ssa_name_index = SSA_NAME_VERSION (ssa_name) * 2 + imag_p;
467 ret = VEC_index (tree, complex_ssa_name_components, ssa_name_index);
468 if (ret == NULL)
470 ret = get_component_var (SSA_NAME_VAR (ssa_name), imag_p);
471 ret = make_ssa_name (ret, NULL);
473 /* Copy some properties from the original. In particular, whether it
474 is used in an abnormal phi, and whether it's uninitialized. */
475 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ret)
476 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name);
477 if (TREE_CODE (SSA_NAME_VAR (ssa_name)) == VAR_DECL
478 && IS_EMPTY_STMT (SSA_NAME_DEF_STMT (ssa_name)))
480 SSA_NAME_DEF_STMT (ret) = SSA_NAME_DEF_STMT (ssa_name);
481 set_default_def (SSA_NAME_VAR (ret), ret);
484 VEC_replace (tree, complex_ssa_name_components, ssa_name_index, ret);
487 return ret;
490 /* Set a value for a complex component of SSA_NAME, return a STMT_LIST of
491 stuff that needs doing. */
493 static tree
494 set_component_ssa_name (tree ssa_name, bool imag_p, tree value)
496 complex_lattice_t lattice = find_lattice_value (ssa_name);
497 size_t ssa_name_index;
498 tree comp, list, last;
500 /* We know the value must be zero, else there's a bug in our lattice
501 analysis. But the value may well be a variable known to contain
502 zero. We should be safe ignoring it. */
503 if (lattice == (imag_p ? ONLY_REAL : ONLY_IMAG))
504 return NULL;
506 /* If we've already assigned an SSA_NAME to this component, then this
507 means that our walk of the basic blocks found a use before the set.
508 This is fine. Now we should create an initialization for the value
509 we created earlier. */
510 ssa_name_index = SSA_NAME_VERSION (ssa_name) * 2 + imag_p;
511 comp = VEC_index (tree, complex_ssa_name_components, ssa_name_index);
512 if (comp)
515 /* If we've nothing assigned, and the value we're given is already stable,
516 then install that as the value for this SSA_NAME. This preemptively
517 copy-propagates the value, which avoids unnecessary memory allocation. */
518 else if (is_gimple_min_invariant (value))
520 VEC_replace (tree, complex_ssa_name_components, ssa_name_index, value);
521 return NULL;
523 else if (TREE_CODE (value) == SSA_NAME
524 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name))
526 /* Replace an anonymous base value with the variable from cvc_lookup.
527 This should result in better debug info. */
528 if (DECL_IGNORED_P (SSA_NAME_VAR (value))
529 && !DECL_IGNORED_P (SSA_NAME_VAR (ssa_name)))
531 comp = get_component_var (SSA_NAME_VAR (ssa_name), imag_p);
532 replace_ssa_name_symbol (value, comp);
535 VEC_replace (tree, complex_ssa_name_components, ssa_name_index, value);
536 return NULL;
539 /* Finally, we need to stabilize the result by installing the value into
540 a new ssa name. */
541 else
542 comp = get_component_ssa_name (ssa_name, imag_p);
544 /* Do all the work to assign VALUE to COMP. */
545 value = force_gimple_operand (value, &list, false, NULL);
546 last = build_gimple_modify_stmt (comp, value);
547 append_to_statement_list (last, &list);
549 gcc_assert (SSA_NAME_DEF_STMT (comp) == NULL);
550 SSA_NAME_DEF_STMT (comp) = last;
552 return list;
555 /* Extract the real or imaginary part of a complex variable or constant.
556 Make sure that it's a proper gimple_val and gimplify it if not.
557 Emit any new code before BSI. */
559 static tree
560 extract_component (block_stmt_iterator *bsi, tree t, bool imagpart_p,
561 bool gimple_p)
563 switch (TREE_CODE (t))
565 case COMPLEX_CST:
566 return imagpart_p ? TREE_IMAGPART (t) : TREE_REALPART (t);
568 case COMPLEX_EXPR:
569 return TREE_OPERAND (t, imagpart_p);
571 case VAR_DECL:
572 case RESULT_DECL:
573 case PARM_DECL:
574 case INDIRECT_REF:
575 case COMPONENT_REF:
576 case ARRAY_REF:
578 tree inner_type = TREE_TYPE (TREE_TYPE (t));
580 t = build1 ((imagpart_p ? IMAGPART_EXPR : REALPART_EXPR),
581 inner_type, unshare_expr (t));
583 if (gimple_p)
584 t = gimplify_val (bsi, inner_type, t);
586 return t;
589 case SSA_NAME:
590 return get_component_ssa_name (t, imagpart_p);
592 default:
593 gcc_unreachable ();
597 /* Update the complex components of the ssa name on the lhs of STMT. */
599 static void
600 update_complex_components (block_stmt_iterator *bsi, tree stmt, tree r, tree i)
602 tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
603 tree list;
605 list = set_component_ssa_name (lhs, false, r);
606 if (list)
607 bsi_insert_after (bsi, list, BSI_CONTINUE_LINKING);
609 list = set_component_ssa_name (lhs, true, i);
610 if (list)
611 bsi_insert_after (bsi, list, BSI_CONTINUE_LINKING);
614 static void
615 update_complex_components_on_edge (edge e, tree lhs, tree r, tree i)
617 tree list;
619 list = set_component_ssa_name (lhs, false, r);
620 if (list)
621 bsi_insert_on_edge (e, list);
623 list = set_component_ssa_name (lhs, true, i);
624 if (list)
625 bsi_insert_on_edge (e, list);
628 /* Update an assignment to a complex variable in place. */
630 static void
631 update_complex_assignment (block_stmt_iterator *bsi, tree r, tree i)
633 tree stmt, mod;
634 tree type;
636 mod = stmt = bsi_stmt (*bsi);
637 if (TREE_CODE (stmt) == RETURN_EXPR)
638 mod = TREE_OPERAND (mod, 0);
639 else if (gimple_in_ssa_p (cfun))
640 update_complex_components (bsi, stmt, r, i);
642 type = TREE_TYPE (GIMPLE_STMT_OPERAND (mod, 1));
643 GIMPLE_STMT_OPERAND (mod, 1) = build2 (COMPLEX_EXPR, type, r, i);
644 update_stmt (stmt);
647 /* Generate code at the entry point of the function to initialize the
648 component variables for a complex parameter. */
650 static void
651 update_parameter_components (void)
653 edge entry_edge = single_succ_edge (ENTRY_BLOCK_PTR);
654 tree parm;
656 for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = TREE_CHAIN (parm))
658 tree type = TREE_TYPE (parm);
659 tree ssa_name, r, i;
661 if (TREE_CODE (type) != COMPLEX_TYPE || !is_gimple_reg (parm))
662 continue;
664 type = TREE_TYPE (type);
665 ssa_name = gimple_default_def (cfun, parm);
666 if (!ssa_name)
667 continue;
669 r = build1 (REALPART_EXPR, type, ssa_name);
670 i = build1 (IMAGPART_EXPR, type, ssa_name);
671 update_complex_components_on_edge (entry_edge, ssa_name, r, i);
675 /* Generate code to set the component variables of a complex variable
676 to match the PHI statements in block BB. */
678 static void
679 update_phi_components (basic_block bb)
681 tree phi;
683 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
684 if (is_complex_reg (PHI_RESULT (phi)))
686 tree lr, li, pr = NULL, pi = NULL;
687 unsigned int i, n;
689 lr = get_component_ssa_name (PHI_RESULT (phi), false);
690 if (TREE_CODE (lr) == SSA_NAME)
692 pr = create_phi_node (lr, bb);
693 SSA_NAME_DEF_STMT (lr) = pr;
696 li = get_component_ssa_name (PHI_RESULT (phi), true);
697 if (TREE_CODE (li) == SSA_NAME)
699 pi = create_phi_node (li, bb);
700 SSA_NAME_DEF_STMT (li) = pi;
703 for (i = 0, n = PHI_NUM_ARGS (phi); i < n; ++i)
705 tree comp, arg = PHI_ARG_DEF (phi, i);
706 if (pr)
708 comp = extract_component (NULL, arg, false, false);
709 SET_PHI_ARG_DEF (pr, i, comp);
711 if (pi)
713 comp = extract_component (NULL, arg, true, false);
714 SET_PHI_ARG_DEF (pi, i, comp);
720 /* Mark each virtual op in STMT for ssa update. */
722 static void
723 update_all_vops (tree stmt)
725 ssa_op_iter iter;
726 tree sym;
728 FOR_EACH_SSA_TREE_OPERAND (sym, stmt, iter, SSA_OP_ALL_VIRTUALS)
730 if (TREE_CODE (sym) == SSA_NAME)
731 sym = SSA_NAME_VAR (sym);
732 mark_sym_for_renaming (sym);
736 /* Expand a complex move to scalars. */
738 static void
739 expand_complex_move (block_stmt_iterator *bsi, tree stmt, tree type,
740 tree lhs, tree rhs)
742 tree inner_type = TREE_TYPE (type);
743 tree r, i;
745 if (TREE_CODE (lhs) == SSA_NAME)
747 if (is_ctrl_altering_stmt (bsi_stmt (*bsi)))
749 edge_iterator ei;
750 edge e;
752 /* The value is not assigned on the exception edges, so we need not
753 concern ourselves there. We do need to update on the fallthru
754 edge. Find it. */
755 FOR_EACH_EDGE (e, ei, bsi->bb->succs)
756 if (e->flags & EDGE_FALLTHRU)
757 goto found_fallthru;
758 gcc_unreachable ();
759 found_fallthru:
761 r = build1 (REALPART_EXPR, inner_type, lhs);
762 i = build1 (IMAGPART_EXPR, inner_type, lhs);
763 update_complex_components_on_edge (e, lhs, r, i);
765 else if (TREE_CODE (rhs) == CALL_EXPR || TREE_SIDE_EFFECTS (rhs)
766 || TREE_CODE (rhs) == PAREN_EXPR)
768 r = build1 (REALPART_EXPR, inner_type, lhs);
769 i = build1 (IMAGPART_EXPR, inner_type, lhs);
770 update_complex_components (bsi, stmt, r, i);
772 else
774 update_all_vops (bsi_stmt (*bsi));
775 r = extract_component (bsi, rhs, 0, true);
776 i = extract_component (bsi, rhs, 1, true);
777 update_complex_assignment (bsi, r, i);
780 else if (TREE_CODE (rhs) == SSA_NAME && !TREE_SIDE_EFFECTS (lhs))
782 tree x;
784 r = extract_component (bsi, rhs, 0, false);
785 i = extract_component (bsi, rhs, 1, false);
787 x = build1 (REALPART_EXPR, inner_type, unshare_expr (lhs));
788 x = build_gimple_modify_stmt (x, r);
789 bsi_insert_before (bsi, x, BSI_SAME_STMT);
791 if (stmt == bsi_stmt (*bsi))
793 x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
794 GIMPLE_STMT_OPERAND (stmt, 0) = x;
795 GIMPLE_STMT_OPERAND (stmt, 1) = i;
797 else
799 x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
800 x = build_gimple_modify_stmt (x, i);
801 bsi_insert_before (bsi, x, BSI_SAME_STMT);
803 stmt = bsi_stmt (*bsi);
804 gcc_assert (TREE_CODE (stmt) == RETURN_EXPR);
805 GIMPLE_STMT_OPERAND (stmt, 0) = lhs;
808 update_all_vops (stmt);
809 update_stmt (stmt);
813 /* Expand complex addition to scalars:
814 a + b = (ar + br) + i(ai + bi)
815 a - b = (ar - br) + i(ai + bi)
818 static void
819 expand_complex_addition (block_stmt_iterator *bsi, tree inner_type,
820 tree ar, tree ai, tree br, tree bi,
821 enum tree_code code,
822 complex_lattice_t al, complex_lattice_t bl)
824 tree rr, ri;
826 switch (PAIR (al, bl))
828 case PAIR (ONLY_REAL, ONLY_REAL):
829 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
830 ri = ai;
831 break;
833 case PAIR (ONLY_REAL, ONLY_IMAG):
834 rr = ar;
835 if (code == MINUS_EXPR)
836 ri = gimplify_build2 (bsi, MINUS_EXPR, inner_type, ai, bi);
837 else
838 ri = bi;
839 break;
841 case PAIR (ONLY_IMAG, ONLY_REAL):
842 if (code == MINUS_EXPR)
843 rr = gimplify_build2 (bsi, MINUS_EXPR, inner_type, ar, br);
844 else
845 rr = br;
846 ri = ai;
847 break;
849 case PAIR (ONLY_IMAG, ONLY_IMAG):
850 rr = ar;
851 ri = gimplify_build2 (bsi, code, inner_type, ai, bi);
852 break;
854 case PAIR (VARYING, ONLY_REAL):
855 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
856 ri = ai;
857 break;
859 case PAIR (VARYING, ONLY_IMAG):
860 rr = ar;
861 ri = gimplify_build2 (bsi, code, inner_type, ai, bi);
862 break;
864 case PAIR (ONLY_REAL, VARYING):
865 if (code == MINUS_EXPR)
866 goto general;
867 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
868 ri = bi;
869 break;
871 case PAIR (ONLY_IMAG, VARYING):
872 if (code == MINUS_EXPR)
873 goto general;
874 rr = br;
875 ri = gimplify_build2 (bsi, code, inner_type, ai, bi);
876 break;
878 case PAIR (VARYING, VARYING):
879 general:
880 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
881 ri = gimplify_build2 (bsi, code, inner_type, ai, bi);
882 break;
884 default:
885 gcc_unreachable ();
888 update_complex_assignment (bsi, rr, ri);
891 /* Expand a complex multiplication or division to a libcall to the c99
892 compliant routines. */
894 static void
895 expand_complex_libcall (block_stmt_iterator *bsi, tree ar, tree ai,
896 tree br, tree bi, enum tree_code code)
898 enum machine_mode mode;
899 enum built_in_function bcode;
900 tree fn, stmt, type;
902 stmt = bsi_stmt (*bsi);
903 type = TREE_TYPE (GIMPLE_STMT_OPERAND (stmt, 1));
905 mode = TYPE_MODE (type);
906 gcc_assert (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT);
907 if (code == MULT_EXPR)
908 bcode = BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT;
909 else if (code == RDIV_EXPR)
910 bcode = BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT;
911 else
912 gcc_unreachable ();
913 fn = built_in_decls[bcode];
915 GIMPLE_STMT_OPERAND (stmt, 1) = build_call_expr (fn, 4, ar, ai, br, bi);
916 update_stmt (stmt);
918 if (gimple_in_ssa_p (cfun))
920 tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
921 type = TREE_TYPE (type);
922 update_complex_components (bsi, stmt,
923 build1 (REALPART_EXPR, type, lhs),
924 build1 (IMAGPART_EXPR, type, lhs));
928 /* Expand complex multiplication to scalars:
929 a * b = (ar*br - ai*bi) + i(ar*bi + br*ai)
932 static void
933 expand_complex_multiplication (block_stmt_iterator *bsi, tree inner_type,
934 tree ar, tree ai, tree br, tree bi,
935 complex_lattice_t al, complex_lattice_t bl)
937 tree rr, ri;
939 if (al < bl)
941 complex_lattice_t tl;
942 rr = ar, ar = br, br = rr;
943 ri = ai, ai = bi, bi = ri;
944 tl = al, al = bl, bl = tl;
947 switch (PAIR (al, bl))
949 case PAIR (ONLY_REAL, ONLY_REAL):
950 rr = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, br);
951 ri = ai;
952 break;
954 case PAIR (ONLY_IMAG, ONLY_REAL):
955 rr = ar;
956 if (TREE_CODE (ai) == REAL_CST
957 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (ai), dconst1))
958 ri = br;
959 else
960 ri = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, br);
961 break;
963 case PAIR (ONLY_IMAG, 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 = ar;
967 break;
969 case PAIR (VARYING, ONLY_REAL):
970 rr = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, br);
971 ri = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, br);
972 break;
974 case PAIR (VARYING, ONLY_IMAG):
975 rr = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, bi);
976 rr = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, rr);
977 ri = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, bi);
978 break;
980 case PAIR (VARYING, VARYING):
981 if (flag_complex_method == 2 && SCALAR_FLOAT_TYPE_P (inner_type))
983 expand_complex_libcall (bsi, ar, ai, br, bi, MULT_EXPR);
984 return;
986 else
988 tree t1, t2, t3, t4;
990 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, br);
991 t2 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, bi);
992 t3 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, bi);
994 /* Avoid expanding redundant multiplication for the common
995 case of squaring a complex number. */
996 if (ar == br && ai == bi)
997 t4 = t3;
998 else
999 t4 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, br);
1001 rr = gimplify_build2 (bsi, MINUS_EXPR, inner_type, t1, t2);
1002 ri = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t3, t4);
1004 break;
1006 default:
1007 gcc_unreachable ();
1010 update_complex_assignment (bsi, rr, ri);
1013 /* Expand complex division to scalars, straightforward algorithm.
1014 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1015 t = br*br + bi*bi
1018 static void
1019 expand_complex_div_straight (block_stmt_iterator *bsi, tree inner_type,
1020 tree ar, tree ai, tree br, tree bi,
1021 enum tree_code code)
1023 tree rr, ri, div, t1, t2, t3;
1025 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, br, br);
1026 t2 = gimplify_build2 (bsi, MULT_EXPR, inner_type, bi, bi);
1027 div = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, t2);
1029 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, br);
1030 t2 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, bi);
1031 t3 = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, t2);
1032 rr = gimplify_build2 (bsi, code, inner_type, t3, div);
1034 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, br);
1035 t2 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, bi);
1036 t3 = gimplify_build2 (bsi, MINUS_EXPR, inner_type, t1, t2);
1037 ri = gimplify_build2 (bsi, code, inner_type, t3, div);
1039 update_complex_assignment (bsi, rr, ri);
1042 /* Expand complex division to scalars, modified algorithm to minimize
1043 overflow with wide input ranges. */
1045 static void
1046 expand_complex_div_wide (block_stmt_iterator *bsi, tree inner_type,
1047 tree ar, tree ai, tree br, tree bi,
1048 enum tree_code code)
1050 tree rr, ri, ratio, div, t1, t2, tr, ti, compare;
1051 basic_block bb_cond, bb_true, bb_false, bb_join;
1053 /* Examine |br| < |bi|, and branch. */
1054 t1 = gimplify_build1 (bsi, ABS_EXPR, inner_type, br);
1055 t2 = gimplify_build1 (bsi, ABS_EXPR, inner_type, bi);
1056 compare = fold_build2 (LT_EXPR, boolean_type_node, t1, t2);
1057 STRIP_NOPS (compare);
1059 bb_cond = bb_true = bb_false = bb_join = NULL;
1060 rr = ri = tr = ti = NULL;
1061 if (!TREE_CONSTANT (compare))
1063 edge e;
1064 tree cond, tmp;
1066 tmp = create_tmp_var (boolean_type_node, NULL);
1067 cond = build_gimple_modify_stmt (tmp, compare);
1068 if (gimple_in_ssa_p (cfun))
1069 tmp = make_ssa_name (tmp, cond);
1070 GIMPLE_STMT_OPERAND (cond, 0) = tmp;
1071 bsi_insert_before (bsi, cond, BSI_SAME_STMT);
1073 cond = build3 (COND_EXPR, void_type_node, tmp, NULL_TREE, NULL_TREE);
1074 bsi_insert_before (bsi, cond, BSI_SAME_STMT);
1076 /* Split the original block, and create the TRUE and FALSE blocks. */
1077 e = split_block (bsi->bb, cond);
1078 bb_cond = e->src;
1079 bb_join = e->dest;
1080 bb_true = create_empty_bb (bb_cond);
1081 bb_false = create_empty_bb (bb_true);
1083 /* Wire the blocks together. */
1084 e->flags = EDGE_TRUE_VALUE;
1085 redirect_edge_succ (e, bb_true);
1086 make_edge (bb_cond, bb_false, EDGE_FALSE_VALUE);
1087 make_edge (bb_true, bb_join, EDGE_FALLTHRU);
1088 make_edge (bb_false, bb_join, EDGE_FALLTHRU);
1090 /* Update dominance info. Note that bb_join's data was
1091 updated by split_block. */
1092 if (dom_info_available_p (CDI_DOMINATORS))
1094 set_immediate_dominator (CDI_DOMINATORS, bb_true, bb_cond);
1095 set_immediate_dominator (CDI_DOMINATORS, bb_false, bb_cond);
1098 rr = make_rename_temp (inner_type, NULL);
1099 ri = make_rename_temp (inner_type, NULL);
1102 /* In the TRUE branch, we compute
1103 ratio = br/bi;
1104 div = (br * ratio) + bi;
1105 tr = (ar * ratio) + ai;
1106 ti = (ai * ratio) - ar;
1107 tr = tr / div;
1108 ti = ti / div; */
1109 if (bb_true || integer_nonzerop (compare))
1111 if (bb_true)
1113 *bsi = bsi_last (bb_true);
1114 bsi_insert_after (bsi, build_empty_stmt (), BSI_NEW_STMT);
1117 ratio = gimplify_build2 (bsi, code, inner_type, br, bi);
1119 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, br, ratio);
1120 div = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, bi);
1122 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, ratio);
1123 tr = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, ai);
1125 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, ratio);
1126 ti = gimplify_build2 (bsi, MINUS_EXPR, inner_type, t1, ar);
1128 tr = gimplify_build2 (bsi, code, inner_type, tr, div);
1129 ti = gimplify_build2 (bsi, code, inner_type, ti, div);
1131 if (bb_true)
1133 t1 = build_gimple_modify_stmt (rr, tr);
1134 bsi_insert_before (bsi, t1, BSI_SAME_STMT);
1135 t1 = build_gimple_modify_stmt (ri, ti);
1136 bsi_insert_before (bsi, t1, BSI_SAME_STMT);
1137 bsi_remove (bsi, true);
1141 /* In the FALSE branch, we compute
1142 ratio = d/c;
1143 divisor = (d * ratio) + c;
1144 tr = (b * ratio) + a;
1145 ti = b - (a * ratio);
1146 tr = tr / div;
1147 ti = ti / div; */
1148 if (bb_false || integer_zerop (compare))
1150 if (bb_false)
1152 *bsi = bsi_last (bb_false);
1153 bsi_insert_after (bsi, build_empty_stmt (), BSI_NEW_STMT);
1156 ratio = gimplify_build2 (bsi, code, inner_type, bi, br);
1158 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, bi, ratio);
1159 div = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, br);
1161 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, ratio);
1162 tr = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, ar);
1164 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, ratio);
1165 ti = gimplify_build2 (bsi, MINUS_EXPR, inner_type, ai, t1);
1167 tr = gimplify_build2 (bsi, code, inner_type, tr, div);
1168 ti = gimplify_build2 (bsi, code, inner_type, ti, div);
1170 if (bb_false)
1172 t1 = build_gimple_modify_stmt (rr, tr);
1173 bsi_insert_before (bsi, t1, BSI_SAME_STMT);
1174 t1 = build_gimple_modify_stmt (ri, ti);
1175 bsi_insert_before (bsi, t1, BSI_SAME_STMT);
1176 bsi_remove (bsi, true);
1180 if (bb_join)
1181 *bsi = bsi_start (bb_join);
1182 else
1183 rr = tr, ri = ti;
1185 update_complex_assignment (bsi, rr, ri);
1188 /* Expand complex division to scalars. */
1190 static void
1191 expand_complex_division (block_stmt_iterator *bsi, tree inner_type,
1192 tree ar, tree ai, tree br, tree bi,
1193 enum tree_code code,
1194 complex_lattice_t al, complex_lattice_t bl)
1196 tree rr, ri;
1198 switch (PAIR (al, bl))
1200 case PAIR (ONLY_REAL, ONLY_REAL):
1201 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
1202 ri = ai;
1203 break;
1205 case PAIR (ONLY_REAL, ONLY_IMAG):
1206 rr = ai;
1207 ri = gimplify_build2 (bsi, code, inner_type, ar, bi);
1208 ri = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, ri);
1209 break;
1211 case PAIR (ONLY_IMAG, ONLY_REAL):
1212 rr = ar;
1213 ri = gimplify_build2 (bsi, code, inner_type, ai, br);
1214 break;
1216 case PAIR (ONLY_IMAG, ONLY_IMAG):
1217 rr = gimplify_build2 (bsi, code, inner_type, ai, bi);
1218 ri = ar;
1219 break;
1221 case PAIR (VARYING, ONLY_REAL):
1222 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
1223 ri = gimplify_build2 (bsi, code, inner_type, ai, br);
1224 break;
1226 case PAIR (VARYING, ONLY_IMAG):
1227 rr = gimplify_build2 (bsi, code, inner_type, ai, bi);
1228 ri = gimplify_build2 (bsi, code, inner_type, ar, bi);
1229 ri = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, ri);
1231 case PAIR (ONLY_REAL, VARYING):
1232 case PAIR (ONLY_IMAG, VARYING):
1233 case PAIR (VARYING, VARYING):
1234 switch (flag_complex_method)
1236 case 0:
1237 /* straightforward implementation of complex divide acceptable. */
1238 expand_complex_div_straight (bsi, inner_type, ar, ai, br, bi, code);
1239 break;
1241 case 2:
1242 if (SCALAR_FLOAT_TYPE_P (inner_type))
1244 expand_complex_libcall (bsi, ar, ai, br, bi, code);
1245 break;
1247 /* FALLTHRU */
1249 case 1:
1250 /* wide ranges of inputs must work for complex divide. */
1251 expand_complex_div_wide (bsi, inner_type, ar, ai, br, bi, code);
1252 break;
1254 default:
1255 gcc_unreachable ();
1257 return;
1259 default:
1260 gcc_unreachable ();
1263 update_complex_assignment (bsi, rr, ri);
1266 /* Expand complex negation to scalars:
1267 -a = (-ar) + i(-ai)
1270 static void
1271 expand_complex_negation (block_stmt_iterator *bsi, tree inner_type,
1272 tree ar, tree ai)
1274 tree rr, ri;
1276 rr = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, ar);
1277 ri = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, ai);
1279 update_complex_assignment (bsi, rr, ri);
1282 /* Expand complex conjugate to scalars:
1283 ~a = (ar) + i(-ai)
1286 static void
1287 expand_complex_conjugate (block_stmt_iterator *bsi, tree inner_type,
1288 tree ar, tree ai)
1290 tree ri;
1292 ri = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, ai);
1294 update_complex_assignment (bsi, ar, ri);
1297 /* Expand complex comparison (EQ or NE only). */
1299 static void
1300 expand_complex_comparison (block_stmt_iterator *bsi, tree ar, tree ai,
1301 tree br, tree bi, enum tree_code code)
1303 tree cr, ci, cc, stmt, expr, type;
1305 cr = gimplify_build2 (bsi, code, boolean_type_node, ar, br);
1306 ci = gimplify_build2 (bsi, code, boolean_type_node, ai, bi);
1307 cc = gimplify_build2 (bsi,
1308 (code == EQ_EXPR ? TRUTH_AND_EXPR : TRUTH_OR_EXPR),
1309 boolean_type_node, cr, ci);
1311 stmt = expr = bsi_stmt (*bsi);
1313 switch (TREE_CODE (stmt))
1315 case RETURN_EXPR:
1316 expr = TREE_OPERAND (stmt, 0);
1317 /* FALLTHRU */
1318 case GIMPLE_MODIFY_STMT:
1319 type = TREE_TYPE (GIMPLE_STMT_OPERAND (expr, 1));
1320 GIMPLE_STMT_OPERAND (expr, 1) = fold_convert (type, cc);
1321 break;
1322 case COND_EXPR:
1323 TREE_OPERAND (stmt, 0) = cc;
1324 break;
1325 default:
1326 gcc_unreachable ();
1329 update_stmt (stmt);
1332 /* Process one statement. If we identify a complex operation, expand it. */
1334 static void
1335 expand_complex_operations_1 (block_stmt_iterator *bsi)
1337 tree stmt = bsi_stmt (*bsi);
1338 tree rhs, type, inner_type;
1339 tree ac, ar, ai, bc, br, bi;
1340 complex_lattice_t al, bl;
1341 enum tree_code code;
1343 switch (TREE_CODE (stmt))
1345 case RETURN_EXPR:
1346 stmt = TREE_OPERAND (stmt, 0);
1347 if (!stmt)
1348 return;
1349 if (TREE_CODE (stmt) != GIMPLE_MODIFY_STMT)
1350 return;
1351 /* FALLTHRU */
1353 case GIMPLE_MODIFY_STMT:
1354 rhs = GIMPLE_STMT_OPERAND (stmt, 1);
1355 break;
1357 case COND_EXPR:
1358 rhs = TREE_OPERAND (stmt, 0);
1359 break;
1361 default:
1362 return;
1365 type = TREE_TYPE (rhs);
1366 code = TREE_CODE (rhs);
1368 /* Initial filter for operations we handle. */
1369 switch (code)
1371 case PLUS_EXPR:
1372 case MINUS_EXPR:
1373 case MULT_EXPR:
1374 case TRUNC_DIV_EXPR:
1375 case CEIL_DIV_EXPR:
1376 case FLOOR_DIV_EXPR:
1377 case ROUND_DIV_EXPR:
1378 case RDIV_EXPR:
1379 case NEGATE_EXPR:
1380 case CONJ_EXPR:
1381 if (TREE_CODE (type) != COMPLEX_TYPE)
1382 return;
1383 inner_type = TREE_TYPE (type);
1384 break;
1386 case EQ_EXPR:
1387 case NE_EXPR:
1388 inner_type = TREE_TYPE (TREE_OPERAND (rhs, 1));
1389 if (TREE_CODE (inner_type) != COMPLEX_TYPE)
1390 return;
1391 break;
1393 default:
1395 tree lhs, rhs;
1397 /* COND_EXPR may also fallthru here, but we do not need to do anything
1398 with it. */
1399 if (TREE_CODE (stmt) != GIMPLE_MODIFY_STMT)
1400 return;
1402 lhs = GIMPLE_STMT_OPERAND (stmt, 0);
1403 rhs = GIMPLE_STMT_OPERAND (stmt, 1);
1405 if (TREE_CODE (type) == COMPLEX_TYPE)
1406 expand_complex_move (bsi, stmt, type, lhs, rhs);
1407 else if ((TREE_CODE (rhs) == REALPART_EXPR
1408 || TREE_CODE (rhs) == IMAGPART_EXPR)
1409 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
1411 GENERIC_TREE_OPERAND (stmt, 1)
1412 = extract_component (bsi, TREE_OPERAND (rhs, 0),
1413 TREE_CODE (rhs) == IMAGPART_EXPR, false);
1414 update_stmt (stmt);
1417 return;
1420 /* Extract the components of the two complex values. Make sure and
1421 handle the common case of the same value used twice specially. */
1422 ac = TREE_OPERAND (rhs, 0);
1423 ar = extract_component (bsi, ac, 0, true);
1424 ai = extract_component (bsi, ac, 1, true);
1426 if (TREE_CODE_CLASS (code) == tcc_unary)
1427 bc = br = bi = NULL;
1428 else
1430 bc = TREE_OPERAND (rhs, 1);
1431 if (ac == bc)
1432 br = ar, bi = ai;
1433 else
1435 br = extract_component (bsi, bc, 0, true);
1436 bi = extract_component (bsi, bc, 1, true);
1440 if (gimple_in_ssa_p (cfun))
1442 al = find_lattice_value (ac);
1443 if (al == UNINITIALIZED)
1444 al = VARYING;
1446 if (TREE_CODE_CLASS (code) == tcc_unary)
1447 bl = UNINITIALIZED;
1448 else if (ac == bc)
1449 bl = al;
1450 else
1452 bl = find_lattice_value (bc);
1453 if (bl == UNINITIALIZED)
1454 bl = VARYING;
1457 else
1458 al = bl = VARYING;
1460 switch (code)
1462 case PLUS_EXPR:
1463 case MINUS_EXPR:
1464 expand_complex_addition (bsi, inner_type, ar, ai, br, bi, code, al, bl);
1465 break;
1467 case MULT_EXPR:
1468 expand_complex_multiplication (bsi, inner_type, ar, ai, br, bi, al, bl);
1469 break;
1471 case TRUNC_DIV_EXPR:
1472 case CEIL_DIV_EXPR:
1473 case FLOOR_DIV_EXPR:
1474 case ROUND_DIV_EXPR:
1475 case RDIV_EXPR:
1476 expand_complex_division (bsi, inner_type, ar, ai, br, bi, code, al, bl);
1477 break;
1479 case NEGATE_EXPR:
1480 expand_complex_negation (bsi, inner_type, ar, ai);
1481 break;
1483 case CONJ_EXPR:
1484 expand_complex_conjugate (bsi, inner_type, ar, ai);
1485 break;
1487 case EQ_EXPR:
1488 case NE_EXPR:
1489 expand_complex_comparison (bsi, ar, ai, br, bi, code);
1490 break;
1492 default:
1493 gcc_unreachable ();
1498 /* Entry point for complex operation lowering during optimization. */
1500 static unsigned int
1501 tree_lower_complex (void)
1503 int old_last_basic_block;
1504 block_stmt_iterator bsi;
1505 basic_block bb;
1507 if (!init_dont_simulate_again ())
1508 return 0;
1510 complex_lattice_values = VEC_alloc (complex_lattice_t, heap, num_ssa_names);
1511 VEC_safe_grow_cleared (complex_lattice_t, heap,
1512 complex_lattice_values, num_ssa_names);
1514 init_parameter_lattice_values ();
1515 ssa_propagate (complex_visit_stmt, complex_visit_phi);
1517 complex_variable_components = htab_create (10, int_tree_map_hash,
1518 int_tree_map_eq, free);
1520 complex_ssa_name_components = VEC_alloc (tree, heap, 2*num_ssa_names);
1521 VEC_safe_grow_cleared (tree, heap, complex_ssa_name_components,
1522 2 * num_ssa_names);
1524 update_parameter_components ();
1526 /* ??? Ideally we'd traverse the blocks in breadth-first order. */
1527 old_last_basic_block = last_basic_block;
1528 FOR_EACH_BB (bb)
1530 if (bb->index >= old_last_basic_block)
1531 continue;
1532 update_phi_components (bb);
1533 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1534 expand_complex_operations_1 (&bsi);
1537 bsi_commit_edge_inserts ();
1539 htab_delete (complex_variable_components);
1540 VEC_free (tree, heap, complex_ssa_name_components);
1541 VEC_free (complex_lattice_t, heap, complex_lattice_values);
1542 return 0;
1545 struct gimple_opt_pass pass_lower_complex =
1548 GIMPLE_PASS,
1549 "cplxlower", /* name */
1550 0, /* gate */
1551 tree_lower_complex, /* execute */
1552 NULL, /* sub */
1553 NULL, /* next */
1554 0, /* static_pass_number */
1555 0, /* tv_id */
1556 PROP_ssa, /* properties_required */
1557 0, /* properties_provided */
1558 0, /* properties_destroyed */
1559 0, /* todo_flags_start */
1560 TODO_dump_func
1561 | TODO_ggc_collect
1562 | TODO_update_ssa
1563 | TODO_verify_stmts /* todo_flags_finish */
1568 /* Entry point for complex operation lowering without optimization. */
1570 static unsigned int
1571 tree_lower_complex_O0 (void)
1573 int old_last_basic_block = last_basic_block;
1574 block_stmt_iterator bsi;
1575 basic_block bb;
1577 FOR_EACH_BB (bb)
1579 if (bb->index >= old_last_basic_block)
1580 continue;
1581 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1582 expand_complex_operations_1 (&bsi);
1584 return 0;
1587 static bool
1588 gate_no_optimization (void)
1590 /* With errors, normal optimization passes are not run. If we don't
1591 lower complex operations at all, rtl expansion will abort. */
1592 return optimize == 0 || sorrycount || errorcount;
1595 struct gimple_opt_pass pass_lower_complex_O0 =
1598 GIMPLE_PASS,
1599 "cplxlower0", /* name */
1600 gate_no_optimization, /* gate */
1601 tree_lower_complex_O0, /* execute */
1602 NULL, /* sub */
1603 NULL, /* next */
1604 0, /* static_pass_number */
1605 0, /* tv_id */
1606 PROP_cfg, /* properties_required */
1607 0, /* properties_provided */
1608 0, /* properties_destroyed */
1609 0, /* todo_flags_start */
1610 TODO_dump_func | TODO_ggc_collect
1611 | TODO_verify_stmts, /* todo_flags_finish */