This is an archive of the discontinued LLVM Phabricator instance.

[indvars] Rotate zext though icmp to reduce loop varying computation
ClosedPublic

Authored by reames on Oct 21 2021, 12:31 PM.

Details

Summary

This change looks for cases where we can prove that an exit test of a loop can be performed in a narrower bitwidth, and that by doing so we can replace a loop-varying extend with a loop-invariant truncate.

The motivation here is that doing this unblocks the trip count analysis for narrow IVs involved in extended compare exit tests. It also has the nice side effect of simply making the code faster, even if we gain no other benefit from the improved analysis ability.

I've noted a few places this could be extended, but I think this stands reasonable on it's own as well.

Diff Detail

Event Timeline

reames created this revision.Oct 21 2021, 12:31 PM
reames requested review of this revision.Oct 21 2021, 12:31 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 21 2021, 12:31 PM
mkazantsev added inline comments.Oct 25 2021, 10:53 PM
llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
1513

Looks more logical to check ICmp->isUnsigned() just where we checked it was icmp.

llvm/test/Transforms/IndVarSimplify/finite-exit-comparisons.ll
528

Please add a negative test where this doesn't work because of precision loss.

reames updated this revision to Diff 382697.Oct 27 2021, 9:41 AM
reames added inline comments.Oct 27 2021, 9:44 AM
llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
1513

Not sure I agree, but done.

llvm/test/Transforms/IndVarSimplify/finite-exit-comparisons.ll
528

Already covered by @slt_neg_no_mustprogress

mkazantsev accepted this revision.Oct 31 2021, 9:49 PM

LGTM, thanks!

This revision is now accepted and ready to land.Oct 31 2021, 9:49 PM
This revision was landed with ongoing or failed builds.Nov 3 2021, 12:09 PM
This revision was automatically updated to reflect the committed changes.

FYI, the must-exit logic from this patch was reverted in d4708fa4. See my response to my own revert on llvm-commits for explanation of why, and why I don't plan to reintroduce a fixed version. The constant range logic remains.