[InstCombine] foldXorOfICmps(): don't give up on non-single-use ICmp's if all users...
authorRoman Lebedev <lebedev.ri@gmail.com>
Tue, 13 Aug 2019 12:49:06 +0000 (12:49 +0000)
committerRoman Lebedev <lebedev.ri@gmail.com>
Tue, 13 Aug 2019 12:49:06 +0000 (12:49 +0000)
commit2635c324daa6dd01402ee959ed7c74ef7942f2d7
tree42643090ad274045d45994f8ab7aba8df7db0172
parent1b6d6e595b5e27566579c03923667aaab58bd3cd
[InstCombine] foldXorOfICmps(): don't give up on non-single-use ICmp's if all users are freely invertible

Summary:
This is rather unconventional..

As the comment there says, we don't have much folds for xor-of-icmps,
we try to turn them into an and-of-icmps, for which we have plenty of folds.
But if the ICmp we need to invert is not single-use - we give up.

As discussed in https://reviews.llvm.org/D65148#1603922,
we may have a non-canonical CLAMP pattern, with bit match and
select-of-threshold that we'll potentially clamp.
As it can be seen in `canonicalize-clamp-with-select-of-constant-threshold-pattern.ll`,
out of all 8 variations of the pattern, only two are **not** canonicalized into
the variant with and+icmp instead of bit math.
The reason is because the ICmp we need to invert is not single-use - we give up.

We indeed can't perform this fold at will, the general rule is that
we should not increase instruction count in InstCombine,

But we wouldn't end up increasing instruction count if we can adapt every other
user to the inverted value. This way the `not` we create **will** get folded,
and in the end the instruction count did not increase.

For that, of course, we need to look at the users of a Value,
which is again rather unconventional for InstCombine :S

Thus i'm proposing to be a little bit more insistive in `foldXorOfICmps()`.
The alternatives would be to not create that `not`, but add duplicate code to
manually invert all users; or to add some even less general combine to handle
some more specific pattern[s].

Reviewers: spatel, nikic, RKSimon, craig.topper

Reviewed By: spatel

Subscribers: hiraditya, jdoerfert, dmgreen, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D65530

llvm-svn: 368685
llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
llvm/lib/Transforms/InstCombine/InstCombineInternal.h
llvm/test/Transforms/InstCombine/canonicalize-clamp-with-select-of-constant-threshold-pattern.ll
llvm/test/Transforms/InstCombine/xor-of-icmps-with-extra-uses.ll