This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] fold icmp of zext bool based on limited range
ClosedPublic

Authored by spatel on May 22 2022, 10:56 AM.

Details

Summary

Op0 <u (zext i1 X) --> Op0 == 0 && X

https://alive2.llvm.org/ce/z/avQDRY

This is a generalization of 4069cccf3b4ff4a based on the post-commit suggestion.
This also adds the i1 type check and tests that were missing from the earlier attempt; that commit caused several bot fails and was reverted.

Diff Detail

Event Timeline

spatel created this revision.May 22 2022, 10:56 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 22 2022, 10:56 AM
spatel requested review of this revision.May 22 2022, 10:56 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 22 2022, 10:56 AM
nikic added a comment.EditedMay 22 2022, 12:07 PM
This comment has been deleted.
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
5647

Might make sense to use m_c_ICmp instead? Something along these lines:

if (match(I, m_c_ICmp(Pred, m_Value(X), m_OneUse(m_ZExt(m_Value(Y))))) &&
    Y->getType()->isIntOrIntVectorTy(1) && Pred == ICmpInst::ICMP_ULT)

That would probably make it easier to extend this to the conjugate sext pattern: https://alive2.llvm.org/ce/z/2JTkdF

spatel added inline comments.May 22 2022, 12:54 PM
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
5647

Ah, thanks - I knew there was a sibling, but I had not worked out the pattern. I'll add a TODO for that.

And yes, we can use the commutative matcher. I wasn't sure if we'd miss the transform if both operands are zexted, but it looks like other folds will convert that to something we can match more easily here. I'll add some more tests to confirm.

spatel updated this revision to Diff 431262.May 22 2022, 1:37 PM

Patch updated:

  1. Use commutative matcher to reduce code.
  2. Add TODO for related pattern.
  3. Verify fold with more tests.
This revision is now accepted and ready to land.May 22 2022, 2:43 PM
This revision was landed with ongoing or failed builds.May 23 2022, 7:01 AM
This revision was automatically updated to reflect the committed changes.