1 /* Lower complex number operations to scalar operations.
2 Copyright (C) 2004-2021 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"
46 /* For each complex ssa name, a lattice value. We're interested in finding
47 out whether a complex number is degenerate in some way, having only real
48 or only complex parts. */
58 /* The type complex_lattice_t holds combinations of the above
60 typedef int complex_lattice_t
;
62 #define PAIR(a, b) ((a) << 2 | (b))
64 class complex_propagate
: public ssa_propagation_engine
66 enum ssa_prop_result
visit_stmt (gimple
*, edge
*, tree
*) FINAL OVERRIDE
;
67 enum ssa_prop_result
visit_phi (gphi
*) FINAL OVERRIDE
;
70 static vec
<complex_lattice_t
> complex_lattice_values
;
72 /* For each complex variable, a pair of variables for the components exists in
74 static int_tree_htab_type
*complex_variable_components
;
76 /* For each complex SSA_NAME, a pair of ssa names for the components. */
77 static vec
<tree
> complex_ssa_name_components
;
79 /* Vector of PHI triplets (original complex PHI and corresponding real and
80 imag PHIs if real and/or imag PHIs contain temporarily
81 non-SSA_NAME/non-invariant args that need to be replaced by SSA_NAMEs. */
82 static vec
<gphi
*> phis_to_revisit
;
84 /* BBs that need EH cleanup. */
85 static bitmap need_eh_cleanup
;
87 /* Lookup UID in the complex_variable_components hashtable and return the
90 cvc_lookup (unsigned int uid
)
92 struct int_tree_map in
;
94 return complex_variable_components
->find_with_hash (in
, uid
).to
;
97 /* Insert the pair UID, TO into the complex_variable_components hashtable. */
100 cvc_insert (unsigned int uid
, tree to
)
106 loc
= complex_variable_components
->find_slot_with_hash (h
, uid
, INSERT
);
111 /* Return true if T is not a zero constant. In the case of real values,
112 we're only interested in +0.0. */
115 some_nonzerop (tree t
)
119 /* Operations with real or imaginary part of a complex number zero
120 cannot be treated the same as operations with a real or imaginary
121 operand if we care about the signs of zeros in the result. */
122 if (TREE_CODE (t
) == REAL_CST
&& !flag_signed_zeros
)
123 zerop
= real_identical (&TREE_REAL_CST (t
), &dconst0
);
124 else if (TREE_CODE (t
) == FIXED_CST
)
125 zerop
= fixed_zerop (t
);
126 else if (TREE_CODE (t
) == INTEGER_CST
)
127 zerop
= integer_zerop (t
);
133 /* Compute a lattice value from the components of a complex type REAL
136 static complex_lattice_t
137 find_lattice_value_parts (tree real
, tree imag
)
140 complex_lattice_t ret
;
142 r
= some_nonzerop (real
);
143 i
= some_nonzerop (imag
);
144 ret
= r
* ONLY_REAL
+ i
* ONLY_IMAG
;
146 /* ??? On occasion we could do better than mapping 0+0i to real, but we
147 certainly don't want to leave it UNINITIALIZED, which eventually gets
148 mapped to VARYING. */
149 if (ret
== UNINITIALIZED
)
156 /* Compute a lattice value from gimple_val T. */
158 static complex_lattice_t
159 find_lattice_value (tree t
)
163 switch (TREE_CODE (t
))
166 return complex_lattice_values
[SSA_NAME_VERSION (t
)];
169 real
= TREE_REALPART (t
);
170 imag
= TREE_IMAGPART (t
);
177 return find_lattice_value_parts (real
, imag
);
180 /* Determine if LHS is something for which we're interested in seeing
181 simulation results. */
184 is_complex_reg (tree lhs
)
186 return TREE_CODE (TREE_TYPE (lhs
)) == COMPLEX_TYPE
&& is_gimple_reg (lhs
);
189 /* Mark the incoming parameters to the function as VARYING. */
192 init_parameter_lattice_values (void)
196 for (parm
= DECL_ARGUMENTS (cfun
->decl
); parm
; parm
= DECL_CHAIN (parm
))
197 if (is_complex_reg (parm
)
198 && (ssa_name
= ssa_default_def (cfun
, parm
)) != NULL_TREE
)
199 complex_lattice_values
[SSA_NAME_VERSION (ssa_name
)] = VARYING
;
202 /* Initialize simulation state for each statement. Return false if we
203 found no statements we want to simulate, and thus there's nothing
204 for the entire pass to do. */
207 init_dont_simulate_again (void)
210 bool saw_a_complex_op
= false;
212 FOR_EACH_BB_FN (bb
, cfun
)
214 for (gphi_iterator gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
);
217 gphi
*phi
= gsi
.phi ();
218 prop_set_simulate_again (phi
,
219 is_complex_reg (gimple_phi_result (phi
)));
222 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);
229 stmt
= gsi_stmt (gsi
);
230 op0
= op1
= NULL_TREE
;
232 /* Most control-altering statements must be initially
233 simulated, else we won't cover the entire cfg. */
234 sim_again_p
= stmt_ends_bb_p (stmt
);
236 switch (gimple_code (stmt
))
239 if (gimple_call_lhs (stmt
))
240 sim_again_p
= is_complex_reg (gimple_call_lhs (stmt
));
244 sim_again_p
= is_complex_reg (gimple_assign_lhs (stmt
));
245 if (gimple_assign_rhs_code (stmt
) == REALPART_EXPR
246 || gimple_assign_rhs_code (stmt
) == IMAGPART_EXPR
)
247 op0
= TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
249 op0
= gimple_assign_rhs1 (stmt
);
250 if (gimple_num_ops (stmt
) > 2)
251 op1
= gimple_assign_rhs2 (stmt
);
255 op0
= gimple_cond_lhs (stmt
);
256 op1
= gimple_cond_rhs (stmt
);
264 switch (gimple_expr_code (stmt
))
276 if (TREE_CODE (TREE_TYPE (op0
)) == COMPLEX_TYPE
277 || TREE_CODE (TREE_TYPE (op1
)) == COMPLEX_TYPE
)
278 saw_a_complex_op
= true;
283 if (TREE_CODE (TREE_TYPE (op0
)) == COMPLEX_TYPE
)
284 saw_a_complex_op
= true;
289 /* The total store transformation performed during
290 gimplification creates such uninitialized loads
291 and we need to lower the statement to be able
293 if (TREE_CODE (op0
) == SSA_NAME
294 && ssa_undefined_value_p (op0
))
295 saw_a_complex_op
= true;
302 prop_set_simulate_again (stmt
, sim_again_p
);
306 return saw_a_complex_op
;
310 /* Evaluate statement STMT against the complex lattice defined above. */
313 complex_propagate::visit_stmt (gimple
*stmt
, edge
*taken_edge_p ATTRIBUTE_UNUSED
,
316 complex_lattice_t new_l
, old_l
, op1_l
, op2_l
;
320 lhs
= gimple_get_lhs (stmt
);
321 /* Skip anything but GIMPLE_ASSIGN and GIMPLE_CALL with a lhs. */
322 if (!lhs
|| SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs
))
323 return SSA_PROP_VARYING
;
325 /* These conditions should be satisfied due to the initial filter
326 set up in init_dont_simulate_again. */
327 gcc_assert (TREE_CODE (lhs
) == SSA_NAME
);
328 gcc_assert (TREE_CODE (TREE_TYPE (lhs
)) == COMPLEX_TYPE
);
331 ver
= SSA_NAME_VERSION (lhs
);
332 old_l
= complex_lattice_values
[ver
];
334 switch (gimple_expr_code (stmt
))
338 new_l
= find_lattice_value (gimple_assign_rhs1 (stmt
));
342 new_l
= find_lattice_value_parts (gimple_assign_rhs1 (stmt
),
343 gimple_assign_rhs2 (stmt
));
348 op1_l
= find_lattice_value (gimple_assign_rhs1 (stmt
));
349 op2_l
= find_lattice_value (gimple_assign_rhs2 (stmt
));
351 /* We've set up the lattice values such that IOR neatly
353 new_l
= op1_l
| op2_l
;
362 op1_l
= find_lattice_value (gimple_assign_rhs1 (stmt
));
363 op2_l
= find_lattice_value (gimple_assign_rhs2 (stmt
));
365 /* Obviously, if either varies, so does the result. */
366 if (op1_l
== VARYING
|| op2_l
== VARYING
)
368 /* Don't prematurely promote variables if we've not yet seen
370 else if (op1_l
== UNINITIALIZED
)
372 else if (op2_l
== UNINITIALIZED
)
376 /* At this point both numbers have only one component. If the
377 numbers are of opposite kind, the result is imaginary,
378 otherwise the result is real. The add/subtract translates
379 the real/imag from/to 0/1; the ^ performs the comparison. */
380 new_l
= ((op1_l
- ONLY_REAL
) ^ (op2_l
- ONLY_REAL
)) + ONLY_REAL
;
382 /* Don't allow the lattice value to flip-flop indefinitely. */
389 new_l
= find_lattice_value (gimple_assign_rhs1 (stmt
));
397 /* If nothing changed this round, let the propagator know. */
399 return SSA_PROP_NOT_INTERESTING
;
401 complex_lattice_values
[ver
] = new_l
;
402 return new_l
== VARYING
? SSA_PROP_VARYING
: SSA_PROP_INTERESTING
;
405 /* Evaluate a PHI node against the complex lattice defined above. */
408 complex_propagate::visit_phi (gphi
*phi
)
410 complex_lattice_t new_l
, old_l
;
415 lhs
= gimple_phi_result (phi
);
417 /* This condition should be satisfied due to the initial filter
418 set up in init_dont_simulate_again. */
419 gcc_assert (TREE_CODE (TREE_TYPE (lhs
)) == COMPLEX_TYPE
);
421 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs
))
422 return SSA_PROP_VARYING
;
424 /* We've set up the lattice values such that IOR neatly models PHI meet. */
425 new_l
= UNINITIALIZED
;
426 for (i
= gimple_phi_num_args (phi
) - 1; i
>= 0; --i
)
427 new_l
|= find_lattice_value (gimple_phi_arg_def (phi
, i
));
429 ver
= SSA_NAME_VERSION (lhs
);
430 old_l
= complex_lattice_values
[ver
];
433 return SSA_PROP_NOT_INTERESTING
;
435 complex_lattice_values
[ver
] = new_l
;
436 return new_l
== VARYING
? SSA_PROP_VARYING
: SSA_PROP_INTERESTING
;
439 /* Create one backing variable for a complex component of ORIG. */
442 create_one_component_var (tree type
, tree orig
, const char *prefix
,
443 const char *suffix
, enum tree_code code
)
445 tree r
= create_tmp_var (type
, prefix
);
447 DECL_SOURCE_LOCATION (r
) = DECL_SOURCE_LOCATION (orig
);
448 DECL_ARTIFICIAL (r
) = 1;
450 if (DECL_NAME (orig
) && !DECL_IGNORED_P (orig
))
452 const char *name
= IDENTIFIER_POINTER (DECL_NAME (orig
));
453 name
= ACONCAT ((name
, suffix
, NULL
));
454 DECL_NAME (r
) = get_identifier (name
);
456 SET_DECL_DEBUG_EXPR (r
, build1 (code
, type
, orig
));
457 DECL_HAS_DEBUG_EXPR_P (r
) = 1;
458 DECL_IGNORED_P (r
) = 0;
459 copy_warning (r
, orig
);
463 DECL_IGNORED_P (r
) = 1;
464 suppress_warning (r
);
470 /* Retrieve a value for a complex component of VAR. */
473 get_component_var (tree var
, bool imag_p
)
475 size_t decl_index
= DECL_UID (var
) * 2 + imag_p
;
476 tree ret
= cvc_lookup (decl_index
);
480 ret
= create_one_component_var (TREE_TYPE (TREE_TYPE (var
)), var
,
481 imag_p
? "CI" : "CR",
482 imag_p
? "$imag" : "$real",
483 imag_p
? IMAGPART_EXPR
: REALPART_EXPR
);
484 cvc_insert (decl_index
, ret
);
490 /* Retrieve a value for a complex component of SSA_NAME. */
493 get_component_ssa_name (tree ssa_name
, bool imag_p
)
495 complex_lattice_t lattice
= find_lattice_value (ssa_name
);
496 size_t ssa_name_index
;
499 if (lattice
== (imag_p
? ONLY_REAL
: ONLY_IMAG
))
501 tree inner_type
= TREE_TYPE (TREE_TYPE (ssa_name
));
502 if (SCALAR_FLOAT_TYPE_P (inner_type
))
503 return build_real (inner_type
, dconst0
);
505 return build_int_cst (inner_type
, 0);
508 ssa_name_index
= SSA_NAME_VERSION (ssa_name
) * 2 + imag_p
;
509 ret
= complex_ssa_name_components
[ssa_name_index
];
512 if (SSA_NAME_VAR (ssa_name
))
513 ret
= get_component_var (SSA_NAME_VAR (ssa_name
), imag_p
);
515 ret
= TREE_TYPE (TREE_TYPE (ssa_name
));
516 ret
= make_ssa_name (ret
);
518 /* Copy some properties from the original. In particular, whether it
519 is used in an abnormal phi, and whether it's uninitialized. */
520 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ret
)
521 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name
);
522 if (SSA_NAME_IS_DEFAULT_DEF (ssa_name
)
523 && TREE_CODE (SSA_NAME_VAR (ssa_name
)) == VAR_DECL
)
525 SSA_NAME_DEF_STMT (ret
) = SSA_NAME_DEF_STMT (ssa_name
);
526 set_ssa_default_def (cfun
, SSA_NAME_VAR (ret
), ret
);
529 complex_ssa_name_components
[ssa_name_index
] = ret
;
535 /* Set a value for a complex component of SSA_NAME, return a
536 gimple_seq of stuff that needs doing. */
539 set_component_ssa_name (tree ssa_name
, bool imag_p
, tree value
)
541 complex_lattice_t lattice
= find_lattice_value (ssa_name
);
542 size_t ssa_name_index
;
547 /* We know the value must be zero, else there's a bug in our lattice
548 analysis. But the value may well be a variable known to contain
549 zero. We should be safe ignoring it. */
550 if (lattice
== (imag_p
? ONLY_REAL
: ONLY_IMAG
))
553 /* If we've already assigned an SSA_NAME to this component, then this
554 means that our walk of the basic blocks found a use before the set.
555 This is fine. Now we should create an initialization for the value
556 we created earlier. */
557 ssa_name_index
= SSA_NAME_VERSION (ssa_name
) * 2 + imag_p
;
558 comp
= complex_ssa_name_components
[ssa_name_index
];
562 /* If we've nothing assigned, and the value we're given is already stable,
563 then install that as the value for this SSA_NAME. This preemptively
564 copy-propagates the value, which avoids unnecessary memory allocation. */
565 else if (is_gimple_min_invariant (value
)
566 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name
))
568 complex_ssa_name_components
[ssa_name_index
] = value
;
571 else if (TREE_CODE (value
) == SSA_NAME
572 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name
))
574 /* Replace an anonymous base value with the variable from cvc_lookup.
575 This should result in better debug info. */
576 if (!SSA_NAME_IS_DEFAULT_DEF (value
)
577 && SSA_NAME_VAR (ssa_name
)
578 && (!SSA_NAME_VAR (value
) || DECL_IGNORED_P (SSA_NAME_VAR (value
)))
579 && !DECL_IGNORED_P (SSA_NAME_VAR (ssa_name
)))
581 comp
= get_component_var (SSA_NAME_VAR (ssa_name
), imag_p
);
582 replace_ssa_name_symbol (value
, comp
);
585 complex_ssa_name_components
[ssa_name_index
] = value
;
589 /* Finally, we need to stabilize the result by installing the value into
592 comp
= get_component_ssa_name (ssa_name
, imag_p
);
594 /* Do all the work to assign VALUE to COMP. */
596 value
= force_gimple_operand (value
, &list
, false, NULL
);
597 last
= gimple_build_assign (comp
, value
);
598 gimple_seq_add_stmt (&list
, last
);
599 gcc_assert (SSA_NAME_DEF_STMT (comp
) == last
);
604 /* Extract the real or imaginary part of a complex variable or constant.
605 Make sure that it's a proper gimple_val and gimplify it if not.
606 Emit any new code before gsi. */
609 extract_component (gimple_stmt_iterator
*gsi
, tree t
, bool imagpart_p
,
610 bool gimple_p
, bool phiarg_p
= false)
612 switch (TREE_CODE (t
))
615 return imagpart_p
? TREE_IMAGPART (t
) : TREE_REALPART (t
);
622 tree inner_type
= TREE_TYPE (TREE_TYPE (t
));
623 t
= unshare_expr (t
);
624 TREE_TYPE (t
) = inner_type
;
625 TREE_OPERAND (t
, 1) = TYPE_SIZE (inner_type
);
627 TREE_OPERAND (t
, 2) = size_binop (PLUS_EXPR
, TREE_OPERAND (t
, 2),
628 TYPE_SIZE (inner_type
));
630 t
= force_gimple_operand_gsi (gsi
, t
, true, NULL
, true,
640 case VIEW_CONVERT_EXPR
:
643 tree inner_type
= TREE_TYPE (TREE_TYPE (t
));
645 t
= build1 ((imagpart_p
? IMAGPART_EXPR
: REALPART_EXPR
),
646 inner_type
, unshare_expr (t
));
649 t
= force_gimple_operand_gsi (gsi
, t
, true, NULL
, true,
656 t
= get_component_ssa_name (t
, imagpart_p
);
657 if (TREE_CODE (t
) == SSA_NAME
&& SSA_NAME_DEF_STMT (t
) == NULL
)
658 gcc_assert (phiarg_p
);
666 /* Update the complex components of the ssa name on the lhs of STMT. */
669 update_complex_components (gimple_stmt_iterator
*gsi
, gimple
*stmt
, tree r
,
675 lhs
= gimple_get_lhs (stmt
);
677 list
= set_component_ssa_name (lhs
, false, r
);
679 gsi_insert_seq_after (gsi
, list
, GSI_CONTINUE_LINKING
);
681 list
= set_component_ssa_name (lhs
, true, i
);
683 gsi_insert_seq_after (gsi
, list
, GSI_CONTINUE_LINKING
);
687 update_complex_components_on_edge (edge e
, tree lhs
, tree r
, tree i
)
691 list
= set_component_ssa_name (lhs
, false, r
);
693 gsi_insert_seq_on_edge (e
, list
);
695 list
= set_component_ssa_name (lhs
, true, i
);
697 gsi_insert_seq_on_edge (e
, list
);
701 /* Update an assignment to a complex variable in place. */
704 update_complex_assignment (gimple_stmt_iterator
*gsi
, tree r
, tree i
)
706 gimple
*old_stmt
= gsi_stmt (*gsi
);
707 gimple_assign_set_rhs_with_ops (gsi
, COMPLEX_EXPR
, r
, i
);
708 gimple
*stmt
= gsi_stmt (*gsi
);
710 if (maybe_clean_or_replace_eh_stmt (old_stmt
, stmt
))
711 bitmap_set_bit (need_eh_cleanup
, gimple_bb (stmt
)->index
);
713 update_complex_components (gsi
, gsi_stmt (*gsi
), r
, i
);
717 /* Generate code at the entry point of the function to initialize the
718 component variables for a complex parameter. */
721 update_parameter_components (void)
723 edge entry_edge
= single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
726 for (parm
= DECL_ARGUMENTS (cfun
->decl
); parm
; parm
= DECL_CHAIN (parm
))
728 tree type
= TREE_TYPE (parm
);
731 if (TREE_CODE (type
) != COMPLEX_TYPE
|| !is_gimple_reg (parm
))
734 type
= TREE_TYPE (type
);
735 ssa_name
= ssa_default_def (cfun
, parm
);
739 r
= build1 (REALPART_EXPR
, type
, ssa_name
);
740 i
= build1 (IMAGPART_EXPR
, type
, ssa_name
);
741 update_complex_components_on_edge (entry_edge
, ssa_name
, r
, i
);
745 /* Generate code to set the component variables of a complex variable
746 to match the PHI statements in block BB. */
749 update_phi_components (basic_block bb
)
753 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
755 gphi
*phi
= gsi
.phi ();
757 if (is_complex_reg (gimple_phi_result (phi
)))
759 gphi
*p
[2] = { NULL
, NULL
};
760 unsigned int i
, j
, n
;
761 bool revisit_phi
= false;
763 for (j
= 0; j
< 2; j
++)
765 tree l
= get_component_ssa_name (gimple_phi_result (phi
), j
> 0);
766 if (TREE_CODE (l
) == SSA_NAME
)
767 p
[j
] = create_phi_node (l
, bb
);
770 for (i
= 0, n
= gimple_phi_num_args (phi
); i
< n
; ++i
)
772 tree comp
, arg
= gimple_phi_arg_def (phi
, i
);
773 for (j
= 0; j
< 2; j
++)
776 comp
= extract_component (NULL
, arg
, j
> 0, false, true);
777 if (TREE_CODE (comp
) == SSA_NAME
778 && SSA_NAME_DEF_STMT (comp
) == NULL
)
780 /* For the benefit of any gimple simplification during
781 this pass that might walk SSA_NAME def stmts,
782 don't add SSA_NAMEs without definitions into the
783 PHI arguments, but put a decl in there instead
784 temporarily, and revisit this PHI later on. */
785 if (SSA_NAME_VAR (comp
))
786 comp
= SSA_NAME_VAR (comp
);
788 comp
= create_tmp_reg (TREE_TYPE (comp
),
792 SET_PHI_ARG_DEF (p
[j
], i
, comp
);
798 phis_to_revisit
.safe_push (phi
);
799 phis_to_revisit
.safe_push (p
[0]);
800 phis_to_revisit
.safe_push (p
[1]);
806 /* Expand a complex move to scalars. */
809 expand_complex_move (gimple_stmt_iterator
*gsi
, tree type
)
811 tree inner_type
= TREE_TYPE (type
);
813 gimple
*stmt
= gsi_stmt (*gsi
);
815 if (is_gimple_assign (stmt
))
817 lhs
= gimple_assign_lhs (stmt
);
818 if (gimple_num_ops (stmt
) == 2)
819 rhs
= gimple_assign_rhs1 (stmt
);
823 else if (is_gimple_call (stmt
))
825 lhs
= gimple_call_lhs (stmt
);
831 if (TREE_CODE (lhs
) == SSA_NAME
)
833 if (is_ctrl_altering_stmt (stmt
))
837 /* The value is not assigned on the exception edges, so we need not
838 concern ourselves there. We do need to update on the fallthru
840 e
= find_fallthru_edge (gsi_bb (*gsi
)->succs
);
844 r
= build1 (REALPART_EXPR
, inner_type
, lhs
);
845 i
= build1 (IMAGPART_EXPR
, inner_type
, lhs
);
846 update_complex_components_on_edge (e
, lhs
, r
, i
);
848 else if (is_gimple_call (stmt
)
849 || gimple_has_side_effects (stmt
)
850 || gimple_assign_rhs_code (stmt
) == PAREN_EXPR
)
852 r
= build1 (REALPART_EXPR
, inner_type
, lhs
);
853 i
= build1 (IMAGPART_EXPR
, inner_type
, lhs
);
854 update_complex_components (gsi
, stmt
, r
, i
);
858 if (gimple_assign_rhs_code (stmt
) != COMPLEX_EXPR
)
860 r
= extract_component (gsi
, rhs
, 0, true);
861 i
= extract_component (gsi
, rhs
, 1, true);
865 r
= gimple_assign_rhs1 (stmt
);
866 i
= gimple_assign_rhs2 (stmt
);
868 update_complex_assignment (gsi
, r
, i
);
871 else if (rhs
&& TREE_CODE (rhs
) == SSA_NAME
&& !TREE_SIDE_EFFECTS (lhs
))
877 loc
= gimple_location (stmt
);
878 r
= extract_component (gsi
, rhs
, 0, false);
879 i
= extract_component (gsi
, rhs
, 1, false);
881 x
= build1 (REALPART_EXPR
, inner_type
, unshare_expr (lhs
));
882 t
= gimple_build_assign (x
, r
);
883 gimple_set_location (t
, loc
);
884 gsi_insert_before (gsi
, t
, GSI_SAME_STMT
);
886 if (stmt
== gsi_stmt (*gsi
))
888 x
= build1 (IMAGPART_EXPR
, inner_type
, unshare_expr (lhs
));
889 gimple_assign_set_lhs (stmt
, x
);
890 gimple_assign_set_rhs1 (stmt
, i
);
894 x
= build1 (IMAGPART_EXPR
, inner_type
, unshare_expr (lhs
));
895 t
= gimple_build_assign (x
, i
);
896 gimple_set_location (t
, loc
);
897 gsi_insert_before (gsi
, t
, GSI_SAME_STMT
);
899 stmt
= gsi_stmt (*gsi
);
900 gcc_assert (gimple_code (stmt
) == GIMPLE_RETURN
);
901 gimple_return_set_retval (as_a
<greturn
*> (stmt
), lhs
);
908 /* Expand complex addition to scalars:
909 a + b = (ar + br) + i(ai + bi)
910 a - b = (ar - br) + i(ai + bi)
914 expand_complex_addition (gimple_stmt_iterator
*gsi
, tree inner_type
,
915 tree ar
, tree ai
, tree br
, tree bi
,
917 complex_lattice_t al
, complex_lattice_t bl
)
920 gimple_seq stmts
= NULL
;
921 location_t loc
= gimple_location (gsi_stmt (*gsi
));
923 switch (PAIR (al
, bl
))
925 case PAIR (ONLY_REAL
, ONLY_REAL
):
926 rr
= gimple_build (&stmts
, loc
, code
, inner_type
, ar
, br
);
930 case PAIR (ONLY_REAL
, ONLY_IMAG
):
932 if (code
== MINUS_EXPR
)
933 ri
= gimple_build (&stmts
, loc
, MINUS_EXPR
, inner_type
, ai
, bi
);
938 case PAIR (ONLY_IMAG
, ONLY_REAL
):
939 if (code
== MINUS_EXPR
)
940 rr
= gimple_build (&stmts
, loc
, MINUS_EXPR
, inner_type
, ar
, br
);
946 case PAIR (ONLY_IMAG
, ONLY_IMAG
):
948 ri
= gimple_build (&stmts
, loc
, code
, inner_type
, ai
, bi
);
951 case PAIR (VARYING
, ONLY_REAL
):
952 rr
= gimple_build (&stmts
, loc
, code
, inner_type
, ar
, br
);
956 case PAIR (VARYING
, ONLY_IMAG
):
958 ri
= gimple_build (&stmts
, loc
, code
, inner_type
, ai
, bi
);
961 case PAIR (ONLY_REAL
, VARYING
):
962 if (code
== MINUS_EXPR
)
964 rr
= gimple_build (&stmts
, loc
, code
, inner_type
, ar
, br
);
968 case PAIR (ONLY_IMAG
, VARYING
):
969 if (code
== MINUS_EXPR
)
972 ri
= gimple_build (&stmts
, loc
, code
, inner_type
, ai
, bi
);
975 case PAIR (VARYING
, VARYING
):
977 rr
= gimple_build (&stmts
, loc
, code
, inner_type
, ar
, br
);
978 ri
= gimple_build (&stmts
, loc
, code
, inner_type
, ai
, bi
);
985 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
986 update_complex_assignment (gsi
, rr
, ri
);
989 /* Expand a complex multiplication or division to a libcall to the c99
990 compliant routines. TYPE is the complex type of the operation.
991 If INPLACE_P replace the statement at GSI with
992 the libcall and return NULL_TREE. Else insert the call, assign its
993 result to an output variable and return that variable. If INPLACE_P
994 is true then the statement being replaced should be an assignment
998 expand_complex_libcall (gimple_stmt_iterator
*gsi
, tree type
, tree ar
, tree ai
,
999 tree br
, tree bi
, enum tree_code code
, bool inplace_p
)
1002 enum built_in_function bcode
;
1006 mode
= TYPE_MODE (type
);
1007 gcc_assert (GET_MODE_CLASS (mode
) == MODE_COMPLEX_FLOAT
);
1009 if (code
== MULT_EXPR
)
1010 bcode
= ((enum built_in_function
)
1011 (BUILT_IN_COMPLEX_MUL_MIN
+ mode
- MIN_MODE_COMPLEX_FLOAT
));
1012 else if (code
== RDIV_EXPR
)
1013 bcode
= ((enum built_in_function
)
1014 (BUILT_IN_COMPLEX_DIV_MIN
+ mode
- MIN_MODE_COMPLEX_FLOAT
));
1017 fn
= builtin_decl_explicit (bcode
);
1018 stmt
= gimple_build_call (fn
, 4, ar
, ai
, br
, bi
);
1022 gimple
*old_stmt
= gsi_stmt (*gsi
);
1023 gimple_call_set_nothrow (stmt
, !stmt_could_throw_p (cfun
, old_stmt
));
1024 lhs
= gimple_assign_lhs (old_stmt
);
1025 gimple_call_set_lhs (stmt
, lhs
);
1026 gsi_replace (gsi
, stmt
, true);
1028 type
= TREE_TYPE (type
);
1029 if (stmt_can_throw_internal (cfun
, stmt
))
1033 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
1034 if (!(e
->flags
& EDGE_EH
))
1036 basic_block bb
= split_edge (e
);
1037 gimple_stmt_iterator gsi2
= gsi_start_bb (bb
);
1038 update_complex_components (&gsi2
, stmt
,
1039 build1 (REALPART_EXPR
, type
, lhs
),
1040 build1 (IMAGPART_EXPR
, type
, lhs
));
1044 update_complex_components (gsi
, stmt
,
1045 build1 (REALPART_EXPR
, type
, lhs
),
1046 build1 (IMAGPART_EXPR
, type
, lhs
));
1047 SSA_NAME_DEF_STMT (lhs
) = stmt
;
1051 gimple_call_set_nothrow (stmt
, true);
1052 lhs
= make_ssa_name (type
);
1053 gimple_call_set_lhs (stmt
, lhs
);
1054 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1059 /* Perform a complex multiplication on two complex constants A, B represented
1060 by AR, AI, BR, BI of type TYPE.
1061 The operation we want is: a * b = (ar*br - ai*bi) + i(ar*bi + br*ai).
1062 Insert the GIMPLE statements into GSI. Store the real and imaginary
1063 components of the result into RR and RI. */
1066 expand_complex_multiplication_components (gimple_seq
*stmts
, location_t loc
,
1067 tree type
, tree ar
, tree ai
,
1071 tree t1
, t2
, t3
, t4
;
1073 t1
= gimple_build (stmts
, loc
, MULT_EXPR
, type
, ar
, br
);
1074 t2
= gimple_build (stmts
, loc
, MULT_EXPR
, type
, ai
, bi
);
1075 t3
= gimple_build (stmts
, loc
, MULT_EXPR
, type
, ar
, bi
);
1077 /* Avoid expanding redundant multiplication for the common
1078 case of squaring a complex number. */
1079 if (ar
== br
&& ai
== bi
)
1082 t4
= gimple_build (stmts
, loc
, MULT_EXPR
, type
, ai
, br
);
1084 *rr
= gimple_build (stmts
, loc
, MINUS_EXPR
, type
, t1
, t2
);
1085 *ri
= gimple_build (stmts
, loc
, PLUS_EXPR
, type
, t3
, t4
);
1088 /* Expand complex multiplication to scalars:
1089 a * b = (ar*br - ai*bi) + i(ar*bi + br*ai)
1093 expand_complex_multiplication (gimple_stmt_iterator
*gsi
, tree type
,
1094 tree ar
, tree ai
, tree br
, tree bi
,
1095 complex_lattice_t al
, complex_lattice_t bl
)
1098 tree inner_type
= TREE_TYPE (type
);
1099 location_t loc
= gimple_location (gsi_stmt (*gsi
));
1100 gimple_seq stmts
= NULL
;
1104 complex_lattice_t tl
;
1105 rr
= ar
, ar
= br
, br
= rr
;
1106 ri
= ai
, ai
= bi
, bi
= ri
;
1107 tl
= al
, al
= bl
, bl
= tl
;
1110 switch (PAIR (al
, bl
))
1112 case PAIR (ONLY_REAL
, ONLY_REAL
):
1113 rr
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ar
, br
);
1117 case PAIR (ONLY_IMAG
, ONLY_REAL
):
1119 if (TREE_CODE (ai
) == REAL_CST
1120 && real_identical (&TREE_REAL_CST (ai
), &dconst1
))
1123 ri
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ai
, br
);
1126 case PAIR (ONLY_IMAG
, ONLY_IMAG
):
1127 rr
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ai
, bi
);
1128 rr
= gimple_build (&stmts
, loc
, NEGATE_EXPR
, inner_type
, rr
);
1132 case PAIR (VARYING
, ONLY_REAL
):
1133 rr
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ar
, br
);
1134 ri
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ai
, br
);
1137 case PAIR (VARYING
, ONLY_IMAG
):
1138 rr
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ai
, bi
);
1139 rr
= gimple_build (&stmts
, loc
, NEGATE_EXPR
, inner_type
, rr
);
1140 ri
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ar
, bi
);
1143 case PAIR (VARYING
, VARYING
):
1144 if (flag_complex_method
== 2 && SCALAR_FLOAT_TYPE_P (inner_type
))
1146 /* If optimizing for size or not at all just do a libcall.
1147 Same if there are exception-handling edges or signaling NaNs. */
1148 if (optimize
== 0 || optimize_bb_for_size_p (gsi_bb (*gsi
))
1149 || stmt_can_throw_internal (cfun
, gsi_stmt (*gsi
))
1150 || flag_signaling_nans
)
1152 expand_complex_libcall (gsi
, type
, ar
, ai
, br
, bi
,
1157 if (!HONOR_NANS (inner_type
))
1159 /* If we are not worrying about NaNs expand to
1160 (ar*br - ai*bi) + i(ar*bi + br*ai) directly. */
1161 expand_complex_multiplication_components (&stmts
, loc
, inner_type
,
1167 /* Else, expand x = a * b into
1168 x = (ar*br - ai*bi) + i(ar*bi + br*ai);
1169 if (isunordered (__real__ x, __imag__ x))
1170 x = __muldc3 (a, b); */
1173 expand_complex_multiplication_components (&stmts
, loc
,
1175 br
, bi
, &tmpr
, &tmpi
);
1176 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1180 = gimple_build_cond (UNORDERED_EXPR
, tmpr
, tmpi
,
1181 NULL_TREE
, NULL_TREE
);
1183 basic_block orig_bb
= gsi_bb (*gsi
);
1184 /* We want to keep track of the original complex multiplication
1185 statement as we're going to modify it later in
1186 update_complex_assignment. Make sure that insert_cond_bb leaves
1187 that statement in the join block. */
1190 = insert_cond_bb (gsi_bb (*gsi
), gsi_stmt (*gsi
), check
,
1191 profile_probability::very_unlikely ());
1193 gimple_stmt_iterator cond_bb_gsi
= gsi_last_bb (cond_bb
);
1194 gsi_insert_after (&cond_bb_gsi
, gimple_build_nop (), GSI_NEW_STMT
);
1197 = expand_complex_libcall (&cond_bb_gsi
, type
, ar
, ai
, br
,
1198 bi
, MULT_EXPR
, false);
1199 gimple_seq stmts2
= NULL
;
1200 tree cond_real
= gimple_build (&stmts2
, loc
, REALPART_EXPR
,
1201 inner_type
, libcall_res
);
1202 tree cond_imag
= gimple_build (&stmts2
, loc
, IMAGPART_EXPR
,
1203 inner_type
, libcall_res
);
1204 gsi_insert_seq_before (&cond_bb_gsi
, stmts2
, GSI_SAME_STMT
);
1206 basic_block join_bb
= single_succ_edge (cond_bb
)->dest
;
1207 *gsi
= gsi_start_nondebug_after_labels_bb (join_bb
);
1209 /* We have a conditional block with some assignments in cond_bb.
1210 Wire up the PHIs to wrap up. */
1211 rr
= make_ssa_name (inner_type
);
1212 ri
= make_ssa_name (inner_type
);
1213 edge cond_to_join
= single_succ_edge (cond_bb
);
1214 edge orig_to_join
= find_edge (orig_bb
, join_bb
);
1216 gphi
*real_phi
= create_phi_node (rr
, gsi_bb (*gsi
));
1217 add_phi_arg (real_phi
, cond_real
, cond_to_join
, UNKNOWN_LOCATION
);
1218 add_phi_arg (real_phi
, tmpr
, orig_to_join
, UNKNOWN_LOCATION
);
1220 gphi
*imag_phi
= create_phi_node (ri
, gsi_bb (*gsi
));
1221 add_phi_arg (imag_phi
, cond_imag
, cond_to_join
, UNKNOWN_LOCATION
);
1222 add_phi_arg (imag_phi
, tmpi
, orig_to_join
, UNKNOWN_LOCATION
);
1225 /* If we are not worrying about NaNs expand to
1226 (ar*br - ai*bi) + i(ar*bi + br*ai) directly. */
1227 expand_complex_multiplication_components (&stmts
, loc
,
1236 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1237 update_complex_assignment (gsi
, rr
, ri
);
1240 /* Keep this algorithm in sync with fold-const.c:const_binop().
1242 Expand complex division to scalars, straightforward algorithm.
1243 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1248 expand_complex_div_straight (gimple_stmt_iterator
*gsi
, tree inner_type
,
1249 tree ar
, tree ai
, tree br
, tree bi
,
1250 enum tree_code code
)
1252 gimple_seq stmts
= NULL
;
1253 location_t loc
= gimple_location (gsi_stmt (*gsi
));
1254 tree rr
, ri
, div
, t1
, t2
, t3
;
1256 t1
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, br
, br
);
1257 t2
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, bi
, bi
);
1258 div
= gimple_build (&stmts
, loc
, PLUS_EXPR
, inner_type
, t1
, t2
);
1260 t1
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ar
, br
);
1261 t2
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ai
, bi
);
1262 t3
= gimple_build (&stmts
, loc
, PLUS_EXPR
, inner_type
, t1
, t2
);
1263 rr
= gimple_build (&stmts
, loc
, code
, inner_type
, t3
, div
);
1265 t1
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ai
, br
);
1266 t2
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ar
, bi
);
1267 t3
= gimple_build (&stmts
, loc
, MINUS_EXPR
, inner_type
, t1
, t2
);
1268 ri
= gimple_build (&stmts
, loc
, code
, inner_type
, t3
, div
);
1270 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1271 update_complex_assignment (gsi
, rr
, ri
);
1274 /* Keep this algorithm in sync with fold-const.c:const_binop().
1276 Expand complex division to scalars, modified algorithm to minimize
1277 overflow with wide input ranges. */
1280 expand_complex_div_wide (gimple_stmt_iterator
*gsi
, tree inner_type
,
1281 tree ar
, tree ai
, tree br
, tree bi
,
1282 enum tree_code code
)
1284 tree rr
, ri
, ratio
, div
, t1
, t2
, tr
, ti
, compare
;
1285 basic_block bb_cond
, bb_true
, bb_false
, bb_join
;
1287 gimple_seq stmts
= NULL
;
1288 location_t loc
= gimple_location (gsi_stmt (*gsi
));
1290 /* Examine |br| < |bi|, and branch. */
1291 t1
= gimple_build (&stmts
, loc
, ABS_EXPR
, inner_type
, br
);
1292 t2
= gimple_build (&stmts
, loc
, ABS_EXPR
, inner_type
, bi
);
1293 compare
= gimple_build (&stmts
, loc
,
1294 LT_EXPR
, boolean_type_node
, t1
, t2
);
1296 bb_cond
= bb_true
= bb_false
= bb_join
= NULL
;
1297 rr
= ri
= tr
= ti
= NULL
;
1298 if (TREE_CODE (compare
) != INTEGER_CST
)
1303 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1305 stmt
= gimple_build_cond (NE_EXPR
, compare
, boolean_false_node
,
1306 NULL_TREE
, NULL_TREE
);
1307 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1309 /* Split the original block, and create the TRUE and FALSE blocks. */
1310 e
= split_block (gsi_bb (*gsi
), stmt
);
1313 bb_true
= create_empty_bb (bb_cond
);
1314 bb_false
= create_empty_bb (bb_true
);
1315 bb_true
->count
= bb_false
->count
1316 = bb_cond
->count
.apply_probability (profile_probability::even ());
1318 /* Wire the blocks together. */
1319 e
->flags
= EDGE_TRUE_VALUE
;
1320 /* TODO: With value profile we could add an historgram to determine real
1322 e
->probability
= profile_probability::even ();
1323 redirect_edge_succ (e
, bb_true
);
1324 edge e2
= make_edge (bb_cond
, bb_false
, EDGE_FALSE_VALUE
);
1325 e2
->probability
= profile_probability::even ();
1326 make_single_succ_edge (bb_true
, bb_join
, EDGE_FALLTHRU
);
1327 make_single_succ_edge (bb_false
, bb_join
, EDGE_FALLTHRU
);
1328 add_bb_to_loop (bb_true
, bb_cond
->loop_father
);
1329 add_bb_to_loop (bb_false
, bb_cond
->loop_father
);
1331 /* Update dominance info. Note that bb_join's data was
1332 updated by split_block. */
1333 if (dom_info_available_p (CDI_DOMINATORS
))
1335 set_immediate_dominator (CDI_DOMINATORS
, bb_true
, bb_cond
);
1336 set_immediate_dominator (CDI_DOMINATORS
, bb_false
, bb_cond
);
1339 rr
= create_tmp_reg (inner_type
);
1340 ri
= create_tmp_reg (inner_type
);
1344 gimple_seq_discard (stmts
);
1348 /* In the TRUE branch, we compute
1350 div = (br * ratio) + bi;
1351 tr = (ar * ratio) + ai;
1352 ti = (ai * ratio) - ar;
1355 if (bb_true
|| integer_nonzerop (compare
))
1359 *gsi
= gsi_last_bb (bb_true
);
1360 gsi_insert_after (gsi
, gimple_build_nop (), GSI_NEW_STMT
);
1363 ratio
= gimple_build (&stmts
, loc
, code
, inner_type
, br
, bi
);
1365 t1
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, br
, ratio
);
1366 div
= gimple_build (&stmts
, loc
, PLUS_EXPR
, inner_type
, t1
, bi
);
1368 t1
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ar
, ratio
);
1369 tr
= gimple_build (&stmts
, loc
, PLUS_EXPR
, inner_type
, t1
, ai
);
1371 t1
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ai
, ratio
);
1372 ti
= gimple_build (&stmts
, loc
, MINUS_EXPR
, inner_type
, t1
, ar
);
1374 tr
= gimple_build (&stmts
, loc
, code
, inner_type
, tr
, div
);
1375 ti
= gimple_build (&stmts
, loc
, code
, inner_type
, ti
, div
);
1376 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1381 stmt
= gimple_build_assign (rr
, tr
);
1382 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1383 stmt
= gimple_build_assign (ri
, ti
);
1384 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1385 gsi_remove (gsi
, true);
1389 /* In the FALSE branch, we compute
1391 divisor = (d * ratio) + c;
1392 tr = (b * ratio) + a;
1393 ti = b - (a * ratio);
1396 if (bb_false
|| integer_zerop (compare
))
1400 *gsi
= gsi_last_bb (bb_false
);
1401 gsi_insert_after (gsi
, gimple_build_nop (), GSI_NEW_STMT
);
1404 ratio
= gimple_build (&stmts
, loc
, code
, inner_type
, bi
, br
);
1406 t1
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, bi
, ratio
);
1407 div
= gimple_build (&stmts
, loc
, PLUS_EXPR
, inner_type
, t1
, br
);
1409 t1
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ai
, ratio
);
1410 tr
= gimple_build (&stmts
, loc
, PLUS_EXPR
, inner_type
, t1
, ar
);
1412 t1
= gimple_build (&stmts
, loc
, MULT_EXPR
, inner_type
, ar
, ratio
);
1413 ti
= gimple_build (&stmts
, loc
, MINUS_EXPR
, inner_type
, ai
, t1
);
1415 tr
= gimple_build (&stmts
, loc
, code
, inner_type
, tr
, div
);
1416 ti
= gimple_build (&stmts
, loc
, code
, inner_type
, ti
, div
);
1417 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1422 stmt
= gimple_build_assign (rr
, tr
);
1423 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1424 stmt
= gimple_build_assign (ri
, ti
);
1425 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1426 gsi_remove (gsi
, true);
1431 *gsi
= gsi_start_bb (bb_join
);
1435 update_complex_assignment (gsi
, rr
, ri
);
1438 /* Expand complex division to scalars. */
1441 expand_complex_division (gimple_stmt_iterator
*gsi
, tree type
,
1442 tree ar
, tree ai
, tree br
, tree bi
,
1443 enum tree_code code
,
1444 complex_lattice_t al
, complex_lattice_t bl
)
1447 gimple_seq stmts
= NULL
;
1448 location_t loc
= gimple_location (gsi_stmt (*gsi
));
1450 tree inner_type
= TREE_TYPE (type
);
1451 switch (PAIR (al
, bl
))
1453 case PAIR (ONLY_REAL
, ONLY_REAL
):
1454 rr
= gimple_build (&stmts
, loc
, code
, inner_type
, ar
, br
);
1458 case PAIR (ONLY_REAL
, ONLY_IMAG
):
1460 ri
= gimple_build (&stmts
, loc
, code
, inner_type
, ar
, bi
);
1461 ri
= gimple_build (&stmts
, loc
, NEGATE_EXPR
, inner_type
, ri
);
1464 case PAIR (ONLY_IMAG
, ONLY_REAL
):
1466 ri
= gimple_build (&stmts
, loc
, code
, inner_type
, ai
, br
);
1469 case PAIR (ONLY_IMAG
, ONLY_IMAG
):
1470 rr
= gimple_build (&stmts
, loc
, code
, inner_type
, ai
, bi
);
1474 case PAIR (VARYING
, ONLY_REAL
):
1475 rr
= gimple_build (&stmts
, loc
, code
, inner_type
, ar
, br
);
1476 ri
= gimple_build (&stmts
, loc
, code
, inner_type
, ai
, br
);
1479 case PAIR (VARYING
, ONLY_IMAG
):
1480 rr
= gimple_build (&stmts
, loc
, code
, inner_type
, ai
, bi
);
1481 ri
= gimple_build (&stmts
, loc
, code
, inner_type
, ar
, bi
);
1482 ri
= gimple_build (&stmts
, loc
, NEGATE_EXPR
, inner_type
, ri
);
1485 case PAIR (ONLY_REAL
, VARYING
):
1486 case PAIR (ONLY_IMAG
, VARYING
):
1487 case PAIR (VARYING
, VARYING
):
1488 switch (flag_complex_method
)
1491 /* straightforward implementation of complex divide acceptable. */
1492 expand_complex_div_straight (gsi
, inner_type
, ar
, ai
, br
, bi
, code
);
1496 if (SCALAR_FLOAT_TYPE_P (inner_type
))
1498 expand_complex_libcall (gsi
, type
, ar
, ai
, br
, bi
, code
, true);
1504 /* wide ranges of inputs must work for complex divide. */
1505 expand_complex_div_wide (gsi
, inner_type
, ar
, ai
, br
, bi
, code
);
1517 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1518 update_complex_assignment (gsi
, rr
, ri
);
1521 /* Expand complex negation to scalars:
1526 expand_complex_negation (gimple_stmt_iterator
*gsi
, tree inner_type
,
1530 gimple_seq stmts
= NULL
;
1531 location_t loc
= gimple_location (gsi_stmt (*gsi
));
1533 rr
= gimple_build (&stmts
, loc
, NEGATE_EXPR
, inner_type
, ar
);
1534 ri
= gimple_build (&stmts
, loc
, NEGATE_EXPR
, inner_type
, ai
);
1536 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1537 update_complex_assignment (gsi
, rr
, ri
);
1540 /* Expand complex conjugate to scalars:
1545 expand_complex_conjugate (gimple_stmt_iterator
*gsi
, tree inner_type
,
1549 gimple_seq stmts
= NULL
;
1550 location_t loc
= gimple_location (gsi_stmt (*gsi
));
1552 ri
= gimple_build (&stmts
, loc
, NEGATE_EXPR
, inner_type
, ai
);
1554 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1555 update_complex_assignment (gsi
, ar
, ri
);
1558 /* Expand complex comparison (EQ or NE only). */
1561 expand_complex_comparison (gimple_stmt_iterator
*gsi
, tree ar
, tree ai
,
1562 tree br
, tree bi
, enum tree_code code
)
1564 tree cr
, ci
, cc
, type
;
1565 gimple
*stmt
= gsi_stmt (*gsi
);
1566 gimple_seq stmts
= NULL
;
1567 location_t loc
= gimple_location (stmt
);
1569 cr
= gimple_build (&stmts
, loc
, code
, boolean_type_node
, ar
, br
);
1570 ci
= gimple_build (&stmts
, loc
, code
, boolean_type_node
, ai
, bi
);
1571 cc
= gimple_build (&stmts
, loc
,
1572 (code
== EQ_EXPR
? BIT_AND_EXPR
: BIT_IOR_EXPR
),
1573 boolean_type_node
, cr
, ci
);
1574 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
1576 switch (gimple_code (stmt
))
1580 greturn
*return_stmt
= as_a
<greturn
*> (stmt
);
1581 type
= TREE_TYPE (gimple_return_retval (return_stmt
));
1582 gimple_return_set_retval (return_stmt
, fold_convert (type
, cc
));
1587 type
= TREE_TYPE (gimple_assign_lhs (stmt
));
1588 gimple_assign_set_rhs_from_tree (gsi
, fold_convert (type
, cc
));
1589 stmt
= gsi_stmt (*gsi
);
1594 gcond
*cond_stmt
= as_a
<gcond
*> (stmt
);
1595 gimple_cond_set_code (cond_stmt
, EQ_EXPR
);
1596 gimple_cond_set_lhs (cond_stmt
, cc
);
1597 gimple_cond_set_rhs (cond_stmt
, boolean_true_node
);
1606 if (maybe_clean_eh_stmt (stmt
))
1607 bitmap_set_bit (need_eh_cleanup
, gimple_bb (stmt
)->index
);
1610 /* Expand inline asm that sets some complex SSA_NAMEs. */
1613 expand_complex_asm (gimple_stmt_iterator
*gsi
)
1615 gasm
*stmt
= as_a
<gasm
*> (gsi_stmt (*gsi
));
1618 for (i
= 0; i
< gimple_asm_noutputs (stmt
); ++i
)
1620 tree link
= gimple_asm_output_op (stmt
, i
);
1621 tree op
= TREE_VALUE (link
);
1622 if (TREE_CODE (op
) == SSA_NAME
1623 && TREE_CODE (TREE_TYPE (op
)) == COMPLEX_TYPE
)
1625 tree type
= TREE_TYPE (op
);
1626 tree inner_type
= TREE_TYPE (type
);
1627 tree r
= build1 (REALPART_EXPR
, inner_type
, op
);
1628 tree i
= build1 (IMAGPART_EXPR
, inner_type
, op
);
1629 gimple_seq list
= set_component_ssa_name (op
, false, r
);
1632 gsi_insert_seq_after (gsi
, list
, GSI_CONTINUE_LINKING
);
1634 list
= set_component_ssa_name (op
, true, i
);
1636 gsi_insert_seq_after (gsi
, list
, GSI_CONTINUE_LINKING
);
1641 /* Process one statement. If we identify a complex operation, expand it. */
1644 expand_complex_operations_1 (gimple_stmt_iterator
*gsi
)
1646 gimple
*stmt
= gsi_stmt (*gsi
);
1647 tree type
, inner_type
, lhs
;
1648 tree ac
, ar
, ai
, bc
, br
, bi
;
1649 complex_lattice_t al
, bl
;
1650 enum tree_code code
;
1652 if (gimple_code (stmt
) == GIMPLE_ASM
)
1654 expand_complex_asm (gsi
);
1658 lhs
= gimple_get_lhs (stmt
);
1659 if (!lhs
&& gimple_code (stmt
) != GIMPLE_COND
)
1662 type
= TREE_TYPE (gimple_op (stmt
, 0));
1663 code
= gimple_expr_code (stmt
);
1665 /* Initial filter for operations we handle. */
1671 case TRUNC_DIV_EXPR
:
1673 case FLOOR_DIV_EXPR
:
1674 case ROUND_DIV_EXPR
:
1678 if (TREE_CODE (type
) != COMPLEX_TYPE
)
1680 inner_type
= TREE_TYPE (type
);
1685 /* Note, both GIMPLE_ASSIGN and GIMPLE_COND may have an EQ_EXPR
1686 subcode, so we need to access the operands using gimple_op. */
1687 inner_type
= TREE_TYPE (gimple_op (stmt
, 1));
1688 if (TREE_CODE (inner_type
) != COMPLEX_TYPE
)
1696 /* GIMPLE_COND may also fallthru here, but we do not need to
1697 do anything with it. */
1698 if (gimple_code (stmt
) == GIMPLE_COND
)
1701 if (TREE_CODE (type
) == COMPLEX_TYPE
)
1702 expand_complex_move (gsi
, type
);
1703 else if (is_gimple_assign (stmt
)
1704 && (gimple_assign_rhs_code (stmt
) == REALPART_EXPR
1705 || gimple_assign_rhs_code (stmt
) == IMAGPART_EXPR
)
1706 && TREE_CODE (lhs
) == SSA_NAME
)
1708 rhs
= gimple_assign_rhs1 (stmt
);
1709 rhs
= extract_component (gsi
, TREE_OPERAND (rhs
, 0),
1710 gimple_assign_rhs_code (stmt
)
1713 gimple_assign_set_rhs_from_tree (gsi
, rhs
);
1714 stmt
= gsi_stmt (*gsi
);
1721 /* Extract the components of the two complex values. Make sure and
1722 handle the common case of the same value used twice specially. */
1723 if (is_gimple_assign (stmt
))
1725 ac
= gimple_assign_rhs1 (stmt
);
1726 bc
= (gimple_num_ops (stmt
) > 2) ? gimple_assign_rhs2 (stmt
) : NULL
;
1728 /* GIMPLE_CALL cannot get here. */
1731 ac
= gimple_cond_lhs (stmt
);
1732 bc
= gimple_cond_rhs (stmt
);
1735 ar
= extract_component (gsi
, ac
, false, true);
1736 ai
= extract_component (gsi
, ac
, true, true);
1742 br
= extract_component (gsi
, bc
, 0, true);
1743 bi
= extract_component (gsi
, bc
, 1, true);
1746 br
= bi
= NULL_TREE
;
1748 al
= find_lattice_value (ac
);
1749 if (al
== UNINITIALIZED
)
1752 if (TREE_CODE_CLASS (code
) == tcc_unary
)
1758 bl
= find_lattice_value (bc
);
1759 if (bl
== UNINITIALIZED
)
1767 expand_complex_addition (gsi
, inner_type
, ar
, ai
, br
, bi
, code
, al
, bl
);
1771 expand_complex_multiplication (gsi
, type
, ar
, ai
, br
, bi
, al
, bl
);
1774 case TRUNC_DIV_EXPR
:
1776 case FLOOR_DIV_EXPR
:
1777 case ROUND_DIV_EXPR
:
1779 expand_complex_division (gsi
, type
, ar
, ai
, br
, bi
, code
, al
, bl
);
1783 expand_complex_negation (gsi
, inner_type
, ar
, ai
);
1787 expand_complex_conjugate (gsi
, inner_type
, ar
, ai
);
1792 expand_complex_comparison (gsi
, ar
, ai
, br
, bi
, code
);
1801 /* Entry point for complex operation lowering during optimization. */
1804 tree_lower_complex (void)
1806 gimple_stmt_iterator gsi
;
1811 if (!init_dont_simulate_again ())
1814 complex_lattice_values
.create (num_ssa_names
);
1815 complex_lattice_values
.safe_grow_cleared (num_ssa_names
, true);
1817 init_parameter_lattice_values ();
1818 class complex_propagate complex_propagate
;
1819 complex_propagate
.ssa_propagate ();
1821 need_eh_cleanup
= BITMAP_ALLOC (NULL
);
1823 complex_variable_components
= new int_tree_htab_type (10);
1825 complex_ssa_name_components
.create (2 * num_ssa_names
);
1826 complex_ssa_name_components
.safe_grow_cleared (2 * num_ssa_names
, true);
1828 update_parameter_components ();
1830 rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
1831 n_bbs
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
1832 for (i
= 0; i
< n_bbs
; i
++)
1834 bb
= BASIC_BLOCK_FOR_FN (cfun
, rpo
[i
]);
1837 update_phi_components (bb
);
1838 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1839 expand_complex_operations_1 (&gsi
);
1844 if (!phis_to_revisit
.is_empty ())
1846 unsigned int n
= phis_to_revisit
.length ();
1847 for (unsigned int j
= 0; j
< n
; j
+= 3)
1848 for (unsigned int k
= 0; k
< 2; k
++)
1849 if (gphi
*phi
= phis_to_revisit
[j
+ k
+ 1])
1851 unsigned int m
= gimple_phi_num_args (phi
);
1852 for (unsigned int l
= 0; l
< m
; ++l
)
1854 tree op
= gimple_phi_arg_def (phi
, l
);
1855 if (TREE_CODE (op
) == SSA_NAME
1856 || is_gimple_min_invariant (op
))
1858 tree arg
= gimple_phi_arg_def (phis_to_revisit
[j
], l
);
1859 op
= extract_component (NULL
, arg
, k
> 0, false, false);
1860 SET_PHI_ARG_DEF (phi
, l
, op
);
1863 phis_to_revisit
.release ();
1866 gsi_commit_edge_inserts ();
1869 = gimple_purge_all_dead_eh_edges (need_eh_cleanup
) ? TODO_cleanup_cfg
: 0;
1870 BITMAP_FREE (need_eh_cleanup
);
1872 delete complex_variable_components
;
1873 complex_variable_components
= NULL
;
1874 complex_ssa_name_components
.release ();
1875 complex_lattice_values
.release ();
1881 const pass_data pass_data_lower_complex
=
1883 GIMPLE_PASS
, /* type */
1884 "cplxlower", /* name */
1885 OPTGROUP_NONE
, /* optinfo_flags */
1886 TV_NONE
, /* tv_id */
1887 PROP_ssa
, /* properties_required */
1888 PROP_gimple_lcx
, /* properties_provided */
1889 0, /* properties_destroyed */
1890 0, /* todo_flags_start */
1891 TODO_update_ssa
, /* todo_flags_finish */
1894 class pass_lower_complex
: public gimple_opt_pass
1897 pass_lower_complex (gcc::context
*ctxt
)
1898 : gimple_opt_pass (pass_data_lower_complex
, ctxt
)
1901 /* opt_pass methods: */
1902 opt_pass
* clone () { return new pass_lower_complex (m_ctxt
); }
1903 virtual unsigned int execute (function
*) { return tree_lower_complex (); }
1905 }; // class pass_lower_complex
1910 make_pass_lower_complex (gcc::context
*ctxt
)
1912 return new pass_lower_complex (ctxt
);
1918 const pass_data pass_data_lower_complex_O0
=
1920 GIMPLE_PASS
, /* type */
1921 "cplxlower0", /* name */
1922 OPTGROUP_NONE
, /* optinfo_flags */
1923 TV_NONE
, /* tv_id */
1924 PROP_cfg
, /* properties_required */
1925 PROP_gimple_lcx
, /* properties_provided */
1926 0, /* properties_destroyed */
1927 0, /* todo_flags_start */
1928 TODO_update_ssa
, /* todo_flags_finish */
1931 class pass_lower_complex_O0
: public gimple_opt_pass
1934 pass_lower_complex_O0 (gcc::context
*ctxt
)
1935 : gimple_opt_pass (pass_data_lower_complex_O0
, ctxt
)
1938 /* opt_pass methods: */
1939 virtual bool gate (function
*fun
)
1941 /* With errors, normal optimization passes are not run. If we don't
1942 lower complex operations at all, rtl expansion will abort. */
1943 return !(fun
->curr_properties
& PROP_gimple_lcx
);
1946 virtual unsigned int execute (function
*) { return tree_lower_complex (); }
1948 }; // class pass_lower_complex_O0
1953 make_pass_lower_complex_O0 (gcc::context
*ctxt
)
1955 return new pass_lower_complex_O0 (ctxt
);