1 /* Optimization of PHI nodes by converting them into straightline code.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation,
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 "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"
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,
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,
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. */
184 tree_ssa_phiopt_worker (bool do_store_elim
)
187 basic_block
*bb_order
;
189 bool cfgchanged
= false;
190 struct pointer_set_t
*nontrap
= 0;
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
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
;
218 cond_stmt
= last_stmt (bb
);
219 /* Check to see if the last statement is a GIMPLE_COND. */
221 || gimple_code (cond_stmt
) != GIMPLE_COND
)
224 e1
= EDGE_SUCC (bb
, 0);
226 e2
= EDGE_SUCC (bb
, 1);
229 /* We cannot do the optimization on abnormal edges. */
230 if ((e1
->flags
& EDGE_ABNORMAL
) != 0
231 || (e2
->flags
& EDGE_ABNORMAL
) != 0)
234 /* If either bb1's succ or bb2 or bb2's succ is non NULL. */
235 if (EDGE_COUNT (bb1
->succs
) == 0
237 || EDGE_COUNT (bb2
->succs
) == 0)
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
;
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)
262 /* Also make sure that bb1 only have one predecessor and that it
264 if (!single_pred_p (bb1
)
265 || single_pred (bb1
) != bb
)
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)
275 if (cond_store_replacement (bb1
, bb2
, e1
, e2
, nontrap
))
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
))
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
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
))
299 else if (value_replacement (bb
, bb1
, e1
, e2
, phi
, arg0
, arg1
))
301 else if (abs_replacement (bb
, bb1
, e1
, e2
, phi
, arg0
, arg1
))
303 else if (minmax_replacement (bb
, bb1
, e1
, e2
, phi
, arg0
, arg1
))
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
;
321 return TODO_cleanup_cfg
;
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
330 blocks_in_phiopt_order (void)
333 basic_block
*order
= XNEWVEC (basic_block
, n_basic_blocks
);
334 unsigned n
= n_basic_blocks
- NUM_FIXED_BLOCKS
;
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
);
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
353 single_pred_p (y
) && !VISITED_P (single_pred (y
));
356 for (y
= x
, i
= n
- np
;
357 single_pred_p (y
) && !VISITED_P (single_pred (y
));
358 y
= single_pred (y
), i
++)
366 gcc_assert (i
== n
- 1);
370 sbitmap_free (visited
);
379 /* Return TRUE if block BB has no executable statements, otherwise return
383 empty_block_p (basic_block bb
)
385 /* BB must have no executable statements. */
386 gimple_stmt_iterator gsi
= gsi_after_labels (bb
);
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). */
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
;
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
))
437 "COND_EXPR in block %d and PHI in block %d converted to straightline code.\n",
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. */
449 conditional_replacement (basic_block cond_bb
, basic_block middle_bb
,
450 edge e0
, edge e1
, gimple phi
,
451 tree arg0
, tree arg1
)
454 gimple stmt
, new_stmt
;
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
)
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
)))
473 if (!empty_block_p (middle_bb
))
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
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
514 gsi
= gsi_for_stmt (stmt
);
515 new_var
= force_gimple_operand_gsi (&gsi
, cond
, true, NULL
, true,
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
,
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
);
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
)
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. */
545 /* The function value_replacement does the main work of doing the value
546 replacement. Return true if the replacement is done. Otherwise return
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. */
552 value_replacement (basic_block cond_bb
, basic_block middle_bb
,
553 edge e0
, edge e1
, gimple phi
,
554 tree arg0
, tree arg1
)
557 edge true_edge
, false_edge
;
560 /* If the type says honor signed zeros we cannot do this
562 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1
))))
565 if (!empty_block_p (middle_bb
))
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
)
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
))))
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. */
617 replace_phi_edge_with_variable (cond_bb
, e1
, phi
, arg
);
619 /* Note that we optimized this PHI. */
625 /* The function minmax_replacement does the main work of doing the minmax
626 replacement. Return true if the replacement is done. Otherwise return
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. */
632 minmax_replacement (basic_block cond_bb
, basic_block middle_bb
,
633 edge e0
, edge e1
, gimple phi
,
634 tree arg0
, tree arg1
)
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
)))
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
);
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);
680 gcc_assert (false_edge
== e1
);
686 gcc_assert (false_edge
== e0
);
687 gcc_assert (true_edge
== e1
);
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
))
699 if (smaller < larger)
705 else if (operand_equal_for_phi_arg_p (arg_false
, smaller
)
706 && operand_equal_for_phi_arg_p (arg_true
, larger
))
713 /* Recognize the following case, assuming d <= u:
719 This is equivalent to
724 gimple assign
= last_and_only_stmt (middle_bb
);
725 tree lhs
, op0
, op1
, bound
;
728 || gimple_code (assign
) != GIMPLE_ASSIGN
)
731 lhs
= gimple_assign_lhs (assign
);
732 ass_code
= gimple_assign_rhs_code (assign
);
733 if (ass_code
!= MAX_EXPR
&& ass_code
!= MIN_EXPR
)
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
))
744 if (operand_equal_for_phi_arg_p (arg_false
, larger
))
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
)
757 if (operand_equal_for_phi_arg_p (op0
, smaller
))
759 else if (operand_equal_for_phi_arg_p (op1
, smaller
))
764 /* We need BOUND <= LARGER. */
765 if (!integer_nonzerop (fold_build2 (LE_EXPR
, boolean_type_node
,
769 else if (operand_equal_for_phi_arg_p (arg_false
, smaller
))
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
)
782 if (operand_equal_for_phi_arg_p (op0
, larger
))
784 else if (operand_equal_for_phi_arg_p (op1
, larger
))
789 /* We need BOUND >= SMALLER. */
790 if (!integer_nonzerop (fold_build2 (GE_EXPR
, boolean_type_node
,
799 /* We got here if the condition is false, i.e., SMALLER > LARGER. */
800 if (!operand_equal_for_phi_arg_p (lhs
, arg_false
))
803 if (operand_equal_for_phi_arg_p (arg_true
, larger
))
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
)
816 if (operand_equal_for_phi_arg_p (op0
, smaller
))
818 else if (operand_equal_for_phi_arg_p (op1
, smaller
))
823 /* We need BOUND >= LARGER. */
824 if (!integer_nonzerop (fold_build2 (GE_EXPR
, boolean_type_node
,
828 else if (operand_equal_for_phi_arg_p (arg_true
, smaller
))
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
)
841 if (operand_equal_for_phi_arg_p (op0
, larger
))
843 else if (operand_equal_for_phi_arg_p (op1
, larger
))
848 /* We need BOUND <= SMALLER. */
849 if (!integer_nonzerop (fold_build2 (LE_EXPR
, boolean_type_node
,
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
);
873 /* The function absolute_replacement does the main work of doing the absolute
874 replacement. Return true if the replacement is done. Otherwise return
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. */
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
)
885 gimple new_stmt
, cond
;
886 gimple_stmt_iterator gsi
;
887 edge true_edge
, false_edge
;
892 enum tree_code cond_code
;
894 /* If the type says honor signed zeros we cannot do this
896 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1
))))
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
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
)
914 lhs
= gimple_assign_lhs (assign
);
916 if (gimple_assign_rhs_code (assign
) != NEGATE_EXPR
)
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
))
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
)
935 /* Make sure the conditional is arg[01] OP y. */
936 if (gimple_cond_lhs (cond
) != rhs
)
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
)))
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
)
958 if (e
->dest
== middle_bb
)
963 result
= duplicate_ssa_name (result
, NULL
);
967 tree tmp
= create_tmp_var (TREE_TYPE (result
), NULL
);
968 add_referenced_var (tmp
);
969 lhs
= make_ssa_name (tmp
, NULL
);
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
);
982 /* Get the right GSI. We want to insert after the recently
983 added ABS_EXPR statement (which we know is the first statement
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. */
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
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. */
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. */
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
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
;
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
;
1073 slot
= htab_find_slot (seen_ssa_names
, &map
, INSERT
);
1074 n2bb
= (struct name_to_bb
*) *slot
;
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
);
1087 /* EXP might trap, so insert it into the hash table. */
1094 n2bb
= XNEW (struct name_to_bb
);
1095 n2bb
->ssa_name
= name
;
1097 n2bb
->store
= store
;
1104 /* Called by walk_dominator_tree, when entering the block BB. */
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. */
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
,
1128 /* Called by walk_dominator_tree, when basic block BB is exited. */
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. */
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
,
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
);
1170 /* Do the main work of conditional store replacement. We already know
1171 that the recognized pattern looks like so:
1174 if (cond) goto MIDDLE_BB; else goto JOIN_BB (edge E1)
1177 fallthrough (edge E0)
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. */
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. */
1198 || gimple_code (assign
) != GIMPLE_ASSIGN
)
1201 locus
= gimple_location (assign
);
1202 lhs
= gimple_assign_lhs (assign
);
1203 rhs
= gimple_assign_rhs1 (assign
);
1204 if (!INDIRECT_REF_P (lhs
))
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
)))
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
))
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
);
1262 gsi_insert_before (&gsi
, new_stmt
, GSI_NEW_STMT
);
1267 /* Always do these optimizations if we have SSA
1268 trees to work on. */
1275 struct gimple_opt_pass pass_phiopt
=
1279 "phiopt", /* name */
1280 gate_phiopt
, /* gate */
1281 tree_ssa_phiopt
, /* execute */
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 */
1294 | TODO_verify_stmts
/* todo_flags_finish */
1301 return flag_tree_cselim
;
1304 struct gimple_opt_pass pass_cselim
=
1308 "cselim", /* name */
1309 gate_cselim
, /* gate */
1310 tree_ssa_cs_elim
, /* execute */
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 */
1323 | TODO_verify_stmts
/* todo_flags_finish */