Richard Guenther - [PATCH] Fix PR30951, missed folding of x == x + 1 (original) (raw)
This is the mail archive of the gcc-patches@gcc.gnu.orgmailing list for the GCC project.
Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
---|---|---|
Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |
Other format: | [Raw text] |
- From: Richard Guenther
- To: gcc-patches at gcc dot gnu dot org
- Date: Sat, 24 Feb 2007 20:55:14 +0100 (CET)
- Subject: [PATCH] Fix PR30951, missed folding of x == x + 1
This fixes PR30951 which I noticed while working on the fix for 30364. We can fold == and != comparisons regardless of overflow behavior for X +- CST op X.
Bootstrapped and tested on x86_64-unknown-linux-gnu, applied to mainline.
Richard.
2007-02-24 Richard Guenther rguenther@suse.de
PR middle-end/30951
* fold-const.c (fold_binary): Fold x +- CST op x for
EQ_EXPR and NE_EXPR.
* gcc.dg/pr30951.c: New testcase.
Index: fold-const.c
*** fold-const.c (revision 122288)
--- fold-const.c (working copy)
*************** fold_binary (enum tree_code code, tree t
*** 11203,11208 ****
--- 11207,11230 ----
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
for !=. Don't do this for ordered comparisons due to overflow. */
if (TREE_CODE (arg0) == MINUS_EXPR
Index: testsuite/gcc.dg/pr30951.c
*** testsuite/gcc.dg/pr30951.c (revision 0) --- testsuite/gcc.dg/pr30951.c (revision 0)
*** 0 **** --- 1,36 ----
- /* { dg-do link } */
- extern void link_error (void);
- void test (int x, unsigned int y)
- {
- if (x + 5 == x)
link_error ();
- if (x == x + 10)
link_error ();
- if (y + 5 == y)
link_error ();
- if (y == y + 10)
link_error ();
- if (x + 5 != x)
;
- else
link_error ();
- if (x != x + 10)
;
- else
link_error ();
- if (y + 5 != y)
;
- else
link_error ();
- if (y != y + 10)
;
- else
link_error ();
- }
- int main()
- {
- return 0;
- }
- Follow-Ups:
- Re: [PATCH] Fix PR30951, missed folding of x == x + 1
* From: Joseph S. Myers
- Re: [PATCH] Fix PR30951, missed folding of x == x + 1
Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
---|---|---|
Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |