2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / tree-complex.c
blob3d600d1df5eda36d5f2675bb5c58a3fd919a8804
1 /* Lower complex number operations to scalar operations.
2 Copyright (C) 2004-2015 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 "input.h"
25 #include "alias.h"
26 #include "symtab.h"
27 #include "tree.h"
28 #include "fold-const.h"
29 #include "stor-layout.h"
30 #include "flags.h"
31 #include "predict.h"
32 #include "hard-reg-set.h"
33 #include "function.h"
34 #include "dominance.h"
35 #include "cfg.h"
36 #include "basic-block.h"
37 #include "tree-ssa-alias.h"
38 #include "internal-fn.h"
39 #include "tree-eh.h"
40 #include "gimple-expr.h"
41 #include "is-a.h"
42 #include "gimple.h"
43 #include "gimplify.h"
44 #include "gimple-iterator.h"
45 #include "gimplify-me.h"
46 #include "gimple-ssa.h"
47 #include "tree-cfg.h"
48 #include "tree-phinodes.h"
49 #include "ssa-iterators.h"
50 #include "stringpool.h"
51 #include "tree-ssanames.h"
52 #include "rtl.h"
53 #include "insn-config.h"
54 #include "expmed.h"
55 #include "dojump.h"
56 #include "explow.h"
57 #include "calls.h"
58 #include "emit-rtl.h"
59 #include "varasm.h"
60 #include "stmt.h"
61 #include "expr.h"
62 #include "tree-dfa.h"
63 #include "tree-ssa.h"
64 #include "tree-iterator.h"
65 #include "tree-pass.h"
66 #include "tree-ssa-propagate.h"
67 #include "tree-hasher.h"
68 #include "cfgloop.h"
71 /* For each complex ssa name, a lattice value. We're interested in finding
72 out whether a complex number is degenerate in some way, having only real
73 or only complex parts. */
75 enum
77 UNINITIALIZED = 0,
78 ONLY_REAL = 1,
79 ONLY_IMAG = 2,
80 VARYING = 3
83 /* The type complex_lattice_t holds combinations of the above
84 constants. */
85 typedef int complex_lattice_t;
87 #define PAIR(a, b) ((a) << 2 | (b))
90 static vec<complex_lattice_t> complex_lattice_values;
92 /* For each complex variable, a pair of variables for the components exists in
93 the hashtable. */
94 static int_tree_htab_type *complex_variable_components;
96 /* For each complex SSA_NAME, a pair of ssa names for the components. */
97 static vec<tree> complex_ssa_name_components;
99 /* Lookup UID in the complex_variable_components hashtable and return the
100 associated tree. */
101 static tree
102 cvc_lookup (unsigned int uid)
104 struct int_tree_map in;
105 in.uid = uid;
106 return complex_variable_components->find_with_hash (in, uid).to;
109 /* Insert the pair UID, TO into the complex_variable_components hashtable. */
111 static void
112 cvc_insert (unsigned int uid, tree to)
114 int_tree_map h;
115 int_tree_map *loc;
117 h.uid = uid;
118 loc = complex_variable_components->find_slot_with_hash (h, uid, INSERT);
119 loc->uid = uid;
120 loc->to = to;
123 /* Return true if T is not a zero constant. In the case of real values,
124 we're only interested in +0.0. */
126 static int
127 some_nonzerop (tree t)
129 int zerop = false;
131 /* Operations with real or imaginary part of a complex number zero
132 cannot be treated the same as operations with a real or imaginary
133 operand if we care about the signs of zeros in the result. */
134 if (TREE_CODE (t) == REAL_CST && !flag_signed_zeros)
135 zerop = REAL_VALUES_IDENTICAL (TREE_REAL_CST (t), dconst0);
136 else if (TREE_CODE (t) == FIXED_CST)
137 zerop = fixed_zerop (t);
138 else if (TREE_CODE (t) == INTEGER_CST)
139 zerop = integer_zerop (t);
141 return !zerop;
145 /* Compute a lattice value from the components of a complex type REAL
146 and IMAG. */
148 static complex_lattice_t
149 find_lattice_value_parts (tree real, tree imag)
151 int r, i;
152 complex_lattice_t ret;
154 r = some_nonzerop (real);
155 i = some_nonzerop (imag);
156 ret = r * ONLY_REAL + i * ONLY_IMAG;
158 /* ??? On occasion we could do better than mapping 0+0i to real, but we
159 certainly don't want to leave it UNINITIALIZED, which eventually gets
160 mapped to VARYING. */
161 if (ret == UNINITIALIZED)
162 ret = ONLY_REAL;
164 return ret;
168 /* Compute a lattice value from gimple_val T. */
170 static complex_lattice_t
171 find_lattice_value (tree t)
173 tree real, imag;
175 switch (TREE_CODE (t))
177 case SSA_NAME:
178 return complex_lattice_values[SSA_NAME_VERSION (t)];
180 case COMPLEX_CST:
181 real = TREE_REALPART (t);
182 imag = TREE_IMAGPART (t);
183 break;
185 default:
186 gcc_unreachable ();
189 return find_lattice_value_parts (real, imag);
192 /* Determine if LHS is something for which we're interested in seeing
193 simulation results. */
195 static bool
196 is_complex_reg (tree lhs)
198 return TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE && is_gimple_reg (lhs);
201 /* Mark the incoming parameters to the function as VARYING. */
203 static void
204 init_parameter_lattice_values (void)
206 tree parm, ssa_name;
208 for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = DECL_CHAIN (parm))
209 if (is_complex_reg (parm)
210 && (ssa_name = ssa_default_def (cfun, parm)) != NULL_TREE)
211 complex_lattice_values[SSA_NAME_VERSION (ssa_name)] = VARYING;
214 /* Initialize simulation state for each statement. Return false if we
215 found no statements we want to simulate, and thus there's nothing
216 for the entire pass to do. */
218 static bool
219 init_dont_simulate_again (void)
221 basic_block bb;
222 bool saw_a_complex_op = false;
224 FOR_EACH_BB_FN (bb, cfun)
226 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
227 gsi_next (&gsi))
229 gphi *phi = gsi.phi ();
230 prop_set_simulate_again (phi,
231 is_complex_reg (gimple_phi_result (phi)));
234 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
235 gsi_next (&gsi))
237 gimple stmt;
238 tree op0, op1;
239 bool sim_again_p;
241 stmt = gsi_stmt (gsi);
242 op0 = op1 = NULL_TREE;
244 /* Most control-altering statements must be initially
245 simulated, else we won't cover the entire cfg. */
246 sim_again_p = stmt_ends_bb_p (stmt);
248 switch (gimple_code (stmt))
250 case GIMPLE_CALL:
251 if (gimple_call_lhs (stmt))
252 sim_again_p = is_complex_reg (gimple_call_lhs (stmt));
253 break;
255 case GIMPLE_ASSIGN:
256 sim_again_p = is_complex_reg (gimple_assign_lhs (stmt));
257 if (gimple_assign_rhs_code (stmt) == REALPART_EXPR
258 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
259 op0 = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
260 else
261 op0 = gimple_assign_rhs1 (stmt);
262 if (gimple_num_ops (stmt) > 2)
263 op1 = gimple_assign_rhs2 (stmt);
264 break;
266 case GIMPLE_COND:
267 op0 = gimple_cond_lhs (stmt);
268 op1 = gimple_cond_rhs (stmt);
269 break;
271 default:
272 break;
275 if (op0 || op1)
276 switch (gimple_expr_code (stmt))
278 case EQ_EXPR:
279 case NE_EXPR:
280 case PLUS_EXPR:
281 case MINUS_EXPR:
282 case MULT_EXPR:
283 case TRUNC_DIV_EXPR:
284 case CEIL_DIV_EXPR:
285 case FLOOR_DIV_EXPR:
286 case ROUND_DIV_EXPR:
287 case RDIV_EXPR:
288 if (TREE_CODE (TREE_TYPE (op0)) == COMPLEX_TYPE
289 || TREE_CODE (TREE_TYPE (op1)) == COMPLEX_TYPE)
290 saw_a_complex_op = true;
291 break;
293 case NEGATE_EXPR:
294 case CONJ_EXPR:
295 if (TREE_CODE (TREE_TYPE (op0)) == COMPLEX_TYPE)
296 saw_a_complex_op = true;
297 break;
299 case REALPART_EXPR:
300 case IMAGPART_EXPR:
301 /* The total store transformation performed during
302 gimplification creates such uninitialized loads
303 and we need to lower the statement to be able
304 to fix things up. */
305 if (TREE_CODE (op0) == SSA_NAME
306 && ssa_undefined_value_p (op0))
307 saw_a_complex_op = true;
308 break;
310 default:
311 break;
314 prop_set_simulate_again (stmt, sim_again_p);
318 return saw_a_complex_op;
322 /* Evaluate statement STMT against the complex lattice defined above. */
324 static enum ssa_prop_result
325 complex_visit_stmt (gimple stmt, edge *taken_edge_p ATTRIBUTE_UNUSED,
326 tree *result_p)
328 complex_lattice_t new_l, old_l, op1_l, op2_l;
329 unsigned int ver;
330 tree lhs;
332 lhs = gimple_get_lhs (stmt);
333 /* Skip anything but GIMPLE_ASSIGN and GIMPLE_CALL with a lhs. */
334 if (!lhs)
335 return SSA_PROP_VARYING;
337 /* These conditions should be satisfied due to the initial filter
338 set up in init_dont_simulate_again. */
339 gcc_assert (TREE_CODE (lhs) == SSA_NAME);
340 gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE);
342 *result_p = lhs;
343 ver = SSA_NAME_VERSION (lhs);
344 old_l = complex_lattice_values[ver];
346 switch (gimple_expr_code (stmt))
348 case SSA_NAME:
349 case COMPLEX_CST:
350 new_l = find_lattice_value (gimple_assign_rhs1 (stmt));
351 break;
353 case COMPLEX_EXPR:
354 new_l = find_lattice_value_parts (gimple_assign_rhs1 (stmt),
355 gimple_assign_rhs2 (stmt));
356 break;
358 case PLUS_EXPR:
359 case MINUS_EXPR:
360 op1_l = find_lattice_value (gimple_assign_rhs1 (stmt));
361 op2_l = find_lattice_value (gimple_assign_rhs2 (stmt));
363 /* We've set up the lattice values such that IOR neatly
364 models addition. */
365 new_l = op1_l | op2_l;
366 break;
368 case MULT_EXPR:
369 case RDIV_EXPR:
370 case TRUNC_DIV_EXPR:
371 case CEIL_DIV_EXPR:
372 case FLOOR_DIV_EXPR:
373 case ROUND_DIV_EXPR:
374 op1_l = find_lattice_value (gimple_assign_rhs1 (stmt));
375 op2_l = find_lattice_value (gimple_assign_rhs2 (stmt));
377 /* Obviously, if either varies, so does the result. */
378 if (op1_l == VARYING || op2_l == VARYING)
379 new_l = VARYING;
380 /* Don't prematurely promote variables if we've not yet seen
381 their inputs. */
382 else if (op1_l == UNINITIALIZED)
383 new_l = op2_l;
384 else if (op2_l == UNINITIALIZED)
385 new_l = op1_l;
386 else
388 /* At this point both numbers have only one component. If the
389 numbers are of opposite kind, the result is imaginary,
390 otherwise the result is real. The add/subtract translates
391 the real/imag from/to 0/1; the ^ performs the comparison. */
392 new_l = ((op1_l - ONLY_REAL) ^ (op2_l - ONLY_REAL)) + ONLY_REAL;
394 /* Don't allow the lattice value to flip-flop indefinitely. */
395 new_l |= old_l;
397 break;
399 case NEGATE_EXPR:
400 case CONJ_EXPR:
401 new_l = find_lattice_value (gimple_assign_rhs1 (stmt));
402 break;
404 default:
405 new_l = VARYING;
406 break;
409 /* If nothing changed this round, let the propagator know. */
410 if (new_l == old_l)
411 return SSA_PROP_NOT_INTERESTING;
413 complex_lattice_values[ver] = new_l;
414 return new_l == VARYING ? SSA_PROP_VARYING : SSA_PROP_INTERESTING;
417 /* Evaluate a PHI node against the complex lattice defined above. */
419 static enum ssa_prop_result
420 complex_visit_phi (gphi *phi)
422 complex_lattice_t new_l, old_l;
423 unsigned int ver;
424 tree lhs;
425 int i;
427 lhs = gimple_phi_result (phi);
429 /* This condition should be satisfied due to the initial filter
430 set up in init_dont_simulate_again. */
431 gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE);
433 /* We've set up the lattice values such that IOR neatly models PHI meet. */
434 new_l = UNINITIALIZED;
435 for (i = gimple_phi_num_args (phi) - 1; i >= 0; --i)
436 new_l |= find_lattice_value (gimple_phi_arg_def (phi, i));
438 ver = SSA_NAME_VERSION (lhs);
439 old_l = complex_lattice_values[ver];
441 if (new_l == old_l)
442 return SSA_PROP_NOT_INTERESTING;
444 complex_lattice_values[ver] = new_l;
445 return new_l == VARYING ? SSA_PROP_VARYING : SSA_PROP_INTERESTING;
448 /* Create one backing variable for a complex component of ORIG. */
450 static tree
451 create_one_component_var (tree type, tree orig, const char *prefix,
452 const char *suffix, enum tree_code code)
454 tree r = create_tmp_var (type, prefix);
456 DECL_SOURCE_LOCATION (r) = DECL_SOURCE_LOCATION (orig);
457 DECL_ARTIFICIAL (r) = 1;
459 if (DECL_NAME (orig) && !DECL_IGNORED_P (orig))
461 const char *name = IDENTIFIER_POINTER (DECL_NAME (orig));
463 DECL_NAME (r) = get_identifier (ACONCAT ((name, suffix, NULL)));
465 SET_DECL_DEBUG_EXPR (r, build1 (code, type, orig));
466 DECL_HAS_DEBUG_EXPR_P (r) = 1;
467 DECL_IGNORED_P (r) = 0;
468 TREE_NO_WARNING (r) = TREE_NO_WARNING (orig);
470 else
472 DECL_IGNORED_P (r) = 1;
473 TREE_NO_WARNING (r) = 1;
476 return r;
479 /* Retrieve a value for a complex component of VAR. */
481 static tree
482 get_component_var (tree var, bool imag_p)
484 size_t decl_index = DECL_UID (var) * 2 + imag_p;
485 tree ret = cvc_lookup (decl_index);
487 if (ret == NULL)
489 ret = create_one_component_var (TREE_TYPE (TREE_TYPE (var)), var,
490 imag_p ? "CI" : "CR",
491 imag_p ? "$imag" : "$real",
492 imag_p ? IMAGPART_EXPR : REALPART_EXPR);
493 cvc_insert (decl_index, ret);
496 return ret;
499 /* Retrieve a value for a complex component of SSA_NAME. */
501 static tree
502 get_component_ssa_name (tree ssa_name, bool imag_p)
504 complex_lattice_t lattice = find_lattice_value (ssa_name);
505 size_t ssa_name_index;
506 tree ret;
508 if (lattice == (imag_p ? ONLY_REAL : ONLY_IMAG))
510 tree inner_type = TREE_TYPE (TREE_TYPE (ssa_name));
511 if (SCALAR_FLOAT_TYPE_P (inner_type))
512 return build_real (inner_type, dconst0);
513 else
514 return build_int_cst (inner_type, 0);
517 ssa_name_index = SSA_NAME_VERSION (ssa_name) * 2 + imag_p;
518 ret = complex_ssa_name_components[ssa_name_index];
519 if (ret == NULL)
521 if (SSA_NAME_VAR (ssa_name))
522 ret = get_component_var (SSA_NAME_VAR (ssa_name), imag_p);
523 else
524 ret = TREE_TYPE (TREE_TYPE (ssa_name));
525 ret = make_ssa_name (ret);
527 /* Copy some properties from the original. In particular, whether it
528 is used in an abnormal phi, and whether it's uninitialized. */
529 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ret)
530 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name);
531 if (SSA_NAME_IS_DEFAULT_DEF (ssa_name)
532 && TREE_CODE (SSA_NAME_VAR (ssa_name)) == VAR_DECL)
534 SSA_NAME_DEF_STMT (ret) = SSA_NAME_DEF_STMT (ssa_name);
535 set_ssa_default_def (cfun, SSA_NAME_VAR (ret), ret);
538 complex_ssa_name_components[ssa_name_index] = ret;
541 return ret;
544 /* Set a value for a complex component of SSA_NAME, return a
545 gimple_seq of stuff that needs doing. */
547 static gimple_seq
548 set_component_ssa_name (tree ssa_name, bool imag_p, tree value)
550 complex_lattice_t lattice = find_lattice_value (ssa_name);
551 size_t ssa_name_index;
552 tree comp;
553 gimple last;
554 gimple_seq list;
556 /* We know the value must be zero, else there's a bug in our lattice
557 analysis. But the value may well be a variable known to contain
558 zero. We should be safe ignoring it. */
559 if (lattice == (imag_p ? ONLY_REAL : ONLY_IMAG))
560 return NULL;
562 /* If we've already assigned an SSA_NAME to this component, then this
563 means that our walk of the basic blocks found a use before the set.
564 This is fine. Now we should create an initialization for the value
565 we created earlier. */
566 ssa_name_index = SSA_NAME_VERSION (ssa_name) * 2 + imag_p;
567 comp = complex_ssa_name_components[ssa_name_index];
568 if (comp)
571 /* If we've nothing assigned, and the value we're given is already stable,
572 then install that as the value for this SSA_NAME. This preemptively
573 copy-propagates the value, which avoids unnecessary memory allocation. */
574 else if (is_gimple_min_invariant (value)
575 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name))
577 complex_ssa_name_components[ssa_name_index] = value;
578 return NULL;
580 else if (TREE_CODE (value) == SSA_NAME
581 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name))
583 /* Replace an anonymous base value with the variable from cvc_lookup.
584 This should result in better debug info. */
585 if (SSA_NAME_VAR (ssa_name)
586 && (!SSA_NAME_VAR (value) || DECL_IGNORED_P (SSA_NAME_VAR (value)))
587 && !DECL_IGNORED_P (SSA_NAME_VAR (ssa_name)))
589 comp = get_component_var (SSA_NAME_VAR (ssa_name), imag_p);
590 replace_ssa_name_symbol (value, comp);
593 complex_ssa_name_components[ssa_name_index] = value;
594 return NULL;
597 /* Finally, we need to stabilize the result by installing the value into
598 a new ssa name. */
599 else
600 comp = get_component_ssa_name (ssa_name, imag_p);
602 /* Do all the work to assign VALUE to COMP. */
603 list = NULL;
604 value = force_gimple_operand (value, &list, false, NULL);
605 last = gimple_build_assign (comp, value);
606 gimple_seq_add_stmt (&list, last);
607 gcc_assert (SSA_NAME_DEF_STMT (comp) == last);
609 return list;
612 /* Extract the real or imaginary part of a complex variable or constant.
613 Make sure that it's a proper gimple_val and gimplify it if not.
614 Emit any new code before gsi. */
616 static tree
617 extract_component (gimple_stmt_iterator *gsi, tree t, bool imagpart_p,
618 bool gimple_p)
620 switch (TREE_CODE (t))
622 case COMPLEX_CST:
623 return imagpart_p ? TREE_IMAGPART (t) : TREE_REALPART (t);
625 case COMPLEX_EXPR:
626 gcc_unreachable ();
628 case VAR_DECL:
629 case RESULT_DECL:
630 case PARM_DECL:
631 case COMPONENT_REF:
632 case ARRAY_REF:
633 case VIEW_CONVERT_EXPR:
634 case MEM_REF:
636 tree inner_type = TREE_TYPE (TREE_TYPE (t));
638 t = build1 ((imagpart_p ? IMAGPART_EXPR : REALPART_EXPR),
639 inner_type, unshare_expr (t));
641 if (gimple_p)
642 t = force_gimple_operand_gsi (gsi, t, true, NULL, true,
643 GSI_SAME_STMT);
645 return t;
648 case SSA_NAME:
649 return get_component_ssa_name (t, imagpart_p);
651 default:
652 gcc_unreachable ();
656 /* Update the complex components of the ssa name on the lhs of STMT. */
658 static void
659 update_complex_components (gimple_stmt_iterator *gsi, gimple stmt, tree r,
660 tree i)
662 tree lhs;
663 gimple_seq list;
665 lhs = gimple_get_lhs (stmt);
667 list = set_component_ssa_name (lhs, false, r);
668 if (list)
669 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
671 list = set_component_ssa_name (lhs, true, i);
672 if (list)
673 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
676 static void
677 update_complex_components_on_edge (edge e, tree lhs, tree r, tree i)
679 gimple_seq list;
681 list = set_component_ssa_name (lhs, false, r);
682 if (list)
683 gsi_insert_seq_on_edge (e, list);
685 list = set_component_ssa_name (lhs, true, i);
686 if (list)
687 gsi_insert_seq_on_edge (e, list);
691 /* Update an assignment to a complex variable in place. */
693 static void
694 update_complex_assignment (gimple_stmt_iterator *gsi, tree r, tree i)
696 gimple stmt;
698 gimple_assign_set_rhs_with_ops (gsi, COMPLEX_EXPR, r, i);
699 stmt = gsi_stmt (*gsi);
700 update_stmt (stmt);
701 if (maybe_clean_eh_stmt (stmt))
702 gimple_purge_dead_eh_edges (gimple_bb (stmt));
704 if (gimple_in_ssa_p (cfun))
705 update_complex_components (gsi, gsi_stmt (*gsi), r, i);
709 /* Generate code at the entry point of the function to initialize the
710 component variables for a complex parameter. */
712 static void
713 update_parameter_components (void)
715 edge entry_edge = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun));
716 tree parm;
718 for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = DECL_CHAIN (parm))
720 tree type = TREE_TYPE (parm);
721 tree ssa_name, r, i;
723 if (TREE_CODE (type) != COMPLEX_TYPE || !is_gimple_reg (parm))
724 continue;
726 type = TREE_TYPE (type);
727 ssa_name = ssa_default_def (cfun, parm);
728 if (!ssa_name)
729 continue;
731 r = build1 (REALPART_EXPR, type, ssa_name);
732 i = build1 (IMAGPART_EXPR, type, ssa_name);
733 update_complex_components_on_edge (entry_edge, ssa_name, r, i);
737 /* Generate code to set the component variables of a complex variable
738 to match the PHI statements in block BB. */
740 static void
741 update_phi_components (basic_block bb)
743 gphi_iterator gsi;
745 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
747 gphi *phi = gsi.phi ();
749 if (is_complex_reg (gimple_phi_result (phi)))
751 tree lr, li;
752 gimple pr = NULL, pi = NULL;
753 unsigned int i, n;
755 lr = get_component_ssa_name (gimple_phi_result (phi), false);
756 if (TREE_CODE (lr) == SSA_NAME)
757 pr = create_phi_node (lr, bb);
759 li = get_component_ssa_name (gimple_phi_result (phi), true);
760 if (TREE_CODE (li) == SSA_NAME)
761 pi = create_phi_node (li, bb);
763 for (i = 0, n = gimple_phi_num_args (phi); i < n; ++i)
765 tree comp, arg = gimple_phi_arg_def (phi, i);
766 if (pr)
768 comp = extract_component (NULL, arg, false, false);
769 SET_PHI_ARG_DEF (pr, i, comp);
771 if (pi)
773 comp = extract_component (NULL, arg, true, false);
774 SET_PHI_ARG_DEF (pi, i, comp);
781 /* Expand a complex move to scalars. */
783 static void
784 expand_complex_move (gimple_stmt_iterator *gsi, tree type)
786 tree inner_type = TREE_TYPE (type);
787 tree r, i, lhs, rhs;
788 gimple stmt = gsi_stmt (*gsi);
790 if (is_gimple_assign (stmt))
792 lhs = gimple_assign_lhs (stmt);
793 if (gimple_num_ops (stmt) == 2)
794 rhs = gimple_assign_rhs1 (stmt);
795 else
796 rhs = NULL_TREE;
798 else if (is_gimple_call (stmt))
800 lhs = gimple_call_lhs (stmt);
801 rhs = NULL_TREE;
803 else
804 gcc_unreachable ();
806 if (TREE_CODE (lhs) == SSA_NAME)
808 if (is_ctrl_altering_stmt (stmt))
810 edge e;
812 /* The value is not assigned on the exception edges, so we need not
813 concern ourselves there. We do need to update on the fallthru
814 edge. Find it. */
815 e = find_fallthru_edge (gsi_bb (*gsi)->succs);
816 if (!e)
817 gcc_unreachable ();
819 r = build1 (REALPART_EXPR, inner_type, lhs);
820 i = build1 (IMAGPART_EXPR, inner_type, lhs);
821 update_complex_components_on_edge (e, lhs, r, i);
823 else if (is_gimple_call (stmt)
824 || gimple_has_side_effects (stmt)
825 || gimple_assign_rhs_code (stmt) == PAREN_EXPR)
827 r = build1 (REALPART_EXPR, inner_type, lhs);
828 i = build1 (IMAGPART_EXPR, inner_type, lhs);
829 update_complex_components (gsi, stmt, r, i);
831 else
833 if (gimple_assign_rhs_code (stmt) != COMPLEX_EXPR)
835 r = extract_component (gsi, rhs, 0, true);
836 i = extract_component (gsi, rhs, 1, true);
838 else
840 r = gimple_assign_rhs1 (stmt);
841 i = gimple_assign_rhs2 (stmt);
843 update_complex_assignment (gsi, r, i);
846 else if (rhs && TREE_CODE (rhs) == SSA_NAME && !TREE_SIDE_EFFECTS (lhs))
848 tree x;
849 gimple t;
850 location_t loc;
852 loc = gimple_location (stmt);
853 r = extract_component (gsi, rhs, 0, false);
854 i = extract_component (gsi, rhs, 1, false);
856 x = build1 (REALPART_EXPR, inner_type, unshare_expr (lhs));
857 t = gimple_build_assign (x, r);
858 gimple_set_location (t, loc);
859 gsi_insert_before (gsi, t, GSI_SAME_STMT);
861 if (stmt == gsi_stmt (*gsi))
863 x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
864 gimple_assign_set_lhs (stmt, x);
865 gimple_assign_set_rhs1 (stmt, i);
867 else
869 x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
870 t = gimple_build_assign (x, i);
871 gimple_set_location (t, loc);
872 gsi_insert_before (gsi, t, GSI_SAME_STMT);
874 stmt = gsi_stmt (*gsi);
875 gcc_assert (gimple_code (stmt) == GIMPLE_RETURN);
876 gimple_return_set_retval (as_a <greturn *> (stmt), lhs);
879 update_stmt (stmt);
883 /* Expand complex addition to scalars:
884 a + b = (ar + br) + i(ai + bi)
885 a - b = (ar - br) + i(ai + bi)
888 static void
889 expand_complex_addition (gimple_stmt_iterator *gsi, tree inner_type,
890 tree ar, tree ai, tree br, tree bi,
891 enum tree_code code,
892 complex_lattice_t al, complex_lattice_t bl)
894 tree rr, ri;
896 switch (PAIR (al, bl))
898 case PAIR (ONLY_REAL, ONLY_REAL):
899 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
900 ri = ai;
901 break;
903 case PAIR (ONLY_REAL, ONLY_IMAG):
904 rr = ar;
905 if (code == MINUS_EXPR)
906 ri = gimplify_build2 (gsi, MINUS_EXPR, inner_type, ai, bi);
907 else
908 ri = bi;
909 break;
911 case PAIR (ONLY_IMAG, ONLY_REAL):
912 if (code == MINUS_EXPR)
913 rr = gimplify_build2 (gsi, MINUS_EXPR, inner_type, ar, br);
914 else
915 rr = br;
916 ri = ai;
917 break;
919 case PAIR (ONLY_IMAG, ONLY_IMAG):
920 rr = ar;
921 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
922 break;
924 case PAIR (VARYING, ONLY_REAL):
925 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
926 ri = ai;
927 break;
929 case PAIR (VARYING, ONLY_IMAG):
930 rr = ar;
931 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
932 break;
934 case PAIR (ONLY_REAL, VARYING):
935 if (code == MINUS_EXPR)
936 goto general;
937 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
938 ri = bi;
939 break;
941 case PAIR (ONLY_IMAG, VARYING):
942 if (code == MINUS_EXPR)
943 goto general;
944 rr = br;
945 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
946 break;
948 case PAIR (VARYING, VARYING):
949 general:
950 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
951 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
952 break;
954 default:
955 gcc_unreachable ();
958 update_complex_assignment (gsi, rr, ri);
961 /* Expand a complex multiplication or division to a libcall to the c99
962 compliant routines. */
964 static void
965 expand_complex_libcall (gimple_stmt_iterator *gsi, tree ar, tree ai,
966 tree br, tree bi, enum tree_code code)
968 machine_mode mode;
969 enum built_in_function bcode;
970 tree fn, type, lhs;
971 gimple old_stmt;
972 gcall *stmt;
974 old_stmt = gsi_stmt (*gsi);
975 lhs = gimple_assign_lhs (old_stmt);
976 type = TREE_TYPE (lhs);
978 mode = TYPE_MODE (type);
979 gcc_assert (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT);
981 if (code == MULT_EXPR)
982 bcode = ((enum built_in_function)
983 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
984 else if (code == RDIV_EXPR)
985 bcode = ((enum built_in_function)
986 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
987 else
988 gcc_unreachable ();
989 fn = builtin_decl_explicit (bcode);
991 stmt = gimple_build_call (fn, 4, ar, ai, br, bi);
992 gimple_call_set_lhs (stmt, lhs);
993 update_stmt (stmt);
994 gsi_replace (gsi, stmt, false);
996 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
997 gimple_purge_dead_eh_edges (gsi_bb (*gsi));
999 if (gimple_in_ssa_p (cfun))
1001 type = TREE_TYPE (type);
1002 update_complex_components (gsi, stmt,
1003 build1 (REALPART_EXPR, type, lhs),
1004 build1 (IMAGPART_EXPR, type, lhs));
1005 SSA_NAME_DEF_STMT (lhs) = stmt;
1009 /* Expand complex multiplication to scalars:
1010 a * b = (ar*br - ai*bi) + i(ar*bi + br*ai)
1013 static void
1014 expand_complex_multiplication (gimple_stmt_iterator *gsi, tree inner_type,
1015 tree ar, tree ai, tree br, tree bi,
1016 complex_lattice_t al, complex_lattice_t bl)
1018 tree rr, ri;
1020 if (al < bl)
1022 complex_lattice_t tl;
1023 rr = ar, ar = br, br = rr;
1024 ri = ai, ai = bi, bi = ri;
1025 tl = al, al = bl, bl = tl;
1028 switch (PAIR (al, bl))
1030 case PAIR (ONLY_REAL, ONLY_REAL):
1031 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1032 ri = ai;
1033 break;
1035 case PAIR (ONLY_IMAG, ONLY_REAL):
1036 rr = ar;
1037 if (TREE_CODE (ai) == REAL_CST
1038 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (ai), dconst1))
1039 ri = br;
1040 else
1041 ri = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1042 break;
1044 case PAIR (ONLY_IMAG, ONLY_IMAG):
1045 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1046 rr = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, rr);
1047 ri = ar;
1048 break;
1050 case PAIR (VARYING, ONLY_REAL):
1051 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1052 ri = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1053 break;
1055 case PAIR (VARYING, ONLY_IMAG):
1056 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1057 rr = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, rr);
1058 ri = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, bi);
1059 break;
1061 case PAIR (VARYING, VARYING):
1062 if (flag_complex_method == 2 && SCALAR_FLOAT_TYPE_P (inner_type))
1064 expand_complex_libcall (gsi, ar, ai, br, bi, MULT_EXPR);
1065 return;
1067 else
1069 tree t1, t2, t3, t4;
1071 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1072 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1073 t3 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, bi);
1075 /* Avoid expanding redundant multiplication for the common
1076 case of squaring a complex number. */
1077 if (ar == br && ai == bi)
1078 t4 = t3;
1079 else
1080 t4 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1082 rr = gimplify_build2 (gsi, MINUS_EXPR, inner_type, t1, t2);
1083 ri = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t3, t4);
1085 break;
1087 default:
1088 gcc_unreachable ();
1091 update_complex_assignment (gsi, rr, ri);
1094 /* Keep this algorithm in sync with fold-const.c:const_binop().
1096 Expand complex division to scalars, straightforward algorithm.
1097 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1098 t = br*br + bi*bi
1101 static void
1102 expand_complex_div_straight (gimple_stmt_iterator *gsi, tree inner_type,
1103 tree ar, tree ai, tree br, tree bi,
1104 enum tree_code code)
1106 tree rr, ri, div, t1, t2, t3;
1108 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, br, br);
1109 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, bi, bi);
1110 div = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, t2);
1112 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1113 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1114 t3 = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, t2);
1115 rr = gimplify_build2 (gsi, code, inner_type, t3, div);
1117 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1118 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, bi);
1119 t3 = gimplify_build2 (gsi, MINUS_EXPR, inner_type, t1, t2);
1120 ri = gimplify_build2 (gsi, code, inner_type, t3, div);
1122 update_complex_assignment (gsi, rr, ri);
1125 /* Keep this algorithm in sync with fold-const.c:const_binop().
1127 Expand complex division to scalars, modified algorithm to minimize
1128 overflow with wide input ranges. */
1130 static void
1131 expand_complex_div_wide (gimple_stmt_iterator *gsi, tree inner_type,
1132 tree ar, tree ai, tree br, tree bi,
1133 enum tree_code code)
1135 tree rr, ri, ratio, div, t1, t2, tr, ti, compare;
1136 basic_block bb_cond, bb_true, bb_false, bb_join;
1137 gimple stmt;
1139 /* Examine |br| < |bi|, and branch. */
1140 t1 = gimplify_build1 (gsi, ABS_EXPR, inner_type, br);
1141 t2 = gimplify_build1 (gsi, ABS_EXPR, inner_type, bi);
1142 compare = fold_build2_loc (gimple_location (gsi_stmt (*gsi)),
1143 LT_EXPR, boolean_type_node, t1, t2);
1144 STRIP_NOPS (compare);
1146 bb_cond = bb_true = bb_false = bb_join = NULL;
1147 rr = ri = tr = ti = NULL;
1148 if (TREE_CODE (compare) != INTEGER_CST)
1150 edge e;
1151 gimple stmt;
1152 tree cond, tmp;
1154 tmp = create_tmp_var (boolean_type_node);
1155 stmt = gimple_build_assign (tmp, compare);
1156 if (gimple_in_ssa_p (cfun))
1158 tmp = make_ssa_name (tmp, stmt);
1159 gimple_assign_set_lhs (stmt, tmp);
1162 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1164 cond = fold_build2_loc (gimple_location (stmt),
1165 EQ_EXPR, boolean_type_node, tmp, boolean_true_node);
1166 stmt = gimple_build_cond_from_tree (cond, NULL_TREE, NULL_TREE);
1167 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1169 /* Split the original block, and create the TRUE and FALSE blocks. */
1170 e = split_block (gsi_bb (*gsi), stmt);
1171 bb_cond = e->src;
1172 bb_join = e->dest;
1173 bb_true = create_empty_bb (bb_cond);
1174 bb_false = create_empty_bb (bb_true);
1176 /* Wire the blocks together. */
1177 e->flags = EDGE_TRUE_VALUE;
1178 redirect_edge_succ (e, bb_true);
1179 make_edge (bb_cond, bb_false, EDGE_FALSE_VALUE);
1180 make_edge (bb_true, bb_join, EDGE_FALLTHRU);
1181 make_edge (bb_false, bb_join, EDGE_FALLTHRU);
1182 add_bb_to_loop (bb_true, bb_cond->loop_father);
1183 add_bb_to_loop (bb_false, bb_cond->loop_father);
1185 /* Update dominance info. Note that bb_join's data was
1186 updated by split_block. */
1187 if (dom_info_available_p (CDI_DOMINATORS))
1189 set_immediate_dominator (CDI_DOMINATORS, bb_true, bb_cond);
1190 set_immediate_dominator (CDI_DOMINATORS, bb_false, bb_cond);
1193 rr = create_tmp_reg (inner_type);
1194 ri = create_tmp_reg (inner_type);
1197 /* In the TRUE branch, we compute
1198 ratio = br/bi;
1199 div = (br * ratio) + bi;
1200 tr = (ar * ratio) + ai;
1201 ti = (ai * ratio) - ar;
1202 tr = tr / div;
1203 ti = ti / div; */
1204 if (bb_true || integer_nonzerop (compare))
1206 if (bb_true)
1208 *gsi = gsi_last_bb (bb_true);
1209 gsi_insert_after (gsi, gimple_build_nop (), GSI_NEW_STMT);
1212 ratio = gimplify_build2 (gsi, code, inner_type, br, bi);
1214 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, br, ratio);
1215 div = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, bi);
1217 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, ratio);
1218 tr = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, ai);
1220 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, ratio);
1221 ti = gimplify_build2 (gsi, MINUS_EXPR, inner_type, t1, ar);
1223 tr = gimplify_build2 (gsi, code, inner_type, tr, div);
1224 ti = gimplify_build2 (gsi, code, inner_type, ti, div);
1226 if (bb_true)
1228 stmt = gimple_build_assign (rr, tr);
1229 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1230 stmt = gimple_build_assign (ri, ti);
1231 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1232 gsi_remove (gsi, true);
1236 /* In the FALSE branch, we compute
1237 ratio = d/c;
1238 divisor = (d * ratio) + c;
1239 tr = (b * ratio) + a;
1240 ti = b - (a * ratio);
1241 tr = tr / div;
1242 ti = ti / div; */
1243 if (bb_false || integer_zerop (compare))
1245 if (bb_false)
1247 *gsi = gsi_last_bb (bb_false);
1248 gsi_insert_after (gsi, gimple_build_nop (), GSI_NEW_STMT);
1251 ratio = gimplify_build2 (gsi, code, inner_type, bi, br);
1253 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, bi, ratio);
1254 div = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, br);
1256 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, ratio);
1257 tr = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, ar);
1259 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, ratio);
1260 ti = gimplify_build2 (gsi, MINUS_EXPR, inner_type, ai, t1);
1262 tr = gimplify_build2 (gsi, code, inner_type, tr, div);
1263 ti = gimplify_build2 (gsi, code, inner_type, ti, div);
1265 if (bb_false)
1267 stmt = gimple_build_assign (rr, tr);
1268 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1269 stmt = gimple_build_assign (ri, ti);
1270 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1271 gsi_remove (gsi, true);
1275 if (bb_join)
1276 *gsi = gsi_start_bb (bb_join);
1277 else
1278 rr = tr, ri = ti;
1280 update_complex_assignment (gsi, rr, ri);
1283 /* Expand complex division to scalars. */
1285 static void
1286 expand_complex_division (gimple_stmt_iterator *gsi, tree inner_type,
1287 tree ar, tree ai, tree br, tree bi,
1288 enum tree_code code,
1289 complex_lattice_t al, complex_lattice_t bl)
1291 tree rr, ri;
1293 switch (PAIR (al, bl))
1295 case PAIR (ONLY_REAL, ONLY_REAL):
1296 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
1297 ri = ai;
1298 break;
1300 case PAIR (ONLY_REAL, ONLY_IMAG):
1301 rr = ai;
1302 ri = gimplify_build2 (gsi, code, inner_type, ar, bi);
1303 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ri);
1304 break;
1306 case PAIR (ONLY_IMAG, ONLY_REAL):
1307 rr = ar;
1308 ri = gimplify_build2 (gsi, code, inner_type, ai, br);
1309 break;
1311 case PAIR (ONLY_IMAG, ONLY_IMAG):
1312 rr = gimplify_build2 (gsi, code, inner_type, ai, bi);
1313 ri = ar;
1314 break;
1316 case PAIR (VARYING, ONLY_REAL):
1317 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
1318 ri = gimplify_build2 (gsi, code, inner_type, ai, br);
1319 break;
1321 case PAIR (VARYING, ONLY_IMAG):
1322 rr = gimplify_build2 (gsi, code, inner_type, ai, bi);
1323 ri = gimplify_build2 (gsi, code, inner_type, ar, bi);
1324 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ri);
1326 case PAIR (ONLY_REAL, VARYING):
1327 case PAIR (ONLY_IMAG, VARYING):
1328 case PAIR (VARYING, VARYING):
1329 switch (flag_complex_method)
1331 case 0:
1332 /* straightforward implementation of complex divide acceptable. */
1333 expand_complex_div_straight (gsi, inner_type, ar, ai, br, bi, code);
1334 break;
1336 case 2:
1337 if (SCALAR_FLOAT_TYPE_P (inner_type))
1339 expand_complex_libcall (gsi, ar, ai, br, bi, code);
1340 break;
1342 /* FALLTHRU */
1344 case 1:
1345 /* wide ranges of inputs must work for complex divide. */
1346 expand_complex_div_wide (gsi, inner_type, ar, ai, br, bi, code);
1347 break;
1349 default:
1350 gcc_unreachable ();
1352 return;
1354 default:
1355 gcc_unreachable ();
1358 update_complex_assignment (gsi, rr, ri);
1361 /* Expand complex negation to scalars:
1362 -a = (-ar) + i(-ai)
1365 static void
1366 expand_complex_negation (gimple_stmt_iterator *gsi, tree inner_type,
1367 tree ar, tree ai)
1369 tree rr, ri;
1371 rr = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ar);
1372 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ai);
1374 update_complex_assignment (gsi, rr, ri);
1377 /* Expand complex conjugate to scalars:
1378 ~a = (ar) + i(-ai)
1381 static void
1382 expand_complex_conjugate (gimple_stmt_iterator *gsi, tree inner_type,
1383 tree ar, tree ai)
1385 tree ri;
1387 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ai);
1389 update_complex_assignment (gsi, ar, ri);
1392 /* Expand complex comparison (EQ or NE only). */
1394 static void
1395 expand_complex_comparison (gimple_stmt_iterator *gsi, tree ar, tree ai,
1396 tree br, tree bi, enum tree_code code)
1398 tree cr, ci, cc, type;
1399 gimple stmt;
1401 cr = gimplify_build2 (gsi, code, boolean_type_node, ar, br);
1402 ci = gimplify_build2 (gsi, code, boolean_type_node, ai, bi);
1403 cc = gimplify_build2 (gsi,
1404 (code == EQ_EXPR ? TRUTH_AND_EXPR : TRUTH_OR_EXPR),
1405 boolean_type_node, cr, ci);
1407 stmt = gsi_stmt (*gsi);
1409 switch (gimple_code (stmt))
1411 case GIMPLE_RETURN:
1413 greturn *return_stmt = as_a <greturn *> (stmt);
1414 type = TREE_TYPE (gimple_return_retval (return_stmt));
1415 gimple_return_set_retval (return_stmt, fold_convert (type, cc));
1417 break;
1419 case GIMPLE_ASSIGN:
1420 type = TREE_TYPE (gimple_assign_lhs (stmt));
1421 gimple_assign_set_rhs_from_tree (gsi, fold_convert (type, cc));
1422 stmt = gsi_stmt (*gsi);
1423 break;
1425 case GIMPLE_COND:
1427 gcond *cond_stmt = as_a <gcond *> (stmt);
1428 gimple_cond_set_code (cond_stmt, EQ_EXPR);
1429 gimple_cond_set_lhs (cond_stmt, cc);
1430 gimple_cond_set_rhs (cond_stmt, boolean_true_node);
1432 break;
1434 default:
1435 gcc_unreachable ();
1438 update_stmt (stmt);
1441 /* Expand inline asm that sets some complex SSA_NAMEs. */
1443 static void
1444 expand_complex_asm (gimple_stmt_iterator *gsi)
1446 gasm *stmt = as_a <gasm *> (gsi_stmt (*gsi));
1447 unsigned int i;
1449 for (i = 0; i < gimple_asm_noutputs (stmt); ++i)
1451 tree link = gimple_asm_output_op (stmt, i);
1452 tree op = TREE_VALUE (link);
1453 if (TREE_CODE (op) == SSA_NAME
1454 && TREE_CODE (TREE_TYPE (op)) == COMPLEX_TYPE)
1456 tree type = TREE_TYPE (op);
1457 tree inner_type = TREE_TYPE (type);
1458 tree r = build1 (REALPART_EXPR, inner_type, op);
1459 tree i = build1 (IMAGPART_EXPR, inner_type, op);
1460 gimple_seq list = set_component_ssa_name (op, false, r);
1462 if (list)
1463 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
1465 list = set_component_ssa_name (op, true, i);
1466 if (list)
1467 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
1472 /* Process one statement. If we identify a complex operation, expand it. */
1474 static void
1475 expand_complex_operations_1 (gimple_stmt_iterator *gsi)
1477 gimple stmt = gsi_stmt (*gsi);
1478 tree type, inner_type, lhs;
1479 tree ac, ar, ai, bc, br, bi;
1480 complex_lattice_t al, bl;
1481 enum tree_code code;
1483 if (gimple_code (stmt) == GIMPLE_ASM)
1485 expand_complex_asm (gsi);
1486 return;
1489 lhs = gimple_get_lhs (stmt);
1490 if (!lhs && gimple_code (stmt) != GIMPLE_COND)
1491 return;
1493 type = TREE_TYPE (gimple_op (stmt, 0));
1494 code = gimple_expr_code (stmt);
1496 /* Initial filter for operations we handle. */
1497 switch (code)
1499 case PLUS_EXPR:
1500 case MINUS_EXPR:
1501 case MULT_EXPR:
1502 case TRUNC_DIV_EXPR:
1503 case CEIL_DIV_EXPR:
1504 case FLOOR_DIV_EXPR:
1505 case ROUND_DIV_EXPR:
1506 case RDIV_EXPR:
1507 case NEGATE_EXPR:
1508 case CONJ_EXPR:
1509 if (TREE_CODE (type) != COMPLEX_TYPE)
1510 return;
1511 inner_type = TREE_TYPE (type);
1512 break;
1514 case EQ_EXPR:
1515 case NE_EXPR:
1516 /* Note, both GIMPLE_ASSIGN and GIMPLE_COND may have an EQ_EXPR
1517 subcode, so we need to access the operands using gimple_op. */
1518 inner_type = TREE_TYPE (gimple_op (stmt, 1));
1519 if (TREE_CODE (inner_type) != COMPLEX_TYPE)
1520 return;
1521 break;
1523 default:
1525 tree rhs;
1527 /* GIMPLE_COND may also fallthru here, but we do not need to
1528 do anything with it. */
1529 if (gimple_code (stmt) == GIMPLE_COND)
1530 return;
1532 if (TREE_CODE (type) == COMPLEX_TYPE)
1533 expand_complex_move (gsi, type);
1534 else if (is_gimple_assign (stmt)
1535 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
1536 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
1537 && TREE_CODE (lhs) == SSA_NAME)
1539 rhs = gimple_assign_rhs1 (stmt);
1540 rhs = extract_component (gsi, TREE_OPERAND (rhs, 0),
1541 gimple_assign_rhs_code (stmt)
1542 == IMAGPART_EXPR,
1543 false);
1544 gimple_assign_set_rhs_from_tree (gsi, rhs);
1545 stmt = gsi_stmt (*gsi);
1546 update_stmt (stmt);
1549 return;
1552 /* Extract the components of the two complex values. Make sure and
1553 handle the common case of the same value used twice specially. */
1554 if (is_gimple_assign (stmt))
1556 ac = gimple_assign_rhs1 (stmt);
1557 bc = (gimple_num_ops (stmt) > 2) ? gimple_assign_rhs2 (stmt) : NULL;
1559 /* GIMPLE_CALL can not get here. */
1560 else
1562 ac = gimple_cond_lhs (stmt);
1563 bc = gimple_cond_rhs (stmt);
1566 ar = extract_component (gsi, ac, false, true);
1567 ai = extract_component (gsi, ac, true, true);
1569 if (ac == bc)
1570 br = ar, bi = ai;
1571 else if (bc)
1573 br = extract_component (gsi, bc, 0, true);
1574 bi = extract_component (gsi, bc, 1, true);
1576 else
1577 br = bi = NULL_TREE;
1579 if (gimple_in_ssa_p (cfun))
1581 al = find_lattice_value (ac);
1582 if (al == UNINITIALIZED)
1583 al = VARYING;
1585 if (TREE_CODE_CLASS (code) == tcc_unary)
1586 bl = UNINITIALIZED;
1587 else if (ac == bc)
1588 bl = al;
1589 else
1591 bl = find_lattice_value (bc);
1592 if (bl == UNINITIALIZED)
1593 bl = VARYING;
1596 else
1597 al = bl = VARYING;
1599 switch (code)
1601 case PLUS_EXPR:
1602 case MINUS_EXPR:
1603 expand_complex_addition (gsi, inner_type, ar, ai, br, bi, code, al, bl);
1604 break;
1606 case MULT_EXPR:
1607 expand_complex_multiplication (gsi, inner_type, ar, ai, br, bi, al, bl);
1608 break;
1610 case TRUNC_DIV_EXPR:
1611 case CEIL_DIV_EXPR:
1612 case FLOOR_DIV_EXPR:
1613 case ROUND_DIV_EXPR:
1614 case RDIV_EXPR:
1615 expand_complex_division (gsi, inner_type, ar, ai, br, bi, code, al, bl);
1616 break;
1618 case NEGATE_EXPR:
1619 expand_complex_negation (gsi, inner_type, ar, ai);
1620 break;
1622 case CONJ_EXPR:
1623 expand_complex_conjugate (gsi, inner_type, ar, ai);
1624 break;
1626 case EQ_EXPR:
1627 case NE_EXPR:
1628 expand_complex_comparison (gsi, ar, ai, br, bi, code);
1629 break;
1631 default:
1632 gcc_unreachable ();
1637 /* Entry point for complex operation lowering during optimization. */
1639 static unsigned int
1640 tree_lower_complex (void)
1642 int old_last_basic_block;
1643 gimple_stmt_iterator gsi;
1644 basic_block bb;
1646 if (!init_dont_simulate_again ())
1647 return 0;
1649 complex_lattice_values.create (num_ssa_names);
1650 complex_lattice_values.safe_grow_cleared (num_ssa_names);
1652 init_parameter_lattice_values ();
1653 ssa_propagate (complex_visit_stmt, complex_visit_phi);
1655 complex_variable_components = new int_tree_htab_type (10);
1657 complex_ssa_name_components.create (2 * num_ssa_names);
1658 complex_ssa_name_components.safe_grow_cleared (2 * num_ssa_names);
1660 update_parameter_components ();
1662 /* ??? Ideally we'd traverse the blocks in breadth-first order. */
1663 old_last_basic_block = last_basic_block_for_fn (cfun);
1664 FOR_EACH_BB_FN (bb, cfun)
1666 if (bb->index >= old_last_basic_block)
1667 continue;
1669 update_phi_components (bb);
1670 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1671 expand_complex_operations_1 (&gsi);
1674 gsi_commit_edge_inserts ();
1676 delete complex_variable_components;
1677 complex_variable_components = NULL;
1678 complex_ssa_name_components.release ();
1679 complex_lattice_values.release ();
1680 return 0;
1683 namespace {
1685 const pass_data pass_data_lower_complex =
1687 GIMPLE_PASS, /* type */
1688 "cplxlower", /* name */
1689 OPTGROUP_NONE, /* optinfo_flags */
1690 TV_NONE, /* tv_id */
1691 PROP_ssa, /* properties_required */
1692 PROP_gimple_lcx, /* properties_provided */
1693 0, /* properties_destroyed */
1694 0, /* todo_flags_start */
1695 TODO_update_ssa, /* todo_flags_finish */
1698 class pass_lower_complex : public gimple_opt_pass
1700 public:
1701 pass_lower_complex (gcc::context *ctxt)
1702 : gimple_opt_pass (pass_data_lower_complex, ctxt)
1705 /* opt_pass methods: */
1706 opt_pass * clone () { return new pass_lower_complex (m_ctxt); }
1707 virtual unsigned int execute (function *) { return tree_lower_complex (); }
1709 }; // class pass_lower_complex
1711 } // anon namespace
1713 gimple_opt_pass *
1714 make_pass_lower_complex (gcc::context *ctxt)
1716 return new pass_lower_complex (ctxt);
1720 namespace {
1722 const pass_data pass_data_lower_complex_O0 =
1724 GIMPLE_PASS, /* type */
1725 "cplxlower0", /* name */
1726 OPTGROUP_NONE, /* optinfo_flags */
1727 TV_NONE, /* tv_id */
1728 PROP_cfg, /* properties_required */
1729 PROP_gimple_lcx, /* properties_provided */
1730 0, /* properties_destroyed */
1731 0, /* todo_flags_start */
1732 TODO_update_ssa, /* todo_flags_finish */
1735 class pass_lower_complex_O0 : public gimple_opt_pass
1737 public:
1738 pass_lower_complex_O0 (gcc::context *ctxt)
1739 : gimple_opt_pass (pass_data_lower_complex_O0, ctxt)
1742 /* opt_pass methods: */
1743 virtual bool gate (function *fun)
1745 /* With errors, normal optimization passes are not run. If we don't
1746 lower complex operations at all, rtl expansion will abort. */
1747 return !(fun->curr_properties & PROP_gimple_lcx);
1750 virtual unsigned int execute (function *) { return tree_lower_complex (); }
1752 }; // class pass_lower_complex_O0
1754 } // anon namespace
1756 gimple_opt_pass *
1757 make_pass_lower_complex_O0 (gcc::context *ctxt)
1759 return new pass_lower_complex_O0 (ctxt);