2014-07-29 Ed Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / gcc / tree-complex.c
blobf1bba8bff8ad3860cb7868d3b10b7028ef422a2b
1 /* Lower complex number operations to scalar operations.
2 Copyright (C) 2004-2014 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 "stor-layout.h"
26 #include "flags.h"
27 #include "basic-block.h"
28 #include "tree-ssa-alias.h"
29 #include "internal-fn.h"
30 #include "tree-eh.h"
31 #include "gimple-expr.h"
32 #include "is-a.h"
33 #include "gimple.h"
34 #include "gimplify.h"
35 #include "gimple-iterator.h"
36 #include "gimplify-me.h"
37 #include "gimple-ssa.h"
38 #include "tree-cfg.h"
39 #include "tree-phinodes.h"
40 #include "ssa-iterators.h"
41 #include "stringpool.h"
42 #include "tree-ssanames.h"
43 #include "expr.h"
44 #include "tree-dfa.h"
45 #include "tree-ssa.h"
46 #include "tree-iterator.h"
47 #include "tree-pass.h"
48 #include "tree-ssa-propagate.h"
49 #include "tree-hasher.h"
50 #include "cfgloop.h"
53 /* For each complex ssa name, a lattice value. We're interested in finding
54 out whether a complex number is degenerate in some way, having only real
55 or only complex parts. */
57 enum
59 UNINITIALIZED = 0,
60 ONLY_REAL = 1,
61 ONLY_IMAG = 2,
62 VARYING = 3
65 /* The type complex_lattice_t holds combinations of the above
66 constants. */
67 typedef int complex_lattice_t;
69 #define PAIR(a, b) ((a) << 2 | (b))
72 static vec<complex_lattice_t> complex_lattice_values;
74 /* For each complex variable, a pair of variables for the components exists in
75 the hashtable. */
76 static int_tree_htab_type *complex_variable_components;
78 /* For each complex SSA_NAME, a pair of ssa names for the components. */
79 static vec<tree> complex_ssa_name_components;
81 /* Lookup UID in the complex_variable_components hashtable and return the
82 associated tree. */
83 static tree
84 cvc_lookup (unsigned int uid)
86 struct int_tree_map in;
87 in.uid = uid;
88 return complex_variable_components->find_with_hash (in, uid).to;
91 /* Insert the pair UID, TO into the complex_variable_components hashtable. */
93 static void
94 cvc_insert (unsigned int uid, tree to)
96 int_tree_map h;
97 int_tree_map *loc;
99 h.uid = uid;
100 loc = complex_variable_components->find_slot_with_hash (h, uid, INSERT);
101 loc->uid = uid;
102 loc->to = to;
105 /* Return true if T is not a zero constant. In the case of real values,
106 we're only interested in +0.0. */
108 static int
109 some_nonzerop (tree t)
111 int zerop = false;
113 /* Operations with real or imaginary part of a complex number zero
114 cannot be treated the same as operations with a real or imaginary
115 operand if we care about the signs of zeros in the result. */
116 if (TREE_CODE (t) == REAL_CST && !flag_signed_zeros)
117 zerop = REAL_VALUES_IDENTICAL (TREE_REAL_CST (t), dconst0);
118 else if (TREE_CODE (t) == FIXED_CST)
119 zerop = fixed_zerop (t);
120 else if (TREE_CODE (t) == INTEGER_CST)
121 zerop = integer_zerop (t);
123 return !zerop;
127 /* Compute a lattice value from the components of a complex type REAL
128 and IMAG. */
130 static complex_lattice_t
131 find_lattice_value_parts (tree real, tree imag)
133 int r, i;
134 complex_lattice_t ret;
136 r = some_nonzerop (real);
137 i = some_nonzerop (imag);
138 ret = r * ONLY_REAL + i * ONLY_IMAG;
140 /* ??? On occasion we could do better than mapping 0+0i to real, but we
141 certainly don't want to leave it UNINITIALIZED, which eventually gets
142 mapped to VARYING. */
143 if (ret == UNINITIALIZED)
144 ret = ONLY_REAL;
146 return ret;
150 /* Compute a lattice value from gimple_val T. */
152 static complex_lattice_t
153 find_lattice_value (tree t)
155 tree real, imag;
157 switch (TREE_CODE (t))
159 case SSA_NAME:
160 return complex_lattice_values[SSA_NAME_VERSION (t)];
162 case COMPLEX_CST:
163 real = TREE_REALPART (t);
164 imag = TREE_IMAGPART (t);
165 break;
167 default:
168 gcc_unreachable ();
171 return find_lattice_value_parts (real, imag);
174 /* Determine if LHS is something for which we're interested in seeing
175 simulation results. */
177 static bool
178 is_complex_reg (tree lhs)
180 return TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE && is_gimple_reg (lhs);
183 /* Mark the incoming parameters to the function as VARYING. */
185 static void
186 init_parameter_lattice_values (void)
188 tree parm, ssa_name;
190 for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = DECL_CHAIN (parm))
191 if (is_complex_reg (parm)
192 && (ssa_name = ssa_default_def (cfun, parm)) != NULL_TREE)
193 complex_lattice_values[SSA_NAME_VERSION (ssa_name)] = VARYING;
196 /* Initialize simulation state for each statement. Return false if we
197 found no statements we want to simulate, and thus there's nothing
198 for the entire pass to do. */
200 static bool
201 init_dont_simulate_again (void)
203 basic_block bb;
204 gimple_stmt_iterator gsi;
205 gimple phi;
206 bool saw_a_complex_op = false;
208 FOR_EACH_BB_FN (bb, cfun)
210 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
212 phi = gsi_stmt (gsi);
213 prop_set_simulate_again (phi,
214 is_complex_reg (gimple_phi_result (phi)));
217 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
219 gimple stmt;
220 tree op0, op1;
221 bool sim_again_p;
223 stmt = gsi_stmt (gsi);
224 op0 = op1 = NULL_TREE;
226 /* Most control-altering statements must be initially
227 simulated, else we won't cover the entire cfg. */
228 sim_again_p = stmt_ends_bb_p (stmt);
230 switch (gimple_code (stmt))
232 case GIMPLE_CALL:
233 if (gimple_call_lhs (stmt))
234 sim_again_p = is_complex_reg (gimple_call_lhs (stmt));
235 break;
237 case GIMPLE_ASSIGN:
238 sim_again_p = is_complex_reg (gimple_assign_lhs (stmt));
239 if (gimple_assign_rhs_code (stmt) == REALPART_EXPR
240 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
241 op0 = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
242 else
243 op0 = gimple_assign_rhs1 (stmt);
244 if (gimple_num_ops (stmt) > 2)
245 op1 = gimple_assign_rhs2 (stmt);
246 break;
248 case GIMPLE_COND:
249 op0 = gimple_cond_lhs (stmt);
250 op1 = gimple_cond_rhs (stmt);
251 break;
253 default:
254 break;
257 if (op0 || op1)
258 switch (gimple_expr_code (stmt))
260 case EQ_EXPR:
261 case NE_EXPR:
262 case PLUS_EXPR:
263 case MINUS_EXPR:
264 case MULT_EXPR:
265 case TRUNC_DIV_EXPR:
266 case CEIL_DIV_EXPR:
267 case FLOOR_DIV_EXPR:
268 case ROUND_DIV_EXPR:
269 case RDIV_EXPR:
270 if (TREE_CODE (TREE_TYPE (op0)) == COMPLEX_TYPE
271 || TREE_CODE (TREE_TYPE (op1)) == COMPLEX_TYPE)
272 saw_a_complex_op = true;
273 break;
275 case NEGATE_EXPR:
276 case CONJ_EXPR:
277 if (TREE_CODE (TREE_TYPE (op0)) == COMPLEX_TYPE)
278 saw_a_complex_op = true;
279 break;
281 case REALPART_EXPR:
282 case IMAGPART_EXPR:
283 /* The total store transformation performed during
284 gimplification creates such uninitialized loads
285 and we need to lower the statement to be able
286 to fix things up. */
287 if (TREE_CODE (op0) == SSA_NAME
288 && ssa_undefined_value_p (op0))
289 saw_a_complex_op = true;
290 break;
292 default:
293 break;
296 prop_set_simulate_again (stmt, sim_again_p);
300 return saw_a_complex_op;
304 /* Evaluate statement STMT against the complex lattice defined above. */
306 static enum ssa_prop_result
307 complex_visit_stmt (gimple stmt, edge *taken_edge_p ATTRIBUTE_UNUSED,
308 tree *result_p)
310 complex_lattice_t new_l, old_l, op1_l, op2_l;
311 unsigned int ver;
312 tree lhs;
314 lhs = gimple_get_lhs (stmt);
315 /* Skip anything but GIMPLE_ASSIGN and GIMPLE_CALL with a lhs. */
316 if (!lhs)
317 return SSA_PROP_VARYING;
319 /* These conditions should be satisfied due to the initial filter
320 set up in init_dont_simulate_again. */
321 gcc_assert (TREE_CODE (lhs) == SSA_NAME);
322 gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE);
324 *result_p = lhs;
325 ver = SSA_NAME_VERSION (lhs);
326 old_l = complex_lattice_values[ver];
328 switch (gimple_expr_code (stmt))
330 case SSA_NAME:
331 case COMPLEX_CST:
332 new_l = find_lattice_value (gimple_assign_rhs1 (stmt));
333 break;
335 case COMPLEX_EXPR:
336 new_l = find_lattice_value_parts (gimple_assign_rhs1 (stmt),
337 gimple_assign_rhs2 (stmt));
338 break;
340 case PLUS_EXPR:
341 case MINUS_EXPR:
342 op1_l = find_lattice_value (gimple_assign_rhs1 (stmt));
343 op2_l = find_lattice_value (gimple_assign_rhs2 (stmt));
345 /* We've set up the lattice values such that IOR neatly
346 models addition. */
347 new_l = op1_l | op2_l;
348 break;
350 case MULT_EXPR:
351 case RDIV_EXPR:
352 case TRUNC_DIV_EXPR:
353 case CEIL_DIV_EXPR:
354 case FLOOR_DIV_EXPR:
355 case ROUND_DIV_EXPR:
356 op1_l = find_lattice_value (gimple_assign_rhs1 (stmt));
357 op2_l = find_lattice_value (gimple_assign_rhs2 (stmt));
359 /* Obviously, if either varies, so does the result. */
360 if (op1_l == VARYING || op2_l == VARYING)
361 new_l = VARYING;
362 /* Don't prematurely promote variables if we've not yet seen
363 their inputs. */
364 else if (op1_l == UNINITIALIZED)
365 new_l = op2_l;
366 else if (op2_l == UNINITIALIZED)
367 new_l = op1_l;
368 else
370 /* At this point both numbers have only one component. If the
371 numbers are of opposite kind, the result is imaginary,
372 otherwise the result is real. The add/subtract translates
373 the real/imag from/to 0/1; the ^ performs the comparison. */
374 new_l = ((op1_l - ONLY_REAL) ^ (op2_l - ONLY_REAL)) + ONLY_REAL;
376 /* Don't allow the lattice value to flip-flop indefinitely. */
377 new_l |= old_l;
379 break;
381 case NEGATE_EXPR:
382 case CONJ_EXPR:
383 new_l = find_lattice_value (gimple_assign_rhs1 (stmt));
384 break;
386 default:
387 new_l = VARYING;
388 break;
391 /* If nothing changed this round, let the propagator know. */
392 if (new_l == old_l)
393 return SSA_PROP_NOT_INTERESTING;
395 complex_lattice_values[ver] = new_l;
396 return new_l == VARYING ? SSA_PROP_VARYING : SSA_PROP_INTERESTING;
399 /* Evaluate a PHI node against the complex lattice defined above. */
401 static enum ssa_prop_result
402 complex_visit_phi (gimple phi)
404 complex_lattice_t new_l, old_l;
405 unsigned int ver;
406 tree lhs;
407 int i;
409 lhs = gimple_phi_result (phi);
411 /* This condition should be satisfied due to the initial filter
412 set up in init_dont_simulate_again. */
413 gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE);
415 /* We've set up the lattice values such that IOR neatly models PHI meet. */
416 new_l = UNINITIALIZED;
417 for (i = gimple_phi_num_args (phi) - 1; i >= 0; --i)
418 new_l |= find_lattice_value (gimple_phi_arg_def (phi, i));
420 ver = SSA_NAME_VERSION (lhs);
421 old_l = complex_lattice_values[ver];
423 if (new_l == old_l)
424 return SSA_PROP_NOT_INTERESTING;
426 complex_lattice_values[ver] = new_l;
427 return new_l == VARYING ? SSA_PROP_VARYING : SSA_PROP_INTERESTING;
430 /* Create one backing variable for a complex component of ORIG. */
432 static tree
433 create_one_component_var (tree type, tree orig, const char *prefix,
434 const char *suffix, enum tree_code code)
436 tree r = create_tmp_var (type, prefix);
438 DECL_SOURCE_LOCATION (r) = DECL_SOURCE_LOCATION (orig);
439 DECL_ARTIFICIAL (r) = 1;
441 if (DECL_NAME (orig) && !DECL_IGNORED_P (orig))
443 const char *name = IDENTIFIER_POINTER (DECL_NAME (orig));
445 DECL_NAME (r) = get_identifier (ACONCAT ((name, suffix, NULL)));
447 SET_DECL_DEBUG_EXPR (r, build1 (code, type, orig));
448 DECL_HAS_DEBUG_EXPR_P (r) = 1;
449 DECL_IGNORED_P (r) = 0;
450 TREE_NO_WARNING (r) = TREE_NO_WARNING (orig);
452 else
454 DECL_IGNORED_P (r) = 1;
455 TREE_NO_WARNING (r) = 1;
458 return r;
461 /* Retrieve a value for a complex component of VAR. */
463 static tree
464 get_component_var (tree var, bool imag_p)
466 size_t decl_index = DECL_UID (var) * 2 + imag_p;
467 tree ret = cvc_lookup (decl_index);
469 if (ret == NULL)
471 ret = create_one_component_var (TREE_TYPE (TREE_TYPE (var)), var,
472 imag_p ? "CI" : "CR",
473 imag_p ? "$imag" : "$real",
474 imag_p ? IMAGPART_EXPR : REALPART_EXPR);
475 cvc_insert (decl_index, ret);
478 return ret;
481 /* Retrieve a value for a complex component of SSA_NAME. */
483 static tree
484 get_component_ssa_name (tree ssa_name, bool imag_p)
486 complex_lattice_t lattice = find_lattice_value (ssa_name);
487 size_t ssa_name_index;
488 tree ret;
490 if (lattice == (imag_p ? ONLY_REAL : ONLY_IMAG))
492 tree inner_type = TREE_TYPE (TREE_TYPE (ssa_name));
493 if (SCALAR_FLOAT_TYPE_P (inner_type))
494 return build_real (inner_type, dconst0);
495 else
496 return build_int_cst (inner_type, 0);
499 ssa_name_index = SSA_NAME_VERSION (ssa_name) * 2 + imag_p;
500 ret = complex_ssa_name_components[ssa_name_index];
501 if (ret == NULL)
503 if (SSA_NAME_VAR (ssa_name))
504 ret = get_component_var (SSA_NAME_VAR (ssa_name), imag_p);
505 else
506 ret = TREE_TYPE (TREE_TYPE (ssa_name));
507 ret = make_ssa_name (ret, NULL);
509 /* Copy some properties from the original. In particular, whether it
510 is used in an abnormal phi, and whether it's uninitialized. */
511 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ret)
512 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name);
513 if (SSA_NAME_IS_DEFAULT_DEF (ssa_name)
514 && TREE_CODE (SSA_NAME_VAR (ssa_name)) == VAR_DECL)
516 SSA_NAME_DEF_STMT (ret) = SSA_NAME_DEF_STMT (ssa_name);
517 set_ssa_default_def (cfun, SSA_NAME_VAR (ret), ret);
520 complex_ssa_name_components[ssa_name_index] = ret;
523 return ret;
526 /* Set a value for a complex component of SSA_NAME, return a
527 gimple_seq of stuff that needs doing. */
529 static gimple_seq
530 set_component_ssa_name (tree ssa_name, bool imag_p, tree value)
532 complex_lattice_t lattice = find_lattice_value (ssa_name);
533 size_t ssa_name_index;
534 tree comp;
535 gimple last;
536 gimple_seq list;
538 /* We know the value must be zero, else there's a bug in our lattice
539 analysis. But the value may well be a variable known to contain
540 zero. We should be safe ignoring it. */
541 if (lattice == (imag_p ? ONLY_REAL : ONLY_IMAG))
542 return NULL;
544 /* If we've already assigned an SSA_NAME to this component, then this
545 means that our walk of the basic blocks found a use before the set.
546 This is fine. Now we should create an initialization for the value
547 we created earlier. */
548 ssa_name_index = SSA_NAME_VERSION (ssa_name) * 2 + imag_p;
549 comp = complex_ssa_name_components[ssa_name_index];
550 if (comp)
553 /* If we've nothing assigned, and the value we're given is already stable,
554 then install that as the value for this SSA_NAME. This preemptively
555 copy-propagates the value, which avoids unnecessary memory allocation. */
556 else if (is_gimple_min_invariant (value)
557 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name))
559 complex_ssa_name_components[ssa_name_index] = value;
560 return NULL;
562 else if (TREE_CODE (value) == SSA_NAME
563 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name))
565 /* Replace an anonymous base value with the variable from cvc_lookup.
566 This should result in better debug info. */
567 if (SSA_NAME_VAR (ssa_name)
568 && (!SSA_NAME_VAR (value) || DECL_IGNORED_P (SSA_NAME_VAR (value)))
569 && !DECL_IGNORED_P (SSA_NAME_VAR (ssa_name)))
571 comp = get_component_var (SSA_NAME_VAR (ssa_name), imag_p);
572 replace_ssa_name_symbol (value, comp);
575 complex_ssa_name_components[ssa_name_index] = value;
576 return NULL;
579 /* Finally, we need to stabilize the result by installing the value into
580 a new ssa name. */
581 else
582 comp = get_component_ssa_name (ssa_name, imag_p);
584 /* Do all the work to assign VALUE to COMP. */
585 list = NULL;
586 value = force_gimple_operand (value, &list, false, NULL);
587 last = gimple_build_assign (comp, value);
588 gimple_seq_add_stmt (&list, last);
589 gcc_assert (SSA_NAME_DEF_STMT (comp) == last);
591 return list;
594 /* Extract the real or imaginary part of a complex variable or constant.
595 Make sure that it's a proper gimple_val and gimplify it if not.
596 Emit any new code before gsi. */
598 static tree
599 extract_component (gimple_stmt_iterator *gsi, tree t, bool imagpart_p,
600 bool gimple_p)
602 switch (TREE_CODE (t))
604 case COMPLEX_CST:
605 return imagpart_p ? TREE_IMAGPART (t) : TREE_REALPART (t);
607 case COMPLEX_EXPR:
608 gcc_unreachable ();
610 case VAR_DECL:
611 case RESULT_DECL:
612 case PARM_DECL:
613 case COMPONENT_REF:
614 case ARRAY_REF:
615 case VIEW_CONVERT_EXPR:
616 case MEM_REF:
618 tree inner_type = TREE_TYPE (TREE_TYPE (t));
620 t = build1 ((imagpart_p ? IMAGPART_EXPR : REALPART_EXPR),
621 inner_type, unshare_expr (t));
623 if (gimple_p)
624 t = force_gimple_operand_gsi (gsi, t, true, NULL, true,
625 GSI_SAME_STMT);
627 return t;
630 case SSA_NAME:
631 return get_component_ssa_name (t, imagpart_p);
633 default:
634 gcc_unreachable ();
638 /* Update the complex components of the ssa name on the lhs of STMT. */
640 static void
641 update_complex_components (gimple_stmt_iterator *gsi, gimple stmt, tree r,
642 tree i)
644 tree lhs;
645 gimple_seq list;
647 lhs = gimple_get_lhs (stmt);
649 list = set_component_ssa_name (lhs, false, r);
650 if (list)
651 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
653 list = set_component_ssa_name (lhs, true, i);
654 if (list)
655 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
658 static void
659 update_complex_components_on_edge (edge e, tree lhs, tree r, tree i)
661 gimple_seq list;
663 list = set_component_ssa_name (lhs, false, r);
664 if (list)
665 gsi_insert_seq_on_edge (e, list);
667 list = set_component_ssa_name (lhs, true, i);
668 if (list)
669 gsi_insert_seq_on_edge (e, list);
673 /* Update an assignment to a complex variable in place. */
675 static void
676 update_complex_assignment (gimple_stmt_iterator *gsi, tree r, tree i)
678 gimple stmt;
680 gimple_assign_set_rhs_with_ops (gsi, COMPLEX_EXPR, r, i);
681 stmt = gsi_stmt (*gsi);
682 update_stmt (stmt);
683 if (maybe_clean_eh_stmt (stmt))
684 gimple_purge_dead_eh_edges (gimple_bb (stmt));
686 if (gimple_in_ssa_p (cfun))
687 update_complex_components (gsi, gsi_stmt (*gsi), r, i);
691 /* Generate code at the entry point of the function to initialize the
692 component variables for a complex parameter. */
694 static void
695 update_parameter_components (void)
697 edge entry_edge = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun));
698 tree parm;
700 for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = DECL_CHAIN (parm))
702 tree type = TREE_TYPE (parm);
703 tree ssa_name, r, i;
705 if (TREE_CODE (type) != COMPLEX_TYPE || !is_gimple_reg (parm))
706 continue;
708 type = TREE_TYPE (type);
709 ssa_name = ssa_default_def (cfun, parm);
710 if (!ssa_name)
711 continue;
713 r = build1 (REALPART_EXPR, type, ssa_name);
714 i = build1 (IMAGPART_EXPR, type, ssa_name);
715 update_complex_components_on_edge (entry_edge, ssa_name, r, i);
719 /* Generate code to set the component variables of a complex variable
720 to match the PHI statements in block BB. */
722 static void
723 update_phi_components (basic_block bb)
725 gimple_stmt_iterator gsi;
727 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
729 gimple phi = gsi_stmt (gsi);
731 if (is_complex_reg (gimple_phi_result (phi)))
733 tree lr, li;
734 gimple pr = NULL, pi = NULL;
735 unsigned int i, n;
737 lr = get_component_ssa_name (gimple_phi_result (phi), false);
738 if (TREE_CODE (lr) == SSA_NAME)
739 pr = create_phi_node (lr, bb);
741 li = get_component_ssa_name (gimple_phi_result (phi), true);
742 if (TREE_CODE (li) == SSA_NAME)
743 pi = create_phi_node (li, bb);
745 for (i = 0, n = gimple_phi_num_args (phi); i < n; ++i)
747 tree comp, arg = gimple_phi_arg_def (phi, i);
748 if (pr)
750 comp = extract_component (NULL, arg, false, false);
751 SET_PHI_ARG_DEF (pr, i, comp);
753 if (pi)
755 comp = extract_component (NULL, arg, true, false);
756 SET_PHI_ARG_DEF (pi, i, comp);
763 /* Expand a complex move to scalars. */
765 static void
766 expand_complex_move (gimple_stmt_iterator *gsi, tree type)
768 tree inner_type = TREE_TYPE (type);
769 tree r, i, lhs, rhs;
770 gimple stmt = gsi_stmt (*gsi);
772 if (is_gimple_assign (stmt))
774 lhs = gimple_assign_lhs (stmt);
775 if (gimple_num_ops (stmt) == 2)
776 rhs = gimple_assign_rhs1 (stmt);
777 else
778 rhs = NULL_TREE;
780 else if (is_gimple_call (stmt))
782 lhs = gimple_call_lhs (stmt);
783 rhs = NULL_TREE;
785 else
786 gcc_unreachable ();
788 if (TREE_CODE (lhs) == SSA_NAME)
790 if (is_ctrl_altering_stmt (stmt))
792 edge e;
794 /* The value is not assigned on the exception edges, so we need not
795 concern ourselves there. We do need to update on the fallthru
796 edge. Find it. */
797 e = find_fallthru_edge (gsi_bb (*gsi)->succs);
798 if (!e)
799 gcc_unreachable ();
801 r = build1 (REALPART_EXPR, inner_type, lhs);
802 i = build1 (IMAGPART_EXPR, inner_type, lhs);
803 update_complex_components_on_edge (e, lhs, r, i);
805 else if (is_gimple_call (stmt)
806 || gimple_has_side_effects (stmt)
807 || gimple_assign_rhs_code (stmt) == PAREN_EXPR)
809 r = build1 (REALPART_EXPR, inner_type, lhs);
810 i = build1 (IMAGPART_EXPR, inner_type, lhs);
811 update_complex_components (gsi, stmt, r, i);
813 else
815 if (gimple_assign_rhs_code (stmt) != COMPLEX_EXPR)
817 r = extract_component (gsi, rhs, 0, true);
818 i = extract_component (gsi, rhs, 1, true);
820 else
822 r = gimple_assign_rhs1 (stmt);
823 i = gimple_assign_rhs2 (stmt);
825 update_complex_assignment (gsi, r, i);
828 else if (rhs && TREE_CODE (rhs) == SSA_NAME && !TREE_SIDE_EFFECTS (lhs))
830 tree x;
831 gimple t;
832 location_t loc;
834 loc = gimple_location (stmt);
835 r = extract_component (gsi, rhs, 0, false);
836 i = extract_component (gsi, rhs, 1, false);
838 x = build1 (REALPART_EXPR, inner_type, unshare_expr (lhs));
839 t = gimple_build_assign (x, r);
840 gimple_set_location (t, loc);
841 gsi_insert_before (gsi, t, GSI_SAME_STMT);
843 if (stmt == gsi_stmt (*gsi))
845 x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
846 gimple_assign_set_lhs (stmt, x);
847 gimple_assign_set_rhs1 (stmt, i);
849 else
851 x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
852 t = gimple_build_assign (x, i);
853 gimple_set_location (t, loc);
854 gsi_insert_before (gsi, t, GSI_SAME_STMT);
856 stmt = gsi_stmt (*gsi);
857 gcc_assert (gimple_code (stmt) == GIMPLE_RETURN);
858 gimple_return_set_retval (stmt, lhs);
861 update_stmt (stmt);
865 /* Expand complex addition to scalars:
866 a + b = (ar + br) + i(ai + bi)
867 a - b = (ar - br) + i(ai + bi)
870 static void
871 expand_complex_addition (gimple_stmt_iterator *gsi, tree inner_type,
872 tree ar, tree ai, tree br, tree bi,
873 enum tree_code code,
874 complex_lattice_t al, complex_lattice_t bl)
876 tree rr, ri;
878 switch (PAIR (al, bl))
880 case PAIR (ONLY_REAL, ONLY_REAL):
881 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
882 ri = ai;
883 break;
885 case PAIR (ONLY_REAL, ONLY_IMAG):
886 rr = ar;
887 if (code == MINUS_EXPR)
888 ri = gimplify_build2 (gsi, MINUS_EXPR, inner_type, ai, bi);
889 else
890 ri = bi;
891 break;
893 case PAIR (ONLY_IMAG, ONLY_REAL):
894 if (code == MINUS_EXPR)
895 rr = gimplify_build2 (gsi, MINUS_EXPR, inner_type, ar, br);
896 else
897 rr = br;
898 ri = ai;
899 break;
901 case PAIR (ONLY_IMAG, ONLY_IMAG):
902 rr = ar;
903 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
904 break;
906 case PAIR (VARYING, ONLY_REAL):
907 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
908 ri = ai;
909 break;
911 case PAIR (VARYING, ONLY_IMAG):
912 rr = ar;
913 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
914 break;
916 case PAIR (ONLY_REAL, VARYING):
917 if (code == MINUS_EXPR)
918 goto general;
919 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
920 ri = bi;
921 break;
923 case PAIR (ONLY_IMAG, VARYING):
924 if (code == MINUS_EXPR)
925 goto general;
926 rr = br;
927 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
928 break;
930 case PAIR (VARYING, VARYING):
931 general:
932 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
933 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
934 break;
936 default:
937 gcc_unreachable ();
940 update_complex_assignment (gsi, rr, ri);
943 /* Expand a complex multiplication or division to a libcall to the c99
944 compliant routines. */
946 static void
947 expand_complex_libcall (gimple_stmt_iterator *gsi, tree ar, tree ai,
948 tree br, tree bi, enum tree_code code)
950 enum machine_mode mode;
951 enum built_in_function bcode;
952 tree fn, type, lhs;
953 gimple old_stmt, stmt;
955 old_stmt = gsi_stmt (*gsi);
956 lhs = gimple_assign_lhs (old_stmt);
957 type = TREE_TYPE (lhs);
959 mode = TYPE_MODE (type);
960 gcc_assert (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT);
962 if (code == MULT_EXPR)
963 bcode = ((enum built_in_function)
964 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
965 else if (code == RDIV_EXPR)
966 bcode = ((enum built_in_function)
967 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
968 else
969 gcc_unreachable ();
970 fn = builtin_decl_explicit (bcode);
972 stmt = gimple_build_call (fn, 4, ar, ai, br, bi);
973 gimple_call_set_lhs (stmt, lhs);
974 update_stmt (stmt);
975 gsi_replace (gsi, stmt, false);
977 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
978 gimple_purge_dead_eh_edges (gsi_bb (*gsi));
980 if (gimple_in_ssa_p (cfun))
982 type = TREE_TYPE (type);
983 update_complex_components (gsi, stmt,
984 build1 (REALPART_EXPR, type, lhs),
985 build1 (IMAGPART_EXPR, type, lhs));
986 SSA_NAME_DEF_STMT (lhs) = stmt;
990 /* Expand complex multiplication to scalars:
991 a * b = (ar*br - ai*bi) + i(ar*bi + br*ai)
994 static void
995 expand_complex_multiplication (gimple_stmt_iterator *gsi, tree inner_type,
996 tree ar, tree ai, tree br, tree bi,
997 complex_lattice_t al, complex_lattice_t bl)
999 tree rr, ri;
1001 if (al < bl)
1003 complex_lattice_t tl;
1004 rr = ar, ar = br, br = rr;
1005 ri = ai, ai = bi, bi = ri;
1006 tl = al, al = bl, bl = tl;
1009 switch (PAIR (al, bl))
1011 case PAIR (ONLY_REAL, ONLY_REAL):
1012 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1013 ri = ai;
1014 break;
1016 case PAIR (ONLY_IMAG, ONLY_REAL):
1017 rr = ar;
1018 if (TREE_CODE (ai) == REAL_CST
1019 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (ai), dconst1))
1020 ri = br;
1021 else
1022 ri = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1023 break;
1025 case PAIR (ONLY_IMAG, ONLY_IMAG):
1026 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1027 rr = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, rr);
1028 ri = ar;
1029 break;
1031 case PAIR (VARYING, ONLY_REAL):
1032 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1033 ri = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1034 break;
1036 case PAIR (VARYING, ONLY_IMAG):
1037 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1038 rr = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, rr);
1039 ri = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, bi);
1040 break;
1042 case PAIR (VARYING, VARYING):
1043 if (flag_complex_method == 2 && SCALAR_FLOAT_TYPE_P (inner_type))
1045 expand_complex_libcall (gsi, ar, ai, br, bi, MULT_EXPR);
1046 return;
1048 else
1050 tree t1, t2, t3, t4;
1052 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1053 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1054 t3 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, bi);
1056 /* Avoid expanding redundant multiplication for the common
1057 case of squaring a complex number. */
1058 if (ar == br && ai == bi)
1059 t4 = t3;
1060 else
1061 t4 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1063 rr = gimplify_build2 (gsi, MINUS_EXPR, inner_type, t1, t2);
1064 ri = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t3, t4);
1066 break;
1068 default:
1069 gcc_unreachable ();
1072 update_complex_assignment (gsi, rr, ri);
1075 /* Keep this algorithm in sync with fold-const.c:const_binop().
1077 Expand complex division to scalars, straightforward algorithm.
1078 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1079 t = br*br + bi*bi
1082 static void
1083 expand_complex_div_straight (gimple_stmt_iterator *gsi, tree inner_type,
1084 tree ar, tree ai, tree br, tree bi,
1085 enum tree_code code)
1087 tree rr, ri, div, t1, t2, t3;
1089 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, br, br);
1090 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, bi, bi);
1091 div = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, t2);
1093 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1094 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1095 t3 = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, t2);
1096 rr = gimplify_build2 (gsi, code, inner_type, t3, div);
1098 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1099 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, bi);
1100 t3 = gimplify_build2 (gsi, MINUS_EXPR, inner_type, t1, t2);
1101 ri = gimplify_build2 (gsi, code, inner_type, t3, div);
1103 update_complex_assignment (gsi, rr, ri);
1106 /* Keep this algorithm in sync with fold-const.c:const_binop().
1108 Expand complex division to scalars, modified algorithm to minimize
1109 overflow with wide input ranges. */
1111 static void
1112 expand_complex_div_wide (gimple_stmt_iterator *gsi, tree inner_type,
1113 tree ar, tree ai, tree br, tree bi,
1114 enum tree_code code)
1116 tree rr, ri, ratio, div, t1, t2, tr, ti, compare;
1117 basic_block bb_cond, bb_true, bb_false, bb_join;
1118 gimple stmt;
1120 /* Examine |br| < |bi|, and branch. */
1121 t1 = gimplify_build1 (gsi, ABS_EXPR, inner_type, br);
1122 t2 = gimplify_build1 (gsi, ABS_EXPR, inner_type, bi);
1123 compare = fold_build2_loc (gimple_location (gsi_stmt (*gsi)),
1124 LT_EXPR, boolean_type_node, t1, t2);
1125 STRIP_NOPS (compare);
1127 bb_cond = bb_true = bb_false = bb_join = NULL;
1128 rr = ri = tr = ti = NULL;
1129 if (TREE_CODE (compare) != INTEGER_CST)
1131 edge e;
1132 gimple stmt;
1133 tree cond, tmp;
1135 tmp = create_tmp_var (boolean_type_node, NULL);
1136 stmt = gimple_build_assign (tmp, compare);
1137 if (gimple_in_ssa_p (cfun))
1139 tmp = make_ssa_name (tmp, stmt);
1140 gimple_assign_set_lhs (stmt, tmp);
1143 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1145 cond = fold_build2_loc (gimple_location (stmt),
1146 EQ_EXPR, boolean_type_node, tmp, boolean_true_node);
1147 stmt = gimple_build_cond_from_tree (cond, NULL_TREE, NULL_TREE);
1148 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1150 /* Split the original block, and create the TRUE and FALSE blocks. */
1151 e = split_block (gsi_bb (*gsi), stmt);
1152 bb_cond = e->src;
1153 bb_join = e->dest;
1154 bb_true = create_empty_bb (bb_cond);
1155 bb_false = create_empty_bb (bb_true);
1157 /* Wire the blocks together. */
1158 e->flags = EDGE_TRUE_VALUE;
1159 redirect_edge_succ (e, bb_true);
1160 make_edge (bb_cond, bb_false, EDGE_FALSE_VALUE);
1161 make_edge (bb_true, bb_join, EDGE_FALLTHRU);
1162 make_edge (bb_false, bb_join, EDGE_FALLTHRU);
1163 add_bb_to_loop (bb_true, bb_cond->loop_father);
1164 add_bb_to_loop (bb_false, bb_cond->loop_father);
1166 /* Update dominance info. Note that bb_join's data was
1167 updated by split_block. */
1168 if (dom_info_available_p (CDI_DOMINATORS))
1170 set_immediate_dominator (CDI_DOMINATORS, bb_true, bb_cond);
1171 set_immediate_dominator (CDI_DOMINATORS, bb_false, bb_cond);
1174 rr = create_tmp_reg (inner_type, NULL);
1175 ri = create_tmp_reg (inner_type, NULL);
1178 /* In the TRUE branch, we compute
1179 ratio = br/bi;
1180 div = (br * ratio) + bi;
1181 tr = (ar * ratio) + ai;
1182 ti = (ai * ratio) - ar;
1183 tr = tr / div;
1184 ti = ti / div; */
1185 if (bb_true || integer_nonzerop (compare))
1187 if (bb_true)
1189 *gsi = gsi_last_bb (bb_true);
1190 gsi_insert_after (gsi, gimple_build_nop (), GSI_NEW_STMT);
1193 ratio = gimplify_build2 (gsi, code, inner_type, br, bi);
1195 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, br, ratio);
1196 div = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, bi);
1198 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, ratio);
1199 tr = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, ai);
1201 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, ratio);
1202 ti = gimplify_build2 (gsi, MINUS_EXPR, inner_type, t1, ar);
1204 tr = gimplify_build2 (gsi, code, inner_type, tr, div);
1205 ti = gimplify_build2 (gsi, code, inner_type, ti, div);
1207 if (bb_true)
1209 stmt = gimple_build_assign (rr, tr);
1210 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1211 stmt = gimple_build_assign (ri, ti);
1212 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1213 gsi_remove (gsi, true);
1217 /* In the FALSE branch, we compute
1218 ratio = d/c;
1219 divisor = (d * ratio) + c;
1220 tr = (b * ratio) + a;
1221 ti = b - (a * ratio);
1222 tr = tr / div;
1223 ti = ti / div; */
1224 if (bb_false || integer_zerop (compare))
1226 if (bb_false)
1228 *gsi = gsi_last_bb (bb_false);
1229 gsi_insert_after (gsi, gimple_build_nop (), GSI_NEW_STMT);
1232 ratio = gimplify_build2 (gsi, code, inner_type, bi, br);
1234 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, bi, ratio);
1235 div = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, br);
1237 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, ratio);
1238 tr = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, ar);
1240 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, ratio);
1241 ti = gimplify_build2 (gsi, MINUS_EXPR, inner_type, ai, t1);
1243 tr = gimplify_build2 (gsi, code, inner_type, tr, div);
1244 ti = gimplify_build2 (gsi, code, inner_type, ti, div);
1246 if (bb_false)
1248 stmt = gimple_build_assign (rr, tr);
1249 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1250 stmt = gimple_build_assign (ri, ti);
1251 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1252 gsi_remove (gsi, true);
1256 if (bb_join)
1257 *gsi = gsi_start_bb (bb_join);
1258 else
1259 rr = tr, ri = ti;
1261 update_complex_assignment (gsi, rr, ri);
1264 /* Expand complex division to scalars. */
1266 static void
1267 expand_complex_division (gimple_stmt_iterator *gsi, tree inner_type,
1268 tree ar, tree ai, tree br, tree bi,
1269 enum tree_code code,
1270 complex_lattice_t al, complex_lattice_t bl)
1272 tree rr, ri;
1274 switch (PAIR (al, bl))
1276 case PAIR (ONLY_REAL, ONLY_REAL):
1277 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
1278 ri = ai;
1279 break;
1281 case PAIR (ONLY_REAL, ONLY_IMAG):
1282 rr = ai;
1283 ri = gimplify_build2 (gsi, code, inner_type, ar, bi);
1284 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ri);
1285 break;
1287 case PAIR (ONLY_IMAG, ONLY_REAL):
1288 rr = ar;
1289 ri = gimplify_build2 (gsi, code, inner_type, ai, br);
1290 break;
1292 case PAIR (ONLY_IMAG, ONLY_IMAG):
1293 rr = gimplify_build2 (gsi, code, inner_type, ai, bi);
1294 ri = ar;
1295 break;
1297 case PAIR (VARYING, ONLY_REAL):
1298 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
1299 ri = gimplify_build2 (gsi, code, inner_type, ai, br);
1300 break;
1302 case PAIR (VARYING, ONLY_IMAG):
1303 rr = gimplify_build2 (gsi, code, inner_type, ai, bi);
1304 ri = gimplify_build2 (gsi, code, inner_type, ar, bi);
1305 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ri);
1307 case PAIR (ONLY_REAL, VARYING):
1308 case PAIR (ONLY_IMAG, VARYING):
1309 case PAIR (VARYING, VARYING):
1310 switch (flag_complex_method)
1312 case 0:
1313 /* straightforward implementation of complex divide acceptable. */
1314 expand_complex_div_straight (gsi, inner_type, ar, ai, br, bi, code);
1315 break;
1317 case 2:
1318 if (SCALAR_FLOAT_TYPE_P (inner_type))
1320 expand_complex_libcall (gsi, ar, ai, br, bi, code);
1321 break;
1323 /* FALLTHRU */
1325 case 1:
1326 /* wide ranges of inputs must work for complex divide. */
1327 expand_complex_div_wide (gsi, inner_type, ar, ai, br, bi, code);
1328 break;
1330 default:
1331 gcc_unreachable ();
1333 return;
1335 default:
1336 gcc_unreachable ();
1339 update_complex_assignment (gsi, rr, ri);
1342 /* Expand complex negation to scalars:
1343 -a = (-ar) + i(-ai)
1346 static void
1347 expand_complex_negation (gimple_stmt_iterator *gsi, tree inner_type,
1348 tree ar, tree ai)
1350 tree rr, ri;
1352 rr = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ar);
1353 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ai);
1355 update_complex_assignment (gsi, rr, ri);
1358 /* Expand complex conjugate to scalars:
1359 ~a = (ar) + i(-ai)
1362 static void
1363 expand_complex_conjugate (gimple_stmt_iterator *gsi, tree inner_type,
1364 tree ar, tree ai)
1366 tree ri;
1368 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ai);
1370 update_complex_assignment (gsi, ar, ri);
1373 /* Expand complex comparison (EQ or NE only). */
1375 static void
1376 expand_complex_comparison (gimple_stmt_iterator *gsi, tree ar, tree ai,
1377 tree br, tree bi, enum tree_code code)
1379 tree cr, ci, cc, type;
1380 gimple stmt;
1382 cr = gimplify_build2 (gsi, code, boolean_type_node, ar, br);
1383 ci = gimplify_build2 (gsi, code, boolean_type_node, ai, bi);
1384 cc = gimplify_build2 (gsi,
1385 (code == EQ_EXPR ? TRUTH_AND_EXPR : TRUTH_OR_EXPR),
1386 boolean_type_node, cr, ci);
1388 stmt = gsi_stmt (*gsi);
1390 switch (gimple_code (stmt))
1392 case GIMPLE_RETURN:
1393 type = TREE_TYPE (gimple_return_retval (stmt));
1394 gimple_return_set_retval (stmt, fold_convert (type, cc));
1395 break;
1397 case GIMPLE_ASSIGN:
1398 type = TREE_TYPE (gimple_assign_lhs (stmt));
1399 gimple_assign_set_rhs_from_tree (gsi, fold_convert (type, cc));
1400 stmt = gsi_stmt (*gsi);
1401 break;
1403 case GIMPLE_COND:
1404 gimple_cond_set_code (stmt, EQ_EXPR);
1405 gimple_cond_set_lhs (stmt, cc);
1406 gimple_cond_set_rhs (stmt, boolean_true_node);
1407 break;
1409 default:
1410 gcc_unreachable ();
1413 update_stmt (stmt);
1416 /* Expand inline asm that sets some complex SSA_NAMEs. */
1418 static void
1419 expand_complex_asm (gimple_stmt_iterator *gsi)
1421 gimple stmt = gsi_stmt (*gsi);
1422 unsigned int i;
1424 for (i = 0; i < gimple_asm_noutputs (stmt); ++i)
1426 tree link = gimple_asm_output_op (stmt, i);
1427 tree op = TREE_VALUE (link);
1428 if (TREE_CODE (op) == SSA_NAME
1429 && TREE_CODE (TREE_TYPE (op)) == COMPLEX_TYPE)
1431 tree type = TREE_TYPE (op);
1432 tree inner_type = TREE_TYPE (type);
1433 tree r = build1 (REALPART_EXPR, inner_type, op);
1434 tree i = build1 (IMAGPART_EXPR, inner_type, op);
1435 gimple_seq list = set_component_ssa_name (op, false, r);
1437 if (list)
1438 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
1440 list = set_component_ssa_name (op, true, i);
1441 if (list)
1442 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
1447 /* Process one statement. If we identify a complex operation, expand it. */
1449 static void
1450 expand_complex_operations_1 (gimple_stmt_iterator *gsi)
1452 gimple stmt = gsi_stmt (*gsi);
1453 tree type, inner_type, lhs;
1454 tree ac, ar, ai, bc, br, bi;
1455 complex_lattice_t al, bl;
1456 enum tree_code code;
1458 if (gimple_code (stmt) == GIMPLE_ASM)
1460 expand_complex_asm (gsi);
1461 return;
1464 lhs = gimple_get_lhs (stmt);
1465 if (!lhs && gimple_code (stmt) != GIMPLE_COND)
1466 return;
1468 type = TREE_TYPE (gimple_op (stmt, 0));
1469 code = gimple_expr_code (stmt);
1471 /* Initial filter for operations we handle. */
1472 switch (code)
1474 case PLUS_EXPR:
1475 case MINUS_EXPR:
1476 case MULT_EXPR:
1477 case TRUNC_DIV_EXPR:
1478 case CEIL_DIV_EXPR:
1479 case FLOOR_DIV_EXPR:
1480 case ROUND_DIV_EXPR:
1481 case RDIV_EXPR:
1482 case NEGATE_EXPR:
1483 case CONJ_EXPR:
1484 if (TREE_CODE (type) != COMPLEX_TYPE)
1485 return;
1486 inner_type = TREE_TYPE (type);
1487 break;
1489 case EQ_EXPR:
1490 case NE_EXPR:
1491 /* Note, both GIMPLE_ASSIGN and GIMPLE_COND may have an EQ_EXPR
1492 subcode, so we need to access the operands using gimple_op. */
1493 inner_type = TREE_TYPE (gimple_op (stmt, 1));
1494 if (TREE_CODE (inner_type) != COMPLEX_TYPE)
1495 return;
1496 break;
1498 default:
1500 tree rhs;
1502 /* GIMPLE_COND may also fallthru here, but we do not need to
1503 do anything with it. */
1504 if (gimple_code (stmt) == GIMPLE_COND)
1505 return;
1507 if (TREE_CODE (type) == COMPLEX_TYPE)
1508 expand_complex_move (gsi, type);
1509 else if (is_gimple_assign (stmt)
1510 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
1511 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
1512 && TREE_CODE (lhs) == SSA_NAME)
1514 rhs = gimple_assign_rhs1 (stmt);
1515 rhs = extract_component (gsi, TREE_OPERAND (rhs, 0),
1516 gimple_assign_rhs_code (stmt)
1517 == IMAGPART_EXPR,
1518 false);
1519 gimple_assign_set_rhs_from_tree (gsi, rhs);
1520 stmt = gsi_stmt (*gsi);
1521 update_stmt (stmt);
1524 return;
1527 /* Extract the components of the two complex values. Make sure and
1528 handle the common case of the same value used twice specially. */
1529 if (is_gimple_assign (stmt))
1531 ac = gimple_assign_rhs1 (stmt);
1532 bc = (gimple_num_ops (stmt) > 2) ? gimple_assign_rhs2 (stmt) : NULL;
1534 /* GIMPLE_CALL can not get here. */
1535 else
1537 ac = gimple_cond_lhs (stmt);
1538 bc = gimple_cond_rhs (stmt);
1541 ar = extract_component (gsi, ac, false, true);
1542 ai = extract_component (gsi, ac, true, true);
1544 if (ac == bc)
1545 br = ar, bi = ai;
1546 else if (bc)
1548 br = extract_component (gsi, bc, 0, true);
1549 bi = extract_component (gsi, bc, 1, true);
1551 else
1552 br = bi = NULL_TREE;
1554 if (gimple_in_ssa_p (cfun))
1556 al = find_lattice_value (ac);
1557 if (al == UNINITIALIZED)
1558 al = VARYING;
1560 if (TREE_CODE_CLASS (code) == tcc_unary)
1561 bl = UNINITIALIZED;
1562 else if (ac == bc)
1563 bl = al;
1564 else
1566 bl = find_lattice_value (bc);
1567 if (bl == UNINITIALIZED)
1568 bl = VARYING;
1571 else
1572 al = bl = VARYING;
1574 switch (code)
1576 case PLUS_EXPR:
1577 case MINUS_EXPR:
1578 expand_complex_addition (gsi, inner_type, ar, ai, br, bi, code, al, bl);
1579 break;
1581 case MULT_EXPR:
1582 expand_complex_multiplication (gsi, inner_type, ar, ai, br, bi, al, bl);
1583 break;
1585 case TRUNC_DIV_EXPR:
1586 case CEIL_DIV_EXPR:
1587 case FLOOR_DIV_EXPR:
1588 case ROUND_DIV_EXPR:
1589 case RDIV_EXPR:
1590 expand_complex_division (gsi, inner_type, ar, ai, br, bi, code, al, bl);
1591 break;
1593 case NEGATE_EXPR:
1594 expand_complex_negation (gsi, inner_type, ar, ai);
1595 break;
1597 case CONJ_EXPR:
1598 expand_complex_conjugate (gsi, inner_type, ar, ai);
1599 break;
1601 case EQ_EXPR:
1602 case NE_EXPR:
1603 expand_complex_comparison (gsi, ar, ai, br, bi, code);
1604 break;
1606 default:
1607 gcc_unreachable ();
1612 /* Entry point for complex operation lowering during optimization. */
1614 static unsigned int
1615 tree_lower_complex (void)
1617 int old_last_basic_block;
1618 gimple_stmt_iterator gsi;
1619 basic_block bb;
1621 if (!init_dont_simulate_again ())
1622 return 0;
1624 complex_lattice_values.create (num_ssa_names);
1625 complex_lattice_values.safe_grow_cleared (num_ssa_names);
1627 init_parameter_lattice_values ();
1628 ssa_propagate (complex_visit_stmt, complex_visit_phi);
1630 complex_variable_components = new int_tree_htab_type (10);
1632 complex_ssa_name_components.create (2 * num_ssa_names);
1633 complex_ssa_name_components.safe_grow_cleared (2 * num_ssa_names);
1635 update_parameter_components ();
1637 /* ??? Ideally we'd traverse the blocks in breadth-first order. */
1638 old_last_basic_block = last_basic_block_for_fn (cfun);
1639 FOR_EACH_BB_FN (bb, cfun)
1641 if (bb->index >= old_last_basic_block)
1642 continue;
1644 update_phi_components (bb);
1645 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1646 expand_complex_operations_1 (&gsi);
1649 gsi_commit_edge_inserts ();
1651 delete complex_variable_components;
1652 complex_variable_components = NULL;
1653 complex_ssa_name_components.release ();
1654 complex_lattice_values.release ();
1655 return 0;
1658 namespace {
1660 const pass_data pass_data_lower_complex =
1662 GIMPLE_PASS, /* type */
1663 "cplxlower", /* name */
1664 OPTGROUP_NONE, /* optinfo_flags */
1665 TV_NONE, /* tv_id */
1666 PROP_ssa, /* properties_required */
1667 PROP_gimple_lcx, /* properties_provided */
1668 0, /* properties_destroyed */
1669 0, /* todo_flags_start */
1670 TODO_update_ssa, /* todo_flags_finish */
1673 class pass_lower_complex : public gimple_opt_pass
1675 public:
1676 pass_lower_complex (gcc::context *ctxt)
1677 : gimple_opt_pass (pass_data_lower_complex, ctxt)
1680 /* opt_pass methods: */
1681 opt_pass * clone () { return new pass_lower_complex (m_ctxt); }
1682 virtual unsigned int execute (function *) { return tree_lower_complex (); }
1684 }; // class pass_lower_complex
1686 } // anon namespace
1688 gimple_opt_pass *
1689 make_pass_lower_complex (gcc::context *ctxt)
1691 return new pass_lower_complex (ctxt);
1695 namespace {
1697 const pass_data pass_data_lower_complex_O0 =
1699 GIMPLE_PASS, /* type */
1700 "cplxlower0", /* name */
1701 OPTGROUP_NONE, /* optinfo_flags */
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_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 virtual bool gate (function *fun)
1720 /* With errors, normal optimization passes are not run. If we don't
1721 lower complex operations at all, rtl expansion will abort. */
1722 return !(fun->curr_properties & PROP_gimple_lcx);
1725 virtual unsigned int execute (function *) { return tree_lower_complex (); }
1727 }; // class pass_lower_complex_O0
1729 } // anon namespace
1731 gimple_opt_pass *
1732 make_pass_lower_complex_O0 (gcc::context *ctxt)
1734 return new pass_lower_complex_O0 (ctxt);