1 /* Lower complex number operations to scalar operations.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
29 #include "tree-flow.h"
31 #include "tree-iterator.h"
32 #include "tree-pass.h"
33 #include "tree-ssa-propagate.h"
34 #include "diagnostic.h"
37 /* For each complex ssa name, a lattice value. We're interested in finding
38 out whether a complex number is degenerate in some way, having only real
39 or only complex parts. */
49 #define PAIR(a, b) ((a) << 2 | (b))
51 DEF_VEC_I(complex_lattice_t);
52 DEF_VEC_ALLOC_I(complex_lattice_t, heap);
54 static VEC(complex_lattice_t, heap) *complex_lattice_values;
56 /* For each complex variable, a pair of variables for the components exists in
58 static htab_t complex_variable_components;
60 /* For each complex SSA_NAME, a pair of ssa names for the components. */
61 static VEC(tree, heap) *complex_ssa_name_components;
63 /* Lookup UID in the complex_variable_components hashtable and return the
66 cvc_lookup (unsigned int uid)
68 struct int_tree_map *h, in;
70 h = (struct int_tree_map *) htab_find_with_hash (complex_variable_components, &in, uid);
71 return h ? h->to : NULL;
74 /* Insert the pair UID, TO into the complex_variable_components hashtable. */
77 cvc_insert (unsigned int uid, tree to)
79 struct int_tree_map *h;
82 h = XNEW (struct int_tree_map);
85 loc = htab_find_slot_with_hash (complex_variable_components, h,
87 *(struct int_tree_map **) loc = h;
90 /* Return true if T is not a zero constant. In the case of real values,
91 we're only interested in +0.0. */
94 some_nonzerop (tree t)
98 if (TREE_CODE (t) == REAL_CST)
99 zerop = REAL_VALUES_IDENTICAL (TREE_REAL_CST (t), dconst0);
100 else if (TREE_CODE (t) == FIXED_CST)
101 zerop = fixed_zerop (t);
102 else if (TREE_CODE (t) == INTEGER_CST)
103 zerop = integer_zerop (t);
109 /* Compute a lattice value from the components of a complex type REAL
112 static complex_lattice_t
113 find_lattice_value_parts (tree real, tree imag)
116 complex_lattice_t ret;
118 r = some_nonzerop (real);
119 i = some_nonzerop (imag);
120 ret = r * ONLY_REAL + i * ONLY_IMAG;
122 /* ??? On occasion we could do better than mapping 0+0i to real, but we
123 certainly don't want to leave it UNINITIALIZED, which eventually gets
124 mapped to VARYING. */
125 if (ret == UNINITIALIZED)
132 /* Compute a lattice value from gimple_val T. */
134 static complex_lattice_t
135 find_lattice_value (tree t)
139 switch (TREE_CODE (t))
142 return VEC_index (complex_lattice_t, complex_lattice_values,
143 SSA_NAME_VERSION (t));
146 real = TREE_REALPART (t);
147 imag = TREE_IMAGPART (t);
154 return find_lattice_value_parts (real, imag);
157 /* Determine if LHS is something for which we're interested in seeing
158 simulation results. */
161 is_complex_reg (tree lhs)
163 return TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE && is_gimple_reg (lhs);
166 /* Mark the incoming parameters to the function as VARYING. */
169 init_parameter_lattice_values (void)
173 for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = TREE_CHAIN (parm))
174 if (is_complex_reg (parm)
175 && var_ann (parm) != NULL
176 && (ssa_name = gimple_default_def (cfun, parm)) != NULL_TREE)
177 VEC_replace (complex_lattice_t, complex_lattice_values,
178 SSA_NAME_VERSION (ssa_name), VARYING);
181 /* Initialize simulation state for each statement. Return false if we
182 found no statements we want to simulate, and thus there's nothing
183 for the entire pass to do. */
186 init_dont_simulate_again (void)
189 gimple_stmt_iterator gsi;
191 bool saw_a_complex_op = false;
195 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
197 phi = gsi_stmt (gsi);
198 prop_set_simulate_again (phi,
199 is_complex_reg (gimple_phi_result (phi)));
202 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
208 stmt = gsi_stmt (gsi);
209 op0 = op1 = NULL_TREE;
211 /* Most control-altering statements must be initially
212 simulated, else we won't cover the entire cfg. */
213 sim_again_p = stmt_ends_bb_p (stmt);
215 switch (gimple_code (stmt))
218 if (gimple_call_lhs (stmt))
219 sim_again_p = is_complex_reg (gimple_call_lhs (stmt));
223 sim_again_p = is_complex_reg (gimple_assign_lhs (stmt));
224 if (gimple_assign_rhs_code (stmt) == REALPART_EXPR
225 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
226 op0 = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
228 op0 = gimple_assign_rhs1 (stmt);
229 if (gimple_num_ops (stmt) > 2)
230 op1 = gimple_assign_rhs2 (stmt);
234 op0 = gimple_cond_lhs (stmt);
235 op1 = gimple_cond_rhs (stmt);
243 switch (gimple_expr_code (stmt))
255 if (TREE_CODE (TREE_TYPE (op0)) == COMPLEX_TYPE
256 || TREE_CODE (TREE_TYPE (op1)) == COMPLEX_TYPE)
257 saw_a_complex_op = true;
262 if (TREE_CODE (TREE_TYPE (op0)) == COMPLEX_TYPE)
263 saw_a_complex_op = true;
268 /* The total store transformation performed during
269 gimplification creates such uninitialized loads
270 and we need to lower the statement to be able
272 if (TREE_CODE (op0) == SSA_NAME
273 && ssa_undefined_value_p (op0))
274 saw_a_complex_op = true;
281 prop_set_simulate_again (stmt, sim_again_p);
285 return saw_a_complex_op;
289 /* Evaluate statement STMT against the complex lattice defined above. */
291 static enum ssa_prop_result
292 complex_visit_stmt (gimple stmt, edge *taken_edge_p ATTRIBUTE_UNUSED,
295 complex_lattice_t new_l, old_l, op1_l, op2_l;
299 lhs = gimple_get_lhs (stmt);
300 /* Skip anything but GIMPLE_ASSIGN and GIMPLE_CALL with a lhs. */
302 return SSA_PROP_VARYING;
304 /* These conditions should be satisfied due to the initial filter
305 set up in init_dont_simulate_again. */
306 gcc_assert (TREE_CODE (lhs) == SSA_NAME);
307 gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE);
310 ver = SSA_NAME_VERSION (lhs);
311 old_l = VEC_index (complex_lattice_t, complex_lattice_values, ver);
313 switch (gimple_expr_code (stmt))
317 new_l = find_lattice_value (gimple_assign_rhs1 (stmt));
321 new_l = find_lattice_value_parts (gimple_assign_rhs1 (stmt),
322 gimple_assign_rhs2 (stmt));
327 op1_l = find_lattice_value (gimple_assign_rhs1 (stmt));
328 op2_l = find_lattice_value (gimple_assign_rhs2 (stmt));
330 /* We've set up the lattice values such that IOR neatly
332 new_l = op1_l | op2_l;
341 op1_l = find_lattice_value (gimple_assign_rhs1 (stmt));
342 op2_l = find_lattice_value (gimple_assign_rhs2 (stmt));
344 /* Obviously, if either varies, so does the result. */
345 if (op1_l == VARYING || op2_l == VARYING)
347 /* Don't prematurely promote variables if we've not yet seen
349 else if (op1_l == UNINITIALIZED)
351 else if (op2_l == UNINITIALIZED)
355 /* At this point both numbers have only one component. If the
356 numbers are of opposite kind, the result is imaginary,
357 otherwise the result is real. The add/subtract translates
358 the real/imag from/to 0/1; the ^ performs the comparison. */
359 new_l = ((op1_l - ONLY_REAL) ^ (op2_l - ONLY_REAL)) + ONLY_REAL;
361 /* Don't allow the lattice value to flip-flop indefinitely. */
368 new_l = find_lattice_value (gimple_assign_rhs1 (stmt));
376 /* If nothing changed this round, let the propagator know. */
378 return SSA_PROP_NOT_INTERESTING;
380 VEC_replace (complex_lattice_t, complex_lattice_values, ver, new_l);
381 return new_l == VARYING ? SSA_PROP_VARYING : SSA_PROP_INTERESTING;
384 /* Evaluate a PHI node against the complex lattice defined above. */
386 static enum ssa_prop_result
387 complex_visit_phi (gimple phi)
389 complex_lattice_t new_l, old_l;
394 lhs = gimple_phi_result (phi);
396 /* This condition should be satisfied due to the initial filter
397 set up in init_dont_simulate_again. */
398 gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE);
400 /* We've set up the lattice values such that IOR neatly models PHI meet. */
401 new_l = UNINITIALIZED;
402 for (i = gimple_phi_num_args (phi) - 1; i >= 0; --i)
403 new_l |= find_lattice_value (gimple_phi_arg_def (phi, i));
405 ver = SSA_NAME_VERSION (lhs);
406 old_l = VEC_index (complex_lattice_t, complex_lattice_values, ver);
409 return SSA_PROP_NOT_INTERESTING;
411 VEC_replace (complex_lattice_t, complex_lattice_values, ver, new_l);
412 return new_l == VARYING ? SSA_PROP_VARYING : SSA_PROP_INTERESTING;
415 /* Create one backing variable for a complex component of ORIG. */
418 create_one_component_var (tree type, tree orig, const char *prefix,
419 const char *suffix, enum tree_code code)
421 tree r = create_tmp_var (type, prefix);
422 add_referenced_var (r);
424 DECL_SOURCE_LOCATION (r) = DECL_SOURCE_LOCATION (orig);
425 DECL_ARTIFICIAL (r) = 1;
427 if (DECL_NAME (orig) && !DECL_IGNORED_P (orig))
429 const char *name = IDENTIFIER_POINTER (DECL_NAME (orig));
432 DECL_NAME (r) = get_identifier (ACONCAT ((name, suffix, NULL)));
434 inner_type = TREE_TYPE (TREE_TYPE (orig));
435 SET_DECL_DEBUG_EXPR (r, build1 (code, type, orig));
436 DECL_DEBUG_EXPR_IS_FROM (r) = 1;
437 DECL_IGNORED_P (r) = 0;
438 TREE_NO_WARNING (r) = TREE_NO_WARNING (orig);
442 DECL_IGNORED_P (r) = 1;
443 TREE_NO_WARNING (r) = 1;
449 /* Retrieve a value for a complex component of VAR. */
452 get_component_var (tree var, bool imag_p)
454 size_t decl_index = DECL_UID (var) * 2 + imag_p;
455 tree ret = cvc_lookup (decl_index);
459 ret = create_one_component_var (TREE_TYPE (TREE_TYPE (var)), var,
460 imag_p ? "CI" : "CR",
461 imag_p ? "$imag" : "$real",
462 imag_p ? IMAGPART_EXPR : REALPART_EXPR);
463 cvc_insert (decl_index, ret);
469 /* Retrieve a value for a complex component of SSA_NAME. */
472 get_component_ssa_name (tree ssa_name, bool imag_p)
474 complex_lattice_t lattice = find_lattice_value (ssa_name);
475 size_t ssa_name_index;
478 if (lattice == (imag_p ? ONLY_REAL : ONLY_IMAG))
480 tree inner_type = TREE_TYPE (TREE_TYPE (ssa_name));
481 if (SCALAR_FLOAT_TYPE_P (inner_type))
482 return build_real (inner_type, dconst0);
484 return build_int_cst (inner_type, 0);
487 ssa_name_index = SSA_NAME_VERSION (ssa_name) * 2 + imag_p;
488 ret = VEC_index (tree, complex_ssa_name_components, ssa_name_index);
491 ret = get_component_var (SSA_NAME_VAR (ssa_name), imag_p);
492 ret = make_ssa_name (ret, NULL);
494 /* Copy some properties from the original. In particular, whether it
495 is used in an abnormal phi, and whether it's uninitialized. */
496 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ret)
497 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name);
498 if (TREE_CODE (SSA_NAME_VAR (ssa_name)) == VAR_DECL
499 && gimple_nop_p (SSA_NAME_DEF_STMT (ssa_name)))
501 SSA_NAME_DEF_STMT (ret) = SSA_NAME_DEF_STMT (ssa_name);
502 set_default_def (SSA_NAME_VAR (ret), ret);
505 VEC_replace (tree, complex_ssa_name_components, ssa_name_index, ret);
511 /* Set a value for a complex component of SSA_NAME, return a
512 gimple_seq of stuff that needs doing. */
515 set_component_ssa_name (tree ssa_name, bool imag_p, tree value)
517 complex_lattice_t lattice = find_lattice_value (ssa_name);
518 size_t ssa_name_index;
523 /* We know the value must be zero, else there's a bug in our lattice
524 analysis. But the value may well be a variable known to contain
525 zero. We should be safe ignoring it. */
526 if (lattice == (imag_p ? ONLY_REAL : ONLY_IMAG))
529 /* If we've already assigned an SSA_NAME to this component, then this
530 means that our walk of the basic blocks found a use before the set.
531 This is fine. Now we should create an initialization for the value
532 we created earlier. */
533 ssa_name_index = SSA_NAME_VERSION (ssa_name) * 2 + imag_p;
534 comp = VEC_index (tree, complex_ssa_name_components, ssa_name_index);
538 /* If we've nothing assigned, and the value we're given is already stable,
539 then install that as the value for this SSA_NAME. This preemptively
540 copy-propagates the value, which avoids unnecessary memory allocation. */
541 else if (is_gimple_min_invariant (value)
542 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name))
544 VEC_replace (tree, complex_ssa_name_components, ssa_name_index, value);
547 else if (TREE_CODE (value) == SSA_NAME
548 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name))
550 /* Replace an anonymous base value with the variable from cvc_lookup.
551 This should result in better debug info. */
552 if (DECL_IGNORED_P (SSA_NAME_VAR (value))
553 && !DECL_IGNORED_P (SSA_NAME_VAR (ssa_name)))
555 comp = get_component_var (SSA_NAME_VAR (ssa_name), imag_p);
556 replace_ssa_name_symbol (value, comp);
559 VEC_replace (tree, complex_ssa_name_components, ssa_name_index, value);
563 /* Finally, we need to stabilize the result by installing the value into
566 comp = get_component_ssa_name (ssa_name, imag_p);
568 /* Do all the work to assign VALUE to COMP. */
570 value = force_gimple_operand (value, &list, false, NULL);
571 last = gimple_build_assign (comp, value);
572 gimple_seq_add_stmt (&list, last);
573 gcc_assert (SSA_NAME_DEF_STMT (comp) == last);
578 /* Extract the real or imaginary part of a complex variable or constant.
579 Make sure that it's a proper gimple_val and gimplify it if not.
580 Emit any new code before gsi. */
583 extract_component (gimple_stmt_iterator *gsi, tree t, bool imagpart_p,
586 switch (TREE_CODE (t))
589 return imagpart_p ? TREE_IMAGPART (t) : TREE_REALPART (t);
601 tree inner_type = TREE_TYPE (TREE_TYPE (t));
603 t = build1 ((imagpart_p ? IMAGPART_EXPR : REALPART_EXPR),
604 inner_type, unshare_expr (t));
607 t = force_gimple_operand_gsi (gsi, t, true, NULL, true,
614 return get_component_ssa_name (t, imagpart_p);
621 /* Update the complex components of the ssa name on the lhs of STMT. */
624 update_complex_components (gimple_stmt_iterator *gsi, gimple stmt, tree r,
630 lhs = gimple_get_lhs (stmt);
632 list = set_component_ssa_name (lhs, false, r);
634 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
636 list = set_component_ssa_name (lhs, true, i);
638 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
642 update_complex_components_on_edge (edge e, tree lhs, tree r, tree i)
646 list = set_component_ssa_name (lhs, false, r);
648 gsi_insert_seq_on_edge (e, list);
650 list = set_component_ssa_name (lhs, true, i);
652 gsi_insert_seq_on_edge (e, list);
656 /* Update an assignment to a complex variable in place. */
659 update_complex_assignment (gimple_stmt_iterator *gsi, tree r, tree i)
661 gimple_stmt_iterator orig_si = *gsi;
663 if (gimple_in_ssa_p (cfun))
664 update_complex_components (gsi, gsi_stmt (*gsi), r, i);
666 gimple_assign_set_rhs_with_ops (&orig_si, COMPLEX_EXPR, r, i);
667 update_stmt (gsi_stmt (orig_si));
671 /* Generate code at the entry point of the function to initialize the
672 component variables for a complex parameter. */
675 update_parameter_components (void)
677 edge entry_edge = single_succ_edge (ENTRY_BLOCK_PTR);
680 for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = TREE_CHAIN (parm))
682 tree type = TREE_TYPE (parm);
685 if (TREE_CODE (type) != COMPLEX_TYPE || !is_gimple_reg (parm))
688 type = TREE_TYPE (type);
689 ssa_name = gimple_default_def (cfun, parm);
693 r = build1 (REALPART_EXPR, type, ssa_name);
694 i = build1 (IMAGPART_EXPR, type, ssa_name);
695 update_complex_components_on_edge (entry_edge, ssa_name, r, i);
699 /* Generate code to set the component variables of a complex variable
700 to match the PHI statements in block BB. */
703 update_phi_components (basic_block bb)
705 gimple_stmt_iterator gsi;
707 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
709 gimple phi = gsi_stmt (gsi);
711 if (is_complex_reg (gimple_phi_result (phi)))
714 gimple pr = NULL, pi = NULL;
717 lr = get_component_ssa_name (gimple_phi_result (phi), false);
718 if (TREE_CODE (lr) == SSA_NAME)
720 pr = create_phi_node (lr, bb);
721 SSA_NAME_DEF_STMT (lr) = pr;
724 li = get_component_ssa_name (gimple_phi_result (phi), true);
725 if (TREE_CODE (li) == SSA_NAME)
727 pi = create_phi_node (li, bb);
728 SSA_NAME_DEF_STMT (li) = pi;
731 for (i = 0, n = gimple_phi_num_args (phi); i < n; ++i)
733 tree comp, arg = gimple_phi_arg_def (phi, i);
736 comp = extract_component (NULL, arg, false, false);
737 SET_PHI_ARG_DEF (pr, i, comp);
741 comp = extract_component (NULL, arg, true, false);
742 SET_PHI_ARG_DEF (pi, i, comp);
749 /* Expand a complex move to scalars. */
752 expand_complex_move (gimple_stmt_iterator *gsi, tree type)
754 tree inner_type = TREE_TYPE (type);
756 gimple stmt = gsi_stmt (*gsi);
758 if (is_gimple_assign (stmt))
760 lhs = gimple_assign_lhs (stmt);
761 if (gimple_num_ops (stmt) == 2)
762 rhs = gimple_assign_rhs1 (stmt);
766 else if (is_gimple_call (stmt))
768 lhs = gimple_call_lhs (stmt);
774 if (TREE_CODE (lhs) == SSA_NAME)
776 if (is_ctrl_altering_stmt (stmt))
781 /* The value is not assigned on the exception edges, so we need not
782 concern ourselves there. We do need to update on the fallthru
784 FOR_EACH_EDGE (e, ei, gsi_bb (*gsi)->succs)
785 if (e->flags & EDGE_FALLTHRU)
790 r = build1 (REALPART_EXPR, inner_type, lhs);
791 i = build1 (IMAGPART_EXPR, inner_type, lhs);
792 update_complex_components_on_edge (e, lhs, r, i);
794 else if (is_gimple_call (stmt)
795 || gimple_has_side_effects (stmt)
796 || gimple_assign_rhs_code (stmt) == PAREN_EXPR)
798 r = build1 (REALPART_EXPR, inner_type, lhs);
799 i = build1 (IMAGPART_EXPR, inner_type, lhs);
800 update_complex_components (gsi, stmt, r, i);
804 if (gimple_assign_rhs_code (stmt) != COMPLEX_EXPR)
806 r = extract_component (gsi, rhs, 0, true);
807 i = extract_component (gsi, rhs, 1, true);
811 r = gimple_assign_rhs1 (stmt);
812 i = gimple_assign_rhs2 (stmt);
814 update_complex_assignment (gsi, r, i);
817 else if (rhs && TREE_CODE (rhs) == SSA_NAME && !TREE_SIDE_EFFECTS (lhs))
822 r = extract_component (gsi, rhs, 0, false);
823 i = extract_component (gsi, rhs, 1, false);
825 x = build1 (REALPART_EXPR, inner_type, unshare_expr (lhs));
826 t = gimple_build_assign (x, r);
827 gsi_insert_before (gsi, t, GSI_SAME_STMT);
829 if (stmt == gsi_stmt (*gsi))
831 x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
832 gimple_assign_set_lhs (stmt, x);
833 gimple_assign_set_rhs1 (stmt, i);
837 x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
838 t = gimple_build_assign (x, i);
839 gsi_insert_before (gsi, t, GSI_SAME_STMT);
841 stmt = gsi_stmt (*gsi);
842 gcc_assert (gimple_code (stmt) == GIMPLE_RETURN);
843 gimple_return_set_retval (stmt, lhs);
850 /* Expand complex addition to scalars:
851 a + b = (ar + br) + i(ai + bi)
852 a - b = (ar - br) + i(ai + bi)
856 expand_complex_addition (gimple_stmt_iterator *gsi, tree inner_type,
857 tree ar, tree ai, tree br, tree bi,
859 complex_lattice_t al, complex_lattice_t bl)
863 switch (PAIR (al, bl))
865 case PAIR (ONLY_REAL, ONLY_REAL):
866 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
870 case PAIR (ONLY_REAL, ONLY_IMAG):
872 if (code == MINUS_EXPR)
873 ri = gimplify_build2 (gsi, MINUS_EXPR, inner_type, ai, bi);
878 case PAIR (ONLY_IMAG, ONLY_REAL):
879 if (code == MINUS_EXPR)
880 rr = gimplify_build2 (gsi, MINUS_EXPR, inner_type, ar, br);
886 case PAIR (ONLY_IMAG, ONLY_IMAG):
888 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
891 case PAIR (VARYING, ONLY_REAL):
892 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
896 case PAIR (VARYING, ONLY_IMAG):
898 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
901 case PAIR (ONLY_REAL, VARYING):
902 if (code == MINUS_EXPR)
904 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
908 case PAIR (ONLY_IMAG, VARYING):
909 if (code == MINUS_EXPR)
912 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
915 case PAIR (VARYING, VARYING):
917 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
918 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
925 update_complex_assignment (gsi, rr, ri);
928 /* Expand a complex multiplication or division to a libcall to the c99
929 compliant routines. */
932 expand_complex_libcall (gimple_stmt_iterator *gsi, tree ar, tree ai,
933 tree br, tree bi, enum tree_code code)
935 enum machine_mode mode;
936 enum built_in_function bcode;
938 gimple old_stmt, stmt;
940 old_stmt = gsi_stmt (*gsi);
941 lhs = gimple_assign_lhs (old_stmt);
942 type = TREE_TYPE (lhs);
944 mode = TYPE_MODE (type);
945 gcc_assert (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT);
947 if (code == MULT_EXPR)
948 bcode = BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT;
949 else if (code == RDIV_EXPR)
950 bcode = BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT;
953 fn = built_in_decls[bcode];
955 stmt = gimple_build_call (fn, 4, ar, ai, br, bi);
956 gimple_call_set_lhs (stmt, lhs);
958 gsi_replace (gsi, stmt, false);
960 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
961 gimple_purge_dead_eh_edges (gsi_bb (*gsi));
963 if (gimple_in_ssa_p (cfun))
965 type = TREE_TYPE (type);
966 update_complex_components (gsi, stmt,
967 build1 (REALPART_EXPR, type, lhs),
968 build1 (IMAGPART_EXPR, type, lhs));
969 SSA_NAME_DEF_STMT (lhs) = stmt;
973 /* Expand complex multiplication to scalars:
974 a * b = (ar*br - ai*bi) + i(ar*bi + br*ai)
978 expand_complex_multiplication (gimple_stmt_iterator *gsi, tree inner_type,
979 tree ar, tree ai, tree br, tree bi,
980 complex_lattice_t al, complex_lattice_t bl)
986 complex_lattice_t tl;
987 rr = ar, ar = br, br = rr;
988 ri = ai, ai = bi, bi = ri;
989 tl = al, al = bl, bl = tl;
992 switch (PAIR (al, bl))
994 case PAIR (ONLY_REAL, ONLY_REAL):
995 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
999 case PAIR (ONLY_IMAG, ONLY_REAL):
1001 if (TREE_CODE (ai) == REAL_CST
1002 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (ai), dconst1))
1005 ri = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1008 case PAIR (ONLY_IMAG, ONLY_IMAG):
1009 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1010 rr = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, rr);
1014 case PAIR (VARYING, ONLY_REAL):
1015 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1016 ri = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1019 case PAIR (VARYING, ONLY_IMAG):
1020 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1021 rr = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, rr);
1022 ri = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, bi);
1025 case PAIR (VARYING, VARYING):
1026 if (flag_complex_method == 2 && SCALAR_FLOAT_TYPE_P (inner_type))
1028 expand_complex_libcall (gsi, ar, ai, br, bi, MULT_EXPR);
1033 tree t1, t2, t3, t4;
1035 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1036 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1037 t3 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, bi);
1039 /* Avoid expanding redundant multiplication for the common
1040 case of squaring a complex number. */
1041 if (ar == br && ai == bi)
1044 t4 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1046 rr = gimplify_build2 (gsi, MINUS_EXPR, inner_type, t1, t2);
1047 ri = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t3, t4);
1055 update_complex_assignment (gsi, rr, ri);
1058 /* Expand complex division to scalars, straightforward algorithm.
1059 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1064 expand_complex_div_straight (gimple_stmt_iterator *gsi, tree inner_type,
1065 tree ar, tree ai, tree br, tree bi,
1066 enum tree_code code)
1068 tree rr, ri, div, t1, t2, t3;
1070 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, br, br);
1071 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, bi, bi);
1072 div = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, t2);
1074 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1075 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1076 t3 = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, t2);
1077 rr = gimplify_build2 (gsi, code, inner_type, t3, div);
1079 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1080 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, bi);
1081 t3 = gimplify_build2 (gsi, MINUS_EXPR, inner_type, t1, t2);
1082 ri = gimplify_build2 (gsi, code, inner_type, t3, div);
1084 update_complex_assignment (gsi, rr, ri);
1087 /* Expand complex division to scalars, modified algorithm to minimize
1088 overflow with wide input ranges. */
1091 expand_complex_div_wide (gimple_stmt_iterator *gsi, tree inner_type,
1092 tree ar, tree ai, tree br, tree bi,
1093 enum tree_code code)
1095 tree rr, ri, ratio, div, t1, t2, tr, ti, compare;
1096 basic_block bb_cond, bb_true, bb_false, bb_join;
1099 /* Examine |br| < |bi|, and branch. */
1100 t1 = gimplify_build1 (gsi, ABS_EXPR, inner_type, br);
1101 t2 = gimplify_build1 (gsi, ABS_EXPR, inner_type, bi);
1102 compare = fold_build2 (LT_EXPR, boolean_type_node, t1, t2);
1103 STRIP_NOPS (compare);
1105 bb_cond = bb_true = bb_false = bb_join = NULL;
1106 rr = ri = tr = ti = NULL;
1107 if (!TREE_CONSTANT (compare))
1113 tmp = create_tmp_var (boolean_type_node, NULL);
1114 stmt = gimple_build_assign (tmp, compare);
1115 if (gimple_in_ssa_p (cfun))
1117 tmp = make_ssa_name (tmp, stmt);
1118 gimple_assign_set_lhs (stmt, tmp);
1121 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1123 cond = fold_build2 (EQ_EXPR, boolean_type_node, tmp, boolean_true_node);
1124 stmt = gimple_build_cond_from_tree (cond, NULL_TREE, NULL_TREE);
1125 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1127 /* Split the original block, and create the TRUE and FALSE blocks. */
1128 e = split_block (gsi_bb (*gsi), stmt);
1131 bb_true = create_empty_bb (bb_cond);
1132 bb_false = create_empty_bb (bb_true);
1134 /* Wire the blocks together. */
1135 e->flags = EDGE_TRUE_VALUE;
1136 redirect_edge_succ (e, bb_true);
1137 make_edge (bb_cond, bb_false, EDGE_FALSE_VALUE);
1138 make_edge (bb_true, bb_join, EDGE_FALLTHRU);
1139 make_edge (bb_false, bb_join, EDGE_FALLTHRU);
1141 /* Update dominance info. Note that bb_join's data was
1142 updated by split_block. */
1143 if (dom_info_available_p (CDI_DOMINATORS))
1145 set_immediate_dominator (CDI_DOMINATORS, bb_true, bb_cond);
1146 set_immediate_dominator (CDI_DOMINATORS, bb_false, bb_cond);
1149 rr = make_rename_temp (inner_type, NULL);
1150 ri = make_rename_temp (inner_type, NULL);
1153 /* In the TRUE branch, we compute
1155 div = (br * ratio) + bi;
1156 tr = (ar * ratio) + ai;
1157 ti = (ai * ratio) - ar;
1160 if (bb_true || integer_nonzerop (compare))
1164 *gsi = gsi_last_bb (bb_true);
1165 gsi_insert_after (gsi, gimple_build_nop (), GSI_NEW_STMT);
1168 ratio = gimplify_build2 (gsi, code, inner_type, br, bi);
1170 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, br, ratio);
1171 div = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, bi);
1173 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, ratio);
1174 tr = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, ai);
1176 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, ratio);
1177 ti = gimplify_build2 (gsi, MINUS_EXPR, inner_type, t1, ar);
1179 tr = gimplify_build2 (gsi, code, inner_type, tr, div);
1180 ti = gimplify_build2 (gsi, code, inner_type, ti, div);
1184 stmt = gimple_build_assign (rr, tr);
1185 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1186 stmt = gimple_build_assign (ri, ti);
1187 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1188 gsi_remove (gsi, true);
1192 /* In the FALSE branch, we compute
1194 divisor = (d * ratio) + c;
1195 tr = (b * ratio) + a;
1196 ti = b - (a * ratio);
1199 if (bb_false || integer_zerop (compare))
1203 *gsi = gsi_last_bb (bb_false);
1204 gsi_insert_after (gsi, gimple_build_nop (), GSI_NEW_STMT);
1207 ratio = gimplify_build2 (gsi, code, inner_type, bi, br);
1209 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, bi, ratio);
1210 div = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, br);
1212 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, ratio);
1213 tr = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, ar);
1215 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, ratio);
1216 ti = gimplify_build2 (gsi, MINUS_EXPR, inner_type, ai, t1);
1218 tr = gimplify_build2 (gsi, code, inner_type, tr, div);
1219 ti = gimplify_build2 (gsi, code, inner_type, ti, div);
1223 stmt = gimple_build_assign (rr, tr);
1224 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1225 stmt = gimple_build_assign (ri, ti);
1226 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1227 gsi_remove (gsi, true);
1232 *gsi = gsi_start_bb (bb_join);
1236 update_complex_assignment (gsi, rr, ri);
1239 /* Expand complex division to scalars. */
1242 expand_complex_division (gimple_stmt_iterator *gsi, tree inner_type,
1243 tree ar, tree ai, tree br, tree bi,
1244 enum tree_code code,
1245 complex_lattice_t al, complex_lattice_t bl)
1249 switch (PAIR (al, bl))
1251 case PAIR (ONLY_REAL, ONLY_REAL):
1252 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
1256 case PAIR (ONLY_REAL, ONLY_IMAG):
1258 ri = gimplify_build2 (gsi, code, inner_type, ar, bi);
1259 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ri);
1262 case PAIR (ONLY_IMAG, ONLY_REAL):
1264 ri = gimplify_build2 (gsi, code, inner_type, ai, br);
1267 case PAIR (ONLY_IMAG, ONLY_IMAG):
1268 rr = gimplify_build2 (gsi, code, inner_type, ai, bi);
1272 case PAIR (VARYING, ONLY_REAL):
1273 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
1274 ri = gimplify_build2 (gsi, code, inner_type, ai, br);
1277 case PAIR (VARYING, ONLY_IMAG):
1278 rr = gimplify_build2 (gsi, code, inner_type, ai, bi);
1279 ri = gimplify_build2 (gsi, code, inner_type, ar, bi);
1280 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ri);
1282 case PAIR (ONLY_REAL, VARYING):
1283 case PAIR (ONLY_IMAG, VARYING):
1284 case PAIR (VARYING, VARYING):
1285 switch (flag_complex_method)
1288 /* straightforward implementation of complex divide acceptable. */
1289 expand_complex_div_straight (gsi, inner_type, ar, ai, br, bi, code);
1293 if (SCALAR_FLOAT_TYPE_P (inner_type))
1295 expand_complex_libcall (gsi, ar, ai, br, bi, code);
1301 /* wide ranges of inputs must work for complex divide. */
1302 expand_complex_div_wide (gsi, inner_type, ar, ai, br, bi, code);
1314 update_complex_assignment (gsi, rr, ri);
1317 /* Expand complex negation to scalars:
1322 expand_complex_negation (gimple_stmt_iterator *gsi, tree inner_type,
1327 rr = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ar);
1328 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ai);
1330 update_complex_assignment (gsi, rr, ri);
1333 /* Expand complex conjugate to scalars:
1338 expand_complex_conjugate (gimple_stmt_iterator *gsi, tree inner_type,
1343 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ai);
1345 update_complex_assignment (gsi, ar, ri);
1348 /* Expand complex comparison (EQ or NE only). */
1351 expand_complex_comparison (gimple_stmt_iterator *gsi, tree ar, tree ai,
1352 tree br, tree bi, enum tree_code code)
1354 tree cr, ci, cc, type;
1357 cr = gimplify_build2 (gsi, code, boolean_type_node, ar, br);
1358 ci = gimplify_build2 (gsi, code, boolean_type_node, ai, bi);
1359 cc = gimplify_build2 (gsi,
1360 (code == EQ_EXPR ? TRUTH_AND_EXPR : TRUTH_OR_EXPR),
1361 boolean_type_node, cr, ci);
1363 stmt = gsi_stmt (*gsi);
1365 switch (gimple_code (stmt))
1368 type = TREE_TYPE (gimple_return_retval (stmt));
1369 gimple_return_set_retval (stmt, fold_convert (type, cc));
1373 type = TREE_TYPE (gimple_assign_lhs (stmt));
1374 gimple_assign_set_rhs_from_tree (gsi, fold_convert (type, cc));
1375 stmt = gsi_stmt (*gsi);
1379 gimple_cond_set_code (stmt, EQ_EXPR);
1380 gimple_cond_set_lhs (stmt, cc);
1381 gimple_cond_set_rhs (stmt, boolean_true_node);
1392 /* Process one statement. If we identify a complex operation, expand it. */
1395 expand_complex_operations_1 (gimple_stmt_iterator *gsi)
1397 gimple stmt = gsi_stmt (*gsi);
1398 tree type, inner_type, lhs;
1399 tree ac, ar, ai, bc, br, bi;
1400 complex_lattice_t al, bl;
1401 enum tree_code code;
1403 lhs = gimple_get_lhs (stmt);
1404 if (!lhs && gimple_code (stmt) != GIMPLE_COND)
1407 type = TREE_TYPE (gimple_op (stmt, 0));
1408 code = gimple_expr_code (stmt);
1410 /* Initial filter for operations we handle. */
1416 case TRUNC_DIV_EXPR:
1418 case FLOOR_DIV_EXPR:
1419 case ROUND_DIV_EXPR:
1423 if (TREE_CODE (type) != COMPLEX_TYPE)
1425 inner_type = TREE_TYPE (type);
1430 /* Note, both GIMPLE_ASSIGN and GIMPLE_COND may have an EQ_EXPR
1431 subocde, so we need to access the operands using gimple_op. */
1432 inner_type = TREE_TYPE (gimple_op (stmt, 1));
1433 if (TREE_CODE (inner_type) != COMPLEX_TYPE)
1441 /* GIMPLE_COND may also fallthru here, but we do not need to
1442 do anything with it. */
1443 if (gimple_code (stmt) == GIMPLE_COND)
1446 if (TREE_CODE (type) == COMPLEX_TYPE)
1447 expand_complex_move (gsi, type);
1448 else if (is_gimple_assign (stmt)
1449 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
1450 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
1451 && TREE_CODE (lhs) == SSA_NAME)
1453 rhs = gimple_assign_rhs1 (stmt);
1454 rhs = extract_component (gsi, TREE_OPERAND (rhs, 0),
1455 gimple_assign_rhs_code (stmt)
1458 gimple_assign_set_rhs_from_tree (gsi, rhs);
1459 stmt = gsi_stmt (*gsi);
1466 /* Extract the components of the two complex values. Make sure and
1467 handle the common case of the same value used twice specially. */
1468 if (is_gimple_assign (stmt))
1470 ac = gimple_assign_rhs1 (stmt);
1471 bc = (gimple_num_ops (stmt) > 2) ? gimple_assign_rhs2 (stmt) : NULL;
1473 /* GIMPLE_CALL can not get here. */
1476 ac = gimple_cond_lhs (stmt);
1477 bc = gimple_cond_rhs (stmt);
1480 ar = extract_component (gsi, ac, false, true);
1481 ai = extract_component (gsi, ac, true, true);
1487 br = extract_component (gsi, bc, 0, true);
1488 bi = extract_component (gsi, bc, 1, true);
1491 br = bi = NULL_TREE;
1493 if (gimple_in_ssa_p (cfun))
1495 al = find_lattice_value (ac);
1496 if (al == UNINITIALIZED)
1499 if (TREE_CODE_CLASS (code) == tcc_unary)
1505 bl = find_lattice_value (bc);
1506 if (bl == UNINITIALIZED)
1517 expand_complex_addition (gsi, inner_type, ar, ai, br, bi, code, al, bl);
1521 expand_complex_multiplication (gsi, inner_type, ar, ai, br, bi, al, bl);
1524 case TRUNC_DIV_EXPR:
1526 case FLOOR_DIV_EXPR:
1527 case ROUND_DIV_EXPR:
1529 expand_complex_division (gsi, inner_type, ar, ai, br, bi, code, al, bl);
1533 expand_complex_negation (gsi, inner_type, ar, ai);
1537 expand_complex_conjugate (gsi, inner_type, ar, ai);
1542 expand_complex_comparison (gsi, ar, ai, br, bi, code);
1551 /* Entry point for complex operation lowering during optimization. */
1554 tree_lower_complex (void)
1556 int old_last_basic_block;
1557 gimple_stmt_iterator gsi;
1560 if (!init_dont_simulate_again ())
1563 complex_lattice_values = VEC_alloc (complex_lattice_t, heap, num_ssa_names);
1564 VEC_safe_grow_cleared (complex_lattice_t, heap,
1565 complex_lattice_values, num_ssa_names);
1567 init_parameter_lattice_values ();
1568 ssa_propagate (complex_visit_stmt, complex_visit_phi);
1570 complex_variable_components = htab_create (10, int_tree_map_hash,
1571 int_tree_map_eq, free);
1573 complex_ssa_name_components = VEC_alloc (tree, heap, 2*num_ssa_names);
1574 VEC_safe_grow_cleared (tree, heap, complex_ssa_name_components,
1577 update_parameter_components ();
1579 /* ??? Ideally we'd traverse the blocks in breadth-first order. */
1580 old_last_basic_block = last_basic_block;
1583 if (bb->index >= old_last_basic_block)
1586 update_phi_components (bb);
1587 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1588 expand_complex_operations_1 (&gsi);
1591 gsi_commit_edge_inserts ();
1593 htab_delete (complex_variable_components);
1594 VEC_free (tree, heap, complex_ssa_name_components);
1595 VEC_free (complex_lattice_t, heap, complex_lattice_values);
1599 struct gimple_opt_pass pass_lower_complex =
1603 "cplxlower", /* name */
1605 tree_lower_complex, /* execute */
1608 0, /* static_pass_number */
1609 TV_NONE, /* tv_id */
1610 PROP_ssa, /* properties_required */
1611 0, /* properties_provided */
1612 0, /* properties_destroyed */
1613 0, /* todo_flags_start */
1617 | TODO_verify_stmts /* todo_flags_finish */
1622 /* Entry point for complex operation lowering without optimization. */
1625 tree_lower_complex_O0 (void)
1627 int old_last_basic_block = last_basic_block;
1628 gimple_stmt_iterator gsi;
1633 if (bb->index >= old_last_basic_block)
1636 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1637 expand_complex_operations_1 (&gsi);
1643 gate_no_optimization (void)
1645 /* With errors, normal optimization passes are not run. If we don't
1646 lower complex operations at all, rtl expansion will abort. */
1647 return optimize == 0 || sorrycount || errorcount;
1650 struct gimple_opt_pass pass_lower_complex_O0 =
1654 "cplxlower0", /* name */
1655 gate_no_optimization, /* gate */
1656 tree_lower_complex_O0, /* execute */
1659 0, /* static_pass_number */
1660 TV_NONE, /* tv_id */
1661 PROP_cfg, /* properties_required */
1662 0, /* properties_provided */
1663 0, /* properties_destroyed */
1664 0, /* todo_flags_start */
1665 TODO_dump_func | TODO_ggc_collect
1666 | TODO_verify_stmts, /* todo_flags_finish */