This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Improve fold icmp of truncated left shift
AbandonedPublic

Authored by Chenbing.Zheng on Sep 22 2022, 12:55 AM.

Details

Summary

Original version only solve shifted constant 1, this patch expend it to PowerOf2

(trunc (2**Z << Y) to iN) == 0 -> Y u>  N - Z + 1
(trunc (2**Z << Y) to iN) != 0 -> Y u<= N - Z + 1

https://alive2.llvm.org/ce/z/zaQFeR
and

(trunc (2**Z << Y) to iN) == 2**C --> Y == C - Z
(trunc (2**Z << Y) to iN) != 2**C --> Y != C - Z

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptSep 22 2022, 12:55 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
Chenbing.Zheng requested review of this revision.Sep 22 2022, 12:55 AM
Chenbing.Zheng edited the summary of this revision. (Show Details)Sep 22 2022, 2:15 AM

The TODO comment was actually suggesting that we handle something like this:
https://alive2.llvm.org/ce/z/dHssjh
...but this patch won't match that? If that's correct, please leave the TODO comment there.

Please add that plus any other baseline tests, so we are just showing the diffs.

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

I prefer to give a constant value some name with "C" to make it obvious that we matched a constant, so C1 or ShiftC or something like that.

1560–1561

Move this comment down, so it is clear that it applies only when C == 0 - just above the C.isZero() line.
Change "Z" in the comment to match the variable name if it is changed.

address comments

RKSimon edited the summary of this revision. (Show Details)Sep 28 2022, 2:20 AM
spatel added inline comments.Sep 28 2022, 6:09 AM
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
1561

That looks correct, but I'd prefer that this comment matches what the code and the alive proof actually do, so I'd change it to something like this:

// (trunc (2**C1 << Y) to iN) == 0 -> Y u> ctlz(trunc(C1))
1569–1570

Add an Alive2 proof for this pattern to the patch description?

llvm/test/Transforms/InstCombine/icmp-trunc.ll
409

Remove TODO.

@Chenbing.Zheng Are you still looking at this?

Chenbing.Zheng abandoned this revision.Oct 23 2022, 7:07 PM

I'm going to abandon this patch. It seems that there is a more complete solution here.