This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: match.pd: Three new patterns
- From: Marek Polacek <polacek at redhat dot com>
- To: Richard Biener <rguenther at suse dot de>
- Cc: Marc Glisse <marc dot glisse at inria dot fr>, GCC Patches <gcc-patches at gcc dot gnu dot org>
- Date: Tue, 23 Jun 2015 12:10:01 +0200
- Subject: Re: match.pd: Three new patterns
- Authentication-results: sourceware.org; auth=none
- References: <20150612121332 dot GH2756 at redhat dot com> <alpine dot DEB dot 2 dot 20 dot 1506131138460 dot 2373 at laptop-mg dot saclay dot inria dot fr> <20150618154117 dot GD10139 at redhat dot com> <20150619152304 dot GH10139 at redhat dot com> <alpine dot DEB dot 2 dot 20 dot 1506191725520 dot 1586 at laptop-mg dot saclay dot inria dot fr> <20150622190319 dot GM10139 at redhat dot com> <alpine dot LSU dot 2 dot 11 dot 1506230951200 dot 31770 at zhemvz dot fhfr dot qr> <20150623081350 dot GO10139 at redhat dot com> <alpine dot LSU dot 2 dot 11 dot 1506231018050 dot 31770 at zhemvz dot fhfr dot qr>
On Tue, Jun 23, 2015 at 10:22:35AM +0200, Richard Biener wrote:
> Who says you can't do bitwise ops on them? I can't see that being
> enforced in the GIMPLE checking in tree-cfg.c. Yes, there is no
> such thing as a "saturating" bitwise and but bitwise and should
> just work fine.
>
> You can check with a arm cross what the C FE does when you use
> bitwise ops but I believe the regular and/ior md patterns work
> just fine (there are no special modes/registers but they seem
> to be shared with regular registers, just special operations
> are available).
Ok ;). Applied the following then.
2015-06-23 Marek Polacek <polacek@redhat.com>
* 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.
diff --git gcc/match.pd gcc/match.pd
index badb80a..9c88e3e 100644
--- gcc/match.pd
+++ 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 gcc/testsuite/c-c++-common/ubsan/overflow-add-5.c gcc/testsuite/c-c++-common/ubsan/overflow-add-5.c
index e69de29..905a60a 100644
--- gcc/testsuite/c-c++-common/ubsan/overflow-add-5.c
+++ 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 gcc/testsuite/gcc.dg/fold-minus-4.c gcc/testsuite/gcc.dg/fold-minus-4.c
index e69de29..2d76b4f 100644
--- gcc/testsuite/gcc.dg/fold-minus-4.c
+++ 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 gcc/testsuite/gcc.dg/fold-minus-5.c gcc/testsuite/gcc.dg/fold-minus-5.c
index e69de29..a31e1cc 100644
--- gcc/testsuite/gcc.dg/fold-minus-5.c
+++ 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" } } */
Marek