[llvm] 214e2d8 - [SCEV] Avoid repeated proveNoSignedWrapViaInduction calls.
Florian Hahn via llvm-commits
llvm-commits at lists.llvm.org
Fri Jul 29 01:15:19 PDT 2022
Author: Florian Hahn
Date: 2022-07-29T09:15:03+01:00
New Revision: 214e2d8fe5729de016b45c19274100a94ddc3454
URL: https://github.com/llvm/llvm-project/commit/214e2d8fe5729de016b45c19274100a94ddc3454
DIFF: https://github.com/llvm/llvm-project/commit/214e2d8fe5729de016b45c19274100a94ddc3454.diff
LOG: [SCEV] Avoid repeated proveNoSignedWrapViaInduction calls.
At the moment, proveNoSignedWrapViaInduction may be called for the
same AddRec a large number of times via getSignExtendExpr. This can have
a severe compile-time impact for very loop-heavy code.
If proveNoSignedWrapViaInduction failed to prove NSW the first time,
it is unlikely to succeed on subsequent tries and the cost doesn't seem
to be justified.
This is the signed version of 8daa338297d533d / D130648.
This can drastically improve compile-time in some excessive cases and
also has a slightly positive compile-time impact on CTMark:
NewPM-O3: -0.06%
NewPM-ReleaseThinLTO: -0.04%
NewPM-ReleaseLTO-g: -0.04%
https://llvm-compile-time-tracker.com/compare.php?from=8daa338297d533db4d1ae8d3770613eb25c29688&to=aed126a196e7a5a9803543d9b4d6bdb233d0009c&stat=instructions
Reviewed By: nikic
Differential Revision: https://reviews.llvm.org/D130694
Added:
Modified:
llvm/include/llvm/Analysis/ScalarEvolution.h
llvm/lib/Analysis/ScalarEvolution.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Analysis/ScalarEvolution.h b/llvm/include/llvm/Analysis/ScalarEvolution.h
index d49775cfd6f0e..b496ac91aaa73 100644
--- a/llvm/include/llvm/Analysis/ScalarEvolution.h
+++ b/llvm/include/llvm/Analysis/ScalarEvolution.h
@@ -2119,6 +2119,10 @@ class ScalarEvolution {
/// tried.
SmallPtrSet<const SCEVAddRecExpr *, 16> UnsignedWrapViaInductionTried;
+ /// Set of AddRecs for which proving NSW via an induction has already been
+ /// tried.
+ SmallPtrSet<const SCEVAddRecExpr *, 16> SignedWrapViaInductionTried;
+
/// The head of a linked list of all SCEVUnknown values that have been
/// allocated. This is used by releaseMemory to locate them all and call
/// their destructors.
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 7e1306895a85e..41aa53d7ab29a 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -4958,6 +4958,10 @@ ScalarEvolution::proveNoSignedWrapViaInduction(const SCEVAddRecExpr *AR) {
if (!AR->isAffine())
return Result;
+ // This function can be expensive, only try to prove NSW once per AddRec.
+ if (!SignedWrapViaInductionTried.insert(AR).second)
+ return Result;
+
const SCEV *Step = AR->getStepRecurrence(*this);
const Loop *L = AR->getLoop();
@@ -13584,8 +13588,10 @@ void ScalarEvolution::forgetMemoizedResultsImpl(const SCEV *S) {
HasRecMap.erase(S);
MinTrailingZerosCache.erase(S);
- if (auto *AR = dyn_cast<SCEVAddRecExpr>(S))
+ if (auto *AR = dyn_cast<SCEVAddRecExpr>(S)) {
UnsignedWrapViaInductionTried.erase(AR);
+ SignedWrapViaInductionTried.erase(AR);
+ }
auto ExprIt = ExprValueMap.find(S);
if (ExprIt != ExprValueMap.end()) {
More information about the llvm-commits
mailing list