* c-c++-common/ubsan/float-cast-overflow-6.c: Add i?86-*-* target.
[official-gcc.git] / gcc / tree-complex.c
blobf7f2135bbdaa03fc46b0b5d731714329d1e0183a
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 "predict.h"
28 #include "vec.h"
29 #include "hashtab.h"
30 #include "hash-set.h"
31 #include "machmode.h"
32 #include "hard-reg-set.h"
33 #include "input.h"
34 #include "function.h"
35 #include "dominance.h"
36 #include "cfg.h"
37 #include "basic-block.h"
38 #include "tree-ssa-alias.h"
39 #include "internal-fn.h"
40 #include "tree-eh.h"
41 #include "gimple-expr.h"
42 #include "is-a.h"
43 #include "gimple.h"
44 #include "gimplify.h"
45 #include "gimple-iterator.h"
46 #include "gimplify-me.h"
47 #include "gimple-ssa.h"
48 #include "tree-cfg.h"
49 #include "tree-phinodes.h"
50 #include "ssa-iterators.h"
51 #include "stringpool.h"
52 #include "tree-ssanames.h"
53 #include "expr.h"
54 #include "tree-dfa.h"
55 #include "tree-ssa.h"
56 #include "tree-iterator.h"
57 #include "tree-pass.h"
58 #include "tree-ssa-propagate.h"
59 #include "tree-hasher.h"
60 #include "cfgloop.h"
63 /* For each complex ssa name, a lattice value. We're interested in finding
64 out whether a complex number is degenerate in some way, having only real
65 or only complex parts. */
67 enum
69 UNINITIALIZED = 0,
70 ONLY_REAL = 1,
71 ONLY_IMAG = 2,
72 VARYING = 3
75 /* The type complex_lattice_t holds combinations of the above
76 constants. */
77 typedef int complex_lattice_t;
79 #define PAIR(a, b) ((a) << 2 | (b))
82 static vec<complex_lattice_t> complex_lattice_values;
84 /* For each complex variable, a pair of variables for the components exists in
85 the hashtable. */
86 static int_tree_htab_type *complex_variable_components;
88 /* For each complex SSA_NAME, a pair of ssa names for the components. */
89 static vec<tree> complex_ssa_name_components;
91 /* Lookup UID in the complex_variable_components hashtable and return the
92 associated tree. */
93 static tree
94 cvc_lookup (unsigned int uid)
96 struct int_tree_map in;
97 in.uid = uid;
98 return complex_variable_components->find_with_hash (in, uid).to;
101 /* Insert the pair UID, TO into the complex_variable_components hashtable. */
103 static void
104 cvc_insert (unsigned int uid, tree to)
106 int_tree_map h;
107 int_tree_map *loc;
109 h.uid = uid;
110 loc = complex_variable_components->find_slot_with_hash (h, uid, INSERT);
111 loc->uid = uid;
112 loc->to = to;
115 /* Return true if T is not a zero constant. In the case of real values,
116 we're only interested in +0.0. */
118 static int
119 some_nonzerop (tree t)
121 int zerop = false;
123 /* Operations with real or imaginary part of a complex number zero
124 cannot be treated the same as operations with a real or imaginary
125 operand if we care about the signs of zeros in the result. */
126 if (TREE_CODE (t) == REAL_CST && !flag_signed_zeros)
127 zerop = REAL_VALUES_IDENTICAL (TREE_REAL_CST (t), dconst0);
128 else if (TREE_CODE (t) == FIXED_CST)
129 zerop = fixed_zerop (t);
130 else if (TREE_CODE (t) == INTEGER_CST)
131 zerop = integer_zerop (t);
133 return !zerop;
137 /* Compute a lattice value from the components of a complex type REAL
138 and IMAG. */
140 static complex_lattice_t
141 find_lattice_value_parts (tree real, tree imag)
143 int r, i;
144 complex_lattice_t ret;
146 r = some_nonzerop (real);
147 i = some_nonzerop (imag);
148 ret = r * ONLY_REAL + i * ONLY_IMAG;
150 /* ??? On occasion we could do better than mapping 0+0i to real, but we
151 certainly don't want to leave it UNINITIALIZED, which eventually gets
152 mapped to VARYING. */
153 if (ret == UNINITIALIZED)
154 ret = ONLY_REAL;
156 return ret;
160 /* Compute a lattice value from gimple_val T. */
162 static complex_lattice_t
163 find_lattice_value (tree t)
165 tree real, imag;
167 switch (TREE_CODE (t))
169 case SSA_NAME:
170 return complex_lattice_values[SSA_NAME_VERSION (t)];
172 case COMPLEX_CST:
173 real = TREE_REALPART (t);
174 imag = TREE_IMAGPART (t);
175 break;
177 default:
178 gcc_unreachable ();
181 return find_lattice_value_parts (real, imag);
184 /* Determine if LHS is something for which we're interested in seeing
185 simulation results. */
187 static bool
188 is_complex_reg (tree lhs)
190 return TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE && is_gimple_reg (lhs);
193 /* Mark the incoming parameters to the function as VARYING. */
195 static void
196 init_parameter_lattice_values (void)
198 tree parm, ssa_name;
200 for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = DECL_CHAIN (parm))
201 if (is_complex_reg (parm)
202 && (ssa_name = ssa_default_def (cfun, parm)) != NULL_TREE)
203 complex_lattice_values[SSA_NAME_VERSION (ssa_name)] = VARYING;
206 /* Initialize simulation state for each statement. Return false if we
207 found no statements we want to simulate, and thus there's nothing
208 for the entire pass to do. */
210 static bool
211 init_dont_simulate_again (void)
213 basic_block bb;
214 gimple_stmt_iterator gsi;
215 gimple phi;
216 bool saw_a_complex_op = false;
218 FOR_EACH_BB_FN (bb, cfun)
220 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
222 phi = gsi_stmt (gsi);
223 prop_set_simulate_again (phi,
224 is_complex_reg (gimple_phi_result (phi)));
227 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
229 gimple stmt;
230 tree op0, op1;
231 bool sim_again_p;
233 stmt = gsi_stmt (gsi);
234 op0 = op1 = NULL_TREE;
236 /* Most control-altering statements must be initially
237 simulated, else we won't cover the entire cfg. */
238 sim_again_p = stmt_ends_bb_p (stmt);
240 switch (gimple_code (stmt))
242 case GIMPLE_CALL:
243 if (gimple_call_lhs (stmt))
244 sim_again_p = is_complex_reg (gimple_call_lhs (stmt));
245 break;
247 case GIMPLE_ASSIGN:
248 sim_again_p = is_complex_reg (gimple_assign_lhs (stmt));
249 if (gimple_assign_rhs_code (stmt) == REALPART_EXPR
250 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
251 op0 = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
252 else
253 op0 = gimple_assign_rhs1 (stmt);
254 if (gimple_num_ops (stmt) > 2)
255 op1 = gimple_assign_rhs2 (stmt);
256 break;
258 case GIMPLE_COND:
259 op0 = gimple_cond_lhs (stmt);
260 op1 = gimple_cond_rhs (stmt);
261 break;
263 default:
264 break;
267 if (op0 || op1)
268 switch (gimple_expr_code (stmt))
270 case EQ_EXPR:
271 case NE_EXPR:
272 case PLUS_EXPR:
273 case MINUS_EXPR:
274 case MULT_EXPR:
275 case TRUNC_DIV_EXPR:
276 case CEIL_DIV_EXPR:
277 case FLOOR_DIV_EXPR:
278 case ROUND_DIV_EXPR:
279 case RDIV_EXPR:
280 if (TREE_CODE (TREE_TYPE (op0)) == COMPLEX_TYPE
281 || TREE_CODE (TREE_TYPE (op1)) == COMPLEX_TYPE)
282 saw_a_complex_op = true;
283 break;
285 case NEGATE_EXPR:
286 case CONJ_EXPR:
287 if (TREE_CODE (TREE_TYPE (op0)) == COMPLEX_TYPE)
288 saw_a_complex_op = true;
289 break;
291 case REALPART_EXPR:
292 case IMAGPART_EXPR:
293 /* The total store transformation performed during
294 gimplification creates such uninitialized loads
295 and we need to lower the statement to be able
296 to fix things up. */
297 if (TREE_CODE (op0) == SSA_NAME
298 && ssa_undefined_value_p (op0))
299 saw_a_complex_op = true;
300 break;
302 default:
303 break;
306 prop_set_simulate_again (stmt, sim_again_p);
310 return saw_a_complex_op;
314 /* Evaluate statement STMT against the complex lattice defined above. */
316 static enum ssa_prop_result
317 complex_visit_stmt (gimple stmt, edge *taken_edge_p ATTRIBUTE_UNUSED,
318 tree *result_p)
320 complex_lattice_t new_l, old_l, op1_l, op2_l;
321 unsigned int ver;
322 tree lhs;
324 lhs = gimple_get_lhs (stmt);
325 /* Skip anything but GIMPLE_ASSIGN and GIMPLE_CALL with a lhs. */
326 if (!lhs)
327 return SSA_PROP_VARYING;
329 /* These conditions should be satisfied due to the initial filter
330 set up in init_dont_simulate_again. */
331 gcc_assert (TREE_CODE (lhs) == SSA_NAME);
332 gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE);
334 *result_p = lhs;
335 ver = SSA_NAME_VERSION (lhs);
336 old_l = complex_lattice_values[ver];
338 switch (gimple_expr_code (stmt))
340 case SSA_NAME:
341 case COMPLEX_CST:
342 new_l = find_lattice_value (gimple_assign_rhs1 (stmt));
343 break;
345 case COMPLEX_EXPR:
346 new_l = find_lattice_value_parts (gimple_assign_rhs1 (stmt),
347 gimple_assign_rhs2 (stmt));
348 break;
350 case PLUS_EXPR:
351 case MINUS_EXPR:
352 op1_l = find_lattice_value (gimple_assign_rhs1 (stmt));
353 op2_l = find_lattice_value (gimple_assign_rhs2 (stmt));
355 /* We've set up the lattice values such that IOR neatly
356 models addition. */
357 new_l = op1_l | op2_l;
358 break;
360 case MULT_EXPR:
361 case RDIV_EXPR:
362 case TRUNC_DIV_EXPR:
363 case CEIL_DIV_EXPR:
364 case FLOOR_DIV_EXPR:
365 case ROUND_DIV_EXPR:
366 op1_l = find_lattice_value (gimple_assign_rhs1 (stmt));
367 op2_l = find_lattice_value (gimple_assign_rhs2 (stmt));
369 /* Obviously, if either varies, so does the result. */
370 if (op1_l == VARYING || op2_l == VARYING)
371 new_l = VARYING;
372 /* Don't prematurely promote variables if we've not yet seen
373 their inputs. */
374 else if (op1_l == UNINITIALIZED)
375 new_l = op2_l;
376 else if (op2_l == UNINITIALIZED)
377 new_l = op1_l;
378 else
380 /* At this point both numbers have only one component. If the
381 numbers are of opposite kind, the result is imaginary,
382 otherwise the result is real. The add/subtract translates
383 the real/imag from/to 0/1; the ^ performs the comparison. */
384 new_l = ((op1_l - ONLY_REAL) ^ (op2_l - ONLY_REAL)) + ONLY_REAL;
386 /* Don't allow the lattice value to flip-flop indefinitely. */
387 new_l |= old_l;
389 break;
391 case NEGATE_EXPR:
392 case CONJ_EXPR:
393 new_l = find_lattice_value (gimple_assign_rhs1 (stmt));
394 break;
396 default:
397 new_l = VARYING;
398 break;
401 /* If nothing changed this round, let the propagator know. */
402 if (new_l == old_l)
403 return SSA_PROP_NOT_INTERESTING;
405 complex_lattice_values[ver] = new_l;
406 return new_l == VARYING ? SSA_PROP_VARYING : SSA_PROP_INTERESTING;
409 /* Evaluate a PHI node against the complex lattice defined above. */
411 static enum ssa_prop_result
412 complex_visit_phi (gimple phi)
414 complex_lattice_t new_l, old_l;
415 unsigned int ver;
416 tree lhs;
417 int i;
419 lhs = gimple_phi_result (phi);
421 /* This condition should be satisfied due to the initial filter
422 set up in init_dont_simulate_again. */
423 gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE);
425 /* We've set up the lattice values such that IOR neatly models PHI meet. */
426 new_l = UNINITIALIZED;
427 for (i = gimple_phi_num_args (phi) - 1; i >= 0; --i)
428 new_l |= find_lattice_value (gimple_phi_arg_def (phi, i));
430 ver = SSA_NAME_VERSION (lhs);
431 old_l = complex_lattice_values[ver];
433 if (new_l == old_l)
434 return SSA_PROP_NOT_INTERESTING;
436 complex_lattice_values[ver] = new_l;
437 return new_l == VARYING ? SSA_PROP_VARYING : SSA_PROP_INTERESTING;
440 /* Create one backing variable for a complex component of ORIG. */
442 static tree
443 create_one_component_var (tree type, tree orig, const char *prefix,
444 const char *suffix, enum tree_code code)
446 tree r = create_tmp_var (type, prefix);
448 DECL_SOURCE_LOCATION (r) = DECL_SOURCE_LOCATION (orig);
449 DECL_ARTIFICIAL (r) = 1;
451 if (DECL_NAME (orig) && !DECL_IGNORED_P (orig))
453 const char *name = IDENTIFIER_POINTER (DECL_NAME (orig));
455 DECL_NAME (r) = get_identifier (ACONCAT ((name, suffix, NULL)));
457 SET_DECL_DEBUG_EXPR (r, build1 (code, type, orig));
458 DECL_HAS_DEBUG_EXPR_P (r) = 1;
459 DECL_IGNORED_P (r) = 0;
460 TREE_NO_WARNING (r) = TREE_NO_WARNING (orig);
462 else
464 DECL_IGNORED_P (r) = 1;
465 TREE_NO_WARNING (r) = 1;
468 return r;
471 /* Retrieve a value for a complex component of VAR. */
473 static tree
474 get_component_var (tree var, bool imag_p)
476 size_t decl_index = DECL_UID (var) * 2 + imag_p;
477 tree ret = cvc_lookup (decl_index);
479 if (ret == NULL)
481 ret = create_one_component_var (TREE_TYPE (TREE_TYPE (var)), var,
482 imag_p ? "CI" : "CR",
483 imag_p ? "$imag" : "$real",
484 imag_p ? IMAGPART_EXPR : REALPART_EXPR);
485 cvc_insert (decl_index, ret);
488 return ret;
491 /* Retrieve a value for a complex component of SSA_NAME. */
493 static tree
494 get_component_ssa_name (tree ssa_name, bool imag_p)
496 complex_lattice_t lattice = find_lattice_value (ssa_name);
497 size_t ssa_name_index;
498 tree ret;
500 if (lattice == (imag_p ? ONLY_REAL : ONLY_IMAG))
502 tree inner_type = TREE_TYPE (TREE_TYPE (ssa_name));
503 if (SCALAR_FLOAT_TYPE_P (inner_type))
504 return build_real (inner_type, dconst0);
505 else
506 return build_int_cst (inner_type, 0);
509 ssa_name_index = SSA_NAME_VERSION (ssa_name) * 2 + imag_p;
510 ret = complex_ssa_name_components[ssa_name_index];
511 if (ret == NULL)
513 if (SSA_NAME_VAR (ssa_name))
514 ret = get_component_var (SSA_NAME_VAR (ssa_name), imag_p);
515 else
516 ret = TREE_TYPE (TREE_TYPE (ssa_name));
517 ret = make_ssa_name (ret, NULL);
519 /* Copy some properties from the original. In particular, whether it
520 is used in an abnormal phi, and whether it's uninitialized. */
521 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ret)
522 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name);
523 if (SSA_NAME_IS_DEFAULT_DEF (ssa_name)
524 && TREE_CODE (SSA_NAME_VAR (ssa_name)) == VAR_DECL)
526 SSA_NAME_DEF_STMT (ret) = SSA_NAME_DEF_STMT (ssa_name);
527 set_ssa_default_def (cfun, SSA_NAME_VAR (ret), ret);
530 complex_ssa_name_components[ssa_name_index] = ret;
533 return ret;
536 /* Set a value for a complex component of SSA_NAME, return a
537 gimple_seq of stuff that needs doing. */
539 static gimple_seq
540 set_component_ssa_name (tree ssa_name, bool imag_p, tree value)
542 complex_lattice_t lattice = find_lattice_value (ssa_name);
543 size_t ssa_name_index;
544 tree comp;
545 gimple last;
546 gimple_seq list;
548 /* We know the value must be zero, else there's a bug in our lattice
549 analysis. But the value may well be a variable known to contain
550 zero. We should be safe ignoring it. */
551 if (lattice == (imag_p ? ONLY_REAL : ONLY_IMAG))
552 return NULL;
554 /* If we've already assigned an SSA_NAME to this component, then this
555 means that our walk of the basic blocks found a use before the set.
556 This is fine. Now we should create an initialization for the value
557 we created earlier. */
558 ssa_name_index = SSA_NAME_VERSION (ssa_name) * 2 + imag_p;
559 comp = complex_ssa_name_components[ssa_name_index];
560 if (comp)
563 /* If we've nothing assigned, and the value we're given is already stable,
564 then install that as the value for this SSA_NAME. This preemptively
565 copy-propagates the value, which avoids unnecessary memory allocation. */
566 else if (is_gimple_min_invariant (value)
567 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name))
569 complex_ssa_name_components[ssa_name_index] = value;
570 return NULL;
572 else if (TREE_CODE (value) == SSA_NAME
573 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name))
575 /* Replace an anonymous base value with the variable from cvc_lookup.
576 This should result in better debug info. */
577 if (SSA_NAME_VAR (ssa_name)
578 && (!SSA_NAME_VAR (value) || DECL_IGNORED_P (SSA_NAME_VAR (value)))
579 && !DECL_IGNORED_P (SSA_NAME_VAR (ssa_name)))
581 comp = get_component_var (SSA_NAME_VAR (ssa_name), imag_p);
582 replace_ssa_name_symbol (value, comp);
585 complex_ssa_name_components[ssa_name_index] = value;
586 return NULL;
589 /* Finally, we need to stabilize the result by installing the value into
590 a new ssa name. */
591 else
592 comp = get_component_ssa_name (ssa_name, imag_p);
594 /* Do all the work to assign VALUE to COMP. */
595 list = NULL;
596 value = force_gimple_operand (value, &list, false, NULL);
597 last = gimple_build_assign (comp, value);
598 gimple_seq_add_stmt (&list, last);
599 gcc_assert (SSA_NAME_DEF_STMT (comp) == last);
601 return list;
604 /* Extract the real or imaginary part of a complex variable or constant.
605 Make sure that it's a proper gimple_val and gimplify it if not.
606 Emit any new code before gsi. */
608 static tree
609 extract_component (gimple_stmt_iterator *gsi, tree t, bool imagpart_p,
610 bool gimple_p)
612 switch (TREE_CODE (t))
614 case COMPLEX_CST:
615 return imagpart_p ? TREE_IMAGPART (t) : TREE_REALPART (t);
617 case COMPLEX_EXPR:
618 gcc_unreachable ();
620 case VAR_DECL:
621 case RESULT_DECL:
622 case PARM_DECL:
623 case COMPONENT_REF:
624 case ARRAY_REF:
625 case VIEW_CONVERT_EXPR:
626 case MEM_REF:
628 tree inner_type = TREE_TYPE (TREE_TYPE (t));
630 t = build1 ((imagpart_p ? IMAGPART_EXPR : REALPART_EXPR),
631 inner_type, unshare_expr (t));
633 if (gimple_p)
634 t = force_gimple_operand_gsi (gsi, t, true, NULL, true,
635 GSI_SAME_STMT);
637 return t;
640 case SSA_NAME:
641 return get_component_ssa_name (t, imagpart_p);
643 default:
644 gcc_unreachable ();
648 /* Update the complex components of the ssa name on the lhs of STMT. */
650 static void
651 update_complex_components (gimple_stmt_iterator *gsi, gimple stmt, tree r,
652 tree i)
654 tree lhs;
655 gimple_seq list;
657 lhs = gimple_get_lhs (stmt);
659 list = set_component_ssa_name (lhs, false, r);
660 if (list)
661 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
663 list = set_component_ssa_name (lhs, true, i);
664 if (list)
665 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
668 static void
669 update_complex_components_on_edge (edge e, tree lhs, tree r, tree i)
671 gimple_seq list;
673 list = set_component_ssa_name (lhs, false, r);
674 if (list)
675 gsi_insert_seq_on_edge (e, list);
677 list = set_component_ssa_name (lhs, true, i);
678 if (list)
679 gsi_insert_seq_on_edge (e, list);
683 /* Update an assignment to a complex variable in place. */
685 static void
686 update_complex_assignment (gimple_stmt_iterator *gsi, tree r, tree i)
688 gimple stmt;
690 gimple_assign_set_rhs_with_ops (gsi, COMPLEX_EXPR, r, i);
691 stmt = gsi_stmt (*gsi);
692 update_stmt (stmt);
693 if (maybe_clean_eh_stmt (stmt))
694 gimple_purge_dead_eh_edges (gimple_bb (stmt));
696 if (gimple_in_ssa_p (cfun))
697 update_complex_components (gsi, gsi_stmt (*gsi), r, i);
701 /* Generate code at the entry point of the function to initialize the
702 component variables for a complex parameter. */
704 static void
705 update_parameter_components (void)
707 edge entry_edge = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun));
708 tree parm;
710 for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = DECL_CHAIN (parm))
712 tree type = TREE_TYPE (parm);
713 tree ssa_name, r, i;
715 if (TREE_CODE (type) != COMPLEX_TYPE || !is_gimple_reg (parm))
716 continue;
718 type = TREE_TYPE (type);
719 ssa_name = ssa_default_def (cfun, parm);
720 if (!ssa_name)
721 continue;
723 r = build1 (REALPART_EXPR, type, ssa_name);
724 i = build1 (IMAGPART_EXPR, type, ssa_name);
725 update_complex_components_on_edge (entry_edge, ssa_name, r, i);
729 /* Generate code to set the component variables of a complex variable
730 to match the PHI statements in block BB. */
732 static void
733 update_phi_components (basic_block bb)
735 gimple_stmt_iterator gsi;
737 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
739 gimple phi = gsi_stmt (gsi);
741 if (is_complex_reg (gimple_phi_result (phi)))
743 tree lr, li;
744 gimple pr = NULL, pi = NULL;
745 unsigned int i, n;
747 lr = get_component_ssa_name (gimple_phi_result (phi), false);
748 if (TREE_CODE (lr) == SSA_NAME)
749 pr = create_phi_node (lr, bb);
751 li = get_component_ssa_name (gimple_phi_result (phi), true);
752 if (TREE_CODE (li) == SSA_NAME)
753 pi = create_phi_node (li, bb);
755 for (i = 0, n = gimple_phi_num_args (phi); i < n; ++i)
757 tree comp, arg = gimple_phi_arg_def (phi, i);
758 if (pr)
760 comp = extract_component (NULL, arg, false, false);
761 SET_PHI_ARG_DEF (pr, i, comp);
763 if (pi)
765 comp = extract_component (NULL, arg, true, false);
766 SET_PHI_ARG_DEF (pi, i, comp);
773 /* Expand a complex move to scalars. */
775 static void
776 expand_complex_move (gimple_stmt_iterator *gsi, tree type)
778 tree inner_type = TREE_TYPE (type);
779 tree r, i, lhs, rhs;
780 gimple stmt = gsi_stmt (*gsi);
782 if (is_gimple_assign (stmt))
784 lhs = gimple_assign_lhs (stmt);
785 if (gimple_num_ops (stmt) == 2)
786 rhs = gimple_assign_rhs1 (stmt);
787 else
788 rhs = NULL_TREE;
790 else if (is_gimple_call (stmt))
792 lhs = gimple_call_lhs (stmt);
793 rhs = NULL_TREE;
795 else
796 gcc_unreachable ();
798 if (TREE_CODE (lhs) == SSA_NAME)
800 if (is_ctrl_altering_stmt (stmt))
802 edge e;
804 /* The value is not assigned on the exception edges, so we need not
805 concern ourselves there. We do need to update on the fallthru
806 edge. Find it. */
807 e = find_fallthru_edge (gsi_bb (*gsi)->succs);
808 if (!e)
809 gcc_unreachable ();
811 r = build1 (REALPART_EXPR, inner_type, lhs);
812 i = build1 (IMAGPART_EXPR, inner_type, lhs);
813 update_complex_components_on_edge (e, lhs, r, i);
815 else if (is_gimple_call (stmt)
816 || gimple_has_side_effects (stmt)
817 || gimple_assign_rhs_code (stmt) == PAREN_EXPR)
819 r = build1 (REALPART_EXPR, inner_type, lhs);
820 i = build1 (IMAGPART_EXPR, inner_type, lhs);
821 update_complex_components (gsi, stmt, r, i);
823 else
825 if (gimple_assign_rhs_code (stmt) != COMPLEX_EXPR)
827 r = extract_component (gsi, rhs, 0, true);
828 i = extract_component (gsi, rhs, 1, true);
830 else
832 r = gimple_assign_rhs1 (stmt);
833 i = gimple_assign_rhs2 (stmt);
835 update_complex_assignment (gsi, r, i);
838 else if (rhs && TREE_CODE (rhs) == SSA_NAME && !TREE_SIDE_EFFECTS (lhs))
840 tree x;
841 gimple t;
842 location_t loc;
844 loc = gimple_location (stmt);
845 r = extract_component (gsi, rhs, 0, false);
846 i = extract_component (gsi, rhs, 1, false);
848 x = build1 (REALPART_EXPR, inner_type, unshare_expr (lhs));
849 t = gimple_build_assign (x, r);
850 gimple_set_location (t, loc);
851 gsi_insert_before (gsi, t, GSI_SAME_STMT);
853 if (stmt == gsi_stmt (*gsi))
855 x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
856 gimple_assign_set_lhs (stmt, x);
857 gimple_assign_set_rhs1 (stmt, i);
859 else
861 x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
862 t = gimple_build_assign (x, i);
863 gimple_set_location (t, loc);
864 gsi_insert_before (gsi, t, GSI_SAME_STMT);
866 stmt = gsi_stmt (*gsi);
867 gcc_assert (gimple_code (stmt) == GIMPLE_RETURN);
868 gimple_return_set_retval (stmt, lhs);
871 update_stmt (stmt);
875 /* Expand complex addition to scalars:
876 a + b = (ar + br) + i(ai + bi)
877 a - b = (ar - br) + i(ai + bi)
880 static void
881 expand_complex_addition (gimple_stmt_iterator *gsi, tree inner_type,
882 tree ar, tree ai, tree br, tree bi,
883 enum tree_code code,
884 complex_lattice_t al, complex_lattice_t bl)
886 tree rr, ri;
888 switch (PAIR (al, bl))
890 case PAIR (ONLY_REAL, ONLY_REAL):
891 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
892 ri = ai;
893 break;
895 case PAIR (ONLY_REAL, ONLY_IMAG):
896 rr = ar;
897 if (code == MINUS_EXPR)
898 ri = gimplify_build2 (gsi, MINUS_EXPR, inner_type, ai, bi);
899 else
900 ri = bi;
901 break;
903 case PAIR (ONLY_IMAG, ONLY_REAL):
904 if (code == MINUS_EXPR)
905 rr = gimplify_build2 (gsi, MINUS_EXPR, inner_type, ar, br);
906 else
907 rr = br;
908 ri = ai;
909 break;
911 case PAIR (ONLY_IMAG, ONLY_IMAG):
912 rr = ar;
913 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
914 break;
916 case PAIR (VARYING, ONLY_REAL):
917 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
918 ri = ai;
919 break;
921 case PAIR (VARYING, ONLY_IMAG):
922 rr = ar;
923 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
924 break;
926 case PAIR (ONLY_REAL, VARYING):
927 if (code == MINUS_EXPR)
928 goto general;
929 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
930 ri = bi;
931 break;
933 case PAIR (ONLY_IMAG, VARYING):
934 if (code == MINUS_EXPR)
935 goto general;
936 rr = br;
937 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
938 break;
940 case PAIR (VARYING, VARYING):
941 general:
942 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
943 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
944 break;
946 default:
947 gcc_unreachable ();
950 update_complex_assignment (gsi, rr, ri);
953 /* Expand a complex multiplication or division to a libcall to the c99
954 compliant routines. */
956 static void
957 expand_complex_libcall (gimple_stmt_iterator *gsi, tree ar, tree ai,
958 tree br, tree bi, enum tree_code code)
960 machine_mode mode;
961 enum built_in_function bcode;
962 tree fn, type, lhs;
963 gimple old_stmt, stmt;
965 old_stmt = gsi_stmt (*gsi);
966 lhs = gimple_assign_lhs (old_stmt);
967 type = TREE_TYPE (lhs);
969 mode = TYPE_MODE (type);
970 gcc_assert (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT);
972 if (code == MULT_EXPR)
973 bcode = ((enum built_in_function)
974 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
975 else if (code == RDIV_EXPR)
976 bcode = ((enum built_in_function)
977 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
978 else
979 gcc_unreachable ();
980 fn = builtin_decl_explicit (bcode);
982 stmt = gimple_build_call (fn, 4, ar, ai, br, bi);
983 gimple_call_set_lhs (stmt, lhs);
984 update_stmt (stmt);
985 gsi_replace (gsi, stmt, false);
987 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
988 gimple_purge_dead_eh_edges (gsi_bb (*gsi));
990 if (gimple_in_ssa_p (cfun))
992 type = TREE_TYPE (type);
993 update_complex_components (gsi, stmt,
994 build1 (REALPART_EXPR, type, lhs),
995 build1 (IMAGPART_EXPR, type, lhs));
996 SSA_NAME_DEF_STMT (lhs) = stmt;
1000 /* Expand complex multiplication to scalars:
1001 a * b = (ar*br - ai*bi) + i(ar*bi + br*ai)
1004 static void
1005 expand_complex_multiplication (gimple_stmt_iterator *gsi, tree inner_type,
1006 tree ar, tree ai, tree br, tree bi,
1007 complex_lattice_t al, complex_lattice_t bl)
1009 tree rr, ri;
1011 if (al < bl)
1013 complex_lattice_t tl;
1014 rr = ar, ar = br, br = rr;
1015 ri = ai, ai = bi, bi = ri;
1016 tl = al, al = bl, bl = tl;
1019 switch (PAIR (al, bl))
1021 case PAIR (ONLY_REAL, ONLY_REAL):
1022 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1023 ri = ai;
1024 break;
1026 case PAIR (ONLY_IMAG, ONLY_REAL):
1027 rr = ar;
1028 if (TREE_CODE (ai) == REAL_CST
1029 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (ai), dconst1))
1030 ri = br;
1031 else
1032 ri = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1033 break;
1035 case PAIR (ONLY_IMAG, ONLY_IMAG):
1036 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1037 rr = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, rr);
1038 ri = ar;
1039 break;
1041 case PAIR (VARYING, ONLY_REAL):
1042 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1043 ri = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1044 break;
1046 case PAIR (VARYING, ONLY_IMAG):
1047 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1048 rr = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, rr);
1049 ri = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, bi);
1050 break;
1052 case PAIR (VARYING, VARYING):
1053 if (flag_complex_method == 2 && SCALAR_FLOAT_TYPE_P (inner_type))
1055 expand_complex_libcall (gsi, ar, ai, br, bi, MULT_EXPR);
1056 return;
1058 else
1060 tree t1, t2, t3, t4;
1062 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1063 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1064 t3 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, bi);
1066 /* Avoid expanding redundant multiplication for the common
1067 case of squaring a complex number. */
1068 if (ar == br && ai == bi)
1069 t4 = t3;
1070 else
1071 t4 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1073 rr = gimplify_build2 (gsi, MINUS_EXPR, inner_type, t1, t2);
1074 ri = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t3, t4);
1076 break;
1078 default:
1079 gcc_unreachable ();
1082 update_complex_assignment (gsi, rr, ri);
1085 /* Keep this algorithm in sync with fold-const.c:const_binop().
1087 Expand complex division to scalars, straightforward algorithm.
1088 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1089 t = br*br + bi*bi
1092 static void
1093 expand_complex_div_straight (gimple_stmt_iterator *gsi, tree inner_type,
1094 tree ar, tree ai, tree br, tree bi,
1095 enum tree_code code)
1097 tree rr, ri, div, t1, t2, t3;
1099 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, br, br);
1100 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, bi, bi);
1101 div = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, t2);
1103 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1104 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1105 t3 = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, t2);
1106 rr = gimplify_build2 (gsi, code, inner_type, t3, div);
1108 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1109 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, bi);
1110 t3 = gimplify_build2 (gsi, MINUS_EXPR, inner_type, t1, t2);
1111 ri = gimplify_build2 (gsi, code, inner_type, t3, div);
1113 update_complex_assignment (gsi, rr, ri);
1116 /* Keep this algorithm in sync with fold-const.c:const_binop().
1118 Expand complex division to scalars, modified algorithm to minimize
1119 overflow with wide input ranges. */
1121 static void
1122 expand_complex_div_wide (gimple_stmt_iterator *gsi, tree inner_type,
1123 tree ar, tree ai, tree br, tree bi,
1124 enum tree_code code)
1126 tree rr, ri, ratio, div, t1, t2, tr, ti, compare;
1127 basic_block bb_cond, bb_true, bb_false, bb_join;
1128 gimple stmt;
1130 /* Examine |br| < |bi|, and branch. */
1131 t1 = gimplify_build1 (gsi, ABS_EXPR, inner_type, br);
1132 t2 = gimplify_build1 (gsi, ABS_EXPR, inner_type, bi);
1133 compare = fold_build2_loc (gimple_location (gsi_stmt (*gsi)),
1134 LT_EXPR, boolean_type_node, t1, t2);
1135 STRIP_NOPS (compare);
1137 bb_cond = bb_true = bb_false = bb_join = NULL;
1138 rr = ri = tr = ti = NULL;
1139 if (TREE_CODE (compare) != INTEGER_CST)
1141 edge e;
1142 gimple stmt;
1143 tree cond, tmp;
1145 tmp = create_tmp_var (boolean_type_node, NULL);
1146 stmt = gimple_build_assign (tmp, compare);
1147 if (gimple_in_ssa_p (cfun))
1149 tmp = make_ssa_name (tmp, stmt);
1150 gimple_assign_set_lhs (stmt, tmp);
1153 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1155 cond = fold_build2_loc (gimple_location (stmt),
1156 EQ_EXPR, boolean_type_node, tmp, boolean_true_node);
1157 stmt = gimple_build_cond_from_tree (cond, NULL_TREE, NULL_TREE);
1158 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1160 /* Split the original block, and create the TRUE and FALSE blocks. */
1161 e = split_block (gsi_bb (*gsi), stmt);
1162 bb_cond = e->src;
1163 bb_join = e->dest;
1164 bb_true = create_empty_bb (bb_cond);
1165 bb_false = create_empty_bb (bb_true);
1167 /* Wire the blocks together. */
1168 e->flags = EDGE_TRUE_VALUE;
1169 redirect_edge_succ (e, bb_true);
1170 make_edge (bb_cond, bb_false, EDGE_FALSE_VALUE);
1171 make_edge (bb_true, bb_join, EDGE_FALLTHRU);
1172 make_edge (bb_false, bb_join, EDGE_FALLTHRU);
1173 add_bb_to_loop (bb_true, bb_cond->loop_father);
1174 add_bb_to_loop (bb_false, bb_cond->loop_father);
1176 /* Update dominance info. Note that bb_join's data was
1177 updated by split_block. */
1178 if (dom_info_available_p (CDI_DOMINATORS))
1180 set_immediate_dominator (CDI_DOMINATORS, bb_true, bb_cond);
1181 set_immediate_dominator (CDI_DOMINATORS, bb_false, bb_cond);
1184 rr = create_tmp_reg (inner_type, NULL);
1185 ri = create_tmp_reg (inner_type, NULL);
1188 /* In the TRUE branch, we compute
1189 ratio = br/bi;
1190 div = (br * ratio) + bi;
1191 tr = (ar * ratio) + ai;
1192 ti = (ai * ratio) - ar;
1193 tr = tr / div;
1194 ti = ti / div; */
1195 if (bb_true || integer_nonzerop (compare))
1197 if (bb_true)
1199 *gsi = gsi_last_bb (bb_true);
1200 gsi_insert_after (gsi, gimple_build_nop (), GSI_NEW_STMT);
1203 ratio = gimplify_build2 (gsi, code, inner_type, br, bi);
1205 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, br, ratio);
1206 div = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, bi);
1208 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, ratio);
1209 tr = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, ai);
1211 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, ratio);
1212 ti = gimplify_build2 (gsi, MINUS_EXPR, inner_type, t1, ar);
1214 tr = gimplify_build2 (gsi, code, inner_type, tr, div);
1215 ti = gimplify_build2 (gsi, code, inner_type, ti, div);
1217 if (bb_true)
1219 stmt = gimple_build_assign (rr, tr);
1220 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1221 stmt = gimple_build_assign (ri, ti);
1222 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1223 gsi_remove (gsi, true);
1227 /* In the FALSE branch, we compute
1228 ratio = d/c;
1229 divisor = (d * ratio) + c;
1230 tr = (b * ratio) + a;
1231 ti = b - (a * ratio);
1232 tr = tr / div;
1233 ti = ti / div; */
1234 if (bb_false || integer_zerop (compare))
1236 if (bb_false)
1238 *gsi = gsi_last_bb (bb_false);
1239 gsi_insert_after (gsi, gimple_build_nop (), GSI_NEW_STMT);
1242 ratio = gimplify_build2 (gsi, code, inner_type, bi, br);
1244 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, bi, ratio);
1245 div = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, br);
1247 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, ratio);
1248 tr = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, ar);
1250 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, ratio);
1251 ti = gimplify_build2 (gsi, MINUS_EXPR, inner_type, ai, t1);
1253 tr = gimplify_build2 (gsi, code, inner_type, tr, div);
1254 ti = gimplify_build2 (gsi, code, inner_type, ti, div);
1256 if (bb_false)
1258 stmt = gimple_build_assign (rr, tr);
1259 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1260 stmt = gimple_build_assign (ri, ti);
1261 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1262 gsi_remove (gsi, true);
1266 if (bb_join)
1267 *gsi = gsi_start_bb (bb_join);
1268 else
1269 rr = tr, ri = ti;
1271 update_complex_assignment (gsi, rr, ri);
1274 /* Expand complex division to scalars. */
1276 static void
1277 expand_complex_division (gimple_stmt_iterator *gsi, tree inner_type,
1278 tree ar, tree ai, tree br, tree bi,
1279 enum tree_code code,
1280 complex_lattice_t al, complex_lattice_t bl)
1282 tree rr, ri;
1284 switch (PAIR (al, bl))
1286 case PAIR (ONLY_REAL, ONLY_REAL):
1287 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
1288 ri = ai;
1289 break;
1291 case PAIR (ONLY_REAL, ONLY_IMAG):
1292 rr = ai;
1293 ri = gimplify_build2 (gsi, code, inner_type, ar, bi);
1294 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ri);
1295 break;
1297 case PAIR (ONLY_IMAG, ONLY_REAL):
1298 rr = ar;
1299 ri = gimplify_build2 (gsi, code, inner_type, ai, br);
1300 break;
1302 case PAIR (ONLY_IMAG, ONLY_IMAG):
1303 rr = gimplify_build2 (gsi, code, inner_type, ai, bi);
1304 ri = ar;
1305 break;
1307 case PAIR (VARYING, ONLY_REAL):
1308 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
1309 ri = gimplify_build2 (gsi, code, inner_type, ai, br);
1310 break;
1312 case PAIR (VARYING, ONLY_IMAG):
1313 rr = gimplify_build2 (gsi, code, inner_type, ai, bi);
1314 ri = gimplify_build2 (gsi, code, inner_type, ar, bi);
1315 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ri);
1317 case PAIR (ONLY_REAL, VARYING):
1318 case PAIR (ONLY_IMAG, VARYING):
1319 case PAIR (VARYING, VARYING):
1320 switch (flag_complex_method)
1322 case 0:
1323 /* straightforward implementation of complex divide acceptable. */
1324 expand_complex_div_straight (gsi, inner_type, ar, ai, br, bi, code);
1325 break;
1327 case 2:
1328 if (SCALAR_FLOAT_TYPE_P (inner_type))
1330 expand_complex_libcall (gsi, ar, ai, br, bi, code);
1331 break;
1333 /* FALLTHRU */
1335 case 1:
1336 /* wide ranges of inputs must work for complex divide. */
1337 expand_complex_div_wide (gsi, inner_type, ar, ai, br, bi, code);
1338 break;
1340 default:
1341 gcc_unreachable ();
1343 return;
1345 default:
1346 gcc_unreachable ();
1349 update_complex_assignment (gsi, rr, ri);
1352 /* Expand complex negation to scalars:
1353 -a = (-ar) + i(-ai)
1356 static void
1357 expand_complex_negation (gimple_stmt_iterator *gsi, tree inner_type,
1358 tree ar, tree ai)
1360 tree rr, ri;
1362 rr = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ar);
1363 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ai);
1365 update_complex_assignment (gsi, rr, ri);
1368 /* Expand complex conjugate to scalars:
1369 ~a = (ar) + i(-ai)
1372 static void
1373 expand_complex_conjugate (gimple_stmt_iterator *gsi, tree inner_type,
1374 tree ar, tree ai)
1376 tree ri;
1378 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ai);
1380 update_complex_assignment (gsi, ar, ri);
1383 /* Expand complex comparison (EQ or NE only). */
1385 static void
1386 expand_complex_comparison (gimple_stmt_iterator *gsi, tree ar, tree ai,
1387 tree br, tree bi, enum tree_code code)
1389 tree cr, ci, cc, type;
1390 gimple stmt;
1392 cr = gimplify_build2 (gsi, code, boolean_type_node, ar, br);
1393 ci = gimplify_build2 (gsi, code, boolean_type_node, ai, bi);
1394 cc = gimplify_build2 (gsi,
1395 (code == EQ_EXPR ? TRUTH_AND_EXPR : TRUTH_OR_EXPR),
1396 boolean_type_node, cr, ci);
1398 stmt = gsi_stmt (*gsi);
1400 switch (gimple_code (stmt))
1402 case GIMPLE_RETURN:
1403 type = TREE_TYPE (gimple_return_retval (stmt));
1404 gimple_return_set_retval (stmt, fold_convert (type, cc));
1405 break;
1407 case GIMPLE_ASSIGN:
1408 type = TREE_TYPE (gimple_assign_lhs (stmt));
1409 gimple_assign_set_rhs_from_tree (gsi, fold_convert (type, cc));
1410 stmt = gsi_stmt (*gsi);
1411 break;
1413 case GIMPLE_COND:
1414 gimple_cond_set_code (stmt, EQ_EXPR);
1415 gimple_cond_set_lhs (stmt, cc);
1416 gimple_cond_set_rhs (stmt, boolean_true_node);
1417 break;
1419 default:
1420 gcc_unreachable ();
1423 update_stmt (stmt);
1426 /* Expand inline asm that sets some complex SSA_NAMEs. */
1428 static void
1429 expand_complex_asm (gimple_stmt_iterator *gsi)
1431 gimple stmt = gsi_stmt (*gsi);
1432 unsigned int i;
1434 for (i = 0; i < gimple_asm_noutputs (stmt); ++i)
1436 tree link = gimple_asm_output_op (stmt, i);
1437 tree op = TREE_VALUE (link);
1438 if (TREE_CODE (op) == SSA_NAME
1439 && TREE_CODE (TREE_TYPE (op)) == COMPLEX_TYPE)
1441 tree type = TREE_TYPE (op);
1442 tree inner_type = TREE_TYPE (type);
1443 tree r = build1 (REALPART_EXPR, inner_type, op);
1444 tree i = build1 (IMAGPART_EXPR, inner_type, op);
1445 gimple_seq list = set_component_ssa_name (op, false, r);
1447 if (list)
1448 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
1450 list = set_component_ssa_name (op, true, i);
1451 if (list)
1452 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
1457 /* Process one statement. If we identify a complex operation, expand it. */
1459 static void
1460 expand_complex_operations_1 (gimple_stmt_iterator *gsi)
1462 gimple stmt = gsi_stmt (*gsi);
1463 tree type, inner_type, lhs;
1464 tree ac, ar, ai, bc, br, bi;
1465 complex_lattice_t al, bl;
1466 enum tree_code code;
1468 if (gimple_code (stmt) == GIMPLE_ASM)
1470 expand_complex_asm (gsi);
1471 return;
1474 lhs = gimple_get_lhs (stmt);
1475 if (!lhs && gimple_code (stmt) != GIMPLE_COND)
1476 return;
1478 type = TREE_TYPE (gimple_op (stmt, 0));
1479 code = gimple_expr_code (stmt);
1481 /* Initial filter for operations we handle. */
1482 switch (code)
1484 case PLUS_EXPR:
1485 case MINUS_EXPR:
1486 case MULT_EXPR:
1487 case TRUNC_DIV_EXPR:
1488 case CEIL_DIV_EXPR:
1489 case FLOOR_DIV_EXPR:
1490 case ROUND_DIV_EXPR:
1491 case RDIV_EXPR:
1492 case NEGATE_EXPR:
1493 case CONJ_EXPR:
1494 if (TREE_CODE (type) != COMPLEX_TYPE)
1495 return;
1496 inner_type = TREE_TYPE (type);
1497 break;
1499 case EQ_EXPR:
1500 case NE_EXPR:
1501 /* Note, both GIMPLE_ASSIGN and GIMPLE_COND may have an EQ_EXPR
1502 subcode, so we need to access the operands using gimple_op. */
1503 inner_type = TREE_TYPE (gimple_op (stmt, 1));
1504 if (TREE_CODE (inner_type) != COMPLEX_TYPE)
1505 return;
1506 break;
1508 default:
1510 tree rhs;
1512 /* GIMPLE_COND may also fallthru here, but we do not need to
1513 do anything with it. */
1514 if (gimple_code (stmt) == GIMPLE_COND)
1515 return;
1517 if (TREE_CODE (type) == COMPLEX_TYPE)
1518 expand_complex_move (gsi, type);
1519 else if (is_gimple_assign (stmt)
1520 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
1521 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
1522 && TREE_CODE (lhs) == SSA_NAME)
1524 rhs = gimple_assign_rhs1 (stmt);
1525 rhs = extract_component (gsi, TREE_OPERAND (rhs, 0),
1526 gimple_assign_rhs_code (stmt)
1527 == IMAGPART_EXPR,
1528 false);
1529 gimple_assign_set_rhs_from_tree (gsi, rhs);
1530 stmt = gsi_stmt (*gsi);
1531 update_stmt (stmt);
1534 return;
1537 /* Extract the components of the two complex values. Make sure and
1538 handle the common case of the same value used twice specially. */
1539 if (is_gimple_assign (stmt))
1541 ac = gimple_assign_rhs1 (stmt);
1542 bc = (gimple_num_ops (stmt) > 2) ? gimple_assign_rhs2 (stmt) : NULL;
1544 /* GIMPLE_CALL can not get here. */
1545 else
1547 ac = gimple_cond_lhs (stmt);
1548 bc = gimple_cond_rhs (stmt);
1551 ar = extract_component (gsi, ac, false, true);
1552 ai = extract_component (gsi, ac, true, true);
1554 if (ac == bc)
1555 br = ar, bi = ai;
1556 else if (bc)
1558 br = extract_component (gsi, bc, 0, true);
1559 bi = extract_component (gsi, bc, 1, true);
1561 else
1562 br = bi = NULL_TREE;
1564 if (gimple_in_ssa_p (cfun))
1566 al = find_lattice_value (ac);
1567 if (al == UNINITIALIZED)
1568 al = VARYING;
1570 if (TREE_CODE_CLASS (code) == tcc_unary)
1571 bl = UNINITIALIZED;
1572 else if (ac == bc)
1573 bl = al;
1574 else
1576 bl = find_lattice_value (bc);
1577 if (bl == UNINITIALIZED)
1578 bl = VARYING;
1581 else
1582 al = bl = VARYING;
1584 switch (code)
1586 case PLUS_EXPR:
1587 case MINUS_EXPR:
1588 expand_complex_addition (gsi, inner_type, ar, ai, br, bi, code, al, bl);
1589 break;
1591 case MULT_EXPR:
1592 expand_complex_multiplication (gsi, inner_type, ar, ai, br, bi, al, bl);
1593 break;
1595 case TRUNC_DIV_EXPR:
1596 case CEIL_DIV_EXPR:
1597 case FLOOR_DIV_EXPR:
1598 case ROUND_DIV_EXPR:
1599 case RDIV_EXPR:
1600 expand_complex_division (gsi, inner_type, ar, ai, br, bi, code, al, bl);
1601 break;
1603 case NEGATE_EXPR:
1604 expand_complex_negation (gsi, inner_type, ar, ai);
1605 break;
1607 case CONJ_EXPR:
1608 expand_complex_conjugate (gsi, inner_type, ar, ai);
1609 break;
1611 case EQ_EXPR:
1612 case NE_EXPR:
1613 expand_complex_comparison (gsi, ar, ai, br, bi, code);
1614 break;
1616 default:
1617 gcc_unreachable ();
1622 /* Entry point for complex operation lowering during optimization. */
1624 static unsigned int
1625 tree_lower_complex (void)
1627 int old_last_basic_block;
1628 gimple_stmt_iterator gsi;
1629 basic_block bb;
1631 if (!init_dont_simulate_again ())
1632 return 0;
1634 complex_lattice_values.create (num_ssa_names);
1635 complex_lattice_values.safe_grow_cleared (num_ssa_names);
1637 init_parameter_lattice_values ();
1638 ssa_propagate (complex_visit_stmt, complex_visit_phi);
1640 complex_variable_components = new int_tree_htab_type (10);
1642 complex_ssa_name_components.create (2 * num_ssa_names);
1643 complex_ssa_name_components.safe_grow_cleared (2 * num_ssa_names);
1645 update_parameter_components ();
1647 /* ??? Ideally we'd traverse the blocks in breadth-first order. */
1648 old_last_basic_block = last_basic_block_for_fn (cfun);
1649 FOR_EACH_BB_FN (bb, cfun)
1651 if (bb->index >= old_last_basic_block)
1652 continue;
1654 update_phi_components (bb);
1655 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1656 expand_complex_operations_1 (&gsi);
1659 gsi_commit_edge_inserts ();
1661 delete complex_variable_components;
1662 complex_variable_components = NULL;
1663 complex_ssa_name_components.release ();
1664 complex_lattice_values.release ();
1665 return 0;
1668 namespace {
1670 const pass_data pass_data_lower_complex =
1672 GIMPLE_PASS, /* type */
1673 "cplxlower", /* name */
1674 OPTGROUP_NONE, /* optinfo_flags */
1675 TV_NONE, /* tv_id */
1676 PROP_ssa, /* properties_required */
1677 PROP_gimple_lcx, /* properties_provided */
1678 0, /* properties_destroyed */
1679 0, /* todo_flags_start */
1680 TODO_update_ssa, /* todo_flags_finish */
1683 class pass_lower_complex : public gimple_opt_pass
1685 public:
1686 pass_lower_complex (gcc::context *ctxt)
1687 : gimple_opt_pass (pass_data_lower_complex, ctxt)
1690 /* opt_pass methods: */
1691 opt_pass * clone () { return new pass_lower_complex (m_ctxt); }
1692 virtual unsigned int execute (function *) { return tree_lower_complex (); }
1694 }; // class pass_lower_complex
1696 } // anon namespace
1698 gimple_opt_pass *
1699 make_pass_lower_complex (gcc::context *ctxt)
1701 return new pass_lower_complex (ctxt);
1705 namespace {
1707 const pass_data pass_data_lower_complex_O0 =
1709 GIMPLE_PASS, /* type */
1710 "cplxlower0", /* name */
1711 OPTGROUP_NONE, /* optinfo_flags */
1712 TV_NONE, /* tv_id */
1713 PROP_cfg, /* properties_required */
1714 PROP_gimple_lcx, /* properties_provided */
1715 0, /* properties_destroyed */
1716 0, /* todo_flags_start */
1717 TODO_update_ssa, /* todo_flags_finish */
1720 class pass_lower_complex_O0 : public gimple_opt_pass
1722 public:
1723 pass_lower_complex_O0 (gcc::context *ctxt)
1724 : gimple_opt_pass (pass_data_lower_complex_O0, ctxt)
1727 /* opt_pass methods: */
1728 virtual bool gate (function *fun)
1730 /* With errors, normal optimization passes are not run. If we don't
1731 lower complex operations at all, rtl expansion will abort. */
1732 return !(fun->curr_properties & PROP_gimple_lcx);
1735 virtual unsigned int execute (function *) { return tree_lower_complex (); }
1737 }; // class pass_lower_complex_O0
1739 } // anon namespace
1741 gimple_opt_pass *
1742 make_pass_lower_complex_O0 (gcc::context *ctxt)
1744 return new pass_lower_complex_O0 (ctxt);