1 /* Lower complex number operations to scalar operations.
2 Copyright (C) 2004, 2005 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 2, 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 COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include "coretypes.h"
29 #include "tree-flow.h"
30 #include "tree-gimple.h"
31 #include "tree-iterator.h"
32 #include "tree-pass.h"
33 #include "tree-ssa-propagate.h"
36 /* For each complex ssa name, a lattice value. We're interested in finding
37 out whether a complex number is degenerate in some way, having only real
38 or only complex parts. */
48 #define PAIR(a, b) ((a) << 2 | (b))
50 DEF_VEC_I(complex_lattice_t
);
51 DEF_VEC_ALLOC_I(complex_lattice_t
, heap
);
53 static VEC(complex_lattice_t
, heap
) *complex_lattice_values
;
55 /* For each complex variable, a pair of variables for the components. */
56 static VEC(tree
, heap
) *complex_variable_components
;
59 /* Return true if T is not a zero constant. In the case of real values,
60 we're only interested in +0.0. */
63 some_nonzerop (tree t
)
67 if (TREE_CODE (t
) == REAL_CST
)
68 zerop
= REAL_VALUES_IDENTICAL (TREE_REAL_CST (t
), dconst0
);
69 else if (TREE_CODE (t
) == INTEGER_CST
)
70 zerop
= integer_zerop (t
);
75 /* Compute a lattice value from T. It may be a gimple_val, or, as a
76 special exception, a COMPLEX_EXPR. */
78 static complex_lattice_t
79 find_lattice_value (tree t
)
83 complex_lattice_t ret
;
85 switch (TREE_CODE (t
))
88 return VEC_index (complex_lattice_t
, complex_lattice_values
,
89 SSA_NAME_VERSION (t
));
92 real
= TREE_REALPART (t
);
93 imag
= TREE_IMAGPART (t
);
97 real
= TREE_OPERAND (t
, 0);
98 imag
= TREE_OPERAND (t
, 1);
105 r
= some_nonzerop (real
);
106 i
= some_nonzerop (imag
);
107 ret
= r
*ONLY_REAL
+ i
*ONLY_IMAG
;
109 /* ??? On occasion we could do better than mapping 0+0i to real, but we
110 certainly don't want to leave it UNINITIALIZED, which eventually gets
111 mapped to VARYING. */
112 if (ret
== UNINITIALIZED
)
118 /* Determine if LHS is something for which we're interested in seeing
119 simulation results. */
122 is_complex_reg (tree lhs
)
124 return TREE_CODE (TREE_TYPE (lhs
)) == COMPLEX_TYPE
&& is_gimple_reg (lhs
);
127 /* Mark the incoming parameters to the function as VARYING. */
130 init_parameter_lattice_values (void)
134 for (parm
= DECL_ARGUMENTS (cfun
->decl
); parm
; parm
= TREE_CHAIN (parm
))
135 if (is_complex_reg (parm
) && var_ann (parm
) != NULL
)
137 tree ssa_name
= default_def (parm
);
138 VEC_replace (complex_lattice_t
, complex_lattice_values
,
139 SSA_NAME_VERSION (ssa_name
), VARYING
);
143 /* Initialize DONT_SIMULATE_AGAIN for each stmt and phi. Return false if
144 we found no statements we want to simulate, and thus there's nothing for
145 the entire pass to do. */
148 init_dont_simulate_again (void)
151 block_stmt_iterator bsi
;
153 bool saw_a_complex_value
= false;
157 for (phi
= phi_nodes (bb
); phi
; phi
= PHI_CHAIN (phi
))
158 DONT_SIMULATE_AGAIN (phi
) = !is_complex_reg (PHI_RESULT (phi
));
160 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
162 tree stmt
= bsi_stmt (bsi
);
165 if (TREE_CODE (stmt
) == MODIFY_EXPR
166 && is_complex_reg (TREE_OPERAND (stmt
, 0)))
169 saw_a_complex_value
= true;
172 DONT_SIMULATE_AGAIN (stmt
) = dsa
;
176 return saw_a_complex_value
;
180 /* Evaluate statement STMT against the complex lattice defined above. */
182 static enum ssa_prop_result
183 complex_visit_stmt (tree stmt
, edge
*taken_edge_p ATTRIBUTE_UNUSED
,
186 complex_lattice_t new_l
, old_l
, op1_l
, op2_l
;
190 /* These conditions should be satisfied due to the initial filter
191 set up in init_dont_simulate_again. */
192 gcc_assert (TREE_CODE (stmt
) == MODIFY_EXPR
);
194 lhs
= TREE_OPERAND (stmt
, 0);
195 rhs
= TREE_OPERAND (stmt
, 1);
197 gcc_assert (TREE_CODE (lhs
) == SSA_NAME
);
198 gcc_assert (TREE_CODE (TREE_TYPE (lhs
)) == COMPLEX_TYPE
);
201 ver
= SSA_NAME_VERSION (lhs
);
202 old_l
= VEC_index (complex_lattice_t
, complex_lattice_values
, ver
);
204 switch (TREE_CODE (rhs
))
209 new_l
= find_lattice_value (rhs
);
214 op1_l
= find_lattice_value (TREE_OPERAND (rhs
, 0));
215 op2_l
= find_lattice_value (TREE_OPERAND (rhs
, 1));
217 /* We've set up the lattice values such that IOR neatly
219 new_l
= op1_l
| op2_l
;
228 op1_l
= find_lattice_value (TREE_OPERAND (rhs
, 0));
229 op2_l
= find_lattice_value (TREE_OPERAND (rhs
, 1));
231 /* Obviously, if either varies, so does the result. */
232 if (op1_l
== VARYING
|| op2_l
== VARYING
)
234 /* Don't prematurely promote variables if we've not yet seen
236 else if (op1_l
== UNINITIALIZED
)
238 else if (op2_l
== UNINITIALIZED
)
242 /* At this point both numbers have only one component. If the
243 numbers are of opposite kind, the result is imaginary,
244 otherwise the result is real. The add/subtract translates
245 the real/imag from/to 0/1; the ^ performs the comparison. */
246 new_l
= ((op1_l
- ONLY_REAL
) ^ (op2_l
- ONLY_REAL
)) + ONLY_REAL
;
248 /* Don't allow the lattice value to flip-flop indefinitely. */
255 new_l
= find_lattice_value (TREE_OPERAND (rhs
, 0));
263 /* If nothing changed this round, let the propagator know. */
265 return SSA_PROP_NOT_INTERESTING
;
267 VEC_replace (complex_lattice_t
, complex_lattice_values
, ver
, new_l
);
268 return new_l
== VARYING
? SSA_PROP_VARYING
: SSA_PROP_INTERESTING
;
271 /* Evaluate a PHI node against the complex lattice defined above. */
273 static enum ssa_prop_result
274 complex_visit_phi (tree phi
)
276 complex_lattice_t new_l
, old_l
;
281 lhs
= PHI_RESULT (phi
);
283 /* This condition should be satisfied due to the initial filter
284 set up in init_dont_simulate_again. */
285 gcc_assert (TREE_CODE (TREE_TYPE (lhs
)) == COMPLEX_TYPE
);
287 /* We've set up the lattice values such that IOR neatly models PHI meet. */
288 new_l
= UNINITIALIZED
;
289 for (i
= PHI_NUM_ARGS (phi
) - 1; i
>= 0; --i
)
290 new_l
|= find_lattice_value (PHI_ARG_DEF (phi
, i
));
292 ver
= SSA_NAME_VERSION (lhs
);
293 old_l
= VEC_index (complex_lattice_t
, complex_lattice_values
, ver
);
296 return SSA_PROP_NOT_INTERESTING
;
298 VEC_replace (complex_lattice_t
, complex_lattice_values
, ver
, new_l
);
299 return new_l
== VARYING
? SSA_PROP_VARYING
: SSA_PROP_INTERESTING
;
302 /* For each referenced complex gimple register, set up a pair of registers
303 to hold the components of the complex value. */
306 create_components (void)
310 n
= num_referenced_vars
;
311 complex_variable_components
= VEC_alloc (tree
, heap
, 2*n
);
312 VEC_safe_grow (tree
, heap
, complex_variable_components
, 2*n
);
314 for (k
= 0; k
< n
; ++k
)
316 tree var
= referenced_var (k
);
317 tree r
= NULL
, i
= NULL
;
320 && TREE_CODE (TREE_TYPE (var
)) == COMPLEX_TYPE
321 && is_gimple_reg (var
))
323 tree inner_type
= TREE_TYPE (TREE_TYPE (var
));
325 r
= make_rename_temp (inner_type
, "CR");
326 i
= make_rename_temp (inner_type
, "CI");
327 DECL_SOURCE_LOCATION (r
) = DECL_SOURCE_LOCATION (var
);
328 DECL_SOURCE_LOCATION (i
) = DECL_SOURCE_LOCATION (var
);
329 DECL_ARTIFICIAL (r
) = 1;
330 DECL_ARTIFICIAL (i
) = 1;
332 if (DECL_NAME (var
) && !DECL_IGNORED_P (var
))
334 const char *name
= IDENTIFIER_POINTER (DECL_NAME (var
));
336 DECL_NAME (r
) = get_identifier (ACONCAT ((name
, "$real", NULL
)));
337 DECL_NAME (i
) = get_identifier (ACONCAT ((name
, "$imag", NULL
)));
339 SET_DECL_DEBUG_EXPR (r
, build1 (REALPART_EXPR
, inner_type
, var
));
340 SET_DECL_DEBUG_EXPR (i
, build1 (IMAGPART_EXPR
, inner_type
, var
));
341 DECL_DEBUG_EXPR_IS_FROM (r
) = 1;
342 DECL_DEBUG_EXPR_IS_FROM (i
) = 1;
344 DECL_IGNORED_P (r
) = 0;
345 DECL_IGNORED_P (i
) = 0;
347 TREE_NO_WARNING (r
) = TREE_NO_WARNING (var
);
348 TREE_NO_WARNING (i
) = TREE_NO_WARNING (var
);
352 DECL_IGNORED_P (r
) = 1;
353 DECL_IGNORED_P (i
) = 1;
354 TREE_NO_WARNING (r
) = 1;
355 TREE_NO_WARNING (i
) = 1;
359 VEC_replace (tree
, complex_variable_components
, 2*k
, r
);
360 VEC_replace (tree
, complex_variable_components
, 2*k
+ 1, i
);
364 /* Extract the real or imaginary part of a complex variable or constant.
365 Make sure that it's a proper gimple_val and gimplify it if not.
366 Emit any new code before BSI. */
369 extract_component (block_stmt_iterator
*bsi
, tree t
, bool imagpart_p
,
372 switch (TREE_CODE (t
))
375 return imagpart_p
? TREE_IMAGPART (t
) : TREE_REALPART (t
);
378 return TREE_OPERAND (t
, imagpart_p
);
386 tree inner_type
= TREE_TYPE (TREE_TYPE (t
));
388 t
= build1 ((imagpart_p
? IMAGPART_EXPR
: REALPART_EXPR
),
389 inner_type
, unshare_expr (t
));
392 t
= gimplify_val (bsi
, inner_type
, t
);
399 tree def
= SSA_NAME_DEF_STMT (t
);
401 if (TREE_CODE (def
) == MODIFY_EXPR
)
403 def
= TREE_OPERAND (def
, 1);
404 if (TREE_CODE (def
) == COMPLEX_CST
)
405 return imagpart_p
? TREE_IMAGPART (def
) : TREE_REALPART (def
);
406 if (TREE_CODE (def
) == COMPLEX_EXPR
)
408 def
= TREE_OPERAND (def
, imagpart_p
);
409 if (TREE_CONSTANT (def
))
414 return VEC_index (tree
, complex_variable_components
,
415 var_ann (SSA_NAME_VAR (t
))->uid
* 2 + imagpart_p
);
423 /* Update the complex components of the ssa name on the lhs of STMT. */
426 update_complex_components (block_stmt_iterator
*bsi
, tree stmt
, tree r
, tree i
)
428 unsigned int uid
= var_ann (SSA_NAME_VAR (TREE_OPERAND (stmt
, 0)))->uid
;
431 v
= VEC_index (tree
, complex_variable_components
, 2*uid
);
432 x
= build2 (MODIFY_EXPR
, TREE_TYPE (v
), v
, r
);
433 SET_EXPR_LOCUS (x
, EXPR_LOCUS (stmt
));
434 TREE_BLOCK (x
) = TREE_BLOCK (stmt
);
435 bsi_insert_after (bsi
, x
, BSI_NEW_STMT
);
437 v
= VEC_index (tree
, complex_variable_components
, 2*uid
+ 1);
438 x
= build2 (MODIFY_EXPR
, TREE_TYPE (v
), v
, i
);
439 SET_EXPR_LOCUS (x
, EXPR_LOCUS (stmt
));
440 TREE_BLOCK (x
) = TREE_BLOCK (stmt
);
441 bsi_insert_after (bsi
, x
, BSI_NEW_STMT
);
445 update_complex_components_on_edge (edge e
, tree stmt
, tree lhs
, tree r
, tree i
)
447 unsigned int uid
= var_ann (SSA_NAME_VAR (lhs
))->uid
;
450 v
= VEC_index (tree
, complex_variable_components
, 2*uid
);
451 x
= build2 (MODIFY_EXPR
, TREE_TYPE (v
), v
, r
);
454 SET_EXPR_LOCUS (x
, EXPR_LOCUS (stmt
));
455 TREE_BLOCK (x
) = TREE_BLOCK (stmt
);
457 bsi_insert_on_edge (e
, x
);
459 v
= VEC_index (tree
, complex_variable_components
, 2*uid
+ 1);
460 x
= build2 (MODIFY_EXPR
, TREE_TYPE (v
), v
, i
);
463 SET_EXPR_LOCUS (x
, EXPR_LOCUS (stmt
));
464 TREE_BLOCK (x
) = TREE_BLOCK (stmt
);
466 bsi_insert_on_edge (e
, x
);
469 /* Update an assignment to a complex variable in place. */
472 update_complex_assignment (block_stmt_iterator
*bsi
, tree r
, tree i
)
477 mod
= stmt
= bsi_stmt (*bsi
);
478 if (TREE_CODE (stmt
) == RETURN_EXPR
)
479 mod
= TREE_OPERAND (mod
, 0);
481 update_complex_components (bsi
, stmt
, r
, i
);
483 type
= TREE_TYPE (TREE_OPERAND (mod
, 1));
484 TREE_OPERAND (mod
, 1) = build (COMPLEX_EXPR
, type
, r
, i
);
488 /* Generate code at the entry point of the function to initialize the
489 component variables for a complex parameter. */
492 update_parameter_components (void)
494 edge entry_edge
= single_succ_edge (ENTRY_BLOCK_PTR
);
497 for (parm
= DECL_ARGUMENTS (cfun
->decl
); parm
; parm
= TREE_CHAIN (parm
))
499 tree type
= TREE_TYPE (parm
);
502 if (TREE_CODE (type
) != COMPLEX_TYPE
|| !is_gimple_reg (parm
))
505 type
= TREE_TYPE (type
);
506 ssa_name
= default_def (parm
);
508 r
= build1 (REALPART_EXPR
, type
, ssa_name
);
509 i
= build1 (IMAGPART_EXPR
, type
, ssa_name
);
510 update_complex_components_on_edge (entry_edge
, NULL
, ssa_name
, r
, i
);
514 /* Generate code to set the component variables of a complex variable
515 to match the PHI statements in block BB. */
518 update_phi_components (basic_block bb
)
522 for (phi
= phi_nodes (bb
); phi
; phi
= PHI_CHAIN (phi
))
523 if (is_complex_reg (PHI_RESULT (phi
)))
526 tree lhs
= PHI_RESULT (phi
);
528 for (i
= 0, n
= PHI_NUM_ARGS (phi
); i
< n
; ++i
)
530 edge e
= PHI_ARG_EDGE (phi
, i
);
531 tree arg
= PHI_ARG_DEF (phi
, i
);
534 r
= extract_component (NULL
, arg
, 0, false);
535 i
= extract_component (NULL
, arg
, 1, false);
536 update_complex_components_on_edge (e
, NULL
, lhs
, r
, i
);
541 /* Mark each virtual op in STMT for ssa update. */
544 update_all_vops (tree stmt
)
549 FOR_EACH_SSA_TREE_OPERAND (sym
, stmt
, iter
, SSA_OP_ALL_VIRTUALS
)
551 if (TREE_CODE (sym
) == SSA_NAME
)
552 sym
= SSA_NAME_VAR (sym
);
553 mark_sym_for_renaming (sym
);
557 /* Expand a complex move to scalars. */
560 expand_complex_move (block_stmt_iterator
*bsi
, tree stmt
, tree type
,
563 tree inner_type
= TREE_TYPE (type
);
566 if (TREE_CODE (lhs
) == SSA_NAME
)
568 if (is_ctrl_altering_stmt (bsi_stmt (*bsi
)))
573 /* The value is not assigned on the exception edges, so we need not
574 concern ourselves there. We do need to update on the fallthru
576 FOR_EACH_EDGE (e
, ei
, bsi
->bb
->succs
)
577 if (e
->flags
& EDGE_FALLTHRU
)
582 r
= build1 (REALPART_EXPR
, inner_type
, lhs
);
583 i
= build1 (IMAGPART_EXPR
, inner_type
, lhs
);
584 update_complex_components_on_edge (e
, stmt
, lhs
, r
, i
);
586 else if (TREE_CODE (rhs
) == CALL_EXPR
|| TREE_SIDE_EFFECTS (rhs
))
588 r
= build1 (REALPART_EXPR
, inner_type
, lhs
);
589 i
= build1 (IMAGPART_EXPR
, inner_type
, lhs
);
590 update_complex_components (bsi
, stmt
, r
, i
);
594 update_all_vops (bsi_stmt (*bsi
));
595 r
= extract_component (bsi
, rhs
, 0, true);
596 i
= extract_component (bsi
, rhs
, 1, true);
597 update_complex_assignment (bsi
, r
, i
);
600 else if (TREE_CODE (rhs
) == SSA_NAME
&& !TREE_SIDE_EFFECTS (lhs
))
604 r
= extract_component (bsi
, rhs
, 0, false);
605 i
= extract_component (bsi
, rhs
, 1, false);
607 x
= build1 (REALPART_EXPR
, inner_type
, unshare_expr (lhs
));
608 x
= build2 (MODIFY_EXPR
, inner_type
, x
, r
);
609 bsi_insert_before (bsi
, x
, BSI_SAME_STMT
);
611 if (stmt
== bsi_stmt (*bsi
))
613 x
= build1 (IMAGPART_EXPR
, inner_type
, unshare_expr (lhs
));
614 TREE_OPERAND (stmt
, 0) = x
;
615 TREE_OPERAND (stmt
, 1) = i
;
616 TREE_TYPE (stmt
) = inner_type
;
620 x
= build1 (IMAGPART_EXPR
, inner_type
, unshare_expr (lhs
));
621 x
= build2 (MODIFY_EXPR
, inner_type
, x
, i
);
622 bsi_insert_before (bsi
, x
, BSI_SAME_STMT
);
624 stmt
= bsi_stmt (*bsi
);
625 gcc_assert (TREE_CODE (stmt
) == RETURN_EXPR
);
626 TREE_OPERAND (stmt
, 0) = lhs
;
629 update_all_vops (stmt
);
634 /* Expand complex addition to scalars:
635 a + b = (ar + br) + i(ai + bi)
636 a - b = (ar - br) + i(ai + bi)
640 expand_complex_addition (block_stmt_iterator
*bsi
, tree inner_type
,
641 tree ar
, tree ai
, tree br
, tree bi
,
643 complex_lattice_t al
, complex_lattice_t bl
)
647 switch (PAIR (al
, bl
))
649 case PAIR (ONLY_REAL
, ONLY_REAL
):
650 rr
= gimplify_build2 (bsi
, code
, inner_type
, ar
, br
);
654 case PAIR (ONLY_REAL
, ONLY_IMAG
):
656 if (code
== MINUS_EXPR
)
657 ri
= gimplify_build2 (bsi
, MINUS_EXPR
, inner_type
, ai
, bi
);
662 case PAIR (ONLY_IMAG
, ONLY_REAL
):
663 if (code
== MINUS_EXPR
)
664 rr
= gimplify_build2 (bsi
, MINUS_EXPR
, inner_type
, ar
, br
);
670 case PAIR (ONLY_IMAG
, ONLY_IMAG
):
672 ri
= gimplify_build2 (bsi
, code
, inner_type
, ai
, bi
);
675 case PAIR (VARYING
, ONLY_REAL
):
676 rr
= gimplify_build2 (bsi
, code
, inner_type
, ar
, br
);
680 case PAIR (VARYING
, ONLY_IMAG
):
682 ri
= gimplify_build2 (bsi
, MINUS_EXPR
, inner_type
, ai
, bi
);
685 case PAIR (ONLY_REAL
, VARYING
):
686 if (code
== MINUS_EXPR
)
688 rr
= gimplify_build2 (bsi
, code
, inner_type
, ar
, br
);
692 case PAIR (ONLY_IMAG
, VARYING
):
693 if (code
== MINUS_EXPR
)
696 ri
= gimplify_build2 (bsi
, MINUS_EXPR
, inner_type
, ai
, bi
);
699 case PAIR (VARYING
, VARYING
):
701 rr
= gimplify_build2 (bsi
, code
, inner_type
, ar
, br
);
702 ri
= gimplify_build2 (bsi
, code
, inner_type
, ai
, bi
);
709 update_complex_assignment (bsi
, rr
, ri
);
712 /* Expand a complex multiplication or division to a libcall to the c99
713 compliant routines. */
716 expand_complex_libcall (block_stmt_iterator
*bsi
, tree ar
, tree ai
,
717 tree br
, tree bi
, enum tree_code code
)
719 enum machine_mode mode
;
720 enum built_in_function bcode
;
721 tree args
, fn
, stmt
, type
;
723 args
= tree_cons (NULL
, bi
, NULL
);
724 args
= tree_cons (NULL
, br
, args
);
725 args
= tree_cons (NULL
, ai
, args
);
726 args
= tree_cons (NULL
, ar
, args
);
728 stmt
= bsi_stmt (*bsi
);
729 type
= TREE_TYPE (TREE_OPERAND (stmt
, 1));
731 mode
= TYPE_MODE (type
);
732 gcc_assert (GET_MODE_CLASS (mode
) == MODE_COMPLEX_FLOAT
);
733 if (code
== MULT_EXPR
)
734 bcode
= BUILT_IN_COMPLEX_MUL_MIN
+ mode
- MIN_MODE_COMPLEX_FLOAT
;
735 else if (code
== RDIV_EXPR
)
736 bcode
= BUILT_IN_COMPLEX_DIV_MIN
+ mode
- MIN_MODE_COMPLEX_FLOAT
;
739 fn
= built_in_decls
[bcode
];
741 TREE_OPERAND (stmt
, 1)
742 = build3 (CALL_EXPR
, type
, build_fold_addr_expr (fn
), args
, NULL
);
747 tree lhs
= TREE_OPERAND (stmt
, 0);
748 update_complex_components (bsi
, stmt
,
749 build1 (REALPART_EXPR
, type
, lhs
),
750 build1 (IMAGPART_EXPR
, type
, lhs
));
754 /* Expand complex multiplication to scalars:
755 a * b = (ar*br - ai*bi) + i(ar*bi + br*ai)
759 expand_complex_multiplication (block_stmt_iterator
*bsi
, tree inner_type
,
760 tree ar
, tree ai
, tree br
, tree bi
,
761 complex_lattice_t al
, complex_lattice_t bl
)
767 complex_lattice_t tl
;
768 rr
= ar
, ar
= br
, br
= rr
;
769 ri
= ai
, ai
= bi
, bi
= ri
;
770 tl
= al
, al
= bl
, bl
= tl
;
773 switch (PAIR (al
, bl
))
775 case PAIR (ONLY_REAL
, ONLY_REAL
):
776 rr
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ar
, br
);
780 case PAIR (ONLY_IMAG
, ONLY_REAL
):
782 if (TREE_CODE (ai
) == REAL_CST
783 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (ai
), dconst1
))
786 ri
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ai
, br
);
789 case PAIR (ONLY_IMAG
, ONLY_IMAG
):
790 rr
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ai
, bi
);
791 rr
= gimplify_build1 (bsi
, NEGATE_EXPR
, inner_type
, rr
);
795 case PAIR (VARYING
, ONLY_REAL
):
796 rr
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ar
, br
);
797 ri
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ai
, br
);
800 case PAIR (VARYING
, ONLY_IMAG
):
801 rr
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ai
, bi
);
802 rr
= gimplify_build1 (bsi
, NEGATE_EXPR
, inner_type
, rr
);
803 ri
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ar
, bi
);
806 case PAIR (VARYING
, VARYING
):
807 if (flag_complex_method
== 2 && SCALAR_FLOAT_TYPE_P (inner_type
))
809 expand_complex_libcall (bsi
, ar
, ai
, br
, bi
, MULT_EXPR
);
816 t1
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ar
, br
);
817 t2
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ai
, bi
);
818 t3
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ar
, bi
);
820 /* Avoid expanding redundant multiplication for the common
821 case of squaring a complex number. */
822 if (ar
== br
&& ai
== bi
)
825 t4
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ai
, br
);
827 rr
= gimplify_build2 (bsi
, MINUS_EXPR
, inner_type
, t1
, t2
);
828 ri
= gimplify_build2 (bsi
, PLUS_EXPR
, inner_type
, t3
, t4
);
836 update_complex_assignment (bsi
, rr
, ri
);
839 /* Expand complex division to scalars, straightforward algorithm.
840 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
845 expand_complex_div_straight (block_stmt_iterator
*bsi
, tree inner_type
,
846 tree ar
, tree ai
, tree br
, tree bi
,
849 tree rr
, ri
, div
, t1
, t2
, t3
;
851 t1
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, br
, br
);
852 t2
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, bi
, bi
);
853 div
= gimplify_build2 (bsi
, PLUS_EXPR
, inner_type
, t1
, t2
);
855 t1
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ar
, br
);
856 t2
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ai
, bi
);
857 t3
= gimplify_build2 (bsi
, PLUS_EXPR
, inner_type
, t1
, t2
);
858 rr
= gimplify_build2 (bsi
, code
, inner_type
, t3
, div
);
860 t1
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ai
, br
);
861 t2
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ar
, bi
);
862 t3
= gimplify_build2 (bsi
, MINUS_EXPR
, inner_type
, t1
, t2
);
863 ri
= gimplify_build2 (bsi
, code
, inner_type
, t3
, div
);
865 update_complex_assignment (bsi
, rr
, ri
);
868 /* Expand complex division to scalars, modified algorithm to minimize
869 overflow with wide input ranges. */
872 expand_complex_div_wide (block_stmt_iterator
*bsi
, tree inner_type
,
873 tree ar
, tree ai
, tree br
, tree bi
,
876 tree rr
, ri
, ratio
, div
, t1
, t2
, tr
, ti
, cond
;
877 basic_block bb_cond
, bb_true
, bb_false
, bb_join
;
879 /* Examine |br| < |bi|, and branch. */
880 t1
= gimplify_build1 (bsi
, ABS_EXPR
, inner_type
, br
);
881 t2
= gimplify_build1 (bsi
, ABS_EXPR
, inner_type
, bi
);
882 cond
= fold (build (LT_EXPR
, boolean_type_node
, t1
, t2
));
885 bb_cond
= bb_true
= bb_false
= bb_join
= NULL
;
886 rr
= ri
= tr
= ti
= NULL
;
887 if (!TREE_CONSTANT (cond
))
891 cond
= build (COND_EXPR
, void_type_node
, cond
, NULL
, NULL
);
892 bsi_insert_before (bsi
, cond
, BSI_SAME_STMT
);
894 /* Split the original block, and create the TRUE and FALSE blocks. */
895 e
= split_block (bsi
->bb
, cond
);
898 bb_true
= create_empty_bb (bb_cond
);
899 bb_false
= create_empty_bb (bb_true
);
901 t1
= build (GOTO_EXPR
, void_type_node
, tree_block_label (bb_true
));
902 t2
= build (GOTO_EXPR
, void_type_node
, tree_block_label (bb_false
));
903 COND_EXPR_THEN (cond
) = t1
;
904 COND_EXPR_ELSE (cond
) = t2
;
906 /* Wire the blocks together. */
907 e
->flags
= EDGE_TRUE_VALUE
;
908 redirect_edge_succ (e
, bb_true
);
909 make_edge (bb_cond
, bb_false
, EDGE_FALSE_VALUE
);
910 make_edge (bb_true
, bb_join
, EDGE_FALLTHRU
);
911 make_edge (bb_false
, bb_join
, EDGE_FALLTHRU
);
913 /* Update dominance info. Note that bb_join's data was
914 updated by split_block. */
915 if (dom_info_available_p (CDI_DOMINATORS
))
917 set_immediate_dominator (CDI_DOMINATORS
, bb_true
, bb_cond
);
918 set_immediate_dominator (CDI_DOMINATORS
, bb_false
, bb_cond
);
921 rr
= make_rename_temp (inner_type
, NULL
);
922 ri
= make_rename_temp (inner_type
, NULL
);
925 /* In the TRUE branch, we compute
927 div = (br * ratio) + bi;
928 tr = (ar * ratio) + ai;
929 ti = (ai * ratio) - ar;
932 if (bb_true
|| integer_nonzerop (cond
))
936 *bsi
= bsi_last (bb_true
);
937 bsi_insert_after (bsi
, build_empty_stmt (), BSI_NEW_STMT
);
940 ratio
= gimplify_build2 (bsi
, code
, inner_type
, br
, bi
);
942 t1
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, br
, ratio
);
943 div
= gimplify_build2 (bsi
, PLUS_EXPR
, inner_type
, t1
, bi
);
945 t1
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ar
, ratio
);
946 tr
= gimplify_build2 (bsi
, PLUS_EXPR
, inner_type
, t1
, ai
);
948 t1
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ai
, ratio
);
949 ti
= gimplify_build2 (bsi
, MINUS_EXPR
, inner_type
, t1
, ar
);
951 tr
= gimplify_build2 (bsi
, code
, inner_type
, tr
, div
);
952 ti
= gimplify_build2 (bsi
, code
, inner_type
, ti
, div
);
956 t1
= build (MODIFY_EXPR
, inner_type
, rr
, tr
);
957 bsi_insert_before (bsi
, t1
, BSI_SAME_STMT
);
958 t1
= build (MODIFY_EXPR
, inner_type
, ri
, ti
);
959 bsi_insert_before (bsi
, t1
, BSI_SAME_STMT
);
964 /* In the FALSE branch, we compute
966 divisor = (d * ratio) + c;
967 tr = (b * ratio) + a;
968 ti = b - (a * ratio);
971 if (bb_false
|| integer_zerop (cond
))
975 *bsi
= bsi_last (bb_false
);
976 bsi_insert_after (bsi
, build_empty_stmt (), BSI_NEW_STMT
);
979 ratio
= gimplify_build2 (bsi
, code
, inner_type
, bi
, br
);
981 t1
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, bi
, ratio
);
982 div
= gimplify_build2 (bsi
, PLUS_EXPR
, inner_type
, t1
, br
);
984 t1
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ai
, ratio
);
985 tr
= gimplify_build2 (bsi
, PLUS_EXPR
, inner_type
, t1
, ar
);
987 t1
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ar
, ratio
);
988 ti
= gimplify_build2 (bsi
, MINUS_EXPR
, inner_type
, ai
, t1
);
990 tr
= gimplify_build2 (bsi
, code
, inner_type
, tr
, div
);
991 ti
= gimplify_build2 (bsi
, code
, inner_type
, ti
, div
);
995 t1
= build (MODIFY_EXPR
, inner_type
, rr
, tr
);
996 bsi_insert_before (bsi
, t1
, BSI_SAME_STMT
);
997 t1
= build (MODIFY_EXPR
, inner_type
, ri
, ti
);
998 bsi_insert_before (bsi
, t1
, BSI_SAME_STMT
);
1004 *bsi
= bsi_start (bb_join
);
1008 update_complex_assignment (bsi
, rr
, ri
);
1011 /* Expand complex division to scalars. */
1014 expand_complex_division (block_stmt_iterator
*bsi
, tree inner_type
,
1015 tree ar
, tree ai
, tree br
, tree bi
,
1016 enum tree_code code
,
1017 complex_lattice_t al
, complex_lattice_t bl
)
1021 switch (PAIR (al
, bl
))
1023 case PAIR (ONLY_REAL
, ONLY_REAL
):
1024 rr
= gimplify_build2 (bsi
, code
, inner_type
, ar
, br
);
1028 case PAIR (ONLY_REAL
, ONLY_IMAG
):
1030 ri
= gimplify_build2 (bsi
, code
, inner_type
, ar
, bi
);
1031 ri
= gimplify_build1 (bsi
, NEGATE_EXPR
, inner_type
, ri
);
1034 case PAIR (ONLY_IMAG
, ONLY_REAL
):
1036 ri
= gimplify_build2 (bsi
, code
, inner_type
, ai
, br
);
1039 case PAIR (ONLY_IMAG
, ONLY_IMAG
):
1040 rr
= gimplify_build2 (bsi
, code
, inner_type
, ai
, bi
);
1044 case PAIR (VARYING
, ONLY_REAL
):
1045 rr
= gimplify_build2 (bsi
, code
, inner_type
, ar
, br
);
1046 ri
= gimplify_build2 (bsi
, code
, inner_type
, ai
, br
);
1049 case PAIR (VARYING
, ONLY_IMAG
):
1050 rr
= gimplify_build2 (bsi
, code
, inner_type
, ai
, bi
);
1051 ri
= gimplify_build2 (bsi
, code
, inner_type
, ar
, bi
);
1052 ri
= gimplify_build1 (bsi
, NEGATE_EXPR
, inner_type
, ri
);
1054 case PAIR (ONLY_REAL
, VARYING
):
1055 case PAIR (ONLY_IMAG
, VARYING
):
1056 case PAIR (VARYING
, VARYING
):
1057 switch (flag_complex_method
)
1060 /* straightforward implementation of complex divide acceptable. */
1061 expand_complex_div_straight (bsi
, inner_type
, ar
, ai
, br
, bi
, code
);
1065 if (SCALAR_FLOAT_TYPE_P (inner_type
))
1067 expand_complex_libcall (bsi
, ar
, ai
, br
, bi
, code
);
1073 /* wide ranges of inputs must work for complex divide. */
1074 expand_complex_div_wide (bsi
, inner_type
, ar
, ai
, br
, bi
, code
);
1086 update_complex_assignment (bsi
, rr
, ri
);
1089 /* Expand complex negation to scalars:
1094 expand_complex_negation (block_stmt_iterator
*bsi
, tree inner_type
,
1099 rr
= gimplify_build1 (bsi
, NEGATE_EXPR
, inner_type
, ar
);
1100 ri
= gimplify_build1 (bsi
, NEGATE_EXPR
, inner_type
, ai
);
1102 update_complex_assignment (bsi
, rr
, ri
);
1105 /* Expand complex conjugate to scalars:
1110 expand_complex_conjugate (block_stmt_iterator
*bsi
, tree inner_type
,
1115 ri
= gimplify_build1 (bsi
, NEGATE_EXPR
, inner_type
, ai
);
1117 update_complex_assignment (bsi
, ar
, ri
);
1120 /* Expand complex comparison (EQ or NE only). */
1123 expand_complex_comparison (block_stmt_iterator
*bsi
, tree ar
, tree ai
,
1124 tree br
, tree bi
, enum tree_code code
)
1126 tree cr
, ci
, cc
, stmt
, expr
, type
;
1128 cr
= gimplify_build2 (bsi
, code
, boolean_type_node
, ar
, br
);
1129 ci
= gimplify_build2 (bsi
, code
, boolean_type_node
, ai
, bi
);
1130 cc
= gimplify_build2 (bsi
,
1131 (code
== EQ_EXPR
? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
),
1132 boolean_type_node
, cr
, ci
);
1134 stmt
= expr
= bsi_stmt (*bsi
);
1136 switch (TREE_CODE (stmt
))
1139 expr
= TREE_OPERAND (stmt
, 0);
1142 type
= TREE_TYPE (TREE_OPERAND (expr
, 1));
1143 TREE_OPERAND (expr
, 1) = fold_convert (type
, cc
);
1146 TREE_OPERAND (stmt
, 0) = cc
;
1155 /* Process one statement. If we identify a complex operation, expand it. */
1158 expand_complex_operations_1 (block_stmt_iterator
*bsi
)
1160 tree stmt
= bsi_stmt (*bsi
);
1161 tree rhs
, type
, inner_type
;
1162 tree ac
, ar
, ai
, bc
, br
, bi
;
1163 complex_lattice_t al
, bl
;
1164 enum tree_code code
;
1166 switch (TREE_CODE (stmt
))
1169 stmt
= TREE_OPERAND (stmt
, 0);
1172 if (TREE_CODE (stmt
) != MODIFY_EXPR
)
1177 rhs
= TREE_OPERAND (stmt
, 1);
1181 rhs
= TREE_OPERAND (stmt
, 0);
1188 type
= TREE_TYPE (rhs
);
1189 code
= TREE_CODE (rhs
);
1191 /* Initial filter for operations we handle. */
1197 case TRUNC_DIV_EXPR
:
1199 case FLOOR_DIV_EXPR
:
1200 case ROUND_DIV_EXPR
:
1204 if (TREE_CODE (type
) != COMPLEX_TYPE
)
1206 inner_type
= TREE_TYPE (type
);
1211 inner_type
= TREE_TYPE (TREE_OPERAND (rhs
, 1));
1212 if (TREE_CODE (inner_type
) != COMPLEX_TYPE
)
1218 tree lhs
= TREE_OPERAND (stmt
, 0);
1219 tree rhs
= TREE_OPERAND (stmt
, 1);
1221 if (TREE_CODE (type
) == COMPLEX_TYPE
)
1222 expand_complex_move (bsi
, stmt
, type
, lhs
, rhs
);
1223 else if ((TREE_CODE (rhs
) == REALPART_EXPR
1224 || TREE_CODE (rhs
) == IMAGPART_EXPR
)
1225 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
)
1227 TREE_OPERAND (stmt
, 1)
1228 = extract_component (bsi
, TREE_OPERAND (rhs
, 0),
1229 TREE_CODE (rhs
) == IMAGPART_EXPR
, false);
1236 /* Extract the components of the two complex values. Make sure and
1237 handle the common case of the same value used twice specially. */
1238 ac
= TREE_OPERAND (rhs
, 0);
1239 ar
= extract_component (bsi
, ac
, 0, true);
1240 ai
= extract_component (bsi
, ac
, 1, true);
1242 if (TREE_CODE_CLASS (code
) == tcc_unary
)
1243 bc
= br
= bi
= NULL
;
1246 bc
= TREE_OPERAND (rhs
, 1);
1251 br
= extract_component (bsi
, bc
, 0, true);
1252 bi
= extract_component (bsi
, bc
, 1, true);
1258 al
= find_lattice_value (ac
);
1259 if (al
== UNINITIALIZED
)
1262 if (TREE_CODE_CLASS (code
) == tcc_unary
)
1268 bl
= find_lattice_value (bc
);
1269 if (bl
== UNINITIALIZED
)
1280 expand_complex_addition (bsi
, inner_type
, ar
, ai
, br
, bi
, code
, al
, bl
);
1284 expand_complex_multiplication (bsi
, inner_type
, ar
, ai
, br
, bi
, al
, bl
);
1287 case TRUNC_DIV_EXPR
:
1289 case FLOOR_DIV_EXPR
:
1290 case ROUND_DIV_EXPR
:
1292 expand_complex_division (bsi
, inner_type
, ar
, ai
, br
, bi
, code
, al
, bl
);
1296 expand_complex_negation (bsi
, inner_type
, ar
, ai
);
1300 expand_complex_conjugate (bsi
, inner_type
, ar
, ai
);
1305 expand_complex_comparison (bsi
, ar
, ai
, br
, bi
, code
);
1314 /* Entry point for complex operation lowering during optimization. */
1317 tree_lower_complex (void)
1319 int old_last_basic_block
;
1320 block_stmt_iterator bsi
;
1323 if (!init_dont_simulate_again ())
1326 complex_lattice_values
= VEC_alloc (complex_lattice_t
, heap
, num_ssa_names
);
1327 VEC_safe_grow (complex_lattice_t
, heap
,
1328 complex_lattice_values
, num_ssa_names
);
1329 memset (VEC_address (complex_lattice_t
, complex_lattice_values
), 0,
1330 num_ssa_names
* sizeof(complex_lattice_t
));
1331 init_parameter_lattice_values ();
1333 ssa_propagate (complex_visit_stmt
, complex_visit_phi
);
1335 create_components ();
1336 update_parameter_components ();
1338 old_last_basic_block
= last_basic_block
;
1341 if (bb
->index
>= old_last_basic_block
)
1343 update_phi_components (bb
);
1344 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
1345 expand_complex_operations_1 (&bsi
);
1348 bsi_commit_edge_inserts ();
1350 VEC_free (tree
, heap
, complex_variable_components
);
1351 VEC_free (complex_lattice_t
, heap
, complex_lattice_values
);
1354 struct tree_opt_pass pass_lower_complex
=
1356 "cplxlower", /* name */
1358 tree_lower_complex
, /* execute */
1361 0, /* static_pass_number */
1363 PROP_ssa
, /* properties_required */
1364 0, /* properties_provided */
1365 0, /* properties_destroyed */
1366 0, /* todo_flags_start */
1367 TODO_dump_func
| TODO_ggc_collect
1369 | TODO_verify_stmts
, /* todo_flags_finish */
1374 /* Entry point for complex operation lowering without optimization. */
1377 tree_lower_complex_O0 (void)
1379 int old_last_basic_block
= last_basic_block
;
1380 block_stmt_iterator bsi
;
1385 if (bb
->index
>= old_last_basic_block
)
1387 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
1388 expand_complex_operations_1 (&bsi
);
1393 gate_no_optimization (void)
1395 return optimize
== 0;
1398 struct tree_opt_pass pass_lower_complex_O0
=
1400 "cplxlower0", /* name */
1401 gate_no_optimization
, /* gate */
1402 tree_lower_complex_O0
, /* execute */
1405 0, /* static_pass_number */
1407 PROP_cfg
, /* properties_required */
1408 0, /* properties_provided */
1409 0, /* properties_destroyed */
1410 0, /* todo_flags_start */
1411 TODO_dump_func
| TODO_ggc_collect
1412 | TODO_verify_stmts
, /* todo_flags_finish */