PR ipa/64481
[official-gcc.git] / gcc / tree-complex.c
blob69559b71fb3cf28cbca89c7082bee0339ec56e13
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 "hash-set.h"
25 #include "machmode.h"
26 #include "vec.h"
27 #include "double-int.h"
28 #include "input.h"
29 #include "alias.h"
30 #include "symtab.h"
31 #include "wide-int.h"
32 #include "inchash.h"
33 #include "real.h"
34 #include "tree.h"
35 #include "fold-const.h"
36 #include "stor-layout.h"
37 #include "flags.h"
38 #include "predict.h"
39 #include "hard-reg-set.h"
40 #include "input.h"
41 #include "function.h"
42 #include "dominance.h"
43 #include "cfg.h"
44 #include "basic-block.h"
45 #include "tree-ssa-alias.h"
46 #include "internal-fn.h"
47 #include "tree-eh.h"
48 #include "gimple-expr.h"
49 #include "is-a.h"
50 #include "gimple.h"
51 #include "gimplify.h"
52 #include "gimple-iterator.h"
53 #include "gimplify-me.h"
54 #include "gimple-ssa.h"
55 #include "tree-cfg.h"
56 #include "tree-phinodes.h"
57 #include "ssa-iterators.h"
58 #include "stringpool.h"
59 #include "tree-ssanames.h"
60 #include "expr.h"
61 #include "tree-dfa.h"
62 #include "tree-ssa.h"
63 #include "tree-iterator.h"
64 #include "tree-pass.h"
65 #include "tree-ssa-propagate.h"
66 #include "tree-hasher.h"
67 #include "cfgloop.h"
70 /* For each complex ssa name, a lattice value. We're interested in finding
71 out whether a complex number is degenerate in some way, having only real
72 or only complex parts. */
74 enum
76 UNINITIALIZED = 0,
77 ONLY_REAL = 1,
78 ONLY_IMAG = 2,
79 VARYING = 3
82 /* The type complex_lattice_t holds combinations of the above
83 constants. */
84 typedef int complex_lattice_t;
86 #define PAIR(a, b) ((a) << 2 | (b))
89 static vec<complex_lattice_t> complex_lattice_values;
91 /* For each complex variable, a pair of variables for the components exists in
92 the hashtable. */
93 static int_tree_htab_type *complex_variable_components;
95 /* For each complex SSA_NAME, a pair of ssa names for the components. */
96 static vec<tree> complex_ssa_name_components;
98 /* Lookup UID in the complex_variable_components hashtable and return the
99 associated tree. */
100 static tree
101 cvc_lookup (unsigned int uid)
103 struct int_tree_map in;
104 in.uid = uid;
105 return complex_variable_components->find_with_hash (in, uid).to;
108 /* Insert the pair UID, TO into the complex_variable_components hashtable. */
110 static void
111 cvc_insert (unsigned int uid, tree to)
113 int_tree_map h;
114 int_tree_map *loc;
116 h.uid = uid;
117 loc = complex_variable_components->find_slot_with_hash (h, uid, INSERT);
118 loc->uid = uid;
119 loc->to = to;
122 /* Return true if T is not a zero constant. In the case of real values,
123 we're only interested in +0.0. */
125 static int
126 some_nonzerop (tree t)
128 int zerop = false;
130 /* Operations with real or imaginary part of a complex number zero
131 cannot be treated the same as operations with a real or imaginary
132 operand if we care about the signs of zeros in the result. */
133 if (TREE_CODE (t) == REAL_CST && !flag_signed_zeros)
134 zerop = REAL_VALUES_IDENTICAL (TREE_REAL_CST (t), dconst0);
135 else if (TREE_CODE (t) == FIXED_CST)
136 zerop = fixed_zerop (t);
137 else if (TREE_CODE (t) == INTEGER_CST)
138 zerop = integer_zerop (t);
140 return !zerop;
144 /* Compute a lattice value from the components of a complex type REAL
145 and IMAG. */
147 static complex_lattice_t
148 find_lattice_value_parts (tree real, tree imag)
150 int r, i;
151 complex_lattice_t ret;
153 r = some_nonzerop (real);
154 i = some_nonzerop (imag);
155 ret = r * ONLY_REAL + i * ONLY_IMAG;
157 /* ??? On occasion we could do better than mapping 0+0i to real, but we
158 certainly don't want to leave it UNINITIALIZED, which eventually gets
159 mapped to VARYING. */
160 if (ret == UNINITIALIZED)
161 ret = ONLY_REAL;
163 return ret;
167 /* Compute a lattice value from gimple_val T. */
169 static complex_lattice_t
170 find_lattice_value (tree t)
172 tree real, imag;
174 switch (TREE_CODE (t))
176 case SSA_NAME:
177 return complex_lattice_values[SSA_NAME_VERSION (t)];
179 case COMPLEX_CST:
180 real = TREE_REALPART (t);
181 imag = TREE_IMAGPART (t);
182 break;
184 default:
185 gcc_unreachable ();
188 return find_lattice_value_parts (real, imag);
191 /* Determine if LHS is something for which we're interested in seeing
192 simulation results. */
194 static bool
195 is_complex_reg (tree lhs)
197 return TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE && is_gimple_reg (lhs);
200 /* Mark the incoming parameters to the function as VARYING. */
202 static void
203 init_parameter_lattice_values (void)
205 tree parm, ssa_name;
207 for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = DECL_CHAIN (parm))
208 if (is_complex_reg (parm)
209 && (ssa_name = ssa_default_def (cfun, parm)) != NULL_TREE)
210 complex_lattice_values[SSA_NAME_VERSION (ssa_name)] = VARYING;
213 /* Initialize simulation state for each statement. Return false if we
214 found no statements we want to simulate, and thus there's nothing
215 for the entire pass to do. */
217 static bool
218 init_dont_simulate_again (void)
220 basic_block bb;
221 bool saw_a_complex_op = false;
223 FOR_EACH_BB_FN (bb, cfun)
225 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
226 gsi_next (&gsi))
228 gphi *phi = gsi.phi ();
229 prop_set_simulate_again (phi,
230 is_complex_reg (gimple_phi_result (phi)));
233 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
234 gsi_next (&gsi))
236 gimple stmt;
237 tree op0, op1;
238 bool sim_again_p;
240 stmt = gsi_stmt (gsi);
241 op0 = op1 = NULL_TREE;
243 /* Most control-altering statements must be initially
244 simulated, else we won't cover the entire cfg. */
245 sim_again_p = stmt_ends_bb_p (stmt);
247 switch (gimple_code (stmt))
249 case GIMPLE_CALL:
250 if (gimple_call_lhs (stmt))
251 sim_again_p = is_complex_reg (gimple_call_lhs (stmt));
252 break;
254 case GIMPLE_ASSIGN:
255 sim_again_p = is_complex_reg (gimple_assign_lhs (stmt));
256 if (gimple_assign_rhs_code (stmt) == REALPART_EXPR
257 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
258 op0 = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
259 else
260 op0 = gimple_assign_rhs1 (stmt);
261 if (gimple_num_ops (stmt) > 2)
262 op1 = gimple_assign_rhs2 (stmt);
263 break;
265 case GIMPLE_COND:
266 op0 = gimple_cond_lhs (stmt);
267 op1 = gimple_cond_rhs (stmt);
268 break;
270 default:
271 break;
274 if (op0 || op1)
275 switch (gimple_expr_code (stmt))
277 case EQ_EXPR:
278 case NE_EXPR:
279 case PLUS_EXPR:
280 case MINUS_EXPR:
281 case MULT_EXPR:
282 case TRUNC_DIV_EXPR:
283 case CEIL_DIV_EXPR:
284 case FLOOR_DIV_EXPR:
285 case ROUND_DIV_EXPR:
286 case RDIV_EXPR:
287 if (TREE_CODE (TREE_TYPE (op0)) == COMPLEX_TYPE
288 || TREE_CODE (TREE_TYPE (op1)) == COMPLEX_TYPE)
289 saw_a_complex_op = true;
290 break;
292 case NEGATE_EXPR:
293 case CONJ_EXPR:
294 if (TREE_CODE (TREE_TYPE (op0)) == COMPLEX_TYPE)
295 saw_a_complex_op = true;
296 break;
298 case REALPART_EXPR:
299 case IMAGPART_EXPR:
300 /* The total store transformation performed during
301 gimplification creates such uninitialized loads
302 and we need to lower the statement to be able
303 to fix things up. */
304 if (TREE_CODE (op0) == SSA_NAME
305 && ssa_undefined_value_p (op0))
306 saw_a_complex_op = true;
307 break;
309 default:
310 break;
313 prop_set_simulate_again (stmt, sim_again_p);
317 return saw_a_complex_op;
321 /* Evaluate statement STMT against the complex lattice defined above. */
323 static enum ssa_prop_result
324 complex_visit_stmt (gimple stmt, edge *taken_edge_p ATTRIBUTE_UNUSED,
325 tree *result_p)
327 complex_lattice_t new_l, old_l, op1_l, op2_l;
328 unsigned int ver;
329 tree lhs;
331 lhs = gimple_get_lhs (stmt);
332 /* Skip anything but GIMPLE_ASSIGN and GIMPLE_CALL with a lhs. */
333 if (!lhs)
334 return SSA_PROP_VARYING;
336 /* These conditions should be satisfied due to the initial filter
337 set up in init_dont_simulate_again. */
338 gcc_assert (TREE_CODE (lhs) == SSA_NAME);
339 gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE);
341 *result_p = lhs;
342 ver = SSA_NAME_VERSION (lhs);
343 old_l = complex_lattice_values[ver];
345 switch (gimple_expr_code (stmt))
347 case SSA_NAME:
348 case COMPLEX_CST:
349 new_l = find_lattice_value (gimple_assign_rhs1 (stmt));
350 break;
352 case COMPLEX_EXPR:
353 new_l = find_lattice_value_parts (gimple_assign_rhs1 (stmt),
354 gimple_assign_rhs2 (stmt));
355 break;
357 case PLUS_EXPR:
358 case MINUS_EXPR:
359 op1_l = find_lattice_value (gimple_assign_rhs1 (stmt));
360 op2_l = find_lattice_value (gimple_assign_rhs2 (stmt));
362 /* We've set up the lattice values such that IOR neatly
363 models addition. */
364 new_l = op1_l | op2_l;
365 break;
367 case MULT_EXPR:
368 case RDIV_EXPR:
369 case TRUNC_DIV_EXPR:
370 case CEIL_DIV_EXPR:
371 case FLOOR_DIV_EXPR:
372 case ROUND_DIV_EXPR:
373 op1_l = find_lattice_value (gimple_assign_rhs1 (stmt));
374 op2_l = find_lattice_value (gimple_assign_rhs2 (stmt));
376 /* Obviously, if either varies, so does the result. */
377 if (op1_l == VARYING || op2_l == VARYING)
378 new_l = VARYING;
379 /* Don't prematurely promote variables if we've not yet seen
380 their inputs. */
381 else if (op1_l == UNINITIALIZED)
382 new_l = op2_l;
383 else if (op2_l == UNINITIALIZED)
384 new_l = op1_l;
385 else
387 /* At this point both numbers have only one component. If the
388 numbers are of opposite kind, the result is imaginary,
389 otherwise the result is real. The add/subtract translates
390 the real/imag from/to 0/1; the ^ performs the comparison. */
391 new_l = ((op1_l - ONLY_REAL) ^ (op2_l - ONLY_REAL)) + ONLY_REAL;
393 /* Don't allow the lattice value to flip-flop indefinitely. */
394 new_l |= old_l;
396 break;
398 case NEGATE_EXPR:
399 case CONJ_EXPR:
400 new_l = find_lattice_value (gimple_assign_rhs1 (stmt));
401 break;
403 default:
404 new_l = VARYING;
405 break;
408 /* If nothing changed this round, let the propagator know. */
409 if (new_l == old_l)
410 return SSA_PROP_NOT_INTERESTING;
412 complex_lattice_values[ver] = new_l;
413 return new_l == VARYING ? SSA_PROP_VARYING : SSA_PROP_INTERESTING;
416 /* Evaluate a PHI node against the complex lattice defined above. */
418 static enum ssa_prop_result
419 complex_visit_phi (gphi *phi)
421 complex_lattice_t new_l, old_l;
422 unsigned int ver;
423 tree lhs;
424 int i;
426 lhs = gimple_phi_result (phi);
428 /* This condition should be satisfied due to the initial filter
429 set up in init_dont_simulate_again. */
430 gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE);
432 /* We've set up the lattice values such that IOR neatly models PHI meet. */
433 new_l = UNINITIALIZED;
434 for (i = gimple_phi_num_args (phi) - 1; i >= 0; --i)
435 new_l |= find_lattice_value (gimple_phi_arg_def (phi, i));
437 ver = SSA_NAME_VERSION (lhs);
438 old_l = complex_lattice_values[ver];
440 if (new_l == old_l)
441 return SSA_PROP_NOT_INTERESTING;
443 complex_lattice_values[ver] = new_l;
444 return new_l == VARYING ? SSA_PROP_VARYING : SSA_PROP_INTERESTING;
447 /* Create one backing variable for a complex component of ORIG. */
449 static tree
450 create_one_component_var (tree type, tree orig, const char *prefix,
451 const char *suffix, enum tree_code code)
453 tree r = create_tmp_var (type, prefix);
455 DECL_SOURCE_LOCATION (r) = DECL_SOURCE_LOCATION (orig);
456 DECL_ARTIFICIAL (r) = 1;
458 if (DECL_NAME (orig) && !DECL_IGNORED_P (orig))
460 const char *name = IDENTIFIER_POINTER (DECL_NAME (orig));
462 DECL_NAME (r) = get_identifier (ACONCAT ((name, suffix, NULL)));
464 SET_DECL_DEBUG_EXPR (r, build1 (code, type, orig));
465 DECL_HAS_DEBUG_EXPR_P (r) = 1;
466 DECL_IGNORED_P (r) = 0;
467 TREE_NO_WARNING (r) = TREE_NO_WARNING (orig);
469 else
471 DECL_IGNORED_P (r) = 1;
472 TREE_NO_WARNING (r) = 1;
475 return r;
478 /* Retrieve a value for a complex component of VAR. */
480 static tree
481 get_component_var (tree var, bool imag_p)
483 size_t decl_index = DECL_UID (var) * 2 + imag_p;
484 tree ret = cvc_lookup (decl_index);
486 if (ret == NULL)
488 ret = create_one_component_var (TREE_TYPE (TREE_TYPE (var)), var,
489 imag_p ? "CI" : "CR",
490 imag_p ? "$imag" : "$real",
491 imag_p ? IMAGPART_EXPR : REALPART_EXPR);
492 cvc_insert (decl_index, ret);
495 return ret;
498 /* Retrieve a value for a complex component of SSA_NAME. */
500 static tree
501 get_component_ssa_name (tree ssa_name, bool imag_p)
503 complex_lattice_t lattice = find_lattice_value (ssa_name);
504 size_t ssa_name_index;
505 tree ret;
507 if (lattice == (imag_p ? ONLY_REAL : ONLY_IMAG))
509 tree inner_type = TREE_TYPE (TREE_TYPE (ssa_name));
510 if (SCALAR_FLOAT_TYPE_P (inner_type))
511 return build_real (inner_type, dconst0);
512 else
513 return build_int_cst (inner_type, 0);
516 ssa_name_index = SSA_NAME_VERSION (ssa_name) * 2 + imag_p;
517 ret = complex_ssa_name_components[ssa_name_index];
518 if (ret == NULL)
520 if (SSA_NAME_VAR (ssa_name))
521 ret = get_component_var (SSA_NAME_VAR (ssa_name), imag_p);
522 else
523 ret = TREE_TYPE (TREE_TYPE (ssa_name));
524 ret = make_ssa_name (ret);
526 /* Copy some properties from the original. In particular, whether it
527 is used in an abnormal phi, and whether it's uninitialized. */
528 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ret)
529 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name);
530 if (SSA_NAME_IS_DEFAULT_DEF (ssa_name)
531 && TREE_CODE (SSA_NAME_VAR (ssa_name)) == VAR_DECL)
533 SSA_NAME_DEF_STMT (ret) = SSA_NAME_DEF_STMT (ssa_name);
534 set_ssa_default_def (cfun, SSA_NAME_VAR (ret), ret);
537 complex_ssa_name_components[ssa_name_index] = ret;
540 return ret;
543 /* Set a value for a complex component of SSA_NAME, return a
544 gimple_seq of stuff that needs doing. */
546 static gimple_seq
547 set_component_ssa_name (tree ssa_name, bool imag_p, tree value)
549 complex_lattice_t lattice = find_lattice_value (ssa_name);
550 size_t ssa_name_index;
551 tree comp;
552 gimple last;
553 gimple_seq list;
555 /* We know the value must be zero, else there's a bug in our lattice
556 analysis. But the value may well be a variable known to contain
557 zero. We should be safe ignoring it. */
558 if (lattice == (imag_p ? ONLY_REAL : ONLY_IMAG))
559 return NULL;
561 /* If we've already assigned an SSA_NAME to this component, then this
562 means that our walk of the basic blocks found a use before the set.
563 This is fine. Now we should create an initialization for the value
564 we created earlier. */
565 ssa_name_index = SSA_NAME_VERSION (ssa_name) * 2 + imag_p;
566 comp = complex_ssa_name_components[ssa_name_index];
567 if (comp)
570 /* If we've nothing assigned, and the value we're given is already stable,
571 then install that as the value for this SSA_NAME. This preemptively
572 copy-propagates the value, which avoids unnecessary memory allocation. */
573 else if (is_gimple_min_invariant (value)
574 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name))
576 complex_ssa_name_components[ssa_name_index] = value;
577 return NULL;
579 else if (TREE_CODE (value) == SSA_NAME
580 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name))
582 /* Replace an anonymous base value with the variable from cvc_lookup.
583 This should result in better debug info. */
584 if (SSA_NAME_VAR (ssa_name)
585 && (!SSA_NAME_VAR (value) || DECL_IGNORED_P (SSA_NAME_VAR (value)))
586 && !DECL_IGNORED_P (SSA_NAME_VAR (ssa_name)))
588 comp = get_component_var (SSA_NAME_VAR (ssa_name), imag_p);
589 replace_ssa_name_symbol (value, comp);
592 complex_ssa_name_components[ssa_name_index] = value;
593 return NULL;
596 /* Finally, we need to stabilize the result by installing the value into
597 a new ssa name. */
598 else
599 comp = get_component_ssa_name (ssa_name, imag_p);
601 /* Do all the work to assign VALUE to COMP. */
602 list = NULL;
603 value = force_gimple_operand (value, &list, false, NULL);
604 last = gimple_build_assign (comp, value);
605 gimple_seq_add_stmt (&list, last);
606 gcc_assert (SSA_NAME_DEF_STMT (comp) == last);
608 return list;
611 /* Extract the real or imaginary part of a complex variable or constant.
612 Make sure that it's a proper gimple_val and gimplify it if not.
613 Emit any new code before gsi. */
615 static tree
616 extract_component (gimple_stmt_iterator *gsi, tree t, bool imagpart_p,
617 bool gimple_p)
619 switch (TREE_CODE (t))
621 case COMPLEX_CST:
622 return imagpart_p ? TREE_IMAGPART (t) : TREE_REALPART (t);
624 case COMPLEX_EXPR:
625 gcc_unreachable ();
627 case VAR_DECL:
628 case RESULT_DECL:
629 case PARM_DECL:
630 case COMPONENT_REF:
631 case ARRAY_REF:
632 case VIEW_CONVERT_EXPR:
633 case MEM_REF:
635 tree inner_type = TREE_TYPE (TREE_TYPE (t));
637 t = build1 ((imagpart_p ? IMAGPART_EXPR : REALPART_EXPR),
638 inner_type, unshare_expr (t));
640 if (gimple_p)
641 t = force_gimple_operand_gsi (gsi, t, true, NULL, true,
642 GSI_SAME_STMT);
644 return t;
647 case SSA_NAME:
648 return get_component_ssa_name (t, imagpart_p);
650 default:
651 gcc_unreachable ();
655 /* Update the complex components of the ssa name on the lhs of STMT. */
657 static void
658 update_complex_components (gimple_stmt_iterator *gsi, gimple stmt, tree r,
659 tree i)
661 tree lhs;
662 gimple_seq list;
664 lhs = gimple_get_lhs (stmt);
666 list = set_component_ssa_name (lhs, false, r);
667 if (list)
668 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
670 list = set_component_ssa_name (lhs, true, i);
671 if (list)
672 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
675 static void
676 update_complex_components_on_edge (edge e, tree lhs, tree r, tree i)
678 gimple_seq list;
680 list = set_component_ssa_name (lhs, false, r);
681 if (list)
682 gsi_insert_seq_on_edge (e, list);
684 list = set_component_ssa_name (lhs, true, i);
685 if (list)
686 gsi_insert_seq_on_edge (e, list);
690 /* Update an assignment to a complex variable in place. */
692 static void
693 update_complex_assignment (gimple_stmt_iterator *gsi, tree r, tree i)
695 gimple stmt;
697 gimple_assign_set_rhs_with_ops (gsi, COMPLEX_EXPR, r, i);
698 stmt = gsi_stmt (*gsi);
699 update_stmt (stmt);
700 if (maybe_clean_eh_stmt (stmt))
701 gimple_purge_dead_eh_edges (gimple_bb (stmt));
703 if (gimple_in_ssa_p (cfun))
704 update_complex_components (gsi, gsi_stmt (*gsi), r, i);
708 /* Generate code at the entry point of the function to initialize the
709 component variables for a complex parameter. */
711 static void
712 update_parameter_components (void)
714 edge entry_edge = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun));
715 tree parm;
717 for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = DECL_CHAIN (parm))
719 tree type = TREE_TYPE (parm);
720 tree ssa_name, r, i;
722 if (TREE_CODE (type) != COMPLEX_TYPE || !is_gimple_reg (parm))
723 continue;
725 type = TREE_TYPE (type);
726 ssa_name = ssa_default_def (cfun, parm);
727 if (!ssa_name)
728 continue;
730 r = build1 (REALPART_EXPR, type, ssa_name);
731 i = build1 (IMAGPART_EXPR, type, ssa_name);
732 update_complex_components_on_edge (entry_edge, ssa_name, r, i);
736 /* Generate code to set the component variables of a complex variable
737 to match the PHI statements in block BB. */
739 static void
740 update_phi_components (basic_block bb)
742 gphi_iterator gsi;
744 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
746 gphi *phi = gsi.phi ();
748 if (is_complex_reg (gimple_phi_result (phi)))
750 tree lr, li;
751 gimple pr = NULL, pi = NULL;
752 unsigned int i, n;
754 lr = get_component_ssa_name (gimple_phi_result (phi), false);
755 if (TREE_CODE (lr) == SSA_NAME)
756 pr = create_phi_node (lr, bb);
758 li = get_component_ssa_name (gimple_phi_result (phi), true);
759 if (TREE_CODE (li) == SSA_NAME)
760 pi = create_phi_node (li, bb);
762 for (i = 0, n = gimple_phi_num_args (phi); i < n; ++i)
764 tree comp, arg = gimple_phi_arg_def (phi, i);
765 if (pr)
767 comp = extract_component (NULL, arg, false, false);
768 SET_PHI_ARG_DEF (pr, i, comp);
770 if (pi)
772 comp = extract_component (NULL, arg, true, false);
773 SET_PHI_ARG_DEF (pi, i, comp);
780 /* Expand a complex move to scalars. */
782 static void
783 expand_complex_move (gimple_stmt_iterator *gsi, tree type)
785 tree inner_type = TREE_TYPE (type);
786 tree r, i, lhs, rhs;
787 gimple stmt = gsi_stmt (*gsi);
789 if (is_gimple_assign (stmt))
791 lhs = gimple_assign_lhs (stmt);
792 if (gimple_num_ops (stmt) == 2)
793 rhs = gimple_assign_rhs1 (stmt);
794 else
795 rhs = NULL_TREE;
797 else if (is_gimple_call (stmt))
799 lhs = gimple_call_lhs (stmt);
800 rhs = NULL_TREE;
802 else
803 gcc_unreachable ();
805 if (TREE_CODE (lhs) == SSA_NAME)
807 if (is_ctrl_altering_stmt (stmt))
809 edge e;
811 /* The value is not assigned on the exception edges, so we need not
812 concern ourselves there. We do need to update on the fallthru
813 edge. Find it. */
814 e = find_fallthru_edge (gsi_bb (*gsi)->succs);
815 if (!e)
816 gcc_unreachable ();
818 r = build1 (REALPART_EXPR, inner_type, lhs);
819 i = build1 (IMAGPART_EXPR, inner_type, lhs);
820 update_complex_components_on_edge (e, lhs, r, i);
822 else if (is_gimple_call (stmt)
823 || gimple_has_side_effects (stmt)
824 || gimple_assign_rhs_code (stmt) == PAREN_EXPR)
826 r = build1 (REALPART_EXPR, inner_type, lhs);
827 i = build1 (IMAGPART_EXPR, inner_type, lhs);
828 update_complex_components (gsi, stmt, r, i);
830 else
832 if (gimple_assign_rhs_code (stmt) != COMPLEX_EXPR)
834 r = extract_component (gsi, rhs, 0, true);
835 i = extract_component (gsi, rhs, 1, true);
837 else
839 r = gimple_assign_rhs1 (stmt);
840 i = gimple_assign_rhs2 (stmt);
842 update_complex_assignment (gsi, r, i);
845 else if (rhs && TREE_CODE (rhs) == SSA_NAME && !TREE_SIDE_EFFECTS (lhs))
847 tree x;
848 gimple t;
849 location_t loc;
851 loc = gimple_location (stmt);
852 r = extract_component (gsi, rhs, 0, false);
853 i = extract_component (gsi, rhs, 1, false);
855 x = build1 (REALPART_EXPR, inner_type, unshare_expr (lhs));
856 t = gimple_build_assign (x, r);
857 gimple_set_location (t, loc);
858 gsi_insert_before (gsi, t, GSI_SAME_STMT);
860 if (stmt == gsi_stmt (*gsi))
862 x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
863 gimple_assign_set_lhs (stmt, x);
864 gimple_assign_set_rhs1 (stmt, i);
866 else
868 x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
869 t = gimple_build_assign (x, i);
870 gimple_set_location (t, loc);
871 gsi_insert_before (gsi, t, GSI_SAME_STMT);
873 stmt = gsi_stmt (*gsi);
874 gcc_assert (gimple_code (stmt) == GIMPLE_RETURN);
875 gimple_return_set_retval (as_a <greturn *> (stmt), lhs);
878 update_stmt (stmt);
882 /* Expand complex addition to scalars:
883 a + b = (ar + br) + i(ai + bi)
884 a - b = (ar - br) + i(ai + bi)
887 static void
888 expand_complex_addition (gimple_stmt_iterator *gsi, tree inner_type,
889 tree ar, tree ai, tree br, tree bi,
890 enum tree_code code,
891 complex_lattice_t al, complex_lattice_t bl)
893 tree rr, ri;
895 switch (PAIR (al, bl))
897 case PAIR (ONLY_REAL, ONLY_REAL):
898 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
899 ri = ai;
900 break;
902 case PAIR (ONLY_REAL, ONLY_IMAG):
903 rr = ar;
904 if (code == MINUS_EXPR)
905 ri = gimplify_build2 (gsi, MINUS_EXPR, inner_type, ai, bi);
906 else
907 ri = bi;
908 break;
910 case PAIR (ONLY_IMAG, ONLY_REAL):
911 if (code == MINUS_EXPR)
912 rr = gimplify_build2 (gsi, MINUS_EXPR, inner_type, ar, br);
913 else
914 rr = br;
915 ri = ai;
916 break;
918 case PAIR (ONLY_IMAG, ONLY_IMAG):
919 rr = ar;
920 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
921 break;
923 case PAIR (VARYING, ONLY_REAL):
924 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
925 ri = ai;
926 break;
928 case PAIR (VARYING, ONLY_IMAG):
929 rr = ar;
930 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
931 break;
933 case PAIR (ONLY_REAL, VARYING):
934 if (code == MINUS_EXPR)
935 goto general;
936 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
937 ri = bi;
938 break;
940 case PAIR (ONLY_IMAG, VARYING):
941 if (code == MINUS_EXPR)
942 goto general;
943 rr = br;
944 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
945 break;
947 case PAIR (VARYING, VARYING):
948 general:
949 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
950 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
951 break;
953 default:
954 gcc_unreachable ();
957 update_complex_assignment (gsi, rr, ri);
960 /* Expand a complex multiplication or division to a libcall to the c99
961 compliant routines. */
963 static void
964 expand_complex_libcall (gimple_stmt_iterator *gsi, tree ar, tree ai,
965 tree br, tree bi, enum tree_code code)
967 machine_mode mode;
968 enum built_in_function bcode;
969 tree fn, type, lhs;
970 gimple old_stmt;
971 gcall *stmt;
973 old_stmt = gsi_stmt (*gsi);
974 lhs = gimple_assign_lhs (old_stmt);
975 type = TREE_TYPE (lhs);
977 mode = TYPE_MODE (type);
978 gcc_assert (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT);
980 if (code == MULT_EXPR)
981 bcode = ((enum built_in_function)
982 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
983 else if (code == RDIV_EXPR)
984 bcode = ((enum built_in_function)
985 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
986 else
987 gcc_unreachable ();
988 fn = builtin_decl_explicit (bcode);
990 stmt = gimple_build_call (fn, 4, ar, ai, br, bi);
991 gimple_call_set_lhs (stmt, lhs);
992 update_stmt (stmt);
993 gsi_replace (gsi, stmt, false);
995 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
996 gimple_purge_dead_eh_edges (gsi_bb (*gsi));
998 if (gimple_in_ssa_p (cfun))
1000 type = TREE_TYPE (type);
1001 update_complex_components (gsi, stmt,
1002 build1 (REALPART_EXPR, type, lhs),
1003 build1 (IMAGPART_EXPR, type, lhs));
1004 SSA_NAME_DEF_STMT (lhs) = stmt;
1008 /* Expand complex multiplication to scalars:
1009 a * b = (ar*br - ai*bi) + i(ar*bi + br*ai)
1012 static void
1013 expand_complex_multiplication (gimple_stmt_iterator *gsi, tree inner_type,
1014 tree ar, tree ai, tree br, tree bi,
1015 complex_lattice_t al, complex_lattice_t bl)
1017 tree rr, ri;
1019 if (al < bl)
1021 complex_lattice_t tl;
1022 rr = ar, ar = br, br = rr;
1023 ri = ai, ai = bi, bi = ri;
1024 tl = al, al = bl, bl = tl;
1027 switch (PAIR (al, bl))
1029 case PAIR (ONLY_REAL, ONLY_REAL):
1030 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1031 ri = ai;
1032 break;
1034 case PAIR (ONLY_IMAG, ONLY_REAL):
1035 rr = ar;
1036 if (TREE_CODE (ai) == REAL_CST
1037 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (ai), dconst1))
1038 ri = br;
1039 else
1040 ri = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1041 break;
1043 case PAIR (ONLY_IMAG, ONLY_IMAG):
1044 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1045 rr = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, rr);
1046 ri = ar;
1047 break;
1049 case PAIR (VARYING, ONLY_REAL):
1050 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1051 ri = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1052 break;
1054 case PAIR (VARYING, ONLY_IMAG):
1055 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1056 rr = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, rr);
1057 ri = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, bi);
1058 break;
1060 case PAIR (VARYING, VARYING):
1061 if (flag_complex_method == 2 && SCALAR_FLOAT_TYPE_P (inner_type))
1063 expand_complex_libcall (gsi, ar, ai, br, bi, MULT_EXPR);
1064 return;
1066 else
1068 tree t1, t2, t3, t4;
1070 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1071 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1072 t3 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, bi);
1074 /* Avoid expanding redundant multiplication for the common
1075 case of squaring a complex number. */
1076 if (ar == br && ai == bi)
1077 t4 = t3;
1078 else
1079 t4 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1081 rr = gimplify_build2 (gsi, MINUS_EXPR, inner_type, t1, t2);
1082 ri = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t3, t4);
1084 break;
1086 default:
1087 gcc_unreachable ();
1090 update_complex_assignment (gsi, rr, ri);
1093 /* Keep this algorithm in sync with fold-const.c:const_binop().
1095 Expand complex division to scalars, straightforward algorithm.
1096 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1097 t = br*br + bi*bi
1100 static void
1101 expand_complex_div_straight (gimple_stmt_iterator *gsi, tree inner_type,
1102 tree ar, tree ai, tree br, tree bi,
1103 enum tree_code code)
1105 tree rr, ri, div, t1, t2, t3;
1107 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, br, br);
1108 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, bi, bi);
1109 div = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, t2);
1111 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1112 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1113 t3 = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, t2);
1114 rr = gimplify_build2 (gsi, code, inner_type, t3, div);
1116 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1117 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, bi);
1118 t3 = gimplify_build2 (gsi, MINUS_EXPR, inner_type, t1, t2);
1119 ri = gimplify_build2 (gsi, code, inner_type, t3, div);
1121 update_complex_assignment (gsi, rr, ri);
1124 /* Keep this algorithm in sync with fold-const.c:const_binop().
1126 Expand complex division to scalars, modified algorithm to minimize
1127 overflow with wide input ranges. */
1129 static void
1130 expand_complex_div_wide (gimple_stmt_iterator *gsi, tree inner_type,
1131 tree ar, tree ai, tree br, tree bi,
1132 enum tree_code code)
1134 tree rr, ri, ratio, div, t1, t2, tr, ti, compare;
1135 basic_block bb_cond, bb_true, bb_false, bb_join;
1136 gimple stmt;
1138 /* Examine |br| < |bi|, and branch. */
1139 t1 = gimplify_build1 (gsi, ABS_EXPR, inner_type, br);
1140 t2 = gimplify_build1 (gsi, ABS_EXPR, inner_type, bi);
1141 compare = fold_build2_loc (gimple_location (gsi_stmt (*gsi)),
1142 LT_EXPR, boolean_type_node, t1, t2);
1143 STRIP_NOPS (compare);
1145 bb_cond = bb_true = bb_false = bb_join = NULL;
1146 rr = ri = tr = ti = NULL;
1147 if (TREE_CODE (compare) != INTEGER_CST)
1149 edge e;
1150 gimple stmt;
1151 tree cond, tmp;
1153 tmp = create_tmp_var (boolean_type_node);
1154 stmt = gimple_build_assign (tmp, compare);
1155 if (gimple_in_ssa_p (cfun))
1157 tmp = make_ssa_name (tmp, stmt);
1158 gimple_assign_set_lhs (stmt, tmp);
1161 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1163 cond = fold_build2_loc (gimple_location (stmt),
1164 EQ_EXPR, boolean_type_node, tmp, boolean_true_node);
1165 stmt = gimple_build_cond_from_tree (cond, NULL_TREE, NULL_TREE);
1166 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1168 /* Split the original block, and create the TRUE and FALSE blocks. */
1169 e = split_block (gsi_bb (*gsi), stmt);
1170 bb_cond = e->src;
1171 bb_join = e->dest;
1172 bb_true = create_empty_bb (bb_cond);
1173 bb_false = create_empty_bb (bb_true);
1175 /* Wire the blocks together. */
1176 e->flags = EDGE_TRUE_VALUE;
1177 redirect_edge_succ (e, bb_true);
1178 make_edge (bb_cond, bb_false, EDGE_FALSE_VALUE);
1179 make_edge (bb_true, bb_join, EDGE_FALLTHRU);
1180 make_edge (bb_false, bb_join, EDGE_FALLTHRU);
1181 add_bb_to_loop (bb_true, bb_cond->loop_father);
1182 add_bb_to_loop (bb_false, bb_cond->loop_father);
1184 /* Update dominance info. Note that bb_join's data was
1185 updated by split_block. */
1186 if (dom_info_available_p (CDI_DOMINATORS))
1188 set_immediate_dominator (CDI_DOMINATORS, bb_true, bb_cond);
1189 set_immediate_dominator (CDI_DOMINATORS, bb_false, bb_cond);
1192 rr = create_tmp_reg (inner_type);
1193 ri = create_tmp_reg (inner_type);
1196 /* In the TRUE branch, we compute
1197 ratio = br/bi;
1198 div = (br * ratio) + bi;
1199 tr = (ar * ratio) + ai;
1200 ti = (ai * ratio) - ar;
1201 tr = tr / div;
1202 ti = ti / div; */
1203 if (bb_true || integer_nonzerop (compare))
1205 if (bb_true)
1207 *gsi = gsi_last_bb (bb_true);
1208 gsi_insert_after (gsi, gimple_build_nop (), GSI_NEW_STMT);
1211 ratio = gimplify_build2 (gsi, code, inner_type, br, bi);
1213 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, br, ratio);
1214 div = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, bi);
1216 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, ratio);
1217 tr = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, ai);
1219 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, ratio);
1220 ti = gimplify_build2 (gsi, MINUS_EXPR, inner_type, t1, ar);
1222 tr = gimplify_build2 (gsi, code, inner_type, tr, div);
1223 ti = gimplify_build2 (gsi, code, inner_type, ti, div);
1225 if (bb_true)
1227 stmt = gimple_build_assign (rr, tr);
1228 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1229 stmt = gimple_build_assign (ri, ti);
1230 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1231 gsi_remove (gsi, true);
1235 /* In the FALSE branch, we compute
1236 ratio = d/c;
1237 divisor = (d * ratio) + c;
1238 tr = (b * ratio) + a;
1239 ti = b - (a * ratio);
1240 tr = tr / div;
1241 ti = ti / div; */
1242 if (bb_false || integer_zerop (compare))
1244 if (bb_false)
1246 *gsi = gsi_last_bb (bb_false);
1247 gsi_insert_after (gsi, gimple_build_nop (), GSI_NEW_STMT);
1250 ratio = gimplify_build2 (gsi, code, inner_type, bi, br);
1252 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, bi, ratio);
1253 div = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, br);
1255 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, ratio);
1256 tr = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, ar);
1258 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, ratio);
1259 ti = gimplify_build2 (gsi, MINUS_EXPR, inner_type, ai, t1);
1261 tr = gimplify_build2 (gsi, code, inner_type, tr, div);
1262 ti = gimplify_build2 (gsi, code, inner_type, ti, div);
1264 if (bb_false)
1266 stmt = gimple_build_assign (rr, tr);
1267 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1268 stmt = gimple_build_assign (ri, ti);
1269 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1270 gsi_remove (gsi, true);
1274 if (bb_join)
1275 *gsi = gsi_start_bb (bb_join);
1276 else
1277 rr = tr, ri = ti;
1279 update_complex_assignment (gsi, rr, ri);
1282 /* Expand complex division to scalars. */
1284 static void
1285 expand_complex_division (gimple_stmt_iterator *gsi, tree inner_type,
1286 tree ar, tree ai, tree br, tree bi,
1287 enum tree_code code,
1288 complex_lattice_t al, complex_lattice_t bl)
1290 tree rr, ri;
1292 switch (PAIR (al, bl))
1294 case PAIR (ONLY_REAL, ONLY_REAL):
1295 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
1296 ri = ai;
1297 break;
1299 case PAIR (ONLY_REAL, ONLY_IMAG):
1300 rr = ai;
1301 ri = gimplify_build2 (gsi, code, inner_type, ar, bi);
1302 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ri);
1303 break;
1305 case PAIR (ONLY_IMAG, ONLY_REAL):
1306 rr = ar;
1307 ri = gimplify_build2 (gsi, code, inner_type, ai, br);
1308 break;
1310 case PAIR (ONLY_IMAG, ONLY_IMAG):
1311 rr = gimplify_build2 (gsi, code, inner_type, ai, bi);
1312 ri = ar;
1313 break;
1315 case PAIR (VARYING, ONLY_REAL):
1316 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
1317 ri = gimplify_build2 (gsi, code, inner_type, ai, br);
1318 break;
1320 case PAIR (VARYING, ONLY_IMAG):
1321 rr = gimplify_build2 (gsi, code, inner_type, ai, bi);
1322 ri = gimplify_build2 (gsi, code, inner_type, ar, bi);
1323 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ri);
1325 case PAIR (ONLY_REAL, VARYING):
1326 case PAIR (ONLY_IMAG, VARYING):
1327 case PAIR (VARYING, VARYING):
1328 switch (flag_complex_method)
1330 case 0:
1331 /* straightforward implementation of complex divide acceptable. */
1332 expand_complex_div_straight (gsi, inner_type, ar, ai, br, bi, code);
1333 break;
1335 case 2:
1336 if (SCALAR_FLOAT_TYPE_P (inner_type))
1338 expand_complex_libcall (gsi, ar, ai, br, bi, code);
1339 break;
1341 /* FALLTHRU */
1343 case 1:
1344 /* wide ranges of inputs must work for complex divide. */
1345 expand_complex_div_wide (gsi, inner_type, ar, ai, br, bi, code);
1346 break;
1348 default:
1349 gcc_unreachable ();
1351 return;
1353 default:
1354 gcc_unreachable ();
1357 update_complex_assignment (gsi, rr, ri);
1360 /* Expand complex negation to scalars:
1361 -a = (-ar) + i(-ai)
1364 static void
1365 expand_complex_negation (gimple_stmt_iterator *gsi, tree inner_type,
1366 tree ar, tree ai)
1368 tree rr, ri;
1370 rr = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ar);
1371 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ai);
1373 update_complex_assignment (gsi, rr, ri);
1376 /* Expand complex conjugate to scalars:
1377 ~a = (ar) + i(-ai)
1380 static void
1381 expand_complex_conjugate (gimple_stmt_iterator *gsi, tree inner_type,
1382 tree ar, tree ai)
1384 tree ri;
1386 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ai);
1388 update_complex_assignment (gsi, ar, ri);
1391 /* Expand complex comparison (EQ or NE only). */
1393 static void
1394 expand_complex_comparison (gimple_stmt_iterator *gsi, tree ar, tree ai,
1395 tree br, tree bi, enum tree_code code)
1397 tree cr, ci, cc, type;
1398 gimple stmt;
1400 cr = gimplify_build2 (gsi, code, boolean_type_node, ar, br);
1401 ci = gimplify_build2 (gsi, code, boolean_type_node, ai, bi);
1402 cc = gimplify_build2 (gsi,
1403 (code == EQ_EXPR ? TRUTH_AND_EXPR : TRUTH_OR_EXPR),
1404 boolean_type_node, cr, ci);
1406 stmt = gsi_stmt (*gsi);
1408 switch (gimple_code (stmt))
1410 case GIMPLE_RETURN:
1412 greturn *return_stmt = as_a <greturn *> (stmt);
1413 type = TREE_TYPE (gimple_return_retval (return_stmt));
1414 gimple_return_set_retval (return_stmt, fold_convert (type, cc));
1416 break;
1418 case GIMPLE_ASSIGN:
1419 type = TREE_TYPE (gimple_assign_lhs (stmt));
1420 gimple_assign_set_rhs_from_tree (gsi, fold_convert (type, cc));
1421 stmt = gsi_stmt (*gsi);
1422 break;
1424 case GIMPLE_COND:
1426 gcond *cond_stmt = as_a <gcond *> (stmt);
1427 gimple_cond_set_code (cond_stmt, EQ_EXPR);
1428 gimple_cond_set_lhs (cond_stmt, cc);
1429 gimple_cond_set_rhs (cond_stmt, boolean_true_node);
1431 break;
1433 default:
1434 gcc_unreachable ();
1437 update_stmt (stmt);
1440 /* Expand inline asm that sets some complex SSA_NAMEs. */
1442 static void
1443 expand_complex_asm (gimple_stmt_iterator *gsi)
1445 gasm *stmt = as_a <gasm *> (gsi_stmt (*gsi));
1446 unsigned int i;
1448 for (i = 0; i < gimple_asm_noutputs (stmt); ++i)
1450 tree link = gimple_asm_output_op (stmt, i);
1451 tree op = TREE_VALUE (link);
1452 if (TREE_CODE (op) == SSA_NAME
1453 && TREE_CODE (TREE_TYPE (op)) == COMPLEX_TYPE)
1455 tree type = TREE_TYPE (op);
1456 tree inner_type = TREE_TYPE (type);
1457 tree r = build1 (REALPART_EXPR, inner_type, op);
1458 tree i = build1 (IMAGPART_EXPR, inner_type, op);
1459 gimple_seq list = set_component_ssa_name (op, false, r);
1461 if (list)
1462 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
1464 list = set_component_ssa_name (op, true, i);
1465 if (list)
1466 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
1471 /* Process one statement. If we identify a complex operation, expand it. */
1473 static void
1474 expand_complex_operations_1 (gimple_stmt_iterator *gsi)
1476 gimple stmt = gsi_stmt (*gsi);
1477 tree type, inner_type, lhs;
1478 tree ac, ar, ai, bc, br, bi;
1479 complex_lattice_t al, bl;
1480 enum tree_code code;
1482 if (gimple_code (stmt) == GIMPLE_ASM)
1484 expand_complex_asm (gsi);
1485 return;
1488 lhs = gimple_get_lhs (stmt);
1489 if (!lhs && gimple_code (stmt) != GIMPLE_COND)
1490 return;
1492 type = TREE_TYPE (gimple_op (stmt, 0));
1493 code = gimple_expr_code (stmt);
1495 /* Initial filter for operations we handle. */
1496 switch (code)
1498 case PLUS_EXPR:
1499 case MINUS_EXPR:
1500 case MULT_EXPR:
1501 case TRUNC_DIV_EXPR:
1502 case CEIL_DIV_EXPR:
1503 case FLOOR_DIV_EXPR:
1504 case ROUND_DIV_EXPR:
1505 case RDIV_EXPR:
1506 case NEGATE_EXPR:
1507 case CONJ_EXPR:
1508 if (TREE_CODE (type) != COMPLEX_TYPE)
1509 return;
1510 inner_type = TREE_TYPE (type);
1511 break;
1513 case EQ_EXPR:
1514 case NE_EXPR:
1515 /* Note, both GIMPLE_ASSIGN and GIMPLE_COND may have an EQ_EXPR
1516 subcode, so we need to access the operands using gimple_op. */
1517 inner_type = TREE_TYPE (gimple_op (stmt, 1));
1518 if (TREE_CODE (inner_type) != COMPLEX_TYPE)
1519 return;
1520 break;
1522 default:
1524 tree rhs;
1526 /* GIMPLE_COND may also fallthru here, but we do not need to
1527 do anything with it. */
1528 if (gimple_code (stmt) == GIMPLE_COND)
1529 return;
1531 if (TREE_CODE (type) == COMPLEX_TYPE)
1532 expand_complex_move (gsi, type);
1533 else if (is_gimple_assign (stmt)
1534 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
1535 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
1536 && TREE_CODE (lhs) == SSA_NAME)
1538 rhs = gimple_assign_rhs1 (stmt);
1539 rhs = extract_component (gsi, TREE_OPERAND (rhs, 0),
1540 gimple_assign_rhs_code (stmt)
1541 == IMAGPART_EXPR,
1542 false);
1543 gimple_assign_set_rhs_from_tree (gsi, rhs);
1544 stmt = gsi_stmt (*gsi);
1545 update_stmt (stmt);
1548 return;
1551 /* Extract the components of the two complex values. Make sure and
1552 handle the common case of the same value used twice specially. */
1553 if (is_gimple_assign (stmt))
1555 ac = gimple_assign_rhs1 (stmt);
1556 bc = (gimple_num_ops (stmt) > 2) ? gimple_assign_rhs2 (stmt) : NULL;
1558 /* GIMPLE_CALL can not get here. */
1559 else
1561 ac = gimple_cond_lhs (stmt);
1562 bc = gimple_cond_rhs (stmt);
1565 ar = extract_component (gsi, ac, false, true);
1566 ai = extract_component (gsi, ac, true, true);
1568 if (ac == bc)
1569 br = ar, bi = ai;
1570 else if (bc)
1572 br = extract_component (gsi, bc, 0, true);
1573 bi = extract_component (gsi, bc, 1, true);
1575 else
1576 br = bi = NULL_TREE;
1578 if (gimple_in_ssa_p (cfun))
1580 al = find_lattice_value (ac);
1581 if (al == UNINITIALIZED)
1582 al = VARYING;
1584 if (TREE_CODE_CLASS (code) == tcc_unary)
1585 bl = UNINITIALIZED;
1586 else if (ac == bc)
1587 bl = al;
1588 else
1590 bl = find_lattice_value (bc);
1591 if (bl == UNINITIALIZED)
1592 bl = VARYING;
1595 else
1596 al = bl = VARYING;
1598 switch (code)
1600 case PLUS_EXPR:
1601 case MINUS_EXPR:
1602 expand_complex_addition (gsi, inner_type, ar, ai, br, bi, code, al, bl);
1603 break;
1605 case MULT_EXPR:
1606 expand_complex_multiplication (gsi, inner_type, ar, ai, br, bi, al, bl);
1607 break;
1609 case TRUNC_DIV_EXPR:
1610 case CEIL_DIV_EXPR:
1611 case FLOOR_DIV_EXPR:
1612 case ROUND_DIV_EXPR:
1613 case RDIV_EXPR:
1614 expand_complex_division (gsi, inner_type, ar, ai, br, bi, code, al, bl);
1615 break;
1617 case NEGATE_EXPR:
1618 expand_complex_negation (gsi, inner_type, ar, ai);
1619 break;
1621 case CONJ_EXPR:
1622 expand_complex_conjugate (gsi, inner_type, ar, ai);
1623 break;
1625 case EQ_EXPR:
1626 case NE_EXPR:
1627 expand_complex_comparison (gsi, ar, ai, br, bi, code);
1628 break;
1630 default:
1631 gcc_unreachable ();
1636 /* Entry point for complex operation lowering during optimization. */
1638 static unsigned int
1639 tree_lower_complex (void)
1641 int old_last_basic_block;
1642 gimple_stmt_iterator gsi;
1643 basic_block bb;
1645 if (!init_dont_simulate_again ())
1646 return 0;
1648 complex_lattice_values.create (num_ssa_names);
1649 complex_lattice_values.safe_grow_cleared (num_ssa_names);
1651 init_parameter_lattice_values ();
1652 ssa_propagate (complex_visit_stmt, complex_visit_phi);
1654 complex_variable_components = new int_tree_htab_type (10);
1656 complex_ssa_name_components.create (2 * num_ssa_names);
1657 complex_ssa_name_components.safe_grow_cleared (2 * num_ssa_names);
1659 update_parameter_components ();
1661 /* ??? Ideally we'd traverse the blocks in breadth-first order. */
1662 old_last_basic_block = last_basic_block_for_fn (cfun);
1663 FOR_EACH_BB_FN (bb, cfun)
1665 if (bb->index >= old_last_basic_block)
1666 continue;
1668 update_phi_components (bb);
1669 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1670 expand_complex_operations_1 (&gsi);
1673 gsi_commit_edge_inserts ();
1675 delete complex_variable_components;
1676 complex_variable_components = NULL;
1677 complex_ssa_name_components.release ();
1678 complex_lattice_values.release ();
1679 return 0;
1682 namespace {
1684 const pass_data pass_data_lower_complex =
1686 GIMPLE_PASS, /* type */
1687 "cplxlower", /* name */
1688 OPTGROUP_NONE, /* optinfo_flags */
1689 TV_NONE, /* tv_id */
1690 PROP_ssa, /* properties_required */
1691 PROP_gimple_lcx, /* properties_provided */
1692 0, /* properties_destroyed */
1693 0, /* todo_flags_start */
1694 TODO_update_ssa, /* todo_flags_finish */
1697 class pass_lower_complex : public gimple_opt_pass
1699 public:
1700 pass_lower_complex (gcc::context *ctxt)
1701 : gimple_opt_pass (pass_data_lower_complex, ctxt)
1704 /* opt_pass methods: */
1705 opt_pass * clone () { return new pass_lower_complex (m_ctxt); }
1706 virtual unsigned int execute (function *) { return tree_lower_complex (); }
1708 }; // class pass_lower_complex
1710 } // anon namespace
1712 gimple_opt_pass *
1713 make_pass_lower_complex (gcc::context *ctxt)
1715 return new pass_lower_complex (ctxt);
1719 namespace {
1721 const pass_data pass_data_lower_complex_O0 =
1723 GIMPLE_PASS, /* type */
1724 "cplxlower0", /* name */
1725 OPTGROUP_NONE, /* optinfo_flags */
1726 TV_NONE, /* tv_id */
1727 PROP_cfg, /* properties_required */
1728 PROP_gimple_lcx, /* properties_provided */
1729 0, /* properties_destroyed */
1730 0, /* todo_flags_start */
1731 TODO_update_ssa, /* todo_flags_finish */
1734 class pass_lower_complex_O0 : public gimple_opt_pass
1736 public:
1737 pass_lower_complex_O0 (gcc::context *ctxt)
1738 : gimple_opt_pass (pass_data_lower_complex_O0, ctxt)
1741 /* opt_pass methods: */
1742 virtual bool gate (function *fun)
1744 /* With errors, normal optimization passes are not run. If we don't
1745 lower complex operations at all, rtl expansion will abort. */
1746 return !(fun->curr_properties & PROP_gimple_lcx);
1749 virtual unsigned int execute (function *) { return tree_lower_complex (); }
1751 }; // class pass_lower_complex_O0
1753 } // anon namespace
1755 gimple_opt_pass *
1756 make_pass_lower_complex_O0 (gcc::context *ctxt)
1758 return new pass_lower_complex_O0 (ctxt);