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