This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] improve fold for icmp_eq_and to icmp_ult
ClosedPublic

Authored by Chenbing.Zheng on Jun 29 2022, 1:51 AM.

Details

Summary

In D95959, the improve analysis for "C >> X" broken the fold
((%x & C) == 0) --> %x u< (-C) iff (-C) is power of two.

It simplifies C, but fails to satisfy the fold condition.
This patch try to restore C before the fold.

Diff Detail

Event Timeline

Chenbing.Zheng created this revision.Jun 29 2022, 1:51 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 29 2022, 1:51 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
Chenbing.Zheng requested review of this revision.Jun 29 2022, 1:51 AM

This seems like it should be a known bits transform instead. Any value with high zeros should allow the transform, not just a 'lshr'.
For example, we might have a udiv of a constant:
https://alive2.llvm.org/ce/z/A5ZTf2

address comment

spatel added inline comments.Jul 1 2022, 5:47 AM
llvm/test/Transforms/InstCombine/lshr-and-negC-icmpeq-zero.ll
215

This is almost the same as @scalar_i32_lshr_and_negC_eq_X_is_constant1.

We would have better coverage if this is changed to use udiv instead of lshr. Also, change predicate to 'ne' so we are testing that code path too.

We should also have a negative test where the high bits are all clear except for the last necessary bit - that way, we will know that the known-bits calculation is correct.

address comments

spatel accepted this revision.Jul 4 2022, 12:35 PM

LGTM

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
1782

Make this comment more exact:

// Set high zeros of C2 to allow matching negated power-of-2.
1788–1789

I think this should be updated to use isNegatedPowerOf2 - if that's correct, you could even commit that change as a NFC patch before this one.

This revision is now accepted and ready to land.Jul 4 2022, 12:35 PM
Chenbing.Zheng added inline comments.Jul 4 2022, 7:59 PM
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
1788–1789
This revision was landed with ongoing or failed builds.Jul 5 2022, 2:18 AM
This revision was automatically updated to reflect the committed changes.