This is an archive of the discontinued LLVM Phabricator instance.

Use LVI to eliminate instructions during VP
Needs RevisionPublic

Authored by sehirst on Jun 22 2023, 8:38 AM.

Details

Reviewers
bryanpkc
nikic
Summary

LVI gives us a ConstantRange which we can use to determine if the
results of div and shr instructions will always be zero. Similarly, we
can determine if one side of max/min instructions will always be
returned.

Diff Detail

Event Timeline

sehirst created this revision.Jun 22 2023, 8:38 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 22 2023, 8:38 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
sehirst requested review of this revision.Jun 22 2023, 8:38 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 22 2023, 8:38 AM
sehirst edited the summary of this revision. (Show Details)Jun 22 2023, 8:39 AM
sehirst updated this revision to Diff 533692.Jun 22 2023, 10:29 AM

Rebased and resolved conflict

nikic requested changes to this revision.Jun 23 2023, 1:32 AM
nikic added a subscriber: nikic.

See https://llvm.org/docs/TestingGuide.html#precommit-workflow-for-tests and the following section for testing requirements.

TBH the entire patch makes very little sense to me.

llvm/include/llvm/IR/ConstantRange.h
283

I think you re-invented the icmp() method here. CR1.icmp(ICmpInst::ICMP_UGT, CR2) etc is how you're supposed to write these.

llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
578

How does your code differ from the existing getPredicateAt() check above?

The tests you added already fold without your change.

This revision now requires changes to proceed.Jun 23 2023, 1:32 AM

TBH the entire patch makes very little sense to me.

Thanks for the review. I think the main problem is that I was unaware of some key ConstantRange and LVI functionality, namely icmp() and getPredicateAt(), as you pointed out. Additionally, when I ported this patch from LLVM 15 to main I missed that some of the opportunities to remove instructions I was catching in this patch had already been caught. However, I ran my tests in this patch on main and found missed opportunities to remove sdiv, ashr, and lshr instructions.

I am re-working the patch to remove the duplication and follow the existing methods (e.g. the code to eliminate udiv's in expandUDivOrURem() using ConstantRange::icmp()).

I'll update this revision in a day or 2, including a fix for the build failure.

nikic added inline comments.Jun 27 2023, 1:16 AM
llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
1098

LVI should be able to determine that this value is constant by itself -- does using LVI->getConstant(SDI, SDI) give the desired result?