[llvm] [LAA] Use computeConstantDifference() (PR #103725)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 14 05:04:20 PDT 2024
https://github.com/nikic updated https://github.com/llvm/llvm-project/pull/103725
>From a83f00e36302cd3da954f06f2af84046ff91c5a2 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 29 Jul 2024 17:22:27 +0200
Subject: [PATCH 1/2] [LAA] Use computeConstantDifference()
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%, see: http://llvm-compile-time-tracker.com/compare.php?from=6da3361f504495cef71caa4de4297234b6ea7fc7&to=b24e3c3338f62214d9abec8c9ef5a502953e7748&stat=instructions%3Au
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).
---
llvm/lib/Analysis/LoopAccessAnalysis.cpp | 6 +++---
llvm/lib/Analysis/ScalarEvolution.cpp | 2 +-
2 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/llvm/lib/Analysis/LoopAccessAnalysis.cpp b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
index d67fd7985f6a50..8c939f261e693d 100644
--- a/llvm/lib/Analysis/LoopAccessAnalysis.cpp
+++ b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
@@ -1599,11 +1599,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;
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index af341c55205de8..71a72fad35b320 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -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;
>From fd99984c251a83659ed4fc73e11019657e9d0795 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Wed, 14 Aug 2024 12:34:55 +0200
Subject: [PATCH 2/2] also modify getMinFromExprs
---
llvm/lib/Analysis/LoopAccessAnalysis.cpp | 8 +++-----
1 file changed, 3 insertions(+), 5 deletions(-)
diff --git a/llvm/lib/Analysis/LoopAccessAnalysis.cpp b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
index 8c939f261e693d..872bc52b82cca7 100644
--- a/llvm/lib/Analysis/LoopAccessAnalysis.cpp
+++ b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
@@ -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);
- 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(
More information about the llvm-commits
mailing list