Skip to content

[LAA] Use computeConstantDifference() #103725

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions llvm/lib/Analysis/LoopAccessAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,12 +412,10 @@ bool RuntimePointerChecking::needsChecking(
/// Return nullptr in case we couldn't find an answer.
static const SCEV *getMinFromExprs(const SCEV *I, const SCEV *J,
ScalarEvolution *SE) {
const SCEV *Diff = SE->getMinusSCEV(J, I);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Off topic - but this is essentially simplifying a three way compare. We've recently added three way compares to IR, and we've got analogous simplification logic inside SCEV for min/max. Should we maybe have an interface on SCEV which does the 3 way compare, and returns either the result or a SCEVCouldNotCompute?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Off topic, but in a slightly different way... It looks like the sole user of this is addPointer. I can't see any reason that the Min and Max can't simply be the SCEV expressions for the min and max of all pointers. We'd need to cost whether generating such a group is profitable, but that's distinct from legality. One concern is that I think the current logic is relying on the min/max simplification to fail when two pointers are based on different underlying objects. We'd need to explicitly handle that. There may also be a profitability concern about forming "too large" a group if the offsets are runtime values with non-overlapping ranges.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think taking a normal SCEV min/max would be fine in terms of correctness, but as you say, profitability is less clear. I also suspect that the logic used here may be stronger than what SCEV does for this particular case. SCEV only uses isKnownViaNonRecursiveReasoning to simplify min/max expression. (Of course, it can also be weaker in other respects...)

const SCEVConstant *C = dyn_cast<const SCEVConstant>(Diff);

if (!C)
std::optional<APInt> Diff = SE->computeConstantDifference(J, I);
if (!Diff)
return nullptr;
return C->getValue()->isNegative() ? J : I;
return Diff->isNegative() ? J : I;
}

bool RuntimeCheckingPtrGroup::addPointer(
Expand Down Expand Up @@ -1599,11 +1597,11 @@ std::optional<int> llvm::getPointersDiff(Type *ElemTyA, Value *PtrA,
// Otherwise compute the distance with SCEV between the base pointers.
const SCEV *PtrSCEVA = SE.getSCEV(PtrA);
const SCEV *PtrSCEVB = SE.getSCEV(PtrB);
const auto *Diff =
dyn_cast<SCEVConstant>(SE.getMinusSCEV(PtrSCEVB, PtrSCEVA));
std::optional<APInt> Diff =
SE.computeConstantDifference(PtrSCEVB, PtrSCEVA);
if (!Diff)
return std::nullopt;
Val = Diff->getAPInt().getSExtValue();
Val = Diff->getSExtValue();
}
int Size = DL.getTypeStoreSize(ElemTyA);
int Dist = Val / Size;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Analysis/ScalarEvolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11956,7 +11956,7 @@ ScalarEvolution::computeConstantDifference(const SCEV *More, const SCEV *Less) {
APInt DiffMul(BW, 1);
// Try various simplifications to reduce the difference to a constant. Limit
// the number of allowed simplifications to keep compile-time low.
for (unsigned I = 0; I < 5; ++I) {
for (unsigned I = 0; I < 8; ++I) {
if (More == Less)
return Diff;

Expand Down
Loading