]> gcc.gnu.org Git - gcc.git/commit
match.pd: Add ~(X - Y) -> ~X + Y simplification [PR96685]
authorJakub Jelinek <jakub@redhat.com>
Sat, 12 Dec 2020 13:49:57 +0000 (14:49 +0100)
committerJakub Jelinek <jakub@redhat.com>
Sat, 12 Dec 2020 13:49:57 +0000 (14:49 +0100)
commit0bd675183d94e6bca100c3aaaf87ee9676fb3c26
tree26b6b8215a5698abd61ab4832ee02ef55ab477fb
parentfe78528c05fdd562f21e12675781473b0fbe892e
match.pd: Add ~(X - Y) -> ~X + Y simplification [PR96685]

This patch adds the ~(X - Y) -> ~X + Y simplification requested
in the PR (plus also ~(X + C) -> ~X + (-C) for constants C that can
be safely negated.

The first two simplify blocks is what has been requested in the PR
and that makes the first testcase pass.
Unfortunately, that change also breaks the second testcase, because
while the same expressions appearing in the same stmt and split
across multiple stmts has been folded (not really) before, with
this optimization fold-const.c optimizes ~X + Y further into
(Y - X) - 1 in fold_binary_loc associate: code, but we have nothing
like that in GIMPLE and so end up with different expressions.

The last simplify is an attempt to deal with just this case,
had to rule out there the Y == -1U case, because then we
reached infinite recursion as ~X + -1U was canonicalized by
the pattern into (-1U - X) + -1U but there is a canonicalization
-1 - A -> ~A that turns it back.  Furthermore, had to make it #if
GIMPLE only, because it otherwise resulted in infinite recursion
when interacting with the associate: optimization.
The end result is that we pass all 3 testcases and thus canonizalize
the 3 possible forms of writing the same thing.

2020-12-12  Jakub Jelinek  <jakub@redhat.com>

PR tree-optimization/96685
* match.pd (~(X - Y) -> ~X + Y): New optimization.
(~X + Y -> (Y - X) - 1): Likewise.

* gcc.dg/tree-ssa/pr96685-1.c: New test.
* gcc.dg/tree-ssa/pr96685-2.c: New test.
* gcc.dg/tree-ssa/pr96685-3.c: New test.
gcc/match.pd
gcc/testsuite/gcc.dg/tree-ssa/pr96685-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/pr96685-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/pr96685-3.c [new file with mode: 0644]
This page took 0.054888 seconds and 5 git commands to generate.