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