Skip to content

Commit 6a84af7

Browse files
authored
[LAA] Use computeConstantDifference() (llvm#103725)
Use computeConstantDifference() instead of casting getMinusSCEV() to SCEVConstant. This can be much faster in some cases, because computeConstantDifference() computes the result without creating new SCEV expressions. This improves LTO/ThinLTO compile-time for lencod by more than 10%. I've verified that computeConstantDifference() does not produce worse results than the previous code for anything in llvm-test-suite. This required raising the iteration cutoff to 6. I ended up increasing it to 8 just to be on the safe side (for code outside llvm-test-suite), and because this doesn't materially affect compile-time anyway (we'll almost always bail out earlier).
1 parent 65390f9 commit 6a84af7

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

llvm/lib/Analysis/LoopAccessAnalysis.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -412,12 +412,10 @@ bool RuntimePointerChecking::needsChecking(
412412
/// Return nullptr in case we couldn't find an answer.
413413
static const SCEV *getMinFromExprs(const SCEV *I, const SCEV *J,
414414
ScalarEvolution *SE) {
415-
const SCEV *Diff = SE->getMinusSCEV(J, I);
416-
const SCEVConstant *C = dyn_cast<const SCEVConstant>(Diff);
417-
418-
if (!C)
415+
std::optional<APInt> Diff = SE->computeConstantDifference(J, I);
416+
if (!Diff)
419417
return nullptr;
420-
return C->getValue()->isNegative() ? J : I;
418+
return Diff->isNegative() ? J : I;
421419
}
422420

423421
bool RuntimeCheckingPtrGroup::addPointer(
@@ -1599,11 +1597,11 @@ std::optional<int> llvm::getPointersDiff(Type *ElemTyA, Value *PtrA,
15991597
// Otherwise compute the distance with SCEV between the base pointers.
16001598
const SCEV *PtrSCEVA = SE.getSCEV(PtrA);
16011599
const SCEV *PtrSCEVB = SE.getSCEV(PtrB);
1602-
const auto *Diff =
1603-
dyn_cast<SCEVConstant>(SE.getMinusSCEV(PtrSCEVB, PtrSCEVA));
1600+
std::optional<APInt> Diff =
1601+
SE.computeConstantDifference(PtrSCEVB, PtrSCEVA);
16041602
if (!Diff)
16051603
return std::nullopt;
1606-
Val = Diff->getAPInt().getSExtValue();
1604+
Val = Diff->getSExtValue();
16071605
}
16081606
int Size = DL.getTypeStoreSize(ElemTyA);
16091607
int Dist = Val / Size;

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11956,7 +11956,7 @@ ScalarEvolution::computeConstantDifference(const SCEV *More, const SCEV *Less) {
1195611956
APInt DiffMul(BW, 1);
1195711957
// Try various simplifications to reduce the difference to a constant. Limit
1195811958
// the number of allowed simplifications to keep compile-time low.
11959-
for (unsigned I = 0; I < 5; ++I) {
11959+
for (unsigned I = 0; I < 8; ++I) {
1196011960
if (More == Less)
1196111961
return Diff;
1196211962

0 commit comments

Comments
 (0)