OSDN Git Service

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