Revise -mdisable-fpregs option and add new -msoft-mult option
[official-gcc.git] / gcc / tree-complex.c
bloba528cdc7feec3a97f7c30d280962504632fcaf25
1 /* Lower complex number operations to scalar operations.
2 Copyright (C) 2004-2021 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"
43 #include "gimple-fold.h"
46 /* For each complex ssa name, a lattice value. We're interested in finding
47 out whether a complex number is degenerate in some way, having only real
48 or only complex parts. */
50 enum
52 UNINITIALIZED = 0,
53 ONLY_REAL = 1,
54 ONLY_IMAG = 2,
55 VARYING = 3
58 /* The type complex_lattice_t holds combinations of the above
59 constants. */
60 typedef int complex_lattice_t;
62 #define PAIR(a, b) ((a) << 2 | (b))
64 class complex_propagate : public ssa_propagation_engine
66 enum ssa_prop_result visit_stmt (gimple *, edge *, tree *) FINAL OVERRIDE;
67 enum ssa_prop_result visit_phi (gphi *) FINAL OVERRIDE;
70 static vec<complex_lattice_t> complex_lattice_values;
72 /* For each complex variable, a pair of variables for the components exists in
73 the hashtable. */
74 static int_tree_htab_type *complex_variable_components;
76 /* For each complex SSA_NAME, a pair of ssa names for the components. */
77 static vec<tree> complex_ssa_name_components;
79 /* Vector of PHI triplets (original complex PHI and corresponding real and
80 imag PHIs if real and/or imag PHIs contain temporarily
81 non-SSA_NAME/non-invariant args that need to be replaced by SSA_NAMEs. */
82 static vec<gphi *> phis_to_revisit;
84 /* BBs that need EH cleanup. */
85 static bitmap need_eh_cleanup;
87 /* Lookup UID in the complex_variable_components hashtable and return the
88 associated tree. */
89 static tree
90 cvc_lookup (unsigned int uid)
92 struct int_tree_map in;
93 in.uid = uid;
94 return complex_variable_components->find_with_hash (in, uid).to;
97 /* Insert the pair UID, TO into the complex_variable_components hashtable. */
99 static void
100 cvc_insert (unsigned int uid, tree to)
102 int_tree_map h;
103 int_tree_map *loc;
105 h.uid = uid;
106 loc = complex_variable_components->find_slot_with_hash (h, uid, INSERT);
107 loc->uid = uid;
108 loc->to = to;
111 /* Return true if T is not a zero constant. In the case of real values,
112 we're only interested in +0.0. */
114 static int
115 some_nonzerop (tree t)
117 int zerop = false;
119 /* Operations with real or imaginary part of a complex number zero
120 cannot be treated the same as operations with a real or imaginary
121 operand if we care about the signs of zeros in the result. */
122 if (TREE_CODE (t) == REAL_CST && !flag_signed_zeros)
123 zerop = real_identical (&TREE_REAL_CST (t), &dconst0);
124 else if (TREE_CODE (t) == FIXED_CST)
125 zerop = fixed_zerop (t);
126 else if (TREE_CODE (t) == INTEGER_CST)
127 zerop = integer_zerop (t);
129 return !zerop;
133 /* Compute a lattice value from the components of a complex type REAL
134 and IMAG. */
136 static complex_lattice_t
137 find_lattice_value_parts (tree real, tree imag)
139 int r, i;
140 complex_lattice_t ret;
142 r = some_nonzerop (real);
143 i = some_nonzerop (imag);
144 ret = r * ONLY_REAL + i * ONLY_IMAG;
146 /* ??? On occasion we could do better than mapping 0+0i to real, but we
147 certainly don't want to leave it UNINITIALIZED, which eventually gets
148 mapped to VARYING. */
149 if (ret == UNINITIALIZED)
150 ret = ONLY_REAL;
152 return ret;
156 /* Compute a lattice value from gimple_val T. */
158 static complex_lattice_t
159 find_lattice_value (tree t)
161 tree real, imag;
163 switch (TREE_CODE (t))
165 case SSA_NAME:
166 return complex_lattice_values[SSA_NAME_VERSION (t)];
168 case COMPLEX_CST:
169 real = TREE_REALPART (t);
170 imag = TREE_IMAGPART (t);
171 break;
173 default:
174 gcc_unreachable ();
177 return find_lattice_value_parts (real, imag);
180 /* Determine if LHS is something for which we're interested in seeing
181 simulation results. */
183 static bool
184 is_complex_reg (tree lhs)
186 return TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE && is_gimple_reg (lhs);
189 /* Mark the incoming parameters to the function as VARYING. */
191 static void
192 init_parameter_lattice_values (void)
194 tree parm, ssa_name;
196 for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = DECL_CHAIN (parm))
197 if (is_complex_reg (parm)
198 && (ssa_name = ssa_default_def (cfun, parm)) != NULL_TREE)
199 complex_lattice_values[SSA_NAME_VERSION (ssa_name)] = VARYING;
202 /* Initialize simulation state for each statement. Return false if we
203 found no statements we want to simulate, and thus there's nothing
204 for the entire pass to do. */
206 static bool
207 init_dont_simulate_again (void)
209 basic_block bb;
210 bool saw_a_complex_op = false;
212 FOR_EACH_BB_FN (bb, cfun)
214 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
215 gsi_next (&gsi))
217 gphi *phi = gsi.phi ();
218 prop_set_simulate_again (phi,
219 is_complex_reg (gimple_phi_result (phi)));
222 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
223 gsi_next (&gsi))
225 gimple *stmt;
226 tree op0, op1;
227 bool sim_again_p;
229 stmt = gsi_stmt (gsi);
230 op0 = op1 = NULL_TREE;
232 /* Most control-altering statements must be initially
233 simulated, else we won't cover the entire cfg. */
234 sim_again_p = stmt_ends_bb_p (stmt);
236 switch (gimple_code (stmt))
238 case GIMPLE_CALL:
239 if (gimple_call_lhs (stmt))
240 sim_again_p = is_complex_reg (gimple_call_lhs (stmt));
241 break;
243 case GIMPLE_ASSIGN:
244 sim_again_p = is_complex_reg (gimple_assign_lhs (stmt));
245 if (gimple_assign_rhs_code (stmt) == REALPART_EXPR
246 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
247 op0 = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
248 else
249 op0 = gimple_assign_rhs1 (stmt);
250 if (gimple_num_ops (stmt) > 2)
251 op1 = gimple_assign_rhs2 (stmt);
252 break;
254 case GIMPLE_COND:
255 op0 = gimple_cond_lhs (stmt);
256 op1 = gimple_cond_rhs (stmt);
257 break;
259 default:
260 break;
263 if (op0 || op1)
264 switch (gimple_expr_code (stmt))
266 case EQ_EXPR:
267 case NE_EXPR:
268 case PLUS_EXPR:
269 case MINUS_EXPR:
270 case MULT_EXPR:
271 case TRUNC_DIV_EXPR:
272 case CEIL_DIV_EXPR:
273 case FLOOR_DIV_EXPR:
274 case ROUND_DIV_EXPR:
275 case RDIV_EXPR:
276 if (TREE_CODE (TREE_TYPE (op0)) == COMPLEX_TYPE
277 || TREE_CODE (TREE_TYPE (op1)) == COMPLEX_TYPE)
278 saw_a_complex_op = true;
279 break;
281 case NEGATE_EXPR:
282 case CONJ_EXPR:
283 if (TREE_CODE (TREE_TYPE (op0)) == COMPLEX_TYPE)
284 saw_a_complex_op = true;
285 break;
287 case REALPART_EXPR:
288 case IMAGPART_EXPR:
289 /* The total store transformation performed during
290 gimplification creates such uninitialized loads
291 and we need to lower the statement to be able
292 to fix things up. */
293 if (TREE_CODE (op0) == SSA_NAME
294 && ssa_undefined_value_p (op0))
295 saw_a_complex_op = true;
296 break;
298 default:
299 break;
302 prop_set_simulate_again (stmt, sim_again_p);
306 return saw_a_complex_op;
310 /* Evaluate statement STMT against the complex lattice defined above. */
312 enum ssa_prop_result
313 complex_propagate::visit_stmt (gimple *stmt, edge *taken_edge_p ATTRIBUTE_UNUSED,
314 tree *result_p)
316 complex_lattice_t new_l, old_l, op1_l, op2_l;
317 unsigned int ver;
318 tree lhs;
320 lhs = gimple_get_lhs (stmt);
321 /* Skip anything but GIMPLE_ASSIGN and GIMPLE_CALL with a lhs. */
322 if (!lhs || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
323 return SSA_PROP_VARYING;
325 /* These conditions should be satisfied due to the initial filter
326 set up in init_dont_simulate_again. */
327 gcc_assert (TREE_CODE (lhs) == SSA_NAME);
328 gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE);
330 *result_p = lhs;
331 ver = SSA_NAME_VERSION (lhs);
332 old_l = complex_lattice_values[ver];
334 switch (gimple_expr_code (stmt))
336 case SSA_NAME:
337 case COMPLEX_CST:
338 new_l = find_lattice_value (gimple_assign_rhs1 (stmt));
339 break;
341 case COMPLEX_EXPR:
342 new_l = find_lattice_value_parts (gimple_assign_rhs1 (stmt),
343 gimple_assign_rhs2 (stmt));
344 break;
346 case PLUS_EXPR:
347 case MINUS_EXPR:
348 op1_l = find_lattice_value (gimple_assign_rhs1 (stmt));
349 op2_l = find_lattice_value (gimple_assign_rhs2 (stmt));
351 /* We've set up the lattice values such that IOR neatly
352 models addition. */
353 new_l = op1_l | op2_l;
354 break;
356 case MULT_EXPR:
357 case RDIV_EXPR:
358 case TRUNC_DIV_EXPR:
359 case CEIL_DIV_EXPR:
360 case FLOOR_DIV_EXPR:
361 case ROUND_DIV_EXPR:
362 op1_l = find_lattice_value (gimple_assign_rhs1 (stmt));
363 op2_l = find_lattice_value (gimple_assign_rhs2 (stmt));
365 /* Obviously, if either varies, so does the result. */
366 if (op1_l == VARYING || op2_l == VARYING)
367 new_l = VARYING;
368 /* Don't prematurely promote variables if we've not yet seen
369 their inputs. */
370 else if (op1_l == UNINITIALIZED)
371 new_l = op2_l;
372 else if (op2_l == UNINITIALIZED)
373 new_l = op1_l;
374 else
376 /* At this point both numbers have only one component. If the
377 numbers are of opposite kind, the result is imaginary,
378 otherwise the result is real. The add/subtract translates
379 the real/imag from/to 0/1; the ^ performs the comparison. */
380 new_l = ((op1_l - ONLY_REAL) ^ (op2_l - ONLY_REAL)) + ONLY_REAL;
382 /* Don't allow the lattice value to flip-flop indefinitely. */
383 new_l |= old_l;
385 break;
387 case NEGATE_EXPR:
388 case CONJ_EXPR:
389 new_l = find_lattice_value (gimple_assign_rhs1 (stmt));
390 break;
392 default:
393 new_l = VARYING;
394 break;
397 /* If nothing changed this round, let the propagator know. */
398 if (new_l == old_l)
399 return SSA_PROP_NOT_INTERESTING;
401 complex_lattice_values[ver] = new_l;
402 return new_l == VARYING ? SSA_PROP_VARYING : SSA_PROP_INTERESTING;
405 /* Evaluate a PHI node against the complex lattice defined above. */
407 enum ssa_prop_result
408 complex_propagate::visit_phi (gphi *phi)
410 complex_lattice_t new_l, old_l;
411 unsigned int ver;
412 tree lhs;
413 int i;
415 lhs = gimple_phi_result (phi);
417 /* This condition should be satisfied due to the initial filter
418 set up in init_dont_simulate_again. */
419 gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE);
421 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
422 return SSA_PROP_VARYING;
424 /* We've set up the lattice values such that IOR neatly models PHI meet. */
425 new_l = UNINITIALIZED;
426 for (i = gimple_phi_num_args (phi) - 1; i >= 0; --i)
427 new_l |= find_lattice_value (gimple_phi_arg_def (phi, i));
429 ver = SSA_NAME_VERSION (lhs);
430 old_l = complex_lattice_values[ver];
432 if (new_l == old_l)
433 return SSA_PROP_NOT_INTERESTING;
435 complex_lattice_values[ver] = new_l;
436 return new_l == VARYING ? SSA_PROP_VARYING : SSA_PROP_INTERESTING;
439 /* Create one backing variable for a complex component of ORIG. */
441 static tree
442 create_one_component_var (tree type, tree orig, const char *prefix,
443 const char *suffix, enum tree_code code)
445 tree r = create_tmp_var (type, prefix);
447 DECL_SOURCE_LOCATION (r) = DECL_SOURCE_LOCATION (orig);
448 DECL_ARTIFICIAL (r) = 1;
450 if (DECL_NAME (orig) && !DECL_IGNORED_P (orig))
452 const char *name = IDENTIFIER_POINTER (DECL_NAME (orig));
453 name = ACONCAT ((name, suffix, NULL));
454 DECL_NAME (r) = get_identifier (name);
456 SET_DECL_DEBUG_EXPR (r, build1 (code, type, orig));
457 DECL_HAS_DEBUG_EXPR_P (r) = 1;
458 DECL_IGNORED_P (r) = 0;
459 copy_warning (r, orig);
461 else
463 DECL_IGNORED_P (r) = 1;
464 suppress_warning (r);
467 return r;
470 /* Retrieve a value for a complex component of VAR. */
472 static tree
473 get_component_var (tree var, bool imag_p)
475 size_t decl_index = DECL_UID (var) * 2 + imag_p;
476 tree ret = cvc_lookup (decl_index);
478 if (ret == NULL)
480 ret = create_one_component_var (TREE_TYPE (TREE_TYPE (var)), var,
481 imag_p ? "CI" : "CR",
482 imag_p ? "$imag" : "$real",
483 imag_p ? IMAGPART_EXPR : REALPART_EXPR);
484 cvc_insert (decl_index, ret);
487 return ret;
490 /* Retrieve a value for a complex component of SSA_NAME. */
492 static tree
493 get_component_ssa_name (tree ssa_name, bool imag_p)
495 complex_lattice_t lattice = find_lattice_value (ssa_name);
496 size_t ssa_name_index;
497 tree ret;
499 if (lattice == (imag_p ? ONLY_REAL : ONLY_IMAG))
501 tree inner_type = TREE_TYPE (TREE_TYPE (ssa_name));
502 if (SCALAR_FLOAT_TYPE_P (inner_type))
503 return build_real (inner_type, dconst0);
504 else
505 return build_int_cst (inner_type, 0);
508 ssa_name_index = SSA_NAME_VERSION (ssa_name) * 2 + imag_p;
509 ret = complex_ssa_name_components[ssa_name_index];
510 if (ret == NULL)
512 if (SSA_NAME_VAR (ssa_name))
513 ret = get_component_var (SSA_NAME_VAR (ssa_name), imag_p);
514 else
515 ret = TREE_TYPE (TREE_TYPE (ssa_name));
516 ret = make_ssa_name (ret);
518 /* Copy some properties from the original. In particular, whether it
519 is used in an abnormal phi, and whether it's uninitialized. */
520 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ret)
521 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name);
522 if (SSA_NAME_IS_DEFAULT_DEF (ssa_name)
523 && TREE_CODE (SSA_NAME_VAR (ssa_name)) == VAR_DECL)
525 SSA_NAME_DEF_STMT (ret) = SSA_NAME_DEF_STMT (ssa_name);
526 set_ssa_default_def (cfun, SSA_NAME_VAR (ret), ret);
529 complex_ssa_name_components[ssa_name_index] = ret;
532 return ret;
535 /* Set a value for a complex component of SSA_NAME, return a
536 gimple_seq of stuff that needs doing. */
538 static gimple_seq
539 set_component_ssa_name (tree ssa_name, bool imag_p, tree value)
541 complex_lattice_t lattice = find_lattice_value (ssa_name);
542 size_t ssa_name_index;
543 tree comp;
544 gimple *last;
545 gimple_seq list;
547 /* We know the value must be zero, else there's a bug in our lattice
548 analysis. But the value may well be a variable known to contain
549 zero. We should be safe ignoring it. */
550 if (lattice == (imag_p ? ONLY_REAL : ONLY_IMAG))
551 return NULL;
553 /* If we've already assigned an SSA_NAME to this component, then this
554 means that our walk of the basic blocks found a use before the set.
555 This is fine. Now we should create an initialization for the value
556 we created earlier. */
557 ssa_name_index = SSA_NAME_VERSION (ssa_name) * 2 + imag_p;
558 comp = complex_ssa_name_components[ssa_name_index];
559 if (comp)
562 /* If we've nothing assigned, and the value we're given is already stable,
563 then install that as the value for this SSA_NAME. This preemptively
564 copy-propagates the value, which avoids unnecessary memory allocation. */
565 else if (is_gimple_min_invariant (value)
566 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name))
568 complex_ssa_name_components[ssa_name_index] = value;
569 return NULL;
571 else if (TREE_CODE (value) == SSA_NAME
572 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name))
574 /* Replace an anonymous base value with the variable from cvc_lookup.
575 This should result in better debug info. */
576 if (!SSA_NAME_IS_DEFAULT_DEF (value)
577 && 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, bool phiarg_p = false)
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 BIT_FIELD_REF:
622 tree inner_type = TREE_TYPE (TREE_TYPE (t));
623 t = unshare_expr (t);
624 TREE_TYPE (t) = inner_type;
625 TREE_OPERAND (t, 1) = TYPE_SIZE (inner_type);
626 if (imagpart_p)
627 TREE_OPERAND (t, 2) = size_binop (PLUS_EXPR, TREE_OPERAND (t, 2),
628 TYPE_SIZE (inner_type));
629 if (gimple_p)
630 t = force_gimple_operand_gsi (gsi, t, true, NULL, true,
631 GSI_SAME_STMT);
632 return t;
635 case VAR_DECL:
636 case RESULT_DECL:
637 case PARM_DECL:
638 case COMPONENT_REF:
639 case ARRAY_REF:
640 case VIEW_CONVERT_EXPR:
641 case MEM_REF:
643 tree inner_type = TREE_TYPE (TREE_TYPE (t));
645 t = build1 ((imagpart_p ? IMAGPART_EXPR : REALPART_EXPR),
646 inner_type, unshare_expr (t));
648 if (gimple_p)
649 t = force_gimple_operand_gsi (gsi, t, true, NULL, true,
650 GSI_SAME_STMT);
652 return t;
655 case SSA_NAME:
656 t = get_component_ssa_name (t, imagpart_p);
657 if (TREE_CODE (t) == SSA_NAME && SSA_NAME_DEF_STMT (t) == NULL)
658 gcc_assert (phiarg_p);
659 return t;
661 default:
662 gcc_unreachable ();
666 /* Update the complex components of the ssa name on the lhs of STMT. */
668 static void
669 update_complex_components (gimple_stmt_iterator *gsi, gimple *stmt, tree r,
670 tree i)
672 tree lhs;
673 gimple_seq list;
675 lhs = gimple_get_lhs (stmt);
677 list = set_component_ssa_name (lhs, false, r);
678 if (list)
679 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
681 list = set_component_ssa_name (lhs, true, i);
682 if (list)
683 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
686 static void
687 update_complex_components_on_edge (edge e, tree lhs, tree r, tree i)
689 gimple_seq list;
691 list = set_component_ssa_name (lhs, false, r);
692 if (list)
693 gsi_insert_seq_on_edge (e, list);
695 list = set_component_ssa_name (lhs, true, i);
696 if (list)
697 gsi_insert_seq_on_edge (e, list);
701 /* Update an assignment to a complex variable in place. */
703 static void
704 update_complex_assignment (gimple_stmt_iterator *gsi, tree r, tree i)
706 gimple *old_stmt = gsi_stmt (*gsi);
707 gimple_assign_set_rhs_with_ops (gsi, COMPLEX_EXPR, r, i);
708 gimple *stmt = gsi_stmt (*gsi);
709 update_stmt (stmt);
710 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
711 bitmap_set_bit (need_eh_cleanup, gimple_bb (stmt)->index);
713 update_complex_components (gsi, gsi_stmt (*gsi), r, i);
717 /* Generate code at the entry point of the function to initialize the
718 component variables for a complex parameter. */
720 static void
721 update_parameter_components (void)
723 edge entry_edge = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun));
724 tree parm;
726 for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = DECL_CHAIN (parm))
728 tree type = TREE_TYPE (parm);
729 tree ssa_name, r, i;
731 if (TREE_CODE (type) != COMPLEX_TYPE || !is_gimple_reg (parm))
732 continue;
734 type = TREE_TYPE (type);
735 ssa_name = ssa_default_def (cfun, parm);
736 if (!ssa_name)
737 continue;
739 r = build1 (REALPART_EXPR, type, ssa_name);
740 i = build1 (IMAGPART_EXPR, type, ssa_name);
741 update_complex_components_on_edge (entry_edge, ssa_name, r, i);
745 /* Generate code to set the component variables of a complex variable
746 to match the PHI statements in block BB. */
748 static void
749 update_phi_components (basic_block bb)
751 gphi_iterator gsi;
753 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
755 gphi *phi = gsi.phi ();
757 if (is_complex_reg (gimple_phi_result (phi)))
759 gphi *p[2] = { NULL, NULL };
760 unsigned int i, j, n;
761 bool revisit_phi = false;
763 for (j = 0; j < 2; j++)
765 tree l = get_component_ssa_name (gimple_phi_result (phi), j > 0);
766 if (TREE_CODE (l) == SSA_NAME)
767 p[j] = create_phi_node (l, bb);
770 for (i = 0, n = gimple_phi_num_args (phi); i < n; ++i)
772 tree comp, arg = gimple_phi_arg_def (phi, i);
773 for (j = 0; j < 2; j++)
774 if (p[j])
776 comp = extract_component (NULL, arg, j > 0, false, true);
777 if (TREE_CODE (comp) == SSA_NAME
778 && SSA_NAME_DEF_STMT (comp) == NULL)
780 /* For the benefit of any gimple simplification during
781 this pass that might walk SSA_NAME def stmts,
782 don't add SSA_NAMEs without definitions into the
783 PHI arguments, but put a decl in there instead
784 temporarily, and revisit this PHI later on. */
785 if (SSA_NAME_VAR (comp))
786 comp = SSA_NAME_VAR (comp);
787 else
788 comp = create_tmp_reg (TREE_TYPE (comp),
789 get_name (comp));
790 revisit_phi = true;
792 SET_PHI_ARG_DEF (p[j], i, comp);
796 if (revisit_phi)
798 phis_to_revisit.safe_push (phi);
799 phis_to_revisit.safe_push (p[0]);
800 phis_to_revisit.safe_push (p[1]);
806 /* Expand a complex move to scalars. */
808 static void
809 expand_complex_move (gimple_stmt_iterator *gsi, tree type)
811 tree inner_type = TREE_TYPE (type);
812 tree r, i, lhs, rhs;
813 gimple *stmt = gsi_stmt (*gsi);
815 if (is_gimple_assign (stmt))
817 lhs = gimple_assign_lhs (stmt);
818 if (gimple_num_ops (stmt) == 2)
819 rhs = gimple_assign_rhs1 (stmt);
820 else
821 rhs = NULL_TREE;
823 else if (is_gimple_call (stmt))
825 lhs = gimple_call_lhs (stmt);
826 rhs = NULL_TREE;
828 else
829 gcc_unreachable ();
831 if (TREE_CODE (lhs) == SSA_NAME)
833 if (is_ctrl_altering_stmt (stmt))
835 edge e;
837 /* The value is not assigned on the exception edges, so we need not
838 concern ourselves there. We do need to update on the fallthru
839 edge. Find it. */
840 e = find_fallthru_edge (gsi_bb (*gsi)->succs);
841 if (!e)
842 gcc_unreachable ();
844 r = build1 (REALPART_EXPR, inner_type, lhs);
845 i = build1 (IMAGPART_EXPR, inner_type, lhs);
846 update_complex_components_on_edge (e, lhs, r, i);
848 else if (is_gimple_call (stmt)
849 || gimple_has_side_effects (stmt)
850 || gimple_assign_rhs_code (stmt) == PAREN_EXPR)
852 r = build1 (REALPART_EXPR, inner_type, lhs);
853 i = build1 (IMAGPART_EXPR, inner_type, lhs);
854 update_complex_components (gsi, stmt, r, i);
856 else
858 if (gimple_assign_rhs_code (stmt) != COMPLEX_EXPR)
860 r = extract_component (gsi, rhs, 0, true);
861 i = extract_component (gsi, rhs, 1, true);
863 else
865 r = gimple_assign_rhs1 (stmt);
866 i = gimple_assign_rhs2 (stmt);
868 update_complex_assignment (gsi, r, i);
871 else if (rhs && TREE_CODE (rhs) == SSA_NAME && !TREE_SIDE_EFFECTS (lhs))
873 tree x;
874 gimple *t;
875 location_t loc;
877 loc = gimple_location (stmt);
878 r = extract_component (gsi, rhs, 0, false);
879 i = extract_component (gsi, rhs, 1, false);
881 x = build1 (REALPART_EXPR, inner_type, unshare_expr (lhs));
882 t = gimple_build_assign (x, r);
883 gimple_set_location (t, loc);
884 gsi_insert_before (gsi, t, GSI_SAME_STMT);
886 if (stmt == gsi_stmt (*gsi))
888 x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
889 gimple_assign_set_lhs (stmt, x);
890 gimple_assign_set_rhs1 (stmt, i);
892 else
894 x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
895 t = gimple_build_assign (x, i);
896 gimple_set_location (t, loc);
897 gsi_insert_before (gsi, t, GSI_SAME_STMT);
899 stmt = gsi_stmt (*gsi);
900 gcc_assert (gimple_code (stmt) == GIMPLE_RETURN);
901 gimple_return_set_retval (as_a <greturn *> (stmt), lhs);
904 update_stmt (stmt);
908 /* Expand complex addition to scalars:
909 a + b = (ar + br) + i(ai + bi)
910 a - b = (ar - br) + i(ai + bi)
913 static void
914 expand_complex_addition (gimple_stmt_iterator *gsi, tree inner_type,
915 tree ar, tree ai, tree br, tree bi,
916 enum tree_code code,
917 complex_lattice_t al, complex_lattice_t bl)
919 tree rr, ri;
920 gimple_seq stmts = NULL;
921 location_t loc = gimple_location (gsi_stmt (*gsi));
923 switch (PAIR (al, bl))
925 case PAIR (ONLY_REAL, ONLY_REAL):
926 rr = gimple_build (&stmts, loc, code, inner_type, ar, br);
927 ri = ai;
928 break;
930 case PAIR (ONLY_REAL, ONLY_IMAG):
931 rr = ar;
932 if (code == MINUS_EXPR)
933 ri = gimple_build (&stmts, loc, MINUS_EXPR, inner_type, ai, bi);
934 else
935 ri = bi;
936 break;
938 case PAIR (ONLY_IMAG, ONLY_REAL):
939 if (code == MINUS_EXPR)
940 rr = gimple_build (&stmts, loc, MINUS_EXPR, inner_type, ar, br);
941 else
942 rr = br;
943 ri = ai;
944 break;
946 case PAIR (ONLY_IMAG, ONLY_IMAG):
947 rr = ar;
948 ri = gimple_build (&stmts, loc, code, inner_type, ai, bi);
949 break;
951 case PAIR (VARYING, ONLY_REAL):
952 rr = gimple_build (&stmts, loc, code, inner_type, ar, br);
953 ri = ai;
954 break;
956 case PAIR (VARYING, ONLY_IMAG):
957 rr = ar;
958 ri = gimple_build (&stmts, loc, code, inner_type, ai, bi);
959 break;
961 case PAIR (ONLY_REAL, VARYING):
962 if (code == MINUS_EXPR)
963 goto general;
964 rr = gimple_build (&stmts, loc, code, inner_type, ar, br);
965 ri = bi;
966 break;
968 case PAIR (ONLY_IMAG, VARYING):
969 if (code == MINUS_EXPR)
970 goto general;
971 rr = br;
972 ri = gimple_build (&stmts, loc, code, inner_type, ai, bi);
973 break;
975 case PAIR (VARYING, VARYING):
976 general:
977 rr = gimple_build (&stmts, loc, code, inner_type, ar, br);
978 ri = gimple_build (&stmts, loc, code, inner_type, ai, bi);
979 break;
981 default:
982 gcc_unreachable ();
985 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
986 update_complex_assignment (gsi, rr, ri);
989 /* Expand a complex multiplication or division to a libcall to the c99
990 compliant routines. TYPE is the complex type of the operation.
991 If INPLACE_P replace the statement at GSI with
992 the libcall and return NULL_TREE. Else insert the call, assign its
993 result to an output variable and return that variable. If INPLACE_P
994 is true then the statement being replaced should be an assignment
995 statement. */
997 static tree
998 expand_complex_libcall (gimple_stmt_iterator *gsi, tree type, tree ar, tree ai,
999 tree br, tree bi, enum tree_code code, bool inplace_p)
1001 machine_mode mode;
1002 enum built_in_function bcode;
1003 tree fn, lhs;
1004 gcall *stmt;
1006 mode = TYPE_MODE (type);
1007 gcc_assert (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT);
1009 if (code == MULT_EXPR)
1010 bcode = ((enum built_in_function)
1011 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
1012 else if (code == RDIV_EXPR)
1013 bcode = ((enum built_in_function)
1014 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
1015 else
1016 gcc_unreachable ();
1017 fn = builtin_decl_explicit (bcode);
1018 stmt = gimple_build_call (fn, 4, ar, ai, br, bi);
1020 if (inplace_p)
1022 gimple *old_stmt = gsi_stmt (*gsi);
1023 gimple_call_set_nothrow (stmt, !stmt_could_throw_p (cfun, old_stmt));
1024 lhs = gimple_assign_lhs (old_stmt);
1025 gimple_call_set_lhs (stmt, lhs);
1026 gsi_replace (gsi, stmt, true);
1028 type = TREE_TYPE (type);
1029 if (stmt_can_throw_internal (cfun, stmt))
1031 edge_iterator ei;
1032 edge e;
1033 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
1034 if (!(e->flags & EDGE_EH))
1035 break;
1036 basic_block bb = split_edge (e);
1037 gimple_stmt_iterator gsi2 = gsi_start_bb (bb);
1038 update_complex_components (&gsi2, stmt,
1039 build1 (REALPART_EXPR, type, lhs),
1040 build1 (IMAGPART_EXPR, type, lhs));
1041 return NULL_TREE;
1043 else
1044 update_complex_components (gsi, stmt,
1045 build1 (REALPART_EXPR, type, lhs),
1046 build1 (IMAGPART_EXPR, type, lhs));
1047 SSA_NAME_DEF_STMT (lhs) = stmt;
1048 return NULL_TREE;
1051 gimple_call_set_nothrow (stmt, true);
1052 lhs = make_ssa_name (type);
1053 gimple_call_set_lhs (stmt, lhs);
1054 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1056 return lhs;
1059 /* Perform a complex multiplication on two complex constants A, B represented
1060 by AR, AI, BR, BI of type TYPE.
1061 The operation we want is: a * b = (ar*br - ai*bi) + i(ar*bi + br*ai).
1062 Insert the GIMPLE statements into GSI. Store the real and imaginary
1063 components of the result into RR and RI. */
1065 static void
1066 expand_complex_multiplication_components (gimple_seq *stmts, location_t loc,
1067 tree type, tree ar, tree ai,
1068 tree br, tree bi,
1069 tree *rr, tree *ri)
1071 tree t1, t2, t3, t4;
1073 t1 = gimple_build (stmts, loc, MULT_EXPR, type, ar, br);
1074 t2 = gimple_build (stmts, loc, MULT_EXPR, type, ai, bi);
1075 t3 = gimple_build (stmts, loc, MULT_EXPR, type, ar, bi);
1077 /* Avoid expanding redundant multiplication for the common
1078 case of squaring a complex number. */
1079 if (ar == br && ai == bi)
1080 t4 = t3;
1081 else
1082 t4 = gimple_build (stmts, loc, MULT_EXPR, type, ai, br);
1084 *rr = gimple_build (stmts, loc, MINUS_EXPR, type, t1, t2);
1085 *ri = gimple_build (stmts, loc, PLUS_EXPR, type, t3, t4);
1088 /* Expand complex multiplication to scalars:
1089 a * b = (ar*br - ai*bi) + i(ar*bi + br*ai)
1092 static void
1093 expand_complex_multiplication (gimple_stmt_iterator *gsi, tree type,
1094 tree ar, tree ai, tree br, tree bi,
1095 complex_lattice_t al, complex_lattice_t bl)
1097 tree rr, ri;
1098 tree inner_type = TREE_TYPE (type);
1099 location_t loc = gimple_location (gsi_stmt (*gsi));
1100 gimple_seq stmts = NULL;
1102 if (al < bl)
1104 complex_lattice_t tl;
1105 rr = ar, ar = br, br = rr;
1106 ri = ai, ai = bi, bi = ri;
1107 tl = al, al = bl, bl = tl;
1110 switch (PAIR (al, bl))
1112 case PAIR (ONLY_REAL, ONLY_REAL):
1113 rr = gimple_build (&stmts, loc, MULT_EXPR, inner_type, ar, br);
1114 ri = ai;
1115 break;
1117 case PAIR (ONLY_IMAG, ONLY_REAL):
1118 rr = ar;
1119 if (TREE_CODE (ai) == REAL_CST
1120 && real_identical (&TREE_REAL_CST (ai), &dconst1))
1121 ri = br;
1122 else
1123 ri = gimple_build (&stmts, loc, MULT_EXPR, inner_type, ai, br);
1124 break;
1126 case PAIR (ONLY_IMAG, ONLY_IMAG):
1127 rr = gimple_build (&stmts, loc, MULT_EXPR, inner_type, ai, bi);
1128 rr = gimple_build (&stmts, loc, NEGATE_EXPR, inner_type, rr);
1129 ri = ar;
1130 break;
1132 case PAIR (VARYING, ONLY_REAL):
1133 rr = gimple_build (&stmts, loc, MULT_EXPR, inner_type, ar, br);
1134 ri = gimple_build (&stmts, loc, MULT_EXPR, inner_type, ai, br);
1135 break;
1137 case PAIR (VARYING, ONLY_IMAG):
1138 rr = gimple_build (&stmts, loc, MULT_EXPR, inner_type, ai, bi);
1139 rr = gimple_build (&stmts, loc, NEGATE_EXPR, inner_type, rr);
1140 ri = gimple_build (&stmts, loc, MULT_EXPR, inner_type, ar, bi);
1141 break;
1143 case PAIR (VARYING, VARYING):
1144 if (flag_complex_method == 2 && SCALAR_FLOAT_TYPE_P (inner_type))
1146 /* If optimizing for size or not at all just do a libcall.
1147 Same if there are exception-handling edges or signaling NaNs. */
1148 if (optimize == 0 || optimize_bb_for_size_p (gsi_bb (*gsi))
1149 || stmt_can_throw_internal (cfun, gsi_stmt (*gsi))
1150 || flag_signaling_nans)
1152 expand_complex_libcall (gsi, type, ar, ai, br, bi,
1153 MULT_EXPR, true);
1154 return;
1157 if (!HONOR_NANS (inner_type))
1159 /* If we are not worrying about NaNs expand to
1160 (ar*br - ai*bi) + i(ar*bi + br*ai) directly. */
1161 expand_complex_multiplication_components (&stmts, loc, inner_type,
1162 ar, ai, br, bi,
1163 &rr, &ri);
1164 break;
1167 /* Else, expand x = a * b into
1168 x = (ar*br - ai*bi) + i(ar*bi + br*ai);
1169 if (isunordered (__real__ x, __imag__ x))
1170 x = __muldc3 (a, b); */
1172 tree tmpr, tmpi;
1173 expand_complex_multiplication_components (&stmts, loc,
1174 inner_type, ar, ai,
1175 br, bi, &tmpr, &tmpi);
1176 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
1177 stmts = NULL;
1179 gimple *check
1180 = gimple_build_cond (UNORDERED_EXPR, tmpr, tmpi,
1181 NULL_TREE, NULL_TREE);
1183 basic_block orig_bb = gsi_bb (*gsi);
1184 /* We want to keep track of the original complex multiplication
1185 statement as we're going to modify it later in
1186 update_complex_assignment. Make sure that insert_cond_bb leaves
1187 that statement in the join block. */
1188 gsi_prev (gsi);
1189 basic_block cond_bb
1190 = insert_cond_bb (gsi_bb (*gsi), gsi_stmt (*gsi), check,
1191 profile_probability::very_unlikely ());
1193 gimple_stmt_iterator cond_bb_gsi = gsi_last_bb (cond_bb);
1194 gsi_insert_after (&cond_bb_gsi, gimple_build_nop (), GSI_NEW_STMT);
1196 tree libcall_res
1197 = expand_complex_libcall (&cond_bb_gsi, type, ar, ai, br,
1198 bi, MULT_EXPR, false);
1199 gimple_seq stmts2 = NULL;
1200 tree cond_real = gimple_build (&stmts2, loc, REALPART_EXPR,
1201 inner_type, libcall_res);
1202 tree cond_imag = gimple_build (&stmts2, loc, IMAGPART_EXPR,
1203 inner_type, libcall_res);
1204 gsi_insert_seq_before (&cond_bb_gsi, stmts2, GSI_SAME_STMT);
1206 basic_block join_bb = single_succ_edge (cond_bb)->dest;
1207 *gsi = gsi_start_nondebug_after_labels_bb (join_bb);
1209 /* We have a conditional block with some assignments in cond_bb.
1210 Wire up the PHIs to wrap up. */
1211 rr = make_ssa_name (inner_type);
1212 ri = make_ssa_name (inner_type);
1213 edge cond_to_join = single_succ_edge (cond_bb);
1214 edge orig_to_join = find_edge (orig_bb, join_bb);
1216 gphi *real_phi = create_phi_node (rr, gsi_bb (*gsi));
1217 add_phi_arg (real_phi, cond_real, cond_to_join, UNKNOWN_LOCATION);
1218 add_phi_arg (real_phi, tmpr, orig_to_join, UNKNOWN_LOCATION);
1220 gphi *imag_phi = create_phi_node (ri, gsi_bb (*gsi));
1221 add_phi_arg (imag_phi, cond_imag, cond_to_join, UNKNOWN_LOCATION);
1222 add_phi_arg (imag_phi, tmpi, orig_to_join, UNKNOWN_LOCATION);
1224 else
1225 /* If we are not worrying about NaNs expand to
1226 (ar*br - ai*bi) + i(ar*bi + br*ai) directly. */
1227 expand_complex_multiplication_components (&stmts, loc,
1228 inner_type, ar, ai,
1229 br, bi, &rr, &ri);
1230 break;
1232 default:
1233 gcc_unreachable ();
1236 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
1237 update_complex_assignment (gsi, rr, ri);
1240 /* Keep this algorithm in sync with fold-const.c:const_binop().
1242 Expand complex division to scalars, straightforward algorithm.
1243 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1244 t = br*br + bi*bi
1247 static void
1248 expand_complex_div_straight (gimple_stmt_iterator *gsi, tree inner_type,
1249 tree ar, tree ai, tree br, tree bi,
1250 enum tree_code code)
1252 gimple_seq stmts = NULL;
1253 location_t loc = gimple_location (gsi_stmt (*gsi));
1254 tree rr, ri, div, t1, t2, t3;
1256 t1 = gimple_build (&stmts, loc, MULT_EXPR, inner_type, br, br);
1257 t2 = gimple_build (&stmts, loc, MULT_EXPR, inner_type, bi, bi);
1258 div = gimple_build (&stmts, loc, PLUS_EXPR, inner_type, t1, t2);
1260 t1 = gimple_build (&stmts, loc, MULT_EXPR, inner_type, ar, br);
1261 t2 = gimple_build (&stmts, loc, MULT_EXPR, inner_type, ai, bi);
1262 t3 = gimple_build (&stmts, loc, PLUS_EXPR, inner_type, t1, t2);
1263 rr = gimple_build (&stmts, loc, code, inner_type, t3, div);
1265 t1 = gimple_build (&stmts, loc, MULT_EXPR, inner_type, ai, br);
1266 t2 = gimple_build (&stmts, loc, MULT_EXPR, inner_type, ar, bi);
1267 t3 = gimple_build (&stmts, loc, MINUS_EXPR, inner_type, t1, t2);
1268 ri = gimple_build (&stmts, loc, code, inner_type, t3, div);
1270 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
1271 update_complex_assignment (gsi, rr, ri);
1274 /* Keep this algorithm in sync with fold-const.c:const_binop().
1276 Expand complex division to scalars, modified algorithm to minimize
1277 overflow with wide input ranges. */
1279 static void
1280 expand_complex_div_wide (gimple_stmt_iterator *gsi, tree inner_type,
1281 tree ar, tree ai, tree br, tree bi,
1282 enum tree_code code)
1284 tree rr, ri, ratio, div, t1, t2, tr, ti, compare;
1285 basic_block bb_cond, bb_true, bb_false, bb_join;
1286 gimple *stmt;
1287 gimple_seq stmts = NULL;
1288 location_t loc = gimple_location (gsi_stmt (*gsi));
1290 /* Examine |br| < |bi|, and branch. */
1291 t1 = gimple_build (&stmts, loc, ABS_EXPR, inner_type, br);
1292 t2 = gimple_build (&stmts, loc, ABS_EXPR, inner_type, bi);
1293 compare = gimple_build (&stmts, loc,
1294 LT_EXPR, boolean_type_node, t1, t2);
1296 bb_cond = bb_true = bb_false = bb_join = NULL;
1297 rr = ri = tr = ti = NULL;
1298 if (TREE_CODE (compare) != INTEGER_CST)
1300 edge e;
1301 gimple *stmt;
1303 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
1304 stmts = NULL;
1305 stmt = gimple_build_cond (NE_EXPR, compare, boolean_false_node,
1306 NULL_TREE, NULL_TREE);
1307 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1309 /* Split the original block, and create the TRUE and FALSE blocks. */
1310 e = split_block (gsi_bb (*gsi), stmt);
1311 bb_cond = e->src;
1312 bb_join = e->dest;
1313 bb_true = create_empty_bb (bb_cond);
1314 bb_false = create_empty_bb (bb_true);
1315 bb_true->count = bb_false->count
1316 = bb_cond->count.apply_probability (profile_probability::even ());
1318 /* Wire the blocks together. */
1319 e->flags = EDGE_TRUE_VALUE;
1320 /* TODO: With value profile we could add an historgram to determine real
1321 branch outcome. */
1322 e->probability = profile_probability::even ();
1323 redirect_edge_succ (e, bb_true);
1324 edge e2 = make_edge (bb_cond, bb_false, EDGE_FALSE_VALUE);
1325 e2->probability = profile_probability::even ();
1326 make_single_succ_edge (bb_true, bb_join, EDGE_FALLTHRU);
1327 make_single_succ_edge (bb_false, bb_join, EDGE_FALLTHRU);
1328 add_bb_to_loop (bb_true, bb_cond->loop_father);
1329 add_bb_to_loop (bb_false, bb_cond->loop_father);
1331 /* Update dominance info. Note that bb_join's data was
1332 updated by split_block. */
1333 if (dom_info_available_p (CDI_DOMINATORS))
1335 set_immediate_dominator (CDI_DOMINATORS, bb_true, bb_cond);
1336 set_immediate_dominator (CDI_DOMINATORS, bb_false, bb_cond);
1339 rr = create_tmp_reg (inner_type);
1340 ri = create_tmp_reg (inner_type);
1342 else
1344 gimple_seq_discard (stmts);
1345 stmts = NULL;
1348 /* In the TRUE branch, we compute
1349 ratio = br/bi;
1350 div = (br * ratio) + bi;
1351 tr = (ar * ratio) + ai;
1352 ti = (ai * ratio) - ar;
1353 tr = tr / div;
1354 ti = ti / div; */
1355 if (bb_true || integer_nonzerop (compare))
1357 if (bb_true)
1359 *gsi = gsi_last_bb (bb_true);
1360 gsi_insert_after (gsi, gimple_build_nop (), GSI_NEW_STMT);
1363 ratio = gimple_build (&stmts, loc, code, inner_type, br, bi);
1365 t1 = gimple_build (&stmts, loc, MULT_EXPR, inner_type, br, ratio);
1366 div = gimple_build (&stmts, loc, PLUS_EXPR, inner_type, t1, bi);
1368 t1 = gimple_build (&stmts, loc, MULT_EXPR, inner_type, ar, ratio);
1369 tr = gimple_build (&stmts, loc, PLUS_EXPR, inner_type, t1, ai);
1371 t1 = gimple_build (&stmts, loc, MULT_EXPR, inner_type, ai, ratio);
1372 ti = gimple_build (&stmts, loc, MINUS_EXPR, inner_type, t1, ar);
1374 tr = gimple_build (&stmts, loc, code, inner_type, tr, div);
1375 ti = gimple_build (&stmts, loc, code, inner_type, ti, div);
1376 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
1377 stmts = NULL;
1379 if (bb_true)
1381 stmt = gimple_build_assign (rr, tr);
1382 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1383 stmt = gimple_build_assign (ri, ti);
1384 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1385 gsi_remove (gsi, true);
1389 /* In the FALSE branch, we compute
1390 ratio = d/c;
1391 divisor = (d * ratio) + c;
1392 tr = (b * ratio) + a;
1393 ti = b - (a * ratio);
1394 tr = tr / div;
1395 ti = ti / div; */
1396 if (bb_false || integer_zerop (compare))
1398 if (bb_false)
1400 *gsi = gsi_last_bb (bb_false);
1401 gsi_insert_after (gsi, gimple_build_nop (), GSI_NEW_STMT);
1404 ratio = gimple_build (&stmts, loc, code, inner_type, bi, br);
1406 t1 = gimple_build (&stmts, loc, MULT_EXPR, inner_type, bi, ratio);
1407 div = gimple_build (&stmts, loc, PLUS_EXPR, inner_type, t1, br);
1409 t1 = gimple_build (&stmts, loc, MULT_EXPR, inner_type, ai, ratio);
1410 tr = gimple_build (&stmts, loc, PLUS_EXPR, inner_type, t1, ar);
1412 t1 = gimple_build (&stmts, loc, MULT_EXPR, inner_type, ar, ratio);
1413 ti = gimple_build (&stmts, loc, MINUS_EXPR, inner_type, ai, t1);
1415 tr = gimple_build (&stmts, loc, code, inner_type, tr, div);
1416 ti = gimple_build (&stmts, loc, code, inner_type, ti, div);
1417 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
1418 stmts = NULL;
1420 if (bb_false)
1422 stmt = gimple_build_assign (rr, tr);
1423 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1424 stmt = gimple_build_assign (ri, ti);
1425 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1426 gsi_remove (gsi, true);
1430 if (bb_join)
1431 *gsi = gsi_start_bb (bb_join);
1432 else
1433 rr = tr, ri = ti;
1435 update_complex_assignment (gsi, rr, ri);
1438 /* Expand complex division to scalars. */
1440 static void
1441 expand_complex_division (gimple_stmt_iterator *gsi, tree type,
1442 tree ar, tree ai, tree br, tree bi,
1443 enum tree_code code,
1444 complex_lattice_t al, complex_lattice_t bl)
1446 tree rr, ri;
1447 gimple_seq stmts = NULL;
1448 location_t loc = gimple_location (gsi_stmt (*gsi));
1450 tree inner_type = TREE_TYPE (type);
1451 switch (PAIR (al, bl))
1453 case PAIR (ONLY_REAL, ONLY_REAL):
1454 rr = gimple_build (&stmts, loc, code, inner_type, ar, br);
1455 ri = ai;
1456 break;
1458 case PAIR (ONLY_REAL, ONLY_IMAG):
1459 rr = ai;
1460 ri = gimple_build (&stmts, loc, code, inner_type, ar, bi);
1461 ri = gimple_build (&stmts, loc, NEGATE_EXPR, inner_type, ri);
1462 break;
1464 case PAIR (ONLY_IMAG, ONLY_REAL):
1465 rr = ar;
1466 ri = gimple_build (&stmts, loc, code, inner_type, ai, br);
1467 break;
1469 case PAIR (ONLY_IMAG, ONLY_IMAG):
1470 rr = gimple_build (&stmts, loc, code, inner_type, ai, bi);
1471 ri = ar;
1472 break;
1474 case PAIR (VARYING, ONLY_REAL):
1475 rr = gimple_build (&stmts, loc, code, inner_type, ar, br);
1476 ri = gimple_build (&stmts, loc, code, inner_type, ai, br);
1477 break;
1479 case PAIR (VARYING, ONLY_IMAG):
1480 rr = gimple_build (&stmts, loc, code, inner_type, ai, bi);
1481 ri = gimple_build (&stmts, loc, code, inner_type, ar, bi);
1482 ri = gimple_build (&stmts, loc, NEGATE_EXPR, inner_type, ri);
1483 break;
1485 case PAIR (ONLY_REAL, VARYING):
1486 case PAIR (ONLY_IMAG, VARYING):
1487 case PAIR (VARYING, VARYING):
1488 switch (flag_complex_method)
1490 case 0:
1491 /* straightforward implementation of complex divide acceptable. */
1492 expand_complex_div_straight (gsi, inner_type, ar, ai, br, bi, code);
1493 break;
1495 case 2:
1496 if (SCALAR_FLOAT_TYPE_P (inner_type))
1498 expand_complex_libcall (gsi, type, ar, ai, br, bi, code, true);
1499 break;
1501 /* FALLTHRU */
1503 case 1:
1504 /* wide ranges of inputs must work for complex divide. */
1505 expand_complex_div_wide (gsi, inner_type, ar, ai, br, bi, code);
1506 break;
1508 default:
1509 gcc_unreachable ();
1511 return;
1513 default:
1514 gcc_unreachable ();
1517 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
1518 update_complex_assignment (gsi, rr, ri);
1521 /* Expand complex negation to scalars:
1522 -a = (-ar) + i(-ai)
1525 static void
1526 expand_complex_negation (gimple_stmt_iterator *gsi, tree inner_type,
1527 tree ar, tree ai)
1529 tree rr, ri;
1530 gimple_seq stmts = NULL;
1531 location_t loc = gimple_location (gsi_stmt (*gsi));
1533 rr = gimple_build (&stmts, loc, NEGATE_EXPR, inner_type, ar);
1534 ri = gimple_build (&stmts, loc, NEGATE_EXPR, inner_type, ai);
1536 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
1537 update_complex_assignment (gsi, rr, ri);
1540 /* Expand complex conjugate to scalars:
1541 ~a = (ar) + i(-ai)
1544 static void
1545 expand_complex_conjugate (gimple_stmt_iterator *gsi, tree inner_type,
1546 tree ar, tree ai)
1548 tree ri;
1549 gimple_seq stmts = NULL;
1550 location_t loc = gimple_location (gsi_stmt (*gsi));
1552 ri = gimple_build (&stmts, loc, NEGATE_EXPR, inner_type, ai);
1554 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
1555 update_complex_assignment (gsi, ar, ri);
1558 /* Expand complex comparison (EQ or NE only). */
1560 static void
1561 expand_complex_comparison (gimple_stmt_iterator *gsi, tree ar, tree ai,
1562 tree br, tree bi, enum tree_code code)
1564 tree cr, ci, cc, type;
1565 gimple *stmt = gsi_stmt (*gsi);
1566 gimple_seq stmts = NULL;
1567 location_t loc = gimple_location (stmt);
1569 cr = gimple_build (&stmts, loc, code, boolean_type_node, ar, br);
1570 ci = gimple_build (&stmts, loc, code, boolean_type_node, ai, bi);
1571 cc = gimple_build (&stmts, loc,
1572 (code == EQ_EXPR ? BIT_AND_EXPR : BIT_IOR_EXPR),
1573 boolean_type_node, cr, ci);
1574 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
1576 switch (gimple_code (stmt))
1578 case GIMPLE_RETURN:
1580 greturn *return_stmt = as_a <greturn *> (stmt);
1581 type = TREE_TYPE (gimple_return_retval (return_stmt));
1582 gimple_return_set_retval (return_stmt, fold_convert (type, cc));
1584 break;
1586 case GIMPLE_ASSIGN:
1587 type = TREE_TYPE (gimple_assign_lhs (stmt));
1588 gimple_assign_set_rhs_from_tree (gsi, fold_convert (type, cc));
1589 stmt = gsi_stmt (*gsi);
1590 break;
1592 case GIMPLE_COND:
1594 gcond *cond_stmt = as_a <gcond *> (stmt);
1595 gimple_cond_set_code (cond_stmt, EQ_EXPR);
1596 gimple_cond_set_lhs (cond_stmt, cc);
1597 gimple_cond_set_rhs (cond_stmt, boolean_true_node);
1599 break;
1601 default:
1602 gcc_unreachable ();
1605 update_stmt (stmt);
1606 if (maybe_clean_eh_stmt (stmt))
1607 bitmap_set_bit (need_eh_cleanup, gimple_bb (stmt)->index);
1610 /* Expand inline asm that sets some complex SSA_NAMEs. */
1612 static void
1613 expand_complex_asm (gimple_stmt_iterator *gsi)
1615 gasm *stmt = as_a <gasm *> (gsi_stmt (*gsi));
1616 unsigned int i;
1618 for (i = 0; i < gimple_asm_noutputs (stmt); ++i)
1620 tree link = gimple_asm_output_op (stmt, i);
1621 tree op = TREE_VALUE (link);
1622 if (TREE_CODE (op) == SSA_NAME
1623 && TREE_CODE (TREE_TYPE (op)) == COMPLEX_TYPE)
1625 tree type = TREE_TYPE (op);
1626 tree inner_type = TREE_TYPE (type);
1627 tree r = build1 (REALPART_EXPR, inner_type, op);
1628 tree i = build1 (IMAGPART_EXPR, inner_type, op);
1629 gimple_seq list = set_component_ssa_name (op, false, r);
1631 if (list)
1632 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
1634 list = set_component_ssa_name (op, true, i);
1635 if (list)
1636 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
1641 /* Process one statement. If we identify a complex operation, expand it. */
1643 static void
1644 expand_complex_operations_1 (gimple_stmt_iterator *gsi)
1646 gimple *stmt = gsi_stmt (*gsi);
1647 tree type, inner_type, lhs;
1648 tree ac, ar, ai, bc, br, bi;
1649 complex_lattice_t al, bl;
1650 enum tree_code code;
1652 if (gimple_code (stmt) == GIMPLE_ASM)
1654 expand_complex_asm (gsi);
1655 return;
1658 lhs = gimple_get_lhs (stmt);
1659 if (!lhs && gimple_code (stmt) != GIMPLE_COND)
1660 return;
1662 type = TREE_TYPE (gimple_op (stmt, 0));
1663 code = gimple_expr_code (stmt);
1665 /* Initial filter for operations we handle. */
1666 switch (code)
1668 case PLUS_EXPR:
1669 case MINUS_EXPR:
1670 case MULT_EXPR:
1671 case TRUNC_DIV_EXPR:
1672 case CEIL_DIV_EXPR:
1673 case FLOOR_DIV_EXPR:
1674 case ROUND_DIV_EXPR:
1675 case RDIV_EXPR:
1676 case NEGATE_EXPR:
1677 case CONJ_EXPR:
1678 if (TREE_CODE (type) != COMPLEX_TYPE)
1679 return;
1680 inner_type = TREE_TYPE (type);
1681 break;
1683 case EQ_EXPR:
1684 case NE_EXPR:
1685 /* Note, both GIMPLE_ASSIGN and GIMPLE_COND may have an EQ_EXPR
1686 subcode, so we need to access the operands using gimple_op. */
1687 inner_type = TREE_TYPE (gimple_op (stmt, 1));
1688 if (TREE_CODE (inner_type) != COMPLEX_TYPE)
1689 return;
1690 break;
1692 default:
1694 tree rhs;
1696 /* GIMPLE_COND may also fallthru here, but we do not need to
1697 do anything with it. */
1698 if (gimple_code (stmt) == GIMPLE_COND)
1699 return;
1701 if (TREE_CODE (type) == COMPLEX_TYPE)
1702 expand_complex_move (gsi, type);
1703 else if (is_gimple_assign (stmt)
1704 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
1705 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
1706 && TREE_CODE (lhs) == SSA_NAME)
1708 rhs = gimple_assign_rhs1 (stmt);
1709 rhs = extract_component (gsi, TREE_OPERAND (rhs, 0),
1710 gimple_assign_rhs_code (stmt)
1711 == IMAGPART_EXPR,
1712 false);
1713 gimple_assign_set_rhs_from_tree (gsi, rhs);
1714 stmt = gsi_stmt (*gsi);
1715 update_stmt (stmt);
1718 return;
1721 /* Extract the components of the two complex values. Make sure and
1722 handle the common case of the same value used twice specially. */
1723 if (is_gimple_assign (stmt))
1725 ac = gimple_assign_rhs1 (stmt);
1726 bc = (gimple_num_ops (stmt) > 2) ? gimple_assign_rhs2 (stmt) : NULL;
1728 /* GIMPLE_CALL cannot get here. */
1729 else
1731 ac = gimple_cond_lhs (stmt);
1732 bc = gimple_cond_rhs (stmt);
1735 ar = extract_component (gsi, ac, false, true);
1736 ai = extract_component (gsi, ac, true, true);
1738 if (ac == bc)
1739 br = ar, bi = ai;
1740 else if (bc)
1742 br = extract_component (gsi, bc, 0, true);
1743 bi = extract_component (gsi, bc, 1, true);
1745 else
1746 br = bi = NULL_TREE;
1748 al = find_lattice_value (ac);
1749 if (al == UNINITIALIZED)
1750 al = VARYING;
1752 if (TREE_CODE_CLASS (code) == tcc_unary)
1753 bl = UNINITIALIZED;
1754 else if (ac == bc)
1755 bl = al;
1756 else
1758 bl = find_lattice_value (bc);
1759 if (bl == UNINITIALIZED)
1760 bl = VARYING;
1763 switch (code)
1765 case PLUS_EXPR:
1766 case MINUS_EXPR:
1767 expand_complex_addition (gsi, inner_type, ar, ai, br, bi, code, al, bl);
1768 break;
1770 case MULT_EXPR:
1771 expand_complex_multiplication (gsi, type, ar, ai, br, bi, al, bl);
1772 break;
1774 case TRUNC_DIV_EXPR:
1775 case CEIL_DIV_EXPR:
1776 case FLOOR_DIV_EXPR:
1777 case ROUND_DIV_EXPR:
1778 case RDIV_EXPR:
1779 expand_complex_division (gsi, type, ar, ai, br, bi, code, al, bl);
1780 break;
1782 case NEGATE_EXPR:
1783 expand_complex_negation (gsi, inner_type, ar, ai);
1784 break;
1786 case CONJ_EXPR:
1787 expand_complex_conjugate (gsi, inner_type, ar, ai);
1788 break;
1790 case EQ_EXPR:
1791 case NE_EXPR:
1792 expand_complex_comparison (gsi, ar, ai, br, bi, code);
1793 break;
1795 default:
1796 gcc_unreachable ();
1801 /* Entry point for complex operation lowering during optimization. */
1803 static unsigned int
1804 tree_lower_complex (void)
1806 gimple_stmt_iterator gsi;
1807 basic_block bb;
1808 int n_bbs, i;
1809 int *rpo;
1811 if (!init_dont_simulate_again ())
1812 return 0;
1814 complex_lattice_values.create (num_ssa_names);
1815 complex_lattice_values.safe_grow_cleared (num_ssa_names, true);
1817 init_parameter_lattice_values ();
1818 class complex_propagate complex_propagate;
1819 complex_propagate.ssa_propagate ();
1821 need_eh_cleanup = BITMAP_ALLOC (NULL);
1823 complex_variable_components = new int_tree_htab_type (10);
1825 complex_ssa_name_components.create (2 * num_ssa_names);
1826 complex_ssa_name_components.safe_grow_cleared (2 * num_ssa_names, true);
1828 update_parameter_components ();
1830 rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
1831 n_bbs = pre_and_rev_post_order_compute (NULL, rpo, false);
1832 for (i = 0; i < n_bbs; i++)
1834 bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
1835 if (!bb)
1836 continue;
1837 update_phi_components (bb);
1838 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1839 expand_complex_operations_1 (&gsi);
1842 free (rpo);
1844 if (!phis_to_revisit.is_empty ())
1846 unsigned int n = phis_to_revisit.length ();
1847 for (unsigned int j = 0; j < n; j += 3)
1848 for (unsigned int k = 0; k < 2; k++)
1849 if (gphi *phi = phis_to_revisit[j + k + 1])
1851 unsigned int m = gimple_phi_num_args (phi);
1852 for (unsigned int l = 0; l < m; ++l)
1854 tree op = gimple_phi_arg_def (phi, l);
1855 if (TREE_CODE (op) == SSA_NAME
1856 || is_gimple_min_invariant (op))
1857 continue;
1858 tree arg = gimple_phi_arg_def (phis_to_revisit[j], l);
1859 op = extract_component (NULL, arg, k > 0, false, false);
1860 SET_PHI_ARG_DEF (phi, l, op);
1863 phis_to_revisit.release ();
1866 gsi_commit_edge_inserts ();
1868 unsigned todo
1869 = gimple_purge_all_dead_eh_edges (need_eh_cleanup) ? TODO_cleanup_cfg : 0;
1870 BITMAP_FREE (need_eh_cleanup);
1872 delete complex_variable_components;
1873 complex_variable_components = NULL;
1874 complex_ssa_name_components.release ();
1875 complex_lattice_values.release ();
1876 return todo;
1879 namespace {
1881 const pass_data pass_data_lower_complex =
1883 GIMPLE_PASS, /* type */
1884 "cplxlower", /* name */
1885 OPTGROUP_NONE, /* optinfo_flags */
1886 TV_NONE, /* tv_id */
1887 PROP_ssa, /* properties_required */
1888 PROP_gimple_lcx, /* properties_provided */
1889 0, /* properties_destroyed */
1890 0, /* todo_flags_start */
1891 TODO_update_ssa, /* todo_flags_finish */
1894 class pass_lower_complex : public gimple_opt_pass
1896 public:
1897 pass_lower_complex (gcc::context *ctxt)
1898 : gimple_opt_pass (pass_data_lower_complex, ctxt)
1901 /* opt_pass methods: */
1902 opt_pass * clone () { return new pass_lower_complex (m_ctxt); }
1903 virtual unsigned int execute (function *) { return tree_lower_complex (); }
1905 }; // class pass_lower_complex
1907 } // anon namespace
1909 gimple_opt_pass *
1910 make_pass_lower_complex (gcc::context *ctxt)
1912 return new pass_lower_complex (ctxt);
1916 namespace {
1918 const pass_data pass_data_lower_complex_O0 =
1920 GIMPLE_PASS, /* type */
1921 "cplxlower0", /* name */
1922 OPTGROUP_NONE, /* optinfo_flags */
1923 TV_NONE, /* tv_id */
1924 PROP_cfg, /* properties_required */
1925 PROP_gimple_lcx, /* properties_provided */
1926 0, /* properties_destroyed */
1927 0, /* todo_flags_start */
1928 TODO_update_ssa, /* todo_flags_finish */
1931 class pass_lower_complex_O0 : public gimple_opt_pass
1933 public:
1934 pass_lower_complex_O0 (gcc::context *ctxt)
1935 : gimple_opt_pass (pass_data_lower_complex_O0, ctxt)
1938 /* opt_pass methods: */
1939 virtual bool gate (function *fun)
1941 /* With errors, normal optimization passes are not run. If we don't
1942 lower complex operations at all, rtl expansion will abort. */
1943 return !(fun->curr_properties & PROP_gimple_lcx);
1946 virtual unsigned int execute (function *) { return tree_lower_complex (); }
1948 }; // class pass_lower_complex_O0
1950 } // anon namespace
1952 gimple_opt_pass *
1953 make_pass_lower_complex_O0 (gcc::context *ctxt)
1955 return new pass_lower_complex_O0 (ctxt);