1 /* Optimization of PHI nodes by converting them into straightline code.
2 Copyright (C) 2004, 2005, 2007 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #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"
37 static unsigned int tree_ssa_phiopt (void);
38 static bool conditional_replacement (basic_block
, basic_block
,
39 edge
, edge
, tree
, tree
, tree
);
40 static bool value_replacement (basic_block
, basic_block
,
41 edge
, edge
, tree
, tree
, tree
);
42 static bool minmax_replacement (basic_block
, basic_block
,
43 edge
, edge
, tree
, tree
, tree
);
44 static bool abs_replacement (basic_block
, basic_block
,
45 edge
, edge
, tree
, tree
, tree
);
46 static void replace_phi_edge_with_variable (basic_block
, edge
, tree
, tree
);
47 static basic_block
*blocks_in_phiopt_order (void);
49 /* This pass tries to replaces an if-then-else block with an
50 assignment. We have four kinds of transformations. Some of these
51 transformations are also performed by the ifcvt RTL optimizer.
53 Conditional Replacement
54 -----------------------
56 This transformation, implemented in conditional_replacement,
60 if (cond) goto bb2; else goto bb1;
63 x = PHI <0 (bb1), 1 (bb0), ...>;
71 x = PHI <x' (bb0), ...>;
73 We remove bb1 as it becomes unreachable. This occurs often due to
74 gimplification of conditionals.
79 This transformation, implemented in value_replacement, replaces
82 if (a != b) goto bb2; else goto bb1;
85 x = PHI <a (bb1), b (bb0), ...>;
91 x = PHI <b (bb0), ...>;
93 This opportunity can sometimes occur as a result of other
99 This transformation, implemented in abs_replacement, replaces
102 if (a >= 0) goto bb2; else goto bb1;
106 x = PHI <x (bb1), a (bb0), ...>;
113 x = PHI <x' (bb0), ...>;
118 This transformation, minmax_replacement replaces
121 if (a <= b) goto bb2; else goto bb1;
124 x = PHI <b (bb1), a (bb0), ...>;
131 x = PHI <x' (bb0), ...>;
133 A similar transformation is done for MAX_EXPR. */
136 tree_ssa_phiopt (void)
139 basic_block
*bb_order
;
141 bool cfgchanged
= false;
143 /* Search every basic block for COND_EXPR we may be able to optimize.
145 We walk the blocks in order that guarantees that a block with
146 a single predecessor is processed before the predecessor.
147 This ensures that we collapse inner ifs before visiting the
148 outer ones, and also that we do not try to visit a removed
150 bb_order
= blocks_in_phiopt_order ();
151 n
= n_basic_blocks
- NUM_FIXED_BLOCKS
;
153 for (i
= 0; i
< n
; i
++)
157 basic_block bb1
, bb2
;
163 cond_expr
= last_stmt (bb
);
164 /* Check to see if the last statement is a COND_EXPR. */
166 || TREE_CODE (cond_expr
) != COND_EXPR
)
169 e1
= EDGE_SUCC (bb
, 0);
171 e2
= EDGE_SUCC (bb
, 1);
174 /* We cannot do the optimization on abnormal edges. */
175 if ((e1
->flags
& EDGE_ABNORMAL
) != 0
176 || (e2
->flags
& EDGE_ABNORMAL
) != 0)
179 /* If either bb1's succ or bb2 or bb2's succ is non NULL. */
180 if (EDGE_COUNT (bb1
->succs
) == 0
182 || EDGE_COUNT (bb2
->succs
) == 0)
185 /* Find the bb which is the fall through to the other. */
186 if (EDGE_SUCC (bb1
, 0)->dest
== bb2
)
188 else if (EDGE_SUCC (bb2
, 0)->dest
== bb1
)
190 basic_block bb_tmp
= bb1
;
200 e1
= EDGE_SUCC (bb1
, 0);
202 /* Make sure that bb1 is just a fall through. */
203 if (!single_succ_p (bb1
)
204 || (e1
->flags
& EDGE_FALLTHRU
) == 0)
207 /* Also make sure that bb1 only have one predecessor and that it
209 if (!single_pred_p (bb1
)
210 || single_pred (bb1
) != bb
)
213 phi
= phi_nodes (bb2
);
215 /* Check to make sure that there is only one PHI node.
216 TODO: we could do it with more than one iff the other PHI nodes
217 have the same elements for these two edges. */
218 if (!phi
|| PHI_CHAIN (phi
) != NULL
)
221 arg0
= PHI_ARG_DEF_TREE (phi
, e1
->dest_idx
);
222 arg1
= PHI_ARG_DEF_TREE (phi
, e2
->dest_idx
);
224 /* Something is wrong if we cannot find the arguments in the PHI
226 gcc_assert (arg0
!= NULL
&& arg1
!= NULL
);
228 /* Do the replacement of conditional if it can be done. */
229 if (conditional_replacement (bb
, bb1
, e1
, e2
, phi
, arg0
, arg1
))
231 else if (value_replacement (bb
, bb1
, e1
, e2
, phi
, arg0
, arg1
))
233 else if (abs_replacement (bb
, bb1
, e1
, e2
, phi
, arg0
, arg1
))
235 else if (minmax_replacement (bb
, bb1
, e1
, e2
, phi
, arg0
, arg1
))
241 /* If the CFG has changed, we should cleanup the CFG. */
242 return cfgchanged
? TODO_cleanup_cfg
: 0;
245 /* Returns the list of basic blocks in the function in an order that guarantees
246 that if a block X has just a single predecessor Y, then Y is after X in the
250 blocks_in_phiopt_order (void)
253 basic_block
*order
= XNEWVEC (basic_block
, n_basic_blocks
);
254 unsigned n
= n_basic_blocks
- NUM_FIXED_BLOCKS
;
256 sbitmap visited
= sbitmap_alloc (last_basic_block
);
258 #define MARK_VISITED(BB) (SET_BIT (visited, (BB)->index))
259 #define VISITED_P(BB) (TEST_BIT (visited, (BB)->index))
261 sbitmap_zero (visited
);
263 MARK_VISITED (ENTRY_BLOCK_PTR
);
269 /* Walk the predecessors of x as long as they have precisely one
270 predecessor and add them to the list, so that they get stored
273 single_pred_p (y
) && !VISITED_P (single_pred (y
));
276 for (y
= x
, i
= n
- np
;
277 single_pred_p (y
) && !VISITED_P (single_pred (y
));
278 y
= single_pred (y
), i
++)
286 gcc_assert (i
== n
- 1);
290 sbitmap_free (visited
);
298 /* Return TRUE if block BB has no executable statements, otherwise return
301 empty_block_p (basic_block bb
)
303 block_stmt_iterator bsi
;
305 /* BB must have no executable statements. */
306 bsi
= bsi_start (bb
);
307 while (!bsi_end_p (bsi
)
308 && (TREE_CODE (bsi_stmt (bsi
)) == LABEL_EXPR
309 || IS_EMPTY_STMT (bsi_stmt (bsi
))))
312 if (!bsi_end_p (bsi
))
318 /* Replace PHI node element whose edge is E in block BB with variable NEW.
319 Remove the edge from COND_BLOCK which does not lead to BB (COND_BLOCK
320 is known to have two edges, one of which must reach BB). */
323 replace_phi_edge_with_variable (basic_block cond_block
,
324 edge e
, tree phi
, tree
new)
326 basic_block bb
= bb_for_stmt (phi
);
327 basic_block block_to_remove
;
328 block_stmt_iterator bsi
;
330 /* Change the PHI argument to new. */
331 SET_USE (PHI_ARG_DEF_PTR (phi
, e
->dest_idx
), new);
333 /* Remove the empty basic block. */
334 if (EDGE_SUCC (cond_block
, 0)->dest
== bb
)
336 EDGE_SUCC (cond_block
, 0)->flags
|= EDGE_FALLTHRU
;
337 EDGE_SUCC (cond_block
, 0)->flags
&= ~(EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
);
338 EDGE_SUCC (cond_block
, 0)->probability
= REG_BR_PROB_BASE
;
339 EDGE_SUCC (cond_block
, 0)->count
+= EDGE_SUCC (cond_block
, 1)->count
;
341 block_to_remove
= EDGE_SUCC (cond_block
, 1)->dest
;
345 EDGE_SUCC (cond_block
, 1)->flags
|= EDGE_FALLTHRU
;
346 EDGE_SUCC (cond_block
, 1)->flags
347 &= ~(EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
);
348 EDGE_SUCC (cond_block
, 1)->probability
= REG_BR_PROB_BASE
;
349 EDGE_SUCC (cond_block
, 1)->count
+= EDGE_SUCC (cond_block
, 0)->count
;
351 block_to_remove
= EDGE_SUCC (cond_block
, 0)->dest
;
353 delete_basic_block (block_to_remove
);
355 /* Eliminate the COND_EXPR at the end of COND_BLOCK. */
356 bsi
= bsi_last (cond_block
);
357 bsi_remove (&bsi
, true);
359 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
361 "COND_EXPR in block %d and PHI in block %d converted to straightline code.\n",
366 /* The function conditional_replacement does the main work of doing the
367 conditional replacement. Return true if the replacement is done.
368 Otherwise return false.
369 BB is the basic block where the replacement is going to be done on. ARG0
370 is argument 0 from PHI. Likewise for ARG1. */
373 conditional_replacement (basic_block cond_bb
, basic_block middle_bb
,
374 edge e0
, edge e1
, tree phi
,
375 tree arg0
, tree arg1
)
378 tree old_result
= NULL
;
380 block_stmt_iterator bsi
;
381 edge true_edge
, false_edge
;
385 /* The PHI arguments have the constants 0 and 1, then convert
386 it to the conditional. */
387 if ((integer_zerop (arg0
) && integer_onep (arg1
))
388 || (integer_zerop (arg1
) && integer_onep (arg0
)))
393 if (!empty_block_p (middle_bb
))
396 /* If the condition is not a naked SSA_NAME and its type does not
397 match the type of the result, then we have to create a new
398 variable to optimize this case as it would likely create
399 non-gimple code when the condition was converted to the
401 cond
= COND_EXPR_COND (last_stmt (cond_bb
));
402 result
= PHI_RESULT (phi
);
403 if (TREE_CODE (cond
) != SSA_NAME
404 && !lang_hooks
.types_compatible_p (TREE_TYPE (cond
), TREE_TYPE (result
)))
408 if (!COMPARISON_CLASS_P (cond
))
411 tmp
= create_tmp_var (TREE_TYPE (cond
), NULL
);
412 add_referenced_var (tmp
);
413 new_var
= make_ssa_name (tmp
, NULL
);
418 /* If the condition was a naked SSA_NAME and the type is not the
419 same as the type of the result, then convert the type of the
421 if (!lang_hooks
.types_compatible_p (TREE_TYPE (cond
), TREE_TYPE (result
)))
422 cond
= fold_convert (TREE_TYPE (result
), cond
);
424 /* We need to know which is the true edge and which is the false
425 edge so that we know when to invert the condition below. */
426 extract_true_false_edges_from_block (cond_bb
, &true_edge
, &false_edge
);
428 /* Insert our new statement at the end of conditional block before the
430 bsi
= bsi_last (cond_bb
);
431 bsi_insert_before (&bsi
, build_empty_stmt (), BSI_NEW_STMT
);
437 new1
= build2 (TREE_CODE (old_result
), TREE_TYPE (old_result
),
438 TREE_OPERAND (old_result
, 0),
439 TREE_OPERAND (old_result
, 1));
441 new1
= build2 (MODIFY_EXPR
, TREE_TYPE (old_result
), new_var
, new1
);
442 SSA_NAME_DEF_STMT (new_var
) = new1
;
444 bsi_insert_after (&bsi
, new1
, BSI_NEW_STMT
);
447 new_var1
= duplicate_ssa_name (PHI_RESULT (phi
), NULL
);
450 /* At this point we know we have a COND_EXPR with two successors.
451 One successor is BB, the other successor is an empty block which
452 falls through into BB.
454 There is a single PHI node at the join point (BB) and its arguments
455 are constants (0, 1).
457 So, given the condition COND, and the two PHI arguments, we can
458 rewrite this PHI into non-branching code:
460 dest = (COND) or dest = COND'
462 We use the condition as-is if the argument associated with the
463 true edge has the value one or the argument associated with the
464 false edge as the value zero. Note that those conditions are not
465 the same since only one of the outgoing edges from the COND_EXPR
466 will directly reach BB and thus be associated with an argument. */
467 if ((e0
== true_edge
&& integer_onep (arg0
))
468 || (e0
== false_edge
&& integer_zerop (arg0
))
469 || (e1
== true_edge
&& integer_onep (arg1
))
470 || (e1
== false_edge
&& integer_zerop (arg1
)))
472 new = build2 (MODIFY_EXPR
, TREE_TYPE (new_var1
), new_var1
, cond
);
476 tree cond1
= invert_truthvalue (cond
);
480 /* If what we get back is a conditional expression, there is no
481 way that it can be gimple. */
482 if (TREE_CODE (cond
) == COND_EXPR
)
484 release_ssa_name (new_var1
);
488 /* If COND is not something we can expect to be reducible to a GIMPLE
489 condition, return early. */
490 if (is_gimple_cast (cond
))
491 cond1
= TREE_OPERAND (cond
, 0);
492 if (TREE_CODE (cond1
) == TRUTH_NOT_EXPR
493 && !is_gimple_val (TREE_OPERAND (cond1
, 0)))
495 release_ssa_name (new_var1
);
499 /* If what we get back is not gimple try to create it as gimple by
500 using a temporary variable. */
501 if (is_gimple_cast (cond
)
502 && !is_gimple_val (TREE_OPERAND (cond
, 0)))
504 tree op0
, tmp
, cond_tmp
;
506 /* Only "real" casts are OK here, not everything that is
507 acceptable to is_gimple_cast. Make sure we don't do
508 anything stupid here. */
509 gcc_assert (TREE_CODE (cond
) == NOP_EXPR
510 || TREE_CODE (cond
) == CONVERT_EXPR
);
512 op0
= TREE_OPERAND (cond
, 0);
513 tmp
= create_tmp_var (TREE_TYPE (op0
), NULL
);
514 add_referenced_var (tmp
);
515 cond_tmp
= make_ssa_name (tmp
, NULL
);
516 new = build2 (MODIFY_EXPR
, TREE_TYPE (cond_tmp
), cond_tmp
, op0
);
517 SSA_NAME_DEF_STMT (cond_tmp
) = new;
519 bsi_insert_after (&bsi
, new, BSI_NEW_STMT
);
520 cond
= fold_convert (TREE_TYPE (result
), cond_tmp
);
523 new = build2 (MODIFY_EXPR
, TREE_TYPE (new_var1
), new_var1
, cond
);
526 bsi_insert_after (&bsi
, new, BSI_NEW_STMT
);
528 SSA_NAME_DEF_STMT (new_var1
) = new;
530 replace_phi_edge_with_variable (cond_bb
, e1
, phi
, new_var1
);
532 /* Note that we optimized this PHI. */
536 /* The function value_replacement does the main work of doing the value
537 replacement. Return true if the replacement is done. Otherwise return
539 BB is the basic block where the replacement is going to be done on. ARG0
540 is argument 0 from the PHI. Likewise for ARG1. */
543 value_replacement (basic_block cond_bb
, basic_block middle_bb
,
544 edge e0
, edge e1
, tree phi
,
545 tree arg0
, tree arg1
)
548 edge true_edge
, false_edge
;
550 /* If the type says honor signed zeros we cannot do this
552 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1
))))
555 if (!empty_block_p (middle_bb
))
558 cond
= COND_EXPR_COND (last_stmt (cond_bb
));
560 /* This transformation is only valid for equality comparisons. */
561 if (TREE_CODE (cond
) != NE_EXPR
&& TREE_CODE (cond
) != EQ_EXPR
)
564 /* We need to know which is the true edge and which is the false
565 edge so that we know if have abs or negative abs. */
566 extract_true_false_edges_from_block (cond_bb
, &true_edge
, &false_edge
);
568 /* At this point we know we have a COND_EXPR with two successors.
569 One successor is BB, the other successor is an empty block which
570 falls through into BB.
572 The condition for the COND_EXPR is known to be NE_EXPR or EQ_EXPR.
574 There is a single PHI node at the join point (BB) with two arguments.
576 We now need to verify that the two arguments in the PHI node match
577 the two arguments to the equality comparison. */
579 if ((operand_equal_for_phi_arg_p (arg0
, TREE_OPERAND (cond
, 0))
580 && operand_equal_for_phi_arg_p (arg1
, TREE_OPERAND (cond
, 1)))
581 || (operand_equal_for_phi_arg_p (arg1
, TREE_OPERAND (cond
, 0))
582 && operand_equal_for_phi_arg_p (arg0
, TREE_OPERAND (cond
, 1))))
587 /* For NE_EXPR, we want to build an assignment result = arg where
588 arg is the PHI argument associated with the true edge. For
589 EQ_EXPR we want the PHI argument associated with the false edge. */
590 e
= (TREE_CODE (cond
) == NE_EXPR
? true_edge
: false_edge
);
592 /* Unfortunately, E may not reach BB (it may instead have gone to
593 OTHER_BLOCK). If that is the case, then we want the single outgoing
594 edge from OTHER_BLOCK which reaches BB and represents the desired
595 path from COND_BLOCK. */
596 if (e
->dest
== middle_bb
)
597 e
= single_succ_edge (e
->dest
);
599 /* Now we know the incoming edge to BB that has the argument for the
600 RHS of our new assignment statement. */
606 replace_phi_edge_with_variable (cond_bb
, e1
, phi
, arg
);
608 /* Note that we optimized this PHI. */
614 /* The function minmax_replacement does the main work of doing the minmax
615 replacement. Return true if the replacement is done. Otherwise return
617 BB is the basic block where the replacement is going to be done on. ARG0
618 is argument 0 from the PHI. Likewise for ARG1. */
621 minmax_replacement (basic_block cond_bb
, basic_block middle_bb
,
622 edge e0
, edge e1
, tree phi
,
623 tree arg0
, tree arg1
)
627 edge true_edge
, false_edge
;
628 enum tree_code cmp
, minmax
, ass_code
;
629 tree smaller
, larger
, arg_true
, arg_false
;
630 block_stmt_iterator bsi
, bsi_from
;
632 type
= TREE_TYPE (PHI_RESULT (phi
));
634 /* The optimization may be unsafe due to NaNs. */
635 if (HONOR_NANS (TYPE_MODE (type
)))
638 cond
= COND_EXPR_COND (last_stmt (cond_bb
));
639 cmp
= TREE_CODE (cond
);
640 result
= PHI_RESULT (phi
);
642 /* This transformation is only valid for order comparisons. Record which
643 operand is smaller/larger if the result of the comparison is true. */
644 if (cmp
== LT_EXPR
|| cmp
== LE_EXPR
)
646 smaller
= TREE_OPERAND (cond
, 0);
647 larger
= TREE_OPERAND (cond
, 1);
649 else if (cmp
== GT_EXPR
|| cmp
== GE_EXPR
)
651 smaller
= TREE_OPERAND (cond
, 1);
652 larger
= TREE_OPERAND (cond
, 0);
657 /* We need to know which is the true edge and which is the false
658 edge so that we know if have abs or negative abs. */
659 extract_true_false_edges_from_block (cond_bb
, &true_edge
, &false_edge
);
661 /* Forward the edges over the middle basic block. */
662 if (true_edge
->dest
== middle_bb
)
663 true_edge
= EDGE_SUCC (true_edge
->dest
, 0);
664 if (false_edge
->dest
== middle_bb
)
665 false_edge
= EDGE_SUCC (false_edge
->dest
, 0);
669 gcc_assert (false_edge
== e1
);
675 gcc_assert (false_edge
== e0
);
676 gcc_assert (true_edge
== e1
);
681 if (empty_block_p (middle_bb
))
683 if (operand_equal_for_phi_arg_p (arg_true
, smaller
)
684 && operand_equal_for_phi_arg_p (arg_false
, larger
))
688 if (smaller < larger)
694 else if (operand_equal_for_phi_arg_p (arg_false
, smaller
)
695 && operand_equal_for_phi_arg_p (arg_true
, larger
))
702 /* Recognize the following case, assuming d <= u:
708 This is equivalent to
713 tree assign
= last_and_only_stmt (middle_bb
);
714 tree lhs
, rhs
, op0
, op1
, bound
;
717 || TREE_CODE (assign
) != MODIFY_EXPR
)
720 lhs
= TREE_OPERAND (assign
, 0);
721 rhs
= TREE_OPERAND (assign
, 1);
722 ass_code
= TREE_CODE (rhs
);
723 if (ass_code
!= MAX_EXPR
&& ass_code
!= MIN_EXPR
)
725 op0
= TREE_OPERAND (rhs
, 0);
726 op1
= TREE_OPERAND (rhs
, 1);
728 if (true_edge
->src
== middle_bb
)
730 /* We got here if the condition is true, i.e., SMALLER < LARGER. */
731 if (!operand_equal_for_phi_arg_p (lhs
, arg_true
))
734 if (operand_equal_for_phi_arg_p (arg_false
, larger
))
738 if (smaller < larger)
740 r' = MAX_EXPR (smaller, bound)
742 r = PHI <r', larger> --> to be turned to MIN_EXPR. */
743 if (ass_code
!= MAX_EXPR
)
747 if (operand_equal_for_phi_arg_p (op0
, smaller
))
749 else if (operand_equal_for_phi_arg_p (op1
, smaller
))
754 /* We need BOUND <= LARGER. */
755 if (!integer_nonzerop (fold_build2 (LE_EXPR
, boolean_type_node
,
759 else if (operand_equal_for_phi_arg_p (arg_false
, smaller
))
763 if (smaller < larger)
765 r' = MIN_EXPR (larger, bound)
767 r = PHI <r', smaller> --> to be turned to MAX_EXPR. */
768 if (ass_code
!= MIN_EXPR
)
772 if (operand_equal_for_phi_arg_p (op0
, larger
))
774 else if (operand_equal_for_phi_arg_p (op1
, larger
))
779 /* We need BOUND >= SMALLER. */
780 if (!integer_nonzerop (fold_build2 (GE_EXPR
, boolean_type_node
,
789 /* We got here if the condition is false, i.e., SMALLER > LARGER. */
790 if (!operand_equal_for_phi_arg_p (lhs
, arg_false
))
793 if (operand_equal_for_phi_arg_p (arg_true
, larger
))
797 if (smaller > larger)
799 r' = MIN_EXPR (smaller, bound)
801 r = PHI <r', larger> --> to be turned to MAX_EXPR. */
802 if (ass_code
!= MIN_EXPR
)
806 if (operand_equal_for_phi_arg_p (op0
, smaller
))
808 else if (operand_equal_for_phi_arg_p (op1
, smaller
))
813 /* We need BOUND >= LARGER. */
814 if (!integer_nonzerop (fold_build2 (GE_EXPR
, boolean_type_node
,
818 else if (operand_equal_for_phi_arg_p (arg_true
, smaller
))
822 if (smaller > larger)
824 r' = MAX_EXPR (larger, bound)
826 r = PHI <r', smaller> --> to be turned to MIN_EXPR. */
827 if (ass_code
!= MAX_EXPR
)
831 if (operand_equal_for_phi_arg_p (op0
, larger
))
833 else if (operand_equal_for_phi_arg_p (op1
, larger
))
838 /* We need BOUND <= SMALLER. */
839 if (!integer_nonzerop (fold_build2 (LE_EXPR
, boolean_type_node
,
847 /* Move the statement from the middle block. */
848 bsi
= bsi_last (cond_bb
);
849 bsi_from
= bsi_last (middle_bb
);
850 bsi_move_before (&bsi_from
, &bsi
);
853 /* Emit the statement to compute min/max. */
854 result
= duplicate_ssa_name (PHI_RESULT (phi
), NULL
);
855 new = build2 (MODIFY_EXPR
, type
, result
,
856 build2 (minmax
, type
, arg0
, arg1
));
857 SSA_NAME_DEF_STMT (result
) = new;
858 bsi
= bsi_last (cond_bb
);
859 bsi_insert_before (&bsi
, new, BSI_NEW_STMT
);
861 replace_phi_edge_with_variable (cond_bb
, e1
, phi
, result
);
865 /* The function absolute_replacement does the main work of doing the absolute
866 replacement. Return true if the replacement is done. Otherwise return
868 bb is the basic block where the replacement is going to be done on. arg0
869 is argument 0 from the phi. Likewise for arg1. */
872 abs_replacement (basic_block cond_bb
, basic_block middle_bb
,
873 edge e0 ATTRIBUTE_UNUSED
, edge e1
,
874 tree phi
, tree arg0
, tree arg1
)
878 block_stmt_iterator bsi
;
879 edge true_edge
, false_edge
;
884 enum tree_code cond_code
;
886 /* If the type says honor signed zeros we cannot do this
888 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1
))))
891 /* OTHER_BLOCK must have only one executable statement which must have the
892 form arg0 = -arg1 or arg1 = -arg0. */
894 assign
= last_and_only_stmt (middle_bb
);
895 /* If we did not find the proper negation assignment, then we can not
900 /* If we got here, then we have found the only executable statement
901 in OTHER_BLOCK. If it is anything other than arg = -arg1 or
902 arg1 = -arg0, then we can not optimize. */
903 if (TREE_CODE (assign
) != MODIFY_EXPR
)
906 lhs
= TREE_OPERAND (assign
, 0);
907 rhs
= TREE_OPERAND (assign
, 1);
909 if (TREE_CODE (rhs
) != NEGATE_EXPR
)
912 rhs
= TREE_OPERAND (rhs
, 0);
914 /* The assignment has to be arg0 = -arg1 or arg1 = -arg0. */
915 if (!(lhs
== arg0
&& rhs
== arg1
)
916 && !(lhs
== arg1
&& rhs
== arg0
))
919 cond
= COND_EXPR_COND (last_stmt (cond_bb
));
920 result
= PHI_RESULT (phi
);
922 /* Only relationals comparing arg[01] against zero are interesting. */
923 cond_code
= TREE_CODE (cond
);
924 if (cond_code
!= GT_EXPR
&& cond_code
!= GE_EXPR
925 && cond_code
!= LT_EXPR
&& cond_code
!= LE_EXPR
)
928 /* Make sure the conditional is arg[01] OP y. */
929 if (TREE_OPERAND (cond
, 0) != rhs
)
932 if (FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (cond
, 1)))
933 ? real_zerop (TREE_OPERAND (cond
, 1))
934 : integer_zerop (TREE_OPERAND (cond
, 1)))
939 /* We need to know which is the true edge and which is the false
940 edge so that we know if have abs or negative abs. */
941 extract_true_false_edges_from_block (cond_bb
, &true_edge
, &false_edge
);
943 /* For GT_EXPR/GE_EXPR, if the true edge goes to OTHER_BLOCK, then we
944 will need to negate the result. Similarly for LT_EXPR/LE_EXPR if
945 the false edge goes to OTHER_BLOCK. */
946 if (cond_code
== GT_EXPR
|| cond_code
== GE_EXPR
)
951 if (e
->dest
== middle_bb
)
956 result
= duplicate_ssa_name (result
, NULL
);
960 tree tmp
= create_tmp_var (TREE_TYPE (result
), NULL
);
961 add_referenced_var (tmp
);
962 lhs
= make_ssa_name (tmp
, NULL
);
967 /* Build the modify expression with abs expression. */
968 new = build2 (MODIFY_EXPR
, TREE_TYPE (lhs
),
969 lhs
, build1 (ABS_EXPR
, TREE_TYPE (lhs
), rhs
));
970 SSA_NAME_DEF_STMT (lhs
) = new;
972 bsi
= bsi_last (cond_bb
);
973 bsi_insert_before (&bsi
, new, BSI_NEW_STMT
);
977 /* Get the right BSI. We want to insert after the recently
978 added ABS_EXPR statement (which we know is the first statement
980 new = build2 (MODIFY_EXPR
, TREE_TYPE (result
),
981 result
, build1 (NEGATE_EXPR
, TREE_TYPE (lhs
), lhs
));
982 SSA_NAME_DEF_STMT (result
) = new;
984 bsi_insert_after (&bsi
, new, BSI_NEW_STMT
);
987 replace_phi_edge_with_variable (cond_bb
, e1
, phi
, result
);
989 /* Note that we optimized this PHI. */
994 /* Always do these optimizations if we have SSA
1002 struct tree_opt_pass pass_phiopt
=
1004 "phiopt", /* name */
1005 gate_phiopt
, /* gate */
1006 tree_ssa_phiopt
, /* execute */
1009 0, /* static_pass_number */
1010 TV_TREE_PHIOPT
, /* tv_id */
1011 PROP_cfg
| PROP_ssa
| PROP_alias
, /* properties_required */
1012 0, /* properties_provided */
1013 0, /* properties_destroyed */
1014 0, /* todo_flags_start */
1019 | TODO_verify_stmts
, /* todo_flags_finish */