1 /* Optimization of PHI nodes by converting them into straightline code.
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 "insn-codes.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "cfghooks.h"
29 #include "tree-pass.h"
30 #include "ssa.h"
31 #include "optabs-tree.h"
32 #include "insn-config.h"
33 #include "gimple-pretty-print.h"
34 #include "fold-const.h"
35 #include "stor-layout.h"
36 #include "cfganal.h"
37 #include "gimplify.h"
38 #include "gimple-iterator.h"
39 #include "gimplify-me.h"
40 #include "tree-cfg.h"
41 #include "tree-dfa.h"
42 #include "domwalk.h"
43 #include "cfgloop.h"
44 #include "tree-data-ref.h"
45 #include "tree-scalar-evolution.h"
46 #include "tree-inline.h"
47 #include "params.h"
48 
49 static unsigned int tree_ssa_phiopt_worker (bool, bool);
50 static bool conditional_replacement (basic_block, basic_block,
51 				     edge, edge, gphi *, tree, tree);
52 static gphi *factor_out_conditional_conversion (edge, edge, gphi *, tree, tree,
53 						gimple *);
54 static int value_replacement (basic_block, basic_block,
55 			      edge, edge, gimple *, tree, tree);
56 static bool minmax_replacement (basic_block, basic_block,
57 				edge, edge, gimple *, tree, tree);
58 static bool abs_replacement (basic_block, basic_block,
59 			     edge, edge, gimple *, tree, tree);
60 static bool cond_store_replacement (basic_block, basic_block, edge, edge,
61 				    hash_set<tree> *);
62 static bool cond_if_else_store_replacement (basic_block, basic_block, basic_block);
63 static hash_set<tree> * get_non_trapping ();
64 static void replace_phi_edge_with_variable (basic_block, edge, gimple *, tree);
65 static void hoist_adjacent_loads (basic_block, basic_block,
66 				  basic_block, basic_block);
67 static bool gate_hoist_loads (void);
68 
69 /* This pass tries to transform conditional stores into unconditional
70    ones, enabling further simplifications with the simpler then and else
71    blocks.  In particular it replaces this:
72 
73      bb0:
74        if (cond) goto bb2; else goto bb1;
75      bb1:
76        *p = RHS;
77      bb2:
78 
79    with
80 
81      bb0:
82        if (cond) goto bb1; else goto bb2;
83      bb1:
84        condtmp' = *p;
85      bb2:
86        condtmp = PHI <RHS, condtmp'>
87        *p = condtmp;
88 
89    This transformation can only be done under several constraints,
90    documented below.  It also replaces:
91 
92      bb0:
93        if (cond) goto bb2; else goto bb1;
94      bb1:
95        *p = RHS1;
96        goto bb3;
97      bb2:
98        *p = RHS2;
99      bb3:
100 
101    with
102 
103      bb0:
104        if (cond) goto bb3; else goto bb1;
105      bb1:
106      bb3:
107        condtmp = PHI <RHS1, RHS2>
108        *p = condtmp;  */
109 
110 static unsigned int
tree_ssa_cs_elim(void)111 tree_ssa_cs_elim (void)
112 {
113   unsigned todo;
114   /* ???  We are not interested in loop related info, but the following
115      will create it, ICEing as we didn't init loops with pre-headers.
116      An interfacing issue of find_data_references_in_bb.  */
117   loop_optimizer_init (LOOPS_NORMAL);
118   scev_initialize ();
119   todo = tree_ssa_phiopt_worker (true, false);
120   scev_finalize ();
121   loop_optimizer_finalize ();
122   return todo;
123 }
124 
125 /* Return the singleton PHI in the SEQ of PHIs for edges E0 and E1. */
126 
127 static gphi *
single_non_singleton_phi_for_edges(gimple_seq seq,edge e0,edge e1)128 single_non_singleton_phi_for_edges (gimple_seq seq, edge e0, edge e1)
129 {
130   gimple_stmt_iterator i;
131   gphi *phi = NULL;
132   if (gimple_seq_singleton_p (seq))
133     return as_a <gphi *> (gsi_stmt (gsi_start (seq)));
134   for (i = gsi_start (seq); !gsi_end_p (i); gsi_next (&i))
135     {
136       gphi *p = as_a <gphi *> (gsi_stmt (i));
137       /* If the PHI arguments are equal then we can skip this PHI. */
138       if (operand_equal_for_phi_arg_p (gimple_phi_arg_def (p, e0->dest_idx),
139 				       gimple_phi_arg_def (p, e1->dest_idx)))
140 	continue;
141 
142       /* If we already have a PHI that has the two edge arguments are
143 	 different, then return it is not a singleton for these PHIs. */
144       if (phi)
145 	return NULL;
146 
147       phi = p;
148     }
149   return phi;
150 }
151 
152 /* The core routine of conditional store replacement and normal
153    phi optimizations.  Both share much of the infrastructure in how
154    to match applicable basic block patterns.  DO_STORE_ELIM is true
155    when we want to do conditional store replacement, false otherwise.
156    DO_HOIST_LOADS is true when we want to hoist adjacent loads out
157    of diamond control flow patterns, false otherwise.  */
158 static unsigned int
tree_ssa_phiopt_worker(bool do_store_elim,bool do_hoist_loads)159 tree_ssa_phiopt_worker (bool do_store_elim, bool do_hoist_loads)
160 {
161   basic_block bb;
162   basic_block *bb_order;
163   unsigned n, i;
164   bool cfgchanged = false;
165   hash_set<tree> *nontrap = 0;
166 
167   if (do_store_elim)
168     /* Calculate the set of non-trapping memory accesses.  */
169     nontrap = get_non_trapping ();
170 
171   /* Search every basic block for COND_EXPR we may be able to optimize.
172 
173      We walk the blocks in order that guarantees that a block with
174      a single predecessor is processed before the predecessor.
175      This ensures that we collapse inner ifs before visiting the
176      outer ones, and also that we do not try to visit a removed
177      block.  */
178   bb_order = single_pred_before_succ_order ();
179   n = n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS;
180 
181   for (i = 0; i < n; i++)
182     {
183       gimple *cond_stmt;
184       gphi *phi;
185       basic_block bb1, bb2;
186       edge e1, e2;
187       tree arg0, arg1;
188 
189       bb = bb_order[i];
190 
191       cond_stmt = last_stmt (bb);
192       /* Check to see if the last statement is a GIMPLE_COND.  */
193       if (!cond_stmt
194           || gimple_code (cond_stmt) != GIMPLE_COND)
195         continue;
196 
197       e1 = EDGE_SUCC (bb, 0);
198       bb1 = e1->dest;
199       e2 = EDGE_SUCC (bb, 1);
200       bb2 = e2->dest;
201 
202       /* We cannot do the optimization on abnormal edges.  */
203       if ((e1->flags & EDGE_ABNORMAL) != 0
204           || (e2->flags & EDGE_ABNORMAL) != 0)
205        continue;
206 
207       /* If either bb1's succ or bb2 or bb2's succ is non NULL.  */
208       if (EDGE_COUNT (bb1->succs) == 0
209           || bb2 == NULL
210 	  || EDGE_COUNT (bb2->succs) == 0)
211         continue;
212 
213       /* Find the bb which is the fall through to the other.  */
214       if (EDGE_SUCC (bb1, 0)->dest == bb2)
215         ;
216       else if (EDGE_SUCC (bb2, 0)->dest == bb1)
217         {
218 	  std::swap (bb1, bb2);
219 	  std::swap (e1, e2);
220 	}
221       else if (do_store_elim
222 	       && EDGE_SUCC (bb1, 0)->dest == EDGE_SUCC (bb2, 0)->dest)
223 	{
224 	  basic_block bb3 = EDGE_SUCC (bb1, 0)->dest;
225 
226 	  if (!single_succ_p (bb1)
227 	      || (EDGE_SUCC (bb1, 0)->flags & EDGE_FALLTHRU) == 0
228 	      || !single_succ_p (bb2)
229 	      || (EDGE_SUCC (bb2, 0)->flags & EDGE_FALLTHRU) == 0
230 	      || EDGE_COUNT (bb3->preds) != 2)
231 	    continue;
232 	  if (cond_if_else_store_replacement (bb1, bb2, bb3))
233 	    cfgchanged = true;
234 	  continue;
235 	}
236       else if (do_hoist_loads
237 	       && EDGE_SUCC (bb1, 0)->dest == EDGE_SUCC (bb2, 0)->dest)
238 	{
239 	  basic_block bb3 = EDGE_SUCC (bb1, 0)->dest;
240 
241 	  if (!FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (cond_stmt)))
242 	      && single_succ_p (bb1)
243 	      && single_succ_p (bb2)
244 	      && single_pred_p (bb1)
245 	      && single_pred_p (bb2)
246 	      && EDGE_COUNT (bb->succs) == 2
247 	      && EDGE_COUNT (bb3->preds) == 2
248 	      /* If one edge or the other is dominant, a conditional move
249 		 is likely to perform worse than the well-predicted branch.  */
250 	      && !predictable_edge_p (EDGE_SUCC (bb, 0))
251 	      && !predictable_edge_p (EDGE_SUCC (bb, 1)))
252 	    hoist_adjacent_loads (bb, bb1, bb2, bb3);
253 	  continue;
254 	}
255       else
256 	continue;
257 
258       e1 = EDGE_SUCC (bb1, 0);
259 
260       /* Make sure that bb1 is just a fall through.  */
261       if (!single_succ_p (bb1)
262 	  || (e1->flags & EDGE_FALLTHRU) == 0)
263         continue;
264 
265       /* Also make sure that bb1 only have one predecessor and that it
266 	 is bb.  */
267       if (!single_pred_p (bb1)
268           || single_pred (bb1) != bb)
269 	continue;
270 
271       if (do_store_elim)
272 	{
273 	  /* bb1 is the middle block, bb2 the join block, bb the split block,
274 	     e1 the fallthrough edge from bb1 to bb2.  We can't do the
275 	     optimization if the join block has more than two predecessors.  */
276 	  if (EDGE_COUNT (bb2->preds) > 2)
277 	    continue;
278 	  if (cond_store_replacement (bb1, bb2, e1, e2, nontrap))
279 	    cfgchanged = true;
280 	}
281       else
282 	{
283 	  gimple_seq phis = phi_nodes (bb2);
284 	  gimple_stmt_iterator gsi;
285 	  bool candorest = true;
286 
287 	  /* Value replacement can work with more than one PHI
288 	     so try that first. */
289 	  for (gsi = gsi_start (phis); !gsi_end_p (gsi); gsi_next (&gsi))
290 	    {
291 	      phi = as_a <gphi *> (gsi_stmt (gsi));
292 	      arg0 = gimple_phi_arg_def (phi, e1->dest_idx);
293 	      arg1 = gimple_phi_arg_def (phi, e2->dest_idx);
294 	      if (value_replacement (bb, bb1, e1, e2, phi, arg0, arg1) == 2)
295 		{
296 		  candorest = false;
297 	          cfgchanged = true;
298 		  break;
299 		}
300 	    }
301 
302 	  if (!candorest)
303 	    continue;
304 
305 	  phi = single_non_singleton_phi_for_edges (phis, e1, e2);
306 	  if (!phi)
307 	    continue;
308 
309 	  arg0 = gimple_phi_arg_def (phi, e1->dest_idx);
310 	  arg1 = gimple_phi_arg_def (phi, e2->dest_idx);
311 
312 	  /* Something is wrong if we cannot find the arguments in the PHI
313 	     node.  */
314 	  gcc_assert (arg0 != NULL_TREE && arg1 != NULL_TREE);
315 
316 	  gphi *newphi = factor_out_conditional_conversion (e1, e2, phi,
317 							    arg0, arg1,
318 							    cond_stmt);
319 	  if (newphi != NULL)
320 	    {
321 	      phi = newphi;
322 	      /* factor_out_conditional_conversion may create a new PHI in
323 		 BB2 and eliminate an existing PHI in BB2.  Recompute values
324 		 that may be affected by that change.  */
325 	      arg0 = gimple_phi_arg_def (phi, e1->dest_idx);
326 	      arg1 = gimple_phi_arg_def (phi, e2->dest_idx);
327 	      gcc_assert (arg0 != NULL_TREE && arg1 != NULL_TREE);
328 	    }
329 
330 	  /* Do the replacement of conditional if it can be done.  */
331 	  if (conditional_replacement (bb, bb1, e1, e2, phi, arg0, arg1))
332 	    cfgchanged = true;
333 	  else if (abs_replacement (bb, bb1, e1, e2, phi, arg0, arg1))
334 	    cfgchanged = true;
335 	  else if (minmax_replacement (bb, bb1, e1, e2, phi, arg0, arg1))
336 	    cfgchanged = true;
337 	}
338     }
339 
340   free (bb_order);
341 
342   if (do_store_elim)
343     delete nontrap;
344   /* If the CFG has changed, we should cleanup the CFG.  */
345   if (cfgchanged && do_store_elim)
346     {
347       /* In cond-store replacement we have added some loads on edges
348          and new VOPS (as we moved the store, and created a load).  */
349       gsi_commit_edge_inserts ();
350       return TODO_cleanup_cfg | TODO_update_ssa_only_virtuals;
351     }
352   else if (cfgchanged)
353     return TODO_cleanup_cfg;
354   return 0;
355 }
356 
357 /* Replace PHI node element whose edge is E in block BB with variable NEW.
358    Remove the edge from COND_BLOCK which does not lead to BB (COND_BLOCK
359    is known to have two edges, one of which must reach BB).  */
360 
361 static void
replace_phi_edge_with_variable(basic_block cond_block,edge e,gimple * phi,tree new_tree)362 replace_phi_edge_with_variable (basic_block cond_block,
363 				edge e, gimple *phi, tree new_tree)
364 {
365   basic_block bb = gimple_bb (phi);
366   basic_block block_to_remove;
367   gimple_stmt_iterator gsi;
368 
369   /* Change the PHI argument to new.  */
370   SET_USE (PHI_ARG_DEF_PTR (phi, e->dest_idx), new_tree);
371 
372   /* Remove the empty basic block.  */
373   if (EDGE_SUCC (cond_block, 0)->dest == bb)
374     {
375       EDGE_SUCC (cond_block, 0)->flags |= EDGE_FALLTHRU;
376       EDGE_SUCC (cond_block, 0)->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
377       EDGE_SUCC (cond_block, 0)->probability = profile_probability::always ();
378 
379       block_to_remove = EDGE_SUCC (cond_block, 1)->dest;
380     }
381   else
382     {
383       EDGE_SUCC (cond_block, 1)->flags |= EDGE_FALLTHRU;
384       EDGE_SUCC (cond_block, 1)->flags
385 	&= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
386       EDGE_SUCC (cond_block, 1)->probability = profile_probability::always ();
387 
388       block_to_remove = EDGE_SUCC (cond_block, 0)->dest;
389     }
390   delete_basic_block (block_to_remove);
391 
392   /* Eliminate the COND_EXPR at the end of COND_BLOCK.  */
393   gsi = gsi_last_bb (cond_block);
394   gsi_remove (&gsi, true);
395 
396   if (dump_file && (dump_flags & TDF_DETAILS))
397     fprintf (dump_file,
398 	      "COND_EXPR in block %d and PHI in block %d converted to straightline code.\n",
399 	      cond_block->index,
400 	      bb->index);
401 }
402 
403 /* PR66726: Factor conversion out of COND_EXPR.  If the arguments of the PHI
404    stmt are CONVERT_STMT, factor out the conversion and perform the conversion
405    to the result of PHI stmt.  COND_STMT is the controlling predicate.
406    Return the newly-created PHI, if any.  */
407 
408 static gphi *
factor_out_conditional_conversion(edge e0,edge e1,gphi * phi,tree arg0,tree arg1,gimple * cond_stmt)409 factor_out_conditional_conversion (edge e0, edge e1, gphi *phi,
410 				   tree arg0, tree arg1, gimple *cond_stmt)
411 {
412   gimple *arg0_def_stmt = NULL, *arg1_def_stmt = NULL, *new_stmt;
413   tree new_arg0 = NULL_TREE, new_arg1 = NULL_TREE;
414   tree temp, result;
415   gphi *newphi;
416   gimple_stmt_iterator gsi, gsi_for_def;
417   source_location locus = gimple_location (phi);
418   enum tree_code convert_code;
419 
420   /* Handle only PHI statements with two arguments.  TODO: If all
421      other arguments to PHI are INTEGER_CST or if their defining
422      statement have the same unary operation, we can handle more
423      than two arguments too.  */
424   if (gimple_phi_num_args (phi) != 2)
425     return NULL;
426 
427   /* First canonicalize to simplify tests.  */
428   if (TREE_CODE (arg0) != SSA_NAME)
429     {
430       std::swap (arg0, arg1);
431       std::swap (e0, e1);
432     }
433 
434   if (TREE_CODE (arg0) != SSA_NAME
435       || (TREE_CODE (arg1) != SSA_NAME
436 	  && TREE_CODE (arg1) != INTEGER_CST))
437     return NULL;
438 
439   /* Check if arg0 is an SSA_NAME and the stmt which defines arg0 is
440      a conversion.  */
441   arg0_def_stmt = SSA_NAME_DEF_STMT (arg0);
442   if (!gimple_assign_cast_p (arg0_def_stmt))
443     return NULL;
444 
445   /* Use the RHS as new_arg0.  */
446   convert_code = gimple_assign_rhs_code (arg0_def_stmt);
447   new_arg0 = gimple_assign_rhs1 (arg0_def_stmt);
448   if (convert_code == VIEW_CONVERT_EXPR)
449     {
450       new_arg0 = TREE_OPERAND (new_arg0, 0);
451       if (!is_gimple_reg_type (TREE_TYPE (new_arg0)))
452 	return NULL;
453     }
454 
455   if (TREE_CODE (arg1) == SSA_NAME)
456     {
457       /* Check if arg1 is an SSA_NAME and the stmt which defines arg1
458 	 is a conversion.  */
459       arg1_def_stmt = SSA_NAME_DEF_STMT (arg1);
460       if (!is_gimple_assign (arg1_def_stmt)
461 	  || gimple_assign_rhs_code (arg1_def_stmt) != convert_code)
462 	return NULL;
463 
464       /* Use the RHS as new_arg1.  */
465       new_arg1 = gimple_assign_rhs1 (arg1_def_stmt);
466       if (convert_code == VIEW_CONVERT_EXPR)
467 	new_arg1 = TREE_OPERAND (new_arg1, 0);
468     }
469   else
470     {
471       /* If arg1 is an INTEGER_CST, fold it to new type.  */
472       if (INTEGRAL_TYPE_P (TREE_TYPE (new_arg0))
473 	  && int_fits_type_p (arg1, TREE_TYPE (new_arg0)))
474 	{
475 	  if (gimple_assign_cast_p (arg0_def_stmt))
476 	    {
477 	      /* For the INTEGER_CST case, we are just moving the
478 		 conversion from one place to another, which can often
479 		 hurt as the conversion moves further away from the
480 		 statement that computes the value.  So, perform this
481 		 only if new_arg0 is an operand of COND_STMT, or
482 		 if arg0_def_stmt is the only non-debug stmt in
483 		 its basic block, because then it is possible this
484 		 could enable further optimizations (minmax replacement
485 		 etc.).  See PR71016.  */
486 	      if (new_arg0 != gimple_cond_lhs (cond_stmt)
487 		  && new_arg0 != gimple_cond_rhs (cond_stmt)
488 		  && gimple_bb (arg0_def_stmt) == e0->src)
489 		{
490 		  gsi = gsi_for_stmt (arg0_def_stmt);
491 		  gsi_prev_nondebug (&gsi);
492 		  if (!gsi_end_p (gsi))
493 		    return NULL;
494 		  gsi = gsi_for_stmt (arg0_def_stmt);
495 		  gsi_next_nondebug (&gsi);
496 		  if (!gsi_end_p (gsi))
497 		    return NULL;
498 		}
499 	      new_arg1 = fold_convert (TREE_TYPE (new_arg0), arg1);
500 	    }
501 	  else
502 	    return NULL;
503 	}
504       else
505 	return NULL;
506     }
507 
508   /*  If arg0/arg1 have > 1 use, then this transformation actually increases
509       the number of expressions evaluated at runtime.  */
510   if (!has_single_use (arg0)
511       || (arg1_def_stmt && !has_single_use (arg1)))
512     return NULL;
513 
514   /* If types of new_arg0 and new_arg1 are different bailout.  */
515   if (!types_compatible_p (TREE_TYPE (new_arg0), TREE_TYPE (new_arg1)))
516     return NULL;
517 
518   /* Create a new PHI stmt.  */
519   result = PHI_RESULT (phi);
520   temp = make_ssa_name (TREE_TYPE (new_arg0), NULL);
521   newphi = create_phi_node (temp, gimple_bb (phi));
522 
523   if (dump_file && (dump_flags & TDF_DETAILS))
524     {
525       fprintf (dump_file, "PHI ");
526       print_generic_expr (dump_file, gimple_phi_result (phi));
527       fprintf (dump_file,
528 	       " changed to factor conversion out from COND_EXPR.\n");
529       fprintf (dump_file, "New stmt with CAST that defines ");
530       print_generic_expr (dump_file, result);
531       fprintf (dump_file, ".\n");
532     }
533 
534   /* Remove the old cast(s) that has single use.  */
535   gsi_for_def = gsi_for_stmt (arg0_def_stmt);
536   gsi_remove (&gsi_for_def, true);
537   release_defs (arg0_def_stmt);
538 
539   if (arg1_def_stmt)
540     {
541       gsi_for_def = gsi_for_stmt (arg1_def_stmt);
542       gsi_remove (&gsi_for_def, true);
543       release_defs (arg1_def_stmt);
544     }
545 
546   add_phi_arg (newphi, new_arg0, e0, locus);
547   add_phi_arg (newphi, new_arg1, e1, locus);
548 
549   /* Create the conversion stmt and insert it.  */
550   if (convert_code == VIEW_CONVERT_EXPR)
551     {
552       temp = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (result), temp);
553       new_stmt = gimple_build_assign (result, temp);
554     }
555   else
556     new_stmt = gimple_build_assign (result, convert_code, temp);
557   gsi = gsi_after_labels (gimple_bb (phi));
558   gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT);
559 
560   /* Remove the original PHI stmt.  */
561   gsi = gsi_for_stmt (phi);
562   gsi_remove (&gsi, true);
563   return newphi;
564 }
565 
566 /*  The function conditional_replacement does the main work of doing the
567     conditional replacement.  Return true if the replacement is done.
568     Otherwise return false.
569     BB is the basic block where the replacement is going to be done on.  ARG0
570     is argument 0 from PHI.  Likewise for ARG1.  */
571 
572 static bool
conditional_replacement(basic_block cond_bb,basic_block middle_bb,edge e0,edge e1,gphi * phi,tree arg0,tree arg1)573 conditional_replacement (basic_block cond_bb, basic_block middle_bb,
574 			 edge e0, edge e1, gphi *phi,
575 			 tree arg0, tree arg1)
576 {
577   tree result;
578   gimple *stmt;
579   gassign *new_stmt;
580   tree cond;
581   gimple_stmt_iterator gsi;
582   edge true_edge, false_edge;
583   tree new_var, new_var2;
584   bool neg;
585 
586   /* FIXME: Gimplification of complex type is too hard for now.  */
587   /* We aren't prepared to handle vectors either (and it is a question
588      if it would be worthwhile anyway).  */
589   if (!(INTEGRAL_TYPE_P (TREE_TYPE (arg0))
590 	|| POINTER_TYPE_P (TREE_TYPE (arg0)))
591       || !(INTEGRAL_TYPE_P (TREE_TYPE (arg1))
592 	   || POINTER_TYPE_P (TREE_TYPE (arg1))))
593     return false;
594 
595   /* The PHI arguments have the constants 0 and 1, or 0 and -1, then
596      convert it to the conditional.  */
597   if ((integer_zerop (arg0) && integer_onep (arg1))
598       || (integer_zerop (arg1) && integer_onep (arg0)))
599     neg = false;
600   else if ((integer_zerop (arg0) && integer_all_onesp (arg1))
601 	   || (integer_zerop (arg1) && integer_all_onesp (arg0)))
602     neg = true;
603   else
604     return false;
605 
606   if (!empty_block_p (middle_bb))
607     return false;
608 
609   /* At this point we know we have a GIMPLE_COND with two successors.
610      One successor is BB, the other successor is an empty block which
611      falls through into BB.
612 
613      There is a single PHI node at the join point (BB) and its arguments
614      are constants (0, 1) or (0, -1).
615 
616      So, given the condition COND, and the two PHI arguments, we can
617      rewrite this PHI into non-branching code:
618 
619        dest = (COND) or dest = COND'
620 
621      We use the condition as-is if the argument associated with the
622      true edge has the value one or the argument associated with the
623      false edge as the value zero.  Note that those conditions are not
624      the same since only one of the outgoing edges from the GIMPLE_COND
625      will directly reach BB and thus be associated with an argument.  */
626 
627   stmt = last_stmt (cond_bb);
628   result = PHI_RESULT (phi);
629 
630   /* To handle special cases like floating point comparison, it is easier and
631      less error-prone to build a tree and gimplify it on the fly though it is
632      less efficient.  */
633   cond = fold_build2_loc (gimple_location (stmt),
634 			  gimple_cond_code (stmt), boolean_type_node,
635 			  gimple_cond_lhs (stmt), gimple_cond_rhs (stmt));
636 
637   /* We need to know which is the true edge and which is the false
638      edge so that we know when to invert the condition below.  */
639   extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
640   if ((e0 == true_edge && integer_zerop (arg0))
641       || (e0 == false_edge && !integer_zerop (arg0))
642       || (e1 == true_edge && integer_zerop (arg1))
643       || (e1 == false_edge && !integer_zerop (arg1)))
644     cond = fold_build1_loc (gimple_location (stmt),
645                             TRUTH_NOT_EXPR, TREE_TYPE (cond), cond);
646 
647   if (neg)
648     {
649       cond = fold_convert_loc (gimple_location (stmt),
650                                TREE_TYPE (result), cond);
651       cond = fold_build1_loc (gimple_location (stmt),
652                               NEGATE_EXPR, TREE_TYPE (cond), cond);
653     }
654 
655   /* Insert our new statements at the end of conditional block before the
656      COND_STMT.  */
657   gsi = gsi_for_stmt (stmt);
658   new_var = force_gimple_operand_gsi (&gsi, cond, true, NULL, true,
659 				      GSI_SAME_STMT);
660 
661   if (!useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (new_var)))
662     {
663       source_location locus_0, locus_1;
664 
665       new_var2 = make_ssa_name (TREE_TYPE (result));
666       new_stmt = gimple_build_assign (new_var2, CONVERT_EXPR, new_var);
667       gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT);
668       new_var = new_var2;
669 
670       /* Set the locus to the first argument, unless is doesn't have one.  */
671       locus_0 = gimple_phi_arg_location (phi, 0);
672       locus_1 = gimple_phi_arg_location (phi, 1);
673       if (locus_0 == UNKNOWN_LOCATION)
674         locus_0 = locus_1;
675       gimple_set_location (new_stmt, locus_0);
676     }
677 
678   replace_phi_edge_with_variable (cond_bb, e1, phi, new_var);
679 
680   /* Note that we optimized this PHI.  */
681   return true;
682 }
683 
684 /* Update *ARG which is defined in STMT so that it contains the
685    computed value if that seems profitable.  Return true if the
686    statement is made dead by that rewriting.  */
687 
688 static bool
jump_function_from_stmt(tree * arg,gimple * stmt)689 jump_function_from_stmt (tree *arg, gimple *stmt)
690 {
691   enum tree_code code = gimple_assign_rhs_code (stmt);
692   if (code == ADDR_EXPR)
693     {
694       /* For arg = &p->i transform it to p, if possible.  */
695       tree rhs1 = gimple_assign_rhs1 (stmt);
696       poly_int64 offset;
697       tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (rhs1, 0),
698 						&offset);
699       if (tem
700 	  && TREE_CODE (tem) == MEM_REF
701 	  && known_eq (mem_ref_offset (tem) + offset, 0))
702 	{
703 	  *arg = TREE_OPERAND (tem, 0);
704 	  return true;
705 	}
706     }
707   /* TODO: Much like IPA-CP jump-functions we want to handle constant
708      additions symbolically here, and we'd need to update the comparison
709      code that compares the arg + cst tuples in our caller.  For now the
710      code above exactly handles the VEC_BASE pattern from vec.h.  */
711   return false;
712 }
713 
714 /* RHS is a source argument in a BIT_AND_EXPR which feeds a conditional
715    of the form SSA_NAME NE 0.
716 
717    If RHS is fed by a simple EQ_EXPR comparison of two values, see if
718    the two input values of the EQ_EXPR match arg0 and arg1.
719 
720    If so update *code and return TRUE.  Otherwise return FALSE.  */
721 
722 static bool
rhs_is_fed_for_value_replacement(const_tree arg0,const_tree arg1,enum tree_code * code,const_tree rhs)723 rhs_is_fed_for_value_replacement (const_tree arg0, const_tree arg1,
724 				  enum tree_code *code, const_tree rhs)
725 {
726   /* Obviously if RHS is not an SSA_NAME, we can't look at the defining
727      statement.  */
728   if (TREE_CODE (rhs) == SSA_NAME)
729     {
730       gimple *def1 = SSA_NAME_DEF_STMT (rhs);
731 
732       /* Verify the defining statement has an EQ_EXPR on the RHS.  */
733       if (is_gimple_assign (def1) && gimple_assign_rhs_code (def1) == EQ_EXPR)
734 	{
735 	  /* Finally verify the source operands of the EQ_EXPR are equal
736 	     to arg0 and arg1.  */
737 	  tree op0 = gimple_assign_rhs1 (def1);
738 	  tree op1 = gimple_assign_rhs2 (def1);
739 	  if ((operand_equal_for_phi_arg_p (arg0, op0)
740 	       && operand_equal_for_phi_arg_p (arg1, op1))
741 	      || (operand_equal_for_phi_arg_p (arg0, op1)
742                && operand_equal_for_phi_arg_p (arg1, op0)))
743 	    {
744 	      /* We will perform the optimization.  */
745 	      *code = gimple_assign_rhs_code (def1);
746 	      return true;
747 	    }
748 	}
749     }
750   return false;
751 }
752 
753 /* Return TRUE if arg0/arg1 are equal to the rhs/lhs or lhs/rhs of COND.
754 
755    Also return TRUE if arg0/arg1 are equal to the source arguments of a
756    an EQ comparison feeding a BIT_AND_EXPR which feeds COND.
757 
758    Return FALSE otherwise.  */
759 
760 static bool
operand_equal_for_value_replacement(const_tree arg0,const_tree arg1,enum tree_code * code,gimple * cond)761 operand_equal_for_value_replacement (const_tree arg0, const_tree arg1,
762 				     enum tree_code *code, gimple *cond)
763 {
764   gimple *def;
765   tree lhs = gimple_cond_lhs (cond);
766   tree rhs = gimple_cond_rhs (cond);
767 
768   if ((operand_equal_for_phi_arg_p (arg0, lhs)
769        && operand_equal_for_phi_arg_p (arg1, rhs))
770       || (operand_equal_for_phi_arg_p (arg1, lhs)
771 	  && operand_equal_for_phi_arg_p (arg0, rhs)))
772     return true;
773 
774   /* Now handle more complex case where we have an EQ comparison
775      which feeds a BIT_AND_EXPR which feeds COND.
776 
777      First verify that COND is of the form SSA_NAME NE 0.  */
778   if (*code != NE_EXPR || !integer_zerop (rhs)
779       || TREE_CODE (lhs) != SSA_NAME)
780     return false;
781 
782   /* Now ensure that SSA_NAME is set by a BIT_AND_EXPR.  */
783   def = SSA_NAME_DEF_STMT (lhs);
784   if (!is_gimple_assign (def) || gimple_assign_rhs_code (def) != BIT_AND_EXPR)
785     return false;
786 
787   /* Now verify arg0/arg1 correspond to the source arguments of an
788      EQ comparison feeding the BIT_AND_EXPR.  */
789 
790   tree tmp = gimple_assign_rhs1 (def);
791   if (rhs_is_fed_for_value_replacement (arg0, arg1, code, tmp))
792     return true;
793 
794   tmp = gimple_assign_rhs2 (def);
795   if (rhs_is_fed_for_value_replacement (arg0, arg1, code, tmp))
796     return true;
797 
798   return false;
799 }
800 
801 /* Returns true if ARG is a neutral element for operation CODE
802    on the RIGHT side.  */
803 
804 static bool
neutral_element_p(tree_code code,tree arg,bool right)805 neutral_element_p (tree_code code, tree arg, bool right)
806 {
807   switch (code)
808     {
809     case PLUS_EXPR:
810     case BIT_IOR_EXPR:
811     case BIT_XOR_EXPR:
812       return integer_zerop (arg);
813 
814     case LROTATE_EXPR:
815     case RROTATE_EXPR:
816     case LSHIFT_EXPR:
817     case RSHIFT_EXPR:
818     case MINUS_EXPR:
819     case POINTER_PLUS_EXPR:
820       return right && integer_zerop (arg);
821 
822     case MULT_EXPR:
823       return integer_onep (arg);
824 
825     case TRUNC_DIV_EXPR:
826     case CEIL_DIV_EXPR:
827     case FLOOR_DIV_EXPR:
828     case ROUND_DIV_EXPR:
829     case EXACT_DIV_EXPR:
830       return right && integer_onep (arg);
831 
832     case BIT_AND_EXPR:
833       return integer_all_onesp (arg);
834 
835     default:
836       return false;
837     }
838 }
839 
840 /* Returns true if ARG is an absorbing element for operation CODE.  */
841 
842 static bool
absorbing_element_p(tree_code code,tree arg,bool right,tree rval)843 absorbing_element_p (tree_code code, tree arg, bool right, tree rval)
844 {
845   switch (code)
846     {
847     case BIT_IOR_EXPR:
848       return integer_all_onesp (arg);
849 
850     case MULT_EXPR:
851     case BIT_AND_EXPR:
852       return integer_zerop (arg);
853 
854     case LSHIFT_EXPR:
855     case RSHIFT_EXPR:
856     case LROTATE_EXPR:
857     case RROTATE_EXPR:
858       return !right && integer_zerop (arg);
859 
860     case TRUNC_DIV_EXPR:
861     case CEIL_DIV_EXPR:
862     case FLOOR_DIV_EXPR:
863     case ROUND_DIV_EXPR:
864     case EXACT_DIV_EXPR:
865     case TRUNC_MOD_EXPR:
866     case CEIL_MOD_EXPR:
867     case FLOOR_MOD_EXPR:
868     case ROUND_MOD_EXPR:
869       return (!right
870 	      && integer_zerop (arg)
871 	      && tree_single_nonzero_warnv_p (rval, NULL));
872 
873     default:
874       return false;
875     }
876 }
877 
878 /*  The function value_replacement does the main work of doing the value
879     replacement.  Return non-zero if the replacement is done.  Otherwise return
880     0.  If we remove the middle basic block, return 2.
881     BB is the basic block where the replacement is going to be done on.  ARG0
882     is argument 0 from the PHI.  Likewise for ARG1.  */
883 
884 static int
value_replacement(basic_block cond_bb,basic_block middle_bb,edge e0,edge e1,gimple * phi,tree arg0,tree arg1)885 value_replacement (basic_block cond_bb, basic_block middle_bb,
886 		   edge e0, edge e1, gimple *phi,
887 		   tree arg0, tree arg1)
888 {
889   gimple_stmt_iterator gsi;
890   gimple *cond;
891   edge true_edge, false_edge;
892   enum tree_code code;
893   bool emtpy_or_with_defined_p = true;
894 
895   /* If the type says honor signed zeros we cannot do this
896      optimization.  */
897   if (HONOR_SIGNED_ZEROS (arg1))
898     return 0;
899 
900   /* If there is a statement in MIDDLE_BB that defines one of the PHI
901      arguments, then adjust arg0 or arg1.  */
902   gsi = gsi_start_nondebug_after_labels_bb (middle_bb);
903   while (!gsi_end_p (gsi))
904     {
905       gimple *stmt = gsi_stmt (gsi);
906       tree lhs;
907       gsi_next_nondebug (&gsi);
908       if (!is_gimple_assign (stmt))
909 	{
910 	  emtpy_or_with_defined_p = false;
911 	  continue;
912 	}
913       /* Now try to adjust arg0 or arg1 according to the computation
914 	 in the statement.  */
915       lhs = gimple_assign_lhs (stmt);
916       if (!(lhs == arg0
917 	     && jump_function_from_stmt (&arg0, stmt))
918 	    || (lhs == arg1
919 		&& jump_function_from_stmt (&arg1, stmt)))
920 	emtpy_or_with_defined_p = false;
921     }
922 
923   cond = last_stmt (cond_bb);
924   code = gimple_cond_code (cond);
925 
926   /* This transformation is only valid for equality comparisons.  */
927   if (code != NE_EXPR && code != EQ_EXPR)
928     return 0;
929 
930   /* We need to know which is the true edge and which is the false
931       edge so that we know if have abs or negative abs.  */
932   extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
933 
934   /* At this point we know we have a COND_EXPR with two successors.
935      One successor is BB, the other successor is an empty block which
936      falls through into BB.
937 
938      The condition for the COND_EXPR is known to be NE_EXPR or EQ_EXPR.
939 
940      There is a single PHI node at the join point (BB) with two arguments.
941 
942      We now need to verify that the two arguments in the PHI node match
943      the two arguments to the equality comparison.  */
944 
945   if (operand_equal_for_value_replacement (arg0, arg1, &code, cond))
946     {
947       edge e;
948       tree arg;
949 
950       /* For NE_EXPR, we want to build an assignment result = arg where
951 	 arg is the PHI argument associated with the true edge.  For
952 	 EQ_EXPR we want the PHI argument associated with the false edge.  */
953       e = (code == NE_EXPR ? true_edge : false_edge);
954 
955       /* Unfortunately, E may not reach BB (it may instead have gone to
956 	 OTHER_BLOCK).  If that is the case, then we want the single outgoing
957 	 edge from OTHER_BLOCK which reaches BB and represents the desired
958 	 path from COND_BLOCK.  */
959       if (e->dest == middle_bb)
960 	e = single_succ_edge (e->dest);
961 
962       /* Now we know the incoming edge to BB that has the argument for the
963 	 RHS of our new assignment statement.  */
964       if (e0 == e)
965 	arg = arg0;
966       else
967 	arg = arg1;
968 
969       /* If the middle basic block was empty or is defining the
970 	 PHI arguments and this is a single phi where the args are different
971 	 for the edges e0 and e1 then we can remove the middle basic block. */
972       if (emtpy_or_with_defined_p
973 	  && single_non_singleton_phi_for_edges (phi_nodes (gimple_bb (phi)),
974 						 e0, e1) == phi)
975 	{
976           replace_phi_edge_with_variable (cond_bb, e1, phi, arg);
977 	  /* Note that we optimized this PHI.  */
978 	  return 2;
979 	}
980       else
981 	{
982 	  /* Replace the PHI arguments with arg. */
983 	  SET_PHI_ARG_DEF (phi, e0->dest_idx, arg);
984 	  SET_PHI_ARG_DEF (phi, e1->dest_idx, arg);
985 	  if (dump_file && (dump_flags & TDF_DETAILS))
986 	    {
987 	      fprintf (dump_file, "PHI ");
988 	      print_generic_expr (dump_file, gimple_phi_result (phi));
989 	      fprintf (dump_file, " reduced for COND_EXPR in block %d to ",
990 		       cond_bb->index);
991 	      print_generic_expr (dump_file, arg);
992 	      fprintf (dump_file, ".\n");
993             }
994           return 1;
995 	}
996 
997     }
998 
999   /* Now optimize (x != 0) ? x + y : y to just x + y.  */
1000   gsi = gsi_last_nondebug_bb (middle_bb);
1001   if (gsi_end_p (gsi))
1002     return 0;
1003 
1004   gimple *assign = gsi_stmt (gsi);
1005   if (!is_gimple_assign (assign)
1006       || gimple_assign_rhs_class (assign) != GIMPLE_BINARY_RHS
1007       || (!INTEGRAL_TYPE_P (TREE_TYPE (arg0))
1008 	  && !POINTER_TYPE_P (TREE_TYPE (arg0))))
1009     return 0;
1010 
1011   /* Punt if there are (degenerate) PHIs in middle_bb, there should not be.  */
1012   if (!gimple_seq_empty_p (phi_nodes (middle_bb)))
1013     return 0;
1014 
1015   /* Allow up to 2 cheap preparation statements that prepare argument
1016      for assign, e.g.:
1017       if (y_4 != 0)
1018 	goto <bb 3>;
1019       else
1020 	goto <bb 4>;
1021      <bb 3>:
1022       _1 = (int) y_4;
1023       iftmp.0_6 = x_5(D) r<< _1;
1024      <bb 4>:
1025       # iftmp.0_2 = PHI <iftmp.0_6(3), x_5(D)(2)>
1026      or:
1027       if (y_3(D) == 0)
1028 	goto <bb 4>;
1029       else
1030 	goto <bb 3>;
1031      <bb 3>:
1032       y_4 = y_3(D) & 31;
1033       _1 = (int) y_4;
1034       _6 = x_5(D) r<< _1;
1035      <bb 4>:
1036       # _2 = PHI <x_5(D)(2), _6(3)>  */
1037   gimple *prep_stmt[2] = { NULL, NULL };
1038   int prep_cnt;
1039   for (prep_cnt = 0; ; prep_cnt++)
1040     {
1041       gsi_prev_nondebug (&gsi);
1042       if (gsi_end_p (gsi))
1043 	break;
1044 
1045       gimple *g = gsi_stmt (gsi);
1046       if (gimple_code (g) == GIMPLE_LABEL)
1047 	break;
1048 
1049       if (prep_cnt == 2 || !is_gimple_assign (g))
1050 	return 0;
1051 
1052       tree lhs = gimple_assign_lhs (g);
1053       tree rhs1 = gimple_assign_rhs1 (g);
1054       use_operand_p use_p;
1055       gimple *use_stmt;
1056       if (TREE_CODE (lhs) != SSA_NAME
1057 	  || TREE_CODE (rhs1) != SSA_NAME
1058 	  || !INTEGRAL_TYPE_P (TREE_TYPE (lhs))
1059 	  || !INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1060 	  || !single_imm_use (lhs, &use_p, &use_stmt)
1061 	  || use_stmt != (prep_cnt ? prep_stmt[prep_cnt - 1] : assign))
1062 	return 0;
1063       switch (gimple_assign_rhs_code (g))
1064 	{
1065 	CASE_CONVERT:
1066 	  break;
1067 	case PLUS_EXPR:
1068 	case BIT_AND_EXPR:
1069 	case BIT_IOR_EXPR:
1070 	case BIT_XOR_EXPR:
1071 	  if (TREE_CODE (gimple_assign_rhs2 (g)) != INTEGER_CST)
1072 	    return 0;
1073 	  break;
1074 	default:
1075 	  return 0;
1076 	}
1077       prep_stmt[prep_cnt] = g;
1078     }
1079 
1080   /* Only transform if it removes the condition.  */
1081   if (!single_non_singleton_phi_for_edges (phi_nodes (gimple_bb (phi)), e0, e1))
1082     return 0;
1083 
1084   /* Size-wise, this is always profitable.  */
1085   if (optimize_bb_for_speed_p (cond_bb)
1086       /* The special case is useless if it has a low probability.  */
1087       && profile_status_for_fn (cfun) != PROFILE_ABSENT
1088       && EDGE_PRED (middle_bb, 0)->probability < profile_probability::even ()
1089       /* If assign is cheap, there is no point avoiding it.  */
1090       && estimate_num_insns (bb_seq (middle_bb), &eni_time_weights)
1091 	 >= 3 * estimate_num_insns (cond, &eni_time_weights))
1092     return 0;
1093 
1094   tree lhs = gimple_assign_lhs (assign);
1095   tree rhs1 = gimple_assign_rhs1 (assign);
1096   tree rhs2 = gimple_assign_rhs2 (assign);
1097   enum tree_code code_def = gimple_assign_rhs_code (assign);
1098   tree cond_lhs = gimple_cond_lhs (cond);
1099   tree cond_rhs = gimple_cond_rhs (cond);
1100 
1101   /* Propagate the cond_rhs constant through preparation stmts,
1102      make sure UB isn't invoked while doing that.  */
1103   for (int i = prep_cnt - 1; i >= 0; --i)
1104     {
1105       gimple *g = prep_stmt[i];
1106       tree grhs1 = gimple_assign_rhs1 (g);
1107       if (!operand_equal_for_phi_arg_p (cond_lhs, grhs1))
1108 	return 0;
1109       cond_lhs = gimple_assign_lhs (g);
1110       cond_rhs = fold_convert (TREE_TYPE (grhs1), cond_rhs);
1111       if (TREE_CODE (cond_rhs) != INTEGER_CST
1112 	  || TREE_OVERFLOW (cond_rhs))
1113 	return 0;
1114       if (gimple_assign_rhs_class (g) == GIMPLE_BINARY_RHS)
1115 	{
1116 	  cond_rhs = int_const_binop (gimple_assign_rhs_code (g), cond_rhs,
1117 				      gimple_assign_rhs2 (g));
1118 	  if (TREE_OVERFLOW (cond_rhs))
1119 	    return 0;
1120 	}
1121       cond_rhs = fold_convert (TREE_TYPE (cond_lhs), cond_rhs);
1122       if (TREE_CODE (cond_rhs) != INTEGER_CST
1123 	  || TREE_OVERFLOW (cond_rhs))
1124 	return 0;
1125     }
1126 
1127   if (((code == NE_EXPR && e1 == false_edge)
1128 	|| (code == EQ_EXPR && e1 == true_edge))
1129       && arg0 == lhs
1130       && ((arg1 == rhs1
1131 	   && operand_equal_for_phi_arg_p (rhs2, cond_lhs)
1132 	   && neutral_element_p (code_def, cond_rhs, true))
1133 	  || (arg1 == rhs2
1134 	      && operand_equal_for_phi_arg_p (rhs1, cond_lhs)
1135 	      && neutral_element_p (code_def, cond_rhs, false))
1136 	  || (operand_equal_for_phi_arg_p (arg1, cond_rhs)
1137 	      && ((operand_equal_for_phi_arg_p (rhs2, cond_lhs)
1138 		   && absorbing_element_p (code_def, cond_rhs, true, rhs2))
1139 		  || (operand_equal_for_phi_arg_p (rhs1, cond_lhs)
1140 		      && absorbing_element_p (code_def,
1141 					      cond_rhs, false, rhs2))))))
1142     {
1143       gsi = gsi_for_stmt (cond);
1144       /* Moving ASSIGN might change VR of lhs, e.g. when moving u_6
1145 	 def-stmt in:
1146 	   if (n_5 != 0)
1147 	     goto <bb 3>;
1148 	   else
1149 	     goto <bb 4>;
1150 
1151 	   <bb 3>:
1152 	   # RANGE [0, 4294967294]
1153 	   u_6 = n_5 + 4294967295;
1154 
1155 	   <bb 4>:
1156 	   # u_3 = PHI <u_6(3), 4294967295(2)>  */
1157       reset_flow_sensitive_info (lhs);
1158       if (INTEGRAL_TYPE_P (TREE_TYPE (lhs)))
1159 	{
1160 	  /* If available, we can use VR of phi result at least.  */
1161 	  tree phires = gimple_phi_result (phi);
1162 	  struct range_info_def *phires_range_info
1163 	    = SSA_NAME_RANGE_INFO (phires);
1164 	  if (phires_range_info)
1165 	    duplicate_ssa_name_range_info (lhs, SSA_NAME_RANGE_TYPE (phires),
1166 					   phires_range_info);
1167 	}
1168       gimple_stmt_iterator gsi_from;
1169       for (int i = prep_cnt - 1; i >= 0; --i)
1170 	{
1171 	  tree plhs = gimple_assign_lhs (prep_stmt[i]);
1172 	  reset_flow_sensitive_info (plhs);
1173 	  gsi_from = gsi_for_stmt (prep_stmt[i]);
1174 	  gsi_move_before (&gsi_from, &gsi);
1175 	}
1176       gsi_from = gsi_for_stmt (assign);
1177       gsi_move_before (&gsi_from, &gsi);
1178       replace_phi_edge_with_variable (cond_bb, e1, phi, lhs);
1179       return 2;
1180     }
1181 
1182   return 0;
1183 }
1184 
1185 /*  The function minmax_replacement does the main work of doing the minmax
1186     replacement.  Return true if the replacement is done.  Otherwise return
1187     false.
1188     BB is the basic block where the replacement is going to be done on.  ARG0
1189     is argument 0 from the PHI.  Likewise for ARG1.  */
1190 
1191 static bool
minmax_replacement(basic_block cond_bb,basic_block middle_bb,edge e0,edge e1,gimple * phi,tree arg0,tree arg1)1192 minmax_replacement (basic_block cond_bb, basic_block middle_bb,
1193 		    edge e0, edge e1, gimple *phi,
1194 		    tree arg0, tree arg1)
1195 {
1196   tree result, type;
1197   gcond *cond;
1198   gassign *new_stmt;
1199   edge true_edge, false_edge;
1200   enum tree_code cmp, minmax, ass_code;
1201   tree smaller, alt_smaller, larger, alt_larger, arg_true, arg_false;
1202   gimple_stmt_iterator gsi, gsi_from;
1203 
1204   type = TREE_TYPE (PHI_RESULT (phi));
1205 
1206   /* The optimization may be unsafe due to NaNs.  */
1207   if (HONOR_NANS (type) || HONOR_SIGNED_ZEROS (type))
1208     return false;
1209 
1210   cond = as_a <gcond *> (last_stmt (cond_bb));
1211   cmp = gimple_cond_code (cond);
1212 
1213   /* This transformation is only valid for order comparisons.  Record which
1214      operand is smaller/larger if the result of the comparison is true.  */
1215   alt_smaller = NULL_TREE;
1216   alt_larger = NULL_TREE;
1217   if (cmp == LT_EXPR || cmp == LE_EXPR)
1218     {
1219       smaller = gimple_cond_lhs (cond);
1220       larger = gimple_cond_rhs (cond);
1221       /* If we have smaller < CST it is equivalent to smaller <= CST-1.
1222 	 Likewise smaller <= CST is equivalent to smaller < CST+1.  */
1223       if (TREE_CODE (larger) == INTEGER_CST)
1224 	{
1225 	  if (cmp == LT_EXPR)
1226 	    {
1227 	      bool overflow;
1228 	      wide_int alt = wi::sub (wi::to_wide (larger), 1,
1229 				      TYPE_SIGN (TREE_TYPE (larger)),
1230 				      &overflow);
1231 	      if (! overflow)
1232 		alt_larger = wide_int_to_tree (TREE_TYPE (larger), alt);
1233 	    }
1234 	  else
1235 	    {
1236 	      bool overflow;
1237 	      wide_int alt = wi::add (wi::to_wide (larger), 1,
1238 				      TYPE_SIGN (TREE_TYPE (larger)),
1239 				      &overflow);
1240 	      if (! overflow)
1241 		alt_larger = wide_int_to_tree (TREE_TYPE (larger), alt);
1242 	    }
1243 	}
1244     }
1245   else if (cmp == GT_EXPR || cmp == GE_EXPR)
1246     {
1247       smaller = gimple_cond_rhs (cond);
1248       larger = gimple_cond_lhs (cond);
1249       /* If we have larger > CST it is equivalent to larger >= CST+1.
1250 	 Likewise larger >= CST is equivalent to larger > CST-1.  */
1251       if (TREE_CODE (smaller) == INTEGER_CST)
1252 	{
1253 	  if (cmp == GT_EXPR)
1254 	    {
1255 	      bool overflow;
1256 	      wide_int alt = wi::add (wi::to_wide (smaller), 1,
1257 				      TYPE_SIGN (TREE_TYPE (smaller)),
1258 				      &overflow);
1259 	      if (! overflow)
1260 		alt_smaller = wide_int_to_tree (TREE_TYPE (smaller), alt);
1261 	    }
1262 	  else
1263 	    {
1264 	      bool overflow;
1265 	      wide_int alt = wi::sub (wi::to_wide (smaller), 1,
1266 				      TYPE_SIGN (TREE_TYPE (smaller)),
1267 				      &overflow);
1268 	      if (! overflow)
1269 		alt_smaller = wide_int_to_tree (TREE_TYPE (smaller), alt);
1270 	    }
1271 	}
1272     }
1273   else
1274     return false;
1275 
1276   /* We need to know which is the true edge and which is the false
1277       edge so that we know if have abs or negative abs.  */
1278   extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
1279 
1280   /* Forward the edges over the middle basic block.  */
1281   if (true_edge->dest == middle_bb)
1282     true_edge = EDGE_SUCC (true_edge->dest, 0);
1283   if (false_edge->dest == middle_bb)
1284     false_edge = EDGE_SUCC (false_edge->dest, 0);
1285 
1286   if (true_edge == e0)
1287     {
1288       gcc_assert (false_edge == e1);
1289       arg_true = arg0;
1290       arg_false = arg1;
1291     }
1292   else
1293     {
1294       gcc_assert (false_edge == e0);
1295       gcc_assert (true_edge == e1);
1296       arg_true = arg1;
1297       arg_false = arg0;
1298     }
1299 
1300   if (empty_block_p (middle_bb))
1301     {
1302       if ((operand_equal_for_phi_arg_p (arg_true, smaller)
1303 	   || (alt_smaller
1304 	       && operand_equal_for_phi_arg_p (arg_true, alt_smaller)))
1305 	  && (operand_equal_for_phi_arg_p (arg_false, larger)
1306 	      || (alt_larger
1307 		  && operand_equal_for_phi_arg_p (arg_true, alt_larger))))
1308 	{
1309 	  /* Case
1310 
1311 	     if (smaller < larger)
1312 	     rslt = smaller;
1313 	     else
1314 	     rslt = larger;  */
1315 	  minmax = MIN_EXPR;
1316 	}
1317       else if ((operand_equal_for_phi_arg_p (arg_false, smaller)
1318 		|| (alt_smaller
1319 		    && operand_equal_for_phi_arg_p (arg_false, alt_smaller)))
1320 	       && (operand_equal_for_phi_arg_p (arg_true, larger)
1321 		   || (alt_larger
1322 		       && operand_equal_for_phi_arg_p (arg_true, alt_larger))))
1323 	minmax = MAX_EXPR;
1324       else
1325 	return false;
1326     }
1327   else
1328     {
1329       /* Recognize the following case, assuming d <= u:
1330 
1331 	 if (a <= u)
1332 	   b = MAX (a, d);
1333 	 x = PHI <b, u>
1334 
1335 	 This is equivalent to
1336 
1337 	 b = MAX (a, d);
1338 	 x = MIN (b, u);  */
1339 
1340       gimple *assign = last_and_only_stmt (middle_bb);
1341       tree lhs, op0, op1, bound;
1342 
1343       if (!assign
1344 	  || gimple_code (assign) != GIMPLE_ASSIGN)
1345 	return false;
1346 
1347       lhs = gimple_assign_lhs (assign);
1348       ass_code = gimple_assign_rhs_code (assign);
1349       if (ass_code != MAX_EXPR && ass_code != MIN_EXPR)
1350 	return false;
1351       op0 = gimple_assign_rhs1 (assign);
1352       op1 = gimple_assign_rhs2 (assign);
1353 
1354       if (true_edge->src == middle_bb)
1355 	{
1356 	  /* We got here if the condition is true, i.e., SMALLER < LARGER.  */
1357 	  if (!operand_equal_for_phi_arg_p (lhs, arg_true))
1358 	    return false;
1359 
1360 	  if (operand_equal_for_phi_arg_p (arg_false, larger)
1361 	      || (alt_larger
1362 		  && operand_equal_for_phi_arg_p (arg_false, alt_larger)))
1363 	    {
1364 	      /* Case
1365 
1366 		 if (smaller < larger)
1367 		   {
1368 		     r' = MAX_EXPR (smaller, bound)
1369 		   }
1370 		 r = PHI <r', larger>  --> to be turned to MIN_EXPR.  */
1371 	      if (ass_code != MAX_EXPR)
1372 		return false;
1373 
1374 	      minmax = MIN_EXPR;
1375 	      if (operand_equal_for_phi_arg_p (op0, smaller)
1376 		  || (alt_smaller
1377 		      && operand_equal_for_phi_arg_p (op0, alt_smaller)))
1378 		bound = op1;
1379 	      else if (operand_equal_for_phi_arg_p (op1, smaller)
1380 		       || (alt_smaller
1381 			   && operand_equal_for_phi_arg_p (op1, alt_smaller)))
1382 		bound = op0;
1383 	      else
1384 		return false;
1385 
1386 	      /* We need BOUND <= LARGER.  */
1387 	      if (!integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node,
1388 						  bound, larger)))
1389 		return false;
1390 	    }
1391 	  else if (operand_equal_for_phi_arg_p (arg_false, smaller)
1392 		   || (alt_smaller
1393 		       && operand_equal_for_phi_arg_p (arg_false, alt_smaller)))
1394 	    {
1395 	      /* Case
1396 
1397 		 if (smaller < larger)
1398 		   {
1399 		     r' = MIN_EXPR (larger, bound)
1400 		   }
1401 		 r = PHI <r', smaller>  --> to be turned to MAX_EXPR.  */
1402 	      if (ass_code != MIN_EXPR)
1403 		return false;
1404 
1405 	      minmax = MAX_EXPR;
1406 	      if (operand_equal_for_phi_arg_p (op0, larger)
1407 		  || (alt_larger
1408 		      && operand_equal_for_phi_arg_p (op0, alt_larger)))
1409 		bound = op1;
1410 	      else if (operand_equal_for_phi_arg_p (op1, larger)
1411 		       || (alt_larger
1412 			   && operand_equal_for_phi_arg_p (op1, alt_larger)))
1413 		bound = op0;
1414 	      else
1415 		return false;
1416 
1417 	      /* We need BOUND >= SMALLER.  */
1418 	      if (!integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node,
1419 						  bound, smaller)))
1420 		return false;
1421 	    }
1422 	  else
1423 	    return false;
1424 	}
1425       else
1426 	{
1427 	  /* We got here if the condition is false, i.e., SMALLER > LARGER.  */
1428 	  if (!operand_equal_for_phi_arg_p (lhs, arg_false))
1429 	    return false;
1430 
1431 	  if (operand_equal_for_phi_arg_p (arg_true, larger)
1432 	      || (alt_larger
1433 		  && operand_equal_for_phi_arg_p (arg_true, alt_larger)))
1434 	    {
1435 	      /* Case
1436 
1437 		 if (smaller > larger)
1438 		   {
1439 		     r' = MIN_EXPR (smaller, bound)
1440 		   }
1441 		 r = PHI <r', larger>  --> to be turned to MAX_EXPR.  */
1442 	      if (ass_code != MIN_EXPR)
1443 		return false;
1444 
1445 	      minmax = MAX_EXPR;
1446 	      if (operand_equal_for_phi_arg_p (op0, smaller)
1447 		  || (alt_smaller
1448 		      && operand_equal_for_phi_arg_p (op0, alt_smaller)))
1449 		bound = op1;
1450 	      else if (operand_equal_for_phi_arg_p (op1, smaller)
1451 		       || (alt_smaller
1452 			   && operand_equal_for_phi_arg_p (op1, alt_smaller)))
1453 		bound = op0;
1454 	      else
1455 		return false;
1456 
1457 	      /* We need BOUND >= LARGER.  */
1458 	      if (!integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node,
1459 						  bound, larger)))
1460 		return false;
1461 	    }
1462 	  else if (operand_equal_for_phi_arg_p (arg_true, smaller)
1463 		   || (alt_smaller
1464 		       && operand_equal_for_phi_arg_p (arg_true, alt_smaller)))
1465 	    {
1466 	      /* Case
1467 
1468 		 if (smaller > larger)
1469 		   {
1470 		     r' = MAX_EXPR (larger, bound)
1471 		   }
1472 		 r = PHI <r', smaller>  --> to be turned to MIN_EXPR.  */
1473 	      if (ass_code != MAX_EXPR)
1474 		return false;
1475 
1476 	      minmax = MIN_EXPR;
1477 	      if (operand_equal_for_phi_arg_p (op0, larger))
1478 		bound = op1;
1479 	      else if (operand_equal_for_phi_arg_p (op1, larger))
1480 		bound = op0;
1481 	      else
1482 		return false;
1483 
1484 	      /* We need BOUND <= SMALLER.  */
1485 	      if (!integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node,
1486 						  bound, smaller)))
1487 		return false;
1488 	    }
1489 	  else
1490 	    return false;
1491 	}
1492 
1493       /* Move the statement from the middle block.  */
1494       gsi = gsi_last_bb (cond_bb);
1495       gsi_from = gsi_last_nondebug_bb (middle_bb);
1496       reset_flow_sensitive_info (SINGLE_SSA_TREE_OPERAND (gsi_stmt (gsi_from),
1497 							  SSA_OP_DEF));
1498       gsi_move_before (&gsi_from, &gsi);
1499     }
1500 
1501   /* Create an SSA var to hold the min/max result.  If we're the only
1502      things setting the target PHI, then we  can clone the PHI
1503      variable.  Otherwise we must create a new one.  */
1504   result = PHI_RESULT (phi);
1505   if (EDGE_COUNT (gimple_bb (phi)->preds) == 2)
1506     result = duplicate_ssa_name (result, NULL);
1507   else
1508     result = make_ssa_name (TREE_TYPE (result));
1509 
1510   /* Emit the statement to compute min/max.  */
1511   new_stmt = gimple_build_assign (result, minmax, arg0, arg1);
1512   gsi = gsi_last_bb (cond_bb);
1513   gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
1514 
1515   replace_phi_edge_with_variable (cond_bb, e1, phi, result);
1516 
1517   return true;
1518 }
1519 
1520 /*  The function absolute_replacement does the main work of doing the absolute
1521     replacement.  Return true if the replacement is done.  Otherwise return
1522     false.
1523     bb is the basic block where the replacement is going to be done on.  arg0
1524     is argument 0 from the phi.  Likewise for arg1.  */
1525 
1526 static bool
abs_replacement(basic_block cond_bb,basic_block middle_bb,edge e0 ATTRIBUTE_UNUSED,edge e1,gimple * phi,tree arg0,tree arg1)1527 abs_replacement (basic_block cond_bb, basic_block middle_bb,
1528 		 edge e0 ATTRIBUTE_UNUSED, edge e1,
1529 		 gimple *phi, tree arg0, tree arg1)
1530 {
1531   tree result;
1532   gassign *new_stmt;
1533   gimple *cond;
1534   gimple_stmt_iterator gsi;
1535   edge true_edge, false_edge;
1536   gimple *assign;
1537   edge e;
1538   tree rhs, lhs;
1539   bool negate;
1540   enum tree_code cond_code;
1541 
1542   /* If the type says honor signed zeros we cannot do this
1543      optimization.  */
1544   if (HONOR_SIGNED_ZEROS (arg1))
1545     return false;
1546 
1547   /* OTHER_BLOCK must have only one executable statement which must have the
1548      form arg0 = -arg1 or arg1 = -arg0.  */
1549 
1550   assign = last_and_only_stmt (middle_bb);
1551   /* If we did not find the proper negation assignment, then we can not
1552      optimize.  */
1553   if (assign == NULL)
1554     return false;
1555 
1556   /* If we got here, then we have found the only executable statement
1557      in OTHER_BLOCK.  If it is anything other than arg = -arg1 or
1558      arg1 = -arg0, then we can not optimize.  */
1559   if (gimple_code (assign) != GIMPLE_ASSIGN)
1560     return false;
1561 
1562   lhs = gimple_assign_lhs (assign);
1563 
1564   if (gimple_assign_rhs_code (assign) != NEGATE_EXPR)
1565     return false;
1566 
1567   rhs = gimple_assign_rhs1 (assign);
1568 
1569   /* The assignment has to be arg0 = -arg1 or arg1 = -arg0.  */
1570   if (!(lhs == arg0 && rhs == arg1)
1571       && !(lhs == arg1 && rhs == arg0))
1572     return false;
1573 
1574   cond = last_stmt (cond_bb);
1575   result = PHI_RESULT (phi);
1576 
1577   /* Only relationals comparing arg[01] against zero are interesting.  */
1578   cond_code = gimple_cond_code (cond);
1579   if (cond_code != GT_EXPR && cond_code != GE_EXPR
1580       && cond_code != LT_EXPR && cond_code != LE_EXPR)
1581     return false;
1582 
1583   /* Make sure the conditional is arg[01] OP y.  */
1584   if (gimple_cond_lhs (cond) != rhs)
1585     return false;
1586 
1587   if (FLOAT_TYPE_P (TREE_TYPE (gimple_cond_rhs (cond)))
1588 	       ? real_zerop (gimple_cond_rhs (cond))
1589 	       : integer_zerop (gimple_cond_rhs (cond)))
1590     ;
1591   else
1592     return false;
1593 
1594   /* We need to know which is the true edge and which is the false
1595      edge so that we know if have abs or negative abs.  */
1596   extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
1597 
1598   /* For GT_EXPR/GE_EXPR, if the true edge goes to OTHER_BLOCK, then we
1599      will need to negate the result.  Similarly for LT_EXPR/LE_EXPR if
1600      the false edge goes to OTHER_BLOCK.  */
1601   if (cond_code == GT_EXPR || cond_code == GE_EXPR)
1602     e = true_edge;
1603   else
1604     e = false_edge;
1605 
1606   if (e->dest == middle_bb)
1607     negate = true;
1608   else
1609     negate = false;
1610 
1611   /* If the code negates only iff positive then make sure to not
1612      introduce undefined behavior when negating or computing the absolute.
1613      ???  We could use range info if present to check for arg1 == INT_MIN.  */
1614   if (negate
1615       && (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg1))
1616 	  && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1))))
1617     return false;
1618 
1619   result = duplicate_ssa_name (result, NULL);
1620 
1621   if (negate)
1622     lhs = make_ssa_name (TREE_TYPE (result));
1623   else
1624     lhs = result;
1625 
1626   /* Build the modify expression with abs expression.  */
1627   new_stmt = gimple_build_assign (lhs, ABS_EXPR, rhs);
1628 
1629   gsi = gsi_last_bb (cond_bb);
1630   gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
1631 
1632   if (negate)
1633     {
1634       /* Get the right GSI.  We want to insert after the recently
1635 	 added ABS_EXPR statement (which we know is the first statement
1636 	 in the block.  */
1637       new_stmt = gimple_build_assign (result, NEGATE_EXPR, lhs);
1638 
1639       gsi_insert_after (&gsi, new_stmt, GSI_NEW_STMT);
1640     }
1641 
1642   replace_phi_edge_with_variable (cond_bb, e1, phi, result);
1643 
1644   /* Note that we optimized this PHI.  */
1645   return true;
1646 }
1647 
1648 /* Auxiliary functions to determine the set of memory accesses which
1649    can't trap because they are preceded by accesses to the same memory
1650    portion.  We do that for MEM_REFs, so we only need to track
1651    the SSA_NAME of the pointer indirectly referenced.  The algorithm
1652    simply is a walk over all instructions in dominator order.  When
1653    we see an MEM_REF we determine if we've already seen a same
1654    ref anywhere up to the root of the dominator tree.  If we do the
1655    current access can't trap.  If we don't see any dominating access
1656    the current access might trap, but might also make later accesses
1657    non-trapping, so we remember it.  We need to be careful with loads
1658    or stores, for instance a load might not trap, while a store would,
1659    so if we see a dominating read access this doesn't mean that a later
1660    write access would not trap.  Hence we also need to differentiate the
1661    type of access(es) seen.
1662 
1663    ??? We currently are very conservative and assume that a load might
1664    trap even if a store doesn't (write-only memory).  This probably is
1665    overly conservative.  */
1666 
1667 /* A hash-table of SSA_NAMEs, and in which basic block an MEM_REF
1668    through it was seen, which would constitute a no-trap region for
1669    same accesses.  */
1670 struct name_to_bb
1671 {
1672   unsigned int ssa_name_ver;
1673   unsigned int phase;
1674   bool store;
1675   HOST_WIDE_INT offset, size;
1676   basic_block bb;
1677 };
1678 
1679 /* Hashtable helpers.  */
1680 
1681 struct ssa_names_hasher : free_ptr_hash <name_to_bb>
1682 {
1683   static inline hashval_t hash (const name_to_bb *);
1684   static inline bool equal (const name_to_bb *, const name_to_bb *);
1685 };
1686 
1687 /* Used for quick clearing of the hash-table when we see calls.
1688    Hash entries with phase < nt_call_phase are invalid.  */
1689 static unsigned int nt_call_phase;
1690 
1691 /* The hash function.  */
1692 
1693 inline hashval_t
hash(const name_to_bb * n)1694 ssa_names_hasher::hash (const name_to_bb *n)
1695 {
1696   return n->ssa_name_ver ^ (((hashval_t) n->store) << 31)
1697          ^ (n->offset << 6) ^ (n->size << 3);
1698 }
1699 
1700 /* The equality function of *P1 and *P2.  */
1701 
1702 inline bool
equal(const name_to_bb * n1,const name_to_bb * n2)1703 ssa_names_hasher::equal (const name_to_bb *n1, const name_to_bb *n2)
1704 {
1705   return n1->ssa_name_ver == n2->ssa_name_ver
1706          && n1->store == n2->store
1707          && n1->offset == n2->offset
1708          && n1->size == n2->size;
1709 }
1710 
1711 class nontrapping_dom_walker : public dom_walker
1712 {
1713 public:
nontrapping_dom_walker(cdi_direction direction,hash_set<tree> * ps)1714   nontrapping_dom_walker (cdi_direction direction, hash_set<tree> *ps)
1715     : dom_walker (direction), m_nontrapping (ps), m_seen_ssa_names (128) {}
1716 
1717   virtual edge before_dom_children (basic_block);
1718   virtual void after_dom_children (basic_block);
1719 
1720 private:
1721 
1722   /* We see the expression EXP in basic block BB.  If it's an interesting
1723      expression (an MEM_REF through an SSA_NAME) possibly insert the
1724      expression into the set NONTRAP or the hash table of seen expressions.
1725      STORE is true if this expression is on the LHS, otherwise it's on
1726      the RHS.  */
1727   void add_or_mark_expr (basic_block, tree, bool);
1728 
1729   hash_set<tree> *m_nontrapping;
1730 
1731   /* The hash table for remembering what we've seen.  */
1732   hash_table<ssa_names_hasher> m_seen_ssa_names;
1733 };
1734 
1735 /* Called by walk_dominator_tree, when entering the block BB.  */
1736 edge
before_dom_children(basic_block bb)1737 nontrapping_dom_walker::before_dom_children (basic_block bb)
1738 {
1739   edge e;
1740   edge_iterator ei;
1741   gimple_stmt_iterator gsi;
1742 
1743   /* If we haven't seen all our predecessors, clear the hash-table.  */
1744   FOR_EACH_EDGE (e, ei, bb->preds)
1745     if ((((size_t)e->src->aux) & 2) == 0)
1746       {
1747 	nt_call_phase++;
1748 	break;
1749       }
1750 
1751   /* Mark this BB as being on the path to dominator root and as visited.  */
1752   bb->aux = (void*)(1 | 2);
1753 
1754   /* And walk the statements in order.  */
1755   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1756     {
1757       gimple *stmt = gsi_stmt (gsi);
1758 
1759       if ((gimple_code (stmt) == GIMPLE_ASM && gimple_vdef (stmt))
1760 	  || (is_gimple_call (stmt)
1761 	      && (!nonfreeing_call_p (stmt) || !nonbarrier_call_p (stmt))))
1762 	nt_call_phase++;
1763       else if (gimple_assign_single_p (stmt) && !gimple_has_volatile_ops (stmt))
1764 	{
1765 	  add_or_mark_expr (bb, gimple_assign_lhs (stmt), true);
1766 	  add_or_mark_expr (bb, gimple_assign_rhs1 (stmt), false);
1767 	}
1768     }
1769   return NULL;
1770 }
1771 
1772 /* Called by walk_dominator_tree, when basic block BB is exited.  */
1773 void
after_dom_children(basic_block bb)1774 nontrapping_dom_walker::after_dom_children (basic_block bb)
1775 {
1776   /* This BB isn't on the path to dominator root anymore.  */
1777   bb->aux = (void*)2;
1778 }
1779 
1780 /* We see the expression EXP in basic block BB.  If it's an interesting
1781    expression (an MEM_REF through an SSA_NAME) possibly insert the
1782    expression into the set NONTRAP or the hash table of seen expressions.
1783    STORE is true if this expression is on the LHS, otherwise it's on
1784    the RHS.  */
1785 void
add_or_mark_expr(basic_block bb,tree exp,bool store)1786 nontrapping_dom_walker::add_or_mark_expr (basic_block bb, tree exp, bool store)
1787 {
1788   HOST_WIDE_INT size;
1789 
1790   if (TREE_CODE (exp) == MEM_REF
1791       && TREE_CODE (TREE_OPERAND (exp, 0)) == SSA_NAME
1792       && tree_fits_shwi_p (TREE_OPERAND (exp, 1))
1793       && (size = int_size_in_bytes (TREE_TYPE (exp))) > 0)
1794     {
1795       tree name = TREE_OPERAND (exp, 0);
1796       struct name_to_bb map;
1797       name_to_bb **slot;
1798       struct name_to_bb *n2bb;
1799       basic_block found_bb = 0;
1800 
1801       /* Try to find the last seen MEM_REF through the same
1802          SSA_NAME, which can trap.  */
1803       map.ssa_name_ver = SSA_NAME_VERSION (name);
1804       map.phase = 0;
1805       map.bb = 0;
1806       map.store = store;
1807       map.offset = tree_to_shwi (TREE_OPERAND (exp, 1));
1808       map.size = size;
1809 
1810       slot = m_seen_ssa_names.find_slot (&map, INSERT);
1811       n2bb = *slot;
1812       if (n2bb && n2bb->phase >= nt_call_phase)
1813         found_bb = n2bb->bb;
1814 
1815       /* If we've found a trapping MEM_REF, _and_ it dominates EXP
1816          (it's in a basic block on the path from us to the dominator root)
1817 	 then we can't trap.  */
1818       if (found_bb && (((size_t)found_bb->aux) & 1) == 1)
1819 	{
1820 	  m_nontrapping->add (exp);
1821 	}
1822       else
1823         {
1824 	  /* EXP might trap, so insert it into the hash table.  */
1825 	  if (n2bb)
1826 	    {
1827 	      n2bb->phase = nt_call_phase;
1828 	      n2bb->bb = bb;
1829 	    }
1830 	  else
1831 	    {
1832 	      n2bb = XNEW (struct name_to_bb);
1833 	      n2bb->ssa_name_ver = SSA_NAME_VERSION (name);
1834 	      n2bb->phase = nt_call_phase;
1835 	      n2bb->bb = bb;
1836 	      n2bb->store = store;
1837 	      n2bb->offset = map.offset;
1838 	      n2bb->size = size;
1839 	      *slot = n2bb;
1840 	    }
1841 	}
1842     }
1843 }
1844 
1845 /* This is the entry point of gathering non trapping memory accesses.
1846    It will do a dominator walk over the whole function, and it will
1847    make use of the bb->aux pointers.  It returns a set of trees
1848    (the MEM_REFs itself) which can't trap.  */
1849 static hash_set<tree> *
get_non_trapping(void)1850 get_non_trapping (void)
1851 {
1852   nt_call_phase = 0;
1853   hash_set<tree> *nontrap = new hash_set<tree>;
1854   /* We're going to do a dominator walk, so ensure that we have
1855      dominance information.  */
1856   calculate_dominance_info (CDI_DOMINATORS);
1857 
1858   nontrapping_dom_walker (CDI_DOMINATORS, nontrap)
1859     .walk (cfun->cfg->x_entry_block_ptr);
1860 
1861   clear_aux_for_blocks ();
1862   return nontrap;
1863 }
1864 
1865 /* Do the main work of conditional store replacement.  We already know
1866    that the recognized pattern looks like so:
1867 
1868    split:
1869      if (cond) goto MIDDLE_BB; else goto JOIN_BB (edge E1)
1870    MIDDLE_BB:
1871      something
1872      fallthrough (edge E0)
1873    JOIN_BB:
1874      some more
1875 
1876    We check that MIDDLE_BB contains only one store, that that store
1877    doesn't trap (not via NOTRAP, but via checking if an access to the same
1878    memory location dominates us) and that the store has a "simple" RHS.  */
1879 
1880 static bool
cond_store_replacement(basic_block middle_bb,basic_block join_bb,edge e0,edge e1,hash_set<tree> * nontrap)1881 cond_store_replacement (basic_block middle_bb, basic_block join_bb,
1882 			edge e0, edge e1, hash_set<tree> *nontrap)
1883 {
1884   gimple *assign = last_and_only_stmt (middle_bb);
1885   tree lhs, rhs, name, name2;
1886   gphi *newphi;
1887   gassign *new_stmt;
1888   gimple_stmt_iterator gsi;
1889   source_location locus;
1890 
1891   /* Check if middle_bb contains of only one store.  */
1892   if (!assign
1893       || !gimple_assign_single_p (assign)
1894       || gimple_has_volatile_ops (assign))
1895     return false;
1896 
1897   /* And no PHI nodes so all uses in the single stmt are also
1898      available where we insert to.  */
1899   if (!gimple_seq_empty_p (phi_nodes (middle_bb)))
1900     return false;
1901 
1902   locus = gimple_location (assign);
1903   lhs = gimple_assign_lhs (assign);
1904   rhs = gimple_assign_rhs1 (assign);
1905   if (TREE_CODE (lhs) != MEM_REF
1906       || TREE_CODE (TREE_OPERAND (lhs, 0)) != SSA_NAME
1907       || !is_gimple_reg_type (TREE_TYPE (lhs)))
1908     return false;
1909 
1910   /* Prove that we can move the store down.  We could also check
1911      TREE_THIS_NOTRAP here, but in that case we also could move stores,
1912      whose value is not available readily, which we want to avoid.  */
1913   if (!nontrap->contains (lhs))
1914     return false;
1915 
1916   /* Now we've checked the constraints, so do the transformation:
1917      1) Remove the single store.  */
1918   gsi = gsi_for_stmt (assign);
1919   unlink_stmt_vdef (assign);
1920   gsi_remove (&gsi, true);
1921   release_defs (assign);
1922 
1923   /* Make both store and load use alias-set zero as we have to
1924      deal with the case of the store being a conditional change
1925      of the dynamic type.  */
1926   lhs = unshare_expr (lhs);
1927   tree *basep = &lhs;
1928   while (handled_component_p (*basep))
1929     basep = &TREE_OPERAND (*basep, 0);
1930   if (TREE_CODE (*basep) == MEM_REF
1931       || TREE_CODE (*basep) == TARGET_MEM_REF)
1932     TREE_OPERAND (*basep, 1)
1933       = fold_convert (ptr_type_node, TREE_OPERAND (*basep, 1));
1934   else
1935     *basep = build2 (MEM_REF, TREE_TYPE (*basep),
1936 		     build_fold_addr_expr (*basep),
1937 		     build_zero_cst (ptr_type_node));
1938 
1939   /* 2) Insert a load from the memory of the store to the temporary
1940         on the edge which did not contain the store.  */
1941   name = make_temp_ssa_name (TREE_TYPE (lhs), NULL, "cstore");
1942   new_stmt = gimple_build_assign (name, lhs);
1943   gimple_set_location (new_stmt, locus);
1944   gsi_insert_on_edge (e1, new_stmt);
1945 
1946   /* 3) Create a PHI node at the join block, with one argument
1947         holding the old RHS, and the other holding the temporary
1948         where we stored the old memory contents.  */
1949   name2 = make_temp_ssa_name (TREE_TYPE (lhs), NULL, "cstore");
1950   newphi = create_phi_node (name2, join_bb);
1951   add_phi_arg (newphi, rhs, e0, locus);
1952   add_phi_arg (newphi, name, e1, locus);
1953 
1954   lhs = unshare_expr (lhs);
1955   new_stmt = gimple_build_assign (lhs, PHI_RESULT (newphi));
1956 
1957   /* 4) Insert that PHI node.  */
1958   gsi = gsi_after_labels (join_bb);
1959   if (gsi_end_p (gsi))
1960     {
1961       gsi = gsi_last_bb (join_bb);
1962       gsi_insert_after (&gsi, new_stmt, GSI_NEW_STMT);
1963     }
1964   else
1965     gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
1966 
1967   return true;
1968 }
1969 
1970 /* Do the main work of conditional store replacement.  */
1971 
1972 static bool
cond_if_else_store_replacement_1(basic_block then_bb,basic_block else_bb,basic_block join_bb,gimple * then_assign,gimple * else_assign)1973 cond_if_else_store_replacement_1 (basic_block then_bb, basic_block else_bb,
1974 				  basic_block join_bb, gimple *then_assign,
1975 				  gimple *else_assign)
1976 {
1977   tree lhs_base, lhs, then_rhs, else_rhs, name;
1978   source_location then_locus, else_locus;
1979   gimple_stmt_iterator gsi;
1980   gphi *newphi;
1981   gassign *new_stmt;
1982 
1983   if (then_assign == NULL
1984       || !gimple_assign_single_p (then_assign)
1985       || gimple_clobber_p (then_assign)
1986       || gimple_has_volatile_ops (then_assign)
1987       || else_assign == NULL
1988       || !gimple_assign_single_p (else_assign)
1989       || gimple_clobber_p (else_assign)
1990       || gimple_has_volatile_ops (else_assign))
1991     return false;
1992 
1993   lhs = gimple_assign_lhs (then_assign);
1994   if (!is_gimple_reg_type (TREE_TYPE (lhs))
1995       || !operand_equal_p (lhs, gimple_assign_lhs (else_assign), 0))
1996     return false;
1997 
1998   lhs_base = get_base_address (lhs);
1999   if (lhs_base == NULL_TREE
2000       || (!DECL_P (lhs_base) && TREE_CODE (lhs_base) != MEM_REF))
2001     return false;
2002 
2003   then_rhs = gimple_assign_rhs1 (then_assign);
2004   else_rhs = gimple_assign_rhs1 (else_assign);
2005   then_locus = gimple_location (then_assign);
2006   else_locus = gimple_location (else_assign);
2007 
2008   /* Now we've checked the constraints, so do the transformation:
2009      1) Remove the stores.  */
2010   gsi = gsi_for_stmt (then_assign);
2011   unlink_stmt_vdef (then_assign);
2012   gsi_remove (&gsi, true);
2013   release_defs (then_assign);
2014 
2015   gsi = gsi_for_stmt (else_assign);
2016   unlink_stmt_vdef (else_assign);
2017   gsi_remove (&gsi, true);
2018   release_defs (else_assign);
2019 
2020   /* 2) Create a PHI node at the join block, with one argument
2021 	holding the old RHS, and the other holding the temporary
2022 	where we stored the old memory contents.  */
2023   name = make_temp_ssa_name (TREE_TYPE (lhs), NULL, "cstore");
2024   newphi = create_phi_node (name, join_bb);
2025   add_phi_arg (newphi, then_rhs, EDGE_SUCC (then_bb, 0), then_locus);
2026   add_phi_arg (newphi, else_rhs, EDGE_SUCC (else_bb, 0), else_locus);
2027 
2028   new_stmt = gimple_build_assign (lhs, PHI_RESULT (newphi));
2029 
2030   /* 3) Insert that PHI node.  */
2031   gsi = gsi_after_labels (join_bb);
2032   if (gsi_end_p (gsi))
2033     {
2034       gsi = gsi_last_bb (join_bb);
2035       gsi_insert_after (&gsi, new_stmt, GSI_NEW_STMT);
2036     }
2037   else
2038     gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
2039 
2040   return true;
2041 }
2042 
2043 /* Return the single store in BB with VDEF or NULL if there are
2044    other stores in the BB or loads following the store.  */
2045 
2046 static gimple *
single_trailing_store_in_bb(basic_block bb,tree vdef)2047 single_trailing_store_in_bb (basic_block bb, tree vdef)
2048 {
2049   if (SSA_NAME_IS_DEFAULT_DEF (vdef))
2050     return NULL;
2051   gimple *store = SSA_NAME_DEF_STMT (vdef);
2052   if (gimple_bb (store) != bb
2053       || gimple_code (store) == GIMPLE_PHI)
2054     return NULL;
2055 
2056   /* Verify there is no other store in this BB.  */
2057   if (!SSA_NAME_IS_DEFAULT_DEF (gimple_vuse (store))
2058       && gimple_bb (SSA_NAME_DEF_STMT (gimple_vuse (store))) == bb
2059       && gimple_code (SSA_NAME_DEF_STMT (gimple_vuse (store))) != GIMPLE_PHI)
2060     return NULL;
2061 
2062   /* Verify there is no load or store after the store.  */
2063   use_operand_p use_p;
2064   imm_use_iterator imm_iter;
2065   FOR_EACH_IMM_USE_FAST (use_p, imm_iter, gimple_vdef (store))
2066     if (USE_STMT (use_p) != store
2067 	&& gimple_bb (USE_STMT (use_p)) == bb)
2068       return NULL;
2069 
2070   return store;
2071 }
2072 
2073 /* Conditional store replacement.  We already know
2074    that the recognized pattern looks like so:
2075 
2076    split:
2077      if (cond) goto THEN_BB; else goto ELSE_BB (edge E1)
2078    THEN_BB:
2079      ...
2080      X = Y;
2081      ...
2082      goto JOIN_BB;
2083    ELSE_BB:
2084      ...
2085      X = Z;
2086      ...
2087      fallthrough (edge E0)
2088    JOIN_BB:
2089      some more
2090 
2091    We check that it is safe to sink the store to JOIN_BB by verifying that
2092    there are no read-after-write or write-after-write dependencies in
2093    THEN_BB and ELSE_BB.  */
2094 
2095 static bool
cond_if_else_store_replacement(basic_block then_bb,basic_block else_bb,basic_block join_bb)2096 cond_if_else_store_replacement (basic_block then_bb, basic_block else_bb,
2097                                 basic_block join_bb)
2098 {
2099   vec<data_reference_p> then_datarefs, else_datarefs;
2100   vec<ddr_p> then_ddrs, else_ddrs;
2101   gimple *then_store, *else_store;
2102   bool found, ok = false, res;
2103   struct data_dependence_relation *ddr;
2104   data_reference_p then_dr, else_dr;
2105   int i, j;
2106   tree then_lhs, else_lhs;
2107   basic_block blocks[3];
2108 
2109   /* Handle the case with single store in THEN_BB and ELSE_BB.  That is
2110      cheap enough to always handle as it allows us to elide dependence
2111      checking.  */
2112   gphi *vphi = NULL;
2113   for (gphi_iterator si = gsi_start_phis (join_bb); !gsi_end_p (si);
2114        gsi_next (&si))
2115     if (virtual_operand_p (gimple_phi_result (si.phi ())))
2116       {
2117 	vphi = si.phi ();
2118 	break;
2119       }
2120   if (!vphi)
2121     return false;
2122   tree then_vdef = PHI_ARG_DEF_FROM_EDGE (vphi, single_succ_edge (then_bb));
2123   tree else_vdef = PHI_ARG_DEF_FROM_EDGE (vphi, single_succ_edge (else_bb));
2124   gimple *then_assign = single_trailing_store_in_bb (then_bb, then_vdef);
2125   if (then_assign)
2126     {
2127       gimple *else_assign = single_trailing_store_in_bb (else_bb, else_vdef);
2128       if (else_assign)
2129 	return cond_if_else_store_replacement_1 (then_bb, else_bb, join_bb,
2130 						 then_assign, else_assign);
2131     }
2132 
2133   if (MAX_STORES_TO_SINK == 0)
2134     return false;
2135 
2136   /* Find data references.  */
2137   then_datarefs.create (1);
2138   else_datarefs.create (1);
2139   if ((find_data_references_in_bb (NULL, then_bb, &then_datarefs)
2140         == chrec_dont_know)
2141       || !then_datarefs.length ()
2142       || (find_data_references_in_bb (NULL, else_bb, &else_datarefs)
2143 	  == chrec_dont_know)
2144       || !else_datarefs.length ())
2145     {
2146       free_data_refs (then_datarefs);
2147       free_data_refs (else_datarefs);
2148       return false;
2149     }
2150 
2151   /* Find pairs of stores with equal LHS.  */
2152   auto_vec<gimple *, 1> then_stores, else_stores;
2153   FOR_EACH_VEC_ELT (then_datarefs, i, then_dr)
2154     {
2155       if (DR_IS_READ (then_dr))
2156         continue;
2157 
2158       then_store = DR_STMT (then_dr);
2159       then_lhs = gimple_get_lhs (then_store);
2160       if (then_lhs == NULL_TREE)
2161 	continue;
2162       found = false;
2163 
2164       FOR_EACH_VEC_ELT (else_datarefs, j, else_dr)
2165         {
2166           if (DR_IS_READ (else_dr))
2167             continue;
2168 
2169           else_store = DR_STMT (else_dr);
2170           else_lhs = gimple_get_lhs (else_store);
2171 	  if (else_lhs == NULL_TREE)
2172 	    continue;
2173 
2174           if (operand_equal_p (then_lhs, else_lhs, 0))
2175             {
2176               found = true;
2177               break;
2178             }
2179         }
2180 
2181       if (!found)
2182         continue;
2183 
2184       then_stores.safe_push (then_store);
2185       else_stores.safe_push (else_store);
2186     }
2187 
2188   /* No pairs of stores found.  */
2189   if (!then_stores.length ()
2190       || then_stores.length () > (unsigned) MAX_STORES_TO_SINK)
2191     {
2192       free_data_refs (then_datarefs);
2193       free_data_refs (else_datarefs);
2194       return false;
2195     }
2196 
2197   /* Compute and check data dependencies in both basic blocks.  */
2198   then_ddrs.create (1);
2199   else_ddrs.create (1);
2200   if (!compute_all_dependences (then_datarefs, &then_ddrs,
2201 				vNULL, false)
2202       || !compute_all_dependences (else_datarefs, &else_ddrs,
2203 				   vNULL, false))
2204     {
2205       free_dependence_relations (then_ddrs);
2206       free_dependence_relations (else_ddrs);
2207       free_data_refs (then_datarefs);
2208       free_data_refs (else_datarefs);
2209       return false;
2210     }
2211   blocks[0] = then_bb;
2212   blocks[1] = else_bb;
2213   blocks[2] = join_bb;
2214   renumber_gimple_stmt_uids_in_blocks (blocks, 3);
2215 
2216   /* Check that there are no read-after-write or write-after-write dependencies
2217      in THEN_BB.  */
2218   FOR_EACH_VEC_ELT (then_ddrs, i, ddr)
2219     {
2220       struct data_reference *dra = DDR_A (ddr);
2221       struct data_reference *drb = DDR_B (ddr);
2222 
2223       if (DDR_ARE_DEPENDENT (ddr) != chrec_known
2224           && ((DR_IS_READ (dra) && DR_IS_WRITE (drb)
2225                && gimple_uid (DR_STMT (dra)) > gimple_uid (DR_STMT (drb)))
2226               || (DR_IS_READ (drb) && DR_IS_WRITE (dra)
2227                   && gimple_uid (DR_STMT (drb)) > gimple_uid (DR_STMT (dra)))
2228               || (DR_IS_WRITE (dra) && DR_IS_WRITE (drb))))
2229         {
2230           free_dependence_relations (then_ddrs);
2231           free_dependence_relations (else_ddrs);
2232 	  free_data_refs (then_datarefs);
2233 	  free_data_refs (else_datarefs);
2234           return false;
2235         }
2236     }
2237 
2238   /* Check that there are no read-after-write or write-after-write dependencies
2239      in ELSE_BB.  */
2240   FOR_EACH_VEC_ELT (else_ddrs, i, ddr)
2241     {
2242       struct data_reference *dra = DDR_A (ddr);
2243       struct data_reference *drb = DDR_B (ddr);
2244 
2245       if (DDR_ARE_DEPENDENT (ddr) != chrec_known
2246           && ((DR_IS_READ (dra) && DR_IS_WRITE (drb)
2247                && gimple_uid (DR_STMT (dra)) > gimple_uid (DR_STMT (drb)))
2248               || (DR_IS_READ (drb) && DR_IS_WRITE (dra)
2249                   && gimple_uid (DR_STMT (drb)) > gimple_uid (DR_STMT (dra)))
2250               || (DR_IS_WRITE (dra) && DR_IS_WRITE (drb))))
2251         {
2252           free_dependence_relations (then_ddrs);
2253           free_dependence_relations (else_ddrs);
2254 	  free_data_refs (then_datarefs);
2255 	  free_data_refs (else_datarefs);
2256           return false;
2257         }
2258     }
2259 
2260   /* Sink stores with same LHS.  */
2261   FOR_EACH_VEC_ELT (then_stores, i, then_store)
2262     {
2263       else_store = else_stores[i];
2264       res = cond_if_else_store_replacement_1 (then_bb, else_bb, join_bb,
2265                                               then_store, else_store);
2266       ok = ok || res;
2267     }
2268 
2269   free_dependence_relations (then_ddrs);
2270   free_dependence_relations (else_ddrs);
2271   free_data_refs (then_datarefs);
2272   free_data_refs (else_datarefs);
2273 
2274   return ok;
2275 }
2276 
2277 /* Return TRUE if STMT has a VUSE whose corresponding VDEF is in BB.  */
2278 
2279 static bool
local_mem_dependence(gimple * stmt,basic_block bb)2280 local_mem_dependence (gimple *stmt, basic_block bb)
2281 {
2282   tree vuse = gimple_vuse (stmt);
2283   gimple *def;
2284 
2285   if (!vuse)
2286     return false;
2287 
2288   def = SSA_NAME_DEF_STMT (vuse);
2289   return (def && gimple_bb (def) == bb);
2290 }
2291 
2292 /* Given a "diamond" control-flow pattern where BB0 tests a condition,
2293    BB1 and BB2 are "then" and "else" blocks dependent on this test,
2294    and BB3 rejoins control flow following BB1 and BB2, look for
2295    opportunities to hoist loads as follows.  If BB3 contains a PHI of
2296    two loads, one each occurring in BB1 and BB2, and the loads are
2297    provably of adjacent fields in the same structure, then move both
2298    loads into BB0.  Of course this can only be done if there are no
2299    dependencies preventing such motion.
2300 
2301    One of the hoisted loads will always be speculative, so the
2302    transformation is currently conservative:
2303 
2304     - The fields must be strictly adjacent.
2305     - The two fields must occupy a single memory block that is
2306       guaranteed to not cross a page boundary.
2307 
2308     The last is difficult to prove, as such memory blocks should be
2309     aligned on the minimum of the stack alignment boundary and the
2310     alignment guaranteed by heap allocation interfaces.  Thus we rely
2311     on a parameter for the alignment value.
2312 
2313     Provided a good value is used for the last case, the first
2314     restriction could possibly be relaxed.  */
2315 
2316 static void
hoist_adjacent_loads(basic_block bb0,basic_block bb1,basic_block bb2,basic_block bb3)2317 hoist_adjacent_loads (basic_block bb0, basic_block bb1,
2318 		      basic_block bb2, basic_block bb3)
2319 {
2320   int param_align = PARAM_VALUE (PARAM_L1_CACHE_LINE_SIZE);
2321   unsigned param_align_bits = (unsigned) (param_align * BITS_PER_UNIT);
2322   gphi_iterator gsi;
2323 
2324   /* Walk the phis in bb3 looking for an opportunity.  We are looking
2325      for phis of two SSA names, one each of which is defined in bb1 and
2326      bb2.  */
2327   for (gsi = gsi_start_phis (bb3); !gsi_end_p (gsi); gsi_next (&gsi))
2328     {
2329       gphi *phi_stmt = gsi.phi ();
2330       gimple *def1, *def2;
2331       tree arg1, arg2, ref1, ref2, field1, field2;
2332       tree tree_offset1, tree_offset2, tree_size2, next;
2333       int offset1, offset2, size2;
2334       unsigned align1;
2335       gimple_stmt_iterator gsi2;
2336       basic_block bb_for_def1, bb_for_def2;
2337 
2338       if (gimple_phi_num_args (phi_stmt) != 2
2339 	  || virtual_operand_p (gimple_phi_result (phi_stmt)))
2340 	continue;
2341 
2342       arg1 = gimple_phi_arg_def (phi_stmt, 0);
2343       arg2 = gimple_phi_arg_def (phi_stmt, 1);
2344 
2345       if (TREE_CODE (arg1) != SSA_NAME
2346 	  || TREE_CODE (arg2) != SSA_NAME
2347 	  || SSA_NAME_IS_DEFAULT_DEF (arg1)
2348 	  || SSA_NAME_IS_DEFAULT_DEF (arg2))
2349 	continue;
2350 
2351       def1 = SSA_NAME_DEF_STMT (arg1);
2352       def2 = SSA_NAME_DEF_STMT (arg2);
2353 
2354       if ((gimple_bb (def1) != bb1 || gimple_bb (def2) != bb2)
2355 	  && (gimple_bb (def2) != bb1 || gimple_bb (def1) != bb2))
2356 	continue;
2357 
2358       /* Check the mode of the arguments to be sure a conditional move
2359 	 can be generated for it.  */
2360       if (optab_handler (movcc_optab, TYPE_MODE (TREE_TYPE (arg1)))
2361 	  == CODE_FOR_nothing)
2362 	continue;
2363 
2364       /* Both statements must be assignments whose RHS is a COMPONENT_REF.  */
2365       if (!gimple_assign_single_p (def1)
2366 	  || !gimple_assign_single_p (def2)
2367 	  || gimple_has_volatile_ops (def1)
2368 	  || gimple_has_volatile_ops (def2))
2369 	continue;
2370 
2371       ref1 = gimple_assign_rhs1 (def1);
2372       ref2 = gimple_assign_rhs1 (def2);
2373 
2374       if (TREE_CODE (ref1) != COMPONENT_REF
2375 	  || TREE_CODE (ref2) != COMPONENT_REF)
2376 	continue;
2377 
2378       /* The zeroth operand of the two component references must be
2379 	 identical.  It is not sufficient to compare get_base_address of
2380 	 the two references, because this could allow for different
2381 	 elements of the same array in the two trees.  It is not safe to
2382 	 assume that the existence of one array element implies the
2383 	 existence of a different one.  */
2384       if (!operand_equal_p (TREE_OPERAND (ref1, 0), TREE_OPERAND (ref2, 0), 0))
2385 	continue;
2386 
2387       field1 = TREE_OPERAND (ref1, 1);
2388       field2 = TREE_OPERAND (ref2, 1);
2389 
2390       /* Check for field adjacency, and ensure field1 comes first.  */
2391       for (next = DECL_CHAIN (field1);
2392 	   next && TREE_CODE (next) != FIELD_DECL;
2393 	   next = DECL_CHAIN (next))
2394 	;
2395 
2396       if (next != field2)
2397 	{
2398 	  for (next = DECL_CHAIN (field2);
2399 	       next && TREE_CODE (next) != FIELD_DECL;
2400 	       next = DECL_CHAIN (next))
2401 	    ;
2402 
2403 	  if (next != field1)
2404 	    continue;
2405 
2406 	  std::swap (field1, field2);
2407 	  std::swap (def1, def2);
2408 	}
2409 
2410       bb_for_def1 = gimple_bb (def1);
2411       bb_for_def2 = gimple_bb (def2);
2412 
2413       /* Check for proper alignment of the first field.  */
2414       tree_offset1 = bit_position (field1);
2415       tree_offset2 = bit_position (field2);
2416       tree_size2 = DECL_SIZE (field2);
2417 
2418       if (!tree_fits_uhwi_p (tree_offset1)
2419 	  || !tree_fits_uhwi_p (tree_offset2)
2420 	  || !tree_fits_uhwi_p (tree_size2))
2421 	continue;
2422 
2423       offset1 = tree_to_uhwi (tree_offset1);
2424       offset2 = tree_to_uhwi (tree_offset2);
2425       size2 = tree_to_uhwi (tree_size2);
2426       align1 = DECL_ALIGN (field1) % param_align_bits;
2427 
2428       if (offset1 % BITS_PER_UNIT != 0)
2429 	continue;
2430 
2431       /* For profitability, the two field references should fit within
2432 	 a single cache line.  */
2433       if (align1 + offset2 - offset1 + size2 > param_align_bits)
2434 	continue;
2435 
2436       /* The two expressions cannot be dependent upon vdefs defined
2437 	 in bb1/bb2.  */
2438       if (local_mem_dependence (def1, bb_for_def1)
2439 	  || local_mem_dependence (def2, bb_for_def2))
2440 	continue;
2441 
2442       /* The conditions are satisfied; hoist the loads from bb1 and bb2 into
2443 	 bb0.  We hoist the first one first so that a cache miss is handled
2444          efficiently regardless of hardware cache-fill policy.  */
2445       gsi2 = gsi_for_stmt (def1);
2446       gsi_move_to_bb_end (&gsi2, bb0);
2447       gsi2 = gsi_for_stmt (def2);
2448       gsi_move_to_bb_end (&gsi2, bb0);
2449 
2450       if (dump_file && (dump_flags & TDF_DETAILS))
2451 	{
2452 	  fprintf (dump_file,
2453 		   "\nHoisting adjacent loads from %d and %d into %d: \n",
2454 		   bb_for_def1->index, bb_for_def2->index, bb0->index);
2455 	  print_gimple_stmt (dump_file, def1, 0, TDF_VOPS|TDF_MEMSYMS);
2456 	  print_gimple_stmt (dump_file, def2, 0, TDF_VOPS|TDF_MEMSYMS);
2457 	}
2458     }
2459 }
2460 
2461 /* Determine whether we should attempt to hoist adjacent loads out of
2462    diamond patterns in pass_phiopt.  Always hoist loads if
2463    -fhoist-adjacent-loads is specified and the target machine has
2464    both a conditional move instruction and a defined cache line size.  */
2465 
2466 static bool
gate_hoist_loads(void)2467 gate_hoist_loads (void)
2468 {
2469   return (flag_hoist_adjacent_loads == 1
2470 	  && PARAM_VALUE (PARAM_L1_CACHE_LINE_SIZE)
2471 	  && HAVE_conditional_move);
2472 }
2473 
2474 /* This pass tries to replaces an if-then-else block with an
2475    assignment.  We have four kinds of transformations.  Some of these
2476    transformations are also performed by the ifcvt RTL optimizer.
2477 
2478    Conditional Replacement
2479    -----------------------
2480 
2481    This transformation, implemented in conditional_replacement,
2482    replaces
2483 
2484      bb0:
2485       if (cond) goto bb2; else goto bb1;
2486      bb1:
2487      bb2:
2488       x = PHI <0 (bb1), 1 (bb0), ...>;
2489 
2490    with
2491 
2492      bb0:
2493       x' = cond;
2494       goto bb2;
2495      bb2:
2496       x = PHI <x' (bb0), ...>;
2497 
2498    We remove bb1 as it becomes unreachable.  This occurs often due to
2499    gimplification of conditionals.
2500 
2501    Value Replacement
2502    -----------------
2503 
2504    This transformation, implemented in value_replacement, replaces
2505 
2506      bb0:
2507        if (a != b) goto bb2; else goto bb1;
2508      bb1:
2509      bb2:
2510        x = PHI <a (bb1), b (bb0), ...>;
2511 
2512    with
2513 
2514      bb0:
2515      bb2:
2516        x = PHI <b (bb0), ...>;
2517 
2518    This opportunity can sometimes occur as a result of other
2519    optimizations.
2520 
2521 
2522    Another case caught by value replacement looks like this:
2523 
2524      bb0:
2525        t1 = a == CONST;
2526        t2 = b > c;
2527        t3 = t1 & t2;
2528        if (t3 != 0) goto bb1; else goto bb2;
2529      bb1:
2530      bb2:
2531        x = PHI (CONST, a)
2532 
2533    Gets replaced with:
2534      bb0:
2535      bb2:
2536        t1 = a == CONST;
2537        t2 = b > c;
2538        t3 = t1 & t2;
2539        x = a;
2540 
2541    ABS Replacement
2542    ---------------
2543 
2544    This transformation, implemented in abs_replacement, replaces
2545 
2546      bb0:
2547        if (a >= 0) goto bb2; else goto bb1;
2548      bb1:
2549        x = -a;
2550      bb2:
2551        x = PHI <x (bb1), a (bb0), ...>;
2552 
2553    with
2554 
2555      bb0:
2556        x' = ABS_EXPR< a >;
2557      bb2:
2558        x = PHI <x' (bb0), ...>;
2559 
2560    MIN/MAX Replacement
2561    -------------------
2562 
2563    This transformation, minmax_replacement replaces
2564 
2565      bb0:
2566        if (a <= b) goto bb2; else goto bb1;
2567      bb1:
2568      bb2:
2569        x = PHI <b (bb1), a (bb0), ...>;
2570 
2571    with
2572 
2573      bb0:
2574        x' = MIN_EXPR (a, b)
2575      bb2:
2576        x = PHI <x' (bb0), ...>;
2577 
2578    A similar transformation is done for MAX_EXPR.
2579 
2580 
2581    This pass also performs a fifth transformation of a slightly different
2582    flavor.
2583 
2584    Factor conversion in COND_EXPR
2585    ------------------------------
2586 
2587    This transformation factors the conversion out of COND_EXPR with
2588    factor_out_conditional_conversion.
2589 
2590    For example:
2591    if (a <= CST) goto <bb 3>; else goto <bb 4>;
2592    <bb 3>:
2593    tmp = (int) a;
2594    <bb 4>:
2595    tmp = PHI <tmp, CST>
2596 
2597    Into:
2598    if (a <= CST) goto <bb 3>; else goto <bb 4>;
2599    <bb 3>:
2600    <bb 4>:
2601    a = PHI <a, CST>
2602    tmp = (int) a;
2603 
2604    Adjacent Load Hoisting
2605    ----------------------
2606 
2607    This transformation replaces
2608 
2609      bb0:
2610        if (...) goto bb2; else goto bb1;
2611      bb1:
2612        x1 = (<expr>).field1;
2613        goto bb3;
2614      bb2:
2615        x2 = (<expr>).field2;
2616      bb3:
2617        # x = PHI <x1, x2>;
2618 
2619    with
2620 
2621      bb0:
2622        x1 = (<expr>).field1;
2623        x2 = (<expr>).field2;
2624        if (...) goto bb2; else goto bb1;
2625      bb1:
2626        goto bb3;
2627      bb2:
2628      bb3:
2629        # x = PHI <x1, x2>;
2630 
2631    The purpose of this transformation is to enable generation of conditional
2632    move instructions such as Intel CMOVE or PowerPC ISEL.  Because one of
2633    the loads is speculative, the transformation is restricted to very
2634    specific cases to avoid introducing a page fault.  We are looking for
2635    the common idiom:
2636 
2637      if (...)
2638        x = y->left;
2639      else
2640        x = y->right;
2641 
2642    where left and right are typically adjacent pointers in a tree structure.  */
2643 
2644 namespace {
2645 
2646 const pass_data pass_data_phiopt =
2647 {
2648   GIMPLE_PASS, /* type */
2649   "phiopt", /* name */
2650   OPTGROUP_NONE, /* optinfo_flags */
2651   TV_TREE_PHIOPT, /* tv_id */
2652   ( PROP_cfg | PROP_ssa ), /* properties_required */
2653   0, /* properties_provided */
2654   0, /* properties_destroyed */
2655   0, /* todo_flags_start */
2656   0, /* todo_flags_finish */
2657 };
2658 
2659 class pass_phiopt : public gimple_opt_pass
2660 {
2661 public:
pass_phiopt(gcc::context * ctxt)2662   pass_phiopt (gcc::context *ctxt)
2663     : gimple_opt_pass (pass_data_phiopt, ctxt)
2664   {}
2665 
2666   /* opt_pass methods: */
clone()2667   opt_pass * clone () { return new pass_phiopt (m_ctxt); }
gate(function *)2668   virtual bool gate (function *) { return flag_ssa_phiopt; }
execute(function *)2669   virtual unsigned int execute (function *)
2670     {
2671       return tree_ssa_phiopt_worker (false, gate_hoist_loads ());
2672     }
2673 
2674 }; // class pass_phiopt
2675 
2676 } // anon namespace
2677 
2678 gimple_opt_pass *
make_pass_phiopt(gcc::context * ctxt)2679 make_pass_phiopt (gcc::context *ctxt)
2680 {
2681   return new pass_phiopt (ctxt);
2682 }
2683 
2684 namespace {
2685 
2686 const pass_data pass_data_cselim =
2687 {
2688   GIMPLE_PASS, /* type */
2689   "cselim", /* name */
2690   OPTGROUP_NONE, /* optinfo_flags */
2691   TV_TREE_PHIOPT, /* tv_id */
2692   ( PROP_cfg | PROP_ssa ), /* properties_required */
2693   0, /* properties_provided */
2694   0, /* properties_destroyed */
2695   0, /* todo_flags_start */
2696   0, /* todo_flags_finish */
2697 };
2698 
2699 class pass_cselim : public gimple_opt_pass
2700 {
2701 public:
pass_cselim(gcc::context * ctxt)2702   pass_cselim (gcc::context *ctxt)
2703     : gimple_opt_pass (pass_data_cselim, ctxt)
2704   {}
2705 
2706   /* opt_pass methods: */
gate(function *)2707   virtual bool gate (function *) { return flag_tree_cselim; }
execute(function *)2708   virtual unsigned int execute (function *) { return tree_ssa_cs_elim (); }
2709 
2710 }; // class pass_cselim
2711 
2712 } // anon namespace
2713 
2714 gimple_opt_pass *
make_pass_cselim(gcc::context * ctxt)2715 make_pass_cselim (gcc::context *ctxt)
2716 {
2717   return new pass_cselim (ctxt);
2718 }
2719