gcc/ada/
[official-gcc.git] / gcc / tree-complex.c
bloba2bacc6bda98cfc3a28c2a69b4fd09f8162f6ebc
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 "alias.h"
25 #include "symtab.h"
26 #include "tree.h"
27 #include "fold-const.h"
28 #include "stor-layout.h"
29 #include "flags.h"
30 #include "predict.h"
31 #include "hard-reg-set.h"
32 #include "function.h"
33 #include "dominance.h"
34 #include "cfg.h"
35 #include "basic-block.h"
36 #include "tree-ssa-alias.h"
37 #include "internal-fn.h"
38 #include "tree-eh.h"
39 #include "gimple-expr.h"
40 #include "gimple.h"
41 #include "gimplify.h"
42 #include "gimple-iterator.h"
43 #include "gimplify-me.h"
44 #include "gimple-ssa.h"
45 #include "tree-cfg.h"
46 #include "tree-phinodes.h"
47 #include "ssa-iterators.h"
48 #include "stringpool.h"
49 #include "tree-ssanames.h"
50 #include "rtl.h"
51 #include "insn-config.h"
52 #include "expmed.h"
53 #include "dojump.h"
54 #include "explow.h"
55 #include "calls.h"
56 #include "emit-rtl.h"
57 #include "varasm.h"
58 #include "stmt.h"
59 #include "expr.h"
60 #include "tree-dfa.h"
61 #include "tree-ssa.h"
62 #include "tree-iterator.h"
63 #include "tree-pass.h"
64 #include "tree-ssa-propagate.h"
65 #include "tree-hasher.h"
66 #include "cfgloop.h"
69 /* For each complex ssa name, a lattice value. We're interested in finding
70 out whether a complex number is degenerate in some way, having only real
71 or only complex parts. */
73 enum
75 UNINITIALIZED = 0,
76 ONLY_REAL = 1,
77 ONLY_IMAG = 2,
78 VARYING = 3
81 /* The type complex_lattice_t holds combinations of the above
82 constants. */
83 typedef int complex_lattice_t;
85 #define PAIR(a, b) ((a) << 2 | (b))
88 static vec<complex_lattice_t> complex_lattice_values;
90 /* For each complex variable, a pair of variables for the components exists in
91 the hashtable. */
92 static int_tree_htab_type *complex_variable_components;
94 /* For each complex SSA_NAME, a pair of ssa names for the components. */
95 static vec<tree> complex_ssa_name_components;
97 /* Lookup UID in the complex_variable_components hashtable and return the
98 associated tree. */
99 static tree
100 cvc_lookup (unsigned int uid)
102 struct int_tree_map in;
103 in.uid = uid;
104 return complex_variable_components->find_with_hash (in, uid).to;
107 /* Insert the pair UID, TO into the complex_variable_components hashtable. */
109 static void
110 cvc_insert (unsigned int uid, tree to)
112 int_tree_map h;
113 int_tree_map *loc;
115 h.uid = uid;
116 loc = complex_variable_components->find_slot_with_hash (h, uid, INSERT);
117 loc->uid = uid;
118 loc->to = to;
121 /* Return true if T is not a zero constant. In the case of real values,
122 we're only interested in +0.0. */
124 static int
125 some_nonzerop (tree t)
127 int zerop = false;
129 /* Operations with real or imaginary part of a complex number zero
130 cannot be treated the same as operations with a real or imaginary
131 operand if we care about the signs of zeros in the result. */
132 if (TREE_CODE (t) == REAL_CST && !flag_signed_zeros)
133 zerop = REAL_VALUES_IDENTICAL (TREE_REAL_CST (t), dconst0);
134 else if (TREE_CODE (t) == FIXED_CST)
135 zerop = fixed_zerop (t);
136 else if (TREE_CODE (t) == INTEGER_CST)
137 zerop = integer_zerop (t);
139 return !zerop;
143 /* Compute a lattice value from the components of a complex type REAL
144 and IMAG. */
146 static complex_lattice_t
147 find_lattice_value_parts (tree real, tree imag)
149 int r, i;
150 complex_lattice_t ret;
152 r = some_nonzerop (real);
153 i = some_nonzerop (imag);
154 ret = r * ONLY_REAL + i * ONLY_IMAG;
156 /* ??? On occasion we could do better than mapping 0+0i to real, but we
157 certainly don't want to leave it UNINITIALIZED, which eventually gets
158 mapped to VARYING. */
159 if (ret == UNINITIALIZED)
160 ret = ONLY_REAL;
162 return ret;
166 /* Compute a lattice value from gimple_val T. */
168 static complex_lattice_t
169 find_lattice_value (tree t)
171 tree real, imag;
173 switch (TREE_CODE (t))
175 case SSA_NAME:
176 return complex_lattice_values[SSA_NAME_VERSION (t)];
178 case COMPLEX_CST:
179 real = TREE_REALPART (t);
180 imag = TREE_IMAGPART (t);
181 break;
183 default:
184 gcc_unreachable ();
187 return find_lattice_value_parts (real, imag);
190 /* Determine if LHS is something for which we're interested in seeing
191 simulation results. */
193 static bool
194 is_complex_reg (tree lhs)
196 return TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE && is_gimple_reg (lhs);
199 /* Mark the incoming parameters to the function as VARYING. */
201 static void
202 init_parameter_lattice_values (void)
204 tree parm, ssa_name;
206 for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = DECL_CHAIN (parm))
207 if (is_complex_reg (parm)
208 && (ssa_name = ssa_default_def (cfun, parm)) != NULL_TREE)
209 complex_lattice_values[SSA_NAME_VERSION (ssa_name)] = VARYING;
212 /* Initialize simulation state for each statement. Return false if we
213 found no statements we want to simulate, and thus there's nothing
214 for the entire pass to do. */
216 static bool
217 init_dont_simulate_again (void)
219 basic_block bb;
220 bool saw_a_complex_op = false;
222 FOR_EACH_BB_FN (bb, cfun)
224 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
225 gsi_next (&gsi))
227 gphi *phi = gsi.phi ();
228 prop_set_simulate_again (phi,
229 is_complex_reg (gimple_phi_result (phi)));
232 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
233 gsi_next (&gsi))
235 gimple stmt;
236 tree op0, op1;
237 bool sim_again_p;
239 stmt = gsi_stmt (gsi);
240 op0 = op1 = NULL_TREE;
242 /* Most control-altering statements must be initially
243 simulated, else we won't cover the entire cfg. */
244 sim_again_p = stmt_ends_bb_p (stmt);
246 switch (gimple_code (stmt))
248 case GIMPLE_CALL:
249 if (gimple_call_lhs (stmt))
250 sim_again_p = is_complex_reg (gimple_call_lhs (stmt));
251 break;
253 case GIMPLE_ASSIGN:
254 sim_again_p = is_complex_reg (gimple_assign_lhs (stmt));
255 if (gimple_assign_rhs_code (stmt) == REALPART_EXPR
256 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
257 op0 = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
258 else
259 op0 = gimple_assign_rhs1 (stmt);
260 if (gimple_num_ops (stmt) > 2)
261 op1 = gimple_assign_rhs2 (stmt);
262 break;
264 case GIMPLE_COND:
265 op0 = gimple_cond_lhs (stmt);
266 op1 = gimple_cond_rhs (stmt);
267 break;
269 default:
270 break;
273 if (op0 || op1)
274 switch (gimple_expr_code (stmt))
276 case EQ_EXPR:
277 case NE_EXPR:
278 case PLUS_EXPR:
279 case MINUS_EXPR:
280 case MULT_EXPR:
281 case TRUNC_DIV_EXPR:
282 case CEIL_DIV_EXPR:
283 case FLOOR_DIV_EXPR:
284 case ROUND_DIV_EXPR:
285 case RDIV_EXPR:
286 if (TREE_CODE (TREE_TYPE (op0)) == COMPLEX_TYPE
287 || TREE_CODE (TREE_TYPE (op1)) == COMPLEX_TYPE)
288 saw_a_complex_op = true;
289 break;
291 case NEGATE_EXPR:
292 case CONJ_EXPR:
293 if (TREE_CODE (TREE_TYPE (op0)) == COMPLEX_TYPE)
294 saw_a_complex_op = true;
295 break;
297 case REALPART_EXPR:
298 case IMAGPART_EXPR:
299 /* The total store transformation performed during
300 gimplification creates such uninitialized loads
301 and we need to lower the statement to be able
302 to fix things up. */
303 if (TREE_CODE (op0) == SSA_NAME
304 && ssa_undefined_value_p (op0))
305 saw_a_complex_op = true;
306 break;
308 default:
309 break;
312 prop_set_simulate_again (stmt, sim_again_p);
316 return saw_a_complex_op;
320 /* Evaluate statement STMT against the complex lattice defined above. */
322 static enum ssa_prop_result
323 complex_visit_stmt (gimple stmt, edge *taken_edge_p ATTRIBUTE_UNUSED,
324 tree *result_p)
326 complex_lattice_t new_l, old_l, op1_l, op2_l;
327 unsigned int ver;
328 tree lhs;
330 lhs = gimple_get_lhs (stmt);
331 /* Skip anything but GIMPLE_ASSIGN and GIMPLE_CALL with a lhs. */
332 if (!lhs)
333 return SSA_PROP_VARYING;
335 /* These conditions should be satisfied due to the initial filter
336 set up in init_dont_simulate_again. */
337 gcc_assert (TREE_CODE (lhs) == SSA_NAME);
338 gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE);
340 *result_p = lhs;
341 ver = SSA_NAME_VERSION (lhs);
342 old_l = complex_lattice_values[ver];
344 switch (gimple_expr_code (stmt))
346 case SSA_NAME:
347 case COMPLEX_CST:
348 new_l = find_lattice_value (gimple_assign_rhs1 (stmt));
349 break;
351 case COMPLEX_EXPR:
352 new_l = find_lattice_value_parts (gimple_assign_rhs1 (stmt),
353 gimple_assign_rhs2 (stmt));
354 break;
356 case PLUS_EXPR:
357 case MINUS_EXPR:
358 op1_l = find_lattice_value (gimple_assign_rhs1 (stmt));
359 op2_l = find_lattice_value (gimple_assign_rhs2 (stmt));
361 /* We've set up the lattice values such that IOR neatly
362 models addition. */
363 new_l = op1_l | op2_l;
364 break;
366 case MULT_EXPR:
367 case RDIV_EXPR:
368 case TRUNC_DIV_EXPR:
369 case CEIL_DIV_EXPR:
370 case FLOOR_DIV_EXPR:
371 case ROUND_DIV_EXPR:
372 op1_l = find_lattice_value (gimple_assign_rhs1 (stmt));
373 op2_l = find_lattice_value (gimple_assign_rhs2 (stmt));
375 /* Obviously, if either varies, so does the result. */
376 if (op1_l == VARYING || op2_l == VARYING)
377 new_l = VARYING;
378 /* Don't prematurely promote variables if we've not yet seen
379 their inputs. */
380 else if (op1_l == UNINITIALIZED)
381 new_l = op2_l;
382 else if (op2_l == UNINITIALIZED)
383 new_l = op1_l;
384 else
386 /* At this point both numbers have only one component. If the
387 numbers are of opposite kind, the result is imaginary,
388 otherwise the result is real. The add/subtract translates
389 the real/imag from/to 0/1; the ^ performs the comparison. */
390 new_l = ((op1_l - ONLY_REAL) ^ (op2_l - ONLY_REAL)) + ONLY_REAL;
392 /* Don't allow the lattice value to flip-flop indefinitely. */
393 new_l |= old_l;
395 break;
397 case NEGATE_EXPR:
398 case CONJ_EXPR:
399 new_l = find_lattice_value (gimple_assign_rhs1 (stmt));
400 break;
402 default:
403 new_l = VARYING;
404 break;
407 /* If nothing changed this round, let the propagator know. */
408 if (new_l == old_l)
409 return SSA_PROP_NOT_INTERESTING;
411 complex_lattice_values[ver] = new_l;
412 return new_l == VARYING ? SSA_PROP_VARYING : SSA_PROP_INTERESTING;
415 /* Evaluate a PHI node against the complex lattice defined above. */
417 static enum ssa_prop_result
418 complex_visit_phi (gphi *phi)
420 complex_lattice_t new_l, old_l;
421 unsigned int ver;
422 tree lhs;
423 int i;
425 lhs = gimple_phi_result (phi);
427 /* This condition should be satisfied due to the initial filter
428 set up in init_dont_simulate_again. */
429 gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE);
431 /* We've set up the lattice values such that IOR neatly models PHI meet. */
432 new_l = UNINITIALIZED;
433 for (i = gimple_phi_num_args (phi) - 1; i >= 0; --i)
434 new_l |= find_lattice_value (gimple_phi_arg_def (phi, i));
436 ver = SSA_NAME_VERSION (lhs);
437 old_l = complex_lattice_values[ver];
439 if (new_l == old_l)
440 return SSA_PROP_NOT_INTERESTING;
442 complex_lattice_values[ver] = new_l;
443 return new_l == VARYING ? SSA_PROP_VARYING : SSA_PROP_INTERESTING;
446 /* Create one backing variable for a complex component of ORIG. */
448 static tree
449 create_one_component_var (tree type, tree orig, const char *prefix,
450 const char *suffix, enum tree_code code)
452 tree r = create_tmp_var (type, prefix);
454 DECL_SOURCE_LOCATION (r) = DECL_SOURCE_LOCATION (orig);
455 DECL_ARTIFICIAL (r) = 1;
457 if (DECL_NAME (orig) && !DECL_IGNORED_P (orig))
459 const char *name = IDENTIFIER_POINTER (DECL_NAME (orig));
461 DECL_NAME (r) = get_identifier (ACONCAT ((name, suffix, NULL)));
463 SET_DECL_DEBUG_EXPR (r, build1 (code, type, orig));
464 DECL_HAS_DEBUG_EXPR_P (r) = 1;
465 DECL_IGNORED_P (r) = 0;
466 TREE_NO_WARNING (r) = TREE_NO_WARNING (orig);
468 else
470 DECL_IGNORED_P (r) = 1;
471 TREE_NO_WARNING (r) = 1;
474 return r;
477 /* Retrieve a value for a complex component of VAR. */
479 static tree
480 get_component_var (tree var, bool imag_p)
482 size_t decl_index = DECL_UID (var) * 2 + imag_p;
483 tree ret = cvc_lookup (decl_index);
485 if (ret == NULL)
487 ret = create_one_component_var (TREE_TYPE (TREE_TYPE (var)), var,
488 imag_p ? "CI" : "CR",
489 imag_p ? "$imag" : "$real",
490 imag_p ? IMAGPART_EXPR : REALPART_EXPR);
491 cvc_insert (decl_index, ret);
494 return ret;
497 /* Retrieve a value for a complex component of SSA_NAME. */
499 static tree
500 get_component_ssa_name (tree ssa_name, bool imag_p)
502 complex_lattice_t lattice = find_lattice_value (ssa_name);
503 size_t ssa_name_index;
504 tree ret;
506 if (lattice == (imag_p ? ONLY_REAL : ONLY_IMAG))
508 tree inner_type = TREE_TYPE (TREE_TYPE (ssa_name));
509 if (SCALAR_FLOAT_TYPE_P (inner_type))
510 return build_real (inner_type, dconst0);
511 else
512 return build_int_cst (inner_type, 0);
515 ssa_name_index = SSA_NAME_VERSION (ssa_name) * 2 + imag_p;
516 ret = complex_ssa_name_components[ssa_name_index];
517 if (ret == NULL)
519 if (SSA_NAME_VAR (ssa_name))
520 ret = get_component_var (SSA_NAME_VAR (ssa_name), imag_p);
521 else
522 ret = TREE_TYPE (TREE_TYPE (ssa_name));
523 ret = make_ssa_name (ret);
525 /* Copy some properties from the original. In particular, whether it
526 is used in an abnormal phi, and whether it's uninitialized. */
527 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ret)
528 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name);
529 if (SSA_NAME_IS_DEFAULT_DEF (ssa_name)
530 && TREE_CODE (SSA_NAME_VAR (ssa_name)) == VAR_DECL)
532 SSA_NAME_DEF_STMT (ret) = SSA_NAME_DEF_STMT (ssa_name);
533 set_ssa_default_def (cfun, SSA_NAME_VAR (ret), ret);
536 complex_ssa_name_components[ssa_name_index] = ret;
539 return ret;
542 /* Set a value for a complex component of SSA_NAME, return a
543 gimple_seq of stuff that needs doing. */
545 static gimple_seq
546 set_component_ssa_name (tree ssa_name, bool imag_p, tree value)
548 complex_lattice_t lattice = find_lattice_value (ssa_name);
549 size_t ssa_name_index;
550 tree comp;
551 gimple last;
552 gimple_seq list;
554 /* We know the value must be zero, else there's a bug in our lattice
555 analysis. But the value may well be a variable known to contain
556 zero. We should be safe ignoring it. */
557 if (lattice == (imag_p ? ONLY_REAL : ONLY_IMAG))
558 return NULL;
560 /* If we've already assigned an SSA_NAME to this component, then this
561 means that our walk of the basic blocks found a use before the set.
562 This is fine. Now we should create an initialization for the value
563 we created earlier. */
564 ssa_name_index = SSA_NAME_VERSION (ssa_name) * 2 + imag_p;
565 comp = complex_ssa_name_components[ssa_name_index];
566 if (comp)
569 /* If we've nothing assigned, and the value we're given is already stable,
570 then install that as the value for this SSA_NAME. This preemptively
571 copy-propagates the value, which avoids unnecessary memory allocation. */
572 else if (is_gimple_min_invariant (value)
573 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name))
575 complex_ssa_name_components[ssa_name_index] = value;
576 return NULL;
578 else if (TREE_CODE (value) == SSA_NAME
579 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name))
581 /* Replace an anonymous base value with the variable from cvc_lookup.
582 This should result in better debug info. */
583 if (SSA_NAME_VAR (ssa_name)
584 && (!SSA_NAME_VAR (value) || DECL_IGNORED_P (SSA_NAME_VAR (value)))
585 && !DECL_IGNORED_P (SSA_NAME_VAR (ssa_name)))
587 comp = get_component_var (SSA_NAME_VAR (ssa_name), imag_p);
588 replace_ssa_name_symbol (value, comp);
591 complex_ssa_name_components[ssa_name_index] = value;
592 return NULL;
595 /* Finally, we need to stabilize the result by installing the value into
596 a new ssa name. */
597 else
598 comp = get_component_ssa_name (ssa_name, imag_p);
600 /* Do all the work to assign VALUE to COMP. */
601 list = NULL;
602 value = force_gimple_operand (value, &list, false, NULL);
603 last = gimple_build_assign (comp, value);
604 gimple_seq_add_stmt (&list, last);
605 gcc_assert (SSA_NAME_DEF_STMT (comp) == last);
607 return list;
610 /* Extract the real or imaginary part of a complex variable or constant.
611 Make sure that it's a proper gimple_val and gimplify it if not.
612 Emit any new code before gsi. */
614 static tree
615 extract_component (gimple_stmt_iterator *gsi, tree t, bool imagpart_p,
616 bool gimple_p)
618 switch (TREE_CODE (t))
620 case COMPLEX_CST:
621 return imagpart_p ? TREE_IMAGPART (t) : TREE_REALPART (t);
623 case COMPLEX_EXPR:
624 gcc_unreachable ();
626 case VAR_DECL:
627 case RESULT_DECL:
628 case PARM_DECL:
629 case COMPONENT_REF:
630 case ARRAY_REF:
631 case VIEW_CONVERT_EXPR:
632 case MEM_REF:
634 tree inner_type = TREE_TYPE (TREE_TYPE (t));
636 t = build1 ((imagpart_p ? IMAGPART_EXPR : REALPART_EXPR),
637 inner_type, unshare_expr (t));
639 if (gimple_p)
640 t = force_gimple_operand_gsi (gsi, t, true, NULL, true,
641 GSI_SAME_STMT);
643 return t;
646 case SSA_NAME:
647 return get_component_ssa_name (t, imagpart_p);
649 default:
650 gcc_unreachable ();
654 /* Update the complex components of the ssa name on the lhs of STMT. */
656 static void
657 update_complex_components (gimple_stmt_iterator *gsi, gimple stmt, tree r,
658 tree i)
660 tree lhs;
661 gimple_seq list;
663 lhs = gimple_get_lhs (stmt);
665 list = set_component_ssa_name (lhs, false, r);
666 if (list)
667 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
669 list = set_component_ssa_name (lhs, true, i);
670 if (list)
671 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
674 static void
675 update_complex_components_on_edge (edge e, tree lhs, tree r, tree i)
677 gimple_seq list;
679 list = set_component_ssa_name (lhs, false, r);
680 if (list)
681 gsi_insert_seq_on_edge (e, list);
683 list = set_component_ssa_name (lhs, true, i);
684 if (list)
685 gsi_insert_seq_on_edge (e, list);
689 /* Update an assignment to a complex variable in place. */
691 static void
692 update_complex_assignment (gimple_stmt_iterator *gsi, tree r, tree i)
694 gimple stmt;
696 gimple_assign_set_rhs_with_ops (gsi, COMPLEX_EXPR, r, i);
697 stmt = gsi_stmt (*gsi);
698 update_stmt (stmt);
699 if (maybe_clean_eh_stmt (stmt))
700 gimple_purge_dead_eh_edges (gimple_bb (stmt));
702 if (gimple_in_ssa_p (cfun))
703 update_complex_components (gsi, gsi_stmt (*gsi), r, i);
707 /* Generate code at the entry point of the function to initialize the
708 component variables for a complex parameter. */
710 static void
711 update_parameter_components (void)
713 edge entry_edge = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun));
714 tree parm;
716 for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = DECL_CHAIN (parm))
718 tree type = TREE_TYPE (parm);
719 tree ssa_name, r, i;
721 if (TREE_CODE (type) != COMPLEX_TYPE || !is_gimple_reg (parm))
722 continue;
724 type = TREE_TYPE (type);
725 ssa_name = ssa_default_def (cfun, parm);
726 if (!ssa_name)
727 continue;
729 r = build1 (REALPART_EXPR, type, ssa_name);
730 i = build1 (IMAGPART_EXPR, type, ssa_name);
731 update_complex_components_on_edge (entry_edge, ssa_name, r, i);
735 /* Generate code to set the component variables of a complex variable
736 to match the PHI statements in block BB. */
738 static void
739 update_phi_components (basic_block bb)
741 gphi_iterator gsi;
743 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
745 gphi *phi = gsi.phi ();
747 if (is_complex_reg (gimple_phi_result (phi)))
749 tree lr, li;
750 gimple pr = NULL, pi = NULL;
751 unsigned int i, n;
753 lr = get_component_ssa_name (gimple_phi_result (phi), false);
754 if (TREE_CODE (lr) == SSA_NAME)
755 pr = create_phi_node (lr, bb);
757 li = get_component_ssa_name (gimple_phi_result (phi), true);
758 if (TREE_CODE (li) == SSA_NAME)
759 pi = create_phi_node (li, bb);
761 for (i = 0, n = gimple_phi_num_args (phi); i < n; ++i)
763 tree comp, arg = gimple_phi_arg_def (phi, i);
764 if (pr)
766 comp = extract_component (NULL, arg, false, false);
767 SET_PHI_ARG_DEF (pr, i, comp);
769 if (pi)
771 comp = extract_component (NULL, arg, true, false);
772 SET_PHI_ARG_DEF (pi, i, comp);
779 /* Expand a complex move to scalars. */
781 static void
782 expand_complex_move (gimple_stmt_iterator *gsi, tree type)
784 tree inner_type = TREE_TYPE (type);
785 tree r, i, lhs, rhs;
786 gimple stmt = gsi_stmt (*gsi);
788 if (is_gimple_assign (stmt))
790 lhs = gimple_assign_lhs (stmt);
791 if (gimple_num_ops (stmt) == 2)
792 rhs = gimple_assign_rhs1 (stmt);
793 else
794 rhs = NULL_TREE;
796 else if (is_gimple_call (stmt))
798 lhs = gimple_call_lhs (stmt);
799 rhs = NULL_TREE;
801 else
802 gcc_unreachable ();
804 if (TREE_CODE (lhs) == SSA_NAME)
806 if (is_ctrl_altering_stmt (stmt))
808 edge e;
810 /* The value is not assigned on the exception edges, so we need not
811 concern ourselves there. We do need to update on the fallthru
812 edge. Find it. */
813 e = find_fallthru_edge (gsi_bb (*gsi)->succs);
814 if (!e)
815 gcc_unreachable ();
817 r = build1 (REALPART_EXPR, inner_type, lhs);
818 i = build1 (IMAGPART_EXPR, inner_type, lhs);
819 update_complex_components_on_edge (e, lhs, r, i);
821 else if (is_gimple_call (stmt)
822 || gimple_has_side_effects (stmt)
823 || gimple_assign_rhs_code (stmt) == PAREN_EXPR)
825 r = build1 (REALPART_EXPR, inner_type, lhs);
826 i = build1 (IMAGPART_EXPR, inner_type, lhs);
827 update_complex_components (gsi, stmt, r, i);
829 else
831 if (gimple_assign_rhs_code (stmt) != COMPLEX_EXPR)
833 r = extract_component (gsi, rhs, 0, true);
834 i = extract_component (gsi, rhs, 1, true);
836 else
838 r = gimple_assign_rhs1 (stmt);
839 i = gimple_assign_rhs2 (stmt);
841 update_complex_assignment (gsi, r, i);
844 else if (rhs && TREE_CODE (rhs) == SSA_NAME && !TREE_SIDE_EFFECTS (lhs))
846 tree x;
847 gimple t;
848 location_t loc;
850 loc = gimple_location (stmt);
851 r = extract_component (gsi, rhs, 0, false);
852 i = extract_component (gsi, rhs, 1, false);
854 x = build1 (REALPART_EXPR, inner_type, unshare_expr (lhs));
855 t = gimple_build_assign (x, r);
856 gimple_set_location (t, loc);
857 gsi_insert_before (gsi, t, GSI_SAME_STMT);
859 if (stmt == gsi_stmt (*gsi))
861 x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
862 gimple_assign_set_lhs (stmt, x);
863 gimple_assign_set_rhs1 (stmt, i);
865 else
867 x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
868 t = gimple_build_assign (x, i);
869 gimple_set_location (t, loc);
870 gsi_insert_before (gsi, t, GSI_SAME_STMT);
872 stmt = gsi_stmt (*gsi);
873 gcc_assert (gimple_code (stmt) == GIMPLE_RETURN);
874 gimple_return_set_retval (as_a <greturn *> (stmt), lhs);
877 update_stmt (stmt);
881 /* Expand complex addition to scalars:
882 a + b = (ar + br) + i(ai + bi)
883 a - b = (ar - br) + i(ai + bi)
886 static void
887 expand_complex_addition (gimple_stmt_iterator *gsi, tree inner_type,
888 tree ar, tree ai, tree br, tree bi,
889 enum tree_code code,
890 complex_lattice_t al, complex_lattice_t bl)
892 tree rr, ri;
894 switch (PAIR (al, bl))
896 case PAIR (ONLY_REAL, ONLY_REAL):
897 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
898 ri = ai;
899 break;
901 case PAIR (ONLY_REAL, ONLY_IMAG):
902 rr = ar;
903 if (code == MINUS_EXPR)
904 ri = gimplify_build2 (gsi, MINUS_EXPR, inner_type, ai, bi);
905 else
906 ri = bi;
907 break;
909 case PAIR (ONLY_IMAG, ONLY_REAL):
910 if (code == MINUS_EXPR)
911 rr = gimplify_build2 (gsi, MINUS_EXPR, inner_type, ar, br);
912 else
913 rr = br;
914 ri = ai;
915 break;
917 case PAIR (ONLY_IMAG, ONLY_IMAG):
918 rr = ar;
919 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
920 break;
922 case PAIR (VARYING, ONLY_REAL):
923 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
924 ri = ai;
925 break;
927 case PAIR (VARYING, ONLY_IMAG):
928 rr = ar;
929 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
930 break;
932 case PAIR (ONLY_REAL, VARYING):
933 if (code == MINUS_EXPR)
934 goto general;
935 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
936 ri = bi;
937 break;
939 case PAIR (ONLY_IMAG, VARYING):
940 if (code == MINUS_EXPR)
941 goto general;
942 rr = br;
943 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
944 break;
946 case PAIR (VARYING, VARYING):
947 general:
948 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
949 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
950 break;
952 default:
953 gcc_unreachable ();
956 update_complex_assignment (gsi, rr, ri);
959 /* Expand a complex multiplication or division to a libcall to the c99
960 compliant routines. */
962 static void
963 expand_complex_libcall (gimple_stmt_iterator *gsi, tree ar, tree ai,
964 tree br, tree bi, enum tree_code code)
966 machine_mode mode;
967 enum built_in_function bcode;
968 tree fn, type, lhs;
969 gimple old_stmt;
970 gcall *stmt;
972 old_stmt = gsi_stmt (*gsi);
973 lhs = gimple_assign_lhs (old_stmt);
974 type = TREE_TYPE (lhs);
976 mode = TYPE_MODE (type);
977 gcc_assert (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT);
979 if (code == MULT_EXPR)
980 bcode = ((enum built_in_function)
981 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
982 else if (code == RDIV_EXPR)
983 bcode = ((enum built_in_function)
984 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
985 else
986 gcc_unreachable ();
987 fn = builtin_decl_explicit (bcode);
989 stmt = gimple_build_call (fn, 4, ar, ai, br, bi);
990 gimple_call_set_lhs (stmt, lhs);
991 update_stmt (stmt);
992 gsi_replace (gsi, stmt, false);
994 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
995 gimple_purge_dead_eh_edges (gsi_bb (*gsi));
997 if (gimple_in_ssa_p (cfun))
999 type = TREE_TYPE (type);
1000 update_complex_components (gsi, stmt,
1001 build1 (REALPART_EXPR, type, lhs),
1002 build1 (IMAGPART_EXPR, type, lhs));
1003 SSA_NAME_DEF_STMT (lhs) = stmt;
1007 /* Expand complex multiplication to scalars:
1008 a * b = (ar*br - ai*bi) + i(ar*bi + br*ai)
1011 static void
1012 expand_complex_multiplication (gimple_stmt_iterator *gsi, tree inner_type,
1013 tree ar, tree ai, tree br, tree bi,
1014 complex_lattice_t al, complex_lattice_t bl)
1016 tree rr, ri;
1018 if (al < bl)
1020 complex_lattice_t tl;
1021 rr = ar, ar = br, br = rr;
1022 ri = ai, ai = bi, bi = ri;
1023 tl = al, al = bl, bl = tl;
1026 switch (PAIR (al, bl))
1028 case PAIR (ONLY_REAL, ONLY_REAL):
1029 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1030 ri = ai;
1031 break;
1033 case PAIR (ONLY_IMAG, ONLY_REAL):
1034 rr = ar;
1035 if (TREE_CODE (ai) == REAL_CST
1036 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (ai), dconst1))
1037 ri = br;
1038 else
1039 ri = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1040 break;
1042 case PAIR (ONLY_IMAG, ONLY_IMAG):
1043 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1044 rr = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, rr);
1045 ri = ar;
1046 break;
1048 case PAIR (VARYING, ONLY_REAL):
1049 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1050 ri = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1051 break;
1053 case PAIR (VARYING, ONLY_IMAG):
1054 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1055 rr = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, rr);
1056 ri = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, bi);
1057 break;
1059 case PAIR (VARYING, VARYING):
1060 if (flag_complex_method == 2 && SCALAR_FLOAT_TYPE_P (inner_type))
1062 expand_complex_libcall (gsi, ar, ai, br, bi, MULT_EXPR);
1063 return;
1065 else
1067 tree t1, t2, t3, t4;
1069 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1070 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1071 t3 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, bi);
1073 /* Avoid expanding redundant multiplication for the common
1074 case of squaring a complex number. */
1075 if (ar == br && ai == bi)
1076 t4 = t3;
1077 else
1078 t4 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1080 rr = gimplify_build2 (gsi, MINUS_EXPR, inner_type, t1, t2);
1081 ri = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t3, t4);
1083 break;
1085 default:
1086 gcc_unreachable ();
1089 update_complex_assignment (gsi, rr, ri);
1092 /* Keep this algorithm in sync with fold-const.c:const_binop().
1094 Expand complex division to scalars, straightforward algorithm.
1095 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1096 t = br*br + bi*bi
1099 static void
1100 expand_complex_div_straight (gimple_stmt_iterator *gsi, tree inner_type,
1101 tree ar, tree ai, tree br, tree bi,
1102 enum tree_code code)
1104 tree rr, ri, div, t1, t2, t3;
1106 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, br, br);
1107 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, bi, bi);
1108 div = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, t2);
1110 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1111 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1112 t3 = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, t2);
1113 rr = gimplify_build2 (gsi, code, inner_type, t3, div);
1115 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1116 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, bi);
1117 t3 = gimplify_build2 (gsi, MINUS_EXPR, inner_type, t1, t2);
1118 ri = gimplify_build2 (gsi, code, inner_type, t3, div);
1120 update_complex_assignment (gsi, rr, ri);
1123 /* Keep this algorithm in sync with fold-const.c:const_binop().
1125 Expand complex division to scalars, modified algorithm to minimize
1126 overflow with wide input ranges. */
1128 static void
1129 expand_complex_div_wide (gimple_stmt_iterator *gsi, tree inner_type,
1130 tree ar, tree ai, tree br, tree bi,
1131 enum tree_code code)
1133 tree rr, ri, ratio, div, t1, t2, tr, ti, compare;
1134 basic_block bb_cond, bb_true, bb_false, bb_join;
1135 gimple stmt;
1137 /* Examine |br| < |bi|, and branch. */
1138 t1 = gimplify_build1 (gsi, ABS_EXPR, inner_type, br);
1139 t2 = gimplify_build1 (gsi, ABS_EXPR, inner_type, bi);
1140 compare = fold_build2_loc (gimple_location (gsi_stmt (*gsi)),
1141 LT_EXPR, boolean_type_node, t1, t2);
1142 STRIP_NOPS (compare);
1144 bb_cond = bb_true = bb_false = bb_join = NULL;
1145 rr = ri = tr = ti = NULL;
1146 if (TREE_CODE (compare) != INTEGER_CST)
1148 edge e;
1149 gimple stmt;
1150 tree cond, tmp;
1152 tmp = create_tmp_var (boolean_type_node);
1153 stmt = gimple_build_assign (tmp, compare);
1154 if (gimple_in_ssa_p (cfun))
1156 tmp = make_ssa_name (tmp, stmt);
1157 gimple_assign_set_lhs (stmt, tmp);
1160 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1162 cond = fold_build2_loc (gimple_location (stmt),
1163 EQ_EXPR, boolean_type_node, tmp, boolean_true_node);
1164 stmt = gimple_build_cond_from_tree (cond, NULL_TREE, NULL_TREE);
1165 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1167 /* Split the original block, and create the TRUE and FALSE blocks. */
1168 e = split_block (gsi_bb (*gsi), stmt);
1169 bb_cond = e->src;
1170 bb_join = e->dest;
1171 bb_true = create_empty_bb (bb_cond);
1172 bb_false = create_empty_bb (bb_true);
1174 /* Wire the blocks together. */
1175 e->flags = EDGE_TRUE_VALUE;
1176 redirect_edge_succ (e, bb_true);
1177 make_edge (bb_cond, bb_false, EDGE_FALSE_VALUE);
1178 make_edge (bb_true, bb_join, EDGE_FALLTHRU);
1179 make_edge (bb_false, bb_join, EDGE_FALLTHRU);
1180 add_bb_to_loop (bb_true, bb_cond->loop_father);
1181 add_bb_to_loop (bb_false, bb_cond->loop_father);
1183 /* Update dominance info. Note that bb_join's data was
1184 updated by split_block. */
1185 if (dom_info_available_p (CDI_DOMINATORS))
1187 set_immediate_dominator (CDI_DOMINATORS, bb_true, bb_cond);
1188 set_immediate_dominator (CDI_DOMINATORS, bb_false, bb_cond);
1191 rr = create_tmp_reg (inner_type);
1192 ri = create_tmp_reg (inner_type);
1195 /* In the TRUE branch, we compute
1196 ratio = br/bi;
1197 div = (br * ratio) + bi;
1198 tr = (ar * ratio) + ai;
1199 ti = (ai * ratio) - ar;
1200 tr = tr / div;
1201 ti = ti / div; */
1202 if (bb_true || integer_nonzerop (compare))
1204 if (bb_true)
1206 *gsi = gsi_last_bb (bb_true);
1207 gsi_insert_after (gsi, gimple_build_nop (), GSI_NEW_STMT);
1210 ratio = gimplify_build2 (gsi, code, inner_type, br, bi);
1212 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, br, ratio);
1213 div = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, bi);
1215 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, ratio);
1216 tr = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, ai);
1218 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, ratio);
1219 ti = gimplify_build2 (gsi, MINUS_EXPR, inner_type, t1, ar);
1221 tr = gimplify_build2 (gsi, code, inner_type, tr, div);
1222 ti = gimplify_build2 (gsi, code, inner_type, ti, div);
1224 if (bb_true)
1226 stmt = gimple_build_assign (rr, tr);
1227 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1228 stmt = gimple_build_assign (ri, ti);
1229 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1230 gsi_remove (gsi, true);
1234 /* In the FALSE branch, we compute
1235 ratio = d/c;
1236 divisor = (d * ratio) + c;
1237 tr = (b * ratio) + a;
1238 ti = b - (a * ratio);
1239 tr = tr / div;
1240 ti = ti / div; */
1241 if (bb_false || integer_zerop (compare))
1243 if (bb_false)
1245 *gsi = gsi_last_bb (bb_false);
1246 gsi_insert_after (gsi, gimple_build_nop (), GSI_NEW_STMT);
1249 ratio = gimplify_build2 (gsi, code, inner_type, bi, br);
1251 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, bi, ratio);
1252 div = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, br);
1254 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, ratio);
1255 tr = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, ar);
1257 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, ratio);
1258 ti = gimplify_build2 (gsi, MINUS_EXPR, inner_type, ai, t1);
1260 tr = gimplify_build2 (gsi, code, inner_type, tr, div);
1261 ti = gimplify_build2 (gsi, code, inner_type, ti, div);
1263 if (bb_false)
1265 stmt = gimple_build_assign (rr, tr);
1266 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1267 stmt = gimple_build_assign (ri, ti);
1268 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1269 gsi_remove (gsi, true);
1273 if (bb_join)
1274 *gsi = gsi_start_bb (bb_join);
1275 else
1276 rr = tr, ri = ti;
1278 update_complex_assignment (gsi, rr, ri);
1281 /* Expand complex division to scalars. */
1283 static void
1284 expand_complex_division (gimple_stmt_iterator *gsi, tree inner_type,
1285 tree ar, tree ai, tree br, tree bi,
1286 enum tree_code code,
1287 complex_lattice_t al, complex_lattice_t bl)
1289 tree rr, ri;
1291 switch (PAIR (al, bl))
1293 case PAIR (ONLY_REAL, ONLY_REAL):
1294 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
1295 ri = ai;
1296 break;
1298 case PAIR (ONLY_REAL, ONLY_IMAG):
1299 rr = ai;
1300 ri = gimplify_build2 (gsi, code, inner_type, ar, bi);
1301 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ri);
1302 break;
1304 case PAIR (ONLY_IMAG, ONLY_REAL):
1305 rr = ar;
1306 ri = gimplify_build2 (gsi, code, inner_type, ai, br);
1307 break;
1309 case PAIR (ONLY_IMAG, ONLY_IMAG):
1310 rr = gimplify_build2 (gsi, code, inner_type, ai, bi);
1311 ri = ar;
1312 break;
1314 case PAIR (VARYING, ONLY_REAL):
1315 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
1316 ri = gimplify_build2 (gsi, code, inner_type, ai, br);
1317 break;
1319 case PAIR (VARYING, ONLY_IMAG):
1320 rr = gimplify_build2 (gsi, code, inner_type, ai, bi);
1321 ri = gimplify_build2 (gsi, code, inner_type, ar, bi);
1322 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ri);
1324 case PAIR (ONLY_REAL, VARYING):
1325 case PAIR (ONLY_IMAG, VARYING):
1326 case PAIR (VARYING, VARYING):
1327 switch (flag_complex_method)
1329 case 0:
1330 /* straightforward implementation of complex divide acceptable. */
1331 expand_complex_div_straight (gsi, inner_type, ar, ai, br, bi, code);
1332 break;
1334 case 2:
1335 if (SCALAR_FLOAT_TYPE_P (inner_type))
1337 expand_complex_libcall (gsi, ar, ai, br, bi, code);
1338 break;
1340 /* FALLTHRU */
1342 case 1:
1343 /* wide ranges of inputs must work for complex divide. */
1344 expand_complex_div_wide (gsi, inner_type, ar, ai, br, bi, code);
1345 break;
1347 default:
1348 gcc_unreachable ();
1350 return;
1352 default:
1353 gcc_unreachable ();
1356 update_complex_assignment (gsi, rr, ri);
1359 /* Expand complex negation to scalars:
1360 -a = (-ar) + i(-ai)
1363 static void
1364 expand_complex_negation (gimple_stmt_iterator *gsi, tree inner_type,
1365 tree ar, tree ai)
1367 tree rr, ri;
1369 rr = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ar);
1370 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ai);
1372 update_complex_assignment (gsi, rr, ri);
1375 /* Expand complex conjugate to scalars:
1376 ~a = (ar) + i(-ai)
1379 static void
1380 expand_complex_conjugate (gimple_stmt_iterator *gsi, tree inner_type,
1381 tree ar, tree ai)
1383 tree ri;
1385 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ai);
1387 update_complex_assignment (gsi, ar, ri);
1390 /* Expand complex comparison (EQ or NE only). */
1392 static void
1393 expand_complex_comparison (gimple_stmt_iterator *gsi, tree ar, tree ai,
1394 tree br, tree bi, enum tree_code code)
1396 tree cr, ci, cc, type;
1397 gimple stmt;
1399 cr = gimplify_build2 (gsi, code, boolean_type_node, ar, br);
1400 ci = gimplify_build2 (gsi, code, boolean_type_node, ai, bi);
1401 cc = gimplify_build2 (gsi,
1402 (code == EQ_EXPR ? TRUTH_AND_EXPR : TRUTH_OR_EXPR),
1403 boolean_type_node, cr, ci);
1405 stmt = gsi_stmt (*gsi);
1407 switch (gimple_code (stmt))
1409 case GIMPLE_RETURN:
1411 greturn *return_stmt = as_a <greturn *> (stmt);
1412 type = TREE_TYPE (gimple_return_retval (return_stmt));
1413 gimple_return_set_retval (return_stmt, fold_convert (type, cc));
1415 break;
1417 case GIMPLE_ASSIGN:
1418 type = TREE_TYPE (gimple_assign_lhs (stmt));
1419 gimple_assign_set_rhs_from_tree (gsi, fold_convert (type, cc));
1420 stmt = gsi_stmt (*gsi);
1421 break;
1423 case GIMPLE_COND:
1425 gcond *cond_stmt = as_a <gcond *> (stmt);
1426 gimple_cond_set_code (cond_stmt, EQ_EXPR);
1427 gimple_cond_set_lhs (cond_stmt, cc);
1428 gimple_cond_set_rhs (cond_stmt, boolean_true_node);
1430 break;
1432 default:
1433 gcc_unreachable ();
1436 update_stmt (stmt);
1439 /* Expand inline asm that sets some complex SSA_NAMEs. */
1441 static void
1442 expand_complex_asm (gimple_stmt_iterator *gsi)
1444 gasm *stmt = as_a <gasm *> (gsi_stmt (*gsi));
1445 unsigned int i;
1447 for (i = 0; i < gimple_asm_noutputs (stmt); ++i)
1449 tree link = gimple_asm_output_op (stmt, i);
1450 tree op = TREE_VALUE (link);
1451 if (TREE_CODE (op) == SSA_NAME
1452 && TREE_CODE (TREE_TYPE (op)) == COMPLEX_TYPE)
1454 tree type = TREE_TYPE (op);
1455 tree inner_type = TREE_TYPE (type);
1456 tree r = build1 (REALPART_EXPR, inner_type, op);
1457 tree i = build1 (IMAGPART_EXPR, inner_type, op);
1458 gimple_seq list = set_component_ssa_name (op, false, r);
1460 if (list)
1461 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
1463 list = set_component_ssa_name (op, true, i);
1464 if (list)
1465 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
1470 /* Process one statement. If we identify a complex operation, expand it. */
1472 static void
1473 expand_complex_operations_1 (gimple_stmt_iterator *gsi)
1475 gimple stmt = gsi_stmt (*gsi);
1476 tree type, inner_type, lhs;
1477 tree ac, ar, ai, bc, br, bi;
1478 complex_lattice_t al, bl;
1479 enum tree_code code;
1481 if (gimple_code (stmt) == GIMPLE_ASM)
1483 expand_complex_asm (gsi);
1484 return;
1487 lhs = gimple_get_lhs (stmt);
1488 if (!lhs && gimple_code (stmt) != GIMPLE_COND)
1489 return;
1491 type = TREE_TYPE (gimple_op (stmt, 0));
1492 code = gimple_expr_code (stmt);
1494 /* Initial filter for operations we handle. */
1495 switch (code)
1497 case PLUS_EXPR:
1498 case MINUS_EXPR:
1499 case MULT_EXPR:
1500 case TRUNC_DIV_EXPR:
1501 case CEIL_DIV_EXPR:
1502 case FLOOR_DIV_EXPR:
1503 case ROUND_DIV_EXPR:
1504 case RDIV_EXPR:
1505 case NEGATE_EXPR:
1506 case CONJ_EXPR:
1507 if (TREE_CODE (type) != COMPLEX_TYPE)
1508 return;
1509 inner_type = TREE_TYPE (type);
1510 break;
1512 case EQ_EXPR:
1513 case NE_EXPR:
1514 /* Note, both GIMPLE_ASSIGN and GIMPLE_COND may have an EQ_EXPR
1515 subcode, so we need to access the operands using gimple_op. */
1516 inner_type = TREE_TYPE (gimple_op (stmt, 1));
1517 if (TREE_CODE (inner_type) != COMPLEX_TYPE)
1518 return;
1519 break;
1521 default:
1523 tree rhs;
1525 /* GIMPLE_COND may also fallthru here, but we do not need to
1526 do anything with it. */
1527 if (gimple_code (stmt) == GIMPLE_COND)
1528 return;
1530 if (TREE_CODE (type) == COMPLEX_TYPE)
1531 expand_complex_move (gsi, type);
1532 else if (is_gimple_assign (stmt)
1533 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
1534 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
1535 && TREE_CODE (lhs) == SSA_NAME)
1537 rhs = gimple_assign_rhs1 (stmt);
1538 rhs = extract_component (gsi, TREE_OPERAND (rhs, 0),
1539 gimple_assign_rhs_code (stmt)
1540 == IMAGPART_EXPR,
1541 false);
1542 gimple_assign_set_rhs_from_tree (gsi, rhs);
1543 stmt = gsi_stmt (*gsi);
1544 update_stmt (stmt);
1547 return;
1550 /* Extract the components of the two complex values. Make sure and
1551 handle the common case of the same value used twice specially. */
1552 if (is_gimple_assign (stmt))
1554 ac = gimple_assign_rhs1 (stmt);
1555 bc = (gimple_num_ops (stmt) > 2) ? gimple_assign_rhs2 (stmt) : NULL;
1557 /* GIMPLE_CALL can not get here. */
1558 else
1560 ac = gimple_cond_lhs (stmt);
1561 bc = gimple_cond_rhs (stmt);
1564 ar = extract_component (gsi, ac, false, true);
1565 ai = extract_component (gsi, ac, true, true);
1567 if (ac == bc)
1568 br = ar, bi = ai;
1569 else if (bc)
1571 br = extract_component (gsi, bc, 0, true);
1572 bi = extract_component (gsi, bc, 1, true);
1574 else
1575 br = bi = NULL_TREE;
1577 if (gimple_in_ssa_p (cfun))
1579 al = find_lattice_value (ac);
1580 if (al == UNINITIALIZED)
1581 al = VARYING;
1583 if (TREE_CODE_CLASS (code) == tcc_unary)
1584 bl = UNINITIALIZED;
1585 else if (ac == bc)
1586 bl = al;
1587 else
1589 bl = find_lattice_value (bc);
1590 if (bl == UNINITIALIZED)
1591 bl = VARYING;
1594 else
1595 al = bl = VARYING;
1597 switch (code)
1599 case PLUS_EXPR:
1600 case MINUS_EXPR:
1601 expand_complex_addition (gsi, inner_type, ar, ai, br, bi, code, al, bl);
1602 break;
1604 case MULT_EXPR:
1605 expand_complex_multiplication (gsi, inner_type, ar, ai, br, bi, al, bl);
1606 break;
1608 case TRUNC_DIV_EXPR:
1609 case CEIL_DIV_EXPR:
1610 case FLOOR_DIV_EXPR:
1611 case ROUND_DIV_EXPR:
1612 case RDIV_EXPR:
1613 expand_complex_division (gsi, inner_type, ar, ai, br, bi, code, al, bl);
1614 break;
1616 case NEGATE_EXPR:
1617 expand_complex_negation (gsi, inner_type, ar, ai);
1618 break;
1620 case CONJ_EXPR:
1621 expand_complex_conjugate (gsi, inner_type, ar, ai);
1622 break;
1624 case EQ_EXPR:
1625 case NE_EXPR:
1626 expand_complex_comparison (gsi, ar, ai, br, bi, code);
1627 break;
1629 default:
1630 gcc_unreachable ();
1635 /* Entry point for complex operation lowering during optimization. */
1637 static unsigned int
1638 tree_lower_complex (void)
1640 int old_last_basic_block;
1641 gimple_stmt_iterator gsi;
1642 basic_block bb;
1644 if (!init_dont_simulate_again ())
1645 return 0;
1647 complex_lattice_values.create (num_ssa_names);
1648 complex_lattice_values.safe_grow_cleared (num_ssa_names);
1650 init_parameter_lattice_values ();
1651 ssa_propagate (complex_visit_stmt, complex_visit_phi);
1653 complex_variable_components = new int_tree_htab_type (10);
1655 complex_ssa_name_components.create (2 * num_ssa_names);
1656 complex_ssa_name_components.safe_grow_cleared (2 * num_ssa_names);
1658 update_parameter_components ();
1660 /* ??? Ideally we'd traverse the blocks in breadth-first order. */
1661 old_last_basic_block = last_basic_block_for_fn (cfun);
1662 FOR_EACH_BB_FN (bb, cfun)
1664 if (bb->index >= old_last_basic_block)
1665 continue;
1667 update_phi_components (bb);
1668 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1669 expand_complex_operations_1 (&gsi);
1672 gsi_commit_edge_inserts ();
1674 delete complex_variable_components;
1675 complex_variable_components = NULL;
1676 complex_ssa_name_components.release ();
1677 complex_lattice_values.release ();
1678 return 0;
1681 namespace {
1683 const pass_data pass_data_lower_complex =
1685 GIMPLE_PASS, /* type */
1686 "cplxlower", /* name */
1687 OPTGROUP_NONE, /* optinfo_flags */
1688 TV_NONE, /* tv_id */
1689 PROP_ssa, /* properties_required */
1690 PROP_gimple_lcx, /* properties_provided */
1691 0, /* properties_destroyed */
1692 0, /* todo_flags_start */
1693 TODO_update_ssa, /* todo_flags_finish */
1696 class pass_lower_complex : public gimple_opt_pass
1698 public:
1699 pass_lower_complex (gcc::context *ctxt)
1700 : gimple_opt_pass (pass_data_lower_complex, ctxt)
1703 /* opt_pass methods: */
1704 opt_pass * clone () { return new pass_lower_complex (m_ctxt); }
1705 virtual unsigned int execute (function *) { return tree_lower_complex (); }
1707 }; // class pass_lower_complex
1709 } // anon namespace
1711 gimple_opt_pass *
1712 make_pass_lower_complex (gcc::context *ctxt)
1714 return new pass_lower_complex (ctxt);
1718 namespace {
1720 const pass_data pass_data_lower_complex_O0 =
1722 GIMPLE_PASS, /* type */
1723 "cplxlower0", /* name */
1724 OPTGROUP_NONE, /* optinfo_flags */
1725 TV_NONE, /* tv_id */
1726 PROP_cfg, /* properties_required */
1727 PROP_gimple_lcx, /* properties_provided */
1728 0, /* properties_destroyed */
1729 0, /* todo_flags_start */
1730 TODO_update_ssa, /* todo_flags_finish */
1733 class pass_lower_complex_O0 : public gimple_opt_pass
1735 public:
1736 pass_lower_complex_O0 (gcc::context *ctxt)
1737 : gimple_opt_pass (pass_data_lower_complex_O0, ctxt)
1740 /* opt_pass methods: */
1741 virtual bool gate (function *fun)
1743 /* With errors, normal optimization passes are not run. If we don't
1744 lower complex operations at all, rtl expansion will abort. */
1745 return !(fun->curr_properties & PROP_gimple_lcx);
1748 virtual unsigned int execute (function *) { return tree_lower_complex (); }
1750 }; // class pass_lower_complex_O0
1752 } // anon namespace
1754 gimple_opt_pass *
1755 make_pass_lower_complex_O0 (gcc::context *ctxt)
1757 return new pass_lower_complex_O0 (ctxt);