phiopt: Optimize (x != cst1 ? x : cst2) != cst3 [PR104639]
authorJakub Jelinek <jakub@redhat.com>
Mon, 11 Apr 2022 08:44:28 +0000 (10:44 +0200)
committerJakub Jelinek <jakub@redhat.com>
Mon, 11 Apr 2022 08:44:28 +0000 (10:44 +0200)
commita42aa68bf1ad745a6b36ab9beed1fc2e77ac3f88
tree188792bf816e9f0a2238c2773fd4d36425a958c5
parent083e8e66d2e90992fa83a53bfc3553dfa91abda1
phiopt: Optimize (x != cst1 ? x : cst2) != cst3 [PR104639]

Here is an attempt to resolve a P1 regression, where due to threading
changes we no longer optimize
bool foo(int i) {
    while (i == 4)
        i += 2;
    return i;
}
to just return i != 0; by enhancing the phiopt value_replacement
optimization.  Normally it will optimize x != cst1 ? x : cst1 to x.
Here we extend it to also optimize x != cst1 ? x : cst2 to x if
it (phi result) has a single immediate use which is a comparison
with some INTEGER_CST cst3 and we can prove that we don't care
whether x is cst1 or cst2 because both compare the same against cst3.

2022-04-11  Jakub Jelinek  <jakub@redhat.com>

PR tree-optimization/104639
* tree-ssa-phiopt.cc: Include tree-ssa-propagate.h.
(value_replacement): Optimize (x != cst1 ? x : cst2) != cst3
into x != cst3.

* gcc.dg/tree-ssa/pr104639-1.c: New test.
* gcc.dg/tree-ssa/pr104639-2.c: New test.
gcc/testsuite/gcc.dg/tree-ssa/pr104639-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/pr104639-2.c [new file with mode: 0644]
gcc/tree-ssa-phiopt.cc