1 /* Lower complex number operations to scalar operations.
2 Copyright (C) 2004-2024 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"
43 #include "gimple-fold.h"
44 #include "diagnostic-core.h"
47 /* For each complex ssa name, a lattice value. We're interested in finding
48 out whether a complex number is degenerate in some way, having only real
49 or only complex parts. */
59 /* The type complex_lattice_t holds combinations of the above
61 typedef int complex_lattice_t
;
63 #define PAIR(a, b) ((a) << 2 | (b))
65 class complex_propagate
: public ssa_propagation_engine
67 enum ssa_prop_result
visit_stmt (gimple
*, edge
*, tree
*) final override
;
68 enum ssa_prop_result
visit_phi (gphi
*) final override
;
71 static vec
<complex_lattice_t
> complex_lattice_values
;
73 /* For each complex variable, a pair of variables for the components exists in
75 static int_tree_htab_type
*complex_variable_components
;
77 /* For each complex SSA_NAME, a pair of ssa names for the components. */
78 static vec
<tree
> complex_ssa_name_components
;
80 /* Vector of PHI triplets (original complex PHI and corresponding real and
81 imag PHIs if real and/or imag PHIs contain temporarily
82 non-SSA_NAME/non-invariant args that need to be replaced by SSA_NAMEs. */
83 static vec
<gphi
*> phis_to_revisit
;
85 /* BBs that need EH cleanup. */
86 static bitmap need_eh_cleanup
;
88 /* Lookup UID in the complex_variable_components hashtable and return the
91 cvc_lookup (unsigned int uid
)
93 struct int_tree_map in
;
95 return complex_variable_components
->find_with_hash (in
, uid
).to
;
98 /* Insert the pair UID, TO into the complex_variable_components hashtable. */
101 cvc_insert (unsigned int uid
, tree to
)
107 loc
= complex_variable_components
->find_slot_with_hash (h
, uid
, INSERT
);
112 /* Return true if T is not a zero constant. In the case of real values,
113 we're only interested in +0.0. */
116 some_nonzerop (tree t
)
120 /* Operations with real or imaginary part of a complex number zero
121 cannot be treated the same as operations with a real or imaginary
122 operand if we care about the signs of zeros in the result. */
123 if (TREE_CODE (t
) == REAL_CST
&& !flag_signed_zeros
)
124 zerop
= real_identical (&TREE_REAL_CST (t
), &dconst0
);
125 else if (TREE_CODE (t
) == FIXED_CST
)
126 zerop
= fixed_zerop (t
);
127 else if (TREE_CODE (t
) == INTEGER_CST
)
128 zerop
= integer_zerop (t
);
134 /* Compute a lattice value from the components of a complex type REAL
137 static complex_lattice_t
138 find_lattice_value_parts (tree real
, tree imag
)
141 complex_lattice_t ret
;
143 r
= some_nonzerop (real
);
144 i
= some_nonzerop (imag
);
145 ret
= r
* ONLY_REAL
+ i
* ONLY_IMAG
;
147 /* ??? On occasion we could do better than mapping 0+0i to real, but we
148 certainly don't want to leave it UNINITIALIZED, which eventually gets
149 mapped to VARYING. */
150 if (ret
== UNINITIALIZED
)
157 /* Compute a lattice value from gimple_val T. */
159 static complex_lattice_t
160 find_lattice_value (tree t
)
164 switch (TREE_CODE (t
))
167 return complex_lattice_values
[SSA_NAME_VERSION (t
)];
170 real
= TREE_REALPART (t
);
171 imag
= TREE_IMAGPART (t
);
178 return find_lattice_value_parts (real
, imag
);
181 /* Determine if LHS is something for which we're interested in seeing
182 simulation results. */
185 is_complex_reg (tree lhs
)
187 return TREE_CODE (TREE_TYPE (lhs
)) == COMPLEX_TYPE
&& is_gimple_reg (lhs
);
190 /* Mark the incoming parameters to the function as VARYING. */
193 init_parameter_lattice_values (void)
197 for (parm
= DECL_ARGUMENTS (cfun
->decl
); parm
; parm
= DECL_CHAIN (parm
))
198 if (is_complex_reg (parm
)
199 && (ssa_name
= ssa_default_def (cfun
, parm
)) != NULL_TREE
)
200 complex_lattice_values
[SSA_NAME_VERSION (ssa_name
)] = VARYING
;
203 /* Initialize simulation state for each statement. Return false if we
204 found no statements we want to simulate, and thus there's nothing
205 for the entire pass to do. */
208 init_dont_simulate_again (void)
211 bool saw_a_complex_op
= false;
213 FOR_EACH_BB_FN (bb
, cfun
)
215 for (gphi_iterator gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
);
218 gphi
*phi
= gsi
.phi ();
219 prop_set_simulate_again (phi
,
220 is_complex_reg (gimple_phi_result (phi
)));
223 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);
230 stmt
= gsi_stmt (gsi
);
231 op0
= op1
= NULL_TREE
;
233 /* Most control-altering statements must be initially
234 simulated, else we won't cover the entire cfg. */
235 sim_again_p
= stmt_ends_bb_p (stmt
);
237 switch (gimple_code (stmt
))
240 if (gimple_call_lhs (stmt
))
241 sim_again_p
= is_complex_reg (gimple_call_lhs (stmt
));
245 sim_again_p
= is_complex_reg (gimple_assign_lhs (stmt
));
246 if (gimple_assign_rhs_code (stmt
) == REALPART_EXPR
247 || gimple_assign_rhs_code (stmt
) == IMAGPART_EXPR
)
248 op0
= TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
250 op0
= gimple_assign_rhs1 (stmt
);
251 if (gimple_num_ops (stmt
) > 2)
252 op1
= gimple_assign_rhs2 (stmt
);
256 op0
= gimple_cond_lhs (stmt
);
257 op1
= gimple_cond_rhs (stmt
);
265 switch (gimple_expr_code (stmt
))
277 if (TREE_CODE (TREE_TYPE (op0
)) == COMPLEX_TYPE
278 || TREE_CODE (TREE_TYPE (op1
)) == COMPLEX_TYPE
)
279 saw_a_complex_op
= true;
284 if (TREE_CODE (TREE_TYPE (op0
)) == COMPLEX_TYPE
)
285 saw_a_complex_op
= true;
290 /* The total store transformation performed during
291 gimplification creates such uninitialized loads
292 and we need to lower the statement to be able
294 if (TREE_CODE (op0
) == SSA_NAME
295 && ssa_undefined_value_p (op0
))
296 saw_a_complex_op
= true;
300 /* When expand_complex_move would trigger make sure we
301 perform lowering even when there is no actual complex
302 operation. This helps consistency and vectorization. */
303 if (TREE_CODE (TREE_TYPE (gimple_op (stmt
, 0))) == COMPLEX_TYPE
)
304 saw_a_complex_op
= true;
308 prop_set_simulate_again (stmt
, sim_again_p
);
312 return saw_a_complex_op
;
316 /* Evaluate statement STMT against the complex lattice defined above. */
319 complex_propagate::visit_stmt (gimple
*stmt
, edge
*taken_edge_p ATTRIBUTE_UNUSED
,
322 complex_lattice_t new_l
, old_l
, op1_l
, op2_l
;
326 lhs
= gimple_get_lhs (stmt
);
327 /* Skip anything but GIMPLE_ASSIGN and GIMPLE_CALL with a lhs. */
328 if (!lhs
|| SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs
))
329 return SSA_PROP_VARYING
;
331 /* These conditions should be satisfied due to the initial filter
332 set up in init_dont_simulate_again. */
333 gcc_assert (TREE_CODE (lhs
) == SSA_NAME
);
334 gcc_assert (TREE_CODE (TREE_TYPE (lhs
)) == COMPLEX_TYPE
);
337 ver
= SSA_NAME_VERSION (lhs
);
338 old_l
= complex_lattice_values
[ver
];
340 switch (gimple_expr_code (stmt
))
344 new_l
= find_lattice_value (gimple_assign_rhs1 (stmt
));
348 new_l
= find_lattice_value_parts (gimple_assign_rhs1 (stmt
),
349 gimple_assign_rhs2 (stmt
));
354 op1_l
= find_lattice_value (gimple_assign_rhs1 (stmt
));
355 op2_l
= find_lattice_value (gimple_assign_rhs2 (stmt
));
357 /* We've set up the lattice values such that IOR neatly
359 new_l
= op1_l
| op2_l
;
368 op1_l
= find_lattice_value (gimple_assign_rhs1 (stmt
));
369 op2_l
= find_lattice_value (gimple_assign_rhs2 (stmt
));
371 /* Obviously, if either varies, so does the result. */
372 if (op1_l
== VARYING
|| op2_l
== VARYING
)
374 /* Don't prematurely promote variables if we've not yet seen
376 else if (op1_l
== UNINITIALIZED
)
378 else if (op2_l
== UNINITIALIZED
)
382 /* At this point both numbers have only one component. If the
383 numbers are of opposite kind, the result is imaginary,
384 otherwise the result is real. The add/subtract translates
385 the real/imag from/to 0/1; the ^ performs the comparison. */
386 new_l
= ((op1_l
- ONLY_REAL
) ^ (op2_l
- ONLY_REAL
)) + ONLY_REAL
;
388 /* Don't allow the lattice value to flip-flop indefinitely. */
395 new_l
= find_lattice_value (gimple_assign_rhs1 (stmt
));
403 /* If nothing changed this round, let the propagator know. */
405 return SSA_PROP_NOT_INTERESTING
;
407 complex_lattice_values
[ver
] = new_l
;
408 return new_l
== VARYING
? SSA_PROP_VARYING
: SSA_PROP_INTERESTING
;
411 /* Evaluate a PHI node against the complex lattice defined above. */
414 complex_propagate::visit_phi (gphi
*phi
)
416 complex_lattice_t new_l
, old_l
;
421 lhs
= gimple_phi_result (phi
);
423 /* This condition should be satisfied due to the initial filter
424 set up in init_dont_simulate_again. */
425 gcc_assert (TREE_CODE (TREE_TYPE (lhs
)) == COMPLEX_TYPE
);
427 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs
))
428 return SSA_PROP_VARYING
;
430 /* We've set up the lattice values such that IOR neatly models PHI meet. */
431 new_l
= UNINITIALIZED
;
432 for (i
= gimple_phi_num_args (phi
) - 1; i
>= 0; --i
)
433 new_l
|= find_lattice_value (gimple_phi_arg_def (phi
, i
));
435 ver
= SSA_NAME_VERSION (lhs
);
436 old_l
= complex_lattice_values
[ver
];
439 return SSA_PROP_NOT_INTERESTING
;
441 complex_lattice_values
[ver
] = new_l
;
442 return new_l
== VARYING
? SSA_PROP_VARYING
: SSA_PROP_INTERESTING
;
445 /* Create one backing variable for a complex component of ORIG. */
448 create_one_component_var (tree type
, tree orig
, const char *prefix
,
449 const char *suffix
, enum tree_code code
)
451 tree r
= create_tmp_var (type
, prefix
);
453 DECL_SOURCE_LOCATION (r
) = DECL_SOURCE_LOCATION (orig
);
454 DECL_ARTIFICIAL (r
) = 1;
456 if (DECL_NAME (orig
) && !DECL_IGNORED_P (orig
))
458 const char *name
= IDENTIFIER_POINTER (DECL_NAME (orig
));
459 name
= ACONCAT ((name
, suffix
, NULL
));
460 DECL_NAME (r
) = get_identifier (name
);
462 SET_DECL_DEBUG_EXPR (r
, build1 (code
, type
, orig
));
463 DECL_HAS_DEBUG_EXPR_P (r
) = 1;
464 DECL_IGNORED_P (r
) = 0;
465 copy_warning (r
, orig
);
469 DECL_IGNORED_P (r
) = 1;
470 suppress_warning (r
);
476 /* Retrieve a value for a complex component of VAR. */
479 get_component_var (tree var
, bool imag_p
)
481 size_t decl_index
= DECL_UID (var
) * 2 + imag_p
;
482 tree ret
= cvc_lookup (decl_index
);
486 ret
= create_one_component_var (TREE_TYPE (TREE_TYPE (var
)), var
,
487 imag_p
? "CI" : "CR",
488 imag_p
? "$imag" : "$real",
489 imag_p
? IMAGPART_EXPR
: REALPART_EXPR
);
490 cvc_insert (decl_index
, ret
);
496 /* Retrieve a value for a complex component of SSA_NAME. */
499 get_component_ssa_name (tree ssa_name
, bool imag_p
)
501 complex_lattice_t lattice
= find_lattice_value (ssa_name
);
502 size_t ssa_name_index
;
505 if (lattice
== (imag_p
? ONLY_REAL
: ONLY_IMAG
))
507 tree inner_type
= TREE_TYPE (TREE_TYPE (ssa_name
));
508 if (SCALAR_FLOAT_TYPE_P (inner_type
))
509 return build_real (inner_type
, dconst0
);
511 return build_int_cst (inner_type
, 0);
514 ssa_name_index
= SSA_NAME_VERSION (ssa_name
) * 2 + imag_p
;
515 ret
= complex_ssa_name_components
[ssa_name_index
];
518 if (SSA_NAME_VAR (ssa_name
))
519 ret
= get_component_var (SSA_NAME_VAR (ssa_name
), imag_p
);
521 ret
= TREE_TYPE (TREE_TYPE (ssa_name
));
522 ret
= make_ssa_name (ret
);
524 /* Copy some properties from the original. In particular, whether it
525 is used in an abnormal phi, and whether it's uninitialized. */
526 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ret
)
527 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name
);
528 if (SSA_NAME_IS_DEFAULT_DEF (ssa_name
)
529 && VAR_P (SSA_NAME_VAR (ssa_name
)))
531 SSA_NAME_DEF_STMT (ret
) = SSA_NAME_DEF_STMT (ssa_name
);
532 set_ssa_default_def (cfun
, SSA_NAME_VAR (ret
), ret
);
535 complex_ssa_name_components
[ssa_name_index
] = ret
;
541 /* Set a value for a complex component of SSA_NAME, return a
542 gimple_seq of stuff that needs doing. */
545 set_component_ssa_name (tree ssa_name
, bool imag_p
, tree value
)
547 complex_lattice_t lattice
= find_lattice_value (ssa_name
);
548 size_t ssa_name_index
;
553 /* We know the value must be zero, else there's a bug in our lattice
554 analysis. But the value may well be a variable known to contain
555 zero. We should be safe ignoring it. */
556 if (lattice
== (imag_p
? ONLY_REAL
: ONLY_IMAG
))
559 /* If we've already assigned an SSA_NAME to this component, then this
560 means that our walk of the basic blocks found a use before the set.
561 This is fine. Now we should create an initialization for the value
562 we created earlier. */
563 ssa_name_index
= SSA_NAME_VERSION (ssa_name
) * 2 + imag_p
;
564 comp
= complex_ssa_name_components
[ssa_name_index
];
568 /* If we've nothing assigned, and the value we're given is already stable,
569 then install that as the value for this SSA_NAME. This preemptively
570 copy-propagates the value, which avoids unnecessary memory allocation. */
571 else if (is_gimple_min_invariant (value
)
572 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name
))
574 complex_ssa_name_components
[ssa_name_index
] = value
;
577 else if (TREE_CODE (value
) == SSA_NAME
578 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name
))
580 /* Replace an anonymous base value with the variable from cvc_lookup.
581 This should result in better debug info. */
582 if (!SSA_NAME_IS_DEFAULT_DEF (value
)
583 && SSA_NAME_VAR (ssa_name
)
584 && (!SSA_NAME_VAR (value
) || DECL_IGNORED_P (SSA_NAME_VAR (value
)))
585 && !DECL_IGNORED_P (SSA_NAME_VAR (ssa_name
)))
587 comp
= get_component_var (SSA_NAME_VAR (ssa_name
), imag_p
);
588 replace_ssa_name_symbol (value
, comp
);
591 complex_ssa_name_components
[ssa_name_index
] = value
;
595 /* Finally, we need to stabilize the result by installing the value into
598 comp
= get_component_ssa_name (ssa_name
, imag_p
);
600 /* Do all the work to assign VALUE to COMP. */
602 value
= force_gimple_operand (value
, &list
, false, NULL
);
603 last
= gimple_build_assign (comp
, value
);
604 gimple_seq_add_stmt (&list
, last
);
605 gcc_assert (SSA_NAME_DEF_STMT (comp
) == last
);
610 /* Extract the real or imaginary part of a complex variable or constant.
611 Make sure that it's a proper gimple_val and gimplify it if not.
612 Emit any new code before gsi. */
615 extract_component (gimple_stmt_iterator
*gsi
, tree t
, bool imagpart_p
,
616 bool gimple_p
, bool phiarg_p
= false)
618 switch (TREE_CODE (t
))
621 return imagpart_p
? TREE_IMAGPART (t
) : TREE_REALPART (t
);
628 tree inner_type
= TREE_TYPE (TREE_TYPE (t
));
629 t
= unshare_expr (t
);
630 TREE_TYPE (t
) = inner_type
;
631 TREE_OPERAND (t
, 1) = TYPE_SIZE (inner_type
);
633 TREE_OPERAND (t
, 2) = size_binop (PLUS_EXPR
, TREE_OPERAND (t
, 2),
634 TYPE_SIZE (inner_type
));
636 t
= force_gimple_operand_gsi (gsi
, t
, true, NULL
, true,
646 case VIEW_CONVERT_EXPR
:
649 tree inner_type
= TREE_TYPE (TREE_TYPE (t
));
651 t
= build1 ((imagpart_p
? IMAGPART_EXPR
: REALPART_EXPR
),
652 inner_type
, unshare_expr (t
));
655 t
= force_gimple_operand_gsi (gsi
, t
, true, NULL
, true,
662 t
= get_component_ssa_name (t
, imagpart_p
);
663 if (TREE_CODE (t
) == SSA_NAME
&& SSA_NAME_DEF_STMT (t
) == NULL
)
664 gcc_assert (phiarg_p
);
672 /* Update the complex components of the ssa name on the lhs of STMT. */
675 update_complex_components (gimple_stmt_iterator
*gsi
, gimple
*stmt
, tree r
,
681 lhs
= gimple_get_lhs (stmt
);
683 list
= set_component_ssa_name (lhs
, false, r
);
685 gsi_insert_seq_after (gsi
, list
, GSI_CONTINUE_LINKING
);
687 list
= set_component_ssa_name (lhs
, true, i
);
689 gsi_insert_seq_after (gsi
, list
, GSI_CONTINUE_LINKING
);
693 update_complex_components_on_edge (edge e
, tree lhs
, tree r
, tree i
)
697 list
= set_component_ssa_name (lhs
, false, r
);
699 gsi_insert_seq_on_edge (e
, list
);
701 list
= set_component_ssa_name (lhs
, true, i
);
703 gsi_insert_seq_on_edge (e
, list
);
707 /* Update an assignment to a complex variable in place. */
710 update_complex_assignment (gimple_stmt_iterator
*gsi
, tree r
, tree i
)
712 gimple
*old_stmt
= gsi_stmt (*gsi
);
713 gimple_assign_set_rhs_with_ops (gsi
, COMPLEX_EXPR
, r
, i
);
714 gimple
*stmt
= gsi_stmt (*gsi
);
716 if (maybe_clean_or_replace_eh_stmt (old_stmt
, stmt
))
717 bitmap_set_bit (need_eh_cleanup
, gimple_bb (stmt
)->index
);
719 update_complex_components (gsi
, gsi_stmt (*gsi
), r
, i
);
723 /* Generate code at the entry point of the function to initialize the
724 component variables for a complex parameter. */
727 update_parameter_components (void)
729 edge entry_edge
= single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
732 for (parm
= DECL_ARGUMENTS (cfun
->decl
); parm
; parm
= DECL_CHAIN (parm
))
734 tree type
= TREE_TYPE (parm
);
737 if (TREE_CODE (type
) != COMPLEX_TYPE
|| !is_gimple_reg (parm
))
740 type
= TREE_TYPE (type
);
741 ssa_name
= ssa_default_def (cfun
, parm
);
745 r
= build1 (REALPART_EXPR
, type
, ssa_name
);
746 i
= build1 (IMAGPART_EXPR
, type
, ssa_name
);
747 update_complex_components_on_edge (entry_edge
, ssa_name
, r
, i
);
751 /* Generate code to set the component variables of a complex variable
752 to match the PHI statements in block BB. */
755 update_phi_components (basic_block bb
)
759 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
761 gphi
*phi
= gsi
.phi ();
763 if (is_complex_reg (gimple_phi_result (phi
)))
765 gphi
*p
[2] = { NULL
, NULL
};
766 unsigned int i
, j
, n
;
767 bool revisit_phi
= false;
769 for (j
= 0; j
< 2; j
++)
771 tree l
= get_component_ssa_name (gimple_phi_result (phi
), j
> 0);
772 if (TREE_CODE (l
) == SSA_NAME
)
773 p
[j
] = create_phi_node (l
, bb
);
776 for (i
= 0, n
= gimple_phi_num_args (phi
); i
< n
; ++i
)
778 tree comp
, arg
= gimple_phi_arg_def (phi
, i
);
779 for (j
= 0; j
< 2; j
++)
782 comp
= extract_component (NULL
, arg
, j
> 0, false, true);
783 if (TREE_CODE (comp
) == SSA_NAME
784 && SSA_NAME_DEF_STMT (comp
) == NULL
)
786 /* For the benefit of any gimple simplification during
787 this pass that might walk SSA_NAME def stmts,
788 don't add SSA_NAMEs without definitions into the
789 PHI arguments, but put a decl in there instead
790 temporarily, and revisit this PHI later on. */
791 if (SSA_NAME_VAR (comp
))
792 comp
= SSA_NAME_VAR (comp
);
794 comp
= create_tmp_reg (TREE_TYPE (comp
),
798 SET_PHI_ARG_DEF (p
[j
], i
, comp
);
804 phis_to_revisit
.safe_push (phi
);
805 phis_to_revisit
.safe_push (p
[0]);
806 phis_to_revisit
.safe_push (p
[1]);
812 /* Expand a complex move to scalars. */
815 expand_complex_move (gimple_stmt_iterator
*gsi
, tree type
)
817 tree inner_type
= TREE_TYPE (type
);
819 gimple
*stmt
= gsi_stmt (*gsi
);
821 if (is_gimple_assign (stmt
))
823 lhs
= gimple_assign_lhs (stmt
);
824 if (gimple_num_ops (stmt
) == 2)
825 rhs
= gimple_assign_rhs1 (stmt
);
829 else if (is_gimple_call (stmt
))
831 lhs
= gimple_call_lhs (stmt
);
837 if (TREE_CODE (lhs
) == SSA_NAME
)
839 if (is_ctrl_altering_stmt (stmt
))
843 /* The value is not assigned on the exception edges, so we need not
844 concern ourselves there. We do need to update on the fallthru
846 e
= find_fallthru_edge (gsi_bb (*gsi
)->succs
);
850 r
= build1 (REALPART_EXPR
, inner_type
, lhs
);
851 i
= build1 (IMAGPART_EXPR
, inner_type
, lhs
);
852 update_complex_components_on_edge (e
, lhs
, r
, i
);
854 else if (is_gimple_call (stmt
)
855 || gimple_has_side_effects (stmt
)
856 || gimple_assign_rhs_code (stmt
) == PAREN_EXPR
)
858 r
= build1 (REALPART_EXPR
, inner_type
, lhs
);
859 i
= build1 (IMAGPART_EXPR
, inner_type
, lhs
);
860 update_complex_components (gsi
, stmt
, r
, i
);
864 if (gimple_assign_rhs_code (stmt
) != COMPLEX_EXPR
)
866 r
= extract_component (gsi
, rhs
, 0, true);
867 i
= extract_component (gsi
, rhs
, 1, true);
871 r
= gimple_assign_rhs1 (stmt
);
872 i
= gimple_assign_rhs2 (stmt
);
874 update_complex_assignment (gsi
, r
, i
);
878 && (TREE_CODE (rhs
) == SSA_NAME
|| TREE_CODE (rhs
) == COMPLEX_CST
)
879 && !TREE_SIDE_EFFECTS (lhs
))
885 loc
= gimple_location (stmt
);
886 r
= extract_component (gsi
, rhs
, 0, false);
887 i
= extract_component (gsi
, rhs
, 1, false);
889 x
= build1 (REALPART_EXPR
, inner_type
, unshare_expr (lhs
));
890 t
= gimple_build_assign (x
, r
);
891 gimple_set_location (t
, loc
);
892 gsi_insert_before (gsi
, t
, GSI_SAME_STMT
);
894 if (stmt
== gsi_stmt (*gsi
))
896 x
= build1 (IMAGPART_EXPR
, inner_type
, unshare_expr (lhs
));
897 gimple_assign_set_lhs (stmt
, x
);
898 gimple_assign_set_rhs1 (stmt
, i
);
902 x
= build1 (IMAGPART_EXPR
, inner_type
, unshare_expr (lhs
));
903 t
= gimple_build_assign (x
, i
);
904 gimple_set_location (t
, loc
);
905 gsi_insert_before (gsi
, t
, GSI_SAME_STMT
);
907 stmt
= gsi_stmt (*gsi
);
908 gcc_assert (gimple_code (stmt
) == GIMPLE_RETURN
);
909 gimple_return_set_retval (as_a
<greturn
*> (stmt
), lhs
);
916 /* Expand complex addition to scalars:
917 a + b = (ar + br) + i(ai + bi)
918 a - b = (ar - br) + i(ai + bi)
922 expand_complex_addition (gimple_stmt_iterator
*gsi
, tree inner_type
,
923 tree ar
, tree ai
, tree br
, tree bi
,
925 complex_lattice_t al
, complex_lattice_t bl
)
928 gimple_seq stmts
= NULL
;
929 location_t loc
= gimple_location (gsi_stmt (*gsi
));
931 switch (PAIR (al
, bl
))
933 case PAIR (ONLY_REAL
, ONLY_REAL
):
934 rr
= gimple_build (&stmts
, loc
, code
, inner_type
, ar
, br
);
938 case PAIR (ONLY_REAL
, ONLY_IMAG
):
940 if (code
== MINUS_EXPR
)
941 ri
= gimple_build (&stmts
, loc
, MINUS_EXPR
, inner_type
, ai
, bi
);
946 case PAIR (ONLY_IMAG
, ONLY_REAL
):
947 if (code
== MINUS_EXPR
)
948 rr
= gimple_build (&stmts
, loc
, MINUS_EXPR
, inner_type
, ar
, br
);
954 case PAIR (ONLY_IMAG
, ONLY_IMAG
):
956 ri
= gimple_build (&stmts
, loc
, code
, inner_type
, ai
, bi
);
959 case PAIR (VARYING
, ONLY_REAL
):
960 rr
= gimple_build (&stmts
, loc
, code
, inner_type
, ar
, br
);
964 case PAIR (VARYING
, ONLY_IMAG
):
966 ri
= gimple_build (&stmts
, loc
, code
, inner_type
, ai
, bi
);
969 case PAIR (ONLY_REAL
, VARYING
):
970 if (code
== MINUS_EXPR
)
972 rr
= gimple_build (&stmts
, loc
, code
, inner_type
, ar
, br
);
976 case PAIR (ONLY_IMAG
, VARYING
):
977 if (code
== MINUS_EXPR
)
980 ri
= gimple_build (&stmts
, loc
, code
, inner_type
, ai
, bi
);
983 case PAIR (VARYING
, VARYING
):
985 rr
= gimple_build (&stmts
, loc
, code
, inner_type
, ar
, br
);
986 ri
= gimple_build (&stmts
, loc
, code
, inner_type
, ai
, bi
);
993 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
994 update_complex_assignment (gsi
, rr
, ri
);
997 /* Expand a complex multiplication or division to a libcall to the c99
998 compliant routines. TYPE is the complex type of the operation.
999 If INPLACE_P replace the statement at GSI with
1000 the libcall and return NULL_TREE. Else insert the call, assign its
1001 result to an output variable and return that variable. If INPLACE_P
1002 is true then the statement being replaced should be an assignment
1006 expand_complex_libcall (gimple_stmt_iterator
*gsi
, tree type
, tree ar
, tree ai
,
1007 tree br
, tree bi
, enum tree_code code
, bool inplace_p
)
1010 enum built_in_function bcode
;
1014 mode
= TYPE_MODE (type
);
1015 gcc_assert (GET_MODE_CLASS (mode
) == MODE_COMPLEX_FLOAT
);
1017 if (code
== MULT_EXPR
)
1018 bcode
= ((enum built_in_function
)
1019 (BUILT_IN_COMPLEX_MUL_MIN
+ mode
- MIN_MODE_COMPLEX_FLOAT
));
1020 else if (code
== RDIV_EXPR
)
1021 bcode
= ((enum built_in_function
)
1022 (BUILT_IN_COMPLEX_DIV_MIN
+ mode
- MIN_MODE_COMPLEX_FLOAT
));
1025 fn
= builtin_decl_explicit (bcode
);
1026 stmt
= gimple_build_call (fn
, 4, ar
, ai
, br
, bi
);
1030 gimple
*old_stmt
= gsi_stmt (*gsi
);
1031 gimple_call_set_nothrow (stmt
, !stmt_could_throw_p (cfun
, old_stmt
));
1032 lhs
= gimple_assign_lhs (old_stmt
);
1033 gimple_call_set_lhs (stmt
, lhs
);
1034 gsi_replace (gsi
, stmt
, true);
1036 type
= TREE_TYPE (type
);
1037 if (stmt_can_throw_internal (cfun
, stmt
))
1041 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
1042 if (!(e
->flags
& EDGE_EH
))
1044 basic_block bb
= split_edge (e
);
1045 gimple_stmt_iterator gsi2
= gsi_start_bb (bb
);
1046 update_complex_components (&gsi2
, stmt
,
1047 build1 (REALPART_EXPR
, type
, lhs
),
1048 build1 (IMAGPART_EXPR
, type
, lhs
));
1052 update_complex_components (gsi
, stmt
,
1053 build1 (REALPART_EXPR
, type
, lhs
),
1054 build1 (IMAGPART_EXPR
, type
, lhs
));
1055 SSA_NAME_DEF_STMT (lhs
) = stmt
;
1059 gimple_call_set_nothrow (stmt
, true);
1060 lhs
= make_ssa_name (type
);
1061 gimple_call_set_lhs (stmt
, lhs
);
1062 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1067 /* Perform a complex multiplication on two complex constants A, B represented
1068 by AR, AI, BR, BI of type TYPE.
1069 The operation we want is: a * b = (ar*br - ai*bi) + i(ar*bi + br*ai).
1070 Insert the GIMPLE statements into GSI. Store the real and imaginary
1071 components of the result into RR and RI. */
1074 expand_complex_multiplication_components (gimple_seq
*stmts
, location_t loc
,
1075 tree type
, tree ar
, tree ai
,
1079 tree t1
, t2
, t3
, t4
;
1081 t1
= gimple_build (stmts
, loc
, MULT_EXPR
, type
, ar
, br
);
1082 t2
= gimple_build (stmts
, loc
, MULT_EXPR
, type
, ai
, bi
);
1083 t3
= gimple_build (stmts
, loc
, MULT_EXPR
, type
, ar
, bi
);
1085 /* Avoid expanding redundant multiplication for the common
1086 case of squaring a complex number. */
1087 if (ar
== br
&& ai
== bi
)
1090 t4
= gimple_build (stmts
, loc
, MULT_EXPR
, type
, ai
, br
);
1092 *rr
= gimple_build (stmts
, loc
, MINUS_EXPR
, type
, t1
, t2
);
1093 *ri
= gimple_build (stmts
, loc
, PLUS_EXPR
, type
, t3
, t4
);
1096 /* Expand complex multiplication to scalars:
1097 a * b = (ar*br - ai*bi) + i(ar*bi + br*ai)
1101 expand_complex_multiplication (gimple_stmt_iterator
*gsi
, tree type
,
1102 tree ar
, tree ai
, tree br
, tree bi
,
1103 complex_lattice_t al
, complex_lattice_t bl
)
1106 tree inner_type
= TREE_TYPE (type
);
1107 location_t loc
= gimple_location (gsi_stmt (*gsi
));
1108 gimple_seq stmts
= NULL
;
1112 complex_lattice_t tl
;
1113 rr
= ar
, ar
= br
, br
= rr
;
1114 ri
= ai
, ai
= bi
, bi
= ri
;
1115 tl
= al
, al
= bl
, bl
= tl
;
1118 switch (PAIR (al
, bl
))
1120 case PAIR (ONLY_REAL
, ONLY_REAL
):
1121 rr
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ar
, br
);
1125 case PAIR (ONLY_IMAG
, ONLY_REAL
):
1127 if (TREE_CODE (ai
) == REAL_CST
1128 && real_identical (&TREE_REAL_CST (ai
), &dconst1
))
1131 ri
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ai
, br
);
1134 case PAIR (ONLY_IMAG
, ONLY_IMAG
):
1135 rr
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ai
, bi
);
1136 rr
= gimple_build (&stmts
, loc
, NEGATE_EXPR
, inner_type
, rr
);
1140 case PAIR (VARYING
, ONLY_REAL
):
1141 rr
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ar
, br
);
1142 ri
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ai
, br
);
1145 case PAIR (VARYING
, ONLY_IMAG
):
1146 rr
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ai
, bi
);
1147 rr
= gimple_build (&stmts
, loc
, NEGATE_EXPR
, inner_type
, rr
);
1148 ri
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ar
, bi
);
1151 case PAIR (VARYING
, VARYING
):
1152 if (flag_complex_method
== 2 && SCALAR_FLOAT_TYPE_P (inner_type
))
1154 /* If optimizing for size or not at all just do a libcall.
1155 Same if there are exception-handling edges or signaling NaNs. */
1156 if (optimize
== 0 || optimize_bb_for_size_p (gsi_bb (*gsi
))
1157 || stmt_can_throw_internal (cfun
, gsi_stmt (*gsi
))
1158 || flag_signaling_nans
)
1160 expand_complex_libcall (gsi
, type
, ar
, ai
, br
, bi
,
1165 if (!HONOR_NANS (inner_type
))
1167 /* If we are not worrying about NaNs expand to
1168 (ar*br - ai*bi) + i(ar*bi + br*ai) directly. */
1169 expand_complex_multiplication_components (&stmts
, loc
, inner_type
,
1175 /* Else, expand x = a * b into
1176 x = (ar*br - ai*bi) + i(ar*bi + br*ai);
1177 if (isunordered (__real__ x, __imag__ x))
1178 x = __muldc3 (a, b); */
1181 expand_complex_multiplication_components (&stmts
, loc
,
1183 br
, bi
, &tmpr
, &tmpi
);
1184 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1188 = gimple_build_cond (UNORDERED_EXPR
, tmpr
, tmpi
,
1189 NULL_TREE
, NULL_TREE
);
1191 basic_block orig_bb
= gsi_bb (*gsi
);
1192 /* We want to keep track of the original complex multiplication
1193 statement as we're going to modify it later in
1194 update_complex_assignment. Make sure that insert_cond_bb leaves
1195 that statement in the join block. */
1198 = insert_cond_bb (gsi_bb (*gsi
), gsi_stmt (*gsi
), check
,
1199 profile_probability::very_unlikely ());
1201 gimple_stmt_iterator cond_bb_gsi
= gsi_last_bb (cond_bb
);
1202 gsi_insert_after (&cond_bb_gsi
, gimple_build_nop (), GSI_NEW_STMT
);
1205 = expand_complex_libcall (&cond_bb_gsi
, type
, ar
, ai
, br
,
1206 bi
, MULT_EXPR
, false);
1207 gimple_seq stmts2
= NULL
;
1208 tree cond_real
= gimple_build (&stmts2
, loc
, REALPART_EXPR
,
1209 inner_type
, libcall_res
);
1210 tree cond_imag
= gimple_build (&stmts2
, loc
, IMAGPART_EXPR
,
1211 inner_type
, libcall_res
);
1212 gsi_insert_seq_before (&cond_bb_gsi
, stmts2
, GSI_SAME_STMT
);
1214 basic_block join_bb
= single_succ_edge (cond_bb
)->dest
;
1215 *gsi
= gsi_start_nondebug_after_labels_bb (join_bb
);
1217 /* We have a conditional block with some assignments in cond_bb.
1218 Wire up the PHIs to wrap up. */
1219 rr
= make_ssa_name (inner_type
);
1220 ri
= make_ssa_name (inner_type
);
1221 edge cond_to_join
= single_succ_edge (cond_bb
);
1222 edge orig_to_join
= find_edge (orig_bb
, join_bb
);
1224 gphi
*real_phi
= create_phi_node (rr
, gsi_bb (*gsi
));
1225 add_phi_arg (real_phi
, cond_real
, cond_to_join
, UNKNOWN_LOCATION
);
1226 add_phi_arg (real_phi
, tmpr
, orig_to_join
, UNKNOWN_LOCATION
);
1228 gphi
*imag_phi
= create_phi_node (ri
, gsi_bb (*gsi
));
1229 add_phi_arg (imag_phi
, cond_imag
, cond_to_join
, UNKNOWN_LOCATION
);
1230 add_phi_arg (imag_phi
, tmpi
, orig_to_join
, UNKNOWN_LOCATION
);
1233 /* If we are not worrying about NaNs expand to
1234 (ar*br - ai*bi) + i(ar*bi + br*ai) directly. */
1235 expand_complex_multiplication_components (&stmts
, loc
,
1244 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1245 update_complex_assignment (gsi
, rr
, ri
);
1248 /* Keep this algorithm in sync with fold-const.cc:const_binop().
1250 Expand complex division to scalars, straightforward algorithm.
1251 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1256 expand_complex_div_straight (gimple_stmt_iterator
*gsi
, tree inner_type
,
1257 tree ar
, tree ai
, tree br
, tree bi
,
1258 enum tree_code code
)
1260 gimple_seq stmts
= NULL
;
1261 location_t loc
= gimple_location (gsi_stmt (*gsi
));
1262 tree rr
, ri
, div
, t1
, t2
, t3
;
1264 t1
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, br
, br
);
1265 t2
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, bi
, bi
);
1266 div
= gimple_build (&stmts
, loc
, PLUS_EXPR
, inner_type
, t1
, t2
);
1268 t1
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ar
, br
);
1269 t2
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ai
, bi
);
1270 t3
= gimple_build (&stmts
, loc
, PLUS_EXPR
, inner_type
, t1
, t2
);
1271 rr
= gimple_build (&stmts
, loc
, code
, inner_type
, t3
, div
);
1273 t1
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ai
, br
);
1274 t2
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ar
, bi
);
1275 t3
= gimple_build (&stmts
, loc
, MINUS_EXPR
, inner_type
, t1
, t2
);
1276 ri
= gimple_build (&stmts
, loc
, code
, inner_type
, t3
, div
);
1278 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1279 update_complex_assignment (gsi
, rr
, ri
);
1282 /* Keep this algorithm in sync with fold-const.cc:const_binop().
1284 Expand complex division to scalars, modified algorithm to minimize
1285 overflow with wide input ranges. */
1288 expand_complex_div_wide (gimple_stmt_iterator
*gsi
, tree inner_type
,
1289 tree ar
, tree ai
, tree br
, tree bi
,
1290 enum tree_code code
)
1292 tree rr
, ri
, ratio
, div
, t1
, t2
, tr
, ti
, compare
;
1293 basic_block bb_cond
, bb_true
, bb_false
, bb_join
;
1295 gimple_seq stmts
= NULL
;
1296 location_t loc
= gimple_location (gsi_stmt (*gsi
));
1298 /* Examine |br| < |bi|, and branch. */
1299 t1
= gimple_build (&stmts
, loc
, ABS_EXPR
, inner_type
, br
);
1300 t2
= gimple_build (&stmts
, loc
, ABS_EXPR
, inner_type
, bi
);
1301 compare
= gimple_build (&stmts
, loc
,
1302 LT_EXPR
, boolean_type_node
, t1
, t2
);
1304 bb_cond
= bb_true
= bb_false
= bb_join
= NULL
;
1305 rr
= ri
= tr
= ti
= NULL
;
1306 if (TREE_CODE (compare
) != INTEGER_CST
)
1311 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1313 stmt
= gimple_build_cond (NE_EXPR
, compare
, boolean_false_node
,
1314 NULL_TREE
, NULL_TREE
);
1315 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1317 /* Split the original block, and create the TRUE and FALSE blocks. */
1318 e
= split_block (gsi_bb (*gsi
), stmt
);
1321 bb_true
= create_empty_bb (bb_cond
);
1322 bb_false
= create_empty_bb (bb_true
);
1323 bb_true
->count
= bb_false
->count
1324 = bb_cond
->count
.apply_probability (profile_probability::even ());
1326 /* Wire the blocks together. */
1327 e
->flags
= EDGE_TRUE_VALUE
;
1328 /* TODO: With value profile we could add an historgram to determine real
1330 e
->probability
= profile_probability::even ();
1331 redirect_edge_succ (e
, bb_true
);
1332 edge e2
= make_edge (bb_cond
, bb_false
, EDGE_FALSE_VALUE
);
1333 e2
->probability
= profile_probability::even ();
1334 make_single_succ_edge (bb_true
, bb_join
, EDGE_FALLTHRU
);
1335 make_single_succ_edge (bb_false
, bb_join
, EDGE_FALLTHRU
);
1336 add_bb_to_loop (bb_true
, bb_cond
->loop_father
);
1337 add_bb_to_loop (bb_false
, bb_cond
->loop_father
);
1339 /* Update dominance info. Note that bb_join's data was
1340 updated by split_block. */
1341 if (dom_info_available_p (CDI_DOMINATORS
))
1343 set_immediate_dominator (CDI_DOMINATORS
, bb_true
, bb_cond
);
1344 set_immediate_dominator (CDI_DOMINATORS
, bb_false
, bb_cond
);
1347 rr
= create_tmp_reg (inner_type
);
1348 ri
= create_tmp_reg (inner_type
);
1352 gimple_seq_discard (stmts
);
1356 /* In the TRUE branch, we compute
1358 div = (br * ratio) + bi;
1359 tr = (ar * ratio) + ai;
1360 ti = (ai * ratio) - ar;
1363 if (bb_true
|| integer_nonzerop (compare
))
1367 *gsi
= gsi_last_bb (bb_true
);
1368 gsi_insert_after (gsi
, gimple_build_nop (), GSI_NEW_STMT
);
1371 ratio
= gimple_build (&stmts
, loc
, code
, inner_type
, br
, bi
);
1373 t1
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, br
, ratio
);
1374 div
= gimple_build (&stmts
, loc
, PLUS_EXPR
, inner_type
, t1
, bi
);
1376 t1
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ar
, ratio
);
1377 tr
= gimple_build (&stmts
, loc
, PLUS_EXPR
, inner_type
, t1
, ai
);
1379 t1
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ai
, ratio
);
1380 ti
= gimple_build (&stmts
, loc
, MINUS_EXPR
, inner_type
, t1
, ar
);
1382 tr
= gimple_build (&stmts
, loc
, code
, inner_type
, tr
, div
);
1383 ti
= gimple_build (&stmts
, loc
, code
, inner_type
, ti
, div
);
1384 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1389 stmt
= gimple_build_assign (rr
, tr
);
1390 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1391 stmt
= gimple_build_assign (ri
, ti
);
1392 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1393 gsi_remove (gsi
, true);
1397 /* In the FALSE branch, we compute
1399 divisor = (d * ratio) + c;
1400 tr = (b * ratio) + a;
1401 ti = b - (a * ratio);
1404 if (bb_false
|| integer_zerop (compare
))
1408 *gsi
= gsi_last_bb (bb_false
);
1409 gsi_insert_after (gsi
, gimple_build_nop (), GSI_NEW_STMT
);
1412 ratio
= gimple_build (&stmts
, loc
, code
, inner_type
, bi
, br
);
1414 t1
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, bi
, ratio
);
1415 div
= gimple_build (&stmts
, loc
, PLUS_EXPR
, inner_type
, t1
, br
);
1417 t1
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ai
, ratio
);
1418 tr
= gimple_build (&stmts
, loc
, PLUS_EXPR
, inner_type
, t1
, ar
);
1420 t1
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ar
, ratio
);
1421 ti
= gimple_build (&stmts
, loc
, MINUS_EXPR
, inner_type
, ai
, t1
);
1423 tr
= gimple_build (&stmts
, loc
, code
, inner_type
, tr
, div
);
1424 ti
= gimple_build (&stmts
, loc
, code
, inner_type
, ti
, div
);
1425 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1430 stmt
= gimple_build_assign (rr
, tr
);
1431 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1432 stmt
= gimple_build_assign (ri
, ti
);
1433 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1434 gsi_remove (gsi
, true);
1439 *gsi
= gsi_start_bb (bb_join
);
1443 update_complex_assignment (gsi
, rr
, ri
);
1446 /* Expand complex division to scalars. */
1449 expand_complex_division (gimple_stmt_iterator
*gsi
, tree type
,
1450 tree ar
, tree ai
, tree br
, tree bi
,
1451 enum tree_code code
,
1452 complex_lattice_t al
, complex_lattice_t bl
)
1455 gimple_seq stmts
= NULL
;
1456 location_t loc
= gimple_location (gsi_stmt (*gsi
));
1458 tree inner_type
= TREE_TYPE (type
);
1459 switch (PAIR (al
, bl
))
1461 case PAIR (ONLY_REAL
, ONLY_REAL
):
1462 rr
= gimple_build (&stmts
, loc
, code
, inner_type
, ar
, br
);
1466 case PAIR (ONLY_REAL
, ONLY_IMAG
):
1468 ri
= gimple_build (&stmts
, loc
, code
, inner_type
, ar
, bi
);
1469 ri
= gimple_build (&stmts
, loc
, NEGATE_EXPR
, inner_type
, ri
);
1472 case PAIR (ONLY_IMAG
, ONLY_REAL
):
1474 ri
= gimple_build (&stmts
, loc
, code
, inner_type
, ai
, br
);
1477 case PAIR (ONLY_IMAG
, ONLY_IMAG
):
1478 rr
= gimple_build (&stmts
, loc
, code
, inner_type
, ai
, bi
);
1482 case PAIR (VARYING
, ONLY_REAL
):
1483 rr
= gimple_build (&stmts
, loc
, code
, inner_type
, ar
, br
);
1484 ri
= gimple_build (&stmts
, loc
, code
, inner_type
, ai
, br
);
1487 case PAIR (VARYING
, ONLY_IMAG
):
1488 rr
= gimple_build (&stmts
, loc
, code
, inner_type
, ai
, bi
);
1489 ri
= gimple_build (&stmts
, loc
, code
, inner_type
, ar
, bi
);
1490 ri
= gimple_build (&stmts
, loc
, NEGATE_EXPR
, inner_type
, ri
);
1493 case PAIR (ONLY_REAL
, VARYING
):
1494 case PAIR (ONLY_IMAG
, VARYING
):
1495 case PAIR (VARYING
, VARYING
):
1496 switch (flag_complex_method
)
1499 /* straightforward implementation of complex divide acceptable. */
1500 expand_complex_div_straight (gsi
, inner_type
, ar
, ai
, br
, bi
, code
);
1504 if (SCALAR_FLOAT_TYPE_P (inner_type
))
1506 expand_complex_libcall (gsi
, type
, ar
, ai
, br
, bi
, code
, true);
1512 /* wide ranges of inputs must work for complex divide. */
1513 expand_complex_div_wide (gsi
, inner_type
, ar
, ai
, br
, bi
, code
);
1525 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1526 update_complex_assignment (gsi
, rr
, ri
);
1529 /* Expand complex negation to scalars:
1534 expand_complex_negation (gimple_stmt_iterator
*gsi
, tree inner_type
,
1538 gimple_seq stmts
= NULL
;
1539 location_t loc
= gimple_location (gsi_stmt (*gsi
));
1541 rr
= gimple_build (&stmts
, loc
, NEGATE_EXPR
, inner_type
, ar
);
1542 ri
= gimple_build (&stmts
, loc
, NEGATE_EXPR
, inner_type
, ai
);
1544 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1545 update_complex_assignment (gsi
, rr
, ri
);
1548 /* Expand complex conjugate to scalars:
1553 expand_complex_conjugate (gimple_stmt_iterator
*gsi
, tree inner_type
,
1557 gimple_seq stmts
= NULL
;
1558 location_t loc
= gimple_location (gsi_stmt (*gsi
));
1560 ri
= gimple_build (&stmts
, loc
, NEGATE_EXPR
, inner_type
, ai
);
1562 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1563 update_complex_assignment (gsi
, ar
, ri
);
1566 /* Expand complex comparison (EQ or NE only). */
1569 expand_complex_comparison (gimple_stmt_iterator
*gsi
, tree ar
, tree ai
,
1570 tree br
, tree bi
, enum tree_code code
)
1572 tree cr
, ci
, cc
, type
;
1573 gimple
*stmt
= gsi_stmt (*gsi
);
1574 gimple_seq stmts
= NULL
;
1575 location_t loc
= gimple_location (stmt
);
1577 cr
= gimple_build (&stmts
, loc
, code
, boolean_type_node
, ar
, br
);
1578 ci
= gimple_build (&stmts
, loc
, code
, boolean_type_node
, ai
, bi
);
1579 cc
= gimple_build (&stmts
, loc
,
1580 (code
== EQ_EXPR
? BIT_AND_EXPR
: BIT_IOR_EXPR
),
1581 boolean_type_node
, cr
, ci
);
1582 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1584 switch (gimple_code (stmt
))
1588 greturn
*return_stmt
= as_a
<greturn
*> (stmt
);
1589 type
= TREE_TYPE (gimple_return_retval (return_stmt
));
1590 gimple_return_set_retval (return_stmt
, fold_convert (type
, cc
));
1595 type
= TREE_TYPE (gimple_assign_lhs (stmt
));
1596 gimple_assign_set_rhs_from_tree (gsi
, fold_convert (type
, cc
));
1597 stmt
= gsi_stmt (*gsi
);
1602 gcond
*cond_stmt
= as_a
<gcond
*> (stmt
);
1603 gimple_cond_set_code (cond_stmt
, EQ_EXPR
);
1604 gimple_cond_set_lhs (cond_stmt
, cc
);
1605 gimple_cond_set_rhs (cond_stmt
, boolean_true_node
);
1614 if (maybe_clean_eh_stmt (stmt
))
1615 bitmap_set_bit (need_eh_cleanup
, gimple_bb (stmt
)->index
);
1618 /* Expand inline asm that sets some complex SSA_NAMEs. */
1621 expand_complex_asm (gimple_stmt_iterator
*gsi
)
1623 gasm
*stmt
= as_a
<gasm
*> (gsi_stmt (*gsi
));
1625 bool diagnosed_p
= false;
1627 for (i
= 0; i
< gimple_asm_noutputs (stmt
); ++i
)
1629 tree link
= gimple_asm_output_op (stmt
, i
);
1630 tree op
= TREE_VALUE (link
);
1631 if (TREE_CODE (op
) == SSA_NAME
1632 && TREE_CODE (TREE_TYPE (op
)) == COMPLEX_TYPE
)
1634 if (gimple_asm_nlabels (stmt
) > 0)
1638 sorry_at (gimple_location (stmt
),
1639 "%<asm goto%> with complex typed outputs");
1642 /* Make sure to not ICE later, see PR105165. */
1643 tree zero
= build_zero_cst (TREE_TYPE (TREE_TYPE (op
)));
1644 set_component_ssa_name (op
, false, zero
);
1645 set_component_ssa_name (op
, true, zero
);
1648 tree type
= TREE_TYPE (op
);
1649 tree inner_type
= TREE_TYPE (type
);
1650 tree r
= build1 (REALPART_EXPR
, inner_type
, op
);
1651 tree i
= build1 (IMAGPART_EXPR
, inner_type
, op
);
1652 gimple_seq list
= set_component_ssa_name (op
, false, r
);
1655 gsi_insert_seq_after (gsi
, list
, GSI_CONTINUE_LINKING
);
1657 list
= set_component_ssa_name (op
, true, i
);
1659 gsi_insert_seq_after (gsi
, list
, GSI_CONTINUE_LINKING
);
1664 /* Process one statement. If we identify a complex operation, expand it. */
1667 expand_complex_operations_1 (gimple_stmt_iterator
*gsi
)
1669 gimple
*stmt
= gsi_stmt (*gsi
);
1670 tree type
, inner_type
, lhs
;
1671 tree ac
, ar
, ai
, bc
, br
, bi
;
1672 complex_lattice_t al
, bl
;
1673 enum tree_code code
;
1675 if (gimple_code (stmt
) == GIMPLE_ASM
)
1677 expand_complex_asm (gsi
);
1681 lhs
= gimple_get_lhs (stmt
);
1682 if (!lhs
&& gimple_code (stmt
) != GIMPLE_COND
)
1685 type
= TREE_TYPE (gimple_op (stmt
, 0));
1686 code
= gimple_expr_code (stmt
);
1688 /* Initial filter for operations we handle. */
1694 case TRUNC_DIV_EXPR
:
1696 case FLOOR_DIV_EXPR
:
1697 case ROUND_DIV_EXPR
:
1701 if (TREE_CODE (type
) != COMPLEX_TYPE
)
1703 inner_type
= TREE_TYPE (type
);
1708 /* Note, both GIMPLE_ASSIGN and GIMPLE_COND may have an EQ_EXPR
1709 subcode, so we need to access the operands using gimple_op. */
1710 inner_type
= TREE_TYPE (gimple_op (stmt
, 1));
1711 if (TREE_CODE (inner_type
) != COMPLEX_TYPE
)
1719 /* GIMPLE_COND may also fallthru here, but we do not need to
1720 do anything with it. */
1721 if (gimple_code (stmt
) == GIMPLE_COND
)
1724 if (TREE_CODE (type
) == COMPLEX_TYPE
)
1725 expand_complex_move (gsi
, type
);
1726 else if (is_gimple_assign (stmt
)
1727 && (gimple_assign_rhs_code (stmt
) == REALPART_EXPR
1728 || gimple_assign_rhs_code (stmt
) == IMAGPART_EXPR
)
1729 && TREE_CODE (lhs
) == SSA_NAME
)
1731 rhs
= gimple_assign_rhs1 (stmt
);
1732 rhs
= extract_component (gsi
, TREE_OPERAND (rhs
, 0),
1733 gimple_assign_rhs_code (stmt
)
1736 gimple_assign_set_rhs_from_tree (gsi
, rhs
);
1737 stmt
= gsi_stmt (*gsi
);
1744 /* Extract the components of the two complex values. Make sure and
1745 handle the common case of the same value used twice specially. */
1746 if (is_gimple_assign (stmt
))
1748 ac
= gimple_assign_rhs1 (stmt
);
1749 bc
= (gimple_num_ops (stmt
) > 2) ? gimple_assign_rhs2 (stmt
) : NULL
;
1751 /* GIMPLE_CALL cannot get here. */
1754 ac
= gimple_cond_lhs (stmt
);
1755 bc
= gimple_cond_rhs (stmt
);
1758 ar
= extract_component (gsi
, ac
, false, true);
1759 ai
= extract_component (gsi
, ac
, true, true);
1765 br
= extract_component (gsi
, bc
, 0, true);
1766 bi
= extract_component (gsi
, bc
, 1, true);
1769 br
= bi
= NULL_TREE
;
1771 al
= find_lattice_value (ac
);
1772 if (al
== UNINITIALIZED
)
1775 if (TREE_CODE_CLASS (code
) == tcc_unary
)
1781 bl
= find_lattice_value (bc
);
1782 if (bl
== UNINITIALIZED
)
1790 expand_complex_addition (gsi
, inner_type
, ar
, ai
, br
, bi
, code
, al
, bl
);
1794 expand_complex_multiplication (gsi
, type
, ar
, ai
, br
, bi
, al
, bl
);
1797 case TRUNC_DIV_EXPR
:
1799 case FLOOR_DIV_EXPR
:
1800 case ROUND_DIV_EXPR
:
1802 expand_complex_division (gsi
, type
, ar
, ai
, br
, bi
, code
, al
, bl
);
1806 expand_complex_negation (gsi
, inner_type
, ar
, ai
);
1810 expand_complex_conjugate (gsi
, inner_type
, ar
, ai
);
1815 expand_complex_comparison (gsi
, ar
, ai
, br
, bi
, code
);
1824 /* Entry point for complex operation lowering during optimization. */
1827 tree_lower_complex (void)
1829 gimple_stmt_iterator gsi
;
1834 if (!init_dont_simulate_again ())
1837 complex_lattice_values
.create (num_ssa_names
);
1838 complex_lattice_values
.safe_grow_cleared (num_ssa_names
, true);
1840 init_parameter_lattice_values ();
1841 class complex_propagate complex_propagate
;
1842 complex_propagate
.ssa_propagate ();
1844 need_eh_cleanup
= BITMAP_ALLOC (NULL
);
1846 complex_variable_components
= new int_tree_htab_type (10);
1848 complex_ssa_name_components
.create (2 * num_ssa_names
);
1849 complex_ssa_name_components
.safe_grow_cleared (2 * num_ssa_names
, true);
1851 update_parameter_components ();
1853 rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
1854 n_bbs
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
1855 for (i
= 0; i
< n_bbs
; i
++)
1857 bb
= BASIC_BLOCK_FOR_FN (cfun
, rpo
[i
]);
1860 update_phi_components (bb
);
1861 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1862 expand_complex_operations_1 (&gsi
);
1867 if (!phis_to_revisit
.is_empty ())
1869 unsigned int n
= phis_to_revisit
.length ();
1870 for (unsigned int j
= 0; j
< n
; j
+= 3)
1871 for (unsigned int k
= 0; k
< 2; k
++)
1872 if (gphi
*phi
= phis_to_revisit
[j
+ k
+ 1])
1874 unsigned int m
= gimple_phi_num_args (phi
);
1875 for (unsigned int l
= 0; l
< m
; ++l
)
1877 tree op
= gimple_phi_arg_def (phi
, l
);
1878 if (TREE_CODE (op
) == SSA_NAME
1879 || is_gimple_min_invariant (op
))
1881 tree arg
= gimple_phi_arg_def (phis_to_revisit
[j
], l
);
1882 op
= extract_component (NULL
, arg
, k
> 0, false, false);
1883 SET_PHI_ARG_DEF (phi
, l
, op
);
1886 phis_to_revisit
.release ();
1889 gsi_commit_edge_inserts ();
1892 = gimple_purge_all_dead_eh_edges (need_eh_cleanup
) ? TODO_cleanup_cfg
: 0;
1893 BITMAP_FREE (need_eh_cleanup
);
1895 delete complex_variable_components
;
1896 complex_variable_components
= NULL
;
1897 complex_ssa_name_components
.release ();
1898 complex_lattice_values
.release ();
1904 const pass_data pass_data_lower_complex
=
1906 GIMPLE_PASS
, /* type */
1907 "cplxlower", /* name */
1908 OPTGROUP_NONE
, /* optinfo_flags */
1909 TV_NONE
, /* tv_id */
1910 PROP_ssa
, /* properties_required */
1911 PROP_gimple_lcx
, /* properties_provided */
1912 0, /* properties_destroyed */
1913 0, /* todo_flags_start */
1914 TODO_update_ssa
, /* todo_flags_finish */
1917 class pass_lower_complex
: public gimple_opt_pass
1920 pass_lower_complex (gcc::context
*ctxt
)
1921 : gimple_opt_pass (pass_data_lower_complex
, ctxt
)
1924 /* opt_pass methods: */
1925 opt_pass
* clone () final override
{ return new pass_lower_complex (m_ctxt
); }
1926 unsigned int execute (function
*) final override
1928 return tree_lower_complex ();
1931 }; // class pass_lower_complex
1936 make_pass_lower_complex (gcc::context
*ctxt
)
1938 return new pass_lower_complex (ctxt
);
1944 const pass_data pass_data_lower_complex_O0
=
1946 GIMPLE_PASS
, /* type */
1947 "cplxlower0", /* name */
1948 OPTGROUP_NONE
, /* optinfo_flags */
1949 TV_NONE
, /* tv_id */
1950 PROP_cfg
, /* properties_required */
1951 PROP_gimple_lcx
, /* properties_provided */
1952 0, /* properties_destroyed */
1953 0, /* todo_flags_start */
1954 TODO_update_ssa
, /* todo_flags_finish */
1957 class pass_lower_complex_O0
: public gimple_opt_pass
1960 pass_lower_complex_O0 (gcc::context
*ctxt
)
1961 : gimple_opt_pass (pass_data_lower_complex_O0
, ctxt
)
1964 /* opt_pass methods: */
1965 bool gate (function
*fun
) final override
1967 /* With errors, normal optimization passes are not run. If we don't
1968 lower complex operations at all, rtl expansion will abort. */
1969 return !(fun
->curr_properties
& PROP_gimple_lcx
);
1972 unsigned int execute (function
*) final override
1974 return tree_lower_complex ();
1977 }; // class pass_lower_complex_O0
1982 make_pass_lower_complex_O0 (gcc::context
*ctxt
)
1984 return new pass_lower_complex_O0 (ctxt
);