aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarek Polacek <polacek@redhat.com>2015-06-23 10:09:05 +0000
committerMarek Polacek <mpolacek@gcc.gnu.org>2015-06-23 10:09:05 +0000
commit9737efaf7e8551eae41e02b5ea61a80a09206f64 (patch)
tree4792b55d392ce53f4fb5ecc1ae08d6372d753f29
parent7b91cc91f23ea9f964678eb4dcf5cca2e009a659 (diff)
downloadgcc-9737efaf7e8551eae41e02b5ea61a80a09206f64.zip
gcc-9737efaf7e8551eae41e02b5ea61a80a09206f64.tar.gz
gcc-9737efaf7e8551eae41e02b5ea61a80a09206f64.tar.bz2
match.pd ((x + y) - (x | y) -> x & y, (x + y) - (x & y) -> x | y): New patterns.
* match.pd ((x + y) - (x | y) -> x & y, (x + y) - (x & y) -> x | y): New patterns. * gcc.dg/fold-minus-4.c: New test. * gcc.dg/fold-minus-5.c: New test. * c-c++-common/ubsan/overflow-add-5.c: New test. From-SVN: r224834
-rw-r--r--gcc/ChangeLog5
-rw-r--r--gcc/match.pd14
-rw-r--r--gcc/testsuite/ChangeLog6
-rw-r--r--gcc/testsuite/c-c++-common/ubsan/overflow-add-5.c30
-rw-r--r--gcc/testsuite/gcc.dg/fold-minus-4.c37
-rw-r--r--gcc/testsuite/gcc.dg/fold-minus-5.c37
6 files changed, 129 insertions, 0 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index ee2e8d7..80c07c8 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,8 @@
+2015-06-23 Marek Polacek <polacek@redhat.com>
+
+ * match.pd ((x + y) - (x | y) -> x & y,
+ (x + y) - (x & y) -> x | y): New patterns.
+
2015-06-23 Ludovic Courtès <ludo@gnu.org>
PR 65711
diff --git a/gcc/match.pd b/gcc/match.pd
index badb80a..9c88e3e 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -343,6 +343,20 @@ along with GCC; see the file COPYING3. If not see
(plus:c (bit_and @0 @1) (bit_ior @0 @1))
(plus @0 @1))
+/* (x + y) - (x | y) -> x & y */
+(simplify
+ (minus (plus @0 @1) (bit_ior @0 @1))
+ (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
+ && !TYPE_SATURATING (type))
+ (bit_and @0 @1)))
+
+/* (x + y) - (x & y) -> x | y */
+(simplify
+ (minus (plus @0 @1) (bit_and @0 @1))
+ (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
+ && !TYPE_SATURATING (type))
+ (bit_ior @0 @1)))
+
/* (x | y) - (x ^ y) -> x & y */
(simplify
(minus (bit_ior @0 @1) (bit_xor @0 @1))
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 19e4a7b..f305d81 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,9 @@
+2015-06-23 Marek Polacek <polacek@redhat.com>
+
+ * gcc.dg/fold-minus-4.c: New test.
+ * gcc.dg/fold-minus-5.c: New test.
+ * c-c++-common/ubsan/overflow-add-5.c: New test.
+
2015-06-23 James Greenhalgh <james.greenhalgh@arm.com>
Add missing testcase from r224672.
diff --git a/gcc/testsuite/c-c++-common/ubsan/overflow-add-5.c b/gcc/testsuite/c-c++-common/ubsan/overflow-add-5.c
new file mode 100644
index 0000000..905a60a
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/ubsan/overflow-add-5.c
@@ -0,0 +1,30 @@
+/* { dg-do run } */
+/* { dg-options "-fsanitize=signed-integer-overflow" } */
+
+int __attribute__ ((noinline))
+foo (int i, int j)
+{
+ return (i + j) - (i | j);
+}
+
+/* { dg-output "signed integer overflow: 2147483647 \\+ 1 cannot be represented in type 'int'\[^\n\r]*(\n|\r\n|\r)" } */
+/* { dg-output "\[^\n\r]*signed integer overflow: -2147483648 - 2147483647 cannot be represented in type 'int'\[^\n\r]*(\n|\r\n|\r)" } */
+
+int __attribute__ ((noinline))
+bar (int i, int j)
+{
+ return (i + j) - (i & j);
+}
+
+/* { dg-output "\[^\n\r]*signed integer overflow: 2147483647 \\+ 1 cannot be represented in type 'int'\[^\n\r]*(\n|\r\n|\r)" } */
+/* { dg-output "\[^\n\r]*signed integer overflow: -2147483648 - 1 cannot be represented in type 'int'" } */
+
+int
+main ()
+{
+ int r = foo (__INT_MAX__, 1);
+ asm volatile ("" : "+g" (r));
+ r = bar (__INT_MAX__, 1);
+ asm volatile ("" : "+g" (r));
+ return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/fold-minus-4.c b/gcc/testsuite/gcc.dg/fold-minus-4.c
new file mode 100644
index 0000000..2d76b4f
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/fold-minus-4.c
@@ -0,0 +1,37 @@
+/* { dg-do compile } */
+/* { dg-options "-O -fdump-tree-cddce1" } */
+
+int
+fn1 (int a, int b)
+{
+ int tem1 = a + b;
+ int tem2 = a & b;
+ return tem1 - tem2;
+}
+
+int
+fn2 (int a, int b)
+{
+ int tem1 = b + a;
+ int tem2 = a & b;
+ return tem1 - tem2;
+}
+
+int
+fn3 (int a, int b)
+{
+ int tem1 = a + b;
+ int tem2 = b & a;
+ return tem1 - tem2;
+}
+
+int
+fn4 (int a, int b)
+{
+ int tem1 = b + a;
+ int tem2 = b & a;
+ return tem1 - tem2;
+}
+
+/* { dg-final { scan-tree-dump-not " & " "cddce1" } } */
+/* { dg-final { scan-tree-dump-not " \\+ " "cddce1" } } */
diff --git a/gcc/testsuite/gcc.dg/fold-minus-5.c b/gcc/testsuite/gcc.dg/fold-minus-5.c
new file mode 100644
index 0000000..a31e1cc
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/fold-minus-5.c
@@ -0,0 +1,37 @@
+/* { dg-do compile } */
+/* { dg-options "-O -fdump-tree-cddce1" } */
+
+int
+fn1 (int a, int b)
+{
+ int tem1 = a + b;
+ int tem2 = a | b;
+ return tem1 - tem2;
+}
+
+int
+fn2 (int a, int b)
+{
+ int tem1 = b + a;
+ int tem2 = a | b;
+ return tem1 - tem2;
+}
+
+int
+fn3 (int a, int b)
+{
+ int tem1 = a + b;
+ int tem2 = b | a;
+ return tem1 - tem2;
+}
+
+int
+fn4 (int a, int b)
+{
+ int tem1 = b + a;
+ int tem2 = b | a;
+ return tem1 - tem2;
+}
+
+/* { dg-final { scan-tree-dump-not " \\+ " "cddce1" } } */
+/* { dg-final { scan-tree-dump-not " \\| " "cddce1" } } */