* fr.po, sv.po: Update.
[official-gcc.git] / gcc / tree-complex.c
blob1fa76a900c3aebc0366af0da0b368a470abe776c
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
9 later version.
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
14 for more details.
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
19 02111-1307, USA. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "real.h"
28 #include "flags.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. */
40 typedef enum
42 UNINITIALIZED = 0,
43 ONLY_REAL = 1,
44 ONLY_IMAG = 2,
45 VARYING = 3
46 } complex_lattice_t;
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. */
62 static int
63 some_nonzerop (tree t)
65 int zerop = false;
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);
72 return !zerop;
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)
81 tree real, imag;
82 int r, i;
83 complex_lattice_t ret;
85 switch (TREE_CODE (t))
87 case SSA_NAME:
88 return VEC_index (complex_lattice_t, complex_lattice_values,
89 SSA_NAME_VERSION (t));
91 case COMPLEX_CST:
92 real = TREE_REALPART (t);
93 imag = TREE_IMAGPART (t);
94 break;
96 case COMPLEX_EXPR:
97 real = TREE_OPERAND (t, 0);
98 imag = TREE_OPERAND (t, 1);
99 break;
101 default:
102 gcc_unreachable ();
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)
113 ret = ONLY_REAL;
115 return ret;
118 /* Determine if LHS is something for which we're interested in seeing
119 simulation results. */
121 static bool
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. */
129 static void
130 init_parameter_lattice_values (void)
132 tree parm;
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. */
147 static bool
148 init_dont_simulate_again (void)
150 basic_block bb;
151 block_stmt_iterator bsi;
152 tree phi;
153 bool saw_a_complex_value = false;
155 FOR_EACH_BB (bb)
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);
163 bool dsa = true;
165 if (TREE_CODE (stmt) == MODIFY_EXPR
166 && is_complex_reg (TREE_OPERAND (stmt, 0)))
168 dsa = false;
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,
184 tree *result_p)
186 complex_lattice_t new_l, old_l, op1_l, op2_l;
187 unsigned int ver;
188 tree lhs, rhs;
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);
200 *result_p = lhs;
201 ver = SSA_NAME_VERSION (lhs);
202 old_l = VEC_index (complex_lattice_t, complex_lattice_values, ver);
204 switch (TREE_CODE (rhs))
206 case SSA_NAME:
207 case COMPLEX_EXPR:
208 case COMPLEX_CST:
209 new_l = find_lattice_value (rhs);
210 break;
212 case PLUS_EXPR:
213 case MINUS_EXPR:
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
218 models addition. */
219 new_l = op1_l | op2_l;
220 break;
222 case MULT_EXPR:
223 case RDIV_EXPR:
224 case TRUNC_DIV_EXPR:
225 case CEIL_DIV_EXPR:
226 case FLOOR_DIV_EXPR:
227 case ROUND_DIV_EXPR:
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)
233 new_l = VARYING;
234 /* Don't prematurely promote variables if we've not yet seen
235 their inputs. */
236 else if (op1_l == UNINITIALIZED)
237 new_l = op2_l;
238 else if (op2_l == UNINITIALIZED)
239 new_l = op1_l;
240 else
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. */
249 new_l |= old_l;
251 break;
253 case NEGATE_EXPR:
254 case CONJ_EXPR:
255 new_l = find_lattice_value (TREE_OPERAND (rhs, 0));
256 break;
258 default:
259 new_l = VARYING;
260 break;
263 /* If nothing changed this round, let the propagator know. */
264 if (new_l == old_l)
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;
277 unsigned int ver;
278 tree lhs;
279 int i;
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);
295 if (new_l == old_l)
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. */
305 static void
306 create_components (void)
308 size_t k, n;
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;
319 if (var != 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);
350 else
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. */
368 static tree
369 extract_component (block_stmt_iterator *bsi, tree t, bool imagpart_p,
370 bool gimple_p)
372 switch (TREE_CODE (t))
374 case COMPLEX_CST:
375 return imagpart_p ? TREE_IMAGPART (t) : TREE_REALPART (t);
377 case COMPLEX_EXPR:
378 return TREE_OPERAND (t, imagpart_p);
380 case VAR_DECL:
381 case PARM_DECL:
382 case INDIRECT_REF:
383 case COMPONENT_REF:
384 case ARRAY_REF:
386 tree inner_type = TREE_TYPE (TREE_TYPE (t));
388 t = build1 ((imagpart_p ? IMAGPART_EXPR : REALPART_EXPR),
389 inner_type, unshare_expr (t));
391 if (gimple_p)
392 t = gimplify_val (bsi, inner_type, t);
394 return t;
397 case SSA_NAME:
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))
410 return def;
414 return VEC_index (tree, complex_variable_components,
415 var_ann (SSA_NAME_VAR (t))->uid * 2 + imagpart_p);
418 default:
419 gcc_unreachable ();
423 /* Update the complex components of the ssa name on the lhs of STMT. */
425 static void
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;
429 tree v, x;
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);
444 static void
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;
448 tree v, x;
450 v = VEC_index (tree, complex_variable_components, 2*uid);
451 x = build2 (MODIFY_EXPR, TREE_TYPE (v), v, r);
452 if (stmt)
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);
461 if (stmt)
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. */
471 static void
472 update_complex_assignment (block_stmt_iterator *bsi, tree r, tree i)
474 tree stmt, mod;
475 tree type;
477 mod = stmt = bsi_stmt (*bsi);
478 if (TREE_CODE (stmt) == RETURN_EXPR)
479 mod = TREE_OPERAND (mod, 0);
480 else if (in_ssa_p)
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);
485 update_stmt (stmt);
488 /* Generate code at the entry point of the function to initialize the
489 component variables for a complex parameter. */
491 static void
492 update_parameter_components (void)
494 edge entry_edge = single_succ_edge (ENTRY_BLOCK_PTR);
495 tree parm;
497 for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = TREE_CHAIN (parm))
499 tree type = TREE_TYPE (parm);
500 tree ssa_name, r, i;
502 if (TREE_CODE (type) != COMPLEX_TYPE || !is_gimple_reg (parm))
503 continue;
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. */
517 static void
518 update_phi_components (basic_block bb)
520 tree phi;
522 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
523 if (is_complex_reg (PHI_RESULT (phi)))
525 unsigned int i, n;
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);
532 tree r, 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. */
543 static void
544 update_all_vops (tree stmt)
546 ssa_op_iter iter;
547 tree sym;
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. */
559 static void
560 expand_complex_move (block_stmt_iterator *bsi, tree stmt, tree type,
561 tree lhs, tree rhs)
563 tree inner_type = TREE_TYPE (type);
564 tree r, i;
566 if (TREE_CODE (lhs) == SSA_NAME)
568 if (is_ctrl_altering_stmt (bsi_stmt (*bsi)))
570 edge_iterator ei;
571 edge e;
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
575 edge. Find it. */
576 FOR_EACH_EDGE (e, ei, bsi->bb->succs)
577 if (e->flags & EDGE_FALLTHRU)
578 goto found_fallthru;
579 gcc_unreachable ();
580 found_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);
592 else
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))
602 tree x;
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;
618 else
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);
630 update_stmt (stmt);
634 /* Expand complex addition to scalars:
635 a + b = (ar + br) + i(ai + bi)
636 a - b = (ar - br) + i(ai + bi)
639 static void
640 expand_complex_addition (block_stmt_iterator *bsi, tree inner_type,
641 tree ar, tree ai, tree br, tree bi,
642 enum tree_code code,
643 complex_lattice_t al, complex_lattice_t bl)
645 tree rr, ri;
647 switch (PAIR (al, bl))
649 case PAIR (ONLY_REAL, ONLY_REAL):
650 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
651 ri = ai;
652 break;
654 case PAIR (ONLY_REAL, ONLY_IMAG):
655 rr = ar;
656 if (code == MINUS_EXPR)
657 ri = gimplify_build2 (bsi, MINUS_EXPR, inner_type, ai, bi);
658 else
659 ri = bi;
660 break;
662 case PAIR (ONLY_IMAG, ONLY_REAL):
663 if (code == MINUS_EXPR)
664 rr = gimplify_build2 (bsi, MINUS_EXPR, inner_type, ar, br);
665 else
666 rr = br;
667 ri = ai;
668 break;
670 case PAIR (ONLY_IMAG, ONLY_IMAG):
671 rr = ar;
672 ri = gimplify_build2 (bsi, code, inner_type, ai, bi);
673 break;
675 case PAIR (VARYING, ONLY_REAL):
676 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
677 ri = ai;
678 break;
680 case PAIR (VARYING, ONLY_IMAG):
681 rr = ar;
682 ri = gimplify_build2 (bsi, MINUS_EXPR, inner_type, ai, bi);
683 break;
685 case PAIR (ONLY_REAL, VARYING):
686 if (code == MINUS_EXPR)
687 goto general;
688 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
689 ri = bi;
690 break;
692 case PAIR (ONLY_IMAG, VARYING):
693 if (code == MINUS_EXPR)
694 goto general;
695 rr = br;
696 ri = gimplify_build2 (bsi, MINUS_EXPR, inner_type, ai, bi);
697 break;
699 case PAIR (VARYING, VARYING):
700 general:
701 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
702 ri = gimplify_build2 (bsi, code, inner_type, ai, bi);
703 break;
705 default:
706 gcc_unreachable ();
709 update_complex_assignment (bsi, rr, ri);
712 /* Expand a complex multiplication or division to a libcall to the c99
713 compliant routines. */
715 static void
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;
737 else
738 gcc_unreachable ();
739 fn = built_in_decls[bcode];
741 TREE_OPERAND (stmt, 1)
742 = build3 (CALL_EXPR, type, build_fold_addr_expr (fn), args, NULL);
743 update_stmt (stmt);
745 if (in_ssa_p)
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)
758 static void
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)
763 tree rr, ri;
765 if (al < 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);
777 ri = ai;
778 break;
780 case PAIR (ONLY_IMAG, ONLY_REAL):
781 rr = ar;
782 if (TREE_CODE (ai) == REAL_CST
783 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (ai), dconst1))
784 ri = br;
785 else
786 ri = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, br);
787 break;
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);
792 ri = ar;
793 break;
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);
798 break;
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);
804 break;
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);
810 return;
812 else
814 tree t1, t2, t3, t4;
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)
823 t4 = t3;
824 else
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);
830 break;
832 default:
833 gcc_unreachable ();
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)
841 t = br*br + bi*bi
844 static void
845 expand_complex_div_straight (block_stmt_iterator *bsi, tree inner_type,
846 tree ar, tree ai, tree br, tree bi,
847 enum tree_code code)
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. */
871 static void
872 expand_complex_div_wide (block_stmt_iterator *bsi, tree inner_type,
873 tree ar, tree ai, tree br, tree bi,
874 enum tree_code code)
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));
883 STRIP_NOPS (cond);
885 bb_cond = bb_true = bb_false = bb_join = NULL;
886 rr = ri = tr = ti = NULL;
887 if (!TREE_CONSTANT (cond))
889 edge e;
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);
896 bb_cond = e->src;
897 bb_join = e->dest;
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
926 ratio = br/bi;
927 div = (br * ratio) + bi;
928 tr = (ar * ratio) + ai;
929 ti = (ai * ratio) - ar;
930 tr = tr / div;
931 ti = ti / div; */
932 if (bb_true || integer_nonzerop (cond))
934 if (bb_true)
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);
954 if (bb_true)
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);
960 bsi_remove (bsi);
964 /* In the FALSE branch, we compute
965 ratio = d/c;
966 divisor = (d * ratio) + c;
967 tr = (b * ratio) + a;
968 ti = b - (a * ratio);
969 tr = tr / div;
970 ti = ti / div; */
971 if (bb_false || integer_zerop (cond))
973 if (bb_false)
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);
993 if (bb_false)
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);
999 bsi_remove (bsi);
1003 if (bb_join)
1004 *bsi = bsi_start (bb_join);
1005 else
1006 rr = tr, ri = ti;
1008 update_complex_assignment (bsi, rr, ri);
1011 /* Expand complex division to scalars. */
1013 static void
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)
1019 tree rr, ri;
1021 switch (PAIR (al, bl))
1023 case PAIR (ONLY_REAL, ONLY_REAL):
1024 rr = gimplify_build2 (bsi, code, inner_type, ar, br);
1025 ri = ai;
1026 break;
1028 case PAIR (ONLY_REAL, ONLY_IMAG):
1029 rr = ai;
1030 ri = gimplify_build2 (bsi, code, inner_type, ar, bi);
1031 ri = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, ri);
1032 break;
1034 case PAIR (ONLY_IMAG, ONLY_REAL):
1035 rr = ar;
1036 ri = gimplify_build2 (bsi, code, inner_type, ai, br);
1037 break;
1039 case PAIR (ONLY_IMAG, ONLY_IMAG):
1040 rr = gimplify_build2 (bsi, code, inner_type, ai, bi);
1041 ri = ar;
1042 break;
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);
1047 break;
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)
1059 case 0:
1060 /* straightforward implementation of complex divide acceptable. */
1061 expand_complex_div_straight (bsi, inner_type, ar, ai, br, bi, code);
1062 break;
1064 case 2:
1065 if (SCALAR_FLOAT_TYPE_P (inner_type))
1067 expand_complex_libcall (bsi, ar, ai, br, bi, code);
1068 break;
1070 /* FALLTHRU */
1072 case 1:
1073 /* wide ranges of inputs must work for complex divide. */
1074 expand_complex_div_wide (bsi, inner_type, ar, ai, br, bi, code);
1075 break;
1077 default:
1078 gcc_unreachable ();
1080 return;
1082 default:
1083 gcc_unreachable ();
1086 update_complex_assignment (bsi, rr, ri);
1089 /* Expand complex negation to scalars:
1090 -a = (-ar) + i(-ai)
1093 static void
1094 expand_complex_negation (block_stmt_iterator *bsi, tree inner_type,
1095 tree ar, tree ai)
1097 tree rr, ri;
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:
1106 ~a = (ar) + i(-ai)
1109 static void
1110 expand_complex_conjugate (block_stmt_iterator *bsi, tree inner_type,
1111 tree ar, tree ai)
1113 tree ri;
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). */
1122 static void
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))
1138 case RETURN_EXPR:
1139 expr = TREE_OPERAND (stmt, 0);
1140 /* FALLTHRU */
1141 case MODIFY_EXPR:
1142 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1143 TREE_OPERAND (expr, 1) = fold_convert (type, cc);
1144 break;
1145 case COND_EXPR:
1146 TREE_OPERAND (stmt, 0) = cc;
1147 break;
1148 default:
1149 gcc_unreachable ();
1152 update_stmt (stmt);
1155 /* Process one statement. If we identify a complex operation, expand it. */
1157 static void
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))
1168 case RETURN_EXPR:
1169 stmt = TREE_OPERAND (stmt, 0);
1170 if (!stmt)
1171 return;
1172 if (TREE_CODE (stmt) != MODIFY_EXPR)
1173 return;
1174 /* FALLTHRU */
1176 case MODIFY_EXPR:
1177 rhs = TREE_OPERAND (stmt, 1);
1178 break;
1180 case COND_EXPR:
1181 rhs = TREE_OPERAND (stmt, 0);
1182 break;
1184 default:
1185 return;
1188 type = TREE_TYPE (rhs);
1189 code = TREE_CODE (rhs);
1191 /* Initial filter for operations we handle. */
1192 switch (code)
1194 case PLUS_EXPR:
1195 case MINUS_EXPR:
1196 case MULT_EXPR:
1197 case TRUNC_DIV_EXPR:
1198 case CEIL_DIV_EXPR:
1199 case FLOOR_DIV_EXPR:
1200 case ROUND_DIV_EXPR:
1201 case RDIV_EXPR:
1202 case NEGATE_EXPR:
1203 case CONJ_EXPR:
1204 if (TREE_CODE (type) != COMPLEX_TYPE)
1205 return;
1206 inner_type = TREE_TYPE (type);
1207 break;
1209 case EQ_EXPR:
1210 case NE_EXPR:
1211 inner_type = TREE_TYPE (TREE_OPERAND (rhs, 1));
1212 if (TREE_CODE (inner_type) != COMPLEX_TYPE)
1213 return;
1214 break;
1216 default:
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);
1230 update_stmt (stmt);
1233 return;
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;
1244 else
1246 bc = TREE_OPERAND (rhs, 1);
1247 if (ac == bc)
1248 br = ar, bi = ai;
1249 else
1251 br = extract_component (bsi, bc, 0, true);
1252 bi = extract_component (bsi, bc, 1, true);
1256 if (in_ssa_p)
1258 al = find_lattice_value (ac);
1259 if (al == UNINITIALIZED)
1260 al = VARYING;
1262 if (TREE_CODE_CLASS (code) == tcc_unary)
1263 bl = UNINITIALIZED;
1264 else if (ac == bc)
1265 bl = al;
1266 else
1268 bl = find_lattice_value (bc);
1269 if (bl == UNINITIALIZED)
1270 bl = VARYING;
1273 else
1274 al = bl = VARYING;
1276 switch (code)
1278 case PLUS_EXPR:
1279 case MINUS_EXPR:
1280 expand_complex_addition (bsi, inner_type, ar, ai, br, bi, code, al, bl);
1281 break;
1283 case MULT_EXPR:
1284 expand_complex_multiplication (bsi, inner_type, ar, ai, br, bi, al, bl);
1285 break;
1287 case TRUNC_DIV_EXPR:
1288 case CEIL_DIV_EXPR:
1289 case FLOOR_DIV_EXPR:
1290 case ROUND_DIV_EXPR:
1291 case RDIV_EXPR:
1292 expand_complex_division (bsi, inner_type, ar, ai, br, bi, code, al, bl);
1293 break;
1295 case NEGATE_EXPR:
1296 expand_complex_negation (bsi, inner_type, ar, ai);
1297 break;
1299 case CONJ_EXPR:
1300 expand_complex_conjugate (bsi, inner_type, ar, ai);
1301 break;
1303 case EQ_EXPR:
1304 case NE_EXPR:
1305 expand_complex_comparison (bsi, ar, ai, br, bi, code);
1306 break;
1308 default:
1309 gcc_unreachable ();
1314 /* Entry point for complex operation lowering during optimization. */
1316 static void
1317 tree_lower_complex (void)
1319 int old_last_basic_block;
1320 block_stmt_iterator bsi;
1321 basic_block bb;
1323 if (!init_dont_simulate_again ())
1324 return;
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;
1339 FOR_EACH_BB (bb)
1341 if (bb->index >= old_last_basic_block)
1342 continue;
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 */
1357 0, /* gate */
1358 tree_lower_complex, /* execute */
1359 NULL, /* sub */
1360 NULL, /* next */
1361 0, /* static_pass_number */
1362 0, /* tv_id */
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
1368 | TODO_update_ssa
1369 | TODO_verify_stmts, /* todo_flags_finish */
1370 0 /* letter */
1374 /* Entry point for complex operation lowering without optimization. */
1376 static void
1377 tree_lower_complex_O0 (void)
1379 int old_last_basic_block = last_basic_block;
1380 block_stmt_iterator bsi;
1381 basic_block bb;
1383 FOR_EACH_BB (bb)
1385 if (bb->index >= old_last_basic_block)
1386 continue;
1387 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1388 expand_complex_operations_1 (&bsi);
1392 static bool
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 */
1403 NULL, /* sub */
1404 NULL, /* next */
1405 0, /* static_pass_number */
1406 0, /* tv_id */
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 */
1413 0 /* letter */