Merge branch 'master' into python
[official-gcc.git] / gcc / tree-ssa-phiopt.c
blobd40aeb2275c13d9a3871db51f64f04bc0f2f0054
1 /* Optimization of PHI nodes by converting them into straightline code.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation,
3 Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
10 later version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "ggc.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "tm_p.h"
29 #include "basic-block.h"
30 #include "timevar.h"
31 #include "diagnostic.h"
32 #include "tree-flow.h"
33 #include "tree-pass.h"
34 #include "tree-dump.h"
35 #include "langhooks.h"
36 #include "pointer-set.h"
37 #include "domwalk.h"
39 static unsigned int tree_ssa_phiopt (void);
40 static unsigned int tree_ssa_phiopt_worker (bool);
41 static bool conditional_replacement (basic_block, basic_block,
42 edge, edge, gimple, tree, tree);
43 static bool value_replacement (basic_block, basic_block,
44 edge, edge, gimple, tree, tree);
45 static bool minmax_replacement (basic_block, basic_block,
46 edge, edge, gimple, tree, tree);
47 static bool abs_replacement (basic_block, basic_block,
48 edge, edge, gimple, tree, tree);
49 static bool cond_store_replacement (basic_block, basic_block, edge, edge,
50 struct pointer_set_t *);
51 static struct pointer_set_t * get_non_trapping (void);
52 static void replace_phi_edge_with_variable (basic_block, edge, gimple, tree);
54 /* This pass tries to replaces an if-then-else block with an
55 assignment. We have four kinds of transformations. Some of these
56 transformations are also performed by the ifcvt RTL optimizer.
58 Conditional Replacement
59 -----------------------
61 This transformation, implemented in conditional_replacement,
62 replaces
64 bb0:
65 if (cond) goto bb2; else goto bb1;
66 bb1:
67 bb2:
68 x = PHI <0 (bb1), 1 (bb0), ...>;
70 with
72 bb0:
73 x' = cond;
74 goto bb2;
75 bb2:
76 x = PHI <x' (bb0), ...>;
78 We remove bb1 as it becomes unreachable. This occurs often due to
79 gimplification of conditionals.
81 Value Replacement
82 -----------------
84 This transformation, implemented in value_replacement, replaces
86 bb0:
87 if (a != b) goto bb2; else goto bb1;
88 bb1:
89 bb2:
90 x = PHI <a (bb1), b (bb0), ...>;
92 with
94 bb0:
95 bb2:
96 x = PHI <b (bb0), ...>;
98 This opportunity can sometimes occur as a result of other
99 optimizations.
101 ABS Replacement
102 ---------------
104 This transformation, implemented in abs_replacement, replaces
106 bb0:
107 if (a >= 0) goto bb2; else goto bb1;
108 bb1:
109 x = -a;
110 bb2:
111 x = PHI <x (bb1), a (bb0), ...>;
113 with
115 bb0:
116 x' = ABS_EXPR< a >;
117 bb2:
118 x = PHI <x' (bb0), ...>;
120 MIN/MAX Replacement
121 -------------------
123 This transformation, minmax_replacement replaces
125 bb0:
126 if (a <= b) goto bb2; else goto bb1;
127 bb1:
128 bb2:
129 x = PHI <b (bb1), a (bb0), ...>;
131 with
133 bb0:
134 x' = MIN_EXPR (a, b)
135 bb2:
136 x = PHI <x' (bb0), ...>;
138 A similar transformation is done for MAX_EXPR. */
140 static unsigned int
141 tree_ssa_phiopt (void)
143 return tree_ssa_phiopt_worker (false);
146 /* This pass tries to transform conditional stores into unconditional
147 ones, enabling further simplifications with the simpler then and else
148 blocks. In particular it replaces this:
150 bb0:
151 if (cond) goto bb2; else goto bb1;
152 bb1:
153 *p = RHS
154 bb2:
156 with
158 bb0:
159 if (cond) goto bb1; else goto bb2;
160 bb1:
161 condtmp' = *p;
162 bb2:
163 condtmp = PHI <RHS, condtmp'>
164 *p = condtmp
166 This transformation can only be done under several constraints,
167 documented below. */
169 static unsigned int
170 tree_ssa_cs_elim (void)
172 return tree_ssa_phiopt_worker (true);
175 /* For conditional store replacement we need a temporary to
176 put the old contents of the memory in. */
177 static tree condstoretemp;
179 /* The core routine of conditional store replacement and normal
180 phi optimizations. Both share much of the infrastructure in how
181 to match applicable basic block patterns. DO_STORE_ELIM is true
182 when we want to do conditional store replacement, false otherwise. */
183 static unsigned int
184 tree_ssa_phiopt_worker (bool do_store_elim)
186 basic_block bb;
187 basic_block *bb_order;
188 unsigned n, i;
189 bool cfgchanged = false;
190 struct pointer_set_t *nontrap = 0;
192 if (do_store_elim)
194 condstoretemp = NULL_TREE;
195 /* Calculate the set of non-trapping memory accesses. */
196 nontrap = get_non_trapping ();
199 /* Search every basic block for COND_EXPR we may be able to optimize.
201 We walk the blocks in order that guarantees that a block with
202 a single predecessor is processed before the predecessor.
203 This ensures that we collapse inner ifs before visiting the
204 outer ones, and also that we do not try to visit a removed
205 block. */
206 bb_order = blocks_in_phiopt_order ();
207 n = n_basic_blocks - NUM_FIXED_BLOCKS;
209 for (i = 0; i < n; i++)
211 gimple cond_stmt, phi;
212 basic_block bb1, bb2;
213 edge e1, e2;
214 tree arg0, arg1;
216 bb = bb_order[i];
218 cond_stmt = last_stmt (bb);
219 /* Check to see if the last statement is a GIMPLE_COND. */
220 if (!cond_stmt
221 || gimple_code (cond_stmt) != GIMPLE_COND)
222 continue;
224 e1 = EDGE_SUCC (bb, 0);
225 bb1 = e1->dest;
226 e2 = EDGE_SUCC (bb, 1);
227 bb2 = e2->dest;
229 /* We cannot do the optimization on abnormal edges. */
230 if ((e1->flags & EDGE_ABNORMAL) != 0
231 || (e2->flags & EDGE_ABNORMAL) != 0)
232 continue;
234 /* If either bb1's succ or bb2 or bb2's succ is non NULL. */
235 if (EDGE_COUNT (bb1->succs) == 0
236 || bb2 == NULL
237 || EDGE_COUNT (bb2->succs) == 0)
238 continue;
240 /* Find the bb which is the fall through to the other. */
241 if (EDGE_SUCC (bb1, 0)->dest == bb2)
243 else if (EDGE_SUCC (bb2, 0)->dest == bb1)
245 basic_block bb_tmp = bb1;
246 edge e_tmp = e1;
247 bb1 = bb2;
248 bb2 = bb_tmp;
249 e1 = e2;
250 e2 = e_tmp;
252 else
253 continue;
255 e1 = EDGE_SUCC (bb1, 0);
257 /* Make sure that bb1 is just a fall through. */
258 if (!single_succ_p (bb1)
259 || (e1->flags & EDGE_FALLTHRU) == 0)
260 continue;
262 /* Also make sure that bb1 only have one predecessor and that it
263 is bb. */
264 if (!single_pred_p (bb1)
265 || single_pred (bb1) != bb)
266 continue;
268 if (do_store_elim)
270 /* bb1 is the middle block, bb2 the join block, bb the split block,
271 e1 the fallthrough edge from bb1 to bb2. We can't do the
272 optimization if the join block has more than two predecessors. */
273 if (EDGE_COUNT (bb2->preds) > 2)
274 continue;
275 if (cond_store_replacement (bb1, bb2, e1, e2, nontrap))
276 cfgchanged = true;
278 else
280 gimple_seq phis = phi_nodes (bb2);
282 /* Check to make sure that there is only one PHI node.
283 TODO: we could do it with more than one iff the other PHI nodes
284 have the same elements for these two edges. */
285 if (! gimple_seq_singleton_p (phis))
286 continue;
288 phi = gsi_stmt (gsi_start (phis));
289 arg0 = gimple_phi_arg_def (phi, e1->dest_idx);
290 arg1 = gimple_phi_arg_def (phi, e2->dest_idx);
292 /* Something is wrong if we cannot find the arguments in the PHI
293 node. */
294 gcc_assert (arg0 != NULL && arg1 != NULL);
296 /* Do the replacement of conditional if it can be done. */
297 if (conditional_replacement (bb, bb1, e1, e2, phi, arg0, arg1))
298 cfgchanged = true;
299 else if (value_replacement (bb, bb1, e1, e2, phi, arg0, arg1))
300 cfgchanged = true;
301 else if (abs_replacement (bb, bb1, e1, e2, phi, arg0, arg1))
302 cfgchanged = true;
303 else if (minmax_replacement (bb, bb1, e1, e2, phi, arg0, arg1))
304 cfgchanged = true;
308 free (bb_order);
310 if (do_store_elim)
311 pointer_set_destroy (nontrap);
312 /* If the CFG has changed, we should cleanup the CFG. */
313 if (cfgchanged && do_store_elim)
315 /* In cond-store replacement we have added some loads on edges
316 and new VOPS (as we moved the store, and created a load). */
317 gsi_commit_edge_inserts ();
318 return TODO_cleanup_cfg | TODO_update_ssa_only_virtuals;
320 else if (cfgchanged)
321 return TODO_cleanup_cfg;
322 return 0;
325 /* Returns the list of basic blocks in the function in an order that guarantees
326 that if a block X has just a single predecessor Y, then Y is after X in the
327 ordering. */
329 basic_block *
330 blocks_in_phiopt_order (void)
332 basic_block x, y;
333 basic_block *order = XNEWVEC (basic_block, n_basic_blocks);
334 unsigned n = n_basic_blocks - NUM_FIXED_BLOCKS;
335 unsigned np, i;
336 sbitmap visited = sbitmap_alloc (last_basic_block);
338 #define MARK_VISITED(BB) (SET_BIT (visited, (BB)->index))
339 #define VISITED_P(BB) (TEST_BIT (visited, (BB)->index))
341 sbitmap_zero (visited);
343 MARK_VISITED (ENTRY_BLOCK_PTR);
344 FOR_EACH_BB (x)
346 if (VISITED_P (x))
347 continue;
349 /* Walk the predecessors of x as long as they have precisely one
350 predecessor and add them to the list, so that they get stored
351 after x. */
352 for (y = x, np = 1;
353 single_pred_p (y) && !VISITED_P (single_pred (y));
354 y = single_pred (y))
355 np++;
356 for (y = x, i = n - np;
357 single_pred_p (y) && !VISITED_P (single_pred (y));
358 y = single_pred (y), i++)
360 order[i] = y;
361 MARK_VISITED (y);
363 order[i] = y;
364 MARK_VISITED (y);
366 gcc_assert (i == n - 1);
367 n -= np;
370 sbitmap_free (visited);
371 gcc_assert (n == 0);
372 return order;
374 #undef MARK_VISITED
375 #undef VISITED_P
379 /* Return TRUE if block BB has no executable statements, otherwise return
380 FALSE. */
382 bool
383 empty_block_p (basic_block bb)
385 /* BB must have no executable statements. */
386 gimple_stmt_iterator gsi = gsi_after_labels (bb);
387 if (gsi_end_p (gsi))
388 return true;
389 if (is_gimple_debug (gsi_stmt (gsi)))
390 gsi_next_nondebug (&gsi);
391 return gsi_end_p (gsi);
394 /* Replace PHI node element whose edge is E in block BB with variable NEW.
395 Remove the edge from COND_BLOCK which does not lead to BB (COND_BLOCK
396 is known to have two edges, one of which must reach BB). */
398 static void
399 replace_phi_edge_with_variable (basic_block cond_block,
400 edge e, gimple phi, tree new_tree)
402 basic_block bb = gimple_bb (phi);
403 basic_block block_to_remove;
404 gimple_stmt_iterator gsi;
406 /* Change the PHI argument to new. */
407 SET_USE (PHI_ARG_DEF_PTR (phi, e->dest_idx), new_tree);
409 /* Remove the empty basic block. */
410 if (EDGE_SUCC (cond_block, 0)->dest == bb)
412 EDGE_SUCC (cond_block, 0)->flags |= EDGE_FALLTHRU;
413 EDGE_SUCC (cond_block, 0)->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
414 EDGE_SUCC (cond_block, 0)->probability = REG_BR_PROB_BASE;
415 EDGE_SUCC (cond_block, 0)->count += EDGE_SUCC (cond_block, 1)->count;
417 block_to_remove = EDGE_SUCC (cond_block, 1)->dest;
419 else
421 EDGE_SUCC (cond_block, 1)->flags |= EDGE_FALLTHRU;
422 EDGE_SUCC (cond_block, 1)->flags
423 &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
424 EDGE_SUCC (cond_block, 1)->probability = REG_BR_PROB_BASE;
425 EDGE_SUCC (cond_block, 1)->count += EDGE_SUCC (cond_block, 0)->count;
427 block_to_remove = EDGE_SUCC (cond_block, 0)->dest;
429 delete_basic_block (block_to_remove);
431 /* Eliminate the COND_EXPR at the end of COND_BLOCK. */
432 gsi = gsi_last_bb (cond_block);
433 gsi_remove (&gsi, true);
435 if (dump_file && (dump_flags & TDF_DETAILS))
436 fprintf (dump_file,
437 "COND_EXPR in block %d and PHI in block %d converted to straightline code.\n",
438 cond_block->index,
439 bb->index);
442 /* The function conditional_replacement does the main work of doing the
443 conditional replacement. Return true if the replacement is done.
444 Otherwise return false.
445 BB is the basic block where the replacement is going to be done on. ARG0
446 is argument 0 from PHI. Likewise for ARG1. */
448 static bool
449 conditional_replacement (basic_block cond_bb, basic_block middle_bb,
450 edge e0, edge e1, gimple phi,
451 tree arg0, tree arg1)
453 tree result;
454 gimple stmt, new_stmt;
455 tree cond;
456 gimple_stmt_iterator gsi;
457 edge true_edge, false_edge;
458 tree new_var, new_var2;
460 /* FIXME: Gimplification of complex type is too hard for now. */
461 if (TREE_CODE (TREE_TYPE (arg0)) == COMPLEX_TYPE
462 || TREE_CODE (TREE_TYPE (arg1)) == COMPLEX_TYPE)
463 return false;
465 /* The PHI arguments have the constants 0 and 1, then convert
466 it to the conditional. */
467 if ((integer_zerop (arg0) && integer_onep (arg1))
468 || (integer_zerop (arg1) && integer_onep (arg0)))
470 else
471 return false;
473 if (!empty_block_p (middle_bb))
474 return false;
476 /* At this point we know we have a GIMPLE_COND with two successors.
477 One successor is BB, the other successor is an empty block which
478 falls through into BB.
480 There is a single PHI node at the join point (BB) and its arguments
481 are constants (0, 1).
483 So, given the condition COND, and the two PHI arguments, we can
484 rewrite this PHI into non-branching code:
486 dest = (COND) or dest = COND'
488 We use the condition as-is if the argument associated with the
489 true edge has the value one or the argument associated with the
490 false edge as the value zero. Note that those conditions are not
491 the same since only one of the outgoing edges from the GIMPLE_COND
492 will directly reach BB and thus be associated with an argument. */
494 stmt = last_stmt (cond_bb);
495 result = PHI_RESULT (phi);
497 /* To handle special cases like floating point comparison, it is easier and
498 less error-prone to build a tree and gimplify it on the fly though it is
499 less efficient. */
500 cond = fold_build2 (gimple_cond_code (stmt), boolean_type_node,
501 gimple_cond_lhs (stmt), gimple_cond_rhs (stmt));
503 /* We need to know which is the true edge and which is the false
504 edge so that we know when to invert the condition below. */
505 extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
506 if ((e0 == true_edge && integer_zerop (arg0))
507 || (e0 == false_edge && integer_onep (arg0))
508 || (e1 == true_edge && integer_zerop (arg1))
509 || (e1 == false_edge && integer_onep (arg1)))
510 cond = fold_build1 (TRUTH_NOT_EXPR, TREE_TYPE (cond), cond);
512 /* Insert our new statements at the end of conditional block before the
513 COND_STMT. */
514 gsi = gsi_for_stmt (stmt);
515 new_var = force_gimple_operand_gsi (&gsi, cond, true, NULL, true,
516 GSI_SAME_STMT);
518 if (!useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (new_var)))
520 source_location locus_0, locus_1;
522 new_var2 = create_tmp_var (TREE_TYPE (result), NULL);
523 add_referenced_var (new_var2);
524 new_stmt = gimple_build_assign_with_ops (CONVERT_EXPR, new_var2,
525 new_var, NULL);
526 new_var2 = make_ssa_name (new_var2, new_stmt);
527 gimple_assign_set_lhs (new_stmt, new_var2);
528 gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT);
529 new_var = new_var2;
531 /* Set the locus to the first argument, unless is doesn't have one. */
532 locus_0 = gimple_phi_arg_location (phi, 0);
533 locus_1 = gimple_phi_arg_location (phi, 1);
534 if (locus_0 == UNKNOWN_LOCATION)
535 locus_0 = locus_1;
536 gimple_set_location (new_stmt, locus_0);
539 replace_phi_edge_with_variable (cond_bb, e1, phi, new_var);
541 /* Note that we optimized this PHI. */
542 return true;
545 /* The function value_replacement does the main work of doing the value
546 replacement. Return true if the replacement is done. Otherwise return
547 false.
548 BB is the basic block where the replacement is going to be done on. ARG0
549 is argument 0 from the PHI. Likewise for ARG1. */
551 static bool
552 value_replacement (basic_block cond_bb, basic_block middle_bb,
553 edge e0, edge e1, gimple phi,
554 tree arg0, tree arg1)
556 gimple cond;
557 edge true_edge, false_edge;
558 enum tree_code code;
560 /* If the type says honor signed zeros we cannot do this
561 optimization. */
562 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
563 return false;
565 if (!empty_block_p (middle_bb))
566 return false;
568 cond = last_stmt (cond_bb);
569 code = gimple_cond_code (cond);
571 /* This transformation is only valid for equality comparisons. */
572 if (code != NE_EXPR && code != EQ_EXPR)
573 return false;
575 /* We need to know which is the true edge and which is the false
576 edge so that we know if have abs or negative abs. */
577 extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
579 /* At this point we know we have a COND_EXPR with two successors.
580 One successor is BB, the other successor is an empty block which
581 falls through into BB.
583 The condition for the COND_EXPR is known to be NE_EXPR or EQ_EXPR.
585 There is a single PHI node at the join point (BB) with two arguments.
587 We now need to verify that the two arguments in the PHI node match
588 the two arguments to the equality comparison. */
590 if ((operand_equal_for_phi_arg_p (arg0, gimple_cond_lhs (cond))
591 && operand_equal_for_phi_arg_p (arg1, gimple_cond_rhs (cond)))
592 || (operand_equal_for_phi_arg_p (arg1, gimple_cond_lhs (cond))
593 && operand_equal_for_phi_arg_p (arg0, gimple_cond_rhs (cond))))
595 edge e;
596 tree arg;
598 /* For NE_EXPR, we want to build an assignment result = arg where
599 arg is the PHI argument associated with the true edge. For
600 EQ_EXPR we want the PHI argument associated with the false edge. */
601 e = (code == NE_EXPR ? true_edge : false_edge);
603 /* Unfortunately, E may not reach BB (it may instead have gone to
604 OTHER_BLOCK). If that is the case, then we want the single outgoing
605 edge from OTHER_BLOCK which reaches BB and represents the desired
606 path from COND_BLOCK. */
607 if (e->dest == middle_bb)
608 e = single_succ_edge (e->dest);
610 /* Now we know the incoming edge to BB that has the argument for the
611 RHS of our new assignment statement. */
612 if (e0 == e)
613 arg = arg0;
614 else
615 arg = arg1;
617 replace_phi_edge_with_variable (cond_bb, e1, phi, arg);
619 /* Note that we optimized this PHI. */
620 return true;
622 return false;
625 /* The function minmax_replacement does the main work of doing the minmax
626 replacement. Return true if the replacement is done. Otherwise return
627 false.
628 BB is the basic block where the replacement is going to be done on. ARG0
629 is argument 0 from the PHI. Likewise for ARG1. */
631 static bool
632 minmax_replacement (basic_block cond_bb, basic_block middle_bb,
633 edge e0, edge e1, gimple phi,
634 tree arg0, tree arg1)
636 tree result, type;
637 gimple cond, new_stmt;
638 edge true_edge, false_edge;
639 enum tree_code cmp, minmax, ass_code;
640 tree smaller, larger, arg_true, arg_false;
641 gimple_stmt_iterator gsi, gsi_from;
643 type = TREE_TYPE (PHI_RESULT (phi));
645 /* The optimization may be unsafe due to NaNs. */
646 if (HONOR_NANS (TYPE_MODE (type)))
647 return false;
649 cond = last_stmt (cond_bb);
650 cmp = gimple_cond_code (cond);
651 result = PHI_RESULT (phi);
653 /* This transformation is only valid for order comparisons. Record which
654 operand is smaller/larger if the result of the comparison is true. */
655 if (cmp == LT_EXPR || cmp == LE_EXPR)
657 smaller = gimple_cond_lhs (cond);
658 larger = gimple_cond_rhs (cond);
660 else if (cmp == GT_EXPR || cmp == GE_EXPR)
662 smaller = gimple_cond_rhs (cond);
663 larger = gimple_cond_lhs (cond);
665 else
666 return false;
668 /* We need to know which is the true edge and which is the false
669 edge so that we know if have abs or negative abs. */
670 extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
672 /* Forward the edges over the middle basic block. */
673 if (true_edge->dest == middle_bb)
674 true_edge = EDGE_SUCC (true_edge->dest, 0);
675 if (false_edge->dest == middle_bb)
676 false_edge = EDGE_SUCC (false_edge->dest, 0);
678 if (true_edge == e0)
680 gcc_assert (false_edge == e1);
681 arg_true = arg0;
682 arg_false = arg1;
684 else
686 gcc_assert (false_edge == e0);
687 gcc_assert (true_edge == e1);
688 arg_true = arg1;
689 arg_false = arg0;
692 if (empty_block_p (middle_bb))
694 if (operand_equal_for_phi_arg_p (arg_true, smaller)
695 && operand_equal_for_phi_arg_p (arg_false, larger))
697 /* Case
699 if (smaller < larger)
700 rslt = smaller;
701 else
702 rslt = larger; */
703 minmax = MIN_EXPR;
705 else if (operand_equal_for_phi_arg_p (arg_false, smaller)
706 && operand_equal_for_phi_arg_p (arg_true, larger))
707 minmax = MAX_EXPR;
708 else
709 return false;
711 else
713 /* Recognize the following case, assuming d <= u:
715 if (a <= u)
716 b = MAX (a, d);
717 x = PHI <b, u>
719 This is equivalent to
721 b = MAX (a, d);
722 x = MIN (b, u); */
724 gimple assign = last_and_only_stmt (middle_bb);
725 tree lhs, op0, op1, bound;
727 if (!assign
728 || gimple_code (assign) != GIMPLE_ASSIGN)
729 return false;
731 lhs = gimple_assign_lhs (assign);
732 ass_code = gimple_assign_rhs_code (assign);
733 if (ass_code != MAX_EXPR && ass_code != MIN_EXPR)
734 return false;
735 op0 = gimple_assign_rhs1 (assign);
736 op1 = gimple_assign_rhs2 (assign);
738 if (true_edge->src == middle_bb)
740 /* We got here if the condition is true, i.e., SMALLER < LARGER. */
741 if (!operand_equal_for_phi_arg_p (lhs, arg_true))
742 return false;
744 if (operand_equal_for_phi_arg_p (arg_false, larger))
746 /* Case
748 if (smaller < larger)
750 r' = MAX_EXPR (smaller, bound)
752 r = PHI <r', larger> --> to be turned to MIN_EXPR. */
753 if (ass_code != MAX_EXPR)
754 return false;
756 minmax = MIN_EXPR;
757 if (operand_equal_for_phi_arg_p (op0, smaller))
758 bound = op1;
759 else if (operand_equal_for_phi_arg_p (op1, smaller))
760 bound = op0;
761 else
762 return false;
764 /* We need BOUND <= LARGER. */
765 if (!integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node,
766 bound, larger)))
767 return false;
769 else if (operand_equal_for_phi_arg_p (arg_false, smaller))
771 /* Case
773 if (smaller < larger)
775 r' = MIN_EXPR (larger, bound)
777 r = PHI <r', smaller> --> to be turned to MAX_EXPR. */
778 if (ass_code != MIN_EXPR)
779 return false;
781 minmax = MAX_EXPR;
782 if (operand_equal_for_phi_arg_p (op0, larger))
783 bound = op1;
784 else if (operand_equal_for_phi_arg_p (op1, larger))
785 bound = op0;
786 else
787 return false;
789 /* We need BOUND >= SMALLER. */
790 if (!integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node,
791 bound, smaller)))
792 return false;
794 else
795 return false;
797 else
799 /* We got here if the condition is false, i.e., SMALLER > LARGER. */
800 if (!operand_equal_for_phi_arg_p (lhs, arg_false))
801 return false;
803 if (operand_equal_for_phi_arg_p (arg_true, larger))
805 /* Case
807 if (smaller > larger)
809 r' = MIN_EXPR (smaller, bound)
811 r = PHI <r', larger> --> to be turned to MAX_EXPR. */
812 if (ass_code != MIN_EXPR)
813 return false;
815 minmax = MAX_EXPR;
816 if (operand_equal_for_phi_arg_p (op0, smaller))
817 bound = op1;
818 else if (operand_equal_for_phi_arg_p (op1, smaller))
819 bound = op0;
820 else
821 return false;
823 /* We need BOUND >= LARGER. */
824 if (!integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node,
825 bound, larger)))
826 return false;
828 else if (operand_equal_for_phi_arg_p (arg_true, smaller))
830 /* Case
832 if (smaller > larger)
834 r' = MAX_EXPR (larger, bound)
836 r = PHI <r', smaller> --> to be turned to MIN_EXPR. */
837 if (ass_code != MAX_EXPR)
838 return false;
840 minmax = MIN_EXPR;
841 if (operand_equal_for_phi_arg_p (op0, larger))
842 bound = op1;
843 else if (operand_equal_for_phi_arg_p (op1, larger))
844 bound = op0;
845 else
846 return false;
848 /* We need BOUND <= SMALLER. */
849 if (!integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node,
850 bound, smaller)))
851 return false;
853 else
854 return false;
857 /* Move the statement from the middle block. */
858 gsi = gsi_last_bb (cond_bb);
859 gsi_from = gsi_last_nondebug_bb (middle_bb);
860 gsi_move_before (&gsi_from, &gsi);
863 /* Emit the statement to compute min/max. */
864 result = duplicate_ssa_name (PHI_RESULT (phi), NULL);
865 new_stmt = gimple_build_assign_with_ops (minmax, result, arg0, arg1);
866 gsi = gsi_last_bb (cond_bb);
867 gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
869 replace_phi_edge_with_variable (cond_bb, e1, phi, result);
870 return true;
873 /* The function absolute_replacement does the main work of doing the absolute
874 replacement. Return true if the replacement is done. Otherwise return
875 false.
876 bb is the basic block where the replacement is going to be done on. arg0
877 is argument 0 from the phi. Likewise for arg1. */
879 static bool
880 abs_replacement (basic_block cond_bb, basic_block middle_bb,
881 edge e0 ATTRIBUTE_UNUSED, edge e1,
882 gimple phi, tree arg0, tree arg1)
884 tree result;
885 gimple new_stmt, cond;
886 gimple_stmt_iterator gsi;
887 edge true_edge, false_edge;
888 gimple assign;
889 edge e;
890 tree rhs, lhs;
891 bool negate;
892 enum tree_code cond_code;
894 /* If the type says honor signed zeros we cannot do this
895 optimization. */
896 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
897 return false;
899 /* OTHER_BLOCK must have only one executable statement which must have the
900 form arg0 = -arg1 or arg1 = -arg0. */
902 assign = last_and_only_stmt (middle_bb);
903 /* If we did not find the proper negation assignment, then we can not
904 optimize. */
905 if (assign == NULL)
906 return false;
908 /* If we got here, then we have found the only executable statement
909 in OTHER_BLOCK. If it is anything other than arg = -arg1 or
910 arg1 = -arg0, then we can not optimize. */
911 if (gimple_code (assign) != GIMPLE_ASSIGN)
912 return false;
914 lhs = gimple_assign_lhs (assign);
916 if (gimple_assign_rhs_code (assign) != NEGATE_EXPR)
917 return false;
919 rhs = gimple_assign_rhs1 (assign);
921 /* The assignment has to be arg0 = -arg1 or arg1 = -arg0. */
922 if (!(lhs == arg0 && rhs == arg1)
923 && !(lhs == arg1 && rhs == arg0))
924 return false;
926 cond = last_stmt (cond_bb);
927 result = PHI_RESULT (phi);
929 /* Only relationals comparing arg[01] against zero are interesting. */
930 cond_code = gimple_cond_code (cond);
931 if (cond_code != GT_EXPR && cond_code != GE_EXPR
932 && cond_code != LT_EXPR && cond_code != LE_EXPR)
933 return false;
935 /* Make sure the conditional is arg[01] OP y. */
936 if (gimple_cond_lhs (cond) != rhs)
937 return false;
939 if (FLOAT_TYPE_P (TREE_TYPE (gimple_cond_rhs (cond)))
940 ? real_zerop (gimple_cond_rhs (cond))
941 : integer_zerop (gimple_cond_rhs (cond)))
943 else
944 return false;
946 /* We need to know which is the true edge and which is the false
947 edge so that we know if have abs or negative abs. */
948 extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
950 /* For GT_EXPR/GE_EXPR, if the true edge goes to OTHER_BLOCK, then we
951 will need to negate the result. Similarly for LT_EXPR/LE_EXPR if
952 the false edge goes to OTHER_BLOCK. */
953 if (cond_code == GT_EXPR || cond_code == GE_EXPR)
954 e = true_edge;
955 else
956 e = false_edge;
958 if (e->dest == middle_bb)
959 negate = true;
960 else
961 negate = false;
963 result = duplicate_ssa_name (result, NULL);
965 if (negate)
967 tree tmp = create_tmp_var (TREE_TYPE (result), NULL);
968 add_referenced_var (tmp);
969 lhs = make_ssa_name (tmp, NULL);
971 else
972 lhs = result;
974 /* Build the modify expression with abs expression. */
975 new_stmt = gimple_build_assign_with_ops (ABS_EXPR, lhs, rhs, NULL);
977 gsi = gsi_last_bb (cond_bb);
978 gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
980 if (negate)
982 /* Get the right GSI. We want to insert after the recently
983 added ABS_EXPR statement (which we know is the first statement
984 in the block. */
985 new_stmt = gimple_build_assign_with_ops (NEGATE_EXPR, result, lhs, NULL);
987 gsi_insert_after (&gsi, new_stmt, GSI_NEW_STMT);
990 replace_phi_edge_with_variable (cond_bb, e1, phi, result);
992 /* Note that we optimized this PHI. */
993 return true;
996 /* Auxiliary functions to determine the set of memory accesses which
997 can't trap because they are preceded by accesses to the same memory
998 portion. We do that for INDIRECT_REFs, so we only need to track
999 the SSA_NAME of the pointer indirectly referenced. The algorithm
1000 simply is a walk over all instructions in dominator order. When
1001 we see an INDIRECT_REF we determine if we've already seen a same
1002 ref anywhere up to the root of the dominator tree. If we do the
1003 current access can't trap. If we don't see any dominating access
1004 the current access might trap, but might also make later accesses
1005 non-trapping, so we remember it. We need to be careful with loads
1006 or stores, for instance a load might not trap, while a store would,
1007 so if we see a dominating read access this doesn't mean that a later
1008 write access would not trap. Hence we also need to differentiate the
1009 type of access(es) seen.
1011 ??? We currently are very conservative and assume that a load might
1012 trap even if a store doesn't (write-only memory). This probably is
1013 overly conservative. */
1015 /* A hash-table of SSA_NAMEs, and in which basic block an INDIRECT_REF
1016 through it was seen, which would constitute a no-trap region for
1017 same accesses. */
1018 struct name_to_bb
1020 tree ssa_name;
1021 basic_block bb;
1022 unsigned store : 1;
1025 /* The hash table for remembering what we've seen. */
1026 static htab_t seen_ssa_names;
1028 /* The set of INDIRECT_REFs which can't trap. */
1029 static struct pointer_set_t *nontrap_set;
1031 /* The hash function, based on the pointer to the pointer SSA_NAME. */
1032 static hashval_t
1033 name_to_bb_hash (const void *p)
1035 const_tree n = ((const struct name_to_bb *)p)->ssa_name;
1036 return htab_hash_pointer (n) ^ ((const struct name_to_bb *)p)->store;
1039 /* The equality function of *P1 and *P2. SSA_NAMEs are shared, so
1040 it's enough to simply compare them for equality. */
1041 static int
1042 name_to_bb_eq (const void *p1, const void *p2)
1044 const struct name_to_bb *n1 = (const struct name_to_bb *)p1;
1045 const struct name_to_bb *n2 = (const struct name_to_bb *)p2;
1047 return n1->ssa_name == n2->ssa_name && n1->store == n2->store;
1050 /* We see the expression EXP in basic block BB. If it's an interesting
1051 expression (an INDIRECT_REF through an SSA_NAME) possibly insert the
1052 expression into the set NONTRAP or the hash table of seen expressions.
1053 STORE is true if this expression is on the LHS, otherwise it's on
1054 the RHS. */
1055 static void
1056 add_or_mark_expr (basic_block bb, tree exp,
1057 struct pointer_set_t *nontrap, bool store)
1059 if (INDIRECT_REF_P (exp)
1060 && TREE_CODE (TREE_OPERAND (exp, 0)) == SSA_NAME)
1062 tree name = TREE_OPERAND (exp, 0);
1063 struct name_to_bb map;
1064 void **slot;
1065 struct name_to_bb *n2bb;
1066 basic_block found_bb = 0;
1068 /* Try to find the last seen INDIRECT_REF through the same
1069 SSA_NAME, which can trap. */
1070 map.ssa_name = name;
1071 map.bb = 0;
1072 map.store = store;
1073 slot = htab_find_slot (seen_ssa_names, &map, INSERT);
1074 n2bb = (struct name_to_bb *) *slot;
1075 if (n2bb)
1076 found_bb = n2bb->bb;
1078 /* If we've found a trapping INDIRECT_REF, _and_ it dominates EXP
1079 (it's in a basic block on the path from us to the dominator root)
1080 then we can't trap. */
1081 if (found_bb && found_bb->aux == (void *)1)
1083 pointer_set_insert (nontrap, exp);
1085 else
1087 /* EXP might trap, so insert it into the hash table. */
1088 if (n2bb)
1090 n2bb->bb = bb;
1092 else
1094 n2bb = XNEW (struct name_to_bb);
1095 n2bb->ssa_name = name;
1096 n2bb->bb = bb;
1097 n2bb->store = store;
1098 *slot = n2bb;
1104 /* Called by walk_dominator_tree, when entering the block BB. */
1105 static void
1106 nt_init_block (struct dom_walk_data *data ATTRIBUTE_UNUSED, basic_block bb)
1108 gimple_stmt_iterator gsi;
1109 /* Mark this BB as being on the path to dominator root. */
1110 bb->aux = (void*)1;
1112 /* And walk the statements in order. */
1113 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1115 gimple stmt = gsi_stmt (gsi);
1117 if (is_gimple_assign (stmt))
1119 add_or_mark_expr (bb, gimple_assign_lhs (stmt), nontrap_set, true);
1120 add_or_mark_expr (bb, gimple_assign_rhs1 (stmt), nontrap_set, false);
1121 if (get_gimple_rhs_num_ops (gimple_assign_rhs_code (stmt)) > 1)
1122 add_or_mark_expr (bb, gimple_assign_rhs2 (stmt), nontrap_set,
1123 false);
1128 /* Called by walk_dominator_tree, when basic block BB is exited. */
1129 static void
1130 nt_fini_block (struct dom_walk_data *data ATTRIBUTE_UNUSED, basic_block bb)
1132 /* This BB isn't on the path to dominator root anymore. */
1133 bb->aux = NULL;
1136 /* This is the entry point of gathering non trapping memory accesses.
1137 It will do a dominator walk over the whole function, and it will
1138 make use of the bb->aux pointers. It returns a set of trees
1139 (the INDIRECT_REFs itself) which can't trap. */
1140 static struct pointer_set_t *
1141 get_non_trapping (void)
1143 struct pointer_set_t *nontrap;
1144 struct dom_walk_data walk_data;
1146 nontrap = pointer_set_create ();
1147 seen_ssa_names = htab_create (128, name_to_bb_hash, name_to_bb_eq,
1148 free);
1149 /* We're going to do a dominator walk, so ensure that we have
1150 dominance information. */
1151 calculate_dominance_info (CDI_DOMINATORS);
1153 /* Setup callbacks for the generic dominator tree walker. */
1154 nontrap_set = nontrap;
1155 walk_data.dom_direction = CDI_DOMINATORS;
1156 walk_data.initialize_block_local_data = NULL;
1157 walk_data.before_dom_children = nt_init_block;
1158 walk_data.after_dom_children = nt_fini_block;
1159 walk_data.global_data = NULL;
1160 walk_data.block_local_data_size = 0;
1162 init_walk_dominator_tree (&walk_data);
1163 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
1164 fini_walk_dominator_tree (&walk_data);
1165 htab_delete (seen_ssa_names);
1167 return nontrap;
1170 /* Do the main work of conditional store replacement. We already know
1171 that the recognized pattern looks like so:
1173 split:
1174 if (cond) goto MIDDLE_BB; else goto JOIN_BB (edge E1)
1175 MIDDLE_BB:
1176 something
1177 fallthrough (edge E0)
1178 JOIN_BB:
1179 some more
1181 We check that MIDDLE_BB contains only one store, that that store
1182 doesn't trap (not via NOTRAP, but via checking if an access to the same
1183 memory location dominates us) and that the store has a "simple" RHS. */
1185 static bool
1186 cond_store_replacement (basic_block middle_bb, basic_block join_bb,
1187 edge e0, edge e1, struct pointer_set_t *nontrap)
1189 gimple assign = last_and_only_stmt (middle_bb);
1190 tree lhs, rhs, name;
1191 gimple newphi, new_stmt;
1192 gimple_stmt_iterator gsi;
1193 source_location locus;
1194 enum tree_code code;
1196 /* Check if middle_bb contains of only one store. */
1197 if (!assign
1198 || gimple_code (assign) != GIMPLE_ASSIGN)
1199 return false;
1201 locus = gimple_location (assign);
1202 lhs = gimple_assign_lhs (assign);
1203 rhs = gimple_assign_rhs1 (assign);
1204 if (!INDIRECT_REF_P (lhs))
1205 return false;
1207 /* RHS is either a single SSA_NAME or a constant. */
1208 code = gimple_assign_rhs_code (assign);
1209 if (get_gimple_rhs_class (code) != GIMPLE_SINGLE_RHS
1210 || (code != SSA_NAME && !is_gimple_min_invariant (rhs)))
1211 return false;
1212 /* Prove that we can move the store down. We could also check
1213 TREE_THIS_NOTRAP here, but in that case we also could move stores,
1214 whose value is not available readily, which we want to avoid. */
1215 if (!pointer_set_contains (nontrap, lhs))
1216 return false;
1218 /* Now we've checked the constraints, so do the transformation:
1219 1) Remove the single store. */
1220 mark_symbols_for_renaming (assign);
1221 gsi = gsi_for_stmt (assign);
1222 gsi_remove (&gsi, true);
1224 /* 2) Create a temporary where we can store the old content
1225 of the memory touched by the store, if we need to. */
1226 if (!condstoretemp || TREE_TYPE (lhs) != TREE_TYPE (condstoretemp))
1228 condstoretemp = create_tmp_reg (TREE_TYPE (lhs), "cstore");
1229 get_var_ann (condstoretemp);
1231 add_referenced_var (condstoretemp);
1233 /* 3) Insert a load from the memory of the store to the temporary
1234 on the edge which did not contain the store. */
1235 lhs = unshare_expr (lhs);
1236 new_stmt = gimple_build_assign (condstoretemp, lhs);
1237 name = make_ssa_name (condstoretemp, new_stmt);
1238 gimple_assign_set_lhs (new_stmt, name);
1239 gimple_set_location (new_stmt, locus);
1240 mark_symbols_for_renaming (new_stmt);
1241 gsi_insert_on_edge (e1, new_stmt);
1243 /* 4) Create a PHI node at the join block, with one argument
1244 holding the old RHS, and the other holding the temporary
1245 where we stored the old memory contents. */
1246 newphi = create_phi_node (condstoretemp, join_bb);
1247 add_phi_arg (newphi, rhs, e0, locus);
1248 add_phi_arg (newphi, name, e1, locus);
1250 lhs = unshare_expr (lhs);
1251 new_stmt = gimple_build_assign (lhs, PHI_RESULT (newphi));
1252 mark_symbols_for_renaming (new_stmt);
1254 /* 5) Insert that PHI node. */
1255 gsi = gsi_after_labels (join_bb);
1256 if (gsi_end_p (gsi))
1258 gsi = gsi_last_bb (join_bb);
1259 gsi_insert_after (&gsi, new_stmt, GSI_NEW_STMT);
1261 else
1262 gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
1264 return true;
1267 /* Always do these optimizations if we have SSA
1268 trees to work on. */
1269 static bool
1270 gate_phiopt (void)
1272 return 1;
1275 struct gimple_opt_pass pass_phiopt =
1278 GIMPLE_PASS,
1279 "phiopt", /* name */
1280 gate_phiopt, /* gate */
1281 tree_ssa_phiopt, /* execute */
1282 NULL, /* sub */
1283 NULL, /* next */
1284 0, /* static_pass_number */
1285 TV_TREE_PHIOPT, /* tv_id */
1286 PROP_cfg | PROP_ssa, /* properties_required */
1287 0, /* properties_provided */
1288 0, /* properties_destroyed */
1289 0, /* todo_flags_start */
1290 TODO_dump_func
1291 | TODO_ggc_collect
1292 | TODO_verify_ssa
1293 | TODO_verify_flow
1294 | TODO_verify_stmts /* todo_flags_finish */
1298 static bool
1299 gate_cselim (void)
1301 return flag_tree_cselim;
1304 struct gimple_opt_pass pass_cselim =
1307 GIMPLE_PASS,
1308 "cselim", /* name */
1309 gate_cselim, /* gate */
1310 tree_ssa_cs_elim, /* execute */
1311 NULL, /* sub */
1312 NULL, /* next */
1313 0, /* static_pass_number */
1314 TV_TREE_PHIOPT, /* tv_id */
1315 PROP_cfg | PROP_ssa, /* properties_required */
1316 0, /* properties_provided */
1317 0, /* properties_destroyed */
1318 0, /* todo_flags_start */
1319 TODO_dump_func
1320 | TODO_ggc_collect
1321 | TODO_verify_ssa
1322 | TODO_verify_flow
1323 | TODO_verify_stmts /* todo_flags_finish */