OSDN Git Service

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