summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gcc/ChangeLog6
-rw-r--r--gcc/fold-const.c7
-rw-r--r--gcc/testsuite/ChangeLog5
-rw-r--r--gcc/testsuite/gcc.dg/fold-xornot-1.c16
4 files changed, 34 insertions, 0 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index c4bd3888235..fbdec1aff07 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,9 @@
+2006-10-29 Roger Sayle <roger@eyesopen.com>
+
+ PR tree-optimization/15458
+ * fold-const.c (fold_binary): Optimize ~X ^ C as X ^ ~C, where C
+ is a constant.
+
2006-10-29 Richard Guenther <rguenther@suse.de>
* config/i386/i386-protos.h (ix86_expand_trunc): Declare.
diff --git a/gcc/fold-const.c b/gcc/fold-const.c
index 8e3c97ae91e..1c3c752ceb4 100644
--- a/gcc/fold-const.c
+++ b/gcc/fold-const.c
@@ -9506,6 +9506,13 @@ fold_binary (enum tree_code code, tree type, tree op0, tree op1)
fold_convert (type, TREE_OPERAND (arg0, 0)),
fold_convert (type, TREE_OPERAND (arg1, 0)));
+ /* Convert ~X ^ C to X ^ ~C. */
+ if (TREE_CODE (arg0) == BIT_NOT_EXPR
+ && TREE_CODE (arg1) == INTEGER_CST)
+ return fold_build2 (code, type,
+ fold_convert (type, TREE_OPERAND (arg0, 0)),
+ fold_build1 (BIT_NOT_EXPR, type, arg1));
+
/* Fold (X & 1) ^ 1 as (X & 1) == 0. */
if (TREE_CODE (arg0) == BIT_AND_EXPR
&& integer_onep (TREE_OPERAND (arg0, 1))
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 067d97e9481..de569bb604f 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2006-10-29 Roger Sayle <roger@eyesopen.com>
+
+ PR tree-optimization/15458
+ * gcc.dg/fold-xornot-1.c: New test case.
+
2006-10-29 Richard Guenther <rguenther@suse.de>
* gcc.target/i386/math-torture/trunc.c: New testcase.
diff --git a/gcc/testsuite/gcc.dg/fold-xornot-1.c b/gcc/testsuite/gcc.dg/fold-xornot-1.c
new file mode 100644
index 00000000000..c92095d081f
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/fold-xornot-1.c
@@ -0,0 +1,16 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-original" } */
+
+int foo(int x)
+{
+ return ~(x ^ 4);
+}
+
+int bar(int y)
+{
+ return ~y ^ 4;
+}
+
+/* { dg-final { scan-tree-dump-times "x \\^ -5" 1 "original" } } */
+/* { dg-final { scan-tree-dump-times "y \\^ -5" 1 "original" } } */
+/* { dg-final { cleanup-tree-dump "original" } } */