1 //===- LoopAnalysisManager.h - Loop analysis management ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This header provides classes for managing per-loop analyses. These are
12 /// typically used as part of a loop pass pipeline over the loop nests of
15 /// Loop analyses are allowed to make some simplifying assumptions:
16 /// 1) Loops are, where possible, in simplified form.
17 /// 2) Loops are *always* in LCSSA form.
18 /// 3) A collection of analysis results are available:
24 /// The primary mechanism to provide these invariants is the loop pass manager,
25 /// but they can also be manually provided in order to reason about a loop from
26 /// outside of a dedicated pass manager.
28 //===----------------------------------------------------------------------===//
30 #ifndef LLVM_ANALYSIS_LOOPANALYSISMANAGER_H
31 #define LLVM_ANALYSIS_LOOPANALYSISMANAGER_H
33 #include "llvm/ADT/PostOrderIterator.h"
34 #include "llvm/ADT/PriorityWorklist.h"
35 #include "llvm/ADT/STLExtras.h"
36 #include "llvm/Analysis/AliasAnalysis.h"
37 #include "llvm/Analysis/BasicAliasAnalysis.h"
38 #include "llvm/Analysis/GlobalsModRef.h"
39 #include "llvm/Analysis/LoopInfo.h"
40 #include "llvm/Analysis/MemorySSA.h"
41 #include "llvm/Analysis/ScalarEvolution.h"
42 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
43 #include "llvm/Analysis/TargetLibraryInfo.h"
44 #include "llvm/Analysis/TargetTransformInfo.h"
45 #include "llvm/IR/Dominators.h"
46 #include "llvm/IR/PassManager.h"
50 /// The adaptor from a function pass to a loop pass computes these analyses and
51 /// makes them available to the loop passes "for free". Each loop pass is
52 /// expected expected to update these analyses if necessary to ensure they're
53 /// valid after it runs.
54 struct LoopStandardAnalysisResults {
60 TargetLibraryInfo &TLI;
61 TargetTransformInfo &TTI;
65 /// Enables memory ssa as a dependency for loop passes.
66 extern cl::opt<bool> EnableMSSALoopDependency;
68 /// Extern template declaration for the analysis set for this IR unit.
69 extern template class AllAnalysesOn<Loop>;
71 extern template class AnalysisManager<Loop, LoopStandardAnalysisResults &>;
72 /// The loop analysis manager.
74 /// See the documentation for the AnalysisManager template for detail
75 /// documentation. This typedef serves as a convenient way to refer to this
76 /// construct in the adaptors and proxies used to integrate this into the larger
77 /// pass manager infrastructure.
78 typedef AnalysisManager<Loop, LoopStandardAnalysisResults &>
81 /// A proxy from a \c LoopAnalysisManager to a \c Function.
82 typedef InnerAnalysisManagerProxy<LoopAnalysisManager, Function>
83 LoopAnalysisManagerFunctionProxy;
85 /// A specialized result for the \c LoopAnalysisManagerFunctionProxy which
86 /// retains a \c LoopInfo reference.
88 /// This allows it to collect loop objects for which analysis results may be
89 /// cached in the \c LoopAnalysisManager.
90 template <> class LoopAnalysisManagerFunctionProxy::Result {
92 explicit Result(LoopAnalysisManager &InnerAM, LoopInfo &LI)
93 : InnerAM(&InnerAM), LI(&LI) {}
94 Result(Result &&Arg) : InnerAM(std::move(Arg.InnerAM)), LI(Arg.LI) {
95 // We have to null out the analysis manager in the moved-from state
96 // because we are taking ownership of the responsibilty to clear the
98 Arg.InnerAM = nullptr;
100 Result &operator=(Result &&RHS) {
101 InnerAM = RHS.InnerAM;
103 // We have to null out the analysis manager in the moved-from state
104 // because we are taking ownership of the responsibilty to clear the
106 RHS.InnerAM = nullptr;
110 // InnerAM is cleared in a moved from state where there is nothing to do.
114 // Clear out the analysis manager if we're being destroyed -- it means we
115 // didn't even see an invalidate call when we got invalidated.
119 /// Accessor for the analysis manager.
120 LoopAnalysisManager &getManager() { return *InnerAM; }
122 /// Handler for invalidation of the proxy for a particular function.
124 /// If the proxy, \c LoopInfo, and associated analyses are preserved, this
125 /// will merely forward the invalidation event to any cached loop analysis
126 /// results for loops within this function.
128 /// If the necessary loop infrastructure is not preserved, this will forcibly
129 /// clear all of the cached analysis results that are keyed on the \c
130 /// LoopInfo for this function.
131 bool invalidate(Function &F, const PreservedAnalyses &PA,
132 FunctionAnalysisManager::Invalidator &Inv);
135 LoopAnalysisManager *InnerAM;
139 /// Provide a specialized run method for the \c LoopAnalysisManagerFunctionProxy
140 /// so it can pass the \c LoopInfo to the result.
142 LoopAnalysisManagerFunctionProxy::Result
143 LoopAnalysisManagerFunctionProxy::run(Function &F, FunctionAnalysisManager &AM);
145 // Ensure the \c LoopAnalysisManagerFunctionProxy is provided as an extern
147 extern template class InnerAnalysisManagerProxy<LoopAnalysisManager, Function>;
149 extern template class OuterAnalysisManagerProxy<FunctionAnalysisManager, Loop,
150 LoopStandardAnalysisResults &>;
151 /// A proxy from a \c FunctionAnalysisManager to a \c Loop.
152 typedef OuterAnalysisManagerProxy<FunctionAnalysisManager, Loop,
153 LoopStandardAnalysisResults &>
154 FunctionAnalysisManagerLoopProxy;
156 /// Returns the minimum set of Analyses that all loop passes must preserve.
157 PreservedAnalyses getLoopPassPreservedAnalyses();
160 #endif // LLVM_ANALYSIS_LOOPANALYSISMANAGER_H