This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Fold ult(add(x,-1),c) -> ule(x,c) iff x != 0 (PR57635)
ClosedPublic

Authored by RKSimon on Sep 19 2022, 3:09 AM.

Details

Summary

Alive2: https://alive2.llvm.org/ce/z/sZ6wwS

As detailed on Issue #57635 and #37628 - for unsigned comparisons, we can compare prior to a loop counter decrement iff the counter is known never to be zero.

Diff Detail

Event Timeline

RKSimon created this revision.Sep 19 2022, 3:09 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 19 2022, 3:09 AM
RKSimon requested review of this revision.Sep 19 2022, 3:09 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 19 2022, 3:09 AM

Needs minimal tests for -instcombine alone.

I looked at this kind of pattern before, and it seems like a case of missing "nuw" analysis, but I didn't see a way to solve it given that we aggressively convert "sub" to "add" even if that means dropping "nuw". Once that information is lost, there's no way to recover without some kind of ValueTracking.

RKSimon updated this revision to Diff 461550.Sep 20 2022, 6:06 AM

rebase with instcombine test coverage

spatel accepted this revision.Sep 20 2022, 7:56 AM

LGTM

This revision is now accepted and ready to land.Sep 20 2022, 7:56 AM
This revision was landed with ongoing or failed builds.Sep 20 2022, 8:45 AM
This revision was automatically updated to reflect the committed changes.