OSDN Git Service

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