Concretize gimple_cond_set_{lhs|rhs}
[official-gcc.git] / gcc / tree-complex.c
blob6f6fef59c7a0db2c2e6c3ac35fbe82ab444accf6
1 /* Lower complex number operations to scalar operations.
2 Copyright (C) 2004-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "stor-layout.h"
26 #include "flags.h"
27 #include "basic-block.h"
28 #include "tree-ssa-alias.h"
29 #include "internal-fn.h"
30 #include "tree-eh.h"
31 #include "gimple-expr.h"
32 #include "is-a.h"
33 #include "gimple.h"
34 #include "gimplify.h"
35 #include "gimple-iterator.h"
36 #include "gimplify-me.h"
37 #include "gimple-ssa.h"
38 #include "tree-cfg.h"
39 #include "tree-phinodes.h"
40 #include "ssa-iterators.h"
41 #include "stringpool.h"
42 #include "tree-ssanames.h"
43 #include "expr.h"
44 #include "tree-dfa.h"
45 #include "tree-ssa.h"
46 #include "tree-iterator.h"
47 #include "tree-pass.h"
48 #include "tree-ssa-propagate.h"
49 #include "tree-hasher.h"
50 #include "cfgloop.h"
53 /* For each complex ssa name, a lattice value. We're interested in finding
54 out whether a complex number is degenerate in some way, having only real
55 or only complex parts. */
57 enum
59 UNINITIALIZED = 0,
60 ONLY_REAL = 1,
61 ONLY_IMAG = 2,
62 VARYING = 3
65 /* The type complex_lattice_t holds combinations of the above
66 constants. */
67 typedef int complex_lattice_t;
69 #define PAIR(a, b) ((a) << 2 | (b))
72 static vec<complex_lattice_t> complex_lattice_values;
74 /* For each complex variable, a pair of variables for the components exists in
75 the hashtable. */
76 static int_tree_htab_type *complex_variable_components;
78 /* For each complex SSA_NAME, a pair of ssa names for the components. */
79 static vec<tree> complex_ssa_name_components;
81 /* Lookup UID in the complex_variable_components hashtable and return the
82 associated tree. */
83 static tree
84 cvc_lookup (unsigned int uid)
86 struct int_tree_map in;
87 in.uid = uid;
88 return complex_variable_components->find_with_hash (in, uid).to;
91 /* Insert the pair UID, TO into the complex_variable_components hashtable. */
93 static void
94 cvc_insert (unsigned int uid, tree to)
96 int_tree_map h;
97 int_tree_map *loc;
99 h.uid = uid;
100 loc = complex_variable_components->find_slot_with_hash (h, uid, INSERT);
101 loc->uid = uid;
102 loc->to = to;
105 /* Return true if T is not a zero constant. In the case of real values,
106 we're only interested in +0.0. */
108 static int
109 some_nonzerop (tree t)
111 int zerop = false;
113 /* Operations with real or imaginary part of a complex number zero
114 cannot be treated the same as operations with a real or imaginary
115 operand if we care about the signs of zeros in the result. */
116 if (TREE_CODE (t) == REAL_CST && !flag_signed_zeros)
117 zerop = REAL_VALUES_IDENTICAL (TREE_REAL_CST (t), dconst0);
118 else if (TREE_CODE (t) == FIXED_CST)
119 zerop = fixed_zerop (t);
120 else if (TREE_CODE (t) == INTEGER_CST)
121 zerop = integer_zerop (t);
123 return !zerop;
127 /* Compute a lattice value from the components of a complex type REAL
128 and IMAG. */
130 static complex_lattice_t
131 find_lattice_value_parts (tree real, tree imag)
133 int r, i;
134 complex_lattice_t ret;
136 r = some_nonzerop (real);
137 i = some_nonzerop (imag);
138 ret = r * ONLY_REAL + i * ONLY_IMAG;
140 /* ??? On occasion we could do better than mapping 0+0i to real, but we
141 certainly don't want to leave it UNINITIALIZED, which eventually gets
142 mapped to VARYING. */
143 if (ret == UNINITIALIZED)
144 ret = ONLY_REAL;
146 return ret;
150 /* Compute a lattice value from gimple_val T. */
152 static complex_lattice_t
153 find_lattice_value (tree t)
155 tree real, imag;
157 switch (TREE_CODE (t))
159 case SSA_NAME:
160 return complex_lattice_values[SSA_NAME_VERSION (t)];
162 case COMPLEX_CST:
163 real = TREE_REALPART (t);
164 imag = TREE_IMAGPART (t);
165 break;
167 default:
168 gcc_unreachable ();
171 return find_lattice_value_parts (real, imag);
174 /* Determine if LHS is something for which we're interested in seeing
175 simulation results. */
177 static bool
178 is_complex_reg (tree lhs)
180 return TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE && is_gimple_reg (lhs);
183 /* Mark the incoming parameters to the function as VARYING. */
185 static void
186 init_parameter_lattice_values (void)
188 tree parm, ssa_name;
190 for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = DECL_CHAIN (parm))
191 if (is_complex_reg (parm)
192 && (ssa_name = ssa_default_def (cfun, parm)) != NULL_TREE)
193 complex_lattice_values[SSA_NAME_VERSION (ssa_name)] = VARYING;
196 /* Initialize simulation state for each statement. Return false if we
197 found no statements we want to simulate, and thus there's nothing
198 for the entire pass to do. */
200 static bool
201 init_dont_simulate_again (void)
203 basic_block bb;
204 gimple_stmt_iterator gsi;
205 gimple phi;
206 bool saw_a_complex_op = false;
208 FOR_EACH_BB_FN (bb, cfun)
210 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
212 phi = gsi_stmt (gsi);
213 prop_set_simulate_again (phi,
214 is_complex_reg (gimple_phi_result (phi)));
217 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
219 gimple stmt;
220 tree op0, op1;
221 bool sim_again_p;
223 stmt = gsi_stmt (gsi);
224 op0 = op1 = NULL_TREE;
226 /* Most control-altering statements must be initially
227 simulated, else we won't cover the entire cfg. */
228 sim_again_p = stmt_ends_bb_p (stmt);
230 switch (gimple_code (stmt))
232 case GIMPLE_CALL:
233 if (gimple_call_lhs (stmt))
234 sim_again_p = is_complex_reg (gimple_call_lhs (stmt));
235 break;
237 case GIMPLE_ASSIGN:
238 sim_again_p = is_complex_reg (gimple_assign_lhs (stmt));
239 if (gimple_assign_rhs_code (stmt) == REALPART_EXPR
240 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
241 op0 = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
242 else
243 op0 = gimple_assign_rhs1 (stmt);
244 if (gimple_num_ops (stmt) > 2)
245 op1 = gimple_assign_rhs2 (stmt);
246 break;
248 case GIMPLE_COND:
249 op0 = gimple_cond_lhs (stmt);
250 op1 = gimple_cond_rhs (stmt);
251 break;
253 default:
254 break;
257 if (op0 || op1)
258 switch (gimple_expr_code (stmt))
260 case EQ_EXPR:
261 case NE_EXPR:
262 case PLUS_EXPR:
263 case MINUS_EXPR:
264 case MULT_EXPR:
265 case TRUNC_DIV_EXPR:
266 case CEIL_DIV_EXPR:
267 case FLOOR_DIV_EXPR:
268 case ROUND_DIV_EXPR:
269 case RDIV_EXPR:
270 if (TREE_CODE (TREE_TYPE (op0)) == COMPLEX_TYPE
271 || TREE_CODE (TREE_TYPE (op1)) == COMPLEX_TYPE)
272 saw_a_complex_op = true;
273 break;
275 case NEGATE_EXPR:
276 case CONJ_EXPR:
277 if (TREE_CODE (TREE_TYPE (op0)) == COMPLEX_TYPE)
278 saw_a_complex_op = true;
279 break;
281 case REALPART_EXPR:
282 case IMAGPART_EXPR:
283 /* The total store transformation performed during
284 gimplification creates such uninitialized loads
285 and we need to lower the statement to be able
286 to fix things up. */
287 if (TREE_CODE (op0) == SSA_NAME
288 && ssa_undefined_value_p (op0))
289 saw_a_complex_op = true;
290 break;
292 default:
293 break;
296 prop_set_simulate_again (stmt, sim_again_p);
300 return saw_a_complex_op;
304 /* Evaluate statement STMT against the complex lattice defined above. */
306 static enum ssa_prop_result
307 complex_visit_stmt (gimple stmt, edge *taken_edge_p ATTRIBUTE_UNUSED,
308 tree *result_p)
310 complex_lattice_t new_l, old_l, op1_l, op2_l;
311 unsigned int ver;
312 tree lhs;
314 lhs = gimple_get_lhs (stmt);
315 /* Skip anything but GIMPLE_ASSIGN and GIMPLE_CALL with a lhs. */
316 if (!lhs)
317 return SSA_PROP_VARYING;
319 /* These conditions should be satisfied due to the initial filter
320 set up in init_dont_simulate_again. */
321 gcc_assert (TREE_CODE (lhs) == SSA_NAME);
322 gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE);
324 *result_p = lhs;
325 ver = SSA_NAME_VERSION (lhs);
326 old_l = complex_lattice_values[ver];
328 switch (gimple_expr_code (stmt))
330 case SSA_NAME:
331 case COMPLEX_CST:
332 new_l = find_lattice_value (gimple_assign_rhs1 (stmt));
333 break;
335 case COMPLEX_EXPR:
336 new_l = find_lattice_value_parts (gimple_assign_rhs1 (stmt),
337 gimple_assign_rhs2 (stmt));
338 break;
340 case PLUS_EXPR:
341 case MINUS_EXPR:
342 op1_l = find_lattice_value (gimple_assign_rhs1 (stmt));
343 op2_l = find_lattice_value (gimple_assign_rhs2 (stmt));
345 /* We've set up the lattice values such that IOR neatly
346 models addition. */
347 new_l = op1_l | op2_l;
348 break;
350 case MULT_EXPR:
351 case RDIV_EXPR:
352 case TRUNC_DIV_EXPR:
353 case CEIL_DIV_EXPR:
354 case FLOOR_DIV_EXPR:
355 case ROUND_DIV_EXPR:
356 op1_l = find_lattice_value (gimple_assign_rhs1 (stmt));
357 op2_l = find_lattice_value (gimple_assign_rhs2 (stmt));
359 /* Obviously, if either varies, so does the result. */
360 if (op1_l == VARYING || op2_l == VARYING)
361 new_l = VARYING;
362 /* Don't prematurely promote variables if we've not yet seen
363 their inputs. */
364 else if (op1_l == UNINITIALIZED)
365 new_l = op2_l;
366 else if (op2_l == UNINITIALIZED)
367 new_l = op1_l;
368 else
370 /* At this point both numbers have only one component. If the
371 numbers are of opposite kind, the result is imaginary,
372 otherwise the result is real. The add/subtract translates
373 the real/imag from/to 0/1; the ^ performs the comparison. */
374 new_l = ((op1_l - ONLY_REAL) ^ (op2_l - ONLY_REAL)) + ONLY_REAL;
376 /* Don't allow the lattice value to flip-flop indefinitely. */
377 new_l |= old_l;
379 break;
381 case NEGATE_EXPR:
382 case CONJ_EXPR:
383 new_l = find_lattice_value (gimple_assign_rhs1 (stmt));
384 break;
386 default:
387 new_l = VARYING;
388 break;
391 /* If nothing changed this round, let the propagator know. */
392 if (new_l == old_l)
393 return SSA_PROP_NOT_INTERESTING;
395 complex_lattice_values[ver] = new_l;
396 return new_l == VARYING ? SSA_PROP_VARYING : SSA_PROP_INTERESTING;
399 /* Evaluate a PHI node against the complex lattice defined above. */
401 static enum ssa_prop_result
402 complex_visit_phi (gimple_phi phi)
404 complex_lattice_t new_l, old_l;
405 unsigned int ver;
406 tree lhs;
407 int i;
409 lhs = gimple_phi_result (phi);
411 /* This condition should be satisfied due to the initial filter
412 set up in init_dont_simulate_again. */
413 gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE);
415 /* We've set up the lattice values such that IOR neatly models PHI meet. */
416 new_l = UNINITIALIZED;
417 for (i = gimple_phi_num_args (phi) - 1; i >= 0; --i)
418 new_l |= find_lattice_value (gimple_phi_arg_def (phi, i));
420 ver = SSA_NAME_VERSION (lhs);
421 old_l = complex_lattice_values[ver];
423 if (new_l == old_l)
424 return SSA_PROP_NOT_INTERESTING;
426 complex_lattice_values[ver] = new_l;
427 return new_l == VARYING ? SSA_PROP_VARYING : SSA_PROP_INTERESTING;
430 /* Create one backing variable for a complex component of ORIG. */
432 static tree
433 create_one_component_var (tree type, tree orig, const char *prefix,
434 const char *suffix, enum tree_code code)
436 tree r = create_tmp_var (type, prefix);
438 DECL_SOURCE_LOCATION (r) = DECL_SOURCE_LOCATION (orig);
439 DECL_ARTIFICIAL (r) = 1;
441 if (DECL_NAME (orig) && !DECL_IGNORED_P (orig))
443 const char *name = IDENTIFIER_POINTER (DECL_NAME (orig));
445 DECL_NAME (r) = get_identifier (ACONCAT ((name, suffix, NULL)));
447 SET_DECL_DEBUG_EXPR (r, build1 (code, type, orig));
448 DECL_HAS_DEBUG_EXPR_P (r) = 1;
449 DECL_IGNORED_P (r) = 0;
450 TREE_NO_WARNING (r) = TREE_NO_WARNING (orig);
452 else
454 DECL_IGNORED_P (r) = 1;
455 TREE_NO_WARNING (r) = 1;
458 return r;
461 /* Retrieve a value for a complex component of VAR. */
463 static tree
464 get_component_var (tree var, bool imag_p)
466 size_t decl_index = DECL_UID (var) * 2 + imag_p;
467 tree ret = cvc_lookup (decl_index);
469 if (ret == NULL)
471 ret = create_one_component_var (TREE_TYPE (TREE_TYPE (var)), var,
472 imag_p ? "CI" : "CR",
473 imag_p ? "$imag" : "$real",
474 imag_p ? IMAGPART_EXPR : REALPART_EXPR);
475 cvc_insert (decl_index, ret);
478 return ret;
481 /* Retrieve a value for a complex component of SSA_NAME. */
483 static tree
484 get_component_ssa_name (tree ssa_name, bool imag_p)
486 complex_lattice_t lattice = find_lattice_value (ssa_name);
487 size_t ssa_name_index;
488 tree ret;
490 if (lattice == (imag_p ? ONLY_REAL : ONLY_IMAG))
492 tree inner_type = TREE_TYPE (TREE_TYPE (ssa_name));
493 if (SCALAR_FLOAT_TYPE_P (inner_type))
494 return build_real (inner_type, dconst0);
495 else
496 return build_int_cst (inner_type, 0);
499 ssa_name_index = SSA_NAME_VERSION (ssa_name) * 2 + imag_p;
500 ret = complex_ssa_name_components[ssa_name_index];
501 if (ret == NULL)
503 if (SSA_NAME_VAR (ssa_name))
504 ret = get_component_var (SSA_NAME_VAR (ssa_name), imag_p);
505 else
506 ret = TREE_TYPE (TREE_TYPE (ssa_name));
507 ret = make_ssa_name (ret, NULL);
509 /* Copy some properties from the original. In particular, whether it
510 is used in an abnormal phi, and whether it's uninitialized. */
511 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ret)
512 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name);
513 if (SSA_NAME_IS_DEFAULT_DEF (ssa_name)
514 && TREE_CODE (SSA_NAME_VAR (ssa_name)) == VAR_DECL)
516 SSA_NAME_DEF_STMT (ret) = SSA_NAME_DEF_STMT (ssa_name);
517 set_ssa_default_def (cfun, SSA_NAME_VAR (ret), ret);
520 complex_ssa_name_components[ssa_name_index] = ret;
523 return ret;
526 /* Set a value for a complex component of SSA_NAME, return a
527 gimple_seq of stuff that needs doing. */
529 static gimple_seq
530 set_component_ssa_name (tree ssa_name, bool imag_p, tree value)
532 complex_lattice_t lattice = find_lattice_value (ssa_name);
533 size_t ssa_name_index;
534 tree comp;
535 gimple last;
536 gimple_seq list;
538 /* We know the value must be zero, else there's a bug in our lattice
539 analysis. But the value may well be a variable known to contain
540 zero. We should be safe ignoring it. */
541 if (lattice == (imag_p ? ONLY_REAL : ONLY_IMAG))
542 return NULL;
544 /* If we've already assigned an SSA_NAME to this component, then this
545 means that our walk of the basic blocks found a use before the set.
546 This is fine. Now we should create an initialization for the value
547 we created earlier. */
548 ssa_name_index = SSA_NAME_VERSION (ssa_name) * 2 + imag_p;
549 comp = complex_ssa_name_components[ssa_name_index];
550 if (comp)
553 /* If we've nothing assigned, and the value we're given is already stable,
554 then install that as the value for this SSA_NAME. This preemptively
555 copy-propagates the value, which avoids unnecessary memory allocation. */
556 else if (is_gimple_min_invariant (value)
557 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name))
559 complex_ssa_name_components[ssa_name_index] = value;
560 return NULL;
562 else if (TREE_CODE (value) == SSA_NAME
563 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name))
565 /* Replace an anonymous base value with the variable from cvc_lookup.
566 This should result in better debug info. */
567 if (SSA_NAME_VAR (ssa_name)
568 && (!SSA_NAME_VAR (value) || DECL_IGNORED_P (SSA_NAME_VAR (value)))
569 && !DECL_IGNORED_P (SSA_NAME_VAR (ssa_name)))
571 comp = get_component_var (SSA_NAME_VAR (ssa_name), imag_p);
572 replace_ssa_name_symbol (value, comp);
575 complex_ssa_name_components[ssa_name_index] = value;
576 return NULL;
579 /* Finally, we need to stabilize the result by installing the value into
580 a new ssa name. */
581 else
582 comp = get_component_ssa_name (ssa_name, imag_p);
584 /* Do all the work to assign VALUE to COMP. */
585 list = NULL;
586 value = force_gimple_operand (value, &list, false, NULL);
587 last = gimple_build_assign (comp, value);
588 gimple_seq_add_stmt (&list, last);
589 gcc_assert (SSA_NAME_DEF_STMT (comp) == last);
591 return list;
594 /* Extract the real or imaginary part of a complex variable or constant.
595 Make sure that it's a proper gimple_val and gimplify it if not.
596 Emit any new code before gsi. */
598 static tree
599 extract_component (gimple_stmt_iterator *gsi, tree t, bool imagpart_p,
600 bool gimple_p)
602 switch (TREE_CODE (t))
604 case COMPLEX_CST:
605 return imagpart_p ? TREE_IMAGPART (t) : TREE_REALPART (t);
607 case COMPLEX_EXPR:
608 gcc_unreachable ();
610 case VAR_DECL:
611 case RESULT_DECL:
612 case PARM_DECL:
613 case COMPONENT_REF:
614 case ARRAY_REF:
615 case VIEW_CONVERT_EXPR:
616 case MEM_REF:
618 tree inner_type = TREE_TYPE (TREE_TYPE (t));
620 t = build1 ((imagpart_p ? IMAGPART_EXPR : REALPART_EXPR),
621 inner_type, unshare_expr (t));
623 if (gimple_p)
624 t = force_gimple_operand_gsi (gsi, t, true, NULL, true,
625 GSI_SAME_STMT);
627 return t;
630 case SSA_NAME:
631 return get_component_ssa_name (t, imagpart_p);
633 default:
634 gcc_unreachable ();
638 /* Update the complex components of the ssa name on the lhs of STMT. */
640 static void
641 update_complex_components (gimple_stmt_iterator *gsi, gimple stmt, tree r,
642 tree i)
644 tree lhs;
645 gimple_seq list;
647 lhs = gimple_get_lhs (stmt);
649 list = set_component_ssa_name (lhs, false, r);
650 if (list)
651 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
653 list = set_component_ssa_name (lhs, true, i);
654 if (list)
655 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
658 static void
659 update_complex_components_on_edge (edge e, tree lhs, tree r, tree i)
661 gimple_seq list;
663 list = set_component_ssa_name (lhs, false, r);
664 if (list)
665 gsi_insert_seq_on_edge (e, list);
667 list = set_component_ssa_name (lhs, true, i);
668 if (list)
669 gsi_insert_seq_on_edge (e, list);
673 /* Update an assignment to a complex variable in place. */
675 static void
676 update_complex_assignment (gimple_stmt_iterator *gsi, tree r, tree i)
678 gimple stmt;
680 gimple_assign_set_rhs_with_ops (gsi, COMPLEX_EXPR, r, i);
681 stmt = gsi_stmt (*gsi);
682 update_stmt (stmt);
683 if (maybe_clean_eh_stmt (stmt))
684 gimple_purge_dead_eh_edges (gimple_bb (stmt));
686 if (gimple_in_ssa_p (cfun))
687 update_complex_components (gsi, gsi_stmt (*gsi), r, i);
691 /* Generate code at the entry point of the function to initialize the
692 component variables for a complex parameter. */
694 static void
695 update_parameter_components (void)
697 edge entry_edge = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun));
698 tree parm;
700 for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = DECL_CHAIN (parm))
702 tree type = TREE_TYPE (parm);
703 tree ssa_name, r, i;
705 if (TREE_CODE (type) != COMPLEX_TYPE || !is_gimple_reg (parm))
706 continue;
708 type = TREE_TYPE (type);
709 ssa_name = ssa_default_def (cfun, parm);
710 if (!ssa_name)
711 continue;
713 r = build1 (REALPART_EXPR, type, ssa_name);
714 i = build1 (IMAGPART_EXPR, type, ssa_name);
715 update_complex_components_on_edge (entry_edge, ssa_name, r, i);
719 /* Generate code to set the component variables of a complex variable
720 to match the PHI statements in block BB. */
722 static void
723 update_phi_components (basic_block bb)
725 gimple_phi_iterator gsi;
727 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
729 gimple_phi phi = gsi.phi ();
731 if (is_complex_reg (gimple_phi_result (phi)))
733 tree lr, li;
734 gimple pr = NULL, pi = NULL;
735 unsigned int i, n;
737 lr = get_component_ssa_name (gimple_phi_result (phi), false);
738 if (TREE_CODE (lr) == SSA_NAME)
739 pr = create_phi_node (lr, bb);
741 li = get_component_ssa_name (gimple_phi_result (phi), true);
742 if (TREE_CODE (li) == SSA_NAME)
743 pi = create_phi_node (li, bb);
745 for (i = 0, n = gimple_phi_num_args (phi); i < n; ++i)
747 tree comp, arg = gimple_phi_arg_def (phi, i);
748 if (pr)
750 comp = extract_component (NULL, arg, false, false);
751 SET_PHI_ARG_DEF (pr, i, comp);
753 if (pi)
755 comp = extract_component (NULL, arg, true, false);
756 SET_PHI_ARG_DEF (pi, i, comp);
763 /* Expand a complex move to scalars. */
765 static void
766 expand_complex_move (gimple_stmt_iterator *gsi, tree type)
768 tree inner_type = TREE_TYPE (type);
769 tree r, i, lhs, rhs;
770 gimple stmt = gsi_stmt (*gsi);
772 if (is_gimple_assign (stmt))
774 lhs = gimple_assign_lhs (stmt);
775 if (gimple_num_ops (stmt) == 2)
776 rhs = gimple_assign_rhs1 (stmt);
777 else
778 rhs = NULL_TREE;
780 else if (is_gimple_call (stmt))
782 lhs = gimple_call_lhs (stmt);
783 rhs = NULL_TREE;
785 else
786 gcc_unreachable ();
788 if (TREE_CODE (lhs) == SSA_NAME)
790 if (is_ctrl_altering_stmt (stmt))
792 edge e;
794 /* The value is not assigned on the exception edges, so we need not
795 concern ourselves there. We do need to update on the fallthru
796 edge. Find it. */
797 e = find_fallthru_edge (gsi_bb (*gsi)->succs);
798 if (!e)
799 gcc_unreachable ();
801 r = build1 (REALPART_EXPR, inner_type, lhs);
802 i = build1 (IMAGPART_EXPR, inner_type, lhs);
803 update_complex_components_on_edge (e, lhs, r, i);
805 else if (is_gimple_call (stmt)
806 || gimple_has_side_effects (stmt)
807 || gimple_assign_rhs_code (stmt) == PAREN_EXPR)
809 r = build1 (REALPART_EXPR, inner_type, lhs);
810 i = build1 (IMAGPART_EXPR, inner_type, lhs);
811 update_complex_components (gsi, stmt, r, i);
813 else
815 if (gimple_assign_rhs_code (stmt) != COMPLEX_EXPR)
817 r = extract_component (gsi, rhs, 0, true);
818 i = extract_component (gsi, rhs, 1, true);
820 else
822 r = gimple_assign_rhs1 (stmt);
823 i = gimple_assign_rhs2 (stmt);
825 update_complex_assignment (gsi, r, i);
828 else if (rhs && TREE_CODE (rhs) == SSA_NAME && !TREE_SIDE_EFFECTS (lhs))
830 tree x;
831 gimple t;
832 location_t loc;
834 loc = gimple_location (stmt);
835 r = extract_component (gsi, rhs, 0, false);
836 i = extract_component (gsi, rhs, 1, false);
838 x = build1 (REALPART_EXPR, inner_type, unshare_expr (lhs));
839 t = gimple_build_assign (x, r);
840 gimple_set_location (t, loc);
841 gsi_insert_before (gsi, t, GSI_SAME_STMT);
843 if (stmt == gsi_stmt (*gsi))
845 x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
846 gimple_assign_set_lhs (stmt, x);
847 gimple_assign_set_rhs1 (stmt, i);
849 else
851 x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
852 t = gimple_build_assign (x, i);
853 gimple_set_location (t, loc);
854 gsi_insert_before (gsi, t, GSI_SAME_STMT);
856 stmt = gsi_stmt (*gsi);
857 gcc_assert (gimple_code (stmt) == GIMPLE_RETURN);
858 gimple_return_set_retval (as_a <gimple_return> (stmt), lhs);
861 update_stmt (stmt);
865 /* Expand complex addition to scalars:
866 a + b = (ar + br) + i(ai + bi)
867 a - b = (ar - br) + i(ai + bi)
870 static void
871 expand_complex_addition (gimple_stmt_iterator *gsi, tree inner_type,
872 tree ar, tree ai, tree br, tree bi,
873 enum tree_code code,
874 complex_lattice_t al, complex_lattice_t bl)
876 tree rr, ri;
878 switch (PAIR (al, bl))
880 case PAIR (ONLY_REAL, ONLY_REAL):
881 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
882 ri = ai;
883 break;
885 case PAIR (ONLY_REAL, ONLY_IMAG):
886 rr = ar;
887 if (code == MINUS_EXPR)
888 ri = gimplify_build2 (gsi, MINUS_EXPR, inner_type, ai, bi);
889 else
890 ri = bi;
891 break;
893 case PAIR (ONLY_IMAG, ONLY_REAL):
894 if (code == MINUS_EXPR)
895 rr = gimplify_build2 (gsi, MINUS_EXPR, inner_type, ar, br);
896 else
897 rr = br;
898 ri = ai;
899 break;
901 case PAIR (ONLY_IMAG, ONLY_IMAG):
902 rr = ar;
903 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
904 break;
906 case PAIR (VARYING, ONLY_REAL):
907 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
908 ri = ai;
909 break;
911 case PAIR (VARYING, ONLY_IMAG):
912 rr = ar;
913 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
914 break;
916 case PAIR (ONLY_REAL, VARYING):
917 if (code == MINUS_EXPR)
918 goto general;
919 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
920 ri = bi;
921 break;
923 case PAIR (ONLY_IMAG, VARYING):
924 if (code == MINUS_EXPR)
925 goto general;
926 rr = br;
927 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
928 break;
930 case PAIR (VARYING, VARYING):
931 general:
932 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
933 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
934 break;
936 default:
937 gcc_unreachable ();
940 update_complex_assignment (gsi, rr, ri);
943 /* Expand a complex multiplication or division to a libcall to the c99
944 compliant routines. */
946 static void
947 expand_complex_libcall (gimple_stmt_iterator *gsi, tree ar, tree ai,
948 tree br, tree bi, enum tree_code code)
950 enum machine_mode mode;
951 enum built_in_function bcode;
952 tree fn, type, lhs;
953 gimple old_stmt;
954 gimple_call stmt;
956 old_stmt = gsi_stmt (*gsi);
957 lhs = gimple_assign_lhs (old_stmt);
958 type = TREE_TYPE (lhs);
960 mode = TYPE_MODE (type);
961 gcc_assert (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT);
963 if (code == MULT_EXPR)
964 bcode = ((enum built_in_function)
965 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
966 else if (code == RDIV_EXPR)
967 bcode = ((enum built_in_function)
968 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
969 else
970 gcc_unreachable ();
971 fn = builtin_decl_explicit (bcode);
973 stmt = gimple_build_call (fn, 4, ar, ai, br, bi);
974 gimple_call_set_lhs (stmt, lhs);
975 update_stmt (stmt);
976 gsi_replace (gsi, stmt, false);
978 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
979 gimple_purge_dead_eh_edges (gsi_bb (*gsi));
981 if (gimple_in_ssa_p (cfun))
983 type = TREE_TYPE (type);
984 update_complex_components (gsi, stmt,
985 build1 (REALPART_EXPR, type, lhs),
986 build1 (IMAGPART_EXPR, type, lhs));
987 SSA_NAME_DEF_STMT (lhs) = stmt;
991 /* Expand complex multiplication to scalars:
992 a * b = (ar*br - ai*bi) + i(ar*bi + br*ai)
995 static void
996 expand_complex_multiplication (gimple_stmt_iterator *gsi, tree inner_type,
997 tree ar, tree ai, tree br, tree bi,
998 complex_lattice_t al, complex_lattice_t bl)
1000 tree rr, ri;
1002 if (al < bl)
1004 complex_lattice_t tl;
1005 rr = ar, ar = br, br = rr;
1006 ri = ai, ai = bi, bi = ri;
1007 tl = al, al = bl, bl = tl;
1010 switch (PAIR (al, bl))
1012 case PAIR (ONLY_REAL, ONLY_REAL):
1013 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1014 ri = ai;
1015 break;
1017 case PAIR (ONLY_IMAG, ONLY_REAL):
1018 rr = ar;
1019 if (TREE_CODE (ai) == REAL_CST
1020 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (ai), dconst1))
1021 ri = br;
1022 else
1023 ri = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1024 break;
1026 case PAIR (ONLY_IMAG, ONLY_IMAG):
1027 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1028 rr = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, rr);
1029 ri = ar;
1030 break;
1032 case PAIR (VARYING, ONLY_REAL):
1033 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1034 ri = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1035 break;
1037 case PAIR (VARYING, ONLY_IMAG):
1038 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1039 rr = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, rr);
1040 ri = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, bi);
1041 break;
1043 case PAIR (VARYING, VARYING):
1044 if (flag_complex_method == 2 && SCALAR_FLOAT_TYPE_P (inner_type))
1046 expand_complex_libcall (gsi, ar, ai, br, bi, MULT_EXPR);
1047 return;
1049 else
1051 tree t1, t2, t3, t4;
1053 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1054 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1055 t3 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, bi);
1057 /* Avoid expanding redundant multiplication for the common
1058 case of squaring a complex number. */
1059 if (ar == br && ai == bi)
1060 t4 = t3;
1061 else
1062 t4 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1064 rr = gimplify_build2 (gsi, MINUS_EXPR, inner_type, t1, t2);
1065 ri = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t3, t4);
1067 break;
1069 default:
1070 gcc_unreachable ();
1073 update_complex_assignment (gsi, rr, ri);
1076 /* Keep this algorithm in sync with fold-const.c:const_binop().
1078 Expand complex division to scalars, straightforward algorithm.
1079 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1080 t = br*br + bi*bi
1083 static void
1084 expand_complex_div_straight (gimple_stmt_iterator *gsi, tree inner_type,
1085 tree ar, tree ai, tree br, tree bi,
1086 enum tree_code code)
1088 tree rr, ri, div, t1, t2, t3;
1090 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, br, br);
1091 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, bi, bi);
1092 div = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, t2);
1094 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1095 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1096 t3 = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, t2);
1097 rr = gimplify_build2 (gsi, code, inner_type, t3, div);
1099 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1100 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, bi);
1101 t3 = gimplify_build2 (gsi, MINUS_EXPR, inner_type, t1, t2);
1102 ri = gimplify_build2 (gsi, code, inner_type, t3, div);
1104 update_complex_assignment (gsi, rr, ri);
1107 /* Keep this algorithm in sync with fold-const.c:const_binop().
1109 Expand complex division to scalars, modified algorithm to minimize
1110 overflow with wide input ranges. */
1112 static void
1113 expand_complex_div_wide (gimple_stmt_iterator *gsi, tree inner_type,
1114 tree ar, tree ai, tree br, tree bi,
1115 enum tree_code code)
1117 tree rr, ri, ratio, div, t1, t2, tr, ti, compare;
1118 basic_block bb_cond, bb_true, bb_false, bb_join;
1119 gimple stmt;
1121 /* Examine |br| < |bi|, and branch. */
1122 t1 = gimplify_build1 (gsi, ABS_EXPR, inner_type, br);
1123 t2 = gimplify_build1 (gsi, ABS_EXPR, inner_type, bi);
1124 compare = fold_build2_loc (gimple_location (gsi_stmt (*gsi)),
1125 LT_EXPR, boolean_type_node, t1, t2);
1126 STRIP_NOPS (compare);
1128 bb_cond = bb_true = bb_false = bb_join = NULL;
1129 rr = ri = tr = ti = NULL;
1130 if (TREE_CODE (compare) != INTEGER_CST)
1132 edge e;
1133 gimple stmt;
1134 tree cond, tmp;
1136 tmp = create_tmp_var (boolean_type_node, NULL);
1137 stmt = gimple_build_assign (tmp, compare);
1138 if (gimple_in_ssa_p (cfun))
1140 tmp = make_ssa_name (tmp, stmt);
1141 gimple_assign_set_lhs (stmt, tmp);
1144 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1146 cond = fold_build2_loc (gimple_location (stmt),
1147 EQ_EXPR, boolean_type_node, tmp, boolean_true_node);
1148 stmt = gimple_build_cond_from_tree (cond, NULL_TREE, NULL_TREE);
1149 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1151 /* Split the original block, and create the TRUE and FALSE blocks. */
1152 e = split_block (gsi_bb (*gsi), stmt);
1153 bb_cond = e->src;
1154 bb_join = e->dest;
1155 bb_true = create_empty_bb (bb_cond);
1156 bb_false = create_empty_bb (bb_true);
1158 /* Wire the blocks together. */
1159 e->flags = EDGE_TRUE_VALUE;
1160 redirect_edge_succ (e, bb_true);
1161 make_edge (bb_cond, bb_false, EDGE_FALSE_VALUE);
1162 make_edge (bb_true, bb_join, EDGE_FALLTHRU);
1163 make_edge (bb_false, bb_join, EDGE_FALLTHRU);
1164 add_bb_to_loop (bb_true, bb_cond->loop_father);
1165 add_bb_to_loop (bb_false, bb_cond->loop_father);
1167 /* Update dominance info. Note that bb_join's data was
1168 updated by split_block. */
1169 if (dom_info_available_p (CDI_DOMINATORS))
1171 set_immediate_dominator (CDI_DOMINATORS, bb_true, bb_cond);
1172 set_immediate_dominator (CDI_DOMINATORS, bb_false, bb_cond);
1175 rr = create_tmp_reg (inner_type, NULL);
1176 ri = create_tmp_reg (inner_type, NULL);
1179 /* In the TRUE branch, we compute
1180 ratio = br/bi;
1181 div = (br * ratio) + bi;
1182 tr = (ar * ratio) + ai;
1183 ti = (ai * ratio) - ar;
1184 tr = tr / div;
1185 ti = ti / div; */
1186 if (bb_true || integer_nonzerop (compare))
1188 if (bb_true)
1190 *gsi = gsi_last_bb (bb_true);
1191 gsi_insert_after (gsi, gimple_build_nop (), GSI_NEW_STMT);
1194 ratio = gimplify_build2 (gsi, code, inner_type, br, bi);
1196 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, br, ratio);
1197 div = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, bi);
1199 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, ratio);
1200 tr = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, ai);
1202 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, ratio);
1203 ti = gimplify_build2 (gsi, MINUS_EXPR, inner_type, t1, ar);
1205 tr = gimplify_build2 (gsi, code, inner_type, tr, div);
1206 ti = gimplify_build2 (gsi, code, inner_type, ti, div);
1208 if (bb_true)
1210 stmt = gimple_build_assign (rr, tr);
1211 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1212 stmt = gimple_build_assign (ri, ti);
1213 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1214 gsi_remove (gsi, true);
1218 /* In the FALSE branch, we compute
1219 ratio = d/c;
1220 divisor = (d * ratio) + c;
1221 tr = (b * ratio) + a;
1222 ti = b - (a * ratio);
1223 tr = tr / div;
1224 ti = ti / div; */
1225 if (bb_false || integer_zerop (compare))
1227 if (bb_false)
1229 *gsi = gsi_last_bb (bb_false);
1230 gsi_insert_after (gsi, gimple_build_nop (), GSI_NEW_STMT);
1233 ratio = gimplify_build2 (gsi, code, inner_type, bi, br);
1235 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, bi, ratio);
1236 div = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, br);
1238 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, ratio);
1239 tr = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, ar);
1241 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, ratio);
1242 ti = gimplify_build2 (gsi, MINUS_EXPR, inner_type, ai, t1);
1244 tr = gimplify_build2 (gsi, code, inner_type, tr, div);
1245 ti = gimplify_build2 (gsi, code, inner_type, ti, div);
1247 if (bb_false)
1249 stmt = gimple_build_assign (rr, tr);
1250 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1251 stmt = gimple_build_assign (ri, ti);
1252 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1253 gsi_remove (gsi, true);
1257 if (bb_join)
1258 *gsi = gsi_start_bb (bb_join);
1259 else
1260 rr = tr, ri = ti;
1262 update_complex_assignment (gsi, rr, ri);
1265 /* Expand complex division to scalars. */
1267 static void
1268 expand_complex_division (gimple_stmt_iterator *gsi, tree inner_type,
1269 tree ar, tree ai, tree br, tree bi,
1270 enum tree_code code,
1271 complex_lattice_t al, complex_lattice_t bl)
1273 tree rr, ri;
1275 switch (PAIR (al, bl))
1277 case PAIR (ONLY_REAL, ONLY_REAL):
1278 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
1279 ri = ai;
1280 break;
1282 case PAIR (ONLY_REAL, ONLY_IMAG):
1283 rr = ai;
1284 ri = gimplify_build2 (gsi, code, inner_type, ar, bi);
1285 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ri);
1286 break;
1288 case PAIR (ONLY_IMAG, ONLY_REAL):
1289 rr = ar;
1290 ri = gimplify_build2 (gsi, code, inner_type, ai, br);
1291 break;
1293 case PAIR (ONLY_IMAG, ONLY_IMAG):
1294 rr = gimplify_build2 (gsi, code, inner_type, ai, bi);
1295 ri = ar;
1296 break;
1298 case PAIR (VARYING, ONLY_REAL):
1299 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
1300 ri = gimplify_build2 (gsi, code, inner_type, ai, br);
1301 break;
1303 case PAIR (VARYING, ONLY_IMAG):
1304 rr = gimplify_build2 (gsi, code, inner_type, ai, bi);
1305 ri = gimplify_build2 (gsi, code, inner_type, ar, bi);
1306 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ri);
1308 case PAIR (ONLY_REAL, VARYING):
1309 case PAIR (ONLY_IMAG, VARYING):
1310 case PAIR (VARYING, VARYING):
1311 switch (flag_complex_method)
1313 case 0:
1314 /* straightforward implementation of complex divide acceptable. */
1315 expand_complex_div_straight (gsi, inner_type, ar, ai, br, bi, code);
1316 break;
1318 case 2:
1319 if (SCALAR_FLOAT_TYPE_P (inner_type))
1321 expand_complex_libcall (gsi, ar, ai, br, bi, code);
1322 break;
1324 /* FALLTHRU */
1326 case 1:
1327 /* wide ranges of inputs must work for complex divide. */
1328 expand_complex_div_wide (gsi, inner_type, ar, ai, br, bi, code);
1329 break;
1331 default:
1332 gcc_unreachable ();
1334 return;
1336 default:
1337 gcc_unreachable ();
1340 update_complex_assignment (gsi, rr, ri);
1343 /* Expand complex negation to scalars:
1344 -a = (-ar) + i(-ai)
1347 static void
1348 expand_complex_negation (gimple_stmt_iterator *gsi, tree inner_type,
1349 tree ar, tree ai)
1351 tree rr, ri;
1353 rr = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ar);
1354 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ai);
1356 update_complex_assignment (gsi, rr, ri);
1359 /* Expand complex conjugate to scalars:
1360 ~a = (ar) + i(-ai)
1363 static void
1364 expand_complex_conjugate (gimple_stmt_iterator *gsi, tree inner_type,
1365 tree ar, tree ai)
1367 tree ri;
1369 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ai);
1371 update_complex_assignment (gsi, ar, ri);
1374 /* Expand complex comparison (EQ or NE only). */
1376 static void
1377 expand_complex_comparison (gimple_stmt_iterator *gsi, tree ar, tree ai,
1378 tree br, tree bi, enum tree_code code)
1380 tree cr, ci, cc, type;
1381 gimple stmt;
1383 cr = gimplify_build2 (gsi, code, boolean_type_node, ar, br);
1384 ci = gimplify_build2 (gsi, code, boolean_type_node, ai, bi);
1385 cc = gimplify_build2 (gsi,
1386 (code == EQ_EXPR ? TRUTH_AND_EXPR : TRUTH_OR_EXPR),
1387 boolean_type_node, cr, ci);
1389 stmt = gsi_stmt (*gsi);
1391 switch (gimple_code (stmt))
1393 case GIMPLE_RETURN:
1395 gimple_return return_stmt = as_a <gimple_return> (stmt);
1396 type = TREE_TYPE (gimple_return_retval (return_stmt));
1397 gimple_return_set_retval (return_stmt, fold_convert (type, cc));
1399 break;
1401 case GIMPLE_ASSIGN:
1402 type = TREE_TYPE (gimple_assign_lhs (stmt));
1403 gimple_assign_set_rhs_from_tree (gsi, fold_convert (type, cc));
1404 stmt = gsi_stmt (*gsi);
1405 break;
1407 case GIMPLE_COND:
1409 gimple_cond cond_stmt = as_a <gimple_cond> (stmt);
1410 gimple_cond_set_code (cond_stmt, EQ_EXPR);
1411 gimple_cond_set_lhs (cond_stmt, cc);
1412 gimple_cond_set_rhs (cond_stmt, boolean_true_node);
1414 break;
1416 default:
1417 gcc_unreachable ();
1420 update_stmt (stmt);
1423 /* Expand inline asm that sets some complex SSA_NAMEs. */
1425 static void
1426 expand_complex_asm (gimple_stmt_iterator *gsi)
1428 gimple_asm stmt = as_a <gimple_asm> (gsi_stmt (*gsi));
1429 unsigned int i;
1431 for (i = 0; i < gimple_asm_noutputs (stmt); ++i)
1433 tree link = gimple_asm_output_op (stmt, i);
1434 tree op = TREE_VALUE (link);
1435 if (TREE_CODE (op) == SSA_NAME
1436 && TREE_CODE (TREE_TYPE (op)) == COMPLEX_TYPE)
1438 tree type = TREE_TYPE (op);
1439 tree inner_type = TREE_TYPE (type);
1440 tree r = build1 (REALPART_EXPR, inner_type, op);
1441 tree i = build1 (IMAGPART_EXPR, inner_type, op);
1442 gimple_seq list = set_component_ssa_name (op, false, r);
1444 if (list)
1445 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
1447 list = set_component_ssa_name (op, true, i);
1448 if (list)
1449 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
1454 /* Process one statement. If we identify a complex operation, expand it. */
1456 static void
1457 expand_complex_operations_1 (gimple_stmt_iterator *gsi)
1459 gimple stmt = gsi_stmt (*gsi);
1460 tree type, inner_type, lhs;
1461 tree ac, ar, ai, bc, br, bi;
1462 complex_lattice_t al, bl;
1463 enum tree_code code;
1465 if (gimple_code (stmt) == GIMPLE_ASM)
1467 expand_complex_asm (gsi);
1468 return;
1471 lhs = gimple_get_lhs (stmt);
1472 if (!lhs && gimple_code (stmt) != GIMPLE_COND)
1473 return;
1475 type = TREE_TYPE (gimple_op (stmt, 0));
1476 code = gimple_expr_code (stmt);
1478 /* Initial filter for operations we handle. */
1479 switch (code)
1481 case PLUS_EXPR:
1482 case MINUS_EXPR:
1483 case MULT_EXPR:
1484 case TRUNC_DIV_EXPR:
1485 case CEIL_DIV_EXPR:
1486 case FLOOR_DIV_EXPR:
1487 case ROUND_DIV_EXPR:
1488 case RDIV_EXPR:
1489 case NEGATE_EXPR:
1490 case CONJ_EXPR:
1491 if (TREE_CODE (type) != COMPLEX_TYPE)
1492 return;
1493 inner_type = TREE_TYPE (type);
1494 break;
1496 case EQ_EXPR:
1497 case NE_EXPR:
1498 /* Note, both GIMPLE_ASSIGN and GIMPLE_COND may have an EQ_EXPR
1499 subcode, so we need to access the operands using gimple_op. */
1500 inner_type = TREE_TYPE (gimple_op (stmt, 1));
1501 if (TREE_CODE (inner_type) != COMPLEX_TYPE)
1502 return;
1503 break;
1505 default:
1507 tree rhs;
1509 /* GIMPLE_COND may also fallthru here, but we do not need to
1510 do anything with it. */
1511 if (gimple_code (stmt) == GIMPLE_COND)
1512 return;
1514 if (TREE_CODE (type) == COMPLEX_TYPE)
1515 expand_complex_move (gsi, type);
1516 else if (is_gimple_assign (stmt)
1517 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
1518 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
1519 && TREE_CODE (lhs) == SSA_NAME)
1521 rhs = gimple_assign_rhs1 (stmt);
1522 rhs = extract_component (gsi, TREE_OPERAND (rhs, 0),
1523 gimple_assign_rhs_code (stmt)
1524 == IMAGPART_EXPR,
1525 false);
1526 gimple_assign_set_rhs_from_tree (gsi, rhs);
1527 stmt = gsi_stmt (*gsi);
1528 update_stmt (stmt);
1531 return;
1534 /* Extract the components of the two complex values. Make sure and
1535 handle the common case of the same value used twice specially. */
1536 if (is_gimple_assign (stmt))
1538 ac = gimple_assign_rhs1 (stmt);
1539 bc = (gimple_num_ops (stmt) > 2) ? gimple_assign_rhs2 (stmt) : NULL;
1541 /* GIMPLE_CALL can not get here. */
1542 else
1544 ac = gimple_cond_lhs (stmt);
1545 bc = gimple_cond_rhs (stmt);
1548 ar = extract_component (gsi, ac, false, true);
1549 ai = extract_component (gsi, ac, true, true);
1551 if (ac == bc)
1552 br = ar, bi = ai;
1553 else if (bc)
1555 br = extract_component (gsi, bc, 0, true);
1556 bi = extract_component (gsi, bc, 1, true);
1558 else
1559 br = bi = NULL_TREE;
1561 if (gimple_in_ssa_p (cfun))
1563 al = find_lattice_value (ac);
1564 if (al == UNINITIALIZED)
1565 al = VARYING;
1567 if (TREE_CODE_CLASS (code) == tcc_unary)
1568 bl = UNINITIALIZED;
1569 else if (ac == bc)
1570 bl = al;
1571 else
1573 bl = find_lattice_value (bc);
1574 if (bl == UNINITIALIZED)
1575 bl = VARYING;
1578 else
1579 al = bl = VARYING;
1581 switch (code)
1583 case PLUS_EXPR:
1584 case MINUS_EXPR:
1585 expand_complex_addition (gsi, inner_type, ar, ai, br, bi, code, al, bl);
1586 break;
1588 case MULT_EXPR:
1589 expand_complex_multiplication (gsi, inner_type, ar, ai, br, bi, al, bl);
1590 break;
1592 case TRUNC_DIV_EXPR:
1593 case CEIL_DIV_EXPR:
1594 case FLOOR_DIV_EXPR:
1595 case ROUND_DIV_EXPR:
1596 case RDIV_EXPR:
1597 expand_complex_division (gsi, inner_type, ar, ai, br, bi, code, al, bl);
1598 break;
1600 case NEGATE_EXPR:
1601 expand_complex_negation (gsi, inner_type, ar, ai);
1602 break;
1604 case CONJ_EXPR:
1605 expand_complex_conjugate (gsi, inner_type, ar, ai);
1606 break;
1608 case EQ_EXPR:
1609 case NE_EXPR:
1610 expand_complex_comparison (gsi, ar, ai, br, bi, code);
1611 break;
1613 default:
1614 gcc_unreachable ();
1619 /* Entry point for complex operation lowering during optimization. */
1621 static unsigned int
1622 tree_lower_complex (void)
1624 int old_last_basic_block;
1625 gimple_stmt_iterator gsi;
1626 basic_block bb;
1628 if (!init_dont_simulate_again ())
1629 return 0;
1631 complex_lattice_values.create (num_ssa_names);
1632 complex_lattice_values.safe_grow_cleared (num_ssa_names);
1634 init_parameter_lattice_values ();
1635 ssa_propagate (complex_visit_stmt, complex_visit_phi);
1637 complex_variable_components = new int_tree_htab_type (10);
1639 complex_ssa_name_components.create (2 * num_ssa_names);
1640 complex_ssa_name_components.safe_grow_cleared (2 * num_ssa_names);
1642 update_parameter_components ();
1644 /* ??? Ideally we'd traverse the blocks in breadth-first order. */
1645 old_last_basic_block = last_basic_block_for_fn (cfun);
1646 FOR_EACH_BB_FN (bb, cfun)
1648 if (bb->index >= old_last_basic_block)
1649 continue;
1651 update_phi_components (bb);
1652 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1653 expand_complex_operations_1 (&gsi);
1656 gsi_commit_edge_inserts ();
1658 delete complex_variable_components;
1659 complex_variable_components = NULL;
1660 complex_ssa_name_components.release ();
1661 complex_lattice_values.release ();
1662 return 0;
1665 namespace {
1667 const pass_data pass_data_lower_complex =
1669 GIMPLE_PASS, /* type */
1670 "cplxlower", /* name */
1671 OPTGROUP_NONE, /* optinfo_flags */
1672 TV_NONE, /* tv_id */
1673 PROP_ssa, /* properties_required */
1674 PROP_gimple_lcx, /* properties_provided */
1675 0, /* properties_destroyed */
1676 0, /* todo_flags_start */
1677 TODO_update_ssa, /* todo_flags_finish */
1680 class pass_lower_complex : public gimple_opt_pass
1682 public:
1683 pass_lower_complex (gcc::context *ctxt)
1684 : gimple_opt_pass (pass_data_lower_complex, ctxt)
1687 /* opt_pass methods: */
1688 opt_pass * clone () { return new pass_lower_complex (m_ctxt); }
1689 virtual unsigned int execute (function *) { return tree_lower_complex (); }
1691 }; // class pass_lower_complex
1693 } // anon namespace
1695 gimple_opt_pass *
1696 make_pass_lower_complex (gcc::context *ctxt)
1698 return new pass_lower_complex (ctxt);
1702 namespace {
1704 const pass_data pass_data_lower_complex_O0 =
1706 GIMPLE_PASS, /* type */
1707 "cplxlower0", /* name */
1708 OPTGROUP_NONE, /* optinfo_flags */
1709 TV_NONE, /* tv_id */
1710 PROP_cfg, /* properties_required */
1711 PROP_gimple_lcx, /* properties_provided */
1712 0, /* properties_destroyed */
1713 0, /* todo_flags_start */
1714 TODO_update_ssa, /* todo_flags_finish */
1717 class pass_lower_complex_O0 : public gimple_opt_pass
1719 public:
1720 pass_lower_complex_O0 (gcc::context *ctxt)
1721 : gimple_opt_pass (pass_data_lower_complex_O0, ctxt)
1724 /* opt_pass methods: */
1725 virtual bool gate (function *fun)
1727 /* With errors, normal optimization passes are not run. If we don't
1728 lower complex operations at all, rtl expansion will abort. */
1729 return !(fun->curr_properties & PROP_gimple_lcx);
1732 virtual unsigned int execute (function *) { return tree_lower_complex (); }
1734 }; // class pass_lower_complex_O0
1736 } // anon namespace
1738 gimple_opt_pass *
1739 make_pass_lower_complex_O0 (gcc::context *ctxt)
1741 return new pass_lower_complex_O0 (ctxt);