match.pd: Optimize ~(~X +- Y) into (X -+ Y) [PR94921]
authorJakub Jelinek <jakub@redhat.com>
Wed, 6 May 2020 09:20:20 +0000 (11:20 +0200)
committerJakub Jelinek <jakub@redhat.com>
Wed, 6 May 2020 09:20:20 +0000 (11:20 +0200)
According to my verification proglet, this transformation for signed types
with undefined overflow doesn't introduce nor remove any UB cases, so should
be valid even for signed integral types.
Not using a for because of the :c on plus which can't be there on minus.

2020-05-06  Jakub Jelinek  <jakub@redhat.com>

PR tree-optimization/94921
* match.pd (~(~X - Y) -> X + Y, ~(~X + Y) -> X - Y): New
simplifications.

* gcc.dg/tree-ssa/pr94921.c: New test.

gcc/ChangeLog
gcc/match.pd
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/tree-ssa/pr94921.c [new file with mode: 0644]

index e0dd54c..8bb2893 100644 (file)
@@ -1,3 +1,9 @@
+2020-05-06  Jakub Jelinek  <jakub@redhat.com>
+
+       PR tree-optimization/94921
+       * match.pd (~(~X - Y) -> X + Y, ~(~X + Y) -> X - Y): New
+       simplifications.
+
 2020-05-06  Richard Biener  <rguenther@suse.de>
 
        PR tree-optimization/94965
index d957517..9259dd4 100644 (file)
@@ -1010,6 +1010,14 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
   @0))
 #endif
 
+/* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y.  */
+(simplify
+ (bit_not (minus (bit_not @0) @1))
+ (plus @0 @1))
+(simplify
+ (bit_not (plus:c (bit_not @0) @1))
+ (minus @0 @1))
+
 /* x + (x & 1) -> (x + 1) & ~1 */
 (simplify
  (plus:c @0 (bit_and:s @0 integer_onep@1))
index 1f01ff0..a66a012 100644 (file)
@@ -1,5 +1,9 @@
 2020-05-06  Jakub Jelinek  <jakub@redhat.com>
 
+       PR tree-optimization/94921
+       * match.pd (~(~X - Y) -> X + Y, ~(~X + Y) -> X - Y): New
+       simplifications.
+
        PR rtl-optimization/94873
        * gcc.dg/pr94873.c: New test.
 
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr94921.c b/gcc/testsuite/gcc.dg/tree-ssa/pr94921.c
new file mode 100644 (file)
index 0000000..2c752ca
--- /dev/null
@@ -0,0 +1,18 @@
+/* PR tree-optimization/94921 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* { dg-final { scan-tree-dump " \[ab]_\[0-9]+\\\(D\\\) \\+ \[ab]_\[0-9]+\\\(D\\\);" "optimized" } } */
+/* { dg-final { scan-tree-dump " c_\[0-9]+\\\(D\\\) - d_\[0-9]+\\\(D\\\);" "optimized" } } */
+/* { dg-final { scan-tree-dump-not " ~\[abcd]\?_\[0-9]\+" "optimized" } } */
+
+int
+foo (int a, int b)
+{
+  return ~(~a - b);
+}
+
+int
+bar (int c, int d)
+{
+  return ~(~c + d);
+}