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.
+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
@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))
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.
--- /dev/null
+/* 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);
+}