From: Marek Polacek Date: Tue, 23 Jun 2015 10:09:05 +0000 (+0000) Subject: match.pd ((x + y) - (x | y) -> x & y, (x + y) - (x & y) -> x | y): New patterns. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=9737efaf7e8551eae41e02b5ea61a80a09206f64;p=gcc.git 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 --- diff --git a/gcc/ChangeLog b/gcc/ChangeLog index ee2e8d73b8a..80c07c82efd 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,8 @@ +2015-06-23 Marek Polacek + + * match.pd ((x + y) - (x | y) -> x & y, + (x + y) - (x & y) -> x | y): New patterns. + 2015-06-23 Ludovic Courtès PR 65711 diff --git a/gcc/match.pd b/gcc/match.pd index badb80a28a7..9c88e3eee39 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 19e4a7bd0ae..f305d812d46 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2015-06-23 Marek Polacek + + * 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 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 00000000000..905a60a73b3 --- /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 00000000000..2d76b4f60e4 --- /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 00000000000..a31e1cc1e20 --- /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" } } */