13bab7e1aSChandler Carruth //===- LoopAnalysisManager.cpp - Loop analysis management -----------------===//
23bab7e1aSChandler Carruth //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63bab7e1aSChandler Carruth //
73bab7e1aSChandler Carruth //===----------------------------------------------------------------------===//
83bab7e1aSChandler Carruth
93bab7e1aSChandler Carruth #include "llvm/Analysis/LoopAnalysisManager.h"
10*2ce38b3fSdfukalov #include "llvm/Analysis/AssumptionCache.h"
113bab7e1aSChandler Carruth #include "llvm/Analysis/LoopInfo.h"
12ff8b8aeaSAlina Sbirlea #include "llvm/Analysis/MemorySSA.h"
133bab7e1aSChandler Carruth #include "llvm/Analysis/ScalarEvolution.h"
143bab7e1aSChandler Carruth #include "llvm/IR/Dominators.h"
15105642afSReid Kleckner #include "llvm/IR/PassManagerImpl.h"
163bab7e1aSChandler Carruth
173bab7e1aSChandler Carruth using namespace llvm;
183bab7e1aSChandler Carruth
19ff8b8aeaSAlina Sbirlea namespace llvm {
20d319674aSVedant Kumar // Explicit template instantiations and specialization definitions for core
213bab7e1aSChandler Carruth // template typedefs.
223bab7e1aSChandler Carruth template class AllAnalysesOn<Loop>;
233bab7e1aSChandler Carruth template class AnalysisManager<Loop, LoopStandardAnalysisResults &>;
243bab7e1aSChandler Carruth template class InnerAnalysisManagerProxy<LoopAnalysisManager, Function>;
25346542b7SChandler Carruth template class OuterAnalysisManagerProxy<FunctionAnalysisManager, Loop,
26346542b7SChandler Carruth LoopStandardAnalysisResults &>;
273bab7e1aSChandler Carruth
invalidate(Function & F,const PreservedAnalyses & PA,FunctionAnalysisManager::Invalidator & Inv)283bab7e1aSChandler Carruth bool LoopAnalysisManagerFunctionProxy::Result::invalidate(
293bab7e1aSChandler Carruth Function &F, const PreservedAnalyses &PA,
303bab7e1aSChandler Carruth FunctionAnalysisManager::Invalidator &Inv) {
313bab7e1aSChandler Carruth // First compute the sequence of IR units covered by this proxy. We will want
323bab7e1aSChandler Carruth // to visit this in postorder, but because this is a tree structure we can do
33f002264dSChandler Carruth // this by building a preorder sequence and walking it backwards. We also
34f002264dSChandler Carruth // want siblings in forward program order to match the LoopPassManager so we
35f002264dSChandler Carruth // get the preorder with siblings reversed.
36f002264dSChandler Carruth SmallVector<Loop *, 4> PreOrderLoops = LI->getLoopsInReverseSiblingPreorder();
373bab7e1aSChandler Carruth
383bab7e1aSChandler Carruth // If this proxy or the loop info is going to be invalidated, we also need
393bab7e1aSChandler Carruth // to clear all the keys coming from that analysis. We also completely blow
403bab7e1aSChandler Carruth // away the loop analyses if any of the standard analyses provided by the
413bab7e1aSChandler Carruth // loop pass manager go away so that loop analyses can freely use these
423bab7e1aSChandler Carruth // without worrying about declaring dependencies on them etc.
433bab7e1aSChandler Carruth // FIXME: It isn't clear if this is the right tradeoff. We could instead make
443bab7e1aSChandler Carruth // loop analyses declare any dependencies on these and use the more general
453bab7e1aSChandler Carruth // invalidation logic below to act on that.
463bab7e1aSChandler Carruth auto PAC = PA.getChecker<LoopAnalysisManagerFunctionProxy>();
47ff8b8aeaSAlina Sbirlea bool invalidateMemorySSAAnalysis = false;
487425179fSAlina Sbirlea if (MSSAUsed)
49ff8b8aeaSAlina Sbirlea invalidateMemorySSAAnalysis = Inv.invalidate<MemorySSAAnalysis>(F, PA);
503bab7e1aSChandler Carruth if (!(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) ||
513bab7e1aSChandler Carruth Inv.invalidate<AAManager>(F, PA) ||
523bab7e1aSChandler Carruth Inv.invalidate<AssumptionAnalysis>(F, PA) ||
533bab7e1aSChandler Carruth Inv.invalidate<DominatorTreeAnalysis>(F, PA) ||
543bab7e1aSChandler Carruth Inv.invalidate<LoopAnalysis>(F, PA) ||
55ff8b8aeaSAlina Sbirlea Inv.invalidate<ScalarEvolutionAnalysis>(F, PA) ||
56ff8b8aeaSAlina Sbirlea invalidateMemorySSAAnalysis) {
573bab7e1aSChandler Carruth // Note that the LoopInfo may be stale at this point, however the loop
583bab7e1aSChandler Carruth // objects themselves remain the only viable keys that could be in the
593bab7e1aSChandler Carruth // analysis manager's cache. So we just walk the keys and forcibly clear
603bab7e1aSChandler Carruth // those results. Note that the order doesn't matter here as this will just
613bab7e1aSChandler Carruth // directly destroy the results without calling methods on them.
62005b88c0SSanjoy Das for (Loop *L : PreOrderLoops) {
63005b88c0SSanjoy Das // NB! `L` may not be in a good enough state to run Loop::getName.
64005b88c0SSanjoy Das InnerAM->clear(*L, "<possibly invalidated loop>");
65005b88c0SSanjoy Das }
663bab7e1aSChandler Carruth
673bab7e1aSChandler Carruth // We also need to null out the inner AM so that when the object gets
683bab7e1aSChandler Carruth // destroyed as invalid we don't try to clear the inner AM again. At that
693bab7e1aSChandler Carruth // point we won't be able to reliably walk the loops for this function and
703bab7e1aSChandler Carruth // only clear results associated with those loops the way we do here.
713bab7e1aSChandler Carruth // FIXME: Making InnerAM null at this point isn't very nice. Most analyses
723bab7e1aSChandler Carruth // try to remain valid during invalidation. Maybe we should add an
733bab7e1aSChandler Carruth // `IsClean` flag?
743bab7e1aSChandler Carruth InnerAM = nullptr;
753bab7e1aSChandler Carruth
763bab7e1aSChandler Carruth // Now return true to indicate this *is* invalid and a fresh proxy result
773bab7e1aSChandler Carruth // needs to be built. This is especially important given the null InnerAM.
783bab7e1aSChandler Carruth return true;
793bab7e1aSChandler Carruth }
803bab7e1aSChandler Carruth
813bab7e1aSChandler Carruth // Directly check if the relevant set is preserved so we can short circuit
823bab7e1aSChandler Carruth // invalidating loops.
833bab7e1aSChandler Carruth bool AreLoopAnalysesPreserved =
843bab7e1aSChandler Carruth PA.allAnalysesInSetPreserved<AllAnalysesOn<Loop>>();
853bab7e1aSChandler Carruth
863bab7e1aSChandler Carruth // Since we have a valid LoopInfo we can actually leave the cached results in
873bab7e1aSChandler Carruth // the analysis manager associated with the Loop keys, but we need to
883bab7e1aSChandler Carruth // propagate any necessary invalidation logic into them. We'd like to
893bab7e1aSChandler Carruth // invalidate things in roughly the same order as they were put into the
903bab7e1aSChandler Carruth // cache and so we walk the preorder list in reverse to form a valid
913bab7e1aSChandler Carruth // postorder.
923bab7e1aSChandler Carruth for (Loop *L : reverse(PreOrderLoops)) {
933bab7e1aSChandler Carruth Optional<PreservedAnalyses> InnerPA;
943bab7e1aSChandler Carruth
953bab7e1aSChandler Carruth // Check to see whether the preserved set needs to be adjusted based on
963bab7e1aSChandler Carruth // function-level analysis invalidation triggering deferred invalidation
973bab7e1aSChandler Carruth // for this loop.
983bab7e1aSChandler Carruth if (auto *OuterProxy =
993bab7e1aSChandler Carruth InnerAM->getCachedResult<FunctionAnalysisManagerLoopProxy>(*L))
1003bab7e1aSChandler Carruth for (const auto &OuterInvalidationPair :
1013bab7e1aSChandler Carruth OuterProxy->getOuterInvalidations()) {
1023bab7e1aSChandler Carruth AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first;
1033bab7e1aSChandler Carruth const auto &InnerAnalysisIDs = OuterInvalidationPair.second;
1043bab7e1aSChandler Carruth if (Inv.invalidate(OuterAnalysisID, F, PA)) {
1053bab7e1aSChandler Carruth if (!InnerPA)
1063bab7e1aSChandler Carruth InnerPA = PA;
1073bab7e1aSChandler Carruth for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs)
1083bab7e1aSChandler Carruth InnerPA->abandon(InnerAnalysisID);
1093bab7e1aSChandler Carruth }
1103bab7e1aSChandler Carruth }
1113bab7e1aSChandler Carruth
1123bab7e1aSChandler Carruth // Check if we needed a custom PA set. If so we'll need to run the inner
1133bab7e1aSChandler Carruth // invalidation.
1143bab7e1aSChandler Carruth if (InnerPA) {
1153bab7e1aSChandler Carruth InnerAM->invalidate(*L, *InnerPA);
1163bab7e1aSChandler Carruth continue;
1173bab7e1aSChandler Carruth }
1183bab7e1aSChandler Carruth
1193bab7e1aSChandler Carruth // Otherwise we only need to do invalidation if the original PA set didn't
1203bab7e1aSChandler Carruth // preserve all Loop analyses.
1213bab7e1aSChandler Carruth if (!AreLoopAnalysesPreserved)
1223bab7e1aSChandler Carruth InnerAM->invalidate(*L, PA);
1233bab7e1aSChandler Carruth }
1243bab7e1aSChandler Carruth
1253bab7e1aSChandler Carruth // Return false to indicate that this result is still a valid proxy.
1263bab7e1aSChandler Carruth return false;
1273bab7e1aSChandler Carruth }
1283bab7e1aSChandler Carruth
1293bab7e1aSChandler Carruth template <>
1303bab7e1aSChandler Carruth LoopAnalysisManagerFunctionProxy::Result
run(Function & F,FunctionAnalysisManager & AM)1313bab7e1aSChandler Carruth LoopAnalysisManagerFunctionProxy::run(Function &F,
1323bab7e1aSChandler Carruth FunctionAnalysisManager &AM) {
1333bab7e1aSChandler Carruth return Result(*InnerAM, AM.getResult<LoopAnalysis>(F));
1343bab7e1aSChandler Carruth }
1353bab7e1aSChandler Carruth }
1363bab7e1aSChandler Carruth
getLoopPassPreservedAnalyses()1373bab7e1aSChandler Carruth PreservedAnalyses llvm::getLoopPassPreservedAnalyses() {
1383bab7e1aSChandler Carruth PreservedAnalyses PA;
1393bab7e1aSChandler Carruth PA.preserve<DominatorTreeAnalysis>();
1403bab7e1aSChandler Carruth PA.preserve<LoopAnalysis>();
1413bab7e1aSChandler Carruth PA.preserve<LoopAnalysisManagerFunctionProxy>();
1423bab7e1aSChandler Carruth PA.preserve<ScalarEvolutionAnalysis>();
1433bab7e1aSChandler Carruth return PA;
1443bab7e1aSChandler Carruth }
145