summaryrefslogtreecommitdiff
path: root/gcc/fold-const.c
diff options
context:
space:
mode:
authorrguenth <rguenth@138bc75d-0d04-0410-961f-82ee72b054a4>2009-03-31 14:28:16 +0000
committerrguenth <rguenth@138bc75d-0d04-0410-961f-82ee72b054a4>2009-03-31 14:28:16 +0000
commit172e662bea3dd45f48f4155bfa34df25d552d465 (patch)
tree3375dd6b6a930ea767bbaf4394885cf7b47d7023 /gcc/fold-const.c
parent401d1fb3672e35a37032eedfbbbdb5d5c6680b69 (diff)
downloadgcc-172e662bea3dd45f48f4155bfa34df25d552d465.tar.gz
2009-03-31 Richard Guenther <rguenther@suse.de>
PR middle-end/31029 * fold-const.c (fold_binary): Fold X +- Y CMP X to Y CMP 0 for equality comparisons. Fold C - X CMP X if C % 2 == 1. * gcc.dg/fold-compare-4.c: New testcase. * gcc.dg/fold-compare-5.c: Likewise. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@145345 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'gcc/fold-const.c')
-rw-r--r--gcc/fold-const.c33
1 files changed, 22 insertions, 11 deletions
diff --git a/gcc/fold-const.c b/gcc/fold-const.c
index 4951600a297..ec069541412 100644
--- a/gcc/fold-const.c
+++ b/gcc/fold-const.c
@@ -12191,22 +12191,33 @@ fold_binary (enum tree_code code, tree type, tree op0, tree op1)
fold_convert (TREE_TYPE (arg0), arg1),
TREE_OPERAND (arg0, 1)));
- /* Transform comparisons of the form X +- C CMP X. */
- if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
+ /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
+ if ((TREE_CODE (arg0) == PLUS_EXPR
+ || TREE_CODE (arg0) == POINTER_PLUS_EXPR
+ || TREE_CODE (arg0) == MINUS_EXPR)
&& operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
- && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
&& (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
|| POINTER_TYPE_P (TREE_TYPE (arg0))))
{
- tree cst = TREE_OPERAND (arg0, 1);
+ tree val = TREE_OPERAND (arg0, 1);
+ return omit_two_operands (type,
+ fold_build2 (code, type,
+ val,
+ build_int_cst (TREE_TYPE (val),
+ 0)),
+ TREE_OPERAND (arg0, 0), arg1);
+ }
- if (code == EQ_EXPR
- && !integer_zerop (cst))
- return omit_two_operands (type, boolean_false_node,
- TREE_OPERAND (arg0, 0), arg1);
- else
- return omit_two_operands (type, boolean_true_node,
- TREE_OPERAND (arg0, 0), arg1);
+ /* Transform comparisons of the form C - X CMP X if C % 2 == 1. */
+ if (TREE_CODE (arg0) == MINUS_EXPR
+ && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
+ && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0)
+ && (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 0)) & 1) == 1)
+ {
+ return omit_two_operands (type,
+ code == NE_EXPR
+ ? boolean_true_node : boolean_false_node,
+ TREE_OPERAND (arg0, 1), arg1);
}
/* If we have X - Y == 0, we can convert that to X == Y and similarly