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