xref: /netbsd-src/external/gpl3/gcc/dist/gcc/tree-ssa-phiopt.cc (revision 0a3071956a3a9fdebdbf7f338cf2d439b45fc728)
1 /* Optimization of PHI nodes by converting them into straightline code.
2    Copyright (C) 2004-2022 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 "tree-ssa.h"
32 #include "optabs-tree.h"
33 #include "insn-config.h"
34 #include "gimple-pretty-print.h"
35 #include "fold-const.h"
36 #include "stor-layout.h"
37 #include "cfganal.h"
38 #include "gimplify.h"
39 #include "gimple-iterator.h"
40 #include "gimplify-me.h"
41 #include "tree-cfg.h"
42 #include "tree-dfa.h"
43 #include "domwalk.h"
44 #include "cfgloop.h"
45 #include "tree-data-ref.h"
46 #include "tree-scalar-evolution.h"
47 #include "tree-inline.h"
48 #include "case-cfn-macros.h"
49 #include "tree-eh.h"
50 #include "gimple-fold.h"
51 #include "internal-fn.h"
52 #include "gimple-range.h"
53 #include "gimple-match.h"
54 #include "dbgcnt.h"
55 #include "tree-ssa-propagate.h"
56 
57 static unsigned int tree_ssa_phiopt_worker (bool, bool, bool);
58 static bool two_value_replacement (basic_block, basic_block, edge, gphi *,
59 				   tree, tree);
60 static bool match_simplify_replacement (basic_block, basic_block,
61 					edge, edge, gphi *, tree, tree, bool);
62 static gphi *factor_out_conditional_conversion (edge, edge, gphi *, tree, tree,
63 						gimple *);
64 static int value_replacement (basic_block, basic_block,
65 			      edge, edge, gphi *, tree, tree);
66 static bool minmax_replacement (basic_block, basic_block,
67 				edge, edge, gphi *, tree, tree);
68 static bool spaceship_replacement (basic_block, basic_block,
69 				   edge, edge, gphi *, tree, tree);
70 static bool cond_removal_in_builtin_zero_pattern (basic_block, basic_block,
71 						  edge, edge, gphi *,
72 						  tree, tree);
73 static bool cond_store_replacement (basic_block, basic_block, edge, edge,
74 				    hash_set<tree> *);
75 static bool cond_if_else_store_replacement (basic_block, basic_block, basic_block);
76 static hash_set<tree> * get_non_trapping ();
77 static void replace_phi_edge_with_variable (basic_block, edge, gphi *, tree);
78 static void hoist_adjacent_loads (basic_block, basic_block,
79 				  basic_block, basic_block);
80 static bool gate_hoist_loads (void);
81 
82 /* This pass tries to transform conditional stores into unconditional
83    ones, enabling further simplifications with the simpler then and else
84    blocks.  In particular it replaces this:
85 
86      bb0:
87        if (cond) goto bb2; else goto bb1;
88      bb1:
89        *p = RHS;
90      bb2:
91 
92    with
93 
94      bb0:
95        if (cond) goto bb1; else goto bb2;
96      bb1:
97        condtmp' = *p;
98      bb2:
99        condtmp = PHI <RHS, condtmp'>
100        *p = condtmp;
101 
102    This transformation can only be done under several constraints,
103    documented below.  It also replaces:
104 
105      bb0:
106        if (cond) goto bb2; else goto bb1;
107      bb1:
108        *p = RHS1;
109        goto bb3;
110      bb2:
111        *p = RHS2;
112      bb3:
113 
114    with
115 
116      bb0:
117        if (cond) goto bb3; else goto bb1;
118      bb1:
119      bb3:
120        condtmp = PHI <RHS1, RHS2>
121        *p = condtmp;  */
122 
123 static unsigned int
tree_ssa_cs_elim(void)124 tree_ssa_cs_elim (void)
125 {
126   unsigned todo;
127   /* ???  We are not interested in loop related info, but the following
128      will create it, ICEing as we didn't init loops with pre-headers.
129      An interfacing issue of find_data_references_in_bb.  */
130   loop_optimizer_init (LOOPS_NORMAL);
131   scev_initialize ();
132   todo = tree_ssa_phiopt_worker (true, false, false);
133   scev_finalize ();
134   loop_optimizer_finalize ();
135   return todo;
136 }
137 
138 /* Return the singleton PHI in the SEQ of PHIs for edges E0 and E1. */
139 
140 static gphi *
single_non_singleton_phi_for_edges(gimple_seq seq,edge e0,edge e1)141 single_non_singleton_phi_for_edges (gimple_seq seq, edge e0, edge e1)
142 {
143   gimple_stmt_iterator i;
144   gphi *phi = NULL;
145   if (gimple_seq_singleton_p (seq))
146     return as_a <gphi *> (gsi_stmt (gsi_start (seq)));
147   for (i = gsi_start (seq); !gsi_end_p (i); gsi_next (&i))
148     {
149       gphi *p = as_a <gphi *> (gsi_stmt (i));
150       /* If the PHI arguments are equal then we can skip this PHI. */
151       if (operand_equal_for_phi_arg_p (gimple_phi_arg_def (p, e0->dest_idx),
152 				       gimple_phi_arg_def (p, e1->dest_idx)))
153 	continue;
154 
155       /* If we already have a PHI that has the two edge arguments are
156 	 different, then return it is not a singleton for these PHIs. */
157       if (phi)
158 	return NULL;
159 
160       phi = p;
161     }
162   return phi;
163 }
164 
165 /* The core routine of conditional store replacement and normal
166    phi optimizations.  Both share much of the infrastructure in how
167    to match applicable basic block patterns.  DO_STORE_ELIM is true
168    when we want to do conditional store replacement, false otherwise.
169    DO_HOIST_LOADS is true when we want to hoist adjacent loads out
170    of diamond control flow patterns, false otherwise.  */
171 static unsigned int
tree_ssa_phiopt_worker(bool do_store_elim,bool do_hoist_loads,bool early_p)172 tree_ssa_phiopt_worker (bool do_store_elim, bool do_hoist_loads, bool early_p)
173 {
174   basic_block bb;
175   basic_block *bb_order;
176   unsigned n, i;
177   bool cfgchanged = false;
178   hash_set<tree> *nontrap = 0;
179 
180   calculate_dominance_info (CDI_DOMINATORS);
181 
182   if (do_store_elim)
183     /* Calculate the set of non-trapping memory accesses.  */
184     nontrap = get_non_trapping ();
185 
186   /* Search every basic block for COND_EXPR we may be able to optimize.
187 
188      We walk the blocks in order that guarantees that a block with
189      a single predecessor is processed before the predecessor.
190      This ensures that we collapse inner ifs before visiting the
191      outer ones, and also that we do not try to visit a removed
192      block.  */
193   bb_order = single_pred_before_succ_order ();
194   n = n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS;
195 
196   for (i = 0; i < n; i++)
197     {
198       gimple *cond_stmt;
199       gphi *phi;
200       basic_block bb1, bb2;
201       edge e1, e2;
202       tree arg0, arg1;
203 
204       bb = bb_order[i];
205 
206       cond_stmt = last_stmt (bb);
207       /* Check to see if the last statement is a GIMPLE_COND.  */
208       if (!cond_stmt
209           || gimple_code (cond_stmt) != GIMPLE_COND)
210         continue;
211 
212       e1 = EDGE_SUCC (bb, 0);
213       bb1 = e1->dest;
214       e2 = EDGE_SUCC (bb, 1);
215       bb2 = e2->dest;
216 
217       /* We cannot do the optimization on abnormal edges.  */
218       if ((e1->flags & EDGE_ABNORMAL) != 0
219           || (e2->flags & EDGE_ABNORMAL) != 0)
220        continue;
221 
222       /* If either bb1's succ or bb2 or bb2's succ is non NULL.  */
223       if (EDGE_COUNT (bb1->succs) == 0
224 	  || EDGE_COUNT (bb2->succs) == 0)
225         continue;
226 
227       /* Find the bb which is the fall through to the other.  */
228       if (EDGE_SUCC (bb1, 0)->dest == bb2)
229         ;
230       else if (EDGE_SUCC (bb2, 0)->dest == bb1)
231         {
232 	  std::swap (bb1, bb2);
233 	  std::swap (e1, e2);
234 	}
235       else if (do_store_elim
236 	       && EDGE_SUCC (bb1, 0)->dest == EDGE_SUCC (bb2, 0)->dest)
237 	{
238 	  basic_block bb3 = EDGE_SUCC (bb1, 0)->dest;
239 
240 	  if (!single_succ_p (bb1)
241 	      || (EDGE_SUCC (bb1, 0)->flags & EDGE_FALLTHRU) == 0
242 	      || !single_succ_p (bb2)
243 	      || (EDGE_SUCC (bb2, 0)->flags & EDGE_FALLTHRU) == 0
244 	      || EDGE_COUNT (bb3->preds) != 2)
245 	    continue;
246 	  if (cond_if_else_store_replacement (bb1, bb2, bb3))
247 	    cfgchanged = true;
248 	  continue;
249 	}
250       else if (do_hoist_loads
251 	       && EDGE_SUCC (bb1, 0)->dest == EDGE_SUCC (bb2, 0)->dest)
252 	{
253 	  basic_block bb3 = EDGE_SUCC (bb1, 0)->dest;
254 
255 	  if (!FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (cond_stmt)))
256 	      && single_succ_p (bb1)
257 	      && single_succ_p (bb2)
258 	      && single_pred_p (bb1)
259 	      && single_pred_p (bb2)
260 	      && EDGE_COUNT (bb->succs) == 2
261 	      && EDGE_COUNT (bb3->preds) == 2
262 	      /* If one edge or the other is dominant, a conditional move
263 		 is likely to perform worse than the well-predicted branch.  */
264 	      && !predictable_edge_p (EDGE_SUCC (bb, 0))
265 	      && !predictable_edge_p (EDGE_SUCC (bb, 1)))
266 	    hoist_adjacent_loads (bb, bb1, bb2, bb3);
267 	  continue;
268 	}
269       else
270 	continue;
271 
272       e1 = EDGE_SUCC (bb1, 0);
273 
274       /* Make sure that bb1 is just a fall through.  */
275       if (!single_succ_p (bb1)
276 	  || (e1->flags & EDGE_FALLTHRU) == 0)
277         continue;
278 
279       if (do_store_elim)
280 	{
281 	  /* Also make sure that bb1 only have one predecessor and that it
282 	     is bb.  */
283 	  if (!single_pred_p (bb1)
284 	      || single_pred (bb1) != bb)
285 	    continue;
286 
287 	  /* bb1 is the middle block, bb2 the join block, bb the split block,
288 	     e1 the fallthrough edge from bb1 to bb2.  We can't do the
289 	     optimization if the join block has more than two predecessors.  */
290 	  if (EDGE_COUNT (bb2->preds) > 2)
291 	    continue;
292 	  if (cond_store_replacement (bb1, bb2, e1, e2, nontrap))
293 	    cfgchanged = true;
294 	}
295       else
296 	{
297 	  gimple_seq phis = phi_nodes (bb2);
298 	  gimple_stmt_iterator gsi;
299 	  bool candorest = true;
300 
301 	  /* Value replacement can work with more than one PHI
302 	     so try that first. */
303 	  if (!early_p)
304 	    for (gsi = gsi_start (phis); !gsi_end_p (gsi); gsi_next (&gsi))
305 	      {
306 		phi = as_a <gphi *> (gsi_stmt (gsi));
307 		arg0 = gimple_phi_arg_def (phi, e1->dest_idx);
308 		arg1 = gimple_phi_arg_def (phi, e2->dest_idx);
309 		if (value_replacement (bb, bb1, e1, e2, phi, arg0, arg1) == 2)
310 		  {
311 		    candorest = false;
312 		    cfgchanged = true;
313 		    break;
314 		  }
315 	      }
316 
317 	  if (!candorest)
318 	    continue;
319 
320 	  phi = single_non_singleton_phi_for_edges (phis, e1, e2);
321 	  if (!phi)
322 	    continue;
323 
324 	  arg0 = gimple_phi_arg_def (phi, e1->dest_idx);
325 	  arg1 = gimple_phi_arg_def (phi, e2->dest_idx);
326 
327 	  /* Something is wrong if we cannot find the arguments in the PHI
328 	     node.  */
329 	  gcc_assert (arg0 != NULL_TREE && arg1 != NULL_TREE);
330 
331 	  gphi *newphi;
332 	  if (single_pred_p (bb1)
333 	      && (newphi = factor_out_conditional_conversion (e1, e2, phi,
334 							      arg0, arg1,
335 							      cond_stmt)))
336 	    {
337 	      phi = newphi;
338 	      /* factor_out_conditional_conversion may create a new PHI in
339 		 BB2 and eliminate an existing PHI in BB2.  Recompute values
340 		 that may be affected by that change.  */
341 	      arg0 = gimple_phi_arg_def (phi, e1->dest_idx);
342 	      arg1 = gimple_phi_arg_def (phi, e2->dest_idx);
343 	      gcc_assert (arg0 != NULL_TREE && arg1 != NULL_TREE);
344 	    }
345 
346 	  /* Do the replacement of conditional if it can be done.  */
347 	  if (!early_p && two_value_replacement (bb, bb1, e2, phi, arg0, arg1))
348 	    cfgchanged = true;
349 	  else if (match_simplify_replacement (bb, bb1, e1, e2, phi,
350 					       arg0, arg1,
351 					       early_p))
352 	    cfgchanged = true;
353 	  else if (!early_p
354 		   && single_pred_p (bb1)
355 		   && cond_removal_in_builtin_zero_pattern (bb, bb1, e1, e2,
356 							    phi, arg0, arg1))
357 	    cfgchanged = true;
358 	  else if (minmax_replacement (bb, bb1, e1, e2, phi, arg0, arg1))
359 	    cfgchanged = true;
360 	  else if (single_pred_p (bb1)
361 		   && spaceship_replacement (bb, bb1, e1, e2, phi, arg0, arg1))
362 	    cfgchanged = true;
363 	}
364     }
365 
366   free (bb_order);
367 
368   if (do_store_elim)
369     delete nontrap;
370   /* If the CFG has changed, we should cleanup the CFG.  */
371   if (cfgchanged && do_store_elim)
372     {
373       /* In cond-store replacement we have added some loads on edges
374          and new VOPS (as we moved the store, and created a load).  */
375       gsi_commit_edge_inserts ();
376       return TODO_cleanup_cfg | TODO_update_ssa_only_virtuals;
377     }
378   else if (cfgchanged)
379     return TODO_cleanup_cfg;
380   return 0;
381 }
382 
383 /* Replace PHI node element whose edge is E in block BB with variable NEW.
384    Remove the edge from COND_BLOCK which does not lead to BB (COND_BLOCK
385    is known to have two edges, one of which must reach BB).  */
386 
387 static void
replace_phi_edge_with_variable(basic_block cond_block,edge e,gphi * phi,tree new_tree)388 replace_phi_edge_with_variable (basic_block cond_block,
389 				edge e, gphi *phi, tree new_tree)
390 {
391   basic_block bb = gimple_bb (phi);
392   gimple_stmt_iterator gsi;
393   tree phi_result = PHI_RESULT (phi);
394 
395   /* Duplicate range info if they are the only things setting the target PHI.
396      This is needed as later on, the new_tree will be replacing
397      The assignement of the PHI.
398      For an example:
399      bb1:
400      _4 = min<a_1, 255>
401      goto bb2
402 
403      # RANGE [-INF, 255]
404      a_3 = PHI<_4(1)>
405      bb3:
406 
407      use(a_3)
408      And _4 gets propagated into the use of a_3 and losing the range info.
409      This can't be done for more than 2 incoming edges as the propagation
410      won't happen.
411      The new_tree needs to be defined in the same basic block as the conditional.  */
412   if (TREE_CODE (new_tree) == SSA_NAME
413       && EDGE_COUNT (gimple_bb (phi)->preds) == 2
414       && INTEGRAL_TYPE_P (TREE_TYPE (phi_result))
415       && !SSA_NAME_RANGE_INFO (new_tree)
416       && SSA_NAME_RANGE_INFO (phi_result)
417       && gimple_bb (SSA_NAME_DEF_STMT (new_tree)) == cond_block
418       && dbg_cnt (phiopt_edge_range))
419     duplicate_ssa_name_range_info (new_tree,
420 				   SSA_NAME_RANGE_TYPE (phi_result),
421 				   SSA_NAME_RANGE_INFO (phi_result));
422 
423   /* Change the PHI argument to new.  */
424   SET_USE (PHI_ARG_DEF_PTR (phi, e->dest_idx), new_tree);
425 
426   /* Remove the empty basic block.  */
427   edge edge_to_remove;
428   if (EDGE_SUCC (cond_block, 0)->dest == bb)
429     edge_to_remove = EDGE_SUCC (cond_block, 1);
430   else
431     edge_to_remove = EDGE_SUCC (cond_block, 0);
432   if (EDGE_COUNT (edge_to_remove->dest->preds) == 1)
433     {
434       e->flags |= EDGE_FALLTHRU;
435       e->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
436       e->probability = profile_probability::always ();
437       delete_basic_block (edge_to_remove->dest);
438 
439       /* Eliminate the COND_EXPR at the end of COND_BLOCK.  */
440       gsi = gsi_last_bb (cond_block);
441       gsi_remove (&gsi, true);
442     }
443   else
444     {
445       /* If there are other edges into the middle block make
446 	 CFG cleanup deal with the edge removal to avoid
447 	 updating dominators here in a non-trivial way.  */
448       gcond *cond = as_a <gcond *> (last_stmt (cond_block));
449       if (edge_to_remove->flags & EDGE_TRUE_VALUE)
450 	gimple_cond_make_false (cond);
451       else
452 	gimple_cond_make_true (cond);
453     }
454 
455   statistics_counter_event (cfun, "Replace PHI with variable", 1);
456 
457   if (dump_file && (dump_flags & TDF_DETAILS))
458     fprintf (dump_file,
459 	      "COND_EXPR in block %d and PHI in block %d converted to straightline code.\n",
460 	      cond_block->index,
461 	      bb->index);
462 }
463 
464 /* PR66726: Factor conversion out of COND_EXPR.  If the arguments of the PHI
465    stmt are CONVERT_STMT, factor out the conversion and perform the conversion
466    to the result of PHI stmt.  COND_STMT is the controlling predicate.
467    Return the newly-created PHI, if any.  */
468 
469 static gphi *
factor_out_conditional_conversion(edge e0,edge e1,gphi * phi,tree arg0,tree arg1,gimple * cond_stmt)470 factor_out_conditional_conversion (edge e0, edge e1, gphi *phi,
471 				   tree arg0, tree arg1, gimple *cond_stmt)
472 {
473   gimple *arg0_def_stmt = NULL, *arg1_def_stmt = NULL, *new_stmt;
474   tree new_arg0 = NULL_TREE, new_arg1 = NULL_TREE;
475   tree temp, result;
476   gphi *newphi;
477   gimple_stmt_iterator gsi, gsi_for_def;
478   location_t locus = gimple_location (phi);
479   enum tree_code convert_code;
480 
481   /* Handle only PHI statements with two arguments.  TODO: If all
482      other arguments to PHI are INTEGER_CST or if their defining
483      statement have the same unary operation, we can handle more
484      than two arguments too.  */
485   if (gimple_phi_num_args (phi) != 2)
486     return NULL;
487 
488   /* First canonicalize to simplify tests.  */
489   if (TREE_CODE (arg0) != SSA_NAME)
490     {
491       std::swap (arg0, arg1);
492       std::swap (e0, e1);
493     }
494 
495   if (TREE_CODE (arg0) != SSA_NAME
496       || (TREE_CODE (arg1) != SSA_NAME
497 	  && TREE_CODE (arg1) != INTEGER_CST))
498     return NULL;
499 
500   /* Check if arg0 is an SSA_NAME and the stmt which defines arg0 is
501      a conversion.  */
502   arg0_def_stmt = SSA_NAME_DEF_STMT (arg0);
503   if (!gimple_assign_cast_p (arg0_def_stmt))
504     return NULL;
505 
506   /* Use the RHS as new_arg0.  */
507   convert_code = gimple_assign_rhs_code (arg0_def_stmt);
508   new_arg0 = gimple_assign_rhs1 (arg0_def_stmt);
509   if (convert_code == VIEW_CONVERT_EXPR)
510     {
511       new_arg0 = TREE_OPERAND (new_arg0, 0);
512       if (!is_gimple_reg_type (TREE_TYPE (new_arg0)))
513 	return NULL;
514     }
515   if (TREE_CODE (new_arg0) == SSA_NAME
516       && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_arg0))
517     return NULL;
518 
519   if (TREE_CODE (arg1) == SSA_NAME)
520     {
521       /* Check if arg1 is an SSA_NAME and the stmt which defines arg1
522 	 is a conversion.  */
523       arg1_def_stmt = SSA_NAME_DEF_STMT (arg1);
524       if (!is_gimple_assign (arg1_def_stmt)
525 	  || gimple_assign_rhs_code (arg1_def_stmt) != convert_code)
526 	return NULL;
527 
528       /* Either arg1_def_stmt or arg0_def_stmt should be conditional.  */
529       if (dominated_by_p (CDI_DOMINATORS, gimple_bb (phi), gimple_bb (arg0_def_stmt))
530 	  && dominated_by_p (CDI_DOMINATORS,
531 			     gimple_bb (phi), gimple_bb (arg1_def_stmt)))
532 	return NULL;
533 
534       /* Use the RHS as new_arg1.  */
535       new_arg1 = gimple_assign_rhs1 (arg1_def_stmt);
536       if (convert_code == VIEW_CONVERT_EXPR)
537 	new_arg1 = TREE_OPERAND (new_arg1, 0);
538       if (TREE_CODE (new_arg1) == SSA_NAME
539 	  && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_arg1))
540 	return NULL;
541     }
542   else
543     {
544       /* arg0_def_stmt should be conditional.  */
545       if (dominated_by_p (CDI_DOMINATORS, gimple_bb (phi), gimple_bb (arg0_def_stmt)))
546 	return NULL;
547       /* If arg1 is an INTEGER_CST, fold it to new type.  */
548       if (INTEGRAL_TYPE_P (TREE_TYPE (new_arg0))
549 	  && int_fits_type_p (arg1, TREE_TYPE (new_arg0)))
550 	{
551 	  if (gimple_assign_cast_p (arg0_def_stmt))
552 	    {
553 	      /* For the INTEGER_CST case, we are just moving the
554 		 conversion from one place to another, which can often
555 		 hurt as the conversion moves further away from the
556 		 statement that computes the value.  So, perform this
557 		 only if new_arg0 is an operand of COND_STMT, or
558 		 if arg0_def_stmt is the only non-debug stmt in
559 		 its basic block, because then it is possible this
560 		 could enable further optimizations (minmax replacement
561 		 etc.).  See PR71016.  */
562 	      if (new_arg0 != gimple_cond_lhs (cond_stmt)
563 		  && new_arg0 != gimple_cond_rhs (cond_stmt)
564 		  && gimple_bb (arg0_def_stmt) == e0->src)
565 		{
566 		  gsi = gsi_for_stmt (arg0_def_stmt);
567 		  gsi_prev_nondebug (&gsi);
568 		  if (!gsi_end_p (gsi))
569 		    {
570 		      if (gassign *assign
571 			    = dyn_cast <gassign *> (gsi_stmt (gsi)))
572 			{
573 			  tree lhs = gimple_assign_lhs (assign);
574 			  enum tree_code ass_code
575 			    = gimple_assign_rhs_code (assign);
576 			  if (ass_code != MAX_EXPR && ass_code != MIN_EXPR)
577 			    return NULL;
578 			  if (lhs != gimple_assign_rhs1 (arg0_def_stmt))
579 			    return NULL;
580 			  gsi_prev_nondebug (&gsi);
581 			  if (!gsi_end_p (gsi))
582 			    return NULL;
583 			}
584 		      else
585 			return NULL;
586 		    }
587 		  gsi = gsi_for_stmt (arg0_def_stmt);
588 		  gsi_next_nondebug (&gsi);
589 		  if (!gsi_end_p (gsi))
590 		    return NULL;
591 		}
592 	      new_arg1 = fold_convert (TREE_TYPE (new_arg0), arg1);
593 	    }
594 	  else
595 	    return NULL;
596 	}
597       else
598 	return NULL;
599     }
600 
601   /*  If arg0/arg1 have > 1 use, then this transformation actually increases
602       the number of expressions evaluated at runtime.  */
603   if (!has_single_use (arg0)
604       || (arg1_def_stmt && !has_single_use (arg1)))
605     return NULL;
606 
607   /* If types of new_arg0 and new_arg1 are different bailout.  */
608   if (!types_compatible_p (TREE_TYPE (new_arg0), TREE_TYPE (new_arg1)))
609     return NULL;
610 
611   /* Create a new PHI stmt.  */
612   result = PHI_RESULT (phi);
613   temp = make_ssa_name (TREE_TYPE (new_arg0), NULL);
614   newphi = create_phi_node (temp, gimple_bb (phi));
615 
616   if (dump_file && (dump_flags & TDF_DETAILS))
617     {
618       fprintf (dump_file, "PHI ");
619       print_generic_expr (dump_file, gimple_phi_result (phi));
620       fprintf (dump_file,
621 	       " changed to factor conversion out from COND_EXPR.\n");
622       fprintf (dump_file, "New stmt with CAST that defines ");
623       print_generic_expr (dump_file, result);
624       fprintf (dump_file, ".\n");
625     }
626 
627   /* Remove the old cast(s) that has single use.  */
628   gsi_for_def = gsi_for_stmt (arg0_def_stmt);
629   gsi_remove (&gsi_for_def, true);
630   release_defs (arg0_def_stmt);
631 
632   if (arg1_def_stmt)
633     {
634       gsi_for_def = gsi_for_stmt (arg1_def_stmt);
635       gsi_remove (&gsi_for_def, true);
636       release_defs (arg1_def_stmt);
637     }
638 
639   add_phi_arg (newphi, new_arg0, e0, locus);
640   add_phi_arg (newphi, new_arg1, e1, locus);
641 
642   /* Create the conversion stmt and insert it.  */
643   if (convert_code == VIEW_CONVERT_EXPR)
644     {
645       temp = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (result), temp);
646       new_stmt = gimple_build_assign (result, temp);
647     }
648   else
649     new_stmt = gimple_build_assign (result, convert_code, temp);
650   gsi = gsi_after_labels (gimple_bb (phi));
651   gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT);
652 
653   /* Remove the original PHI stmt.  */
654   gsi = gsi_for_stmt (phi);
655   gsi_remove (&gsi, true);
656 
657   statistics_counter_event (cfun, "factored out cast", 1);
658 
659   return newphi;
660 }
661 
662 /* Optimize
663    # x_5 in range [cst1, cst2] where cst2 = cst1 + 1
664    if (x_5 op cstN) # where op is == or != and N is 1 or 2
665      goto bb3;
666    else
667      goto bb4;
668    bb3:
669    bb4:
670    # r_6 = PHI<cst3(2), cst4(3)> # where cst3 == cst4 + 1 or cst4 == cst3 + 1
671 
672    to r_6 = x_5 + (min (cst3, cst4) - cst1) or
673    r_6 = (min (cst3, cst4) + cst1) - x_5 depending on op, N and which
674    of cst3 and cst4 is smaller.  */
675 
676 static bool
two_value_replacement(basic_block cond_bb,basic_block middle_bb,edge e1,gphi * phi,tree arg0,tree arg1)677 two_value_replacement (basic_block cond_bb, basic_block middle_bb,
678 		       edge e1, gphi *phi, tree arg0, tree arg1)
679 {
680   /* Only look for adjacent integer constants.  */
681   if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0))
682       || !INTEGRAL_TYPE_P (TREE_TYPE (arg1))
683       || TREE_CODE (arg0) != INTEGER_CST
684       || TREE_CODE (arg1) != INTEGER_CST
685       || (tree_int_cst_lt (arg0, arg1)
686 	  ? wi::to_widest (arg0) + 1 != wi::to_widest (arg1)
687 	  : wi::to_widest (arg1) + 1 != wi::to_widest (arg0)))
688     return false;
689 
690   if (!empty_block_p (middle_bb))
691     return false;
692 
693   gimple *stmt = last_stmt (cond_bb);
694   tree lhs = gimple_cond_lhs (stmt);
695   tree rhs = gimple_cond_rhs (stmt);
696 
697   if (TREE_CODE (lhs) != SSA_NAME
698       || !INTEGRAL_TYPE_P (TREE_TYPE (lhs))
699       || TREE_CODE (rhs) != INTEGER_CST)
700     return false;
701 
702   switch (gimple_cond_code (stmt))
703     {
704     case EQ_EXPR:
705     case NE_EXPR:
706       break;
707     default:
708       return false;
709     }
710 
711   /* Defer boolean x ? 0 : {1,-1} or x ? {1,-1} : 0 to
712      match_simplify_replacement.  */
713   if (TREE_CODE (TREE_TYPE (lhs)) == BOOLEAN_TYPE
714       && (integer_zerop (arg0)
715 	  || integer_zerop (arg1)
716 	  || TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE
717 	  || (TYPE_PRECISION (TREE_TYPE (arg0))
718 	      <= TYPE_PRECISION (TREE_TYPE (lhs)))))
719     return false;
720 
721   wide_int min, max;
722   value_range r;
723   get_range_query (cfun)->range_of_expr (r, lhs);
724 
725   if (r.kind () == VR_RANGE)
726     {
727       min = r.lower_bound ();
728       max = r.upper_bound ();
729     }
730   else
731     {
732       int prec = TYPE_PRECISION (TREE_TYPE (lhs));
733       signop sgn = TYPE_SIGN (TREE_TYPE (lhs));
734       min = wi::min_value (prec, sgn);
735       max = wi::max_value (prec, sgn);
736     }
737   if (min + 1 != max
738       || (wi::to_wide (rhs) != min
739 	  && wi::to_wide (rhs) != max))
740     return false;
741 
742   /* We need to know which is the true edge and which is the false
743      edge so that we know when to invert the condition below.  */
744   edge true_edge, false_edge;
745   extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
746   if ((gimple_cond_code (stmt) == EQ_EXPR)
747       ^ (wi::to_wide (rhs) == max)
748       ^ (e1 == false_edge))
749     std::swap (arg0, arg1);
750 
751   tree type;
752   if (TYPE_PRECISION (TREE_TYPE (lhs)) == TYPE_PRECISION (TREE_TYPE (arg0)))
753     {
754       /* Avoid performing the arithmetics in bool type which has different
755 	 semantics, otherwise prefer unsigned types from the two with
756 	 the same precision.  */
757       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE
758 	  || !TYPE_UNSIGNED (TREE_TYPE (arg0)))
759 	type = TREE_TYPE (lhs);
760       else
761 	type = TREE_TYPE (arg0);
762     }
763   else if (TYPE_PRECISION (TREE_TYPE (lhs)) > TYPE_PRECISION (TREE_TYPE (arg0)))
764     type = TREE_TYPE (lhs);
765   else
766     type = TREE_TYPE (arg0);
767 
768   min = wide_int::from (min, TYPE_PRECISION (type),
769 			TYPE_SIGN (TREE_TYPE (lhs)));
770   wide_int a = wide_int::from (wi::to_wide (arg0), TYPE_PRECISION (type),
771 			       TYPE_SIGN (TREE_TYPE (arg0)));
772   enum tree_code code;
773   wi::overflow_type ovf;
774   if (tree_int_cst_lt (arg0, arg1))
775     {
776       code = PLUS_EXPR;
777       a -= min;
778       if (!TYPE_UNSIGNED (type))
779 	{
780 	  /* lhs is known to be in range [min, min+1] and we want to add a
781 	     to it.  Check if that operation can overflow for those 2 values
782 	     and if yes, force unsigned type.  */
783 	  wi::add (min + (wi::neg_p (a) ? 0 : 1), a, SIGNED, &ovf);
784 	  if (ovf)
785 	    type = unsigned_type_for (type);
786 	}
787     }
788   else
789     {
790       code = MINUS_EXPR;
791       a += min;
792       if (!TYPE_UNSIGNED (type))
793 	{
794 	  /* lhs is known to be in range [min, min+1] and we want to subtract
795 	     it from a.  Check if that operation can overflow for those 2
796 	     values and if yes, force unsigned type.  */
797 	  wi::sub (a, min + (wi::neg_p (min) ? 0 : 1), SIGNED, &ovf);
798 	  if (ovf)
799 	    type = unsigned_type_for (type);
800 	}
801     }
802 
803   tree arg = wide_int_to_tree (type, a);
804   gimple_seq stmts = NULL;
805   lhs = gimple_convert (&stmts, type, lhs);
806   tree new_rhs;
807   if (code == PLUS_EXPR)
808     new_rhs = gimple_build (&stmts, PLUS_EXPR, type, lhs, arg);
809   else
810     new_rhs = gimple_build (&stmts, MINUS_EXPR, type, arg, lhs);
811   new_rhs = gimple_convert (&stmts, TREE_TYPE (arg0), new_rhs);
812   gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
813   gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
814 
815   replace_phi_edge_with_variable (cond_bb, e1, phi, new_rhs);
816 
817   /* Note that we optimized this PHI.  */
818   return true;
819 }
820 
821 /* Return TRUE if SEQ/OP pair should be allowed during early phiopt.
822    Currently this is to allow MIN/MAX and ABS/NEGATE and constants.  */
823 static bool
phiopt_early_allow(gimple_seq & seq,gimple_match_op & op)824 phiopt_early_allow (gimple_seq &seq, gimple_match_op &op)
825 {
826   /* Don't allow functions. */
827   if (!op.code.is_tree_code ())
828     return false;
829   tree_code code = (tree_code)op.code;
830 
831   /* For non-empty sequence, only allow one statement.  */
832   if (!gimple_seq_empty_p (seq))
833     {
834       /* Check to make sure op was already a SSA_NAME.  */
835       if (code != SSA_NAME)
836 	return false;
837       if (!gimple_seq_singleton_p (seq))
838 	return false;
839       gimple *stmt = gimple_seq_first_stmt (seq);
840       /* Only allow assignments.  */
841       if (!is_gimple_assign (stmt))
842 	return false;
843       if (gimple_assign_lhs (stmt) != op.ops[0])
844 	return false;
845       code = gimple_assign_rhs_code (stmt);
846     }
847 
848   switch (code)
849     {
850       case MIN_EXPR:
851       case MAX_EXPR:
852       case ABS_EXPR:
853       case ABSU_EXPR:
854       case NEGATE_EXPR:
855       case SSA_NAME:
856 	return true;
857       case INTEGER_CST:
858       case REAL_CST:
859       case VECTOR_CST:
860       case FIXED_CST:
861 	return true;
862       default:
863 	return false;
864     }
865 }
866 
867 /* gimple_simplify_phiopt is like gimple_simplify but designed for PHIOPT.
868    Return NULL if nothing can be simplified or the resulting simplified value
869    with parts pushed if EARLY_P was true. Also rejects non allowed tree code
870    if EARLY_P is set.
871    Takes the comparison from COMP_STMT and two args, ARG0 and ARG1 and tries
872    to simplify CMP ? ARG0 : ARG1.
873    Also try to simplify (!CMP) ? ARG1 : ARG0 if the non-inverse failed.  */
874 static tree
gimple_simplify_phiopt(bool early_p,tree type,gimple * comp_stmt,tree arg0,tree arg1,gimple_seq * seq)875 gimple_simplify_phiopt (bool early_p, tree type, gimple *comp_stmt,
876 			tree arg0, tree arg1,
877 			gimple_seq *seq)
878 {
879   tree result;
880   gimple_seq seq1 = NULL;
881   enum tree_code comp_code = gimple_cond_code (comp_stmt);
882   location_t loc = gimple_location (comp_stmt);
883   tree cmp0 = gimple_cond_lhs (comp_stmt);
884   tree cmp1 = gimple_cond_rhs (comp_stmt);
885   /* To handle special cases like floating point comparison, it is easier and
886      less error-prone to build a tree and gimplify it on the fly though it is
887      less efficient.
888      Don't use fold_build2 here as that might create (bool)a instead of just
889      "a != 0".  */
890   tree cond = build2_loc (loc, comp_code, boolean_type_node,
891 			  cmp0, cmp1);
892   gimple_match_op op (gimple_match_cond::UNCOND,
893 		      COND_EXPR, type, cond, arg0, arg1);
894 
895   if (op.resimplify (&seq1, follow_all_ssa_edges))
896     {
897       /* Early we want only to allow some generated tree codes. */
898       if (!early_p
899 	  || phiopt_early_allow (seq1, op))
900 	{
901 	  result = maybe_push_res_to_seq (&op, &seq1);
902 	  if (result)
903 	    {
904 	      if (loc != UNKNOWN_LOCATION)
905 		annotate_all_with_location (seq1, loc);
906 	      gimple_seq_add_seq_without_update (seq, seq1);
907 	      return result;
908 	    }
909 	}
910     }
911   gimple_seq_discard (seq1);
912   seq1 = NULL;
913 
914   /* Try the inverted comparison, that is !COMP ? ARG1 : ARG0. */
915   comp_code = invert_tree_comparison (comp_code, HONOR_NANS (cmp0));
916 
917   if (comp_code == ERROR_MARK)
918     return NULL;
919 
920   cond = build2_loc (loc,
921 		     comp_code, boolean_type_node,
922 		     cmp0, cmp1);
923   gimple_match_op op1 (gimple_match_cond::UNCOND,
924 		       COND_EXPR, type, cond, arg1, arg0);
925 
926   if (op1.resimplify (&seq1, follow_all_ssa_edges))
927     {
928       /* Early we want only to allow some generated tree codes. */
929       if (!early_p
930 	  || phiopt_early_allow (seq1, op1))
931 	{
932 	  result = maybe_push_res_to_seq (&op1, &seq1);
933 	  if (result)
934 	    {
935 	      if (loc != UNKNOWN_LOCATION)
936 		annotate_all_with_location (seq1, loc);
937 	      gimple_seq_add_seq_without_update (seq, seq1);
938 	      return result;
939 	    }
940 	}
941     }
942   gimple_seq_discard (seq1);
943 
944   return NULL;
945 }
946 
947 /*  The function match_simplify_replacement does the main work of doing the
948     replacement using match and simplify.  Return true if the replacement is done.
949     Otherwise return false.
950     BB is the basic block where the replacement is going to be done on.  ARG0
951     is argument 0 from PHI.  Likewise for ARG1.  */
952 
953 static bool
match_simplify_replacement(basic_block cond_bb,basic_block middle_bb,edge e0,edge e1,gphi * phi,tree arg0,tree arg1,bool early_p)954 match_simplify_replacement (basic_block cond_bb, basic_block middle_bb,
955 			    edge e0, edge e1, gphi *phi,
956 			    tree arg0, tree arg1, bool early_p)
957 {
958   gimple *stmt;
959   gimple_stmt_iterator gsi;
960   edge true_edge, false_edge;
961   gimple_seq seq = NULL;
962   tree result;
963   gimple *stmt_to_move = NULL;
964 
965   /* Special case A ? B : B as this will always simplify to B. */
966   if (operand_equal_for_phi_arg_p (arg0, arg1))
967     return false;
968 
969   /* If the basic block only has a cheap preparation statement,
970      allow it and move it once the transformation is done. */
971   if (!empty_block_p (middle_bb))
972     {
973       if (!single_pred_p (middle_bb))
974 	return false;
975 
976       /* The middle bb cannot have phi nodes as we don't
977 	 move those assignments yet. */
978       if (!gimple_seq_empty_p (phi_nodes (middle_bb)))
979 	return false;
980 
981       stmt_to_move = last_and_only_stmt (middle_bb);
982       if (!stmt_to_move)
983 	return false;
984 
985       if (gimple_vuse (stmt_to_move))
986 	return false;
987 
988       if (gimple_could_trap_p (stmt_to_move)
989 	  || gimple_has_side_effects (stmt_to_move))
990 	return false;
991 
992       if (gimple_uses_undefined_value_p (stmt_to_move))
993 	return false;
994 
995       /* Allow assignments and not no calls.
996 	 As const calls don't match any of the above, yet they could
997 	 still have some side-effects - they could contain
998 	 gimple_could_trap_p statements, like floating point
999 	 exceptions or integer division by zero.  See PR70586.
1000 	 FIXME: perhaps gimple_has_side_effects or gimple_could_trap_p
1001 	 should handle this.  */
1002       if (!is_gimple_assign (stmt_to_move))
1003 	return false;
1004 
1005       tree lhs = gimple_assign_lhs (stmt_to_move);
1006       gimple *use_stmt;
1007       use_operand_p use_p;
1008 
1009       /* Allow only a statement which feeds into the phi.  */
1010       if (!lhs || TREE_CODE (lhs) != SSA_NAME
1011 	  || !single_imm_use (lhs, &use_p, &use_stmt)
1012 	  || use_stmt != phi)
1013 	return false;
1014     }
1015 
1016     /* At this point we know we have a GIMPLE_COND with two successors.
1017      One successor is BB, the other successor is an empty block which
1018      falls through into BB.
1019 
1020      There is a single PHI node at the join point (BB).
1021 
1022      So, given the condition COND, and the two PHI arguments, match and simplify
1023      can happen on (COND) ? arg0 : arg1. */
1024 
1025   stmt = last_stmt (cond_bb);
1026 
1027   /* We need to know which is the true edge and which is the false
1028      edge so that we know when to invert the condition below.  */
1029   extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
1030   if (e1 == true_edge || e0 == false_edge)
1031     std::swap (arg0, arg1);
1032 
1033   tree type = TREE_TYPE (gimple_phi_result (phi));
1034   result = gimple_simplify_phiopt (early_p, type, stmt,
1035 				   arg0, arg1,
1036 				   &seq);
1037   if (!result)
1038     return false;
1039 
1040   gsi = gsi_last_bb (cond_bb);
1041   /* Insert the sequence generated from gimple_simplify_phiopt.  */
1042   if (seq)
1043     gsi_insert_seq_before (&gsi, seq, GSI_CONTINUE_LINKING);
1044 
1045   /* If there was a statement to move and the result of the statement
1046      is going to be used, move it to right before the original
1047      conditional.  */
1048   if (stmt_to_move
1049       && (gimple_assign_lhs (stmt_to_move) == result
1050 	  || !has_single_use (gimple_assign_lhs (stmt_to_move))))
1051     {
1052       if (dump_file && (dump_flags & TDF_DETAILS))
1053 	{
1054 	  fprintf (dump_file, "statement un-sinked:\n");
1055 	  print_gimple_stmt (dump_file, stmt_to_move, 0,
1056 			   TDF_VOPS|TDF_MEMSYMS);
1057 	}
1058       gimple_stmt_iterator gsi1 = gsi_for_stmt (stmt_to_move);
1059       gsi_move_before (&gsi1, &gsi);
1060       reset_flow_sensitive_info (gimple_assign_lhs (stmt_to_move));
1061     }
1062 
1063   replace_phi_edge_with_variable (cond_bb, e1, phi, result);
1064 
1065   /* Add Statistic here even though replace_phi_edge_with_variable already
1066      does it as we want to be able to count when match-simplify happens vs
1067      the others.  */
1068   statistics_counter_event (cfun, "match-simplify PHI replacement", 1);
1069 
1070   /* Note that we optimized this PHI.  */
1071   return true;
1072 }
1073 
1074 /* Update *ARG which is defined in STMT so that it contains the
1075    computed value if that seems profitable.  Return true if the
1076    statement is made dead by that rewriting.  */
1077 
1078 static bool
jump_function_from_stmt(tree * arg,gimple * stmt)1079 jump_function_from_stmt (tree *arg, gimple *stmt)
1080 {
1081   enum tree_code code = gimple_assign_rhs_code (stmt);
1082   if (code == ADDR_EXPR)
1083     {
1084       /* For arg = &p->i transform it to p, if possible.  */
1085       tree rhs1 = gimple_assign_rhs1 (stmt);
1086       poly_int64 offset;
1087       tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (rhs1, 0),
1088 						&offset);
1089       if (tem
1090 	  && TREE_CODE (tem) == MEM_REF
1091 	  && known_eq (mem_ref_offset (tem) + offset, 0))
1092 	{
1093 	  *arg = TREE_OPERAND (tem, 0);
1094 	  return true;
1095 	}
1096     }
1097   /* TODO: Much like IPA-CP jump-functions we want to handle constant
1098      additions symbolically here, and we'd need to update the comparison
1099      code that compares the arg + cst tuples in our caller.  For now the
1100      code above exactly handles the VEC_BASE pattern from vec.h.  */
1101   return false;
1102 }
1103 
1104 /* RHS is a source argument in a BIT_AND_EXPR which feeds a conditional
1105    of the form SSA_NAME NE 0.
1106 
1107    If RHS is fed by a simple EQ_EXPR comparison of two values, see if
1108    the two input values of the EQ_EXPR match arg0 and arg1.
1109 
1110    If so update *code and return TRUE.  Otherwise return FALSE.  */
1111 
1112 static bool
rhs_is_fed_for_value_replacement(const_tree arg0,const_tree arg1,enum tree_code * code,const_tree rhs)1113 rhs_is_fed_for_value_replacement (const_tree arg0, const_tree arg1,
1114 				  enum tree_code *code, const_tree rhs)
1115 {
1116   /* Obviously if RHS is not an SSA_NAME, we can't look at the defining
1117      statement.  */
1118   if (TREE_CODE (rhs) == SSA_NAME)
1119     {
1120       gimple *def1 = SSA_NAME_DEF_STMT (rhs);
1121 
1122       /* Verify the defining statement has an EQ_EXPR on the RHS.  */
1123       if (is_gimple_assign (def1) && gimple_assign_rhs_code (def1) == EQ_EXPR)
1124 	{
1125 	  /* Finally verify the source operands of the EQ_EXPR are equal
1126 	     to arg0 and arg1.  */
1127 	  tree op0 = gimple_assign_rhs1 (def1);
1128 	  tree op1 = gimple_assign_rhs2 (def1);
1129 	  if ((operand_equal_for_phi_arg_p (arg0, op0)
1130 	       && operand_equal_for_phi_arg_p (arg1, op1))
1131 	      || (operand_equal_for_phi_arg_p (arg0, op1)
1132                && operand_equal_for_phi_arg_p (arg1, op0)))
1133 	    {
1134 	      /* We will perform the optimization.  */
1135 	      *code = gimple_assign_rhs_code (def1);
1136 	      return true;
1137 	    }
1138 	}
1139     }
1140   return false;
1141 }
1142 
1143 /* Return TRUE if arg0/arg1 are equal to the rhs/lhs or lhs/rhs of COND.
1144 
1145    Also return TRUE if arg0/arg1 are equal to the source arguments of a
1146    an EQ comparison feeding a BIT_AND_EXPR which feeds COND.
1147 
1148    Return FALSE otherwise.  */
1149 
1150 static bool
operand_equal_for_value_replacement(const_tree arg0,const_tree arg1,enum tree_code * code,gimple * cond)1151 operand_equal_for_value_replacement (const_tree arg0, const_tree arg1,
1152 				     enum tree_code *code, gimple *cond)
1153 {
1154   gimple *def;
1155   tree lhs = gimple_cond_lhs (cond);
1156   tree rhs = gimple_cond_rhs (cond);
1157 
1158   if ((operand_equal_for_phi_arg_p (arg0, lhs)
1159        && operand_equal_for_phi_arg_p (arg1, rhs))
1160       || (operand_equal_for_phi_arg_p (arg1, lhs)
1161 	  && operand_equal_for_phi_arg_p (arg0, rhs)))
1162     return true;
1163 
1164   /* Now handle more complex case where we have an EQ comparison
1165      which feeds a BIT_AND_EXPR which feeds COND.
1166 
1167      First verify that COND is of the form SSA_NAME NE 0.  */
1168   if (*code != NE_EXPR || !integer_zerop (rhs)
1169       || TREE_CODE (lhs) != SSA_NAME)
1170     return false;
1171 
1172   /* Now ensure that SSA_NAME is set by a BIT_AND_EXPR.  */
1173   def = SSA_NAME_DEF_STMT (lhs);
1174   if (!is_gimple_assign (def) || gimple_assign_rhs_code (def) != BIT_AND_EXPR)
1175     return false;
1176 
1177   /* Now verify arg0/arg1 correspond to the source arguments of an
1178      EQ comparison feeding the BIT_AND_EXPR.  */
1179 
1180   tree tmp = gimple_assign_rhs1 (def);
1181   if (rhs_is_fed_for_value_replacement (arg0, arg1, code, tmp))
1182     return true;
1183 
1184   tmp = gimple_assign_rhs2 (def);
1185   if (rhs_is_fed_for_value_replacement (arg0, arg1, code, tmp))
1186     return true;
1187 
1188   return false;
1189 }
1190 
1191 /* Returns true if ARG is a neutral element for operation CODE
1192    on the RIGHT side.  */
1193 
1194 static bool
neutral_element_p(tree_code code,tree arg,bool right)1195 neutral_element_p (tree_code code, tree arg, bool right)
1196 {
1197   switch (code)
1198     {
1199     case PLUS_EXPR:
1200     case BIT_IOR_EXPR:
1201     case BIT_XOR_EXPR:
1202       return integer_zerop (arg);
1203 
1204     case LROTATE_EXPR:
1205     case RROTATE_EXPR:
1206     case LSHIFT_EXPR:
1207     case RSHIFT_EXPR:
1208     case MINUS_EXPR:
1209     case POINTER_PLUS_EXPR:
1210       return right && integer_zerop (arg);
1211 
1212     case MULT_EXPR:
1213       return integer_onep (arg);
1214 
1215     case TRUNC_DIV_EXPR:
1216     case CEIL_DIV_EXPR:
1217     case FLOOR_DIV_EXPR:
1218     case ROUND_DIV_EXPR:
1219     case EXACT_DIV_EXPR:
1220       return right && integer_onep (arg);
1221 
1222     case BIT_AND_EXPR:
1223       return integer_all_onesp (arg);
1224 
1225     default:
1226       return false;
1227     }
1228 }
1229 
1230 /* Returns true if ARG is an absorbing element for operation CODE.  */
1231 
1232 static bool
absorbing_element_p(tree_code code,tree arg,bool right,tree rval)1233 absorbing_element_p (tree_code code, tree arg, bool right, tree rval)
1234 {
1235   switch (code)
1236     {
1237     case BIT_IOR_EXPR:
1238       return integer_all_onesp (arg);
1239 
1240     case MULT_EXPR:
1241     case BIT_AND_EXPR:
1242       return integer_zerop (arg);
1243 
1244     case LSHIFT_EXPR:
1245     case RSHIFT_EXPR:
1246     case LROTATE_EXPR:
1247     case RROTATE_EXPR:
1248       return !right && integer_zerop (arg);
1249 
1250     case TRUNC_DIV_EXPR:
1251     case CEIL_DIV_EXPR:
1252     case FLOOR_DIV_EXPR:
1253     case ROUND_DIV_EXPR:
1254     case EXACT_DIV_EXPR:
1255     case TRUNC_MOD_EXPR:
1256     case CEIL_MOD_EXPR:
1257     case FLOOR_MOD_EXPR:
1258     case ROUND_MOD_EXPR:
1259       return (!right
1260 	      && integer_zerop (arg)
1261 	      && tree_single_nonzero_warnv_p (rval, NULL));
1262 
1263     default:
1264       return false;
1265     }
1266 }
1267 
1268 /*  The function value_replacement does the main work of doing the value
1269     replacement.  Return non-zero if the replacement is done.  Otherwise return
1270     0.  If we remove the middle basic block, return 2.
1271     BB is the basic block where the replacement is going to be done on.  ARG0
1272     is argument 0 from the PHI.  Likewise for ARG1.  */
1273 
1274 static int
value_replacement(basic_block cond_bb,basic_block middle_bb,edge e0,edge e1,gphi * phi,tree arg0,tree arg1)1275 value_replacement (basic_block cond_bb, basic_block middle_bb,
1276 		   edge e0, edge e1, gphi *phi, tree arg0, tree arg1)
1277 {
1278   gimple_stmt_iterator gsi;
1279   gimple *cond;
1280   edge true_edge, false_edge;
1281   enum tree_code code;
1282   bool empty_or_with_defined_p = true;
1283 
1284   /* If the type says honor signed zeros we cannot do this
1285      optimization.  */
1286   if (HONOR_SIGNED_ZEROS (arg1))
1287     return 0;
1288 
1289   /* If there is a statement in MIDDLE_BB that defines one of the PHI
1290      arguments, then adjust arg0 or arg1.  */
1291   gsi = gsi_start_nondebug_after_labels_bb (middle_bb);
1292   while (!gsi_end_p (gsi))
1293     {
1294       gimple *stmt = gsi_stmt (gsi);
1295       tree lhs;
1296       gsi_next_nondebug (&gsi);
1297       if (!is_gimple_assign (stmt))
1298 	{
1299 	  if (gimple_code (stmt) != GIMPLE_PREDICT
1300 	      && gimple_code (stmt) != GIMPLE_NOP)
1301 	    empty_or_with_defined_p = false;
1302 	  continue;
1303 	}
1304       /* Now try to adjust arg0 or arg1 according to the computation
1305 	 in the statement.  */
1306       lhs = gimple_assign_lhs (stmt);
1307       if (!(lhs == arg0
1308 	     && jump_function_from_stmt (&arg0, stmt))
1309 	    || (lhs == arg1
1310 		&& jump_function_from_stmt (&arg1, stmt)))
1311 	empty_or_with_defined_p = false;
1312     }
1313 
1314   cond = last_stmt (cond_bb);
1315   code = gimple_cond_code (cond);
1316 
1317   /* This transformation is only valid for equality comparisons.  */
1318   if (code != NE_EXPR && code != EQ_EXPR)
1319     return 0;
1320 
1321   /* We need to know which is the true edge and which is the false
1322       edge so that we know if have abs or negative abs.  */
1323   extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
1324 
1325   /* At this point we know we have a COND_EXPR with two successors.
1326      One successor is BB, the other successor is an empty block which
1327      falls through into BB.
1328 
1329      The condition for the COND_EXPR is known to be NE_EXPR or EQ_EXPR.
1330 
1331      There is a single PHI node at the join point (BB) with two arguments.
1332 
1333      We now need to verify that the two arguments in the PHI node match
1334      the two arguments to the equality comparison.  */
1335 
1336   bool equal_p = operand_equal_for_value_replacement (arg0, arg1, &code, cond);
1337   bool maybe_equal_p = false;
1338   if (!equal_p
1339       && empty_or_with_defined_p
1340       && TREE_CODE (gimple_cond_rhs (cond)) == INTEGER_CST
1341       && (operand_equal_for_phi_arg_p (gimple_cond_lhs (cond), arg0)
1342 	  ? TREE_CODE (arg1) == INTEGER_CST
1343 	  : (operand_equal_for_phi_arg_p (gimple_cond_lhs (cond), arg1)
1344 	     && TREE_CODE (arg0) == INTEGER_CST)))
1345     maybe_equal_p = true;
1346   if (equal_p || maybe_equal_p)
1347     {
1348       edge e;
1349       tree arg;
1350 
1351       /* For NE_EXPR, we want to build an assignment result = arg where
1352 	 arg is the PHI argument associated with the true edge.  For
1353 	 EQ_EXPR we want the PHI argument associated with the false edge.  */
1354       e = (code == NE_EXPR ? true_edge : false_edge);
1355 
1356       /* Unfortunately, E may not reach BB (it may instead have gone to
1357 	 OTHER_BLOCK).  If that is the case, then we want the single outgoing
1358 	 edge from OTHER_BLOCK which reaches BB and represents the desired
1359 	 path from COND_BLOCK.  */
1360       if (e->dest == middle_bb)
1361 	e = single_succ_edge (e->dest);
1362 
1363       /* Now we know the incoming edge to BB that has the argument for the
1364 	 RHS of our new assignment statement.  */
1365       if (e0 == e)
1366 	arg = arg0;
1367       else
1368 	arg = arg1;
1369 
1370       /* If the middle basic block was empty or is defining the
1371 	 PHI arguments and this is a single phi where the args are different
1372 	 for the edges e0 and e1 then we can remove the middle basic block. */
1373       if (empty_or_with_defined_p
1374 	  && single_non_singleton_phi_for_edges (phi_nodes (gimple_bb (phi)),
1375 						 e0, e1) == phi)
1376 	{
1377 	  use_operand_p use_p;
1378 	  gimple *use_stmt;
1379 
1380 	  /* Even if arg0/arg1 isn't equal to second operand of cond, we
1381 	     can optimize away the bb if we can prove it doesn't care whether
1382 	     phi result is arg0/arg1 or second operand of cond.  Consider:
1383 	     <bb 2> [local count: 118111600]:
1384 	     if (i_2(D) == 4)
1385 	       goto <bb 4>; [97.00%]
1386 	     else
1387 	       goto <bb 3>; [3.00%]
1388 
1389 	     <bb 3> [local count: 3540129]:
1390 
1391 	     <bb 4> [local count: 118111600]:
1392 	     # i_6 = PHI <i_2(D)(3), 6(2)>
1393 	     _3 = i_6 != 0;
1394 	     Here, carg is 4, oarg is 6, crhs is 0, and because
1395 	     (4 != 0) == (6 != 0), we don't care if i_6 is 4 or 6, both
1396 	     have the same outcome.  So, can can optimize this to:
1397 	     _3 = i_2(D) != 0;
1398 	     If the single imm use of phi result >, >=, < or <=, similarly
1399 	     we can check if both carg and oarg compare the same against
1400 	     crhs using ccode.  */
1401 	  if (maybe_equal_p
1402 	      && TREE_CODE (arg) != INTEGER_CST
1403 	      && single_imm_use (gimple_phi_result (phi), &use_p, &use_stmt))
1404 	    {
1405 	      enum tree_code ccode = ERROR_MARK;
1406 	      tree clhs = NULL_TREE, crhs = NULL_TREE;
1407 	      tree carg = gimple_cond_rhs (cond);
1408 	      tree oarg = e0 == e ? arg1 : arg0;
1409 	      if (is_gimple_assign (use_stmt)
1410 		  && (TREE_CODE_CLASS (gimple_assign_rhs_code (use_stmt))
1411 		      == tcc_comparison))
1412 		{
1413 		  ccode = gimple_assign_rhs_code (use_stmt);
1414 		  clhs = gimple_assign_rhs1 (use_stmt);
1415 		  crhs = gimple_assign_rhs2 (use_stmt);
1416 		}
1417 	      else if (gimple_code (use_stmt) == GIMPLE_COND)
1418 		{
1419 		  ccode = gimple_cond_code (use_stmt);
1420 		  clhs = gimple_cond_lhs (use_stmt);
1421 		  crhs = gimple_cond_rhs (use_stmt);
1422 		}
1423 	      if (ccode != ERROR_MARK
1424 		  && clhs == gimple_phi_result (phi)
1425 		  && TREE_CODE (crhs) == INTEGER_CST)
1426 		switch (ccode)
1427 		  {
1428 		  case EQ_EXPR:
1429 		  case NE_EXPR:
1430 		    if (!tree_int_cst_equal (crhs, carg)
1431 			&& !tree_int_cst_equal (crhs, oarg))
1432 		      equal_p = true;
1433 		    break;
1434 		  case GT_EXPR:
1435 		    if (tree_int_cst_lt (crhs, carg)
1436 			== tree_int_cst_lt (crhs, oarg))
1437 		      equal_p = true;
1438 		    break;
1439 		  case GE_EXPR:
1440 		    if (tree_int_cst_le (crhs, carg)
1441 			== tree_int_cst_le (crhs, oarg))
1442 		      equal_p = true;
1443 		    break;
1444 		  case LT_EXPR:
1445 		    if (tree_int_cst_lt (carg, crhs)
1446 			== tree_int_cst_lt (oarg, crhs))
1447 		      equal_p = true;
1448 		    break;
1449 		  case LE_EXPR:
1450 		    if (tree_int_cst_le (carg, crhs)
1451 			== tree_int_cst_le (oarg, crhs))
1452 		      equal_p = true;
1453 		    break;
1454 		  default:
1455 		    break;
1456 		  }
1457 	      if (equal_p)
1458 		/* After the optimization PHI result can have value
1459 		   which it couldn't have previously.
1460 		   We could instead of resetting it union the range
1461 		   info with oarg.  */
1462 		reset_flow_sensitive_info (gimple_phi_result (phi));
1463 	      if (equal_p && MAY_HAVE_DEBUG_BIND_STMTS)
1464 		{
1465 		  imm_use_iterator imm_iter;
1466 		  tree phires = gimple_phi_result (phi);
1467 		  tree temp = NULL_TREE;
1468 		  bool reset_p = false;
1469 
1470 		  /* Add # DEBUG D#1 => arg != carg ? arg : oarg.  */
1471 		  FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, phires)
1472 		    {
1473 		      if (!is_gimple_debug (use_stmt))
1474 			continue;
1475 		      if (temp == NULL_TREE)
1476 			{
1477 			  if (!single_pred_p (middle_bb)
1478 			      || EDGE_COUNT (gimple_bb (phi)->preds) != 2)
1479 			    {
1480 			      /* But only if middle_bb has a single
1481 				 predecessor and phi bb has two, otherwise
1482 				 we could use a SSA_NAME not usable in that
1483 				 place or wrong-debug.  */
1484 			      reset_p = true;
1485 			      break;
1486 			    }
1487 			  gimple_stmt_iterator gsi
1488 			    = gsi_after_labels (gimple_bb (phi));
1489 			  tree type = TREE_TYPE (phires);
1490 			  temp = build_debug_expr_decl (type);
1491 			  tree t = build2 (NE_EXPR, boolean_type_node,
1492 					   arg, carg);
1493 			  t = build3 (COND_EXPR, type, t, arg, oarg);
1494 			  gimple *g = gimple_build_debug_bind (temp, t, phi);
1495 			  gsi_insert_before (&gsi, g, GSI_SAME_STMT);
1496 			}
1497 		      FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
1498 			replace_exp (use_p, temp);
1499 		      update_stmt (use_stmt);
1500 		    }
1501 		  if (reset_p)
1502 		    reset_debug_uses (phi);
1503 		}
1504 	    }
1505 	  if (equal_p)
1506 	    {
1507 	      replace_phi_edge_with_variable (cond_bb, e1, phi, arg);
1508 	      /* Note that we optimized this PHI.  */
1509 	      return 2;
1510 	    }
1511 	}
1512       else if (equal_p)
1513 	{
1514 	  if (!single_pred_p (middle_bb))
1515 	    return 0;
1516 	  statistics_counter_event (cfun, "Replace PHI with "
1517 				    "variable/value_replacement", 1);
1518 
1519 	  /* Replace the PHI arguments with arg. */
1520 	  SET_PHI_ARG_DEF (phi, e0->dest_idx, arg);
1521 	  SET_PHI_ARG_DEF (phi, e1->dest_idx, arg);
1522 	  if (dump_file && (dump_flags & TDF_DETAILS))
1523 	    {
1524 	      fprintf (dump_file, "PHI ");
1525 	      print_generic_expr (dump_file, gimple_phi_result (phi));
1526 	      fprintf (dump_file, " reduced for COND_EXPR in block %d to ",
1527 		       cond_bb->index);
1528 	      print_generic_expr (dump_file, arg);
1529 	      fprintf (dump_file, ".\n");
1530             }
1531           return 1;
1532 	}
1533     }
1534 
1535   if (!single_pred_p (middle_bb))
1536     return 0;
1537 
1538   /* Now optimize (x != 0) ? x + y : y to just x + y.  */
1539   gsi = gsi_last_nondebug_bb (middle_bb);
1540   if (gsi_end_p (gsi))
1541     return 0;
1542 
1543   gimple *assign = gsi_stmt (gsi);
1544   if (!is_gimple_assign (assign)
1545       || (!INTEGRAL_TYPE_P (TREE_TYPE (arg0))
1546 	  && !POINTER_TYPE_P (TREE_TYPE (arg0))))
1547     return 0;
1548 
1549   if (gimple_assign_rhs_class (assign) != GIMPLE_BINARY_RHS)
1550     {
1551       /* If last stmt of the middle_bb is a conversion, handle it like
1552 	 a preparation statement through constant evaluation with
1553 	 checking for UB.  */
1554       enum tree_code sc = gimple_assign_rhs_code (assign);
1555       if (CONVERT_EXPR_CODE_P (sc))
1556 	assign = NULL;
1557       else
1558 	return 0;
1559     }
1560 
1561   /* Punt if there are (degenerate) PHIs in middle_bb, there should not be.  */
1562   if (!gimple_seq_empty_p (phi_nodes (middle_bb)))
1563     return 0;
1564 
1565   /* Allow up to 2 cheap preparation statements that prepare argument
1566      for assign, e.g.:
1567       if (y_4 != 0)
1568 	goto <bb 3>;
1569       else
1570 	goto <bb 4>;
1571      <bb 3>:
1572       _1 = (int) y_4;
1573       iftmp.0_6 = x_5(D) r<< _1;
1574      <bb 4>:
1575       # iftmp.0_2 = PHI <iftmp.0_6(3), x_5(D)(2)>
1576      or:
1577       if (y_3(D) == 0)
1578 	goto <bb 4>;
1579       else
1580 	goto <bb 3>;
1581      <bb 3>:
1582       y_4 = y_3(D) & 31;
1583       _1 = (int) y_4;
1584       _6 = x_5(D) r<< _1;
1585      <bb 4>:
1586       # _2 = PHI <x_5(D)(2), _6(3)>  */
1587   gimple *prep_stmt[2] = { NULL, NULL };
1588   int prep_cnt;
1589   for (prep_cnt = 0; ; prep_cnt++)
1590     {
1591       if (prep_cnt || assign)
1592 	gsi_prev_nondebug (&gsi);
1593       if (gsi_end_p (gsi))
1594 	break;
1595 
1596       gimple *g = gsi_stmt (gsi);
1597       if (gimple_code (g) == GIMPLE_LABEL)
1598 	break;
1599 
1600       if (prep_cnt == 2 || !is_gimple_assign (g))
1601 	return 0;
1602 
1603       tree lhs = gimple_assign_lhs (g);
1604       tree rhs1 = gimple_assign_rhs1 (g);
1605       use_operand_p use_p;
1606       gimple *use_stmt;
1607       if (TREE_CODE (lhs) != SSA_NAME
1608 	  || TREE_CODE (rhs1) != SSA_NAME
1609 	  || !INTEGRAL_TYPE_P (TREE_TYPE (lhs))
1610 	  || !INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1611 	  || !single_imm_use (lhs, &use_p, &use_stmt)
1612 	  || ((prep_cnt || assign)
1613 	      && use_stmt != (prep_cnt ? prep_stmt[prep_cnt - 1] : assign)))
1614 	return 0;
1615       switch (gimple_assign_rhs_code (g))
1616 	{
1617 	CASE_CONVERT:
1618 	  break;
1619 	case PLUS_EXPR:
1620 	case BIT_AND_EXPR:
1621 	case BIT_IOR_EXPR:
1622 	case BIT_XOR_EXPR:
1623 	  if (TREE_CODE (gimple_assign_rhs2 (g)) != INTEGER_CST)
1624 	    return 0;
1625 	  break;
1626 	default:
1627 	  return 0;
1628 	}
1629       prep_stmt[prep_cnt] = g;
1630     }
1631 
1632   /* Only transform if it removes the condition.  */
1633   if (!single_non_singleton_phi_for_edges (phi_nodes (gimple_bb (phi)), e0, e1))
1634     return 0;
1635 
1636   /* Size-wise, this is always profitable.  */
1637   if (optimize_bb_for_speed_p (cond_bb)
1638       /* The special case is useless if it has a low probability.  */
1639       && profile_status_for_fn (cfun) != PROFILE_ABSENT
1640       && EDGE_PRED (middle_bb, 0)->probability < profile_probability::even ()
1641       /* If assign is cheap, there is no point avoiding it.  */
1642       && estimate_num_insns_seq (bb_seq (middle_bb), &eni_time_weights)
1643 	 >= 3 * estimate_num_insns (cond, &eni_time_weights))
1644     return 0;
1645 
1646   tree cond_lhs = gimple_cond_lhs (cond);
1647   tree cond_rhs = gimple_cond_rhs (cond);
1648 
1649   /* Propagate the cond_rhs constant through preparation stmts,
1650      make sure UB isn't invoked while doing that.  */
1651   for (int i = prep_cnt - 1; i >= 0; --i)
1652     {
1653       gimple *g = prep_stmt[i];
1654       tree grhs1 = gimple_assign_rhs1 (g);
1655       if (!operand_equal_for_phi_arg_p (cond_lhs, grhs1))
1656 	return 0;
1657       cond_lhs = gimple_assign_lhs (g);
1658       cond_rhs = fold_convert (TREE_TYPE (grhs1), cond_rhs);
1659       if (TREE_CODE (cond_rhs) != INTEGER_CST
1660 	  || TREE_OVERFLOW (cond_rhs))
1661 	return 0;
1662       if (gimple_assign_rhs_class (g) == GIMPLE_BINARY_RHS)
1663 	{
1664 	  cond_rhs = int_const_binop (gimple_assign_rhs_code (g), cond_rhs,
1665 				      gimple_assign_rhs2 (g));
1666 	  if (TREE_OVERFLOW (cond_rhs))
1667 	    return 0;
1668 	}
1669       cond_rhs = fold_convert (TREE_TYPE (cond_lhs), cond_rhs);
1670       if (TREE_CODE (cond_rhs) != INTEGER_CST
1671 	  || TREE_OVERFLOW (cond_rhs))
1672 	return 0;
1673     }
1674 
1675   tree lhs, rhs1, rhs2;
1676   enum tree_code code_def;
1677   if (assign)
1678     {
1679       lhs = gimple_assign_lhs (assign);
1680       rhs1 = gimple_assign_rhs1 (assign);
1681       rhs2 = gimple_assign_rhs2 (assign);
1682       code_def = gimple_assign_rhs_code (assign);
1683     }
1684   else
1685     {
1686       gcc_assert (prep_cnt > 0);
1687       lhs = cond_lhs;
1688       rhs1 = NULL_TREE;
1689       rhs2 = NULL_TREE;
1690       code_def = ERROR_MARK;
1691     }
1692 
1693   if (((code == NE_EXPR && e1 == false_edge)
1694 	|| (code == EQ_EXPR && e1 == true_edge))
1695       && arg0 == lhs
1696       && ((assign == NULL
1697 	   && operand_equal_for_phi_arg_p (arg1, cond_rhs))
1698 	  || (assign
1699 	      && arg1 == rhs1
1700 	      && operand_equal_for_phi_arg_p (rhs2, cond_lhs)
1701 	      && neutral_element_p (code_def, cond_rhs, true))
1702 	  || (assign
1703 	      && arg1 == rhs2
1704 	      && operand_equal_for_phi_arg_p (rhs1, cond_lhs)
1705 	      && neutral_element_p (code_def, cond_rhs, false))
1706 	  || (assign
1707 	      && operand_equal_for_phi_arg_p (arg1, cond_rhs)
1708 	      && ((operand_equal_for_phi_arg_p (rhs2, cond_lhs)
1709 		   && absorbing_element_p (code_def, cond_rhs, true, rhs2))
1710 		  || (operand_equal_for_phi_arg_p (rhs1, cond_lhs)
1711 		      && absorbing_element_p (code_def,
1712 					      cond_rhs, false, rhs2))))))
1713     {
1714       gsi = gsi_for_stmt (cond);
1715       /* Moving ASSIGN might change VR of lhs, e.g. when moving u_6
1716 	 def-stmt in:
1717 	   if (n_5 != 0)
1718 	     goto <bb 3>;
1719 	   else
1720 	     goto <bb 4>;
1721 
1722 	   <bb 3>:
1723 	   # RANGE [0, 4294967294]
1724 	   u_6 = n_5 + 4294967295;
1725 
1726 	   <bb 4>:
1727 	   # u_3 = PHI <u_6(3), 4294967295(2)>  */
1728       reset_flow_sensitive_info (lhs);
1729       gimple_stmt_iterator gsi_from;
1730       for (int i = prep_cnt - 1; i >= 0; --i)
1731 	{
1732 	  tree plhs = gimple_assign_lhs (prep_stmt[i]);
1733 	  reset_flow_sensitive_info (plhs);
1734 	  gsi_from = gsi_for_stmt (prep_stmt[i]);
1735 	  gsi_move_before (&gsi_from, &gsi);
1736 	}
1737       if (assign)
1738 	{
1739 	  gsi_from = gsi_for_stmt (assign);
1740 	  gsi_move_before (&gsi_from, &gsi);
1741 	}
1742       replace_phi_edge_with_variable (cond_bb, e1, phi, lhs);
1743       return 2;
1744     }
1745 
1746   return 0;
1747 }
1748 
1749 /*  The function minmax_replacement does the main work of doing the minmax
1750     replacement.  Return true if the replacement is done.  Otherwise return
1751     false.
1752     BB is the basic block where the replacement is going to be done on.  ARG0
1753     is argument 0 from the PHI.  Likewise for ARG1.  */
1754 
1755 static bool
minmax_replacement(basic_block cond_bb,basic_block middle_bb,edge e0,edge e1,gphi * phi,tree arg0,tree arg1)1756 minmax_replacement (basic_block cond_bb, basic_block middle_bb,
1757 		    edge e0, edge e1, gphi *phi, tree arg0, tree arg1)
1758 {
1759   tree result;
1760   edge true_edge, false_edge;
1761   enum tree_code minmax, ass_code;
1762   tree smaller, larger, arg_true, arg_false;
1763   gimple_stmt_iterator gsi, gsi_from;
1764 
1765   tree type = TREE_TYPE (PHI_RESULT (phi));
1766 
1767   /* The optimization may be unsafe due to NaNs.  */
1768   if (HONOR_NANS (type) || HONOR_SIGNED_ZEROS (type))
1769     return false;
1770 
1771   gcond *cond = as_a <gcond *> (last_stmt (cond_bb));
1772   enum tree_code cmp = gimple_cond_code (cond);
1773   tree rhs = gimple_cond_rhs (cond);
1774 
1775   /* Turn EQ/NE of extreme values to order comparisons.  */
1776   if ((cmp == NE_EXPR || cmp == EQ_EXPR)
1777       && TREE_CODE (rhs) == INTEGER_CST
1778       && INTEGRAL_TYPE_P (TREE_TYPE (rhs)))
1779     {
1780       if (wi::eq_p (wi::to_wide (rhs), wi::min_value (TREE_TYPE (rhs))))
1781 	{
1782 	  cmp = (cmp == EQ_EXPR) ? LT_EXPR : GE_EXPR;
1783 	  rhs = wide_int_to_tree (TREE_TYPE (rhs),
1784 				  wi::min_value (TREE_TYPE (rhs)) + 1);
1785 	}
1786       else if (wi::eq_p (wi::to_wide (rhs), wi::max_value (TREE_TYPE (rhs))))
1787 	{
1788 	  cmp = (cmp == EQ_EXPR) ? GT_EXPR : LE_EXPR;
1789 	  rhs = wide_int_to_tree (TREE_TYPE (rhs),
1790 				  wi::max_value (TREE_TYPE (rhs)) - 1);
1791 	}
1792     }
1793 
1794   /* This transformation is only valid for order comparisons.  Record which
1795      operand is smaller/larger if the result of the comparison is true.  */
1796   tree alt_smaller = NULL_TREE;
1797   tree alt_larger = NULL_TREE;
1798   if (cmp == LT_EXPR || cmp == LE_EXPR)
1799     {
1800       smaller = gimple_cond_lhs (cond);
1801       larger = rhs;
1802       /* If we have smaller < CST it is equivalent to smaller <= CST-1.
1803 	 Likewise smaller <= CST is equivalent to smaller < CST+1.  */
1804       if (TREE_CODE (larger) == INTEGER_CST
1805 	  && INTEGRAL_TYPE_P (TREE_TYPE (larger)))
1806 	{
1807 	  if (cmp == LT_EXPR)
1808 	    {
1809 	      wi::overflow_type overflow;
1810 	      wide_int alt = wi::sub (wi::to_wide (larger), 1,
1811 				      TYPE_SIGN (TREE_TYPE (larger)),
1812 				      &overflow);
1813 	      if (! overflow)
1814 		alt_larger = wide_int_to_tree (TREE_TYPE (larger), alt);
1815 	    }
1816 	  else
1817 	    {
1818 	      wi::overflow_type overflow;
1819 	      wide_int alt = wi::add (wi::to_wide (larger), 1,
1820 				      TYPE_SIGN (TREE_TYPE (larger)),
1821 				      &overflow);
1822 	      if (! overflow)
1823 		alt_larger = wide_int_to_tree (TREE_TYPE (larger), alt);
1824 	    }
1825 	}
1826     }
1827   else if (cmp == GT_EXPR || cmp == GE_EXPR)
1828     {
1829       smaller = rhs;
1830       larger = gimple_cond_lhs (cond);
1831       /* If we have larger > CST it is equivalent to larger >= CST+1.
1832 	 Likewise larger >= CST is equivalent to larger > CST-1.  */
1833       if (TREE_CODE (smaller) == INTEGER_CST
1834 	  && INTEGRAL_TYPE_P (TREE_TYPE (smaller)))
1835 	{
1836 	  wi::overflow_type overflow;
1837 	  if (cmp == GT_EXPR)
1838 	    {
1839 	      wide_int alt = wi::add (wi::to_wide (smaller), 1,
1840 				      TYPE_SIGN (TREE_TYPE (smaller)),
1841 				      &overflow);
1842 	      if (! overflow)
1843 		alt_smaller = wide_int_to_tree (TREE_TYPE (smaller), alt);
1844 	    }
1845 	  else
1846 	    {
1847 	      wide_int alt = wi::sub (wi::to_wide (smaller), 1,
1848 				      TYPE_SIGN (TREE_TYPE (smaller)),
1849 				      &overflow);
1850 	      if (! overflow)
1851 		alt_smaller = wide_int_to_tree (TREE_TYPE (smaller), alt);
1852 	    }
1853 	}
1854     }
1855   else
1856     return false;
1857 
1858   /* Handle the special case of (signed_type)x < 0 being equivalent
1859      to x > MAX_VAL(signed_type) and (signed_type)x >= 0 equivalent
1860      to x <= MAX_VAL(signed_type).  */
1861   if ((cmp == GE_EXPR || cmp == LT_EXPR)
1862       && INTEGRAL_TYPE_P (type)
1863       && TYPE_UNSIGNED (type)
1864       && integer_zerop (rhs))
1865     {
1866       tree op = gimple_cond_lhs (cond);
1867       if (TREE_CODE (op) == SSA_NAME
1868 	  && INTEGRAL_TYPE_P (TREE_TYPE (op))
1869 	  && !TYPE_UNSIGNED (TREE_TYPE (op)))
1870 	{
1871 	  gimple *def_stmt = SSA_NAME_DEF_STMT (op);
1872 	  if (gimple_assign_cast_p (def_stmt))
1873 	    {
1874 	      tree op1 = gimple_assign_rhs1 (def_stmt);
1875 	      if (INTEGRAL_TYPE_P (TREE_TYPE (op1))
1876 		  && TYPE_UNSIGNED (TREE_TYPE (op1))
1877 		  && (TYPE_PRECISION (TREE_TYPE (op))
1878 		      == TYPE_PRECISION (TREE_TYPE (op1)))
1879 		  && useless_type_conversion_p (type, TREE_TYPE (op1)))
1880 		{
1881 		  wide_int w1 = wi::max_value (TREE_TYPE (op));
1882 		  wide_int w2 = wi::add (w1, 1);
1883 		  if (cmp == LT_EXPR)
1884 		    {
1885 		      larger = op1;
1886 		      smaller = wide_int_to_tree (TREE_TYPE (op1), w1);
1887 		      alt_smaller = wide_int_to_tree (TREE_TYPE (op1), w2);
1888 		      alt_larger = NULL_TREE;
1889 		    }
1890 		  else
1891 		    {
1892 		      smaller = op1;
1893 		      larger = wide_int_to_tree (TREE_TYPE (op1), w1);
1894 		      alt_larger = wide_int_to_tree (TREE_TYPE (op1), w2);
1895 		      alt_smaller = NULL_TREE;
1896 		    }
1897 		}
1898 	    }
1899 	}
1900     }
1901 
1902   /* We need to know which is the true edge and which is the false
1903       edge so that we know if have abs or negative abs.  */
1904   extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
1905 
1906   /* Forward the edges over the middle basic block.  */
1907   if (true_edge->dest == middle_bb)
1908     true_edge = EDGE_SUCC (true_edge->dest, 0);
1909   if (false_edge->dest == middle_bb)
1910     false_edge = EDGE_SUCC (false_edge->dest, 0);
1911 
1912   if (true_edge == e0)
1913     {
1914       gcc_assert (false_edge == e1);
1915       arg_true = arg0;
1916       arg_false = arg1;
1917     }
1918   else
1919     {
1920       gcc_assert (false_edge == e0);
1921       gcc_assert (true_edge == e1);
1922       arg_true = arg1;
1923       arg_false = arg0;
1924     }
1925 
1926   if (empty_block_p (middle_bb))
1927     {
1928       if ((operand_equal_for_phi_arg_p (arg_true, smaller)
1929 	   || (alt_smaller
1930 	       && operand_equal_for_phi_arg_p (arg_true, alt_smaller)))
1931 	  && (operand_equal_for_phi_arg_p (arg_false, larger)
1932 	      || (alt_larger
1933 		  && operand_equal_for_phi_arg_p (arg_true, alt_larger))))
1934 	{
1935 	  /* Case
1936 
1937 	     if (smaller < larger)
1938 	     rslt = smaller;
1939 	     else
1940 	     rslt = larger;  */
1941 	  minmax = MIN_EXPR;
1942 	}
1943       else if ((operand_equal_for_phi_arg_p (arg_false, smaller)
1944 		|| (alt_smaller
1945 		    && operand_equal_for_phi_arg_p (arg_false, alt_smaller)))
1946 	       && (operand_equal_for_phi_arg_p (arg_true, larger)
1947 		   || (alt_larger
1948 		       && operand_equal_for_phi_arg_p (arg_true, alt_larger))))
1949 	minmax = MAX_EXPR;
1950       else
1951 	return false;
1952     }
1953   else
1954     {
1955       /* Recognize the following case, assuming d <= u:
1956 
1957 	 if (a <= u)
1958 	   b = MAX (a, d);
1959 	 x = PHI <b, u>
1960 
1961 	 This is equivalent to
1962 
1963 	 b = MAX (a, d);
1964 	 x = MIN (b, u);  */
1965 
1966       gimple *assign = last_and_only_stmt (middle_bb);
1967       tree lhs, op0, op1, bound;
1968 
1969       if (!single_pred_p (middle_bb))
1970 	return false;
1971 
1972       if (!assign
1973 	  || gimple_code (assign) != GIMPLE_ASSIGN)
1974 	return false;
1975 
1976       /* There cannot be any phi nodes in the middle bb. */
1977       if (!gimple_seq_empty_p (phi_nodes (middle_bb)))
1978 	return false;
1979 
1980       lhs = gimple_assign_lhs (assign);
1981       ass_code = gimple_assign_rhs_code (assign);
1982       if (ass_code != MAX_EXPR && ass_code != MIN_EXPR)
1983 	return false;
1984       op0 = gimple_assign_rhs1 (assign);
1985       op1 = gimple_assign_rhs2 (assign);
1986 
1987       if (true_edge->src == middle_bb)
1988 	{
1989 	  /* We got here if the condition is true, i.e., SMALLER < LARGER.  */
1990 	  if (!operand_equal_for_phi_arg_p (lhs, arg_true))
1991 	    return false;
1992 
1993 	  if (operand_equal_for_phi_arg_p (arg_false, larger)
1994 	      || (alt_larger
1995 		  && operand_equal_for_phi_arg_p (arg_false, alt_larger)))
1996 	    {
1997 	      /* Case
1998 
1999 		 if (smaller < larger)
2000 		   {
2001 		     r' = MAX_EXPR (smaller, bound)
2002 		   }
2003 		 r = PHI <r', larger>  --> to be turned to MIN_EXPR.  */
2004 	      if (ass_code != MAX_EXPR)
2005 		return false;
2006 
2007 	      minmax = MIN_EXPR;
2008 	      if (operand_equal_for_phi_arg_p (op0, smaller)
2009 		  || (alt_smaller
2010 		      && operand_equal_for_phi_arg_p (op0, alt_smaller)))
2011 		bound = op1;
2012 	      else if (operand_equal_for_phi_arg_p (op1, smaller)
2013 		       || (alt_smaller
2014 			   && operand_equal_for_phi_arg_p (op1, alt_smaller)))
2015 		bound = op0;
2016 	      else
2017 		return false;
2018 
2019 	      /* We need BOUND <= LARGER.  */
2020 	      if (!integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node,
2021 						  bound, arg_false)))
2022 		return false;
2023 	    }
2024 	  else if (operand_equal_for_phi_arg_p (arg_false, smaller)
2025 		   || (alt_smaller
2026 		       && operand_equal_for_phi_arg_p (arg_false, alt_smaller)))
2027 	    {
2028 	      /* Case
2029 
2030 		 if (smaller < larger)
2031 		   {
2032 		     r' = MIN_EXPR (larger, bound)
2033 		   }
2034 		 r = PHI <r', smaller>  --> to be turned to MAX_EXPR.  */
2035 	      if (ass_code != MIN_EXPR)
2036 		return false;
2037 
2038 	      minmax = MAX_EXPR;
2039 	      if (operand_equal_for_phi_arg_p (op0, larger)
2040 		  || (alt_larger
2041 		      && operand_equal_for_phi_arg_p (op0, alt_larger)))
2042 		bound = op1;
2043 	      else if (operand_equal_for_phi_arg_p (op1, larger)
2044 		       || (alt_larger
2045 			   && operand_equal_for_phi_arg_p (op1, alt_larger)))
2046 		bound = op0;
2047 	      else
2048 		return false;
2049 
2050 	      /* We need BOUND >= SMALLER.  */
2051 	      if (!integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node,
2052 						  bound, arg_false)))
2053 		return false;
2054 	    }
2055 	  else
2056 	    return false;
2057 	}
2058       else
2059 	{
2060 	  /* We got here if the condition is false, i.e., SMALLER > LARGER.  */
2061 	  if (!operand_equal_for_phi_arg_p (lhs, arg_false))
2062 	    return false;
2063 
2064 	  if (operand_equal_for_phi_arg_p (arg_true, larger)
2065 	      || (alt_larger
2066 		  && operand_equal_for_phi_arg_p (arg_true, alt_larger)))
2067 	    {
2068 	      /* Case
2069 
2070 		 if (smaller > larger)
2071 		   {
2072 		     r' = MIN_EXPR (smaller, bound)
2073 		   }
2074 		 r = PHI <r', larger>  --> to be turned to MAX_EXPR.  */
2075 	      if (ass_code != MIN_EXPR)
2076 		return false;
2077 
2078 	      minmax = MAX_EXPR;
2079 	      if (operand_equal_for_phi_arg_p (op0, smaller)
2080 		  || (alt_smaller
2081 		      && operand_equal_for_phi_arg_p (op0, alt_smaller)))
2082 		bound = op1;
2083 	      else if (operand_equal_for_phi_arg_p (op1, smaller)
2084 		       || (alt_smaller
2085 			   && operand_equal_for_phi_arg_p (op1, alt_smaller)))
2086 		bound = op0;
2087 	      else
2088 		return false;
2089 
2090 	      /* We need BOUND >= LARGER.  */
2091 	      if (!integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node,
2092 						  bound, arg_true)))
2093 		return false;
2094 	    }
2095 	  else if (operand_equal_for_phi_arg_p (arg_true, smaller)
2096 		   || (alt_smaller
2097 		       && operand_equal_for_phi_arg_p (arg_true, alt_smaller)))
2098 	    {
2099 	      /* Case
2100 
2101 		 if (smaller > larger)
2102 		   {
2103 		     r' = MAX_EXPR (larger, bound)
2104 		   }
2105 		 r = PHI <r', smaller>  --> to be turned to MIN_EXPR.  */
2106 	      if (ass_code != MAX_EXPR)
2107 		return false;
2108 
2109 	      minmax = MIN_EXPR;
2110 	      if (operand_equal_for_phi_arg_p (op0, larger))
2111 		bound = op1;
2112 	      else if (operand_equal_for_phi_arg_p (op1, larger))
2113 		bound = op0;
2114 	      else
2115 		return false;
2116 
2117 	      /* We need BOUND <= SMALLER.  */
2118 	      if (!integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node,
2119 						  bound, arg_true)))
2120 		return false;
2121 	    }
2122 	  else
2123 	    return false;
2124 	}
2125 
2126       /* Move the statement from the middle block.  */
2127       gsi = gsi_last_bb (cond_bb);
2128       gsi_from = gsi_last_nondebug_bb (middle_bb);
2129       reset_flow_sensitive_info (SINGLE_SSA_TREE_OPERAND (gsi_stmt (gsi_from),
2130 							  SSA_OP_DEF));
2131       gsi_move_before (&gsi_from, &gsi);
2132     }
2133 
2134   /* Emit the statement to compute min/max.  */
2135   gimple_seq stmts = NULL;
2136   tree phi_result = PHI_RESULT (phi);
2137   result = gimple_build (&stmts, minmax, TREE_TYPE (phi_result), arg0, arg1);
2138 
2139   gsi = gsi_last_bb (cond_bb);
2140   gsi_insert_seq_before (&gsi, stmts, GSI_NEW_STMT);
2141 
2142   replace_phi_edge_with_variable (cond_bb, e1, phi, result);
2143 
2144   return true;
2145 }
2146 
2147 /* Attempt to optimize (x <=> y) cmp 0 and similar comparisons.
2148    For strong ordering <=> try to match something like:
2149     <bb 2> :  // cond3_bb (== cond2_bb)
2150     if (x_4(D) != y_5(D))
2151       goto <bb 3>; [INV]
2152     else
2153       goto <bb 6>; [INV]
2154 
2155     <bb 3> :  // cond_bb
2156     if (x_4(D) < y_5(D))
2157       goto <bb 6>; [INV]
2158     else
2159       goto <bb 4>; [INV]
2160 
2161     <bb 4> :  // middle_bb
2162 
2163     <bb 6> :  // phi_bb
2164     # iftmp.0_2 = PHI <1(4), 0(2), -1(3)>
2165     _1 = iftmp.0_2 == 0;
2166 
2167    and for partial ordering <=> something like:
2168 
2169     <bb 2> :  // cond3_bb
2170     if (a_3(D) == b_5(D))
2171       goto <bb 6>; [50.00%]
2172     else
2173       goto <bb 3>; [50.00%]
2174 
2175     <bb 3> [local count: 536870913]:  // cond2_bb
2176     if (a_3(D) < b_5(D))
2177       goto <bb 6>; [50.00%]
2178     else
2179       goto <bb 4>; [50.00%]
2180 
2181     <bb 4> [local count: 268435456]:  // cond_bb
2182     if (a_3(D) > b_5(D))
2183       goto <bb 6>; [50.00%]
2184     else
2185       goto <bb 5>; [50.00%]
2186 
2187     <bb 5> [local count: 134217728]:  // middle_bb
2188 
2189     <bb 6> [local count: 1073741824]:  // phi_bb
2190     # SR.27_4 = PHI <0(2), -1(3), 1(4), 2(5)>
2191     _2 = SR.27_4 > 0;  */
2192 
2193 static bool
spaceship_replacement(basic_block cond_bb,basic_block middle_bb,edge e0,edge e1,gphi * phi,tree arg0,tree arg1)2194 spaceship_replacement (basic_block cond_bb, basic_block middle_bb,
2195 		       edge e0, edge e1, gphi *phi,
2196 		       tree arg0, tree arg1)
2197 {
2198   tree phires = PHI_RESULT (phi);
2199   if (!INTEGRAL_TYPE_P (TREE_TYPE (phires))
2200       || TYPE_UNSIGNED (TREE_TYPE (phires))
2201       || !tree_fits_shwi_p (arg0)
2202       || !tree_fits_shwi_p (arg1)
2203       || !IN_RANGE (tree_to_shwi (arg0), -1, 2)
2204       || !IN_RANGE (tree_to_shwi (arg1), -1, 2))
2205     return false;
2206 
2207   basic_block phi_bb = gimple_bb (phi);
2208   gcc_assert (phi_bb == e0->dest && phi_bb == e1->dest);
2209   if (!IN_RANGE (EDGE_COUNT (phi_bb->preds), 3, 4))
2210     return false;
2211 
2212   use_operand_p use_p;
2213   gimple *use_stmt;
2214   if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (phires))
2215     return false;
2216   if (!single_imm_use (phires, &use_p, &use_stmt))
2217     return false;
2218   enum tree_code cmp;
2219   tree lhs, rhs;
2220   gimple *orig_use_stmt = use_stmt;
2221   tree orig_use_lhs = NULL_TREE;
2222   int prec = TYPE_PRECISION (TREE_TYPE (phires));
2223   bool is_cast = false;
2224 
2225   /* Deal with the case when match.pd has rewritten the (res & ~1) == 0
2226      into res <= 1 and has left a type-cast for signed types.  */
2227   if (gimple_assign_cast_p (use_stmt))
2228     {
2229       orig_use_lhs = gimple_assign_lhs (use_stmt);
2230       /* match.pd would have only done this for a signed type,
2231 	 so the conversion must be to an unsigned one.  */
2232       tree ty1 = TREE_TYPE (gimple_assign_rhs1 (use_stmt));
2233       tree ty2 = TREE_TYPE (orig_use_lhs);
2234 
2235       if (!TYPE_UNSIGNED (ty2) || !INTEGRAL_TYPE_P (ty2))
2236 	return false;
2237       if (TYPE_PRECISION (ty1) != TYPE_PRECISION (ty2))
2238 	return false;
2239       if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig_use_lhs))
2240 	return false;
2241       if (EDGE_COUNT (phi_bb->preds) != 4)
2242 	return false;
2243       if (!single_imm_use (orig_use_lhs, &use_p, &use_stmt))
2244 	return false;
2245 
2246       is_cast = true;
2247     }
2248   else if (is_gimple_assign (use_stmt)
2249 	   && gimple_assign_rhs_code (use_stmt) == BIT_AND_EXPR
2250 	   && TREE_CODE (gimple_assign_rhs2 (use_stmt)) == INTEGER_CST
2251 	   && (wi::to_wide (gimple_assign_rhs2 (use_stmt))
2252 	       == wi::shifted_mask (1, prec - 1, false, prec)))
2253     {
2254       /* For partial_ordering result operator>= with unspec as second
2255 	 argument is (res & 1) == res, folded by match.pd into
2256 	 (res & ~1) == 0.  */
2257       orig_use_lhs = gimple_assign_lhs (use_stmt);
2258       if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig_use_lhs))
2259 	return false;
2260       if (EDGE_COUNT (phi_bb->preds) != 4)
2261 	return false;
2262       if (!single_imm_use (orig_use_lhs, &use_p, &use_stmt))
2263 	return false;
2264     }
2265   if (gimple_code (use_stmt) == GIMPLE_COND)
2266     {
2267       cmp = gimple_cond_code (use_stmt);
2268       lhs = gimple_cond_lhs (use_stmt);
2269       rhs = gimple_cond_rhs (use_stmt);
2270     }
2271   else if (is_gimple_assign (use_stmt))
2272     {
2273       if (gimple_assign_rhs_class (use_stmt) == GIMPLE_BINARY_RHS)
2274 	{
2275 	  cmp = gimple_assign_rhs_code (use_stmt);
2276 	  lhs = gimple_assign_rhs1 (use_stmt);
2277 	  rhs = gimple_assign_rhs2 (use_stmt);
2278 	}
2279       else if (gimple_assign_rhs_code (use_stmt) == COND_EXPR)
2280 	{
2281 	  tree cond = gimple_assign_rhs1 (use_stmt);
2282 	  if (!COMPARISON_CLASS_P (cond))
2283 	    return false;
2284 	  cmp = TREE_CODE (cond);
2285 	  lhs = TREE_OPERAND (cond, 0);
2286 	  rhs = TREE_OPERAND (cond, 1);
2287 	}
2288       else
2289 	return false;
2290     }
2291   else
2292     return false;
2293   switch (cmp)
2294     {
2295     case EQ_EXPR:
2296     case NE_EXPR:
2297     case LT_EXPR:
2298     case GT_EXPR:
2299     case LE_EXPR:
2300     case GE_EXPR:
2301       break;
2302     default:
2303       return false;
2304     }
2305   if (lhs != (orig_use_lhs ? orig_use_lhs : phires)
2306       || !tree_fits_shwi_p (rhs)
2307       || !IN_RANGE (tree_to_shwi (rhs), -1, 1))
2308     return false;
2309 
2310   if (is_cast)
2311     {
2312       if (TREE_CODE (rhs) != INTEGER_CST)
2313 	return false;
2314       /* As for -ffast-math we assume the 2 return to be
2315 	 impossible, canonicalize (unsigned) res <= 1U or
2316 	 (unsigned) res < 2U into res >= 0 and (unsigned) res > 1U
2317 	 or (unsigned) res >= 2U as res < 0.  */
2318       switch (cmp)
2319 	{
2320 	case LE_EXPR:
2321 	  if (!integer_onep (rhs))
2322 	    return false;
2323 	  cmp = GE_EXPR;
2324 	  break;
2325 	case LT_EXPR:
2326 	  if (wi::ne_p (wi::to_widest (rhs), 2))
2327 	    return false;
2328 	  cmp = GE_EXPR;
2329 	  break;
2330 	case GT_EXPR:
2331 	  if (!integer_onep (rhs))
2332 	    return false;
2333 	  cmp = LT_EXPR;
2334 	  break;
2335 	case GE_EXPR:
2336 	  if (wi::ne_p (wi::to_widest (rhs), 2))
2337 	    return false;
2338 	  cmp = LT_EXPR;
2339 	  break;
2340 	default:
2341 	  return false;
2342 	}
2343       rhs = build_zero_cst (TREE_TYPE (phires));
2344     }
2345   else if (orig_use_lhs)
2346     {
2347       if ((cmp != EQ_EXPR && cmp != NE_EXPR) || !integer_zerop (rhs))
2348 	return false;
2349       /* As for -ffast-math we assume the 2 return to be
2350 	 impossible, canonicalize (res & ~1) == 0 into
2351 	 res >= 0 and (res & ~1) != 0 as res < 0.  */
2352       cmp = cmp == EQ_EXPR ? GE_EXPR : LT_EXPR;
2353     }
2354 
2355   if (!empty_block_p (middle_bb))
2356     return false;
2357 
2358   gcond *cond1 = as_a <gcond *> (last_stmt (cond_bb));
2359   enum tree_code cmp1 = gimple_cond_code (cond1);
2360   switch (cmp1)
2361     {
2362     case LT_EXPR:
2363     case LE_EXPR:
2364     case GT_EXPR:
2365     case GE_EXPR:
2366       break;
2367     default:
2368       return false;
2369     }
2370   tree lhs1 = gimple_cond_lhs (cond1);
2371   tree rhs1 = gimple_cond_rhs (cond1);
2372   /* The optimization may be unsafe due to NaNs.  */
2373   if (HONOR_NANS (TREE_TYPE (lhs1)))
2374     return false;
2375   if (TREE_CODE (lhs1) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs1))
2376     return false;
2377   if (TREE_CODE (rhs1) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1))
2378     return false;
2379 
2380   if (!single_pred_p (cond_bb) || !cond_only_block_p (cond_bb))
2381     return false;
2382 
2383   basic_block cond2_bb = single_pred (cond_bb);
2384   if (EDGE_COUNT (cond2_bb->succs) != 2)
2385     return false;
2386   edge cond2_phi_edge;
2387   if (EDGE_SUCC (cond2_bb, 0)->dest == cond_bb)
2388     {
2389       if (EDGE_SUCC (cond2_bb, 1)->dest != phi_bb)
2390 	return false;
2391       cond2_phi_edge = EDGE_SUCC (cond2_bb, 1);
2392     }
2393   else if (EDGE_SUCC (cond2_bb, 0)->dest != phi_bb)
2394     return false;
2395   else
2396     cond2_phi_edge = EDGE_SUCC (cond2_bb, 0);
2397   tree arg2 = gimple_phi_arg_def (phi, cond2_phi_edge->dest_idx);
2398   if (!tree_fits_shwi_p (arg2))
2399     return false;
2400   gimple *cond2 = last_stmt (cond2_bb);
2401   if (cond2 == NULL || gimple_code (cond2) != GIMPLE_COND)
2402     return false;
2403   enum tree_code cmp2 = gimple_cond_code (cond2);
2404   tree lhs2 = gimple_cond_lhs (cond2);
2405   tree rhs2 = gimple_cond_rhs (cond2);
2406   if (lhs2 == lhs1)
2407     {
2408       if (!operand_equal_p (rhs2, rhs1, 0))
2409 	{
2410 	  if ((cmp2 == EQ_EXPR || cmp2 == NE_EXPR)
2411 	      && TREE_CODE (rhs1) == INTEGER_CST
2412 	      && TREE_CODE (rhs2) == INTEGER_CST)
2413 	    {
2414 	      /* For integers, we can have cond2 x == 5
2415 		 and cond1 x < 5, x <= 4, x <= 5, x < 6,
2416 		 x > 5, x >= 6, x >= 5 or x > 4.  */
2417 	      if (tree_int_cst_lt (rhs1, rhs2))
2418 		{
2419 		  if (wi::ne_p (wi::to_wide (rhs1) + 1, wi::to_wide (rhs2)))
2420 		    return false;
2421 		  if (cmp1 == LE_EXPR)
2422 		    cmp1 = LT_EXPR;
2423 		  else if (cmp1 == GT_EXPR)
2424 		    cmp1 = GE_EXPR;
2425 		  else
2426 		    return false;
2427 		}
2428 	      else
2429 		{
2430 		  gcc_checking_assert (tree_int_cst_lt (rhs2, rhs1));
2431 		  if (wi::ne_p (wi::to_wide (rhs2) + 1, wi::to_wide (rhs1)))
2432 		    return false;
2433 		  if (cmp1 == LT_EXPR)
2434 		    cmp1 = LE_EXPR;
2435 		  else if (cmp1 == GE_EXPR)
2436 		    cmp1 = GT_EXPR;
2437 		  else
2438 		    return false;
2439 		}
2440 	      rhs1 = rhs2;
2441 	    }
2442 	  else
2443 	    return false;
2444 	}
2445     }
2446   else if (lhs2 == rhs1)
2447     {
2448       if (rhs2 != lhs1)
2449 	return false;
2450     }
2451   else
2452     return false;
2453 
2454   tree arg3 = arg2;
2455   basic_block cond3_bb = cond2_bb;
2456   edge cond3_phi_edge = cond2_phi_edge;
2457   gimple *cond3 = cond2;
2458   enum tree_code cmp3 = cmp2;
2459   tree lhs3 = lhs2;
2460   tree rhs3 = rhs2;
2461   if (EDGE_COUNT (phi_bb->preds) == 4)
2462     {
2463       if (absu_hwi (tree_to_shwi (arg2)) != 1)
2464 	return false;
2465       if (e1->flags & EDGE_TRUE_VALUE)
2466 	{
2467 	  if (tree_to_shwi (arg0) != 2
2468 	      || absu_hwi (tree_to_shwi (arg1)) != 1
2469 	      || wi::to_widest (arg1) == wi::to_widest (arg2))
2470 	    return false;
2471 	}
2472       else if (tree_to_shwi (arg1) != 2
2473 	       || absu_hwi (tree_to_shwi (arg0)) != 1
2474 	       || wi::to_widest (arg0) == wi::to_widest (arg1))
2475 	return false;
2476       switch (cmp2)
2477 	{
2478 	case LT_EXPR:
2479 	case LE_EXPR:
2480 	case GT_EXPR:
2481 	case GE_EXPR:
2482 	  break;
2483 	default:
2484 	  return false;
2485 	}
2486       /* if (x < y) goto phi_bb; else fallthru;
2487 	 if (x > y) goto phi_bb; else fallthru;
2488 	 bbx:;
2489 	 phi_bb:;
2490 	 is ok, but if x and y are swapped in one of the comparisons,
2491 	 or the comparisons are the same and operands not swapped,
2492 	 or the true and false edges are swapped, it is not.  */
2493       if ((lhs2 == lhs1)
2494 	  ^ (((cond2_phi_edge->flags
2495 	       & ((cmp2 == LT_EXPR || cmp2 == LE_EXPR)
2496 		  ? EDGE_TRUE_VALUE : EDGE_FALSE_VALUE)) != 0)
2497 	     != ((e1->flags
2498 		  & ((cmp1 == LT_EXPR || cmp1 == LE_EXPR)
2499 		     ? EDGE_TRUE_VALUE : EDGE_FALSE_VALUE)) != 0)))
2500 	return false;
2501       if (!single_pred_p (cond2_bb) || !cond_only_block_p (cond2_bb))
2502 	return false;
2503       cond3_bb = single_pred (cond2_bb);
2504       if (EDGE_COUNT (cond2_bb->succs) != 2)
2505 	return false;
2506       if (EDGE_SUCC (cond3_bb, 0)->dest == cond2_bb)
2507 	{
2508 	  if (EDGE_SUCC (cond3_bb, 1)->dest != phi_bb)
2509 	    return false;
2510 	  cond3_phi_edge = EDGE_SUCC (cond3_bb, 1);
2511 	}
2512       else if (EDGE_SUCC (cond3_bb, 0)->dest != phi_bb)
2513 	return false;
2514       else
2515 	cond3_phi_edge = EDGE_SUCC (cond3_bb, 0);
2516       arg3 = gimple_phi_arg_def (phi, cond3_phi_edge->dest_idx);
2517       cond3 = last_stmt (cond3_bb);
2518       if (cond3 == NULL || gimple_code (cond3) != GIMPLE_COND)
2519 	return false;
2520       cmp3 = gimple_cond_code (cond3);
2521       lhs3 = gimple_cond_lhs (cond3);
2522       rhs3 = gimple_cond_rhs (cond3);
2523       if (lhs3 == lhs1)
2524 	{
2525 	  if (!operand_equal_p (rhs3, rhs1, 0))
2526 	    return false;
2527 	}
2528       else if (lhs3 == rhs1)
2529 	{
2530 	  if (rhs3 != lhs1)
2531 	    return false;
2532 	}
2533       else
2534 	return false;
2535     }
2536   else if (absu_hwi (tree_to_shwi (arg0)) != 1
2537 	   || absu_hwi (tree_to_shwi (arg1)) != 1
2538 	   || wi::to_widest (arg0) == wi::to_widest (arg1))
2539     return false;
2540 
2541   if (!integer_zerop (arg3) || (cmp3 != EQ_EXPR && cmp3 != NE_EXPR))
2542     return false;
2543   if ((cond3_phi_edge->flags & (cmp3 == EQ_EXPR
2544 				? EDGE_TRUE_VALUE : EDGE_FALSE_VALUE)) == 0)
2545     return false;
2546 
2547   /* lhs1 one_cmp rhs1 results in phires of 1.  */
2548   enum tree_code one_cmp;
2549   if ((cmp1 == LT_EXPR || cmp1 == LE_EXPR)
2550       ^ (!integer_onep ((e1->flags & EDGE_TRUE_VALUE) ? arg1 : arg0)))
2551     one_cmp = LT_EXPR;
2552   else
2553     one_cmp = GT_EXPR;
2554 
2555   enum tree_code res_cmp;
2556   switch (cmp)
2557     {
2558     case EQ_EXPR:
2559       if (integer_zerop (rhs))
2560 	res_cmp = EQ_EXPR;
2561       else if (integer_minus_onep (rhs))
2562 	res_cmp = one_cmp == LT_EXPR ? GT_EXPR : LT_EXPR;
2563       else if (integer_onep (rhs))
2564 	res_cmp = one_cmp;
2565       else
2566 	return false;
2567       break;
2568     case NE_EXPR:
2569       if (integer_zerop (rhs))
2570 	res_cmp = NE_EXPR;
2571       else if (integer_minus_onep (rhs))
2572 	res_cmp = one_cmp == LT_EXPR ? LE_EXPR : GE_EXPR;
2573       else if (integer_onep (rhs))
2574 	res_cmp = one_cmp == LT_EXPR ? GE_EXPR : LE_EXPR;
2575       else
2576 	return false;
2577       break;
2578     case LT_EXPR:
2579       if (integer_onep (rhs))
2580 	res_cmp = one_cmp == LT_EXPR ? GE_EXPR : LE_EXPR;
2581       else if (integer_zerop (rhs))
2582 	res_cmp = one_cmp == LT_EXPR ? GT_EXPR : LT_EXPR;
2583       else
2584 	return false;
2585       break;
2586     case LE_EXPR:
2587       if (integer_zerop (rhs))
2588 	res_cmp = one_cmp == LT_EXPR ? GE_EXPR : LE_EXPR;
2589       else if (integer_minus_onep (rhs))
2590 	res_cmp = one_cmp == LT_EXPR ? GT_EXPR : LT_EXPR;
2591       else
2592 	return false;
2593       break;
2594     case GT_EXPR:
2595       if (integer_minus_onep (rhs))
2596 	res_cmp = one_cmp == LT_EXPR ? LE_EXPR : GE_EXPR;
2597       else if (integer_zerop (rhs))
2598 	res_cmp = one_cmp;
2599       else
2600 	return false;
2601       break;
2602     case GE_EXPR:
2603       if (integer_zerop (rhs))
2604 	res_cmp = one_cmp == LT_EXPR ? LE_EXPR : GE_EXPR;
2605       else if (integer_onep (rhs))
2606 	res_cmp = one_cmp;
2607       else
2608 	return false;
2609       break;
2610     default:
2611       gcc_unreachable ();
2612     }
2613 
2614   if (gimple_code (use_stmt) == GIMPLE_COND)
2615     {
2616       gcond *use_cond = as_a <gcond *> (use_stmt);
2617       gimple_cond_set_code (use_cond, res_cmp);
2618       gimple_cond_set_lhs (use_cond, lhs1);
2619       gimple_cond_set_rhs (use_cond, rhs1);
2620     }
2621   else if (gimple_assign_rhs_class (use_stmt) == GIMPLE_BINARY_RHS)
2622     {
2623       gimple_assign_set_rhs_code (use_stmt, res_cmp);
2624       gimple_assign_set_rhs1 (use_stmt, lhs1);
2625       gimple_assign_set_rhs2 (use_stmt, rhs1);
2626     }
2627   else
2628     {
2629       tree cond = build2 (res_cmp, TREE_TYPE (gimple_assign_rhs1 (use_stmt)),
2630 			  lhs1, rhs1);
2631       gimple_assign_set_rhs1 (use_stmt, cond);
2632     }
2633   update_stmt (use_stmt);
2634 
2635   if (MAY_HAVE_DEBUG_BIND_STMTS)
2636     {
2637       use_operand_p use_p;
2638       imm_use_iterator iter;
2639       bool has_debug_uses = false;
2640       bool has_cast_debug_uses = false;
2641       FOR_EACH_IMM_USE_FAST (use_p, iter, phires)
2642 	{
2643 	  gimple *use_stmt = USE_STMT (use_p);
2644 	  if (orig_use_lhs && use_stmt == orig_use_stmt)
2645 	    continue;
2646 	  gcc_assert (is_gimple_debug (use_stmt));
2647 	  has_debug_uses = true;
2648 	  break;
2649 	}
2650       if (orig_use_lhs)
2651 	{
2652 	  if (!has_debug_uses || is_cast)
2653 	    FOR_EACH_IMM_USE_FAST (use_p, iter, orig_use_lhs)
2654 	      {
2655 		gimple *use_stmt = USE_STMT (use_p);
2656 		gcc_assert (is_gimple_debug (use_stmt));
2657 		has_debug_uses = true;
2658 		if (is_cast)
2659 		  has_cast_debug_uses = true;
2660 	      }
2661 	  gimple_stmt_iterator gsi = gsi_for_stmt (orig_use_stmt);
2662 	  tree zero = build_zero_cst (TREE_TYPE (orig_use_lhs));
2663 	  gimple_assign_set_rhs_with_ops (&gsi, INTEGER_CST, zero);
2664 	  update_stmt (orig_use_stmt);
2665 	}
2666 
2667       if (has_debug_uses)
2668 	{
2669 	  /* If there are debug uses, emit something like:
2670 	     # DEBUG D#1 => i_2(D) > j_3(D) ? 1 : -1
2671 	     # DEBUG D#2 => i_2(D) == j_3(D) ? 0 : D#1
2672 	     where > stands for the comparison that yielded 1
2673 	     and replace debug uses of phi result with that D#2.
2674 	     Ignore the value of 2, because if NaNs aren't expected,
2675 	     all floating point numbers should be comparable.  */
2676 	  gimple_stmt_iterator gsi = gsi_after_labels (gimple_bb (phi));
2677 	  tree type = TREE_TYPE (phires);
2678 	  tree temp1 = build_debug_expr_decl (type);
2679 	  tree t = build2 (one_cmp, boolean_type_node, lhs1, rhs2);
2680 	  t = build3 (COND_EXPR, type, t, build_one_cst (type),
2681 		      build_int_cst (type, -1));
2682 	  gimple *g = gimple_build_debug_bind (temp1, t, phi);
2683 	  gsi_insert_before (&gsi, g, GSI_SAME_STMT);
2684 	  tree temp2 = build_debug_expr_decl (type);
2685 	  t = build2 (EQ_EXPR, boolean_type_node, lhs1, rhs2);
2686 	  t = build3 (COND_EXPR, type, t, build_zero_cst (type), temp1);
2687 	  g = gimple_build_debug_bind (temp2, t, phi);
2688 	  gsi_insert_before (&gsi, g, GSI_SAME_STMT);
2689 	  replace_uses_by (phires, temp2);
2690 	  if (orig_use_lhs)
2691 	    {
2692 	      if (has_cast_debug_uses)
2693 		{
2694 		  tree temp3 = make_node (DEBUG_EXPR_DECL);
2695 		  DECL_ARTIFICIAL (temp3) = 1;
2696 		  TREE_TYPE (temp3) = TREE_TYPE (orig_use_lhs);
2697 		  SET_DECL_MODE (temp3, TYPE_MODE (type));
2698 		  t = fold_convert (TREE_TYPE (temp3), temp2);
2699 		  g = gimple_build_debug_bind (temp3, t, phi);
2700 		  gsi_insert_before (&gsi, g, GSI_SAME_STMT);
2701 		  replace_uses_by (orig_use_lhs, temp3);
2702 		}
2703 	      else
2704 		replace_uses_by (orig_use_lhs, temp2);
2705 	    }
2706 	}
2707     }
2708 
2709   if (orig_use_lhs)
2710     {
2711       gimple_stmt_iterator gsi = gsi_for_stmt (orig_use_stmt);
2712       gsi_remove (&gsi, true);
2713     }
2714 
2715   gimple_stmt_iterator psi = gsi_for_stmt (phi);
2716   remove_phi_node (&psi, true);
2717   statistics_counter_event (cfun, "spaceship replacement", 1);
2718 
2719   return true;
2720 }
2721 
2722 /* Optimize x ? __builtin_fun (x) : C, where C is __builtin_fun (0).
2723    Convert
2724 
2725    <bb 2>
2726    if (b_4(D) != 0)
2727    goto <bb 3>
2728    else
2729    goto <bb 4>
2730 
2731    <bb 3>
2732    _2 = (unsigned long) b_4(D);
2733    _9 = __builtin_popcountl (_2);
2734    OR
2735    _9 = __builtin_popcountl (b_4(D));
2736 
2737    <bb 4>
2738    c_12 = PHI <0(2), _9(3)>
2739 
2740    Into
2741    <bb 2>
2742    _2 = (unsigned long) b_4(D);
2743    _9 = __builtin_popcountl (_2);
2744    OR
2745    _9 = __builtin_popcountl (b_4(D));
2746 
2747    <bb 4>
2748    c_12 = PHI <_9(2)>
2749 
2750    Similarly for __builtin_clz or __builtin_ctz if
2751    C?Z_DEFINED_VALUE_AT_ZERO is 2, optab is present and
2752    instead of 0 above it uses the value from that macro.  */
2753 
2754 static bool
cond_removal_in_builtin_zero_pattern(basic_block cond_bb,basic_block middle_bb,edge e1,edge e2,gphi * phi,tree arg0,tree arg1)2755 cond_removal_in_builtin_zero_pattern (basic_block cond_bb,
2756 				      basic_block middle_bb,
2757 				      edge e1, edge e2, gphi *phi,
2758 				      tree arg0, tree arg1)
2759 {
2760   gimple *cond;
2761   gimple_stmt_iterator gsi, gsi_from;
2762   gimple *call;
2763   gimple *cast = NULL;
2764   tree lhs, arg;
2765 
2766   /* Check that
2767    _2 = (unsigned long) b_4(D);
2768    _9 = __builtin_popcountl (_2);
2769    OR
2770    _9 = __builtin_popcountl (b_4(D));
2771    are the only stmts in the middle_bb.  */
2772 
2773   gsi = gsi_start_nondebug_after_labels_bb (middle_bb);
2774   if (gsi_end_p (gsi))
2775     return false;
2776   cast = gsi_stmt (gsi);
2777   gsi_next_nondebug (&gsi);
2778   if (!gsi_end_p (gsi))
2779     {
2780       call = gsi_stmt (gsi);
2781       gsi_next_nondebug (&gsi);
2782       if (!gsi_end_p (gsi))
2783 	return false;
2784     }
2785   else
2786     {
2787       call = cast;
2788       cast = NULL;
2789     }
2790 
2791   /* Check that we have a popcount/clz/ctz builtin.  */
2792   if (!is_gimple_call (call) || gimple_call_num_args (call) != 1)
2793     return false;
2794 
2795   arg = gimple_call_arg (call, 0);
2796   lhs = gimple_get_lhs (call);
2797 
2798   if (lhs == NULL_TREE)
2799     return false;
2800 
2801   combined_fn cfn = gimple_call_combined_fn (call);
2802   internal_fn ifn = IFN_LAST;
2803   int val = 0;
2804   switch (cfn)
2805     {
2806     case CFN_BUILT_IN_BSWAP16:
2807     case CFN_BUILT_IN_BSWAP32:
2808     case CFN_BUILT_IN_BSWAP64:
2809     case CFN_BUILT_IN_BSWAP128:
2810     CASE_CFN_FFS:
2811     CASE_CFN_PARITY:
2812     CASE_CFN_POPCOUNT:
2813       break;
2814     CASE_CFN_CLZ:
2815       if (INTEGRAL_TYPE_P (TREE_TYPE (arg)))
2816 	{
2817 	  tree type = TREE_TYPE (arg);
2818 	  if (direct_internal_fn_supported_p (IFN_CLZ, type, OPTIMIZE_FOR_BOTH)
2819 	      && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type),
2820 					    val) == 2)
2821 	    {
2822 	      ifn = IFN_CLZ;
2823 	      break;
2824 	    }
2825 	}
2826       return false;
2827     CASE_CFN_CTZ:
2828       if (INTEGRAL_TYPE_P (TREE_TYPE (arg)))
2829 	{
2830 	  tree type = TREE_TYPE (arg);
2831 	  if (direct_internal_fn_supported_p (IFN_CTZ, type, OPTIMIZE_FOR_BOTH)
2832 	      && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type),
2833 					    val) == 2)
2834 	    {
2835 	      ifn = IFN_CTZ;
2836 	      break;
2837 	    }
2838 	}
2839       return false;
2840     case CFN_BUILT_IN_CLRSB:
2841       val = TYPE_PRECISION (integer_type_node) - 1;
2842       break;
2843     case CFN_BUILT_IN_CLRSBL:
2844       val = TYPE_PRECISION (long_integer_type_node) - 1;
2845       break;
2846     case CFN_BUILT_IN_CLRSBLL:
2847       val = TYPE_PRECISION (long_long_integer_type_node) - 1;
2848       break;
2849     default:
2850       return false;
2851     }
2852 
2853   if (cast)
2854     {
2855       /* We have a cast stmt feeding popcount/clz/ctz builtin.  */
2856       /* Check that we have a cast prior to that.  */
2857       if (gimple_code (cast) != GIMPLE_ASSIGN
2858 	  || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (cast)))
2859 	return false;
2860       /* Result of the cast stmt is the argument to the builtin.  */
2861       if (arg != gimple_assign_lhs (cast))
2862 	return false;
2863       arg = gimple_assign_rhs1 (cast);
2864     }
2865 
2866   cond = last_stmt (cond_bb);
2867 
2868   /* Cond_bb has a check for b_4 [!=|==] 0 before calling the popcount/clz/ctz
2869      builtin.  */
2870   if (gimple_code (cond) != GIMPLE_COND
2871       || (gimple_cond_code (cond) != NE_EXPR
2872 	  && gimple_cond_code (cond) != EQ_EXPR)
2873       || !integer_zerop (gimple_cond_rhs (cond))
2874       || arg != gimple_cond_lhs (cond))
2875     return false;
2876 
2877   /* Canonicalize.  */
2878   if ((e2->flags & EDGE_TRUE_VALUE
2879        && gimple_cond_code (cond) == NE_EXPR)
2880       || (e1->flags & EDGE_TRUE_VALUE
2881 	  && gimple_cond_code (cond) == EQ_EXPR))
2882     {
2883       std::swap (arg0, arg1);
2884       std::swap (e1, e2);
2885     }
2886 
2887   /* Check PHI arguments.  */
2888   if (lhs != arg0
2889       || TREE_CODE (arg1) != INTEGER_CST
2890       || wi::to_wide (arg1) != val)
2891     return false;
2892 
2893   /* And insert the popcount/clz/ctz builtin and cast stmt before the
2894      cond_bb.  */
2895   gsi = gsi_last_bb (cond_bb);
2896   if (cast)
2897     {
2898       gsi_from = gsi_for_stmt (cast);
2899       gsi_move_before (&gsi_from, &gsi);
2900       reset_flow_sensitive_info (gimple_get_lhs (cast));
2901     }
2902   gsi_from = gsi_for_stmt (call);
2903   if (ifn == IFN_LAST || gimple_call_internal_p (call))
2904     gsi_move_before (&gsi_from, &gsi);
2905   else
2906     {
2907       /* For __builtin_c[lt]z* force .C[LT]Z ifn, because only
2908 	 the latter is well defined at zero.  */
2909       call = gimple_build_call_internal (ifn, 1, gimple_call_arg (call, 0));
2910       gimple_call_set_lhs (call, lhs);
2911       gsi_insert_before (&gsi, call, GSI_SAME_STMT);
2912       gsi_remove (&gsi_from, true);
2913     }
2914   reset_flow_sensitive_info (lhs);
2915 
2916   /* Now update the PHI and remove unneeded bbs.  */
2917   replace_phi_edge_with_variable (cond_bb, e2, phi, lhs);
2918   return true;
2919 }
2920 
2921 /* Auxiliary functions to determine the set of memory accesses which
2922    can't trap because they are preceded by accesses to the same memory
2923    portion.  We do that for MEM_REFs, so we only need to track
2924    the SSA_NAME of the pointer indirectly referenced.  The algorithm
2925    simply is a walk over all instructions in dominator order.  When
2926    we see an MEM_REF we determine if we've already seen a same
2927    ref anywhere up to the root of the dominator tree.  If we do the
2928    current access can't trap.  If we don't see any dominating access
2929    the current access might trap, but might also make later accesses
2930    non-trapping, so we remember it.  We need to be careful with loads
2931    or stores, for instance a load might not trap, while a store would,
2932    so if we see a dominating read access this doesn't mean that a later
2933    write access would not trap.  Hence we also need to differentiate the
2934    type of access(es) seen.
2935 
2936    ??? We currently are very conservative and assume that a load might
2937    trap even if a store doesn't (write-only memory).  This probably is
2938    overly conservative.
2939 
2940    We currently support a special case that for !TREE_ADDRESSABLE automatic
2941    variables, it could ignore whether something is a load or store because the
2942    local stack should be always writable.  */
2943 
2944 /* A hash-table of references (MEM_REF/ARRAY_REF/COMPONENT_REF), and in which
2945    basic block an *_REF through it was seen, which would constitute a
2946    no-trap region for same accesses.
2947 
2948    Size is needed to support 2 MEM_REFs of different types, like
2949    MEM<double>(s_1) and MEM<long>(s_1), which would compare equal with
2950    OEP_ADDRESS_OF.  */
2951 struct ref_to_bb
2952 {
2953   tree exp;
2954   HOST_WIDE_INT size;
2955   unsigned int phase;
2956   basic_block bb;
2957 };
2958 
2959 /* Hashtable helpers.  */
2960 
2961 struct refs_hasher : free_ptr_hash<ref_to_bb>
2962 {
2963   static inline hashval_t hash (const ref_to_bb *);
2964   static inline bool equal (const ref_to_bb *, const ref_to_bb *);
2965 };
2966 
2967 /* Used for quick clearing of the hash-table when we see calls.
2968    Hash entries with phase < nt_call_phase are invalid.  */
2969 static unsigned int nt_call_phase;
2970 
2971 /* The hash function.  */
2972 
2973 inline hashval_t
hash(const ref_to_bb * n)2974 refs_hasher::hash (const ref_to_bb *n)
2975 {
2976   inchash::hash hstate;
2977   inchash::add_expr (n->exp, hstate, OEP_ADDRESS_OF);
2978   hstate.add_hwi (n->size);
2979   return hstate.end ();
2980 }
2981 
2982 /* The equality function of *P1 and *P2.  */
2983 
2984 inline bool
equal(const ref_to_bb * n1,const ref_to_bb * n2)2985 refs_hasher::equal (const ref_to_bb *n1, const ref_to_bb *n2)
2986 {
2987   return operand_equal_p (n1->exp, n2->exp, OEP_ADDRESS_OF)
2988 	 && n1->size == n2->size;
2989 }
2990 
2991 class nontrapping_dom_walker : public dom_walker
2992 {
2993 public:
nontrapping_dom_walker(cdi_direction direction,hash_set<tree> * ps)2994   nontrapping_dom_walker (cdi_direction direction, hash_set<tree> *ps)
2995     : dom_walker (direction), m_nontrapping (ps), m_seen_refs (128)
2996   {}
2997 
2998   virtual edge before_dom_children (basic_block);
2999   virtual void after_dom_children (basic_block);
3000 
3001 private:
3002 
3003   /* We see the expression EXP in basic block BB.  If it's an interesting
3004      expression (an MEM_REF through an SSA_NAME) possibly insert the
3005      expression into the set NONTRAP or the hash table of seen expressions.
3006      STORE is true if this expression is on the LHS, otherwise it's on
3007      the RHS.  */
3008   void add_or_mark_expr (basic_block, tree, bool);
3009 
3010   hash_set<tree> *m_nontrapping;
3011 
3012   /* The hash table for remembering what we've seen.  */
3013   hash_table<refs_hasher> m_seen_refs;
3014 };
3015 
3016 /* Called by walk_dominator_tree, when entering the block BB.  */
3017 edge
before_dom_children(basic_block bb)3018 nontrapping_dom_walker::before_dom_children (basic_block bb)
3019 {
3020   edge e;
3021   edge_iterator ei;
3022   gimple_stmt_iterator gsi;
3023 
3024   /* If we haven't seen all our predecessors, clear the hash-table.  */
3025   FOR_EACH_EDGE (e, ei, bb->preds)
3026     if ((((size_t)e->src->aux) & 2) == 0)
3027       {
3028 	nt_call_phase++;
3029 	break;
3030       }
3031 
3032   /* Mark this BB as being on the path to dominator root and as visited.  */
3033   bb->aux = (void*)(1 | 2);
3034 
3035   /* And walk the statements in order.  */
3036   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3037     {
3038       gimple *stmt = gsi_stmt (gsi);
3039 
3040       if ((gimple_code (stmt) == GIMPLE_ASM && gimple_vdef (stmt))
3041 	  || (is_gimple_call (stmt)
3042 	      && (!nonfreeing_call_p (stmt) || !nonbarrier_call_p (stmt))))
3043 	nt_call_phase++;
3044       else if (gimple_assign_single_p (stmt) && !gimple_has_volatile_ops (stmt))
3045 	{
3046 	  add_or_mark_expr (bb, gimple_assign_lhs (stmt), true);
3047 	  add_or_mark_expr (bb, gimple_assign_rhs1 (stmt), false);
3048 	}
3049     }
3050   return NULL;
3051 }
3052 
3053 /* Called by walk_dominator_tree, when basic block BB is exited.  */
3054 void
after_dom_children(basic_block bb)3055 nontrapping_dom_walker::after_dom_children (basic_block bb)
3056 {
3057   /* This BB isn't on the path to dominator root anymore.  */
3058   bb->aux = (void*)2;
3059 }
3060 
3061 /* We see the expression EXP in basic block BB.  If it's an interesting
3062    expression of:
3063      1) MEM_REF
3064      2) ARRAY_REF
3065      3) COMPONENT_REF
3066    possibly insert the expression into the set NONTRAP or the hash table
3067    of seen expressions.  STORE is true if this expression is on the LHS,
3068    otherwise it's on the RHS.  */
3069 void
add_or_mark_expr(basic_block bb,tree exp,bool store)3070 nontrapping_dom_walker::add_or_mark_expr (basic_block bb, tree exp, bool store)
3071 {
3072   HOST_WIDE_INT size;
3073 
3074   if ((TREE_CODE (exp) == MEM_REF || TREE_CODE (exp) == ARRAY_REF
3075        || TREE_CODE (exp) == COMPONENT_REF)
3076       && (size = int_size_in_bytes (TREE_TYPE (exp))) > 0)
3077     {
3078       struct ref_to_bb map;
3079       ref_to_bb **slot;
3080       struct ref_to_bb *r2bb;
3081       basic_block found_bb = 0;
3082 
3083       if (!store)
3084 	{
3085 	  tree base = get_base_address (exp);
3086 	  /* Only record a LOAD of a local variable without address-taken, as
3087 	     the local stack is always writable.  This allows cselim on a STORE
3088 	     with a dominating LOAD.  */
3089 	  if (!auto_var_p (base) || TREE_ADDRESSABLE (base))
3090 	    return;
3091 	}
3092 
3093       /* Try to find the last seen *_REF, which can trap.  */
3094       map.exp = exp;
3095       map.size = size;
3096       slot = m_seen_refs.find_slot (&map, INSERT);
3097       r2bb = *slot;
3098       if (r2bb && r2bb->phase >= nt_call_phase)
3099 	found_bb = r2bb->bb;
3100 
3101       /* If we've found a trapping *_REF, _and_ it dominates EXP
3102 	 (it's in a basic block on the path from us to the dominator root)
3103 	 then we can't trap.  */
3104       if (found_bb && (((size_t)found_bb->aux) & 1) == 1)
3105 	{
3106 	  m_nontrapping->add (exp);
3107 	}
3108       else
3109 	{
3110 	  /* EXP might trap, so insert it into the hash table.  */
3111 	  if (r2bb)
3112 	    {
3113 	      r2bb->phase = nt_call_phase;
3114 	      r2bb->bb = bb;
3115 	    }
3116 	  else
3117 	    {
3118 	      r2bb = XNEW (struct ref_to_bb);
3119 	      r2bb->phase = nt_call_phase;
3120 	      r2bb->bb = bb;
3121 	      r2bb->exp = exp;
3122 	      r2bb->size = size;
3123 	      *slot = r2bb;
3124 	    }
3125 	}
3126     }
3127 }
3128 
3129 /* This is the entry point of gathering non trapping memory accesses.
3130    It will do a dominator walk over the whole function, and it will
3131    make use of the bb->aux pointers.  It returns a set of trees
3132    (the MEM_REFs itself) which can't trap.  */
3133 static hash_set<tree> *
get_non_trapping(void)3134 get_non_trapping (void)
3135 {
3136   nt_call_phase = 0;
3137   hash_set<tree> *nontrap = new hash_set<tree>;
3138 
3139   nontrapping_dom_walker (CDI_DOMINATORS, nontrap)
3140     .walk (cfun->cfg->x_entry_block_ptr);
3141 
3142   clear_aux_for_blocks ();
3143   return nontrap;
3144 }
3145 
3146 /* Do the main work of conditional store replacement.  We already know
3147    that the recognized pattern looks like so:
3148 
3149    split:
3150      if (cond) goto MIDDLE_BB; else goto JOIN_BB (edge E1)
3151    MIDDLE_BB:
3152      something
3153      fallthrough (edge E0)
3154    JOIN_BB:
3155      some more
3156 
3157    We check that MIDDLE_BB contains only one store, that that store
3158    doesn't trap (not via NOTRAP, but via checking if an access to the same
3159    memory location dominates us, or the store is to a local addressable
3160    object) and that the store has a "simple" RHS.  */
3161 
3162 static bool
cond_store_replacement(basic_block middle_bb,basic_block join_bb,edge e0,edge e1,hash_set<tree> * nontrap)3163 cond_store_replacement (basic_block middle_bb, basic_block join_bb,
3164 			edge e0, edge e1, hash_set<tree> *nontrap)
3165 {
3166   gimple *assign = last_and_only_stmt (middle_bb);
3167   tree lhs, rhs, name, name2;
3168   gphi *newphi;
3169   gassign *new_stmt;
3170   gimple_stmt_iterator gsi;
3171   location_t locus;
3172 
3173   /* Check if middle_bb contains of only one store.  */
3174   if (!assign
3175       || !gimple_assign_single_p (assign)
3176       || gimple_has_volatile_ops (assign))
3177     return false;
3178 
3179   /* And no PHI nodes so all uses in the single stmt are also
3180      available where we insert to.  */
3181   if (!gimple_seq_empty_p (phi_nodes (middle_bb)))
3182     return false;
3183 
3184   locus = gimple_location (assign);
3185   lhs = gimple_assign_lhs (assign);
3186   rhs = gimple_assign_rhs1 (assign);
3187   if ((!REFERENCE_CLASS_P (lhs)
3188        && !DECL_P (lhs))
3189       || !is_gimple_reg_type (TREE_TYPE (lhs)))
3190     return false;
3191 
3192   /* Prove that we can move the store down.  We could also check
3193      TREE_THIS_NOTRAP here, but in that case we also could move stores,
3194      whose value is not available readily, which we want to avoid.  */
3195   if (!nontrap->contains (lhs))
3196     {
3197       /* If LHS is an access to a local variable without address-taken
3198 	 (or when we allow data races) and known not to trap, we could
3199 	 always safely move down the store.  */
3200       tree base = get_base_address (lhs);
3201       if (!auto_var_p (base)
3202 	  || (TREE_ADDRESSABLE (base) && !flag_store_data_races)
3203 	  || tree_could_trap_p (lhs))
3204 	return false;
3205     }
3206 
3207   /* Now we've checked the constraints, so do the transformation:
3208      1) Remove the single store.  */
3209   gsi = gsi_for_stmt (assign);
3210   unlink_stmt_vdef (assign);
3211   gsi_remove (&gsi, true);
3212   release_defs (assign);
3213 
3214   /* Make both store and load use alias-set zero as we have to
3215      deal with the case of the store being a conditional change
3216      of the dynamic type.  */
3217   lhs = unshare_expr (lhs);
3218   tree *basep = &lhs;
3219   while (handled_component_p (*basep))
3220     basep = &TREE_OPERAND (*basep, 0);
3221   if (TREE_CODE (*basep) == MEM_REF
3222       || TREE_CODE (*basep) == TARGET_MEM_REF)
3223     TREE_OPERAND (*basep, 1)
3224       = fold_convert (ptr_type_node, TREE_OPERAND (*basep, 1));
3225   else
3226     *basep = build2 (MEM_REF, TREE_TYPE (*basep),
3227 		     build_fold_addr_expr (*basep),
3228 		     build_zero_cst (ptr_type_node));
3229 
3230   /* 2) Insert a load from the memory of the store to the temporary
3231         on the edge which did not contain the store.  */
3232   name = make_temp_ssa_name (TREE_TYPE (lhs), NULL, "cstore");
3233   new_stmt = gimple_build_assign (name, lhs);
3234   gimple_set_location (new_stmt, locus);
3235   lhs = unshare_expr (lhs);
3236   {
3237     /* Set the no-warning bit on the rhs of the load to avoid uninit
3238        warnings.  */
3239     tree rhs1 = gimple_assign_rhs1 (new_stmt);
3240     suppress_warning (rhs1, OPT_Wuninitialized);
3241   }
3242   gsi_insert_on_edge (e1, new_stmt);
3243 
3244   /* 3) Create a PHI node at the join block, with one argument
3245         holding the old RHS, and the other holding the temporary
3246         where we stored the old memory contents.  */
3247   name2 = make_temp_ssa_name (TREE_TYPE (lhs), NULL, "cstore");
3248   newphi = create_phi_node (name2, join_bb);
3249   add_phi_arg (newphi, rhs, e0, locus);
3250   add_phi_arg (newphi, name, e1, locus);
3251 
3252   new_stmt = gimple_build_assign (lhs, PHI_RESULT (newphi));
3253 
3254   /* 4) Insert that PHI node.  */
3255   gsi = gsi_after_labels (join_bb);
3256   if (gsi_end_p (gsi))
3257     {
3258       gsi = gsi_last_bb (join_bb);
3259       gsi_insert_after (&gsi, new_stmt, GSI_NEW_STMT);
3260     }
3261   else
3262     gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
3263 
3264   if (dump_file && (dump_flags & TDF_DETAILS))
3265     {
3266       fprintf (dump_file, "\nConditional store replacement happened!");
3267       fprintf (dump_file, "\nReplaced the store with a load.");
3268       fprintf (dump_file, "\nInserted a new PHI statement in joint block:\n");
3269       print_gimple_stmt (dump_file, new_stmt, 0, TDF_VOPS|TDF_MEMSYMS);
3270     }
3271   statistics_counter_event (cfun, "conditional store replacement", 1);
3272 
3273   return true;
3274 }
3275 
3276 /* Do the main work of conditional store replacement.  */
3277 
3278 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)3279 cond_if_else_store_replacement_1 (basic_block then_bb, basic_block else_bb,
3280 				  basic_block join_bb, gimple *then_assign,
3281 				  gimple *else_assign)
3282 {
3283   tree lhs_base, lhs, then_rhs, else_rhs, name;
3284   location_t then_locus, else_locus;
3285   gimple_stmt_iterator gsi;
3286   gphi *newphi;
3287   gassign *new_stmt;
3288 
3289   if (then_assign == NULL
3290       || !gimple_assign_single_p (then_assign)
3291       || gimple_clobber_p (then_assign)
3292       || gimple_has_volatile_ops (then_assign)
3293       || else_assign == NULL
3294       || !gimple_assign_single_p (else_assign)
3295       || gimple_clobber_p (else_assign)
3296       || gimple_has_volatile_ops (else_assign))
3297     return false;
3298 
3299   lhs = gimple_assign_lhs (then_assign);
3300   if (!is_gimple_reg_type (TREE_TYPE (lhs))
3301       || !operand_equal_p (lhs, gimple_assign_lhs (else_assign), 0))
3302     return false;
3303 
3304   lhs_base = get_base_address (lhs);
3305   if (lhs_base == NULL_TREE
3306       || (!DECL_P (lhs_base) && TREE_CODE (lhs_base) != MEM_REF))
3307     return false;
3308 
3309   then_rhs = gimple_assign_rhs1 (then_assign);
3310   else_rhs = gimple_assign_rhs1 (else_assign);
3311   then_locus = gimple_location (then_assign);
3312   else_locus = gimple_location (else_assign);
3313 
3314   /* Now we've checked the constraints, so do the transformation:
3315      1) Remove the stores.  */
3316   gsi = gsi_for_stmt (then_assign);
3317   unlink_stmt_vdef (then_assign);
3318   gsi_remove (&gsi, true);
3319   release_defs (then_assign);
3320 
3321   gsi = gsi_for_stmt (else_assign);
3322   unlink_stmt_vdef (else_assign);
3323   gsi_remove (&gsi, true);
3324   release_defs (else_assign);
3325 
3326   /* 2) Create a PHI node at the join block, with one argument
3327 	holding the old RHS, and the other holding the temporary
3328 	where we stored the old memory contents.  */
3329   name = make_temp_ssa_name (TREE_TYPE (lhs), NULL, "cstore");
3330   newphi = create_phi_node (name, join_bb);
3331   add_phi_arg (newphi, then_rhs, EDGE_SUCC (then_bb, 0), then_locus);
3332   add_phi_arg (newphi, else_rhs, EDGE_SUCC (else_bb, 0), else_locus);
3333 
3334   new_stmt = gimple_build_assign (lhs, PHI_RESULT (newphi));
3335 
3336   /* 3) Insert that PHI node.  */
3337   gsi = gsi_after_labels (join_bb);
3338   if (gsi_end_p (gsi))
3339     {
3340       gsi = gsi_last_bb (join_bb);
3341       gsi_insert_after (&gsi, new_stmt, GSI_NEW_STMT);
3342     }
3343   else
3344     gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
3345 
3346   statistics_counter_event (cfun, "if-then-else store replacement", 1);
3347 
3348   return true;
3349 }
3350 
3351 /* Return the single store in BB with VDEF or NULL if there are
3352    other stores in the BB or loads following the store.  */
3353 
3354 static gimple *
single_trailing_store_in_bb(basic_block bb,tree vdef)3355 single_trailing_store_in_bb (basic_block bb, tree vdef)
3356 {
3357   if (SSA_NAME_IS_DEFAULT_DEF (vdef))
3358     return NULL;
3359   gimple *store = SSA_NAME_DEF_STMT (vdef);
3360   if (gimple_bb (store) != bb
3361       || gimple_code (store) == GIMPLE_PHI)
3362     return NULL;
3363 
3364   /* Verify there is no other store in this BB.  */
3365   if (!SSA_NAME_IS_DEFAULT_DEF (gimple_vuse (store))
3366       && gimple_bb (SSA_NAME_DEF_STMT (gimple_vuse (store))) == bb
3367       && gimple_code (SSA_NAME_DEF_STMT (gimple_vuse (store))) != GIMPLE_PHI)
3368     return NULL;
3369 
3370   /* Verify there is no load or store after the store.  */
3371   use_operand_p use_p;
3372   imm_use_iterator imm_iter;
3373   FOR_EACH_IMM_USE_FAST (use_p, imm_iter, gimple_vdef (store))
3374     if (USE_STMT (use_p) != store
3375 	&& gimple_bb (USE_STMT (use_p)) == bb)
3376       return NULL;
3377 
3378   return store;
3379 }
3380 
3381 /* Conditional store replacement.  We already know
3382    that the recognized pattern looks like so:
3383 
3384    split:
3385      if (cond) goto THEN_BB; else goto ELSE_BB (edge E1)
3386    THEN_BB:
3387      ...
3388      X = Y;
3389      ...
3390      goto JOIN_BB;
3391    ELSE_BB:
3392      ...
3393      X = Z;
3394      ...
3395      fallthrough (edge E0)
3396    JOIN_BB:
3397      some more
3398 
3399    We check that it is safe to sink the store to JOIN_BB by verifying that
3400    there are no read-after-write or write-after-write dependencies in
3401    THEN_BB and ELSE_BB.  */
3402 
3403 static bool
cond_if_else_store_replacement(basic_block then_bb,basic_block else_bb,basic_block join_bb)3404 cond_if_else_store_replacement (basic_block then_bb, basic_block else_bb,
3405                                 basic_block join_bb)
3406 {
3407   vec<data_reference_p> then_datarefs, else_datarefs;
3408   vec<ddr_p> then_ddrs, else_ddrs;
3409   gimple *then_store, *else_store;
3410   bool found, ok = false, res;
3411   struct data_dependence_relation *ddr;
3412   data_reference_p then_dr, else_dr;
3413   int i, j;
3414   tree then_lhs, else_lhs;
3415   basic_block blocks[3];
3416 
3417   /* Handle the case with single store in THEN_BB and ELSE_BB.  That is
3418      cheap enough to always handle as it allows us to elide dependence
3419      checking.  */
3420   gphi *vphi = NULL;
3421   for (gphi_iterator si = gsi_start_phis (join_bb); !gsi_end_p (si);
3422        gsi_next (&si))
3423     if (virtual_operand_p (gimple_phi_result (si.phi ())))
3424       {
3425 	vphi = si.phi ();
3426 	break;
3427       }
3428   if (!vphi)
3429     return false;
3430   tree then_vdef = PHI_ARG_DEF_FROM_EDGE (vphi, single_succ_edge (then_bb));
3431   tree else_vdef = PHI_ARG_DEF_FROM_EDGE (vphi, single_succ_edge (else_bb));
3432   gimple *then_assign = single_trailing_store_in_bb (then_bb, then_vdef);
3433   if (then_assign)
3434     {
3435       gimple *else_assign = single_trailing_store_in_bb (else_bb, else_vdef);
3436       if (else_assign)
3437 	return cond_if_else_store_replacement_1 (then_bb, else_bb, join_bb,
3438 						 then_assign, else_assign);
3439     }
3440 
3441   /* If either vectorization or if-conversion is disabled then do
3442      not sink any stores.  */
3443   if (param_max_stores_to_sink == 0
3444       || (!flag_tree_loop_vectorize && !flag_tree_slp_vectorize)
3445       || !flag_tree_loop_if_convert)
3446     return false;
3447 
3448   /* Find data references.  */
3449   then_datarefs.create (1);
3450   else_datarefs.create (1);
3451   if ((find_data_references_in_bb (NULL, then_bb, &then_datarefs)
3452         == chrec_dont_know)
3453       || !then_datarefs.length ()
3454       || (find_data_references_in_bb (NULL, else_bb, &else_datarefs)
3455 	  == chrec_dont_know)
3456       || !else_datarefs.length ())
3457     {
3458       free_data_refs (then_datarefs);
3459       free_data_refs (else_datarefs);
3460       return false;
3461     }
3462 
3463   /* Find pairs of stores with equal LHS.  */
3464   auto_vec<gimple *, 1> then_stores, else_stores;
3465   FOR_EACH_VEC_ELT (then_datarefs, i, then_dr)
3466     {
3467       if (DR_IS_READ (then_dr))
3468         continue;
3469 
3470       then_store = DR_STMT (then_dr);
3471       then_lhs = gimple_get_lhs (then_store);
3472       if (then_lhs == NULL_TREE)
3473 	continue;
3474       found = false;
3475 
3476       FOR_EACH_VEC_ELT (else_datarefs, j, else_dr)
3477         {
3478           if (DR_IS_READ (else_dr))
3479             continue;
3480 
3481           else_store = DR_STMT (else_dr);
3482           else_lhs = gimple_get_lhs (else_store);
3483 	  if (else_lhs == NULL_TREE)
3484 	    continue;
3485 
3486           if (operand_equal_p (then_lhs, else_lhs, 0))
3487             {
3488               found = true;
3489               break;
3490             }
3491         }
3492 
3493       if (!found)
3494         continue;
3495 
3496       then_stores.safe_push (then_store);
3497       else_stores.safe_push (else_store);
3498     }
3499 
3500   /* No pairs of stores found.  */
3501   if (!then_stores.length ()
3502       || then_stores.length () > (unsigned) param_max_stores_to_sink)
3503     {
3504       free_data_refs (then_datarefs);
3505       free_data_refs (else_datarefs);
3506       return false;
3507     }
3508 
3509   /* Compute and check data dependencies in both basic blocks.  */
3510   then_ddrs.create (1);
3511   else_ddrs.create (1);
3512   if (!compute_all_dependences (then_datarefs, &then_ddrs,
3513 				vNULL, false)
3514       || !compute_all_dependences (else_datarefs, &else_ddrs,
3515 				   vNULL, false))
3516     {
3517       free_dependence_relations (then_ddrs);
3518       free_dependence_relations (else_ddrs);
3519       free_data_refs (then_datarefs);
3520       free_data_refs (else_datarefs);
3521       return false;
3522     }
3523   blocks[0] = then_bb;
3524   blocks[1] = else_bb;
3525   blocks[2] = join_bb;
3526   renumber_gimple_stmt_uids_in_blocks (blocks, 3);
3527 
3528   /* Check that there are no read-after-write or write-after-write dependencies
3529      in THEN_BB.  */
3530   FOR_EACH_VEC_ELT (then_ddrs, i, ddr)
3531     {
3532       struct data_reference *dra = DDR_A (ddr);
3533       struct data_reference *drb = DDR_B (ddr);
3534 
3535       if (DDR_ARE_DEPENDENT (ddr) != chrec_known
3536           && ((DR_IS_READ (dra) && DR_IS_WRITE (drb)
3537                && gimple_uid (DR_STMT (dra)) > gimple_uid (DR_STMT (drb)))
3538               || (DR_IS_READ (drb) && DR_IS_WRITE (dra)
3539                   && gimple_uid (DR_STMT (drb)) > gimple_uid (DR_STMT (dra)))
3540               || (DR_IS_WRITE (dra) && DR_IS_WRITE (drb))))
3541         {
3542           free_dependence_relations (then_ddrs);
3543           free_dependence_relations (else_ddrs);
3544 	  free_data_refs (then_datarefs);
3545 	  free_data_refs (else_datarefs);
3546           return false;
3547         }
3548     }
3549 
3550   /* Check that there are no read-after-write or write-after-write dependencies
3551      in ELSE_BB.  */
3552   FOR_EACH_VEC_ELT (else_ddrs, i, ddr)
3553     {
3554       struct data_reference *dra = DDR_A (ddr);
3555       struct data_reference *drb = DDR_B (ddr);
3556 
3557       if (DDR_ARE_DEPENDENT (ddr) != chrec_known
3558           && ((DR_IS_READ (dra) && DR_IS_WRITE (drb)
3559                && gimple_uid (DR_STMT (dra)) > gimple_uid (DR_STMT (drb)))
3560               || (DR_IS_READ (drb) && DR_IS_WRITE (dra)
3561                   && gimple_uid (DR_STMT (drb)) > gimple_uid (DR_STMT (dra)))
3562               || (DR_IS_WRITE (dra) && DR_IS_WRITE (drb))))
3563         {
3564           free_dependence_relations (then_ddrs);
3565           free_dependence_relations (else_ddrs);
3566 	  free_data_refs (then_datarefs);
3567 	  free_data_refs (else_datarefs);
3568           return false;
3569         }
3570     }
3571 
3572   /* Sink stores with same LHS.  */
3573   FOR_EACH_VEC_ELT (then_stores, i, then_store)
3574     {
3575       else_store = else_stores[i];
3576       res = cond_if_else_store_replacement_1 (then_bb, else_bb, join_bb,
3577                                               then_store, else_store);
3578       ok = ok || res;
3579     }
3580 
3581   free_dependence_relations (then_ddrs);
3582   free_dependence_relations (else_ddrs);
3583   free_data_refs (then_datarefs);
3584   free_data_refs (else_datarefs);
3585 
3586   return ok;
3587 }
3588 
3589 /* Return TRUE if STMT has a VUSE whose corresponding VDEF is in BB.  */
3590 
3591 static bool
local_mem_dependence(gimple * stmt,basic_block bb)3592 local_mem_dependence (gimple *stmt, basic_block bb)
3593 {
3594   tree vuse = gimple_vuse (stmt);
3595   gimple *def;
3596 
3597   if (!vuse)
3598     return false;
3599 
3600   def = SSA_NAME_DEF_STMT (vuse);
3601   return (def && gimple_bb (def) == bb);
3602 }
3603 
3604 /* Given a "diamond" control-flow pattern where BB0 tests a condition,
3605    BB1 and BB2 are "then" and "else" blocks dependent on this test,
3606    and BB3 rejoins control flow following BB1 and BB2, look for
3607    opportunities to hoist loads as follows.  If BB3 contains a PHI of
3608    two loads, one each occurring in BB1 and BB2, and the loads are
3609    provably of adjacent fields in the same structure, then move both
3610    loads into BB0.  Of course this can only be done if there are no
3611    dependencies preventing such motion.
3612 
3613    One of the hoisted loads will always be speculative, so the
3614    transformation is currently conservative:
3615 
3616     - The fields must be strictly adjacent.
3617     - The two fields must occupy a single memory block that is
3618       guaranteed to not cross a page boundary.
3619 
3620     The last is difficult to prove, as such memory blocks should be
3621     aligned on the minimum of the stack alignment boundary and the
3622     alignment guaranteed by heap allocation interfaces.  Thus we rely
3623     on a parameter for the alignment value.
3624 
3625     Provided a good value is used for the last case, the first
3626     restriction could possibly be relaxed.  */
3627 
3628 static void
hoist_adjacent_loads(basic_block bb0,basic_block bb1,basic_block bb2,basic_block bb3)3629 hoist_adjacent_loads (basic_block bb0, basic_block bb1,
3630 		      basic_block bb2, basic_block bb3)
3631 {
3632   int param_align = param_l1_cache_line_size;
3633   unsigned param_align_bits = (unsigned) (param_align * BITS_PER_UNIT);
3634   gphi_iterator gsi;
3635 
3636   /* Walk the phis in bb3 looking for an opportunity.  We are looking
3637      for phis of two SSA names, one each of which is defined in bb1 and
3638      bb2.  */
3639   for (gsi = gsi_start_phis (bb3); !gsi_end_p (gsi); gsi_next (&gsi))
3640     {
3641       gphi *phi_stmt = gsi.phi ();
3642       gimple *def1, *def2;
3643       tree arg1, arg2, ref1, ref2, field1, field2;
3644       tree tree_offset1, tree_offset2, tree_size2, next;
3645       int offset1, offset2, size2;
3646       unsigned align1;
3647       gimple_stmt_iterator gsi2;
3648       basic_block bb_for_def1, bb_for_def2;
3649 
3650       if (gimple_phi_num_args (phi_stmt) != 2
3651 	  || virtual_operand_p (gimple_phi_result (phi_stmt)))
3652 	continue;
3653 
3654       arg1 = gimple_phi_arg_def (phi_stmt, 0);
3655       arg2 = gimple_phi_arg_def (phi_stmt, 1);
3656 
3657       if (TREE_CODE (arg1) != SSA_NAME
3658 	  || TREE_CODE (arg2) != SSA_NAME
3659 	  || SSA_NAME_IS_DEFAULT_DEF (arg1)
3660 	  || SSA_NAME_IS_DEFAULT_DEF (arg2))
3661 	continue;
3662 
3663       def1 = SSA_NAME_DEF_STMT (arg1);
3664       def2 = SSA_NAME_DEF_STMT (arg2);
3665 
3666       if ((gimple_bb (def1) != bb1 || gimple_bb (def2) != bb2)
3667 	  && (gimple_bb (def2) != bb1 || gimple_bb (def1) != bb2))
3668 	continue;
3669 
3670       /* Check the mode of the arguments to be sure a conditional move
3671 	 can be generated for it.  */
3672       if (optab_handler (movcc_optab, TYPE_MODE (TREE_TYPE (arg1)))
3673 	  == CODE_FOR_nothing)
3674 	continue;
3675 
3676       /* Both statements must be assignments whose RHS is a COMPONENT_REF.  */
3677       if (!gimple_assign_single_p (def1)
3678 	  || !gimple_assign_single_p (def2)
3679 	  || gimple_has_volatile_ops (def1)
3680 	  || gimple_has_volatile_ops (def2))
3681 	continue;
3682 
3683       ref1 = gimple_assign_rhs1 (def1);
3684       ref2 = gimple_assign_rhs1 (def2);
3685 
3686       if (TREE_CODE (ref1) != COMPONENT_REF
3687 	  || TREE_CODE (ref2) != COMPONENT_REF)
3688 	continue;
3689 
3690       /* The zeroth operand of the two component references must be
3691 	 identical.  It is not sufficient to compare get_base_address of
3692 	 the two references, because this could allow for different
3693 	 elements of the same array in the two trees.  It is not safe to
3694 	 assume that the existence of one array element implies the
3695 	 existence of a different one.  */
3696       if (!operand_equal_p (TREE_OPERAND (ref1, 0), TREE_OPERAND (ref2, 0), 0))
3697 	continue;
3698 
3699       field1 = TREE_OPERAND (ref1, 1);
3700       field2 = TREE_OPERAND (ref2, 1);
3701 
3702       /* Check for field adjacency, and ensure field1 comes first.  */
3703       for (next = DECL_CHAIN (field1);
3704 	   next && TREE_CODE (next) != FIELD_DECL;
3705 	   next = DECL_CHAIN (next))
3706 	;
3707 
3708       if (next != field2)
3709 	{
3710 	  for (next = DECL_CHAIN (field2);
3711 	       next && TREE_CODE (next) != FIELD_DECL;
3712 	       next = DECL_CHAIN (next))
3713 	    ;
3714 
3715 	  if (next != field1)
3716 	    continue;
3717 
3718 	  std::swap (field1, field2);
3719 	  std::swap (def1, def2);
3720 	}
3721 
3722       bb_for_def1 = gimple_bb (def1);
3723       bb_for_def2 = gimple_bb (def2);
3724 
3725       /* Check for proper alignment of the first field.  */
3726       tree_offset1 = bit_position (field1);
3727       tree_offset2 = bit_position (field2);
3728       tree_size2 = DECL_SIZE (field2);
3729 
3730       if (!tree_fits_uhwi_p (tree_offset1)
3731 	  || !tree_fits_uhwi_p (tree_offset2)
3732 	  || !tree_fits_uhwi_p (tree_size2))
3733 	continue;
3734 
3735       offset1 = tree_to_uhwi (tree_offset1);
3736       offset2 = tree_to_uhwi (tree_offset2);
3737       size2 = tree_to_uhwi (tree_size2);
3738       align1 = DECL_ALIGN (field1) % param_align_bits;
3739 
3740       if (offset1 % BITS_PER_UNIT != 0)
3741 	continue;
3742 
3743       /* For profitability, the two field references should fit within
3744 	 a single cache line.  */
3745       if (align1 + offset2 - offset1 + size2 > param_align_bits)
3746 	continue;
3747 
3748       /* The two expressions cannot be dependent upon vdefs defined
3749 	 in bb1/bb2.  */
3750       if (local_mem_dependence (def1, bb_for_def1)
3751 	  || local_mem_dependence (def2, bb_for_def2))
3752 	continue;
3753 
3754       /* The conditions are satisfied; hoist the loads from bb1 and bb2 into
3755 	 bb0.  We hoist the first one first so that a cache miss is handled
3756          efficiently regardless of hardware cache-fill policy.  */
3757       gsi2 = gsi_for_stmt (def1);
3758       gsi_move_to_bb_end (&gsi2, bb0);
3759       gsi2 = gsi_for_stmt (def2);
3760       gsi_move_to_bb_end (&gsi2, bb0);
3761       statistics_counter_event (cfun, "hoisted loads", 1);
3762 
3763       if (dump_file && (dump_flags & TDF_DETAILS))
3764 	{
3765 	  fprintf (dump_file,
3766 		   "\nHoisting adjacent loads from %d and %d into %d: \n",
3767 		   bb_for_def1->index, bb_for_def2->index, bb0->index);
3768 	  print_gimple_stmt (dump_file, def1, 0, TDF_VOPS|TDF_MEMSYMS);
3769 	  print_gimple_stmt (dump_file, def2, 0, TDF_VOPS|TDF_MEMSYMS);
3770 	}
3771     }
3772 }
3773 
3774 /* Determine whether we should attempt to hoist adjacent loads out of
3775    diamond patterns in pass_phiopt.  Always hoist loads if
3776    -fhoist-adjacent-loads is specified and the target machine has
3777    both a conditional move instruction and a defined cache line size.  */
3778 
3779 static bool
gate_hoist_loads(void)3780 gate_hoist_loads (void)
3781 {
3782   return (flag_hoist_adjacent_loads == 1
3783 	  && param_l1_cache_line_size
3784 	  && HAVE_conditional_move);
3785 }
3786 
3787 /* This pass tries to replaces an if-then-else block with an
3788    assignment.  We have four kinds of transformations.  Some of these
3789    transformations are also performed by the ifcvt RTL optimizer.
3790 
3791    Conditional Replacement
3792    -----------------------
3793 
3794    This transformation, implemented in match_simplify_replacement,
3795    replaces
3796 
3797      bb0:
3798       if (cond) goto bb2; else goto bb1;
3799      bb1:
3800      bb2:
3801       x = PHI <0 (bb1), 1 (bb0), ...>;
3802 
3803    with
3804 
3805      bb0:
3806       x' = cond;
3807       goto bb2;
3808      bb2:
3809       x = PHI <x' (bb0), ...>;
3810 
3811    We remove bb1 as it becomes unreachable.  This occurs often due to
3812    gimplification of conditionals.
3813 
3814    Value Replacement
3815    -----------------
3816 
3817    This transformation, implemented in value_replacement, replaces
3818 
3819      bb0:
3820        if (a != b) goto bb2; else goto bb1;
3821      bb1:
3822      bb2:
3823        x = PHI <a (bb1), b (bb0), ...>;
3824 
3825    with
3826 
3827      bb0:
3828      bb2:
3829        x = PHI <b (bb0), ...>;
3830 
3831    This opportunity can sometimes occur as a result of other
3832    optimizations.
3833 
3834 
3835    Another case caught by value replacement looks like this:
3836 
3837      bb0:
3838        t1 = a == CONST;
3839        t2 = b > c;
3840        t3 = t1 & t2;
3841        if (t3 != 0) goto bb1; else goto bb2;
3842      bb1:
3843      bb2:
3844        x = PHI (CONST, a)
3845 
3846    Gets replaced with:
3847      bb0:
3848      bb2:
3849        t1 = a == CONST;
3850        t2 = b > c;
3851        t3 = t1 & t2;
3852        x = a;
3853 
3854    ABS Replacement
3855    ---------------
3856 
3857    This transformation, implemented in match_simplify_replacement, replaces
3858 
3859      bb0:
3860        if (a >= 0) goto bb2; else goto bb1;
3861      bb1:
3862        x = -a;
3863      bb2:
3864        x = PHI <x (bb1), a (bb0), ...>;
3865 
3866    with
3867 
3868      bb0:
3869        x' = ABS_EXPR< a >;
3870      bb2:
3871        x = PHI <x' (bb0), ...>;
3872 
3873    MIN/MAX Replacement
3874    -------------------
3875 
3876    This transformation, minmax_replacement replaces
3877 
3878      bb0:
3879        if (a <= b) goto bb2; else goto bb1;
3880      bb1:
3881      bb2:
3882        x = PHI <b (bb1), a (bb0), ...>;
3883 
3884    with
3885 
3886      bb0:
3887        x' = MIN_EXPR (a, b)
3888      bb2:
3889        x = PHI <x' (bb0), ...>;
3890 
3891    A similar transformation is done for MAX_EXPR.
3892 
3893 
3894    This pass also performs a fifth transformation of a slightly different
3895    flavor.
3896 
3897    Factor conversion in COND_EXPR
3898    ------------------------------
3899 
3900    This transformation factors the conversion out of COND_EXPR with
3901    factor_out_conditional_conversion.
3902 
3903    For example:
3904    if (a <= CST) goto <bb 3>; else goto <bb 4>;
3905    <bb 3>:
3906    tmp = (int) a;
3907    <bb 4>:
3908    tmp = PHI <tmp, CST>
3909 
3910    Into:
3911    if (a <= CST) goto <bb 3>; else goto <bb 4>;
3912    <bb 3>:
3913    <bb 4>:
3914    a = PHI <a, CST>
3915    tmp = (int) a;
3916 
3917    Adjacent Load Hoisting
3918    ----------------------
3919 
3920    This transformation replaces
3921 
3922      bb0:
3923        if (...) goto bb2; else goto bb1;
3924      bb1:
3925        x1 = (<expr>).field1;
3926        goto bb3;
3927      bb2:
3928        x2 = (<expr>).field2;
3929      bb3:
3930        # x = PHI <x1, x2>;
3931 
3932    with
3933 
3934      bb0:
3935        x1 = (<expr>).field1;
3936        x2 = (<expr>).field2;
3937        if (...) goto bb2; else goto bb1;
3938      bb1:
3939        goto bb3;
3940      bb2:
3941      bb3:
3942        # x = PHI <x1, x2>;
3943 
3944    The purpose of this transformation is to enable generation of conditional
3945    move instructions such as Intel CMOVE or PowerPC ISEL.  Because one of
3946    the loads is speculative, the transformation is restricted to very
3947    specific cases to avoid introducing a page fault.  We are looking for
3948    the common idiom:
3949 
3950      if (...)
3951        x = y->left;
3952      else
3953        x = y->right;
3954 
3955    where left and right are typically adjacent pointers in a tree structure.  */
3956 
3957 namespace {
3958 
3959 const pass_data pass_data_phiopt =
3960 {
3961   GIMPLE_PASS, /* type */
3962   "phiopt", /* name */
3963   OPTGROUP_NONE, /* optinfo_flags */
3964   TV_TREE_PHIOPT, /* tv_id */
3965   ( PROP_cfg | PROP_ssa ), /* properties_required */
3966   0, /* properties_provided */
3967   0, /* properties_destroyed */
3968   0, /* todo_flags_start */
3969   0, /* todo_flags_finish */
3970 };
3971 
3972 class pass_phiopt : public gimple_opt_pass
3973 {
3974 public:
pass_phiopt(gcc::context * ctxt)3975   pass_phiopt (gcc::context *ctxt)
3976     : gimple_opt_pass (pass_data_phiopt, ctxt), early_p (false)
3977   {}
3978 
3979   /* opt_pass methods: */
clone()3980   opt_pass * clone () { return new pass_phiopt (m_ctxt); }
set_pass_param(unsigned n,bool param)3981   void set_pass_param (unsigned n, bool param)
3982     {
3983       gcc_assert (n == 0);
3984       early_p = param;
3985     }
gate(function *)3986   virtual bool gate (function *) { return flag_ssa_phiopt; }
execute(function *)3987   virtual unsigned int execute (function *)
3988     {
3989       return tree_ssa_phiopt_worker (false,
3990 				     !early_p ? gate_hoist_loads () : false,
3991 				     early_p);
3992     }
3993 
3994 private:
3995   bool early_p;
3996 }; // class pass_phiopt
3997 
3998 } // anon namespace
3999 
4000 gimple_opt_pass *
make_pass_phiopt(gcc::context * ctxt)4001 make_pass_phiopt (gcc::context *ctxt)
4002 {
4003   return new pass_phiopt (ctxt);
4004 }
4005 
4006 namespace {
4007 
4008 const pass_data pass_data_cselim =
4009 {
4010   GIMPLE_PASS, /* type */
4011   "cselim", /* name */
4012   OPTGROUP_NONE, /* optinfo_flags */
4013   TV_TREE_PHIOPT, /* tv_id */
4014   ( PROP_cfg | PROP_ssa ), /* properties_required */
4015   0, /* properties_provided */
4016   0, /* properties_destroyed */
4017   0, /* todo_flags_start */
4018   0, /* todo_flags_finish */
4019 };
4020 
4021 class pass_cselim : public gimple_opt_pass
4022 {
4023 public:
pass_cselim(gcc::context * ctxt)4024   pass_cselim (gcc::context *ctxt)
4025     : gimple_opt_pass (pass_data_cselim, ctxt)
4026   {}
4027 
4028   /* opt_pass methods: */
gate(function *)4029   virtual bool gate (function *) { return flag_tree_cselim; }
execute(function *)4030   virtual unsigned int execute (function *) { return tree_ssa_cs_elim (); }
4031 
4032 }; // class pass_cselim
4033 
4034 } // anon namespace
4035 
4036 gimple_opt_pass *
make_pass_cselim(gcc::context * ctxt)4037 make_pass_cselim (gcc::context *ctxt)
4038 {
4039   return new pass_cselim (ctxt);
4040 }
4041