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"
29 #include "tree-pass.h"
31 #include "fold-const.h"
32 #include "stor-layout.h"
35 #include "gimple-iterator.h"
36 #include "gimplify-me.h"
40 #include "tree-ssa-propagate.h"
41 #include "tree-hasher.h"
44 #include "gimple-fold.h"
45 #include "diagnostic-core.h"
46 #include "case-cfn-macros.h"
48 #include "optabs-tree.h"
49 #include "tree-ssa-dce.h"
51 /* For each complex ssa name, a lattice value. We're interested in finding
52 out whether a complex number is degenerate in some way, having only real
53 or only complex parts. */
63 /* The type complex_lattice_t holds combinations of the above
65 typedef int complex_lattice_t
;
67 #define PAIR(a, b) ((a) << 2 | (b))
69 class complex_propagate
: public ssa_propagation_engine
71 enum ssa_prop_result
visit_stmt (gimple
*, edge
*, tree
*) final override
;
72 enum ssa_prop_result
visit_phi (gphi
*) final override
;
75 static vec
<complex_lattice_t
> complex_lattice_values
;
77 /* For each complex variable, a pair of variables for the components exists in
79 static int_tree_htab_type
*complex_variable_components
;
81 /* For each complex SSA_NAME, a pair of ssa names for the components. */
82 static vec
<tree
> complex_ssa_name_components
;
84 /* Vector of PHI triplets (original complex PHI and corresponding real and
85 imag PHIs if real and/or imag PHIs contain temporarily
86 non-SSA_NAME/non-invariant args that need to be replaced by SSA_NAMEs. */
87 static vec
<gphi
*> phis_to_revisit
;
89 /* BBs that need EH cleanup. */
90 static bitmap need_eh_cleanup
;
92 /* SSA defs we should try to DCE. */
93 static bitmap dce_worklist
;
95 /* Lookup UID in the complex_variable_components hashtable and return the
98 cvc_lookup (unsigned int uid
)
100 struct int_tree_map in
;
102 return complex_variable_components
->find_with_hash (in
, uid
).to
;
105 /* Insert the pair UID, TO into the complex_variable_components hashtable. */
108 cvc_insert (unsigned int uid
, tree to
)
114 loc
= complex_variable_components
->find_slot_with_hash (h
, uid
, INSERT
);
119 /* Return true if T is not a zero constant. In the case of real values,
120 we're only interested in +0.0. */
123 some_nonzerop (tree t
)
127 /* Operations with real or imaginary part of a complex number zero
128 cannot be treated the same as operations with a real or imaginary
129 operand if we care about the signs of zeros in the result. */
130 if (TREE_CODE (t
) == REAL_CST
&& !flag_signed_zeros
)
131 zerop
= real_identical (&TREE_REAL_CST (t
), &dconst0
);
132 else if (TREE_CODE (t
) == FIXED_CST
)
133 zerop
= fixed_zerop (t
);
134 else if (TREE_CODE (t
) == INTEGER_CST
)
135 zerop
= integer_zerop (t
);
141 /* Compute a lattice value from the components of a complex type REAL
144 static complex_lattice_t
145 find_lattice_value_parts (tree real
, tree imag
)
148 complex_lattice_t ret
;
150 r
= some_nonzerop (real
);
151 i
= some_nonzerop (imag
);
152 ret
= r
* ONLY_REAL
+ i
* ONLY_IMAG
;
154 /* ??? On occasion we could do better than mapping 0+0i to real, but we
155 certainly don't want to leave it UNINITIALIZED, which eventually gets
156 mapped to VARYING. */
157 if (ret
== UNINITIALIZED
)
164 /* Compute a lattice value from gimple_val T. */
166 static complex_lattice_t
167 find_lattice_value (tree t
)
171 switch (TREE_CODE (t
))
174 return complex_lattice_values
[SSA_NAME_VERSION (t
)];
177 real
= TREE_REALPART (t
);
178 imag
= TREE_IMAGPART (t
);
185 return find_lattice_value_parts (real
, imag
);
188 /* Determine if LHS is something for which we're interested in seeing
189 simulation results. */
192 is_complex_reg (tree lhs
)
194 return TREE_CODE (TREE_TYPE (lhs
)) == COMPLEX_TYPE
&& is_gimple_reg (lhs
);
197 /* Mark the incoming parameters to the function as VARYING. */
200 init_parameter_lattice_values (void)
204 for (parm
= DECL_ARGUMENTS (cfun
->decl
); parm
; parm
= DECL_CHAIN (parm
))
205 if (is_complex_reg (parm
)
206 && (ssa_name
= ssa_default_def (cfun
, parm
)) != NULL_TREE
)
207 complex_lattice_values
[SSA_NAME_VERSION (ssa_name
)] = VARYING
;
210 /* Initialize simulation state for each statement. Return false if we
211 found no statements we want to simulate, and thus there's nothing
212 for the entire pass to do. */
215 init_dont_simulate_again (void)
218 bool saw_a_complex_op
= false;
220 FOR_EACH_BB_FN (bb
, cfun
)
222 for (gphi_iterator gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
);
225 gphi
*phi
= gsi
.phi ();
226 prop_set_simulate_again (phi
,
227 is_complex_reg (gimple_phi_result (phi
)));
230 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);
237 stmt
= gsi_stmt (gsi
);
238 op0
= op1
= NULL_TREE
;
240 /* Most control-altering statements must be initially
241 simulated, else we won't cover the entire cfg. */
242 sim_again_p
= stmt_ends_bb_p (stmt
);
244 switch (gimple_code (stmt
))
247 if (gimple_call_lhs (stmt
))
249 sim_again_p
= is_complex_reg (gimple_call_lhs (stmt
));
250 switch (gimple_call_combined_fn (stmt
))
253 /* Expand cabs only if unsafe math and optimizing. */
254 if (optimize
&& flag_unsafe_math_optimizations
)
255 saw_a_complex_op
= true;
263 sim_again_p
= is_complex_reg (gimple_assign_lhs (stmt
));
264 if (gimple_assign_rhs_code (stmt
) == REALPART_EXPR
265 || gimple_assign_rhs_code (stmt
) == IMAGPART_EXPR
)
266 op0
= TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
268 op0
= gimple_assign_rhs1 (stmt
);
269 if (gimple_num_ops (stmt
) > 2)
270 op1
= gimple_assign_rhs2 (stmt
);
274 op0
= gimple_cond_lhs (stmt
);
275 op1
= gimple_cond_rhs (stmt
);
283 switch (gimple_expr_code (stmt
))
295 if (TREE_CODE (TREE_TYPE (op0
)) == COMPLEX_TYPE
296 || TREE_CODE (TREE_TYPE (op1
)) == COMPLEX_TYPE
)
297 saw_a_complex_op
= true;
303 if (TREE_CODE (TREE_TYPE (op0
)) == COMPLEX_TYPE
)
304 saw_a_complex_op
= true;
309 /* The total store transformation performed during
310 gimplification creates such uninitialized loads
311 and we need to lower the statement to be able
313 if (TREE_CODE (op0
) == SSA_NAME
314 && ssa_undefined_value_p (op0
))
315 saw_a_complex_op
= true;
319 /* When expand_complex_move would trigger make sure we
320 perform lowering even when there is no actual complex
321 operation. This helps consistency and vectorization. */
322 if (TREE_CODE (TREE_TYPE (gimple_op (stmt
, 0))) == COMPLEX_TYPE
)
323 saw_a_complex_op
= true;
327 prop_set_simulate_again (stmt
, sim_again_p
);
331 return saw_a_complex_op
;
335 /* Evaluate statement STMT against the complex lattice defined above. */
338 complex_propagate::visit_stmt (gimple
*stmt
, edge
*taken_edge_p ATTRIBUTE_UNUSED
,
341 complex_lattice_t new_l
, old_l
, op1_l
, op2_l
;
345 lhs
= gimple_get_lhs (stmt
);
346 /* Skip anything but GIMPLE_ASSIGN and GIMPLE_CALL with a lhs. */
347 if (!lhs
|| SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs
))
348 return SSA_PROP_VARYING
;
350 /* These conditions should be satisfied due to the initial filter
351 set up in init_dont_simulate_again. */
352 gcc_assert (TREE_CODE (lhs
) == SSA_NAME
);
353 gcc_assert (TREE_CODE (TREE_TYPE (lhs
)) == COMPLEX_TYPE
);
356 ver
= SSA_NAME_VERSION (lhs
);
357 old_l
= complex_lattice_values
[ver
];
359 switch (gimple_expr_code (stmt
))
363 new_l
= find_lattice_value (gimple_assign_rhs1 (stmt
));
367 new_l
= find_lattice_value_parts (gimple_assign_rhs1 (stmt
),
368 gimple_assign_rhs2 (stmt
));
373 op1_l
= find_lattice_value (gimple_assign_rhs1 (stmt
));
374 op2_l
= find_lattice_value (gimple_assign_rhs2 (stmt
));
376 /* We've set up the lattice values such that IOR neatly
378 new_l
= op1_l
| op2_l
;
387 op1_l
= find_lattice_value (gimple_assign_rhs1 (stmt
));
388 op2_l
= find_lattice_value (gimple_assign_rhs2 (stmt
));
390 /* Obviously, if either varies, so does the result. */
391 if (op1_l
== VARYING
|| op2_l
== VARYING
)
393 /* Don't prematurely promote variables if we've not yet seen
395 else if (op1_l
== UNINITIALIZED
)
397 else if (op2_l
== UNINITIALIZED
)
401 /* At this point both numbers have only one component. If the
402 numbers are of opposite kind, the result is imaginary,
403 otherwise the result is real. The add/subtract translates
404 the real/imag from/to 0/1; the ^ performs the comparison. */
405 new_l
= ((op1_l
- ONLY_REAL
) ^ (op2_l
- ONLY_REAL
)) + ONLY_REAL
;
407 /* Don't allow the lattice value to flip-flop indefinitely. */
415 new_l
= find_lattice_value (gimple_assign_rhs1 (stmt
));
423 /* If nothing changed this round, let the propagator know. */
425 return SSA_PROP_NOT_INTERESTING
;
427 complex_lattice_values
[ver
] = new_l
;
428 return new_l
== VARYING
? SSA_PROP_VARYING
: SSA_PROP_INTERESTING
;
431 /* Evaluate a PHI node against the complex lattice defined above. */
434 complex_propagate::visit_phi (gphi
*phi
)
436 complex_lattice_t new_l
, old_l
;
441 lhs
= gimple_phi_result (phi
);
443 /* This condition should be satisfied due to the initial filter
444 set up in init_dont_simulate_again. */
445 gcc_assert (TREE_CODE (TREE_TYPE (lhs
)) == COMPLEX_TYPE
);
447 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs
))
448 return SSA_PROP_VARYING
;
450 /* We've set up the lattice values such that IOR neatly models PHI meet. */
451 new_l
= UNINITIALIZED
;
452 for (i
= gimple_phi_num_args (phi
) - 1; i
>= 0; --i
)
453 new_l
|= find_lattice_value (gimple_phi_arg_def (phi
, i
));
455 ver
= SSA_NAME_VERSION (lhs
);
456 old_l
= complex_lattice_values
[ver
];
459 return SSA_PROP_NOT_INTERESTING
;
461 complex_lattice_values
[ver
] = new_l
;
462 return new_l
== VARYING
? SSA_PROP_VARYING
: SSA_PROP_INTERESTING
;
465 /* Create one backing variable for a complex component of ORIG. */
468 create_one_component_var (tree type
, tree orig
, const char *prefix
,
469 const char *suffix
, enum tree_code code
)
471 tree r
= create_tmp_var (type
, prefix
);
473 DECL_SOURCE_LOCATION (r
) = DECL_SOURCE_LOCATION (orig
);
474 DECL_ARTIFICIAL (r
) = 1;
476 if (DECL_NAME (orig
) && !DECL_IGNORED_P (orig
))
478 const char *name
= IDENTIFIER_POINTER (DECL_NAME (orig
));
479 name
= ACONCAT ((name
, suffix
, NULL
));
480 DECL_NAME (r
) = get_identifier (name
);
482 SET_DECL_DEBUG_EXPR (r
, build1 (code
, type
, orig
));
483 DECL_HAS_DEBUG_EXPR_P (r
) = 1;
484 DECL_IGNORED_P (r
) = 0;
485 copy_warning (r
, orig
);
489 DECL_IGNORED_P (r
) = 1;
490 suppress_warning (r
);
496 /* Retrieve a value for a complex component of VAR. */
499 get_component_var (tree var
, bool imag_p
)
501 size_t decl_index
= DECL_UID (var
) * 2 + imag_p
;
502 tree ret
= cvc_lookup (decl_index
);
506 ret
= create_one_component_var (TREE_TYPE (TREE_TYPE (var
)), var
,
507 imag_p
? "CI" : "CR",
508 imag_p
? "$imag" : "$real",
509 imag_p
? IMAGPART_EXPR
: REALPART_EXPR
);
510 cvc_insert (decl_index
, ret
);
516 /* Retrieve a value for a complex component of SSA_NAME. */
519 get_component_ssa_name (tree ssa_name
, bool imag_p
)
521 complex_lattice_t lattice
= find_lattice_value (ssa_name
);
522 size_t ssa_name_index
;
525 if (lattice
== (imag_p
? ONLY_REAL
: ONLY_IMAG
))
527 tree inner_type
= TREE_TYPE (TREE_TYPE (ssa_name
));
528 if (SCALAR_FLOAT_TYPE_P (inner_type
))
529 return build_real (inner_type
, dconst0
);
531 return build_int_cst (inner_type
, 0);
534 ssa_name_index
= SSA_NAME_VERSION (ssa_name
) * 2 + imag_p
;
535 ret
= complex_ssa_name_components
[ssa_name_index
];
538 if (SSA_NAME_VAR (ssa_name
))
539 ret
= get_component_var (SSA_NAME_VAR (ssa_name
), imag_p
);
541 ret
= TREE_TYPE (TREE_TYPE (ssa_name
));
542 ret
= make_ssa_name (ret
);
544 /* Copy some properties from the original. In particular, whether it
545 is used in an abnormal phi, and whether it's uninitialized. */
546 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ret
)
547 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name
);
548 if (SSA_NAME_IS_DEFAULT_DEF (ssa_name
)
549 && VAR_P (SSA_NAME_VAR (ssa_name
)))
551 SSA_NAME_DEF_STMT (ret
) = SSA_NAME_DEF_STMT (ssa_name
);
552 set_ssa_default_def (cfun
, SSA_NAME_VAR (ret
), ret
);
555 complex_ssa_name_components
[ssa_name_index
] = ret
;
561 /* Set a value for a complex component of SSA_NAME, return a
562 gimple_seq of stuff that needs doing. */
565 set_component_ssa_name (tree ssa_name
, bool imag_p
, tree value
)
567 complex_lattice_t lattice
= find_lattice_value (ssa_name
);
568 size_t ssa_name_index
;
573 /* We know the value must be zero, else there's a bug in our lattice
574 analysis. But the value may well be a variable known to contain
575 zero. We should be safe ignoring it. */
576 if (lattice
== (imag_p
? ONLY_REAL
: ONLY_IMAG
))
579 /* If we've already assigned an SSA_NAME to this component, then this
580 means that our walk of the basic blocks found a use before the set.
581 This is fine. Now we should create an initialization for the value
582 we created earlier. */
583 ssa_name_index
= SSA_NAME_VERSION (ssa_name
) * 2 + imag_p
;
584 comp
= complex_ssa_name_components
[ssa_name_index
];
588 /* If we've nothing assigned, and the value we're given is already stable,
589 then install that as the value for this SSA_NAME. This preemptively
590 copy-propagates the value, which avoids unnecessary memory allocation. */
591 else if (is_gimple_min_invariant (value
)
592 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name
))
594 complex_ssa_name_components
[ssa_name_index
] = value
;
597 else if (TREE_CODE (value
) == SSA_NAME
598 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name
))
600 /* Replace an anonymous base value with the variable from cvc_lookup.
601 This should result in better debug info. */
602 if (!SSA_NAME_IS_DEFAULT_DEF (value
)
603 && SSA_NAME_VAR (ssa_name
)
604 && (!SSA_NAME_VAR (value
) || DECL_IGNORED_P (SSA_NAME_VAR (value
)))
605 && !DECL_IGNORED_P (SSA_NAME_VAR (ssa_name
)))
607 comp
= get_component_var (SSA_NAME_VAR (ssa_name
), imag_p
);
608 replace_ssa_name_symbol (value
, comp
);
611 complex_ssa_name_components
[ssa_name_index
] = value
;
615 /* Finally, we need to stabilize the result by installing the value into
618 comp
= get_component_ssa_name (ssa_name
, imag_p
);
620 /* Do all the work to assign VALUE to COMP. */
622 value
= force_gimple_operand (value
, &list
, false, NULL
);
623 last
= gimple_build_assign (comp
, value
);
624 gimple_seq_add_stmt (&list
, last
);
625 gcc_assert (SSA_NAME_DEF_STMT (comp
) == last
);
630 /* Extract the real or imaginary part of a complex variable or constant.
631 Make sure that it's a proper gimple_val and gimplify it if not.
632 Emit any new code before gsi. */
635 extract_component (gimple_stmt_iterator
*gsi
, tree t
, bool imagpart_p
,
636 bool gimple_p
, bool phiarg_p
= false)
638 switch (TREE_CODE (t
))
641 return imagpart_p
? TREE_IMAGPART (t
) : TREE_REALPART (t
);
648 tree inner_type
= TREE_TYPE (TREE_TYPE (t
));
649 t
= unshare_expr (t
);
650 TREE_TYPE (t
) = inner_type
;
651 TREE_OPERAND (t
, 1) = TYPE_SIZE (inner_type
);
653 TREE_OPERAND (t
, 2) = size_binop (PLUS_EXPR
, TREE_OPERAND (t
, 2),
654 TYPE_SIZE (inner_type
));
656 t
= force_gimple_operand_gsi (gsi
, t
, true, NULL
, true,
666 case VIEW_CONVERT_EXPR
:
669 tree inner_type
= TREE_TYPE (TREE_TYPE (t
));
671 t
= build1 ((imagpart_p
? IMAGPART_EXPR
: REALPART_EXPR
),
672 inner_type
, unshare_expr (t
));
675 t
= force_gimple_operand_gsi (gsi
, t
, true, NULL
, true,
682 t
= get_component_ssa_name (t
, imagpart_p
);
683 if (TREE_CODE (t
) == SSA_NAME
&& SSA_NAME_DEF_STMT (t
) == NULL
)
684 gcc_assert (phiarg_p
);
692 /* Update the complex components of the ssa name on the lhs of STMT. */
695 update_complex_components (gimple_stmt_iterator
*gsi
, gimple
*stmt
, tree r
,
701 lhs
= gimple_get_lhs (stmt
);
703 list
= set_component_ssa_name (lhs
, false, r
);
705 gsi_insert_seq_after (gsi
, list
, GSI_CONTINUE_LINKING
);
707 list
= set_component_ssa_name (lhs
, true, i
);
709 gsi_insert_seq_after (gsi
, list
, GSI_CONTINUE_LINKING
);
713 update_complex_components_on_edge (edge e
, tree lhs
, tree r
, tree i
)
717 list
= set_component_ssa_name (lhs
, false, r
);
719 gsi_insert_seq_on_edge (e
, list
);
721 list
= set_component_ssa_name (lhs
, true, i
);
723 gsi_insert_seq_on_edge (e
, list
);
727 /* Update an assignment to a complex variable in place. */
730 update_complex_assignment (gimple_stmt_iterator
*gsi
, tree r
, tree i
)
732 gimple
*old_stmt
= gsi_stmt (*gsi
);
733 gimple_assign_set_rhs_with_ops (gsi
, COMPLEX_EXPR
, r
, i
);
734 gimple
*stmt
= gsi_stmt (*gsi
);
736 if (maybe_clean_or_replace_eh_stmt (old_stmt
, stmt
))
737 bitmap_set_bit (need_eh_cleanup
, gimple_bb (stmt
)->index
);
738 bitmap_set_bit (dce_worklist
, SSA_NAME_VERSION (gimple_assign_lhs (stmt
)));
740 update_complex_components (gsi
, gsi_stmt (*gsi
), r
, i
);
744 /* Generate code at the entry point of the function to initialize the
745 component variables for a complex parameter. */
748 update_parameter_components (void)
750 edge entry_edge
= single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
753 for (parm
= DECL_ARGUMENTS (cfun
->decl
); parm
; parm
= DECL_CHAIN (parm
))
755 tree type
= TREE_TYPE (parm
);
758 if (TREE_CODE (type
) != COMPLEX_TYPE
|| !is_gimple_reg (parm
))
761 type
= TREE_TYPE (type
);
762 ssa_name
= ssa_default_def (cfun
, parm
);
766 r
= build1 (REALPART_EXPR
, type
, ssa_name
);
767 i
= build1 (IMAGPART_EXPR
, type
, ssa_name
);
768 update_complex_components_on_edge (entry_edge
, ssa_name
, r
, i
);
772 /* Generate code to set the component variables of a complex variable
773 to match the PHI statements in block BB. */
776 update_phi_components (basic_block bb
)
780 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
782 gphi
*phi
= gsi
.phi ();
784 if (is_complex_reg (gimple_phi_result (phi
)))
786 gphi
*p
[2] = { NULL
, NULL
};
787 unsigned int i
, j
, n
;
788 bool revisit_phi
= false;
790 for (j
= 0; j
< 2; j
++)
792 tree l
= get_component_ssa_name (gimple_phi_result (phi
), j
> 0);
793 if (TREE_CODE (l
) == SSA_NAME
)
794 p
[j
] = create_phi_node (l
, bb
);
797 for (i
= 0, n
= gimple_phi_num_args (phi
); i
< n
; ++i
)
799 tree comp
, arg
= gimple_phi_arg_def (phi
, i
);
800 for (j
= 0; j
< 2; j
++)
803 comp
= extract_component (NULL
, arg
, j
> 0, false, true);
804 if (TREE_CODE (comp
) == SSA_NAME
805 && SSA_NAME_DEF_STMT (comp
) == NULL
)
807 /* For the benefit of any gimple simplification during
808 this pass that might walk SSA_NAME def stmts,
809 don't add SSA_NAMEs without definitions into the
810 PHI arguments, but put a decl in there instead
811 temporarily, and revisit this PHI later on. */
812 if (SSA_NAME_VAR (comp
))
813 comp
= SSA_NAME_VAR (comp
);
815 comp
= create_tmp_reg (TREE_TYPE (comp
),
819 SET_PHI_ARG_DEF (p
[j
], i
, comp
);
825 phis_to_revisit
.safe_push (phi
);
826 phis_to_revisit
.safe_push (p
[0]);
827 phis_to_revisit
.safe_push (p
[1]);
833 /* Expand a complex move to scalars. */
836 expand_complex_move (gimple_stmt_iterator
*gsi
, tree type
)
838 tree inner_type
= TREE_TYPE (type
);
840 gimple
*stmt
= gsi_stmt (*gsi
);
842 if (is_gimple_assign (stmt
))
844 lhs
= gimple_assign_lhs (stmt
);
845 if (gimple_num_ops (stmt
) == 2)
846 rhs
= gimple_assign_rhs1 (stmt
);
850 else if (is_gimple_call (stmt
))
852 lhs
= gimple_call_lhs (stmt
);
858 if (TREE_CODE (lhs
) == SSA_NAME
)
860 if (is_ctrl_altering_stmt (stmt
))
864 /* The value is not assigned on the exception edges, so we need not
865 concern ourselves there. We do need to update on the fallthru
867 e
= find_fallthru_edge (gsi_bb (*gsi
)->succs
);
871 r
= build1 (REALPART_EXPR
, inner_type
, lhs
);
872 i
= build1 (IMAGPART_EXPR
, inner_type
, lhs
);
873 update_complex_components_on_edge (e
, lhs
, r
, i
);
875 else if (is_gimple_call (stmt
)
876 || gimple_has_side_effects (stmt
))
878 r
= build1 (REALPART_EXPR
, inner_type
, lhs
);
879 i
= build1 (IMAGPART_EXPR
, inner_type
, lhs
);
880 update_complex_components (gsi
, stmt
, r
, i
);
884 if (gimple_assign_rhs_code (stmt
) != COMPLEX_EXPR
)
886 r
= extract_component (gsi
, rhs
, 0, true);
887 i
= extract_component (gsi
, rhs
, 1, true);
891 r
= gimple_assign_rhs1 (stmt
);
892 i
= gimple_assign_rhs2 (stmt
);
894 update_complex_assignment (gsi
, r
, i
);
898 && (TREE_CODE (rhs
) == SSA_NAME
|| TREE_CODE (rhs
) == COMPLEX_CST
)
899 && !TREE_SIDE_EFFECTS (lhs
))
905 loc
= gimple_location (stmt
);
906 r
= extract_component (gsi
, rhs
, 0, false);
907 i
= extract_component (gsi
, rhs
, 1, false);
909 x
= build1 (REALPART_EXPR
, inner_type
, unshare_expr (lhs
));
910 t
= gimple_build_assign (x
, r
);
911 gimple_set_location (t
, loc
);
912 gsi_insert_before (gsi
, t
, GSI_SAME_STMT
);
914 if (stmt
== gsi_stmt (*gsi
))
916 x
= build1 (IMAGPART_EXPR
, inner_type
, unshare_expr (lhs
));
917 gimple_assign_set_lhs (stmt
, x
);
918 gimple_assign_set_rhs1 (stmt
, i
);
922 x
= build1 (IMAGPART_EXPR
, inner_type
, unshare_expr (lhs
));
923 t
= gimple_build_assign (x
, i
);
924 gimple_set_location (t
, loc
);
925 gsi_insert_before (gsi
, t
, GSI_SAME_STMT
);
927 stmt
= gsi_stmt (*gsi
);
928 gcc_assert (gimple_code (stmt
) == GIMPLE_RETURN
);
929 gimple_return_set_retval (as_a
<greturn
*> (stmt
), lhs
);
936 /* Expand complex addition to scalars:
937 a + b = (ar + br) + i(ai + bi)
938 a - b = (ar - br) + i(ai + bi)
942 expand_complex_addition (gimple_stmt_iterator
*gsi
, tree inner_type
,
943 tree ar
, tree ai
, tree br
, tree bi
,
945 complex_lattice_t al
, complex_lattice_t bl
)
948 gimple_seq stmts
= NULL
;
949 location_t loc
= gimple_location (gsi_stmt (*gsi
));
951 switch (PAIR (al
, bl
))
953 case PAIR (ONLY_REAL
, ONLY_REAL
):
954 rr
= gimple_build (&stmts
, loc
, code
, inner_type
, ar
, br
);
958 case PAIR (ONLY_REAL
, ONLY_IMAG
):
960 if (code
== MINUS_EXPR
)
961 ri
= gimple_build (&stmts
, loc
, MINUS_EXPR
, inner_type
, ai
, bi
);
966 case PAIR (ONLY_IMAG
, ONLY_REAL
):
967 if (code
== MINUS_EXPR
)
968 rr
= gimple_build (&stmts
, loc
, MINUS_EXPR
, inner_type
, ar
, br
);
974 case PAIR (ONLY_IMAG
, ONLY_IMAG
):
976 ri
= gimple_build (&stmts
, loc
, code
, inner_type
, ai
, bi
);
979 case PAIR (VARYING
, ONLY_REAL
):
980 rr
= gimple_build (&stmts
, loc
, code
, inner_type
, ar
, br
);
984 case PAIR (VARYING
, ONLY_IMAG
):
986 ri
= gimple_build (&stmts
, loc
, code
, inner_type
, ai
, bi
);
989 case PAIR (ONLY_REAL
, VARYING
):
990 if (code
== MINUS_EXPR
)
992 rr
= gimple_build (&stmts
, loc
, code
, inner_type
, ar
, br
);
996 case PAIR (ONLY_IMAG
, VARYING
):
997 if (code
== MINUS_EXPR
)
1000 ri
= gimple_build (&stmts
, loc
, code
, inner_type
, ai
, bi
);
1003 case PAIR (VARYING
, VARYING
):
1005 rr
= gimple_build (&stmts
, loc
, code
, inner_type
, ar
, br
);
1006 /* (a+ai) + (b+bi) -> (a+b)+(a+b)i
1007 small optimization to remove one new statement. */
1008 if (operand_equal_p (ar
, ai
) && operand_equal_p (br
, bi
))
1011 ri
= gimple_build (&stmts
, loc
, code
, inner_type
, ai
, bi
);
1018 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1019 update_complex_assignment (gsi
, rr
, ri
);
1022 /* Expand a complex multiplication or division to a libcall to the c99
1023 compliant routines. TYPE is the complex type of the operation.
1024 If INPLACE_P replace the statement at GSI with
1025 the libcall and return NULL_TREE. Else insert the call, assign its
1026 result to an output variable and return that variable. If INPLACE_P
1027 is true then the statement being replaced should be an assignment
1031 expand_complex_libcall (gimple_stmt_iterator
*gsi
, tree type
, tree ar
, tree ai
,
1032 tree br
, tree bi
, enum tree_code code
, bool inplace_p
)
1035 enum built_in_function bcode
;
1039 mode
= TYPE_MODE (type
);
1040 gcc_assert (GET_MODE_CLASS (mode
) == MODE_COMPLEX_FLOAT
);
1042 if (code
== MULT_EXPR
)
1043 bcode
= ((enum built_in_function
)
1044 (BUILT_IN_COMPLEX_MUL_MIN
+ mode
- MIN_MODE_COMPLEX_FLOAT
));
1045 else if (code
== RDIV_EXPR
)
1046 bcode
= ((enum built_in_function
)
1047 (BUILT_IN_COMPLEX_DIV_MIN
+ mode
- MIN_MODE_COMPLEX_FLOAT
));
1050 fn
= builtin_decl_explicit (bcode
);
1051 stmt
= gimple_build_call (fn
, 4, ar
, ai
, br
, bi
);
1055 gimple
*old_stmt
= gsi_stmt (*gsi
);
1056 gimple_call_set_nothrow (stmt
, !stmt_could_throw_p (cfun
, old_stmt
));
1057 lhs
= gimple_assign_lhs (old_stmt
);
1058 gimple_call_set_lhs (stmt
, lhs
);
1059 gsi_replace (gsi
, stmt
, true);
1061 type
= TREE_TYPE (type
);
1062 if (stmt_can_throw_internal (cfun
, stmt
))
1066 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
1067 if (!(e
->flags
& EDGE_EH
))
1069 basic_block bb
= split_edge (e
);
1070 gimple_stmt_iterator gsi2
= gsi_start_bb (bb
);
1071 update_complex_components (&gsi2
, stmt
,
1072 build1 (REALPART_EXPR
, type
, lhs
),
1073 build1 (IMAGPART_EXPR
, type
, lhs
));
1077 update_complex_components (gsi
, stmt
,
1078 build1 (REALPART_EXPR
, type
, lhs
),
1079 build1 (IMAGPART_EXPR
, type
, lhs
));
1080 SSA_NAME_DEF_STMT (lhs
) = stmt
;
1084 gimple_call_set_nothrow (stmt
, true);
1085 lhs
= make_ssa_name (type
);
1086 gimple_call_set_lhs (stmt
, lhs
);
1087 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1092 /* Perform a complex multiplication on two complex constants A, B represented
1093 by AR, AI, BR, BI of type TYPE.
1094 The operation we want is: a * b = (ar*br - ai*bi) + i(ar*bi + br*ai).
1095 Insert the GIMPLE statements into GSI. Store the real and imaginary
1096 components of the result into RR and RI. */
1099 expand_complex_multiplication_components (gimple_seq
*stmts
, location_t loc
,
1100 tree type
, tree ar
, tree ai
,
1104 tree t1
, t2
, t3
, t4
;
1106 t1
= gimple_build (stmts
, loc
, MULT_EXPR
, type
, ar
, br
);
1107 t2
= gimple_build (stmts
, loc
, MULT_EXPR
, type
, ai
, bi
);
1108 t3
= gimple_build (stmts
, loc
, MULT_EXPR
, type
, ar
, bi
);
1110 /* Avoid expanding redundant multiplication for the common
1111 case of squaring a complex number. */
1112 if (ar
== br
&& ai
== bi
)
1115 t4
= gimple_build (stmts
, loc
, MULT_EXPR
, type
, ai
, br
);
1117 *rr
= gimple_build (stmts
, loc
, MINUS_EXPR
, type
, t1
, t2
);
1118 *ri
= gimple_build (stmts
, loc
, PLUS_EXPR
, type
, t3
, t4
);
1121 /* Expand complex multiplication to scalars:
1122 a * b = (ar*br - ai*bi) + i(ar*bi + br*ai)
1126 expand_complex_multiplication (gimple_stmt_iterator
*gsi
, tree type
,
1127 tree ar
, tree ai
, tree br
, tree bi
,
1128 complex_lattice_t al
, complex_lattice_t bl
)
1131 tree inner_type
= TREE_TYPE (type
);
1132 location_t loc
= gimple_location (gsi_stmt (*gsi
));
1133 gimple_seq stmts
= NULL
;
1137 complex_lattice_t tl
;
1138 rr
= ar
, ar
= br
, br
= rr
;
1139 ri
= ai
, ai
= bi
, bi
= ri
;
1140 tl
= al
, al
= bl
, bl
= tl
;
1143 switch (PAIR (al
, bl
))
1145 case PAIR (ONLY_REAL
, ONLY_REAL
):
1146 rr
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ar
, br
);
1150 case PAIR (ONLY_IMAG
, ONLY_REAL
):
1152 if (TREE_CODE (ai
) == REAL_CST
1153 && real_identical (&TREE_REAL_CST (ai
), &dconst1
))
1156 ri
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ai
, br
);
1159 case PAIR (ONLY_IMAG
, ONLY_IMAG
):
1160 rr
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ai
, bi
);
1161 rr
= gimple_build (&stmts
, loc
, NEGATE_EXPR
, inner_type
, rr
);
1165 case PAIR (VARYING
, ONLY_REAL
):
1166 rr
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ar
, br
);
1167 ri
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ai
, br
);
1170 case PAIR (VARYING
, ONLY_IMAG
):
1171 rr
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ai
, bi
);
1172 rr
= gimple_build (&stmts
, loc
, NEGATE_EXPR
, inner_type
, rr
);
1173 ri
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ar
, bi
);
1176 case PAIR (VARYING
, VARYING
):
1177 if (flag_complex_method
== 2 && SCALAR_FLOAT_TYPE_P (inner_type
))
1179 /* If optimizing for size or not at all just do a libcall.
1180 Same if there are exception-handling edges or signaling NaNs. */
1181 if (optimize
== 0 || optimize_bb_for_size_p (gsi_bb (*gsi
))
1182 || stmt_can_throw_internal (cfun
, gsi_stmt (*gsi
))
1183 || flag_signaling_nans
)
1185 expand_complex_libcall (gsi
, type
, ar
, ai
, br
, bi
,
1190 if (!HONOR_NANS (inner_type
))
1192 /* If we are not worrying about NaNs expand to
1193 (ar*br - ai*bi) + i(ar*bi + br*ai) directly. */
1194 expand_complex_multiplication_components (&stmts
, loc
, inner_type
,
1200 /* Else, expand x = a * b into
1201 x = (ar*br - ai*bi) + i(ar*bi + br*ai);
1202 if (isunordered (__real__ x, __imag__ x))
1203 x = __muldc3 (a, b); */
1206 expand_complex_multiplication_components (&stmts
, loc
,
1208 br
, bi
, &tmpr
, &tmpi
);
1209 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1213 = gimple_build_cond (UNORDERED_EXPR
, tmpr
, tmpi
,
1214 NULL_TREE
, NULL_TREE
);
1216 basic_block orig_bb
= gsi_bb (*gsi
);
1217 /* We want to keep track of the original complex multiplication
1218 statement as we're going to modify it later in
1219 update_complex_assignment. Make sure that insert_cond_bb leaves
1220 that statement in the join block. */
1223 = insert_cond_bb (gsi_bb (*gsi
), gsi_stmt (*gsi
), check
,
1224 profile_probability::very_unlikely ());
1226 gimple_stmt_iterator cond_bb_gsi
= gsi_last_bb (cond_bb
);
1227 gsi_insert_after (&cond_bb_gsi
, gimple_build_nop (), GSI_NEW_STMT
);
1230 = expand_complex_libcall (&cond_bb_gsi
, type
, ar
, ai
, br
,
1231 bi
, MULT_EXPR
, false);
1232 gimple_seq stmts2
= NULL
;
1233 tree cond_real
= gimple_build (&stmts2
, loc
, REALPART_EXPR
,
1234 inner_type
, libcall_res
);
1235 tree cond_imag
= gimple_build (&stmts2
, loc
, IMAGPART_EXPR
,
1236 inner_type
, libcall_res
);
1237 gsi_insert_seq_before (&cond_bb_gsi
, stmts2
, GSI_SAME_STMT
);
1239 basic_block join_bb
= single_succ_edge (cond_bb
)->dest
;
1240 *gsi
= gsi_start_nondebug_after_labels_bb (join_bb
);
1242 /* We have a conditional block with some assignments in cond_bb.
1243 Wire up the PHIs to wrap up. */
1244 rr
= make_ssa_name (inner_type
);
1245 ri
= make_ssa_name (inner_type
);
1246 edge cond_to_join
= single_succ_edge (cond_bb
);
1247 edge orig_to_join
= find_edge (orig_bb
, join_bb
);
1249 gphi
*real_phi
= create_phi_node (rr
, gsi_bb (*gsi
));
1250 add_phi_arg (real_phi
, cond_real
, cond_to_join
, UNKNOWN_LOCATION
);
1251 add_phi_arg (real_phi
, tmpr
, orig_to_join
, UNKNOWN_LOCATION
);
1253 gphi
*imag_phi
= create_phi_node (ri
, gsi_bb (*gsi
));
1254 add_phi_arg (imag_phi
, cond_imag
, cond_to_join
, UNKNOWN_LOCATION
);
1255 add_phi_arg (imag_phi
, tmpi
, orig_to_join
, UNKNOWN_LOCATION
);
1258 /* If we are not worrying about NaNs expand to
1259 (ar*br - ai*bi) + i(ar*bi + br*ai) directly. */
1260 expand_complex_multiplication_components (&stmts
, loc
,
1269 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1270 update_complex_assignment (gsi
, rr
, ri
);
1273 /* Keep this algorithm in sync with fold-const.cc:const_binop().
1275 Expand complex division to scalars, straightforward algorithm.
1276 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1281 expand_complex_div_straight (gimple_stmt_iterator
*gsi
, tree inner_type
,
1282 tree ar
, tree ai
, tree br
, tree bi
,
1283 enum tree_code code
)
1285 gimple_seq stmts
= NULL
;
1286 location_t loc
= gimple_location (gsi_stmt (*gsi
));
1287 tree rr
, ri
, div
, t1
, t2
, t3
;
1289 t1
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, br
, br
);
1290 t2
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, bi
, bi
);
1291 div
= gimple_build (&stmts
, loc
, PLUS_EXPR
, inner_type
, t1
, t2
);
1293 t1
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ar
, br
);
1294 t2
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ai
, bi
);
1295 t3
= gimple_build (&stmts
, loc
, PLUS_EXPR
, inner_type
, t1
, t2
);
1296 rr
= gimple_build (&stmts
, loc
, code
, inner_type
, t3
, div
);
1298 t1
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ai
, br
);
1299 t2
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ar
, bi
);
1300 t3
= gimple_build (&stmts
, loc
, MINUS_EXPR
, inner_type
, t1
, t2
);
1301 ri
= gimple_build (&stmts
, loc
, code
, inner_type
, t3
, div
);
1303 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1304 update_complex_assignment (gsi
, rr
, ri
);
1307 /* Keep this algorithm in sync with fold-const.cc:const_binop().
1309 Expand complex division to scalars, modified algorithm to minimize
1310 overflow with wide input ranges. */
1313 expand_complex_div_wide (gimple_stmt_iterator
*gsi
, tree inner_type
,
1314 tree ar
, tree ai
, tree br
, tree bi
,
1315 enum tree_code code
)
1317 tree rr
, ri
, ratio
, div
, t1
, t2
, tr
, ti
, compare
;
1318 basic_block bb_cond
, bb_true
, bb_false
, bb_join
;
1320 gimple_seq stmts
= NULL
;
1321 location_t loc
= gimple_location (gsi_stmt (*gsi
));
1323 /* Examine |br| < |bi|, and branch. */
1324 t1
= gimple_build (&stmts
, loc
, ABS_EXPR
, inner_type
, br
);
1325 t2
= gimple_build (&stmts
, loc
, ABS_EXPR
, inner_type
, bi
);
1326 compare
= gimple_build (&stmts
, loc
,
1327 LT_EXPR
, boolean_type_node
, t1
, t2
);
1329 bb_cond
= bb_true
= bb_false
= bb_join
= NULL
;
1330 rr
= ri
= tr
= ti
= NULL
;
1331 if (TREE_CODE (compare
) != INTEGER_CST
)
1336 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1338 stmt
= gimple_build_cond (NE_EXPR
, compare
, boolean_false_node
,
1339 NULL_TREE
, NULL_TREE
);
1340 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1342 /* Split the original block, and create the TRUE and FALSE blocks. */
1343 e
= split_block (gsi_bb (*gsi
), stmt
);
1346 bb_true
= create_empty_bb (bb_cond
);
1347 bb_false
= create_empty_bb (bb_true
);
1348 bb_true
->count
= bb_false
->count
1349 = bb_cond
->count
.apply_probability (profile_probability::even ());
1351 /* Wire the blocks together. */
1352 e
->flags
= EDGE_TRUE_VALUE
;
1353 /* TODO: With value profile we could add an historgram to determine real
1355 e
->probability
= profile_probability::even ();
1356 redirect_edge_succ (e
, bb_true
);
1357 edge e2
= make_edge (bb_cond
, bb_false
, EDGE_FALSE_VALUE
);
1358 e2
->probability
= profile_probability::even ();
1359 make_single_succ_edge (bb_true
, bb_join
, EDGE_FALLTHRU
);
1360 make_single_succ_edge (bb_false
, bb_join
, EDGE_FALLTHRU
);
1361 add_bb_to_loop (bb_true
, bb_cond
->loop_father
);
1362 add_bb_to_loop (bb_false
, bb_cond
->loop_father
);
1364 /* Update dominance info. Note that bb_join's data was
1365 updated by split_block. */
1366 if (dom_info_available_p (CDI_DOMINATORS
))
1368 set_immediate_dominator (CDI_DOMINATORS
, bb_true
, bb_cond
);
1369 set_immediate_dominator (CDI_DOMINATORS
, bb_false
, bb_cond
);
1372 rr
= create_tmp_reg (inner_type
);
1373 ri
= create_tmp_reg (inner_type
);
1377 gimple_seq_discard (stmts
);
1381 /* In the TRUE branch, we compute
1383 div = (br * ratio) + bi;
1384 tr = (ar * ratio) + ai;
1385 ti = (ai * ratio) - ar;
1388 if (bb_true
|| integer_nonzerop (compare
))
1392 *gsi
= gsi_last_bb (bb_true
);
1393 gsi_insert_after (gsi
, gimple_build_nop (), GSI_NEW_STMT
);
1396 ratio
= gimple_build (&stmts
, loc
, code
, inner_type
, br
, bi
);
1398 t1
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, br
, ratio
);
1399 div
= gimple_build (&stmts
, loc
, PLUS_EXPR
, inner_type
, t1
, bi
);
1401 t1
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ar
, ratio
);
1402 tr
= gimple_build (&stmts
, loc
, PLUS_EXPR
, inner_type
, t1
, ai
);
1404 t1
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ai
, ratio
);
1405 ti
= gimple_build (&stmts
, loc
, MINUS_EXPR
, inner_type
, t1
, ar
);
1407 tr
= gimple_build (&stmts
, loc
, code
, inner_type
, tr
, div
);
1408 ti
= gimple_build (&stmts
, loc
, code
, inner_type
, ti
, div
);
1409 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1414 stmt
= gimple_build_assign (rr
, tr
);
1415 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1416 stmt
= gimple_build_assign (ri
, ti
);
1417 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1418 gsi_remove (gsi
, true);
1422 /* In the FALSE branch, we compute
1424 divisor = (d * ratio) + c;
1425 tr = (b * ratio) + a;
1426 ti = b - (a * ratio);
1429 if (bb_false
|| integer_zerop (compare
))
1433 *gsi
= gsi_last_bb (bb_false
);
1434 gsi_insert_after (gsi
, gimple_build_nop (), GSI_NEW_STMT
);
1437 ratio
= gimple_build (&stmts
, loc
, code
, inner_type
, bi
, br
);
1439 t1
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, bi
, ratio
);
1440 div
= gimple_build (&stmts
, loc
, PLUS_EXPR
, inner_type
, t1
, br
);
1442 t1
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ai
, ratio
);
1443 tr
= gimple_build (&stmts
, loc
, PLUS_EXPR
, inner_type
, t1
, ar
);
1445 t1
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ar
, ratio
);
1446 ti
= gimple_build (&stmts
, loc
, MINUS_EXPR
, inner_type
, ai
, t1
);
1448 tr
= gimple_build (&stmts
, loc
, code
, inner_type
, tr
, div
);
1449 ti
= gimple_build (&stmts
, loc
, code
, inner_type
, ti
, div
);
1450 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1455 stmt
= gimple_build_assign (rr
, tr
);
1456 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1457 stmt
= gimple_build_assign (ri
, ti
);
1458 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1459 gsi_remove (gsi
, true);
1464 *gsi
= gsi_start_bb (bb_join
);
1468 update_complex_assignment (gsi
, rr
, ri
);
1471 /* Expand complex division to scalars. */
1474 expand_complex_division (gimple_stmt_iterator
*gsi
, tree type
,
1475 tree ar
, tree ai
, tree br
, tree bi
,
1476 enum tree_code code
,
1477 complex_lattice_t al
, complex_lattice_t bl
)
1480 gimple_seq stmts
= NULL
;
1481 location_t loc
= gimple_location (gsi_stmt (*gsi
));
1483 tree inner_type
= TREE_TYPE (type
);
1484 switch (PAIR (al
, bl
))
1486 case PAIR (ONLY_REAL
, ONLY_REAL
):
1487 rr
= gimple_build (&stmts
, loc
, code
, inner_type
, ar
, br
);
1491 case PAIR (ONLY_REAL
, ONLY_IMAG
):
1493 ri
= gimple_build (&stmts
, loc
, code
, inner_type
, ar
, bi
);
1494 ri
= gimple_build (&stmts
, loc
, NEGATE_EXPR
, inner_type
, ri
);
1497 case PAIR (ONLY_IMAG
, ONLY_REAL
):
1499 ri
= gimple_build (&stmts
, loc
, code
, inner_type
, ai
, br
);
1502 case PAIR (ONLY_IMAG
, ONLY_IMAG
):
1503 rr
= gimple_build (&stmts
, loc
, code
, inner_type
, ai
, bi
);
1507 case PAIR (VARYING
, ONLY_REAL
):
1508 rr
= gimple_build (&stmts
, loc
, code
, inner_type
, ar
, br
);
1509 ri
= gimple_build (&stmts
, loc
, code
, inner_type
, ai
, br
);
1512 case PAIR (VARYING
, ONLY_IMAG
):
1513 rr
= gimple_build (&stmts
, loc
, code
, inner_type
, ai
, bi
);
1514 ri
= gimple_build (&stmts
, loc
, code
, inner_type
, ar
, bi
);
1515 ri
= gimple_build (&stmts
, loc
, NEGATE_EXPR
, inner_type
, ri
);
1518 case PAIR (ONLY_REAL
, VARYING
):
1519 case PAIR (ONLY_IMAG
, VARYING
):
1520 case PAIR (VARYING
, VARYING
):
1521 switch (flag_complex_method
)
1524 /* straightforward implementation of complex divide acceptable. */
1525 expand_complex_div_straight (gsi
, inner_type
, ar
, ai
, br
, bi
, code
);
1529 if (SCALAR_FLOAT_TYPE_P (inner_type
))
1531 expand_complex_libcall (gsi
, type
, ar
, ai
, br
, bi
, code
, true);
1537 /* wide ranges of inputs must work for complex divide. */
1538 expand_complex_div_wide (gsi
, inner_type
, ar
, ai
, br
, bi
, code
);
1550 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1551 update_complex_assignment (gsi
, rr
, ri
);
1554 /* Expand complex negation to scalars:
1559 expand_complex_negation (gimple_stmt_iterator
*gsi
, tree inner_type
,
1563 gimple_seq stmts
= NULL
;
1564 location_t loc
= gimple_location (gsi_stmt (*gsi
));
1566 rr
= gimple_build (&stmts
, loc
, NEGATE_EXPR
, inner_type
, ar
);
1567 ri
= gimple_build (&stmts
, loc
, NEGATE_EXPR
, inner_type
, ai
);
1569 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1570 update_complex_assignment (gsi
, rr
, ri
);
1573 /* Expand complex paren to scalars:
1574 ((a)) = ((ar)) + i((ai))
1578 expand_complex_paren (gimple_stmt_iterator
*gsi
, tree inner_type
,
1582 gimple_seq stmts
= NULL
;
1583 location_t loc
= gimple_location (gsi_stmt (*gsi
));
1585 rr
= gimple_build (&stmts
, loc
, PAREN_EXPR
, inner_type
, ar
);
1586 ri
= gimple_build (&stmts
, loc
, PAREN_EXPR
, inner_type
, ai
);
1588 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1589 update_complex_assignment (gsi
, rr
, ri
);
1592 /* Expand complex conjugate to scalars:
1597 expand_complex_conjugate (gimple_stmt_iterator
*gsi
, tree inner_type
,
1601 gimple_seq stmts
= NULL
;
1602 location_t loc
= gimple_location (gsi_stmt (*gsi
));
1604 ri
= gimple_build (&stmts
, loc
, NEGATE_EXPR
, inner_type
, ai
);
1606 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1607 update_complex_assignment (gsi
, ar
, ri
);
1610 /* Expand complex comparison (EQ or NE only). */
1613 expand_complex_comparison (gimple_stmt_iterator
*gsi
, tree ar
, tree ai
,
1614 tree br
, tree bi
, enum tree_code code
)
1616 tree cr
, ci
, cc
, type
;
1617 gimple
*stmt
= gsi_stmt (*gsi
);
1618 gimple_seq stmts
= NULL
;
1619 location_t loc
= gimple_location (stmt
);
1621 cr
= gimple_build (&stmts
, loc
, code
, boolean_type_node
, ar
, br
);
1622 ci
= gimple_build (&stmts
, loc
, code
, boolean_type_node
, ai
, bi
);
1623 cc
= gimple_build (&stmts
, loc
,
1624 (code
== EQ_EXPR
? BIT_AND_EXPR
: BIT_IOR_EXPR
),
1625 boolean_type_node
, cr
, ci
);
1626 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1628 switch (gimple_code (stmt
))
1631 type
= TREE_TYPE (gimple_assign_lhs (stmt
));
1632 gimple_assign_set_rhs_from_tree (gsi
, fold_convert (type
, cc
));
1633 stmt
= gsi_stmt (*gsi
);
1638 gcond
*cond_stmt
= as_a
<gcond
*> (stmt
);
1639 gimple_cond_set_code (cond_stmt
, EQ_EXPR
);
1640 gimple_cond_set_lhs (cond_stmt
, cc
);
1641 gimple_cond_set_rhs (cond_stmt
, boolean_true_node
);
1650 if (maybe_clean_eh_stmt (stmt
))
1651 bitmap_set_bit (need_eh_cleanup
, gimple_bb (stmt
)->index
);
1654 /* Expand inline asm that sets some complex SSA_NAMEs. */
1657 expand_complex_asm (gimple_stmt_iterator
*gsi
)
1659 gasm
*stmt
= as_a
<gasm
*> (gsi_stmt (*gsi
));
1661 bool diagnosed_p
= false;
1663 for (i
= 0; i
< gimple_asm_noutputs (stmt
); ++i
)
1665 tree link
= gimple_asm_output_op (stmt
, i
);
1666 tree op
= TREE_VALUE (link
);
1667 if (TREE_CODE (op
) == SSA_NAME
1668 && TREE_CODE (TREE_TYPE (op
)) == COMPLEX_TYPE
)
1670 if (gimple_asm_nlabels (stmt
) > 0)
1674 sorry_at (gimple_location (stmt
),
1675 "%<asm goto%> with complex typed outputs");
1678 /* Make sure to not ICE later, see PR105165. */
1679 tree zero
= build_zero_cst (TREE_TYPE (TREE_TYPE (op
)));
1680 set_component_ssa_name (op
, false, zero
);
1681 set_component_ssa_name (op
, true, zero
);
1684 tree type
= TREE_TYPE (op
);
1685 tree inner_type
= TREE_TYPE (type
);
1686 tree r
= build1 (REALPART_EXPR
, inner_type
, op
);
1687 tree i
= build1 (IMAGPART_EXPR
, inner_type
, op
);
1688 gimple_seq list
= set_component_ssa_name (op
, false, r
);
1691 gsi_insert_seq_after (gsi
, list
, GSI_CONTINUE_LINKING
);
1693 list
= set_component_ssa_name (op
, true, i
);
1695 gsi_insert_seq_after (gsi
, list
, GSI_CONTINUE_LINKING
);
1701 /* ARG is the argument to a cabs builtin call in GSI from the
1702 original OLD_STMT. Create a sequence of statements prior
1703 to GSI that calculates sqrt(R*R + I*I), where R and
1704 I are the real and imaginary components of ARG, respectively. */
1707 gimple_expand_builtin_cabs (gimple_stmt_iterator
*gsi
, gimple
*old_stmt
)
1709 tree real_part
, imag_part
, addend1
, addend2
, sum
;
1710 tree arg
= gimple_call_arg (old_stmt
, 0);
1711 tree type
= TREE_TYPE (TREE_TYPE (arg
));
1712 machine_mode mode
= TYPE_MODE (type
);
1715 tree lhs
= gimple_call_lhs (old_stmt
);
1717 real_part
= extract_component (gsi
, arg
, false, true);
1718 imag_part
= extract_component (gsi
, arg
, true, true);
1719 location_t loc
= gimple_location (old_stmt
);
1721 gimple_seq stmts
= NULL
;
1723 /* cabs(x+0i) -> abs(x).
1724 cabs(0+xi) -> abs(x).
1725 These 2 can be done even without unsafe math optimizations. */
1726 if (real_zerop (imag_part
)
1727 || real_zerop (real_part
))
1729 tree other
= real_zerop (imag_part
) ? real_part
: imag_part
;
1730 sum
= gimple_build (&stmts
, loc
, ABS_EXPR
, type
, other
);
1731 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1732 new_stmt
= gimple_build_assign (lhs
, sum
);
1733 gimple_set_location (new_stmt
, loc
);
1734 gsi_replace (gsi
, new_stmt
, true);
1738 if (!flag_unsafe_math_optimizations
)
1741 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
1742 if (operand_equal_p (real_part
, imag_part
))
1744 tree sqrt2
= build_real_truncate (type
, dconst_sqrt2 ());
1745 sum
= gimple_build (&stmts
, loc
, ABS_EXPR
, type
, real_part
);
1746 sum
= gimple_build (&stmts
, loc
, MULT_EXPR
, type
, sum
, sqrt2
);
1747 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1748 new_stmt
= gimple_build_assign (lhs
, sum
);
1749 gimple_set_location (new_stmt
, loc
);
1750 gsi_replace (gsi
, new_stmt
, true);
1754 /* cabs(a+bi) -> sqrt(a*a+b*b) if sqrt exists on the target
1755 and optimizing for speed. */
1756 tree sqrtfn
= mathfn_built_in (type
, BUILT_IN_SQRT
);
1757 if (!optimize_bb_for_speed_p (gimple_bb (old_stmt
))
1759 || optab_handler (sqrt_optab
, mode
) == CODE_FOR_nothing
)
1762 addend1
= gimple_build (&stmts
, loc
, MULT_EXPR
, type
, real_part
, real_part
);
1763 addend2
= gimple_build (&stmts
, loc
, MULT_EXPR
, type
, imag_part
, imag_part
);
1764 sum
= gimple_build (&stmts
, loc
, PLUS_EXPR
, type
, addend1
, addend2
);
1765 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1767 /* Build the sqrt call. */
1768 new_stmt
= gimple_build_call (sqrtfn
, 1, sum
);
1769 gimple_set_location (new_stmt
, loc
);
1770 gimple_call_set_lhs (new_stmt
, lhs
);
1771 gsi_replace (gsi
, new_stmt
, true);
1774 /* Process one statement. If we identify a complex operation, expand it. */
1777 expand_complex_operations_1 (gimple_stmt_iterator
*gsi
)
1779 gimple
*stmt
= gsi_stmt (*gsi
);
1780 tree type
, inner_type
, lhs
;
1781 tree ac
, ar
, ai
, bc
, br
, bi
;
1782 complex_lattice_t al
, bl
;
1783 enum tree_code code
;
1784 if (gimple_code (stmt
) == GIMPLE_CALL
)
1786 switch (gimple_call_combined_fn (stmt
))
1789 gimple_expand_builtin_cabs (gsi
, stmt
);
1795 if (gimple_code (stmt
) == GIMPLE_ASM
)
1797 expand_complex_asm (gsi
);
1801 lhs
= gimple_get_lhs (stmt
);
1802 if (!lhs
&& gimple_code (stmt
) != GIMPLE_COND
)
1805 type
= TREE_TYPE (gimple_op (stmt
, 0));
1806 code
= gimple_expr_code (stmt
);
1808 /* Initial filter for operations we handle. */
1814 case TRUNC_DIV_EXPR
:
1816 case FLOOR_DIV_EXPR
:
1817 case ROUND_DIV_EXPR
:
1822 if (TREE_CODE (type
) != COMPLEX_TYPE
)
1824 inner_type
= TREE_TYPE (type
);
1829 /* Note, both GIMPLE_ASSIGN and GIMPLE_COND may have an EQ_EXPR
1830 subcode, so we need to access the operands using gimple_op. */
1831 inner_type
= TREE_TYPE (gimple_op (stmt
, 1));
1832 if (TREE_CODE (inner_type
) != COMPLEX_TYPE
)
1840 /* GIMPLE_COND may also fallthru here, but we do not need to
1841 do anything with it. */
1842 if (gimple_code (stmt
) == GIMPLE_COND
)
1845 if (TREE_CODE (type
) == COMPLEX_TYPE
)
1846 expand_complex_move (gsi
, type
);
1847 else if (is_gimple_assign (stmt
)
1848 && (gimple_assign_rhs_code (stmt
) == REALPART_EXPR
1849 || gimple_assign_rhs_code (stmt
) == IMAGPART_EXPR
)
1850 && TREE_CODE (lhs
) == SSA_NAME
)
1852 rhs
= gimple_assign_rhs1 (stmt
);
1853 rhs
= extract_component (gsi
, TREE_OPERAND (rhs
, 0),
1854 gimple_assign_rhs_code (stmt
)
1857 gimple_assign_set_rhs_from_tree (gsi
, rhs
);
1858 stmt
= gsi_stmt (*gsi
);
1865 /* Extract the components of the two complex values. Make sure and
1866 handle the common case of the same value used twice specially. */
1867 if (is_gimple_assign (stmt
))
1869 ac
= gimple_assign_rhs1 (stmt
);
1870 bc
= (gimple_num_ops (stmt
) > 2) ? gimple_assign_rhs2 (stmt
) : NULL
;
1872 /* GIMPLE_CALL cannot get here. */
1875 ac
= gimple_cond_lhs (stmt
);
1876 bc
= gimple_cond_rhs (stmt
);
1879 ar
= extract_component (gsi
, ac
, false, true);
1880 ai
= extract_component (gsi
, ac
, true, true);
1886 br
= extract_component (gsi
, bc
, 0, true);
1887 bi
= extract_component (gsi
, bc
, 1, true);
1890 br
= bi
= NULL_TREE
;
1892 al
= find_lattice_value (ac
);
1893 if (al
== UNINITIALIZED
)
1896 if (TREE_CODE_CLASS (code
) == tcc_unary
)
1902 bl
= find_lattice_value (bc
);
1903 if (bl
== UNINITIALIZED
)
1911 expand_complex_addition (gsi
, inner_type
, ar
, ai
, br
, bi
, code
, al
, bl
);
1915 expand_complex_multiplication (gsi
, type
, ar
, ai
, br
, bi
, al
, bl
);
1918 case TRUNC_DIV_EXPR
:
1920 case FLOOR_DIV_EXPR
:
1921 case ROUND_DIV_EXPR
:
1923 expand_complex_division (gsi
, type
, ar
, ai
, br
, bi
, code
, al
, bl
);
1927 expand_complex_negation (gsi
, inner_type
, ar
, ai
);
1931 expand_complex_conjugate (gsi
, inner_type
, ar
, ai
);
1936 expand_complex_comparison (gsi
, ar
, ai
, br
, bi
, code
);
1940 expand_complex_paren (gsi
, inner_type
, ar
, ai
);
1949 /* Entry point for complex operation lowering during optimization. */
1952 tree_lower_complex (void)
1954 gimple_stmt_iterator gsi
;
1959 if (!init_dont_simulate_again ())
1962 complex_lattice_values
.create (num_ssa_names
);
1963 complex_lattice_values
.safe_grow_cleared (num_ssa_names
, true);
1965 init_parameter_lattice_values ();
1966 class complex_propagate complex_propagate
;
1967 complex_propagate
.ssa_propagate ();
1969 need_eh_cleanup
= BITMAP_ALLOC (NULL
);
1970 dce_worklist
= BITMAP_ALLOC (NULL
);
1972 complex_variable_components
= new int_tree_htab_type (10);
1974 complex_ssa_name_components
.create (2 * num_ssa_names
);
1975 complex_ssa_name_components
.safe_grow_cleared (2 * num_ssa_names
, true);
1977 update_parameter_components ();
1979 rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
1980 n_bbs
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
1981 for (i
= 0; i
< n_bbs
; i
++)
1983 bb
= BASIC_BLOCK_FOR_FN (cfun
, rpo
[i
]);
1986 update_phi_components (bb
);
1987 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1988 expand_complex_operations_1 (&gsi
);
1993 if (!phis_to_revisit
.is_empty ())
1995 unsigned int n
= phis_to_revisit
.length ();
1996 for (unsigned int j
= 0; j
< n
; j
+= 3)
1997 for (unsigned int k
= 0; k
< 2; k
++)
1998 if (gphi
*phi
= phis_to_revisit
[j
+ k
+ 1])
2000 unsigned int m
= gimple_phi_num_args (phi
);
2001 for (unsigned int l
= 0; l
< m
; ++l
)
2003 tree op
= gimple_phi_arg_def (phi
, l
);
2004 if (TREE_CODE (op
) == SSA_NAME
2005 || is_gimple_min_invariant (op
))
2007 tree arg
= gimple_phi_arg_def (phis_to_revisit
[j
], l
);
2008 op
= extract_component (NULL
, arg
, k
> 0, false, false);
2009 SET_PHI_ARG_DEF (phi
, l
, op
);
2012 phis_to_revisit
.release ();
2015 gsi_commit_edge_inserts ();
2017 simple_dce_from_worklist (dce_worklist
, need_eh_cleanup
);
2018 BITMAP_FREE (dce_worklist
);
2021 = gimple_purge_all_dead_eh_edges (need_eh_cleanup
) ? TODO_cleanup_cfg
: 0;
2022 BITMAP_FREE (need_eh_cleanup
);
2024 delete complex_variable_components
;
2025 complex_variable_components
= NULL
;
2026 complex_ssa_name_components
.release ();
2027 complex_lattice_values
.release ();
2033 const pass_data pass_data_lower_complex
=
2035 GIMPLE_PASS
, /* type */
2036 "cplxlower", /* name */
2037 OPTGROUP_NONE
, /* optinfo_flags */
2038 TV_NONE
, /* tv_id */
2039 PROP_ssa
, /* properties_required */
2040 PROP_gimple_lcx
, /* properties_provided */
2041 0, /* properties_destroyed */
2042 0, /* todo_flags_start */
2043 TODO_update_ssa
, /* todo_flags_finish */
2046 class pass_lower_complex
: public gimple_opt_pass
2049 pass_lower_complex (gcc::context
*ctxt
)
2050 : gimple_opt_pass (pass_data_lower_complex
, ctxt
)
2053 /* opt_pass methods: */
2054 opt_pass
* clone () final override
{ return new pass_lower_complex (m_ctxt
); }
2055 unsigned int execute (function
*) final override
2057 return tree_lower_complex ();
2060 }; // class pass_lower_complex
2065 make_pass_lower_complex (gcc::context
*ctxt
)
2067 return new pass_lower_complex (ctxt
);
2073 const pass_data pass_data_lower_complex_O0
=
2075 GIMPLE_PASS
, /* type */
2076 "cplxlower0", /* name */
2077 OPTGROUP_NONE
, /* optinfo_flags */
2078 TV_NONE
, /* tv_id */
2079 PROP_cfg
, /* properties_required */
2080 PROP_gimple_lcx
, /* properties_provided */
2081 0, /* properties_destroyed */
2082 0, /* todo_flags_start */
2083 TODO_update_ssa
, /* todo_flags_finish */
2086 class pass_lower_complex_O0
: public gimple_opt_pass
2089 pass_lower_complex_O0 (gcc::context
*ctxt
)
2090 : gimple_opt_pass (pass_data_lower_complex_O0
, ctxt
)
2093 /* opt_pass methods: */
2094 bool gate (function
*fun
) final override
2096 /* With errors, normal optimization passes are not run. If we don't
2097 lower complex operations at all, rtl expansion will abort. */
2098 return !(fun
->curr_properties
& PROP_gimple_lcx
);
2101 unsigned int execute (function
*) final override
2103 return tree_lower_complex ();
2106 }; // class pass_lower_complex_O0
2111 make_pass_lower_complex_O0 (gcc::context
*ctxt
)
2113 return new pass_lower_complex_O0 (ctxt
);