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