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/BasicAliasAnalysis.h"
123bab7e1aSChandler Carruth #include "llvm/Analysis/GlobalsModRef.h"
133bab7e1aSChandler Carruth #include "llvm/Analysis/LoopInfo.h"
14ff8b8aeaSAlina Sbirlea #include "llvm/Analysis/MemorySSA.h"
153bab7e1aSChandler Carruth #include "llvm/Analysis/ScalarEvolution.h"
163bab7e1aSChandler Carruth #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
173bab7e1aSChandler Carruth #include "llvm/IR/Dominators.h"
18105642afSReid Kleckner #include "llvm/IR/PassManagerImpl.h"
193bab7e1aSChandler Carruth 
203bab7e1aSChandler Carruth using namespace llvm;
213bab7e1aSChandler Carruth 
22ff8b8aeaSAlina Sbirlea namespace llvm {
23d319674aSVedant Kumar // Explicit template instantiations and specialization definitions for core
243bab7e1aSChandler Carruth // template typedefs.
253bab7e1aSChandler Carruth template class AllAnalysesOn<Loop>;
263bab7e1aSChandler Carruth template class AnalysisManager<Loop, LoopStandardAnalysisResults &>;
273bab7e1aSChandler Carruth template class InnerAnalysisManagerProxy<LoopAnalysisManager, Function>;
28346542b7SChandler Carruth template class OuterAnalysisManagerProxy<FunctionAnalysisManager, Loop,
29346542b7SChandler Carruth                                          LoopStandardAnalysisResults &>;
303bab7e1aSChandler Carruth 
313bab7e1aSChandler Carruth bool LoopAnalysisManagerFunctionProxy::Result::invalidate(
323bab7e1aSChandler Carruth     Function &F, const PreservedAnalyses &PA,
333bab7e1aSChandler Carruth     FunctionAnalysisManager::Invalidator &Inv) {
343bab7e1aSChandler Carruth   // First compute the sequence of IR units covered by this proxy. We will want
353bab7e1aSChandler Carruth   // to visit this in postorder, but because this is a tree structure we can do
36f002264dSChandler Carruth   // this by building a preorder sequence and walking it backwards. We also
37f002264dSChandler Carruth   // want siblings in forward program order to match the LoopPassManager so we
38f002264dSChandler Carruth   // get the preorder with siblings reversed.
39f002264dSChandler Carruth   SmallVector<Loop *, 4> PreOrderLoops = LI->getLoopsInReverseSiblingPreorder();
403bab7e1aSChandler Carruth 
413bab7e1aSChandler Carruth   // If this proxy or the loop info is going to be invalidated, we also need
423bab7e1aSChandler Carruth   // to clear all the keys coming from that analysis. We also completely blow
433bab7e1aSChandler Carruth   // away the loop analyses if any of the standard analyses provided by the
443bab7e1aSChandler Carruth   // loop pass manager go away so that loop analyses can freely use these
453bab7e1aSChandler Carruth   // without worrying about declaring dependencies on them etc.
463bab7e1aSChandler Carruth   // FIXME: It isn't clear if this is the right tradeoff. We could instead make
473bab7e1aSChandler Carruth   // loop analyses declare any dependencies on these and use the more general
483bab7e1aSChandler Carruth   // invalidation logic below to act on that.
493bab7e1aSChandler Carruth   auto PAC = PA.getChecker<LoopAnalysisManagerFunctionProxy>();
50ff8b8aeaSAlina Sbirlea   bool invalidateMemorySSAAnalysis = false;
517425179fSAlina Sbirlea   if (MSSAUsed)
52ff8b8aeaSAlina Sbirlea     invalidateMemorySSAAnalysis = Inv.invalidate<MemorySSAAnalysis>(F, PA);
533bab7e1aSChandler Carruth   if (!(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) ||
543bab7e1aSChandler Carruth       Inv.invalidate<AAManager>(F, PA) ||
553bab7e1aSChandler Carruth       Inv.invalidate<AssumptionAnalysis>(F, PA) ||
563bab7e1aSChandler Carruth       Inv.invalidate<DominatorTreeAnalysis>(F, PA) ||
573bab7e1aSChandler Carruth       Inv.invalidate<LoopAnalysis>(F, PA) ||
58ff8b8aeaSAlina Sbirlea       Inv.invalidate<ScalarEvolutionAnalysis>(F, PA) ||
59ff8b8aeaSAlina Sbirlea       invalidateMemorySSAAnalysis) {
603bab7e1aSChandler Carruth     // Note that the LoopInfo may be stale at this point, however the loop
613bab7e1aSChandler Carruth     // objects themselves remain the only viable keys that could be in the
623bab7e1aSChandler Carruth     // analysis manager's cache. So we just walk the keys and forcibly clear
633bab7e1aSChandler Carruth     // those results. Note that the order doesn't matter here as this will just
643bab7e1aSChandler Carruth     // directly destroy the results without calling methods on them.
65005b88c0SSanjoy Das     for (Loop *L : PreOrderLoops) {
66005b88c0SSanjoy Das       // NB! `L` may not be in a good enough state to run Loop::getName.
67005b88c0SSanjoy Das       InnerAM->clear(*L, "<possibly invalidated loop>");
68005b88c0SSanjoy Das     }
693bab7e1aSChandler Carruth 
703bab7e1aSChandler Carruth     // We also need to null out the inner AM so that when the object gets
713bab7e1aSChandler Carruth     // destroyed as invalid we don't try to clear the inner AM again. At that
723bab7e1aSChandler Carruth     // point we won't be able to reliably walk the loops for this function and
733bab7e1aSChandler Carruth     // only clear results associated with those loops the way we do here.
743bab7e1aSChandler Carruth     // FIXME: Making InnerAM null at this point isn't very nice. Most analyses
753bab7e1aSChandler Carruth     // try to remain valid during invalidation. Maybe we should add an
763bab7e1aSChandler Carruth     // `IsClean` flag?
773bab7e1aSChandler Carruth     InnerAM = nullptr;
783bab7e1aSChandler Carruth 
793bab7e1aSChandler Carruth     // Now return true to indicate this *is* invalid and a fresh proxy result
803bab7e1aSChandler Carruth     // needs to be built. This is especially important given the null InnerAM.
813bab7e1aSChandler Carruth     return true;
823bab7e1aSChandler Carruth   }
833bab7e1aSChandler Carruth 
843bab7e1aSChandler Carruth   // Directly check if the relevant set is preserved so we can short circuit
853bab7e1aSChandler Carruth   // invalidating loops.
863bab7e1aSChandler Carruth   bool AreLoopAnalysesPreserved =
873bab7e1aSChandler Carruth       PA.allAnalysesInSetPreserved<AllAnalysesOn<Loop>>();
883bab7e1aSChandler Carruth 
893bab7e1aSChandler Carruth   // Since we have a valid LoopInfo we can actually leave the cached results in
903bab7e1aSChandler Carruth   // the analysis manager associated with the Loop keys, but we need to
913bab7e1aSChandler Carruth   // propagate any necessary invalidation logic into them. We'd like to
923bab7e1aSChandler Carruth   // invalidate things in roughly the same order as they were put into the
933bab7e1aSChandler Carruth   // cache and so we walk the preorder list in reverse to form a valid
943bab7e1aSChandler Carruth   // postorder.
953bab7e1aSChandler Carruth   for (Loop *L : reverse(PreOrderLoops)) {
963bab7e1aSChandler Carruth     Optional<PreservedAnalyses> InnerPA;
973bab7e1aSChandler Carruth 
983bab7e1aSChandler Carruth     // Check to see whether the preserved set needs to be adjusted based on
993bab7e1aSChandler Carruth     // function-level analysis invalidation triggering deferred invalidation
1003bab7e1aSChandler Carruth     // for this loop.
1013bab7e1aSChandler Carruth     if (auto *OuterProxy =
1023bab7e1aSChandler Carruth             InnerAM->getCachedResult<FunctionAnalysisManagerLoopProxy>(*L))
1033bab7e1aSChandler Carruth       for (const auto &OuterInvalidationPair :
1043bab7e1aSChandler Carruth            OuterProxy->getOuterInvalidations()) {
1053bab7e1aSChandler Carruth         AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first;
1063bab7e1aSChandler Carruth         const auto &InnerAnalysisIDs = OuterInvalidationPair.second;
1073bab7e1aSChandler Carruth         if (Inv.invalidate(OuterAnalysisID, F, PA)) {
1083bab7e1aSChandler Carruth           if (!InnerPA)
1093bab7e1aSChandler Carruth             InnerPA = PA;
1103bab7e1aSChandler Carruth           for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs)
1113bab7e1aSChandler Carruth             InnerPA->abandon(InnerAnalysisID);
1123bab7e1aSChandler Carruth         }
1133bab7e1aSChandler Carruth       }
1143bab7e1aSChandler Carruth 
1153bab7e1aSChandler Carruth     // Check if we needed a custom PA set. If so we'll need to run the inner
1163bab7e1aSChandler Carruth     // invalidation.
1173bab7e1aSChandler Carruth     if (InnerPA) {
1183bab7e1aSChandler Carruth       InnerAM->invalidate(*L, *InnerPA);
1193bab7e1aSChandler Carruth       continue;
1203bab7e1aSChandler Carruth     }
1213bab7e1aSChandler Carruth 
1223bab7e1aSChandler Carruth     // Otherwise we only need to do invalidation if the original PA set didn't
1233bab7e1aSChandler Carruth     // preserve all Loop analyses.
1243bab7e1aSChandler Carruth     if (!AreLoopAnalysesPreserved)
1253bab7e1aSChandler Carruth       InnerAM->invalidate(*L, PA);
1263bab7e1aSChandler Carruth   }
1273bab7e1aSChandler Carruth 
1283bab7e1aSChandler Carruth   // Return false to indicate that this result is still a valid proxy.
1293bab7e1aSChandler Carruth   return false;
1303bab7e1aSChandler Carruth }
1313bab7e1aSChandler Carruth 
1323bab7e1aSChandler Carruth template <>
1333bab7e1aSChandler Carruth LoopAnalysisManagerFunctionProxy::Result
1343bab7e1aSChandler Carruth LoopAnalysisManagerFunctionProxy::run(Function &F,
1353bab7e1aSChandler Carruth                                       FunctionAnalysisManager &AM) {
1363bab7e1aSChandler Carruth   return Result(*InnerAM, AM.getResult<LoopAnalysis>(F));
1373bab7e1aSChandler Carruth }
1383bab7e1aSChandler Carruth }
1393bab7e1aSChandler Carruth 
1403bab7e1aSChandler Carruth PreservedAnalyses llvm::getLoopPassPreservedAnalyses() {
1413bab7e1aSChandler Carruth   PreservedAnalyses PA;
1423bab7e1aSChandler Carruth   PA.preserve<DominatorTreeAnalysis>();
1433bab7e1aSChandler Carruth   PA.preserve<LoopAnalysis>();
1443bab7e1aSChandler Carruth   PA.preserve<LoopAnalysisManagerFunctionProxy>();
1453bab7e1aSChandler Carruth   PA.preserve<ScalarEvolutionAnalysis>();
146ff8b8aeaSAlina Sbirlea   // FIXME: What we really want to do here is preserve an AA category, but that
1473bab7e1aSChandler Carruth   // concept doesn't exist yet.
1483bab7e1aSChandler Carruth   PA.preserve<AAManager>();
1493bab7e1aSChandler Carruth   PA.preserve<BasicAA>();
1503bab7e1aSChandler Carruth   PA.preserve<GlobalsAA>();
1513bab7e1aSChandler Carruth   PA.preserve<SCEVAA>();
1523bab7e1aSChandler Carruth   return PA;
1533bab7e1aSChandler Carruth }
154