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
);
315 /* Check to make sure that there is only one PHI node.
316 TODO: we could do it with more than one iff the other PHI nodes
317 have the same elements for these two edges. */
318 if (! gimple_seq_singleton_p (phis
))
321 phi
= gsi_stmt (gsi_start (phis
));
322 arg0
= gimple_phi_arg_def (phi
, e1
->dest_idx
);
323 arg1
= gimple_phi_arg_def (phi
, e2
->dest_idx
);
325 /* Something is wrong if we cannot find the arguments in the PHI
327 gcc_assert (arg0
!= NULL
&& arg1
!= NULL
);
329 /* Do the replacement of conditional if it can be done. */
330 if (conditional_replacement (bb
, bb1
, e1
, e2
, phi
, arg0
, arg1
))
332 else if (value_replacement (bb
, bb1
, e1
, e2
, phi
, arg0
, arg1
))
334 else if (abs_replacement (bb
, bb1
, e1
, e2
, phi
, arg0
, arg1
))
336 else if (minmax_replacement (bb
, bb1
, e1
, e2
, phi
, arg0
, arg1
))
344 pointer_set_destroy (nontrap
);
345 /* If the CFG has changed, we should cleanup the CFG. */
346 if (cfgchanged
&& do_store_elim
)
348 /* In cond-store replacement we have added some loads on edges
349 and new VOPS (as we moved the store, and created a load). */
350 gsi_commit_edge_inserts ();
351 return TODO_cleanup_cfg
| TODO_update_ssa_only_virtuals
;
354 return TODO_cleanup_cfg
;
358 /* Returns the list of basic blocks in the function in an order that guarantees
359 that if a block X has just a single predecessor Y, then Y is after X in the
363 blocks_in_phiopt_order (void)
366 basic_block
*order
= XNEWVEC (basic_block
, n_basic_blocks
);
367 unsigned n
= n_basic_blocks
- NUM_FIXED_BLOCKS
;
369 sbitmap visited
= sbitmap_alloc (last_basic_block
);
371 #define MARK_VISITED(BB) (SET_BIT (visited, (BB)->index))
372 #define VISITED_P(BB) (TEST_BIT (visited, (BB)->index))
374 sbitmap_zero (visited
);
376 MARK_VISITED (ENTRY_BLOCK_PTR
);
382 /* Walk the predecessors of x as long as they have precisely one
383 predecessor and add them to the list, so that they get stored
386 single_pred_p (y
) && !VISITED_P (single_pred (y
));
389 for (y
= x
, i
= n
- np
;
390 single_pred_p (y
) && !VISITED_P (single_pred (y
));
391 y
= single_pred (y
), i
++)
399 gcc_assert (i
== n
- 1);
403 sbitmap_free (visited
);
412 /* Return TRUE if block BB has no executable statements, otherwise return
416 empty_block_p (basic_block bb
)
418 /* BB must have no executable statements. */
419 gimple_stmt_iterator gsi
= gsi_after_labels (bb
);
422 if (is_gimple_debug (gsi_stmt (gsi
)))
423 gsi_next_nondebug (&gsi
);
424 return gsi_end_p (gsi
);
427 /* Replace PHI node element whose edge is E in block BB with variable NEW.
428 Remove the edge from COND_BLOCK which does not lead to BB (COND_BLOCK
429 is known to have two edges, one of which must reach BB). */
432 replace_phi_edge_with_variable (basic_block cond_block
,
433 edge e
, gimple phi
, tree new_tree
)
435 basic_block bb
= gimple_bb (phi
);
436 basic_block block_to_remove
;
437 gimple_stmt_iterator gsi
;
439 /* Change the PHI argument to new. */
440 SET_USE (PHI_ARG_DEF_PTR (phi
, e
->dest_idx
), new_tree
);
442 /* Remove the empty basic block. */
443 if (EDGE_SUCC (cond_block
, 0)->dest
== bb
)
445 EDGE_SUCC (cond_block
, 0)->flags
|= EDGE_FALLTHRU
;
446 EDGE_SUCC (cond_block
, 0)->flags
&= ~(EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
);
447 EDGE_SUCC (cond_block
, 0)->probability
= REG_BR_PROB_BASE
;
448 EDGE_SUCC (cond_block
, 0)->count
+= EDGE_SUCC (cond_block
, 1)->count
;
450 block_to_remove
= EDGE_SUCC (cond_block
, 1)->dest
;
454 EDGE_SUCC (cond_block
, 1)->flags
|= EDGE_FALLTHRU
;
455 EDGE_SUCC (cond_block
, 1)->flags
456 &= ~(EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
);
457 EDGE_SUCC (cond_block
, 1)->probability
= REG_BR_PROB_BASE
;
458 EDGE_SUCC (cond_block
, 1)->count
+= EDGE_SUCC (cond_block
, 0)->count
;
460 block_to_remove
= EDGE_SUCC (cond_block
, 0)->dest
;
462 delete_basic_block (block_to_remove
);
464 /* Eliminate the COND_EXPR at the end of COND_BLOCK. */
465 gsi
= gsi_last_bb (cond_block
);
466 gsi_remove (&gsi
, true);
468 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
470 "COND_EXPR in block %d and PHI in block %d converted to straightline code.\n",
475 /* The function conditional_replacement does the main work of doing the
476 conditional replacement. Return true if the replacement is done.
477 Otherwise return false.
478 BB is the basic block where the replacement is going to be done on. ARG0
479 is argument 0 from PHI. Likewise for ARG1. */
482 conditional_replacement (basic_block cond_bb
, basic_block middle_bb
,
483 edge e0
, edge e1
, gimple phi
,
484 tree arg0
, tree arg1
)
487 gimple stmt
, new_stmt
;
489 gimple_stmt_iterator gsi
;
490 edge true_edge
, false_edge
;
491 tree new_var
, new_var2
;
493 /* FIXME: Gimplification of complex type is too hard for now. */
494 if (TREE_CODE (TREE_TYPE (arg0
)) == COMPLEX_TYPE
495 || TREE_CODE (TREE_TYPE (arg1
)) == COMPLEX_TYPE
)
498 /* The PHI arguments have the constants 0 and 1, then convert
499 it to the conditional. */
500 if ((integer_zerop (arg0
) && integer_onep (arg1
))
501 || (integer_zerop (arg1
) && integer_onep (arg0
)))
506 if (!empty_block_p (middle_bb
))
509 /* At this point we know we have a GIMPLE_COND with two successors.
510 One successor is BB, the other successor is an empty block which
511 falls through into BB.
513 There is a single PHI node at the join point (BB) and its arguments
514 are constants (0, 1).
516 So, given the condition COND, and the two PHI arguments, we can
517 rewrite this PHI into non-branching code:
519 dest = (COND) or dest = COND'
521 We use the condition as-is if the argument associated with the
522 true edge has the value one or the argument associated with the
523 false edge as the value zero. Note that those conditions are not
524 the same since only one of the outgoing edges from the GIMPLE_COND
525 will directly reach BB and thus be associated with an argument. */
527 stmt
= last_stmt (cond_bb
);
528 result
= PHI_RESULT (phi
);
530 /* To handle special cases like floating point comparison, it is easier and
531 less error-prone to build a tree and gimplify it on the fly though it is
533 cond
= fold_build2 (gimple_cond_code (stmt
), boolean_type_node
,
534 gimple_cond_lhs (stmt
), gimple_cond_rhs (stmt
));
536 /* We need to know which is the true edge and which is the false
537 edge so that we know when to invert the condition below. */
538 extract_true_false_edges_from_block (cond_bb
, &true_edge
, &false_edge
);
539 if ((e0
== true_edge
&& integer_zerop (arg0
))
540 || (e0
== false_edge
&& integer_onep (arg0
))
541 || (e1
== true_edge
&& integer_zerop (arg1
))
542 || (e1
== false_edge
&& integer_onep (arg1
)))
543 cond
= fold_build1 (TRUTH_NOT_EXPR
, TREE_TYPE (cond
), cond
);
545 /* Insert our new statements at the end of conditional block before the
547 gsi
= gsi_for_stmt (stmt
);
548 new_var
= force_gimple_operand_gsi (&gsi
, cond
, true, NULL
, true,
551 if (!useless_type_conversion_p (TREE_TYPE (result
), TREE_TYPE (new_var
)))
553 source_location locus_0
, locus_1
;
555 new_var2
= create_tmp_var (TREE_TYPE (result
), NULL
);
556 add_referenced_var (new_var2
);
557 new_stmt
= gimple_build_assign_with_ops (CONVERT_EXPR
, new_var2
,
559 new_var2
= make_ssa_name (new_var2
, new_stmt
);
560 gimple_assign_set_lhs (new_stmt
, new_var2
);
561 gsi_insert_before (&gsi
, new_stmt
, GSI_SAME_STMT
);
564 /* Set the locus to the first argument, unless is doesn't have one. */
565 locus_0
= gimple_phi_arg_location (phi
, 0);
566 locus_1
= gimple_phi_arg_location (phi
, 1);
567 if (locus_0
== UNKNOWN_LOCATION
)
569 gimple_set_location (new_stmt
, locus_0
);
572 replace_phi_edge_with_variable (cond_bb
, e1
, phi
, new_var
);
574 /* Note that we optimized this PHI. */
578 /* The function value_replacement does the main work of doing the value
579 replacement. Return true if the replacement is done. Otherwise return
581 BB is the basic block where the replacement is going to be done on. ARG0
582 is argument 0 from the PHI. Likewise for ARG1. */
585 value_replacement (basic_block cond_bb
, basic_block middle_bb
,
586 edge e0
, edge e1
, gimple phi
,
587 tree arg0
, tree arg1
)
590 edge true_edge
, false_edge
;
593 /* If the type says honor signed zeros we cannot do this
595 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1
))))
598 if (!empty_block_p (middle_bb
))
601 cond
= last_stmt (cond_bb
);
602 code
= gimple_cond_code (cond
);
604 /* This transformation is only valid for equality comparisons. */
605 if (code
!= NE_EXPR
&& code
!= EQ_EXPR
)
608 /* We need to know which is the true edge and which is the false
609 edge so that we know if have abs or negative abs. */
610 extract_true_false_edges_from_block (cond_bb
, &true_edge
, &false_edge
);
612 /* At this point we know we have a COND_EXPR with two successors.
613 One successor is BB, the other successor is an empty block which
614 falls through into BB.
616 The condition for the COND_EXPR is known to be NE_EXPR or EQ_EXPR.
618 There is a single PHI node at the join point (BB) with two arguments.
620 We now need to verify that the two arguments in the PHI node match
621 the two arguments to the equality comparison. */
623 if ((operand_equal_for_phi_arg_p (arg0
, gimple_cond_lhs (cond
))
624 && operand_equal_for_phi_arg_p (arg1
, gimple_cond_rhs (cond
)))
625 || (operand_equal_for_phi_arg_p (arg1
, gimple_cond_lhs (cond
))
626 && operand_equal_for_phi_arg_p (arg0
, gimple_cond_rhs (cond
))))
631 /* For NE_EXPR, we want to build an assignment result = arg where
632 arg is the PHI argument associated with the true edge. For
633 EQ_EXPR we want the PHI argument associated with the false edge. */
634 e
= (code
== NE_EXPR
? true_edge
: false_edge
);
636 /* Unfortunately, E may not reach BB (it may instead have gone to
637 OTHER_BLOCK). If that is the case, then we want the single outgoing
638 edge from OTHER_BLOCK which reaches BB and represents the desired
639 path from COND_BLOCK. */
640 if (e
->dest
== middle_bb
)
641 e
= single_succ_edge (e
->dest
);
643 /* Now we know the incoming edge to BB that has the argument for the
644 RHS of our new assignment statement. */
650 replace_phi_edge_with_variable (cond_bb
, e1
, phi
, arg
);
652 /* Note that we optimized this PHI. */
658 /* The function minmax_replacement does the main work of doing the minmax
659 replacement. Return true if the replacement is done. Otherwise return
661 BB is the basic block where the replacement is going to be done on. ARG0
662 is argument 0 from the PHI. Likewise for ARG1. */
665 minmax_replacement (basic_block cond_bb
, basic_block middle_bb
,
666 edge e0
, edge e1
, gimple phi
,
667 tree arg0
, tree arg1
)
670 gimple cond
, new_stmt
;
671 edge true_edge
, false_edge
;
672 enum tree_code cmp
, minmax
, ass_code
;
673 tree smaller
, larger
, arg_true
, arg_false
;
674 gimple_stmt_iterator gsi
, gsi_from
;
676 type
= TREE_TYPE (PHI_RESULT (phi
));
678 /* The optimization may be unsafe due to NaNs. */
679 if (HONOR_NANS (TYPE_MODE (type
)))
682 cond
= last_stmt (cond_bb
);
683 cmp
= gimple_cond_code (cond
);
684 result
= PHI_RESULT (phi
);
686 /* This transformation is only valid for order comparisons. Record which
687 operand is smaller/larger if the result of the comparison is true. */
688 if (cmp
== LT_EXPR
|| cmp
== LE_EXPR
)
690 smaller
= gimple_cond_lhs (cond
);
691 larger
= gimple_cond_rhs (cond
);
693 else if (cmp
== GT_EXPR
|| cmp
== GE_EXPR
)
695 smaller
= gimple_cond_rhs (cond
);
696 larger
= gimple_cond_lhs (cond
);
701 /* We need to know which is the true edge and which is the false
702 edge so that we know if have abs or negative abs. */
703 extract_true_false_edges_from_block (cond_bb
, &true_edge
, &false_edge
);
705 /* Forward the edges over the middle basic block. */
706 if (true_edge
->dest
== middle_bb
)
707 true_edge
= EDGE_SUCC (true_edge
->dest
, 0);
708 if (false_edge
->dest
== middle_bb
)
709 false_edge
= EDGE_SUCC (false_edge
->dest
, 0);
713 gcc_assert (false_edge
== e1
);
719 gcc_assert (false_edge
== e0
);
720 gcc_assert (true_edge
== e1
);
725 if (empty_block_p (middle_bb
))
727 if (operand_equal_for_phi_arg_p (arg_true
, smaller
)
728 && operand_equal_for_phi_arg_p (arg_false
, larger
))
732 if (smaller < larger)
738 else if (operand_equal_for_phi_arg_p (arg_false
, smaller
)
739 && operand_equal_for_phi_arg_p (arg_true
, larger
))
746 /* Recognize the following case, assuming d <= u:
752 This is equivalent to
757 gimple assign
= last_and_only_stmt (middle_bb
);
758 tree lhs
, op0
, op1
, bound
;
761 || gimple_code (assign
) != GIMPLE_ASSIGN
)
764 lhs
= gimple_assign_lhs (assign
);
765 ass_code
= gimple_assign_rhs_code (assign
);
766 if (ass_code
!= MAX_EXPR
&& ass_code
!= MIN_EXPR
)
768 op0
= gimple_assign_rhs1 (assign
);
769 op1
= gimple_assign_rhs2 (assign
);
771 if (true_edge
->src
== middle_bb
)
773 /* We got here if the condition is true, i.e., SMALLER < LARGER. */
774 if (!operand_equal_for_phi_arg_p (lhs
, arg_true
))
777 if (operand_equal_for_phi_arg_p (arg_false
, larger
))
781 if (smaller < larger)
783 r' = MAX_EXPR (smaller, bound)
785 r = PHI <r', larger> --> to be turned to MIN_EXPR. */
786 if (ass_code
!= MAX_EXPR
)
790 if (operand_equal_for_phi_arg_p (op0
, smaller
))
792 else if (operand_equal_for_phi_arg_p (op1
, smaller
))
797 /* We need BOUND <= LARGER. */
798 if (!integer_nonzerop (fold_build2 (LE_EXPR
, boolean_type_node
,
802 else if (operand_equal_for_phi_arg_p (arg_false
, smaller
))
806 if (smaller < larger)
808 r' = MIN_EXPR (larger, bound)
810 r = PHI <r', smaller> --> to be turned to MAX_EXPR. */
811 if (ass_code
!= MIN_EXPR
)
815 if (operand_equal_for_phi_arg_p (op0
, larger
))
817 else if (operand_equal_for_phi_arg_p (op1
, larger
))
822 /* We need BOUND >= SMALLER. */
823 if (!integer_nonzerop (fold_build2 (GE_EXPR
, boolean_type_node
,
832 /* We got here if the condition is false, i.e., SMALLER > LARGER. */
833 if (!operand_equal_for_phi_arg_p (lhs
, arg_false
))
836 if (operand_equal_for_phi_arg_p (arg_true
, larger
))
840 if (smaller > larger)
842 r' = MIN_EXPR (smaller, bound)
844 r = PHI <r', larger> --> to be turned to MAX_EXPR. */
845 if (ass_code
!= MIN_EXPR
)
849 if (operand_equal_for_phi_arg_p (op0
, smaller
))
851 else if (operand_equal_for_phi_arg_p (op1
, smaller
))
856 /* We need BOUND >= LARGER. */
857 if (!integer_nonzerop (fold_build2 (GE_EXPR
, boolean_type_node
,
861 else if (operand_equal_for_phi_arg_p (arg_true
, smaller
))
865 if (smaller > larger)
867 r' = MAX_EXPR (larger, bound)
869 r = PHI <r', smaller> --> to be turned to MIN_EXPR. */
870 if (ass_code
!= MAX_EXPR
)
874 if (operand_equal_for_phi_arg_p (op0
, larger
))
876 else if (operand_equal_for_phi_arg_p (op1
, larger
))
881 /* We need BOUND <= SMALLER. */
882 if (!integer_nonzerop (fold_build2 (LE_EXPR
, boolean_type_node
,
890 /* Move the statement from the middle block. */
891 gsi
= gsi_last_bb (cond_bb
);
892 gsi_from
= gsi_last_nondebug_bb (middle_bb
);
893 gsi_move_before (&gsi_from
, &gsi
);
896 /* Emit the statement to compute min/max. */
897 result
= duplicate_ssa_name (PHI_RESULT (phi
), NULL
);
898 new_stmt
= gimple_build_assign_with_ops (minmax
, result
, arg0
, arg1
);
899 gsi
= gsi_last_bb (cond_bb
);
900 gsi_insert_before (&gsi
, new_stmt
, GSI_NEW_STMT
);
902 replace_phi_edge_with_variable (cond_bb
, e1
, phi
, result
);
906 /* The function absolute_replacement does the main work of doing the absolute
907 replacement. Return true if the replacement is done. Otherwise return
909 bb is the basic block where the replacement is going to be done on. arg0
910 is argument 0 from the phi. Likewise for arg1. */
913 abs_replacement (basic_block cond_bb
, basic_block middle_bb
,
914 edge e0 ATTRIBUTE_UNUSED
, edge e1
,
915 gimple phi
, tree arg0
, tree arg1
)
918 gimple new_stmt
, cond
;
919 gimple_stmt_iterator gsi
;
920 edge true_edge
, false_edge
;
925 enum tree_code cond_code
;
927 /* If the type says honor signed zeros we cannot do this
929 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1
))))
932 /* OTHER_BLOCK must have only one executable statement which must have the
933 form arg0 = -arg1 or arg1 = -arg0. */
935 assign
= last_and_only_stmt (middle_bb
);
936 /* If we did not find the proper negation assignment, then we can not
941 /* If we got here, then we have found the only executable statement
942 in OTHER_BLOCK. If it is anything other than arg = -arg1 or
943 arg1 = -arg0, then we can not optimize. */
944 if (gimple_code (assign
) != GIMPLE_ASSIGN
)
947 lhs
= gimple_assign_lhs (assign
);
949 if (gimple_assign_rhs_code (assign
) != NEGATE_EXPR
)
952 rhs
= gimple_assign_rhs1 (assign
);
954 /* The assignment has to be arg0 = -arg1 or arg1 = -arg0. */
955 if (!(lhs
== arg0
&& rhs
== arg1
)
956 && !(lhs
== arg1
&& rhs
== arg0
))
959 cond
= last_stmt (cond_bb
);
960 result
= PHI_RESULT (phi
);
962 /* Only relationals comparing arg[01] against zero are interesting. */
963 cond_code
= gimple_cond_code (cond
);
964 if (cond_code
!= GT_EXPR
&& cond_code
!= GE_EXPR
965 && cond_code
!= LT_EXPR
&& cond_code
!= LE_EXPR
)
968 /* Make sure the conditional is arg[01] OP y. */
969 if (gimple_cond_lhs (cond
) != rhs
)
972 if (FLOAT_TYPE_P (TREE_TYPE (gimple_cond_rhs (cond
)))
973 ? real_zerop (gimple_cond_rhs (cond
))
974 : integer_zerop (gimple_cond_rhs (cond
)))
979 /* We need to know which is the true edge and which is the false
980 edge so that we know if have abs or negative abs. */
981 extract_true_false_edges_from_block (cond_bb
, &true_edge
, &false_edge
);
983 /* For GT_EXPR/GE_EXPR, if the true edge goes to OTHER_BLOCK, then we
984 will need to negate the result. Similarly for LT_EXPR/LE_EXPR if
985 the false edge goes to OTHER_BLOCK. */
986 if (cond_code
== GT_EXPR
|| cond_code
== GE_EXPR
)
991 if (e
->dest
== middle_bb
)
996 result
= duplicate_ssa_name (result
, NULL
);
1000 tree tmp
= create_tmp_var (TREE_TYPE (result
), NULL
);
1001 add_referenced_var (tmp
);
1002 lhs
= make_ssa_name (tmp
, NULL
);
1007 /* Build the modify expression with abs expression. */
1008 new_stmt
= gimple_build_assign_with_ops (ABS_EXPR
, lhs
, rhs
, NULL
);
1010 gsi
= gsi_last_bb (cond_bb
);
1011 gsi_insert_before (&gsi
, new_stmt
, GSI_NEW_STMT
);
1015 /* Get the right GSI. We want to insert after the recently
1016 added ABS_EXPR statement (which we know is the first statement
1018 new_stmt
= gimple_build_assign_with_ops (NEGATE_EXPR
, result
, lhs
, NULL
);
1020 gsi_insert_after (&gsi
, new_stmt
, GSI_NEW_STMT
);
1023 replace_phi_edge_with_variable (cond_bb
, e1
, phi
, result
);
1025 /* Note that we optimized this PHI. */
1029 /* Auxiliary functions to determine the set of memory accesses which
1030 can't trap because they are preceded by accesses to the same memory
1031 portion. We do that for MEM_REFs, so we only need to track
1032 the SSA_NAME of the pointer indirectly referenced. The algorithm
1033 simply is a walk over all instructions in dominator order. When
1034 we see an MEM_REF we determine if we've already seen a same
1035 ref anywhere up to the root of the dominator tree. If we do the
1036 current access can't trap. If we don't see any dominating access
1037 the current access might trap, but might also make later accesses
1038 non-trapping, so we remember it. We need to be careful with loads
1039 or stores, for instance a load might not trap, while a store would,
1040 so if we see a dominating read access this doesn't mean that a later
1041 write access would not trap. Hence we also need to differentiate the
1042 type of access(es) seen.
1044 ??? We currently are very conservative and assume that a load might
1045 trap even if a store doesn't (write-only memory). This probably is
1046 overly conservative. */
1048 /* A hash-table of SSA_NAMEs, and in which basic block an MEM_REF
1049 through it was seen, which would constitute a no-trap region for
1058 /* The hash table for remembering what we've seen. */
1059 static htab_t seen_ssa_names
;
1061 /* The set of MEM_REFs which can't trap. */
1062 static struct pointer_set_t
*nontrap_set
;
1064 /* The hash function, based on the pointer to the pointer SSA_NAME. */
1066 name_to_bb_hash (const void *p
)
1068 const_tree n
= ((const struct name_to_bb
*)p
)->ssa_name
;
1069 return htab_hash_pointer (n
) ^ ((const struct name_to_bb
*)p
)->store
;
1072 /* The equality function of *P1 and *P2. SSA_NAMEs are shared, so
1073 it's enough to simply compare them for equality. */
1075 name_to_bb_eq (const void *p1
, const void *p2
)
1077 const struct name_to_bb
*n1
= (const struct name_to_bb
*)p1
;
1078 const struct name_to_bb
*n2
= (const struct name_to_bb
*)p2
;
1080 return n1
->ssa_name
== n2
->ssa_name
&& n1
->store
== n2
->store
;
1083 /* We see the expression EXP in basic block BB. If it's an interesting
1084 expression (an MEM_REF through an SSA_NAME) possibly insert the
1085 expression into the set NONTRAP or the hash table of seen expressions.
1086 STORE is true if this expression is on the LHS, otherwise it's on
1089 add_or_mark_expr (basic_block bb
, tree exp
,
1090 struct pointer_set_t
*nontrap
, bool store
)
1092 if (TREE_CODE (exp
) == MEM_REF
1093 && TREE_CODE (TREE_OPERAND (exp
, 0)) == SSA_NAME
)
1095 tree name
= TREE_OPERAND (exp
, 0);
1096 struct name_to_bb map
;
1098 struct name_to_bb
*n2bb
;
1099 basic_block found_bb
= 0;
1101 /* Try to find the last seen MEM_REF through the same
1102 SSA_NAME, which can trap. */
1103 map
.ssa_name
= name
;
1106 slot
= htab_find_slot (seen_ssa_names
, &map
, INSERT
);
1107 n2bb
= (struct name_to_bb
*) *slot
;
1109 found_bb
= n2bb
->bb
;
1111 /* If we've found a trapping MEM_REF, _and_ it dominates EXP
1112 (it's in a basic block on the path from us to the dominator root)
1113 then we can't trap. */
1114 if (found_bb
&& found_bb
->aux
== (void *)1)
1116 pointer_set_insert (nontrap
, exp
);
1120 /* EXP might trap, so insert it into the hash table. */
1127 n2bb
= XNEW (struct name_to_bb
);
1128 n2bb
->ssa_name
= name
;
1130 n2bb
->store
= store
;
1137 /* Called by walk_dominator_tree, when entering the block BB. */
1139 nt_init_block (struct dom_walk_data
*data ATTRIBUTE_UNUSED
, basic_block bb
)
1141 gimple_stmt_iterator gsi
;
1142 /* Mark this BB as being on the path to dominator root. */
1145 /* And walk the statements in order. */
1146 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1148 gimple stmt
= gsi_stmt (gsi
);
1150 if (is_gimple_assign (stmt
))
1152 add_or_mark_expr (bb
, gimple_assign_lhs (stmt
), nontrap_set
, true);
1153 add_or_mark_expr (bb
, gimple_assign_rhs1 (stmt
), nontrap_set
, false);
1154 if (get_gimple_rhs_num_ops (gimple_assign_rhs_code (stmt
)) > 1)
1155 add_or_mark_expr (bb
, gimple_assign_rhs2 (stmt
), nontrap_set
,
1161 /* Called by walk_dominator_tree, when basic block BB is exited. */
1163 nt_fini_block (struct dom_walk_data
*data ATTRIBUTE_UNUSED
, basic_block bb
)
1165 /* This BB isn't on the path to dominator root anymore. */
1169 /* This is the entry point of gathering non trapping memory accesses.
1170 It will do a dominator walk over the whole function, and it will
1171 make use of the bb->aux pointers. It returns a set of trees
1172 (the MEM_REFs itself) which can't trap. */
1173 static struct pointer_set_t
*
1174 get_non_trapping (void)
1176 struct pointer_set_t
*nontrap
;
1177 struct dom_walk_data walk_data
;
1179 nontrap
= pointer_set_create ();
1180 seen_ssa_names
= htab_create (128, name_to_bb_hash
, name_to_bb_eq
,
1182 /* We're going to do a dominator walk, so ensure that we have
1183 dominance information. */
1184 calculate_dominance_info (CDI_DOMINATORS
);
1186 /* Setup callbacks for the generic dominator tree walker. */
1187 nontrap_set
= nontrap
;
1188 walk_data
.dom_direction
= CDI_DOMINATORS
;
1189 walk_data
.initialize_block_local_data
= NULL
;
1190 walk_data
.before_dom_children
= nt_init_block
;
1191 walk_data
.after_dom_children
= nt_fini_block
;
1192 walk_data
.global_data
= NULL
;
1193 walk_data
.block_local_data_size
= 0;
1195 init_walk_dominator_tree (&walk_data
);
1196 walk_dominator_tree (&walk_data
, ENTRY_BLOCK_PTR
);
1197 fini_walk_dominator_tree (&walk_data
);
1198 htab_delete (seen_ssa_names
);
1203 /* Do the main work of conditional store replacement. We already know
1204 that the recognized pattern looks like so:
1207 if (cond) goto MIDDLE_BB; else goto JOIN_BB (edge E1)
1210 fallthrough (edge E0)
1214 We check that MIDDLE_BB contains only one store, that that store
1215 doesn't trap (not via NOTRAP, but via checking if an access to the same
1216 memory location dominates us) and that the store has a "simple" RHS. */
1219 cond_store_replacement (basic_block middle_bb
, basic_block join_bb
,
1220 edge e0
, edge e1
, struct pointer_set_t
*nontrap
)
1222 gimple assign
= last_and_only_stmt (middle_bb
);
1223 tree lhs
, rhs
, name
;
1224 gimple newphi
, new_stmt
;
1225 gimple_stmt_iterator gsi
;
1226 source_location locus
;
1228 /* Check if middle_bb contains of only one store. */
1230 || !gimple_assign_single_p (assign
))
1233 locus
= gimple_location (assign
);
1234 lhs
= gimple_assign_lhs (assign
);
1235 rhs
= gimple_assign_rhs1 (assign
);
1236 if (TREE_CODE (lhs
) != MEM_REF
1237 || TREE_CODE (TREE_OPERAND (lhs
, 0)) != SSA_NAME
1238 || !is_gimple_reg_type (TREE_TYPE (lhs
)))
1241 /* Prove that we can move the store down. We could also check
1242 TREE_THIS_NOTRAP here, but in that case we also could move stores,
1243 whose value is not available readily, which we want to avoid. */
1244 if (!pointer_set_contains (nontrap
, lhs
))
1247 /* Now we've checked the constraints, so do the transformation:
1248 1) Remove the single store. */
1249 gsi
= gsi_for_stmt (assign
);
1250 unlink_stmt_vdef (assign
);
1251 gsi_remove (&gsi
, true);
1252 release_defs (assign
);
1254 /* 2) Create a temporary where we can store the old content
1255 of the memory touched by the store, if we need to. */
1256 if (!condstoretemp
|| TREE_TYPE (lhs
) != TREE_TYPE (condstoretemp
))
1258 condstoretemp
= create_tmp_reg (TREE_TYPE (lhs
), "cstore");
1259 get_var_ann (condstoretemp
);
1261 add_referenced_var (condstoretemp
);
1263 /* 3) Insert a load from the memory of the store to the temporary
1264 on the edge which did not contain the store. */
1265 lhs
= unshare_expr (lhs
);
1266 new_stmt
= gimple_build_assign (condstoretemp
, lhs
);
1267 name
= make_ssa_name (condstoretemp
, new_stmt
);
1268 gimple_assign_set_lhs (new_stmt
, name
);
1269 gimple_set_location (new_stmt
, locus
);
1270 gsi_insert_on_edge (e1
, new_stmt
);
1272 /* 4) Create a PHI node at the join block, with one argument
1273 holding the old RHS, and the other holding the temporary
1274 where we stored the old memory contents. */
1275 newphi
= create_phi_node (condstoretemp
, join_bb
);
1276 add_phi_arg (newphi
, rhs
, e0
, locus
);
1277 add_phi_arg (newphi
, name
, e1
, locus
);
1279 lhs
= unshare_expr (lhs
);
1280 new_stmt
= gimple_build_assign (lhs
, PHI_RESULT (newphi
));
1282 /* 5) Insert that PHI node. */
1283 gsi
= gsi_after_labels (join_bb
);
1284 if (gsi_end_p (gsi
))
1286 gsi
= gsi_last_bb (join_bb
);
1287 gsi_insert_after (&gsi
, new_stmt
, GSI_NEW_STMT
);
1290 gsi_insert_before (&gsi
, new_stmt
, GSI_NEW_STMT
);
1295 /* Do the main work of conditional store replacement. We already know
1296 that the recognized pattern looks like so:
1299 if (cond) goto THEN_BB; else goto ELSE_BB (edge E1)
1305 fallthrough (edge E0)
1309 We check that THEN_BB and ELSE_BB contain only one store
1310 that the stores have a "simple" RHS. */
1313 cond_if_else_store_replacement (basic_block then_bb
, basic_block else_bb
,
1314 basic_block join_bb
)
1316 gimple then_assign
= last_and_only_stmt (then_bb
);
1317 gimple else_assign
= last_and_only_stmt (else_bb
);
1318 tree lhs_base
, lhs
, then_rhs
, else_rhs
;
1319 source_location then_locus
, else_locus
;
1320 gimple_stmt_iterator gsi
;
1321 gimple newphi
, new_stmt
;
1323 /* Check if then_bb and else_bb contain only one store each. */
1324 if (then_assign
== NULL
1325 || !gimple_assign_single_p (then_assign
)
1326 || else_assign
== NULL
1327 || !gimple_assign_single_p (else_assign
))
1330 lhs
= gimple_assign_lhs (then_assign
);
1331 if (!is_gimple_reg_type (TREE_TYPE (lhs
))
1332 || !operand_equal_p (lhs
, gimple_assign_lhs (else_assign
), 0))
1335 lhs_base
= get_base_address (lhs
);
1336 if (lhs_base
== NULL_TREE
1337 || (!DECL_P (lhs_base
) && TREE_CODE (lhs_base
) != MEM_REF
))
1340 then_rhs
= gimple_assign_rhs1 (then_assign
);
1341 else_rhs
= gimple_assign_rhs1 (else_assign
);
1342 then_locus
= gimple_location (then_assign
);
1343 else_locus
= gimple_location (else_assign
);
1345 /* Now we've checked the constraints, so do the transformation:
1346 1) Remove the stores. */
1347 gsi
= gsi_for_stmt (then_assign
);
1348 unlink_stmt_vdef (then_assign
);
1349 gsi_remove (&gsi
, true);
1350 release_defs (then_assign
);
1352 gsi
= gsi_for_stmt (else_assign
);
1353 unlink_stmt_vdef (else_assign
);
1354 gsi_remove (&gsi
, true);
1355 release_defs (else_assign
);
1357 /* 2) Create a temporary where we can store the old content
1358 of the memory touched by the store, if we need to. */
1359 if (!condstoretemp
|| TREE_TYPE (lhs
) != TREE_TYPE (condstoretemp
))
1361 condstoretemp
= create_tmp_reg (TREE_TYPE (lhs
), "cstore");
1362 get_var_ann (condstoretemp
);
1364 add_referenced_var (condstoretemp
);
1366 /* 3) Create a PHI node at the join block, with one argument
1367 holding the old RHS, and the other holding the temporary
1368 where we stored the old memory contents. */
1369 newphi
= create_phi_node (condstoretemp
, join_bb
);
1370 add_phi_arg (newphi
, then_rhs
, EDGE_SUCC (then_bb
, 0), then_locus
);
1371 add_phi_arg (newphi
, else_rhs
, EDGE_SUCC (else_bb
, 0), else_locus
);
1373 new_stmt
= gimple_build_assign (lhs
, PHI_RESULT (newphi
));
1375 /* 4) Insert that PHI node. */
1376 gsi
= gsi_after_labels (join_bb
);
1377 if (gsi_end_p (gsi
))
1379 gsi
= gsi_last_bb (join_bb
);
1380 gsi_insert_after (&gsi
, new_stmt
, GSI_NEW_STMT
);
1383 gsi_insert_before (&gsi
, new_stmt
, GSI_NEW_STMT
);
1388 /* Always do these optimizations if we have SSA
1389 trees to work on. */
1396 struct gimple_opt_pass pass_phiopt
=
1400 "phiopt", /* name */
1401 gate_phiopt
, /* gate */
1402 tree_ssa_phiopt
, /* execute */
1405 0, /* static_pass_number */
1406 TV_TREE_PHIOPT
, /* tv_id */
1407 PROP_cfg
| PROP_ssa
, /* properties_required */
1408 0, /* properties_provided */
1409 0, /* properties_destroyed */
1410 0, /* todo_flags_start */
1415 | TODO_verify_stmts
/* todo_flags_finish */
1422 return flag_tree_cselim
;
1425 struct gimple_opt_pass pass_cselim
=
1429 "cselim", /* name */
1430 gate_cselim
, /* gate */
1431 tree_ssa_cs_elim
, /* execute */
1434 0, /* static_pass_number */
1435 TV_TREE_PHIOPT
, /* tv_id */
1436 PROP_cfg
| PROP_ssa
, /* properties_required */
1437 0, /* properties_provided */
1438 0, /* properties_destroyed */
1439 0, /* todo_flags_start */
1444 | TODO_verify_stmts
/* todo_flags_finish */