OSDN Git Service

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