Daily bump.
[official-gcc.git] / gcc / tree-complex.c
blobc5febcea3eb1c123db491ff53729239e98e20bb8
1 /* Lower complex number operations to scalar operations.
2 Copyright (C) 2004-2020 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 "backend.h"
24 #include "rtl.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "cfghooks.h"
28 #include "tree-pass.h"
29 #include "ssa.h"
30 #include "fold-const.h"
31 #include "stor-layout.h"
32 #include "tree-eh.h"
33 #include "gimplify.h"
34 #include "gimple-iterator.h"
35 #include "gimplify-me.h"
36 #include "tree-cfg.h"
37 #include "tree-dfa.h"
38 #include "tree-ssa.h"
39 #include "tree-ssa-propagate.h"
40 #include "tree-hasher.h"
41 #include "cfgloop.h"
42 #include "cfganal.h"
45 /* For each complex ssa name, a lattice value. We're interested in finding
46 out whether a complex number is degenerate in some way, having only real
47 or only complex parts. */
49 enum
51 UNINITIALIZED = 0,
52 ONLY_REAL = 1,
53 ONLY_IMAG = 2,
54 VARYING = 3
57 /* The type complex_lattice_t holds combinations of the above
58 constants. */
59 typedef int complex_lattice_t;
61 #define PAIR(a, b) ((a) << 2 | (b))
63 class complex_propagate : public ssa_propagation_engine
65 enum ssa_prop_result visit_stmt (gimple *, edge *, tree *) FINAL OVERRIDE;
66 enum ssa_prop_result visit_phi (gphi *) FINAL OVERRIDE;
69 static vec<complex_lattice_t> complex_lattice_values;
71 /* For each complex variable, a pair of variables for the components exists in
72 the hashtable. */
73 static int_tree_htab_type *complex_variable_components;
75 /* For each complex SSA_NAME, a pair of ssa names for the components. */
76 static vec<tree> complex_ssa_name_components;
78 /* Vector of PHI triplets (original complex PHI and corresponding real and
79 imag PHIs if real and/or imag PHIs contain temporarily
80 non-SSA_NAME/non-invariant args that need to be replaced by SSA_NAMEs. */
81 static vec<gphi *> phis_to_revisit;
83 /* BBs that need EH cleanup. */
84 static bitmap need_eh_cleanup;
86 /* Lookup UID in the complex_variable_components hashtable and return the
87 associated tree. */
88 static tree
89 cvc_lookup (unsigned int uid)
91 struct int_tree_map in;
92 in.uid = uid;
93 return complex_variable_components->find_with_hash (in, uid).to;
96 /* Insert the pair UID, TO into the complex_variable_components hashtable. */
98 static void
99 cvc_insert (unsigned int uid, tree to)
101 int_tree_map h;
102 int_tree_map *loc;
104 h.uid = uid;
105 loc = complex_variable_components->find_slot_with_hash (h, uid, INSERT);
106 loc->uid = uid;
107 loc->to = to;
110 /* Return true if T is not a zero constant. In the case of real values,
111 we're only interested in +0.0. */
113 static int
114 some_nonzerop (tree t)
116 int zerop = false;
118 /* Operations with real or imaginary part of a complex number zero
119 cannot be treated the same as operations with a real or imaginary
120 operand if we care about the signs of zeros in the result. */
121 if (TREE_CODE (t) == REAL_CST && !flag_signed_zeros)
122 zerop = real_identical (&TREE_REAL_CST (t), &dconst0);
123 else if (TREE_CODE (t) == FIXED_CST)
124 zerop = fixed_zerop (t);
125 else if (TREE_CODE (t) == INTEGER_CST)
126 zerop = integer_zerop (t);
128 return !zerop;
132 /* Compute a lattice value from the components of a complex type REAL
133 and IMAG. */
135 static complex_lattice_t
136 find_lattice_value_parts (tree real, tree imag)
138 int r, i;
139 complex_lattice_t ret;
141 r = some_nonzerop (real);
142 i = some_nonzerop (imag);
143 ret = r * ONLY_REAL + i * ONLY_IMAG;
145 /* ??? On occasion we could do better than mapping 0+0i to real, but we
146 certainly don't want to leave it UNINITIALIZED, which eventually gets
147 mapped to VARYING. */
148 if (ret == UNINITIALIZED)
149 ret = ONLY_REAL;
151 return ret;
155 /* Compute a lattice value from gimple_val T. */
157 static complex_lattice_t
158 find_lattice_value (tree t)
160 tree real, imag;
162 switch (TREE_CODE (t))
164 case SSA_NAME:
165 return complex_lattice_values[SSA_NAME_VERSION (t)];
167 case COMPLEX_CST:
168 real = TREE_REALPART (t);
169 imag = TREE_IMAGPART (t);
170 break;
172 default:
173 gcc_unreachable ();
176 return find_lattice_value_parts (real, imag);
179 /* Determine if LHS is something for which we're interested in seeing
180 simulation results. */
182 static bool
183 is_complex_reg (tree lhs)
185 return TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE && is_gimple_reg (lhs);
188 /* Mark the incoming parameters to the function as VARYING. */
190 static void
191 init_parameter_lattice_values (void)
193 tree parm, ssa_name;
195 for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = DECL_CHAIN (parm))
196 if (is_complex_reg (parm)
197 && (ssa_name = ssa_default_def (cfun, parm)) != NULL_TREE)
198 complex_lattice_values[SSA_NAME_VERSION (ssa_name)] = VARYING;
201 /* Initialize simulation state for each statement. Return false if we
202 found no statements we want to simulate, and thus there's nothing
203 for the entire pass to do. */
205 static bool
206 init_dont_simulate_again (void)
208 basic_block bb;
209 bool saw_a_complex_op = false;
211 FOR_EACH_BB_FN (bb, cfun)
213 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
214 gsi_next (&gsi))
216 gphi *phi = gsi.phi ();
217 prop_set_simulate_again (phi,
218 is_complex_reg (gimple_phi_result (phi)));
221 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
222 gsi_next (&gsi))
224 gimple *stmt;
225 tree op0, op1;
226 bool sim_again_p;
228 stmt = gsi_stmt (gsi);
229 op0 = op1 = NULL_TREE;
231 /* Most control-altering statements must be initially
232 simulated, else we won't cover the entire cfg. */
233 sim_again_p = stmt_ends_bb_p (stmt);
235 switch (gimple_code (stmt))
237 case GIMPLE_CALL:
238 if (gimple_call_lhs (stmt))
239 sim_again_p = is_complex_reg (gimple_call_lhs (stmt));
240 break;
242 case GIMPLE_ASSIGN:
243 sim_again_p = is_complex_reg (gimple_assign_lhs (stmt));
244 if (gimple_assign_rhs_code (stmt) == REALPART_EXPR
245 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
246 op0 = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
247 else
248 op0 = gimple_assign_rhs1 (stmt);
249 if (gimple_num_ops (stmt) > 2)
250 op1 = gimple_assign_rhs2 (stmt);
251 break;
253 case GIMPLE_COND:
254 op0 = gimple_cond_lhs (stmt);
255 op1 = gimple_cond_rhs (stmt);
256 break;
258 default:
259 break;
262 if (op0 || op1)
263 switch (gimple_expr_code (stmt))
265 case EQ_EXPR:
266 case NE_EXPR:
267 case PLUS_EXPR:
268 case MINUS_EXPR:
269 case MULT_EXPR:
270 case TRUNC_DIV_EXPR:
271 case CEIL_DIV_EXPR:
272 case FLOOR_DIV_EXPR:
273 case ROUND_DIV_EXPR:
274 case RDIV_EXPR:
275 if (TREE_CODE (TREE_TYPE (op0)) == COMPLEX_TYPE
276 || TREE_CODE (TREE_TYPE (op1)) == COMPLEX_TYPE)
277 saw_a_complex_op = true;
278 break;
280 case NEGATE_EXPR:
281 case CONJ_EXPR:
282 if (TREE_CODE (TREE_TYPE (op0)) == COMPLEX_TYPE)
283 saw_a_complex_op = true;
284 break;
286 case REALPART_EXPR:
287 case IMAGPART_EXPR:
288 /* The total store transformation performed during
289 gimplification creates such uninitialized loads
290 and we need to lower the statement to be able
291 to fix things up. */
292 if (TREE_CODE (op0) == SSA_NAME
293 && ssa_undefined_value_p (op0))
294 saw_a_complex_op = true;
295 break;
297 default:
298 break;
301 prop_set_simulate_again (stmt, sim_again_p);
305 return saw_a_complex_op;
309 /* Evaluate statement STMT against the complex lattice defined above. */
311 enum ssa_prop_result
312 complex_propagate::visit_stmt (gimple *stmt, edge *taken_edge_p ATTRIBUTE_UNUSED,
313 tree *result_p)
315 complex_lattice_t new_l, old_l, op1_l, op2_l;
316 unsigned int ver;
317 tree lhs;
319 lhs = gimple_get_lhs (stmt);
320 /* Skip anything but GIMPLE_ASSIGN and GIMPLE_CALL with a lhs. */
321 if (!lhs)
322 return SSA_PROP_VARYING;
324 /* These conditions should be satisfied due to the initial filter
325 set up in init_dont_simulate_again. */
326 gcc_assert (TREE_CODE (lhs) == SSA_NAME);
327 gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE);
329 *result_p = lhs;
330 ver = SSA_NAME_VERSION (lhs);
331 old_l = complex_lattice_values[ver];
333 switch (gimple_expr_code (stmt))
335 case SSA_NAME:
336 case COMPLEX_CST:
337 new_l = find_lattice_value (gimple_assign_rhs1 (stmt));
338 break;
340 case COMPLEX_EXPR:
341 new_l = find_lattice_value_parts (gimple_assign_rhs1 (stmt),
342 gimple_assign_rhs2 (stmt));
343 break;
345 case PLUS_EXPR:
346 case MINUS_EXPR:
347 op1_l = find_lattice_value (gimple_assign_rhs1 (stmt));
348 op2_l = find_lattice_value (gimple_assign_rhs2 (stmt));
350 /* We've set up the lattice values such that IOR neatly
351 models addition. */
352 new_l = op1_l | op2_l;
353 break;
355 case MULT_EXPR:
356 case RDIV_EXPR:
357 case TRUNC_DIV_EXPR:
358 case CEIL_DIV_EXPR:
359 case FLOOR_DIV_EXPR:
360 case ROUND_DIV_EXPR:
361 op1_l = find_lattice_value (gimple_assign_rhs1 (stmt));
362 op2_l = find_lattice_value (gimple_assign_rhs2 (stmt));
364 /* Obviously, if either varies, so does the result. */
365 if (op1_l == VARYING || op2_l == VARYING)
366 new_l = VARYING;
367 /* Don't prematurely promote variables if we've not yet seen
368 their inputs. */
369 else if (op1_l == UNINITIALIZED)
370 new_l = op2_l;
371 else if (op2_l == UNINITIALIZED)
372 new_l = op1_l;
373 else
375 /* At this point both numbers have only one component. If the
376 numbers are of opposite kind, the result is imaginary,
377 otherwise the result is real. The add/subtract translates
378 the real/imag from/to 0/1; the ^ performs the comparison. */
379 new_l = ((op1_l - ONLY_REAL) ^ (op2_l - ONLY_REAL)) + ONLY_REAL;
381 /* Don't allow the lattice value to flip-flop indefinitely. */
382 new_l |= old_l;
384 break;
386 case NEGATE_EXPR:
387 case CONJ_EXPR:
388 new_l = find_lattice_value (gimple_assign_rhs1 (stmt));
389 break;
391 default:
392 new_l = VARYING;
393 break;
396 /* If nothing changed this round, let the propagator know. */
397 if (new_l == old_l)
398 return SSA_PROP_NOT_INTERESTING;
400 complex_lattice_values[ver] = new_l;
401 return new_l == VARYING ? SSA_PROP_VARYING : SSA_PROP_INTERESTING;
404 /* Evaluate a PHI node against the complex lattice defined above. */
406 enum ssa_prop_result
407 complex_propagate::visit_phi (gphi *phi)
409 complex_lattice_t new_l, old_l;
410 unsigned int ver;
411 tree lhs;
412 int i;
414 lhs = gimple_phi_result (phi);
416 /* This condition should be satisfied due to the initial filter
417 set up in init_dont_simulate_again. */
418 gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE);
420 /* We've set up the lattice values such that IOR neatly models PHI meet. */
421 new_l = UNINITIALIZED;
422 for (i = gimple_phi_num_args (phi) - 1; i >= 0; --i)
423 new_l |= find_lattice_value (gimple_phi_arg_def (phi, i));
425 ver = SSA_NAME_VERSION (lhs);
426 old_l = complex_lattice_values[ver];
428 if (new_l == old_l)
429 return SSA_PROP_NOT_INTERESTING;
431 complex_lattice_values[ver] = new_l;
432 return new_l == VARYING ? SSA_PROP_VARYING : SSA_PROP_INTERESTING;
435 /* Create one backing variable for a complex component of ORIG. */
437 static tree
438 create_one_component_var (tree type, tree orig, const char *prefix,
439 const char *suffix, enum tree_code code)
441 tree r = create_tmp_var (type, prefix);
443 DECL_SOURCE_LOCATION (r) = DECL_SOURCE_LOCATION (orig);
444 DECL_ARTIFICIAL (r) = 1;
446 if (DECL_NAME (orig) && !DECL_IGNORED_P (orig))
448 const char *name = IDENTIFIER_POINTER (DECL_NAME (orig));
449 name = ACONCAT ((name, suffix, NULL));
450 DECL_NAME (r) = get_identifier (name);
452 SET_DECL_DEBUG_EXPR (r, build1 (code, type, orig));
453 DECL_HAS_DEBUG_EXPR_P (r) = 1;
454 DECL_IGNORED_P (r) = 0;
455 TREE_NO_WARNING (r) = TREE_NO_WARNING (orig);
457 else
459 DECL_IGNORED_P (r) = 1;
460 TREE_NO_WARNING (r) = 1;
463 return r;
466 /* Retrieve a value for a complex component of VAR. */
468 static tree
469 get_component_var (tree var, bool imag_p)
471 size_t decl_index = DECL_UID (var) * 2 + imag_p;
472 tree ret = cvc_lookup (decl_index);
474 if (ret == NULL)
476 ret = create_one_component_var (TREE_TYPE (TREE_TYPE (var)), var,
477 imag_p ? "CI" : "CR",
478 imag_p ? "$imag" : "$real",
479 imag_p ? IMAGPART_EXPR : REALPART_EXPR);
480 cvc_insert (decl_index, ret);
483 return ret;
486 /* Retrieve a value for a complex component of SSA_NAME. */
488 static tree
489 get_component_ssa_name (tree ssa_name, bool imag_p)
491 complex_lattice_t lattice = find_lattice_value (ssa_name);
492 size_t ssa_name_index;
493 tree ret;
495 if (lattice == (imag_p ? ONLY_REAL : ONLY_IMAG))
497 tree inner_type = TREE_TYPE (TREE_TYPE (ssa_name));
498 if (SCALAR_FLOAT_TYPE_P (inner_type))
499 return build_real (inner_type, dconst0);
500 else
501 return build_int_cst (inner_type, 0);
504 ssa_name_index = SSA_NAME_VERSION (ssa_name) * 2 + imag_p;
505 ret = complex_ssa_name_components[ssa_name_index];
506 if (ret == NULL)
508 if (SSA_NAME_VAR (ssa_name))
509 ret = get_component_var (SSA_NAME_VAR (ssa_name), imag_p);
510 else
511 ret = TREE_TYPE (TREE_TYPE (ssa_name));
512 ret = make_ssa_name (ret);
514 /* Copy some properties from the original. In particular, whether it
515 is used in an abnormal phi, and whether it's uninitialized. */
516 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ret)
517 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name);
518 if (SSA_NAME_IS_DEFAULT_DEF (ssa_name)
519 && TREE_CODE (SSA_NAME_VAR (ssa_name)) == VAR_DECL)
521 SSA_NAME_DEF_STMT (ret) = SSA_NAME_DEF_STMT (ssa_name);
522 set_ssa_default_def (cfun, SSA_NAME_VAR (ret), ret);
525 complex_ssa_name_components[ssa_name_index] = ret;
528 return ret;
531 /* Set a value for a complex component of SSA_NAME, return a
532 gimple_seq of stuff that needs doing. */
534 static gimple_seq
535 set_component_ssa_name (tree ssa_name, bool imag_p, tree value)
537 complex_lattice_t lattice = find_lattice_value (ssa_name);
538 size_t ssa_name_index;
539 tree comp;
540 gimple *last;
541 gimple_seq list;
543 /* We know the value must be zero, else there's a bug in our lattice
544 analysis. But the value may well be a variable known to contain
545 zero. We should be safe ignoring it. */
546 if (lattice == (imag_p ? ONLY_REAL : ONLY_IMAG))
547 return NULL;
549 /* If we've already assigned an SSA_NAME to this component, then this
550 means that our walk of the basic blocks found a use before the set.
551 This is fine. Now we should create an initialization for the value
552 we created earlier. */
553 ssa_name_index = SSA_NAME_VERSION (ssa_name) * 2 + imag_p;
554 comp = complex_ssa_name_components[ssa_name_index];
555 if (comp)
558 /* If we've nothing assigned, and the value we're given is already stable,
559 then install that as the value for this SSA_NAME. This preemptively
560 copy-propagates the value, which avoids unnecessary memory allocation. */
561 else if (is_gimple_min_invariant (value)
562 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name))
564 complex_ssa_name_components[ssa_name_index] = value;
565 return NULL;
567 else if (TREE_CODE (value) == SSA_NAME
568 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name))
570 /* Replace an anonymous base value with the variable from cvc_lookup.
571 This should result in better debug info. */
572 if (SSA_NAME_VAR (ssa_name)
573 && (!SSA_NAME_VAR (value) || DECL_IGNORED_P (SSA_NAME_VAR (value)))
574 && !DECL_IGNORED_P (SSA_NAME_VAR (ssa_name)))
576 comp = get_component_var (SSA_NAME_VAR (ssa_name), imag_p);
577 replace_ssa_name_symbol (value, comp);
580 complex_ssa_name_components[ssa_name_index] = value;
581 return NULL;
584 /* Finally, we need to stabilize the result by installing the value into
585 a new ssa name. */
586 else
587 comp = get_component_ssa_name (ssa_name, imag_p);
589 /* Do all the work to assign VALUE to COMP. */
590 list = NULL;
591 value = force_gimple_operand (value, &list, false, NULL);
592 last = gimple_build_assign (comp, value);
593 gimple_seq_add_stmt (&list, last);
594 gcc_assert (SSA_NAME_DEF_STMT (comp) == last);
596 return list;
599 /* Extract the real or imaginary part of a complex variable or constant.
600 Make sure that it's a proper gimple_val and gimplify it if not.
601 Emit any new code before gsi. */
603 static tree
604 extract_component (gimple_stmt_iterator *gsi, tree t, bool imagpart_p,
605 bool gimple_p, bool phiarg_p = false)
607 switch (TREE_CODE (t))
609 case COMPLEX_CST:
610 return imagpart_p ? TREE_IMAGPART (t) : TREE_REALPART (t);
612 case COMPLEX_EXPR:
613 gcc_unreachable ();
615 case BIT_FIELD_REF:
617 tree inner_type = TREE_TYPE (TREE_TYPE (t));
618 t = unshare_expr (t);
619 TREE_TYPE (t) = inner_type;
620 TREE_OPERAND (t, 1) = TYPE_SIZE (inner_type);
621 if (imagpart_p)
622 TREE_OPERAND (t, 2) = size_binop (PLUS_EXPR, TREE_OPERAND (t, 2),
623 TYPE_SIZE (inner_type));
624 if (gimple_p)
625 t = force_gimple_operand_gsi (gsi, t, true, NULL, true,
626 GSI_SAME_STMT);
627 return t;
630 case VAR_DECL:
631 case RESULT_DECL:
632 case PARM_DECL:
633 case COMPONENT_REF:
634 case ARRAY_REF:
635 case VIEW_CONVERT_EXPR:
636 case MEM_REF:
638 tree inner_type = TREE_TYPE (TREE_TYPE (t));
640 t = build1 ((imagpart_p ? IMAGPART_EXPR : REALPART_EXPR),
641 inner_type, unshare_expr (t));
643 if (gimple_p)
644 t = force_gimple_operand_gsi (gsi, t, true, NULL, true,
645 GSI_SAME_STMT);
647 return t;
650 case SSA_NAME:
651 t = get_component_ssa_name (t, imagpart_p);
652 if (TREE_CODE (t) == SSA_NAME && SSA_NAME_DEF_STMT (t) == NULL)
653 gcc_assert (phiarg_p);
654 return t;
656 default:
657 gcc_unreachable ();
661 /* Update the complex components of the ssa name on the lhs of STMT. */
663 static void
664 update_complex_components (gimple_stmt_iterator *gsi, gimple *stmt, tree r,
665 tree i)
667 tree lhs;
668 gimple_seq list;
670 lhs = gimple_get_lhs (stmt);
672 list = set_component_ssa_name (lhs, false, r);
673 if (list)
674 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
676 list = set_component_ssa_name (lhs, true, i);
677 if (list)
678 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
681 static void
682 update_complex_components_on_edge (edge e, tree lhs, tree r, tree i)
684 gimple_seq list;
686 list = set_component_ssa_name (lhs, false, r);
687 if (list)
688 gsi_insert_seq_on_edge (e, list);
690 list = set_component_ssa_name (lhs, true, i);
691 if (list)
692 gsi_insert_seq_on_edge (e, list);
696 /* Update an assignment to a complex variable in place. */
698 static void
699 update_complex_assignment (gimple_stmt_iterator *gsi, tree r, tree i)
701 gimple *old_stmt = gsi_stmt (*gsi);
702 gimple_assign_set_rhs_with_ops (gsi, COMPLEX_EXPR, r, i);
703 gimple *stmt = gsi_stmt (*gsi);
704 update_stmt (stmt);
705 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
706 bitmap_set_bit (need_eh_cleanup, gimple_bb (stmt)->index);
708 update_complex_components (gsi, gsi_stmt (*gsi), r, i);
712 /* Generate code at the entry point of the function to initialize the
713 component variables for a complex parameter. */
715 static void
716 update_parameter_components (void)
718 edge entry_edge = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun));
719 tree parm;
721 for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = DECL_CHAIN (parm))
723 tree type = TREE_TYPE (parm);
724 tree ssa_name, r, i;
726 if (TREE_CODE (type) != COMPLEX_TYPE || !is_gimple_reg (parm))
727 continue;
729 type = TREE_TYPE (type);
730 ssa_name = ssa_default_def (cfun, parm);
731 if (!ssa_name)
732 continue;
734 r = build1 (REALPART_EXPR, type, ssa_name);
735 i = build1 (IMAGPART_EXPR, type, ssa_name);
736 update_complex_components_on_edge (entry_edge, ssa_name, r, i);
740 /* Generate code to set the component variables of a complex variable
741 to match the PHI statements in block BB. */
743 static void
744 update_phi_components (basic_block bb)
746 gphi_iterator gsi;
748 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
750 gphi *phi = gsi.phi ();
752 if (is_complex_reg (gimple_phi_result (phi)))
754 gphi *p[2] = { NULL, NULL };
755 unsigned int i, j, n;
756 bool revisit_phi = false;
758 for (j = 0; j < 2; j++)
760 tree l = get_component_ssa_name (gimple_phi_result (phi), j > 0);
761 if (TREE_CODE (l) == SSA_NAME)
762 p[j] = create_phi_node (l, bb);
765 for (i = 0, n = gimple_phi_num_args (phi); i < n; ++i)
767 tree comp, arg = gimple_phi_arg_def (phi, i);
768 for (j = 0; j < 2; j++)
769 if (p[j])
771 comp = extract_component (NULL, arg, j > 0, false, true);
772 if (TREE_CODE (comp) == SSA_NAME
773 && SSA_NAME_DEF_STMT (comp) == NULL)
775 /* For the benefit of any gimple simplification during
776 this pass that might walk SSA_NAME def stmts,
777 don't add SSA_NAMEs without definitions into the
778 PHI arguments, but put a decl in there instead
779 temporarily, and revisit this PHI later on. */
780 if (SSA_NAME_VAR (comp))
781 comp = SSA_NAME_VAR (comp);
782 else
783 comp = create_tmp_reg (TREE_TYPE (comp),
784 get_name (comp));
785 revisit_phi = true;
787 SET_PHI_ARG_DEF (p[j], i, comp);
791 if (revisit_phi)
793 phis_to_revisit.safe_push (phi);
794 phis_to_revisit.safe_push (p[0]);
795 phis_to_revisit.safe_push (p[1]);
801 /* Expand a complex move to scalars. */
803 static void
804 expand_complex_move (gimple_stmt_iterator *gsi, tree type)
806 tree inner_type = TREE_TYPE (type);
807 tree r, i, lhs, rhs;
808 gimple *stmt = gsi_stmt (*gsi);
810 if (is_gimple_assign (stmt))
812 lhs = gimple_assign_lhs (stmt);
813 if (gimple_num_ops (stmt) == 2)
814 rhs = gimple_assign_rhs1 (stmt);
815 else
816 rhs = NULL_TREE;
818 else if (is_gimple_call (stmt))
820 lhs = gimple_call_lhs (stmt);
821 rhs = NULL_TREE;
823 else
824 gcc_unreachable ();
826 if (TREE_CODE (lhs) == SSA_NAME)
828 if (is_ctrl_altering_stmt (stmt))
830 edge e;
832 /* The value is not assigned on the exception edges, so we need not
833 concern ourselves there. We do need to update on the fallthru
834 edge. Find it. */
835 e = find_fallthru_edge (gsi_bb (*gsi)->succs);
836 if (!e)
837 gcc_unreachable ();
839 r = build1 (REALPART_EXPR, inner_type, lhs);
840 i = build1 (IMAGPART_EXPR, inner_type, lhs);
841 update_complex_components_on_edge (e, lhs, r, i);
843 else if (is_gimple_call (stmt)
844 || gimple_has_side_effects (stmt)
845 || gimple_assign_rhs_code (stmt) == PAREN_EXPR)
847 r = build1 (REALPART_EXPR, inner_type, lhs);
848 i = build1 (IMAGPART_EXPR, inner_type, lhs);
849 update_complex_components (gsi, stmt, r, i);
851 else
853 if (gimple_assign_rhs_code (stmt) != COMPLEX_EXPR)
855 r = extract_component (gsi, rhs, 0, true);
856 i = extract_component (gsi, rhs, 1, true);
858 else
860 r = gimple_assign_rhs1 (stmt);
861 i = gimple_assign_rhs2 (stmt);
863 update_complex_assignment (gsi, r, i);
866 else if (rhs && TREE_CODE (rhs) == SSA_NAME && !TREE_SIDE_EFFECTS (lhs))
868 tree x;
869 gimple *t;
870 location_t loc;
872 loc = gimple_location (stmt);
873 r = extract_component (gsi, rhs, 0, false);
874 i = extract_component (gsi, rhs, 1, false);
876 x = build1 (REALPART_EXPR, inner_type, unshare_expr (lhs));
877 t = gimple_build_assign (x, r);
878 gimple_set_location (t, loc);
879 gsi_insert_before (gsi, t, GSI_SAME_STMT);
881 if (stmt == gsi_stmt (*gsi))
883 x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
884 gimple_assign_set_lhs (stmt, x);
885 gimple_assign_set_rhs1 (stmt, i);
887 else
889 x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
890 t = gimple_build_assign (x, i);
891 gimple_set_location (t, loc);
892 gsi_insert_before (gsi, t, GSI_SAME_STMT);
894 stmt = gsi_stmt (*gsi);
895 gcc_assert (gimple_code (stmt) == GIMPLE_RETURN);
896 gimple_return_set_retval (as_a <greturn *> (stmt), lhs);
899 update_stmt (stmt);
903 /* Expand complex addition to scalars:
904 a + b = (ar + br) + i(ai + bi)
905 a - b = (ar - br) + i(ai + bi)
908 static void
909 expand_complex_addition (gimple_stmt_iterator *gsi, tree inner_type,
910 tree ar, tree ai, tree br, tree bi,
911 enum tree_code code,
912 complex_lattice_t al, complex_lattice_t bl)
914 tree rr, ri;
916 switch (PAIR (al, bl))
918 case PAIR (ONLY_REAL, ONLY_REAL):
919 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
920 ri = ai;
921 break;
923 case PAIR (ONLY_REAL, ONLY_IMAG):
924 rr = ar;
925 if (code == MINUS_EXPR)
926 ri = gimplify_build2 (gsi, MINUS_EXPR, inner_type, ai, bi);
927 else
928 ri = bi;
929 break;
931 case PAIR (ONLY_IMAG, ONLY_REAL):
932 if (code == MINUS_EXPR)
933 rr = gimplify_build2 (gsi, MINUS_EXPR, inner_type, ar, br);
934 else
935 rr = br;
936 ri = ai;
937 break;
939 case PAIR (ONLY_IMAG, ONLY_IMAG):
940 rr = ar;
941 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
942 break;
944 case PAIR (VARYING, ONLY_REAL):
945 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
946 ri = ai;
947 break;
949 case PAIR (VARYING, ONLY_IMAG):
950 rr = ar;
951 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
952 break;
954 case PAIR (ONLY_REAL, VARYING):
955 if (code == MINUS_EXPR)
956 goto general;
957 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
958 ri = bi;
959 break;
961 case PAIR (ONLY_IMAG, VARYING):
962 if (code == MINUS_EXPR)
963 goto general;
964 rr = br;
965 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
966 break;
968 case PAIR (VARYING, VARYING):
969 general:
970 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
971 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
972 break;
974 default:
975 gcc_unreachable ();
978 update_complex_assignment (gsi, rr, ri);
981 /* Expand a complex multiplication or division to a libcall to the c99
982 compliant routines. TYPE is the complex type of the operation.
983 If INPLACE_P replace the statement at GSI with
984 the libcall and return NULL_TREE. Else insert the call, assign its
985 result to an output variable and return that variable. If INPLACE_P
986 is true then the statement being replaced should be an assignment
987 statement. */
989 static tree
990 expand_complex_libcall (gimple_stmt_iterator *gsi, tree type, tree ar, tree ai,
991 tree br, tree bi, enum tree_code code, bool inplace_p)
993 machine_mode mode;
994 enum built_in_function bcode;
995 tree fn, lhs;
996 gcall *stmt;
998 mode = TYPE_MODE (type);
999 gcc_assert (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT);
1001 if (code == MULT_EXPR)
1002 bcode = ((enum built_in_function)
1003 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
1004 else if (code == RDIV_EXPR)
1005 bcode = ((enum built_in_function)
1006 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
1007 else
1008 gcc_unreachable ();
1009 fn = builtin_decl_explicit (bcode);
1010 stmt = gimple_build_call (fn, 4, ar, ai, br, bi);
1012 if (inplace_p)
1014 gimple *old_stmt = gsi_stmt (*gsi);
1015 gimple_call_set_nothrow (stmt, !stmt_could_throw_p (cfun, old_stmt));
1016 lhs = gimple_assign_lhs (old_stmt);
1017 gimple_call_set_lhs (stmt, lhs);
1018 gsi_replace (gsi, stmt, true);
1020 type = TREE_TYPE (type);
1021 if (stmt_can_throw_internal (cfun, stmt))
1023 edge_iterator ei;
1024 edge e;
1025 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
1026 if (!(e->flags & EDGE_EH))
1027 break;
1028 basic_block bb = split_edge (e);
1029 gimple_stmt_iterator gsi2 = gsi_start_bb (bb);
1030 update_complex_components (&gsi2, stmt,
1031 build1 (REALPART_EXPR, type, lhs),
1032 build1 (IMAGPART_EXPR, type, lhs));
1033 return NULL_TREE;
1035 else
1036 update_complex_components (gsi, stmt,
1037 build1 (REALPART_EXPR, type, lhs),
1038 build1 (IMAGPART_EXPR, type, lhs));
1039 SSA_NAME_DEF_STMT (lhs) = stmt;
1040 return NULL_TREE;
1043 gimple_call_set_nothrow (stmt, true);
1044 lhs = make_ssa_name (type);
1045 gimple_call_set_lhs (stmt, lhs);
1046 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1048 return lhs;
1051 /* Perform a complex multiplication on two complex constants A, B represented
1052 by AR, AI, BR, BI of type TYPE.
1053 The operation we want is: a * b = (ar*br - ai*bi) + i(ar*bi + br*ai).
1054 Insert the GIMPLE statements into GSI. Store the real and imaginary
1055 components of the result into RR and RI. */
1057 static void
1058 expand_complex_multiplication_components (gimple_stmt_iterator *gsi,
1059 tree type, tree ar, tree ai,
1060 tree br, tree bi,
1061 tree *rr, tree *ri)
1063 tree t1, t2, t3, t4;
1065 t1 = gimplify_build2 (gsi, MULT_EXPR, type, ar, br);
1066 t2 = gimplify_build2 (gsi, MULT_EXPR, type, ai, bi);
1067 t3 = gimplify_build2 (gsi, MULT_EXPR, type, ar, bi);
1069 /* Avoid expanding redundant multiplication for the common
1070 case of squaring a complex number. */
1071 if (ar == br && ai == bi)
1072 t4 = t3;
1073 else
1074 t4 = gimplify_build2 (gsi, MULT_EXPR, type, ai, br);
1076 *rr = gimplify_build2 (gsi, MINUS_EXPR, type, t1, t2);
1077 *ri = gimplify_build2 (gsi, PLUS_EXPR, type, t3, t4);
1080 /* Expand complex multiplication to scalars:
1081 a * b = (ar*br - ai*bi) + i(ar*bi + br*ai)
1084 static void
1085 expand_complex_multiplication (gimple_stmt_iterator *gsi, tree type,
1086 tree ar, tree ai, tree br, tree bi,
1087 complex_lattice_t al, complex_lattice_t bl)
1089 tree rr, ri;
1090 tree inner_type = TREE_TYPE (type);
1092 if (al < bl)
1094 complex_lattice_t tl;
1095 rr = ar, ar = br, br = rr;
1096 ri = ai, ai = bi, bi = ri;
1097 tl = al, al = bl, bl = tl;
1100 switch (PAIR (al, bl))
1102 case PAIR (ONLY_REAL, ONLY_REAL):
1103 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1104 ri = ai;
1105 break;
1107 case PAIR (ONLY_IMAG, ONLY_REAL):
1108 rr = ar;
1109 if (TREE_CODE (ai) == REAL_CST
1110 && real_identical (&TREE_REAL_CST (ai), &dconst1))
1111 ri = br;
1112 else
1113 ri = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1114 break;
1116 case PAIR (ONLY_IMAG, ONLY_IMAG):
1117 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1118 rr = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, rr);
1119 ri = ar;
1120 break;
1122 case PAIR (VARYING, ONLY_REAL):
1123 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1124 ri = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1125 break;
1127 case PAIR (VARYING, ONLY_IMAG):
1128 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1129 rr = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, rr);
1130 ri = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, bi);
1131 break;
1133 case PAIR (VARYING, VARYING):
1134 if (flag_complex_method == 2 && SCALAR_FLOAT_TYPE_P (inner_type))
1136 /* If optimizing for size or not at all just do a libcall.
1137 Same if there are exception-handling edges or signaling NaNs. */
1138 if (optimize == 0 || optimize_bb_for_size_p (gsi_bb (*gsi))
1139 || stmt_can_throw_internal (cfun, gsi_stmt (*gsi))
1140 || flag_signaling_nans)
1142 expand_complex_libcall (gsi, type, ar, ai, br, bi,
1143 MULT_EXPR, true);
1144 return;
1147 if (!HONOR_NANS (inner_type))
1149 /* If we are not worrying about NaNs expand to
1150 (ar*br - ai*bi) + i(ar*bi + br*ai) directly. */
1151 expand_complex_multiplication_components (gsi, inner_type,
1152 ar, ai, br, bi,
1153 &rr, &ri);
1154 break;
1157 /* Else, expand x = a * b into
1158 x = (ar*br - ai*bi) + i(ar*bi + br*ai);
1159 if (isunordered (__real__ x, __imag__ x))
1160 x = __muldc3 (a, b); */
1162 tree tmpr, tmpi;
1163 expand_complex_multiplication_components (gsi, inner_type, ar, ai,
1164 br, bi, &tmpr, &tmpi);
1166 gimple *check
1167 = gimple_build_cond (UNORDERED_EXPR, tmpr, tmpi,
1168 NULL_TREE, NULL_TREE);
1170 basic_block orig_bb = gsi_bb (*gsi);
1171 /* We want to keep track of the original complex multiplication
1172 statement as we're going to modify it later in
1173 update_complex_assignment. Make sure that insert_cond_bb leaves
1174 that statement in the join block. */
1175 gsi_prev (gsi);
1176 basic_block cond_bb
1177 = insert_cond_bb (gsi_bb (*gsi), gsi_stmt (*gsi), check,
1178 profile_probability::very_unlikely ());
1180 gimple_stmt_iterator cond_bb_gsi = gsi_last_bb (cond_bb);
1181 gsi_insert_after (&cond_bb_gsi, gimple_build_nop (), GSI_NEW_STMT);
1183 tree libcall_res
1184 = expand_complex_libcall (&cond_bb_gsi, type, ar, ai, br,
1185 bi, MULT_EXPR, false);
1186 tree cond_real = gimplify_build1 (&cond_bb_gsi, REALPART_EXPR,
1187 inner_type, libcall_res);
1188 tree cond_imag = gimplify_build1 (&cond_bb_gsi, IMAGPART_EXPR,
1189 inner_type, libcall_res);
1191 basic_block join_bb = single_succ_edge (cond_bb)->dest;
1192 *gsi = gsi_start_nondebug_after_labels_bb (join_bb);
1194 /* We have a conditional block with some assignments in cond_bb.
1195 Wire up the PHIs to wrap up. */
1196 rr = make_ssa_name (inner_type);
1197 ri = make_ssa_name (inner_type);
1198 edge cond_to_join = single_succ_edge (cond_bb);
1199 edge orig_to_join = find_edge (orig_bb, join_bb);
1201 gphi *real_phi = create_phi_node (rr, gsi_bb (*gsi));
1202 add_phi_arg (real_phi, cond_real, cond_to_join, UNKNOWN_LOCATION);
1203 add_phi_arg (real_phi, tmpr, orig_to_join, UNKNOWN_LOCATION);
1205 gphi *imag_phi = create_phi_node (ri, gsi_bb (*gsi));
1206 add_phi_arg (imag_phi, cond_imag, cond_to_join, UNKNOWN_LOCATION);
1207 add_phi_arg (imag_phi, tmpi, orig_to_join, UNKNOWN_LOCATION);
1209 else
1210 /* If we are not worrying about NaNs expand to
1211 (ar*br - ai*bi) + i(ar*bi + br*ai) directly. */
1212 expand_complex_multiplication_components (gsi, inner_type, ar, ai,
1213 br, bi, &rr, &ri);
1214 break;
1216 default:
1217 gcc_unreachable ();
1220 update_complex_assignment (gsi, rr, ri);
1223 /* Keep this algorithm in sync with fold-const.c:const_binop().
1225 Expand complex division to scalars, straightforward algorithm.
1226 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1227 t = br*br + bi*bi
1230 static void
1231 expand_complex_div_straight (gimple_stmt_iterator *gsi, tree inner_type,
1232 tree ar, tree ai, tree br, tree bi,
1233 enum tree_code code)
1235 tree rr, ri, div, t1, t2, t3;
1237 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, br, br);
1238 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, bi, bi);
1239 div = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, t2);
1241 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1242 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1243 t3 = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, t2);
1244 rr = gimplify_build2 (gsi, code, inner_type, t3, div);
1246 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1247 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, bi);
1248 t3 = gimplify_build2 (gsi, MINUS_EXPR, inner_type, t1, t2);
1249 ri = gimplify_build2 (gsi, code, inner_type, t3, div);
1251 update_complex_assignment (gsi, rr, ri);
1254 /* Keep this algorithm in sync with fold-const.c:const_binop().
1256 Expand complex division to scalars, modified algorithm to minimize
1257 overflow with wide input ranges. */
1259 static void
1260 expand_complex_div_wide (gimple_stmt_iterator *gsi, tree inner_type,
1261 tree ar, tree ai, tree br, tree bi,
1262 enum tree_code code)
1264 tree rr, ri, ratio, div, t1, t2, tr, ti, compare;
1265 basic_block bb_cond, bb_true, bb_false, bb_join;
1266 gimple *stmt;
1268 /* Examine |br| < |bi|, and branch. */
1269 t1 = gimplify_build1 (gsi, ABS_EXPR, inner_type, br);
1270 t2 = gimplify_build1 (gsi, ABS_EXPR, inner_type, bi);
1271 compare = fold_build2_loc (gimple_location (gsi_stmt (*gsi)),
1272 LT_EXPR, boolean_type_node, t1, t2);
1273 STRIP_NOPS (compare);
1275 bb_cond = bb_true = bb_false = bb_join = NULL;
1276 rr = ri = tr = ti = NULL;
1277 if (TREE_CODE (compare) != INTEGER_CST)
1279 edge e;
1280 gimple *stmt;
1281 tree cond, tmp;
1283 tmp = make_ssa_name (boolean_type_node);
1284 stmt = gimple_build_assign (tmp, compare);
1285 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1287 cond = fold_build2_loc (gimple_location (stmt),
1288 EQ_EXPR, boolean_type_node, tmp, boolean_true_node);
1289 stmt = gimple_build_cond_from_tree (cond, NULL_TREE, NULL_TREE);
1290 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1292 /* Split the original block, and create the TRUE and FALSE blocks. */
1293 e = split_block (gsi_bb (*gsi), stmt);
1294 bb_cond = e->src;
1295 bb_join = e->dest;
1296 bb_true = create_empty_bb (bb_cond);
1297 bb_false = create_empty_bb (bb_true);
1298 bb_true->count = bb_false->count
1299 = bb_cond->count.apply_probability (profile_probability::even ());
1301 /* Wire the blocks together. */
1302 e->flags = EDGE_TRUE_VALUE;
1303 /* TODO: With value profile we could add an historgram to determine real
1304 branch outcome. */
1305 e->probability = profile_probability::even ();
1306 redirect_edge_succ (e, bb_true);
1307 edge e2 = make_edge (bb_cond, bb_false, EDGE_FALSE_VALUE);
1308 e2->probability = profile_probability::even ();
1309 make_single_succ_edge (bb_true, bb_join, EDGE_FALLTHRU);
1310 make_single_succ_edge (bb_false, bb_join, EDGE_FALLTHRU);
1311 add_bb_to_loop (bb_true, bb_cond->loop_father);
1312 add_bb_to_loop (bb_false, bb_cond->loop_father);
1314 /* Update dominance info. Note that bb_join's data was
1315 updated by split_block. */
1316 if (dom_info_available_p (CDI_DOMINATORS))
1318 set_immediate_dominator (CDI_DOMINATORS, bb_true, bb_cond);
1319 set_immediate_dominator (CDI_DOMINATORS, bb_false, bb_cond);
1322 rr = create_tmp_reg (inner_type);
1323 ri = create_tmp_reg (inner_type);
1326 /* In the TRUE branch, we compute
1327 ratio = br/bi;
1328 div = (br * ratio) + bi;
1329 tr = (ar * ratio) + ai;
1330 ti = (ai * ratio) - ar;
1331 tr = tr / div;
1332 ti = ti / div; */
1333 if (bb_true || integer_nonzerop (compare))
1335 if (bb_true)
1337 *gsi = gsi_last_bb (bb_true);
1338 gsi_insert_after (gsi, gimple_build_nop (), GSI_NEW_STMT);
1341 ratio = gimplify_build2 (gsi, code, inner_type, br, bi);
1343 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, br, ratio);
1344 div = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, bi);
1346 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, ratio);
1347 tr = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, ai);
1349 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, ratio);
1350 ti = gimplify_build2 (gsi, MINUS_EXPR, inner_type, t1, ar);
1352 tr = gimplify_build2 (gsi, code, inner_type, tr, div);
1353 ti = gimplify_build2 (gsi, code, inner_type, ti, div);
1355 if (bb_true)
1357 stmt = gimple_build_assign (rr, tr);
1358 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1359 stmt = gimple_build_assign (ri, ti);
1360 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1361 gsi_remove (gsi, true);
1365 /* In the FALSE branch, we compute
1366 ratio = d/c;
1367 divisor = (d * ratio) + c;
1368 tr = (b * ratio) + a;
1369 ti = b - (a * ratio);
1370 tr = tr / div;
1371 ti = ti / div; */
1372 if (bb_false || integer_zerop (compare))
1374 if (bb_false)
1376 *gsi = gsi_last_bb (bb_false);
1377 gsi_insert_after (gsi, gimple_build_nop (), GSI_NEW_STMT);
1380 ratio = gimplify_build2 (gsi, code, inner_type, bi, br);
1382 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, bi, ratio);
1383 div = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, br);
1385 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, ratio);
1386 tr = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, ar);
1388 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, ratio);
1389 ti = gimplify_build2 (gsi, MINUS_EXPR, inner_type, ai, t1);
1391 tr = gimplify_build2 (gsi, code, inner_type, tr, div);
1392 ti = gimplify_build2 (gsi, code, inner_type, ti, div);
1394 if (bb_false)
1396 stmt = gimple_build_assign (rr, tr);
1397 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1398 stmt = gimple_build_assign (ri, ti);
1399 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1400 gsi_remove (gsi, true);
1404 if (bb_join)
1405 *gsi = gsi_start_bb (bb_join);
1406 else
1407 rr = tr, ri = ti;
1409 update_complex_assignment (gsi, rr, ri);
1412 /* Expand complex division to scalars. */
1414 static void
1415 expand_complex_division (gimple_stmt_iterator *gsi, tree type,
1416 tree ar, tree ai, tree br, tree bi,
1417 enum tree_code code,
1418 complex_lattice_t al, complex_lattice_t bl)
1420 tree rr, ri;
1422 tree inner_type = TREE_TYPE (type);
1423 switch (PAIR (al, bl))
1425 case PAIR (ONLY_REAL, ONLY_REAL):
1426 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
1427 ri = ai;
1428 break;
1430 case PAIR (ONLY_REAL, ONLY_IMAG):
1431 rr = ai;
1432 ri = gimplify_build2 (gsi, code, inner_type, ar, bi);
1433 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ri);
1434 break;
1436 case PAIR (ONLY_IMAG, ONLY_REAL):
1437 rr = ar;
1438 ri = gimplify_build2 (gsi, code, inner_type, ai, br);
1439 break;
1441 case PAIR (ONLY_IMAG, ONLY_IMAG):
1442 rr = gimplify_build2 (gsi, code, inner_type, ai, bi);
1443 ri = ar;
1444 break;
1446 case PAIR (VARYING, ONLY_REAL):
1447 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
1448 ri = gimplify_build2 (gsi, code, inner_type, ai, br);
1449 break;
1451 case PAIR (VARYING, ONLY_IMAG):
1452 rr = gimplify_build2 (gsi, code, inner_type, ai, bi);
1453 ri = gimplify_build2 (gsi, code, inner_type, ar, bi);
1454 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ri);
1455 break;
1457 case PAIR (ONLY_REAL, VARYING):
1458 case PAIR (ONLY_IMAG, VARYING):
1459 case PAIR (VARYING, VARYING):
1460 switch (flag_complex_method)
1462 case 0:
1463 /* straightforward implementation of complex divide acceptable. */
1464 expand_complex_div_straight (gsi, inner_type, ar, ai, br, bi, code);
1465 break;
1467 case 2:
1468 if (SCALAR_FLOAT_TYPE_P (inner_type))
1470 expand_complex_libcall (gsi, type, ar, ai, br, bi, code, true);
1471 break;
1473 /* FALLTHRU */
1475 case 1:
1476 /* wide ranges of inputs must work for complex divide. */
1477 expand_complex_div_wide (gsi, inner_type, ar, ai, br, bi, code);
1478 break;
1480 default:
1481 gcc_unreachable ();
1483 return;
1485 default:
1486 gcc_unreachable ();
1489 update_complex_assignment (gsi, rr, ri);
1492 /* Expand complex negation to scalars:
1493 -a = (-ar) + i(-ai)
1496 static void
1497 expand_complex_negation (gimple_stmt_iterator *gsi, tree inner_type,
1498 tree ar, tree ai)
1500 tree rr, ri;
1502 rr = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ar);
1503 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ai);
1505 update_complex_assignment (gsi, rr, ri);
1508 /* Expand complex conjugate to scalars:
1509 ~a = (ar) + i(-ai)
1512 static void
1513 expand_complex_conjugate (gimple_stmt_iterator *gsi, tree inner_type,
1514 tree ar, tree ai)
1516 tree ri;
1518 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ai);
1520 update_complex_assignment (gsi, ar, ri);
1523 /* Expand complex comparison (EQ or NE only). */
1525 static void
1526 expand_complex_comparison (gimple_stmt_iterator *gsi, tree ar, tree ai,
1527 tree br, tree bi, enum tree_code code)
1529 tree cr, ci, cc, type;
1530 gimple *stmt;
1532 cr = gimplify_build2 (gsi, code, boolean_type_node, ar, br);
1533 ci = gimplify_build2 (gsi, code, boolean_type_node, ai, bi);
1534 cc = gimplify_build2 (gsi,
1535 (code == EQ_EXPR ? TRUTH_AND_EXPR : TRUTH_OR_EXPR),
1536 boolean_type_node, cr, ci);
1538 stmt = gsi_stmt (*gsi);
1540 switch (gimple_code (stmt))
1542 case GIMPLE_RETURN:
1544 greturn *return_stmt = as_a <greturn *> (stmt);
1545 type = TREE_TYPE (gimple_return_retval (return_stmt));
1546 gimple_return_set_retval (return_stmt, fold_convert (type, cc));
1548 break;
1550 case GIMPLE_ASSIGN:
1551 type = TREE_TYPE (gimple_assign_lhs (stmt));
1552 gimple_assign_set_rhs_from_tree (gsi, fold_convert (type, cc));
1553 stmt = gsi_stmt (*gsi);
1554 break;
1556 case GIMPLE_COND:
1558 gcond *cond_stmt = as_a <gcond *> (stmt);
1559 gimple_cond_set_code (cond_stmt, EQ_EXPR);
1560 gimple_cond_set_lhs (cond_stmt, cc);
1561 gimple_cond_set_rhs (cond_stmt, boolean_true_node);
1563 break;
1565 default:
1566 gcc_unreachable ();
1569 update_stmt (stmt);
1570 if (maybe_clean_eh_stmt (stmt))
1571 bitmap_set_bit (need_eh_cleanup, gimple_bb (stmt)->index);
1574 /* Expand inline asm that sets some complex SSA_NAMEs. */
1576 static void
1577 expand_complex_asm (gimple_stmt_iterator *gsi)
1579 gasm *stmt = as_a <gasm *> (gsi_stmt (*gsi));
1580 unsigned int i;
1582 for (i = 0; i < gimple_asm_noutputs (stmt); ++i)
1584 tree link = gimple_asm_output_op (stmt, i);
1585 tree op = TREE_VALUE (link);
1586 if (TREE_CODE (op) == SSA_NAME
1587 && TREE_CODE (TREE_TYPE (op)) == COMPLEX_TYPE)
1589 tree type = TREE_TYPE (op);
1590 tree inner_type = TREE_TYPE (type);
1591 tree r = build1 (REALPART_EXPR, inner_type, op);
1592 tree i = build1 (IMAGPART_EXPR, inner_type, op);
1593 gimple_seq list = set_component_ssa_name (op, false, r);
1595 if (list)
1596 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
1598 list = set_component_ssa_name (op, true, i);
1599 if (list)
1600 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
1605 /* Process one statement. If we identify a complex operation, expand it. */
1607 static void
1608 expand_complex_operations_1 (gimple_stmt_iterator *gsi)
1610 gimple *stmt = gsi_stmt (*gsi);
1611 tree type, inner_type, lhs;
1612 tree ac, ar, ai, bc, br, bi;
1613 complex_lattice_t al, bl;
1614 enum tree_code code;
1616 if (gimple_code (stmt) == GIMPLE_ASM)
1618 expand_complex_asm (gsi);
1619 return;
1622 lhs = gimple_get_lhs (stmt);
1623 if (!lhs && gimple_code (stmt) != GIMPLE_COND)
1624 return;
1626 type = TREE_TYPE (gimple_op (stmt, 0));
1627 code = gimple_expr_code (stmt);
1629 /* Initial filter for operations we handle. */
1630 switch (code)
1632 case PLUS_EXPR:
1633 case MINUS_EXPR:
1634 case MULT_EXPR:
1635 case TRUNC_DIV_EXPR:
1636 case CEIL_DIV_EXPR:
1637 case FLOOR_DIV_EXPR:
1638 case ROUND_DIV_EXPR:
1639 case RDIV_EXPR:
1640 case NEGATE_EXPR:
1641 case CONJ_EXPR:
1642 if (TREE_CODE (type) != COMPLEX_TYPE)
1643 return;
1644 inner_type = TREE_TYPE (type);
1645 break;
1647 case EQ_EXPR:
1648 case NE_EXPR:
1649 /* Note, both GIMPLE_ASSIGN and GIMPLE_COND may have an EQ_EXPR
1650 subcode, so we need to access the operands using gimple_op. */
1651 inner_type = TREE_TYPE (gimple_op (stmt, 1));
1652 if (TREE_CODE (inner_type) != COMPLEX_TYPE)
1653 return;
1654 break;
1656 default:
1658 tree rhs;
1660 /* GIMPLE_COND may also fallthru here, but we do not need to
1661 do anything with it. */
1662 if (gimple_code (stmt) == GIMPLE_COND)
1663 return;
1665 if (TREE_CODE (type) == COMPLEX_TYPE)
1666 expand_complex_move (gsi, type);
1667 else if (is_gimple_assign (stmt)
1668 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
1669 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
1670 && TREE_CODE (lhs) == SSA_NAME)
1672 rhs = gimple_assign_rhs1 (stmt);
1673 rhs = extract_component (gsi, TREE_OPERAND (rhs, 0),
1674 gimple_assign_rhs_code (stmt)
1675 == IMAGPART_EXPR,
1676 false);
1677 gimple_assign_set_rhs_from_tree (gsi, rhs);
1678 stmt = gsi_stmt (*gsi);
1679 update_stmt (stmt);
1682 return;
1685 /* Extract the components of the two complex values. Make sure and
1686 handle the common case of the same value used twice specially. */
1687 if (is_gimple_assign (stmt))
1689 ac = gimple_assign_rhs1 (stmt);
1690 bc = (gimple_num_ops (stmt) > 2) ? gimple_assign_rhs2 (stmt) : NULL;
1692 /* GIMPLE_CALL cannot get here. */
1693 else
1695 ac = gimple_cond_lhs (stmt);
1696 bc = gimple_cond_rhs (stmt);
1699 ar = extract_component (gsi, ac, false, true);
1700 ai = extract_component (gsi, ac, true, true);
1702 if (ac == bc)
1703 br = ar, bi = ai;
1704 else if (bc)
1706 br = extract_component (gsi, bc, 0, true);
1707 bi = extract_component (gsi, bc, 1, true);
1709 else
1710 br = bi = NULL_TREE;
1712 al = find_lattice_value (ac);
1713 if (al == UNINITIALIZED)
1714 al = VARYING;
1716 if (TREE_CODE_CLASS (code) == tcc_unary)
1717 bl = UNINITIALIZED;
1718 else if (ac == bc)
1719 bl = al;
1720 else
1722 bl = find_lattice_value (bc);
1723 if (bl == UNINITIALIZED)
1724 bl = VARYING;
1727 switch (code)
1729 case PLUS_EXPR:
1730 case MINUS_EXPR:
1731 expand_complex_addition (gsi, inner_type, ar, ai, br, bi, code, al, bl);
1732 break;
1734 case MULT_EXPR:
1735 expand_complex_multiplication (gsi, type, ar, ai, br, bi, al, bl);
1736 break;
1738 case TRUNC_DIV_EXPR:
1739 case CEIL_DIV_EXPR:
1740 case FLOOR_DIV_EXPR:
1741 case ROUND_DIV_EXPR:
1742 case RDIV_EXPR:
1743 expand_complex_division (gsi, type, ar, ai, br, bi, code, al, bl);
1744 break;
1746 case NEGATE_EXPR:
1747 expand_complex_negation (gsi, inner_type, ar, ai);
1748 break;
1750 case CONJ_EXPR:
1751 expand_complex_conjugate (gsi, inner_type, ar, ai);
1752 break;
1754 case EQ_EXPR:
1755 case NE_EXPR:
1756 expand_complex_comparison (gsi, ar, ai, br, bi, code);
1757 break;
1759 default:
1760 gcc_unreachable ();
1765 /* Entry point for complex operation lowering during optimization. */
1767 static unsigned int
1768 tree_lower_complex (void)
1770 gimple_stmt_iterator gsi;
1771 basic_block bb;
1772 int n_bbs, i;
1773 int *rpo;
1775 if (!init_dont_simulate_again ())
1776 return 0;
1778 complex_lattice_values.create (num_ssa_names);
1779 complex_lattice_values.safe_grow_cleared (num_ssa_names);
1781 init_parameter_lattice_values ();
1782 class complex_propagate complex_propagate;
1783 complex_propagate.ssa_propagate ();
1785 need_eh_cleanup = BITMAP_ALLOC (NULL);
1787 complex_variable_components = new int_tree_htab_type (10);
1789 complex_ssa_name_components.create (2 * num_ssa_names);
1790 complex_ssa_name_components.safe_grow_cleared (2 * num_ssa_names);
1792 update_parameter_components ();
1794 rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
1795 n_bbs = pre_and_rev_post_order_compute (NULL, rpo, false);
1796 for (i = 0; i < n_bbs; i++)
1798 bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
1799 if (!bb)
1800 continue;
1801 update_phi_components (bb);
1802 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1803 expand_complex_operations_1 (&gsi);
1806 free (rpo);
1808 if (!phis_to_revisit.is_empty ())
1810 unsigned int n = phis_to_revisit.length ();
1811 for (unsigned int j = 0; j < n; j += 3)
1812 for (unsigned int k = 0; k < 2; k++)
1813 if (gphi *phi = phis_to_revisit[j + k + 1])
1815 unsigned int m = gimple_phi_num_args (phi);
1816 for (unsigned int l = 0; l < m; ++l)
1818 tree op = gimple_phi_arg_def (phi, l);
1819 if (TREE_CODE (op) == SSA_NAME
1820 || is_gimple_min_invariant (op))
1821 continue;
1822 tree arg = gimple_phi_arg_def (phis_to_revisit[j], l);
1823 op = extract_component (NULL, arg, k > 0, false, false);
1824 SET_PHI_ARG_DEF (phi, l, op);
1827 phis_to_revisit.release ();
1830 gsi_commit_edge_inserts ();
1832 unsigned todo
1833 = gimple_purge_all_dead_eh_edges (need_eh_cleanup) ? TODO_cleanup_cfg : 0;
1834 BITMAP_FREE (need_eh_cleanup);
1836 delete complex_variable_components;
1837 complex_variable_components = NULL;
1838 complex_ssa_name_components.release ();
1839 complex_lattice_values.release ();
1840 return todo;
1843 namespace {
1845 const pass_data pass_data_lower_complex =
1847 GIMPLE_PASS, /* type */
1848 "cplxlower", /* name */
1849 OPTGROUP_NONE, /* optinfo_flags */
1850 TV_NONE, /* tv_id */
1851 PROP_ssa, /* properties_required */
1852 PROP_gimple_lcx, /* properties_provided */
1853 0, /* properties_destroyed */
1854 0, /* todo_flags_start */
1855 TODO_update_ssa, /* todo_flags_finish */
1858 class pass_lower_complex : public gimple_opt_pass
1860 public:
1861 pass_lower_complex (gcc::context *ctxt)
1862 : gimple_opt_pass (pass_data_lower_complex, ctxt)
1865 /* opt_pass methods: */
1866 opt_pass * clone () { return new pass_lower_complex (m_ctxt); }
1867 virtual unsigned int execute (function *) { return tree_lower_complex (); }
1869 }; // class pass_lower_complex
1871 } // anon namespace
1873 gimple_opt_pass *
1874 make_pass_lower_complex (gcc::context *ctxt)
1876 return new pass_lower_complex (ctxt);
1880 namespace {
1882 const pass_data pass_data_lower_complex_O0 =
1884 GIMPLE_PASS, /* type */
1885 "cplxlower0", /* name */
1886 OPTGROUP_NONE, /* optinfo_flags */
1887 TV_NONE, /* tv_id */
1888 PROP_cfg, /* properties_required */
1889 PROP_gimple_lcx, /* properties_provided */
1890 0, /* properties_destroyed */
1891 0, /* todo_flags_start */
1892 TODO_update_ssa, /* todo_flags_finish */
1895 class pass_lower_complex_O0 : public gimple_opt_pass
1897 public:
1898 pass_lower_complex_O0 (gcc::context *ctxt)
1899 : gimple_opt_pass (pass_data_lower_complex_O0, ctxt)
1902 /* opt_pass methods: */
1903 virtual bool gate (function *fun)
1905 /* With errors, normal optimization passes are not run. If we don't
1906 lower complex operations at all, rtl expansion will abort. */
1907 return !(fun->curr_properties & PROP_gimple_lcx);
1910 virtual unsigned int execute (function *) { return tree_lower_complex (); }
1912 }; // class pass_lower_complex_O0
1914 } // anon namespace
1916 gimple_opt_pass *
1917 make_pass_lower_complex_O0 (gcc::context *ctxt)
1919 return new pass_lower_complex_O0 (ctxt);