1 /* Lower complex number operations to scalar operations.
2 Copyright (C) 2004-2022 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;
303 prop_set_simulate_again (stmt
, sim_again_p
);
307 return saw_a_complex_op
;
311 /* Evaluate statement STMT against the complex lattice defined above. */
314 complex_propagate::visit_stmt (gimple
*stmt
, edge
*taken_edge_p ATTRIBUTE_UNUSED
,
317 complex_lattice_t new_l
, old_l
, op1_l
, op2_l
;
321 lhs
= gimple_get_lhs (stmt
);
322 /* Skip anything but GIMPLE_ASSIGN and GIMPLE_CALL with a lhs. */
323 if (!lhs
|| SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs
))
324 return SSA_PROP_VARYING
;
326 /* These conditions should be satisfied due to the initial filter
327 set up in init_dont_simulate_again. */
328 gcc_assert (TREE_CODE (lhs
) == SSA_NAME
);
329 gcc_assert (TREE_CODE (TREE_TYPE (lhs
)) == COMPLEX_TYPE
);
332 ver
= SSA_NAME_VERSION (lhs
);
333 old_l
= complex_lattice_values
[ver
];
335 switch (gimple_expr_code (stmt
))
339 new_l
= find_lattice_value (gimple_assign_rhs1 (stmt
));
343 new_l
= find_lattice_value_parts (gimple_assign_rhs1 (stmt
),
344 gimple_assign_rhs2 (stmt
));
349 op1_l
= find_lattice_value (gimple_assign_rhs1 (stmt
));
350 op2_l
= find_lattice_value (gimple_assign_rhs2 (stmt
));
352 /* We've set up the lattice values such that IOR neatly
354 new_l
= op1_l
| op2_l
;
363 op1_l
= find_lattice_value (gimple_assign_rhs1 (stmt
));
364 op2_l
= find_lattice_value (gimple_assign_rhs2 (stmt
));
366 /* Obviously, if either varies, so does the result. */
367 if (op1_l
== VARYING
|| op2_l
== VARYING
)
369 /* Don't prematurely promote variables if we've not yet seen
371 else if (op1_l
== UNINITIALIZED
)
373 else if (op2_l
== UNINITIALIZED
)
377 /* At this point both numbers have only one component. If the
378 numbers are of opposite kind, the result is imaginary,
379 otherwise the result is real. The add/subtract translates
380 the real/imag from/to 0/1; the ^ performs the comparison. */
381 new_l
= ((op1_l
- ONLY_REAL
) ^ (op2_l
- ONLY_REAL
)) + ONLY_REAL
;
383 /* Don't allow the lattice value to flip-flop indefinitely. */
390 new_l
= find_lattice_value (gimple_assign_rhs1 (stmt
));
398 /* If nothing changed this round, let the propagator know. */
400 return SSA_PROP_NOT_INTERESTING
;
402 complex_lattice_values
[ver
] = new_l
;
403 return new_l
== VARYING
? SSA_PROP_VARYING
: SSA_PROP_INTERESTING
;
406 /* Evaluate a PHI node against the complex lattice defined above. */
409 complex_propagate::visit_phi (gphi
*phi
)
411 complex_lattice_t new_l
, old_l
;
416 lhs
= gimple_phi_result (phi
);
418 /* This condition should be satisfied due to the initial filter
419 set up in init_dont_simulate_again. */
420 gcc_assert (TREE_CODE (TREE_TYPE (lhs
)) == COMPLEX_TYPE
);
422 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs
))
423 return SSA_PROP_VARYING
;
425 /* We've set up the lattice values such that IOR neatly models PHI meet. */
426 new_l
= UNINITIALIZED
;
427 for (i
= gimple_phi_num_args (phi
) - 1; i
>= 0; --i
)
428 new_l
|= find_lattice_value (gimple_phi_arg_def (phi
, i
));
430 ver
= SSA_NAME_VERSION (lhs
);
431 old_l
= complex_lattice_values
[ver
];
434 return SSA_PROP_NOT_INTERESTING
;
436 complex_lattice_values
[ver
] = new_l
;
437 return new_l
== VARYING
? SSA_PROP_VARYING
: SSA_PROP_INTERESTING
;
440 /* Create one backing variable for a complex component of ORIG. */
443 create_one_component_var (tree type
, tree orig
, const char *prefix
,
444 const char *suffix
, enum tree_code code
)
446 tree r
= create_tmp_var (type
, prefix
);
448 DECL_SOURCE_LOCATION (r
) = DECL_SOURCE_LOCATION (orig
);
449 DECL_ARTIFICIAL (r
) = 1;
451 if (DECL_NAME (orig
) && !DECL_IGNORED_P (orig
))
453 const char *name
= IDENTIFIER_POINTER (DECL_NAME (orig
));
454 name
= ACONCAT ((name
, suffix
, NULL
));
455 DECL_NAME (r
) = get_identifier (name
);
457 SET_DECL_DEBUG_EXPR (r
, build1 (code
, type
, orig
));
458 DECL_HAS_DEBUG_EXPR_P (r
) = 1;
459 DECL_IGNORED_P (r
) = 0;
460 copy_warning (r
, orig
);
464 DECL_IGNORED_P (r
) = 1;
465 suppress_warning (r
);
471 /* Retrieve a value for a complex component of VAR. */
474 get_component_var (tree var
, bool imag_p
)
476 size_t decl_index
= DECL_UID (var
) * 2 + imag_p
;
477 tree ret
= cvc_lookup (decl_index
);
481 ret
= create_one_component_var (TREE_TYPE (TREE_TYPE (var
)), var
,
482 imag_p
? "CI" : "CR",
483 imag_p
? "$imag" : "$real",
484 imag_p
? IMAGPART_EXPR
: REALPART_EXPR
);
485 cvc_insert (decl_index
, ret
);
491 /* Retrieve a value for a complex component of SSA_NAME. */
494 get_component_ssa_name (tree ssa_name
, bool imag_p
)
496 complex_lattice_t lattice
= find_lattice_value (ssa_name
);
497 size_t ssa_name_index
;
500 if (lattice
== (imag_p
? ONLY_REAL
: ONLY_IMAG
))
502 tree inner_type
= TREE_TYPE (TREE_TYPE (ssa_name
));
503 if (SCALAR_FLOAT_TYPE_P (inner_type
))
504 return build_real (inner_type
, dconst0
);
506 return build_int_cst (inner_type
, 0);
509 ssa_name_index
= SSA_NAME_VERSION (ssa_name
) * 2 + imag_p
;
510 ret
= complex_ssa_name_components
[ssa_name_index
];
513 if (SSA_NAME_VAR (ssa_name
))
514 ret
= get_component_var (SSA_NAME_VAR (ssa_name
), imag_p
);
516 ret
= TREE_TYPE (TREE_TYPE (ssa_name
));
517 ret
= make_ssa_name (ret
);
519 /* Copy some properties from the original. In particular, whether it
520 is used in an abnormal phi, and whether it's uninitialized. */
521 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ret
)
522 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name
);
523 if (SSA_NAME_IS_DEFAULT_DEF (ssa_name
)
524 && TREE_CODE (SSA_NAME_VAR (ssa_name
)) == VAR_DECL
)
526 SSA_NAME_DEF_STMT (ret
) = SSA_NAME_DEF_STMT (ssa_name
);
527 set_ssa_default_def (cfun
, SSA_NAME_VAR (ret
), ret
);
530 complex_ssa_name_components
[ssa_name_index
] = ret
;
536 /* Set a value for a complex component of SSA_NAME, return a
537 gimple_seq of stuff that needs doing. */
540 set_component_ssa_name (tree ssa_name
, bool imag_p
, tree value
)
542 complex_lattice_t lattice
= find_lattice_value (ssa_name
);
543 size_t ssa_name_index
;
548 /* We know the value must be zero, else there's a bug in our lattice
549 analysis. But the value may well be a variable known to contain
550 zero. We should be safe ignoring it. */
551 if (lattice
== (imag_p
? ONLY_REAL
: ONLY_IMAG
))
554 /* If we've already assigned an SSA_NAME to this component, then this
555 means that our walk of the basic blocks found a use before the set.
556 This is fine. Now we should create an initialization for the value
557 we created earlier. */
558 ssa_name_index
= SSA_NAME_VERSION (ssa_name
) * 2 + imag_p
;
559 comp
= complex_ssa_name_components
[ssa_name_index
];
563 /* If we've nothing assigned, and the value we're given is already stable,
564 then install that as the value for this SSA_NAME. This preemptively
565 copy-propagates the value, which avoids unnecessary memory allocation. */
566 else if (is_gimple_min_invariant (value
)
567 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name
))
569 complex_ssa_name_components
[ssa_name_index
] = value
;
572 else if (TREE_CODE (value
) == SSA_NAME
573 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name
))
575 /* Replace an anonymous base value with the variable from cvc_lookup.
576 This should result in better debug info. */
577 if (!SSA_NAME_IS_DEFAULT_DEF (value
)
578 && SSA_NAME_VAR (ssa_name
)
579 && (!SSA_NAME_VAR (value
) || DECL_IGNORED_P (SSA_NAME_VAR (value
)))
580 && !DECL_IGNORED_P (SSA_NAME_VAR (ssa_name
)))
582 comp
= get_component_var (SSA_NAME_VAR (ssa_name
), imag_p
);
583 replace_ssa_name_symbol (value
, comp
);
586 complex_ssa_name_components
[ssa_name_index
] = value
;
590 /* Finally, we need to stabilize the result by installing the value into
593 comp
= get_component_ssa_name (ssa_name
, imag_p
);
595 /* Do all the work to assign VALUE to COMP. */
597 value
= force_gimple_operand (value
, &list
, false, NULL
);
598 last
= gimple_build_assign (comp
, value
);
599 gimple_seq_add_stmt (&list
, last
);
600 gcc_assert (SSA_NAME_DEF_STMT (comp
) == last
);
605 /* Extract the real or imaginary part of a complex variable or constant.
606 Make sure that it's a proper gimple_val and gimplify it if not.
607 Emit any new code before gsi. */
610 extract_component (gimple_stmt_iterator
*gsi
, tree t
, bool imagpart_p
,
611 bool gimple_p
, bool phiarg_p
= false)
613 switch (TREE_CODE (t
))
616 return imagpart_p
? TREE_IMAGPART (t
) : TREE_REALPART (t
);
623 tree inner_type
= TREE_TYPE (TREE_TYPE (t
));
624 t
= unshare_expr (t
);
625 TREE_TYPE (t
) = inner_type
;
626 TREE_OPERAND (t
, 1) = TYPE_SIZE (inner_type
);
628 TREE_OPERAND (t
, 2) = size_binop (PLUS_EXPR
, TREE_OPERAND (t
, 2),
629 TYPE_SIZE (inner_type
));
631 t
= force_gimple_operand_gsi (gsi
, t
, true, NULL
, true,
641 case VIEW_CONVERT_EXPR
:
644 tree inner_type
= TREE_TYPE (TREE_TYPE (t
));
646 t
= build1 ((imagpart_p
? IMAGPART_EXPR
: REALPART_EXPR
),
647 inner_type
, unshare_expr (t
));
650 t
= force_gimple_operand_gsi (gsi
, t
, true, NULL
, true,
657 t
= get_component_ssa_name (t
, imagpart_p
);
658 if (TREE_CODE (t
) == SSA_NAME
&& SSA_NAME_DEF_STMT (t
) == NULL
)
659 gcc_assert (phiarg_p
);
667 /* Update the complex components of the ssa name on the lhs of STMT. */
670 update_complex_components (gimple_stmt_iterator
*gsi
, gimple
*stmt
, tree r
,
676 lhs
= gimple_get_lhs (stmt
);
678 list
= set_component_ssa_name (lhs
, false, r
);
680 gsi_insert_seq_after (gsi
, list
, GSI_CONTINUE_LINKING
);
682 list
= set_component_ssa_name (lhs
, true, i
);
684 gsi_insert_seq_after (gsi
, list
, GSI_CONTINUE_LINKING
);
688 update_complex_components_on_edge (edge e
, tree lhs
, tree r
, tree i
)
692 list
= set_component_ssa_name (lhs
, false, r
);
694 gsi_insert_seq_on_edge (e
, list
);
696 list
= set_component_ssa_name (lhs
, true, i
);
698 gsi_insert_seq_on_edge (e
, list
);
702 /* Update an assignment to a complex variable in place. */
705 update_complex_assignment (gimple_stmt_iterator
*gsi
, tree r
, tree i
)
707 gimple
*old_stmt
= gsi_stmt (*gsi
);
708 gimple_assign_set_rhs_with_ops (gsi
, COMPLEX_EXPR
, r
, i
);
709 gimple
*stmt
= gsi_stmt (*gsi
);
711 if (maybe_clean_or_replace_eh_stmt (old_stmt
, stmt
))
712 bitmap_set_bit (need_eh_cleanup
, gimple_bb (stmt
)->index
);
714 update_complex_components (gsi
, gsi_stmt (*gsi
), r
, i
);
718 /* Generate code at the entry point of the function to initialize the
719 component variables for a complex parameter. */
722 update_parameter_components (void)
724 edge entry_edge
= single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
727 for (parm
= DECL_ARGUMENTS (cfun
->decl
); parm
; parm
= DECL_CHAIN (parm
))
729 tree type
= TREE_TYPE (parm
);
732 if (TREE_CODE (type
) != COMPLEX_TYPE
|| !is_gimple_reg (parm
))
735 type
= TREE_TYPE (type
);
736 ssa_name
= ssa_default_def (cfun
, parm
);
740 r
= build1 (REALPART_EXPR
, type
, ssa_name
);
741 i
= build1 (IMAGPART_EXPR
, type
, ssa_name
);
742 update_complex_components_on_edge (entry_edge
, ssa_name
, r
, i
);
746 /* Generate code to set the component variables of a complex variable
747 to match the PHI statements in block BB. */
750 update_phi_components (basic_block bb
)
754 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
756 gphi
*phi
= gsi
.phi ();
758 if (is_complex_reg (gimple_phi_result (phi
)))
760 gphi
*p
[2] = { NULL
, NULL
};
761 unsigned int i
, j
, n
;
762 bool revisit_phi
= false;
764 for (j
= 0; j
< 2; j
++)
766 tree l
= get_component_ssa_name (gimple_phi_result (phi
), j
> 0);
767 if (TREE_CODE (l
) == SSA_NAME
)
768 p
[j
] = create_phi_node (l
, bb
);
771 for (i
= 0, n
= gimple_phi_num_args (phi
); i
< n
; ++i
)
773 tree comp
, arg
= gimple_phi_arg_def (phi
, i
);
774 for (j
= 0; j
< 2; j
++)
777 comp
= extract_component (NULL
, arg
, j
> 0, false, true);
778 if (TREE_CODE (comp
) == SSA_NAME
779 && SSA_NAME_DEF_STMT (comp
) == NULL
)
781 /* For the benefit of any gimple simplification during
782 this pass that might walk SSA_NAME def stmts,
783 don't add SSA_NAMEs without definitions into the
784 PHI arguments, but put a decl in there instead
785 temporarily, and revisit this PHI later on. */
786 if (SSA_NAME_VAR (comp
))
787 comp
= SSA_NAME_VAR (comp
);
789 comp
= create_tmp_reg (TREE_TYPE (comp
),
793 SET_PHI_ARG_DEF (p
[j
], i
, comp
);
799 phis_to_revisit
.safe_push (phi
);
800 phis_to_revisit
.safe_push (p
[0]);
801 phis_to_revisit
.safe_push (p
[1]);
807 /* Expand a complex move to scalars. */
810 expand_complex_move (gimple_stmt_iterator
*gsi
, tree type
)
812 tree inner_type
= TREE_TYPE (type
);
814 gimple
*stmt
= gsi_stmt (*gsi
);
816 if (is_gimple_assign (stmt
))
818 lhs
= gimple_assign_lhs (stmt
);
819 if (gimple_num_ops (stmt
) == 2)
820 rhs
= gimple_assign_rhs1 (stmt
);
824 else if (is_gimple_call (stmt
))
826 lhs
= gimple_call_lhs (stmt
);
832 if (TREE_CODE (lhs
) == SSA_NAME
)
834 if (is_ctrl_altering_stmt (stmt
))
838 /* The value is not assigned on the exception edges, so we need not
839 concern ourselves there. We do need to update on the fallthru
841 e
= find_fallthru_edge (gsi_bb (*gsi
)->succs
);
845 r
= build1 (REALPART_EXPR
, inner_type
, lhs
);
846 i
= build1 (IMAGPART_EXPR
, inner_type
, lhs
);
847 update_complex_components_on_edge (e
, lhs
, r
, i
);
849 else if (is_gimple_call (stmt
)
850 || gimple_has_side_effects (stmt
)
851 || gimple_assign_rhs_code (stmt
) == PAREN_EXPR
)
853 r
= build1 (REALPART_EXPR
, inner_type
, lhs
);
854 i
= build1 (IMAGPART_EXPR
, inner_type
, lhs
);
855 update_complex_components (gsi
, stmt
, r
, i
);
859 if (gimple_assign_rhs_code (stmt
) != COMPLEX_EXPR
)
861 r
= extract_component (gsi
, rhs
, 0, true);
862 i
= extract_component (gsi
, rhs
, 1, true);
866 r
= gimple_assign_rhs1 (stmt
);
867 i
= gimple_assign_rhs2 (stmt
);
869 update_complex_assignment (gsi
, r
, i
);
872 else if (rhs
&& TREE_CODE (rhs
) == SSA_NAME
&& !TREE_SIDE_EFFECTS (lhs
))
878 loc
= gimple_location (stmt
);
879 r
= extract_component (gsi
, rhs
, 0, false);
880 i
= extract_component (gsi
, rhs
, 1, false);
882 x
= build1 (REALPART_EXPR
, inner_type
, unshare_expr (lhs
));
883 t
= gimple_build_assign (x
, r
);
884 gimple_set_location (t
, loc
);
885 gsi_insert_before (gsi
, t
, GSI_SAME_STMT
);
887 if (stmt
== gsi_stmt (*gsi
))
889 x
= build1 (IMAGPART_EXPR
, inner_type
, unshare_expr (lhs
));
890 gimple_assign_set_lhs (stmt
, x
);
891 gimple_assign_set_rhs1 (stmt
, i
);
895 x
= build1 (IMAGPART_EXPR
, inner_type
, unshare_expr (lhs
));
896 t
= gimple_build_assign (x
, i
);
897 gimple_set_location (t
, loc
);
898 gsi_insert_before (gsi
, t
, GSI_SAME_STMT
);
900 stmt
= gsi_stmt (*gsi
);
901 gcc_assert (gimple_code (stmt
) == GIMPLE_RETURN
);
902 gimple_return_set_retval (as_a
<greturn
*> (stmt
), lhs
);
909 /* Expand complex addition to scalars:
910 a + b = (ar + br) + i(ai + bi)
911 a - b = (ar - br) + i(ai + bi)
915 expand_complex_addition (gimple_stmt_iterator
*gsi
, tree inner_type
,
916 tree ar
, tree ai
, tree br
, tree bi
,
918 complex_lattice_t al
, complex_lattice_t bl
)
921 gimple_seq stmts
= NULL
;
922 location_t loc
= gimple_location (gsi_stmt (*gsi
));
924 switch (PAIR (al
, bl
))
926 case PAIR (ONLY_REAL
, ONLY_REAL
):
927 rr
= gimple_build (&stmts
, loc
, code
, inner_type
, ar
, br
);
931 case PAIR (ONLY_REAL
, ONLY_IMAG
):
933 if (code
== MINUS_EXPR
)
934 ri
= gimple_build (&stmts
, loc
, MINUS_EXPR
, inner_type
, ai
, bi
);
939 case PAIR (ONLY_IMAG
, ONLY_REAL
):
940 if (code
== MINUS_EXPR
)
941 rr
= gimple_build (&stmts
, loc
, MINUS_EXPR
, inner_type
, ar
, br
);
947 case PAIR (ONLY_IMAG
, ONLY_IMAG
):
949 ri
= gimple_build (&stmts
, loc
, code
, inner_type
, ai
, bi
);
952 case PAIR (VARYING
, ONLY_REAL
):
953 rr
= gimple_build (&stmts
, loc
, code
, inner_type
, ar
, br
);
957 case PAIR (VARYING
, ONLY_IMAG
):
959 ri
= gimple_build (&stmts
, loc
, code
, inner_type
, ai
, bi
);
962 case PAIR (ONLY_REAL
, VARYING
):
963 if (code
== MINUS_EXPR
)
965 rr
= gimple_build (&stmts
, loc
, code
, inner_type
, ar
, br
);
969 case PAIR (ONLY_IMAG
, VARYING
):
970 if (code
== MINUS_EXPR
)
973 ri
= gimple_build (&stmts
, loc
, code
, inner_type
, ai
, bi
);
976 case PAIR (VARYING
, VARYING
):
978 rr
= gimple_build (&stmts
, loc
, code
, inner_type
, ar
, br
);
979 ri
= gimple_build (&stmts
, loc
, code
, inner_type
, ai
, bi
);
986 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
987 update_complex_assignment (gsi
, rr
, ri
);
990 /* Expand a complex multiplication or division to a libcall to the c99
991 compliant routines. TYPE is the complex type of the operation.
992 If INPLACE_P replace the statement at GSI with
993 the libcall and return NULL_TREE. Else insert the call, assign its
994 result to an output variable and return that variable. If INPLACE_P
995 is true then the statement being replaced should be an assignment
999 expand_complex_libcall (gimple_stmt_iterator
*gsi
, tree type
, tree ar
, tree ai
,
1000 tree br
, tree bi
, enum tree_code code
, bool inplace_p
)
1003 enum built_in_function bcode
;
1007 mode
= TYPE_MODE (type
);
1008 gcc_assert (GET_MODE_CLASS (mode
) == MODE_COMPLEX_FLOAT
);
1010 if (code
== MULT_EXPR
)
1011 bcode
= ((enum built_in_function
)
1012 (BUILT_IN_COMPLEX_MUL_MIN
+ mode
- MIN_MODE_COMPLEX_FLOAT
));
1013 else if (code
== RDIV_EXPR
)
1014 bcode
= ((enum built_in_function
)
1015 (BUILT_IN_COMPLEX_DIV_MIN
+ mode
- MIN_MODE_COMPLEX_FLOAT
));
1018 fn
= builtin_decl_explicit (bcode
);
1019 stmt
= gimple_build_call (fn
, 4, ar
, ai
, br
, bi
);
1023 gimple
*old_stmt
= gsi_stmt (*gsi
);
1024 gimple_call_set_nothrow (stmt
, !stmt_could_throw_p (cfun
, old_stmt
));
1025 lhs
= gimple_assign_lhs (old_stmt
);
1026 gimple_call_set_lhs (stmt
, lhs
);
1027 gsi_replace (gsi
, stmt
, true);
1029 type
= TREE_TYPE (type
);
1030 if (stmt_can_throw_internal (cfun
, stmt
))
1034 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
1035 if (!(e
->flags
& EDGE_EH
))
1037 basic_block bb
= split_edge (e
);
1038 gimple_stmt_iterator gsi2
= gsi_start_bb (bb
);
1039 update_complex_components (&gsi2
, stmt
,
1040 build1 (REALPART_EXPR
, type
, lhs
),
1041 build1 (IMAGPART_EXPR
, type
, lhs
));
1045 update_complex_components (gsi
, stmt
,
1046 build1 (REALPART_EXPR
, type
, lhs
),
1047 build1 (IMAGPART_EXPR
, type
, lhs
));
1048 SSA_NAME_DEF_STMT (lhs
) = stmt
;
1052 gimple_call_set_nothrow (stmt
, true);
1053 lhs
= make_ssa_name (type
);
1054 gimple_call_set_lhs (stmt
, lhs
);
1055 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1060 /* Perform a complex multiplication on two complex constants A, B represented
1061 by AR, AI, BR, BI of type TYPE.
1062 The operation we want is: a * b = (ar*br - ai*bi) + i(ar*bi + br*ai).
1063 Insert the GIMPLE statements into GSI. Store the real and imaginary
1064 components of the result into RR and RI. */
1067 expand_complex_multiplication_components (gimple_seq
*stmts
, location_t loc
,
1068 tree type
, tree ar
, tree ai
,
1072 tree t1
, t2
, t3
, t4
;
1074 t1
= gimple_build (stmts
, loc
, MULT_EXPR
, type
, ar
, br
);
1075 t2
= gimple_build (stmts
, loc
, MULT_EXPR
, type
, ai
, bi
);
1076 t3
= gimple_build (stmts
, loc
, MULT_EXPR
, type
, ar
, bi
);
1078 /* Avoid expanding redundant multiplication for the common
1079 case of squaring a complex number. */
1080 if (ar
== br
&& ai
== bi
)
1083 t4
= gimple_build (stmts
, loc
, MULT_EXPR
, type
, ai
, br
);
1085 *rr
= gimple_build (stmts
, loc
, MINUS_EXPR
, type
, t1
, t2
);
1086 *ri
= gimple_build (stmts
, loc
, PLUS_EXPR
, type
, t3
, t4
);
1089 /* Expand complex multiplication to scalars:
1090 a * b = (ar*br - ai*bi) + i(ar*bi + br*ai)
1094 expand_complex_multiplication (gimple_stmt_iterator
*gsi
, tree type
,
1095 tree ar
, tree ai
, tree br
, tree bi
,
1096 complex_lattice_t al
, complex_lattice_t bl
)
1099 tree inner_type
= TREE_TYPE (type
);
1100 location_t loc
= gimple_location (gsi_stmt (*gsi
));
1101 gimple_seq stmts
= NULL
;
1105 complex_lattice_t tl
;
1106 rr
= ar
, ar
= br
, br
= rr
;
1107 ri
= ai
, ai
= bi
, bi
= ri
;
1108 tl
= al
, al
= bl
, bl
= tl
;
1111 switch (PAIR (al
, bl
))
1113 case PAIR (ONLY_REAL
, ONLY_REAL
):
1114 rr
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ar
, br
);
1118 case PAIR (ONLY_IMAG
, ONLY_REAL
):
1120 if (TREE_CODE (ai
) == REAL_CST
1121 && real_identical (&TREE_REAL_CST (ai
), &dconst1
))
1124 ri
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ai
, br
);
1127 case PAIR (ONLY_IMAG
, ONLY_IMAG
):
1128 rr
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ai
, bi
);
1129 rr
= gimple_build (&stmts
, loc
, NEGATE_EXPR
, inner_type
, rr
);
1133 case PAIR (VARYING
, ONLY_REAL
):
1134 rr
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ar
, br
);
1135 ri
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ai
, br
);
1138 case PAIR (VARYING
, ONLY_IMAG
):
1139 rr
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ai
, bi
);
1140 rr
= gimple_build (&stmts
, loc
, NEGATE_EXPR
, inner_type
, rr
);
1141 ri
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ar
, bi
);
1144 case PAIR (VARYING
, VARYING
):
1145 if (flag_complex_method
== 2 && SCALAR_FLOAT_TYPE_P (inner_type
))
1147 /* If optimizing for size or not at all just do a libcall.
1148 Same if there are exception-handling edges or signaling NaNs. */
1149 if (optimize
== 0 || optimize_bb_for_size_p (gsi_bb (*gsi
))
1150 || stmt_can_throw_internal (cfun
, gsi_stmt (*gsi
))
1151 || flag_signaling_nans
)
1153 expand_complex_libcall (gsi
, type
, ar
, ai
, br
, bi
,
1158 if (!HONOR_NANS (inner_type
))
1160 /* If we are not worrying about NaNs expand to
1161 (ar*br - ai*bi) + i(ar*bi + br*ai) directly. */
1162 expand_complex_multiplication_components (&stmts
, loc
, inner_type
,
1168 /* Else, expand x = a * b into
1169 x = (ar*br - ai*bi) + i(ar*bi + br*ai);
1170 if (isunordered (__real__ x, __imag__ x))
1171 x = __muldc3 (a, b); */
1174 expand_complex_multiplication_components (&stmts
, loc
,
1176 br
, bi
, &tmpr
, &tmpi
);
1177 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1181 = gimple_build_cond (UNORDERED_EXPR
, tmpr
, tmpi
,
1182 NULL_TREE
, NULL_TREE
);
1184 basic_block orig_bb
= gsi_bb (*gsi
);
1185 /* We want to keep track of the original complex multiplication
1186 statement as we're going to modify it later in
1187 update_complex_assignment. Make sure that insert_cond_bb leaves
1188 that statement in the join block. */
1191 = insert_cond_bb (gsi_bb (*gsi
), gsi_stmt (*gsi
), check
,
1192 profile_probability::very_unlikely ());
1194 gimple_stmt_iterator cond_bb_gsi
= gsi_last_bb (cond_bb
);
1195 gsi_insert_after (&cond_bb_gsi
, gimple_build_nop (), GSI_NEW_STMT
);
1198 = expand_complex_libcall (&cond_bb_gsi
, type
, ar
, ai
, br
,
1199 bi
, MULT_EXPR
, false);
1200 gimple_seq stmts2
= NULL
;
1201 tree cond_real
= gimple_build (&stmts2
, loc
, REALPART_EXPR
,
1202 inner_type
, libcall_res
);
1203 tree cond_imag
= gimple_build (&stmts2
, loc
, IMAGPART_EXPR
,
1204 inner_type
, libcall_res
);
1205 gsi_insert_seq_before (&cond_bb_gsi
, stmts2
, GSI_SAME_STMT
);
1207 basic_block join_bb
= single_succ_edge (cond_bb
)->dest
;
1208 *gsi
= gsi_start_nondebug_after_labels_bb (join_bb
);
1210 /* We have a conditional block with some assignments in cond_bb.
1211 Wire up the PHIs to wrap up. */
1212 rr
= make_ssa_name (inner_type
);
1213 ri
= make_ssa_name (inner_type
);
1214 edge cond_to_join
= single_succ_edge (cond_bb
);
1215 edge orig_to_join
= find_edge (orig_bb
, join_bb
);
1217 gphi
*real_phi
= create_phi_node (rr
, gsi_bb (*gsi
));
1218 add_phi_arg (real_phi
, cond_real
, cond_to_join
, UNKNOWN_LOCATION
);
1219 add_phi_arg (real_phi
, tmpr
, orig_to_join
, UNKNOWN_LOCATION
);
1221 gphi
*imag_phi
= create_phi_node (ri
, gsi_bb (*gsi
));
1222 add_phi_arg (imag_phi
, cond_imag
, cond_to_join
, UNKNOWN_LOCATION
);
1223 add_phi_arg (imag_phi
, tmpi
, orig_to_join
, UNKNOWN_LOCATION
);
1226 /* If we are not worrying about NaNs expand to
1227 (ar*br - ai*bi) + i(ar*bi + br*ai) directly. */
1228 expand_complex_multiplication_components (&stmts
, loc
,
1237 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1238 update_complex_assignment (gsi
, rr
, ri
);
1241 /* Keep this algorithm in sync with fold-const.cc:const_binop().
1243 Expand complex division to scalars, straightforward algorithm.
1244 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1249 expand_complex_div_straight (gimple_stmt_iterator
*gsi
, tree inner_type
,
1250 tree ar
, tree ai
, tree br
, tree bi
,
1251 enum tree_code code
)
1253 gimple_seq stmts
= NULL
;
1254 location_t loc
= gimple_location (gsi_stmt (*gsi
));
1255 tree rr
, ri
, div
, t1
, t2
, t3
;
1257 t1
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, br
, br
);
1258 t2
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, bi
, bi
);
1259 div
= gimple_build (&stmts
, loc
, PLUS_EXPR
, inner_type
, t1
, t2
);
1261 t1
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ar
, br
);
1262 t2
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ai
, bi
);
1263 t3
= gimple_build (&stmts
, loc
, PLUS_EXPR
, inner_type
, t1
, t2
);
1264 rr
= gimple_build (&stmts
, loc
, code
, inner_type
, t3
, div
);
1266 t1
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ai
, br
);
1267 t2
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ar
, bi
);
1268 t3
= gimple_build (&stmts
, loc
, MINUS_EXPR
, inner_type
, t1
, t2
);
1269 ri
= gimple_build (&stmts
, loc
, code
, inner_type
, t3
, div
);
1271 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1272 update_complex_assignment (gsi
, rr
, ri
);
1275 /* Keep this algorithm in sync with fold-const.cc:const_binop().
1277 Expand complex division to scalars, modified algorithm to minimize
1278 overflow with wide input ranges. */
1281 expand_complex_div_wide (gimple_stmt_iterator
*gsi
, tree inner_type
,
1282 tree ar
, tree ai
, tree br
, tree bi
,
1283 enum tree_code code
)
1285 tree rr
, ri
, ratio
, div
, t1
, t2
, tr
, ti
, compare
;
1286 basic_block bb_cond
, bb_true
, bb_false
, bb_join
;
1288 gimple_seq stmts
= NULL
;
1289 location_t loc
= gimple_location (gsi_stmt (*gsi
));
1291 /* Examine |br| < |bi|, and branch. */
1292 t1
= gimple_build (&stmts
, loc
, ABS_EXPR
, inner_type
, br
);
1293 t2
= gimple_build (&stmts
, loc
, ABS_EXPR
, inner_type
, bi
);
1294 compare
= gimple_build (&stmts
, loc
,
1295 LT_EXPR
, boolean_type_node
, t1
, t2
);
1297 bb_cond
= bb_true
= bb_false
= bb_join
= NULL
;
1298 rr
= ri
= tr
= ti
= NULL
;
1299 if (TREE_CODE (compare
) != INTEGER_CST
)
1304 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1306 stmt
= gimple_build_cond (NE_EXPR
, compare
, boolean_false_node
,
1307 NULL_TREE
, NULL_TREE
);
1308 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1310 /* Split the original block, and create the TRUE and FALSE blocks. */
1311 e
= split_block (gsi_bb (*gsi
), stmt
);
1314 bb_true
= create_empty_bb (bb_cond
);
1315 bb_false
= create_empty_bb (bb_true
);
1316 bb_true
->count
= bb_false
->count
1317 = bb_cond
->count
.apply_probability (profile_probability::even ());
1319 /* Wire the blocks together. */
1320 e
->flags
= EDGE_TRUE_VALUE
;
1321 /* TODO: With value profile we could add an historgram to determine real
1323 e
->probability
= profile_probability::even ();
1324 redirect_edge_succ (e
, bb_true
);
1325 edge e2
= make_edge (bb_cond
, bb_false
, EDGE_FALSE_VALUE
);
1326 e2
->probability
= profile_probability::even ();
1327 make_single_succ_edge (bb_true
, bb_join
, EDGE_FALLTHRU
);
1328 make_single_succ_edge (bb_false
, bb_join
, EDGE_FALLTHRU
);
1329 add_bb_to_loop (bb_true
, bb_cond
->loop_father
);
1330 add_bb_to_loop (bb_false
, bb_cond
->loop_father
);
1332 /* Update dominance info. Note that bb_join's data was
1333 updated by split_block. */
1334 if (dom_info_available_p (CDI_DOMINATORS
))
1336 set_immediate_dominator (CDI_DOMINATORS
, bb_true
, bb_cond
);
1337 set_immediate_dominator (CDI_DOMINATORS
, bb_false
, bb_cond
);
1340 rr
= create_tmp_reg (inner_type
);
1341 ri
= create_tmp_reg (inner_type
);
1345 gimple_seq_discard (stmts
);
1349 /* In the TRUE branch, we compute
1351 div = (br * ratio) + bi;
1352 tr = (ar * ratio) + ai;
1353 ti = (ai * ratio) - ar;
1356 if (bb_true
|| integer_nonzerop (compare
))
1360 *gsi
= gsi_last_bb (bb_true
);
1361 gsi_insert_after (gsi
, gimple_build_nop (), GSI_NEW_STMT
);
1364 ratio
= gimple_build (&stmts
, loc
, code
, inner_type
, br
, bi
);
1366 t1
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, br
, ratio
);
1367 div
= gimple_build (&stmts
, loc
, PLUS_EXPR
, inner_type
, t1
, bi
);
1369 t1
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ar
, ratio
);
1370 tr
= gimple_build (&stmts
, loc
, PLUS_EXPR
, inner_type
, t1
, ai
);
1372 t1
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ai
, ratio
);
1373 ti
= gimple_build (&stmts
, loc
, MINUS_EXPR
, inner_type
, t1
, ar
);
1375 tr
= gimple_build (&stmts
, loc
, code
, inner_type
, tr
, div
);
1376 ti
= gimple_build (&stmts
, loc
, code
, inner_type
, ti
, div
);
1377 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1382 stmt
= gimple_build_assign (rr
, tr
);
1383 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1384 stmt
= gimple_build_assign (ri
, ti
);
1385 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1386 gsi_remove (gsi
, true);
1390 /* In the FALSE branch, we compute
1392 divisor = (d * ratio) + c;
1393 tr = (b * ratio) + a;
1394 ti = b - (a * ratio);
1397 if (bb_false
|| integer_zerop (compare
))
1401 *gsi
= gsi_last_bb (bb_false
);
1402 gsi_insert_after (gsi
, gimple_build_nop (), GSI_NEW_STMT
);
1405 ratio
= gimple_build (&stmts
, loc
, code
, inner_type
, bi
, br
);
1407 t1
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, bi
, ratio
);
1408 div
= gimple_build (&stmts
, loc
, PLUS_EXPR
, inner_type
, t1
, br
);
1410 t1
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ai
, ratio
);
1411 tr
= gimple_build (&stmts
, loc
, PLUS_EXPR
, inner_type
, t1
, ar
);
1413 t1
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ar
, ratio
);
1414 ti
= gimple_build (&stmts
, loc
, MINUS_EXPR
, inner_type
, ai
, t1
);
1416 tr
= gimple_build (&stmts
, loc
, code
, inner_type
, tr
, div
);
1417 ti
= gimple_build (&stmts
, loc
, code
, inner_type
, ti
, div
);
1418 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1423 stmt
= gimple_build_assign (rr
, tr
);
1424 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1425 stmt
= gimple_build_assign (ri
, ti
);
1426 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1427 gsi_remove (gsi
, true);
1432 *gsi
= gsi_start_bb (bb_join
);
1436 update_complex_assignment (gsi
, rr
, ri
);
1439 /* Expand complex division to scalars. */
1442 expand_complex_division (gimple_stmt_iterator
*gsi
, tree type
,
1443 tree ar
, tree ai
, tree br
, tree bi
,
1444 enum tree_code code
,
1445 complex_lattice_t al
, complex_lattice_t bl
)
1448 gimple_seq stmts
= NULL
;
1449 location_t loc
= gimple_location (gsi_stmt (*gsi
));
1451 tree inner_type
= TREE_TYPE (type
);
1452 switch (PAIR (al
, bl
))
1454 case PAIR (ONLY_REAL
, ONLY_REAL
):
1455 rr
= gimple_build (&stmts
, loc
, code
, inner_type
, ar
, br
);
1459 case PAIR (ONLY_REAL
, ONLY_IMAG
):
1461 ri
= gimple_build (&stmts
, loc
, code
, inner_type
, ar
, bi
);
1462 ri
= gimple_build (&stmts
, loc
, NEGATE_EXPR
, inner_type
, ri
);
1465 case PAIR (ONLY_IMAG
, ONLY_REAL
):
1467 ri
= gimple_build (&stmts
, loc
, code
, inner_type
, ai
, br
);
1470 case PAIR (ONLY_IMAG
, ONLY_IMAG
):
1471 rr
= gimple_build (&stmts
, loc
, code
, inner_type
, ai
, bi
);
1475 case PAIR (VARYING
, ONLY_REAL
):
1476 rr
= gimple_build (&stmts
, loc
, code
, inner_type
, ar
, br
);
1477 ri
= gimple_build (&stmts
, loc
, code
, inner_type
, ai
, br
);
1480 case PAIR (VARYING
, ONLY_IMAG
):
1481 rr
= gimple_build (&stmts
, loc
, code
, inner_type
, ai
, bi
);
1482 ri
= gimple_build (&stmts
, loc
, code
, inner_type
, ar
, bi
);
1483 ri
= gimple_build (&stmts
, loc
, NEGATE_EXPR
, inner_type
, ri
);
1486 case PAIR (ONLY_REAL
, VARYING
):
1487 case PAIR (ONLY_IMAG
, VARYING
):
1488 case PAIR (VARYING
, VARYING
):
1489 switch (flag_complex_method
)
1492 /* straightforward implementation of complex divide acceptable. */
1493 expand_complex_div_straight (gsi
, inner_type
, ar
, ai
, br
, bi
, code
);
1497 if (SCALAR_FLOAT_TYPE_P (inner_type
))
1499 expand_complex_libcall (gsi
, type
, ar
, ai
, br
, bi
, code
, true);
1505 /* wide ranges of inputs must work for complex divide. */
1506 expand_complex_div_wide (gsi
, inner_type
, ar
, ai
, br
, bi
, code
);
1518 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1519 update_complex_assignment (gsi
, rr
, ri
);
1522 /* Expand complex negation to scalars:
1527 expand_complex_negation (gimple_stmt_iterator
*gsi
, tree inner_type
,
1531 gimple_seq stmts
= NULL
;
1532 location_t loc
= gimple_location (gsi_stmt (*gsi
));
1534 rr
= gimple_build (&stmts
, loc
, NEGATE_EXPR
, inner_type
, ar
);
1535 ri
= gimple_build (&stmts
, loc
, NEGATE_EXPR
, inner_type
, ai
);
1537 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1538 update_complex_assignment (gsi
, rr
, ri
);
1541 /* Expand complex conjugate to scalars:
1546 expand_complex_conjugate (gimple_stmt_iterator
*gsi
, tree inner_type
,
1550 gimple_seq stmts
= NULL
;
1551 location_t loc
= gimple_location (gsi_stmt (*gsi
));
1553 ri
= gimple_build (&stmts
, loc
, NEGATE_EXPR
, inner_type
, ai
);
1555 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1556 update_complex_assignment (gsi
, ar
, ri
);
1559 /* Expand complex comparison (EQ or NE only). */
1562 expand_complex_comparison (gimple_stmt_iterator
*gsi
, tree ar
, tree ai
,
1563 tree br
, tree bi
, enum tree_code code
)
1565 tree cr
, ci
, cc
, type
;
1566 gimple
*stmt
= gsi_stmt (*gsi
);
1567 gimple_seq stmts
= NULL
;
1568 location_t loc
= gimple_location (stmt
);
1570 cr
= gimple_build (&stmts
, loc
, code
, boolean_type_node
, ar
, br
);
1571 ci
= gimple_build (&stmts
, loc
, code
, boolean_type_node
, ai
, bi
);
1572 cc
= gimple_build (&stmts
, loc
,
1573 (code
== EQ_EXPR
? BIT_AND_EXPR
: BIT_IOR_EXPR
),
1574 boolean_type_node
, cr
, ci
);
1575 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1577 switch (gimple_code (stmt
))
1581 greturn
*return_stmt
= as_a
<greturn
*> (stmt
);
1582 type
= TREE_TYPE (gimple_return_retval (return_stmt
));
1583 gimple_return_set_retval (return_stmt
, fold_convert (type
, cc
));
1588 type
= TREE_TYPE (gimple_assign_lhs (stmt
));
1589 gimple_assign_set_rhs_from_tree (gsi
, fold_convert (type
, cc
));
1590 stmt
= gsi_stmt (*gsi
);
1595 gcond
*cond_stmt
= as_a
<gcond
*> (stmt
);
1596 gimple_cond_set_code (cond_stmt
, EQ_EXPR
);
1597 gimple_cond_set_lhs (cond_stmt
, cc
);
1598 gimple_cond_set_rhs (cond_stmt
, boolean_true_node
);
1607 if (maybe_clean_eh_stmt (stmt
))
1608 bitmap_set_bit (need_eh_cleanup
, gimple_bb (stmt
)->index
);
1611 /* Expand inline asm that sets some complex SSA_NAMEs. */
1614 expand_complex_asm (gimple_stmt_iterator
*gsi
)
1616 gasm
*stmt
= as_a
<gasm
*> (gsi_stmt (*gsi
));
1618 bool diagnosed_p
= false;
1620 for (i
= 0; i
< gimple_asm_noutputs (stmt
); ++i
)
1622 tree link
= gimple_asm_output_op (stmt
, i
);
1623 tree op
= TREE_VALUE (link
);
1624 if (TREE_CODE (op
) == SSA_NAME
1625 && TREE_CODE (TREE_TYPE (op
)) == COMPLEX_TYPE
)
1627 if (gimple_asm_nlabels (stmt
) > 0)
1631 sorry_at (gimple_location (stmt
),
1632 "%<asm goto%> with complex typed outputs");
1635 /* Make sure to not ICE later, see PR105165. */
1636 tree zero
= build_zero_cst (TREE_TYPE (TREE_TYPE (op
)));
1637 set_component_ssa_name (op
, false, zero
);
1638 set_component_ssa_name (op
, true, zero
);
1641 tree type
= TREE_TYPE (op
);
1642 tree inner_type
= TREE_TYPE (type
);
1643 tree r
= build1 (REALPART_EXPR
, inner_type
, op
);
1644 tree i
= build1 (IMAGPART_EXPR
, inner_type
, op
);
1645 gimple_seq list
= set_component_ssa_name (op
, false, r
);
1648 gsi_insert_seq_after (gsi
, list
, GSI_CONTINUE_LINKING
);
1650 list
= set_component_ssa_name (op
, true, i
);
1652 gsi_insert_seq_after (gsi
, list
, GSI_CONTINUE_LINKING
);
1657 /* Process one statement. If we identify a complex operation, expand it. */
1660 expand_complex_operations_1 (gimple_stmt_iterator
*gsi
)
1662 gimple
*stmt
= gsi_stmt (*gsi
);
1663 tree type
, inner_type
, lhs
;
1664 tree ac
, ar
, ai
, bc
, br
, bi
;
1665 complex_lattice_t al
, bl
;
1666 enum tree_code code
;
1668 if (gimple_code (stmt
) == GIMPLE_ASM
)
1670 expand_complex_asm (gsi
);
1674 lhs
= gimple_get_lhs (stmt
);
1675 if (!lhs
&& gimple_code (stmt
) != GIMPLE_COND
)
1678 type
= TREE_TYPE (gimple_op (stmt
, 0));
1679 code
= gimple_expr_code (stmt
);
1681 /* Initial filter for operations we handle. */
1687 case TRUNC_DIV_EXPR
:
1689 case FLOOR_DIV_EXPR
:
1690 case ROUND_DIV_EXPR
:
1694 if (TREE_CODE (type
) != COMPLEX_TYPE
)
1696 inner_type
= TREE_TYPE (type
);
1701 /* Note, both GIMPLE_ASSIGN and GIMPLE_COND may have an EQ_EXPR
1702 subcode, so we need to access the operands using gimple_op. */
1703 inner_type
= TREE_TYPE (gimple_op (stmt
, 1));
1704 if (TREE_CODE (inner_type
) != COMPLEX_TYPE
)
1712 /* GIMPLE_COND may also fallthru here, but we do not need to
1713 do anything with it. */
1714 if (gimple_code (stmt
) == GIMPLE_COND
)
1717 if (TREE_CODE (type
) == COMPLEX_TYPE
)
1718 expand_complex_move (gsi
, type
);
1719 else if (is_gimple_assign (stmt
)
1720 && (gimple_assign_rhs_code (stmt
) == REALPART_EXPR
1721 || gimple_assign_rhs_code (stmt
) == IMAGPART_EXPR
)
1722 && TREE_CODE (lhs
) == SSA_NAME
)
1724 rhs
= gimple_assign_rhs1 (stmt
);
1725 rhs
= extract_component (gsi
, TREE_OPERAND (rhs
, 0),
1726 gimple_assign_rhs_code (stmt
)
1729 gimple_assign_set_rhs_from_tree (gsi
, rhs
);
1730 stmt
= gsi_stmt (*gsi
);
1737 /* Extract the components of the two complex values. Make sure and
1738 handle the common case of the same value used twice specially. */
1739 if (is_gimple_assign (stmt
))
1741 ac
= gimple_assign_rhs1 (stmt
);
1742 bc
= (gimple_num_ops (stmt
) > 2) ? gimple_assign_rhs2 (stmt
) : NULL
;
1744 /* GIMPLE_CALL cannot get here. */
1747 ac
= gimple_cond_lhs (stmt
);
1748 bc
= gimple_cond_rhs (stmt
);
1751 ar
= extract_component (gsi
, ac
, false, true);
1752 ai
= extract_component (gsi
, ac
, true, true);
1758 br
= extract_component (gsi
, bc
, 0, true);
1759 bi
= extract_component (gsi
, bc
, 1, true);
1762 br
= bi
= NULL_TREE
;
1764 al
= find_lattice_value (ac
);
1765 if (al
== UNINITIALIZED
)
1768 if (TREE_CODE_CLASS (code
) == tcc_unary
)
1774 bl
= find_lattice_value (bc
);
1775 if (bl
== UNINITIALIZED
)
1783 expand_complex_addition (gsi
, inner_type
, ar
, ai
, br
, bi
, code
, al
, bl
);
1787 expand_complex_multiplication (gsi
, type
, ar
, ai
, br
, bi
, al
, bl
);
1790 case TRUNC_DIV_EXPR
:
1792 case FLOOR_DIV_EXPR
:
1793 case ROUND_DIV_EXPR
:
1795 expand_complex_division (gsi
, type
, ar
, ai
, br
, bi
, code
, al
, bl
);
1799 expand_complex_negation (gsi
, inner_type
, ar
, ai
);
1803 expand_complex_conjugate (gsi
, inner_type
, ar
, ai
);
1808 expand_complex_comparison (gsi
, ar
, ai
, br
, bi
, code
);
1817 /* Entry point for complex operation lowering during optimization. */
1820 tree_lower_complex (void)
1822 gimple_stmt_iterator gsi
;
1827 if (!init_dont_simulate_again ())
1830 complex_lattice_values
.create (num_ssa_names
);
1831 complex_lattice_values
.safe_grow_cleared (num_ssa_names
, true);
1833 init_parameter_lattice_values ();
1834 class complex_propagate complex_propagate
;
1835 complex_propagate
.ssa_propagate ();
1837 need_eh_cleanup
= BITMAP_ALLOC (NULL
);
1839 complex_variable_components
= new int_tree_htab_type (10);
1841 complex_ssa_name_components
.create (2 * num_ssa_names
);
1842 complex_ssa_name_components
.safe_grow_cleared (2 * num_ssa_names
, true);
1844 update_parameter_components ();
1846 rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
1847 n_bbs
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
1848 for (i
= 0; i
< n_bbs
; i
++)
1850 bb
= BASIC_BLOCK_FOR_FN (cfun
, rpo
[i
]);
1853 update_phi_components (bb
);
1854 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1855 expand_complex_operations_1 (&gsi
);
1860 if (!phis_to_revisit
.is_empty ())
1862 unsigned int n
= phis_to_revisit
.length ();
1863 for (unsigned int j
= 0; j
< n
; j
+= 3)
1864 for (unsigned int k
= 0; k
< 2; k
++)
1865 if (gphi
*phi
= phis_to_revisit
[j
+ k
+ 1])
1867 unsigned int m
= gimple_phi_num_args (phi
);
1868 for (unsigned int l
= 0; l
< m
; ++l
)
1870 tree op
= gimple_phi_arg_def (phi
, l
);
1871 if (TREE_CODE (op
) == SSA_NAME
1872 || is_gimple_min_invariant (op
))
1874 tree arg
= gimple_phi_arg_def (phis_to_revisit
[j
], l
);
1875 op
= extract_component (NULL
, arg
, k
> 0, false, false);
1876 SET_PHI_ARG_DEF (phi
, l
, op
);
1879 phis_to_revisit
.release ();
1882 gsi_commit_edge_inserts ();
1885 = gimple_purge_all_dead_eh_edges (need_eh_cleanup
) ? TODO_cleanup_cfg
: 0;
1886 BITMAP_FREE (need_eh_cleanup
);
1888 delete complex_variable_components
;
1889 complex_variable_components
= NULL
;
1890 complex_ssa_name_components
.release ();
1891 complex_lattice_values
.release ();
1897 const pass_data pass_data_lower_complex
=
1899 GIMPLE_PASS
, /* type */
1900 "cplxlower", /* name */
1901 OPTGROUP_NONE
, /* optinfo_flags */
1902 TV_NONE
, /* tv_id */
1903 PROP_ssa
, /* properties_required */
1904 PROP_gimple_lcx
, /* properties_provided */
1905 0, /* properties_destroyed */
1906 0, /* todo_flags_start */
1907 TODO_update_ssa
, /* todo_flags_finish */
1910 class pass_lower_complex
: public gimple_opt_pass
1913 pass_lower_complex (gcc::context
*ctxt
)
1914 : gimple_opt_pass (pass_data_lower_complex
, ctxt
)
1917 /* opt_pass methods: */
1918 opt_pass
* clone () { return new pass_lower_complex (m_ctxt
); }
1919 virtual unsigned int execute (function
*) { return tree_lower_complex (); }
1921 }; // class pass_lower_complex
1926 make_pass_lower_complex (gcc::context
*ctxt
)
1928 return new pass_lower_complex (ctxt
);
1934 const pass_data pass_data_lower_complex_O0
=
1936 GIMPLE_PASS
, /* type */
1937 "cplxlower0", /* name */
1938 OPTGROUP_NONE
, /* optinfo_flags */
1939 TV_NONE
, /* tv_id */
1940 PROP_cfg
, /* properties_required */
1941 PROP_gimple_lcx
, /* properties_provided */
1942 0, /* properties_destroyed */
1943 0, /* todo_flags_start */
1944 TODO_update_ssa
, /* todo_flags_finish */
1947 class pass_lower_complex_O0
: public gimple_opt_pass
1950 pass_lower_complex_O0 (gcc::context
*ctxt
)
1951 : gimple_opt_pass (pass_data_lower_complex_O0
, ctxt
)
1954 /* opt_pass methods: */
1955 virtual bool gate (function
*fun
)
1957 /* With errors, normal optimization passes are not run. If we don't
1958 lower complex operations at all, rtl expansion will abort. */
1959 return !(fun
->curr_properties
& PROP_gimple_lcx
);
1962 virtual unsigned int execute (function
*) { return tree_lower_complex (); }
1964 }; // class pass_lower_complex_O0
1969 make_pass_lower_complex_O0 (gcc::context
*ctxt
)
1971 return new pass_lower_complex_O0 (ctxt
);