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_op
= 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 orig_stmt
, stmt
, rhs
= NULL
;
165 orig_stmt
= stmt
= bsi_stmt (bsi
);
166 switch (TREE_CODE (stmt
))
169 stmt
= TREE_OPERAND (stmt
, 0);
170 if (!stmt
|| TREE_CODE (stmt
) != MODIFY_EXPR
)
175 dsa
= !is_complex_reg (TREE_OPERAND (stmt
, 0));
176 rhs
= TREE_OPERAND (stmt
, 1);
180 rhs
= TREE_OPERAND (stmt
, 0);
188 switch (TREE_CODE (rhs
))
192 rhs
= TREE_OPERAND (rhs
, 0);
205 if (TREE_CODE (TREE_TYPE (rhs
)) == COMPLEX_TYPE
)
206 saw_a_complex_op
= true;
213 DONT_SIMULATE_AGAIN (orig_stmt
) = dsa
;
217 return saw_a_complex_op
;
221 /* Evaluate statement STMT against the complex lattice defined above. */
223 static enum ssa_prop_result
224 complex_visit_stmt (tree stmt
, edge
*taken_edge_p ATTRIBUTE_UNUSED
,
227 complex_lattice_t new_l
, old_l
, op1_l
, op2_l
;
231 /* These conditions should be satisfied due to the initial filter
232 set up in init_dont_simulate_again. */
233 if (TREE_CODE (stmt
) == RETURN_EXPR
)
234 stmt
= TREE_OPERAND (stmt
, 0);
235 gcc_assert (TREE_CODE (stmt
) == MODIFY_EXPR
);
237 lhs
= TREE_OPERAND (stmt
, 0);
238 rhs
= TREE_OPERAND (stmt
, 1);
240 gcc_assert (TREE_CODE (lhs
) == SSA_NAME
);
241 gcc_assert (TREE_CODE (TREE_TYPE (lhs
)) == COMPLEX_TYPE
);
244 ver
= SSA_NAME_VERSION (lhs
);
245 old_l
= VEC_index (complex_lattice_t
, complex_lattice_values
, ver
);
247 switch (TREE_CODE (rhs
))
252 new_l
= find_lattice_value (rhs
);
257 op1_l
= find_lattice_value (TREE_OPERAND (rhs
, 0));
258 op2_l
= find_lattice_value (TREE_OPERAND (rhs
, 1));
260 /* We've set up the lattice values such that IOR neatly
262 new_l
= op1_l
| op2_l
;
271 op1_l
= find_lattice_value (TREE_OPERAND (rhs
, 0));
272 op2_l
= find_lattice_value (TREE_OPERAND (rhs
, 1));
274 /* Obviously, if either varies, so does the result. */
275 if (op1_l
== VARYING
|| op2_l
== VARYING
)
277 /* Don't prematurely promote variables if we've not yet seen
279 else if (op1_l
== UNINITIALIZED
)
281 else if (op2_l
== UNINITIALIZED
)
285 /* At this point both numbers have only one component. If the
286 numbers are of opposite kind, the result is imaginary,
287 otherwise the result is real. The add/subtract translates
288 the real/imag from/to 0/1; the ^ performs the comparison. */
289 new_l
= ((op1_l
- ONLY_REAL
) ^ (op2_l
- ONLY_REAL
)) + ONLY_REAL
;
291 /* Don't allow the lattice value to flip-flop indefinitely. */
298 new_l
= find_lattice_value (TREE_OPERAND (rhs
, 0));
306 /* If nothing changed this round, let the propagator know. */
308 return SSA_PROP_NOT_INTERESTING
;
310 VEC_replace (complex_lattice_t
, complex_lattice_values
, ver
, new_l
);
311 return new_l
== VARYING
? SSA_PROP_VARYING
: SSA_PROP_INTERESTING
;
314 /* Evaluate a PHI node against the complex lattice defined above. */
316 static enum ssa_prop_result
317 complex_visit_phi (tree phi
)
319 complex_lattice_t new_l
, old_l
;
324 lhs
= PHI_RESULT (phi
);
326 /* This condition should be satisfied due to the initial filter
327 set up in init_dont_simulate_again. */
328 gcc_assert (TREE_CODE (TREE_TYPE (lhs
)) == COMPLEX_TYPE
);
330 /* We've set up the lattice values such that IOR neatly models PHI meet. */
331 new_l
= UNINITIALIZED
;
332 for (i
= PHI_NUM_ARGS (phi
) - 1; i
>= 0; --i
)
333 new_l
|= find_lattice_value (PHI_ARG_DEF (phi
, i
));
335 ver
= SSA_NAME_VERSION (lhs
);
336 old_l
= VEC_index (complex_lattice_t
, complex_lattice_values
, ver
);
339 return SSA_PROP_NOT_INTERESTING
;
341 VEC_replace (complex_lattice_t
, complex_lattice_values
, ver
, new_l
);
342 return new_l
== VARYING
? SSA_PROP_VARYING
: SSA_PROP_INTERESTING
;
345 /* For each referenced complex gimple register, set up a pair of registers
346 to hold the components of the complex value. */
349 create_components (void)
353 n
= num_referenced_vars
;
357 complex_variable_components
= VEC_alloc (tree
, heap
, 2*n
);
358 VEC_safe_grow (tree
, heap
, complex_variable_components
, 2*n
);
360 for (k
= 0; k
< n
; ++k
)
362 tree var
= referenced_var (k
);
363 tree r
= NULL
, i
= NULL
;
366 && TREE_CODE (TREE_TYPE (var
)) == COMPLEX_TYPE
367 && is_gimple_reg (var
))
369 tree inner_type
= TREE_TYPE (TREE_TYPE (var
));
371 r
= make_rename_temp (inner_type
, "CR");
372 i
= make_rename_temp (inner_type
, "CI");
373 DECL_SOURCE_LOCATION (r
) = DECL_SOURCE_LOCATION (var
);
374 DECL_SOURCE_LOCATION (i
) = DECL_SOURCE_LOCATION (var
);
375 DECL_ARTIFICIAL (r
) = 1;
376 DECL_ARTIFICIAL (i
) = 1;
378 if (DECL_NAME (var
) && !DECL_IGNORED_P (var
))
380 const char *name
= IDENTIFIER_POINTER (DECL_NAME (var
));
382 DECL_NAME (r
) = get_identifier (ACONCAT ((name
, "$real", NULL
)));
383 DECL_NAME (i
) = get_identifier (ACONCAT ((name
, "$imag", NULL
)));
385 SET_DECL_DEBUG_EXPR (r
, build1 (REALPART_EXPR
, inner_type
, var
));
386 SET_DECL_DEBUG_EXPR (i
, build1 (IMAGPART_EXPR
, inner_type
, var
));
387 DECL_DEBUG_EXPR_IS_FROM (r
) = 1;
388 DECL_DEBUG_EXPR_IS_FROM (i
) = 1;
390 DECL_IGNORED_P (r
) = 0;
391 DECL_IGNORED_P (i
) = 0;
393 TREE_NO_WARNING (r
) = TREE_NO_WARNING (var
);
394 TREE_NO_WARNING (i
) = TREE_NO_WARNING (var
);
398 DECL_IGNORED_P (r
) = 1;
399 DECL_IGNORED_P (i
) = 1;
400 TREE_NO_WARNING (r
) = 1;
401 TREE_NO_WARNING (i
) = 1;
405 VEC_replace (tree
, complex_variable_components
, 2*k
, r
);
406 VEC_replace (tree
, complex_variable_components
, 2*k
+ 1, i
);
410 /* Extract the real or imaginary part of a complex variable or constant.
411 Make sure that it's a proper gimple_val and gimplify it if not.
412 Emit any new code before BSI. */
415 extract_component (block_stmt_iterator
*bsi
, tree t
, bool imagpart_p
,
418 switch (TREE_CODE (t
))
421 return imagpart_p
? TREE_IMAGPART (t
) : TREE_REALPART (t
);
424 return TREE_OPERAND (t
, imagpart_p
);
432 tree inner_type
= TREE_TYPE (TREE_TYPE (t
));
434 t
= build1 ((imagpart_p
? IMAGPART_EXPR
: REALPART_EXPR
),
435 inner_type
, unshare_expr (t
));
438 t
= gimplify_val (bsi
, inner_type
, t
);
445 tree def
= SSA_NAME_DEF_STMT (t
);
447 if (TREE_CODE (def
) == MODIFY_EXPR
)
449 def
= TREE_OPERAND (def
, 1);
450 if (TREE_CODE (def
) == COMPLEX_CST
)
451 return imagpart_p
? TREE_IMAGPART (def
) : TREE_REALPART (def
);
452 if (TREE_CODE (def
) == COMPLEX_EXPR
)
454 def
= TREE_OPERAND (def
, imagpart_p
);
455 if (TREE_CONSTANT (def
))
460 return VEC_index (tree
, complex_variable_components
,
461 var_ann (SSA_NAME_VAR (t
))->uid
* 2 + imagpart_p
);
469 /* Update the complex components of the ssa name on the lhs of STMT. */
472 update_complex_components (block_stmt_iterator
*bsi
, tree stmt
, tree r
, tree i
)
474 unsigned int uid
= var_ann (SSA_NAME_VAR (TREE_OPERAND (stmt
, 0)))->uid
;
477 v
= VEC_index (tree
, complex_variable_components
, 2*uid
);
478 x
= build2 (MODIFY_EXPR
, TREE_TYPE (v
), v
, r
);
479 SET_EXPR_LOCUS (x
, EXPR_LOCUS (stmt
));
480 TREE_BLOCK (x
) = TREE_BLOCK (stmt
);
481 bsi_insert_after (bsi
, x
, BSI_NEW_STMT
);
483 v
= VEC_index (tree
, complex_variable_components
, 2*uid
+ 1);
484 x
= build2 (MODIFY_EXPR
, TREE_TYPE (v
), v
, i
);
485 SET_EXPR_LOCUS (x
, EXPR_LOCUS (stmt
));
486 TREE_BLOCK (x
) = TREE_BLOCK (stmt
);
487 bsi_insert_after (bsi
, x
, BSI_NEW_STMT
);
491 update_complex_components_on_edge (edge e
, tree stmt
, tree lhs
, tree r
, tree i
)
493 unsigned int uid
= var_ann (SSA_NAME_VAR (lhs
))->uid
;
496 v
= VEC_index (tree
, complex_variable_components
, 2*uid
);
497 x
= build2 (MODIFY_EXPR
, TREE_TYPE (v
), v
, r
);
500 SET_EXPR_LOCUS (x
, EXPR_LOCUS (stmt
));
501 TREE_BLOCK (x
) = TREE_BLOCK (stmt
);
503 bsi_insert_on_edge (e
, x
);
505 v
= VEC_index (tree
, complex_variable_components
, 2*uid
+ 1);
506 x
= build2 (MODIFY_EXPR
, TREE_TYPE (v
), v
, i
);
509 SET_EXPR_LOCUS (x
, EXPR_LOCUS (stmt
));
510 TREE_BLOCK (x
) = TREE_BLOCK (stmt
);
512 bsi_insert_on_edge (e
, x
);
515 /* Update an assignment to a complex variable in place. */
518 update_complex_assignment (block_stmt_iterator
*bsi
, tree r
, tree i
)
523 mod
= stmt
= bsi_stmt (*bsi
);
524 if (TREE_CODE (stmt
) == RETURN_EXPR
)
525 mod
= TREE_OPERAND (mod
, 0);
527 update_complex_components (bsi
, stmt
, r
, i
);
529 type
= TREE_TYPE (TREE_OPERAND (mod
, 1));
530 TREE_OPERAND (mod
, 1) = build (COMPLEX_EXPR
, type
, r
, i
);
534 /* Generate code at the entry point of the function to initialize the
535 component variables for a complex parameter. */
538 update_parameter_components (void)
540 edge entry_edge
= single_succ_edge (ENTRY_BLOCK_PTR
);
543 for (parm
= DECL_ARGUMENTS (cfun
->decl
); parm
; parm
= TREE_CHAIN (parm
))
545 tree type
= TREE_TYPE (parm
);
548 if (TREE_CODE (type
) != COMPLEX_TYPE
|| !is_gimple_reg (parm
))
551 type
= TREE_TYPE (type
);
552 ssa_name
= default_def (parm
);
554 r
= build1 (REALPART_EXPR
, type
, ssa_name
);
555 i
= build1 (IMAGPART_EXPR
, type
, ssa_name
);
556 update_complex_components_on_edge (entry_edge
, NULL
, ssa_name
, r
, i
);
560 /* Generate code to set the component variables of a complex variable
561 to match the PHI statements in block BB. */
564 update_phi_components (basic_block bb
)
568 for (phi
= phi_nodes (bb
); phi
; phi
= PHI_CHAIN (phi
))
569 if (is_complex_reg (PHI_RESULT (phi
)))
572 tree lhs
= PHI_RESULT (phi
);
574 for (i
= 0, n
= PHI_NUM_ARGS (phi
); i
< n
; ++i
)
576 edge e
= PHI_ARG_EDGE (phi
, i
);
577 tree arg
= PHI_ARG_DEF (phi
, i
);
580 /* Avoid no-op assignments. This also prevents insertting stmts
581 onto abnormal edges, assuming the PHI isn't already broken. */
582 if (TREE_CODE (arg
) == SSA_NAME
583 && SSA_NAME_VAR (arg
) == SSA_NAME_VAR (lhs
))
586 r
= extract_component (NULL
, arg
, 0, false);
587 i
= extract_component (NULL
, arg
, 1, false);
588 update_complex_components_on_edge (e
, NULL
, lhs
, r
, i
);
593 /* Mark each virtual op in STMT for ssa update. */
596 update_all_vops (tree stmt
)
601 FOR_EACH_SSA_TREE_OPERAND (sym
, stmt
, iter
, SSA_OP_ALL_VIRTUALS
)
603 if (TREE_CODE (sym
) == SSA_NAME
)
604 sym
= SSA_NAME_VAR (sym
);
605 mark_sym_for_renaming (sym
);
609 /* Expand a complex move to scalars. */
612 expand_complex_move (block_stmt_iterator
*bsi
, tree stmt
, tree type
,
615 tree inner_type
= TREE_TYPE (type
);
618 if (TREE_CODE (lhs
) == SSA_NAME
)
620 if (is_ctrl_altering_stmt (bsi_stmt (*bsi
)))
625 /* The value is not assigned on the exception edges, so we need not
626 concern ourselves there. We do need to update on the fallthru
628 FOR_EACH_EDGE (e
, ei
, bsi
->bb
->succs
)
629 if (e
->flags
& EDGE_FALLTHRU
)
634 r
= build1 (REALPART_EXPR
, inner_type
, lhs
);
635 i
= build1 (IMAGPART_EXPR
, inner_type
, lhs
);
636 update_complex_components_on_edge (e
, stmt
, lhs
, r
, i
);
638 else if (TREE_CODE (rhs
) == CALL_EXPR
|| TREE_SIDE_EFFECTS (rhs
))
640 r
= build1 (REALPART_EXPR
, inner_type
, lhs
);
641 i
= build1 (IMAGPART_EXPR
, inner_type
, lhs
);
642 update_complex_components (bsi
, stmt
, r
, i
);
646 update_all_vops (bsi_stmt (*bsi
));
647 r
= extract_component (bsi
, rhs
, 0, true);
648 i
= extract_component (bsi
, rhs
, 1, true);
649 update_complex_assignment (bsi
, r
, i
);
652 else if (TREE_CODE (rhs
) == SSA_NAME
&& !TREE_SIDE_EFFECTS (lhs
))
656 r
= extract_component (bsi
, rhs
, 0, false);
657 i
= extract_component (bsi
, rhs
, 1, false);
659 x
= build1 (REALPART_EXPR
, inner_type
, unshare_expr (lhs
));
660 x
= build2 (MODIFY_EXPR
, inner_type
, x
, r
);
661 bsi_insert_before (bsi
, x
, BSI_SAME_STMT
);
663 if (stmt
== bsi_stmt (*bsi
))
665 x
= build1 (IMAGPART_EXPR
, inner_type
, unshare_expr (lhs
));
666 TREE_OPERAND (stmt
, 0) = x
;
667 TREE_OPERAND (stmt
, 1) = i
;
668 TREE_TYPE (stmt
) = inner_type
;
672 x
= build1 (IMAGPART_EXPR
, inner_type
, unshare_expr (lhs
));
673 x
= build2 (MODIFY_EXPR
, inner_type
, x
, i
);
674 bsi_insert_before (bsi
, x
, BSI_SAME_STMT
);
676 stmt
= bsi_stmt (*bsi
);
677 gcc_assert (TREE_CODE (stmt
) == RETURN_EXPR
);
678 TREE_OPERAND (stmt
, 0) = lhs
;
681 update_all_vops (stmt
);
686 /* Expand complex addition to scalars:
687 a + b = (ar + br) + i(ai + bi)
688 a - b = (ar - br) + i(ai + bi)
692 expand_complex_addition (block_stmt_iterator
*bsi
, tree inner_type
,
693 tree ar
, tree ai
, tree br
, tree bi
,
695 complex_lattice_t al
, complex_lattice_t bl
)
699 switch (PAIR (al
, bl
))
701 case PAIR (ONLY_REAL
, ONLY_REAL
):
702 rr
= gimplify_build2 (bsi
, code
, inner_type
, ar
, br
);
706 case PAIR (ONLY_REAL
, ONLY_IMAG
):
708 if (code
== MINUS_EXPR
)
709 ri
= gimplify_build2 (bsi
, MINUS_EXPR
, inner_type
, ai
, bi
);
714 case PAIR (ONLY_IMAG
, ONLY_REAL
):
715 if (code
== MINUS_EXPR
)
716 rr
= gimplify_build2 (bsi
, MINUS_EXPR
, inner_type
, ar
, br
);
722 case PAIR (ONLY_IMAG
, ONLY_IMAG
):
724 ri
= gimplify_build2 (bsi
, code
, inner_type
, ai
, bi
);
727 case PAIR (VARYING
, ONLY_REAL
):
728 rr
= gimplify_build2 (bsi
, code
, inner_type
, ar
, br
);
732 case PAIR (VARYING
, ONLY_IMAG
):
734 ri
= gimplify_build2 (bsi
, MINUS_EXPR
, inner_type
, ai
, bi
);
737 case PAIR (ONLY_REAL
, VARYING
):
738 if (code
== MINUS_EXPR
)
740 rr
= gimplify_build2 (bsi
, code
, inner_type
, ar
, br
);
744 case PAIR (ONLY_IMAG
, VARYING
):
745 if (code
== MINUS_EXPR
)
748 ri
= gimplify_build2 (bsi
, MINUS_EXPR
, inner_type
, ai
, bi
);
751 case PAIR (VARYING
, VARYING
):
753 rr
= gimplify_build2 (bsi
, code
, inner_type
, ar
, br
);
754 ri
= gimplify_build2 (bsi
, code
, inner_type
, ai
, bi
);
761 update_complex_assignment (bsi
, rr
, ri
);
764 /* Expand a complex multiplication or division to a libcall to the c99
765 compliant routines. */
768 expand_complex_libcall (block_stmt_iterator
*bsi
, tree ar
, tree ai
,
769 tree br
, tree bi
, enum tree_code code
)
771 enum machine_mode mode
;
772 enum built_in_function bcode
;
773 tree args
, fn
, stmt
, type
;
775 args
= tree_cons (NULL
, bi
, NULL
);
776 args
= tree_cons (NULL
, br
, args
);
777 args
= tree_cons (NULL
, ai
, args
);
778 args
= tree_cons (NULL
, ar
, args
);
780 stmt
= bsi_stmt (*bsi
);
781 type
= TREE_TYPE (TREE_OPERAND (stmt
, 1));
783 mode
= TYPE_MODE (type
);
784 gcc_assert (GET_MODE_CLASS (mode
) == MODE_COMPLEX_FLOAT
);
785 if (code
== MULT_EXPR
)
786 bcode
= BUILT_IN_COMPLEX_MUL_MIN
+ mode
- MIN_MODE_COMPLEX_FLOAT
;
787 else if (code
== RDIV_EXPR
)
788 bcode
= BUILT_IN_COMPLEX_DIV_MIN
+ mode
- MIN_MODE_COMPLEX_FLOAT
;
791 fn
= built_in_decls
[bcode
];
793 TREE_OPERAND (stmt
, 1)
794 = build3 (CALL_EXPR
, type
, build_fold_addr_expr (fn
), args
, NULL
);
799 tree lhs
= TREE_OPERAND (stmt
, 0);
800 update_complex_components (bsi
, stmt
,
801 build1 (REALPART_EXPR
, type
, lhs
),
802 build1 (IMAGPART_EXPR
, type
, lhs
));
806 /* Expand complex multiplication to scalars:
807 a * b = (ar*br - ai*bi) + i(ar*bi + br*ai)
811 expand_complex_multiplication (block_stmt_iterator
*bsi
, tree inner_type
,
812 tree ar
, tree ai
, tree br
, tree bi
,
813 complex_lattice_t al
, complex_lattice_t bl
)
819 complex_lattice_t tl
;
820 rr
= ar
, ar
= br
, br
= rr
;
821 ri
= ai
, ai
= bi
, bi
= ri
;
822 tl
= al
, al
= bl
, bl
= tl
;
825 switch (PAIR (al
, bl
))
827 case PAIR (ONLY_REAL
, ONLY_REAL
):
828 rr
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ar
, br
);
832 case PAIR (ONLY_IMAG
, ONLY_REAL
):
834 if (TREE_CODE (ai
) == REAL_CST
835 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (ai
), dconst1
))
838 ri
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ai
, br
);
841 case PAIR (ONLY_IMAG
, ONLY_IMAG
):
842 rr
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ai
, bi
);
843 rr
= gimplify_build1 (bsi
, NEGATE_EXPR
, inner_type
, rr
);
847 case PAIR (VARYING
, ONLY_REAL
):
848 rr
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ar
, br
);
849 ri
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ai
, br
);
852 case PAIR (VARYING
, ONLY_IMAG
):
853 rr
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ai
, bi
);
854 rr
= gimplify_build1 (bsi
, NEGATE_EXPR
, inner_type
, rr
);
855 ri
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ar
, bi
);
858 case PAIR (VARYING
, VARYING
):
859 if (flag_complex_method
== 2 && SCALAR_FLOAT_TYPE_P (inner_type
))
861 expand_complex_libcall (bsi
, ar
, ai
, br
, bi
, MULT_EXPR
);
868 t1
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ar
, br
);
869 t2
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ai
, bi
);
870 t3
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ar
, bi
);
872 /* Avoid expanding redundant multiplication for the common
873 case of squaring a complex number. */
874 if (ar
== br
&& ai
== bi
)
877 t4
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ai
, br
);
879 rr
= gimplify_build2 (bsi
, MINUS_EXPR
, inner_type
, t1
, t2
);
880 ri
= gimplify_build2 (bsi
, PLUS_EXPR
, inner_type
, t3
, t4
);
888 update_complex_assignment (bsi
, rr
, ri
);
891 /* Expand complex division to scalars, straightforward algorithm.
892 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
897 expand_complex_div_straight (block_stmt_iterator
*bsi
, tree inner_type
,
898 tree ar
, tree ai
, tree br
, tree bi
,
901 tree rr
, ri
, div
, t1
, t2
, t3
;
903 t1
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, br
, br
);
904 t2
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, bi
, bi
);
905 div
= gimplify_build2 (bsi
, PLUS_EXPR
, inner_type
, t1
, t2
);
907 t1
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ar
, br
);
908 t2
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ai
, bi
);
909 t3
= gimplify_build2 (bsi
, PLUS_EXPR
, inner_type
, t1
, t2
);
910 rr
= gimplify_build2 (bsi
, code
, inner_type
, t3
, div
);
912 t1
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ai
, br
);
913 t2
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ar
, bi
);
914 t3
= gimplify_build2 (bsi
, MINUS_EXPR
, inner_type
, t1
, t2
);
915 ri
= gimplify_build2 (bsi
, code
, inner_type
, t3
, div
);
917 update_complex_assignment (bsi
, rr
, ri
);
920 /* Expand complex division to scalars, modified algorithm to minimize
921 overflow with wide input ranges. */
924 expand_complex_div_wide (block_stmt_iterator
*bsi
, tree inner_type
,
925 tree ar
, tree ai
, tree br
, tree bi
,
928 tree rr
, ri
, ratio
, div
, t1
, t2
, tr
, ti
, cond
;
929 basic_block bb_cond
, bb_true
, bb_false
, bb_join
;
931 /* Examine |br| < |bi|, and branch. */
932 t1
= gimplify_build1 (bsi
, ABS_EXPR
, inner_type
, br
);
933 t2
= gimplify_build1 (bsi
, ABS_EXPR
, inner_type
, bi
);
934 cond
= fold (build (LT_EXPR
, boolean_type_node
, t1
, t2
));
937 bb_cond
= bb_true
= bb_false
= bb_join
= NULL
;
938 rr
= ri
= tr
= ti
= NULL
;
939 if (!TREE_CONSTANT (cond
))
943 cond
= build (COND_EXPR
, void_type_node
, cond
, NULL
, NULL
);
944 bsi_insert_before (bsi
, cond
, BSI_SAME_STMT
);
946 /* Split the original block, and create the TRUE and FALSE blocks. */
947 e
= split_block (bsi
->bb
, cond
);
950 bb_true
= create_empty_bb (bb_cond
);
951 bb_false
= create_empty_bb (bb_true
);
953 t1
= build (GOTO_EXPR
, void_type_node
, tree_block_label (bb_true
));
954 t2
= build (GOTO_EXPR
, void_type_node
, tree_block_label (bb_false
));
955 COND_EXPR_THEN (cond
) = t1
;
956 COND_EXPR_ELSE (cond
) = t2
;
958 /* Wire the blocks together. */
959 e
->flags
= EDGE_TRUE_VALUE
;
960 redirect_edge_succ (e
, bb_true
);
961 make_edge (bb_cond
, bb_false
, EDGE_FALSE_VALUE
);
962 make_edge (bb_true
, bb_join
, EDGE_FALLTHRU
);
963 make_edge (bb_false
, bb_join
, EDGE_FALLTHRU
);
965 /* Update dominance info. Note that bb_join's data was
966 updated by split_block. */
967 if (dom_info_available_p (CDI_DOMINATORS
))
969 set_immediate_dominator (CDI_DOMINATORS
, bb_true
, bb_cond
);
970 set_immediate_dominator (CDI_DOMINATORS
, bb_false
, bb_cond
);
973 rr
= make_rename_temp (inner_type
, NULL
);
974 ri
= make_rename_temp (inner_type
, NULL
);
977 /* In the TRUE branch, we compute
979 div = (br * ratio) + bi;
980 tr = (ar * ratio) + ai;
981 ti = (ai * ratio) - ar;
984 if (bb_true
|| integer_nonzerop (cond
))
988 *bsi
= bsi_last (bb_true
);
989 bsi_insert_after (bsi
, build_empty_stmt (), BSI_NEW_STMT
);
992 ratio
= gimplify_build2 (bsi
, code
, inner_type
, br
, bi
);
994 t1
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, br
, ratio
);
995 div
= gimplify_build2 (bsi
, PLUS_EXPR
, inner_type
, t1
, bi
);
997 t1
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ar
, ratio
);
998 tr
= gimplify_build2 (bsi
, PLUS_EXPR
, inner_type
, t1
, ai
);
1000 t1
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ai
, ratio
);
1001 ti
= gimplify_build2 (bsi
, MINUS_EXPR
, inner_type
, t1
, ar
);
1003 tr
= gimplify_build2 (bsi
, code
, inner_type
, tr
, div
);
1004 ti
= gimplify_build2 (bsi
, code
, inner_type
, ti
, div
);
1008 t1
= build (MODIFY_EXPR
, inner_type
, rr
, tr
);
1009 bsi_insert_before (bsi
, t1
, BSI_SAME_STMT
);
1010 t1
= build (MODIFY_EXPR
, inner_type
, ri
, ti
);
1011 bsi_insert_before (bsi
, t1
, BSI_SAME_STMT
);
1016 /* In the FALSE branch, we compute
1018 divisor = (d * ratio) + c;
1019 tr = (b * ratio) + a;
1020 ti = b - (a * ratio);
1023 if (bb_false
|| integer_zerop (cond
))
1027 *bsi
= bsi_last (bb_false
);
1028 bsi_insert_after (bsi
, build_empty_stmt (), BSI_NEW_STMT
);
1031 ratio
= gimplify_build2 (bsi
, code
, inner_type
, bi
, br
);
1033 t1
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, bi
, ratio
);
1034 div
= gimplify_build2 (bsi
, PLUS_EXPR
, inner_type
, t1
, br
);
1036 t1
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ai
, ratio
);
1037 tr
= gimplify_build2 (bsi
, PLUS_EXPR
, inner_type
, t1
, ar
);
1039 t1
= gimplify_build2 (bsi
, MULT_EXPR
, inner_type
, ar
, ratio
);
1040 ti
= gimplify_build2 (bsi
, MINUS_EXPR
, inner_type
, ai
, t1
);
1042 tr
= gimplify_build2 (bsi
, code
, inner_type
, tr
, div
);
1043 ti
= gimplify_build2 (bsi
, code
, inner_type
, ti
, div
);
1047 t1
= build (MODIFY_EXPR
, inner_type
, rr
, tr
);
1048 bsi_insert_before (bsi
, t1
, BSI_SAME_STMT
);
1049 t1
= build (MODIFY_EXPR
, inner_type
, ri
, ti
);
1050 bsi_insert_before (bsi
, t1
, BSI_SAME_STMT
);
1056 *bsi
= bsi_start (bb_join
);
1060 update_complex_assignment (bsi
, rr
, ri
);
1063 /* Expand complex division to scalars. */
1066 expand_complex_division (block_stmt_iterator
*bsi
, tree inner_type
,
1067 tree ar
, tree ai
, tree br
, tree bi
,
1068 enum tree_code code
,
1069 complex_lattice_t al
, complex_lattice_t bl
)
1073 switch (PAIR (al
, bl
))
1075 case PAIR (ONLY_REAL
, ONLY_REAL
):
1076 rr
= gimplify_build2 (bsi
, code
, inner_type
, ar
, br
);
1080 case PAIR (ONLY_REAL
, ONLY_IMAG
):
1082 ri
= gimplify_build2 (bsi
, code
, inner_type
, ar
, bi
);
1083 ri
= gimplify_build1 (bsi
, NEGATE_EXPR
, inner_type
, ri
);
1086 case PAIR (ONLY_IMAG
, ONLY_REAL
):
1088 ri
= gimplify_build2 (bsi
, code
, inner_type
, ai
, br
);
1091 case PAIR (ONLY_IMAG
, ONLY_IMAG
):
1092 rr
= gimplify_build2 (bsi
, code
, inner_type
, ai
, bi
);
1096 case PAIR (VARYING
, ONLY_REAL
):
1097 rr
= gimplify_build2 (bsi
, code
, inner_type
, ar
, br
);
1098 ri
= gimplify_build2 (bsi
, code
, inner_type
, ai
, br
);
1101 case PAIR (VARYING
, ONLY_IMAG
):
1102 rr
= gimplify_build2 (bsi
, code
, inner_type
, ai
, bi
);
1103 ri
= gimplify_build2 (bsi
, code
, inner_type
, ar
, bi
);
1104 ri
= gimplify_build1 (bsi
, NEGATE_EXPR
, inner_type
, ri
);
1106 case PAIR (ONLY_REAL
, VARYING
):
1107 case PAIR (ONLY_IMAG
, VARYING
):
1108 case PAIR (VARYING
, VARYING
):
1109 switch (flag_complex_method
)
1112 /* straightforward implementation of complex divide acceptable. */
1113 expand_complex_div_straight (bsi
, inner_type
, ar
, ai
, br
, bi
, code
);
1117 if (SCALAR_FLOAT_TYPE_P (inner_type
))
1119 expand_complex_libcall (bsi
, ar
, ai
, br
, bi
, code
);
1125 /* wide ranges of inputs must work for complex divide. */
1126 expand_complex_div_wide (bsi
, inner_type
, ar
, ai
, br
, bi
, code
);
1138 update_complex_assignment (bsi
, rr
, ri
);
1141 /* Expand complex negation to scalars:
1146 expand_complex_negation (block_stmt_iterator
*bsi
, tree inner_type
,
1151 rr
= gimplify_build1 (bsi
, NEGATE_EXPR
, inner_type
, ar
);
1152 ri
= gimplify_build1 (bsi
, NEGATE_EXPR
, inner_type
, ai
);
1154 update_complex_assignment (bsi
, rr
, ri
);
1157 /* Expand complex conjugate to scalars:
1162 expand_complex_conjugate (block_stmt_iterator
*bsi
, tree inner_type
,
1167 ri
= gimplify_build1 (bsi
, NEGATE_EXPR
, inner_type
, ai
);
1169 update_complex_assignment (bsi
, ar
, ri
);
1172 /* Expand complex comparison (EQ or NE only). */
1175 expand_complex_comparison (block_stmt_iterator
*bsi
, tree ar
, tree ai
,
1176 tree br
, tree bi
, enum tree_code code
)
1178 tree cr
, ci
, cc
, stmt
, expr
, type
;
1180 cr
= gimplify_build2 (bsi
, code
, boolean_type_node
, ar
, br
);
1181 ci
= gimplify_build2 (bsi
, code
, boolean_type_node
, ai
, bi
);
1182 cc
= gimplify_build2 (bsi
,
1183 (code
== EQ_EXPR
? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
),
1184 boolean_type_node
, cr
, ci
);
1186 stmt
= expr
= bsi_stmt (*bsi
);
1188 switch (TREE_CODE (stmt
))
1191 expr
= TREE_OPERAND (stmt
, 0);
1194 type
= TREE_TYPE (TREE_OPERAND (expr
, 1));
1195 TREE_OPERAND (expr
, 1) = fold_convert (type
, cc
);
1198 TREE_OPERAND (stmt
, 0) = cc
;
1207 /* Process one statement. If we identify a complex operation, expand it. */
1210 expand_complex_operations_1 (block_stmt_iterator
*bsi
)
1212 tree stmt
= bsi_stmt (*bsi
);
1213 tree rhs
, type
, inner_type
;
1214 tree ac
, ar
, ai
, bc
, br
, bi
;
1215 complex_lattice_t al
, bl
;
1216 enum tree_code code
;
1218 switch (TREE_CODE (stmt
))
1221 stmt
= TREE_OPERAND (stmt
, 0);
1224 if (TREE_CODE (stmt
) != MODIFY_EXPR
)
1229 rhs
= TREE_OPERAND (stmt
, 1);
1233 rhs
= TREE_OPERAND (stmt
, 0);
1240 type
= TREE_TYPE (rhs
);
1241 code
= TREE_CODE (rhs
);
1243 /* Initial filter for operations we handle. */
1249 case TRUNC_DIV_EXPR
:
1251 case FLOOR_DIV_EXPR
:
1252 case ROUND_DIV_EXPR
:
1256 if (TREE_CODE (type
) != COMPLEX_TYPE
)
1258 inner_type
= TREE_TYPE (type
);
1263 inner_type
= TREE_TYPE (TREE_OPERAND (rhs
, 1));
1264 if (TREE_CODE (inner_type
) != COMPLEX_TYPE
)
1270 tree lhs
= TREE_OPERAND (stmt
, 0);
1271 tree rhs
= TREE_OPERAND (stmt
, 1);
1273 if (TREE_CODE (type
) == COMPLEX_TYPE
)
1274 expand_complex_move (bsi
, stmt
, type
, lhs
, rhs
);
1275 else if ((TREE_CODE (rhs
) == REALPART_EXPR
1276 || TREE_CODE (rhs
) == IMAGPART_EXPR
)
1277 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
)
1279 TREE_OPERAND (stmt
, 1)
1280 = extract_component (bsi
, TREE_OPERAND (rhs
, 0),
1281 TREE_CODE (rhs
) == IMAGPART_EXPR
, false);
1288 /* Extract the components of the two complex values. Make sure and
1289 handle the common case of the same value used twice specially. */
1290 ac
= TREE_OPERAND (rhs
, 0);
1291 ar
= extract_component (bsi
, ac
, 0, true);
1292 ai
= extract_component (bsi
, ac
, 1, true);
1294 if (TREE_CODE_CLASS (code
) == tcc_unary
)
1295 bc
= br
= bi
= NULL
;
1298 bc
= TREE_OPERAND (rhs
, 1);
1303 br
= extract_component (bsi
, bc
, 0, true);
1304 bi
= extract_component (bsi
, bc
, 1, true);
1310 al
= find_lattice_value (ac
);
1311 if (al
== UNINITIALIZED
)
1314 if (TREE_CODE_CLASS (code
) == tcc_unary
)
1320 bl
= find_lattice_value (bc
);
1321 if (bl
== UNINITIALIZED
)
1332 expand_complex_addition (bsi
, inner_type
, ar
, ai
, br
, bi
, code
, al
, bl
);
1336 expand_complex_multiplication (bsi
, inner_type
, ar
, ai
, br
, bi
, al
, bl
);
1339 case TRUNC_DIV_EXPR
:
1341 case FLOOR_DIV_EXPR
:
1342 case ROUND_DIV_EXPR
:
1344 expand_complex_division (bsi
, inner_type
, ar
, ai
, br
, bi
, code
, al
, bl
);
1348 expand_complex_negation (bsi
, inner_type
, ar
, ai
);
1352 expand_complex_conjugate (bsi
, inner_type
, ar
, ai
);
1357 expand_complex_comparison (bsi
, ar
, ai
, br
, bi
, code
);
1366 /* Entry point for complex operation lowering during optimization. */
1369 tree_lower_complex (void)
1371 int old_last_basic_block
;
1372 block_stmt_iterator bsi
;
1375 if (!init_dont_simulate_again ())
1378 complex_lattice_values
= VEC_alloc (complex_lattice_t
, heap
, num_ssa_names
);
1379 VEC_safe_grow (complex_lattice_t
, heap
,
1380 complex_lattice_values
, num_ssa_names
);
1381 memset (VEC_address (complex_lattice_t
, complex_lattice_values
), 0,
1382 num_ssa_names
* sizeof(complex_lattice_t
));
1383 init_parameter_lattice_values ();
1385 ssa_propagate (complex_visit_stmt
, complex_visit_phi
);
1387 create_components ();
1388 update_parameter_components ();
1390 old_last_basic_block
= last_basic_block
;
1393 if (bb
->index
>= old_last_basic_block
)
1395 update_phi_components (bb
);
1396 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
1397 expand_complex_operations_1 (&bsi
);
1400 bsi_commit_edge_inserts ();
1402 VEC_free (tree
, heap
, complex_variable_components
);
1403 VEC_free (complex_lattice_t
, heap
, complex_lattice_values
);
1406 struct tree_opt_pass pass_lower_complex
=
1408 "cplxlower", /* name */
1410 tree_lower_complex
, /* execute */
1413 0, /* static_pass_number */
1415 PROP_ssa
, /* properties_required */
1416 0, /* properties_provided */
1417 0, /* properties_destroyed */
1418 0, /* todo_flags_start */
1419 TODO_dump_func
| TODO_ggc_collect
1421 | TODO_verify_stmts
, /* todo_flags_finish */
1426 /* Entry point for complex operation lowering without optimization. */
1429 tree_lower_complex_O0 (void)
1431 int old_last_basic_block
= last_basic_block
;
1432 block_stmt_iterator bsi
;
1437 if (bb
->index
>= old_last_basic_block
)
1439 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
1440 expand_complex_operations_1 (&bsi
);
1445 gate_no_optimization (void)
1447 return optimize
== 0;
1450 struct tree_opt_pass pass_lower_complex_O0
=
1452 "cplxlower0", /* name */
1453 gate_no_optimization
, /* gate */
1454 tree_lower_complex_O0
, /* execute */
1457 0, /* static_pass_number */
1459 PROP_cfg
, /* properties_required */
1460 0, /* properties_provided */
1461 0, /* properties_destroyed */
1462 0, /* todo_flags_start */
1463 TODO_dump_func
| TODO_ggc_collect
1464 | TODO_verify_stmts
, /* todo_flags_finish */