This is an archive of the discontinued LLVM Phabricator instance.

[InstSimplify] Use knownbits for simplifying `(icmp ugt/ule (or X, Y), X)`; PR64610
AbandonedPublic

Authored by goldstein.w.n on Aug 11 2023, 1:19 PM.

Details

Reviewers
nikic
Allen
Summary

(icmp ule (or X, Y), X)

If there are any unique bits between `X` and `Y`
--> false

(icmp ugt (or X, Y), X)

If there are any unique bits between `X` and `Y`
--> true

Proofs: https://alive2.llvm.org/ce/z/igZM4Q

This patch adds new functionality to InstCombine/InstSimplify,
although it should be noted, this logic did exist elsewhere in
LLVM pipeline.

The rationale is InstCombine can produce (icmp (or X, Y), X)
relatively late in the pipeline (after the other passes that would
normally simplify this well have run), leading to missed
optimizations: https://github.com/llvm/llvm-project/issues/64610

Diff Detail

Event Timeline

goldstein.w.n created this revision.Aug 11 2023, 1:19 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 11 2023, 1:19 PM
goldstein.w.n requested review of this revision.Aug 11 2023, 1:19 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 11 2023, 1:19 PM
nikic added inline comments.Aug 12 2023, 6:54 AM
llvm/lib/Analysis/InstructionSimplify.cpp
3135–3140
goldstein.w.n added inline comments.Aug 12 2023, 10:11 AM
llvm/lib/Analysis/InstructionSimplify.cpp
3135–3140

Is that right. It's really 'have a common bit not set', it's okay if there are some though

Rebase

llvm/lib/Analysis/InstructionSimplify.cpp
3135–3140

I added some tests that have some overlapping bits to make it more clear.

nikic accepted this revision.Aug 12 2023, 10:49 AM

LGTM

llvm/lib/Analysis/InstructionSimplify.cpp
3135–3140

Oh, I see, I misunderstood what you're going for here. In that case I don't think your comment in the patch description that this does not add new capability is probably not right. I don't think we'd fold the case where there are common bits elsewhere.

I personally think writing the condition here as if ((YKnown.One & RHSKnown.Zero) != 0) would be cleaner.

This revision is now accepted and ready to land.Aug 12 2023, 10:49 AM
goldstein.w.n added inline comments.Aug 12 2023, 11:24 AM
llvm/lib/Analysis/InstructionSimplify.cpp
3135–3140

We handle this in other passes. Its new to instsimplify/instcombine.

Use nikics suggestion.

goldstein.w.n edited the summary of this revision. (Show Details)Aug 12 2023, 11:35 AM
goldstein.w.n marked an inline comment as done.
goldstein.w.n added inline comments.
llvm/lib/Analysis/InstructionSimplify.cpp
3135–3140

Used your suggestion, also updated summary to be more clear this is new to instcombine.

I just had the thought that a better way to handle this would be to fold ugt to ne and ule to eq. In fact, this is what D144610 does. I think that would fix the motivating case as well?

llvm/lib/Analysis/InstructionSimplify.cpp
3135

equivalent

I just had the thought that a better way to handle this would be to fold ugt to ne and ule to eq. In fact, this is what D144610 does. I think that would fix the motivating case as well?

I believe that would work as well. Happy to replace with D144610

goldstein.w.n abandoned this revision.Aug 12 2023, 2:54 PM

Going to go forward with D144610 instead.

foad added a comment.Aug 14 2023, 1:59 AM

(icmp ule (or X, Y), X)

If there are any unique bits between X and Y
--> false

I don't understand "unique bits between". I think you mean: if there are any bits set in Y that are not set in X?