LLVM  8.0.1
LoopAnalysisManager.cpp
Go to the documentation of this file.
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 
13 #include "llvm/Analysis/LoopInfo.h"
17 #include "llvm/IR/Dominators.h"
18 
19 using namespace llvm;
20 
21 namespace llvm {
22 /// Enables memory ssa as a dependency for loop passes in legacy pass manager.
24  "enable-mssa-loop-dependency", cl::Hidden, cl::init(false),
25  cl::desc("Enable MemorySSA dependency for loop pass manager"));
26 
27 // Explicit template instantiations and specialization definitions for core
28 // template typedefs.
29 template class AllAnalysesOn<Loop>;
34 
36  Function &F, const PreservedAnalyses &PA,
38  // First compute the sequence of IR units covered by this proxy. We will want
39  // to visit this in postorder, but because this is a tree structure we can do
40  // this by building a preorder sequence and walking it backwards. We also
41  // want siblings in forward program order to match the LoopPassManager so we
42  // get the preorder with siblings reversed.
43  SmallVector<Loop *, 4> PreOrderLoops = LI->getLoopsInReverseSiblingPreorder();
44 
45  // If this proxy or the loop info is going to be invalidated, we also need
46  // to clear all the keys coming from that analysis. We also completely blow
47  // away the loop analyses if any of the standard analyses provided by the
48  // loop pass manager go away so that loop analyses can freely use these
49  // without worrying about declaring dependencies on them etc.
50  // FIXME: It isn't clear if this is the right tradeoff. We could instead make
51  // loop analyses declare any dependencies on these and use the more general
52  // invalidation logic below to act on that.
54  bool invalidateMemorySSAAnalysis = false;
56  invalidateMemorySSAAnalysis = Inv.invalidate<MemorySSAAnalysis>(F, PA);
57  if (!(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) ||
58  Inv.invalidate<AAManager>(F, PA) ||
59  Inv.invalidate<AssumptionAnalysis>(F, PA) ||
61  Inv.invalidate<LoopAnalysis>(F, PA) ||
63  invalidateMemorySSAAnalysis) {
64  // Note that the LoopInfo may be stale at this point, however the loop
65  // objects themselves remain the only viable keys that could be in the
66  // analysis manager's cache. So we just walk the keys and forcibly clear
67  // those results. Note that the order doesn't matter here as this will just
68  // directly destroy the results without calling methods on them.
69  for (Loop *L : PreOrderLoops) {
70  // NB! `L` may not be in a good enough state to run Loop::getName.
71  InnerAM->clear(*L, "<possibly invalidated loop>");
72  }
73 
74  // We also need to null out the inner AM so that when the object gets
75  // destroyed as invalid we don't try to clear the inner AM again. At that
76  // point we won't be able to reliably walk the loops for this function and
77  // only clear results associated with those loops the way we do here.
78  // FIXME: Making InnerAM null at this point isn't very nice. Most analyses
79  // try to remain valid during invalidation. Maybe we should add an
80  // `IsClean` flag?
81  InnerAM = nullptr;
82 
83  // Now return true to indicate this *is* invalid and a fresh proxy result
84  // needs to be built. This is especially important given the null InnerAM.
85  return true;
86  }
87 
88  // Directly check if the relevant set is preserved so we can short circuit
89  // invalidating loops.
90  bool AreLoopAnalysesPreserved =
92 
93  // Since we have a valid LoopInfo we can actually leave the cached results in
94  // the analysis manager associated with the Loop keys, but we need to
95  // propagate any necessary invalidation logic into them. We'd like to
96  // invalidate things in roughly the same order as they were put into the
97  // cache and so we walk the preorder list in reverse to form a valid
98  // postorder.
99  for (Loop *L : reverse(PreOrderLoops)) {
101 
102  // Check to see whether the preserved set needs to be adjusted based on
103  // function-level analysis invalidation triggering deferred invalidation
104  // for this loop.
105  if (auto *OuterProxy =
106  InnerAM->getCachedResult<FunctionAnalysisManagerLoopProxy>(*L))
107  for (const auto &OuterInvalidationPair :
108  OuterProxy->getOuterInvalidations()) {
109  AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first;
110  const auto &InnerAnalysisIDs = OuterInvalidationPair.second;
111  if (Inv.invalidate(OuterAnalysisID, F, PA)) {
112  if (!InnerPA)
113  InnerPA = PA;
114  for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs)
115  InnerPA->abandon(InnerAnalysisID);
116  }
117  }
118 
119  // Check if we needed a custom PA set. If so we'll need to run the inner
120  // invalidation.
121  if (InnerPA) {
122  InnerAM->invalidate(*L, *InnerPA);
123  continue;
124  }
125 
126  // Otherwise we only need to do invalidation if the original PA set didn't
127  // preserve all Loop analyses.
128  if (!AreLoopAnalysesPreserved)
129  InnerAM->invalidate(*L, PA);
130  }
131 
132  // Return false to indicate that this result is still a valid proxy.
133  return false;
134 }
135 
136 template <>
139  FunctionAnalysisManager &AM) {
140  return Result(*InnerAM, AM.getResult<LoopAnalysis>(F));
141 }
142 }
143 
147  PA.preserve<LoopAnalysis>();
152  // FIXME: What we really want to do here is preserve an AA category, but that
153  // concept doesn't exist yet.
154  PA.preserve<AAManager>();
155  PA.preserve<BasicAA>();
156  PA.preserve<GlobalsAA>();
157  PA.preserve<SCEVAA>();
158  return PA;
159 }
void abandon()
Mark an analysis as abandoned.
Definition: PassManager.h:208
bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA)
Trigger the invalidation of some other analysis pass if not already handled and return whether it was...
Definition: PassManager.h:660
PreservedAnalyses getLoopPassPreservedAnalyses()
Returns the minimum set of Analyses that all loop passes must preserve.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:770
This class represents lattice values for constants.
Definition: AllocatorList.h:24
This is the interface for a simple mod/ref and alias analysis over globals.
bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA, typename AnalysisManager< IRUnitT, ExtraArgTs... >::Invalidator &Inv)
Handler for invalidation of the outer IR unit, IRUnitT.
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:231
F(f)
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
Definition: PassManager.h:305
This is the interface for a SCEV-based alias analysis.
Analysis pass that exposes the LoopInfo for a function.
Definition: LoopInfo.h:945
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
Definition: STLExtras.h:267
This header provides classes for managing per-loop analyses.
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:423
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:154
A manager for alias analyses.
Analysis pass providing a never-invalidated alias analysis result.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
Definition: PassManager.h:1019
Analysis pass providing a never-invalidated alias analysis result.
A function analysis which provides an AssumptionCache.
An analysis over an "inner" IR unit that provides access to an analysis manager over a "outer" IR uni...
Definition: PassManager.h:1154
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
Result run(IRUnitT &IR, AnalysisManager< IRUnitT, ExtraArgTs... > &AM, ExtraArgTs...)
Run the analysis pass and create our proxy result object.
Definition: PassManager.h:1101
An analysis that produces MemorySSA for a function.
Definition: MemorySSA.h:920
Result(AnalysisManagerT &InnerAM)
Definition: PassManager.h:1044
Analysis pass that exposes the ScalarEvolution for a function.
Analysis pass providing a never-invalidated alias analysis result.
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:465
void preserve()
Mark an analysis as preserved.
Definition: PassManager.h:175
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:642
This templated class represents "all analyses that operate over <a particular IR unit>" (e...
Definition: PassManager.h:92
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
This is the interface for LLVM&#39;s primary stateless and local alias analysis.
A container for analyses that lazily runs them and caches their results.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:71
bool allAnalysesInSetPreserved() const
Directly test whether a set of analyses is preserved.
Definition: PassManager.h:330
An analysis over an "outer" IR unit that provides access to an analysis manager over an "inner" IR un...
Definition: PassManager.h:1038
cl::opt< bool > EnableMSSALoopDependency
Enables memory ssa as a dependency for loop passes.