1 /* Lower complex number operations to scalar operations.
2 Copyright (C) 2004, 2005, 2006, 2007 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
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 COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
28 #include "tree-flow.h"
29 #include "tree-gimple.h"
30 #include "tree-iterator.h"
31 #include "tree-pass.h"
32 #include "tree-ssa-propagate.h"
33 #include "diagnostic.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 /* For each complex SSA_NAME, a pair of ssa names for the components. */
60 static VEC(tree, heap) *complex_ssa_name_components;
62 /* Lookup UID in the complex_variable_components hashtable and return the
65 cvc_lookup (unsigned int uid)
67 struct int_tree_map *h, in;
69 h = htab_find_with_hash (complex_variable_components, &in, uid);
70 return h ? h->to : NULL;
73 /* Insert the pair UID, TO into the complex_variable_components hashtable. */
76 cvc_insert (unsigned int uid, tree to)
78 struct int_tree_map *h;
81 h = XNEW (struct int_tree_map);
84 loc = htab_find_slot_with_hash (complex_variable_components, h,
86 *(struct int_tree_map **) loc = h;
89 /* Return true if T is not a zero constant. In the case of real values,
90 we're only interested in +0.0. */
93 some_nonzerop (tree t)
97 if (TREE_CODE (t) == REAL_CST)
98 zerop = REAL_VALUES_IDENTICAL (TREE_REAL_CST (t), dconst0);
99 else if (TREE_CODE (t) == INTEGER_CST)
100 zerop = integer_zerop (t);
105 /* Compute a lattice value from T. It may be a gimple_val, or, as a
106 special exception, a COMPLEX_EXPR. */
108 static complex_lattice_t
109 find_lattice_value (tree t)
113 complex_lattice_t ret;
115 switch (TREE_CODE (t))
118 return VEC_index (complex_lattice_t, complex_lattice_values,
119 SSA_NAME_VERSION (t));
122 real = TREE_REALPART (t);
123 imag = TREE_IMAGPART (t);
127 real = TREE_OPERAND (t, 0);
128 imag = TREE_OPERAND (t, 1);
135 r = some_nonzerop (real);
136 i = some_nonzerop (imag);
137 ret = r*ONLY_REAL + i*ONLY_IMAG;
139 /* ??? On occasion we could do better than mapping 0+0i to real, but we
140 certainly don't want to leave it UNINITIALIZED, which eventually gets
141 mapped to VARYING. */
142 if (ret == UNINITIALIZED)
148 /* Determine if LHS is something for which we're interested in seeing
149 simulation results. */
152 is_complex_reg (tree lhs)
154 return TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE && is_gimple_reg (lhs);
157 /* Mark the incoming parameters to the function as VARYING. */
160 init_parameter_lattice_values (void)
164 for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = TREE_CHAIN (parm))
165 if (is_complex_reg (parm) && var_ann (parm) != NULL)
167 tree ssa_name = gimple_default_def (cfun, parm);
168 VEC_replace (complex_lattice_t, complex_lattice_values,
169 SSA_NAME_VERSION (ssa_name), VARYING);
173 /* Initialize DONT_SIMULATE_AGAIN for each stmt and phi. Return false if
174 we found no statements we want to simulate, and thus there's nothing for
175 the entire pass to do. */
178 init_dont_simulate_again (void)
181 block_stmt_iterator bsi;
183 bool saw_a_complex_op = false;
187 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
188 DONT_SIMULATE_AGAIN (phi) = !is_complex_reg (PHI_RESULT (phi));
190 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
192 tree orig_stmt, stmt, rhs = NULL;
195 orig_stmt = stmt = bsi_stmt (bsi);
197 /* Most control-altering statements must be initially
198 simulated, else we won't cover the entire cfg. */
199 dsa = !stmt_ends_bb_p (stmt);
201 switch (TREE_CODE (stmt))
204 /* We don't care what the lattice value of <retval> is,
205 since it's never used as an input to another computation. */
207 stmt = TREE_OPERAND (stmt, 0);
208 if (!stmt || TREE_CODE (stmt) != GIMPLE_MODIFY_STMT)
212 case GIMPLE_MODIFY_STMT:
213 dsa = !is_complex_reg (GIMPLE_STMT_OPERAND (stmt, 0));
214 rhs = GIMPLE_STMT_OPERAND (stmt, 1);
218 rhs = TREE_OPERAND (stmt, 0);
226 switch (TREE_CODE (rhs))
230 rhs = TREE_OPERAND (rhs, 0);
243 if (TREE_CODE (TREE_TYPE (rhs)) == COMPLEX_TYPE)
244 saw_a_complex_op = true;
251 DONT_SIMULATE_AGAIN (orig_stmt) = dsa;
255 return saw_a_complex_op;
259 /* Evaluate statement STMT against the complex lattice defined above. */
261 static enum ssa_prop_result
262 complex_visit_stmt (tree stmt, edge *taken_edge_p ATTRIBUTE_UNUSED,
265 complex_lattice_t new_l, old_l, op1_l, op2_l;
269 if (TREE_CODE (stmt) != GIMPLE_MODIFY_STMT)
270 return SSA_PROP_VARYING;
272 lhs = GIMPLE_STMT_OPERAND (stmt, 0);
273 rhs = GIMPLE_STMT_OPERAND (stmt, 1);
275 /* These conditions should be satisfied due to the initial filter
276 set up in init_dont_simulate_again. */
277 gcc_assert (TREE_CODE (lhs) == SSA_NAME);
278 gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE);
281 ver = SSA_NAME_VERSION (lhs);
282 old_l = VEC_index (complex_lattice_t, complex_lattice_values, ver);
284 switch (TREE_CODE (rhs))
289 new_l = find_lattice_value (rhs);
294 op1_l = find_lattice_value (TREE_OPERAND (rhs, 0));
295 op2_l = find_lattice_value (TREE_OPERAND (rhs, 1));
297 /* We've set up the lattice values such that IOR neatly
299 new_l = op1_l | op2_l;
308 op1_l = find_lattice_value (TREE_OPERAND (rhs, 0));
309 op2_l = find_lattice_value (TREE_OPERAND (rhs, 1));
311 /* Obviously, if either varies, so does the result. */
312 if (op1_l == VARYING || op2_l == VARYING)
314 /* Don't prematurely promote variables if we've not yet seen
316 else if (op1_l == UNINITIALIZED)
318 else if (op2_l == UNINITIALIZED)
322 /* At this point both numbers have only one component. If the
323 numbers are of opposite kind, the result is imaginary,
324 otherwise the result is real. The add/subtract translates
325 the real/imag from/to 0/1; the ^ performs the comparison. */
326 new_l = ((op1_l - ONLY_REAL) ^ (op2_l - ONLY_REAL)) + ONLY_REAL;
328 /* Don't allow the lattice value to flip-flop indefinitely. */
335 new_l = find_lattice_value (TREE_OPERAND (rhs, 0));
343 /* If nothing changed this round, let the propagator know. */
345 return SSA_PROP_NOT_INTERESTING;
347 VEC_replace (complex_lattice_t, complex_lattice_values, ver, new_l);
348 return new_l == VARYING ? SSA_PROP_VARYING : SSA_PROP_INTERESTING;
351 /* Evaluate a PHI node against the complex lattice defined above. */
353 static enum ssa_prop_result
354 complex_visit_phi (tree phi)
356 complex_lattice_t new_l, old_l;
361 lhs = PHI_RESULT (phi);
363 /* This condition should be satisfied due to the initial filter
364 set up in init_dont_simulate_again. */
365 gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE);
367 /* We've set up the lattice values such that IOR neatly models PHI meet. */
368 new_l = UNINITIALIZED;
369 for (i = PHI_NUM_ARGS (phi) - 1; i >= 0; --i)
370 new_l |= find_lattice_value (PHI_ARG_DEF (phi, i));
372 ver = SSA_NAME_VERSION (lhs);
373 old_l = VEC_index (complex_lattice_t, complex_lattice_values, ver);
376 return SSA_PROP_NOT_INTERESTING;
378 VEC_replace (complex_lattice_t, complex_lattice_values, ver, new_l);
379 return new_l == VARYING ? SSA_PROP_VARYING : SSA_PROP_INTERESTING;
382 /* Create one backing variable for a complex component of ORIG. */
385 create_one_component_var (tree type, tree orig, const char *prefix,
386 const char *suffix, enum tree_code code)
388 tree r = create_tmp_var (type, prefix);
389 add_referenced_var (r);
391 DECL_SOURCE_LOCATION (r) = DECL_SOURCE_LOCATION (orig);
392 DECL_ARTIFICIAL (r) = 1;
394 if (DECL_NAME (orig) && !DECL_IGNORED_P (orig))
396 const char *name = IDENTIFIER_POINTER (DECL_NAME (orig));
399 DECL_NAME (r) = get_identifier (ACONCAT ((name, suffix, NULL)));
401 inner_type = TREE_TYPE (TREE_TYPE (orig));
402 SET_DECL_DEBUG_EXPR (r, build1 (code, type, orig));
403 DECL_DEBUG_EXPR_IS_FROM (r) = 1;
404 DECL_IGNORED_P (r) = 0;
405 TREE_NO_WARNING (r) = TREE_NO_WARNING (orig);
409 DECL_IGNORED_P (r) = 1;
410 TREE_NO_WARNING (r) = 1;
416 /* Retrieve a value for a complex component of VAR. */
419 get_component_var (tree var, bool imag_p)
421 size_t decl_index = DECL_UID (var) * 2 + imag_p;
422 tree ret = cvc_lookup (decl_index);
426 ret = create_one_component_var (TREE_TYPE (TREE_TYPE (var)), var,
427 imag_p ? "CI" : "CR",
428 imag_p ? "$imag" : "$real",
429 imag_p ? IMAGPART_EXPR : REALPART_EXPR);
430 cvc_insert (decl_index, ret);
436 /* Retrieve a value for a complex component of SSA_NAME. */
439 get_component_ssa_name (tree ssa_name, bool imag_p)
441 complex_lattice_t lattice = find_lattice_value (ssa_name);
442 size_t ssa_name_index;
445 if (lattice == (imag_p ? ONLY_REAL : ONLY_IMAG))
447 tree inner_type = TREE_TYPE (TREE_TYPE (ssa_name));
448 if (SCALAR_FLOAT_TYPE_P (inner_type))
449 return build_real (inner_type, dconst0);
451 return build_int_cst (inner_type, 0);
454 ssa_name_index = SSA_NAME_VERSION (ssa_name) * 2 + imag_p;
455 ret = VEC_index (tree, complex_ssa_name_components, ssa_name_index);
458 ret = get_component_var (SSA_NAME_VAR (ssa_name), imag_p);
459 ret = make_ssa_name (ret, NULL);
461 /* Copy some properties from the original. In particular, whether it
462 is used in an abnormal phi, and whether it's uninitialized. */
463 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ret)
464 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name);
465 if (TREE_CODE (SSA_NAME_VAR (ssa_name)) == VAR_DECL
466 && IS_EMPTY_STMT (SSA_NAME_DEF_STMT (ssa_name)))
468 SSA_NAME_DEF_STMT (ret) = SSA_NAME_DEF_STMT (ssa_name);
469 set_default_def (SSA_NAME_VAR (ret), ret);
472 VEC_replace (tree, complex_ssa_name_components, ssa_name_index, ret);
478 /* Set a value for a complex component of SSA_NAME, return a STMT_LIST of
479 stuff that needs doing. */
482 set_component_ssa_name (tree ssa_name, bool imag_p, tree value)
484 complex_lattice_t lattice = find_lattice_value (ssa_name);
485 size_t ssa_name_index;
486 tree comp, list, last;
488 /* We know the value must be zero, else there's a bug in our lattice
489 analysis. But the value may well be a variable known to contain
490 zero. We should be safe ignoring it. */
491 if (lattice == (imag_p ? ONLY_REAL : ONLY_IMAG))
494 /* If we've already assigned an SSA_NAME to this component, then this
495 means that our walk of the basic blocks found a use before the set.
496 This is fine. Now we should create an initialization for the value
497 we created earlier. */
498 ssa_name_index = SSA_NAME_VERSION (ssa_name) * 2 + imag_p;
499 comp = VEC_index (tree, complex_ssa_name_components, ssa_name_index);
503 /* If we've nothing assigned, and the value we're given is already stable,
504 then install that as the value for this SSA_NAME. This preemptively
505 copy-propagates the value, which avoids unnecessary memory allocation. */
506 else if (is_gimple_min_invariant (value))
508 VEC_replace (tree, complex_ssa_name_components, ssa_name_index, value);
511 else if (TREE_CODE (value) == SSA_NAME
512 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name))
514 /* Replace an anonymous base value with the variable from cvc_lookup.
515 This should result in better debug info. */
516 if (DECL_IGNORED_P (SSA_NAME_VAR (value))
517 && !DECL_IGNORED_P (SSA_NAME_VAR (ssa_name)))
519 comp = get_component_var (SSA_NAME_VAR (ssa_name), imag_p);
520 replace_ssa_name_symbol (value, comp);
523 VEC_replace (tree, complex_ssa_name_components, ssa_name_index, value);
527 /* Finally, we need to stabilize the result by installing the value into
530 comp = get_component_ssa_name (ssa_name, imag_p);
532 /* Do all the work to assign VALUE to COMP. */
533 value = force_gimple_operand (value, &list, false, NULL);
534 last = build_gimple_modify_stmt (comp, value);
535 append_to_statement_list (last, &list);
537 gcc_assert (SSA_NAME_DEF_STMT (comp) == NULL);
538 SSA_NAME_DEF_STMT (comp) = last;
543 /* Extract the real or imaginary part of a complex variable or constant.
544 Make sure that it's a proper gimple_val and gimplify it if not.
545 Emit any new code before BSI. */
548 extract_component (block_stmt_iterator *bsi, tree t, bool imagpart_p,
551 switch (TREE_CODE (t))
554 return imagpart_p ? TREE_IMAGPART (t) : TREE_REALPART (t);
557 return TREE_OPERAND (t, imagpart_p);
566 tree inner_type = TREE_TYPE (TREE_TYPE (t));
568 t = build1 ((imagpart_p ? IMAGPART_EXPR : REALPART_EXPR),
569 inner_type, unshare_expr (t));
572 t = gimplify_val (bsi, inner_type, t);
578 return get_component_ssa_name (t, imagpart_p);
585 /* Update the complex components of the ssa name on the lhs of STMT. */
588 update_complex_components (block_stmt_iterator *bsi, tree stmt, tree r, tree i)
590 tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
593 list = set_component_ssa_name (lhs, false, r);
595 bsi_insert_after (bsi, list, BSI_CONTINUE_LINKING);
597 list = set_component_ssa_name (lhs, true, i);
599 bsi_insert_after (bsi, list, BSI_CONTINUE_LINKING);
603 update_complex_components_on_edge (edge e, tree lhs, tree r, tree i)
607 list = set_component_ssa_name (lhs, false, r);
609 bsi_insert_on_edge (e, list);
611 list = set_component_ssa_name (lhs, true, i);
613 bsi_insert_on_edge (e, list);
616 /* Update an assignment to a complex variable in place. */
619 update_complex_assignment (block_stmt_iterator *bsi, tree r, tree i)
624 mod = stmt = bsi_stmt (*bsi);
625 if (TREE_CODE (stmt) == RETURN_EXPR)
626 mod = TREE_OPERAND (mod, 0);
627 else if (gimple_in_ssa_p (cfun))
628 update_complex_components (bsi, stmt, r, i);
630 type = TREE_TYPE (GIMPLE_STMT_OPERAND (mod, 1));
631 GIMPLE_STMT_OPERAND (mod, 1) = build2 (COMPLEX_EXPR, type, r, i);
635 /* Generate code at the entry point of the function to initialize the
636 component variables for a complex parameter. */
639 update_parameter_components (void)
641 edge entry_edge = single_succ_edge (ENTRY_BLOCK_PTR);
644 for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = TREE_CHAIN (parm))
646 tree type = TREE_TYPE (parm);
649 if (TREE_CODE (type) != COMPLEX_TYPE || !is_gimple_reg (parm))
652 type = TREE_TYPE (type);
653 ssa_name = gimple_default_def (cfun, parm);
657 r = build1 (REALPART_EXPR, type, ssa_name);
658 i = build1 (IMAGPART_EXPR, type, ssa_name);
659 update_complex_components_on_edge (entry_edge, ssa_name, r, i);
663 /* Generate code to set the component variables of a complex variable
664 to match the PHI statements in block BB. */
667 update_phi_components (basic_block bb)
671 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
672 if (is_complex_reg (PHI_RESULT (phi)))
674 tree lr, li, pr = NULL, pi = NULL;
677 lr = get_component_ssa_name (PHI_RESULT (phi), false);
678 if (TREE_CODE (lr) == SSA_NAME)
680 pr = create_phi_node (lr, bb);
681 SSA_NAME_DEF_STMT (lr) = pr;
684 li = get_component_ssa_name (PHI_RESULT (phi), true);
685 if (TREE_CODE (li) == SSA_NAME)
687 pi = create_phi_node (li, bb);
688 SSA_NAME_DEF_STMT (li) = pi;
691 for (i = 0, n = PHI_NUM_ARGS (phi); i < n; ++i)
693 tree comp, arg = PHI_ARG_DEF (phi, i);
696 comp = extract_component (NULL, arg, false, false);
697 SET_PHI_ARG_DEF (pr, i, comp);
701 comp = extract_component (NULL, arg, true, false);
702 SET_PHI_ARG_DEF (pi, i, comp);
708 /* Mark each virtual op in STMT for ssa update. */
711 update_all_vops (tree stmt)
716 FOR_EACH_SSA_TREE_OPERAND (sym, stmt, iter, SSA_OP_ALL_VIRTUALS)
718 if (TREE_CODE (sym) == SSA_NAME)
719 sym = SSA_NAME_VAR (sym);
720 mark_sym_for_renaming (sym);
724 /* Expand a complex move to scalars. */
727 expand_complex_move (block_stmt_iterator *bsi, tree stmt, tree type,
730 tree inner_type = TREE_TYPE (type);
733 if (TREE_CODE (lhs) == SSA_NAME)
735 if (is_ctrl_altering_stmt (bsi_stmt (*bsi)))
740 /* The value is not assigned on the exception edges, so we need not
741 concern ourselves there. We do need to update on the fallthru
743 FOR_EACH_EDGE (e, ei, bsi->bb->succs)
744 if (e->flags & EDGE_FALLTHRU)
749 r = build1 (REALPART_EXPR, inner_type, lhs);
750 i = build1 (IMAGPART_EXPR, inner_type, lhs);
751 update_complex_components_on_edge (e, lhs, r, i);
753 else if (TREE_CODE (rhs) == CALL_EXPR || TREE_SIDE_EFFECTS (rhs))
755 r = build1 (REALPART_EXPR, inner_type, lhs);
756 i = build1 (IMAGPART_EXPR, inner_type, lhs);
757 update_complex_components (bsi, stmt, r, i);
761 update_all_vops (bsi_stmt (*bsi));
762 r = extract_component (bsi, rhs, 0, true);
763 i = extract_component (bsi, rhs, 1, true);
764 update_complex_assignment (bsi, r, i);
767 else if (TREE_CODE (rhs) == SSA_NAME && !TREE_SIDE_EFFECTS (lhs))
771 r = extract_component (bsi, rhs, 0, false);
772 i = extract_component (bsi, rhs, 1, false);
774 x = build1 (REALPART_EXPR, inner_type, unshare_expr (lhs));
775 x = build_gimple_modify_stmt (x, r);
776 bsi_insert_before (bsi, x, BSI_SAME_STMT);
778 if (stmt == bsi_stmt (*bsi))
780 x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
781 GIMPLE_STMT_OPERAND (stmt, 0) = x;
782 GIMPLE_STMT_OPERAND (stmt, 1) = i;
786 x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
787 x = build_gimple_modify_stmt (x, i);
788 bsi_insert_before (bsi, x, BSI_SAME_STMT);
790 stmt = bsi_stmt (*bsi);
791 gcc_assert (TREE_CODE (stmt) == RETURN_EXPR);
792 GIMPLE_STMT_OPERAND (stmt, 0) = lhs;
795 update_all_vops (stmt);
800 /* Expand complex addition to scalars:
801 a + b = (ar + br) + i(ai + bi)
802 a - b = (ar - br) + i(ai + bi)
806 expand_complex_addition (block_stmt_iterator *bsi, tree inner_type,
807 tree ar, tree ai, tree br, tree bi,
809 complex_lattice_t al, complex_lattice_t bl)
813 switch (PAIR (al, bl))
815 case PAIR (ONLY_REAL, ONLY_REAL):
816 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
820 case PAIR (ONLY_REAL, ONLY_IMAG):
822 if (code == MINUS_EXPR)
823 ri = gimplify_build2 (bsi, MINUS_EXPR, inner_type, ai, bi);
828 case PAIR (ONLY_IMAG, ONLY_REAL):
829 if (code == MINUS_EXPR)
830 rr = gimplify_build2 (bsi, MINUS_EXPR, inner_type, ar, br);
836 case PAIR (ONLY_IMAG, ONLY_IMAG):
838 ri = gimplify_build2 (bsi, code, inner_type, ai, bi);
841 case PAIR (VARYING, ONLY_REAL):
842 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
846 case PAIR (VARYING, ONLY_IMAG):
848 ri = gimplify_build2 (bsi, code, inner_type, ai, bi);
851 case PAIR (ONLY_REAL, VARYING):
852 if (code == MINUS_EXPR)
854 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
858 case PAIR (ONLY_IMAG, VARYING):
859 if (code == MINUS_EXPR)
862 ri = gimplify_build2 (bsi, code, inner_type, ai, bi);
865 case PAIR (VARYING, VARYING):
867 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
868 ri = gimplify_build2 (bsi, code, inner_type, ai, bi);
875 update_complex_assignment (bsi, rr, ri);
878 /* Expand a complex multiplication or division to a libcall to the c99
879 compliant routines. */
882 expand_complex_libcall (block_stmt_iterator *bsi, tree ar, tree ai,
883 tree br, tree bi, enum tree_code code)
885 enum machine_mode mode;
886 enum built_in_function bcode;
889 stmt = bsi_stmt (*bsi);
890 type = TREE_TYPE (GIMPLE_STMT_OPERAND (stmt, 1));
892 mode = TYPE_MODE (type);
893 gcc_assert (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT);
894 if (code == MULT_EXPR)
895 bcode = BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT;
896 else if (code == RDIV_EXPR)
897 bcode = BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT;
900 fn = built_in_decls[bcode];
902 GIMPLE_STMT_OPERAND (stmt, 1) = build_call_expr (fn, 4, ar, ai, br, bi);
905 if (gimple_in_ssa_p (cfun))
907 tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
908 type = TREE_TYPE (type);
909 update_complex_components (bsi, stmt,
910 build1 (REALPART_EXPR, type, lhs),
911 build1 (IMAGPART_EXPR, type, lhs));
915 /* Expand complex multiplication to scalars:
916 a * b = (ar*br - ai*bi) + i(ar*bi + br*ai)
920 expand_complex_multiplication (block_stmt_iterator *bsi, tree inner_type,
921 tree ar, tree ai, tree br, tree bi,
922 complex_lattice_t al, complex_lattice_t bl)
928 complex_lattice_t tl;
929 rr = ar, ar = br, br = rr;
930 ri = ai, ai = bi, bi = ri;
931 tl = al, al = bl, bl = tl;
934 switch (PAIR (al, bl))
936 case PAIR (ONLY_REAL, ONLY_REAL):
937 rr = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, br);
941 case PAIR (ONLY_IMAG, ONLY_REAL):
943 if (TREE_CODE (ai) == REAL_CST
944 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (ai), dconst1))
947 ri = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, br);
950 case PAIR (ONLY_IMAG, ONLY_IMAG):
951 rr = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, bi);
952 rr = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, rr);
956 case PAIR (VARYING, ONLY_REAL):
957 rr = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, br);
958 ri = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, br);
961 case PAIR (VARYING, ONLY_IMAG):
962 rr = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, bi);
963 rr = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, rr);
964 ri = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, bi);
967 case PAIR (VARYING, VARYING):
968 if (flag_complex_method == 2 && SCALAR_FLOAT_TYPE_P (inner_type))
970 expand_complex_libcall (bsi, ar, ai, br, bi, MULT_EXPR);
977 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, br);
978 t2 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, bi);
979 t3 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, bi);
981 /* Avoid expanding redundant multiplication for the common
982 case of squaring a complex number. */
983 if (ar == br && ai == bi)
986 t4 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, br);
988 rr = gimplify_build2 (bsi, MINUS_EXPR, inner_type, t1, t2);
989 ri = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t3, t4);
997 update_complex_assignment (bsi, rr, ri);
1000 /* Expand complex division to scalars, straightforward algorithm.
1001 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1006 expand_complex_div_straight (block_stmt_iterator *bsi, tree inner_type,
1007 tree ar, tree ai, tree br, tree bi,
1008 enum tree_code code)
1010 tree rr, ri, div, t1, t2, t3;
1012 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, br, br);
1013 t2 = gimplify_build2 (bsi, MULT_EXPR, inner_type, bi, bi);
1014 div = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, t2);
1016 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, br);
1017 t2 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, bi);
1018 t3 = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, t2);
1019 rr = gimplify_build2 (bsi, code, inner_type, t3, div);
1021 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, br);
1022 t2 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, bi);
1023 t3 = gimplify_build2 (bsi, MINUS_EXPR, inner_type, t1, t2);
1024 ri = gimplify_build2 (bsi, code, inner_type, t3, div);
1026 update_complex_assignment (bsi, rr, ri);
1029 /* Expand complex division to scalars, modified algorithm to minimize
1030 overflow with wide input ranges. */
1033 expand_complex_div_wide (block_stmt_iterator *bsi, tree inner_type,
1034 tree ar, tree ai, tree br, tree bi,
1035 enum tree_code code)
1037 tree rr, ri, ratio, div, t1, t2, tr, ti, cond;
1038 basic_block bb_cond, bb_true, bb_false, bb_join;
1040 /* Examine |br| < |bi|, and branch. */
1041 t1 = gimplify_build1 (bsi, ABS_EXPR, inner_type, br);
1042 t2 = gimplify_build1 (bsi, ABS_EXPR, inner_type, bi);
1043 cond = fold_build2 (LT_EXPR, boolean_type_node, t1, t2);
1046 bb_cond = bb_true = bb_false = bb_join = NULL;
1047 rr = ri = tr = ti = NULL;
1048 if (!TREE_CONSTANT (cond))
1052 cond = build3 (COND_EXPR, void_type_node, cond, NULL_TREE, NULL_TREE);
1053 bsi_insert_before (bsi, cond, BSI_SAME_STMT);
1055 /* Split the original block, and create the TRUE and FALSE blocks. */
1056 e = split_block (bsi->bb, cond);
1059 bb_true = create_empty_bb (bb_cond);
1060 bb_false = create_empty_bb (bb_true);
1062 /* Wire the blocks together. */
1063 e->flags = EDGE_TRUE_VALUE;
1064 redirect_edge_succ (e, bb_true);
1065 make_edge (bb_cond, bb_false, EDGE_FALSE_VALUE);
1066 make_edge (bb_true, bb_join, EDGE_FALLTHRU);
1067 make_edge (bb_false, bb_join, EDGE_FALLTHRU);
1069 /* Update dominance info. Note that bb_join's data was
1070 updated by split_block. */
1071 if (dom_info_available_p (CDI_DOMINATORS))
1073 set_immediate_dominator (CDI_DOMINATORS, bb_true, bb_cond);
1074 set_immediate_dominator (CDI_DOMINATORS, bb_false, bb_cond);
1077 rr = make_rename_temp (inner_type, NULL);
1078 ri = make_rename_temp (inner_type, NULL);
1081 /* In the TRUE branch, we compute
1083 div = (br * ratio) + bi;
1084 tr = (ar * ratio) + ai;
1085 ti = (ai * ratio) - ar;
1088 if (bb_true || integer_nonzerop (cond))
1092 *bsi = bsi_last (bb_true);
1093 bsi_insert_after (bsi, build_empty_stmt (), BSI_NEW_STMT);
1096 ratio = gimplify_build2 (bsi, code, inner_type, br, bi);
1098 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, br, ratio);
1099 div = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, bi);
1101 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, ratio);
1102 tr = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, ai);
1104 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, ratio);
1105 ti = gimplify_build2 (bsi, MINUS_EXPR, inner_type, t1, ar);
1107 tr = gimplify_build2 (bsi, code, inner_type, tr, div);
1108 ti = gimplify_build2 (bsi, code, inner_type, ti, div);
1112 t1 = build_gimple_modify_stmt (rr, tr);
1113 bsi_insert_before (bsi, t1, BSI_SAME_STMT);
1114 t1 = build_gimple_modify_stmt (ri, ti);
1115 bsi_insert_before (bsi, t1, BSI_SAME_STMT);
1116 bsi_remove (bsi, true);
1120 /* In the FALSE branch, we compute
1122 divisor = (d * ratio) + c;
1123 tr = (b * ratio) + a;
1124 ti = b - (a * ratio);
1127 if (bb_false || integer_zerop (cond))
1131 *bsi = bsi_last (bb_false);
1132 bsi_insert_after (bsi, build_empty_stmt (), BSI_NEW_STMT);
1135 ratio = gimplify_build2 (bsi, code, inner_type, bi, br);
1137 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, bi, ratio);
1138 div = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, br);
1140 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, ratio);
1141 tr = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, ar);
1143 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, ratio);
1144 ti = gimplify_build2 (bsi, MINUS_EXPR, inner_type, ai, t1);
1146 tr = gimplify_build2 (bsi, code, inner_type, tr, div);
1147 ti = gimplify_build2 (bsi, code, inner_type, ti, div);
1151 t1 = build_gimple_modify_stmt (rr, tr);
1152 bsi_insert_before (bsi, t1, BSI_SAME_STMT);
1153 t1 = build_gimple_modify_stmt (ri, ti);
1154 bsi_insert_before (bsi, t1, BSI_SAME_STMT);
1155 bsi_remove (bsi, true);
1160 *bsi = bsi_start (bb_join);
1164 update_complex_assignment (bsi, rr, ri);
1167 /* Expand complex division to scalars. */
1170 expand_complex_division (block_stmt_iterator *bsi, tree inner_type,
1171 tree ar, tree ai, tree br, tree bi,
1172 enum tree_code code,
1173 complex_lattice_t al, complex_lattice_t bl)
1177 switch (PAIR (al, bl))
1179 case PAIR (ONLY_REAL, ONLY_REAL):
1180 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
1184 case PAIR (ONLY_REAL, ONLY_IMAG):
1186 ri = gimplify_build2 (bsi, code, inner_type, ar, bi);
1187 ri = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, ri);
1190 case PAIR (ONLY_IMAG, ONLY_REAL):
1192 ri = gimplify_build2 (bsi, code, inner_type, ai, br);
1195 case PAIR (ONLY_IMAG, ONLY_IMAG):
1196 rr = gimplify_build2 (bsi, code, inner_type, ai, bi);
1200 case PAIR (VARYING, ONLY_REAL):
1201 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
1202 ri = gimplify_build2 (bsi, code, inner_type, ai, br);
1205 case PAIR (VARYING, ONLY_IMAG):
1206 rr = gimplify_build2 (bsi, code, inner_type, ai, bi);
1207 ri = gimplify_build2 (bsi, code, inner_type, ar, bi);
1208 ri = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, ri);
1210 case PAIR (ONLY_REAL, VARYING):
1211 case PAIR (ONLY_IMAG, VARYING):
1212 case PAIR (VARYING, VARYING):
1213 switch (flag_complex_method)
1216 /* straightforward implementation of complex divide acceptable. */
1217 expand_complex_div_straight (bsi, inner_type, ar, ai, br, bi, code);
1221 if (SCALAR_FLOAT_TYPE_P (inner_type))
1223 expand_complex_libcall (bsi, ar, ai, br, bi, code);
1229 /* wide ranges of inputs must work for complex divide. */
1230 expand_complex_div_wide (bsi, inner_type, ar, ai, br, bi, code);
1242 update_complex_assignment (bsi, rr, ri);
1245 /* Expand complex negation to scalars:
1250 expand_complex_negation (block_stmt_iterator *bsi, tree inner_type,
1255 rr = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, ar);
1256 ri = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, ai);
1258 update_complex_assignment (bsi, rr, ri);
1261 /* Expand complex conjugate to scalars:
1266 expand_complex_conjugate (block_stmt_iterator *bsi, tree inner_type,
1271 ri = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, ai);
1273 update_complex_assignment (bsi, ar, ri);
1276 /* Expand complex comparison (EQ or NE only). */
1279 expand_complex_comparison (block_stmt_iterator *bsi, tree ar, tree ai,
1280 tree br, tree bi, enum tree_code code)
1282 tree cr, ci, cc, stmt, expr, type;
1284 cr = gimplify_build2 (bsi, code, boolean_type_node, ar, br);
1285 ci = gimplify_build2 (bsi, code, boolean_type_node, ai, bi);
1286 cc = gimplify_build2 (bsi,
1287 (code == EQ_EXPR ? TRUTH_AND_EXPR : TRUTH_OR_EXPR),
1288 boolean_type_node, cr, ci);
1290 stmt = expr = bsi_stmt (*bsi);
1292 switch (TREE_CODE (stmt))
1295 expr = TREE_OPERAND (stmt, 0);
1297 case GIMPLE_MODIFY_STMT:
1298 type = TREE_TYPE (GIMPLE_STMT_OPERAND (expr, 1));
1299 GIMPLE_STMT_OPERAND (expr, 1) = fold_convert (type, cc);
1302 TREE_OPERAND (stmt, 0) = cc;
1311 /* Process one statement. If we identify a complex operation, expand it. */
1314 expand_complex_operations_1 (block_stmt_iterator *bsi)
1316 tree stmt = bsi_stmt (*bsi);
1317 tree rhs, type, inner_type;
1318 tree ac, ar, ai, bc, br, bi;
1319 complex_lattice_t al, bl;
1320 enum tree_code code;
1322 switch (TREE_CODE (stmt))
1325 stmt = TREE_OPERAND (stmt, 0);
1328 if (TREE_CODE (stmt) != GIMPLE_MODIFY_STMT)
1332 case GIMPLE_MODIFY_STMT:
1333 rhs = GIMPLE_STMT_OPERAND (stmt, 1);
1337 rhs = TREE_OPERAND (stmt, 0);
1344 type = TREE_TYPE (rhs);
1345 code = TREE_CODE (rhs);
1347 /* Initial filter for operations we handle. */
1353 case TRUNC_DIV_EXPR:
1355 case FLOOR_DIV_EXPR:
1356 case ROUND_DIV_EXPR:
1360 if (TREE_CODE (type) != COMPLEX_TYPE)
1362 inner_type = TREE_TYPE (type);
1367 inner_type = TREE_TYPE (TREE_OPERAND (rhs, 1));
1368 if (TREE_CODE (inner_type) != COMPLEX_TYPE)
1376 /* COND_EXPR may also fallthru here, but we do not need to do anything
1378 if (TREE_CODE (stmt) != GIMPLE_MODIFY_STMT)
1381 lhs = GIMPLE_STMT_OPERAND (stmt, 0);
1382 rhs = GIMPLE_STMT_OPERAND (stmt, 1);
1384 if (TREE_CODE (type) == COMPLEX_TYPE)
1385 expand_complex_move (bsi, stmt, type, lhs, rhs);
1386 else if ((TREE_CODE (rhs) == REALPART_EXPR
1387 || TREE_CODE (rhs) == IMAGPART_EXPR)
1388 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
1390 GENERIC_TREE_OPERAND (stmt, 1)
1391 = extract_component (bsi, TREE_OPERAND (rhs, 0),
1392 TREE_CODE (rhs) == IMAGPART_EXPR, false);
1399 /* Extract the components of the two complex values. Make sure and
1400 handle the common case of the same value used twice specially. */
1401 ac = TREE_OPERAND (rhs, 0);
1402 ar = extract_component (bsi, ac, 0, true);
1403 ai = extract_component (bsi, ac, 1, true);
1405 if (TREE_CODE_CLASS (code) == tcc_unary)
1406 bc = br = bi = NULL;
1409 bc = TREE_OPERAND (rhs, 1);
1414 br = extract_component (bsi, bc, 0, true);
1415 bi = extract_component (bsi, bc, 1, true);
1419 if (gimple_in_ssa_p (cfun))
1421 al = find_lattice_value (ac);
1422 if (al == UNINITIALIZED)
1425 if (TREE_CODE_CLASS (code) == tcc_unary)
1431 bl = find_lattice_value (bc);
1432 if (bl == UNINITIALIZED)
1443 expand_complex_addition (bsi, inner_type, ar, ai, br, bi, code, al, bl);
1447 expand_complex_multiplication (bsi, inner_type, ar, ai, br, bi, al, bl);
1450 case TRUNC_DIV_EXPR:
1452 case FLOOR_DIV_EXPR:
1453 case ROUND_DIV_EXPR:
1455 expand_complex_division (bsi, inner_type, ar, ai, br, bi, code, al, bl);
1459 expand_complex_negation (bsi, inner_type, ar, ai);
1463 expand_complex_conjugate (bsi, inner_type, ar, ai);
1468 expand_complex_comparison (bsi, ar, ai, br, bi, code);
1477 /* Entry point for complex operation lowering during optimization. */
1480 tree_lower_complex (void)
1482 int old_last_basic_block;
1483 block_stmt_iterator bsi;
1486 if (!init_dont_simulate_again ())
1489 complex_lattice_values = VEC_alloc (complex_lattice_t, heap, num_ssa_names);
1490 VEC_safe_grow_cleared (complex_lattice_t, heap,
1491 complex_lattice_values, num_ssa_names);
1493 init_parameter_lattice_values ();
1494 ssa_propagate (complex_visit_stmt, complex_visit_phi);
1496 complex_variable_components = htab_create (10, int_tree_map_hash,
1497 int_tree_map_eq, free);
1499 complex_ssa_name_components = VEC_alloc (tree, heap, 2*num_ssa_names);
1500 VEC_safe_grow_cleared (tree, heap, complex_ssa_name_components,
1503 update_parameter_components ();
1505 /* ??? Ideally we'd traverse the blocks in breadth-first order. */
1506 old_last_basic_block = last_basic_block;
1509 if (bb->index >= old_last_basic_block)
1511 update_phi_components (bb);
1512 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1513 expand_complex_operations_1 (&bsi);
1516 bsi_commit_edge_inserts ();
1518 htab_delete (complex_variable_components);
1519 VEC_free (tree, heap, complex_ssa_name_components);
1520 VEC_free (complex_lattice_t, heap, complex_lattice_values);
1524 struct tree_opt_pass pass_lower_complex =
1526 "cplxlower", /* name */
1528 tree_lower_complex, /* execute */
1531 0, /* static_pass_number */
1533 PROP_ssa, /* properties_required */
1534 0, /* properties_provided */
1535 0, /* properties_destroyed */
1536 0, /* todo_flags_start */
1540 | TODO_verify_stmts, /* todo_flags_finish */
1545 /* Entry point for complex operation lowering without optimization. */
1548 tree_lower_complex_O0 (void)
1550 int old_last_basic_block = last_basic_block;
1551 block_stmt_iterator bsi;
1556 if (bb->index >= old_last_basic_block)
1558 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1559 expand_complex_operations_1 (&bsi);
1565 gate_no_optimization (void)
1567 /* With errors, normal optimization passes are not run. If we don't
1568 lower complex operations at all, rtl expansion will abort. */
1569 return optimize == 0 || sorrycount || errorcount;
1572 struct tree_opt_pass pass_lower_complex_O0 =
1574 "cplxlower0", /* name */
1575 gate_no_optimization, /* gate */
1576 tree_lower_complex_O0, /* execute */
1579 0, /* static_pass_number */
1581 PROP_cfg, /* properties_required */
1582 0, /* properties_provided */
1583 0, /* properties_destroyed */
1584 0, /* todo_flags_start */
1585 TODO_dump_func | TODO_ggc_collect
1586 | TODO_verify_stmts, /* todo_flags_finish */