]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/tree-complex.c
gimple.h: Remove all includes.
[thirdparty/gcc.git] / gcc / tree-complex.c
1 /* Lower complex number operations to scalar operations.
2 Copyright (C) 2004-2013 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
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"
51
52
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. */
56
57 enum
58 {
59 UNINITIALIZED = 0,
60 ONLY_REAL = 1,
61 ONLY_IMAG = 2,
62 VARYING = 3
63 };
64
65 /* The type complex_lattice_t holds combinations of the above
66 constants. */
67 typedef int complex_lattice_t;
68
69 #define PAIR(a, b) ((a) << 2 | (b))
70
71
72 static vec<complex_lattice_t> complex_lattice_values;
73
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;
77
78 /* For each complex SSA_NAME, a pair of ssa names for the components. */
79 static vec<tree> complex_ssa_name_components;
80
81 /* Lookup UID in the complex_variable_components hashtable and return the
82 associated tree. */
83 static tree
84 cvc_lookup (unsigned int uid)
85 {
86 struct int_tree_map *h, in;
87 in.uid = uid;
88 h = complex_variable_components.find_with_hash (&in, uid);
89 return h ? h->to : NULL;
90 }
91
92 /* Insert the pair UID, TO into the complex_variable_components hashtable. */
93
94 static void
95 cvc_insert (unsigned int uid, tree to)
96 {
97 struct int_tree_map *h;
98 int_tree_map **loc;
99
100 h = XNEW (struct int_tree_map);
101 h->uid = uid;
102 h->to = to;
103 loc = complex_variable_components.find_slot_with_hash (h, uid, INSERT);
104 *loc = h;
105 }
106
107 /* Return true if T is not a zero constant. In the case of real values,
108 we're only interested in +0.0. */
109
110 static int
111 some_nonzerop (tree t)
112 {
113 int zerop = false;
114
115 /* Operations with real or imaginary part of a complex number zero
116 cannot be treated the same as operations with a real or imaginary
117 operand if we care about the signs of zeros in the result. */
118 if (TREE_CODE (t) == REAL_CST && !flag_signed_zeros)
119 zerop = REAL_VALUES_IDENTICAL (TREE_REAL_CST (t), dconst0);
120 else if (TREE_CODE (t) == FIXED_CST)
121 zerop = fixed_zerop (t);
122 else if (TREE_CODE (t) == INTEGER_CST)
123 zerop = integer_zerop (t);
124
125 return !zerop;
126 }
127
128
129 /* Compute a lattice value from the components of a complex type REAL
130 and IMAG. */
131
132 static complex_lattice_t
133 find_lattice_value_parts (tree real, tree imag)
134 {
135 int r, i;
136 complex_lattice_t ret;
137
138 r = some_nonzerop (real);
139 i = some_nonzerop (imag);
140 ret = r * ONLY_REAL + i * ONLY_IMAG;
141
142 /* ??? On occasion we could do better than mapping 0+0i to real, but we
143 certainly don't want to leave it UNINITIALIZED, which eventually gets
144 mapped to VARYING. */
145 if (ret == UNINITIALIZED)
146 ret = ONLY_REAL;
147
148 return ret;
149 }
150
151
152 /* Compute a lattice value from gimple_val T. */
153
154 static complex_lattice_t
155 find_lattice_value (tree t)
156 {
157 tree real, imag;
158
159 switch (TREE_CODE (t))
160 {
161 case SSA_NAME:
162 return complex_lattice_values[SSA_NAME_VERSION (t)];
163
164 case COMPLEX_CST:
165 real = TREE_REALPART (t);
166 imag = TREE_IMAGPART (t);
167 break;
168
169 default:
170 gcc_unreachable ();
171 }
172
173 return find_lattice_value_parts (real, imag);
174 }
175
176 /* Determine if LHS is something for which we're interested in seeing
177 simulation results. */
178
179 static bool
180 is_complex_reg (tree lhs)
181 {
182 return TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE && is_gimple_reg (lhs);
183 }
184
185 /* Mark the incoming parameters to the function as VARYING. */
186
187 static void
188 init_parameter_lattice_values (void)
189 {
190 tree parm, ssa_name;
191
192 for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = DECL_CHAIN (parm))
193 if (is_complex_reg (parm)
194 && (ssa_name = ssa_default_def (cfun, parm)) != NULL_TREE)
195 complex_lattice_values[SSA_NAME_VERSION (ssa_name)] = VARYING;
196 }
197
198 /* Initialize simulation state for each statement. Return false if we
199 found no statements we want to simulate, and thus there's nothing
200 for the entire pass to do. */
201
202 static bool
203 init_dont_simulate_again (void)
204 {
205 basic_block bb;
206 gimple_stmt_iterator gsi;
207 gimple phi;
208 bool saw_a_complex_op = false;
209
210 FOR_EACH_BB (bb)
211 {
212 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
213 {
214 phi = gsi_stmt (gsi);
215 prop_set_simulate_again (phi,
216 is_complex_reg (gimple_phi_result (phi)));
217 }
218
219 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
220 {
221 gimple stmt;
222 tree op0, op1;
223 bool sim_again_p;
224
225 stmt = gsi_stmt (gsi);
226 op0 = op1 = NULL_TREE;
227
228 /* Most control-altering statements must be initially
229 simulated, else we won't cover the entire cfg. */
230 sim_again_p = stmt_ends_bb_p (stmt);
231
232 switch (gimple_code (stmt))
233 {
234 case GIMPLE_CALL:
235 if (gimple_call_lhs (stmt))
236 sim_again_p = is_complex_reg (gimple_call_lhs (stmt));
237 break;
238
239 case GIMPLE_ASSIGN:
240 sim_again_p = is_complex_reg (gimple_assign_lhs (stmt));
241 if (gimple_assign_rhs_code (stmt) == REALPART_EXPR
242 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
243 op0 = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
244 else
245 op0 = gimple_assign_rhs1 (stmt);
246 if (gimple_num_ops (stmt) > 2)
247 op1 = gimple_assign_rhs2 (stmt);
248 break;
249
250 case GIMPLE_COND:
251 op0 = gimple_cond_lhs (stmt);
252 op1 = gimple_cond_rhs (stmt);
253 break;
254
255 default:
256 break;
257 }
258
259 if (op0 || op1)
260 switch (gimple_expr_code (stmt))
261 {
262 case EQ_EXPR:
263 case NE_EXPR:
264 case PLUS_EXPR:
265 case MINUS_EXPR:
266 case MULT_EXPR:
267 case TRUNC_DIV_EXPR:
268 case CEIL_DIV_EXPR:
269 case FLOOR_DIV_EXPR:
270 case ROUND_DIV_EXPR:
271 case RDIV_EXPR:
272 if (TREE_CODE (TREE_TYPE (op0)) == COMPLEX_TYPE
273 || TREE_CODE (TREE_TYPE (op1)) == COMPLEX_TYPE)
274 saw_a_complex_op = true;
275 break;
276
277 case NEGATE_EXPR:
278 case CONJ_EXPR:
279 if (TREE_CODE (TREE_TYPE (op0)) == COMPLEX_TYPE)
280 saw_a_complex_op = true;
281 break;
282
283 case REALPART_EXPR:
284 case IMAGPART_EXPR:
285 /* The total store transformation performed during
286 gimplification creates such uninitialized loads
287 and we need to lower the statement to be able
288 to fix things up. */
289 if (TREE_CODE (op0) == SSA_NAME
290 && ssa_undefined_value_p (op0))
291 saw_a_complex_op = true;
292 break;
293
294 default:
295 break;
296 }
297
298 prop_set_simulate_again (stmt, sim_again_p);
299 }
300 }
301
302 return saw_a_complex_op;
303 }
304
305
306 /* Evaluate statement STMT against the complex lattice defined above. */
307
308 static enum ssa_prop_result
309 complex_visit_stmt (gimple stmt, edge *taken_edge_p ATTRIBUTE_UNUSED,
310 tree *result_p)
311 {
312 complex_lattice_t new_l, old_l, op1_l, op2_l;
313 unsigned int ver;
314 tree lhs;
315
316 lhs = gimple_get_lhs (stmt);
317 /* Skip anything but GIMPLE_ASSIGN and GIMPLE_CALL with a lhs. */
318 if (!lhs)
319 return SSA_PROP_VARYING;
320
321 /* These conditions should be satisfied due to the initial filter
322 set up in init_dont_simulate_again. */
323 gcc_assert (TREE_CODE (lhs) == SSA_NAME);
324 gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE);
325
326 *result_p = lhs;
327 ver = SSA_NAME_VERSION (lhs);
328 old_l = complex_lattice_values[ver];
329
330 switch (gimple_expr_code (stmt))
331 {
332 case SSA_NAME:
333 case COMPLEX_CST:
334 new_l = find_lattice_value (gimple_assign_rhs1 (stmt));
335 break;
336
337 case COMPLEX_EXPR:
338 new_l = find_lattice_value_parts (gimple_assign_rhs1 (stmt),
339 gimple_assign_rhs2 (stmt));
340 break;
341
342 case PLUS_EXPR:
343 case MINUS_EXPR:
344 op1_l = find_lattice_value (gimple_assign_rhs1 (stmt));
345 op2_l = find_lattice_value (gimple_assign_rhs2 (stmt));
346
347 /* We've set up the lattice values such that IOR neatly
348 models addition. */
349 new_l = op1_l | op2_l;
350 break;
351
352 case MULT_EXPR:
353 case RDIV_EXPR:
354 case TRUNC_DIV_EXPR:
355 case CEIL_DIV_EXPR:
356 case FLOOR_DIV_EXPR:
357 case ROUND_DIV_EXPR:
358 op1_l = find_lattice_value (gimple_assign_rhs1 (stmt));
359 op2_l = find_lattice_value (gimple_assign_rhs2 (stmt));
360
361 /* Obviously, if either varies, so does the result. */
362 if (op1_l == VARYING || op2_l == VARYING)
363 new_l = VARYING;
364 /* Don't prematurely promote variables if we've not yet seen
365 their inputs. */
366 else if (op1_l == UNINITIALIZED)
367 new_l = op2_l;
368 else if (op2_l == UNINITIALIZED)
369 new_l = op1_l;
370 else
371 {
372 /* At this point both numbers have only one component. If the
373 numbers are of opposite kind, the result is imaginary,
374 otherwise the result is real. The add/subtract translates
375 the real/imag from/to 0/1; the ^ performs the comparison. */
376 new_l = ((op1_l - ONLY_REAL) ^ (op2_l - ONLY_REAL)) + ONLY_REAL;
377
378 /* Don't allow the lattice value to flip-flop indefinitely. */
379 new_l |= old_l;
380 }
381 break;
382
383 case NEGATE_EXPR:
384 case CONJ_EXPR:
385 new_l = find_lattice_value (gimple_assign_rhs1 (stmt));
386 break;
387
388 default:
389 new_l = VARYING;
390 break;
391 }
392
393 /* If nothing changed this round, let the propagator know. */
394 if (new_l == old_l)
395 return SSA_PROP_NOT_INTERESTING;
396
397 complex_lattice_values[ver] = new_l;
398 return new_l == VARYING ? SSA_PROP_VARYING : SSA_PROP_INTERESTING;
399 }
400
401 /* Evaluate a PHI node against the complex lattice defined above. */
402
403 static enum ssa_prop_result
404 complex_visit_phi (gimple phi)
405 {
406 complex_lattice_t new_l, old_l;
407 unsigned int ver;
408 tree lhs;
409 int i;
410
411 lhs = gimple_phi_result (phi);
412
413 /* This condition should be satisfied due to the initial filter
414 set up in init_dont_simulate_again. */
415 gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE);
416
417 /* We've set up the lattice values such that IOR neatly models PHI meet. */
418 new_l = UNINITIALIZED;
419 for (i = gimple_phi_num_args (phi) - 1; i >= 0; --i)
420 new_l |= find_lattice_value (gimple_phi_arg_def (phi, i));
421
422 ver = SSA_NAME_VERSION (lhs);
423 old_l = complex_lattice_values[ver];
424
425 if (new_l == old_l)
426 return SSA_PROP_NOT_INTERESTING;
427
428 complex_lattice_values[ver] = new_l;
429 return new_l == VARYING ? SSA_PROP_VARYING : SSA_PROP_INTERESTING;
430 }
431
432 /* Create one backing variable for a complex component of ORIG. */
433
434 static tree
435 create_one_component_var (tree type, tree orig, const char *prefix,
436 const char *suffix, enum tree_code code)
437 {
438 tree r = create_tmp_var (type, prefix);
439
440 DECL_SOURCE_LOCATION (r) = DECL_SOURCE_LOCATION (orig);
441 DECL_ARTIFICIAL (r) = 1;
442
443 if (DECL_NAME (orig) && !DECL_IGNORED_P (orig))
444 {
445 const char *name = IDENTIFIER_POINTER (DECL_NAME (orig));
446
447 DECL_NAME (r) = get_identifier (ACONCAT ((name, suffix, NULL)));
448
449 SET_DECL_DEBUG_EXPR (r, build1 (code, type, orig));
450 DECL_HAS_DEBUG_EXPR_P (r) = 1;
451 DECL_IGNORED_P (r) = 0;
452 TREE_NO_WARNING (r) = TREE_NO_WARNING (orig);
453 }
454 else
455 {
456 DECL_IGNORED_P (r) = 1;
457 TREE_NO_WARNING (r) = 1;
458 }
459
460 return r;
461 }
462
463 /* Retrieve a value for a complex component of VAR. */
464
465 static tree
466 get_component_var (tree var, bool imag_p)
467 {
468 size_t decl_index = DECL_UID (var) * 2 + imag_p;
469 tree ret = cvc_lookup (decl_index);
470
471 if (ret == NULL)
472 {
473 ret = create_one_component_var (TREE_TYPE (TREE_TYPE (var)), var,
474 imag_p ? "CI" : "CR",
475 imag_p ? "$imag" : "$real",
476 imag_p ? IMAGPART_EXPR : REALPART_EXPR);
477 cvc_insert (decl_index, ret);
478 }
479
480 return ret;
481 }
482
483 /* Retrieve a value for a complex component of SSA_NAME. */
484
485 static tree
486 get_component_ssa_name (tree ssa_name, bool imag_p)
487 {
488 complex_lattice_t lattice = find_lattice_value (ssa_name);
489 size_t ssa_name_index;
490 tree ret;
491
492 if (lattice == (imag_p ? ONLY_REAL : ONLY_IMAG))
493 {
494 tree inner_type = TREE_TYPE (TREE_TYPE (ssa_name));
495 if (SCALAR_FLOAT_TYPE_P (inner_type))
496 return build_real (inner_type, dconst0);
497 else
498 return build_int_cst (inner_type, 0);
499 }
500
501 ssa_name_index = SSA_NAME_VERSION (ssa_name) * 2 + imag_p;
502 ret = complex_ssa_name_components[ssa_name_index];
503 if (ret == NULL)
504 {
505 if (SSA_NAME_VAR (ssa_name))
506 ret = get_component_var (SSA_NAME_VAR (ssa_name), imag_p);
507 else
508 ret = TREE_TYPE (TREE_TYPE (ssa_name));
509 ret = make_ssa_name (ret, NULL);
510
511 /* Copy some properties from the original. In particular, whether it
512 is used in an abnormal phi, and whether it's uninitialized. */
513 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ret)
514 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name);
515 if (SSA_NAME_IS_DEFAULT_DEF (ssa_name)
516 && TREE_CODE (SSA_NAME_VAR (ssa_name)) == VAR_DECL)
517 {
518 SSA_NAME_DEF_STMT (ret) = SSA_NAME_DEF_STMT (ssa_name);
519 set_ssa_default_def (cfun, SSA_NAME_VAR (ret), ret);
520 }
521
522 complex_ssa_name_components[ssa_name_index] = ret;
523 }
524
525 return ret;
526 }
527
528 /* Set a value for a complex component of SSA_NAME, return a
529 gimple_seq of stuff that needs doing. */
530
531 static gimple_seq
532 set_component_ssa_name (tree ssa_name, bool imag_p, tree value)
533 {
534 complex_lattice_t lattice = find_lattice_value (ssa_name);
535 size_t ssa_name_index;
536 tree comp;
537 gimple last;
538 gimple_seq list;
539
540 /* We know the value must be zero, else there's a bug in our lattice
541 analysis. But the value may well be a variable known to contain
542 zero. We should be safe ignoring it. */
543 if (lattice == (imag_p ? ONLY_REAL : ONLY_IMAG))
544 return NULL;
545
546 /* If we've already assigned an SSA_NAME to this component, then this
547 means that our walk of the basic blocks found a use before the set.
548 This is fine. Now we should create an initialization for the value
549 we created earlier. */
550 ssa_name_index = SSA_NAME_VERSION (ssa_name) * 2 + imag_p;
551 comp = complex_ssa_name_components[ssa_name_index];
552 if (comp)
553 ;
554
555 /* If we've nothing assigned, and the value we're given is already stable,
556 then install that as the value for this SSA_NAME. This preemptively
557 copy-propagates the value, which avoids unnecessary memory allocation. */
558 else if (is_gimple_min_invariant (value)
559 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name))
560 {
561 complex_ssa_name_components[ssa_name_index] = value;
562 return NULL;
563 }
564 else if (TREE_CODE (value) == SSA_NAME
565 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name))
566 {
567 /* Replace an anonymous base value with the variable from cvc_lookup.
568 This should result in better debug info. */
569 if (SSA_NAME_VAR (ssa_name)
570 && (!SSA_NAME_VAR (value) || DECL_IGNORED_P (SSA_NAME_VAR (value)))
571 && !DECL_IGNORED_P (SSA_NAME_VAR (ssa_name)))
572 {
573 comp = get_component_var (SSA_NAME_VAR (ssa_name), imag_p);
574 replace_ssa_name_symbol (value, comp);
575 }
576
577 complex_ssa_name_components[ssa_name_index] = value;
578 return NULL;
579 }
580
581 /* Finally, we need to stabilize the result by installing the value into
582 a new ssa name. */
583 else
584 comp = get_component_ssa_name (ssa_name, imag_p);
585
586 /* Do all the work to assign VALUE to COMP. */
587 list = NULL;
588 value = force_gimple_operand (value, &list, false, NULL);
589 last = gimple_build_assign (comp, value);
590 gimple_seq_add_stmt (&list, last);
591 gcc_assert (SSA_NAME_DEF_STMT (comp) == last);
592
593 return list;
594 }
595
596 /* Extract the real or imaginary part of a complex variable or constant.
597 Make sure that it's a proper gimple_val and gimplify it if not.
598 Emit any new code before gsi. */
599
600 static tree
601 extract_component (gimple_stmt_iterator *gsi, tree t, bool imagpart_p,
602 bool gimple_p)
603 {
604 switch (TREE_CODE (t))
605 {
606 case COMPLEX_CST:
607 return imagpart_p ? TREE_IMAGPART (t) : TREE_REALPART (t);
608
609 case COMPLEX_EXPR:
610 gcc_unreachable ();
611
612 case VAR_DECL:
613 case RESULT_DECL:
614 case PARM_DECL:
615 case COMPONENT_REF:
616 case ARRAY_REF:
617 case VIEW_CONVERT_EXPR:
618 case MEM_REF:
619 {
620 tree inner_type = TREE_TYPE (TREE_TYPE (t));
621
622 t = build1 ((imagpart_p ? IMAGPART_EXPR : REALPART_EXPR),
623 inner_type, unshare_expr (t));
624
625 if (gimple_p)
626 t = force_gimple_operand_gsi (gsi, t, true, NULL, true,
627 GSI_SAME_STMT);
628
629 return t;
630 }
631
632 case SSA_NAME:
633 return get_component_ssa_name (t, imagpart_p);
634
635 default:
636 gcc_unreachable ();
637 }
638 }
639
640 /* Update the complex components of the ssa name on the lhs of STMT. */
641
642 static void
643 update_complex_components (gimple_stmt_iterator *gsi, gimple stmt, tree r,
644 tree i)
645 {
646 tree lhs;
647 gimple_seq list;
648
649 lhs = gimple_get_lhs (stmt);
650
651 list = set_component_ssa_name (lhs, false, r);
652 if (list)
653 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
654
655 list = set_component_ssa_name (lhs, true, i);
656 if (list)
657 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
658 }
659
660 static void
661 update_complex_components_on_edge (edge e, tree lhs, tree r, tree i)
662 {
663 gimple_seq list;
664
665 list = set_component_ssa_name (lhs, false, r);
666 if (list)
667 gsi_insert_seq_on_edge (e, list);
668
669 list = set_component_ssa_name (lhs, true, i);
670 if (list)
671 gsi_insert_seq_on_edge (e, list);
672 }
673
674
675 /* Update an assignment to a complex variable in place. */
676
677 static void
678 update_complex_assignment (gimple_stmt_iterator *gsi, tree r, tree i)
679 {
680 gimple stmt;
681
682 gimple_assign_set_rhs_with_ops (gsi, COMPLEX_EXPR, r, i);
683 stmt = gsi_stmt (*gsi);
684 update_stmt (stmt);
685 if (maybe_clean_eh_stmt (stmt))
686 gimple_purge_dead_eh_edges (gimple_bb (stmt));
687
688 if (gimple_in_ssa_p (cfun))
689 update_complex_components (gsi, gsi_stmt (*gsi), r, i);
690 }
691
692
693 /* Generate code at the entry point of the function to initialize the
694 component variables for a complex parameter. */
695
696 static void
697 update_parameter_components (void)
698 {
699 edge entry_edge = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun));
700 tree parm;
701
702 for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = DECL_CHAIN (parm))
703 {
704 tree type = TREE_TYPE (parm);
705 tree ssa_name, r, i;
706
707 if (TREE_CODE (type) != COMPLEX_TYPE || !is_gimple_reg (parm))
708 continue;
709
710 type = TREE_TYPE (type);
711 ssa_name = ssa_default_def (cfun, parm);
712 if (!ssa_name)
713 continue;
714
715 r = build1 (REALPART_EXPR, type, ssa_name);
716 i = build1 (IMAGPART_EXPR, type, ssa_name);
717 update_complex_components_on_edge (entry_edge, ssa_name, r, i);
718 }
719 }
720
721 /* Generate code to set the component variables of a complex variable
722 to match the PHI statements in block BB. */
723
724 static void
725 update_phi_components (basic_block bb)
726 {
727 gimple_stmt_iterator gsi;
728
729 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
730 {
731 gimple phi = gsi_stmt (gsi);
732
733 if (is_complex_reg (gimple_phi_result (phi)))
734 {
735 tree lr, li;
736 gimple pr = NULL, pi = NULL;
737 unsigned int i, n;
738
739 lr = get_component_ssa_name (gimple_phi_result (phi), false);
740 if (TREE_CODE (lr) == SSA_NAME)
741 pr = create_phi_node (lr, bb);
742
743 li = get_component_ssa_name (gimple_phi_result (phi), true);
744 if (TREE_CODE (li) == SSA_NAME)
745 pi = create_phi_node (li, bb);
746
747 for (i = 0, n = gimple_phi_num_args (phi); i < n; ++i)
748 {
749 tree comp, arg = gimple_phi_arg_def (phi, i);
750 if (pr)
751 {
752 comp = extract_component (NULL, arg, false, false);
753 SET_PHI_ARG_DEF (pr, i, comp);
754 }
755 if (pi)
756 {
757 comp = extract_component (NULL, arg, true, false);
758 SET_PHI_ARG_DEF (pi, i, comp);
759 }
760 }
761 }
762 }
763 }
764
765 /* Expand a complex move to scalars. */
766
767 static void
768 expand_complex_move (gimple_stmt_iterator *gsi, tree type)
769 {
770 tree inner_type = TREE_TYPE (type);
771 tree r, i, lhs, rhs;
772 gimple stmt = gsi_stmt (*gsi);
773
774 if (is_gimple_assign (stmt))
775 {
776 lhs = gimple_assign_lhs (stmt);
777 if (gimple_num_ops (stmt) == 2)
778 rhs = gimple_assign_rhs1 (stmt);
779 else
780 rhs = NULL_TREE;
781 }
782 else if (is_gimple_call (stmt))
783 {
784 lhs = gimple_call_lhs (stmt);
785 rhs = NULL_TREE;
786 }
787 else
788 gcc_unreachable ();
789
790 if (TREE_CODE (lhs) == SSA_NAME)
791 {
792 if (is_ctrl_altering_stmt (stmt))
793 {
794 edge e;
795
796 /* The value is not assigned on the exception edges, so we need not
797 concern ourselves there. We do need to update on the fallthru
798 edge. Find it. */
799 e = find_fallthru_edge (gsi_bb (*gsi)->succs);
800 if (!e)
801 gcc_unreachable ();
802
803 r = build1 (REALPART_EXPR, inner_type, lhs);
804 i = build1 (IMAGPART_EXPR, inner_type, lhs);
805 update_complex_components_on_edge (e, lhs, r, i);
806 }
807 else if (is_gimple_call (stmt)
808 || gimple_has_side_effects (stmt)
809 || gimple_assign_rhs_code (stmt) == PAREN_EXPR)
810 {
811 r = build1 (REALPART_EXPR, inner_type, lhs);
812 i = build1 (IMAGPART_EXPR, inner_type, lhs);
813 update_complex_components (gsi, stmt, r, i);
814 }
815 else
816 {
817 if (gimple_assign_rhs_code (stmt) != COMPLEX_EXPR)
818 {
819 r = extract_component (gsi, rhs, 0, true);
820 i = extract_component (gsi, rhs, 1, true);
821 }
822 else
823 {
824 r = gimple_assign_rhs1 (stmt);
825 i = gimple_assign_rhs2 (stmt);
826 }
827 update_complex_assignment (gsi, r, i);
828 }
829 }
830 else if (rhs && TREE_CODE (rhs) == SSA_NAME && !TREE_SIDE_EFFECTS (lhs))
831 {
832 tree x;
833 gimple t;
834
835 r = extract_component (gsi, rhs, 0, false);
836 i = extract_component (gsi, rhs, 1, false);
837
838 x = build1 (REALPART_EXPR, inner_type, unshare_expr (lhs));
839 t = gimple_build_assign (x, r);
840 gsi_insert_before (gsi, t, GSI_SAME_STMT);
841
842 if (stmt == gsi_stmt (*gsi))
843 {
844 x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
845 gimple_assign_set_lhs (stmt, x);
846 gimple_assign_set_rhs1 (stmt, i);
847 }
848 else
849 {
850 x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
851 t = gimple_build_assign (x, i);
852 gsi_insert_before (gsi, t, GSI_SAME_STMT);
853
854 stmt = gsi_stmt (*gsi);
855 gcc_assert (gimple_code (stmt) == GIMPLE_RETURN);
856 gimple_return_set_retval (stmt, lhs);
857 }
858
859 update_stmt (stmt);
860 }
861 }
862
863 /* Expand complex addition to scalars:
864 a + b = (ar + br) + i(ai + bi)
865 a - b = (ar - br) + i(ai + bi)
866 */
867
868 static void
869 expand_complex_addition (gimple_stmt_iterator *gsi, tree inner_type,
870 tree ar, tree ai, tree br, tree bi,
871 enum tree_code code,
872 complex_lattice_t al, complex_lattice_t bl)
873 {
874 tree rr, ri;
875
876 switch (PAIR (al, bl))
877 {
878 case PAIR (ONLY_REAL, ONLY_REAL):
879 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
880 ri = ai;
881 break;
882
883 case PAIR (ONLY_REAL, ONLY_IMAG):
884 rr = ar;
885 if (code == MINUS_EXPR)
886 ri = gimplify_build2 (gsi, MINUS_EXPR, inner_type, ai, bi);
887 else
888 ri = bi;
889 break;
890
891 case PAIR (ONLY_IMAG, ONLY_REAL):
892 if (code == MINUS_EXPR)
893 rr = gimplify_build2 (gsi, MINUS_EXPR, inner_type, ar, br);
894 else
895 rr = br;
896 ri = ai;
897 break;
898
899 case PAIR (ONLY_IMAG, ONLY_IMAG):
900 rr = ar;
901 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
902 break;
903
904 case PAIR (VARYING, ONLY_REAL):
905 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
906 ri = ai;
907 break;
908
909 case PAIR (VARYING, ONLY_IMAG):
910 rr = ar;
911 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
912 break;
913
914 case PAIR (ONLY_REAL, VARYING):
915 if (code == MINUS_EXPR)
916 goto general;
917 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
918 ri = bi;
919 break;
920
921 case PAIR (ONLY_IMAG, VARYING):
922 if (code == MINUS_EXPR)
923 goto general;
924 rr = br;
925 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
926 break;
927
928 case PAIR (VARYING, VARYING):
929 general:
930 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
931 ri = gimplify_build2 (gsi, code, inner_type, ai, bi);
932 break;
933
934 default:
935 gcc_unreachable ();
936 }
937
938 update_complex_assignment (gsi, rr, ri);
939 }
940
941 /* Expand a complex multiplication or division to a libcall to the c99
942 compliant routines. */
943
944 static void
945 expand_complex_libcall (gimple_stmt_iterator *gsi, tree ar, tree ai,
946 tree br, tree bi, enum tree_code code)
947 {
948 enum machine_mode mode;
949 enum built_in_function bcode;
950 tree fn, type, lhs;
951 gimple old_stmt, stmt;
952
953 old_stmt = gsi_stmt (*gsi);
954 lhs = gimple_assign_lhs (old_stmt);
955 type = TREE_TYPE (lhs);
956
957 mode = TYPE_MODE (type);
958 gcc_assert (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT);
959
960 if (code == MULT_EXPR)
961 bcode = ((enum built_in_function)
962 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
963 else if (code == RDIV_EXPR)
964 bcode = ((enum built_in_function)
965 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
966 else
967 gcc_unreachable ();
968 fn = builtin_decl_explicit (bcode);
969
970 stmt = gimple_build_call (fn, 4, ar, ai, br, bi);
971 gimple_call_set_lhs (stmt, lhs);
972 update_stmt (stmt);
973 gsi_replace (gsi, stmt, false);
974
975 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
976 gimple_purge_dead_eh_edges (gsi_bb (*gsi));
977
978 if (gimple_in_ssa_p (cfun))
979 {
980 type = TREE_TYPE (type);
981 update_complex_components (gsi, stmt,
982 build1 (REALPART_EXPR, type, lhs),
983 build1 (IMAGPART_EXPR, type, lhs));
984 SSA_NAME_DEF_STMT (lhs) = stmt;
985 }
986 }
987
988 /* Expand complex multiplication to scalars:
989 a * b = (ar*br - ai*bi) + i(ar*bi + br*ai)
990 */
991
992 static void
993 expand_complex_multiplication (gimple_stmt_iterator *gsi, tree inner_type,
994 tree ar, tree ai, tree br, tree bi,
995 complex_lattice_t al, complex_lattice_t bl)
996 {
997 tree rr, ri;
998
999 if (al < bl)
1000 {
1001 complex_lattice_t tl;
1002 rr = ar, ar = br, br = rr;
1003 ri = ai, ai = bi, bi = ri;
1004 tl = al, al = bl, bl = tl;
1005 }
1006
1007 switch (PAIR (al, bl))
1008 {
1009 case PAIR (ONLY_REAL, ONLY_REAL):
1010 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1011 ri = ai;
1012 break;
1013
1014 case PAIR (ONLY_IMAG, ONLY_REAL):
1015 rr = ar;
1016 if (TREE_CODE (ai) == REAL_CST
1017 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (ai), dconst1))
1018 ri = br;
1019 else
1020 ri = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1021 break;
1022
1023 case PAIR (ONLY_IMAG, ONLY_IMAG):
1024 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1025 rr = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, rr);
1026 ri = ar;
1027 break;
1028
1029 case PAIR (VARYING, ONLY_REAL):
1030 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1031 ri = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1032 break;
1033
1034 case PAIR (VARYING, ONLY_IMAG):
1035 rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1036 rr = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, rr);
1037 ri = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, bi);
1038 break;
1039
1040 case PAIR (VARYING, VARYING):
1041 if (flag_complex_method == 2 && SCALAR_FLOAT_TYPE_P (inner_type))
1042 {
1043 expand_complex_libcall (gsi, ar, ai, br, bi, MULT_EXPR);
1044 return;
1045 }
1046 else
1047 {
1048 tree t1, t2, t3, t4;
1049
1050 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1051 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1052 t3 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, bi);
1053
1054 /* Avoid expanding redundant multiplication for the common
1055 case of squaring a complex number. */
1056 if (ar == br && ai == bi)
1057 t4 = t3;
1058 else
1059 t4 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1060
1061 rr = gimplify_build2 (gsi, MINUS_EXPR, inner_type, t1, t2);
1062 ri = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t3, t4);
1063 }
1064 break;
1065
1066 default:
1067 gcc_unreachable ();
1068 }
1069
1070 update_complex_assignment (gsi, rr, ri);
1071 }
1072
1073 /* Keep this algorithm in sync with fold-const.c:const_binop().
1074
1075 Expand complex division to scalars, straightforward algorithm.
1076 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1077 t = br*br + bi*bi
1078 */
1079
1080 static void
1081 expand_complex_div_straight (gimple_stmt_iterator *gsi, tree inner_type,
1082 tree ar, tree ai, tree br, tree bi,
1083 enum tree_code code)
1084 {
1085 tree rr, ri, div, t1, t2, t3;
1086
1087 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, br, br);
1088 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, bi, bi);
1089 div = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, t2);
1090
1091 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br);
1092 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi);
1093 t3 = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, t2);
1094 rr = gimplify_build2 (gsi, code, inner_type, t3, div);
1095
1096 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br);
1097 t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, bi);
1098 t3 = gimplify_build2 (gsi, MINUS_EXPR, inner_type, t1, t2);
1099 ri = gimplify_build2 (gsi, code, inner_type, t3, div);
1100
1101 update_complex_assignment (gsi, rr, ri);
1102 }
1103
1104 /* Keep this algorithm in sync with fold-const.c:const_binop().
1105
1106 Expand complex division to scalars, modified algorithm to minimize
1107 overflow with wide input ranges. */
1108
1109 static void
1110 expand_complex_div_wide (gimple_stmt_iterator *gsi, tree inner_type,
1111 tree ar, tree ai, tree br, tree bi,
1112 enum tree_code code)
1113 {
1114 tree rr, ri, ratio, div, t1, t2, tr, ti, compare;
1115 basic_block bb_cond, bb_true, bb_false, bb_join;
1116 gimple stmt;
1117
1118 /* Examine |br| < |bi|, and branch. */
1119 t1 = gimplify_build1 (gsi, ABS_EXPR, inner_type, br);
1120 t2 = gimplify_build1 (gsi, ABS_EXPR, inner_type, bi);
1121 compare = fold_build2_loc (gimple_location (gsi_stmt (*gsi)),
1122 LT_EXPR, boolean_type_node, t1, t2);
1123 STRIP_NOPS (compare);
1124
1125 bb_cond = bb_true = bb_false = bb_join = NULL;
1126 rr = ri = tr = ti = NULL;
1127 if (TREE_CODE (compare) != INTEGER_CST)
1128 {
1129 edge e;
1130 gimple stmt;
1131 tree cond, tmp;
1132
1133 tmp = create_tmp_var (boolean_type_node, NULL);
1134 stmt = gimple_build_assign (tmp, compare);
1135 if (gimple_in_ssa_p (cfun))
1136 {
1137 tmp = make_ssa_name (tmp, stmt);
1138 gimple_assign_set_lhs (stmt, tmp);
1139 }
1140
1141 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1142
1143 cond = fold_build2_loc (gimple_location (stmt),
1144 EQ_EXPR, boolean_type_node, tmp, boolean_true_node);
1145 stmt = gimple_build_cond_from_tree (cond, NULL_TREE, NULL_TREE);
1146 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1147
1148 /* Split the original block, and create the TRUE and FALSE blocks. */
1149 e = split_block (gsi_bb (*gsi), stmt);
1150 bb_cond = e->src;
1151 bb_join = e->dest;
1152 bb_true = create_empty_bb (bb_cond);
1153 bb_false = create_empty_bb (bb_true);
1154
1155 /* Wire the blocks together. */
1156 e->flags = EDGE_TRUE_VALUE;
1157 redirect_edge_succ (e, bb_true);
1158 make_edge (bb_cond, bb_false, EDGE_FALSE_VALUE);
1159 make_edge (bb_true, bb_join, EDGE_FALLTHRU);
1160 make_edge (bb_false, bb_join, EDGE_FALLTHRU);
1161 if (current_loops)
1162 {
1163 add_bb_to_loop (bb_true, bb_cond->loop_father);
1164 add_bb_to_loop (bb_false, bb_cond->loop_father);
1165 }
1166
1167 /* Update dominance info. Note that bb_join's data was
1168 updated by split_block. */
1169 if (dom_info_available_p (CDI_DOMINATORS))
1170 {
1171 set_immediate_dominator (CDI_DOMINATORS, bb_true, bb_cond);
1172 set_immediate_dominator (CDI_DOMINATORS, bb_false, bb_cond);
1173 }
1174
1175 rr = create_tmp_reg (inner_type, NULL);
1176 ri = create_tmp_reg (inner_type, NULL);
1177 }
1178
1179 /* In the TRUE branch, we compute
1180 ratio = br/bi;
1181 div = (br * ratio) + bi;
1182 tr = (ar * ratio) + ai;
1183 ti = (ai * ratio) - ar;
1184 tr = tr / div;
1185 ti = ti / div; */
1186 if (bb_true || integer_nonzerop (compare))
1187 {
1188 if (bb_true)
1189 {
1190 *gsi = gsi_last_bb (bb_true);
1191 gsi_insert_after (gsi, gimple_build_nop (), GSI_NEW_STMT);
1192 }
1193
1194 ratio = gimplify_build2 (gsi, code, inner_type, br, bi);
1195
1196 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, br, ratio);
1197 div = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, bi);
1198
1199 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, ratio);
1200 tr = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, ai);
1201
1202 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, ratio);
1203 ti = gimplify_build2 (gsi, MINUS_EXPR, inner_type, t1, ar);
1204
1205 tr = gimplify_build2 (gsi, code, inner_type, tr, div);
1206 ti = gimplify_build2 (gsi, code, inner_type, ti, div);
1207
1208 if (bb_true)
1209 {
1210 stmt = gimple_build_assign (rr, tr);
1211 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1212 stmt = gimple_build_assign (ri, ti);
1213 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1214 gsi_remove (gsi, true);
1215 }
1216 }
1217
1218 /* In the FALSE branch, we compute
1219 ratio = d/c;
1220 divisor = (d * ratio) + c;
1221 tr = (b * ratio) + a;
1222 ti = b - (a * ratio);
1223 tr = tr / div;
1224 ti = ti / div; */
1225 if (bb_false || integer_zerop (compare))
1226 {
1227 if (bb_false)
1228 {
1229 *gsi = gsi_last_bb (bb_false);
1230 gsi_insert_after (gsi, gimple_build_nop (), GSI_NEW_STMT);
1231 }
1232
1233 ratio = gimplify_build2 (gsi, code, inner_type, bi, br);
1234
1235 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, bi, ratio);
1236 div = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, br);
1237
1238 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, ratio);
1239 tr = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, ar);
1240
1241 t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, ratio);
1242 ti = gimplify_build2 (gsi, MINUS_EXPR, inner_type, ai, t1);
1243
1244 tr = gimplify_build2 (gsi, code, inner_type, tr, div);
1245 ti = gimplify_build2 (gsi, code, inner_type, ti, div);
1246
1247 if (bb_false)
1248 {
1249 stmt = gimple_build_assign (rr, tr);
1250 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1251 stmt = gimple_build_assign (ri, ti);
1252 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1253 gsi_remove (gsi, true);
1254 }
1255 }
1256
1257 if (bb_join)
1258 *gsi = gsi_start_bb (bb_join);
1259 else
1260 rr = tr, ri = ti;
1261
1262 update_complex_assignment (gsi, rr, ri);
1263 }
1264
1265 /* Expand complex division to scalars. */
1266
1267 static void
1268 expand_complex_division (gimple_stmt_iterator *gsi, tree inner_type,
1269 tree ar, tree ai, tree br, tree bi,
1270 enum tree_code code,
1271 complex_lattice_t al, complex_lattice_t bl)
1272 {
1273 tree rr, ri;
1274
1275 switch (PAIR (al, bl))
1276 {
1277 case PAIR (ONLY_REAL, ONLY_REAL):
1278 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
1279 ri = ai;
1280 break;
1281
1282 case PAIR (ONLY_REAL, ONLY_IMAG):
1283 rr = ai;
1284 ri = gimplify_build2 (gsi, code, inner_type, ar, bi);
1285 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ri);
1286 break;
1287
1288 case PAIR (ONLY_IMAG, ONLY_REAL):
1289 rr = ar;
1290 ri = gimplify_build2 (gsi, code, inner_type, ai, br);
1291 break;
1292
1293 case PAIR (ONLY_IMAG, ONLY_IMAG):
1294 rr = gimplify_build2 (gsi, code, inner_type, ai, bi);
1295 ri = ar;
1296 break;
1297
1298 case PAIR (VARYING, ONLY_REAL):
1299 rr = gimplify_build2 (gsi, code, inner_type, ar, br);
1300 ri = gimplify_build2 (gsi, code, inner_type, ai, br);
1301 break;
1302
1303 case PAIR (VARYING, ONLY_IMAG):
1304 rr = gimplify_build2 (gsi, code, inner_type, ai, bi);
1305 ri = gimplify_build2 (gsi, code, inner_type, ar, bi);
1306 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ri);
1307
1308 case PAIR (ONLY_REAL, VARYING):
1309 case PAIR (ONLY_IMAG, VARYING):
1310 case PAIR (VARYING, VARYING):
1311 switch (flag_complex_method)
1312 {
1313 case 0:
1314 /* straightforward implementation of complex divide acceptable. */
1315 expand_complex_div_straight (gsi, inner_type, ar, ai, br, bi, code);
1316 break;
1317
1318 case 2:
1319 if (SCALAR_FLOAT_TYPE_P (inner_type))
1320 {
1321 expand_complex_libcall (gsi, ar, ai, br, bi, code);
1322 break;
1323 }
1324 /* FALLTHRU */
1325
1326 case 1:
1327 /* wide ranges of inputs must work for complex divide. */
1328 expand_complex_div_wide (gsi, inner_type, ar, ai, br, bi, code);
1329 break;
1330
1331 default:
1332 gcc_unreachable ();
1333 }
1334 return;
1335
1336 default:
1337 gcc_unreachable ();
1338 }
1339
1340 update_complex_assignment (gsi, rr, ri);
1341 }
1342
1343 /* Expand complex negation to scalars:
1344 -a = (-ar) + i(-ai)
1345 */
1346
1347 static void
1348 expand_complex_negation (gimple_stmt_iterator *gsi, tree inner_type,
1349 tree ar, tree ai)
1350 {
1351 tree rr, ri;
1352
1353 rr = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ar);
1354 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ai);
1355
1356 update_complex_assignment (gsi, rr, ri);
1357 }
1358
1359 /* Expand complex conjugate to scalars:
1360 ~a = (ar) + i(-ai)
1361 */
1362
1363 static void
1364 expand_complex_conjugate (gimple_stmt_iterator *gsi, tree inner_type,
1365 tree ar, tree ai)
1366 {
1367 tree ri;
1368
1369 ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ai);
1370
1371 update_complex_assignment (gsi, ar, ri);
1372 }
1373
1374 /* Expand complex comparison (EQ or NE only). */
1375
1376 static void
1377 expand_complex_comparison (gimple_stmt_iterator *gsi, tree ar, tree ai,
1378 tree br, tree bi, enum tree_code code)
1379 {
1380 tree cr, ci, cc, type;
1381 gimple stmt;
1382
1383 cr = gimplify_build2 (gsi, code, boolean_type_node, ar, br);
1384 ci = gimplify_build2 (gsi, code, boolean_type_node, ai, bi);
1385 cc = gimplify_build2 (gsi,
1386 (code == EQ_EXPR ? TRUTH_AND_EXPR : TRUTH_OR_EXPR),
1387 boolean_type_node, cr, ci);
1388
1389 stmt = gsi_stmt (*gsi);
1390
1391 switch (gimple_code (stmt))
1392 {
1393 case GIMPLE_RETURN:
1394 type = TREE_TYPE (gimple_return_retval (stmt));
1395 gimple_return_set_retval (stmt, fold_convert (type, cc));
1396 break;
1397
1398 case GIMPLE_ASSIGN:
1399 type = TREE_TYPE (gimple_assign_lhs (stmt));
1400 gimple_assign_set_rhs_from_tree (gsi, fold_convert (type, cc));
1401 stmt = gsi_stmt (*gsi);
1402 break;
1403
1404 case GIMPLE_COND:
1405 gimple_cond_set_code (stmt, EQ_EXPR);
1406 gimple_cond_set_lhs (stmt, cc);
1407 gimple_cond_set_rhs (stmt, boolean_true_node);
1408 break;
1409
1410 default:
1411 gcc_unreachable ();
1412 }
1413
1414 update_stmt (stmt);
1415 }
1416
1417 /* Expand inline asm that sets some complex SSA_NAMEs. */
1418
1419 static void
1420 expand_complex_asm (gimple_stmt_iterator *gsi)
1421 {
1422 gimple stmt = gsi_stmt (*gsi);
1423 unsigned int i;
1424
1425 for (i = 0; i < gimple_asm_noutputs (stmt); ++i)
1426 {
1427 tree link = gimple_asm_output_op (stmt, i);
1428 tree op = TREE_VALUE (link);
1429 if (TREE_CODE (op) == SSA_NAME
1430 && TREE_CODE (TREE_TYPE (op)) == COMPLEX_TYPE)
1431 {
1432 tree type = TREE_TYPE (op);
1433 tree inner_type = TREE_TYPE (type);
1434 tree r = build1 (REALPART_EXPR, inner_type, op);
1435 tree i = build1 (IMAGPART_EXPR, inner_type, op);
1436 gimple_seq list = set_component_ssa_name (op, false, r);
1437
1438 if (list)
1439 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
1440
1441 list = set_component_ssa_name (op, true, i);
1442 if (list)
1443 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
1444 }
1445 }
1446 }
1447
1448 /* Process one statement. If we identify a complex operation, expand it. */
1449
1450 static void
1451 expand_complex_operations_1 (gimple_stmt_iterator *gsi)
1452 {
1453 gimple stmt = gsi_stmt (*gsi);
1454 tree type, inner_type, lhs;
1455 tree ac, ar, ai, bc, br, bi;
1456 complex_lattice_t al, bl;
1457 enum tree_code code;
1458
1459 if (gimple_code (stmt) == GIMPLE_ASM)
1460 {
1461 expand_complex_asm (gsi);
1462 return;
1463 }
1464
1465 lhs = gimple_get_lhs (stmt);
1466 if (!lhs && gimple_code (stmt) != GIMPLE_COND)
1467 return;
1468
1469 type = TREE_TYPE (gimple_op (stmt, 0));
1470 code = gimple_expr_code (stmt);
1471
1472 /* Initial filter for operations we handle. */
1473 switch (code)
1474 {
1475 case PLUS_EXPR:
1476 case MINUS_EXPR:
1477 case MULT_EXPR:
1478 case TRUNC_DIV_EXPR:
1479 case CEIL_DIV_EXPR:
1480 case FLOOR_DIV_EXPR:
1481 case ROUND_DIV_EXPR:
1482 case RDIV_EXPR:
1483 case NEGATE_EXPR:
1484 case CONJ_EXPR:
1485 if (TREE_CODE (type) != COMPLEX_TYPE)
1486 return;
1487 inner_type = TREE_TYPE (type);
1488 break;
1489
1490 case EQ_EXPR:
1491 case NE_EXPR:
1492 /* Note, both GIMPLE_ASSIGN and GIMPLE_COND may have an EQ_EXPR
1493 subcode, so we need to access the operands using gimple_op. */
1494 inner_type = TREE_TYPE (gimple_op (stmt, 1));
1495 if (TREE_CODE (inner_type) != COMPLEX_TYPE)
1496 return;
1497 break;
1498
1499 default:
1500 {
1501 tree rhs;
1502
1503 /* GIMPLE_COND may also fallthru here, but we do not need to
1504 do anything with it. */
1505 if (gimple_code (stmt) == GIMPLE_COND)
1506 return;
1507
1508 if (TREE_CODE (type) == COMPLEX_TYPE)
1509 expand_complex_move (gsi, type);
1510 else if (is_gimple_assign (stmt)
1511 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
1512 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
1513 && TREE_CODE (lhs) == SSA_NAME)
1514 {
1515 rhs = gimple_assign_rhs1 (stmt);
1516 rhs = extract_component (gsi, TREE_OPERAND (rhs, 0),
1517 gimple_assign_rhs_code (stmt)
1518 == IMAGPART_EXPR,
1519 false);
1520 gimple_assign_set_rhs_from_tree (gsi, rhs);
1521 stmt = gsi_stmt (*gsi);
1522 update_stmt (stmt);
1523 }
1524 }
1525 return;
1526 }
1527
1528 /* Extract the components of the two complex values. Make sure and
1529 handle the common case of the same value used twice specially. */
1530 if (is_gimple_assign (stmt))
1531 {
1532 ac = gimple_assign_rhs1 (stmt);
1533 bc = (gimple_num_ops (stmt) > 2) ? gimple_assign_rhs2 (stmt) : NULL;
1534 }
1535 /* GIMPLE_CALL can not get here. */
1536 else
1537 {
1538 ac = gimple_cond_lhs (stmt);
1539 bc = gimple_cond_rhs (stmt);
1540 }
1541
1542 ar = extract_component (gsi, ac, false, true);
1543 ai = extract_component (gsi, ac, true, true);
1544
1545 if (ac == bc)
1546 br = ar, bi = ai;
1547 else if (bc)
1548 {
1549 br = extract_component (gsi, bc, 0, true);
1550 bi = extract_component (gsi, bc, 1, true);
1551 }
1552 else
1553 br = bi = NULL_TREE;
1554
1555 if (gimple_in_ssa_p (cfun))
1556 {
1557 al = find_lattice_value (ac);
1558 if (al == UNINITIALIZED)
1559 al = VARYING;
1560
1561 if (TREE_CODE_CLASS (code) == tcc_unary)
1562 bl = UNINITIALIZED;
1563 else if (ac == bc)
1564 bl = al;
1565 else
1566 {
1567 bl = find_lattice_value (bc);
1568 if (bl == UNINITIALIZED)
1569 bl = VARYING;
1570 }
1571 }
1572 else
1573 al = bl = VARYING;
1574
1575 switch (code)
1576 {
1577 case PLUS_EXPR:
1578 case MINUS_EXPR:
1579 expand_complex_addition (gsi, inner_type, ar, ai, br, bi, code, al, bl);
1580 break;
1581
1582 case MULT_EXPR:
1583 expand_complex_multiplication (gsi, inner_type, ar, ai, br, bi, al, bl);
1584 break;
1585
1586 case TRUNC_DIV_EXPR:
1587 case CEIL_DIV_EXPR:
1588 case FLOOR_DIV_EXPR:
1589 case ROUND_DIV_EXPR:
1590 case RDIV_EXPR:
1591 expand_complex_division (gsi, inner_type, ar, ai, br, bi, code, al, bl);
1592 break;
1593
1594 case NEGATE_EXPR:
1595 expand_complex_negation (gsi, inner_type, ar, ai);
1596 break;
1597
1598 case CONJ_EXPR:
1599 expand_complex_conjugate (gsi, inner_type, ar, ai);
1600 break;
1601
1602 case EQ_EXPR:
1603 case NE_EXPR:
1604 expand_complex_comparison (gsi, ar, ai, br, bi, code);
1605 break;
1606
1607 default:
1608 gcc_unreachable ();
1609 }
1610 }
1611
1612 \f
1613 /* Entry point for complex operation lowering during optimization. */
1614
1615 static unsigned int
1616 tree_lower_complex (void)
1617 {
1618 int old_last_basic_block;
1619 gimple_stmt_iterator gsi;
1620 basic_block bb;
1621
1622 if (!init_dont_simulate_again ())
1623 return 0;
1624
1625 complex_lattice_values.create (num_ssa_names);
1626 complex_lattice_values.safe_grow_cleared (num_ssa_names);
1627
1628 init_parameter_lattice_values ();
1629 ssa_propagate (complex_visit_stmt, complex_visit_phi);
1630
1631 complex_variable_components.create (10);
1632
1633 complex_ssa_name_components.create (2 * num_ssa_names);
1634 complex_ssa_name_components.safe_grow_cleared (2 * num_ssa_names);
1635
1636 update_parameter_components ();
1637
1638 /* ??? Ideally we'd traverse the blocks in breadth-first order. */
1639 old_last_basic_block = last_basic_block;
1640 FOR_EACH_BB (bb)
1641 {
1642 if (bb->index >= old_last_basic_block)
1643 continue;
1644
1645 update_phi_components (bb);
1646 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1647 expand_complex_operations_1 (&gsi);
1648 }
1649
1650 gsi_commit_edge_inserts ();
1651
1652 complex_variable_components.dispose ();
1653 complex_ssa_name_components.release ();
1654 complex_lattice_values.release ();
1655 return 0;
1656 }
1657
1658 namespace {
1659
1660 const pass_data pass_data_lower_complex =
1661 {
1662 GIMPLE_PASS, /* type */
1663 "cplxlower", /* name */
1664 OPTGROUP_NONE, /* optinfo_flags */
1665 false, /* has_gate */
1666 true, /* has_execute */
1667 TV_NONE, /* tv_id */
1668 PROP_ssa, /* properties_required */
1669 PROP_gimple_lcx, /* properties_provided */
1670 0, /* properties_destroyed */
1671 0, /* todo_flags_start */
1672 ( TODO_update_ssa | TODO_verify_stmts ), /* todo_flags_finish */
1673 };
1674
1675 class pass_lower_complex : public gimple_opt_pass
1676 {
1677 public:
1678 pass_lower_complex (gcc::context *ctxt)
1679 : gimple_opt_pass (pass_data_lower_complex, ctxt)
1680 {}
1681
1682 /* opt_pass methods: */
1683 opt_pass * clone () { return new pass_lower_complex (m_ctxt); }
1684 unsigned int execute () { return tree_lower_complex (); }
1685
1686 }; // class pass_lower_complex
1687
1688 } // anon namespace
1689
1690 gimple_opt_pass *
1691 make_pass_lower_complex (gcc::context *ctxt)
1692 {
1693 return new pass_lower_complex (ctxt);
1694 }
1695
1696 \f
1697 static bool
1698 gate_no_optimization (void)
1699 {
1700 /* With errors, normal optimization passes are not run. If we don't
1701 lower complex operations at all, rtl expansion will abort. */
1702 return !(cfun->curr_properties & PROP_gimple_lcx);
1703 }
1704
1705 namespace {
1706
1707 const pass_data pass_data_lower_complex_O0 =
1708 {
1709 GIMPLE_PASS, /* type */
1710 "cplxlower0", /* name */
1711 OPTGROUP_NONE, /* optinfo_flags */
1712 true, /* has_gate */
1713 true, /* has_execute */
1714 TV_NONE, /* tv_id */
1715 PROP_cfg, /* properties_required */
1716 PROP_gimple_lcx, /* properties_provided */
1717 0, /* properties_destroyed */
1718 0, /* todo_flags_start */
1719 ( TODO_update_ssa | TODO_verify_stmts ), /* todo_flags_finish */
1720 };
1721
1722 class pass_lower_complex_O0 : public gimple_opt_pass
1723 {
1724 public:
1725 pass_lower_complex_O0 (gcc::context *ctxt)
1726 : gimple_opt_pass (pass_data_lower_complex_O0, ctxt)
1727 {}
1728
1729 /* opt_pass methods: */
1730 bool gate () { return gate_no_optimization (); }
1731 unsigned int execute () { return tree_lower_complex (); }
1732
1733 }; // class pass_lower_complex_O0
1734
1735 } // anon namespace
1736
1737 gimple_opt_pass *
1738 make_pass_lower_complex_O0 (gcc::context *ctxt)
1739 {
1740 return new pass_lower_complex_O0 (ctxt);
1741 }