From 5028dea65266ab8b7f8f9ebd5d5e01faacebc645 Mon Sep 17 00:00:00 2001 From: Florian Hahn Date: Sat, 6 Jul 2024 22:14:01 +0100 Subject: [PATCH] [LAA] Only invalidate loops that require runtime checks (NFCI). LAA doesn't keep references to IR outside the loop or references to SCEVs that may be invalidated, unless runtime checks are needed (either memory or SCEV predicates). For the current LAA users, it should be sufficient to invalidate entries for loops that require runtime checks, thus avoiding analyzing loops again unnecessarily. This helps reduce compile-time, in particular when removing the restrictions added in 234cc40adc6. https://llvm-compile-time-tracker.com/compare.php?from=73894dba2cdbcc00678d0c13a6b61765675f60b4&to=05c6bdc41b5f63696ebeb7116325725fa94f66d6&stat=instructions:u --- llvm/include/llvm/Analysis/LoopAccessAnalysis.h | 2 +- llvm/lib/Analysis/LoopAccessAnalysis.cpp | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/llvm/include/llvm/Analysis/LoopAccessAnalysis.h b/llvm/include/llvm/Analysis/LoopAccessAnalysis.h index c74e76604e786d..f6bb044392938e 100644 --- a/llvm/include/llvm/Analysis/LoopAccessAnalysis.h +++ b/llvm/include/llvm/Analysis/LoopAccessAnalysis.h @@ -863,7 +863,7 @@ class LoopAccessInfoManager { const LoopAccessInfo &getInfo(Loop &L); - void clear() { LoopAccessInfoMap.clear(); } + void clear(); bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &Inv); diff --git a/llvm/lib/Analysis/LoopAccessAnalysis.cpp b/llvm/lib/Analysis/LoopAccessAnalysis.cpp index f132e455405253..018861a665c4cd 100644 --- a/llvm/lib/Analysis/LoopAccessAnalysis.cpp +++ b/llvm/lib/Analysis/LoopAccessAnalysis.cpp @@ -3084,6 +3084,22 @@ const LoopAccessInfo &LoopAccessInfoManager::getInfo(Loop &L) { return *It->second; } +void LoopAccessInfoManager::clear() { + SmallVector ToRemove; + // Collect LoopAccessInfo entries that may keep references to IR outside the + // analyzed loop or SCEVs that may have been modified or invalidated. At the + // moment, that is loops requiring memory or SCEV runtime checks, as those cache + // SCEVs, e.g. for pointer expressions. + for (const auto &[L, LAI] : LoopAccessInfoMap) { + if (LAI->getRuntimePointerChecking()->getChecks().empty() && + LAI->getPSE().getPredicate().isAlwaysTrue()) + continue; + ToRemove.push_back(L); + } + + for (Loop *L : ToRemove) + LoopAccessInfoMap.erase(L); +} bool LoopAccessInfoManager::invalidate( Function &F, const PreservedAnalyses &PA,