This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] don't use DeMorgan's Law on integer constants
ClosedPublic

Authored by spatel on Apr 28 2017, 4:55 PM.

Details

Summary

This is the fold that causes the infinite loop in BoringSSL (https://github.com/google/boringssl/blob/master/crypto/cipher/e_rc2.c) when we fix instcombine demanded bits to prefer 'not' ops as in D32255.

There are 2 or 3 problems with dyn_castNotVal, and I don't think we can reinstate D32255 until dyn_castNotVal is completely eliminated:

  1. As shown here, it transforms 'not' into random xor. This transform is harmful to SCEV and codegen because 'not' can often be folded while random xor cannot.
  2. It does not transform vector constants. This is actually a good thing, but if you don't believe the above argument, then we shouldn't have excluded vectors.
  3. It tries to avoid transforming not(not(X)). That's nice, but it doesn't match the greedy nature of instcombine. If we DeMorganize a pattern that has an extra 'not' in it:

~(~(~X) & Y) --> (~X | ~Y)
That's just another case of DeMorgan, so we should trust that we'll get that too:
(~X | ~ Y) --> ~(X & Y)

Diff Detail

Repository
rL LLVM

Event Timeline

spatel created this revision.Apr 28 2017, 4:55 PM
efriedma edited edge metadata.Apr 28 2017, 10:10 PM

Your reasoning makes sense.

lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
2441 ↗(On Diff #97172)

Do we need to check the number of uses for the inner operations?

spatel added inline comments.Apr 30 2017, 8:30 AM
lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
2441 ↗(On Diff #97172)

Good point - I'll add some tests so we can see what that trade-off looks like.

spatel added inline comments.May 1 2017, 10:16 AM
lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
2441 ↗(On Diff #97172)

I made a separate patch to answer this part since it exists independently of this change:
D32703

spatel updated this revision to Diff 97355.May 1 2017, 3:56 PM

Patch updated:
We added a one-use check in D32703, so keep that in the translation to the 'match' patterns.

efriedma accepted this revision.May 1 2017, 4:06 PM

LGTM.

This revision is now accepted and ready to land.May 1 2017, 4:06 PM
This revision was automatically updated to reflect the committed changes.