xref: /llvm-project/llvm/lib/Analysis/LoopAnalysisManager.cpp (revision 28dc4d51221b5ecd3cabd1881c34b3f189dfdf04)
1 //===- LoopAnalysisManager.cpp - Loop analysis management -----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/Analysis/LoopAnalysisManager.h"
11 #include "llvm/Analysis/BasicAliasAnalysis.h"
12 #include "llvm/Analysis/GlobalsModRef.h"
13 #include "llvm/Analysis/LoopInfo.h"
14 #include "llvm/Analysis/ScalarEvolution.h"
15 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
16 #include "llvm/IR/Dominators.h"
17 
18 using namespace llvm;
19 
20 // Explicit template instantiations and specialization defininitions for core
21 // template typedefs.
22 namespace llvm {
23 template class AllAnalysesOn<Loop>;
24 template class AnalysisManager<Loop, LoopStandardAnalysisResults &>;
25 template class InnerAnalysisManagerProxy<LoopAnalysisManager, Function>;
26 template class OuterAnalysisManagerProxy<FunctionAnalysisManager, Loop>;
27 
28 bool LoopAnalysisManagerFunctionProxy::Result::invalidate(
29     Function &F, const PreservedAnalyses &PA,
30     FunctionAnalysisManager::Invalidator &Inv) {
31   // First compute the sequence of IR units covered by this proxy. We will want
32   // to visit this in postorder, but because this is a tree structure we can do
33   // this by building a preorder sequence and walking it backwards. We also
34   // want siblings in forward program order to match the LoopPassManager so we
35   // get the preorder with siblings reversed.
36   SmallVector<Loop *, 4> PreOrderLoops = LI->getLoopsInReverseSiblingPreorder();
37 
38   // If this proxy or the loop info is going to be invalidated, we also need
39   // to clear all the keys coming from that analysis. We also completely blow
40   // away the loop analyses if any of the standard analyses provided by the
41   // loop pass manager go away so that loop analyses can freely use these
42   // without worrying about declaring dependencies on them etc.
43   // FIXME: It isn't clear if this is the right tradeoff. We could instead make
44   // loop analyses declare any dependencies on these and use the more general
45   // invalidation logic below to act on that.
46   auto PAC = PA.getChecker<LoopAnalysisManagerFunctionProxy>();
47   if (!(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) ||
48       Inv.invalidate<AAManager>(F, PA) ||
49       Inv.invalidate<AssumptionAnalysis>(F, PA) ||
50       Inv.invalidate<DominatorTreeAnalysis>(F, PA) ||
51       Inv.invalidate<LoopAnalysis>(F, PA) ||
52       Inv.invalidate<ScalarEvolutionAnalysis>(F, PA)) {
53     // Note that the LoopInfo may be stale at this point, however the loop
54     // objects themselves remain the only viable keys that could be in the
55     // analysis manager's cache. So we just walk the keys and forcibly clear
56     // those results. Note that the order doesn't matter here as this will just
57     // directly destroy the results without calling methods on them.
58     for (Loop *L : PreOrderLoops)
59       InnerAM->clear(*L);
60 
61     // We also need to null out the inner AM so that when the object gets
62     // destroyed as invalid we don't try to clear the inner AM again. At that
63     // point we won't be able to reliably walk the loops for this function and
64     // only clear results associated with those loops the way we do here.
65     // FIXME: Making InnerAM null at this point isn't very nice. Most analyses
66     // try to remain valid during invalidation. Maybe we should add an
67     // `IsClean` flag?
68     InnerAM = nullptr;
69 
70     // Now return true to indicate this *is* invalid and a fresh proxy result
71     // needs to be built. This is especially important given the null InnerAM.
72     return true;
73   }
74 
75   // Directly check if the relevant set is preserved so we can short circuit
76   // invalidating loops.
77   bool AreLoopAnalysesPreserved =
78       PA.allAnalysesInSetPreserved<AllAnalysesOn<Loop>>();
79 
80   // Since we have a valid LoopInfo we can actually leave the cached results in
81   // the analysis manager associated with the Loop keys, but we need to
82   // propagate any necessary invalidation logic into them. We'd like to
83   // invalidate things in roughly the same order as they were put into the
84   // cache and so we walk the preorder list in reverse to form a valid
85   // postorder.
86   for (Loop *L : reverse(PreOrderLoops)) {
87     Optional<PreservedAnalyses> InnerPA;
88 
89     // Check to see whether the preserved set needs to be adjusted based on
90     // function-level analysis invalidation triggering deferred invalidation
91     // for this loop.
92     if (auto *OuterProxy =
93             InnerAM->getCachedResult<FunctionAnalysisManagerLoopProxy>(*L))
94       for (const auto &OuterInvalidationPair :
95            OuterProxy->getOuterInvalidations()) {
96         AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first;
97         const auto &InnerAnalysisIDs = OuterInvalidationPair.second;
98         if (Inv.invalidate(OuterAnalysisID, F, PA)) {
99           if (!InnerPA)
100             InnerPA = PA;
101           for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs)
102             InnerPA->abandon(InnerAnalysisID);
103         }
104       }
105 
106     // Check if we needed a custom PA set. If so we'll need to run the inner
107     // invalidation.
108     if (InnerPA) {
109       InnerAM->invalidate(*L, *InnerPA);
110       continue;
111     }
112 
113     // Otherwise we only need to do invalidation if the original PA set didn't
114     // preserve all Loop analyses.
115     if (!AreLoopAnalysesPreserved)
116       InnerAM->invalidate(*L, PA);
117   }
118 
119   // Return false to indicate that this result is still a valid proxy.
120   return false;
121 }
122 
123 template <>
124 LoopAnalysisManagerFunctionProxy::Result
125 LoopAnalysisManagerFunctionProxy::run(Function &F,
126                                       FunctionAnalysisManager &AM) {
127   return Result(*InnerAM, AM.getResult<LoopAnalysis>(F));
128 }
129 }
130 
131 PreservedAnalyses llvm::getLoopPassPreservedAnalyses() {
132   PreservedAnalyses PA;
133   PA.preserve<DominatorTreeAnalysis>();
134   PA.preserve<LoopAnalysis>();
135   PA.preserve<LoopAnalysisManagerFunctionProxy>();
136   PA.preserve<ScalarEvolutionAnalysis>();
137   // TODO: What we really want to do here is preserve an AA category, but that
138   // concept doesn't exist yet.
139   PA.preserve<AAManager>();
140   PA.preserve<BasicAA>();
141   PA.preserve<GlobalsAA>();
142   PA.preserve<SCEVAA>();
143   return PA;
144 }
145