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