1 /* Optimization of PHI nodes by converting them into straightline code.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, 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
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
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/>. */
23 #include "coretypes.h"
29 #include "basic-block.h"
31 #include "tree-flow.h"
32 #include "tree-pass.h"
33 #include "tree-dump.h"
34 #include "langhooks.h"
35 #include "pointer-set.h"
38 static unsigned int tree_ssa_phiopt (void);
39 static unsigned int tree_ssa_phiopt_worker (bool);
40 static bool conditional_replacement (basic_block
, basic_block
,
41 edge
, edge
, gimple
, tree
, tree
);
42 static bool value_replacement (basic_block
, basic_block
,
43 edge
, edge
, gimple
, tree
, tree
);
44 static bool minmax_replacement (basic_block
, basic_block
,
45 edge
, edge
, gimple
, tree
, tree
);
46 static bool abs_replacement (basic_block
, basic_block
,
47 edge
, edge
, gimple
, tree
, tree
);
48 static bool cond_store_replacement (basic_block
, basic_block
, edge
, edge
,
49 struct pointer_set_t
*);
50 static bool cond_if_else_store_replacement (basic_block
, basic_block
, basic_block
);
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,
65 if (cond) goto bb2; else goto bb1;
68 x = PHI <0 (bb1), 1 (bb0), ...>;
76 x = PHI <x' (bb0), ...>;
78 We remove bb1 as it becomes unreachable. This occurs often due to
79 gimplification of conditionals.
84 This transformation, implemented in value_replacement, replaces
87 if (a != b) goto bb2; else goto bb1;
90 x = PHI <a (bb1), b (bb0), ...>;
96 x = PHI <b (bb0), ...>;
98 This opportunity can sometimes occur as a result of other
104 This transformation, implemented in abs_replacement, replaces
107 if (a >= 0) goto bb2; else goto bb1;
111 x = PHI <x (bb1), a (bb0), ...>;
118 x = PHI <x' (bb0), ...>;
123 This transformation, minmax_replacement replaces
126 if (a <= b) goto bb2; else goto bb1;
129 x = PHI <b (bb1), a (bb0), ...>;
136 x = PHI <x' (bb0), ...>;
138 A similar transformation is done for MAX_EXPR. */
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:
151 if (cond) goto bb2; else goto bb1;
159 if (cond) goto bb1; else goto bb2;
163 condtmp = PHI <RHS, condtmp'>
166 This transformation can only be done under several constraints,
167 documented below. It also replaces:
170 if (cond) goto bb2; else goto bb1;
181 if (cond) goto bb3; else goto bb1;
184 condtmp = PHI <RHS1, RHS2>
188 tree_ssa_cs_elim (void)
190 return tree_ssa_phiopt_worker (true);
193 /* For conditional store replacement we need a temporary to
194 put the old contents of the memory in. */
195 static tree condstoretemp
;
197 /* The core routine of conditional store replacement and normal
198 phi optimizations. Both share much of the infrastructure in how
199 to match applicable basic block patterns. DO_STORE_ELIM is true
200 when we want to do conditional store replacement, false otherwise. */
202 tree_ssa_phiopt_worker (bool do_store_elim
)
205 basic_block
*bb_order
;
207 bool cfgchanged
= false;
208 struct pointer_set_t
*nontrap
= 0;
212 condstoretemp
= NULL_TREE
;
213 /* Calculate the set of non-trapping memory accesses. */
214 nontrap
= get_non_trapping ();
217 /* Search every basic block for COND_EXPR we may be able to optimize.
219 We walk the blocks in order that guarantees that a block with
220 a single predecessor is processed before the predecessor.
221 This ensures that we collapse inner ifs before visiting the
222 outer ones, and also that we do not try to visit a removed
224 bb_order
= blocks_in_phiopt_order ();
225 n
= n_basic_blocks
- NUM_FIXED_BLOCKS
;
227 for (i
= 0; i
< n
; i
++)
229 gimple cond_stmt
, phi
;
230 basic_block bb1
, bb2
;
236 cond_stmt
= last_stmt (bb
);
237 /* Check to see if the last statement is a GIMPLE_COND. */
239 || gimple_code (cond_stmt
) != GIMPLE_COND
)
242 e1
= EDGE_SUCC (bb
, 0);
244 e2
= EDGE_SUCC (bb
, 1);
247 /* We cannot do the optimization on abnormal edges. */
248 if ((e1
->flags
& EDGE_ABNORMAL
) != 0
249 || (e2
->flags
& EDGE_ABNORMAL
) != 0)
252 /* If either bb1's succ or bb2 or bb2's succ is non NULL. */
253 if (EDGE_COUNT (bb1
->succs
) == 0
255 || EDGE_COUNT (bb2
->succs
) == 0)
258 /* Find the bb which is the fall through to the other. */
259 if (EDGE_SUCC (bb1
, 0)->dest
== bb2
)
261 else if (EDGE_SUCC (bb2
, 0)->dest
== bb1
)
263 basic_block bb_tmp
= bb1
;
270 else if (do_store_elim
271 && EDGE_SUCC (bb1
, 0)->dest
== EDGE_SUCC (bb2
, 0)->dest
)
273 basic_block bb3
= EDGE_SUCC (bb1
, 0)->dest
;
275 if (!single_succ_p (bb1
)
276 || (EDGE_SUCC (bb1
, 0)->flags
& EDGE_FALLTHRU
) == 0
277 || !single_succ_p (bb2
)
278 || (EDGE_SUCC (bb2
, 0)->flags
& EDGE_FALLTHRU
) == 0
279 || EDGE_COUNT (bb3
->preds
) != 2)
281 if (cond_if_else_store_replacement (bb1
, bb2
, bb3
))
288 e1
= EDGE_SUCC (bb1
, 0);
290 /* Make sure that bb1 is just a fall through. */
291 if (!single_succ_p (bb1
)
292 || (e1
->flags
& EDGE_FALLTHRU
) == 0)
295 /* Also make sure that bb1 only have one predecessor and that it
297 if (!single_pred_p (bb1
)
298 || single_pred (bb1
) != bb
)
303 /* bb1 is the middle block, bb2 the join block, bb the split block,
304 e1 the fallthrough edge from bb1 to bb2. We can't do the
305 optimization if the join block has more than two predecessors. */
306 if (EDGE_COUNT (bb2
->preds
) > 2)
308 if (cond_store_replacement (bb1
, bb2
, e1
, e2
, nontrap
))
313 gimple_seq phis
= phi_nodes (bb2
);
314 gimple_stmt_iterator gsi
;
316 /* Check to make sure that there is only one non-virtual PHI node.
317 TODO: we could do it with more than one iff the other PHI nodes
318 have the same elements for these two edges. */
320 for (gsi
= gsi_start (phis
); !gsi_end_p (gsi
); gsi_next (&gsi
))
322 if (!is_gimple_reg (gimple_phi_result (gsi_stmt (gsi
))))
329 phi
= gsi_stmt (gsi
);
334 arg0
= gimple_phi_arg_def (phi
, e1
->dest_idx
);
335 arg1
= gimple_phi_arg_def (phi
, e2
->dest_idx
);
337 /* Something is wrong if we cannot find the arguments in the PHI
339 gcc_assert (arg0
!= NULL
&& arg1
!= NULL
);
341 /* Do the replacement of conditional if it can be done. */
342 if (conditional_replacement (bb
, bb1
, e1
, e2
, phi
, arg0
, arg1
))
344 else if (value_replacement (bb
, bb1
, e1
, e2
, phi
, arg0
, arg1
))
346 else if (abs_replacement (bb
, bb1
, e1
, e2
, phi
, arg0
, arg1
))
348 else if (minmax_replacement (bb
, bb1
, e1
, e2
, phi
, arg0
, arg1
))
356 pointer_set_destroy (nontrap
);
357 /* If the CFG has changed, we should cleanup the CFG. */
358 if (cfgchanged
&& do_store_elim
)
360 /* In cond-store replacement we have added some loads on edges
361 and new VOPS (as we moved the store, and created a load). */
362 gsi_commit_edge_inserts ();
363 return TODO_cleanup_cfg
| TODO_update_ssa_only_virtuals
;
366 return TODO_cleanup_cfg
;
370 /* Returns the list of basic blocks in the function in an order that guarantees
371 that if a block X has just a single predecessor Y, then Y is after X in the
375 blocks_in_phiopt_order (void)
378 basic_block
*order
= XNEWVEC (basic_block
, n_basic_blocks
);
379 unsigned n
= n_basic_blocks
- NUM_FIXED_BLOCKS
;
381 sbitmap visited
= sbitmap_alloc (last_basic_block
);
383 #define MARK_VISITED(BB) (SET_BIT (visited, (BB)->index))
384 #define VISITED_P(BB) (TEST_BIT (visited, (BB)->index))
386 sbitmap_zero (visited
);
388 MARK_VISITED (ENTRY_BLOCK_PTR
);
394 /* Walk the predecessors of x as long as they have precisely one
395 predecessor and add them to the list, so that they get stored
398 single_pred_p (y
) && !VISITED_P (single_pred (y
));
401 for (y
= x
, i
= n
- np
;
402 single_pred_p (y
) && !VISITED_P (single_pred (y
));
403 y
= single_pred (y
), i
++)
411 gcc_assert (i
== n
- 1);
415 sbitmap_free (visited
);
424 /* Return TRUE if block BB has no executable statements, otherwise return
428 empty_block_p (basic_block bb
)
430 /* BB must have no executable statements. */
431 gimple_stmt_iterator gsi
= gsi_after_labels (bb
);
434 if (is_gimple_debug (gsi_stmt (gsi
)))
435 gsi_next_nondebug (&gsi
);
436 return gsi_end_p (gsi
);
439 /* Replace PHI node element whose edge is E in block BB with variable NEW.
440 Remove the edge from COND_BLOCK which does not lead to BB (COND_BLOCK
441 is known to have two edges, one of which must reach BB). */
444 replace_phi_edge_with_variable (basic_block cond_block
,
445 edge e
, gimple phi
, tree new_tree
)
447 basic_block bb
= gimple_bb (phi
);
448 basic_block block_to_remove
;
449 gimple_stmt_iterator gsi
;
451 /* Change the PHI argument to new. */
452 SET_USE (PHI_ARG_DEF_PTR (phi
, e
->dest_idx
), new_tree
);
454 /* Remove the empty basic block. */
455 if (EDGE_SUCC (cond_block
, 0)->dest
== bb
)
457 EDGE_SUCC (cond_block
, 0)->flags
|= EDGE_FALLTHRU
;
458 EDGE_SUCC (cond_block
, 0)->flags
&= ~(EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
);
459 EDGE_SUCC (cond_block
, 0)->probability
= REG_BR_PROB_BASE
;
460 EDGE_SUCC (cond_block
, 0)->count
+= EDGE_SUCC (cond_block
, 1)->count
;
462 block_to_remove
= EDGE_SUCC (cond_block
, 1)->dest
;
466 EDGE_SUCC (cond_block
, 1)->flags
|= EDGE_FALLTHRU
;
467 EDGE_SUCC (cond_block
, 1)->flags
468 &= ~(EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
);
469 EDGE_SUCC (cond_block
, 1)->probability
= REG_BR_PROB_BASE
;
470 EDGE_SUCC (cond_block
, 1)->count
+= EDGE_SUCC (cond_block
, 0)->count
;
472 block_to_remove
= EDGE_SUCC (cond_block
, 0)->dest
;
474 delete_basic_block (block_to_remove
);
476 /* Eliminate the COND_EXPR at the end of COND_BLOCK. */
477 gsi
= gsi_last_bb (cond_block
);
478 gsi_remove (&gsi
, true);
480 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
482 "COND_EXPR in block %d and PHI in block %d converted to straightline code.\n",
487 /* The function conditional_replacement does the main work of doing the
488 conditional replacement. Return true if the replacement is done.
489 Otherwise return false.
490 BB is the basic block where the replacement is going to be done on. ARG0
491 is argument 0 from PHI. Likewise for ARG1. */
494 conditional_replacement (basic_block cond_bb
, basic_block middle_bb
,
495 edge e0
, edge e1
, gimple phi
,
496 tree arg0
, tree arg1
)
499 gimple stmt
, new_stmt
;
501 gimple_stmt_iterator gsi
;
502 edge true_edge
, false_edge
;
503 tree new_var
, new_var2
;
505 /* FIXME: Gimplification of complex type is too hard for now. */
506 if (TREE_CODE (TREE_TYPE (arg0
)) == COMPLEX_TYPE
507 || TREE_CODE (TREE_TYPE (arg1
)) == COMPLEX_TYPE
)
510 /* The PHI arguments have the constants 0 and 1, then convert
511 it to the conditional. */
512 if ((integer_zerop (arg0
) && integer_onep (arg1
))
513 || (integer_zerop (arg1
) && integer_onep (arg0
)))
518 if (!empty_block_p (middle_bb
))
521 /* At this point we know we have a GIMPLE_COND with two successors.
522 One successor is BB, the other successor is an empty block which
523 falls through into BB.
525 There is a single PHI node at the join point (BB) and its arguments
526 are constants (0, 1).
528 So, given the condition COND, and the two PHI arguments, we can
529 rewrite this PHI into non-branching code:
531 dest = (COND) or dest = COND'
533 We use the condition as-is if the argument associated with the
534 true edge has the value one or the argument associated with the
535 false edge as the value zero. Note that those conditions are not
536 the same since only one of the outgoing edges from the GIMPLE_COND
537 will directly reach BB and thus be associated with an argument. */
539 stmt
= last_stmt (cond_bb
);
540 result
= PHI_RESULT (phi
);
542 /* To handle special cases like floating point comparison, it is easier and
543 less error-prone to build a tree and gimplify it on the fly though it is
545 cond
= fold_build2 (gimple_cond_code (stmt
), boolean_type_node
,
546 gimple_cond_lhs (stmt
), gimple_cond_rhs (stmt
));
548 /* We need to know which is the true edge and which is the false
549 edge so that we know when to invert the condition below. */
550 extract_true_false_edges_from_block (cond_bb
, &true_edge
, &false_edge
);
551 if ((e0
== true_edge
&& integer_zerop (arg0
))
552 || (e0
== false_edge
&& integer_onep (arg0
))
553 || (e1
== true_edge
&& integer_zerop (arg1
))
554 || (e1
== false_edge
&& integer_onep (arg1
)))
555 cond
= fold_build1 (TRUTH_NOT_EXPR
, TREE_TYPE (cond
), cond
);
557 /* Insert our new statements at the end of conditional block before the
559 gsi
= gsi_for_stmt (stmt
);
560 new_var
= force_gimple_operand_gsi (&gsi
, cond
, true, NULL
, true,
563 if (!useless_type_conversion_p (TREE_TYPE (result
), TREE_TYPE (new_var
)))
565 source_location locus_0
, locus_1
;
567 new_var2
= create_tmp_var (TREE_TYPE (result
), NULL
);
568 add_referenced_var (new_var2
);
569 new_stmt
= gimple_build_assign_with_ops (CONVERT_EXPR
, new_var2
,
571 new_var2
= make_ssa_name (new_var2
, new_stmt
);
572 gimple_assign_set_lhs (new_stmt
, new_var2
);
573 gsi_insert_before (&gsi
, new_stmt
, GSI_SAME_STMT
);
576 /* Set the locus to the first argument, unless is doesn't have one. */
577 locus_0
= gimple_phi_arg_location (phi
, 0);
578 locus_1
= gimple_phi_arg_location (phi
, 1);
579 if (locus_0
== UNKNOWN_LOCATION
)
581 gimple_set_location (new_stmt
, locus_0
);
584 replace_phi_edge_with_variable (cond_bb
, e1
, phi
, new_var
);
586 /* Note that we optimized this PHI. */
590 /* The function value_replacement does the main work of doing the value
591 replacement. Return true if the replacement is done. Otherwise return
593 BB is the basic block where the replacement is going to be done on. ARG0
594 is argument 0 from the PHI. Likewise for ARG1. */
597 value_replacement (basic_block cond_bb
, basic_block middle_bb
,
598 edge e0
, edge e1
, gimple phi
,
599 tree arg0
, tree arg1
)
602 edge true_edge
, false_edge
;
605 /* If the type says honor signed zeros we cannot do this
607 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1
))))
610 if (!empty_block_p (middle_bb
))
613 cond
= last_stmt (cond_bb
);
614 code
= gimple_cond_code (cond
);
616 /* This transformation is only valid for equality comparisons. */
617 if (code
!= NE_EXPR
&& code
!= EQ_EXPR
)
620 /* We need to know which is the true edge and which is the false
621 edge so that we know if have abs or negative abs. */
622 extract_true_false_edges_from_block (cond_bb
, &true_edge
, &false_edge
);
624 /* At this point we know we have a COND_EXPR with two successors.
625 One successor is BB, the other successor is an empty block which
626 falls through into BB.
628 The condition for the COND_EXPR is known to be NE_EXPR or EQ_EXPR.
630 There is a single PHI node at the join point (BB) with two arguments.
632 We now need to verify that the two arguments in the PHI node match
633 the two arguments to the equality comparison. */
635 if ((operand_equal_for_phi_arg_p (arg0
, gimple_cond_lhs (cond
))
636 && operand_equal_for_phi_arg_p (arg1
, gimple_cond_rhs (cond
)))
637 || (operand_equal_for_phi_arg_p (arg1
, gimple_cond_lhs (cond
))
638 && operand_equal_for_phi_arg_p (arg0
, gimple_cond_rhs (cond
))))
643 /* For NE_EXPR, we want to build an assignment result = arg where
644 arg is the PHI argument associated with the true edge. For
645 EQ_EXPR we want the PHI argument associated with the false edge. */
646 e
= (code
== NE_EXPR
? true_edge
: false_edge
);
648 /* Unfortunately, E may not reach BB (it may instead have gone to
649 OTHER_BLOCK). If that is the case, then we want the single outgoing
650 edge from OTHER_BLOCK which reaches BB and represents the desired
651 path from COND_BLOCK. */
652 if (e
->dest
== middle_bb
)
653 e
= single_succ_edge (e
->dest
);
655 /* Now we know the incoming edge to BB that has the argument for the
656 RHS of our new assignment statement. */
662 replace_phi_edge_with_variable (cond_bb
, e1
, phi
, arg
);
664 /* Note that we optimized this PHI. */
670 /* The function minmax_replacement does the main work of doing the minmax
671 replacement. Return true if the replacement is done. Otherwise return
673 BB is the basic block where the replacement is going to be done on. ARG0
674 is argument 0 from the PHI. Likewise for ARG1. */
677 minmax_replacement (basic_block cond_bb
, basic_block middle_bb
,
678 edge e0
, edge e1
, gimple phi
,
679 tree arg0
, tree arg1
)
682 gimple cond
, new_stmt
;
683 edge true_edge
, false_edge
;
684 enum tree_code cmp
, minmax
, ass_code
;
685 tree smaller
, larger
, arg_true
, arg_false
;
686 gimple_stmt_iterator gsi
, gsi_from
;
688 type
= TREE_TYPE (PHI_RESULT (phi
));
690 /* The optimization may be unsafe due to NaNs. */
691 if (HONOR_NANS (TYPE_MODE (type
)))
694 cond
= last_stmt (cond_bb
);
695 cmp
= gimple_cond_code (cond
);
696 result
= PHI_RESULT (phi
);
698 /* This transformation is only valid for order comparisons. Record which
699 operand is smaller/larger if the result of the comparison is true. */
700 if (cmp
== LT_EXPR
|| cmp
== LE_EXPR
)
702 smaller
= gimple_cond_lhs (cond
);
703 larger
= gimple_cond_rhs (cond
);
705 else if (cmp
== GT_EXPR
|| cmp
== GE_EXPR
)
707 smaller
= gimple_cond_rhs (cond
);
708 larger
= gimple_cond_lhs (cond
);
713 /* We need to know which is the true edge and which is the false
714 edge so that we know if have abs or negative abs. */
715 extract_true_false_edges_from_block (cond_bb
, &true_edge
, &false_edge
);
717 /* Forward the edges over the middle basic block. */
718 if (true_edge
->dest
== middle_bb
)
719 true_edge
= EDGE_SUCC (true_edge
->dest
, 0);
720 if (false_edge
->dest
== middle_bb
)
721 false_edge
= EDGE_SUCC (false_edge
->dest
, 0);
725 gcc_assert (false_edge
== e1
);
731 gcc_assert (false_edge
== e0
);
732 gcc_assert (true_edge
== e1
);
737 if (empty_block_p (middle_bb
))
739 if (operand_equal_for_phi_arg_p (arg_true
, smaller
)
740 && operand_equal_for_phi_arg_p (arg_false
, larger
))
744 if (smaller < larger)
750 else if (operand_equal_for_phi_arg_p (arg_false
, smaller
)
751 && operand_equal_for_phi_arg_p (arg_true
, larger
))
758 /* Recognize the following case, assuming d <= u:
764 This is equivalent to
769 gimple assign
= last_and_only_stmt (middle_bb
);
770 tree lhs
, op0
, op1
, bound
;
773 || gimple_code (assign
) != GIMPLE_ASSIGN
)
776 lhs
= gimple_assign_lhs (assign
);
777 ass_code
= gimple_assign_rhs_code (assign
);
778 if (ass_code
!= MAX_EXPR
&& ass_code
!= MIN_EXPR
)
780 op0
= gimple_assign_rhs1 (assign
);
781 op1
= gimple_assign_rhs2 (assign
);
783 if (true_edge
->src
== middle_bb
)
785 /* We got here if the condition is true, i.e., SMALLER < LARGER. */
786 if (!operand_equal_for_phi_arg_p (lhs
, arg_true
))
789 if (operand_equal_for_phi_arg_p (arg_false
, larger
))
793 if (smaller < larger)
795 r' = MAX_EXPR (smaller, bound)
797 r = PHI <r', larger> --> to be turned to MIN_EXPR. */
798 if (ass_code
!= MAX_EXPR
)
802 if (operand_equal_for_phi_arg_p (op0
, smaller
))
804 else if (operand_equal_for_phi_arg_p (op1
, smaller
))
809 /* We need BOUND <= LARGER. */
810 if (!integer_nonzerop (fold_build2 (LE_EXPR
, boolean_type_node
,
814 else if (operand_equal_for_phi_arg_p (arg_false
, smaller
))
818 if (smaller < larger)
820 r' = MIN_EXPR (larger, bound)
822 r = PHI <r', smaller> --> to be turned to MAX_EXPR. */
823 if (ass_code
!= MIN_EXPR
)
827 if (operand_equal_for_phi_arg_p (op0
, larger
))
829 else if (operand_equal_for_phi_arg_p (op1
, larger
))
834 /* We need BOUND >= SMALLER. */
835 if (!integer_nonzerop (fold_build2 (GE_EXPR
, boolean_type_node
,
844 /* We got here if the condition is false, i.e., SMALLER > LARGER. */
845 if (!operand_equal_for_phi_arg_p (lhs
, arg_false
))
848 if (operand_equal_for_phi_arg_p (arg_true
, larger
))
852 if (smaller > larger)
854 r' = MIN_EXPR (smaller, bound)
856 r = PHI <r', larger> --> to be turned to MAX_EXPR. */
857 if (ass_code
!= MIN_EXPR
)
861 if (operand_equal_for_phi_arg_p (op0
, smaller
))
863 else if (operand_equal_for_phi_arg_p (op1
, smaller
))
868 /* We need BOUND >= LARGER. */
869 if (!integer_nonzerop (fold_build2 (GE_EXPR
, boolean_type_node
,
873 else if (operand_equal_for_phi_arg_p (arg_true
, smaller
))
877 if (smaller > larger)
879 r' = MAX_EXPR (larger, bound)
881 r = PHI <r', smaller> --> to be turned to MIN_EXPR. */
882 if (ass_code
!= MAX_EXPR
)
886 if (operand_equal_for_phi_arg_p (op0
, larger
))
888 else if (operand_equal_for_phi_arg_p (op1
, larger
))
893 /* We need BOUND <= SMALLER. */
894 if (!integer_nonzerop (fold_build2 (LE_EXPR
, boolean_type_node
,
902 /* Move the statement from the middle block. */
903 gsi
= gsi_last_bb (cond_bb
);
904 gsi_from
= gsi_last_nondebug_bb (middle_bb
);
905 gsi_move_before (&gsi_from
, &gsi
);
908 /* Emit the statement to compute min/max. */
909 result
= duplicate_ssa_name (PHI_RESULT (phi
), NULL
);
910 new_stmt
= gimple_build_assign_with_ops (minmax
, result
, arg0
, arg1
);
911 gsi
= gsi_last_bb (cond_bb
);
912 gsi_insert_before (&gsi
, new_stmt
, GSI_NEW_STMT
);
914 replace_phi_edge_with_variable (cond_bb
, e1
, phi
, result
);
918 /* The function absolute_replacement does the main work of doing the absolute
919 replacement. Return true if the replacement is done. Otherwise return
921 bb is the basic block where the replacement is going to be done on. arg0
922 is argument 0 from the phi. Likewise for arg1. */
925 abs_replacement (basic_block cond_bb
, basic_block middle_bb
,
926 edge e0 ATTRIBUTE_UNUSED
, edge e1
,
927 gimple phi
, tree arg0
, tree arg1
)
930 gimple new_stmt
, cond
;
931 gimple_stmt_iterator gsi
;
932 edge true_edge
, false_edge
;
937 enum tree_code cond_code
;
939 /* If the type says honor signed zeros we cannot do this
941 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1
))))
944 /* OTHER_BLOCK must have only one executable statement which must have the
945 form arg0 = -arg1 or arg1 = -arg0. */
947 assign
= last_and_only_stmt (middle_bb
);
948 /* If we did not find the proper negation assignment, then we can not
953 /* If we got here, then we have found the only executable statement
954 in OTHER_BLOCK. If it is anything other than arg = -arg1 or
955 arg1 = -arg0, then we can not optimize. */
956 if (gimple_code (assign
) != GIMPLE_ASSIGN
)
959 lhs
= gimple_assign_lhs (assign
);
961 if (gimple_assign_rhs_code (assign
) != NEGATE_EXPR
)
964 rhs
= gimple_assign_rhs1 (assign
);
966 /* The assignment has to be arg0 = -arg1 or arg1 = -arg0. */
967 if (!(lhs
== arg0
&& rhs
== arg1
)
968 && !(lhs
== arg1
&& rhs
== arg0
))
971 cond
= last_stmt (cond_bb
);
972 result
= PHI_RESULT (phi
);
974 /* Only relationals comparing arg[01] against zero are interesting. */
975 cond_code
= gimple_cond_code (cond
);
976 if (cond_code
!= GT_EXPR
&& cond_code
!= GE_EXPR
977 && cond_code
!= LT_EXPR
&& cond_code
!= LE_EXPR
)
980 /* Make sure the conditional is arg[01] OP y. */
981 if (gimple_cond_lhs (cond
) != rhs
)
984 if (FLOAT_TYPE_P (TREE_TYPE (gimple_cond_rhs (cond
)))
985 ? real_zerop (gimple_cond_rhs (cond
))
986 : integer_zerop (gimple_cond_rhs (cond
)))
991 /* We need to know which is the true edge and which is the false
992 edge so that we know if have abs or negative abs. */
993 extract_true_false_edges_from_block (cond_bb
, &true_edge
, &false_edge
);
995 /* For GT_EXPR/GE_EXPR, if the true edge goes to OTHER_BLOCK, then we
996 will need to negate the result. Similarly for LT_EXPR/LE_EXPR if
997 the false edge goes to OTHER_BLOCK. */
998 if (cond_code
== GT_EXPR
|| cond_code
== GE_EXPR
)
1003 if (e
->dest
== middle_bb
)
1008 result
= duplicate_ssa_name (result
, NULL
);
1012 tree tmp
= create_tmp_var (TREE_TYPE (result
), NULL
);
1013 add_referenced_var (tmp
);
1014 lhs
= make_ssa_name (tmp
, NULL
);
1019 /* Build the modify expression with abs expression. */
1020 new_stmt
= gimple_build_assign_with_ops (ABS_EXPR
, lhs
, rhs
, NULL
);
1022 gsi
= gsi_last_bb (cond_bb
);
1023 gsi_insert_before (&gsi
, new_stmt
, GSI_NEW_STMT
);
1027 /* Get the right GSI. We want to insert after the recently
1028 added ABS_EXPR statement (which we know is the first statement
1030 new_stmt
= gimple_build_assign_with_ops (NEGATE_EXPR
, result
, lhs
, NULL
);
1032 gsi_insert_after (&gsi
, new_stmt
, GSI_NEW_STMT
);
1035 replace_phi_edge_with_variable (cond_bb
, e1
, phi
, result
);
1037 /* Note that we optimized this PHI. */
1041 /* Auxiliary functions to determine the set of memory accesses which
1042 can't trap because they are preceded by accesses to the same memory
1043 portion. We do that for MEM_REFs, so we only need to track
1044 the SSA_NAME of the pointer indirectly referenced. The algorithm
1045 simply is a walk over all instructions in dominator order. When
1046 we see an MEM_REF we determine if we've already seen a same
1047 ref anywhere up to the root of the dominator tree. If we do the
1048 current access can't trap. If we don't see any dominating access
1049 the current access might trap, but might also make later accesses
1050 non-trapping, so we remember it. We need to be careful with loads
1051 or stores, for instance a load might not trap, while a store would,
1052 so if we see a dominating read access this doesn't mean that a later
1053 write access would not trap. Hence we also need to differentiate the
1054 type of access(es) seen.
1056 ??? We currently are very conservative and assume that a load might
1057 trap even if a store doesn't (write-only memory). This probably is
1058 overly conservative. */
1060 /* A hash-table of SSA_NAMEs, and in which basic block an MEM_REF
1061 through it was seen, which would constitute a no-trap region for
1070 /* The hash table for remembering what we've seen. */
1071 static htab_t seen_ssa_names
;
1073 /* The set of MEM_REFs which can't trap. */
1074 static struct pointer_set_t
*nontrap_set
;
1076 /* The hash function, based on the pointer to the pointer SSA_NAME. */
1078 name_to_bb_hash (const void *p
)
1080 const_tree n
= ((const struct name_to_bb
*)p
)->ssa_name
;
1081 return htab_hash_pointer (n
) ^ ((const struct name_to_bb
*)p
)->store
;
1084 /* The equality function of *P1 and *P2. SSA_NAMEs are shared, so
1085 it's enough to simply compare them for equality. */
1087 name_to_bb_eq (const void *p1
, const void *p2
)
1089 const struct name_to_bb
*n1
= (const struct name_to_bb
*)p1
;
1090 const struct name_to_bb
*n2
= (const struct name_to_bb
*)p2
;
1092 return n1
->ssa_name
== n2
->ssa_name
&& n1
->store
== n2
->store
;
1095 /* We see the expression EXP in basic block BB. If it's an interesting
1096 expression (an MEM_REF through an SSA_NAME) possibly insert the
1097 expression into the set NONTRAP or the hash table of seen expressions.
1098 STORE is true if this expression is on the LHS, otherwise it's on
1101 add_or_mark_expr (basic_block bb
, tree exp
,
1102 struct pointer_set_t
*nontrap
, bool store
)
1104 if (TREE_CODE (exp
) == MEM_REF
1105 && TREE_CODE (TREE_OPERAND (exp
, 0)) == SSA_NAME
)
1107 tree name
= TREE_OPERAND (exp
, 0);
1108 struct name_to_bb map
;
1110 struct name_to_bb
*n2bb
;
1111 basic_block found_bb
= 0;
1113 /* Try to find the last seen MEM_REF through the same
1114 SSA_NAME, which can trap. */
1115 map
.ssa_name
= name
;
1118 slot
= htab_find_slot (seen_ssa_names
, &map
, INSERT
);
1119 n2bb
= (struct name_to_bb
*) *slot
;
1121 found_bb
= n2bb
->bb
;
1123 /* If we've found a trapping MEM_REF, _and_ it dominates EXP
1124 (it's in a basic block on the path from us to the dominator root)
1125 then we can't trap. */
1126 if (found_bb
&& found_bb
->aux
== (void *)1)
1128 pointer_set_insert (nontrap
, exp
);
1132 /* EXP might trap, so insert it into the hash table. */
1139 n2bb
= XNEW (struct name_to_bb
);
1140 n2bb
->ssa_name
= name
;
1142 n2bb
->store
= store
;
1149 /* Called by walk_dominator_tree, when entering the block BB. */
1151 nt_init_block (struct dom_walk_data
*data ATTRIBUTE_UNUSED
, basic_block bb
)
1153 gimple_stmt_iterator gsi
;
1154 /* Mark this BB as being on the path to dominator root. */
1157 /* And walk the statements in order. */
1158 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1160 gimple stmt
= gsi_stmt (gsi
);
1162 if (is_gimple_assign (stmt
))
1164 add_or_mark_expr (bb
, gimple_assign_lhs (stmt
), nontrap_set
, true);
1165 add_or_mark_expr (bb
, gimple_assign_rhs1 (stmt
), nontrap_set
, false);
1166 if (get_gimple_rhs_num_ops (gimple_assign_rhs_code (stmt
)) > 1)
1167 add_or_mark_expr (bb
, gimple_assign_rhs2 (stmt
), nontrap_set
,
1173 /* Called by walk_dominator_tree, when basic block BB is exited. */
1175 nt_fini_block (struct dom_walk_data
*data ATTRIBUTE_UNUSED
, basic_block bb
)
1177 /* This BB isn't on the path to dominator root anymore. */
1181 /* This is the entry point of gathering non trapping memory accesses.
1182 It will do a dominator walk over the whole function, and it will
1183 make use of the bb->aux pointers. It returns a set of trees
1184 (the MEM_REFs itself) which can't trap. */
1185 static struct pointer_set_t
*
1186 get_non_trapping (void)
1188 struct pointer_set_t
*nontrap
;
1189 struct dom_walk_data walk_data
;
1191 nontrap
= pointer_set_create ();
1192 seen_ssa_names
= htab_create (128, name_to_bb_hash
, name_to_bb_eq
,
1194 /* We're going to do a dominator walk, so ensure that we have
1195 dominance information. */
1196 calculate_dominance_info (CDI_DOMINATORS
);
1198 /* Setup callbacks for the generic dominator tree walker. */
1199 nontrap_set
= nontrap
;
1200 walk_data
.dom_direction
= CDI_DOMINATORS
;
1201 walk_data
.initialize_block_local_data
= NULL
;
1202 walk_data
.before_dom_children
= nt_init_block
;
1203 walk_data
.after_dom_children
= nt_fini_block
;
1204 walk_data
.global_data
= NULL
;
1205 walk_data
.block_local_data_size
= 0;
1207 init_walk_dominator_tree (&walk_data
);
1208 walk_dominator_tree (&walk_data
, ENTRY_BLOCK_PTR
);
1209 fini_walk_dominator_tree (&walk_data
);
1210 htab_delete (seen_ssa_names
);
1215 /* Do the main work of conditional store replacement. We already know
1216 that the recognized pattern looks like so:
1219 if (cond) goto MIDDLE_BB; else goto JOIN_BB (edge E1)
1222 fallthrough (edge E0)
1226 We check that MIDDLE_BB contains only one store, that that store
1227 doesn't trap (not via NOTRAP, but via checking if an access to the same
1228 memory location dominates us) and that the store has a "simple" RHS. */
1231 cond_store_replacement (basic_block middle_bb
, basic_block join_bb
,
1232 edge e0
, edge e1
, struct pointer_set_t
*nontrap
)
1234 gimple assign
= last_and_only_stmt (middle_bb
);
1235 tree lhs
, rhs
, name
;
1236 gimple newphi
, new_stmt
;
1237 gimple_stmt_iterator gsi
;
1238 source_location locus
;
1240 /* Check if middle_bb contains of only one store. */
1242 || !gimple_assign_single_p (assign
))
1245 locus
= gimple_location (assign
);
1246 lhs
= gimple_assign_lhs (assign
);
1247 rhs
= gimple_assign_rhs1 (assign
);
1248 if (TREE_CODE (lhs
) != MEM_REF
1249 || TREE_CODE (TREE_OPERAND (lhs
, 0)) != SSA_NAME
1250 || !is_gimple_reg_type (TREE_TYPE (lhs
)))
1253 /* Prove that we can move the store down. We could also check
1254 TREE_THIS_NOTRAP here, but in that case we also could move stores,
1255 whose value is not available readily, which we want to avoid. */
1256 if (!pointer_set_contains (nontrap
, lhs
))
1259 /* Now we've checked the constraints, so do the transformation:
1260 1) Remove the single store. */
1261 gsi
= gsi_for_stmt (assign
);
1262 unlink_stmt_vdef (assign
);
1263 gsi_remove (&gsi
, true);
1264 release_defs (assign
);
1266 /* 2) Create a temporary where we can store the old content
1267 of the memory touched by the store, if we need to. */
1268 if (!condstoretemp
|| TREE_TYPE (lhs
) != TREE_TYPE (condstoretemp
))
1270 condstoretemp
= create_tmp_reg (TREE_TYPE (lhs
), "cstore");
1271 get_var_ann (condstoretemp
);
1273 add_referenced_var (condstoretemp
);
1275 /* 3) Insert a load from the memory of the store to the temporary
1276 on the edge which did not contain the store. */
1277 lhs
= unshare_expr (lhs
);
1278 new_stmt
= gimple_build_assign (condstoretemp
, lhs
);
1279 name
= make_ssa_name (condstoretemp
, new_stmt
);
1280 gimple_assign_set_lhs (new_stmt
, name
);
1281 gimple_set_location (new_stmt
, locus
);
1282 gsi_insert_on_edge (e1
, new_stmt
);
1284 /* 4) Create a PHI node at the join block, with one argument
1285 holding the old RHS, and the other holding the temporary
1286 where we stored the old memory contents. */
1287 newphi
= create_phi_node (condstoretemp
, join_bb
);
1288 add_phi_arg (newphi
, rhs
, e0
, locus
);
1289 add_phi_arg (newphi
, name
, e1
, locus
);
1291 lhs
= unshare_expr (lhs
);
1292 new_stmt
= gimple_build_assign (lhs
, PHI_RESULT (newphi
));
1294 /* 5) Insert that PHI node. */
1295 gsi
= gsi_after_labels (join_bb
);
1296 if (gsi_end_p (gsi
))
1298 gsi
= gsi_last_bb (join_bb
);
1299 gsi_insert_after (&gsi
, new_stmt
, GSI_NEW_STMT
);
1302 gsi_insert_before (&gsi
, new_stmt
, GSI_NEW_STMT
);
1307 /* Do the main work of conditional store replacement. We already know
1308 that the recognized pattern looks like so:
1311 if (cond) goto THEN_BB; else goto ELSE_BB (edge E1)
1317 fallthrough (edge E0)
1321 We check that THEN_BB and ELSE_BB contain only one store
1322 that the stores have a "simple" RHS. */
1325 cond_if_else_store_replacement (basic_block then_bb
, basic_block else_bb
,
1326 basic_block join_bb
)
1328 gimple then_assign
= last_and_only_stmt (then_bb
);
1329 gimple else_assign
= last_and_only_stmt (else_bb
);
1330 tree lhs_base
, lhs
, then_rhs
, else_rhs
;
1331 source_location then_locus
, else_locus
;
1332 gimple_stmt_iterator gsi
;
1333 gimple newphi
, new_stmt
;
1335 /* Check if then_bb and else_bb contain only one store each. */
1336 if (then_assign
== NULL
1337 || !gimple_assign_single_p (then_assign
)
1338 || else_assign
== NULL
1339 || !gimple_assign_single_p (else_assign
))
1342 lhs
= gimple_assign_lhs (then_assign
);
1343 if (!is_gimple_reg_type (TREE_TYPE (lhs
))
1344 || !operand_equal_p (lhs
, gimple_assign_lhs (else_assign
), 0))
1347 lhs_base
= get_base_address (lhs
);
1348 if (lhs_base
== NULL_TREE
1349 || (!DECL_P (lhs_base
) && TREE_CODE (lhs_base
) != MEM_REF
))
1352 then_rhs
= gimple_assign_rhs1 (then_assign
);
1353 else_rhs
= gimple_assign_rhs1 (else_assign
);
1354 then_locus
= gimple_location (then_assign
);
1355 else_locus
= gimple_location (else_assign
);
1357 /* Now we've checked the constraints, so do the transformation:
1358 1) Remove the stores. */
1359 gsi
= gsi_for_stmt (then_assign
);
1360 unlink_stmt_vdef (then_assign
);
1361 gsi_remove (&gsi
, true);
1362 release_defs (then_assign
);
1364 gsi
= gsi_for_stmt (else_assign
);
1365 unlink_stmt_vdef (else_assign
);
1366 gsi_remove (&gsi
, true);
1367 release_defs (else_assign
);
1369 /* 2) Create a temporary where we can store the old content
1370 of the memory touched by the store, if we need to. */
1371 if (!condstoretemp
|| TREE_TYPE (lhs
) != TREE_TYPE (condstoretemp
))
1373 condstoretemp
= create_tmp_reg (TREE_TYPE (lhs
), "cstore");
1374 get_var_ann (condstoretemp
);
1376 add_referenced_var (condstoretemp
);
1378 /* 3) Create a PHI node at the join block, with one argument
1379 holding the old RHS, and the other holding the temporary
1380 where we stored the old memory contents. */
1381 newphi
= create_phi_node (condstoretemp
, join_bb
);
1382 add_phi_arg (newphi
, then_rhs
, EDGE_SUCC (then_bb
, 0), then_locus
);
1383 add_phi_arg (newphi
, else_rhs
, EDGE_SUCC (else_bb
, 0), else_locus
);
1385 new_stmt
= gimple_build_assign (lhs
, PHI_RESULT (newphi
));
1387 /* 4) Insert that PHI node. */
1388 gsi
= gsi_after_labels (join_bb
);
1389 if (gsi_end_p (gsi
))
1391 gsi
= gsi_last_bb (join_bb
);
1392 gsi_insert_after (&gsi
, new_stmt
, GSI_NEW_STMT
);
1395 gsi_insert_before (&gsi
, new_stmt
, GSI_NEW_STMT
);
1400 /* Always do these optimizations if we have SSA
1401 trees to work on. */
1408 struct gimple_opt_pass pass_phiopt
=
1412 "phiopt", /* name */
1413 gate_phiopt
, /* gate */
1414 tree_ssa_phiopt
, /* execute */
1417 0, /* static_pass_number */
1418 TV_TREE_PHIOPT
, /* tv_id */
1419 PROP_cfg
| PROP_ssa
, /* properties_required */
1420 0, /* properties_provided */
1421 0, /* properties_destroyed */
1422 0, /* todo_flags_start */
1427 | TODO_verify_stmts
/* todo_flags_finish */
1434 return flag_tree_cselim
;
1437 struct gimple_opt_pass pass_cselim
=
1441 "cselim", /* name */
1442 gate_cselim
, /* gate */
1443 tree_ssa_cs_elim
, /* execute */
1446 0, /* static_pass_number */
1447 TV_TREE_PHIOPT
, /* tv_id */
1448 PROP_cfg
| PROP_ssa
, /* properties_required */
1449 0, /* properties_provided */
1450 0, /* properties_destroyed */
1451 0, /* todo_flags_start */
1456 | TODO_verify_stmts
/* todo_flags_finish */