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