This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] improve fold for icmp-ugt-ashr
ClosedPublic

Authored by Chenbing.Zheng on Jun 7 2022, 1:03 AM.

Details

Summary

Existing condition for
fold icmp ugt (ashr X, ShAmtC), C --> icmp ugt X, ((C + 1) << ShAmtC) - 1
missed some boundary. It cause this fold don't work for some cases, and the
reason is due to signed number overflow.

Diff Detail

Event Timeline

Chenbing.Zheng created this revision.Jun 7 2022, 1:03 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 7 2022, 1:03 AM
Chenbing.Zheng requested review of this revision.Jun 7 2022, 1:03 AM

Do you have an alive2 test?

For reference, this was the Alive2 from the original review ( D117365 ):
https://alive2.llvm.org/ce/z/tFAcZt

To catch the overflow case, I think we check if the incremented + shifted constant became signed min (0x8000...):
https://alive2.llvm.org/ce/z/RZdftJ

So that should be simpler than the pre-condition currently shown in this patch.

(C + 1).shl(ShAmtVal).isMinSignedValue()

address comment

Chenbing.Zheng edited the summary of this revision. (Show Details)Jun 7 2022, 7:20 PM
spatel accepted this revision.Jun 8 2022, 11:42 AM

LGTM - it may be worth looking at the related folds to see if there's some generalization that we missed (see inline comment about the other TODO comment in the test file).

llvm/test/Transforms/InstCombine/icmp-shr.ll
857–858

Check the history to see where this got fixed? Either way, we should delete the TODO since it optimized now.

This revision is now accepted and ready to land.Jun 8 2022, 11:42 AM
spatel added inline comments.Jun 8 2022, 11:47 AM
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
2258–2259

Rephrase this comment:

// 'C + 1 << ShAmtC' can overflow as a signed number, so the 2nd
// clause accounts for that pattern.
This revision was landed with ongoing or failed builds.Jun 9 2022, 1:22 AM
This revision was automatically updated to reflect the committed changes.
Chenbing.Zheng marked 2 inline comments as done.Jun 9 2022, 1:25 AM
Chenbing.Zheng added inline comments.
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
2258–2259

I modified it when commit

llvm/test/Transforms/InstCombine/icmp-shr.ll
857–858

I modified it when commit