1 /* Lower complex number operations to scalar operations.
2 Copyright (C) 2004, 2005 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 2, or (at your option) any
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
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
23 #include "coretypes.h"
29 #include "tree-flow.h"
30 #include "tree-gimple.h"
31 #include "tree-iterator.h"
32 #include "tree-pass.h"
33 #include "tree-ssa-propagate.h"
36 /* For each complex ssa name, a lattice value. We're interested in finding
37 out whether a complex number is degenerate in some way, having only real
38 or only complex parts. */
48 #define PAIR(a, b) ((a) << 2 | (b))
50 DEF_VEC_I(complex_lattice_t);
51 DEF_VEC_ALLOC_I(complex_lattice_t, heap);
53 static VEC(complex_lattice_t, heap) *complex_lattice_values;
55 /* For each complex variable, a pair of variables for the components exists in
57 static htab_t complex_variable_components;
59 /* Lookup UID in the complex_variable_components hashtable and return the
62 cvc_lookup (unsigned int uid)
64 struct int_tree_map *h, in;
66 h = htab_find_with_hash (complex_variable_components, &in, uid);
71 /* Insert the pair UID, TO into the complex_variable_components hashtable. */
74 cvc_insert (unsigned int uid, tree to)
76 struct int_tree_map *h;
79 h = xmalloc (sizeof (struct int_tree_map));
82 loc = htab_find_slot_with_hash (complex_variable_components, h,
84 *(struct int_tree_map **) loc = h;
88 /* Return true if T is not a zero constant. In the case of real values,
89 we're only interested in +0.0. */
92 some_nonzerop (tree t)
96 if (TREE_CODE (t) == REAL_CST)
97 zerop = REAL_VALUES_IDENTICAL (TREE_REAL_CST (t), dconst0);
98 else if (TREE_CODE (t) == INTEGER_CST)
99 zerop = integer_zerop (t);
104 /* Compute a lattice value from T. It may be a gimple_val, or, as a
105 special exception, a COMPLEX_EXPR. */
107 static complex_lattice_t
108 find_lattice_value (tree t)
112 complex_lattice_t ret;
114 switch (TREE_CODE (t))
117 return VEC_index (complex_lattice_t, complex_lattice_values,
118 SSA_NAME_VERSION (t));
121 real = TREE_REALPART (t);
122 imag = TREE_IMAGPART (t);
126 real = TREE_OPERAND (t, 0);
127 imag = TREE_OPERAND (t, 1);
134 r = some_nonzerop (real);
135 i = some_nonzerop (imag);
136 ret = r*ONLY_REAL + i*ONLY_IMAG;
138 /* ??? On occasion we could do better than mapping 0+0i to real, but we
139 certainly don't want to leave it UNINITIALIZED, which eventually gets
140 mapped to VARYING. */
141 if (ret == UNINITIALIZED)
147 /* Determine if LHS is something for which we're interested in seeing
148 simulation results. */
151 is_complex_reg (tree lhs)
153 return TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE && is_gimple_reg (lhs);
156 /* Mark the incoming parameters to the function as VARYING. */
159 init_parameter_lattice_values (void)
163 for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = TREE_CHAIN (parm))
164 if (is_complex_reg (parm) && var_ann (parm) != NULL)
166 tree ssa_name = default_def (parm);
167 VEC_replace (complex_lattice_t, complex_lattice_values,
168 SSA_NAME_VERSION (ssa_name), VARYING);
172 /* Initialize DONT_SIMULATE_AGAIN for each stmt and phi. Return false if
173 we found no statements we want to simulate, and thus there's nothing for
174 the entire pass to do. */
177 init_dont_simulate_again (void)
180 block_stmt_iterator bsi;
182 bool saw_a_complex_op = false;
186 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
187 DONT_SIMULATE_AGAIN (phi) = !is_complex_reg (PHI_RESULT (phi));
189 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
191 tree orig_stmt, stmt, rhs = NULL;
194 orig_stmt = stmt = bsi_stmt (bsi);
196 /* Most control-altering statements must be initially
197 simulated, else we won't cover the entire cfg. */
198 dsa = !stmt_ends_bb_p (stmt);
200 switch (TREE_CODE (stmt))
203 /* We don't care what the lattice value of <retval> is,
204 since it's never used as an input to another computation. */
206 stmt = TREE_OPERAND (stmt, 0);
207 if (!stmt || TREE_CODE (stmt) != MODIFY_EXPR)
212 dsa = !is_complex_reg (TREE_OPERAND (stmt, 0));
213 rhs = TREE_OPERAND (stmt, 1);
217 rhs = TREE_OPERAND (stmt, 0);
225 switch (TREE_CODE (rhs))
229 rhs = TREE_OPERAND (rhs, 0);
242 if (TREE_CODE (TREE_TYPE (rhs)) == COMPLEX_TYPE)
243 saw_a_complex_op = true;
250 DONT_SIMULATE_AGAIN (orig_stmt) = dsa;
254 return saw_a_complex_op;
258 /* Evaluate statement STMT against the complex lattice defined above. */
260 static enum ssa_prop_result
261 complex_visit_stmt (tree stmt, edge *taken_edge_p ATTRIBUTE_UNUSED,
264 complex_lattice_t new_l, old_l, op1_l, op2_l;
268 if (TREE_CODE (stmt) != MODIFY_EXPR)
269 return SSA_PROP_VARYING;
271 lhs = TREE_OPERAND (stmt, 0);
272 rhs = TREE_OPERAND (stmt, 1);
274 /* These conditions should be satisfied due to the initial filter
275 set up in init_dont_simulate_again. */
276 gcc_assert (TREE_CODE (lhs) == SSA_NAME);
277 gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE);
280 ver = SSA_NAME_VERSION (lhs);
281 old_l = VEC_index (complex_lattice_t, complex_lattice_values, ver);
283 switch (TREE_CODE (rhs))
288 new_l = find_lattice_value (rhs);
293 op1_l = find_lattice_value (TREE_OPERAND (rhs, 0));
294 op2_l = find_lattice_value (TREE_OPERAND (rhs, 1));
296 /* We've set up the lattice values such that IOR neatly
298 new_l = op1_l | op2_l;
307 op1_l = find_lattice_value (TREE_OPERAND (rhs, 0));
308 op2_l = find_lattice_value (TREE_OPERAND (rhs, 1));
310 /* Obviously, if either varies, so does the result. */
311 if (op1_l == VARYING || op2_l == VARYING)
313 /* Don't prematurely promote variables if we've not yet seen
315 else if (op1_l == UNINITIALIZED)
317 else if (op2_l == UNINITIALIZED)
321 /* At this point both numbers have only one component. If the
322 numbers are of opposite kind, the result is imaginary,
323 otherwise the result is real. The add/subtract translates
324 the real/imag from/to 0/1; the ^ performs the comparison. */
325 new_l = ((op1_l - ONLY_REAL) ^ (op2_l - ONLY_REAL)) + ONLY_REAL;
327 /* Don't allow the lattice value to flip-flop indefinitely. */
334 new_l = find_lattice_value (TREE_OPERAND (rhs, 0));
342 /* If nothing changed this round, let the propagator know. */
344 return SSA_PROP_NOT_INTERESTING;
346 VEC_replace (complex_lattice_t, complex_lattice_values, ver, new_l);
347 return new_l == VARYING ? SSA_PROP_VARYING : SSA_PROP_INTERESTING;
350 /* Evaluate a PHI node against the complex lattice defined above. */
352 static enum ssa_prop_result
353 complex_visit_phi (tree phi)
355 complex_lattice_t new_l, old_l;
360 lhs = PHI_RESULT (phi);
362 /* This condition should be satisfied due to the initial filter
363 set up in init_dont_simulate_again. */
364 gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE);
366 /* We've set up the lattice values such that IOR neatly models PHI meet. */
367 new_l = UNINITIALIZED;
368 for (i = PHI_NUM_ARGS (phi) - 1; i >= 0; --i)
369 new_l |= find_lattice_value (PHI_ARG_DEF (phi, i));
371 ver = SSA_NAME_VERSION (lhs);
372 old_l = VEC_index (complex_lattice_t, complex_lattice_values, ver);
375 return SSA_PROP_NOT_INTERESTING;
377 VEC_replace (complex_lattice_t, complex_lattice_values, ver, new_l);
378 return new_l == VARYING ? SSA_PROP_VARYING : SSA_PROP_INTERESTING;
381 /* For each referenced complex gimple register, set up a pair of registers
382 to hold the components of the complex value. */
385 create_components (void)
389 referenced_var_iterator rvi;
391 n = num_referenced_vars;
395 complex_variable_components = htab_create (10, int_tree_map_hash,
396 int_tree_map_eq, free);
398 FOR_EACH_REFERENCED_VAR (var, rvi)
400 tree r = NULL, i = NULL;
403 && TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE
404 && is_gimple_reg (var))
406 tree inner_type = TREE_TYPE (TREE_TYPE (var));
408 r = make_rename_temp (inner_type, "CR");
409 i = make_rename_temp (inner_type, "CI");
410 DECL_SOURCE_LOCATION (r) = DECL_SOURCE_LOCATION (var);
411 DECL_SOURCE_LOCATION (i) = DECL_SOURCE_LOCATION (var);
412 DECL_ARTIFICIAL (r) = 1;
413 DECL_ARTIFICIAL (i) = 1;
415 if (DECL_NAME (var) && !DECL_IGNORED_P (var))
417 const char *name = IDENTIFIER_POINTER (DECL_NAME (var));
419 DECL_NAME (r) = get_identifier (ACONCAT ((name, "$real", NULL)));
420 DECL_NAME (i) = get_identifier (ACONCAT ((name, "$imag", NULL)));
422 SET_DECL_DEBUG_EXPR (r, build1 (REALPART_EXPR, inner_type, var));
423 SET_DECL_DEBUG_EXPR (i, build1 (IMAGPART_EXPR, inner_type, var));
424 DECL_DEBUG_EXPR_IS_FROM (r) = 1;
425 DECL_DEBUG_EXPR_IS_FROM (i) = 1;
427 DECL_IGNORED_P (r) = 0;
428 DECL_IGNORED_P (i) = 0;
430 TREE_NO_WARNING (r) = TREE_NO_WARNING (var);
431 TREE_NO_WARNING (i) = TREE_NO_WARNING (var);
435 DECL_IGNORED_P (r) = 1;
436 DECL_IGNORED_P (i) = 1;
437 TREE_NO_WARNING (r) = 1;
438 TREE_NO_WARNING (i) = 1;
442 cvc_insert (2 * DECL_UID (var), r);
443 cvc_insert (2 * DECL_UID (var) + 1, i);
447 /* Extract the real or imaginary part of a complex variable or constant.
448 Make sure that it's a proper gimple_val and gimplify it if not.
449 Emit any new code before BSI. */
452 extract_component (block_stmt_iterator *bsi, tree t, bool imagpart_p,
455 switch (TREE_CODE (t))
458 return imagpart_p ? TREE_IMAGPART (t) : TREE_REALPART (t);
461 return TREE_OPERAND (t, imagpart_p);
469 tree inner_type = TREE_TYPE (TREE_TYPE (t));
471 t = build1 ((imagpart_p ? IMAGPART_EXPR : REALPART_EXPR),
472 inner_type, unshare_expr (t));
475 t = gimplify_val (bsi, inner_type, t);
482 tree def = SSA_NAME_DEF_STMT (t);
484 if (TREE_CODE (def) == MODIFY_EXPR)
486 def = TREE_OPERAND (def, 1);
487 if (TREE_CODE (def) == COMPLEX_CST)
488 return imagpart_p ? TREE_IMAGPART (def) : TREE_REALPART (def);
489 if (TREE_CODE (def) == COMPLEX_EXPR)
491 def = TREE_OPERAND (def, imagpart_p);
492 if (TREE_CONSTANT (def))
497 return cvc_lookup (DECL_UID (SSA_NAME_VAR (t)) * 2 + imagpart_p);
505 /* Update the complex components of the ssa name on the lhs of STMT. */
508 update_complex_components (block_stmt_iterator *bsi, tree stmt, tree r, tree i)
510 unsigned int uid = DECL_UID (SSA_NAME_VAR (TREE_OPERAND (stmt, 0)));
513 v = cvc_lookup (2*uid);
514 x = build2 (MODIFY_EXPR, TREE_TYPE (v), v, r);
515 SET_EXPR_LOCUS (x, EXPR_LOCUS (stmt));
516 TREE_BLOCK (x) = TREE_BLOCK (stmt);
517 bsi_insert_after (bsi, x, BSI_NEW_STMT);
519 v = cvc_lookup (2*uid + 1);
520 x = build2 (MODIFY_EXPR, TREE_TYPE (v), v, i);
521 SET_EXPR_LOCUS (x, EXPR_LOCUS (stmt));
522 TREE_BLOCK (x) = TREE_BLOCK (stmt);
523 bsi_insert_after (bsi, x, BSI_NEW_STMT);
527 update_complex_components_on_edge (edge e, tree stmt, tree lhs, tree r, tree i)
529 unsigned int uid = DECL_UID (SSA_NAME_VAR (lhs));
532 v = cvc_lookup (2*uid);
533 x = build2 (MODIFY_EXPR, TREE_TYPE (v), v, r);
536 SET_EXPR_LOCUS (x, EXPR_LOCUS (stmt));
537 TREE_BLOCK (x) = TREE_BLOCK (stmt);
539 bsi_insert_on_edge (e, x);
541 v = cvc_lookup (2*uid + 1);
542 x = build2 (MODIFY_EXPR, TREE_TYPE (v), v, i);
545 SET_EXPR_LOCUS (x, EXPR_LOCUS (stmt));
546 TREE_BLOCK (x) = TREE_BLOCK (stmt);
548 bsi_insert_on_edge (e, x);
551 /* Update an assignment to a complex variable in place. */
554 update_complex_assignment (block_stmt_iterator *bsi, tree r, tree i)
559 mod = stmt = bsi_stmt (*bsi);
560 if (TREE_CODE (stmt) == RETURN_EXPR)
561 mod = TREE_OPERAND (mod, 0);
563 update_complex_components (bsi, stmt, r, i);
565 type = TREE_TYPE (TREE_OPERAND (mod, 1));
566 TREE_OPERAND (mod, 1) = build (COMPLEX_EXPR, type, r, i);
570 /* Generate code at the entry point of the function to initialize the
571 component variables for a complex parameter. */
574 update_parameter_components (void)
576 edge entry_edge = single_succ_edge (ENTRY_BLOCK_PTR);
579 for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = TREE_CHAIN (parm))
581 tree type = TREE_TYPE (parm);
584 if (TREE_CODE (type) != COMPLEX_TYPE || !is_gimple_reg (parm))
587 type = TREE_TYPE (type);
588 ssa_name = default_def (parm);
590 r = build1 (REALPART_EXPR, type, ssa_name);
591 i = build1 (IMAGPART_EXPR, type, ssa_name);
592 update_complex_components_on_edge (entry_edge, NULL, ssa_name, r, i);
596 /* Generate code to set the component variables of a complex variable
597 to match the PHI statements in block BB. */
600 update_phi_components (basic_block bb)
604 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
605 if (is_complex_reg (PHI_RESULT (phi)))
608 tree lhs = PHI_RESULT (phi);
610 for (i = 0, n = PHI_NUM_ARGS (phi); i < n; ++i)
612 edge e = PHI_ARG_EDGE (phi, i);
613 tree arg = PHI_ARG_DEF (phi, i);
616 /* Avoid no-op assignments. This also prevents insertting stmts
617 onto abnormal edges, assuming the PHI isn't already broken. */
618 if (TREE_CODE (arg) == SSA_NAME
619 && SSA_NAME_VAR (arg) == SSA_NAME_VAR (lhs))
622 r = extract_component (NULL, arg, 0, false);
623 i = extract_component (NULL, arg, 1, false);
624 update_complex_components_on_edge (e, NULL, lhs, r, i);
629 /* Mark each virtual op in STMT for ssa update. */
632 update_all_vops (tree stmt)
637 FOR_EACH_SSA_TREE_OPERAND (sym, stmt, iter, SSA_OP_ALL_VIRTUALS)
639 if (TREE_CODE (sym) == SSA_NAME)
640 sym = SSA_NAME_VAR (sym);
641 mark_sym_for_renaming (sym);
645 /* Expand a complex move to scalars. */
648 expand_complex_move (block_stmt_iterator *bsi, tree stmt, tree type,
651 tree inner_type = TREE_TYPE (type);
654 if (TREE_CODE (lhs) == SSA_NAME)
656 if (is_ctrl_altering_stmt (bsi_stmt (*bsi)))
661 /* The value is not assigned on the exception edges, so we need not
662 concern ourselves there. We do need to update on the fallthru
664 FOR_EACH_EDGE (e, ei, bsi->bb->succs)
665 if (e->flags & EDGE_FALLTHRU)
670 r = build1 (REALPART_EXPR, inner_type, lhs);
671 i = build1 (IMAGPART_EXPR, inner_type, lhs);
672 update_complex_components_on_edge (e, stmt, lhs, r, i);
674 else if (TREE_CODE (rhs) == CALL_EXPR || TREE_SIDE_EFFECTS (rhs))
676 r = build1 (REALPART_EXPR, inner_type, lhs);
677 i = build1 (IMAGPART_EXPR, inner_type, lhs);
678 update_complex_components (bsi, stmt, r, i);
682 update_all_vops (bsi_stmt (*bsi));
683 r = extract_component (bsi, rhs, 0, true);
684 i = extract_component (bsi, rhs, 1, true);
685 update_complex_assignment (bsi, r, i);
688 else if (TREE_CODE (rhs) == SSA_NAME && !TREE_SIDE_EFFECTS (lhs))
692 r = extract_component (bsi, rhs, 0, false);
693 i = extract_component (bsi, rhs, 1, false);
695 x = build1 (REALPART_EXPR, inner_type, unshare_expr (lhs));
696 x = build2 (MODIFY_EXPR, inner_type, x, r);
697 bsi_insert_before (bsi, x, BSI_SAME_STMT);
699 if (stmt == bsi_stmt (*bsi))
701 x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
702 TREE_OPERAND (stmt, 0) = x;
703 TREE_OPERAND (stmt, 1) = i;
704 TREE_TYPE (stmt) = inner_type;
708 x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
709 x = build2 (MODIFY_EXPR, inner_type, x, i);
710 bsi_insert_before (bsi, x, BSI_SAME_STMT);
712 stmt = bsi_stmt (*bsi);
713 gcc_assert (TREE_CODE (stmt) == RETURN_EXPR);
714 TREE_OPERAND (stmt, 0) = lhs;
717 update_all_vops (stmt);
722 /* Expand complex addition to scalars:
723 a + b = (ar + br) + i(ai + bi)
724 a - b = (ar - br) + i(ai + bi)
728 expand_complex_addition (block_stmt_iterator *bsi, tree inner_type,
729 tree ar, tree ai, tree br, tree bi,
731 complex_lattice_t al, complex_lattice_t bl)
735 switch (PAIR (al, bl))
737 case PAIR (ONLY_REAL, ONLY_REAL):
738 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
742 case PAIR (ONLY_REAL, ONLY_IMAG):
744 if (code == MINUS_EXPR)
745 ri = gimplify_build2 (bsi, MINUS_EXPR, inner_type, ai, bi);
750 case PAIR (ONLY_IMAG, ONLY_REAL):
751 if (code == MINUS_EXPR)
752 rr = gimplify_build2 (bsi, MINUS_EXPR, inner_type, ar, br);
758 case PAIR (ONLY_IMAG, ONLY_IMAG):
760 ri = gimplify_build2 (bsi, code, inner_type, ai, bi);
763 case PAIR (VARYING, ONLY_REAL):
764 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
768 case PAIR (VARYING, ONLY_IMAG):
770 ri = gimplify_build2 (bsi, MINUS_EXPR, inner_type, ai, bi);
773 case PAIR (ONLY_REAL, VARYING):
774 if (code == MINUS_EXPR)
776 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
780 case PAIR (ONLY_IMAG, VARYING):
781 if (code == MINUS_EXPR)
784 ri = gimplify_build2 (bsi, MINUS_EXPR, inner_type, ai, bi);
787 case PAIR (VARYING, VARYING):
789 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
790 ri = gimplify_build2 (bsi, code, inner_type, ai, bi);
797 update_complex_assignment (bsi, rr, ri);
800 /* Expand a complex multiplication or division to a libcall to the c99
801 compliant routines. */
804 expand_complex_libcall (block_stmt_iterator *bsi, tree ar, tree ai,
805 tree br, tree bi, enum tree_code code)
807 enum machine_mode mode;
808 enum built_in_function bcode;
809 tree args, fn, stmt, type;
811 args = tree_cons (NULL, bi, NULL);
812 args = tree_cons (NULL, br, args);
813 args = tree_cons (NULL, ai, args);
814 args = tree_cons (NULL, ar, args);
816 stmt = bsi_stmt (*bsi);
817 type = TREE_TYPE (TREE_OPERAND (stmt, 1));
819 mode = TYPE_MODE (type);
820 gcc_assert (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT);
821 if (code == MULT_EXPR)
822 bcode = BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT;
823 else if (code == RDIV_EXPR)
824 bcode = BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT;
827 fn = built_in_decls[bcode];
829 TREE_OPERAND (stmt, 1)
830 = build3 (CALL_EXPR, type, build_fold_addr_expr (fn), args, NULL);
835 tree lhs = TREE_OPERAND (stmt, 0);
836 type = TREE_TYPE (type);
837 update_complex_components (bsi, stmt,
838 build1 (REALPART_EXPR, type, lhs),
839 build1 (IMAGPART_EXPR, type, lhs));
843 /* Expand complex multiplication to scalars:
844 a * b = (ar*br - ai*bi) + i(ar*bi + br*ai)
848 expand_complex_multiplication (block_stmt_iterator *bsi, tree inner_type,
849 tree ar, tree ai, tree br, tree bi,
850 complex_lattice_t al, complex_lattice_t bl)
856 complex_lattice_t tl;
857 rr = ar, ar = br, br = rr;
858 ri = ai, ai = bi, bi = ri;
859 tl = al, al = bl, bl = tl;
862 switch (PAIR (al, bl))
864 case PAIR (ONLY_REAL, ONLY_REAL):
865 rr = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, br);
869 case PAIR (ONLY_IMAG, ONLY_REAL):
871 if (TREE_CODE (ai) == REAL_CST
872 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (ai), dconst1))
875 ri = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, br);
878 case PAIR (ONLY_IMAG, ONLY_IMAG):
879 rr = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, bi);
880 rr = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, rr);
884 case PAIR (VARYING, ONLY_REAL):
885 rr = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, br);
886 ri = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, br);
889 case PAIR (VARYING, ONLY_IMAG):
890 rr = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, bi);
891 rr = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, rr);
892 ri = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, bi);
895 case PAIR (VARYING, VARYING):
896 if (flag_complex_method == 2 && SCALAR_FLOAT_TYPE_P (inner_type))
898 expand_complex_libcall (bsi, ar, ai, br, bi, MULT_EXPR);
905 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, br);
906 t2 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, bi);
907 t3 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, bi);
909 /* Avoid expanding redundant multiplication for the common
910 case of squaring a complex number. */
911 if (ar == br && ai == bi)
914 t4 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, br);
916 rr = gimplify_build2 (bsi, MINUS_EXPR, inner_type, t1, t2);
917 ri = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t3, t4);
925 update_complex_assignment (bsi, rr, ri);
928 /* Expand complex division to scalars, straightforward algorithm.
929 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
934 expand_complex_div_straight (block_stmt_iterator *bsi, tree inner_type,
935 tree ar, tree ai, tree br, tree bi,
938 tree rr, ri, div, t1, t2, t3;
940 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, br, br);
941 t2 = gimplify_build2 (bsi, MULT_EXPR, inner_type, bi, bi);
942 div = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, t2);
944 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, br);
945 t2 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, bi);
946 t3 = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, t2);
947 rr = gimplify_build2 (bsi, code, inner_type, t3, div);
949 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, br);
950 t2 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, bi);
951 t3 = gimplify_build2 (bsi, MINUS_EXPR, inner_type, t1, t2);
952 ri = gimplify_build2 (bsi, code, inner_type, t3, div);
954 update_complex_assignment (bsi, rr, ri);
957 /* Expand complex division to scalars, modified algorithm to minimize
958 overflow with wide input ranges. */
961 expand_complex_div_wide (block_stmt_iterator *bsi, tree inner_type,
962 tree ar, tree ai, tree br, tree bi,
965 tree rr, ri, ratio, div, t1, t2, tr, ti, cond;
966 basic_block bb_cond, bb_true, bb_false, bb_join;
968 /* Examine |br| < |bi|, and branch. */
969 t1 = gimplify_build1 (bsi, ABS_EXPR, inner_type, br);
970 t2 = gimplify_build1 (bsi, ABS_EXPR, inner_type, bi);
971 cond = fold_build2 (LT_EXPR, boolean_type_node, t1, t2);
974 bb_cond = bb_true = bb_false = bb_join = NULL;
975 rr = ri = tr = ti = NULL;
976 if (!TREE_CONSTANT (cond))
980 cond = build (COND_EXPR, void_type_node, cond, NULL, NULL);
981 bsi_insert_before (bsi, cond, BSI_SAME_STMT);
983 /* Split the original block, and create the TRUE and FALSE blocks. */
984 e = split_block (bsi->bb, cond);
987 bb_true = create_empty_bb (bb_cond);
988 bb_false = create_empty_bb (bb_true);
990 t1 = build (GOTO_EXPR, void_type_node, tree_block_label (bb_true));
991 t2 = build (GOTO_EXPR, void_type_node, tree_block_label (bb_false));
992 COND_EXPR_THEN (cond) = t1;
993 COND_EXPR_ELSE (cond) = t2;
995 /* Wire the blocks together. */
996 e->flags = EDGE_TRUE_VALUE;
997 redirect_edge_succ (e, bb_true);
998 make_edge (bb_cond, bb_false, EDGE_FALSE_VALUE);
999 make_edge (bb_true, bb_join, EDGE_FALLTHRU);
1000 make_edge (bb_false, bb_join, EDGE_FALLTHRU);
1002 /* Update dominance info. Note that bb_join's data was
1003 updated by split_block. */
1004 if (dom_info_available_p (CDI_DOMINATORS))
1006 set_immediate_dominator (CDI_DOMINATORS, bb_true, bb_cond);
1007 set_immediate_dominator (CDI_DOMINATORS, bb_false, bb_cond);
1010 rr = make_rename_temp (inner_type, NULL);
1011 ri = make_rename_temp (inner_type, NULL);
1014 /* In the TRUE branch, we compute
1016 div = (br * ratio) + bi;
1017 tr = (ar * ratio) + ai;
1018 ti = (ai * ratio) - ar;
1021 if (bb_true || integer_nonzerop (cond))
1025 *bsi = bsi_last (bb_true);
1026 bsi_insert_after (bsi, build_empty_stmt (), BSI_NEW_STMT);
1029 ratio = gimplify_build2 (bsi, code, inner_type, br, bi);
1031 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, br, ratio);
1032 div = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, bi);
1034 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, ratio);
1035 tr = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, ai);
1037 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, ratio);
1038 ti = gimplify_build2 (bsi, MINUS_EXPR, inner_type, t1, ar);
1040 tr = gimplify_build2 (bsi, code, inner_type, tr, div);
1041 ti = gimplify_build2 (bsi, code, inner_type, ti, div);
1045 t1 = build (MODIFY_EXPR, inner_type, rr, tr);
1046 bsi_insert_before (bsi, t1, BSI_SAME_STMT);
1047 t1 = build (MODIFY_EXPR, inner_type, ri, ti);
1048 bsi_insert_before (bsi, t1, BSI_SAME_STMT);
1053 /* In the FALSE branch, we compute
1055 divisor = (d * ratio) + c;
1056 tr = (b * ratio) + a;
1057 ti = b - (a * ratio);
1060 if (bb_false || integer_zerop (cond))
1064 *bsi = bsi_last (bb_false);
1065 bsi_insert_after (bsi, build_empty_stmt (), BSI_NEW_STMT);
1068 ratio = gimplify_build2 (bsi, code, inner_type, bi, br);
1070 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, bi, ratio);
1071 div = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, br);
1073 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, ratio);
1074 tr = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, ar);
1076 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, ratio);
1077 ti = gimplify_build2 (bsi, MINUS_EXPR, inner_type, ai, t1);
1079 tr = gimplify_build2 (bsi, code, inner_type, tr, div);
1080 ti = gimplify_build2 (bsi, code, inner_type, ti, div);
1084 t1 = build (MODIFY_EXPR, inner_type, rr, tr);
1085 bsi_insert_before (bsi, t1, BSI_SAME_STMT);
1086 t1 = build (MODIFY_EXPR, inner_type, ri, ti);
1087 bsi_insert_before (bsi, t1, BSI_SAME_STMT);
1093 *bsi = bsi_start (bb_join);
1097 update_complex_assignment (bsi, rr, ri);
1100 /* Expand complex division to scalars. */
1103 expand_complex_division (block_stmt_iterator *bsi, tree inner_type,
1104 tree ar, tree ai, tree br, tree bi,
1105 enum tree_code code,
1106 complex_lattice_t al, complex_lattice_t bl)
1110 switch (PAIR (al, bl))
1112 case PAIR (ONLY_REAL, ONLY_REAL):
1113 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
1117 case PAIR (ONLY_REAL, ONLY_IMAG):
1119 ri = gimplify_build2 (bsi, code, inner_type, ar, bi);
1120 ri = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, ri);
1123 case PAIR (ONLY_IMAG, ONLY_REAL):
1125 ri = gimplify_build2 (bsi, code, inner_type, ai, br);
1128 case PAIR (ONLY_IMAG, ONLY_IMAG):
1129 rr = gimplify_build2 (bsi, code, inner_type, ai, bi);
1133 case PAIR (VARYING, ONLY_REAL):
1134 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
1135 ri = gimplify_build2 (bsi, code, inner_type, ai, br);
1138 case PAIR (VARYING, ONLY_IMAG):
1139 rr = gimplify_build2 (bsi, code, inner_type, ai, bi);
1140 ri = gimplify_build2 (bsi, code, inner_type, ar, bi);
1141 ri = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, ri);
1143 case PAIR (ONLY_REAL, VARYING):
1144 case PAIR (ONLY_IMAG, VARYING):
1145 case PAIR (VARYING, VARYING):
1146 switch (flag_complex_method)
1149 /* straightforward implementation of complex divide acceptable. */
1150 expand_complex_div_straight (bsi, inner_type, ar, ai, br, bi, code);
1154 if (SCALAR_FLOAT_TYPE_P (inner_type))
1156 expand_complex_libcall (bsi, ar, ai, br, bi, code);
1162 /* wide ranges of inputs must work for complex divide. */
1163 expand_complex_div_wide (bsi, inner_type, ar, ai, br, bi, code);
1175 update_complex_assignment (bsi, rr, ri);
1178 /* Expand complex negation to scalars:
1183 expand_complex_negation (block_stmt_iterator *bsi, tree inner_type,
1188 rr = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, ar);
1189 ri = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, ai);
1191 update_complex_assignment (bsi, rr, ri);
1194 /* Expand complex conjugate to scalars:
1199 expand_complex_conjugate (block_stmt_iterator *bsi, tree inner_type,
1204 ri = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, ai);
1206 update_complex_assignment (bsi, ar, ri);
1209 /* Expand complex comparison (EQ or NE only). */
1212 expand_complex_comparison (block_stmt_iterator *bsi, tree ar, tree ai,
1213 tree br, tree bi, enum tree_code code)
1215 tree cr, ci, cc, stmt, expr, type;
1217 cr = gimplify_build2 (bsi, code, boolean_type_node, ar, br);
1218 ci = gimplify_build2 (bsi, code, boolean_type_node, ai, bi);
1219 cc = gimplify_build2 (bsi,
1220 (code == EQ_EXPR ? TRUTH_AND_EXPR : TRUTH_OR_EXPR),
1221 boolean_type_node, cr, ci);
1223 stmt = expr = bsi_stmt (*bsi);
1225 switch (TREE_CODE (stmt))
1228 expr = TREE_OPERAND (stmt, 0);
1231 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1232 TREE_OPERAND (expr, 1) = fold_convert (type, cc);
1235 TREE_OPERAND (stmt, 0) = cc;
1244 /* Process one statement. If we identify a complex operation, expand it. */
1247 expand_complex_operations_1 (block_stmt_iterator *bsi)
1249 tree stmt = bsi_stmt (*bsi);
1250 tree rhs, type, inner_type;
1251 tree ac, ar, ai, bc, br, bi;
1252 complex_lattice_t al, bl;
1253 enum tree_code code;
1255 switch (TREE_CODE (stmt))
1258 stmt = TREE_OPERAND (stmt, 0);
1261 if (TREE_CODE (stmt) != MODIFY_EXPR)
1266 rhs = TREE_OPERAND (stmt, 1);
1270 rhs = TREE_OPERAND (stmt, 0);
1277 type = TREE_TYPE (rhs);
1278 code = TREE_CODE (rhs);
1280 /* Initial filter for operations we handle. */
1286 case TRUNC_DIV_EXPR:
1288 case FLOOR_DIV_EXPR:
1289 case ROUND_DIV_EXPR:
1293 if (TREE_CODE (type) != COMPLEX_TYPE)
1295 inner_type = TREE_TYPE (type);
1300 inner_type = TREE_TYPE (TREE_OPERAND (rhs, 1));
1301 if (TREE_CODE (inner_type) != COMPLEX_TYPE)
1307 tree lhs = TREE_OPERAND (stmt, 0);
1308 tree rhs = TREE_OPERAND (stmt, 1);
1310 if (TREE_CODE (type) == COMPLEX_TYPE)
1311 expand_complex_move (bsi, stmt, type, lhs, rhs);
1312 else if ((TREE_CODE (rhs) == REALPART_EXPR
1313 || TREE_CODE (rhs) == IMAGPART_EXPR)
1314 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
1316 TREE_OPERAND (stmt, 1)
1317 = extract_component (bsi, TREE_OPERAND (rhs, 0),
1318 TREE_CODE (rhs) == IMAGPART_EXPR, false);
1325 /* Extract the components of the two complex values. Make sure and
1326 handle the common case of the same value used twice specially. */
1327 ac = TREE_OPERAND (rhs, 0);
1328 ar = extract_component (bsi, ac, 0, true);
1329 ai = extract_component (bsi, ac, 1, true);
1331 if (TREE_CODE_CLASS (code) == tcc_unary)
1332 bc = br = bi = NULL;
1335 bc = TREE_OPERAND (rhs, 1);
1340 br = extract_component (bsi, bc, 0, true);
1341 bi = extract_component (bsi, bc, 1, true);
1347 al = find_lattice_value (ac);
1348 if (al == UNINITIALIZED)
1351 if (TREE_CODE_CLASS (code) == tcc_unary)
1357 bl = find_lattice_value (bc);
1358 if (bl == UNINITIALIZED)
1369 expand_complex_addition (bsi, inner_type, ar, ai, br, bi, code, al, bl);
1373 expand_complex_multiplication (bsi, inner_type, ar, ai, br, bi, al, bl);
1376 case TRUNC_DIV_EXPR:
1378 case FLOOR_DIV_EXPR:
1379 case ROUND_DIV_EXPR:
1381 expand_complex_division (bsi, inner_type, ar, ai, br, bi, code, al, bl);
1385 expand_complex_negation (bsi, inner_type, ar, ai);
1389 expand_complex_conjugate (bsi, inner_type, ar, ai);
1394 expand_complex_comparison (bsi, ar, ai, br, bi, code);
1403 /* Entry point for complex operation lowering during optimization. */
1406 tree_lower_complex (void)
1408 int old_last_basic_block;
1409 block_stmt_iterator bsi;
1412 if (!init_dont_simulate_again ())
1415 complex_lattice_values = VEC_alloc (complex_lattice_t, heap, num_ssa_names);
1416 VEC_safe_grow (complex_lattice_t, heap,
1417 complex_lattice_values, num_ssa_names);
1418 memset (VEC_address (complex_lattice_t, complex_lattice_values), 0,
1419 num_ssa_names * sizeof(complex_lattice_t));
1420 init_parameter_lattice_values ();
1422 ssa_propagate (complex_visit_stmt, complex_visit_phi);
1424 create_components ();
1425 update_parameter_components ();
1427 old_last_basic_block = last_basic_block;
1430 if (bb->index >= old_last_basic_block)
1432 update_phi_components (bb);
1433 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1434 expand_complex_operations_1 (&bsi);
1437 bsi_commit_edge_inserts ();
1439 if (complex_variable_components)
1440 htab_delete (complex_variable_components);
1442 VEC_free (complex_lattice_t, heap, complex_lattice_values);
1445 struct tree_opt_pass pass_lower_complex =
1447 "cplxlower", /* name */
1449 tree_lower_complex, /* execute */
1452 0, /* static_pass_number */
1454 PROP_ssa, /* properties_required */
1455 0, /* properties_provided */
1456 0, /* properties_destroyed */
1457 0, /* todo_flags_start */
1458 TODO_dump_func | TODO_ggc_collect
1460 | TODO_verify_stmts, /* todo_flags_finish */
1465 /* Entry point for complex operation lowering without optimization. */
1468 tree_lower_complex_O0 (void)
1470 int old_last_basic_block = last_basic_block;
1471 block_stmt_iterator bsi;
1476 if (bb->index >= old_last_basic_block)
1478 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1479 expand_complex_operations_1 (&bsi);
1484 gate_no_optimization (void)
1486 return optimize == 0;
1489 struct tree_opt_pass pass_lower_complex_O0 =
1491 "cplxlower0", /* name */
1492 gate_no_optimization, /* gate */
1493 tree_lower_complex_O0, /* execute */
1496 0, /* static_pass_number */
1498 PROP_cfg, /* properties_required */
1499 0, /* properties_provided */
1500 0, /* properties_destroyed */
1501 0, /* todo_flags_start */
1502 TODO_dump_func | TODO_ggc_collect
1503 | TODO_verify_stmts, /* todo_flags_finish */