1 /* Optimization of PHI nodes by converting them into straightline code.
2 Copyright (C) 2004, 2005 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 2, 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 COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
23 #include "coretypes.h"
30 #include "basic-block.h"
32 #include "diagnostic.h"
33 #include "tree-flow.h"
34 #include "tree-pass.h"
35 #include "tree-dump.h"
36 #include "langhooks.h"
38 static void tree_ssa_phiopt (void);
39 static bool conditional_replacement (basic_block
, basic_block
,
40 edge
, edge
, tree
, tree
, tree
);
41 static bool value_replacement (basic_block
, basic_block
,
42 edge
, edge
, tree
, tree
, tree
);
43 static bool minmax_replacement (basic_block
, basic_block
,
44 edge
, edge
, tree
, tree
, tree
);
45 static bool abs_replacement (basic_block
, basic_block
,
46 edge
, edge
, tree
, tree
, tree
);
47 static void replace_phi_edge_with_variable (basic_block
, edge
, tree
, tree
);
48 static basic_block
*blocks_in_phiopt_order (void);
50 /* This pass tries to replaces an if-then-else block with an
51 assignment. We have four kinds of transformations. Some of these
52 transformations are also performed by the ifcvt RTL optimizer.
54 Conditional Replacement
55 -----------------------
57 This transformation, implemented in conditional_replacement,
61 if (cond) goto bb2; else goto bb1;
64 x = PHI <0 (bb1), 1 (bb0), ...>;
72 x = PHI <x' (bb0), ...>;
74 We remove bb1 as it becomes unreachable. This occurs often due to
75 gimplification of conditionals.
80 This transformation, implemented in value_replacement, replaces
83 if (a != b) goto bb2; else goto bb1;
86 x = PHI <a (bb1), b (bb0), ...>;
92 x = PHI <b (bb0), ...>;
94 This opportunity can sometimes occur as a result of other
100 This transformation, implemented in abs_replacement, replaces
103 if (a >= 0) goto bb2; else goto bb1;
107 x = PHI <x (bb1), a (bb0), ...>;
114 x = PHI <x' (bb0), ...>;
119 This transformation, minmax_replacement replaces
122 if (a <= b) goto bb2; else goto bb1;
125 x = PHI <b (bb1), a (bb0), ...>;
132 x = PHI <x' (bb0), ...>;
134 A similar transformation is done for MAX_EXPR. */
137 tree_ssa_phiopt (void)
140 basic_block
*bb_order
;
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 ();
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
))
236 minmax_replacement (bb
, bb1
, e1
, e2
, phi
, arg0
, arg1
);
242 /* Returns the list of basic blocks in the function in an order that guarantees
243 that if a block X has just a single predecessor Y, then Y is after X in the
247 blocks_in_phiopt_order (void)
250 basic_block
*order
= xmalloc (sizeof (basic_block
) * n_basic_blocks
);
251 unsigned n
= n_basic_blocks
, np
, i
;
252 sbitmap visited
= sbitmap_alloc (last_basic_block
+ 2);
254 #define MARK_VISITED(BB) (SET_BIT (visited, (BB)->index + 2))
255 #define VISITED_P(BB) (TEST_BIT (visited, (BB)->index + 2))
257 sbitmap_zero (visited
);
259 MARK_VISITED (ENTRY_BLOCK_PTR
);
265 /* Walk the predecessors of x as long as they have precisely one
266 predecessor and add them to the list, so that they get stored
269 single_pred_p (y
) && !VISITED_P (single_pred (y
));
272 for (y
= x
, i
= n
- np
;
273 single_pred_p (y
) && !VISITED_P (single_pred (y
));
274 y
= single_pred (y
), i
++)
282 gcc_assert (i
== n
- 1);
286 sbitmap_free (visited
);
294 /* Return TRUE if block BB has no executable statements, otherwise return
297 empty_block_p (basic_block bb
)
299 block_stmt_iterator bsi
;
301 /* BB must have no executable statements. */
302 bsi
= bsi_start (bb
);
303 while (!bsi_end_p (bsi
)
304 && (TREE_CODE (bsi_stmt (bsi
)) == LABEL_EXPR
305 || IS_EMPTY_STMT (bsi_stmt (bsi
))))
308 if (!bsi_end_p (bsi
))
314 /* Replace PHI node element whose edge is E in block BB with variable NEW.
315 Remove the edge from COND_BLOCK which does not lead to BB (COND_BLOCK
316 is known to have two edges, one of which must reach BB). */
319 replace_phi_edge_with_variable (basic_block cond_block
,
320 edge e
, tree phi
, tree
new)
322 basic_block bb
= bb_for_stmt (phi
);
323 basic_block block_to_remove
;
324 block_stmt_iterator bsi
;
326 /* Change the PHI argument to new. */
327 SET_USE (PHI_ARG_DEF_PTR (phi
, e
->dest_idx
), new);
329 /* Remove the empty basic block. */
330 if (EDGE_SUCC (cond_block
, 0)->dest
== bb
)
332 EDGE_SUCC (cond_block
, 0)->flags
|= EDGE_FALLTHRU
;
333 EDGE_SUCC (cond_block
, 0)->flags
&= ~(EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
);
334 EDGE_SUCC (cond_block
, 0)->probability
= REG_BR_PROB_BASE
;
335 EDGE_SUCC (cond_block
, 0)->count
+= EDGE_SUCC (cond_block
, 1)->count
;
337 block_to_remove
= EDGE_SUCC (cond_block
, 1)->dest
;
341 EDGE_SUCC (cond_block
, 1)->flags
|= EDGE_FALLTHRU
;
342 EDGE_SUCC (cond_block
, 1)->flags
343 &= ~(EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
);
344 EDGE_SUCC (cond_block
, 1)->probability
= REG_BR_PROB_BASE
;
345 EDGE_SUCC (cond_block
, 1)->count
+= EDGE_SUCC (cond_block
, 0)->count
;
347 block_to_remove
= EDGE_SUCC (cond_block
, 0)->dest
;
349 delete_basic_block (block_to_remove
);
351 /* Eliminate the COND_EXPR at the end of COND_BLOCK. */
352 bsi
= bsi_last (cond_block
);
355 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
357 "COND_EXPR in block %d and PHI in block %d converted to straightline code.\n",
362 /* The function conditional_replacement does the main work of doing the
363 conditional replacement. Return true if the replacement is done.
364 Otherwise return false.
365 BB is the basic block where the replacement is going to be done on. ARG0
366 is argument 0 from PHI. Likewise for ARG1. */
369 conditional_replacement (basic_block cond_bb
, basic_block middle_bb
,
370 edge e0
, edge e1
, tree phi
,
371 tree arg0
, tree arg1
)
374 tree old_result
= NULL
;
376 block_stmt_iterator bsi
;
377 edge true_edge
, false_edge
;
381 /* The PHI arguments have the constants 0 and 1, then convert
382 it to the conditional. */
383 if ((integer_zerop (arg0
) && integer_onep (arg1
))
384 || (integer_zerop (arg1
) && integer_onep (arg0
)))
389 if (!empty_block_p (middle_bb
))
392 /* If the condition is not a naked SSA_NAME and its type does not
393 match the type of the result, then we have to create a new
394 variable to optimize this case as it would likely create
395 non-gimple code when the condition was converted to the
397 cond
= COND_EXPR_COND (last_stmt (cond_bb
));
398 result
= PHI_RESULT (phi
);
399 if (TREE_CODE (cond
) != SSA_NAME
400 && !lang_hooks
.types_compatible_p (TREE_TYPE (cond
), TREE_TYPE (result
)))
404 if (!COMPARISON_CLASS_P (cond
))
407 tmp
= create_tmp_var (TREE_TYPE (cond
), NULL
);
408 add_referenced_tmp_var (tmp
);
409 new_var
= make_ssa_name (tmp
, NULL
);
414 /* If the condition was a naked SSA_NAME and the type is not the
415 same as the type of the result, then convert the type of the
417 if (!lang_hooks
.types_compatible_p (TREE_TYPE (cond
), TREE_TYPE (result
)))
418 cond
= fold_convert (TREE_TYPE (result
), cond
);
420 /* We need to know which is the true edge and which is the false
421 edge so that we know when to invert the condition below. */
422 extract_true_false_edges_from_block (cond_bb
, &true_edge
, &false_edge
);
424 /* Insert our new statement at the end of conditional block before the
426 bsi
= bsi_last (cond_bb
);
427 bsi_insert_before (&bsi
, build_empty_stmt (), BSI_NEW_STMT
);
433 new1
= build2 (TREE_CODE (old_result
), TREE_TYPE (old_result
),
434 TREE_OPERAND (old_result
, 0),
435 TREE_OPERAND (old_result
, 1));
437 new1
= build2 (MODIFY_EXPR
, TREE_TYPE (old_result
), new_var
, new1
);
438 SSA_NAME_DEF_STMT (new_var
) = new1
;
440 bsi_insert_after (&bsi
, new1
, BSI_NEW_STMT
);
443 new_var1
= duplicate_ssa_name (PHI_RESULT (phi
), NULL
);
446 /* At this point we know we have a COND_EXPR with two successors.
447 One successor is BB, the other successor is an empty block which
448 falls through into BB.
450 There is a single PHI node at the join point (BB) and its arguments
451 are constants (0, 1).
453 So, given the condition COND, and the two PHI arguments, we can
454 rewrite this PHI into non-branching code:
456 dest = (COND) or dest = COND'
458 We use the condition as-is if the argument associated with the
459 true edge has the value one or the argument associated with the
460 false edge as the value zero. Note that those conditions are not
461 the same since only one of the outgoing edges from the COND_EXPR
462 will directly reach BB and thus be associated with an argument. */
463 if ((e0
== true_edge
&& integer_onep (arg0
))
464 || (e0
== false_edge
&& integer_zerop (arg0
))
465 || (e1
== true_edge
&& integer_onep (arg1
))
466 || (e1
== false_edge
&& integer_zerop (arg1
)))
468 new = build2 (MODIFY_EXPR
, TREE_TYPE (new_var1
), new_var1
, cond
);
472 tree cond1
= invert_truthvalue (cond
);
476 /* If what we get back is a conditional expression, there is no
477 way that it can be gimple. */
478 if (TREE_CODE (cond
) == COND_EXPR
)
480 release_ssa_name (new_var1
);
484 /* If COND is not something we can expect to be reducible to a GIMPLE
485 condition, return early. */
486 if (is_gimple_cast (cond
))
487 cond1
= TREE_OPERAND (cond
, 0);
488 if (TREE_CODE (cond1
) == TRUTH_NOT_EXPR
489 && !is_gimple_val (TREE_OPERAND (cond1
, 0)))
491 release_ssa_name (new_var1
);
495 /* If what we get back is not gimple try to create it as gimple by
496 using a temporary variable. */
497 if (is_gimple_cast (cond
)
498 && !is_gimple_val (TREE_OPERAND (cond
, 0)))
500 tree op0
, tmp
, cond_tmp
;
502 /* Only "real" casts are OK here, not everything that is
503 acceptable to is_gimple_cast. Make sure we don't do
504 anything stupid here. */
505 gcc_assert (TREE_CODE (cond
) == NOP_EXPR
506 || TREE_CODE (cond
) == CONVERT_EXPR
);
508 op0
= TREE_OPERAND (cond
, 0);
509 tmp
= create_tmp_var (TREE_TYPE (op0
), NULL
);
510 add_referenced_tmp_var (tmp
);
511 cond_tmp
= make_ssa_name (tmp
, NULL
);
512 new = build2 (MODIFY_EXPR
, TREE_TYPE (cond_tmp
), cond_tmp
, op0
);
513 SSA_NAME_DEF_STMT (cond_tmp
) = new;
515 bsi_insert_after (&bsi
, new, BSI_NEW_STMT
);
516 cond
= fold_convert (TREE_TYPE (result
), cond_tmp
);
519 new = build2 (MODIFY_EXPR
, TREE_TYPE (new_var1
), new_var1
, cond
);
522 bsi_insert_after (&bsi
, new, BSI_NEW_STMT
);
524 SSA_NAME_DEF_STMT (new_var1
) = new;
526 replace_phi_edge_with_variable (cond_bb
, e1
, phi
, new_var1
);
528 /* Note that we optimized this PHI. */
532 /* The function value_replacement does the main work of doing the value
533 replacement. Return true if the replacement is done. Otherwise return
535 BB is the basic block where the replacement is going to be done on. ARG0
536 is argument 0 from the PHI. Likewise for ARG1. */
539 value_replacement (basic_block cond_bb
, basic_block middle_bb
,
540 edge e0
, edge e1
, tree phi
,
541 tree arg0
, tree arg1
)
544 edge true_edge
, false_edge
;
546 /* If the type says honor signed zeros we cannot do this
548 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1
))))
551 if (!empty_block_p (middle_bb
))
554 cond
= COND_EXPR_COND (last_stmt (cond_bb
));
556 /* This transformation is only valid for equality comparisons. */
557 if (TREE_CODE (cond
) != NE_EXPR
&& TREE_CODE (cond
) != EQ_EXPR
)
560 /* We need to know which is the true edge and which is the false
561 edge so that we know if have abs or negative abs. */
562 extract_true_false_edges_from_block (cond_bb
, &true_edge
, &false_edge
);
564 /* At this point we know we have a COND_EXPR with two successors.
565 One successor is BB, the other successor is an empty block which
566 falls through into BB.
568 The condition for the COND_EXPR is known to be NE_EXPR or EQ_EXPR.
570 There is a single PHI node at the join point (BB) with two arguments.
572 We now need to verify that the two arguments in the PHI node match
573 the two arguments to the equality comparison. */
575 if ((operand_equal_for_phi_arg_p (arg0
, TREE_OPERAND (cond
, 0))
576 && operand_equal_for_phi_arg_p (arg1
, TREE_OPERAND (cond
, 1)))
577 || (operand_equal_for_phi_arg_p (arg1
, TREE_OPERAND (cond
, 0))
578 && operand_equal_for_phi_arg_p (arg0
, TREE_OPERAND (cond
, 1))))
583 /* For NE_EXPR, we want to build an assignment result = arg where
584 arg is the PHI argument associated with the true edge. For
585 EQ_EXPR we want the PHI argument associated with the false edge. */
586 e
= (TREE_CODE (cond
) == NE_EXPR
? true_edge
: false_edge
);
588 /* Unfortunately, E may not reach BB (it may instead have gone to
589 OTHER_BLOCK). If that is the case, then we want the single outgoing
590 edge from OTHER_BLOCK which reaches BB and represents the desired
591 path from COND_BLOCK. */
592 if (e
->dest
== middle_bb
)
593 e
= single_succ_edge (e
->dest
);
595 /* Now we know the incoming edge to BB that has the argument for the
596 RHS of our new assignment statement. */
602 replace_phi_edge_with_variable (cond_bb
, e1
, phi
, arg
);
604 /* Note that we optimized this PHI. */
610 /* The function minmax_replacement does the main work of doing the minmax
611 replacement. Return true if the replacement is done. Otherwise return
613 BB is the basic block where the replacement is going to be done on. ARG0
614 is argument 0 from the PHI. Likewise for ARG1. */
617 minmax_replacement (basic_block cond_bb
, basic_block middle_bb
,
618 edge e0
, edge e1
, tree phi
,
619 tree arg0
, tree arg1
)
623 edge true_edge
, false_edge
;
624 enum tree_code cmp
, minmax
, ass_code
;
625 tree smaller
, larger
, arg_true
, arg_false
;
626 block_stmt_iterator bsi
, bsi_from
;
628 type
= TREE_TYPE (PHI_RESULT (phi
));
630 /* The optimization may be unsafe due to NaNs. */
631 if (HONOR_NANS (TYPE_MODE (type
)))
634 cond
= COND_EXPR_COND (last_stmt (cond_bb
));
635 cmp
= TREE_CODE (cond
);
636 result
= PHI_RESULT (phi
);
638 /* This transformation is only valid for order comparisons. Record which
639 operand is smaller/larger if the result of the comparison is true. */
640 if (cmp
== LT_EXPR
|| cmp
== LE_EXPR
)
642 smaller
= TREE_OPERAND (cond
, 0);
643 larger
= TREE_OPERAND (cond
, 1);
645 else if (cmp
== GT_EXPR
|| cmp
== GE_EXPR
)
647 smaller
= TREE_OPERAND (cond
, 1);
648 larger
= TREE_OPERAND (cond
, 0);
653 /* We need to know which is the true edge and which is the false
654 edge so that we know if have abs or negative abs. */
655 extract_true_false_edges_from_block (cond_bb
, &true_edge
, &false_edge
);
657 /* Forward the edges over the middle basic block. */
658 if (true_edge
->dest
== middle_bb
)
659 true_edge
= EDGE_SUCC (true_edge
->dest
, 0);
660 if (false_edge
->dest
== middle_bb
)
661 false_edge
= EDGE_SUCC (false_edge
->dest
, 0);
665 gcc_assert (false_edge
== e1
);
671 gcc_assert (false_edge
== e0
);
672 gcc_assert (true_edge
== e1
);
677 if (empty_block_p (middle_bb
))
679 if (operand_equal_for_phi_arg_p (arg_true
, smaller
)
680 && operand_equal_for_phi_arg_p (arg_false
, larger
))
684 if (smaller < larger)
690 else if (operand_equal_for_phi_arg_p (arg_false
, smaller
)
691 && operand_equal_for_phi_arg_p (arg_true
, larger
))
698 /* Recognize the following case, assuming d <= u:
704 This is equivalent to
709 tree assign
= last_and_only_stmt (middle_bb
);
710 tree lhs
, rhs
, op0
, op1
, bound
;
713 || TREE_CODE (assign
) != MODIFY_EXPR
)
716 lhs
= TREE_OPERAND (assign
, 0);
717 rhs
= TREE_OPERAND (assign
, 1);
718 ass_code
= TREE_CODE (rhs
);
719 if (ass_code
!= MAX_EXPR
&& ass_code
!= MIN_EXPR
)
721 op0
= TREE_OPERAND (rhs
, 0);
722 op1
= TREE_OPERAND (rhs
, 1);
724 if (true_edge
->src
== middle_bb
)
726 /* We got here if the condition is true, i.e., SMALLER < LARGER. */
727 if (!operand_equal_for_phi_arg_p (lhs
, arg_true
))
730 if (operand_equal_for_phi_arg_p (arg_false
, larger
))
734 if (smaller < larger)
736 r' = MAX_EXPR (smaller, bound)
738 r = PHI <r', larger> --> to be turned to MIN_EXPR. */
739 if (ass_code
!= MAX_EXPR
)
743 if (operand_equal_for_phi_arg_p (op0
, smaller
))
745 else if (operand_equal_for_phi_arg_p (op1
, smaller
))
750 /* We need BOUND <= LARGER. */
751 if (!integer_nonzerop (fold_build2 (LE_EXPR
, boolean_type_node
,
755 else if (operand_equal_for_phi_arg_p (arg_false
, smaller
))
759 if (smaller < larger)
761 r' = MIN_EXPR (larger, bound)
763 r = PHI <r', smaller> --> to be turned to MAX_EXPR. */
764 if (ass_code
!= MIN_EXPR
)
768 if (operand_equal_for_phi_arg_p (op0
, larger
))
770 else if (operand_equal_for_phi_arg_p (op1
, larger
))
775 /* We need BOUND >= SMALLER. */
776 if (!integer_nonzerop (fold_build2 (GE_EXPR
, boolean_type_node
,
785 /* We got here if the condition is false, i.e., SMALLER > LARGER. */
786 if (!operand_equal_for_phi_arg_p (lhs
, arg_false
))
789 if (operand_equal_for_phi_arg_p (arg_true
, larger
))
793 if (smaller > larger)
795 r' = MIN_EXPR (smaller, bound)
797 r = PHI <r', larger> --> to be turned to MAX_EXPR. */
798 if (ass_code
!= MIN_EXPR
)
802 if (operand_equal_for_phi_arg_p (op0
, smaller
))
804 else if (operand_equal_for_phi_arg_p (op1
, smaller
))
809 /* We need BOUND >= LARGER. */
810 if (!integer_nonzerop (fold_build2 (GE_EXPR
, boolean_type_node
,
814 else if (operand_equal_for_phi_arg_p (arg_true
, smaller
))
818 if (smaller > larger)
820 r' = MAX_EXPR (larger, bound)
822 r = PHI <r', smaller> --> to be turned to MIN_EXPR. */
823 if (ass_code
!= MAX_EXPR
)
827 if (operand_equal_for_phi_arg_p (op0
, larger
))
829 else if (operand_equal_for_phi_arg_p (op1
, larger
))
834 /* We need BOUND <= SMALLER. */
835 if (!integer_nonzerop (fold_build2 (LE_EXPR
, boolean_type_node
,
843 /* Move the statement from the middle block. */
844 bsi
= bsi_last (cond_bb
);
845 bsi_from
= bsi_last (middle_bb
);
846 bsi_move_before (&bsi_from
, &bsi
);
849 /* Emit the statement to compute min/max. */
850 result
= duplicate_ssa_name (PHI_RESULT (phi
), NULL
);
851 new = build2 (MODIFY_EXPR
, type
, result
,
852 build2 (minmax
, type
, arg0
, arg1
));
853 SSA_NAME_DEF_STMT (result
) = new;
854 bsi
= bsi_last (cond_bb
);
855 bsi_insert_before (&bsi
, new, BSI_NEW_STMT
);
857 replace_phi_edge_with_variable (cond_bb
, e1
, phi
, result
);
861 /* The function absolute_replacement does the main work of doing the absolute
862 replacement. Return true if the replacement is done. Otherwise return
864 bb is the basic block where the replacement is going to be done on. arg0
865 is argument 0 from the phi. Likewise for arg1. */
868 abs_replacement (basic_block cond_bb
, basic_block middle_bb
,
869 edge e0 ATTRIBUTE_UNUSED
, edge e1
,
870 tree phi
, tree arg0
, tree arg1
)
874 block_stmt_iterator bsi
;
875 edge true_edge
, false_edge
;
880 enum tree_code cond_code
;
882 /* If the type says honor signed zeros we cannot do this
884 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1
))))
887 /* OTHER_BLOCK must have only one executable statement which must have the
888 form arg0 = -arg1 or arg1 = -arg0. */
890 assign
= last_and_only_stmt (middle_bb
);
891 /* If we did not find the proper negation assignment, then we can not
896 /* If we got here, then we have found the only executable statement
897 in OTHER_BLOCK. If it is anything other than arg = -arg1 or
898 arg1 = -arg0, then we can not optimize. */
899 if (TREE_CODE (assign
) != MODIFY_EXPR
)
902 lhs
= TREE_OPERAND (assign
, 0);
903 rhs
= TREE_OPERAND (assign
, 1);
905 if (TREE_CODE (rhs
) != NEGATE_EXPR
)
908 rhs
= TREE_OPERAND (rhs
, 0);
910 /* The assignment has to be arg0 = -arg1 or arg1 = -arg0. */
911 if (!(lhs
== arg0
&& rhs
== arg1
)
912 && !(lhs
== arg1
&& rhs
== arg0
))
915 cond
= COND_EXPR_COND (last_stmt (cond_bb
));
916 result
= PHI_RESULT (phi
);
918 /* Only relationals comparing arg[01] against zero are interesting. */
919 cond_code
= TREE_CODE (cond
);
920 if (cond_code
!= GT_EXPR
&& cond_code
!= GE_EXPR
921 && cond_code
!= LT_EXPR
&& cond_code
!= LE_EXPR
)
924 /* Make sure the conditional is arg[01] OP y. */
925 if (TREE_OPERAND (cond
, 0) != rhs
)
928 if (FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (cond
, 1)))
929 ? real_zerop (TREE_OPERAND (cond
, 1))
930 : integer_zerop (TREE_OPERAND (cond
, 1)))
935 /* We need to know which is the true edge and which is the false
936 edge so that we know if have abs or negative abs. */
937 extract_true_false_edges_from_block (cond_bb
, &true_edge
, &false_edge
);
939 /* For GT_EXPR/GE_EXPR, if the true edge goes to OTHER_BLOCK, then we
940 will need to negate the result. Similarly for LT_EXPR/LE_EXPR if
941 the false edge goes to OTHER_BLOCK. */
942 if (cond_code
== GT_EXPR
|| cond_code
== GE_EXPR
)
947 if (e
->dest
== middle_bb
)
952 result
= duplicate_ssa_name (result
, NULL
);
956 tree tmp
= create_tmp_var (TREE_TYPE (result
), NULL
);
957 add_referenced_tmp_var (tmp
);
958 lhs
= make_ssa_name (tmp
, NULL
);
963 /* Build the modify expression with abs expression. */
964 new = build2 (MODIFY_EXPR
, TREE_TYPE (lhs
),
965 lhs
, build1 (ABS_EXPR
, TREE_TYPE (lhs
), rhs
));
966 SSA_NAME_DEF_STMT (lhs
) = new;
968 bsi
= bsi_last (cond_bb
);
969 bsi_insert_before (&bsi
, new, BSI_NEW_STMT
);
973 /* Get the right BSI. We want to insert after the recently
974 added ABS_EXPR statement (which we know is the first statement
976 new = build2 (MODIFY_EXPR
, TREE_TYPE (result
),
977 result
, build1 (NEGATE_EXPR
, TREE_TYPE (lhs
), lhs
));
978 SSA_NAME_DEF_STMT (result
) = new;
980 bsi_insert_after (&bsi
, new, BSI_NEW_STMT
);
983 replace_phi_edge_with_variable (cond_bb
, e1
, phi
, result
);
985 /* Note that we optimized this PHI. */
990 /* Always do these optimizations if we have SSA
998 struct tree_opt_pass pass_phiopt
=
1000 "phiopt", /* name */
1001 gate_phiopt
, /* gate */
1002 tree_ssa_phiopt
, /* execute */
1005 0, /* static_pass_number */
1006 TV_TREE_PHIOPT
, /* tv_id */
1007 PROP_cfg
| PROP_ssa
| PROP_alias
, /* properties_required */
1008 0, /* properties_provided */
1009 0, /* properties_destroyed */
1010 0, /* todo_flags_start */
1016 | TODO_verify_stmts
, /* todo_flags_finish */