[llvm] [LAA] Use computeConstantDifference() (PR #103725)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 14 02:17:03 PDT 2024
https://github.com/nikic created https://github.com/llvm/llvm-project/pull/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%, 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).
>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] [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;
More information about the llvm-commits
mailing list