OSDN Git Service

a675f0aa136d07f8feba44e70ced11e3348be815
[pf3gnuchains/gcc-fork.git] / gcc / tree-complex.c
1 /* Lower complex number operations to scalar operations.
2    Copyright (C) 2004, 2005 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 2, 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 COPYING.  If not, write to the Free
18 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301, USA.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "real.h"
28 #include "flags.h"
29 #include "tree-flow.h"
30 #include "tree-gimple.h"
31 #include "tree-iterator.h"
32 #include "tree-pass.h"
33 #include "tree-ssa-propagate.h"
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 = xmalloc (sizeof (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 = default_def (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) != MODIFY_EXPR)
209                 break;
210               /* FALLTHRU */
211
212             case MODIFY_EXPR:
213               dsa = !is_complex_reg (TREE_OPERAND (stmt, 0));
214               rhs = TREE_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) != MODIFY_EXPR)
270     return SSA_PROP_VARYING;
271
272   lhs = TREE_OPERAND (stmt, 0);
273   rhs = TREE_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_tmp_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 pre-emptively
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 = build2 (MODIFY_EXPR, TREE_TYPE (comp), 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 PARM_DECL:
561     case INDIRECT_REF:
562     case COMPONENT_REF:
563     case ARRAY_REF:
564       {
565         tree inner_type = TREE_TYPE (TREE_TYPE (t));
566
567         t = build1 ((imagpart_p ? IMAGPART_EXPR : REALPART_EXPR),
568                     inner_type, unshare_expr (t));
569
570         if (gimple_p)
571           t = gimplify_val (bsi, inner_type, t);
572
573         return t;
574       }
575
576     case SSA_NAME:
577       return get_component_ssa_name (t, imagpart_p);
578
579     default:
580       gcc_unreachable ();
581     }
582 }
583
584 /* Update the complex components of the ssa name on the lhs of STMT.  */
585
586 static void
587 update_complex_components (block_stmt_iterator *bsi, tree stmt, tree r, tree i)
588 {
589   tree lhs = TREE_OPERAND (stmt, 0);
590   tree list;
591
592   list = set_component_ssa_name (lhs, false, r);
593   if (list)
594     bsi_insert_after (bsi, list, BSI_CONTINUE_LINKING);
595
596   list = set_component_ssa_name (lhs, true, i);
597   if (list)
598     bsi_insert_after (bsi, list, BSI_CONTINUE_LINKING);
599 }
600
601 static void
602 update_complex_components_on_edge (edge e, tree lhs, tree r, tree i)
603 {
604   tree list;
605
606   list = set_component_ssa_name (lhs, false, r);
607   if (list)
608     bsi_insert_on_edge (e, list);
609
610   list = set_component_ssa_name (lhs, true, i);
611   if (list)
612     bsi_insert_on_edge (e, list);
613 }
614
615 /* Update an assignment to a complex variable in place.  */
616
617 static void
618 update_complex_assignment (block_stmt_iterator *bsi, tree r, tree i)
619 {
620   tree stmt, mod;
621   tree type;
622
623   mod = stmt = bsi_stmt (*bsi);
624   if (TREE_CODE (stmt) == RETURN_EXPR)
625     mod = TREE_OPERAND (mod, 0);
626   else if (in_ssa_p)
627     update_complex_components (bsi, stmt, r, i);
628   
629   type = TREE_TYPE (TREE_OPERAND (mod, 1));
630   TREE_OPERAND (mod, 1) = build (COMPLEX_EXPR, type, r, i);
631   update_stmt (stmt);
632 }
633
634 /* Generate code at the entry point of the function to initialize the
635    component variables for a complex parameter.  */
636
637 static void
638 update_parameter_components (void)
639 {
640   edge entry_edge = single_succ_edge (ENTRY_BLOCK_PTR);
641   tree parm;
642
643   for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = TREE_CHAIN (parm))
644     {
645       tree type = TREE_TYPE (parm);
646       tree ssa_name, r, i;
647
648       if (TREE_CODE (type) != COMPLEX_TYPE || !is_gimple_reg (parm))
649         continue;
650
651       type = TREE_TYPE (type);
652       ssa_name = default_def (parm);
653
654       r = build1 (REALPART_EXPR, type, ssa_name);
655       i = build1 (IMAGPART_EXPR, type, ssa_name);
656       update_complex_components_on_edge (entry_edge, ssa_name, r, i);
657     }
658 }
659
660 /* Generate code to set the component variables of a complex variable
661    to match the PHI statements in block BB.  */
662
663 static void
664 update_phi_components (basic_block bb)
665 {
666   tree phi;
667
668   for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
669     if (is_complex_reg (PHI_RESULT (phi)))
670       {
671         tree lr, li, pr = NULL, pi = NULL;
672         unsigned int i, n;
673
674         lr = get_component_ssa_name (PHI_RESULT (phi), false);
675         if (TREE_CODE (lr) == SSA_NAME)
676           {
677             pr = create_phi_node (lr, bb);
678             SSA_NAME_DEF_STMT (lr) = pr;
679           }
680
681         li = get_component_ssa_name (PHI_RESULT (phi), true);
682         if (TREE_CODE (li) == SSA_NAME)
683           {
684             pi = create_phi_node (li, bb);
685             SSA_NAME_DEF_STMT (li) = pi;
686           }
687         
688         for (i = 0, n = PHI_NUM_ARGS (phi); i < n; ++i)
689           {
690             tree comp, arg = PHI_ARG_DEF (phi, i);
691             if (pr)
692               {
693                 comp = extract_component (NULL, arg, false, false);
694                 SET_PHI_ARG_DEF (pr, i, comp);
695               }
696             if (pi)
697               {
698                 comp = extract_component (NULL, arg, true, false);
699                 SET_PHI_ARG_DEF (pi, i, comp);
700               }
701           }
702       }
703 }
704
705 /* Mark each virtual op in STMT for ssa update.  */
706
707 static void
708 update_all_vops (tree stmt)
709 {
710   ssa_op_iter iter;
711   tree sym;
712
713   FOR_EACH_SSA_TREE_OPERAND (sym, stmt, iter, SSA_OP_ALL_VIRTUALS)
714     {
715       if (TREE_CODE (sym) == SSA_NAME)
716         sym = SSA_NAME_VAR (sym);
717       mark_sym_for_renaming (sym);
718     }
719 }
720
721 /* Expand a complex move to scalars.  */
722
723 static void
724 expand_complex_move (block_stmt_iterator *bsi, tree stmt, tree type,
725                      tree lhs, tree rhs)
726 {
727   tree inner_type = TREE_TYPE (type);
728   tree r, i;
729
730   if (TREE_CODE (lhs) == SSA_NAME)
731     {
732       if (is_ctrl_altering_stmt (bsi_stmt (*bsi)))
733         {
734           edge_iterator ei;
735           edge e;
736
737           /* The value is not assigned on the exception edges, so we need not
738              concern ourselves there.  We do need to update on the fallthru
739              edge.  Find it.  */
740           FOR_EACH_EDGE (e, ei, bsi->bb->succs)
741             if (e->flags & EDGE_FALLTHRU)
742               goto found_fallthru;
743           gcc_unreachable ();
744         found_fallthru:
745
746           r = build1 (REALPART_EXPR, inner_type, lhs);
747           i = build1 (IMAGPART_EXPR, inner_type, lhs);
748           update_complex_components_on_edge (e, lhs, r, i);
749         }
750       else if (TREE_CODE (rhs) == CALL_EXPR || TREE_SIDE_EFFECTS (rhs))
751         {
752           r = build1 (REALPART_EXPR, inner_type, lhs);
753           i = build1 (IMAGPART_EXPR, inner_type, lhs);
754           update_complex_components (bsi, stmt, r, i);
755         }
756       else
757         {
758           update_all_vops (bsi_stmt (*bsi));
759           r = extract_component (bsi, rhs, 0, true);
760           i = extract_component (bsi, rhs, 1, true);
761           update_complex_assignment (bsi, r, i);
762         }
763     }
764   else if (TREE_CODE (rhs) == SSA_NAME && !TREE_SIDE_EFFECTS (lhs))
765     {
766       tree x;
767
768       r = extract_component (bsi, rhs, 0, false);
769       i = extract_component (bsi, rhs, 1, false);
770
771       x = build1 (REALPART_EXPR, inner_type, unshare_expr (lhs));
772       x = build2 (MODIFY_EXPR, inner_type, x, r);
773       bsi_insert_before (bsi, x, BSI_SAME_STMT);
774
775       if (stmt == bsi_stmt (*bsi))
776         {
777           x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
778           TREE_OPERAND (stmt, 0) = x;
779           TREE_OPERAND (stmt, 1) = i;
780           TREE_TYPE (stmt) = inner_type;
781         }
782       else
783         {
784           x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
785           x = build2 (MODIFY_EXPR, inner_type, x, i);
786           bsi_insert_before (bsi, x, BSI_SAME_STMT);
787
788           stmt = bsi_stmt (*bsi);
789           gcc_assert (TREE_CODE (stmt) == RETURN_EXPR);
790           TREE_OPERAND (stmt, 0) = lhs;
791         }
792
793       update_all_vops (stmt);
794       update_stmt (stmt);
795     }
796 }
797
798 /* Expand complex addition to scalars:
799         a + b = (ar + br) + i(ai + bi)
800         a - b = (ar - br) + i(ai + bi)
801 */
802
803 static void
804 expand_complex_addition (block_stmt_iterator *bsi, tree inner_type,
805                          tree ar, tree ai, tree br, tree bi,
806                          enum tree_code code,
807                          complex_lattice_t al, complex_lattice_t bl)
808 {
809   tree rr, ri;
810
811   switch (PAIR (al, bl))
812     {
813     case PAIR (ONLY_REAL, ONLY_REAL):
814       rr = gimplify_build2 (bsi, code, inner_type, ar, br);
815       ri = ai;
816       break;
817
818     case PAIR (ONLY_REAL, ONLY_IMAG):
819       rr = ar;
820       if (code == MINUS_EXPR)
821         ri = gimplify_build2 (bsi, MINUS_EXPR, inner_type, ai, bi);
822       else
823         ri = bi;
824       break;
825
826     case PAIR (ONLY_IMAG, ONLY_REAL):
827       if (code == MINUS_EXPR)
828         rr = gimplify_build2 (bsi, MINUS_EXPR, inner_type, ar, br);
829       else
830         rr = br;
831       ri = ai;
832       break;
833
834     case PAIR (ONLY_IMAG, ONLY_IMAG):
835       rr = ar;
836       ri = gimplify_build2 (bsi, code, inner_type, ai, bi);
837       break;
838
839     case PAIR (VARYING, ONLY_REAL):
840       rr = gimplify_build2 (bsi, code, inner_type, ar, br);
841       ri = ai;
842       break;
843
844     case PAIR (VARYING, ONLY_IMAG):
845       rr = ar;
846       ri = gimplify_build2 (bsi, MINUS_EXPR, inner_type, ai, bi);
847       break;
848
849     case PAIR (ONLY_REAL, VARYING):
850       if (code == MINUS_EXPR)
851         goto general;
852       rr = gimplify_build2 (bsi, code, inner_type, ar, br);
853       ri = bi;
854       break;
855
856     case PAIR (ONLY_IMAG, VARYING):
857       if (code == MINUS_EXPR)
858         goto general;
859       rr = br;
860       ri = gimplify_build2 (bsi, MINUS_EXPR, inner_type, ai, bi);
861       break;
862
863     case PAIR (VARYING, VARYING):
864     general:
865       rr = gimplify_build2 (bsi, code, inner_type, ar, br);
866       ri = gimplify_build2 (bsi, code, inner_type, ai, bi);
867       break;
868
869     default:
870       gcc_unreachable ();
871     }
872
873   update_complex_assignment (bsi, rr, ri);
874 }
875
876 /* Expand a complex multiplication or division to a libcall to the c99
877    compliant routines.  */
878
879 static void
880 expand_complex_libcall (block_stmt_iterator *bsi, tree ar, tree ai,
881                         tree br, tree bi, enum tree_code code)
882 {
883   enum machine_mode mode;
884   enum built_in_function bcode;
885   tree args, fn, stmt, type;
886
887   args = tree_cons (NULL, bi, NULL);
888   args = tree_cons (NULL, br, args);
889   args = tree_cons (NULL, ai, args);
890   args = tree_cons (NULL, ar, args);
891
892   stmt = bsi_stmt (*bsi);
893   type = TREE_TYPE (TREE_OPERAND (stmt, 1));
894
895   mode = TYPE_MODE (type);
896   gcc_assert (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT);
897   if (code == MULT_EXPR)
898     bcode = BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT;
899   else if (code == RDIV_EXPR)
900     bcode = BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT;
901   else
902     gcc_unreachable ();
903   fn = built_in_decls[bcode];
904
905   TREE_OPERAND (stmt, 1)
906     = build3 (CALL_EXPR, type, build_fold_addr_expr (fn), args, NULL);
907   update_stmt (stmt);
908
909   if (in_ssa_p)
910     {
911       tree lhs = TREE_OPERAND (stmt, 0);
912       type = TREE_TYPE (type);
913       update_complex_components (bsi, stmt,
914                                  build1 (REALPART_EXPR, type, lhs),
915                                  build1 (IMAGPART_EXPR, type, lhs));
916     }
917 }
918
919 /* Expand complex multiplication to scalars:
920         a * b = (ar*br - ai*bi) + i(ar*bi + br*ai)
921 */
922
923 static void
924 expand_complex_multiplication (block_stmt_iterator *bsi, tree inner_type,
925                                tree ar, tree ai, tree br, tree bi,
926                                complex_lattice_t al, complex_lattice_t bl)
927 {
928   tree rr, ri;
929
930   if (al < bl)
931     {
932       complex_lattice_t tl;
933       rr = ar, ar = br, br = rr;
934       ri = ai, ai = bi, bi = ri;
935       tl = al, al = bl, bl = tl;
936     }
937
938   switch (PAIR (al, bl))
939     {
940     case PAIR (ONLY_REAL, ONLY_REAL):
941       rr = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, br);
942       ri = ai;
943       break;
944
945     case PAIR (ONLY_IMAG, ONLY_REAL):
946       rr = ar;
947       if (TREE_CODE (ai) == REAL_CST
948           && REAL_VALUES_IDENTICAL (TREE_REAL_CST (ai), dconst1))
949         ri = br;
950       else
951         ri = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, br);
952       break;
953
954     case PAIR (ONLY_IMAG, ONLY_IMAG):
955       rr = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, bi);
956       rr = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, rr);
957       ri = ar;
958       break;
959
960     case PAIR (VARYING, ONLY_REAL):
961       rr = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, br);
962       ri = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, br);
963       break;
964
965     case PAIR (VARYING, ONLY_IMAG):
966       rr = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, bi);
967       rr = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, rr);
968       ri = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, bi);
969       break;
970
971     case PAIR (VARYING, VARYING):
972       if (flag_complex_method == 2 && SCALAR_FLOAT_TYPE_P (inner_type))
973         {
974           expand_complex_libcall (bsi, ar, ai, br, bi, MULT_EXPR);
975           return;
976         }
977       else
978         {
979           tree t1, t2, t3, t4;
980
981           t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, br);
982           t2 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, bi);
983           t3 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, bi);
984
985           /* Avoid expanding redundant multiplication for the common
986              case of squaring a complex number.  */
987           if (ar == br && ai == bi)
988             t4 = t3;
989           else
990             t4 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, br);
991
992           rr = gimplify_build2 (bsi, MINUS_EXPR, inner_type, t1, t2);
993           ri = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t3, t4);
994         }
995       break;
996
997     default:
998       gcc_unreachable ();
999     }
1000
1001   update_complex_assignment (bsi, rr, ri);
1002 }
1003
1004 /* Expand complex division to scalars, straightforward algorithm.
1005         a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1006             t = br*br + bi*bi
1007 */
1008
1009 static void
1010 expand_complex_div_straight (block_stmt_iterator *bsi, tree inner_type,
1011                              tree ar, tree ai, tree br, tree bi,
1012                              enum tree_code code)
1013 {
1014   tree rr, ri, div, t1, t2, t3;
1015
1016   t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, br, br);
1017   t2 = gimplify_build2 (bsi, MULT_EXPR, inner_type, bi, bi);
1018   div = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, t2);
1019
1020   t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, br);
1021   t2 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, bi);
1022   t3 = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, t2);
1023   rr = gimplify_build2 (bsi, code, inner_type, t3, div);
1024
1025   t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, br);
1026   t2 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, bi);
1027   t3 = gimplify_build2 (bsi, MINUS_EXPR, inner_type, t1, t2);
1028   ri = gimplify_build2 (bsi, code, inner_type, t3, div);
1029
1030   update_complex_assignment (bsi, rr, ri);
1031 }
1032
1033 /* Expand complex division to scalars, modified algorithm to minimize
1034    overflow with wide input ranges.  */
1035
1036 static void
1037 expand_complex_div_wide (block_stmt_iterator *bsi, tree inner_type,
1038                          tree ar, tree ai, tree br, tree bi,
1039                          enum tree_code code)
1040 {
1041   tree rr, ri, ratio, div, t1, t2, tr, ti, cond;
1042   basic_block bb_cond, bb_true, bb_false, bb_join;
1043
1044   /* Examine |br| < |bi|, and branch.  */
1045   t1 = gimplify_build1 (bsi, ABS_EXPR, inner_type, br);
1046   t2 = gimplify_build1 (bsi, ABS_EXPR, inner_type, bi);
1047   cond = fold_build2 (LT_EXPR, boolean_type_node, t1, t2);
1048   STRIP_NOPS (cond);
1049
1050   bb_cond = bb_true = bb_false = bb_join = NULL;
1051   rr = ri = tr = ti = NULL;
1052   if (!TREE_CONSTANT (cond))
1053     {
1054       edge e;
1055
1056       cond = build (COND_EXPR, void_type_node, cond, NULL, NULL);
1057       bsi_insert_before (bsi, cond, BSI_SAME_STMT);
1058
1059       /* Split the original block, and create the TRUE and FALSE blocks.  */
1060       e = split_block (bsi->bb, cond);
1061       bb_cond = e->src;
1062       bb_join = e->dest;
1063       bb_true = create_empty_bb (bb_cond);
1064       bb_false = create_empty_bb (bb_true);
1065
1066       t1 = build (GOTO_EXPR, void_type_node, tree_block_label (bb_true));
1067       t2 = build (GOTO_EXPR, void_type_node, tree_block_label (bb_false));
1068       COND_EXPR_THEN (cond) = t1;
1069       COND_EXPR_ELSE (cond) = t2;
1070
1071       /* Wire the blocks together.  */
1072       e->flags = EDGE_TRUE_VALUE;
1073       redirect_edge_succ (e, bb_true);
1074       make_edge (bb_cond, bb_false, EDGE_FALSE_VALUE);
1075       make_edge (bb_true, bb_join, EDGE_FALLTHRU);
1076       make_edge (bb_false, bb_join, EDGE_FALLTHRU);
1077
1078       /* Update dominance info.  Note that bb_join's data was
1079          updated by split_block.  */
1080       if (dom_info_available_p (CDI_DOMINATORS))
1081         {
1082           set_immediate_dominator (CDI_DOMINATORS, bb_true, bb_cond);
1083           set_immediate_dominator (CDI_DOMINATORS, bb_false, bb_cond);
1084         }
1085
1086       rr = make_rename_temp (inner_type, NULL);
1087       ri = make_rename_temp (inner_type, NULL);
1088     }
1089
1090   /* In the TRUE branch, we compute
1091       ratio = br/bi;
1092       div = (br * ratio) + bi;
1093       tr = (ar * ratio) + ai;
1094       ti = (ai * ratio) - ar;
1095       tr = tr / div;
1096       ti = ti / div;  */
1097   if (bb_true || integer_nonzerop (cond))
1098     {
1099       if (bb_true)
1100         {
1101           *bsi = bsi_last (bb_true);
1102           bsi_insert_after (bsi, build_empty_stmt (), BSI_NEW_STMT);
1103         }
1104
1105       ratio = gimplify_build2 (bsi, code, inner_type, br, bi);
1106
1107       t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, br, ratio);
1108       div = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, bi);
1109
1110       t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, ratio);
1111       tr = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, ai);
1112
1113       t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, ratio);
1114       ti = gimplify_build2 (bsi, MINUS_EXPR, inner_type, t1, ar);
1115
1116       tr = gimplify_build2 (bsi, code, inner_type, tr, div);
1117       ti = gimplify_build2 (bsi, code, inner_type, ti, div);
1118
1119      if (bb_true)
1120        {
1121          t1 = build (MODIFY_EXPR, inner_type, rr, tr);
1122          bsi_insert_before (bsi, t1, BSI_SAME_STMT);
1123          t1 = build (MODIFY_EXPR, inner_type, ri, ti);
1124          bsi_insert_before (bsi, t1, BSI_SAME_STMT);
1125          bsi_remove (bsi);
1126        }
1127     }
1128
1129   /* In the FALSE branch, we compute
1130       ratio = d/c;
1131       divisor = (d * ratio) + c;
1132       tr = (b * ratio) + a;
1133       ti = b - (a * ratio);
1134       tr = tr / div;
1135       ti = ti / div;  */
1136   if (bb_false || integer_zerop (cond))
1137     {
1138       if (bb_false)
1139         {
1140           *bsi = bsi_last (bb_false);
1141           bsi_insert_after (bsi, build_empty_stmt (), BSI_NEW_STMT);
1142         }
1143
1144       ratio = gimplify_build2 (bsi, code, inner_type, bi, br);
1145
1146       t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, bi, ratio);
1147       div = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, br);
1148
1149       t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ai, ratio);
1150       tr = gimplify_build2 (bsi, PLUS_EXPR, inner_type, t1, ar);
1151
1152       t1 = gimplify_build2 (bsi, MULT_EXPR, inner_type, ar, ratio);
1153       ti = gimplify_build2 (bsi, MINUS_EXPR, inner_type, ai, t1);
1154
1155       tr = gimplify_build2 (bsi, code, inner_type, tr, div);
1156       ti = gimplify_build2 (bsi, code, inner_type, ti, div);
1157
1158      if (bb_false)
1159        {
1160          t1 = build (MODIFY_EXPR, inner_type, rr, tr);
1161          bsi_insert_before (bsi, t1, BSI_SAME_STMT);
1162          t1 = build (MODIFY_EXPR, inner_type, ri, ti);
1163          bsi_insert_before (bsi, t1, BSI_SAME_STMT);
1164          bsi_remove (bsi);
1165        }
1166     }
1167
1168   if (bb_join)
1169     *bsi = bsi_start (bb_join);
1170   else
1171     rr = tr, ri = ti;
1172
1173   update_complex_assignment (bsi, rr, ri);
1174 }
1175
1176 /* Expand complex division to scalars.  */
1177
1178 static void
1179 expand_complex_division (block_stmt_iterator *bsi, tree inner_type,
1180                          tree ar, tree ai, tree br, tree bi,
1181                          enum tree_code code,
1182                          complex_lattice_t al, complex_lattice_t bl)
1183 {
1184   tree rr, ri;
1185
1186   switch (PAIR (al, bl))
1187     {
1188     case PAIR (ONLY_REAL, ONLY_REAL):
1189       rr = gimplify_build2 (bsi, code, inner_type, ar, br);
1190       ri = ai;
1191       break;
1192
1193     case PAIR (ONLY_REAL, ONLY_IMAG):
1194       rr = ai;
1195       ri = gimplify_build2 (bsi, code, inner_type, ar, bi);
1196       ri = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, ri);
1197       break;
1198
1199     case PAIR (ONLY_IMAG, ONLY_REAL):
1200       rr = ar;
1201       ri = gimplify_build2 (bsi, code, inner_type, ai, br);
1202       break;
1203
1204     case PAIR (ONLY_IMAG, ONLY_IMAG):
1205       rr = gimplify_build2 (bsi, code, inner_type, ai, bi);
1206       ri = ar;
1207       break;
1208
1209     case PAIR (VARYING, ONLY_REAL):
1210       rr = gimplify_build2 (bsi, code, inner_type, ar, br);
1211       ri = gimplify_build2 (bsi, code, inner_type, ai, br);
1212       break;
1213
1214     case PAIR (VARYING, ONLY_IMAG):
1215       rr = gimplify_build2 (bsi, code, inner_type, ai, bi);
1216       ri = gimplify_build2 (bsi, code, inner_type, ar, bi);
1217       ri = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, ri);
1218
1219     case PAIR (ONLY_REAL, VARYING):
1220     case PAIR (ONLY_IMAG, VARYING):
1221     case PAIR (VARYING, VARYING):
1222       switch (flag_complex_method)
1223         {
1224         case 0:
1225           /* straightforward implementation of complex divide acceptable.  */
1226           expand_complex_div_straight (bsi, inner_type, ar, ai, br, bi, code);
1227           break;
1228
1229         case 2:
1230           if (SCALAR_FLOAT_TYPE_P (inner_type))
1231             {
1232               expand_complex_libcall (bsi, ar, ai, br, bi, code);
1233               break;
1234             }
1235           /* FALLTHRU */
1236
1237         case 1:
1238           /* wide ranges of inputs must work for complex divide.  */
1239           expand_complex_div_wide (bsi, inner_type, ar, ai, br, bi, code);
1240           break;
1241
1242         default:
1243           gcc_unreachable ();
1244         }
1245       return;
1246
1247     default:
1248       gcc_unreachable ();
1249     }
1250
1251   update_complex_assignment (bsi, rr, ri);
1252 }
1253
1254 /* Expand complex negation to scalars:
1255         -a = (-ar) + i(-ai)
1256 */
1257
1258 static void
1259 expand_complex_negation (block_stmt_iterator *bsi, tree inner_type,
1260                          tree ar, tree ai)
1261 {
1262   tree rr, ri;
1263
1264   rr = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, ar);
1265   ri = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, ai);
1266
1267   update_complex_assignment (bsi, rr, ri);
1268 }
1269
1270 /* Expand complex conjugate to scalars:
1271         ~a = (ar) + i(-ai)
1272 */
1273
1274 static void
1275 expand_complex_conjugate (block_stmt_iterator *bsi, tree inner_type,
1276                           tree ar, tree ai)
1277 {
1278   tree ri;
1279
1280   ri = gimplify_build1 (bsi, NEGATE_EXPR, inner_type, ai);
1281
1282   update_complex_assignment (bsi, ar, ri);
1283 }
1284
1285 /* Expand complex comparison (EQ or NE only).  */
1286
1287 static void
1288 expand_complex_comparison (block_stmt_iterator *bsi, tree ar, tree ai,
1289                            tree br, tree bi, enum tree_code code)
1290 {
1291   tree cr, ci, cc, stmt, expr, type;
1292
1293   cr = gimplify_build2 (bsi, code, boolean_type_node, ar, br);
1294   ci = gimplify_build2 (bsi, code, boolean_type_node, ai, bi);
1295   cc = gimplify_build2 (bsi,
1296                         (code == EQ_EXPR ? TRUTH_AND_EXPR : TRUTH_OR_EXPR),
1297                         boolean_type_node, cr, ci);
1298
1299   stmt = expr = bsi_stmt (*bsi);
1300
1301   switch (TREE_CODE (stmt))
1302     {
1303     case RETURN_EXPR:
1304       expr = TREE_OPERAND (stmt, 0);
1305       /* FALLTHRU */
1306     case MODIFY_EXPR:
1307       type = TREE_TYPE (TREE_OPERAND (expr, 1));
1308       TREE_OPERAND (expr, 1) = fold_convert (type, cc);
1309       break;
1310     case COND_EXPR:
1311       TREE_OPERAND (stmt, 0) = cc;
1312       break;
1313     default:
1314       gcc_unreachable ();
1315     }
1316
1317   update_stmt (stmt);
1318 }
1319
1320 /* Process one statement.  If we identify a complex operation, expand it.  */
1321
1322 static void
1323 expand_complex_operations_1 (block_stmt_iterator *bsi)
1324 {
1325   tree stmt = bsi_stmt (*bsi);
1326   tree rhs, type, inner_type;
1327   tree ac, ar, ai, bc, br, bi;
1328   complex_lattice_t al, bl;
1329   enum tree_code code;
1330
1331   switch (TREE_CODE (stmt))
1332     {
1333     case RETURN_EXPR:
1334       stmt = TREE_OPERAND (stmt, 0);
1335       if (!stmt)
1336         return;
1337       if (TREE_CODE (stmt) != MODIFY_EXPR)
1338         return;
1339       /* FALLTHRU */
1340
1341     case MODIFY_EXPR:
1342       rhs = TREE_OPERAND (stmt, 1);
1343       break;
1344
1345     case COND_EXPR:
1346       rhs = TREE_OPERAND (stmt, 0);
1347       break;
1348
1349     default:
1350       return;
1351     }
1352
1353   type = TREE_TYPE (rhs);
1354   code = TREE_CODE (rhs);
1355
1356   /* Initial filter for operations we handle.  */
1357   switch (code)
1358     {
1359     case PLUS_EXPR:
1360     case MINUS_EXPR:
1361     case MULT_EXPR:
1362     case TRUNC_DIV_EXPR:
1363     case CEIL_DIV_EXPR:
1364     case FLOOR_DIV_EXPR:
1365     case ROUND_DIV_EXPR:
1366     case RDIV_EXPR:
1367     case NEGATE_EXPR:
1368     case CONJ_EXPR:
1369       if (TREE_CODE (type) != COMPLEX_TYPE)
1370         return;
1371       inner_type = TREE_TYPE (type);
1372       break;
1373
1374     case EQ_EXPR:
1375     case NE_EXPR:
1376       inner_type = TREE_TYPE (TREE_OPERAND (rhs, 1));
1377       if (TREE_CODE (inner_type) != COMPLEX_TYPE)
1378         return;
1379       break;
1380
1381     default:
1382       {
1383         tree lhs = TREE_OPERAND (stmt, 0);
1384         tree rhs = TREE_OPERAND (stmt, 1);
1385
1386         if (TREE_CODE (type) == COMPLEX_TYPE)
1387           expand_complex_move (bsi, stmt, type, lhs, rhs);
1388         else if ((TREE_CODE (rhs) == REALPART_EXPR
1389                   || TREE_CODE (rhs) == IMAGPART_EXPR)
1390                  && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
1391           {
1392             TREE_OPERAND (stmt, 1)
1393               = extract_component (bsi, TREE_OPERAND (rhs, 0),
1394                                    TREE_CODE (rhs) == IMAGPART_EXPR, false);
1395             update_stmt (stmt);
1396           }
1397       }
1398       return;
1399     }
1400
1401   /* Extract the components of the two complex values.  Make sure and
1402      handle the common case of the same value used twice specially.  */
1403   ac = TREE_OPERAND (rhs, 0);
1404   ar = extract_component (bsi, ac, 0, true);
1405   ai = extract_component (bsi, ac, 1, true);
1406
1407   if (TREE_CODE_CLASS (code) == tcc_unary)
1408     bc = br = bi = NULL;
1409   else
1410     {
1411       bc = TREE_OPERAND (rhs, 1);
1412       if (ac == bc)
1413         br = ar, bi = ai;
1414       else
1415         {
1416           br = extract_component (bsi, bc, 0, true);
1417           bi = extract_component (bsi, bc, 1, true);
1418         }
1419     }
1420
1421   if (in_ssa_p)
1422     {
1423       al = find_lattice_value (ac);
1424       if (al == UNINITIALIZED)
1425         al = VARYING;
1426
1427       if (TREE_CODE_CLASS (code) == tcc_unary)
1428         bl = UNINITIALIZED;
1429       else if (ac == bc)
1430         bl = al;
1431       else
1432         {
1433           bl = find_lattice_value (bc);
1434           if (bl == UNINITIALIZED)
1435             bl = VARYING;
1436         }
1437     }
1438   else
1439     al = bl = VARYING;
1440
1441   switch (code)
1442     {
1443     case PLUS_EXPR:
1444     case MINUS_EXPR:
1445       expand_complex_addition (bsi, inner_type, ar, ai, br, bi, code, al, bl);
1446       break;
1447
1448     case MULT_EXPR:
1449       expand_complex_multiplication (bsi, inner_type, ar, ai, br, bi, al, bl);
1450       break;
1451
1452     case TRUNC_DIV_EXPR:
1453     case CEIL_DIV_EXPR:
1454     case FLOOR_DIV_EXPR:
1455     case ROUND_DIV_EXPR:
1456     case RDIV_EXPR:
1457       expand_complex_division (bsi, inner_type, ar, ai, br, bi, code, al, bl);
1458       break;
1459       
1460     case NEGATE_EXPR:
1461       expand_complex_negation (bsi, inner_type, ar, ai);
1462       break;
1463
1464     case CONJ_EXPR:
1465       expand_complex_conjugate (bsi, inner_type, ar, ai);
1466       break;
1467
1468     case EQ_EXPR:
1469     case NE_EXPR:
1470       expand_complex_comparison (bsi, ar, ai, br, bi, code);
1471       break;
1472
1473     default:
1474       gcc_unreachable ();
1475     }
1476 }
1477
1478 \f
1479 /* Entry point for complex operation lowering during optimization.  */
1480
1481 static void
1482 tree_lower_complex (void)
1483 {
1484   int old_last_basic_block;
1485   block_stmt_iterator bsi;
1486   basic_block bb;
1487
1488   if (!init_dont_simulate_again ())
1489     return;
1490
1491   complex_lattice_values = VEC_alloc (complex_lattice_t, heap, num_ssa_names);
1492   VEC_safe_grow (complex_lattice_t, heap,
1493                  complex_lattice_values, num_ssa_names);
1494   memset (VEC_address (complex_lattice_t, complex_lattice_values), 0,
1495           num_ssa_names * sizeof(complex_lattice_t));
1496
1497   init_parameter_lattice_values ();
1498   ssa_propagate (complex_visit_stmt, complex_visit_phi);
1499
1500   complex_variable_components = htab_create (10,  int_tree_map_hash,
1501                                              int_tree_map_eq, free);
1502
1503   complex_ssa_name_components = VEC_alloc (tree, heap, 2*num_ssa_names);
1504   VEC_safe_grow (tree, heap, complex_ssa_name_components, 2*num_ssa_names);
1505   memset (VEC_address (tree, complex_ssa_name_components), 0,
1506           2 * num_ssa_names * sizeof(tree));
1507
1508   update_parameter_components ();
1509
1510   /* ??? Ideally we'd traverse the blocks in breadth-first order.  */
1511   old_last_basic_block = last_basic_block;
1512   FOR_EACH_BB (bb)
1513     {
1514       if (bb->index >= old_last_basic_block)
1515         continue;
1516       update_phi_components (bb);
1517       for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1518         expand_complex_operations_1 (&bsi);
1519     }
1520
1521   bsi_commit_edge_inserts ();
1522
1523   htab_delete (complex_variable_components);
1524   VEC_free (tree, heap, complex_ssa_name_components);
1525   VEC_free (complex_lattice_t, heap, complex_lattice_values);
1526 }
1527
1528 struct tree_opt_pass pass_lower_complex = 
1529 {
1530   "cplxlower",                          /* name */
1531   0,                                    /* gate */
1532   tree_lower_complex,                   /* execute */
1533   NULL,                                 /* sub */
1534   NULL,                                 /* next */
1535   0,                                    /* static_pass_number */
1536   0,                                    /* tv_id */
1537   PROP_ssa,                             /* properties_required */
1538   0,                                    /* properties_provided */
1539   0,                                    /* properties_destroyed */
1540   0,                                    /* todo_flags_start */
1541   TODO_dump_func | TODO_ggc_collect
1542     | TODO_update_ssa
1543     | TODO_verify_stmts,                /* todo_flags_finish */
1544   0                                     /* letter */
1545 };
1546
1547 \f
1548 /* Entry point for complex operation lowering without optimization.  */
1549
1550 static void
1551 tree_lower_complex_O0 (void)
1552 {
1553   int old_last_basic_block = last_basic_block;
1554   block_stmt_iterator bsi;
1555   basic_block bb;
1556
1557   FOR_EACH_BB (bb)
1558     {
1559       if (bb->index >= old_last_basic_block)
1560         continue;
1561       for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1562         expand_complex_operations_1 (&bsi);
1563     }
1564 }
1565
1566 static bool
1567 gate_no_optimization (void)
1568 {
1569   return optimize == 0;
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 };