OSDN Git Service

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