13bab7e1aSChandler Carruth //===- LoopAnalysisManager.cpp - Loop analysis management -----------------===//
23bab7e1aSChandler Carruth //
33bab7e1aSChandler Carruth //                     The LLVM Compiler Infrastructure
43bab7e1aSChandler Carruth //
53bab7e1aSChandler Carruth // This file is distributed under the University of Illinois Open Source
63bab7e1aSChandler Carruth // License. See LICENSE.TXT for details.
73bab7e1aSChandler Carruth //
83bab7e1aSChandler Carruth //===----------------------------------------------------------------------===//
93bab7e1aSChandler Carruth 
103bab7e1aSChandler Carruth #include "llvm/Analysis/LoopAnalysisManager.h"
113bab7e1aSChandler Carruth #include "llvm/Analysis/BasicAliasAnalysis.h"
123bab7e1aSChandler Carruth #include "llvm/Analysis/GlobalsModRef.h"
133bab7e1aSChandler Carruth #include "llvm/Analysis/LoopInfo.h"
14*ff8b8aeaSAlina 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"
183bab7e1aSChandler Carruth 
193bab7e1aSChandler Carruth using namespace llvm;
203bab7e1aSChandler Carruth 
21*ff8b8aeaSAlina Sbirlea namespace llvm {
22*ff8b8aeaSAlina Sbirlea /// Enables memory ssa as a dependency for loop passes in legacy pass manager.
23*ff8b8aeaSAlina Sbirlea cl::opt<bool> EnableMSSALoopDependency(
24*ff8b8aeaSAlina Sbirlea     "enable-mssa-loop-dependency", cl::Hidden, cl::init(false),
25*ff8b8aeaSAlina Sbirlea     cl::desc("Enable MemorySSA dependency for loop pass manager"));
26*ff8b8aeaSAlina Sbirlea 
273bab7e1aSChandler Carruth // Explicit template instantiations and specialization defininitions for core
283bab7e1aSChandler Carruth // template typedefs.
293bab7e1aSChandler Carruth template class AllAnalysesOn<Loop>;
303bab7e1aSChandler Carruth template class AnalysisManager<Loop, LoopStandardAnalysisResults &>;
313bab7e1aSChandler Carruth template class InnerAnalysisManagerProxy<LoopAnalysisManager, Function>;
32346542b7SChandler Carruth template class OuterAnalysisManagerProxy<FunctionAnalysisManager, Loop,
33346542b7SChandler Carruth                                          LoopStandardAnalysisResults &>;
343bab7e1aSChandler Carruth 
353bab7e1aSChandler Carruth bool LoopAnalysisManagerFunctionProxy::Result::invalidate(
363bab7e1aSChandler Carruth     Function &F, const PreservedAnalyses &PA,
373bab7e1aSChandler Carruth     FunctionAnalysisManager::Invalidator &Inv) {
383bab7e1aSChandler Carruth   // First compute the sequence of IR units covered by this proxy. We will want
393bab7e1aSChandler Carruth   // to visit this in postorder, but because this is a tree structure we can do
40f002264dSChandler Carruth   // this by building a preorder sequence and walking it backwards. We also
41f002264dSChandler Carruth   // want siblings in forward program order to match the LoopPassManager so we
42f002264dSChandler Carruth   // get the preorder with siblings reversed.
43f002264dSChandler Carruth   SmallVector<Loop *, 4> PreOrderLoops = LI->getLoopsInReverseSiblingPreorder();
443bab7e1aSChandler Carruth 
453bab7e1aSChandler Carruth   // If this proxy or the loop info is going to be invalidated, we also need
463bab7e1aSChandler Carruth   // to clear all the keys coming from that analysis. We also completely blow
473bab7e1aSChandler Carruth   // away the loop analyses if any of the standard analyses provided by the
483bab7e1aSChandler Carruth   // loop pass manager go away so that loop analyses can freely use these
493bab7e1aSChandler Carruth   // without worrying about declaring dependencies on them etc.
503bab7e1aSChandler Carruth   // FIXME: It isn't clear if this is the right tradeoff. We could instead make
513bab7e1aSChandler Carruth   // loop analyses declare any dependencies on these and use the more general
523bab7e1aSChandler Carruth   // invalidation logic below to act on that.
533bab7e1aSChandler Carruth   auto PAC = PA.getChecker<LoopAnalysisManagerFunctionProxy>();
54*ff8b8aeaSAlina Sbirlea   bool invalidateMemorySSAAnalysis = false;
55*ff8b8aeaSAlina Sbirlea   if (EnableMSSALoopDependency)
56*ff8b8aeaSAlina Sbirlea     invalidateMemorySSAAnalysis = Inv.invalidate<MemorySSAAnalysis>(F, PA);
573bab7e1aSChandler Carruth   if (!(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) ||
583bab7e1aSChandler Carruth       Inv.invalidate<AAManager>(F, PA) ||
593bab7e1aSChandler Carruth       Inv.invalidate<AssumptionAnalysis>(F, PA) ||
603bab7e1aSChandler Carruth       Inv.invalidate<DominatorTreeAnalysis>(F, PA) ||
613bab7e1aSChandler Carruth       Inv.invalidate<LoopAnalysis>(F, PA) ||
62*ff8b8aeaSAlina Sbirlea       Inv.invalidate<ScalarEvolutionAnalysis>(F, PA) ||
63*ff8b8aeaSAlina Sbirlea       invalidateMemorySSAAnalysis) {
643bab7e1aSChandler Carruth     // Note that the LoopInfo may be stale at this point, however the loop
653bab7e1aSChandler Carruth     // objects themselves remain the only viable keys that could be in the
663bab7e1aSChandler Carruth     // analysis manager's cache. So we just walk the keys and forcibly clear
673bab7e1aSChandler Carruth     // those results. Note that the order doesn't matter here as this will just
683bab7e1aSChandler Carruth     // directly destroy the results without calling methods on them.
69005b88c0SSanjoy Das     for (Loop *L : PreOrderLoops) {
70005b88c0SSanjoy Das       // NB! `L` may not be in a good enough state to run Loop::getName.
71005b88c0SSanjoy Das       InnerAM->clear(*L, "<possibly invalidated loop>");
72005b88c0SSanjoy Das     }
733bab7e1aSChandler Carruth 
743bab7e1aSChandler Carruth     // We also need to null out the inner AM so that when the object gets
753bab7e1aSChandler Carruth     // destroyed as invalid we don't try to clear the inner AM again. At that
763bab7e1aSChandler Carruth     // point we won't be able to reliably walk the loops for this function and
773bab7e1aSChandler Carruth     // only clear results associated with those loops the way we do here.
783bab7e1aSChandler Carruth     // FIXME: Making InnerAM null at this point isn't very nice. Most analyses
793bab7e1aSChandler Carruth     // try to remain valid during invalidation. Maybe we should add an
803bab7e1aSChandler Carruth     // `IsClean` flag?
813bab7e1aSChandler Carruth     InnerAM = nullptr;
823bab7e1aSChandler Carruth 
833bab7e1aSChandler Carruth     // Now return true to indicate this *is* invalid and a fresh proxy result
843bab7e1aSChandler Carruth     // needs to be built. This is especially important given the null InnerAM.
853bab7e1aSChandler Carruth     return true;
863bab7e1aSChandler Carruth   }
873bab7e1aSChandler Carruth 
883bab7e1aSChandler Carruth   // Directly check if the relevant set is preserved so we can short circuit
893bab7e1aSChandler Carruth   // invalidating loops.
903bab7e1aSChandler Carruth   bool AreLoopAnalysesPreserved =
913bab7e1aSChandler Carruth       PA.allAnalysesInSetPreserved<AllAnalysesOn<Loop>>();
923bab7e1aSChandler Carruth 
933bab7e1aSChandler Carruth   // Since we have a valid LoopInfo we can actually leave the cached results in
943bab7e1aSChandler Carruth   // the analysis manager associated with the Loop keys, but we need to
953bab7e1aSChandler Carruth   // propagate any necessary invalidation logic into them. We'd like to
963bab7e1aSChandler Carruth   // invalidate things in roughly the same order as they were put into the
973bab7e1aSChandler Carruth   // cache and so we walk the preorder list in reverse to form a valid
983bab7e1aSChandler Carruth   // postorder.
993bab7e1aSChandler Carruth   for (Loop *L : reverse(PreOrderLoops)) {
1003bab7e1aSChandler Carruth     Optional<PreservedAnalyses> InnerPA;
1013bab7e1aSChandler Carruth 
1023bab7e1aSChandler Carruth     // Check to see whether the preserved set needs to be adjusted based on
1033bab7e1aSChandler Carruth     // function-level analysis invalidation triggering deferred invalidation
1043bab7e1aSChandler Carruth     // for this loop.
1053bab7e1aSChandler Carruth     if (auto *OuterProxy =
1063bab7e1aSChandler Carruth             InnerAM->getCachedResult<FunctionAnalysisManagerLoopProxy>(*L))
1073bab7e1aSChandler Carruth       for (const auto &OuterInvalidationPair :
1083bab7e1aSChandler Carruth            OuterProxy->getOuterInvalidations()) {
1093bab7e1aSChandler Carruth         AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first;
1103bab7e1aSChandler Carruth         const auto &InnerAnalysisIDs = OuterInvalidationPair.second;
1113bab7e1aSChandler Carruth         if (Inv.invalidate(OuterAnalysisID, F, PA)) {
1123bab7e1aSChandler Carruth           if (!InnerPA)
1133bab7e1aSChandler Carruth             InnerPA = PA;
1143bab7e1aSChandler Carruth           for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs)
1153bab7e1aSChandler Carruth             InnerPA->abandon(InnerAnalysisID);
1163bab7e1aSChandler Carruth         }
1173bab7e1aSChandler Carruth       }
1183bab7e1aSChandler Carruth 
1193bab7e1aSChandler Carruth     // Check if we needed a custom PA set. If so we'll need to run the inner
1203bab7e1aSChandler Carruth     // invalidation.
1213bab7e1aSChandler Carruth     if (InnerPA) {
1223bab7e1aSChandler Carruth       InnerAM->invalidate(*L, *InnerPA);
1233bab7e1aSChandler Carruth       continue;
1243bab7e1aSChandler Carruth     }
1253bab7e1aSChandler Carruth 
1263bab7e1aSChandler Carruth     // Otherwise we only need to do invalidation if the original PA set didn't
1273bab7e1aSChandler Carruth     // preserve all Loop analyses.
1283bab7e1aSChandler Carruth     if (!AreLoopAnalysesPreserved)
1293bab7e1aSChandler Carruth       InnerAM->invalidate(*L, PA);
1303bab7e1aSChandler Carruth   }
1313bab7e1aSChandler Carruth 
1323bab7e1aSChandler Carruth   // Return false to indicate that this result is still a valid proxy.
1333bab7e1aSChandler Carruth   return false;
1343bab7e1aSChandler Carruth }
1353bab7e1aSChandler Carruth 
1363bab7e1aSChandler Carruth template <>
1373bab7e1aSChandler Carruth LoopAnalysisManagerFunctionProxy::Result
1383bab7e1aSChandler Carruth LoopAnalysisManagerFunctionProxy::run(Function &F,
1393bab7e1aSChandler Carruth                                       FunctionAnalysisManager &AM) {
1403bab7e1aSChandler Carruth   return Result(*InnerAM, AM.getResult<LoopAnalysis>(F));
1413bab7e1aSChandler Carruth }
1423bab7e1aSChandler Carruth }
1433bab7e1aSChandler Carruth 
1443bab7e1aSChandler Carruth PreservedAnalyses llvm::getLoopPassPreservedAnalyses() {
1453bab7e1aSChandler Carruth   PreservedAnalyses PA;
1463bab7e1aSChandler Carruth   PA.preserve<DominatorTreeAnalysis>();
1473bab7e1aSChandler Carruth   PA.preserve<LoopAnalysis>();
1483bab7e1aSChandler Carruth   PA.preserve<LoopAnalysisManagerFunctionProxy>();
1493bab7e1aSChandler Carruth   PA.preserve<ScalarEvolutionAnalysis>();
150*ff8b8aeaSAlina Sbirlea   // FIXME: Uncomment this when all loop passes preserve MemorySSA
151*ff8b8aeaSAlina Sbirlea   // PA.preserve<MemorySSAAnalysis>();
152*ff8b8aeaSAlina Sbirlea   // FIXME: What we really want to do here is preserve an AA category, but that
1533bab7e1aSChandler Carruth   // concept doesn't exist yet.
1543bab7e1aSChandler Carruth   PA.preserve<AAManager>();
1553bab7e1aSChandler Carruth   PA.preserve<BasicAA>();
1563bab7e1aSChandler Carruth   PA.preserve<GlobalsAA>();
1573bab7e1aSChandler Carruth   PA.preserve<SCEVAA>();
1583bab7e1aSChandler Carruth   return PA;
1593bab7e1aSChandler Carruth }
160