1 /* Lower complex number operations to scalar operations.
2 Copyright (C) 2004-2017 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 /* Lookup UID in the complex_variable_components hashtable and return the
86 cvc_lookup (unsigned int uid
)
88 struct int_tree_map in
;
90 return complex_variable_components
->find_with_hash (in
, uid
).to
;
93 /* Insert the pair UID, TO into the complex_variable_components hashtable. */
96 cvc_insert (unsigned int uid
, tree to
)
102 loc
= complex_variable_components
->find_slot_with_hash (h
, uid
, INSERT
);
107 /* Return true if T is not a zero constant. In the case of real values,
108 we're only interested in +0.0. */
111 some_nonzerop (tree t
)
115 /* Operations with real or imaginary part of a complex number zero
116 cannot be treated the same as operations with a real or imaginary
117 operand if we care about the signs of zeros in the result. */
118 if (TREE_CODE (t
) == REAL_CST
&& !flag_signed_zeros
)
119 zerop
= real_identical (&TREE_REAL_CST (t
), &dconst0
);
120 else if (TREE_CODE (t
) == FIXED_CST
)
121 zerop
= fixed_zerop (t
);
122 else if (TREE_CODE (t
) == INTEGER_CST
)
123 zerop
= integer_zerop (t
);
129 /* Compute a lattice value from the components of a complex type REAL
132 static complex_lattice_t
133 find_lattice_value_parts (tree real
, tree imag
)
136 complex_lattice_t ret
;
138 r
= some_nonzerop (real
);
139 i
= some_nonzerop (imag
);
140 ret
= r
* ONLY_REAL
+ i
* ONLY_IMAG
;
142 /* ??? On occasion we could do better than mapping 0+0i to real, but we
143 certainly don't want to leave it UNINITIALIZED, which eventually gets
144 mapped to VARYING. */
145 if (ret
== UNINITIALIZED
)
152 /* Compute a lattice value from gimple_val T. */
154 static complex_lattice_t
155 find_lattice_value (tree t
)
159 switch (TREE_CODE (t
))
162 return complex_lattice_values
[SSA_NAME_VERSION (t
)];
165 real
= TREE_REALPART (t
);
166 imag
= TREE_IMAGPART (t
);
173 return find_lattice_value_parts (real
, imag
);
176 /* Determine if LHS is something for which we're interested in seeing
177 simulation results. */
180 is_complex_reg (tree lhs
)
182 return TREE_CODE (TREE_TYPE (lhs
)) == COMPLEX_TYPE
&& is_gimple_reg (lhs
);
185 /* Mark the incoming parameters to the function as VARYING. */
188 init_parameter_lattice_values (void)
192 for (parm
= DECL_ARGUMENTS (cfun
->decl
); parm
; parm
= DECL_CHAIN (parm
))
193 if (is_complex_reg (parm
)
194 && (ssa_name
= ssa_default_def (cfun
, parm
)) != NULL_TREE
)
195 complex_lattice_values
[SSA_NAME_VERSION (ssa_name
)] = VARYING
;
198 /* Initialize simulation state for each statement. Return false if we
199 found no statements we want to simulate, and thus there's nothing
200 for the entire pass to do. */
203 init_dont_simulate_again (void)
206 bool saw_a_complex_op
= false;
208 FOR_EACH_BB_FN (bb
, cfun
)
210 for (gphi_iterator gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
);
213 gphi
*phi
= gsi
.phi ();
214 prop_set_simulate_again (phi
,
215 is_complex_reg (gimple_phi_result (phi
)));
218 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);
225 stmt
= gsi_stmt (gsi
);
226 op0
= op1
= NULL_TREE
;
228 /* Most control-altering statements must be initially
229 simulated, else we won't cover the entire cfg. */
230 sim_again_p
= stmt_ends_bb_p (stmt
);
232 switch (gimple_code (stmt
))
235 if (gimple_call_lhs (stmt
))
236 sim_again_p
= is_complex_reg (gimple_call_lhs (stmt
));
240 sim_again_p
= is_complex_reg (gimple_assign_lhs (stmt
));
241 if (gimple_assign_rhs_code (stmt
) == REALPART_EXPR
242 || gimple_assign_rhs_code (stmt
) == IMAGPART_EXPR
)
243 op0
= TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
245 op0
= gimple_assign_rhs1 (stmt
);
246 if (gimple_num_ops (stmt
) > 2)
247 op1
= gimple_assign_rhs2 (stmt
);
251 op0
= gimple_cond_lhs (stmt
);
252 op1
= gimple_cond_rhs (stmt
);
260 switch (gimple_expr_code (stmt
))
272 if (TREE_CODE (TREE_TYPE (op0
)) == COMPLEX_TYPE
273 || TREE_CODE (TREE_TYPE (op1
)) == COMPLEX_TYPE
)
274 saw_a_complex_op
= true;
279 if (TREE_CODE (TREE_TYPE (op0
)) == COMPLEX_TYPE
)
280 saw_a_complex_op
= true;
285 /* The total store transformation performed during
286 gimplification creates such uninitialized loads
287 and we need to lower the statement to be able
289 if (TREE_CODE (op0
) == SSA_NAME
290 && ssa_undefined_value_p (op0
))
291 saw_a_complex_op
= true;
298 prop_set_simulate_again (stmt
, sim_again_p
);
302 return saw_a_complex_op
;
306 /* Evaluate statement STMT against the complex lattice defined above. */
309 complex_propagate::visit_stmt (gimple
*stmt
, edge
*taken_edge_p ATTRIBUTE_UNUSED
,
312 complex_lattice_t new_l
, old_l
, op1_l
, op2_l
;
316 lhs
= gimple_get_lhs (stmt
);
317 /* Skip anything but GIMPLE_ASSIGN and GIMPLE_CALL with a lhs. */
319 return SSA_PROP_VARYING
;
321 /* These conditions should be satisfied due to the initial filter
322 set up in init_dont_simulate_again. */
323 gcc_assert (TREE_CODE (lhs
) == SSA_NAME
);
324 gcc_assert (TREE_CODE (TREE_TYPE (lhs
)) == COMPLEX_TYPE
);
327 ver
= SSA_NAME_VERSION (lhs
);
328 old_l
= complex_lattice_values
[ver
];
330 switch (gimple_expr_code (stmt
))
334 new_l
= find_lattice_value (gimple_assign_rhs1 (stmt
));
338 new_l
= find_lattice_value_parts (gimple_assign_rhs1 (stmt
),
339 gimple_assign_rhs2 (stmt
));
344 op1_l
= find_lattice_value (gimple_assign_rhs1 (stmt
));
345 op2_l
= find_lattice_value (gimple_assign_rhs2 (stmt
));
347 /* We've set up the lattice values such that IOR neatly
349 new_l
= op1_l
| op2_l
;
358 op1_l
= find_lattice_value (gimple_assign_rhs1 (stmt
));
359 op2_l
= find_lattice_value (gimple_assign_rhs2 (stmt
));
361 /* Obviously, if either varies, so does the result. */
362 if (op1_l
== VARYING
|| op2_l
== VARYING
)
364 /* Don't prematurely promote variables if we've not yet seen
366 else if (op1_l
== UNINITIALIZED
)
368 else if (op2_l
== UNINITIALIZED
)
372 /* At this point both numbers have only one component. If the
373 numbers are of opposite kind, the result is imaginary,
374 otherwise the result is real. The add/subtract translates
375 the real/imag from/to 0/1; the ^ performs the comparison. */
376 new_l
= ((op1_l
- ONLY_REAL
) ^ (op2_l
- ONLY_REAL
)) + ONLY_REAL
;
378 /* Don't allow the lattice value to flip-flop indefinitely. */
385 new_l
= find_lattice_value (gimple_assign_rhs1 (stmt
));
393 /* If nothing changed this round, let the propagator know. */
395 return SSA_PROP_NOT_INTERESTING
;
397 complex_lattice_values
[ver
] = new_l
;
398 return new_l
== VARYING
? SSA_PROP_VARYING
: SSA_PROP_INTERESTING
;
401 /* Evaluate a PHI node against the complex lattice defined above. */
404 complex_propagate::visit_phi (gphi
*phi
)
406 complex_lattice_t new_l
, old_l
;
411 lhs
= gimple_phi_result (phi
);
413 /* This condition should be satisfied due to the initial filter
414 set up in init_dont_simulate_again. */
415 gcc_assert (TREE_CODE (TREE_TYPE (lhs
)) == COMPLEX_TYPE
);
417 /* We've set up the lattice values such that IOR neatly models PHI meet. */
418 new_l
= UNINITIALIZED
;
419 for (i
= gimple_phi_num_args (phi
) - 1; i
>= 0; --i
)
420 new_l
|= find_lattice_value (gimple_phi_arg_def (phi
, i
));
422 ver
= SSA_NAME_VERSION (lhs
);
423 old_l
= complex_lattice_values
[ver
];
426 return SSA_PROP_NOT_INTERESTING
;
428 complex_lattice_values
[ver
] = new_l
;
429 return new_l
== VARYING
? SSA_PROP_VARYING
: SSA_PROP_INTERESTING
;
432 /* Create one backing variable for a complex component of ORIG. */
435 create_one_component_var (tree type
, tree orig
, const char *prefix
,
436 const char *suffix
, enum tree_code code
)
438 tree r
= create_tmp_var (type
, prefix
);
440 DECL_SOURCE_LOCATION (r
) = DECL_SOURCE_LOCATION (orig
);
441 DECL_ARTIFICIAL (r
) = 1;
443 if (DECL_NAME (orig
) && !DECL_IGNORED_P (orig
))
445 const char *name
= IDENTIFIER_POINTER (DECL_NAME (orig
));
446 name
= ACONCAT ((name
, suffix
, NULL
));
447 DECL_NAME (r
) = get_identifier (name
);
449 SET_DECL_DEBUG_EXPR (r
, build1 (code
, type
, orig
));
450 DECL_HAS_DEBUG_EXPR_P (r
) = 1;
451 DECL_IGNORED_P (r
) = 0;
452 TREE_NO_WARNING (r
) = TREE_NO_WARNING (orig
);
456 DECL_IGNORED_P (r
) = 1;
457 TREE_NO_WARNING (r
) = 1;
463 /* Retrieve a value for a complex component of VAR. */
466 get_component_var (tree var
, bool imag_p
)
468 size_t decl_index
= DECL_UID (var
) * 2 + imag_p
;
469 tree ret
= cvc_lookup (decl_index
);
473 ret
= create_one_component_var (TREE_TYPE (TREE_TYPE (var
)), var
,
474 imag_p
? "CI" : "CR",
475 imag_p
? "$imag" : "$real",
476 imag_p
? IMAGPART_EXPR
: REALPART_EXPR
);
477 cvc_insert (decl_index
, ret
);
483 /* Retrieve a value for a complex component of SSA_NAME. */
486 get_component_ssa_name (tree ssa_name
, bool imag_p
)
488 complex_lattice_t lattice
= find_lattice_value (ssa_name
);
489 size_t ssa_name_index
;
492 if (lattice
== (imag_p
? ONLY_REAL
: ONLY_IMAG
))
494 tree inner_type
= TREE_TYPE (TREE_TYPE (ssa_name
));
495 if (SCALAR_FLOAT_TYPE_P (inner_type
))
496 return build_real (inner_type
, dconst0
);
498 return build_int_cst (inner_type
, 0);
501 ssa_name_index
= SSA_NAME_VERSION (ssa_name
) * 2 + imag_p
;
502 ret
= complex_ssa_name_components
[ssa_name_index
];
505 if (SSA_NAME_VAR (ssa_name
))
506 ret
= get_component_var (SSA_NAME_VAR (ssa_name
), imag_p
);
508 ret
= TREE_TYPE (TREE_TYPE (ssa_name
));
509 ret
= make_ssa_name (ret
);
511 /* Copy some properties from the original. In particular, whether it
512 is used in an abnormal phi, and whether it's uninitialized. */
513 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ret
)
514 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name
);
515 if (SSA_NAME_IS_DEFAULT_DEF (ssa_name
)
516 && TREE_CODE (SSA_NAME_VAR (ssa_name
)) == VAR_DECL
)
518 SSA_NAME_DEF_STMT (ret
) = SSA_NAME_DEF_STMT (ssa_name
);
519 set_ssa_default_def (cfun
, SSA_NAME_VAR (ret
), ret
);
522 complex_ssa_name_components
[ssa_name_index
] = ret
;
528 /* Set a value for a complex component of SSA_NAME, return a
529 gimple_seq of stuff that needs doing. */
532 set_component_ssa_name (tree ssa_name
, bool imag_p
, tree value
)
534 complex_lattice_t lattice
= find_lattice_value (ssa_name
);
535 size_t ssa_name_index
;
540 /* We know the value must be zero, else there's a bug in our lattice
541 analysis. But the value may well be a variable known to contain
542 zero. We should be safe ignoring it. */
543 if (lattice
== (imag_p
? ONLY_REAL
: ONLY_IMAG
))
546 /* If we've already assigned an SSA_NAME to this component, then this
547 means that our walk of the basic blocks found a use before the set.
548 This is fine. Now we should create an initialization for the value
549 we created earlier. */
550 ssa_name_index
= SSA_NAME_VERSION (ssa_name
) * 2 + imag_p
;
551 comp
= complex_ssa_name_components
[ssa_name_index
];
555 /* If we've nothing assigned, and the value we're given is already stable,
556 then install that as the value for this SSA_NAME. This preemptively
557 copy-propagates the value, which avoids unnecessary memory allocation. */
558 else if (is_gimple_min_invariant (value
)
559 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name
))
561 complex_ssa_name_components
[ssa_name_index
] = value
;
564 else if (TREE_CODE (value
) == SSA_NAME
565 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name
))
567 /* Replace an anonymous base value with the variable from cvc_lookup.
568 This should result in better debug info. */
569 if (SSA_NAME_VAR (ssa_name
)
570 && (!SSA_NAME_VAR (value
) || DECL_IGNORED_P (SSA_NAME_VAR (value
)))
571 && !DECL_IGNORED_P (SSA_NAME_VAR (ssa_name
)))
573 comp
= get_component_var (SSA_NAME_VAR (ssa_name
), imag_p
);
574 replace_ssa_name_symbol (value
, comp
);
577 complex_ssa_name_components
[ssa_name_index
] = value
;
581 /* Finally, we need to stabilize the result by installing the value into
584 comp
= get_component_ssa_name (ssa_name
, imag_p
);
586 /* Do all the work to assign VALUE to COMP. */
588 value
= force_gimple_operand (value
, &list
, false, NULL
);
589 last
= gimple_build_assign (comp
, value
);
590 gimple_seq_add_stmt (&list
, last
);
591 gcc_assert (SSA_NAME_DEF_STMT (comp
) == last
);
596 /* Extract the real or imaginary part of a complex variable or constant.
597 Make sure that it's a proper gimple_val and gimplify it if not.
598 Emit any new code before gsi. */
601 extract_component (gimple_stmt_iterator
*gsi
, tree t
, bool imagpart_p
,
602 bool gimple_p
, bool phiarg_p
= false)
604 switch (TREE_CODE (t
))
607 return imagpart_p
? TREE_IMAGPART (t
) : TREE_REALPART (t
);
614 tree inner_type
= TREE_TYPE (TREE_TYPE (t
));
615 t
= unshare_expr (t
);
616 TREE_TYPE (t
) = inner_type
;
617 TREE_OPERAND (t
, 1) = TYPE_SIZE (inner_type
);
619 TREE_OPERAND (t
, 2) = size_binop (PLUS_EXPR
, TREE_OPERAND (t
, 2),
620 TYPE_SIZE (inner_type
));
622 t
= force_gimple_operand_gsi (gsi
, t
, true, NULL
, true,
632 case VIEW_CONVERT_EXPR
:
635 tree inner_type
= TREE_TYPE (TREE_TYPE (t
));
637 t
= build1 ((imagpart_p
? IMAGPART_EXPR
: REALPART_EXPR
),
638 inner_type
, unshare_expr (t
));
641 t
= force_gimple_operand_gsi (gsi
, t
, true, NULL
, true,
648 t
= get_component_ssa_name (t
, imagpart_p
);
649 if (TREE_CODE (t
) == SSA_NAME
&& SSA_NAME_DEF_STMT (t
) == NULL
)
650 gcc_assert (phiarg_p
);
658 /* Update the complex components of the ssa name on the lhs of STMT. */
661 update_complex_components (gimple_stmt_iterator
*gsi
, gimple
*stmt
, tree r
,
667 lhs
= gimple_get_lhs (stmt
);
669 list
= set_component_ssa_name (lhs
, false, r
);
671 gsi_insert_seq_after (gsi
, list
, GSI_CONTINUE_LINKING
);
673 list
= set_component_ssa_name (lhs
, true, i
);
675 gsi_insert_seq_after (gsi
, list
, GSI_CONTINUE_LINKING
);
679 update_complex_components_on_edge (edge e
, tree lhs
, tree r
, tree i
)
683 list
= set_component_ssa_name (lhs
, false, r
);
685 gsi_insert_seq_on_edge (e
, list
);
687 list
= set_component_ssa_name (lhs
, true, i
);
689 gsi_insert_seq_on_edge (e
, list
);
693 /* Update an assignment to a complex variable in place. */
696 update_complex_assignment (gimple_stmt_iterator
*gsi
, tree r
, tree i
)
700 gimple_assign_set_rhs_with_ops (gsi
, COMPLEX_EXPR
, r
, i
);
701 stmt
= gsi_stmt (*gsi
);
703 if (maybe_clean_eh_stmt (stmt
))
704 gimple_purge_dead_eh_edges (gimple_bb (stmt
));
706 if (gimple_in_ssa_p (cfun
))
707 update_complex_components (gsi
, gsi_stmt (*gsi
), r
, i
);
711 /* Generate code at the entry point of the function to initialize the
712 component variables for a complex parameter. */
715 update_parameter_components (void)
717 edge entry_edge
= single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
720 for (parm
= DECL_ARGUMENTS (cfun
->decl
); parm
; parm
= DECL_CHAIN (parm
))
722 tree type
= TREE_TYPE (parm
);
725 if (TREE_CODE (type
) != COMPLEX_TYPE
|| !is_gimple_reg (parm
))
728 type
= TREE_TYPE (type
);
729 ssa_name
= ssa_default_def (cfun
, parm
);
733 r
= build1 (REALPART_EXPR
, type
, ssa_name
);
734 i
= build1 (IMAGPART_EXPR
, type
, ssa_name
);
735 update_complex_components_on_edge (entry_edge
, ssa_name
, r
, i
);
739 /* Generate code to set the component variables of a complex variable
740 to match the PHI statements in block BB. */
743 update_phi_components (basic_block bb
)
747 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
749 gphi
*phi
= gsi
.phi ();
751 if (is_complex_reg (gimple_phi_result (phi
)))
753 gphi
*p
[2] = { NULL
, NULL
};
754 unsigned int i
, j
, n
;
755 bool revisit_phi
= false;
757 for (j
= 0; j
< 2; j
++)
759 tree l
= get_component_ssa_name (gimple_phi_result (phi
), j
> 0);
760 if (TREE_CODE (l
) == SSA_NAME
)
761 p
[j
] = create_phi_node (l
, bb
);
764 for (i
= 0, n
= gimple_phi_num_args (phi
); i
< n
; ++i
)
766 tree comp
, arg
= gimple_phi_arg_def (phi
, i
);
767 for (j
= 0; j
< 2; j
++)
770 comp
= extract_component (NULL
, arg
, j
> 0, false, true);
771 if (TREE_CODE (comp
) == SSA_NAME
772 && SSA_NAME_DEF_STMT (comp
) == NULL
)
774 /* For the benefit of any gimple simplification during
775 this pass that might walk SSA_NAME def stmts,
776 don't add SSA_NAMEs without definitions into the
777 PHI arguments, but put a decl in there instead
778 temporarily, and revisit this PHI later on. */
779 if (SSA_NAME_VAR (comp
))
780 comp
= SSA_NAME_VAR (comp
);
782 comp
= create_tmp_reg (TREE_TYPE (comp
),
786 SET_PHI_ARG_DEF (p
[j
], i
, comp
);
792 phis_to_revisit
.safe_push (phi
);
793 phis_to_revisit
.safe_push (p
[0]);
794 phis_to_revisit
.safe_push (p
[1]);
800 /* Expand a complex move to scalars. */
803 expand_complex_move (gimple_stmt_iterator
*gsi
, tree type
)
805 tree inner_type
= TREE_TYPE (type
);
807 gimple
*stmt
= gsi_stmt (*gsi
);
809 if (is_gimple_assign (stmt
))
811 lhs
= gimple_assign_lhs (stmt
);
812 if (gimple_num_ops (stmt
) == 2)
813 rhs
= gimple_assign_rhs1 (stmt
);
817 else if (is_gimple_call (stmt
))
819 lhs
= gimple_call_lhs (stmt
);
825 if (TREE_CODE (lhs
) == SSA_NAME
)
827 if (is_ctrl_altering_stmt (stmt
))
831 /* The value is not assigned on the exception edges, so we need not
832 concern ourselves there. We do need to update on the fallthru
834 e
= find_fallthru_edge (gsi_bb (*gsi
)->succs
);
838 r
= build1 (REALPART_EXPR
, inner_type
, lhs
);
839 i
= build1 (IMAGPART_EXPR
, inner_type
, lhs
);
840 update_complex_components_on_edge (e
, lhs
, r
, i
);
842 else if (is_gimple_call (stmt
)
843 || gimple_has_side_effects (stmt
)
844 || gimple_assign_rhs_code (stmt
) == PAREN_EXPR
)
846 r
= build1 (REALPART_EXPR
, inner_type
, lhs
);
847 i
= build1 (IMAGPART_EXPR
, inner_type
, lhs
);
848 update_complex_components (gsi
, stmt
, r
, i
);
852 if (gimple_assign_rhs_code (stmt
) != COMPLEX_EXPR
)
854 r
= extract_component (gsi
, rhs
, 0, true);
855 i
= extract_component (gsi
, rhs
, 1, true);
859 r
= gimple_assign_rhs1 (stmt
);
860 i
= gimple_assign_rhs2 (stmt
);
862 update_complex_assignment (gsi
, r
, i
);
865 else if (rhs
&& TREE_CODE (rhs
) == SSA_NAME
&& !TREE_SIDE_EFFECTS (lhs
))
871 loc
= gimple_location (stmt
);
872 r
= extract_component (gsi
, rhs
, 0, false);
873 i
= extract_component (gsi
, rhs
, 1, false);
875 x
= build1 (REALPART_EXPR
, inner_type
, unshare_expr (lhs
));
876 t
= gimple_build_assign (x
, r
);
877 gimple_set_location (t
, loc
);
878 gsi_insert_before (gsi
, t
, GSI_SAME_STMT
);
880 if (stmt
== gsi_stmt (*gsi
))
882 x
= build1 (IMAGPART_EXPR
, inner_type
, unshare_expr (lhs
));
883 gimple_assign_set_lhs (stmt
, x
);
884 gimple_assign_set_rhs1 (stmt
, i
);
888 x
= build1 (IMAGPART_EXPR
, inner_type
, unshare_expr (lhs
));
889 t
= gimple_build_assign (x
, i
);
890 gimple_set_location (t
, loc
);
891 gsi_insert_before (gsi
, t
, GSI_SAME_STMT
);
893 stmt
= gsi_stmt (*gsi
);
894 gcc_assert (gimple_code (stmt
) == GIMPLE_RETURN
);
895 gimple_return_set_retval (as_a
<greturn
*> (stmt
), lhs
);
902 /* Expand complex addition to scalars:
903 a + b = (ar + br) + i(ai + bi)
904 a - b = (ar - br) + i(ai + bi)
908 expand_complex_addition (gimple_stmt_iterator
*gsi
, tree inner_type
,
909 tree ar
, tree ai
, tree br
, tree bi
,
911 complex_lattice_t al
, complex_lattice_t bl
)
915 switch (PAIR (al
, bl
))
917 case PAIR (ONLY_REAL
, ONLY_REAL
):
918 rr
= gimplify_build2 (gsi
, code
, inner_type
, ar
, br
);
922 case PAIR (ONLY_REAL
, ONLY_IMAG
):
924 if (code
== MINUS_EXPR
)
925 ri
= gimplify_build2 (gsi
, MINUS_EXPR
, inner_type
, ai
, bi
);
930 case PAIR (ONLY_IMAG
, ONLY_REAL
):
931 if (code
== MINUS_EXPR
)
932 rr
= gimplify_build2 (gsi
, MINUS_EXPR
, inner_type
, ar
, br
);
938 case PAIR (ONLY_IMAG
, ONLY_IMAG
):
940 ri
= gimplify_build2 (gsi
, code
, inner_type
, ai
, bi
);
943 case PAIR (VARYING
, ONLY_REAL
):
944 rr
= gimplify_build2 (gsi
, code
, inner_type
, ar
, br
);
948 case PAIR (VARYING
, ONLY_IMAG
):
950 ri
= gimplify_build2 (gsi
, code
, inner_type
, ai
, bi
);
953 case PAIR (ONLY_REAL
, VARYING
):
954 if (code
== MINUS_EXPR
)
956 rr
= gimplify_build2 (gsi
, code
, inner_type
, ar
, br
);
960 case PAIR (ONLY_IMAG
, VARYING
):
961 if (code
== MINUS_EXPR
)
964 ri
= gimplify_build2 (gsi
, code
, inner_type
, ai
, bi
);
967 case PAIR (VARYING
, VARYING
):
969 rr
= gimplify_build2 (gsi
, code
, inner_type
, ar
, br
);
970 ri
= gimplify_build2 (gsi
, code
, inner_type
, ai
, bi
);
977 update_complex_assignment (gsi
, rr
, ri
);
980 /* Expand a complex multiplication or division to a libcall to the c99
981 compliant routines. */
984 expand_complex_libcall (gimple_stmt_iterator
*gsi
, tree ar
, tree ai
,
985 tree br
, tree bi
, enum tree_code code
)
988 enum built_in_function bcode
;
993 old_stmt
= gsi_stmt (*gsi
);
994 lhs
= gimple_assign_lhs (old_stmt
);
995 type
= TREE_TYPE (lhs
);
997 mode
= TYPE_MODE (type
);
998 gcc_assert (GET_MODE_CLASS (mode
) == MODE_COMPLEX_FLOAT
);
1000 if (code
== MULT_EXPR
)
1001 bcode
= ((enum built_in_function
)
1002 (BUILT_IN_COMPLEX_MUL_MIN
+ mode
- MIN_MODE_COMPLEX_FLOAT
));
1003 else if (code
== RDIV_EXPR
)
1004 bcode
= ((enum built_in_function
)
1005 (BUILT_IN_COMPLEX_DIV_MIN
+ mode
- MIN_MODE_COMPLEX_FLOAT
));
1008 fn
= builtin_decl_explicit (bcode
);
1010 stmt
= gimple_build_call (fn
, 4, ar
, ai
, br
, bi
);
1011 gimple_call_set_lhs (stmt
, lhs
);
1013 gsi_replace (gsi
, stmt
, false);
1015 if (maybe_clean_or_replace_eh_stmt (old_stmt
, stmt
))
1016 gimple_purge_dead_eh_edges (gsi_bb (*gsi
));
1018 if (gimple_in_ssa_p (cfun
))
1020 type
= TREE_TYPE (type
);
1021 update_complex_components (gsi
, stmt
,
1022 build1 (REALPART_EXPR
, type
, lhs
),
1023 build1 (IMAGPART_EXPR
, type
, lhs
));
1024 SSA_NAME_DEF_STMT (lhs
) = stmt
;
1028 /* Expand complex multiplication to scalars:
1029 a * b = (ar*br - ai*bi) + i(ar*bi + br*ai)
1033 expand_complex_multiplication (gimple_stmt_iterator
*gsi
, tree inner_type
,
1034 tree ar
, tree ai
, tree br
, tree bi
,
1035 complex_lattice_t al
, complex_lattice_t bl
)
1041 complex_lattice_t tl
;
1042 rr
= ar
, ar
= br
, br
= rr
;
1043 ri
= ai
, ai
= bi
, bi
= ri
;
1044 tl
= al
, al
= bl
, bl
= tl
;
1047 switch (PAIR (al
, bl
))
1049 case PAIR (ONLY_REAL
, ONLY_REAL
):
1050 rr
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, ar
, br
);
1054 case PAIR (ONLY_IMAG
, ONLY_REAL
):
1056 if (TREE_CODE (ai
) == REAL_CST
1057 && real_identical (&TREE_REAL_CST (ai
), &dconst1
))
1060 ri
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, ai
, br
);
1063 case PAIR (ONLY_IMAG
, ONLY_IMAG
):
1064 rr
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, ai
, bi
);
1065 rr
= gimplify_build1 (gsi
, NEGATE_EXPR
, inner_type
, rr
);
1069 case PAIR (VARYING
, ONLY_REAL
):
1070 rr
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, ar
, br
);
1071 ri
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, ai
, br
);
1074 case PAIR (VARYING
, ONLY_IMAG
):
1075 rr
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, ai
, bi
);
1076 rr
= gimplify_build1 (gsi
, NEGATE_EXPR
, inner_type
, rr
);
1077 ri
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, ar
, bi
);
1080 case PAIR (VARYING
, VARYING
):
1081 if (flag_complex_method
== 2 && SCALAR_FLOAT_TYPE_P (inner_type
))
1083 expand_complex_libcall (gsi
, ar
, ai
, br
, bi
, MULT_EXPR
);
1088 tree t1
, t2
, t3
, t4
;
1090 t1
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, ar
, br
);
1091 t2
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, ai
, bi
);
1092 t3
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, ar
, bi
);
1094 /* Avoid expanding redundant multiplication for the common
1095 case of squaring a complex number. */
1096 if (ar
== br
&& ai
== bi
)
1099 t4
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, ai
, br
);
1101 rr
= gimplify_build2 (gsi
, MINUS_EXPR
, inner_type
, t1
, t2
);
1102 ri
= gimplify_build2 (gsi
, PLUS_EXPR
, inner_type
, t3
, t4
);
1110 update_complex_assignment (gsi
, rr
, ri
);
1113 /* Keep this algorithm in sync with fold-const.c:const_binop().
1115 Expand complex division to scalars, straightforward algorithm.
1116 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1121 expand_complex_div_straight (gimple_stmt_iterator
*gsi
, tree inner_type
,
1122 tree ar
, tree ai
, tree br
, tree bi
,
1123 enum tree_code code
)
1125 tree rr
, ri
, div
, t1
, t2
, t3
;
1127 t1
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, br
, br
);
1128 t2
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, bi
, bi
);
1129 div
= gimplify_build2 (gsi
, PLUS_EXPR
, inner_type
, t1
, t2
);
1131 t1
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, ar
, br
);
1132 t2
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, ai
, bi
);
1133 t3
= gimplify_build2 (gsi
, PLUS_EXPR
, inner_type
, t1
, t2
);
1134 rr
= gimplify_build2 (gsi
, code
, inner_type
, t3
, div
);
1136 t1
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, ai
, br
);
1137 t2
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, ar
, bi
);
1138 t3
= gimplify_build2 (gsi
, MINUS_EXPR
, inner_type
, t1
, t2
);
1139 ri
= gimplify_build2 (gsi
, code
, inner_type
, t3
, div
);
1141 update_complex_assignment (gsi
, rr
, ri
);
1144 /* Keep this algorithm in sync with fold-const.c:const_binop().
1146 Expand complex division to scalars, modified algorithm to minimize
1147 overflow with wide input ranges. */
1150 expand_complex_div_wide (gimple_stmt_iterator
*gsi
, tree inner_type
,
1151 tree ar
, tree ai
, tree br
, tree bi
,
1152 enum tree_code code
)
1154 tree rr
, ri
, ratio
, div
, t1
, t2
, tr
, ti
, compare
;
1155 basic_block bb_cond
, bb_true
, bb_false
, bb_join
;
1158 /* Examine |br| < |bi|, and branch. */
1159 t1
= gimplify_build1 (gsi
, ABS_EXPR
, inner_type
, br
);
1160 t2
= gimplify_build1 (gsi
, ABS_EXPR
, inner_type
, bi
);
1161 compare
= fold_build2_loc (gimple_location (gsi_stmt (*gsi
)),
1162 LT_EXPR
, boolean_type_node
, t1
, t2
);
1163 STRIP_NOPS (compare
);
1165 bb_cond
= bb_true
= bb_false
= bb_join
= NULL
;
1166 rr
= ri
= tr
= ti
= NULL
;
1167 if (TREE_CODE (compare
) != INTEGER_CST
)
1173 tmp
= create_tmp_var (boolean_type_node
);
1174 stmt
= gimple_build_assign (tmp
, compare
);
1175 if (gimple_in_ssa_p (cfun
))
1177 tmp
= make_ssa_name (tmp
, stmt
);
1178 gimple_assign_set_lhs (stmt
, tmp
);
1181 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1183 cond
= fold_build2_loc (gimple_location (stmt
),
1184 EQ_EXPR
, boolean_type_node
, tmp
, boolean_true_node
);
1185 stmt
= gimple_build_cond_from_tree (cond
, NULL_TREE
, NULL_TREE
);
1186 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1188 /* Split the original block, and create the TRUE and FALSE blocks. */
1189 e
= split_block (gsi_bb (*gsi
), stmt
);
1192 bb_true
= create_empty_bb (bb_cond
);
1193 bb_false
= create_empty_bb (bb_true
);
1194 bb_true
->count
= bb_false
->count
1195 = bb_cond
->count
.apply_probability (profile_probability::even ());
1197 /* Wire the blocks together. */
1198 e
->flags
= EDGE_TRUE_VALUE
;
1199 /* TODO: With value profile we could add an historgram to determine real
1201 e
->probability
= profile_probability::even ();
1202 redirect_edge_succ (e
, bb_true
);
1203 edge e2
= make_edge (bb_cond
, bb_false
, EDGE_FALSE_VALUE
);
1204 e2
->probability
= profile_probability::even ();
1205 make_single_succ_edge (bb_true
, bb_join
, EDGE_FALLTHRU
);
1206 make_single_succ_edge (bb_false
, bb_join
, EDGE_FALLTHRU
);
1207 add_bb_to_loop (bb_true
, bb_cond
->loop_father
);
1208 add_bb_to_loop (bb_false
, bb_cond
->loop_father
);
1210 /* Update dominance info. Note that bb_join's data was
1211 updated by split_block. */
1212 if (dom_info_available_p (CDI_DOMINATORS
))
1214 set_immediate_dominator (CDI_DOMINATORS
, bb_true
, bb_cond
);
1215 set_immediate_dominator (CDI_DOMINATORS
, bb_false
, bb_cond
);
1218 rr
= create_tmp_reg (inner_type
);
1219 ri
= create_tmp_reg (inner_type
);
1222 /* In the TRUE branch, we compute
1224 div = (br * ratio) + bi;
1225 tr = (ar * ratio) + ai;
1226 ti = (ai * ratio) - ar;
1229 if (bb_true
|| integer_nonzerop (compare
))
1233 *gsi
= gsi_last_bb (bb_true
);
1234 gsi_insert_after (gsi
, gimple_build_nop (), GSI_NEW_STMT
);
1237 ratio
= gimplify_build2 (gsi
, code
, inner_type
, br
, bi
);
1239 t1
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, br
, ratio
);
1240 div
= gimplify_build2 (gsi
, PLUS_EXPR
, inner_type
, t1
, bi
);
1242 t1
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, ar
, ratio
);
1243 tr
= gimplify_build2 (gsi
, PLUS_EXPR
, inner_type
, t1
, ai
);
1245 t1
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, ai
, ratio
);
1246 ti
= gimplify_build2 (gsi
, MINUS_EXPR
, inner_type
, t1
, ar
);
1248 tr
= gimplify_build2 (gsi
, code
, inner_type
, tr
, div
);
1249 ti
= gimplify_build2 (gsi
, code
, inner_type
, ti
, div
);
1253 stmt
= gimple_build_assign (rr
, tr
);
1254 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1255 stmt
= gimple_build_assign (ri
, ti
);
1256 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1257 gsi_remove (gsi
, true);
1261 /* In the FALSE branch, we compute
1263 divisor = (d * ratio) + c;
1264 tr = (b * ratio) + a;
1265 ti = b - (a * ratio);
1268 if (bb_false
|| integer_zerop (compare
))
1272 *gsi
= gsi_last_bb (bb_false
);
1273 gsi_insert_after (gsi
, gimple_build_nop (), GSI_NEW_STMT
);
1276 ratio
= gimplify_build2 (gsi
, code
, inner_type
, bi
, br
);
1278 t1
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, bi
, ratio
);
1279 div
= gimplify_build2 (gsi
, PLUS_EXPR
, inner_type
, t1
, br
);
1281 t1
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, ai
, ratio
);
1282 tr
= gimplify_build2 (gsi
, PLUS_EXPR
, inner_type
, t1
, ar
);
1284 t1
= gimplify_build2 (gsi
, MULT_EXPR
, inner_type
, ar
, ratio
);
1285 ti
= gimplify_build2 (gsi
, MINUS_EXPR
, inner_type
, ai
, t1
);
1287 tr
= gimplify_build2 (gsi
, code
, inner_type
, tr
, div
);
1288 ti
= gimplify_build2 (gsi
, code
, inner_type
, ti
, div
);
1292 stmt
= gimple_build_assign (rr
, tr
);
1293 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1294 stmt
= gimple_build_assign (ri
, ti
);
1295 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1296 gsi_remove (gsi
, true);
1301 *gsi
= gsi_start_bb (bb_join
);
1305 update_complex_assignment (gsi
, rr
, ri
);
1308 /* Expand complex division to scalars. */
1311 expand_complex_division (gimple_stmt_iterator
*gsi
, tree inner_type
,
1312 tree ar
, tree ai
, tree br
, tree bi
,
1313 enum tree_code code
,
1314 complex_lattice_t al
, complex_lattice_t bl
)
1318 switch (PAIR (al
, bl
))
1320 case PAIR (ONLY_REAL
, ONLY_REAL
):
1321 rr
= gimplify_build2 (gsi
, code
, inner_type
, ar
, br
);
1325 case PAIR (ONLY_REAL
, ONLY_IMAG
):
1327 ri
= gimplify_build2 (gsi
, code
, inner_type
, ar
, bi
);
1328 ri
= gimplify_build1 (gsi
, NEGATE_EXPR
, inner_type
, ri
);
1331 case PAIR (ONLY_IMAG
, ONLY_REAL
):
1333 ri
= gimplify_build2 (gsi
, code
, inner_type
, ai
, br
);
1336 case PAIR (ONLY_IMAG
, ONLY_IMAG
):
1337 rr
= gimplify_build2 (gsi
, code
, inner_type
, ai
, bi
);
1341 case PAIR (VARYING
, ONLY_REAL
):
1342 rr
= gimplify_build2 (gsi
, code
, inner_type
, ar
, br
);
1343 ri
= gimplify_build2 (gsi
, code
, inner_type
, ai
, br
);
1346 case PAIR (VARYING
, ONLY_IMAG
):
1347 rr
= gimplify_build2 (gsi
, code
, inner_type
, ai
, bi
);
1348 ri
= gimplify_build2 (gsi
, code
, inner_type
, ar
, bi
);
1349 ri
= gimplify_build1 (gsi
, NEGATE_EXPR
, inner_type
, ri
);
1352 case PAIR (ONLY_REAL
, VARYING
):
1353 case PAIR (ONLY_IMAG
, VARYING
):
1354 case PAIR (VARYING
, VARYING
):
1355 switch (flag_complex_method
)
1358 /* straightforward implementation of complex divide acceptable. */
1359 expand_complex_div_straight (gsi
, inner_type
, ar
, ai
, br
, bi
, code
);
1363 if (SCALAR_FLOAT_TYPE_P (inner_type
))
1365 expand_complex_libcall (gsi
, ar
, ai
, br
, bi
, code
);
1371 /* wide ranges of inputs must work for complex divide. */
1372 expand_complex_div_wide (gsi
, inner_type
, ar
, ai
, br
, bi
, code
);
1384 update_complex_assignment (gsi
, rr
, ri
);
1387 /* Expand complex negation to scalars:
1392 expand_complex_negation (gimple_stmt_iterator
*gsi
, tree inner_type
,
1397 rr
= gimplify_build1 (gsi
, NEGATE_EXPR
, inner_type
, ar
);
1398 ri
= gimplify_build1 (gsi
, NEGATE_EXPR
, inner_type
, ai
);
1400 update_complex_assignment (gsi
, rr
, ri
);
1403 /* Expand complex conjugate to scalars:
1408 expand_complex_conjugate (gimple_stmt_iterator
*gsi
, tree inner_type
,
1413 ri
= gimplify_build1 (gsi
, NEGATE_EXPR
, inner_type
, ai
);
1415 update_complex_assignment (gsi
, ar
, ri
);
1418 /* Expand complex comparison (EQ or NE only). */
1421 expand_complex_comparison (gimple_stmt_iterator
*gsi
, tree ar
, tree ai
,
1422 tree br
, tree bi
, enum tree_code code
)
1424 tree cr
, ci
, cc
, type
;
1427 cr
= gimplify_build2 (gsi
, code
, boolean_type_node
, ar
, br
);
1428 ci
= gimplify_build2 (gsi
, code
, boolean_type_node
, ai
, bi
);
1429 cc
= gimplify_build2 (gsi
,
1430 (code
== EQ_EXPR
? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
),
1431 boolean_type_node
, cr
, ci
);
1433 stmt
= gsi_stmt (*gsi
);
1435 switch (gimple_code (stmt
))
1439 greturn
*return_stmt
= as_a
<greturn
*> (stmt
);
1440 type
= TREE_TYPE (gimple_return_retval (return_stmt
));
1441 gimple_return_set_retval (return_stmt
, fold_convert (type
, cc
));
1446 type
= TREE_TYPE (gimple_assign_lhs (stmt
));
1447 gimple_assign_set_rhs_from_tree (gsi
, fold_convert (type
, cc
));
1448 stmt
= gsi_stmt (*gsi
);
1453 gcond
*cond_stmt
= as_a
<gcond
*> (stmt
);
1454 gimple_cond_set_code (cond_stmt
, EQ_EXPR
);
1455 gimple_cond_set_lhs (cond_stmt
, cc
);
1456 gimple_cond_set_rhs (cond_stmt
, boolean_true_node
);
1467 /* Expand inline asm that sets some complex SSA_NAMEs. */
1470 expand_complex_asm (gimple_stmt_iterator
*gsi
)
1472 gasm
*stmt
= as_a
<gasm
*> (gsi_stmt (*gsi
));
1475 for (i
= 0; i
< gimple_asm_noutputs (stmt
); ++i
)
1477 tree link
= gimple_asm_output_op (stmt
, i
);
1478 tree op
= TREE_VALUE (link
);
1479 if (TREE_CODE (op
) == SSA_NAME
1480 && TREE_CODE (TREE_TYPE (op
)) == COMPLEX_TYPE
)
1482 tree type
= TREE_TYPE (op
);
1483 tree inner_type
= TREE_TYPE (type
);
1484 tree r
= build1 (REALPART_EXPR
, inner_type
, op
);
1485 tree i
= build1 (IMAGPART_EXPR
, inner_type
, op
);
1486 gimple_seq list
= set_component_ssa_name (op
, false, r
);
1489 gsi_insert_seq_after (gsi
, list
, GSI_CONTINUE_LINKING
);
1491 list
= set_component_ssa_name (op
, true, i
);
1493 gsi_insert_seq_after (gsi
, list
, GSI_CONTINUE_LINKING
);
1498 /* Process one statement. If we identify a complex operation, expand it. */
1501 expand_complex_operations_1 (gimple_stmt_iterator
*gsi
)
1503 gimple
*stmt
= gsi_stmt (*gsi
);
1504 tree type
, inner_type
, lhs
;
1505 tree ac
, ar
, ai
, bc
, br
, bi
;
1506 complex_lattice_t al
, bl
;
1507 enum tree_code code
;
1509 if (gimple_code (stmt
) == GIMPLE_ASM
)
1511 expand_complex_asm (gsi
);
1515 lhs
= gimple_get_lhs (stmt
);
1516 if (!lhs
&& gimple_code (stmt
) != GIMPLE_COND
)
1519 type
= TREE_TYPE (gimple_op (stmt
, 0));
1520 code
= gimple_expr_code (stmt
);
1522 /* Initial filter for operations we handle. */
1528 case TRUNC_DIV_EXPR
:
1530 case FLOOR_DIV_EXPR
:
1531 case ROUND_DIV_EXPR
:
1535 if (TREE_CODE (type
) != COMPLEX_TYPE
)
1537 inner_type
= TREE_TYPE (type
);
1542 /* Note, both GIMPLE_ASSIGN and GIMPLE_COND may have an EQ_EXPR
1543 subcode, so we need to access the operands using gimple_op. */
1544 inner_type
= TREE_TYPE (gimple_op (stmt
, 1));
1545 if (TREE_CODE (inner_type
) != COMPLEX_TYPE
)
1553 /* GIMPLE_COND may also fallthru here, but we do not need to
1554 do anything with it. */
1555 if (gimple_code (stmt
) == GIMPLE_COND
)
1558 if (TREE_CODE (type
) == COMPLEX_TYPE
)
1559 expand_complex_move (gsi
, type
);
1560 else if (is_gimple_assign (stmt
)
1561 && (gimple_assign_rhs_code (stmt
) == REALPART_EXPR
1562 || gimple_assign_rhs_code (stmt
) == IMAGPART_EXPR
)
1563 && TREE_CODE (lhs
) == SSA_NAME
)
1565 rhs
= gimple_assign_rhs1 (stmt
);
1566 rhs
= extract_component (gsi
, TREE_OPERAND (rhs
, 0),
1567 gimple_assign_rhs_code (stmt
)
1570 gimple_assign_set_rhs_from_tree (gsi
, rhs
);
1571 stmt
= gsi_stmt (*gsi
);
1578 /* Extract the components of the two complex values. Make sure and
1579 handle the common case of the same value used twice specially. */
1580 if (is_gimple_assign (stmt
))
1582 ac
= gimple_assign_rhs1 (stmt
);
1583 bc
= (gimple_num_ops (stmt
) > 2) ? gimple_assign_rhs2 (stmt
) : NULL
;
1585 /* GIMPLE_CALL can not get here. */
1588 ac
= gimple_cond_lhs (stmt
);
1589 bc
= gimple_cond_rhs (stmt
);
1592 ar
= extract_component (gsi
, ac
, false, true);
1593 ai
= extract_component (gsi
, ac
, true, true);
1599 br
= extract_component (gsi
, bc
, 0, true);
1600 bi
= extract_component (gsi
, bc
, 1, true);
1603 br
= bi
= NULL_TREE
;
1605 if (gimple_in_ssa_p (cfun
))
1607 al
= find_lattice_value (ac
);
1608 if (al
== UNINITIALIZED
)
1611 if (TREE_CODE_CLASS (code
) == tcc_unary
)
1617 bl
= find_lattice_value (bc
);
1618 if (bl
== UNINITIALIZED
)
1629 expand_complex_addition (gsi
, inner_type
, ar
, ai
, br
, bi
, code
, al
, bl
);
1633 expand_complex_multiplication (gsi
, inner_type
, ar
, ai
, br
, bi
, al
, bl
);
1636 case TRUNC_DIV_EXPR
:
1638 case FLOOR_DIV_EXPR
:
1639 case ROUND_DIV_EXPR
:
1641 expand_complex_division (gsi
, inner_type
, ar
, ai
, br
, bi
, code
, al
, bl
);
1645 expand_complex_negation (gsi
, inner_type
, ar
, ai
);
1649 expand_complex_conjugate (gsi
, inner_type
, ar
, ai
);
1654 expand_complex_comparison (gsi
, ar
, ai
, br
, bi
, code
);
1663 /* Entry point for complex operation lowering during optimization. */
1666 tree_lower_complex (void)
1668 gimple_stmt_iterator gsi
;
1673 if (!init_dont_simulate_again ())
1676 complex_lattice_values
.create (num_ssa_names
);
1677 complex_lattice_values
.safe_grow_cleared (num_ssa_names
);
1679 init_parameter_lattice_values ();
1680 class complex_propagate complex_propagate
;
1681 complex_propagate
.ssa_propagate ();
1683 complex_variable_components
= new int_tree_htab_type (10);
1685 complex_ssa_name_components
.create (2 * num_ssa_names
);
1686 complex_ssa_name_components
.safe_grow_cleared (2 * num_ssa_names
);
1688 update_parameter_components ();
1690 rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
1691 n_bbs
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
1692 for (i
= 0; i
< n_bbs
; i
++)
1694 bb
= BASIC_BLOCK_FOR_FN (cfun
, rpo
[i
]);
1695 update_phi_components (bb
);
1696 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1697 expand_complex_operations_1 (&gsi
);
1702 if (!phis_to_revisit
.is_empty ())
1704 unsigned int n
= phis_to_revisit
.length ();
1705 for (unsigned int j
= 0; j
< n
; j
+= 3)
1706 for (unsigned int k
= 0; k
< 2; k
++)
1707 if (gphi
*phi
= phis_to_revisit
[j
+ k
+ 1])
1709 unsigned int m
= gimple_phi_num_args (phi
);
1710 for (unsigned int l
= 0; l
< m
; ++l
)
1712 tree op
= gimple_phi_arg_def (phi
, l
);
1713 if (TREE_CODE (op
) == SSA_NAME
1714 || is_gimple_min_invariant (op
))
1716 tree arg
= gimple_phi_arg_def (phis_to_revisit
[j
], l
);
1717 op
= extract_component (NULL
, arg
, k
> 0, false, false);
1718 SET_PHI_ARG_DEF (phi
, l
, op
);
1721 phis_to_revisit
.release ();
1724 gsi_commit_edge_inserts ();
1726 delete complex_variable_components
;
1727 complex_variable_components
= NULL
;
1728 complex_ssa_name_components
.release ();
1729 complex_lattice_values
.release ();
1735 const pass_data pass_data_lower_complex
=
1737 GIMPLE_PASS
, /* type */
1738 "cplxlower", /* name */
1739 OPTGROUP_NONE
, /* optinfo_flags */
1740 TV_NONE
, /* tv_id */
1741 PROP_ssa
, /* properties_required */
1742 PROP_gimple_lcx
, /* properties_provided */
1743 0, /* properties_destroyed */
1744 0, /* todo_flags_start */
1745 TODO_update_ssa
, /* todo_flags_finish */
1748 class pass_lower_complex
: public gimple_opt_pass
1751 pass_lower_complex (gcc::context
*ctxt
)
1752 : gimple_opt_pass (pass_data_lower_complex
, ctxt
)
1755 /* opt_pass methods: */
1756 opt_pass
* clone () { return new pass_lower_complex (m_ctxt
); }
1757 virtual unsigned int execute (function
*) { return tree_lower_complex (); }
1759 }; // class pass_lower_complex
1764 make_pass_lower_complex (gcc::context
*ctxt
)
1766 return new pass_lower_complex (ctxt
);
1772 const pass_data pass_data_lower_complex_O0
=
1774 GIMPLE_PASS
, /* type */
1775 "cplxlower0", /* name */
1776 OPTGROUP_NONE
, /* optinfo_flags */
1777 TV_NONE
, /* tv_id */
1778 PROP_cfg
, /* properties_required */
1779 PROP_gimple_lcx
, /* properties_provided */
1780 0, /* properties_destroyed */
1781 0, /* todo_flags_start */
1782 TODO_update_ssa
, /* todo_flags_finish */
1785 class pass_lower_complex_O0
: public gimple_opt_pass
1788 pass_lower_complex_O0 (gcc::context
*ctxt
)
1789 : gimple_opt_pass (pass_data_lower_complex_O0
, ctxt
)
1792 /* opt_pass methods: */
1793 virtual bool gate (function
*fun
)
1795 /* With errors, normal optimization passes are not run. If we don't
1796 lower complex operations at all, rtl expansion will abort. */
1797 return !(fun
->curr_properties
& PROP_gimple_lcx
);
1800 virtual unsigned int execute (function
*) { return tree_lower_complex (); }
1802 }; // class pass_lower_complex_O0
1807 make_pass_lower_complex_O0 (gcc::context
*ctxt
)
1809 return new pass_lower_complex_O0 (ctxt
);