• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- LoopAnalysisManager.h - Loop analysis management ---------*- C++ -*-===//
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 /// \file
10 ///
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
13 /// a function.
14 ///
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:
19 ///    - LoopInfo
20 ///    - DominatorTree
21 ///    - ScalarEvolution
22 ///    - AAManager
23 ///
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.
27 ///
28 //===----------------------------------------------------------------------===//
29 
30 #ifndef LLVM_ANALYSIS_LOOPANALYSISMANAGER_H
31 #define LLVM_ANALYSIS_LOOPANALYSISMANAGER_H
32 
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"
47 
48 namespace llvm {
49 
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 {
55   AAResults &AA;
56   AssumptionCache ∾
57   DominatorTree &DT;
58   LoopInfo &LI;
59   ScalarEvolution &SE;
60   TargetLibraryInfo &TLI;
61   TargetTransformInfo &TTI;
62   MemorySSA *MSSA;
63 };
64 
65 /// Enables memory ssa as a dependency for loop passes.
66 extern cl::opt<bool> EnableMSSALoopDependency;
67 
68 /// Extern template declaration for the analysis set for this IR unit.
69 extern template class AllAnalysesOn<Loop>;
70 
71 extern template class AnalysisManager<Loop, LoopStandardAnalysisResults &>;
72 /// The loop analysis manager.
73 ///
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 &>
79     LoopAnalysisManager;
80 
81 /// A proxy from a \c LoopAnalysisManager to a \c Function.
82 typedef InnerAnalysisManagerProxy<LoopAnalysisManager, Function>
83     LoopAnalysisManagerFunctionProxy;
84 
85 /// A specialized result for the \c LoopAnalysisManagerFunctionProxy which
86 /// retains a \c LoopInfo reference.
87 ///
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 {
91 public:
Result(LoopAnalysisManager & InnerAM,LoopInfo & LI)92   explicit Result(LoopAnalysisManager &InnerAM, LoopInfo &LI)
93       : InnerAM(&InnerAM), LI(&LI) {}
Result(Result && Arg)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
97     // analysis state.
98     Arg.InnerAM = nullptr;
99   }
100   Result &operator=(Result &&RHS) {
101     InnerAM = RHS.InnerAM;
102     LI = RHS.LI;
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
105     // analysis state.
106     RHS.InnerAM = nullptr;
107     return *this;
108   }
~Result()109   ~Result() {
110     // InnerAM is cleared in a moved from state where there is nothing to do.
111     if (!InnerAM)
112       return;
113 
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.
116     InnerAM->clear();
117   }
118 
119   /// Accessor for the analysis manager.
getManager()120   LoopAnalysisManager &getManager() { return *InnerAM; }
121 
122   /// Handler for invalidation of the proxy for a particular function.
123   ///
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.
127   ///
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);
133 
134 private:
135   LoopAnalysisManager *InnerAM;
136   LoopInfo *LI;
137 };
138 
139 /// Provide a specialized run method for the \c LoopAnalysisManagerFunctionProxy
140 /// so it can pass the \c LoopInfo to the result.
141 template <>
142 LoopAnalysisManagerFunctionProxy::Result
143 LoopAnalysisManagerFunctionProxy::run(Function &F, FunctionAnalysisManager &AM);
144 
145 // Ensure the \c LoopAnalysisManagerFunctionProxy is provided as an extern
146 // template.
147 extern template class InnerAnalysisManagerProxy<LoopAnalysisManager, Function>;
148 
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;
155 
156 /// Returns the minimum set of Analyses that all loop passes must preserve.
157 PreservedAnalyses getLoopPassPreservedAnalyses();
158 }
159 
160 #endif // LLVM_ANALYSIS_LOOPANALYSISMANAGER_H
161