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 /* 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 = xmalloc (sizeof (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 = default_def (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) != MODIFY_EXPR)
213 dsa = !is_complex_reg (TREE_OPERAND (stmt, 0));
214 rhs = TREE_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) != MODIFY_EXPR)
270 return SSA_PROP_VARYING;
272 lhs = TREE_OPERAND (stmt, 0);
273 rhs = TREE_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_tmp_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 pre-emptively
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 = build2 (MODIFY_EXPR, TREE_TYPE (comp), 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);
565 tree inner_type = TREE_TYPE (TREE_TYPE (t));
567 t = build1 ((imagpart_p ? IMAGPART_EXPR : REALPART_EXPR),
568 inner_type, unshare_expr (t));
571 t = gimplify_val (bsi, inner_type, t);
577 return get_component_ssa_name (t, imagpart_p);
584 /* Update the complex components of the ssa name on the lhs of STMT. */
587 update_complex_components (block_stmt_iterator *bsi, tree stmt, tree r, tree i)
589 tree lhs = TREE_OPERAND (stmt, 0);
592 list = set_component_ssa_name (lhs, false, r);
594 bsi_insert_after (bsi, list, BSI_CONTINUE_LINKING);
596 list = set_component_ssa_name (lhs, true, i);
598 bsi_insert_after (bsi, list, BSI_CONTINUE_LINKING);
602 update_complex_components_on_edge (edge e, tree lhs, tree r, tree i)
606 list = set_component_ssa_name (lhs, false, r);
608 bsi_insert_on_edge (e, list);
610 list = set_component_ssa_name (lhs, true, i);
612 bsi_insert_on_edge (e, list);
615 /* Update an assignment to a complex variable in place. */
618 update_complex_assignment (block_stmt_iterator *bsi, tree r, tree i)
623 mod = stmt = bsi_stmt (*bsi);
624 if (TREE_CODE (stmt) == RETURN_EXPR)
625 mod = TREE_OPERAND (mod, 0);
627 update_complex_components (bsi, stmt, r, i);
629 type = TREE_TYPE (TREE_OPERAND (mod, 1));
630 TREE_OPERAND (mod, 1) = build (COMPLEX_EXPR, type, r, i);
634 /* Generate code at the entry point of the function to initialize the
635 component variables for a complex parameter. */
638 update_parameter_components (void)
640 edge entry_edge = single_succ_edge (ENTRY_BLOCK_PTR);
643 for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = TREE_CHAIN (parm))
645 tree type = TREE_TYPE (parm);
648 if (TREE_CODE (type) != COMPLEX_TYPE || !is_gimple_reg (parm))
651 type = TREE_TYPE (type);
652 ssa_name = default_def (parm);
654 r = build1 (REALPART_EXPR, type, ssa_name);
655 i = build1 (IMAGPART_EXPR, type, ssa_name);
656 update_complex_components_on_edge (entry_edge, ssa_name, r, i);
660 /* Generate code to set the component variables of a complex variable
661 to match the PHI statements in block BB. */
664 update_phi_components (basic_block bb)
668 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
669 if (is_complex_reg (PHI_RESULT (phi)))
671 tree lr, li, pr = NULL, pi = NULL;
674 lr = get_component_ssa_name (PHI_RESULT (phi), false);
675 if (TREE_CODE (lr) == SSA_NAME)
677 pr = create_phi_node (lr, bb);
678 SSA_NAME_DEF_STMT (lr) = pr;
681 li = get_component_ssa_name (PHI_RESULT (phi), true);
682 if (TREE_CODE (li) == SSA_NAME)
684 pi = create_phi_node (li, bb);
685 SSA_NAME_DEF_STMT (li) = pi;
688 for (i = 0, n = PHI_NUM_ARGS (phi); i < n; ++i)
690 tree comp, arg = PHI_ARG_DEF (phi, i);
693 comp = extract_component (NULL, arg, false, false);
694 SET_PHI_ARG_DEF (pr, i, comp);
698 comp = extract_component (NULL, arg, true, false);
699 SET_PHI_ARG_DEF (pi, i, comp);
705 /* Mark each virtual op in STMT for ssa update. */
708 update_all_vops (tree stmt)
713 FOR_EACH_SSA_TREE_OPERAND (sym, stmt, iter, SSA_OP_ALL_VIRTUALS)
715 if (TREE_CODE (sym) == SSA_NAME)
716 sym = SSA_NAME_VAR (sym);
717 mark_sym_for_renaming (sym);
721 /* Expand a complex move to scalars. */
724 expand_complex_move (block_stmt_iterator *bsi, tree stmt, tree type,
727 tree inner_type = TREE_TYPE (type);
730 if (TREE_CODE (lhs) == SSA_NAME)
732 if (is_ctrl_altering_stmt (bsi_stmt (*bsi)))
737 /* The value is not assigned on the exception edges, so we need not
738 concern ourselves there. We do need to update on the fallthru
740 FOR_EACH_EDGE (e, ei, bsi->bb->succs)
741 if (e->flags & EDGE_FALLTHRU)
746 r = build1 (REALPART_EXPR, inner_type, lhs);
747 i = build1 (IMAGPART_EXPR, inner_type, lhs);
748 update_complex_components_on_edge (e, lhs, r, i);
750 else if (TREE_CODE (rhs) == CALL_EXPR || TREE_SIDE_EFFECTS (rhs))
752 r = build1 (REALPART_EXPR, inner_type, lhs);
753 i = build1 (IMAGPART_EXPR, inner_type, lhs);
754 update_complex_components (bsi, stmt, r, i);
758 update_all_vops (bsi_stmt (*bsi));
759 r = extract_component (bsi, rhs, 0, true);
760 i = extract_component (bsi, rhs, 1, true);
761 update_complex_assignment (bsi, r, i);
764 else if (TREE_CODE (rhs) == SSA_NAME && !TREE_SIDE_EFFECTS (lhs))
768 r = extract_component (bsi, rhs, 0, false);
769 i = extract_component (bsi, rhs, 1, false);
771 x = build1 (REALPART_EXPR, inner_type, unshare_expr (lhs));
772 x = build2 (MODIFY_EXPR, inner_type, x, r);
773 bsi_insert_before (bsi, x, BSI_SAME_STMT);
775 if (stmt == bsi_stmt (*bsi))
777 x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
778 TREE_OPERAND (stmt, 0) = x;
779 TREE_OPERAND (stmt, 1) = i;
780 TREE_TYPE (stmt) = inner_type;
784 x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
785 x = build2 (MODIFY_EXPR, inner_type, x, i);
786 bsi_insert_before (bsi, x, BSI_SAME_STMT);
788 stmt = bsi_stmt (*bsi);
789 gcc_assert (TREE_CODE (stmt) == RETURN_EXPR);
790 TREE_OPERAND (stmt, 0) = lhs;
793 update_all_vops (stmt);
798 /* Expand complex addition to scalars:
799 a + b = (ar + br) + i(ai + bi)
800 a - b = (ar - br) + i(ai + bi)
804 expand_complex_addition (block_stmt_iterator *bsi, tree inner_type,
805 tree ar, tree ai, tree br, tree bi,
807 complex_lattice_t al, complex_lattice_t bl)
811 switch (PAIR (al, bl))
813 case PAIR (ONLY_REAL, ONLY_REAL):
814 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
818 case PAIR (ONLY_REAL, ONLY_IMAG):
820 if (code == MINUS_EXPR)
821 ri = gimplify_build2 (bsi, MINUS_EXPR, inner_type, ai, bi);
826 case PAIR (ONLY_IMAG, ONLY_REAL):
827 if (code == MINUS_EXPR)
828 rr = gimplify_build2 (bsi, MINUS_EXPR, inner_type, ar, br);
834 case PAIR (ONLY_IMAG, ONLY_IMAG):
836 ri = gimplify_build2 (bsi, code, inner_type, ai, bi);
839 case PAIR (VARYING, ONLY_REAL):
840 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
844 case PAIR (VARYING, ONLY_IMAG):
846 ri = gimplify_build2 (bsi, MINUS_EXPR, inner_type, ai, bi);
849 case PAIR (ONLY_REAL, VARYING):
850 if (code == MINUS_EXPR)
852 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
856 case PAIR (ONLY_IMAG, VARYING):
857 if (code == MINUS_EXPR)
860 ri = gimplify_build2 (bsi, MINUS_EXPR, inner_type, ai, bi);
863 case PAIR (VARYING, VARYING):
865 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
866 ri = gimplify_build2 (bsi, code, inner_type, ai, bi);
873 update_complex_assignment (bsi, rr, ri);
876 /* Expand a complex multiplication or division to a libcall to the c99
877 compliant routines. */
880 expand_complex_libcall (block_stmt_iterator *bsi, tree ar, tree ai,
881 tree br, tree bi, enum tree_code code)
883 enum machine_mode mode;
884 enum built_in_function bcode;
885 tree args, fn, stmt, type;
887 args = tree_cons (NULL, bi, NULL);
888 args = tree_cons (NULL, br, args);
889 args = tree_cons (NULL, ai, args);
890 args = tree_cons (NULL, ar, args);
892 stmt = bsi_stmt (*bsi);
893 type = TREE_TYPE (TREE_OPERAND (stmt, 1));
895 mode = TYPE_MODE (type);
896 gcc_assert (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT);
897 if (code == MULT_EXPR)
898 bcode = BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT;
899 else if (code == RDIV_EXPR)
900 bcode = BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT;
903 fn = built_in_decls[bcode];
905 TREE_OPERAND (stmt, 1)
906 = build3 (CALL_EXPR, type, build_fold_addr_expr (fn), args, NULL);
911 tree lhs = TREE_OPERAND (stmt, 0);
912 type = TREE_TYPE (type);
913 update_complex_components (bsi, stmt,
914 build1 (REALPART_EXPR, type, lhs),
915 build1 (IMAGPART_EXPR, type, lhs));
919 /* Expand complex multiplication to scalars:
920 a * b = (ar*br - ai*bi) + i(ar*bi + br*ai)
924 expand_complex_multiplication (block_stmt_iterator *bsi, tree inner_type,
925 tree ar, tree ai, tree br, tree bi,
926 complex_lattice_t al, complex_lattice_t bl)
932 complex_lattice_t tl;
933 rr = ar, ar = br, br = rr;
934 ri = ai, ai = bi, bi = ri;
935 tl = al, al = bl, bl = tl;
938 switch (PAIR (al, bl))
940 case PAIR (ONLY_REAL, ONLY_REAL):
941 rr = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, br);
945 case PAIR (ONLY_IMAG, ONLY_REAL):
947 if (TREE_CODE (ai) == REAL_CST
948 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (ai), dconst1))
951 ri = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, br);
954 case PAIR (ONLY_IMAG, ONLY_IMAG):
955 rr = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, bi);
956 rr = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, rr);
960 case PAIR (VARYING, ONLY_REAL):
961 rr = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, br);
962 ri = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, br);
965 case PAIR (VARYING, ONLY_IMAG):
966 rr = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, bi);
967 rr = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, rr);
968 ri = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, bi);
971 case PAIR (VARYING, VARYING):
972 if (flag_complex_method == 2 && SCALAR_FLOAT_TYPE_P (inner_type))
974 expand_complex_libcall (bsi, ar, ai, br, bi, MULT_EXPR);
981 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, br);
982 t2 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, bi);
983 t3 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, bi);
985 /* Avoid expanding redundant multiplication for the common
986 case of squaring a complex number. */
987 if (ar == br && ai == bi)
990 t4 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, br);
992 rr = gimplify_build2 (bsi, MINUS_EXPR, inner_type, t1, t2);
993 ri = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t3, t4);
1001 update_complex_assignment (bsi, rr, ri);
1004 /* Expand complex division to scalars, straightforward algorithm.
1005 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1010 expand_complex_div_straight (block_stmt_iterator *bsi, tree inner_type,
1011 tree ar, tree ai, tree br, tree bi,
1012 enum tree_code code)
1014 tree rr, ri, div, t1, t2, t3;
1016 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, br, br);
1017 t2 = gimplify_build2 (bsi, MULT_EXPR, inner_type, bi, bi);
1018 div = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, t2);
1020 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, br);
1021 t2 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, bi);
1022 t3 = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, t2);
1023 rr = gimplify_build2 (bsi, code, inner_type, t3, div);
1025 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, br);
1026 t2 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, bi);
1027 t3 = gimplify_build2 (bsi, MINUS_EXPR, inner_type, t1, t2);
1028 ri = gimplify_build2 (bsi, code, inner_type, t3, div);
1030 update_complex_assignment (bsi, rr, ri);
1033 /* Expand complex division to scalars, modified algorithm to minimize
1034 overflow with wide input ranges. */
1037 expand_complex_div_wide (block_stmt_iterator *bsi, tree inner_type,
1038 tree ar, tree ai, tree br, tree bi,
1039 enum tree_code code)
1041 tree rr, ri, ratio, div, t1, t2, tr, ti, cond;
1042 basic_block bb_cond, bb_true, bb_false, bb_join;
1044 /* Examine |br| < |bi|, and branch. */
1045 t1 = gimplify_build1 (bsi, ABS_EXPR, inner_type, br);
1046 t2 = gimplify_build1 (bsi, ABS_EXPR, inner_type, bi);
1047 cond = fold_build2 (LT_EXPR, boolean_type_node, t1, t2);
1050 bb_cond = bb_true = bb_false = bb_join = NULL;
1051 rr = ri = tr = ti = NULL;
1052 if (!TREE_CONSTANT (cond))
1056 cond = build (COND_EXPR, void_type_node, cond, NULL, NULL);
1057 bsi_insert_before (bsi, cond, BSI_SAME_STMT);
1059 /* Split the original block, and create the TRUE and FALSE blocks. */
1060 e = split_block (bsi->bb, cond);
1063 bb_true = create_empty_bb (bb_cond);
1064 bb_false = create_empty_bb (bb_true);
1066 t1 = build (GOTO_EXPR, void_type_node, tree_block_label (bb_true));
1067 t2 = build (GOTO_EXPR, void_type_node, tree_block_label (bb_false));
1068 COND_EXPR_THEN (cond) = t1;
1069 COND_EXPR_ELSE (cond) = t2;
1071 /* Wire the blocks together. */
1072 e->flags = EDGE_TRUE_VALUE;
1073 redirect_edge_succ (e, bb_true);
1074 make_edge (bb_cond, bb_false, EDGE_FALSE_VALUE);
1075 make_edge (bb_true, bb_join, EDGE_FALLTHRU);
1076 make_edge (bb_false, bb_join, EDGE_FALLTHRU);
1078 /* Update dominance info. Note that bb_join's data was
1079 updated by split_block. */
1080 if (dom_info_available_p (CDI_DOMINATORS))
1082 set_immediate_dominator (CDI_DOMINATORS, bb_true, bb_cond);
1083 set_immediate_dominator (CDI_DOMINATORS, bb_false, bb_cond);
1086 rr = make_rename_temp (inner_type, NULL);
1087 ri = make_rename_temp (inner_type, NULL);
1090 /* In the TRUE branch, we compute
1092 div = (br * ratio) + bi;
1093 tr = (ar * ratio) + ai;
1094 ti = (ai * ratio) - ar;
1097 if (bb_true || integer_nonzerop (cond))
1101 *bsi = bsi_last (bb_true);
1102 bsi_insert_after (bsi, build_empty_stmt (), BSI_NEW_STMT);
1105 ratio = gimplify_build2 (bsi, code, inner_type, br, bi);
1107 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, br, ratio);
1108 div = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, bi);
1110 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, ratio);
1111 tr = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, ai);
1113 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, ratio);
1114 ti = gimplify_build2 (bsi, MINUS_EXPR, inner_type, t1, ar);
1116 tr = gimplify_build2 (bsi, code, inner_type, tr, div);
1117 ti = gimplify_build2 (bsi, code, inner_type, ti, div);
1121 t1 = build (MODIFY_EXPR, inner_type, rr, tr);
1122 bsi_insert_before (bsi, t1, BSI_SAME_STMT);
1123 t1 = build (MODIFY_EXPR, inner_type, ri, ti);
1124 bsi_insert_before (bsi, t1, BSI_SAME_STMT);
1129 /* In the FALSE branch, we compute
1131 divisor = (d * ratio) + c;
1132 tr = (b * ratio) + a;
1133 ti = b - (a * ratio);
1136 if (bb_false || integer_zerop (cond))
1140 *bsi = bsi_last (bb_false);
1141 bsi_insert_after (bsi, build_empty_stmt (), BSI_NEW_STMT);
1144 ratio = gimplify_build2 (bsi, code, inner_type, bi, br);
1146 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, bi, ratio);
1147 div = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, br);
1149 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, ratio);
1150 tr = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, ar);
1152 t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, ratio);
1153 ti = gimplify_build2 (bsi, MINUS_EXPR, inner_type, ai, t1);
1155 tr = gimplify_build2 (bsi, code, inner_type, tr, div);
1156 ti = gimplify_build2 (bsi, code, inner_type, ti, div);
1160 t1 = build (MODIFY_EXPR, inner_type, rr, tr);
1161 bsi_insert_before (bsi, t1, BSI_SAME_STMT);
1162 t1 = build (MODIFY_EXPR, inner_type, ri, ti);
1163 bsi_insert_before (bsi, t1, BSI_SAME_STMT);
1169 *bsi = bsi_start (bb_join);
1173 update_complex_assignment (bsi, rr, ri);
1176 /* Expand complex division to scalars. */
1179 expand_complex_division (block_stmt_iterator *bsi, tree inner_type,
1180 tree ar, tree ai, tree br, tree bi,
1181 enum tree_code code,
1182 complex_lattice_t al, complex_lattice_t bl)
1186 switch (PAIR (al, bl))
1188 case PAIR (ONLY_REAL, ONLY_REAL):
1189 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
1193 case PAIR (ONLY_REAL, ONLY_IMAG):
1195 ri = gimplify_build2 (bsi, code, inner_type, ar, bi);
1196 ri = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, ri);
1199 case PAIR (ONLY_IMAG, ONLY_REAL):
1201 ri = gimplify_build2 (bsi, code, inner_type, ai, br);
1204 case PAIR (ONLY_IMAG, ONLY_IMAG):
1205 rr = gimplify_build2 (bsi, code, inner_type, ai, bi);
1209 case PAIR (VARYING, ONLY_REAL):
1210 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
1211 ri = gimplify_build2 (bsi, code, inner_type, ai, br);
1214 case PAIR (VARYING, ONLY_IMAG):
1215 rr = gimplify_build2 (bsi, code, inner_type, ai, bi);
1216 ri = gimplify_build2 (bsi, code, inner_type, ar, bi);
1217 ri = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, ri);
1219 case PAIR (ONLY_REAL, VARYING):
1220 case PAIR (ONLY_IMAG, VARYING):
1221 case PAIR (VARYING, VARYING):
1222 switch (flag_complex_method)
1225 /* straightforward implementation of complex divide acceptable. */
1226 expand_complex_div_straight (bsi, inner_type, ar, ai, br, bi, code);
1230 if (SCALAR_FLOAT_TYPE_P (inner_type))
1232 expand_complex_libcall (bsi, ar, ai, br, bi, code);
1238 /* wide ranges of inputs must work for complex divide. */
1239 expand_complex_div_wide (bsi, inner_type, ar, ai, br, bi, code);
1251 update_complex_assignment (bsi, rr, ri);
1254 /* Expand complex negation to scalars:
1259 expand_complex_negation (block_stmt_iterator *bsi, tree inner_type,
1264 rr = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, ar);
1265 ri = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, ai);
1267 update_complex_assignment (bsi, rr, ri);
1270 /* Expand complex conjugate to scalars:
1275 expand_complex_conjugate (block_stmt_iterator *bsi, tree inner_type,
1280 ri = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, ai);
1282 update_complex_assignment (bsi, ar, ri);
1285 /* Expand complex comparison (EQ or NE only). */
1288 expand_complex_comparison (block_stmt_iterator *bsi, tree ar, tree ai,
1289 tree br, tree bi, enum tree_code code)
1291 tree cr, ci, cc, stmt, expr, type;
1293 cr = gimplify_build2 (bsi, code, boolean_type_node, ar, br);
1294 ci = gimplify_build2 (bsi, code, boolean_type_node, ai, bi);
1295 cc = gimplify_build2 (bsi,
1296 (code == EQ_EXPR ? TRUTH_AND_EXPR : TRUTH_OR_EXPR),
1297 boolean_type_node, cr, ci);
1299 stmt = expr = bsi_stmt (*bsi);
1301 switch (TREE_CODE (stmt))
1304 expr = TREE_OPERAND (stmt, 0);
1307 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1308 TREE_OPERAND (expr, 1) = fold_convert (type, cc);
1311 TREE_OPERAND (stmt, 0) = cc;
1320 /* Process one statement. If we identify a complex operation, expand it. */
1323 expand_complex_operations_1 (block_stmt_iterator *bsi)
1325 tree stmt = bsi_stmt (*bsi);
1326 tree rhs, type, inner_type;
1327 tree ac, ar, ai, bc, br, bi;
1328 complex_lattice_t al, bl;
1329 enum tree_code code;
1331 switch (TREE_CODE (stmt))
1334 stmt = TREE_OPERAND (stmt, 0);
1337 if (TREE_CODE (stmt) != MODIFY_EXPR)
1342 rhs = TREE_OPERAND (stmt, 1);
1346 rhs = TREE_OPERAND (stmt, 0);
1353 type = TREE_TYPE (rhs);
1354 code = TREE_CODE (rhs);
1356 /* Initial filter for operations we handle. */
1362 case TRUNC_DIV_EXPR:
1364 case FLOOR_DIV_EXPR:
1365 case ROUND_DIV_EXPR:
1369 if (TREE_CODE (type) != COMPLEX_TYPE)
1371 inner_type = TREE_TYPE (type);
1376 inner_type = TREE_TYPE (TREE_OPERAND (rhs, 1));
1377 if (TREE_CODE (inner_type) != COMPLEX_TYPE)
1383 tree lhs = TREE_OPERAND (stmt, 0);
1384 tree rhs = TREE_OPERAND (stmt, 1);
1386 if (TREE_CODE (type) == COMPLEX_TYPE)
1387 expand_complex_move (bsi, stmt, type, lhs, rhs);
1388 else if ((TREE_CODE (rhs) == REALPART_EXPR
1389 || TREE_CODE (rhs) == IMAGPART_EXPR)
1390 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
1392 TREE_OPERAND (stmt, 1)
1393 = extract_component (bsi, TREE_OPERAND (rhs, 0),
1394 TREE_CODE (rhs) == IMAGPART_EXPR, false);
1401 /* Extract the components of the two complex values. Make sure and
1402 handle the common case of the same value used twice specially. */
1403 ac = TREE_OPERAND (rhs, 0);
1404 ar = extract_component (bsi, ac, 0, true);
1405 ai = extract_component (bsi, ac, 1, true);
1407 if (TREE_CODE_CLASS (code) == tcc_unary)
1408 bc = br = bi = NULL;
1411 bc = TREE_OPERAND (rhs, 1);
1416 br = extract_component (bsi, bc, 0, true);
1417 bi = extract_component (bsi, bc, 1, true);
1423 al = find_lattice_value (ac);
1424 if (al == UNINITIALIZED)
1427 if (TREE_CODE_CLASS (code) == tcc_unary)
1433 bl = find_lattice_value (bc);
1434 if (bl == UNINITIALIZED)
1445 expand_complex_addition (bsi, inner_type, ar, ai, br, bi, code, al, bl);
1449 expand_complex_multiplication (bsi, inner_type, ar, ai, br, bi, al, bl);
1452 case TRUNC_DIV_EXPR:
1454 case FLOOR_DIV_EXPR:
1455 case ROUND_DIV_EXPR:
1457 expand_complex_division (bsi, inner_type, ar, ai, br, bi, code, al, bl);
1461 expand_complex_negation (bsi, inner_type, ar, ai);
1465 expand_complex_conjugate (bsi, inner_type, ar, ai);
1470 expand_complex_comparison (bsi, ar, ai, br, bi, code);
1479 /* Entry point for complex operation lowering during optimization. */
1482 tree_lower_complex (void)
1484 int old_last_basic_block;
1485 block_stmt_iterator bsi;
1488 if (!init_dont_simulate_again ())
1491 complex_lattice_values = VEC_alloc (complex_lattice_t, heap, num_ssa_names);
1492 VEC_safe_grow (complex_lattice_t, heap,
1493 complex_lattice_values, num_ssa_names);
1494 memset (VEC_address (complex_lattice_t, complex_lattice_values), 0,
1495 num_ssa_names * sizeof(complex_lattice_t));
1497 init_parameter_lattice_values ();
1498 ssa_propagate (complex_visit_stmt, complex_visit_phi);
1500 complex_variable_components = htab_create (10, int_tree_map_hash,
1501 int_tree_map_eq, free);
1503 complex_ssa_name_components = VEC_alloc (tree, heap, 2*num_ssa_names);
1504 VEC_safe_grow (tree, heap, complex_ssa_name_components, 2*num_ssa_names);
1505 memset (VEC_address (tree, complex_ssa_name_components), 0,
1506 2 * num_ssa_names * sizeof(tree));
1508 update_parameter_components ();
1510 /* ??? Ideally we'd traverse the blocks in breadth-first order. */
1511 old_last_basic_block = last_basic_block;
1514 if (bb->index >= old_last_basic_block)
1516 update_phi_components (bb);
1517 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1518 expand_complex_operations_1 (&bsi);
1521 bsi_commit_edge_inserts ();
1523 htab_delete (complex_variable_components);
1524 VEC_free (tree, heap, complex_ssa_name_components);
1525 VEC_free (complex_lattice_t, heap, complex_lattice_values);
1528 struct tree_opt_pass pass_lower_complex =
1530 "cplxlower", /* name */
1532 tree_lower_complex, /* execute */
1535 0, /* static_pass_number */
1537 PROP_ssa, /* properties_required */
1538 0, /* properties_provided */
1539 0, /* properties_destroyed */
1540 0, /* todo_flags_start */
1541 TODO_dump_func | TODO_ggc_collect
1543 | TODO_verify_stmts, /* todo_flags_finish */
1548 /* Entry point for complex operation lowering without optimization. */
1551 tree_lower_complex_O0 (void)
1553 int old_last_basic_block = last_basic_block;
1554 block_stmt_iterator bsi;
1559 if (bb->index >= old_last_basic_block)
1561 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1562 expand_complex_operations_1 (&bsi);
1567 gate_no_optimization (void)
1569 return optimize == 0;
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 */