1 /* Lower complex number operations to scalar operations.
2 Copyright (C) 2004-2020 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-pass.h"
30 #include "fold-const.h"
31 #include "stor-layout.h"
34 #include "gimple-iterator.h"
35 #include "gimplify-me.h"
39 #include "tree-ssa-propagate.h"
40 #include "tree-hasher.h"
45 /* For each complex ssa name, a lattice value. We're interested in finding
46 out whether a complex number is degenerate in some way, having only real
47 or only complex parts. */
57 /* The type complex_lattice_t holds combinations of the above
59 typedef int complex_lattice_t
;
61 #define PAIR(a, b) ((a) << 2 | (b))
63 class complex_propagate
: public ssa_propagation_engine
65 enum ssa_prop_result
visit_stmt (gimple
*, edge
*, tree
*) FINAL OVERRIDE
;
66 enum ssa_prop_result
visit_phi (gphi
*) FINAL OVERRIDE
;
69 static vec
<complex_lattice_t
> complex_lattice_values
;
71 /* For each complex variable, a pair of variables for the components exists in
73 static int_tree_htab_type
*complex_variable_components
;
75 /* For each complex SSA_NAME, a pair of ssa names for the components. */
76 static vec
<tree
> complex_ssa_name_components
;
78 /* Vector of PHI triplets (original complex PHI and corresponding real and
79 imag PHIs if real and/or imag PHIs contain temporarily
80 non-SSA_NAME/non-invariant args that need to be replaced by SSA_NAMEs. */
81 static vec
<gphi
*> phis_to_revisit
;
83 /* BBs that need EH cleanup. */
84 static bitmap need_eh_cleanup
;
86 /* Lookup UID in the complex_variable_components hashtable and return the
89 cvc_lookup (unsigned int uid
)
91 struct int_tree_map in
;
93 return complex_variable_components
->find_with_hash (in
, uid
).to
;
96 /* Insert the pair UID, TO into the complex_variable_components hashtable. */
99 cvc_insert (unsigned int uid
, tree to
)
105 loc
= complex_variable_components
->find_slot_with_hash (h
, uid
, INSERT
);
110 /* Return true if T is not a zero constant. In the case of real values,
111 we're only interested in +0.0. */
114 some_nonzerop (tree t
)
118 /* Operations with real or imaginary part of a complex number zero
119 cannot be treated the same as operations with a real or imaginary
120 operand if we care about the signs of zeros in the result. */
121 if (TREE_CODE (t
) == REAL_CST
&& !flag_signed_zeros
)
122 zerop
= real_identical (&TREE_REAL_CST (t
), &dconst0
);
123 else if (TREE_CODE (t
) == FIXED_CST
)
124 zerop
= fixed_zerop (t
);
125 else if (TREE_CODE (t
) == INTEGER_CST
)
126 zerop
= integer_zerop (t
);
132 /* Compute a lattice value from the components of a complex type REAL
135 static complex_lattice_t
136 find_lattice_value_parts (tree real
, tree imag
)
139 complex_lattice_t ret
;
141 r
= some_nonzerop (real
);
142 i
= some_nonzerop (imag
);
143 ret
= r
* ONLY_REAL
+ i
* ONLY_IMAG
;
145 /* ??? On occasion we could do better than mapping 0+0i to real, but we
146 certainly don't want to leave it UNINITIALIZED, which eventually gets
147 mapped to VARYING. */
148 if (ret
== UNINITIALIZED
)
155 /* Compute a lattice value from gimple_val T. */
157 static complex_lattice_t
158 find_lattice_value (tree t
)
162 switch (TREE_CODE (t
))
165 return complex_lattice_values
[SSA_NAME_VERSION (t
)];
168 real
= TREE_REALPART (t
);
169 imag
= TREE_IMAGPART (t
);
176 return find_lattice_value_parts (real
, imag
);
179 /* Determine if LHS is something for which we're interested in seeing
180 simulation results. */
183 is_complex_reg (tree lhs
)
185 return TREE_CODE (TREE_TYPE (lhs
)) == COMPLEX_TYPE
&& is_gimple_reg (lhs
);
188 /* Mark the incoming parameters to the function as VARYING. */
191 init_parameter_lattice_values (void)
195 for (parm
= DECL_ARGUMENTS (cfun
->decl
); parm
; parm
= DECL_CHAIN (parm
))
196 if (is_complex_reg (parm
)
197 && (ssa_name
= ssa_default_def (cfun
, parm
)) != NULL_TREE
)
198 complex_lattice_values
[SSA_NAME_VERSION (ssa_name
)] = VARYING
;
201 /* Initialize simulation state for each statement. Return false if we
202 found no statements we want to simulate, and thus there's nothing
203 for the entire pass to do. */
206 init_dont_simulate_again (void)
209 bool saw_a_complex_op
= false;
211 FOR_EACH_BB_FN (bb
, cfun
)
213 for (gphi_iterator gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
);
216 gphi
*phi
= gsi
.phi ();
217 prop_set_simulate_again (phi
,
218 is_complex_reg (gimple_phi_result (phi
)));
221 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);
228 stmt
= gsi_stmt (gsi
);
229 op0
= op1
= NULL_TREE
;
231 /* Most control-altering statements must be initially
232 simulated, else we won't cover the entire cfg. */
233 sim_again_p
= stmt_ends_bb_p (stmt
);
235 switch (gimple_code (stmt
))
238 if (gimple_call_lhs (stmt
))
239 sim_again_p
= is_complex_reg (gimple_call_lhs (stmt
));
243 sim_again_p
= is_complex_reg (gimple_assign_lhs (stmt
));
244 if (gimple_assign_rhs_code (stmt
) == REALPART_EXPR
245 || gimple_assign_rhs_code (stmt
) == IMAGPART_EXPR
)
246 op0
= TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
248 op0
= gimple_assign_rhs1 (stmt
);
249 if (gimple_num_ops (stmt
) > 2)
250 op1
= gimple_assign_rhs2 (stmt
);
254 op0
= gimple_cond_lhs (stmt
);
255 op1
= gimple_cond_rhs (stmt
);
263 switch (gimple_expr_code (stmt
))
275 if (TREE_CODE (TREE_TYPE (op0
)) == COMPLEX_TYPE
276 || TREE_CODE (TREE_TYPE (op1
)) == COMPLEX_TYPE
)
277 saw_a_complex_op
= true;
282 if (TREE_CODE (TREE_TYPE (op0
)) == COMPLEX_TYPE
)
283 saw_a_complex_op
= true;
288 /* The total store transformation performed during
289 gimplification creates such uninitialized loads
290 and we need to lower the statement to be able
292 if (TREE_CODE (op0
) == SSA_NAME
293 && ssa_undefined_value_p (op0
))
294 saw_a_complex_op
= true;
301 prop_set_simulate_again (stmt
, sim_again_p
);
305 return saw_a_complex_op
;
309 /* Evaluate statement STMT against the complex lattice defined above. */
312 complex_propagate::visit_stmt (gimple
*stmt
, edge
*taken_edge_p ATTRIBUTE_UNUSED
,
315 complex_lattice_t new_l
, old_l
, op1_l
, op2_l
;
319 lhs
= gimple_get_lhs (stmt
);
320 /* Skip anything but GIMPLE_ASSIGN and GIMPLE_CALL with a lhs. */
322 return SSA_PROP_VARYING
;
324 /* These conditions should be satisfied due to the initial filter
325 set up in init_dont_simulate_again. */
326 gcc_assert (TREE_CODE (lhs
) == SSA_NAME
);
327 gcc_assert (TREE_CODE (TREE_TYPE (lhs
)) == COMPLEX_TYPE
);
330 ver
= SSA_NAME_VERSION (lhs
);
331 old_l
= complex_lattice_values
[ver
];
333 switch (gimple_expr_code (stmt
))
337 new_l
= find_lattice_value (gimple_assign_rhs1 (stmt
));
341 new_l
= find_lattice_value_parts (gimple_assign_rhs1 (stmt
),
342 gimple_assign_rhs2 (stmt
));
347 op1_l
= find_lattice_value (gimple_assign_rhs1 (stmt
));
348 op2_l
= find_lattice_value (gimple_assign_rhs2 (stmt
));
350 /* We've set up the lattice values such that IOR neatly
352 new_l
= op1_l
| op2_l
;
361 op1_l
= find_lattice_value (gimple_assign_rhs1 (stmt
));
362 op2_l
= find_lattice_value (gimple_assign_rhs2 (stmt
));
364 /* Obviously, if either varies, so does the result. */
365 if (op1_l
== VARYING
|| op2_l
== VARYING
)
367 /* Don't prematurely promote variables if we've not yet seen
369 else if (op1_l
== UNINITIALIZED
)
371 else if (op2_l
== UNINITIALIZED
)
375 /* At this point both numbers have only one component. If the
376 numbers are of opposite kind, the result is imaginary,
377 otherwise the result is real. The add/subtract translates
378 the real/imag from/to 0/1; the ^ performs the comparison. */
379 new_l
= ((op1_l
- ONLY_REAL
) ^ (op2_l
- ONLY_REAL
)) + ONLY_REAL
;
381 /* Don't allow the lattice value to flip-flop indefinitely. */
388 new_l
= find_lattice_value (gimple_assign_rhs1 (stmt
));
396 /* If nothing changed this round, let the propagator know. */
398 return SSA_PROP_NOT_INTERESTING
;
400 complex_lattice_values
[ver
] = new_l
;
401 return new_l
== VARYING
? SSA_PROP_VARYING
: SSA_PROP_INTERESTING
;
404 /* Evaluate a PHI node against the complex lattice defined above. */
407 complex_propagate::visit_phi (gphi
*phi
)
409 complex_lattice_t new_l
, old_l
;
414 lhs
= gimple_phi_result (phi
);
416 /* This condition should be satisfied due to the initial filter
417 set up in init_dont_simulate_again. */
418 gcc_assert (TREE_CODE (TREE_TYPE (lhs
)) == COMPLEX_TYPE
);
420 /* We've set up the lattice values such that IOR neatly models PHI meet. */
421 new_l
= UNINITIALIZED
;
422 for (i
= gimple_phi_num_args (phi
) - 1; i
>= 0; --i
)
423 new_l
|= find_lattice_value (gimple_phi_arg_def (phi
, i
));
425 ver
= SSA_NAME_VERSION (lhs
);
426 old_l
= complex_lattice_values
[ver
];
429 return SSA_PROP_NOT_INTERESTING
;
431 complex_lattice_values
[ver
] = new_l
;
432 return new_l
== VARYING
? SSA_PROP_VARYING
: SSA_PROP_INTERESTING
;
435 /* Create one backing variable for a complex component of ORIG. */
438 create_one_component_var (tree type
, tree orig
, const char *prefix
,
439 const char *suffix
, enum tree_code code
)
441 tree r
= create_tmp_var (type
, prefix
);
443 DECL_SOURCE_LOCATION (r
) = DECL_SOURCE_LOCATION (orig
);
444 DECL_ARTIFICIAL (r
) = 1;
446 if (DECL_NAME (orig
) && !DECL_IGNORED_P (orig
))
448 const char *name
= IDENTIFIER_POINTER (DECL_NAME (orig
));
449 name
= ACONCAT ((name
, suffix
, NULL
));
450 DECL_NAME (r
) = get_identifier (name
);
452 SET_DECL_DEBUG_EXPR (r
, build1 (code
, type
, orig
));
453 DECL_HAS_DEBUG_EXPR_P (r
) = 1;
454 DECL_IGNORED_P (r
) = 0;
455 TREE_NO_WARNING (r
) = TREE_NO_WARNING (orig
);
459 DECL_IGNORED_P (r
) = 1;
460 TREE_NO_WARNING (r
) = 1;
466 /* Retrieve a value for a complex component of VAR. */
469 get_component_var (tree var
, bool imag_p
)
471 size_t decl_index
= DECL_UID (var
) * 2 + imag_p
;
472 tree ret
= cvc_lookup (decl_index
);
476 ret
= create_one_component_var (TREE_TYPE (TREE_TYPE (var
)), var
,
477 imag_p
? "CI" : "CR",
478 imag_p
? "$imag" : "$real",
479 imag_p
? IMAGPART_EXPR
: REALPART_EXPR
);
480 cvc_insert (decl_index
, ret
);
486 /* Retrieve a value for a complex component of SSA_NAME. */
489 get_component_ssa_name (tree ssa_name
, bool imag_p
)
491 complex_lattice_t lattice
= find_lattice_value (ssa_name
);
492 size_t ssa_name_index
;
495 if (lattice
== (imag_p
? ONLY_REAL
: ONLY_IMAG
))
497 tree inner_type
= TREE_TYPE (TREE_TYPE (ssa_name
));
498 if (SCALAR_FLOAT_TYPE_P (inner_type
))
499 return build_real (inner_type
, dconst0
);
501 return build_int_cst (inner_type
, 0);
504 ssa_name_index
= SSA_NAME_VERSION (ssa_name
) * 2 + imag_p
;
505 ret
= complex_ssa_name_components
[ssa_name_index
];
508 if (SSA_NAME_VAR (ssa_name
))
509 ret
= get_component_var (SSA_NAME_VAR (ssa_name
), imag_p
);
511 ret
= TREE_TYPE (TREE_TYPE (ssa_name
));
512 ret
= make_ssa_name (ret
);
514 /* Copy some properties from the original. In particular, whether it
515 is used in an abnormal phi, and whether it's uninitialized. */
516 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ret
)
517 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name
);
518 if (SSA_NAME_IS_DEFAULT_DEF (ssa_name
)
519 && TREE_CODE (SSA_NAME_VAR (ssa_name
)) == VAR_DECL
)
521 SSA_NAME_DEF_STMT (ret
) = SSA_NAME_DEF_STMT (ssa_name
);
522 set_ssa_default_def (cfun
, SSA_NAME_VAR (ret
), ret
);
525 complex_ssa_name_components
[ssa_name_index
] = ret
;
531 /* Set a value for a complex component of SSA_NAME, return a
532 gimple_seq of stuff that needs doing. */
535 set_component_ssa_name (tree ssa_name
, bool imag_p
, tree value
)
537 complex_lattice_t lattice
= find_lattice_value (ssa_name
);
538 size_t ssa_name_index
;
543 /* We know the value must be zero, else there's a bug in our lattice
544 analysis. But the value may well be a variable known to contain
545 zero. We should be safe ignoring it. */
546 if (lattice
== (imag_p
? ONLY_REAL
: ONLY_IMAG
))
549 /* If we've already assigned an SSA_NAME to this component, then this
550 means that our walk of the basic blocks found a use before the set.
551 This is fine. Now we should create an initialization for the value
552 we created earlier. */
553 ssa_name_index
= SSA_NAME_VERSION (ssa_name
) * 2 + imag_p
;
554 comp
= complex_ssa_name_components
[ssa_name_index
];
558 /* If we've nothing assigned, and the value we're given is already stable,
559 then install that as the value for this SSA_NAME. This preemptively
560 copy-propagates the value, which avoids unnecessary memory allocation. */
561 else if (is_gimple_min_invariant (value
)
562 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name
))
564 complex_ssa_name_components
[ssa_name_index
] = value
;
567 else if (TREE_CODE (value
) == SSA_NAME
568 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name
))
570 /* Replace an anonymous base value with the variable from cvc_lookup.
571 This should result in better debug info. */
572 if (!SSA_NAME_IS_DEFAULT_DEF (value
)
573 && SSA_NAME_VAR (ssa_name
)
574 && (!SSA_NAME_VAR (value
) || DECL_IGNORED_P (SSA_NAME_VAR (value
)))
575 && !DECL_IGNORED_P (SSA_NAME_VAR (ssa_name
)))
577 comp
= get_component_var (SSA_NAME_VAR (ssa_name
), imag_p
);
578 replace_ssa_name_symbol (value
, comp
);
581 complex_ssa_name_components
[ssa_name_index
] = value
;
585 /* Finally, we need to stabilize the result by installing the value into
588 comp
= get_component_ssa_name (ssa_name
, imag_p
);
590 /* Do all the work to assign VALUE to COMP. */
592 value
= force_gimple_operand (value
, &list
, false, NULL
);
593 last
= gimple_build_assign (comp
, value
);
594 gimple_seq_add_stmt (&list
, last
);
595 gcc_assert (SSA_NAME_DEF_STMT (comp
) == last
);
600 /* Extract the real or imaginary part of a complex variable or constant.
601 Make sure that it's a proper gimple_val and gimplify it if not.
602 Emit any new code before gsi. */
605 extract_component (gimple_stmt_iterator
*gsi
, tree t
, bool imagpart_p
,
606 bool gimple_p
, bool phiarg_p
= false)
608 switch (TREE_CODE (t
))
611 return imagpart_p
? TREE_IMAGPART (t
) : TREE_REALPART (t
);
618 tree inner_type
= TREE_TYPE (TREE_TYPE (t
));
619 t
= unshare_expr (t
);
620 TREE_TYPE (t
) = inner_type
;
621 TREE_OPERAND (t
, 1) = TYPE_SIZE (inner_type
);
623 TREE_OPERAND (t
, 2) = size_binop (PLUS_EXPR
, TREE_OPERAND (t
, 2),
624 TYPE_SIZE (inner_type
));
626 t
= force_gimple_operand_gsi (gsi
, t
, true, NULL
, true,
636 case VIEW_CONVERT_EXPR
:
639 tree inner_type
= TREE_TYPE (TREE_TYPE (t
));
641 t
= build1 ((imagpart_p
? IMAGPART_EXPR
: REALPART_EXPR
),
642 inner_type
, unshare_expr (t
));
645 t
= force_gimple_operand_gsi (gsi
, t
, true, NULL
, true,
652 t
= get_component_ssa_name (t
, imagpart_p
);
653 if (TREE_CODE (t
) == SSA_NAME
&& SSA_NAME_DEF_STMT (t
) == NULL
)
654 gcc_assert (phiarg_p
);
662 /* Update the complex components of the ssa name on the lhs of STMT. */
665 update_complex_components (gimple_stmt_iterator
*gsi
, gimple
*stmt
, tree r
,
671 lhs
= gimple_get_lhs (stmt
);
673 list
= set_component_ssa_name (lhs
, false, r
);
675 gsi_insert_seq_after (gsi
, list
, GSI_CONTINUE_LINKING
);
677 list
= set_component_ssa_name (lhs
, true, i
);
679 gsi_insert_seq_after (gsi
, list
, GSI_CONTINUE_LINKING
);
683 update_complex_components_on_edge (edge e
, tree lhs
, tree r
, tree i
)
687 list
= set_component_ssa_name (lhs
, false, r
);
689 gsi_insert_seq_on_edge (e
, list
);
691 list
= set_component_ssa_name (lhs
, true, i
);
693 gsi_insert_seq_on_edge (e
, list
);
697 /* Update an assignment to a complex variable in place. */
700 update_complex_assignment (gimple_stmt_iterator
*gsi
, tree r
, tree i
)
702 gimple
*old_stmt
= gsi_stmt (*gsi
);
703 gimple_assign_set_rhs_with_ops (gsi
, COMPLEX_EXPR
, r
, i
);
704 gimple
*stmt
= gsi_stmt (*gsi
);
706 if (maybe_clean_or_replace_eh_stmt (old_stmt
, stmt
))
707 bitmap_set_bit (need_eh_cleanup
, gimple_bb (stmt
)->index
);
709 update_complex_components (gsi
, gsi_stmt (*gsi
), r
, i
);
713 /* Generate code at the entry point of the function to initialize the
714 component variables for a complex parameter. */
717 update_parameter_components (void)
719 edge entry_edge
= single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
722 for (parm
= DECL_ARGUMENTS (cfun
->decl
); parm
; parm
= DECL_CHAIN (parm
))
724 tree type
= TREE_TYPE (parm
);
727 if (TREE_CODE (type
) != COMPLEX_TYPE
|| !is_gimple_reg (parm
))
730 type
= TREE_TYPE (type
);
731 ssa_name
= ssa_default_def (cfun
, parm
);
735 r
= build1 (REALPART_EXPR
, type
, ssa_name
);
736 i
= build1 (IMAGPART_EXPR
, type
, ssa_name
);
737 update_complex_components_on_edge (entry_edge
, ssa_name
, r
, i
);
741 /* Generate code to set the component variables of a complex variable
742 to match the PHI statements in block BB. */
745 update_phi_components (basic_block bb
)
749 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
751 gphi
*phi
= gsi
.phi ();
753 if (is_complex_reg (gimple_phi_result (phi
)))
755 gphi
*p
[2] = { NULL
, NULL
};
756 unsigned int i
, j
, n
;
757 bool revisit_phi
= false;
759 for (j
= 0; j
< 2; j
++)
761 tree l
= get_component_ssa_name (gimple_phi_result (phi
), j
> 0);
762 if (TREE_CODE (l
) == SSA_NAME
)
763 p
[j
] = create_phi_node (l
, bb
);
766 for (i
= 0, n
= gimple_phi_num_args (phi
); i
< n
; ++i
)
768 tree comp
, arg
= gimple_phi_arg_def (phi
, i
);
769 for (j
= 0; j
< 2; j
++)
772 comp
= extract_component (NULL
, arg
, j
> 0, false, true);
773 if (TREE_CODE (comp
) == SSA_NAME
774 && SSA_NAME_DEF_STMT (comp
) == NULL
)
776 /* For the benefit of any gimple simplification during
777 this pass that might walk SSA_NAME def stmts,
778 don't add SSA_NAMEs without definitions into the
779 PHI arguments, but put a decl in there instead
780 temporarily, and revisit this PHI later on. */
781 if (SSA_NAME_VAR (comp
))
782 comp
= SSA_NAME_VAR (comp
);
784 comp
= create_tmp_reg (TREE_TYPE (comp
),
788 SET_PHI_ARG_DEF (p
[j
], i
, comp
);
794 phis_to_revisit
.safe_push (phi
);
795 phis_to_revisit
.safe_push (p
[0]);
796 phis_to_revisit
.safe_push (p
[1]);
802 /* Expand a complex move to scalars. */
805 expand_complex_move (gimple_stmt_iterator
*gsi
, tree type
)
807 tree inner_type
= TREE_TYPE (type
);
809 gimple
*stmt
= gsi_stmt (*gsi
);
811 if (is_gimple_assign (stmt
))
813 lhs
= gimple_assign_lhs (stmt
);
814 if (gimple_num_ops (stmt
) == 2)
815 rhs
= gimple_assign_rhs1 (stmt
);
819 else if (is_gimple_call (stmt
))
821 lhs
= gimple_call_lhs (stmt
);
827 if (TREE_CODE (lhs
) == SSA_NAME
)
829 if (is_ctrl_altering_stmt (stmt
))
833 /* The value is not assigned on the exception edges, so we need not
834 concern ourselves there. We do need to update on the fallthru
836 e
= find_fallthru_edge (gsi_bb (*gsi
)->succs
);
840 r
= build1 (REALPART_EXPR
, inner_type
, lhs
);
841 i
= build1 (IMAGPART_EXPR
, inner_type
, lhs
);
842 update_complex_components_on_edge (e
, lhs
, r
, i
);
844 else if (is_gimple_call (stmt
)
845 || gimple_has_side_effects (stmt
)
846 || gimple_assign_rhs_code (stmt
) == PAREN_EXPR
)
848 r
= build1 (REALPART_EXPR
, inner_type
, lhs
);
849 i
= build1 (IMAGPART_EXPR
, inner_type
, lhs
);
850 update_complex_components (gsi
, stmt
, r
, i
);
854 if (gimple_assign_rhs_code (stmt
) != COMPLEX_EXPR
)
856 r
= extract_component (gsi
, rhs
, 0, true);
857 i
= extract_component (gsi
, rhs
, 1, true);
861 r
= gimple_assign_rhs1 (stmt
);
862 i
= gimple_assign_rhs2 (stmt
);
864 update_complex_assignment (gsi
, r
, i
);
867 else if (rhs
&& TREE_CODE (rhs
) == SSA_NAME
&& !TREE_SIDE_EFFECTS (lhs
))
873 loc
= gimple_location (stmt
);
874 r
= extract_component (gsi
, rhs
, 0, false);
875 i
= extract_component (gsi
, rhs
, 1, false);
877 x
= build1 (REALPART_EXPR
, inner_type
, unshare_expr (lhs
));
878 t
= gimple_build_assign (x
, r
);
879 gimple_set_location (t
, loc
);
880 gsi_insert_before (gsi
, t
, GSI_SAME_STMT
);
882 if (stmt
== gsi_stmt (*gsi
))
884 x
= build1 (IMAGPART_EXPR
, inner_type
, unshare_expr (lhs
));
885 gimple_assign_set_lhs (stmt
, x
);
886 gimple_assign_set_rhs1 (stmt
, i
);
890 x
= build1 (IMAGPART_EXPR
, inner_type
, unshare_expr (lhs
));
891 t
= gimple_build_assign (x
, i
);
892 gimple_set_location (t
, loc
);
893 gsi_insert_before (gsi
, t
, GSI_SAME_STMT
);
895 stmt
= gsi_stmt (*gsi
);
896 gcc_assert (gimple_code (stmt
) == GIMPLE_RETURN
);
897 gimple_return_set_retval (as_a
<greturn
*> (stmt
), lhs
);
904 /* Expand complex addition to scalars:
905 a + b = (ar + br) + i(ai + bi)
906 a - b = (ar - br) + i(ai + bi)
910 expand_complex_addition (gimple_stmt_iterator
*gsi
, tree inner_type
,
911 tree ar
, tree ai
, tree br
, tree bi
,
913 complex_lattice_t al
, complex_lattice_t bl
)
917 switch (PAIR (al
, bl
))
919 case PAIR (ONLY_REAL
, ONLY_REAL
):
920 rr
= gimplify_build2 (gsi
, code
, inner_type
, ar
, br
);
924 case PAIR (ONLY_REAL
, ONLY_IMAG
):
926 if (code
== MINUS_EXPR
)
927 ri
= gimplify_build2 (gsi
, MINUS_EXPR
, inner_type
, ai
, bi
);
932 case PAIR (ONLY_IMAG
, ONLY_REAL
):
933 if (code
== MINUS_EXPR
)
934 rr
= gimplify_build2 (gsi
, MINUS_EXPR
, inner_type
, ar
, br
);
940 case PAIR (ONLY_IMAG
, ONLY_IMAG
):
942 ri
= gimplify_build2 (gsi
, code
, inner_type
, ai
, bi
);
945 case PAIR (VARYING
, ONLY_REAL
):
946 rr
= gimplify_build2 (gsi
, code
, inner_type
, ar
, br
);
950 case PAIR (VARYING
, ONLY_IMAG
):
952 ri
= gimplify_build2 (gsi
, code
, inner_type
, ai
, bi
);
955 case PAIR (ONLY_REAL
, VARYING
):
956 if (code
== MINUS_EXPR
)
958 rr
= gimplify_build2 (gsi
, code
, inner_type
, ar
, br
);
962 case PAIR (ONLY_IMAG
, VARYING
):
963 if (code
== MINUS_EXPR
)
966 ri
= gimplify_build2 (gsi
, code
, inner_type
, ai
, bi
);
969 case PAIR (VARYING
, VARYING
):
971 rr
= gimplify_build2 (gsi
, code
, inner_type
, ar
, br
);
972 ri
= gimplify_build2 (gsi
, code
, inner_type
, ai
, bi
);
979 update_complex_assignment (gsi
, rr
, ri
);
982 /* Expand a complex multiplication or division to a libcall to the c99
983 compliant routines. TYPE is the complex type of the operation.
984 If INPLACE_P replace the statement at GSI with
985 the libcall and return NULL_TREE. Else insert the call, assign its
986 result to an output variable and return that variable. If INPLACE_P
987 is true then the statement being replaced should be an assignment
991 expand_complex_libcall (gimple_stmt_iterator
*gsi
, tree type
, tree ar
, tree ai
,
992 tree br
, tree bi
, enum tree_code code
, bool inplace_p
)
995 enum built_in_function bcode
;
999 mode
= TYPE_MODE (type
);
1000 gcc_assert (GET_MODE_CLASS (mode
) == MODE_COMPLEX_FLOAT
);
1002 if (code
== MULT_EXPR
)
1003 bcode
= ((enum built_in_function
)
1004 (BUILT_IN_COMPLEX_MUL_MIN
+ mode
- MIN_MODE_COMPLEX_FLOAT
));
1005 else if (code
== RDIV_EXPR
)
1006 bcode
= ((enum built_in_function
)
1007 (BUILT_IN_COMPLEX_DIV_MIN
+ mode
- MIN_MODE_COMPLEX_FLOAT
));
1010 fn
= builtin_decl_explicit (bcode
);
1011 stmt
= gimple_build_call (fn
, 4, ar
, ai
, br
, bi
);
1015 gimple
*old_stmt
= gsi_stmt (*gsi
);
1016 gimple_call_set_nothrow (stmt
, !stmt_could_throw_p (cfun
, old_stmt
));
1017 lhs
= gimple_assign_lhs (old_stmt
);
1018 gimple_call_set_lhs (stmt
, lhs
);
1019 gsi_replace (gsi
, stmt
, true);
1021 type
= TREE_TYPE (type
);
1022 if (stmt_can_throw_internal (cfun
, stmt
))
1026 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
1027 if (!(e
->flags
& EDGE_EH
))
1029 basic_block bb
= split_edge (e
);
1030 gimple_stmt_iterator gsi2
= gsi_start_bb (bb
);
1031 update_complex_components (&gsi2
, stmt
,
1032 build1 (REALPART_EXPR
, type
, lhs
),
1033 build1 (IMAGPART_EXPR
, type
, lhs
));
1037 update_complex_components (gsi
, stmt
,
1038 build1 (REALPART_EXPR
, type
, lhs
),
1039 build1 (IMAGPART_EXPR
, type
, lhs
));
1040 SSA_NAME_DEF_STMT (lhs
) = stmt
;
1044 gimple_call_set_nothrow (stmt
, true);
1045 lhs
= make_ssa_name (type
);
1046 gimple_call_set_lhs (stmt
, lhs
);
1047 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1052 /* Perform a complex multiplication on two complex constants A, B represented
1053 by AR, AI, BR, BI of type TYPE.
1054 The operation we want is: a * b = (ar*br - ai*bi) + i(ar*bi + br*ai).
1055 Insert the GIMPLE statements into GSI. Store the real and imaginary
1056 components of the result into RR and RI. */
1059 expand_complex_multiplication_components (gimple_stmt_iterator
*gsi
,
1060 tree type
, tree ar
, tree ai
,
1064 tree t1
, t2
, t3
, t4
;
1066 t1
= gimplify_build2 (gsi
, MULT_EXPR
, type
, ar
, br
);
1067 t2
= gimplify_build2 (gsi
, MULT_EXPR
, type
, ai
, bi
);
1068 t3
= gimplify_build2 (gsi
, MULT_EXPR
, type
, ar
, bi
);
1070 /* Avoid expanding redundant multiplication for the common
1071 case of squaring a complex number. */
1072 if (ar
== br
&& ai
== bi
)
1075 t4
= gimplify_build2 (gsi
, MULT_EXPR
, type
, ai
, br
);
1077 *rr
= gimplify_build2 (gsi
, MINUS_EXPR
, type
, t1
, t2
);
1078 *ri
= gimplify_build2 (gsi
, PLUS_EXPR
, type
, t3
, t4
);
1081 /* Expand complex multiplication to scalars:
1082 a * b = (ar*br - ai*bi) + i(ar*bi + br*ai)
1086 expand_complex_multiplication (gimple_stmt_iterator
*gsi
, tree type
,
1087 tree ar
, tree ai
, tree br
, tree bi
,
1088 complex_lattice_t al
, complex_lattice_t bl
)
1091 tree inner_type
= TREE_TYPE (type
);
1095 complex_lattice_t tl
;
1096 rr
= ar
, ar
= br
, br
= rr
;
1097 ri
= ai
, ai
= bi
, bi
= ri
;
1098 tl
= al
, al
= bl
, bl
= tl
;
1101 switch (PAIR (al
, bl
))
1103 case PAIR (ONLY_REAL
, ONLY_REAL
):
1104 rr
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, ar
, br
);
1108 case PAIR (ONLY_IMAG
, ONLY_REAL
):
1110 if (TREE_CODE (ai
) == REAL_CST
1111 && real_identical (&TREE_REAL_CST (ai
), &dconst1
))
1114 ri
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, ai
, br
);
1117 case PAIR (ONLY_IMAG
, ONLY_IMAG
):
1118 rr
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, ai
, bi
);
1119 rr
= gimplify_build1 (gsi
, NEGATE_EXPR
, inner_type
, rr
);
1123 case PAIR (VARYING
, ONLY_REAL
):
1124 rr
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, ar
, br
);
1125 ri
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, ai
, br
);
1128 case PAIR (VARYING
, ONLY_IMAG
):
1129 rr
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, ai
, bi
);
1130 rr
= gimplify_build1 (gsi
, NEGATE_EXPR
, inner_type
, rr
);
1131 ri
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, ar
, bi
);
1134 case PAIR (VARYING
, VARYING
):
1135 if (flag_complex_method
== 2 && SCALAR_FLOAT_TYPE_P (inner_type
))
1137 /* If optimizing for size or not at all just do a libcall.
1138 Same if there are exception-handling edges or signaling NaNs. */
1139 if (optimize
== 0 || optimize_bb_for_size_p (gsi_bb (*gsi
))
1140 || stmt_can_throw_internal (cfun
, gsi_stmt (*gsi
))
1141 || flag_signaling_nans
)
1143 expand_complex_libcall (gsi
, type
, ar
, ai
, br
, bi
,
1148 if (!HONOR_NANS (inner_type
))
1150 /* If we are not worrying about NaNs expand to
1151 (ar*br - ai*bi) + i(ar*bi + br*ai) directly. */
1152 expand_complex_multiplication_components (gsi
, inner_type
,
1158 /* Else, expand x = a * b into
1159 x = (ar*br - ai*bi) + i(ar*bi + br*ai);
1160 if (isunordered (__real__ x, __imag__ x))
1161 x = __muldc3 (a, b); */
1164 expand_complex_multiplication_components (gsi
, inner_type
, ar
, ai
,
1165 br
, bi
, &tmpr
, &tmpi
);
1168 = gimple_build_cond (UNORDERED_EXPR
, tmpr
, tmpi
,
1169 NULL_TREE
, NULL_TREE
);
1171 basic_block orig_bb
= gsi_bb (*gsi
);
1172 /* We want to keep track of the original complex multiplication
1173 statement as we're going to modify it later in
1174 update_complex_assignment. Make sure that insert_cond_bb leaves
1175 that statement in the join block. */
1178 = insert_cond_bb (gsi_bb (*gsi
), gsi_stmt (*gsi
), check
,
1179 profile_probability::very_unlikely ());
1181 gimple_stmt_iterator cond_bb_gsi
= gsi_last_bb (cond_bb
);
1182 gsi_insert_after (&cond_bb_gsi
, gimple_build_nop (), GSI_NEW_STMT
);
1185 = expand_complex_libcall (&cond_bb_gsi
, type
, ar
, ai
, br
,
1186 bi
, MULT_EXPR
, false);
1187 tree cond_real
= gimplify_build1 (&cond_bb_gsi
, REALPART_EXPR
,
1188 inner_type
, libcall_res
);
1189 tree cond_imag
= gimplify_build1 (&cond_bb_gsi
, IMAGPART_EXPR
,
1190 inner_type
, libcall_res
);
1192 basic_block join_bb
= single_succ_edge (cond_bb
)->dest
;
1193 *gsi
= gsi_start_nondebug_after_labels_bb (join_bb
);
1195 /* We have a conditional block with some assignments in cond_bb.
1196 Wire up the PHIs to wrap up. */
1197 rr
= make_ssa_name (inner_type
);
1198 ri
= make_ssa_name (inner_type
);
1199 edge cond_to_join
= single_succ_edge (cond_bb
);
1200 edge orig_to_join
= find_edge (orig_bb
, join_bb
);
1202 gphi
*real_phi
= create_phi_node (rr
, gsi_bb (*gsi
));
1203 add_phi_arg (real_phi
, cond_real
, cond_to_join
, UNKNOWN_LOCATION
);
1204 add_phi_arg (real_phi
, tmpr
, orig_to_join
, UNKNOWN_LOCATION
);
1206 gphi
*imag_phi
= create_phi_node (ri
, gsi_bb (*gsi
));
1207 add_phi_arg (imag_phi
, cond_imag
, cond_to_join
, UNKNOWN_LOCATION
);
1208 add_phi_arg (imag_phi
, tmpi
, orig_to_join
, UNKNOWN_LOCATION
);
1211 /* If we are not worrying about NaNs expand to
1212 (ar*br - ai*bi) + i(ar*bi + br*ai) directly. */
1213 expand_complex_multiplication_components (gsi
, inner_type
, ar
, ai
,
1221 update_complex_assignment (gsi
, rr
, ri
);
1224 /* Keep this algorithm in sync with fold-const.c:const_binop().
1226 Expand complex division to scalars, straightforward algorithm.
1227 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1232 expand_complex_div_straight (gimple_stmt_iterator
*gsi
, tree inner_type
,
1233 tree ar
, tree ai
, tree br
, tree bi
,
1234 enum tree_code code
)
1236 tree rr
, ri
, div
, t1
, t2
, t3
;
1238 t1
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, br
, br
);
1239 t2
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, bi
, bi
);
1240 div
= gimplify_build2 (gsi
, PLUS_EXPR
, inner_type
, t1
, t2
);
1242 t1
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, ar
, br
);
1243 t2
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, ai
, bi
);
1244 t3
= gimplify_build2 (gsi
, PLUS_EXPR
, inner_type
, t1
, t2
);
1245 rr
= gimplify_build2 (gsi
, code
, inner_type
, t3
, div
);
1247 t1
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, ai
, br
);
1248 t2
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, ar
, bi
);
1249 t3
= gimplify_build2 (gsi
, MINUS_EXPR
, inner_type
, t1
, t2
);
1250 ri
= gimplify_build2 (gsi
, code
, inner_type
, t3
, div
);
1252 update_complex_assignment (gsi
, rr
, ri
);
1255 /* Keep this algorithm in sync with fold-const.c:const_binop().
1257 Expand complex division to scalars, modified algorithm to minimize
1258 overflow with wide input ranges. */
1261 expand_complex_div_wide (gimple_stmt_iterator
*gsi
, tree inner_type
,
1262 tree ar
, tree ai
, tree br
, tree bi
,
1263 enum tree_code code
)
1265 tree rr
, ri
, ratio
, div
, t1
, t2
, tr
, ti
, compare
;
1266 basic_block bb_cond
, bb_true
, bb_false
, bb_join
;
1269 /* Examine |br| < |bi|, and branch. */
1270 t1
= gimplify_build1 (gsi
, ABS_EXPR
, inner_type
, br
);
1271 t2
= gimplify_build1 (gsi
, ABS_EXPR
, inner_type
, bi
);
1272 compare
= fold_build2_loc (gimple_location (gsi_stmt (*gsi
)),
1273 LT_EXPR
, boolean_type_node
, t1
, t2
);
1274 STRIP_NOPS (compare
);
1276 bb_cond
= bb_true
= bb_false
= bb_join
= NULL
;
1277 rr
= ri
= tr
= ti
= NULL
;
1278 if (TREE_CODE (compare
) != INTEGER_CST
)
1284 tmp
= make_ssa_name (boolean_type_node
);
1285 stmt
= gimple_build_assign (tmp
, compare
);
1286 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1288 cond
= fold_build2_loc (gimple_location (stmt
),
1289 EQ_EXPR
, boolean_type_node
, tmp
, boolean_true_node
);
1290 stmt
= gimple_build_cond_from_tree (cond
, NULL_TREE
, NULL_TREE
);
1291 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1293 /* Split the original block, and create the TRUE and FALSE blocks. */
1294 e
= split_block (gsi_bb (*gsi
), stmt
);
1297 bb_true
= create_empty_bb (bb_cond
);
1298 bb_false
= create_empty_bb (bb_true
);
1299 bb_true
->count
= bb_false
->count
1300 = bb_cond
->count
.apply_probability (profile_probability::even ());
1302 /* Wire the blocks together. */
1303 e
->flags
= EDGE_TRUE_VALUE
;
1304 /* TODO: With value profile we could add an historgram to determine real
1306 e
->probability
= profile_probability::even ();
1307 redirect_edge_succ (e
, bb_true
);
1308 edge e2
= make_edge (bb_cond
, bb_false
, EDGE_FALSE_VALUE
);
1309 e2
->probability
= profile_probability::even ();
1310 make_single_succ_edge (bb_true
, bb_join
, EDGE_FALLTHRU
);
1311 make_single_succ_edge (bb_false
, bb_join
, EDGE_FALLTHRU
);
1312 add_bb_to_loop (bb_true
, bb_cond
->loop_father
);
1313 add_bb_to_loop (bb_false
, bb_cond
->loop_father
);
1315 /* Update dominance info. Note that bb_join's data was
1316 updated by split_block. */
1317 if (dom_info_available_p (CDI_DOMINATORS
))
1319 set_immediate_dominator (CDI_DOMINATORS
, bb_true
, bb_cond
);
1320 set_immediate_dominator (CDI_DOMINATORS
, bb_false
, bb_cond
);
1323 rr
= create_tmp_reg (inner_type
);
1324 ri
= create_tmp_reg (inner_type
);
1327 /* In the TRUE branch, we compute
1329 div = (br * ratio) + bi;
1330 tr = (ar * ratio) + ai;
1331 ti = (ai * ratio) - ar;
1334 if (bb_true
|| integer_nonzerop (compare
))
1338 *gsi
= gsi_last_bb (bb_true
);
1339 gsi_insert_after (gsi
, gimple_build_nop (), GSI_NEW_STMT
);
1342 ratio
= gimplify_build2 (gsi
, code
, inner_type
, br
, bi
);
1344 t1
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, br
, ratio
);
1345 div
= gimplify_build2 (gsi
, PLUS_EXPR
, inner_type
, t1
, bi
);
1347 t1
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, ar
, ratio
);
1348 tr
= gimplify_build2 (gsi
, PLUS_EXPR
, inner_type
, t1
, ai
);
1350 t1
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, ai
, ratio
);
1351 ti
= gimplify_build2 (gsi
, MINUS_EXPR
, inner_type
, t1
, ar
);
1353 tr
= gimplify_build2 (gsi
, code
, inner_type
, tr
, div
);
1354 ti
= gimplify_build2 (gsi
, code
, inner_type
, ti
, div
);
1358 stmt
= gimple_build_assign (rr
, tr
);
1359 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1360 stmt
= gimple_build_assign (ri
, ti
);
1361 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1362 gsi_remove (gsi
, true);
1366 /* In the FALSE branch, we compute
1368 divisor = (d * ratio) + c;
1369 tr = (b * ratio) + a;
1370 ti = b - (a * ratio);
1373 if (bb_false
|| integer_zerop (compare
))
1377 *gsi
= gsi_last_bb (bb_false
);
1378 gsi_insert_after (gsi
, gimple_build_nop (), GSI_NEW_STMT
);
1381 ratio
= gimplify_build2 (gsi
, code
, inner_type
, bi
, br
);
1383 t1
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, bi
, ratio
);
1384 div
= gimplify_build2 (gsi
, PLUS_EXPR
, inner_type
, t1
, br
);
1386 t1
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, ai
, ratio
);
1387 tr
= gimplify_build2 (gsi
, PLUS_EXPR
, inner_type
, t1
, ar
);
1389 t1
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, ar
, ratio
);
1390 ti
= gimplify_build2 (gsi
, MINUS_EXPR
, inner_type
, ai
, t1
);
1392 tr
= gimplify_build2 (gsi
, code
, inner_type
, tr
, div
);
1393 ti
= gimplify_build2 (gsi
, code
, inner_type
, ti
, div
);
1397 stmt
= gimple_build_assign (rr
, tr
);
1398 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1399 stmt
= gimple_build_assign (ri
, ti
);
1400 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1401 gsi_remove (gsi
, true);
1406 *gsi
= gsi_start_bb (bb_join
);
1410 update_complex_assignment (gsi
, rr
, ri
);
1413 /* Expand complex division to scalars. */
1416 expand_complex_division (gimple_stmt_iterator
*gsi
, tree type
,
1417 tree ar
, tree ai
, tree br
, tree bi
,
1418 enum tree_code code
,
1419 complex_lattice_t al
, complex_lattice_t bl
)
1423 tree inner_type
= TREE_TYPE (type
);
1424 switch (PAIR (al
, bl
))
1426 case PAIR (ONLY_REAL
, ONLY_REAL
):
1427 rr
= gimplify_build2 (gsi
, code
, inner_type
, ar
, br
);
1431 case PAIR (ONLY_REAL
, ONLY_IMAG
):
1433 ri
= gimplify_build2 (gsi
, code
, inner_type
, ar
, bi
);
1434 ri
= gimplify_build1 (gsi
, NEGATE_EXPR
, inner_type
, ri
);
1437 case PAIR (ONLY_IMAG
, ONLY_REAL
):
1439 ri
= gimplify_build2 (gsi
, code
, inner_type
, ai
, br
);
1442 case PAIR (ONLY_IMAG
, ONLY_IMAG
):
1443 rr
= gimplify_build2 (gsi
, code
, inner_type
, ai
, bi
);
1447 case PAIR (VARYING
, ONLY_REAL
):
1448 rr
= gimplify_build2 (gsi
, code
, inner_type
, ar
, br
);
1449 ri
= gimplify_build2 (gsi
, code
, inner_type
, ai
, br
);
1452 case PAIR (VARYING
, ONLY_IMAG
):
1453 rr
= gimplify_build2 (gsi
, code
, inner_type
, ai
, bi
);
1454 ri
= gimplify_build2 (gsi
, code
, inner_type
, ar
, bi
);
1455 ri
= gimplify_build1 (gsi
, NEGATE_EXPR
, inner_type
, ri
);
1458 case PAIR (ONLY_REAL
, VARYING
):
1459 case PAIR (ONLY_IMAG
, VARYING
):
1460 case PAIR (VARYING
, VARYING
):
1461 switch (flag_complex_method
)
1464 /* straightforward implementation of complex divide acceptable. */
1465 expand_complex_div_straight (gsi
, inner_type
, ar
, ai
, br
, bi
, code
);
1469 if (SCALAR_FLOAT_TYPE_P (inner_type
))
1471 expand_complex_libcall (gsi
, type
, ar
, ai
, br
, bi
, code
, true);
1477 /* wide ranges of inputs must work for complex divide. */
1478 expand_complex_div_wide (gsi
, inner_type
, ar
, ai
, br
, bi
, code
);
1490 update_complex_assignment (gsi
, rr
, ri
);
1493 /* Expand complex negation to scalars:
1498 expand_complex_negation (gimple_stmt_iterator
*gsi
, tree inner_type
,
1503 rr
= gimplify_build1 (gsi
, NEGATE_EXPR
, inner_type
, ar
);
1504 ri
= gimplify_build1 (gsi
, NEGATE_EXPR
, inner_type
, ai
);
1506 update_complex_assignment (gsi
, rr
, ri
);
1509 /* Expand complex conjugate to scalars:
1514 expand_complex_conjugate (gimple_stmt_iterator
*gsi
, tree inner_type
,
1519 ri
= gimplify_build1 (gsi
, NEGATE_EXPR
, inner_type
, ai
);
1521 update_complex_assignment (gsi
, ar
, ri
);
1524 /* Expand complex comparison (EQ or NE only). */
1527 expand_complex_comparison (gimple_stmt_iterator
*gsi
, tree ar
, tree ai
,
1528 tree br
, tree bi
, enum tree_code code
)
1530 tree cr
, ci
, cc
, type
;
1533 cr
= gimplify_build2 (gsi
, code
, boolean_type_node
, ar
, br
);
1534 ci
= gimplify_build2 (gsi
, code
, boolean_type_node
, ai
, bi
);
1535 cc
= gimplify_build2 (gsi
,
1536 (code
== EQ_EXPR
? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
),
1537 boolean_type_node
, cr
, ci
);
1539 stmt
= gsi_stmt (*gsi
);
1541 switch (gimple_code (stmt
))
1545 greturn
*return_stmt
= as_a
<greturn
*> (stmt
);
1546 type
= TREE_TYPE (gimple_return_retval (return_stmt
));
1547 gimple_return_set_retval (return_stmt
, fold_convert (type
, cc
));
1552 type
= TREE_TYPE (gimple_assign_lhs (stmt
));
1553 gimple_assign_set_rhs_from_tree (gsi
, fold_convert (type
, cc
));
1554 stmt
= gsi_stmt (*gsi
);
1559 gcond
*cond_stmt
= as_a
<gcond
*> (stmt
);
1560 gimple_cond_set_code (cond_stmt
, EQ_EXPR
);
1561 gimple_cond_set_lhs (cond_stmt
, cc
);
1562 gimple_cond_set_rhs (cond_stmt
, boolean_true_node
);
1571 if (maybe_clean_eh_stmt (stmt
))
1572 bitmap_set_bit (need_eh_cleanup
, gimple_bb (stmt
)->index
);
1575 /* Expand inline asm that sets some complex SSA_NAMEs. */
1578 expand_complex_asm (gimple_stmt_iterator
*gsi
)
1580 gasm
*stmt
= as_a
<gasm
*> (gsi_stmt (*gsi
));
1583 for (i
= 0; i
< gimple_asm_noutputs (stmt
); ++i
)
1585 tree link
= gimple_asm_output_op (stmt
, i
);
1586 tree op
= TREE_VALUE (link
);
1587 if (TREE_CODE (op
) == SSA_NAME
1588 && TREE_CODE (TREE_TYPE (op
)) == COMPLEX_TYPE
)
1590 tree type
= TREE_TYPE (op
);
1591 tree inner_type
= TREE_TYPE (type
);
1592 tree r
= build1 (REALPART_EXPR
, inner_type
, op
);
1593 tree i
= build1 (IMAGPART_EXPR
, inner_type
, op
);
1594 gimple_seq list
= set_component_ssa_name (op
, false, r
);
1597 gsi_insert_seq_after (gsi
, list
, GSI_CONTINUE_LINKING
);
1599 list
= set_component_ssa_name (op
, true, i
);
1601 gsi_insert_seq_after (gsi
, list
, GSI_CONTINUE_LINKING
);
1606 /* Process one statement. If we identify a complex operation, expand it. */
1609 expand_complex_operations_1 (gimple_stmt_iterator
*gsi
)
1611 gimple
*stmt
= gsi_stmt (*gsi
);
1612 tree type
, inner_type
, lhs
;
1613 tree ac
, ar
, ai
, bc
, br
, bi
;
1614 complex_lattice_t al
, bl
;
1615 enum tree_code code
;
1617 if (gimple_code (stmt
) == GIMPLE_ASM
)
1619 expand_complex_asm (gsi
);
1623 lhs
= gimple_get_lhs (stmt
);
1624 if (!lhs
&& gimple_code (stmt
) != GIMPLE_COND
)
1627 type
= TREE_TYPE (gimple_op (stmt
, 0));
1628 code
= gimple_expr_code (stmt
);
1630 /* Initial filter for operations we handle. */
1636 case TRUNC_DIV_EXPR
:
1638 case FLOOR_DIV_EXPR
:
1639 case ROUND_DIV_EXPR
:
1643 if (TREE_CODE (type
) != COMPLEX_TYPE
)
1645 inner_type
= TREE_TYPE (type
);
1650 /* Note, both GIMPLE_ASSIGN and GIMPLE_COND may have an EQ_EXPR
1651 subcode, so we need to access the operands using gimple_op. */
1652 inner_type
= TREE_TYPE (gimple_op (stmt
, 1));
1653 if (TREE_CODE (inner_type
) != COMPLEX_TYPE
)
1661 /* GIMPLE_COND may also fallthru here, but we do not need to
1662 do anything with it. */
1663 if (gimple_code (stmt
) == GIMPLE_COND
)
1666 if (TREE_CODE (type
) == COMPLEX_TYPE
)
1667 expand_complex_move (gsi
, type
);
1668 else if (is_gimple_assign (stmt
)
1669 && (gimple_assign_rhs_code (stmt
) == REALPART_EXPR
1670 || gimple_assign_rhs_code (stmt
) == IMAGPART_EXPR
)
1671 && TREE_CODE (lhs
) == SSA_NAME
)
1673 rhs
= gimple_assign_rhs1 (stmt
);
1674 rhs
= extract_component (gsi
, TREE_OPERAND (rhs
, 0),
1675 gimple_assign_rhs_code (stmt
)
1678 gimple_assign_set_rhs_from_tree (gsi
, rhs
);
1679 stmt
= gsi_stmt (*gsi
);
1686 /* Extract the components of the two complex values. Make sure and
1687 handle the common case of the same value used twice specially. */
1688 if (is_gimple_assign (stmt
))
1690 ac
= gimple_assign_rhs1 (stmt
);
1691 bc
= (gimple_num_ops (stmt
) > 2) ? gimple_assign_rhs2 (stmt
) : NULL
;
1693 /* GIMPLE_CALL cannot get here. */
1696 ac
= gimple_cond_lhs (stmt
);
1697 bc
= gimple_cond_rhs (stmt
);
1700 ar
= extract_component (gsi
, ac
, false, true);
1701 ai
= extract_component (gsi
, ac
, true, true);
1707 br
= extract_component (gsi
, bc
, 0, true);
1708 bi
= extract_component (gsi
, bc
, 1, true);
1711 br
= bi
= NULL_TREE
;
1713 al
= find_lattice_value (ac
);
1714 if (al
== UNINITIALIZED
)
1717 if (TREE_CODE_CLASS (code
) == tcc_unary
)
1723 bl
= find_lattice_value (bc
);
1724 if (bl
== UNINITIALIZED
)
1732 expand_complex_addition (gsi
, inner_type
, ar
, ai
, br
, bi
, code
, al
, bl
);
1736 expand_complex_multiplication (gsi
, type
, ar
, ai
, br
, bi
, al
, bl
);
1739 case TRUNC_DIV_EXPR
:
1741 case FLOOR_DIV_EXPR
:
1742 case ROUND_DIV_EXPR
:
1744 expand_complex_division (gsi
, type
, ar
, ai
, br
, bi
, code
, al
, bl
);
1748 expand_complex_negation (gsi
, inner_type
, ar
, ai
);
1752 expand_complex_conjugate (gsi
, inner_type
, ar
, ai
);
1757 expand_complex_comparison (gsi
, ar
, ai
, br
, bi
, code
);
1766 /* Entry point for complex operation lowering during optimization. */
1769 tree_lower_complex (void)
1771 gimple_stmt_iterator gsi
;
1776 if (!init_dont_simulate_again ())
1779 complex_lattice_values
.create (num_ssa_names
);
1780 complex_lattice_values
.safe_grow_cleared (num_ssa_names
, true);
1782 init_parameter_lattice_values ();
1783 class complex_propagate complex_propagate
;
1784 complex_propagate
.ssa_propagate ();
1786 need_eh_cleanup
= BITMAP_ALLOC (NULL
);
1788 complex_variable_components
= new int_tree_htab_type (10);
1790 complex_ssa_name_components
.create (2 * num_ssa_names
);
1791 complex_ssa_name_components
.safe_grow_cleared (2 * num_ssa_names
, true);
1793 update_parameter_components ();
1795 rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
1796 n_bbs
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
1797 for (i
= 0; i
< n_bbs
; i
++)
1799 bb
= BASIC_BLOCK_FOR_FN (cfun
, rpo
[i
]);
1802 update_phi_components (bb
);
1803 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1804 expand_complex_operations_1 (&gsi
);
1809 if (!phis_to_revisit
.is_empty ())
1811 unsigned int n
= phis_to_revisit
.length ();
1812 for (unsigned int j
= 0; j
< n
; j
+= 3)
1813 for (unsigned int k
= 0; k
< 2; k
++)
1814 if (gphi
*phi
= phis_to_revisit
[j
+ k
+ 1])
1816 unsigned int m
= gimple_phi_num_args (phi
);
1817 for (unsigned int l
= 0; l
< m
; ++l
)
1819 tree op
= gimple_phi_arg_def (phi
, l
);
1820 if (TREE_CODE (op
) == SSA_NAME
1821 || is_gimple_min_invariant (op
))
1823 tree arg
= gimple_phi_arg_def (phis_to_revisit
[j
], l
);
1824 op
= extract_component (NULL
, arg
, k
> 0, false, false);
1825 SET_PHI_ARG_DEF (phi
, l
, op
);
1828 phis_to_revisit
.release ();
1831 gsi_commit_edge_inserts ();
1834 = gimple_purge_all_dead_eh_edges (need_eh_cleanup
) ? TODO_cleanup_cfg
: 0;
1835 BITMAP_FREE (need_eh_cleanup
);
1837 delete complex_variable_components
;
1838 complex_variable_components
= NULL
;
1839 complex_ssa_name_components
.release ();
1840 complex_lattice_values
.release ();
1846 const pass_data pass_data_lower_complex
=
1848 GIMPLE_PASS
, /* type */
1849 "cplxlower", /* name */
1850 OPTGROUP_NONE
, /* optinfo_flags */
1851 TV_NONE
, /* tv_id */
1852 PROP_ssa
, /* properties_required */
1853 PROP_gimple_lcx
, /* properties_provided */
1854 0, /* properties_destroyed */
1855 0, /* todo_flags_start */
1856 TODO_update_ssa
, /* todo_flags_finish */
1859 class pass_lower_complex
: public gimple_opt_pass
1862 pass_lower_complex (gcc::context
*ctxt
)
1863 : gimple_opt_pass (pass_data_lower_complex
, ctxt
)
1866 /* opt_pass methods: */
1867 opt_pass
* clone () { return new pass_lower_complex (m_ctxt
); }
1868 virtual unsigned int execute (function
*) { return tree_lower_complex (); }
1870 }; // class pass_lower_complex
1875 make_pass_lower_complex (gcc::context
*ctxt
)
1877 return new pass_lower_complex (ctxt
);
1883 const pass_data pass_data_lower_complex_O0
=
1885 GIMPLE_PASS
, /* type */
1886 "cplxlower0", /* name */
1887 OPTGROUP_NONE
, /* optinfo_flags */
1888 TV_NONE
, /* tv_id */
1889 PROP_cfg
, /* properties_required */
1890 PROP_gimple_lcx
, /* properties_provided */
1891 0, /* properties_destroyed */
1892 0, /* todo_flags_start */
1893 TODO_update_ssa
, /* todo_flags_finish */
1896 class pass_lower_complex_O0
: public gimple_opt_pass
1899 pass_lower_complex_O0 (gcc::context
*ctxt
)
1900 : gimple_opt_pass (pass_data_lower_complex_O0
, ctxt
)
1903 /* opt_pass methods: */
1904 virtual bool gate (function
*fun
)
1906 /* With errors, normal optimization passes are not run. If we don't
1907 lower complex operations at all, rtl expansion will abort. */
1908 return !(fun
->curr_properties
& PROP_gimple_lcx
);
1911 virtual unsigned int execute (function
*) { return tree_lower_complex (); }
1913 }; // class pass_lower_complex_O0
1918 make_pass_lower_complex_O0 (gcc::context
*ctxt
)
1920 return new pass_lower_complex_O0 (ctxt
);