2013-11-12 Andrew MacLeod <amacleod@redhat.com>
[official-gcc.git] / gcc / tree-complex.c
blobb75c732e4f81c2b7d84aa28b11d6ab3102399583
1 /* Lower complex number operations to scalar operations.
2 Copyright (C) 2004-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
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 COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "flags.h"
26 #include "gimplify.h"
27 #include "gimple-ssa.h"
28 #include "tree-cfg.h"
29 #include "tree-phinodes.h"
30 #include "ssa-iterators.h"
31 #include "tree-ssanames.h"
32 #include "tree-dfa.h"
33 #include "tree-ssa.h"
34 #include "tree-iterator.h"
35 #include "tree-pass.h"
36 #include "tree-ssa-propagate.h"
37 #include "tree-hasher.h"
38 #include "cfgloop.h"
41 /* For each complex ssa name, a lattice value. We're interested in finding
42 out whether a complex number is degenerate in some way, having only real
43 or only complex parts. */
45 enum
47 UNINITIALIZED = 0,
48 ONLY_REAL = 1,
49 ONLY_IMAG = 2,
50 VARYING = 3
53 /* The type complex_lattice_t holds combinations of the above
54 constants. */
55 typedef int complex_lattice_t;
57 #define PAIR(a, b) ((a) << 2 | (b))
60 static vec<complex_lattice_t> complex_lattice_values;
62 /* For each complex variable, a pair of variables for the components exists in
63 the hashtable. */
64 static int_tree_htab_type complex_variable_components;
66 /* For each complex SSA_NAME, a pair of ssa names for the components. */
67 static vec<tree> complex_ssa_name_components;
69 /* Lookup UID in the complex_variable_components hashtable and return the
70 associated tree. */
71 static tree
72 cvc_lookup (unsigned int uid)
74 struct int_tree_map *h, in;
75 in.uid = uid;
76 h = complex_variable_components.find_with_hash (&in, uid);
77 return h ? h->to : NULL;
80 /* Insert the pair UID, TO into the complex_variable_components hashtable. */
82 static void
83 cvc_insert (unsigned int uid, tree to)
85 struct int_tree_map *h;
86 int_tree_map **loc;
88 h = XNEW (struct int_tree_map);
89 h->uid = uid;
90 h->to = to;
91 loc = complex_variable_components.find_slot_with_hash (h, uid, INSERT);
92 *loc = h;
95 /* Return true if T is not a zero constant. In the case of real values,
96 we're only interested in +0.0. */
98 static int
99 some_nonzerop (tree t)
101 int zerop = false;
103 /* Operations with real or imaginary part of a complex number zero
104 cannot be treated the same as operations with a real or imaginary
105 operand if we care about the signs of zeros in the result. */
106 if (TREE_CODE (t) == REAL_CST && !flag_signed_zeros)
107 zerop = REAL_VALUES_IDENTICAL (TREE_REAL_CST (t), dconst0);
108 else if (TREE_CODE (t) == FIXED_CST)
109 zerop = fixed_zerop (t);
110 else if (TREE_CODE (t) == INTEGER_CST)
111 zerop = integer_zerop (t);
113 return !zerop;
117 /* Compute a lattice value from the components of a complex type REAL
118 and IMAG. */
120 static complex_lattice_t
121 find_lattice_value_parts (tree real, tree imag)
123 int r, i;
124 complex_lattice_t ret;
126 r = some_nonzerop (real);
127 i = some_nonzerop (imag);
128 ret = r * ONLY_REAL + i * ONLY_IMAG;
130 /* ??? On occasion we could do better than mapping 0+0i to real, but we
131 certainly don't want to leave it UNINITIALIZED, which eventually gets
132 mapped to VARYING. */
133 if (ret == UNINITIALIZED)
134 ret = ONLY_REAL;
136 return ret;
140 /* Compute a lattice value from gimple_val T. */
142 static complex_lattice_t
143 find_lattice_value (tree t)
145 tree real, imag;
147 switch (TREE_CODE (t))
149 case SSA_NAME:
150 return complex_lattice_values[SSA_NAME_VERSION (t)];
152 case COMPLEX_CST:
153 real = TREE_REALPART (t);
154 imag = TREE_IMAGPART (t);
155 break;
157 default:
158 gcc_unreachable ();
161 return find_lattice_value_parts (real, imag);
164 /* Determine if LHS is something for which we're interested in seeing
165 simulation results. */
167 static bool
168 is_complex_reg (tree lhs)
170 return TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE && is_gimple_reg (lhs);
173 /* Mark the incoming parameters to the function as VARYING. */
175 static void
176 init_parameter_lattice_values (void)
178 tree parm, ssa_name;
180 for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = DECL_CHAIN (parm))
181 if (is_complex_reg (parm)
182 && (ssa_name = ssa_default_def (cfun, parm)) != NULL_TREE)
183 complex_lattice_values[SSA_NAME_VERSION (ssa_name)] = VARYING;
186 /* Initialize simulation state for each statement. Return false if we
187 found no statements we want to simulate, and thus there's nothing
188 for the entire pass to do. */
190 static bool
191 init_dont_simulate_again (void)
193 basic_block bb;
194 gimple_stmt_iterator gsi;
195 gimple phi;
196 bool saw_a_complex_op = false;
198 FOR_EACH_BB (bb)
200 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
202 phi = gsi_stmt (gsi);
203 prop_set_simulate_again (phi,
204 is_complex_reg (gimple_phi_result (phi)));
207 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
209 gimple stmt;
210 tree op0, op1;
211 bool sim_again_p;
213 stmt = gsi_stmt (gsi);
214 op0 = op1 = NULL_TREE;
216 /* Most control-altering statements must be initially
217 simulated, else we won't cover the entire cfg. */
218 sim_again_p = stmt_ends_bb_p (stmt);
220 switch (gimple_code (stmt))
222 case GIMPLE_CALL:
223 if (gimple_call_lhs (stmt))
224 sim_again_p = is_complex_reg (gimple_call_lhs (stmt));
225 break;
227 case GIMPLE_ASSIGN:
228 sim_again_p = is_complex_reg (gimple_assign_lhs (stmt));
229 if (gimple_assign_rhs_code (stmt) == REALPART_EXPR
230 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
231 op0 = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
232 else
233 op0 = gimple_assign_rhs1 (stmt);
234 if (gimple_num_ops (stmt) > 2)
235 op1 = gimple_assign_rhs2 (stmt);
236 break;
238 case GIMPLE_COND:
239 op0 = gimple_cond_lhs (stmt);
240 op1 = gimple_cond_rhs (stmt);
241 break;
243 default:
244 break;
247 if (op0 || op1)
248 switch (gimple_expr_code (stmt))
250 case EQ_EXPR:
251 case NE_EXPR:
252 case PLUS_EXPR:
253 case MINUS_EXPR:
254 case MULT_EXPR:
255 case TRUNC_DIV_EXPR:
256 case CEIL_DIV_EXPR:
257 case FLOOR_DIV_EXPR:
258 case ROUND_DIV_EXPR:
259 case RDIV_EXPR:
260 if (TREE_CODE (TREE_TYPE (op0)) == COMPLEX_TYPE
261 || TREE_CODE (TREE_TYPE (op1)) == COMPLEX_TYPE)
262 saw_a_complex_op = true;
263 break;
265 case NEGATE_EXPR:
266 case CONJ_EXPR:
267 if (TREE_CODE (TREE_TYPE (op0)) == COMPLEX_TYPE)
268 saw_a_complex_op = true;
269 break;
271 case REALPART_EXPR:
272 case IMAGPART_EXPR:
273 /* The total store transformation performed during
274 gimplification creates such uninitialized loads
275 and we need to lower the statement to be able
276 to fix things up. */
277 if (TREE_CODE (op0) == SSA_NAME
278 && ssa_undefined_value_p (op0))
279 saw_a_complex_op = true;
280 break;
282 default:
283 break;
286 prop_set_simulate_again (stmt, sim_again_p);
290 return saw_a_complex_op;
294 /* Evaluate statement STMT against the complex lattice defined above. */
296 static enum ssa_prop_result
297 complex_visit_stmt (gimple stmt, edge *taken_edge_p ATTRIBUTE_UNUSED,
298 tree *result_p)
300 complex_lattice_t new_l, old_l, op1_l, op2_l;
301 unsigned int ver;
302 tree lhs;
304 lhs = gimple_get_lhs (stmt);
305 /* Skip anything but GIMPLE_ASSIGN and GIMPLE_CALL with a lhs. */
306 if (!lhs)
307 return SSA_PROP_VARYING;
309 /* These conditions should be satisfied due to the initial filter
310 set up in init_dont_simulate_again. */
311 gcc_assert (TREE_CODE (lhs) == SSA_NAME);
312 gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE);
314 *result_p = lhs;
315 ver = SSA_NAME_VERSION (lhs);
316 old_l = complex_lattice_values[ver];
318 switch (gimple_expr_code (stmt))
320 case SSA_NAME:
321 case COMPLEX_CST:
322 new_l = find_lattice_value (gimple_assign_rhs1 (stmt));
323 break;
325 case COMPLEX_EXPR:
326 new_l = find_lattice_value_parts (gimple_assign_rhs1 (stmt),
327 gimple_assign_rhs2 (stmt));
328 break;
330 case PLUS_EXPR:
331 case MINUS_EXPR:
332 op1_l = find_lattice_value (gimple_assign_rhs1 (stmt));
333 op2_l = find_lattice_value (gimple_assign_rhs2 (stmt));
335 /* We've set up the lattice values such that IOR neatly
336 models addition. */
337 new_l = op1_l | op2_l;
338 break;
340 case MULT_EXPR:
341 case RDIV_EXPR:
342 case TRUNC_DIV_EXPR:
343 case CEIL_DIV_EXPR:
344 case FLOOR_DIV_EXPR:
345 case ROUND_DIV_EXPR:
346 op1_l = find_lattice_value (gimple_assign_rhs1 (stmt));
347 op2_l = find_lattice_value (gimple_assign_rhs2 (stmt));
349 /* Obviously, if either varies, so does the result. */
350 if (op1_l == VARYING || op2_l == VARYING)
351 new_l = VARYING;
352 /* Don't prematurely promote variables if we've not yet seen
353 their inputs. */
354 else if (op1_l == UNINITIALIZED)
355 new_l = op2_l;
356 else if (op2_l == UNINITIALIZED)
357 new_l = op1_l;
358 else
360 /* At this point both numbers have only one component. If the
361 numbers are of opposite kind, the result is imaginary,
362 otherwise the result is real. The add/subtract translates
363 the real/imag from/to 0/1; the ^ performs the comparison. */
364 new_l = ((op1_l - ONLY_REAL) ^ (op2_l - ONLY_REAL)) + ONLY_REAL;
366 /* Don't allow the lattice value to flip-flop indefinitely. */
367 new_l |= old_l;
369 break;
371 case NEGATE_EXPR:
372 case CONJ_EXPR:
373 new_l = find_lattice_value (gimple_assign_rhs1 (stmt));
374 break;
376 default:
377 new_l = VARYING;
378 break;
381 /* If nothing changed this round, let the propagator know. */
382 if (new_l == old_l)
383 return SSA_PROP_NOT_INTERESTING;
385 complex_lattice_values[ver] = new_l;
386 return new_l == VARYING ? SSA_PROP_VARYING : SSA_PROP_INTERESTING;
389 /* Evaluate a PHI node against the complex lattice defined above. */
391 static enum ssa_prop_result
392 complex_visit_phi (gimple phi)
394 complex_lattice_t new_l, old_l;
395 unsigned int ver;
396 tree lhs;
397 int i;
399 lhs = gimple_phi_result (phi);
401 /* This condition should be satisfied due to the initial filter
402 set up in init_dont_simulate_again. */
403 gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE);
405 /* We've set up the lattice values such that IOR neatly models PHI meet. */
406 new_l = UNINITIALIZED;
407 for (i = gimple_phi_num_args (phi) - 1; i >= 0; --i)
408 new_l |= find_lattice_value (gimple_phi_arg_def (phi, i));
410 ver = SSA_NAME_VERSION (lhs);
411 old_l = complex_lattice_values[ver];
413 if (new_l == old_l)
414 return SSA_PROP_NOT_INTERESTING;
416 complex_lattice_values[ver] = new_l;
417 return new_l == VARYING ? SSA_PROP_VARYING : SSA_PROP_INTERESTING;
420 /* Create one backing variable for a complex component of ORIG. */
422 static tree
423 create_one_component_var (tree type, tree orig, const char *prefix,
424 const char *suffix, enum tree_code code)
426 tree r = create_tmp_var (type, prefix);
428 DECL_SOURCE_LOCATION (r) = DECL_SOURCE_LOCATION (orig);
429 DECL_ARTIFICIAL (r) = 1;
431 if (DECL_NAME (orig) && !DECL_IGNORED_P (orig))
433 const char *name = IDENTIFIER_POINTER (DECL_NAME (orig));
435 DECL_NAME (r) = get_identifier (ACONCAT ((name, suffix, NULL)));
437 SET_DECL_DEBUG_EXPR (r, build1 (code, type, orig));
438 DECL_HAS_DEBUG_EXPR_P (r) = 1;
439 DECL_IGNORED_P (r) = 0;
440 TREE_NO_WARNING (r) = TREE_NO_WARNING (orig);
442 else
444 DECL_IGNORED_P (r) = 1;
445 TREE_NO_WARNING (r) = 1;
448 return r;
451 /* Retrieve a value for a complex component of VAR. */
453 static tree
454 get_component_var (tree var, bool imag_p)
456 size_t decl_index = DECL_UID (var) * 2 + imag_p;
457 tree ret = cvc_lookup (decl_index);
459 if (ret == NULL)
461 ret = create_one_component_var (TREE_TYPE (TREE_TYPE (var)), var,
462 imag_p ? "CI" : "CR",
463 imag_p ? "$imag" : "$real",
464 imag_p ? IMAGPART_EXPR : REALPART_EXPR);
465 cvc_insert (decl_index, ret);
468 return ret;
471 /* Retrieve a value for a complex component of SSA_NAME. */
473 static tree
474 get_component_ssa_name (tree ssa_name, bool imag_p)
476 complex_lattice_t lattice = find_lattice_value (ssa_name);
477 size_t ssa_name_index;
478 tree ret;
480 if (lattice == (imag_p ? ONLY_REAL : ONLY_IMAG))
482 tree inner_type = TREE_TYPE (TREE_TYPE (ssa_name));
483 if (SCALAR_FLOAT_TYPE_P (inner_type))
484 return build_real (inner_type, dconst0);
485 else
486 return build_int_cst (inner_type, 0);
489 ssa_name_index = SSA_NAME_VERSION (ssa_name) * 2 + imag_p;
490 ret = complex_ssa_name_components[ssa_name_index];
491 if (ret == NULL)
493 if (SSA_NAME_VAR (ssa_name))
494 ret = get_component_var (SSA_NAME_VAR (ssa_name), imag_p);
495 else
496 ret = TREE_TYPE (TREE_TYPE (ssa_name));
497 ret = make_ssa_name (ret, NULL);
499 /* Copy some properties from the original. In particular, whether it
500 is used in an abnormal phi, and whether it's uninitialized. */
501 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ret)
502 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name);
503 if (SSA_NAME_IS_DEFAULT_DEF (ssa_name)
504 && TREE_CODE (SSA_NAME_VAR (ssa_name)) == VAR_DECL)
506 SSA_NAME_DEF_STMT (ret) = SSA_NAME_DEF_STMT (ssa_name);
507 set_ssa_default_def (cfun, SSA_NAME_VAR (ret), ret);
510 complex_ssa_name_components[ssa_name_index] = ret;
513 return ret;
516 /* Set a value for a complex component of SSA_NAME, return a
517 gimple_seq of stuff that needs doing. */
519 static gimple_seq
520 set_component_ssa_name (tree ssa_name, bool imag_p, tree value)
522 complex_lattice_t lattice = find_lattice_value (ssa_name);
523 size_t ssa_name_index;
524 tree comp;
525 gimple last;
526 gimple_seq list;
528 /* We know the value must be zero, else there's a bug in our lattice
529 analysis. But the value may well be a variable known to contain
530 zero. We should be safe ignoring it. */
531 if (lattice == (imag_p ? ONLY_REAL : ONLY_IMAG))
532 return NULL;
534 /* If we've already assigned an SSA_NAME to this component, then this
535 means that our walk of the basic blocks found a use before the set.
536 This is fine. Now we should create an initialization for the value
537 we created earlier. */
538 ssa_name_index = SSA_NAME_VERSION (ssa_name) * 2 + imag_p;
539 comp = complex_ssa_name_components[ssa_name_index];
540 if (comp)
543 /* If we've nothing assigned, and the value we're given is already stable,
544 then install that as the value for this SSA_NAME. This preemptively
545 copy-propagates the value, which avoids unnecessary memory allocation. */
546 else if (is_gimple_min_invariant (value)
547 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name))
549 complex_ssa_name_components[ssa_name_index] = value;
550 return NULL;
552 else if (TREE_CODE (value) == SSA_NAME
553 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name))
555 /* Replace an anonymous base value with the variable from cvc_lookup.
556 This should result in better debug info. */
557 if (SSA_NAME_VAR (ssa_name)
558 && (!SSA_NAME_VAR (value) || DECL_IGNORED_P (SSA_NAME_VAR (value)))
559 && !DECL_IGNORED_P (SSA_NAME_VAR (ssa_name)))
561 comp = get_component_var (SSA_NAME_VAR (ssa_name), imag_p);
562 replace_ssa_name_symbol (value, comp);
565 complex_ssa_name_components[ssa_name_index] = value;
566 return NULL;
569 /* Finally, we need to stabilize the result by installing the value into
570 a new ssa name. */
571 else
572 comp = get_component_ssa_name (ssa_name, imag_p);
574 /* Do all the work to assign VALUE to COMP. */
575 list = NULL;
576 value = force_gimple_operand (value, &list, false, NULL);
577 last = gimple_build_assign (comp, value);
578 gimple_seq_add_stmt (&list, last);
579 gcc_assert (SSA_NAME_DEF_STMT (comp) == last);
581 return list;
584 /* Extract the real or imaginary part of a complex variable or constant.
585 Make sure that it's a proper gimple_val and gimplify it if not.
586 Emit any new code before gsi. */
588 static tree
589 extract_component (gimple_stmt_iterator *gsi, tree t, bool imagpart_p,
590 bool gimple_p)
592 switch (TREE_CODE (t))
594 case COMPLEX_CST:
595 return imagpart_p ? TREE_IMAGPART (t) : TREE_REALPART (t);
597 case COMPLEX_EXPR:
598 gcc_unreachable ();
600 case VAR_DECL:
601 case RESULT_DECL:
602 case PARM_DECL:
603 case COMPONENT_REF:
604 case ARRAY_REF:
605 case VIEW_CONVERT_EXPR:
606 case MEM_REF:
608 tree inner_type = TREE_TYPE (TREE_TYPE (t));
610 t = build1 ((imagpart_p ? IMAGPART_EXPR : REALPART_EXPR),
611 inner_type, unshare_expr (t));
613 if (gimple_p)
614 t = force_gimple_operand_gsi (gsi, t, true, NULL, true,
615 GSI_SAME_STMT);
617 return t;
620 case SSA_NAME:
621 return get_component_ssa_name (t, imagpart_p);
623 default:
624 gcc_unreachable ();
628 /* Update the complex components of the ssa name on the lhs of STMT. */
630 static void
631 update_complex_components (gimple_stmt_iterator *gsi, gimple stmt, tree r,
632 tree i)
634 tree lhs;
635 gimple_seq list;
637 lhs = gimple_get_lhs (stmt);
639 list = set_component_ssa_name (lhs, false, r);
640 if (list)
641 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
643 list = set_component_ssa_name (lhs, true, i);
644 if (list)
645 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
648 static void
649 update_complex_components_on_edge (edge e, tree lhs, tree r, tree i)
651 gimple_seq list;
653 list = set_component_ssa_name (lhs, false, r);
654 if (list)
655 gsi_insert_seq_on_edge (e, list);
657 list = set_component_ssa_name (lhs, true, i);
658 if (list)
659 gsi_insert_seq_on_edge (e, list);
663 /* Update an assignment to a complex variable in place. */
665 static void
666 update_complex_assignment (gimple_stmt_iterator *gsi, tree r, tree i)
668 gimple stmt;
670 gimple_assign_set_rhs_with_ops (gsi, COMPLEX_EXPR, r, i);
671 stmt = gsi_stmt (*gsi);
672 update_stmt (stmt);
673 if (maybe_clean_eh_stmt (stmt))
674 gimple_purge_dead_eh_edges (gimple_bb (stmt));
676 if (gimple_in_ssa_p (cfun))
677 update_complex_components (gsi, gsi_stmt (*gsi), r, i);
681 /* Generate code at the entry point of the function to initialize the
682 component variables for a complex parameter. */
684 static void
685 update_parameter_components (void)
687 edge entry_edge = single_succ_edge (ENTRY_BLOCK_PTR);
688 tree parm;
690 for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = DECL_CHAIN (parm))
692 tree type = TREE_TYPE (parm);
693 tree ssa_name, r, i;
695 if (TREE_CODE (type) != COMPLEX_TYPE || !is_gimple_reg (parm))
696 continue;
698 type = TREE_TYPE (type);
699 ssa_name = ssa_default_def (cfun, parm);
700 if (!ssa_name)
701 continue;
703 r = build1 (REALPART_EXPR, type, ssa_name);
704 i = build1 (IMAGPART_EXPR, type, ssa_name);
705 update_complex_components_on_edge (entry_edge, ssa_name, r, i);
709 /* Generate code to set the component variables of a complex variable
710 to match the PHI statements in block BB. */
712 static void
713 update_phi_components (basic_block bb)
715 gimple_stmt_iterator gsi;
717 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
719 gimple phi = gsi_stmt (gsi);
721 if (is_complex_reg (gimple_phi_result (phi)))
723 tree lr, li;
724 gimple pr = NULL, pi = NULL;
725 unsigned int i, n;
727 lr = get_component_ssa_name (gimple_phi_result (phi), false);
728 if (TREE_CODE (lr) == SSA_NAME)
729 pr = create_phi_node (lr, bb);
731 li = get_component_ssa_name (gimple_phi_result (phi), true);
732 if (TREE_CODE (li) == SSA_NAME)
733 pi = create_phi_node (li, bb);
735 for (i = 0, n = gimple_phi_num_args (phi); i < n; ++i)
737 tree comp, arg = gimple_phi_arg_def (phi, i);
738 if (pr)
740 comp = extract_component (NULL, arg, false, false);
741 SET_PHI_ARG_DEF (pr, i, comp);
743 if (pi)
745 comp = extract_component (NULL, arg, true, false);
746 SET_PHI_ARG_DEF (pi, i, comp);
753 /* Expand a complex move to scalars. */
755 static void
756 expand_complex_move (gimple_stmt_iterator *gsi, tree type)
758 tree inner_type = TREE_TYPE (type);
759 tree r, i, lhs, rhs;
760 gimple stmt = gsi_stmt (*gsi);
762 if (is_gimple_assign (stmt))
764 lhs = gimple_assign_lhs (stmt);
765 if (gimple_num_ops (stmt) == 2)
766 rhs = gimple_assign_rhs1 (stmt);
767 else
768 rhs = NULL_TREE;
770 else if (is_gimple_call (stmt))
772 lhs = gimple_call_lhs (stmt);
773 rhs = NULL_TREE;
775 else
776 gcc_unreachable ();
778 if (TREE_CODE (lhs) == SSA_NAME)
780 if (is_ctrl_altering_stmt (stmt))
782 edge e;
784 /* The value is not assigned on the exception edges, so we need not
785 concern ourselves there. We do need to update on the fallthru
786 edge. Find it. */
787 e = find_fallthru_edge (gsi_bb (*gsi)->succs);
788 if (!e)
789 gcc_unreachable ();
791 r = build1 (REALPART_EXPR, inner_type, lhs);
792 i = build1 (IMAGPART_EXPR, inner_type, lhs);
793 update_complex_components_on_edge (e, lhs, r, i);
795 else if (is_gimple_call (stmt)
796 || gimple_has_side_effects (stmt)
797 || gimple_assign_rhs_code (stmt) == PAREN_EXPR)
799 r = build1 (REALPART_EXPR, inner_type, lhs);
800 i = build1 (IMAGPART_EXPR, inner_type, lhs);
801 update_complex_components (gsi, stmt, r, i);
803 else
805 if (gimple_assign_rhs_code (stmt) != COMPLEX_EXPR)
807 r = extract_component (gsi, rhs, 0, true);
808 i = extract_component (gsi, rhs, 1, true);
810 else
812 r = gimple_assign_rhs1 (stmt);
813 i = gimple_assign_rhs2 (stmt);
815 update_complex_assignment (gsi, r, i);
818 else if (rhs && TREE_CODE (rhs) == SSA_NAME && !TREE_SIDE_EFFECTS (lhs))
820 tree x;
821 gimple t;
823 r = extract_component (gsi, rhs, 0, false);
824 i = extract_component (gsi, rhs, 1, false);
826 x = build1 (REALPART_EXPR, inner_type, unshare_expr (lhs));
827 t = gimple_build_assign (x, r);
828 gsi_insert_before (gsi, t, GSI_SAME_STMT);
830 if (stmt == gsi_stmt (*gsi))
832 x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
833 gimple_assign_set_lhs (stmt, x);
834 gimple_assign_set_rhs1 (stmt, i);
836 else
838 x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
839 t = gimple_build_assign (x, i);
840 gsi_insert_before (gsi, t, GSI_SAME_STMT);
842 stmt = gsi_stmt (*gsi);
843 gcc_assert (gimple_code (stmt) == GIMPLE_RETURN);
844 gimple_return_set_retval (stmt, lhs);
847 update_stmt (stmt);
851 /* Expand complex addition to scalars:
852 a + b = (ar + br) + i(ai + bi)
853 a - b = (ar - br) + i(ai + bi)
856 static void
857 expand_complex_addition (gimple_stmt_iterator *gsi, tree inner_type,
858 tree ar, tree ai, tree br, tree bi,
859 enum tree_code code,
860 complex_lattice_t al, complex_lattice_t bl)
862 tree rr, ri;
864 switch (PAIR (al, bl))
866 case PAIR (ONLY_REAL, ONLY_REAL):
867 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
868 ri = ai;
869 break;
871 case PAIR (ONLY_REAL, ONLY_IMAG):
872 rr = ar;
873 if (code == MINUS_EXPR)
874 ri = gimplify_build2 (gsi, MINUS_EXPR, inner_type, ai, bi);
875 else
876 ri = bi;
877 break;
879 case PAIR (ONLY_IMAG, ONLY_REAL):
880 if (code == MINUS_EXPR)
881 rr = gimplify_build2 (gsi, MINUS_EXPR, inner_type, ar, br);
882 else
883 rr = br;
884 ri = ai;
885 break;
887 case PAIR (ONLY_IMAG, ONLY_IMAG):
888 rr = ar;
889 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
890 break;
892 case PAIR (VARYING, ONLY_REAL):
893 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
894 ri = ai;
895 break;
897 case PAIR (VARYING, ONLY_IMAG):
898 rr = ar;
899 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
900 break;
902 case PAIR (ONLY_REAL, VARYING):
903 if (code == MINUS_EXPR)
904 goto general;
905 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
906 ri = bi;
907 break;
909 case PAIR (ONLY_IMAG, VARYING):
910 if (code == MINUS_EXPR)
911 goto general;
912 rr = br;
913 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
914 break;
916 case PAIR (VARYING, VARYING):
917 general:
918 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
919 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
920 break;
922 default:
923 gcc_unreachable ();
926 update_complex_assignment (gsi, rr, ri);
929 /* Expand a complex multiplication or division to a libcall to the c99
930 compliant routines. */
932 static void
933 expand_complex_libcall (gimple_stmt_iterator *gsi, tree ar, tree ai,
934 tree br, tree bi, enum tree_code code)
936 enum machine_mode mode;
937 enum built_in_function bcode;
938 tree fn, type, lhs;
939 gimple old_stmt, stmt;
941 old_stmt = gsi_stmt (*gsi);
942 lhs = gimple_assign_lhs (old_stmt);
943 type = TREE_TYPE (lhs);
945 mode = TYPE_MODE (type);
946 gcc_assert (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT);
948 if (code == MULT_EXPR)
949 bcode = ((enum built_in_function)
950 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
951 else if (code == RDIV_EXPR)
952 bcode = ((enum built_in_function)
953 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
954 else
955 gcc_unreachable ();
956 fn = builtin_decl_explicit (bcode);
958 stmt = gimple_build_call (fn, 4, ar, ai, br, bi);
959 gimple_call_set_lhs (stmt, lhs);
960 update_stmt (stmt);
961 gsi_replace (gsi, stmt, false);
963 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
964 gimple_purge_dead_eh_edges (gsi_bb (*gsi));
966 if (gimple_in_ssa_p (cfun))
968 type = TREE_TYPE (type);
969 update_complex_components (gsi, stmt,
970 build1 (REALPART_EXPR, type, lhs),
971 build1 (IMAGPART_EXPR, type, lhs));
972 SSA_NAME_DEF_STMT (lhs) = stmt;
976 /* Expand complex multiplication to scalars:
977 a * b = (ar*br - ai*bi) + i(ar*bi + br*ai)
980 static void
981 expand_complex_multiplication (gimple_stmt_iterator *gsi, tree inner_type,
982 tree ar, tree ai, tree br, tree bi,
983 complex_lattice_t al, complex_lattice_t bl)
985 tree rr, ri;
987 if (al < bl)
989 complex_lattice_t tl;
990 rr = ar, ar = br, br = rr;
991 ri = ai, ai = bi, bi = ri;
992 tl = al, al = bl, bl = tl;
995 switch (PAIR (al, bl))
997 case PAIR (ONLY_REAL, ONLY_REAL):
998 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
999 ri = ai;
1000 break;
1002 case PAIR (ONLY_IMAG, ONLY_REAL):
1003 rr = ar;
1004 if (TREE_CODE (ai) == REAL_CST
1005 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (ai), dconst1))
1006 ri = br;
1007 else
1008 ri = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1009 break;
1011 case PAIR (ONLY_IMAG, ONLY_IMAG):
1012 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1013 rr = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, rr);
1014 ri = ar;
1015 break;
1017 case PAIR (VARYING, ONLY_REAL):
1018 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1019 ri = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1020 break;
1022 case PAIR (VARYING, ONLY_IMAG):
1023 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1024 rr = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, rr);
1025 ri = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, bi);
1026 break;
1028 case PAIR (VARYING, VARYING):
1029 if (flag_complex_method == 2 && SCALAR_FLOAT_TYPE_P (inner_type))
1031 expand_complex_libcall (gsi, ar, ai, br, bi, MULT_EXPR);
1032 return;
1034 else
1036 tree t1, t2, t3, t4;
1038 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1039 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1040 t3 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, bi);
1042 /* Avoid expanding redundant multiplication for the common
1043 case of squaring a complex number. */
1044 if (ar == br && ai == bi)
1045 t4 = t3;
1046 else
1047 t4 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1049 rr = gimplify_build2 (gsi, MINUS_EXPR, inner_type, t1, t2);
1050 ri = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t3, t4);
1052 break;
1054 default:
1055 gcc_unreachable ();
1058 update_complex_assignment (gsi, rr, ri);
1061 /* Keep this algorithm in sync with fold-const.c:const_binop().
1063 Expand complex division to scalars, straightforward algorithm.
1064 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1065 t = br*br + bi*bi
1068 static void
1069 expand_complex_div_straight (gimple_stmt_iterator *gsi, tree inner_type,
1070 tree ar, tree ai, tree br, tree bi,
1071 enum tree_code code)
1073 tree rr, ri, div, t1, t2, t3;
1075 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, br, br);
1076 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, bi, bi);
1077 div = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, t2);
1079 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1080 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1081 t3 = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, t2);
1082 rr = gimplify_build2 (gsi, code, inner_type, t3, div);
1084 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1085 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, bi);
1086 t3 = gimplify_build2 (gsi, MINUS_EXPR, inner_type, t1, t2);
1087 ri = gimplify_build2 (gsi, code, inner_type, t3, div);
1089 update_complex_assignment (gsi, rr, ri);
1092 /* Keep this algorithm in sync with fold-const.c:const_binop().
1094 Expand complex division to scalars, modified algorithm to minimize
1095 overflow with wide input ranges. */
1097 static void
1098 expand_complex_div_wide (gimple_stmt_iterator *gsi, tree inner_type,
1099 tree ar, tree ai, tree br, tree bi,
1100 enum tree_code code)
1102 tree rr, ri, ratio, div, t1, t2, tr, ti, compare;
1103 basic_block bb_cond, bb_true, bb_false, bb_join;
1104 gimple stmt;
1106 /* Examine |br| < |bi|, and branch. */
1107 t1 = gimplify_build1 (gsi, ABS_EXPR, inner_type, br);
1108 t2 = gimplify_build1 (gsi, ABS_EXPR, inner_type, bi);
1109 compare = fold_build2_loc (gimple_location (gsi_stmt (*gsi)),
1110 LT_EXPR, boolean_type_node, t1, t2);
1111 STRIP_NOPS (compare);
1113 bb_cond = bb_true = bb_false = bb_join = NULL;
1114 rr = ri = tr = ti = NULL;
1115 if (TREE_CODE (compare) != INTEGER_CST)
1117 edge e;
1118 gimple stmt;
1119 tree cond, tmp;
1121 tmp = create_tmp_var (boolean_type_node, NULL);
1122 stmt = gimple_build_assign (tmp, compare);
1123 if (gimple_in_ssa_p (cfun))
1125 tmp = make_ssa_name (tmp, stmt);
1126 gimple_assign_set_lhs (stmt, tmp);
1129 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1131 cond = fold_build2_loc (gimple_location (stmt),
1132 EQ_EXPR, boolean_type_node, tmp, boolean_true_node);
1133 stmt = gimple_build_cond_from_tree (cond, NULL_TREE, NULL_TREE);
1134 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1136 /* Split the original block, and create the TRUE and FALSE blocks. */
1137 e = split_block (gsi_bb (*gsi), stmt);
1138 bb_cond = e->src;
1139 bb_join = e->dest;
1140 bb_true = create_empty_bb (bb_cond);
1141 bb_false = create_empty_bb (bb_true);
1143 /* Wire the blocks together. */
1144 e->flags = EDGE_TRUE_VALUE;
1145 redirect_edge_succ (e, bb_true);
1146 make_edge (bb_cond, bb_false, EDGE_FALSE_VALUE);
1147 make_edge (bb_true, bb_join, EDGE_FALLTHRU);
1148 make_edge (bb_false, bb_join, EDGE_FALLTHRU);
1149 if (current_loops)
1151 add_bb_to_loop (bb_true, bb_cond->loop_father);
1152 add_bb_to_loop (bb_false, bb_cond->loop_father);
1155 /* Update dominance info. Note that bb_join's data was
1156 updated by split_block. */
1157 if (dom_info_available_p (CDI_DOMINATORS))
1159 set_immediate_dominator (CDI_DOMINATORS, bb_true, bb_cond);
1160 set_immediate_dominator (CDI_DOMINATORS, bb_false, bb_cond);
1163 rr = create_tmp_reg (inner_type, NULL);
1164 ri = create_tmp_reg (inner_type, NULL);
1167 /* In the TRUE branch, we compute
1168 ratio = br/bi;
1169 div = (br * ratio) + bi;
1170 tr = (ar * ratio) + ai;
1171 ti = (ai * ratio) - ar;
1172 tr = tr / div;
1173 ti = ti / div; */
1174 if (bb_true || integer_nonzerop (compare))
1176 if (bb_true)
1178 *gsi = gsi_last_bb (bb_true);
1179 gsi_insert_after (gsi, gimple_build_nop (), GSI_NEW_STMT);
1182 ratio = gimplify_build2 (gsi, code, inner_type, br, bi);
1184 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, br, ratio);
1185 div = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, bi);
1187 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, ratio);
1188 tr = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, ai);
1190 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, ratio);
1191 ti = gimplify_build2 (gsi, MINUS_EXPR, inner_type, t1, ar);
1193 tr = gimplify_build2 (gsi, code, inner_type, tr, div);
1194 ti = gimplify_build2 (gsi, code, inner_type, ti, div);
1196 if (bb_true)
1198 stmt = gimple_build_assign (rr, tr);
1199 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1200 stmt = gimple_build_assign (ri, ti);
1201 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1202 gsi_remove (gsi, true);
1206 /* In the FALSE branch, we compute
1207 ratio = d/c;
1208 divisor = (d * ratio) + c;
1209 tr = (b * ratio) + a;
1210 ti = b - (a * ratio);
1211 tr = tr / div;
1212 ti = ti / div; */
1213 if (bb_false || integer_zerop (compare))
1215 if (bb_false)
1217 *gsi = gsi_last_bb (bb_false);
1218 gsi_insert_after (gsi, gimple_build_nop (), GSI_NEW_STMT);
1221 ratio = gimplify_build2 (gsi, code, inner_type, bi, br);
1223 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, bi, ratio);
1224 div = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, br);
1226 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, ratio);
1227 tr = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, ar);
1229 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, ratio);
1230 ti = gimplify_build2 (gsi, MINUS_EXPR, inner_type, ai, t1);
1232 tr = gimplify_build2 (gsi, code, inner_type, tr, div);
1233 ti = gimplify_build2 (gsi, code, inner_type, ti, div);
1235 if (bb_false)
1237 stmt = gimple_build_assign (rr, tr);
1238 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1239 stmt = gimple_build_assign (ri, ti);
1240 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1241 gsi_remove (gsi, true);
1245 if (bb_join)
1246 *gsi = gsi_start_bb (bb_join);
1247 else
1248 rr = tr, ri = ti;
1250 update_complex_assignment (gsi, rr, ri);
1253 /* Expand complex division to scalars. */
1255 static void
1256 expand_complex_division (gimple_stmt_iterator *gsi, tree inner_type,
1257 tree ar, tree ai, tree br, tree bi,
1258 enum tree_code code,
1259 complex_lattice_t al, complex_lattice_t bl)
1261 tree rr, ri;
1263 switch (PAIR (al, bl))
1265 case PAIR (ONLY_REAL, ONLY_REAL):
1266 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
1267 ri = ai;
1268 break;
1270 case PAIR (ONLY_REAL, ONLY_IMAG):
1271 rr = ai;
1272 ri = gimplify_build2 (gsi, code, inner_type, ar, bi);
1273 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ri);
1274 break;
1276 case PAIR (ONLY_IMAG, ONLY_REAL):
1277 rr = ar;
1278 ri = gimplify_build2 (gsi, code, inner_type, ai, br);
1279 break;
1281 case PAIR (ONLY_IMAG, ONLY_IMAG):
1282 rr = gimplify_build2 (gsi, code, inner_type, ai, bi);
1283 ri = ar;
1284 break;
1286 case PAIR (VARYING, ONLY_REAL):
1287 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
1288 ri = gimplify_build2 (gsi, code, inner_type, ai, br);
1289 break;
1291 case PAIR (VARYING, ONLY_IMAG):
1292 rr = gimplify_build2 (gsi, code, inner_type, ai, bi);
1293 ri = gimplify_build2 (gsi, code, inner_type, ar, bi);
1294 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ri);
1296 case PAIR (ONLY_REAL, VARYING):
1297 case PAIR (ONLY_IMAG, VARYING):
1298 case PAIR (VARYING, VARYING):
1299 switch (flag_complex_method)
1301 case 0:
1302 /* straightforward implementation of complex divide acceptable. */
1303 expand_complex_div_straight (gsi, inner_type, ar, ai, br, bi, code);
1304 break;
1306 case 2:
1307 if (SCALAR_FLOAT_TYPE_P (inner_type))
1309 expand_complex_libcall (gsi, ar, ai, br, bi, code);
1310 break;
1312 /* FALLTHRU */
1314 case 1:
1315 /* wide ranges of inputs must work for complex divide. */
1316 expand_complex_div_wide (gsi, inner_type, ar, ai, br, bi, code);
1317 break;
1319 default:
1320 gcc_unreachable ();
1322 return;
1324 default:
1325 gcc_unreachable ();
1328 update_complex_assignment (gsi, rr, ri);
1331 /* Expand complex negation to scalars:
1332 -a = (-ar) + i(-ai)
1335 static void
1336 expand_complex_negation (gimple_stmt_iterator *gsi, tree inner_type,
1337 tree ar, tree ai)
1339 tree rr, ri;
1341 rr = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ar);
1342 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ai);
1344 update_complex_assignment (gsi, rr, ri);
1347 /* Expand complex conjugate to scalars:
1348 ~a = (ar) + i(-ai)
1351 static void
1352 expand_complex_conjugate (gimple_stmt_iterator *gsi, tree inner_type,
1353 tree ar, tree ai)
1355 tree ri;
1357 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ai);
1359 update_complex_assignment (gsi, ar, ri);
1362 /* Expand complex comparison (EQ or NE only). */
1364 static void
1365 expand_complex_comparison (gimple_stmt_iterator *gsi, tree ar, tree ai,
1366 tree br, tree bi, enum tree_code code)
1368 tree cr, ci, cc, type;
1369 gimple stmt;
1371 cr = gimplify_build2 (gsi, code, boolean_type_node, ar, br);
1372 ci = gimplify_build2 (gsi, code, boolean_type_node, ai, bi);
1373 cc = gimplify_build2 (gsi,
1374 (code == EQ_EXPR ? TRUTH_AND_EXPR : TRUTH_OR_EXPR),
1375 boolean_type_node, cr, ci);
1377 stmt = gsi_stmt (*gsi);
1379 switch (gimple_code (stmt))
1381 case GIMPLE_RETURN:
1382 type = TREE_TYPE (gimple_return_retval (stmt));
1383 gimple_return_set_retval (stmt, fold_convert (type, cc));
1384 break;
1386 case GIMPLE_ASSIGN:
1387 type = TREE_TYPE (gimple_assign_lhs (stmt));
1388 gimple_assign_set_rhs_from_tree (gsi, fold_convert (type, cc));
1389 stmt = gsi_stmt (*gsi);
1390 break;
1392 case GIMPLE_COND:
1393 gimple_cond_set_code (stmt, EQ_EXPR);
1394 gimple_cond_set_lhs (stmt, cc);
1395 gimple_cond_set_rhs (stmt, boolean_true_node);
1396 break;
1398 default:
1399 gcc_unreachable ();
1402 update_stmt (stmt);
1405 /* Expand inline asm that sets some complex SSA_NAMEs. */
1407 static void
1408 expand_complex_asm (gimple_stmt_iterator *gsi)
1410 gimple stmt = gsi_stmt (*gsi);
1411 unsigned int i;
1413 for (i = 0; i < gimple_asm_noutputs (stmt); ++i)
1415 tree link = gimple_asm_output_op (stmt, i);
1416 tree op = TREE_VALUE (link);
1417 if (TREE_CODE (op) == SSA_NAME
1418 && TREE_CODE (TREE_TYPE (op)) == COMPLEX_TYPE)
1420 tree type = TREE_TYPE (op);
1421 tree inner_type = TREE_TYPE (type);
1422 tree r = build1 (REALPART_EXPR, inner_type, op);
1423 tree i = build1 (IMAGPART_EXPR, inner_type, op);
1424 gimple_seq list = set_component_ssa_name (op, false, r);
1426 if (list)
1427 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
1429 list = set_component_ssa_name (op, true, i);
1430 if (list)
1431 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
1436 /* Process one statement. If we identify a complex operation, expand it. */
1438 static void
1439 expand_complex_operations_1 (gimple_stmt_iterator *gsi)
1441 gimple stmt = gsi_stmt (*gsi);
1442 tree type, inner_type, lhs;
1443 tree ac, ar, ai, bc, br, bi;
1444 complex_lattice_t al, bl;
1445 enum tree_code code;
1447 if (gimple_code (stmt) == GIMPLE_ASM)
1449 expand_complex_asm (gsi);
1450 return;
1453 lhs = gimple_get_lhs (stmt);
1454 if (!lhs && gimple_code (stmt) != GIMPLE_COND)
1455 return;
1457 type = TREE_TYPE (gimple_op (stmt, 0));
1458 code = gimple_expr_code (stmt);
1460 /* Initial filter for operations we handle. */
1461 switch (code)
1463 case PLUS_EXPR:
1464 case MINUS_EXPR:
1465 case MULT_EXPR:
1466 case TRUNC_DIV_EXPR:
1467 case CEIL_DIV_EXPR:
1468 case FLOOR_DIV_EXPR:
1469 case ROUND_DIV_EXPR:
1470 case RDIV_EXPR:
1471 case NEGATE_EXPR:
1472 case CONJ_EXPR:
1473 if (TREE_CODE (type) != COMPLEX_TYPE)
1474 return;
1475 inner_type = TREE_TYPE (type);
1476 break;
1478 case EQ_EXPR:
1479 case NE_EXPR:
1480 /* Note, both GIMPLE_ASSIGN and GIMPLE_COND may have an EQ_EXPR
1481 subcode, so we need to access the operands using gimple_op. */
1482 inner_type = TREE_TYPE (gimple_op (stmt, 1));
1483 if (TREE_CODE (inner_type) != COMPLEX_TYPE)
1484 return;
1485 break;
1487 default:
1489 tree rhs;
1491 /* GIMPLE_COND may also fallthru here, but we do not need to
1492 do anything with it. */
1493 if (gimple_code (stmt) == GIMPLE_COND)
1494 return;
1496 if (TREE_CODE (type) == COMPLEX_TYPE)
1497 expand_complex_move (gsi, type);
1498 else if (is_gimple_assign (stmt)
1499 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
1500 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
1501 && TREE_CODE (lhs) == SSA_NAME)
1503 rhs = gimple_assign_rhs1 (stmt);
1504 rhs = extract_component (gsi, TREE_OPERAND (rhs, 0),
1505 gimple_assign_rhs_code (stmt)
1506 == IMAGPART_EXPR,
1507 false);
1508 gimple_assign_set_rhs_from_tree (gsi, rhs);
1509 stmt = gsi_stmt (*gsi);
1510 update_stmt (stmt);
1513 return;
1516 /* Extract the components of the two complex values. Make sure and
1517 handle the common case of the same value used twice specially. */
1518 if (is_gimple_assign (stmt))
1520 ac = gimple_assign_rhs1 (stmt);
1521 bc = (gimple_num_ops (stmt) > 2) ? gimple_assign_rhs2 (stmt) : NULL;
1523 /* GIMPLE_CALL can not get here. */
1524 else
1526 ac = gimple_cond_lhs (stmt);
1527 bc = gimple_cond_rhs (stmt);
1530 ar = extract_component (gsi, ac, false, true);
1531 ai = extract_component (gsi, ac, true, true);
1533 if (ac == bc)
1534 br = ar, bi = ai;
1535 else if (bc)
1537 br = extract_component (gsi, bc, 0, true);
1538 bi = extract_component (gsi, bc, 1, true);
1540 else
1541 br = bi = NULL_TREE;
1543 if (gimple_in_ssa_p (cfun))
1545 al = find_lattice_value (ac);
1546 if (al == UNINITIALIZED)
1547 al = VARYING;
1549 if (TREE_CODE_CLASS (code) == tcc_unary)
1550 bl = UNINITIALIZED;
1551 else if (ac == bc)
1552 bl = al;
1553 else
1555 bl = find_lattice_value (bc);
1556 if (bl == UNINITIALIZED)
1557 bl = VARYING;
1560 else
1561 al = bl = VARYING;
1563 switch (code)
1565 case PLUS_EXPR:
1566 case MINUS_EXPR:
1567 expand_complex_addition (gsi, inner_type, ar, ai, br, bi, code, al, bl);
1568 break;
1570 case MULT_EXPR:
1571 expand_complex_multiplication (gsi, inner_type, ar, ai, br, bi, al, bl);
1572 break;
1574 case TRUNC_DIV_EXPR:
1575 case CEIL_DIV_EXPR:
1576 case FLOOR_DIV_EXPR:
1577 case ROUND_DIV_EXPR:
1578 case RDIV_EXPR:
1579 expand_complex_division (gsi, inner_type, ar, ai, br, bi, code, al, bl);
1580 break;
1582 case NEGATE_EXPR:
1583 expand_complex_negation (gsi, inner_type, ar, ai);
1584 break;
1586 case CONJ_EXPR:
1587 expand_complex_conjugate (gsi, inner_type, ar, ai);
1588 break;
1590 case EQ_EXPR:
1591 case NE_EXPR:
1592 expand_complex_comparison (gsi, ar, ai, br, bi, code);
1593 break;
1595 default:
1596 gcc_unreachable ();
1601 /* Entry point for complex operation lowering during optimization. */
1603 static unsigned int
1604 tree_lower_complex (void)
1606 int old_last_basic_block;
1607 gimple_stmt_iterator gsi;
1608 basic_block bb;
1610 if (!init_dont_simulate_again ())
1611 return 0;
1613 complex_lattice_values.create (num_ssa_names);
1614 complex_lattice_values.safe_grow_cleared (num_ssa_names);
1616 init_parameter_lattice_values ();
1617 ssa_propagate (complex_visit_stmt, complex_visit_phi);
1619 complex_variable_components.create (10);
1621 complex_ssa_name_components.create (2 * num_ssa_names);
1622 complex_ssa_name_components.safe_grow_cleared (2 * num_ssa_names);
1624 update_parameter_components ();
1626 /* ??? Ideally we'd traverse the blocks in breadth-first order. */
1627 old_last_basic_block = last_basic_block;
1628 FOR_EACH_BB (bb)
1630 if (bb->index >= old_last_basic_block)
1631 continue;
1633 update_phi_components (bb);
1634 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1635 expand_complex_operations_1 (&gsi);
1638 gsi_commit_edge_inserts ();
1640 complex_variable_components.dispose ();
1641 complex_ssa_name_components.release ();
1642 complex_lattice_values.release ();
1643 return 0;
1646 namespace {
1648 const pass_data pass_data_lower_complex =
1650 GIMPLE_PASS, /* type */
1651 "cplxlower", /* name */
1652 OPTGROUP_NONE, /* optinfo_flags */
1653 false, /* has_gate */
1654 true, /* has_execute */
1655 TV_NONE, /* tv_id */
1656 PROP_ssa, /* properties_required */
1657 PROP_gimple_lcx, /* properties_provided */
1658 0, /* properties_destroyed */
1659 0, /* todo_flags_start */
1660 ( TODO_update_ssa | TODO_verify_stmts ), /* todo_flags_finish */
1663 class pass_lower_complex : public gimple_opt_pass
1665 public:
1666 pass_lower_complex (gcc::context *ctxt)
1667 : gimple_opt_pass (pass_data_lower_complex, ctxt)
1670 /* opt_pass methods: */
1671 opt_pass * clone () { return new pass_lower_complex (m_ctxt); }
1672 unsigned int execute () { return tree_lower_complex (); }
1674 }; // class pass_lower_complex
1676 } // anon namespace
1678 gimple_opt_pass *
1679 make_pass_lower_complex (gcc::context *ctxt)
1681 return new pass_lower_complex (ctxt);
1685 static bool
1686 gate_no_optimization (void)
1688 /* With errors, normal optimization passes are not run. If we don't
1689 lower complex operations at all, rtl expansion will abort. */
1690 return !(cfun->curr_properties & PROP_gimple_lcx);
1693 namespace {
1695 const pass_data pass_data_lower_complex_O0 =
1697 GIMPLE_PASS, /* type */
1698 "cplxlower0", /* name */
1699 OPTGROUP_NONE, /* optinfo_flags */
1700 true, /* has_gate */
1701 true, /* has_execute */
1702 TV_NONE, /* tv_id */
1703 PROP_cfg, /* properties_required */
1704 PROP_gimple_lcx, /* properties_provided */
1705 0, /* properties_destroyed */
1706 0, /* todo_flags_start */
1707 ( TODO_update_ssa | TODO_verify_stmts ), /* todo_flags_finish */
1710 class pass_lower_complex_O0 : public gimple_opt_pass
1712 public:
1713 pass_lower_complex_O0 (gcc::context *ctxt)
1714 : gimple_opt_pass (pass_data_lower_complex_O0, ctxt)
1717 /* opt_pass methods: */
1718 bool gate () { return gate_no_optimization (); }
1719 unsigned int execute () { return tree_lower_complex (); }
1721 }; // class pass_lower_complex_O0
1723 } // anon namespace
1725 gimple_opt_pass *
1726 make_pass_lower_complex_O0 (gcc::context *ctxt)
1728 return new pass_lower_complex_O0 (ctxt);