This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH][4.5] Fix PR31029, missed folding of C - X CMP X
- From: Richard Guenther <rguenther at suse dot de>
- To: gcc-patches at gcc dot gnu dot org
- Date: Sun, 2 Nov 2008 18:24:05 +0100 (CET)
- Subject: [PATCH][4.5] Fix PR31029, missed folding of C - X CMP X
This canonic variant is missed currently.
Bootstrapped and tested on x86_64-unknown-linux-gnu, queued for 4.5.
Richard.
2008-11-02 Richard Guenther <rguenther@suse.de>
PR middle-end/31029
* fold-const.c (fold_binary): Fold C - X CMP X.
* gcc.dg/fold-compare-4.c: New testcase.
Index: gcc/fold-const.c
===================================================================
*** gcc/fold-const.c (revision 141533)
--- gcc/fold-const.c (working copy)
*************** fold_binary (enum tree_code code, tree t
*** 11838,11859 ****
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)
- && 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);
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);
}
/* If we have X - Y == 0, we can convert that to X == Y and similarly
--- 11838,11860 ----
fold_convert (TREE_TYPE (arg0), arg1),
TREE_OPERAND (arg0, 1)));
! /* Transform comparisons of the form X +- C CMP X and C - X CMP X. */
if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
&& (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
! || POINTER_TYPE_P (TREE_TYPE (arg0)))
! && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
! && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
! || (TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
! && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))))
{
! tree cst = (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
! ? TREE_OPERAND (arg0, 1) : TREE_OPERAND (arg0, 0));
if (code == EQ_EXPR
&& !integer_zerop (cst))
! return omit_two_operands (type, boolean_false_node, arg0, arg1);
else
! return omit_two_operands (type, boolean_true_node, arg0, arg1);
}
/* If we have X - Y == 0, we can convert that to X == Y and similarly
Index: gcc/testsuite/gcc.dg/fold-compare-4.c
===================================================================
*** gcc/testsuite/gcc.dg/fold-compare-4.c (revision 0)
--- gcc/testsuite/gcc.dg/fold-compare-4.c (revision 0)
***************
*** 0 ****
--- 1,10 ----
+ /* { dg-do link } */
+
+ extern void link_error (void);
+
+ int main(int argc, char **argv)
+ {
+ if (argc == 1 - argc)
+ link_error ();
+ return 0;
+ }