llvm.org GIT mirror llvm / c68d25f
[PM] Separate the LoopAnalysisManager from the LoopPassManager and move the latter to the Transforms library. While the loop PM uses an analysis to form the IR units, the current plan is to have the PM itself establish and enforce both loop simplified form and LCSSA. This would be a layering violation in the analysis library. Fundamentally, the idea behind the loop PM is to *transform* loops in addition to running passes over them, so it really seemed like the most natural place to sink this was into the transforms library. We can't just move *everything* because we also have loop analyses that rely on a subset of the invariants. So this patch splits the the loop infrastructure into the analysis management that has to be part of the analysis library, and the transform-aware pass manager. This also required splitting the loop analyses' printer passes out to the transforms library, which makes sense to me as running these will transform the code into LCSSA in theory. I haven't split the unittest though because testing one component without the other seems nearly intractable. Differential Revision: https://reviews.llvm.org/D28452 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@291662 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 2 years ago
47 changed file(s) with 2370 addition(s) and 2218 deletion(s). Raw diff Collapse all Expand all
1414 #ifndef LLVM_ANALYSIS_IVUSERS_H
1515 #define LLVM_ANALYSIS_IVUSERS_H
1616
17 #include "llvm/Analysis/LoopAnalysisManager.h"
1718 #include "llvm/Analysis/LoopPass.h"
18 #include "llvm/Analysis/LoopPassManager.h"
1919 #include "llvm/Analysis/ScalarEvolutionNormalization.h"
2020 #include "llvm/IR/ValueHandle.h"
2121
196196 LoopStandardAnalysisResults &AR);
197197 };
198198
199 /// Printer pass for the \c IVUsers for a loop.
200 class IVUsersPrinterPass : public PassInfoMixin {
201 raw_ostream &OS;
202
203 public:
204 explicit IVUsersPrinterPass(raw_ostream &OS) : OS(OS) {}
205 PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
206 LoopStandardAnalysisResults &AR, LPMUpdater &U);
207 };
208199 }
209200
210201 #endif
1919 #include "llvm/ADT/SetVector.h"
2020 #include "llvm/Analysis/AliasAnalysis.h"
2121 #include "llvm/Analysis/AliasSetTracker.h"
22 #include "llvm/Analysis/LoopPassManager.h"
22 #include "llvm/Analysis/LoopAnalysisManager.h"
2323 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
2424 #include "llvm/IR/DiagnosticInfo.h"
2525 #include "llvm/IR/ValueHandle.h"
756756 Result run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR);
757757 };
758758
759 /// \brief Printer pass for the \c LoopAccessInfo results.
760 class LoopAccessInfoPrinterPass
761 : public PassInfoMixin {
762 raw_ostream &OS;
763
764 public:
765 explicit LoopAccessInfoPrinterPass(raw_ostream &OS) : OS(OS) {}
766 PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
767 LoopStandardAnalysisResults &AR, LPMUpdater &U);
768 };
769
770759 inline Instruction *MemoryDepChecker::Dependence::getSource(
771760 const LoopAccessInfo &LAI) const {
772761 return LAI.getDepChecker().getMemoryInstructions()[Source];
0 //===- LoopAnalysisManager.h - Loop analysis management ---------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 /// \file
9 ///
10 /// This header provides classes for managing per-loop analyses. These are
11 /// typically used as part of a loop pass pipeline over the loop nests of
12 /// a function.
13 ///
14 /// Loop analyses are allowed to make some simplifying assumptions:
15 /// 1) Loops are, where possible, in simplified form.
16 /// 2) Loops are *always* in LCSSA form.
17 /// 3) A collection of analysis results are available:
18 /// - LoopInfo
19 /// - DominatorTree
20 /// - ScalarEvolution
21 /// - AAManager
22 ///
23 /// The primary mechanism to provide these invariants is the loop pass manager,
24 /// but they can also be manually provided in order to reason about a loop from
25 /// outside of a dedicated pass manager.
26 ///
27 //===----------------------------------------------------------------------===//
28
29 #ifndef LLVM_ANALYSIS_LOOPANALYSISMANAGER_H
30 #define LLVM_ANALYSIS_LOOPANALYSISMANAGER_H
31
32 #include "llvm/ADT/PostOrderIterator.h"
33 #include "llvm/ADT/PriorityWorklist.h"
34 #include "llvm/ADT/STLExtras.h"
35 #include "llvm/Analysis/AliasAnalysis.h"
36 #include "llvm/Analysis/BasicAliasAnalysis.h"
37 #include "llvm/Analysis/GlobalsModRef.h"
38 #include "llvm/Analysis/LoopInfo.h"
39 #include "llvm/Analysis/ScalarEvolution.h"
40 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
41 #include "llvm/Analysis/TargetLibraryInfo.h"
42 #include "llvm/Analysis/TargetTransformInfo.h"
43 #include "llvm/IR/Dominators.h"
44 #include "llvm/IR/PassManager.h"
45
46 namespace llvm {
47
48 /// The adaptor from a function pass to a loop pass computes these analyses and
49 /// makes them available to the loop passes "for free". Each loop pass is
50 /// expected expected to update these analyses if necessary to ensure they're
51 /// valid after it runs.
52 struct LoopStandardAnalysisResults {
53 AAResults &AA;
54 AssumptionCache ∾
55 DominatorTree &DT;
56 LoopInfo &LI;
57 ScalarEvolution &SE;
58 TargetLibraryInfo &TLI;
59 TargetTransformInfo &TTI;
60 };
61
62 /// Extern template declaration for the analysis set for this IR unit.
63 extern template class AllAnalysesOn;
64
65 extern template class AnalysisManager;
66 /// \brief The loop analysis manager.
67 ///
68 /// See the documentation for the AnalysisManager template for detail
69 /// documentation. This typedef serves as a convenient way to refer to this
70 /// construct in the adaptors and proxies used to integrate this into the larger
71 /// pass manager infrastructure.
72 typedef AnalysisManager
73 LoopAnalysisManager;
74
75 /// A proxy from a \c LoopAnalysisManager to a \c Function.
76 typedef InnerAnalysisManagerProxy
77 LoopAnalysisManagerFunctionProxy;
78
79 /// A specialized result for the \c LoopAnalysisManagerFunctionProxy which
80 /// retains a \c LoopInfo reference.
81 ///
82 /// This allows it to collect loop objects for which analysis results may be
83 /// cached in the \c LoopAnalysisManager.
84 template <> class LoopAnalysisManagerFunctionProxy::Result {
85 public:
86 explicit Result(LoopAnalysisManager &InnerAM, LoopInfo &LI)
87 : InnerAM(&InnerAM), LI(&LI) {}
88 Result(Result &&Arg) : InnerAM(std::move(Arg.InnerAM)), LI(Arg.LI) {
89 // We have to null out the analysis manager in the moved-from state
90 // because we are taking ownership of the responsibilty to clear the
91 // analysis state.
92 Arg.InnerAM = nullptr;
93 }
94 Result &operator=(Result &&RHS) {
95 InnerAM = RHS.InnerAM;
96 LI = RHS.LI;
97 // We have to null out the analysis manager in the moved-from state
98 // because we are taking ownership of the responsibilty to clear the
99 // analysis state.
100 RHS.InnerAM = nullptr;
101 return *this;
102 }
103 ~Result() {
104 // InnerAM is cleared in a moved from state where there is nothing to do.
105 if (!InnerAM)
106 return;
107
108 // Clear out the analysis manager if we're being destroyed -- it means we
109 // didn't even see an invalidate call when we got invalidated.
110 InnerAM->clear();
111 }
112
113 /// Accessor for the analysis manager.
114 LoopAnalysisManager &getManager() { return *InnerAM; }
115
116 /// Handler for invalidation of the proxy for a particular function.
117 ///
118 /// If the proxy, \c LoopInfo, and associated analyses are preserved, this
119 /// will merely forward the invalidation event to any cached loop analysis
120 /// results for loops within this function.
121 ///
122 /// If the necessary loop infrastructure is not preserved, this will forcibly
123 /// clear all of the cached analysis results that are keyed on the \c
124 /// LoopInfo for this function.
125 bool invalidate(Function &F, const PreservedAnalyses &PA,
126 FunctionAnalysisManager::Invalidator &Inv);
127
128 private:
129 LoopAnalysisManager *InnerAM;
130 LoopInfo *LI;
131 };
132
133 /// Provide a specialized run method for the \c LoopAnalysisManagerFunctionProxy
134 /// so it can pass the \c LoopInfo to the result.
135 template <>
136 LoopAnalysisManagerFunctionProxy::Result
137 LoopAnalysisManagerFunctionProxy::run(Function &F, FunctionAnalysisManager &AM);
138
139 // Ensure the \c LoopAnalysisManagerFunctionProxy is provided as an extern
140 // template.
141 extern template class InnerAnalysisManagerProxy;
142
143 extern template class OuterAnalysisManagerProxy
144 LoopStandardAnalysisResults &>;
145 /// A proxy from a \c FunctionAnalysisManager to a \c Loop.
146 typedef OuterAnalysisManagerProxy
147 LoopStandardAnalysisResults &>
148 FunctionAnalysisManagerLoopProxy;
149
150 /// Returns the minimum set of Analyses that all loop passes must preserve.
151 PreservedAnalyses getLoopPassPreservedAnalyses();
152 }
153
154 #endif // LLVM_ANALYSIS_LOOPANALYSISMANAGER_H
+0
-471
include/llvm/Analysis/LoopPassManager.h less more
None //===- LoopPassManager.h - Loop pass management -----------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 /// \file
9 ///
10 /// This header provides classes for managing a pipeline of passes over loops
11 /// in LLVM IR.
12 ///
13 /// The primary loop pass pipeline is managed in a very particular way to
14 /// provide a set of core guarantees:
15 /// 1) Loops are, where possible, in simplified form.
16 /// 2) Loops are *always* in LCSSA form.
17 /// 3) A collection of Loop-specific analysis results are available:
18 /// - LoopInfo
19 /// - DominatorTree
20 /// - ScalarEvolution
21 /// - AAManager
22 /// 4) All loop passes preserve #1 (where possible), #2, and #3.
23 /// 5) Loop passes run over each loop in the loop nest from the innermost to
24 /// the outermost. Specifically, all inner loops are processed before
25 /// passes run over outer loops. When running the pipeline across an inner
26 /// loop creates new inner loops, those are added and processed in this
27 /// order as well.
28 ///
29 /// This process is designed to facilitate transformations which simplify,
30 /// reduce, and remove loops. For passes which are more oriented towards
31 /// optimizing loops, especially optimizing loop *nests* instead of single
32 /// loops in isolation, this framework is less interesting.
33 ///
34 //===----------------------------------------------------------------------===//
35
36 #ifndef LLVM_ANALYSIS_LOOPPASSMANAGER_H
37 #define LLVM_ANALYSIS_LOOPPASSMANAGER_H
38
39 #include "llvm/ADT/PostOrderIterator.h"
40 #include "llvm/ADT/PriorityWorklist.h"
41 #include "llvm/ADT/STLExtras.h"
42 #include "llvm/Analysis/AliasAnalysis.h"
43 #include "llvm/Analysis/BasicAliasAnalysis.h"
44 #include "llvm/Analysis/GlobalsModRef.h"
45 #include "llvm/Analysis/LoopInfo.h"
46 #include "llvm/Analysis/ScalarEvolution.h"
47 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
48 #include "llvm/Analysis/TargetLibraryInfo.h"
49 #include "llvm/Analysis/TargetTransformInfo.h"
50 #include "llvm/IR/Dominators.h"
51 #include "llvm/IR/PassManager.h"
52
53 namespace llvm {
54
55 // Forward declarations of a update tracking and analysis result tracking
56 // structures used in the API of loop passes that work within this
57 // infrastructure.
58 class LPMUpdater;
59 struct LoopStandardAnalysisResults;
60
61 /// Extern template declaration for the analysis set for this IR unit.
62 extern template class AllAnalysesOn;
63
64 extern template class AnalysisManager;
65 /// \brief The loop analysis manager.
66 ///
67 /// See the documentation for the AnalysisManager template for detail
68 /// documentation. This typedef serves as a convenient way to refer to this
69 /// construct in the adaptors and proxies used to integrate this into the larger
70 /// pass manager infrastructure.
71 typedef AnalysisManager
72 LoopAnalysisManager;
73
74 // Explicit specialization and instantiation declarations for the pass manager.
75 // See the comments on the definition of the specialization for details on how
76 // it differs from the primary template.
77 template <>
78 PreservedAnalyses
79 PassManager
80 LPMUpdater &>::run(Loop &InitialL, LoopAnalysisManager &AM,
81 LoopStandardAnalysisResults &AnalysisResults,
82 LPMUpdater &U);
83 extern template class PassManager
84 LoopStandardAnalysisResults &, LPMUpdater &>;
85
86 /// \brief The Loop pass manager.
87 ///
88 /// See the documentation for the PassManager template for details. It runs
89 /// a sequence of Loop passes over each Loop that the manager is run over. This
90 /// typedef serves as a convenient way to refer to this construct.
91 typedef PassManager
92 LPMUpdater &>
93 LoopPassManager;
94
95 /// A partial specialization of the require analysis template pass to forward
96 /// the extra parameters from a transformation's run method to the
97 /// AnalysisManager's getResult.
98 template
99 struct RequireAnalysisPass
100 LoopStandardAnalysisResults &, LPMUpdater &>
101 : PassInfoMixin<
102 RequireAnalysisPass
103 LoopStandardAnalysisResults &, LPMUpdater &>> {
104 PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
105 LoopStandardAnalysisResults &AR, LPMUpdater &) {
106 (void)AM.template getResult(L, AR);
107 return PreservedAnalyses::all();
108 }
109 };
110
111 /// An alias template to easily name a require analysis loop pass.
112 template
113 using RequireAnalysisLoopPass =
114 RequireAnalysisPass
115 LoopStandardAnalysisResults &, LPMUpdater &>;
116
117 /// A proxy from a \c LoopAnalysisManager to a \c Function.
118 typedef InnerAnalysisManagerProxy
119 LoopAnalysisManagerFunctionProxy;
120
121 /// A specialized result for the \c LoopAnalysisManagerFunctionProxy which
122 /// retains a \c LoopInfo reference.
123 ///
124 /// This allows it to collect loop objects for which analysis results may be
125 /// cached in the \c LoopAnalysisManager.
126 template <> class LoopAnalysisManagerFunctionProxy::Result {
127 public:
128 explicit Result(LoopAnalysisManager &InnerAM, LoopInfo &LI)
129 : InnerAM(&InnerAM), LI(&LI) {}
130 Result(Result &&Arg) : InnerAM(std::move(Arg.InnerAM)), LI(Arg.LI) {
131 // We have to null out the analysis manager in the moved-from state
132 // because we are taking ownership of the responsibilty to clear the
133 // analysis state.
134 Arg.InnerAM = nullptr;
135 }
136 Result &operator=(Result &&RHS) {
137 InnerAM = RHS.InnerAM;
138 LI = RHS.LI;
139 // We have to null out the analysis manager in the moved-from state
140 // because we are taking ownership of the responsibilty to clear the
141 // analysis state.
142 RHS.InnerAM = nullptr;
143 return *this;
144 }
145 ~Result() {
146 // InnerAM is cleared in a moved from state where there is nothing to do.
147 if (!InnerAM)
148 return;
149
150 // Clear out the analysis manager if we're being destroyed -- it means we
151 // didn't even see an invalidate call when we got invalidated.
152 InnerAM->clear();
153 }
154
155 /// Accessor for the analysis manager.
156 LoopAnalysisManager &getManager() { return *InnerAM; }
157
158 /// Handler for invalidation of the proxy for a particular function.
159 ///
160 /// If the proxy, \c LoopInfo, and associated analyses are preserved, this
161 /// will merely forward the invalidation event to any cached loop analysis
162 /// results for loops within this function.
163 ///
164 /// If the necessary loop infrastructure is not preserved, this will forcibly
165 /// clear all of the cached analysis results that are keyed on the \c
166 /// LoopInfo for this function.
167 bool invalidate(Function &F, const PreservedAnalyses &PA,
168 FunctionAnalysisManager::Invalidator &Inv);
169
170 private:
171 LoopAnalysisManager *InnerAM;
172 LoopInfo *LI;
173 };
174
175 /// Provide a specialized run method for the \c LoopAnalysisManagerFunctionProxy
176 /// so it can pass the \c LoopInfo to the result.
177 template <>
178 LoopAnalysisManagerFunctionProxy::Result
179 LoopAnalysisManagerFunctionProxy::run(Function &F, FunctionAnalysisManager &AM);
180
181 // Ensure the \c LoopAnalysisManagerFunctionProxy is provided as an extern
182 // template.
183 extern template class InnerAnalysisManagerProxy;
184
185 extern template class OuterAnalysisManagerProxy
186 LoopStandardAnalysisResults &>;
187 /// A proxy from a \c FunctionAnalysisManager to a \c Loop.
188 typedef OuterAnalysisManagerProxy
189 LoopStandardAnalysisResults &>
190 FunctionAnalysisManagerLoopProxy;
191
192 /// Returns the minimum set of Analyses that all loop passes must preserve.
193 PreservedAnalyses getLoopPassPreservedAnalyses();
194
195 namespace internal {
196 /// Helper to implement appending of loops onto a worklist.
197 ///
198 /// We want to process loops in postorder, but the worklist is a LIFO data
199 /// structure, so we append to it in *reverse* postorder.
200 ///
201 /// For trees, a preorder traversal is a viable reverse postorder, so we
202 /// actually append using a preorder walk algorithm.
203 template
204 inline void appendLoopsToWorklist(RangeT &&Loops,
205 SmallPriorityWorklist &Worklist) {
206 // We use an internal worklist to build up the preorder traversal without
207 // recursion.
208 SmallVector PreOrderLoops, PreOrderWorklist;
209
210 // We walk the initial sequence of loops in reverse because we generally want
211 // to visit defs before uses and the worklist is LIFO.
212 for (Loop *RootL : reverse(Loops)) {
213 assert(PreOrderLoops.empty() && "Must start with an empty preorder walk.");
214 assert(PreOrderWorklist.empty() &&
215 "Must start with an empty preorder walk worklist.");
216 PreOrderWorklist.push_back(RootL);
217 do {
218 Loop *L = PreOrderWorklist.pop_back_val();
219 PreOrderWorklist.append(L->begin(), L->end());
220 PreOrderLoops.push_back(L);
221 } while (!PreOrderWorklist.empty());
222
223 Worklist.insert(std::move(PreOrderLoops));
224 PreOrderLoops.clear();
225 }
226 }
227 }
228
229 /// The adaptor from a function pass to a loop pass directly computes
230 /// a standard set of analyses that are especially useful to loop passes and
231 /// makes them available in the API. Loop passes are also expected to update
232 /// all of these so that they remain correct across the entire loop pipeline.
233 struct LoopStandardAnalysisResults {
234 AAResults &AA;
235 AssumptionCache &AC;
236 DominatorTree &DT;
237 LoopInfo &LI;
238 ScalarEvolution &SE;
239 TargetLibraryInfo &TLI;
240 TargetTransformInfo &TTI;
241 };
242
243 template class FunctionToLoopPassAdaptor;
244
245 /// This class provides an interface for updating the loop pass manager based
246 /// on mutations to the loop nest.
247 ///
248 /// A reference to an instance of this class is passed as an argument to each
249 /// Loop pass, and Loop passes should use it to update LPM infrastructure if
250 /// they modify the loop nest structure.
251 class LPMUpdater {
252 public:
253 /// This can be queried by loop passes which run other loop passes (like pass
254 /// managers) to know whether the loop needs to be skipped due to updates to
255 /// the loop nest.
256 ///
257 /// If this returns true, the loop object may have been deleted, so passes
258 /// should take care not to touch the object.
259 bool skipCurrentLoop() const { return SkipCurrentLoop; }
260
261 /// Loop passes should use this method to indicate they have deleted a loop
262 /// from the nest.
263 ///
264 /// Note that this loop must either be the current loop or a subloop of the
265 /// current loop. This routine must be called prior to removing the loop from
266 /// the loop nest.
267 ///
268 /// If this is called for the current loop, in addition to clearing any
269 /// state, this routine will mark that the current loop should be skipped by
270 /// the rest of the pass management infrastructure.
271 void markLoopAsDeleted(Loop &L) {
272 LAM.clear(L);
273 assert(CurrentL->contains(&L) && "Cannot delete a loop outside of the "
274 "subloop tree currently being processed.");
275 if (&L == CurrentL)
276 SkipCurrentLoop = true;
277 }
278
279 /// Loop passes should use this method to indicate they have added new child
280 /// loops of the current loop.
281 ///
282 /// \p NewChildLoops must contain only the immediate children. Any nested
283 /// loops within them will be visited in postorder as usual for the loop pass
284 /// manager.
285 void addChildLoops(ArrayRef NewChildLoops) {
286 // Insert ourselves back into the worklist first, as this loop should be
287 // revisited after all the children have been processed.
288 Worklist.insert(CurrentL);
289
290 #ifndef NDEBUG
291 for (Loop *NewL : NewChildLoops)
292 assert(NewL->getParentLoop() == CurrentL && "All of the new loops must "
293 "be immediate children of "
294 "the current loop!");
295 #endif
296
297 internal::appendLoopsToWorklist(NewChildLoops, Worklist);
298
299 // Also skip further processing of the current loop--it will be revisited
300 // after all of its newly added children are accounted for.
301 SkipCurrentLoop = true;
302 }
303
304 /// Loop passes should use this method to indicate they have added new
305 /// sibling loops to the current loop.
306 ///
307 /// \p NewSibLoops must only contain the immediate sibling loops. Any nested
308 /// loops within them will be visited in postorder as usual for the loop pass
309 /// manager.
310 void addSiblingLoops(ArrayRef NewSibLoops) {
311 #ifndef NDEBUG
312 for (Loop *NewL : NewSibLoops)
313 assert(NewL->getParentLoop() == ParentL &&
314 "All of the new loops must be siblings of the current loop!");
315 #endif
316
317 internal::appendLoopsToWorklist(NewSibLoops, Worklist);
318
319 // No need to skip the current loop or revisit it, as sibling loops
320 // shouldn't impact anything.
321 }
322
323 private:
324 template friend class llvm::FunctionToLoopPassAdaptor;
325
326 /// The \c FunctionToLoopPassAdaptor's worklist of loops to process.
327 SmallPriorityWorklist &Worklist;
328
329 /// The analysis manager for use in the current loop nest.
330 LoopAnalysisManager &LAM;
331
332 Loop *CurrentL;
333 bool SkipCurrentLoop;
334
335 #ifndef NDEBUG
336 // In debug builds we also track the parent loop to implement asserts even in
337 // the face of loop deletion.
338 Loop *ParentL;
339 #endif
340
341 LPMUpdater(SmallPriorityWorklist &Worklist,
342 LoopAnalysisManager &LAM)
343 : Worklist(Worklist), LAM(LAM) {}
344 };
345
346 /// \brief Adaptor that maps from a function to its loops.
347 ///
348 /// Designed to allow composition of a LoopPass(Manager) and a
349 /// FunctionPassManager. Note that if this pass is constructed with a \c
350 /// FunctionAnalysisManager it will run the \c LoopAnalysisManagerFunctionProxy
351 /// analysis prior to running the loop passes over the function to enable a \c
352 /// LoopAnalysisManager to be used within this run safely.
353 template
354 class FunctionToLoopPassAdaptor
355 : public PassInfoMixin> {
356 public:
357 explicit FunctionToLoopPassAdaptor(LoopPassT Pass)
358 : Pass(std::move(Pass)) {}
359
360 /// \brief Runs the loop passes across every loop in the function.
361 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM) {
362 // Setup the loop analysis manager from its proxy.
363 LoopAnalysisManager &LAM =
364 AM.getResult(F).getManager();
365 // Get the loop structure for this function
366 LoopInfo &LI = AM.getResult(F);
367
368 // If there are no loops, there is nothing to do here.
369 if (LI.empty())
370 return PreservedAnalyses::all();
371
372 // Get the analysis results needed by loop passes.
373 LoopStandardAnalysisResults LAR = {AM.getResult(F),
374 AM.getResult(F),
375 AM.getResult(F),
376 AM.getResult(F),
377 AM.getResult(F),
378 AM.getResult(F),
379 AM.getResult(F)};
380
381 PreservedAnalyses PA = PreservedAnalyses::all();
382
383 // A postorder worklist of loops to process.
384 SmallPriorityWorklist Worklist;
385
386 // Register the worklist and loop analysis manager so that loop passes can
387 // update them when they mutate the loop nest structure.
388 LPMUpdater Updater(Worklist, LAM);
389
390 // Add the loop nests in the reverse order of LoopInfo. For some reason,
391 // they are stored in RPO w.r.t. the control flow graph in LoopInfo. For
392 // the purpose of unrolling, loop deletion, and LICM, we largely want to
393 // work forward across the CFG so that we visit defs before uses and can
394 // propagate simplifications from one loop nest into the next.
395 // FIXME: Consider changing the order in LoopInfo.
396 internal::appendLoopsToWorklist(reverse(LI), Worklist);
397
398 do {
399 Loop *L = Worklist.pop_back_val();
400
401 // Reset the update structure for this loop.
402 Updater.CurrentL = L;
403 Updater.SkipCurrentLoop = false;
404 #ifndef NDEBUG
405 Updater.ParentL = L->getParentLoop();
406 #endif
407
408 PreservedAnalyses PassPA = Pass.run(*L, LAM, LAR, Updater);
409 // FIXME: We should verify the set of analyses relevant to Loop passes
410 // are preserved.
411
412 // If the loop hasn't been deleted, we need to handle invalidation here.
413 if (!Updater.skipCurrentLoop())
414 // We know that the loop pass couldn't have invalidated any other
415 // loop's analyses (that's the contract of a loop pass), so directly
416 // handle the loop analysis manager's invalidation here.
417 LAM.invalidate(*L, PassPA);
418
419 // Then intersect the preserved set so that invalidation of module
420 // analyses will eventually occur when the module pass completes.
421 PA.intersect(std::move(PassPA));
422 } while (!Worklist.empty());
423
424 // By definition we preserve the proxy. We also preserve all analyses on
425 // Loops. This precludes *any* invalidation of loop analyses by the proxy,
426 // but that's OK because we've taken care to invalidate analyses in the
427 // loop analysis manager incrementally above.
428 PA.preserveSet>();
429 PA.preserve();
430 // We also preserve the set of standard analyses.
431 PA.preserve();
432 PA.preserve();
433 PA.preserve();
434 PA.preserve();
435 // FIXME: What we really want to do here is preserve an AA category, but
436 // that concept doesn't exist yet.
437 PA.preserve();
438 PA.preserve();
439 PA.preserve();
440 PA.preserve();
441 return PA;
442 }
443
444 private:
445 LoopPassT Pass;
446 };
447
448 /// \brief A function to deduce a loop pass type and wrap it in the templated
449 /// adaptor.
450 template
451 FunctionToLoopPassAdaptor
452 createFunctionToLoopPassAdaptor(LoopPassT Pass) {
453 return FunctionToLoopPassAdaptor(std::move(Pass));
454 }
455
456 /// \brief Pass for printing a loop's contents as textual IR.
457 class PrintLoopPass : public PassInfoMixin {
458 raw_ostream &OS;
459 std::string Banner;
460
461 public:
462 PrintLoopPass();
463 PrintLoopPass(raw_ostream &OS, const std::string &Banner = "");
464
465 PreservedAnalyses run(Loop &L, LoopAnalysisManager &,
466 LoopStandardAnalysisResults &, LPMUpdater &);
467 };
468 }
469
470 #endif // LLVM_ANALYSIS_LOOPPASSMANAGER_H
1717
1818 #include "llvm/ADT/Optional.h"
1919 #include "llvm/Analysis/CGSCCPassManager.h"
20 #include "llvm/Analysis/LoopPassManager.h"
2120 #include "llvm/IR/PassManager.h"
21 #include "llvm/Transforms/Scalar/LoopPassManager.h"
2222 #include
2323
2424 namespace llvm {
0 //===- IVUsersPrinter.h - Induction Variable Users Printing -----*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_TRANSFORMS_SCALAR_IVUSERSPRINTER_H
10 #define LLVM_TRANSFORMS_SCALAR_IVUSERSPRINTER_H
11
12 #include "llvm/Analysis/IVUsers.h"
13 #include "llvm/Support/raw_ostream.h"
14 #include "llvm/Transforms/Scalar/LoopPassManager.h"
15
16 namespace llvm {
17
18 /// Printer pass for the \c IVUsers for a loop.
19 class IVUsersPrinterPass : public PassInfoMixin {
20 raw_ostream &OS;
21
22 public:
23 explicit IVUsersPrinterPass(raw_ostream &OS) : OS(OS) {}
24 PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
25 LoopStandardAnalysisResults &AR, LPMUpdater &U);
26 };
27 }
28
29 #endif
1515 #define LLVM_TRANSFORMS_SCALAR_INDVARSIMPLIFY_H
1616
1717 #include "llvm/Analysis/LoopInfo.h"
18 #include "llvm/Analysis/LoopPassManager.h"
1918 #include "llvm/IR/PassManager.h"
19 #include "llvm/Transforms/Scalar/LoopPassManager.h"
2020
2121 namespace llvm {
2222
3333 #define LLVM_TRANSFORMS_SCALAR_LICM_H
3434
3535 #include "llvm/Analysis/LoopInfo.h"
36 #include "llvm/Analysis/LoopPassManager.h"
3736 #include "llvm/IR/PassManager.h"
37 #include "llvm/Transforms/Scalar/LoopPassManager.h"
3838
3939 namespace llvm {
4040
0 //===- llvm/Analysis/LoopAccessAnalysisPrinter.h ----------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_TRANSFORMS_SCALAR_LOOPACCESSANALYSISPRINTER_H
10 #define LLVM_TRANSFORMS_SCALAR_LOOPACCESSANALYSISPRINTER_H
11
12 #include "llvm/Support/raw_ostream.h"
13 #include "llvm/Transforms/Scalar/LoopPassManager.h"
14
15 namespace llvm {
16
17 /// \brief Printer pass for the \c LoopAccessInfo results.
18 class LoopAccessInfoPrinterPass
19 : public PassInfoMixin {
20 raw_ostream &OS;
21
22 public:
23 explicit LoopAccessInfoPrinterPass(raw_ostream &OS) : OS(OS) {}
24 PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
25 LoopStandardAnalysisResults &AR, LPMUpdater &U);
26 };
27
28 } // End llvm namespace
29
30 #endif
1414 #define LLVM_TRANSFORMS_SCALAR_LOOPDELETION_H
1515
1616 #include "llvm/Analysis/LoopInfo.h"
17 #include "llvm/Analysis/LoopPassManager.h"
1817 #include "llvm/Analysis/ScalarEvolution.h"
1918 #include "llvm/IR/PassManager.h"
19 #include "llvm/Transforms/Scalar/LoopPassManager.h"
2020
2121 namespace llvm {
2222
1616 #define LLVM_TRANSFORMS_SCALAR_LOOPIDIOMRECOGNIZE_H
1717
1818 #include "llvm/Analysis/LoopInfo.h"
19 #include "llvm/Analysis/LoopPassManager.h"
2019 #include "llvm/IR/PassManager.h"
20 #include "llvm/Transforms/Scalar/LoopPassManager.h"
2121
2222 namespace llvm {
2323
1414 #define LLVM_TRANSFORMS_SCALAR_LOOPINSTSIMPLIFY_H
1515
1616 #include "llvm/Analysis/LoopInfo.h"
17 #include "llvm/Analysis/LoopPassManager.h"
1817 #include "llvm/IR/PassManager.h"
18 #include "llvm/Transforms/Scalar/LoopPassManager.h"
1919
2020 namespace llvm {
2121
0 //===- LoopPassManager.h - Loop pass management -----------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 /// \file
9 ///
10 /// This header provides classes for managing a pipeline of passes over loops
11 /// in LLVM IR.
12 ///
13 /// The primary loop pass pipeline is managed in a very particular way to
14 /// provide a set of core guarantees:
15 /// 1) Loops are, where possible, in simplified form.
16 /// 2) Loops are *always* in LCSSA form.
17 /// 3) A collection of Loop-specific analysis results are available:
18 /// - LoopInfo
19 /// - DominatorTree
20 /// - ScalarEvolution
21 /// - AAManager
22 /// 4) All loop passes preserve #1 (where possible), #2, and #3.
23 /// 5) Loop passes run over each loop in the loop nest from the innermost to
24 /// the outermost. Specifically, all inner loops are processed before
25 /// passes run over outer loops. When running the pipeline across an inner
26 /// loop creates new inner loops, those are added and processed in this
27 /// order as well.
28 ///
29 /// This process is designed to facilitate transformations which simplify,
30 /// reduce, and remove loops. For passes which are more oriented towards
31 /// optimizing loops, especially optimizing loop *nests* instead of single
32 /// loops in isolation, this framework is less interesting.
33 ///
34 //===----------------------------------------------------------------------===//
35
36 #ifndef LLVM_TRANSFORMS_SCALAR_LOOPPASSMANAGER_H
37 #define LLVM_TRANSFORMS_SCALAR_LOOPPASSMANAGER_H
38
39 #include "llvm/ADT/PostOrderIterator.h"
40 #include "llvm/ADT/PriorityWorklist.h"
41 #include "llvm/ADT/STLExtras.h"
42 #include "llvm/Analysis/AliasAnalysis.h"
43 #include "llvm/Analysis/BasicAliasAnalysis.h"
44 #include "llvm/Analysis/GlobalsModRef.h"
45 #include "llvm/Analysis/LoopAnalysisManager.h"
46 #include "llvm/Analysis/LoopInfo.h"
47 #include "llvm/Analysis/ScalarEvolution.h"
48 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
49 #include "llvm/Analysis/TargetLibraryInfo.h"
50 #include "llvm/Analysis/TargetTransformInfo.h"
51 #include "llvm/IR/Dominators.h"
52 #include "llvm/IR/PassManager.h"
53
54 namespace llvm {
55
56 // Forward declarations of an update tracking API used in the pass manager.
57 class LPMUpdater;
58
59 // Explicit specialization and instantiation declarations for the pass manager.
60 // See the comments on the definition of the specialization for details on how
61 // it differs from the primary template.
62 template <>
63 PreservedAnalyses
64 PassManager
65 LPMUpdater &>::run(Loop &InitialL, LoopAnalysisManager &AM,
66 LoopStandardAnalysisResults &AnalysisResults,
67 LPMUpdater &U);
68 extern template class PassManager
69 LoopStandardAnalysisResults &, LPMUpdater &>;
70
71 /// \brief The Loop pass manager.
72 ///
73 /// See the documentation for the PassManager template for details. It runs
74 /// a sequence of Loop passes over each Loop that the manager is run over. This
75 /// typedef serves as a convenient way to refer to this construct.
76 typedef PassManager
77 LPMUpdater &>
78 LoopPassManager;
79
80 /// A partial specialization of the require analysis template pass to forward
81 /// the extra parameters from a transformation's run method to the
82 /// AnalysisManager's getResult.
83 template
84 struct RequireAnalysisPass
85 LoopStandardAnalysisResults &, LPMUpdater &>
86 : PassInfoMixin<
87 RequireAnalysisPass
88 LoopStandardAnalysisResults &, LPMUpdater &>> {
89 PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
90 LoopStandardAnalysisResults &AR, LPMUpdater &) {
91 (void)AM.template getResult(L, AR);
92 return PreservedAnalyses::all();
93 }
94 };
95
96 /// An alias template to easily name a require analysis loop pass.
97 template
98 using RequireAnalysisLoopPass =
99 RequireAnalysisPass
100 LoopStandardAnalysisResults &, LPMUpdater &>;
101
102 namespace internal {
103 /// Helper to implement appending of loops onto a worklist.
104 ///
105 /// We want to process loops in postorder, but the worklist is a LIFO data
106 /// structure, so we append to it in *reverse* postorder.
107 ///
108 /// For trees, a preorder traversal is a viable reverse postorder, so we
109 /// actually append using a preorder walk algorithm.
110 template
111 inline void appendLoopsToWorklist(RangeT &&Loops,
112 SmallPriorityWorklist &Worklist) {
113 // We use an internal worklist to build up the preorder traversal without
114 // recursion.
115 SmallVector PreOrderLoops, PreOrderWorklist;
116
117 // We walk the initial sequence of loops in reverse because we generally want
118 // to visit defs before uses and the worklist is LIFO.
119 for (Loop *RootL : reverse(Loops)) {
120 assert(PreOrderLoops.empty() && "Must start with an empty preorder walk.");
121 assert(PreOrderWorklist.empty() &&
122 "Must start with an empty preorder walk worklist.");
123 PreOrderWorklist.push_back(RootL);
124 do {
125 Loop *L = PreOrderWorklist.pop_back_val();
126 PreOrderWorklist.append(L->begin(), L->end());
127 PreOrderLoops.push_back(L);
128 } while (!PreOrderWorklist.empty());
129
130 Worklist.insert(std::move(PreOrderLoops));
131 PreOrderLoops.clear();
132 }
133 }
134 }
135
136 template class FunctionToLoopPassAdaptor;
137
138 /// This class provides an interface for updating the loop pass manager based
139 /// on mutations to the loop nest.
140 ///
141 /// A reference to an instance of this class is passed as an argument to each
142 /// Loop pass, and Loop passes should use it to update LPM infrastructure if
143 /// they modify the loop nest structure.
144 class LPMUpdater {
145 public:
146 /// This can be queried by loop passes which run other loop passes (like pass
147 /// managers) to know whether the loop needs to be skipped due to updates to
148 /// the loop nest.
149 ///
150 /// If this returns true, the loop object may have been deleted, so passes
151 /// should take care not to touch the object.
152 bool skipCurrentLoop() const { return SkipCurrentLoop; }
153
154 /// Loop passes should use this method to indicate they have deleted a loop
155 /// from the nest.
156 ///
157 /// Note that this loop must either be the current loop or a subloop of the
158 /// current loop. This routine must be called prior to removing the loop from
159 /// the loop nest.
160 ///
161 /// If this is called for the current loop, in addition to clearing any
162 /// state, this routine will mark that the current loop should be skipped by
163 /// the rest of the pass management infrastructure.
164 void markLoopAsDeleted(Loop &L) {
165 LAM.clear(L);
166 assert(CurrentL->contains(&L) && "Cannot delete a loop outside of the "
167 "subloop tree currently being processed.");
168 if (&L == CurrentL)
169 SkipCurrentLoop = true;
170 }
171
172 /// Loop passes should use this method to indicate they have added new child
173 /// loops of the current loop.
174 ///
175 /// \p NewChildLoops must contain only the immediate children. Any nested
176 /// loops within them will be visited in postorder as usual for the loop pass
177 /// manager.
178 void addChildLoops(ArrayRef NewChildLoops) {
179 // Insert ourselves back into the worklist first, as this loop should be
180 // revisited after all the children have been processed.
181 Worklist.insert(CurrentL);
182
183 #ifndef NDEBUG
184 for (Loop *NewL : NewChildLoops)
185 assert(NewL->getParentLoop() == CurrentL && "All of the new loops must "
186 "be immediate children of "
187 "the current loop!");
188 #endif
189
190 internal::appendLoopsToWorklist(NewChildLoops, Worklist);
191
192 // Also skip further processing of the current loop--it will be revisited
193 // after all of its newly added children are accounted for.
194 SkipCurrentLoop = true;
195 }
196
197 /// Loop passes should use this method to indicate they have added new
198 /// sibling loops to the current loop.
199 ///
200 /// \p NewSibLoops must only contain the immediate sibling loops. Any nested
201 /// loops within them will be visited in postorder as usual for the loop pass
202 /// manager.
203 void addSiblingLoops(ArrayRef NewSibLoops) {
204 #ifndef NDEBUG
205 for (Loop *NewL : NewSibLoops)
206 assert(NewL->getParentLoop() == ParentL &&
207 "All of the new loops must be siblings of the current loop!");
208 #endif
209
210 internal::appendLoopsToWorklist(NewSibLoops, Worklist);
211
212 // No need to skip the current loop or revisit it, as sibling loops
213 // shouldn't impact anything.
214 }
215
216 private:
217 template friend class llvm::FunctionToLoopPassAdaptor;
218
219 /// The \c FunctionToLoopPassAdaptor's worklist of loops to process.
220 SmallPriorityWorklist &Worklist;
221
222 /// The analysis manager for use in the current loop nest.
223 LoopAnalysisManager &LAM;
224
225 Loop *CurrentL;
226 bool SkipCurrentLoop;
227
228 #ifndef NDEBUG
229 // In debug builds we also track the parent loop to implement asserts even in
230 // the face of loop deletion.
231 Loop *ParentL;
232 #endif
233
234 LPMUpdater(SmallPriorityWorklist &Worklist,
235 LoopAnalysisManager &LAM)
236 : Worklist(Worklist), LAM(LAM) {}
237 };
238
239 /// \brief Adaptor that maps from a function to its loops.
240 ///
241 /// Designed to allow composition of a LoopPass(Manager) and a
242 /// FunctionPassManager. Note that if this pass is constructed with a \c
243 /// FunctionAnalysisManager it will run the \c LoopAnalysisManagerFunctionProxy
244 /// analysis prior to running the loop passes over the function to enable a \c
245 /// LoopAnalysisManager to be used within this run safely.
246 template
247 class FunctionToLoopPassAdaptor
248 : public PassInfoMixin> {
249 public:
250 explicit FunctionToLoopPassAdaptor(LoopPassT Pass) : Pass(std::move(Pass)) {}
251
252 /// \brief Runs the loop passes across every loop in the function.
253 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM) {
254 // Setup the loop analysis manager from its proxy.
255 LoopAnalysisManager &LAM =
256 AM.getResult(F).getManager();
257 // Get the loop structure for this function
258 LoopInfo &LI = AM.getResult(F);
259
260 // If there are no loops, there is nothing to do here.
261 if (LI.empty())
262 return PreservedAnalyses::all();
263
264 // Get the analysis results needed by loop passes.
265 LoopStandardAnalysisResults LAR = {AM.getResult(F),
266 AM.getResult(F),
267 AM.getResult(F),
268 AM.getResult(F),
269 AM.getResult(F),
270 AM.getResult(F),
271 AM.getResult(F)};
272
273 PreservedAnalyses PA = PreservedAnalyses::all();
274
275 // A postorder worklist of loops to process.
276 SmallPriorityWorklist Worklist;
277
278 // Register the worklist and loop analysis manager so that loop passes can
279 // update them when they mutate the loop nest structure.
280 LPMUpdater Updater(Worklist, LAM);
281
282 // Add the loop nests in the reverse order of LoopInfo. For some reason,
283 // they are stored in RPO w.r.t. the control flow graph in LoopInfo. For
284 // the purpose of unrolling, loop deletion, and LICM, we largely want to
285 // work forward across the CFG so that we visit defs before uses and can
286 // propagate simplifications from one loop nest into the next.
287 // FIXME: Consider changing the order in LoopInfo.
288 internal::appendLoopsToWorklist(reverse(LI), Worklist);
289
290 do {
291 Loop *L = Worklist.pop_back_val();
292
293 // Reset the update structure for this loop.
294 Updater.CurrentL = L;
295 Updater.SkipCurrentLoop = false;
296 #ifndef NDEBUG
297 Updater.ParentL = L->getParentLoop();
298 #endif
299
300 PreservedAnalyses PassPA = Pass.run(*L, LAM, LAR, Updater);
301 // FIXME: We should verify the set of analyses relevant to Loop passes
302 // are preserved.
303
304 // If the loop hasn't been deleted, we need to handle invalidation here.
305 if (!Updater.skipCurrentLoop())
306 // We know that the loop pass couldn't have invalidated any other
307 // loop's analyses (that's the contract of a loop pass), so directly
308 // handle the loop analysis manager's invalidation here.
309 LAM.invalidate(*L, PassPA);
310
311 // Then intersect the preserved set so that invalidation of module
312 // analyses will eventually occur when the module pass completes.
313 PA.intersect(std::move(PassPA));
314 } while (!Worklist.empty());
315
316 // By definition we preserve the proxy. We also preserve all analyses on
317 // Loops. This precludes *any* invalidation of loop analyses by the proxy,
318 // but that's OK because we've taken care to invalidate analyses in the
319 // loop analysis manager incrementally above.
320 PA.preserveSet>();
321 PA.preserve();
322 // We also preserve the set of standard analyses.
323 PA.preserve();
324 PA.preserve();
325 PA.preserve();
326 PA.preserve();
327 // FIXME: What we really want to do here is preserve an AA category, but
328 // that concept doesn't exist yet.
329 PA.preserve();
330 PA.preserve();
331 PA.preserve();
332 PA.preserve();
333 return PA;
334 }
335
336 private:
337 LoopPassT Pass;
338 };
339
340 /// \brief A function to deduce a loop pass type and wrap it in the templated
341 /// adaptor.
342 template
343 FunctionToLoopPassAdaptor
344 createFunctionToLoopPassAdaptor(LoopPassT Pass) {
345 return FunctionToLoopPassAdaptor(std::move(Pass));
346 }
347
348 /// \brief Pass for printing a loop's contents as textual IR.
349 class PrintLoopPass : public PassInfoMixin {
350 raw_ostream &OS;
351 std::string Banner;
352
353 public:
354 PrintLoopPass();
355 PrintLoopPass(raw_ostream &OS, const std::string &Banner = "");
356
357 PreservedAnalyses run(Loop &L, LoopAnalysisManager &,
358 LoopStandardAnalysisResults &, LPMUpdater &);
359 };
360 }
361
362 #endif // LLVM_TRANSFORMS_SCALAR_LOOPPASSMANAGER_H
1414 #define LLVM_TRANSFORMS_SCALAR_LOOPROTATION_H
1515
1616 #include "llvm/Analysis/LoopInfo.h"
17 #include "llvm/Analysis/LoopPassManager.h"
1817 #include "llvm/IR/PassManager.h"
18 #include "llvm/Transforms/Scalar/LoopPassManager.h"
1919
2020 namespace llvm {
2121
1717 #define LLVM_TRANSFORMS_SCALAR_LOOPSIMPLIFYCFG_H
1818
1919 #include "llvm/Analysis/LoopInfo.h"
20 #include "llvm/Analysis/LoopPassManager.h"
2120 #include "llvm/IR/PassManager.h"
21 #include "llvm/Transforms/Scalar/LoopPassManager.h"
2222
2323 namespace llvm {
2424
2222 #define LLVM_TRANSFORMS_SCALAR_LOOPSTRENGTHREDUCE_H
2323
2424 #include "llvm/Analysis/LoopInfo.h"
25 #include "llvm/Analysis/LoopPassManager.h"
2625 #include "llvm/IR/PassManager.h"
26 #include "llvm/Transforms/Scalar/LoopPassManager.h"
2727
2828 namespace llvm {
2929
1010 #define LLVM_TRANSFORMS_SCALAR_LOOPUNROLLPASS_H
1111
1212 #include "llvm/Analysis/LoopInfo.h"
13 #include "llvm/Analysis/LoopPassManager.h"
1413 #include "llvm/IR/PassManager.h"
14 #include "llvm/Transforms/Scalar/LoopPassManager.h"
1515
1616 namespace llvm {
1717
5656 #include "llvm/Analysis/DemandedBits.h"
5757 #include "llvm/Analysis/LoopAccessAnalysis.h"
5858 #include "llvm/Analysis/LoopInfo.h"
59 #include "llvm/Analysis/LoopPassManager.h"
6059 #include "llvm/Analysis/OptimizationDiagnosticInfo.h"
6160 #include "llvm/Analysis/ScalarEvolution.h"
6261 #include "llvm/Analysis/TargetTransformInfo.h"
6362 #include "llvm/IR/Function.h"
6463 #include "llvm/IR/PassManager.h"
64 #include "llvm/Transforms/Scalar/LoopPassManager.h"
6565 #include
6666
6767 namespace llvm {
4343 Lint.cpp
4444 Loads.cpp
4545 LoopAccessAnalysis.cpp
46 LoopAnalysisManager.cpp
4647 LoopUnrollAnalyzer.cpp
4748 LoopInfo.cpp
4849 LoopPass.cpp
49 LoopPassManager.cpp
5050 MemDepPrinter.cpp
5151 MemDerefPrinter.cpp
5252 MemoryBuiltins.cpp
1515 #include "llvm/ADT/STLExtras.h"
1616 #include "llvm/Analysis/AssumptionCache.h"
1717 #include "llvm/Analysis/CodeMetrics.h"
18 #include "llvm/Analysis/LoopAnalysisManager.h"
1819 #include "llvm/Analysis/LoopPass.h"
19 #include "llvm/Analysis/LoopPassManager.h"
2020 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
2121 #include "llvm/Analysis/ValueTracking.h"
2222 #include "llvm/IR/Constants.h"
3838 IVUsers IVUsersAnalysis::run(Loop &L, LoopAnalysisManager &AM,
3939 LoopStandardAnalysisResults &AR) {
4040 return IVUsers(&L, &AR.AC, &AR.LI, &AR.DT, &AR.SE);
41 }
42
43 PreservedAnalyses IVUsersPrinterPass::run(Loop &L, LoopAnalysisManager &AM,
44 LoopStandardAnalysisResults &AR,
45 LPMUpdater &U) {
46 AM.getResult(L, AR).print(OS);
47 return PreservedAnalyses::all();
4841 }
4942
5043 char IVUsersWrapperPass::ID = 0;
1111 //
1212 //===----------------------------------------------------------------------===//
1313
14 #include "llvm/Analysis/LoopAccessAnalysis.h"
1415 #include "llvm/ADT/APInt.h"
1516 #include "llvm/ADT/DenseMap.h"
1617 #include "llvm/ADT/DepthFirstIterator.h"
1718 #include "llvm/ADT/EquivalenceClasses.h"
18 #include "llvm/ADT/iterator_range.h"
1919 #include "llvm/ADT/PointerIntPair.h"
20 #include "llvm/ADT/STLExtras.h"
2021 #include "llvm/ADT/SetVector.h"
2122 #include "llvm/ADT/SmallPtrSet.h"
2223 #include "llvm/ADT/SmallSet.h"
2324 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/ADT/iterator_range.h"
2526 #include "llvm/Analysis/AliasAnalysis.h"
2627 #include "llvm/Analysis/AliasSetTracker.h"
27 #include "llvm/Analysis/LoopAccessAnalysis.h"
28 #include "llvm/Analysis/LoopAnalysisManager.h"
2829 #include "llvm/Analysis/LoopInfo.h"
29 #include "llvm/Analysis/LoopPassManager.h"
3030 #include "llvm/Analysis/MemoryLocation.h"
3131 #include "llvm/Analysis/OptimizationDiagnosticInfo.h"
3232 #include "llvm/Analysis/ScalarEvolution.h"
4343 #include "llvm/IR/DiagnosticInfo.h"
4444 #include "llvm/IR/Dominators.h"
4545 #include "llvm/IR/Function.h"
46 #include "llvm/IR/IRBuilder.h"
4647 #include "llvm/IR/InstrTypes.h"
4748 #include "llvm/IR/Instruction.h"
4849 #include "llvm/IR/Instructions.h"
49 #include "llvm/IR/IRBuilder.h"
5050 #include "llvm/IR/Operator.h"
5151 #include "llvm/IR/PassManager.h"
5252 #include "llvm/IR/Type.h"
21242124 return LoopAccessInfo(&L, &AR.SE, &AR.TLI, &AR.AA, &AR.DT, &AR.LI);
21252125 }
21262126
2127 PreservedAnalyses
2128 LoopAccessInfoPrinterPass::run(Loop &L, LoopAnalysisManager &AM,
2129 LoopStandardAnalysisResults &AR, LPMUpdater &) {
2130 Function &F = *L.getHeader()->getParent();
2131 auto &LAI = AM.getResult(L, AR);
2132 OS << "Loop access info in function '" << F.getName() << "':\n";
2133 OS.indent(2) << L.getHeader()->getName() << ":\n";
2134 LAI.print(OS, 4);
2135 return PreservedAnalyses::all();
2136 }
2137
21382127 namespace llvm {
21392128
21402129 Pass *createLAAPass() {
0 //===- LoopAnalysisManager.cpp - Loop analysis management -----------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/Analysis/LoopAnalysisManager.h"
10 #include "llvm/Analysis/BasicAliasAnalysis.h"
11 #include "llvm/Analysis/GlobalsModRef.h"
12 #include "llvm/Analysis/LoopInfo.h"
13 #include "llvm/Analysis/ScalarEvolution.h"
14 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
15 #include "llvm/IR/Dominators.h"
16
17 using namespace llvm;
18
19 // Explicit template instantiations and specialization defininitions for core
20 // template typedefs.
21 namespace llvm {
22 template class AllAnalysesOn;
23 template class AnalysisManager;
24 template class InnerAnalysisManagerProxy;
25 template class OuterAnalysisManagerProxy
26 LoopStandardAnalysisResults &>;
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 in reverse.
34 SmallVector PreOrderLoops, PreOrderWorklist;
35 // Note that we want to walk the roots in reverse order because we will end
36 // up reversing the preorder sequence. However, it happens that the loop nest
37 // roots are in reverse order within the LoopInfo object. So we just walk
38 // forward here.
39 // FIXME: If we change the order of LoopInfo we will want to add a reverse
40 // here.
41 for (Loop *RootL : *LI) {
42 assert(PreOrderWorklist.empty() &&
43 "Must start with an empty preorder walk worklist.");
44 PreOrderWorklist.push_back(RootL);
45 do {
46 Loop *L = PreOrderWorklist.pop_back_val();
47 PreOrderWorklist.append(L->begin(), L->end());
48 PreOrderLoops.push_back(L);
49 } while (!PreOrderWorklist.empty());
50 }
51
52 // If this proxy or the loop info is going to be invalidated, we also need
53 // to clear all the keys coming from that analysis. We also completely blow
54 // away the loop analyses if any of the standard analyses provided by the
55 // loop pass manager go away so that loop analyses can freely use these
56 // without worrying about declaring dependencies on them etc.
57 // FIXME: It isn't clear if this is the right tradeoff. We could instead make
58 // loop analyses declare any dependencies on these and use the more general
59 // invalidation logic below to act on that.
60 auto PAC = PA.getChecker();
61 if (!(PAC.preserved() || PAC.preservedSet>()) ||
62 Inv.invalidate(F, PA) ||
63 Inv.invalidate(F, PA) ||
64 Inv.invalidate(F, PA) ||
65 Inv.invalidate(F, PA) ||
66 Inv.invalidate(F, PA)) {
67 // Note that the LoopInfo may be stale at this point, however the loop
68 // objects themselves remain the only viable keys that could be in the
69 // analysis manager's cache. So we just walk the keys and forcibly clear
70 // those results. Note that the order doesn't matter here as this will just
71 // directly destroy the results without calling methods on them.
72 for (Loop *L : PreOrderLoops)
73 InnerAM->clear(*L);
74
75 // We also need to null out the inner AM so that when the object gets
76 // destroyed as invalid we don't try to clear the inner AM again. At that
77 // point we won't be able to reliably walk the loops for this function and
78 // only clear results associated with those loops the way we do here.
79 // FIXME: Making InnerAM null at this point isn't very nice. Most analyses
80 // try to remain valid during invalidation. Maybe we should add an
81 // `IsClean` flag?
82 InnerAM = nullptr;
83
84 // Now return true to indicate this *is* invalid and a fresh proxy result
85 // needs to be built. This is especially important given the null InnerAM.
86 return true;
87 }
88
89 // Directly check if the relevant set is preserved so we can short circuit
90 // invalidating loops.
91 bool AreLoopAnalysesPreserved =
92 PA.allAnalysesInSetPreserved>();
93
94 // Since we have a valid LoopInfo we can actually leave the cached results in
95 // the analysis manager associated with the Loop keys, but we need to
96 // propagate any necessary invalidation logic into them. We'd like to
97 // invalidate things in roughly the same order as they were put into the
98 // cache and so we walk the preorder list in reverse to form a valid
99 // postorder.
100 for (Loop *L : reverse(PreOrderLoops)) {
101 Optional InnerPA;
102
103 // Check to see whether the preserved set needs to be adjusted based on
104 // function-level analysis invalidation triggering deferred invalidation
105 // for this loop.
106 if (auto *OuterProxy =
107 InnerAM->getCachedResult(*L))
108 for (const auto &OuterInvalidationPair :
109 OuterProxy->getOuterInvalidations()) {
110 AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first;
111 const auto &InnerAnalysisIDs = OuterInvalidationPair.second;
112 if (Inv.invalidate(OuterAnalysisID, F, PA)) {
113 if (!InnerPA)
114 InnerPA = PA;
115 for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs)
116 InnerPA->abandon(InnerAnalysisID);
117 }
118 }
119
120 // Check if we needed a custom PA set. If so we'll need to run the inner
121 // invalidation.
122 if (InnerPA) {
123 InnerAM->invalidate(*L, *InnerPA);
124 continue;
125 }
126
127 // Otherwise we only need to do invalidation if the original PA set didn't
128 // preserve all Loop analyses.
129 if (!AreLoopAnalysesPreserved)
130 InnerAM->invalidate(*L, PA);
131 }
132
133 // Return false to indicate that this result is still a valid proxy.
134 return false;
135 }
136
137 template <>
138 LoopAnalysisManagerFunctionProxy::Result
139 LoopAnalysisManagerFunctionProxy::run(Function &F,
140 FunctionAnalysisManager &AM) {
141 return Result(*InnerAM, AM.getResult(F));
142 }
143 }
144
145 PreservedAnalyses llvm::getLoopPassPreservedAnalyses() {
146 PreservedAnalyses PA;
147 PA.preserve();
148 PA.preserve();
149 PA.preserve();
150 PA.preserve();
151 PA.preserve();
152 // TODO: What we really want to do here is preserve an AA category, but that
153 // concept doesn't exist yet.
154 PA.preserve();
155 PA.preserve();
156 PA.preserve();
157 PA.preserve();
158 return PA;
159 }
1313 //===----------------------------------------------------------------------===//
1414
1515 #include "llvm/Analysis/LoopPass.h"
16 #include "llvm/Analysis/LoopPassManager.h"
16 #include "llvm/Analysis/LoopAnalysisManager.h"
1717 #include "llvm/IR/Dominators.h"
1818 #include "llvm/IR/IRPrintingPasses.h"
1919 #include "llvm/IR/LLVMContext.h"
+0
-227
lib/Analysis/LoopPassManager.cpp less more
None //===- LoopPassManager.cpp - Loop pass management -------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/Analysis/LoopPassManager.h"
10 #include "llvm/Analysis/BasicAliasAnalysis.h"
11 #include "llvm/Analysis/GlobalsModRef.h"
12 #include "llvm/Analysis/LoopInfo.h"
13 #include "llvm/Analysis/ScalarEvolution.h"
14 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
15 #include "llvm/IR/Dominators.h"
16
17 using namespace llvm;
18
19 // Explicit template instantiations and specialization defininitions for core
20 // template typedefs.
21 namespace llvm {
22 template class AllAnalysesOn;
23 template class AnalysisManager;
24 template class PassManager
25 LoopStandardAnalysisResults &, LPMUpdater &>;
26 template class InnerAnalysisManagerProxy;
27 template class OuterAnalysisManagerProxy
28 LoopStandardAnalysisResults &>;
29
30 /// Explicitly specialize the pass manager's run method to handle loop nest
31 /// structure updates.
32 template <>
33 PreservedAnalyses
34 PassManager
35 LPMUpdater &>::run(Loop &L, LoopAnalysisManager &AM,
36 LoopStandardAnalysisResults &AR, LPMUpdater &U) {
37 PreservedAnalyses PA = PreservedAnalyses::all();
38
39 if (DebugLogging)
40 dbgs() << "Starting Loop pass manager run.\n";
41
42 for (auto &Pass : Passes) {
43 if (DebugLogging)
44 dbgs() << "Running pass: " << Pass->name() << " on " << L;
45
46 PreservedAnalyses PassPA = Pass->run(L, AM, AR, U);
47
48 // If the loop was deleted, abort the run and return to the outer walk.
49 if (U.skipCurrentLoop()) {
50 PA.intersect(std::move(PassPA));
51 break;
52 }
53
54 // Update the analysis manager as each pass runs and potentially
55 // invalidates analyses.
56 AM.invalidate(L, PassPA);
57
58 // Finally, we intersect the final preserved analyses to compute the
59 // aggregate preserved set for this pass manager.
60 PA.intersect(std::move(PassPA));
61
62 // FIXME: Historically, the pass managers all called the LLVM context's
63 // yield function here. We don't have a generic way to acquire the
64 // context and it isn't yet clear what the right pattern is for yielding
65 // in the new pass manager so it is currently omitted.
66 // ...getContext().yield();
67 }
68
69 // Invalidation for the current loop should be handled above, and other loop
70 // analysis results shouldn't be impacted by runs over this loop. Therefore,
71 // the remaining analysis results in the AnalysisManager are preserved. We
72 // mark this with a set so that we don't need to inspect each one
73 // individually.
74 // FIXME: This isn't correct! This loop and all nested loops' analyses should
75 // be preserved, but unrolling should invalidate the parent loop's analyses.
76 PA.preserveSet>();
77
78 if (DebugLogging)
79 dbgs() << "Finished Loop pass manager run.\n";
80
81 return PA;
82 }
83
84 bool LoopAnalysisManagerFunctionProxy::Result::invalidate(
85 Function &F, const PreservedAnalyses &PA,
86 FunctionAnalysisManager::Invalidator &Inv) {
87 // First compute the sequence of IR units covered by this proxy. We will want
88 // to visit this in postorder, but because this is a tree structure we can do
89 // this by building a preorder sequence and walking it in reverse.
90 SmallVector PreOrderLoops, PreOrderWorklist;
91 // Note that we want to walk the roots in reverse order because we will end
92 // up reversing the preorder sequence. However, it happens that the loop nest
93 // roots are in reverse order within the LoopInfo object. So we just walk
94 // forward here.
95 // FIXME: If we change the order of LoopInfo we will want to add a reverse
96 // here.
97 for (Loop *RootL : *LI) {
98 assert(PreOrderWorklist.empty() &&
99 "Must start with an empty preorder walk worklist.");
100 PreOrderWorklist.push_back(RootL);
101 do {
102 Loop *L = PreOrderWorklist.pop_back_val();
103 PreOrderWorklist.append(L->begin(), L->end());
104 PreOrderLoops.push_back(L);
105 } while (!PreOrderWorklist.empty());
106 }
107
108 // If this proxy or the loop info is going to be invalidated, we also need
109 // to clear all the keys coming from that analysis. We also completely blow
110 // away the loop analyses if any of the standard analyses provided by the
111 // loop pass manager go away so that loop analyses can freely use these
112 // without worrying about declaring dependencies on them etc.
113 // FIXME: It isn't clear if this is the right tradeoff. We could instead make
114 // loop analyses declare any dependencies on these and use the more general
115 // invalidation logic below to act on that.
116 auto PAC = PA.getChecker();
117 if (!(PAC.preserved() || PAC.preservedSet>()) ||
118 Inv.invalidate(F, PA) ||
119 Inv.invalidate(F, PA) ||
120 Inv.invalidate(F, PA) ||
121 Inv.invalidate(F, PA) ||
122 Inv.invalidate(F, PA)) {
123 // Note that the LoopInfo may be stale at this point, however the loop
124 // objects themselves remain the only viable keys that could be in the
125 // analysis manager's cache. So we just walk the keys and forcibly clear
126 // those results. Note that the order doesn't matter here as this will just
127 // directly destroy the results without calling methods on them.
128 for (Loop *L : PreOrderLoops)
129 InnerAM->clear(*L);
130
131 // We also need to null out the inner AM so that when the object gets
132 // destroyed as invalid we don't try to clear the inner AM again. At that
133 // point we won't be able to reliably walk the loops for this function and
134 // only clear results associated with those loops the way we do here.
135 // FIXME: Making InnerAM null at this point isn't very nice. Most analyses
136 // try to remain valid during invalidation. Maybe we should add an
137 // `IsClean` flag?
138 InnerAM = nullptr;
139
140 // Now return true to indicate this *is* invalid and a fresh proxy result
141 // needs to be built. This is especially important given the null InnerAM.
142 return true;
143 }
144
145 // Directly check if the relevant set is preserved so we can short circuit
146 // invalidating loops.
147 bool AreLoopAnalysesPreserved =
148 PA.allAnalysesInSetPreserved>();
149
150 // Since we have a valid LoopInfo we can actually leave the cached results in
151 // the analysis manager associated with the Loop keys, but we need to
152 // propagate any necessary invalidation logic into them. We'd like to
153 // invalidate things in roughly the same order as they were put into the
154 // cache and so we walk the preorder list in reverse to form a valid
155 // postorder.
156 for (Loop *L : reverse(PreOrderLoops)) {
157 Optional InnerPA;
158
159 // Check to see whether the preserved set needs to be adjusted based on
160 // function-level analysis invalidation triggering deferred invalidation
161 // for this loop.
162 if (auto *OuterProxy =
163 InnerAM->getCachedResult(*L))
164 for (const auto &OuterInvalidationPair :
165 OuterProxy->getOuterInvalidations()) {
166 AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first;
167 const auto &InnerAnalysisIDs = OuterInvalidationPair.second;
168 if (Inv.invalidate(OuterAnalysisID, F, PA)) {
169 if (!InnerPA)
170 InnerPA = PA;
171 for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs)
172 InnerPA->abandon(InnerAnalysisID);
173 }
174 }
175
176 // Check if we needed a custom PA set. If so we'll need to run the inner
177 // invalidation.
178 if (InnerPA) {
179 InnerAM->invalidate(*L, *InnerPA);
180 continue;
181 }
182
183 // Otherwise we only need to do invalidation if the original PA set didn't
184 // preserve all Loop analyses.
185 if (!AreLoopAnalysesPreserved)
186 InnerAM->invalidate(*L, PA);
187 }
188
189 // Return false to indicate that this result is still a valid proxy.
190 return false;
191 }
192
193 template <>
194 LoopAnalysisManagerFunctionProxy::Result
195 LoopAnalysisManagerFunctionProxy::run(Function &F,
196 FunctionAnalysisManager &AM) {
197 return Result(*InnerAM, AM.getResult(F));
198 }
199 }
200
201 PreservedAnalyses llvm::getLoopPassPreservedAnalyses() {
202 PreservedAnalyses PA;
203 PA.preserve();
204 PA.preserve();
205 PA.preserve();
206 PA.preserve();
207 PA.preserve();
208 // TODO: What we really want to do here is preserve an AA category, but that
209 // concept doesn't exist yet.
210 PA.preserve();
211 PA.preserve();
212 PA.preserve();
213 PA.preserve();
214 return PA;
215 }
216
217 PrintLoopPass::PrintLoopPass() : OS(dbgs()) {}
218 PrintLoopPass::PrintLoopPass(raw_ostream &OS, const std::string &Banner)
219 : OS(OS), Banner(Banner) {}
220
221 PreservedAnalyses PrintLoopPass::run(Loop &L, LoopAnalysisManager &,
222 LoopStandardAnalysisResults &,
223 LPMUpdater &) {
224 printLoop(L, OS, Banner);
225 return PreservedAnalyses::all();
226 }
1616 #include "llvm/LTO/LTOBackend.h"
1717 #include "llvm/Analysis/AliasAnalysis.h"
1818 #include "llvm/Analysis/CGSCCPassManager.h"
19 #include "llvm/Analysis/LoopPassManager.h"
2019 #include "llvm/Analysis/TargetLibraryInfo.h"
2120 #include "llvm/Analysis/TargetTransformInfo.h"
2221 #include "llvm/Bitcode/BitcodeReader.h"
3534 #include "llvm/Target/TargetMachine.h"
3635 #include "llvm/Transforms/IPO.h"
3736 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
37 #include "llvm/Transforms/Scalar/LoopPassManager.h"
3838 #include "llvm/Transforms/Utils/FunctionImportUtils.h"
3939 #include "llvm/Transforms/Utils/SplitModule.h"
4040
3737 #include "llvm/Analysis/LazyValueInfo.h"
3838 #include "llvm/Analysis/LoopAccessAnalysis.h"
3939 #include "llvm/Analysis/LoopInfo.h"
40 #include "llvm/Analysis/LoopPassManager.h"
4140 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
4241 #include "llvm/Analysis/ModuleSummaryAnalysis.h"
4342 #include "llvm/Analysis/OptimizationDiagnosticInfo.h"
9493 #include "llvm/Transforms/Scalar/Float2Int.h"
9594 #include "llvm/Transforms/Scalar/GVN.h"
9695 #include "llvm/Transforms/Scalar/GuardWidening.h"
96 #include "llvm/Transforms/Scalar/IVUsersPrinter.h"
9797 #include "llvm/Transforms/Scalar/IndVarSimplify.h"
9898 #include "llvm/Transforms/Scalar/JumpThreading.h"
9999 #include "llvm/Transforms/Scalar/LICM.h"
100 #include "llvm/Transforms/Scalar/LoopAccessAnalysisPrinter.h"
100101 #include "llvm/Transforms/Scalar/LoopDataPrefetch.h"
101102 #include "llvm/Transforms/Scalar/LoopDeletion.h"
102103 #include "llvm/Transforms/Scalar/LoopDistribute.h"
103104 #include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
104105 #include "llvm/Transforms/Scalar/LoopInstSimplify.h"
106 #include "llvm/Transforms/Scalar/LoopPassManager.h"
105107 #include "llvm/Transforms/Scalar/LoopRotation.h"
106108 #include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
107109 #include "llvm/Transforms/Scalar/LoopStrengthReduce.h"
1212 GuardWidening.cpp
1313 GVN.cpp
1414 GVNHoist.cpp
15 IVUsersPrinter.cpp
1516 InductiveRangeCheckElimination.cpp
1617 IndVarSimplify.cpp
1718 JumpThreading.cpp
1819 LICM.cpp
20 LoopAccessAnalysisPrinter.cpp
1921 LoopSink.cpp
2022 LoadCombine.cpp
2123 LoopDeletion.cpp
2527 LoopInstSimplify.cpp
2628 LoopInterchange.cpp
2729 LoopLoadElimination.cpp
30 LoopPassManager.cpp
2831 LoopRerollPass.cpp
2932 LoopRotation.cpp
3033 LoopSimplifyCFG.cpp
0 //===- IVUsersPrinter.cpp - Induction Variable Users Printer ----*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/Transforms/Scalar/IVUsersPrinter.h"
10 #include "llvm/Analysis/IVUsers.h"
11 #include "llvm/Support/Debug.h"
12 using namespace llvm;
13
14 #define DEBUG_TYPE "iv-users"
15
16 PreservedAnalyses IVUsersPrinterPass::run(Loop &L, LoopAnalysisManager &AM,
17 LoopStandardAnalysisResults &AR,
18 LPMUpdater &U) {
19 AM.getResult(L, AR).print(OS);
20 return PreservedAnalyses::all();
21 }
2424 //===----------------------------------------------------------------------===//
2525
2626 #include "llvm/Transforms/Scalar/IndVarSimplify.h"
27 #include "llvm/Transforms/Scalar.h"
2827 #include "llvm/ADT/SmallVector.h"
2928 #include "llvm/ADT/Statistic.h"
3029 #include "llvm/Analysis/GlobalsModRef.h"
3130 #include "llvm/Analysis/LoopInfo.h"
3231 #include "llvm/Analysis/LoopPass.h"
33 #include "llvm/Analysis/LoopPassManager.h"
32 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
3433 #include "llvm/Analysis/ScalarEvolutionExpander.h"
35 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
3634 #include "llvm/Analysis/TargetLibraryInfo.h"
3735 #include "llvm/Analysis/TargetTransformInfo.h"
3836 #include "llvm/IR/BasicBlock.h"
4846 #include "llvm/Support/CommandLine.h"
4947 #include "llvm/Support/Debug.h"
5048 #include "llvm/Support/raw_ostream.h"
49 #include "llvm/Transforms/Scalar.h"
50 #include "llvm/Transforms/Scalar/LoopPassManager.h"
5151 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
5252 #include "llvm/Transforms/Utils/Local.h"
5353 #include "llvm/Transforms/Utils/LoopUtils.h"
4040 #include "llvm/Analysis/Loads.h"
4141 #include "llvm/Analysis/LoopInfo.h"
4242 #include "llvm/Analysis/LoopPass.h"
43 #include "llvm/Analysis/LoopPassManager.h"
4443 #include "llvm/Analysis/MemoryBuiltins.h"
4544 #include "llvm/Analysis/OptimizationDiagnosticInfo.h"
4645 #include "llvm/Analysis/ScalarEvolution.h"
6160 #include "llvm/Support/Debug.h"
6261 #include "llvm/Support/raw_ostream.h"
6362 #include "llvm/Transforms/Scalar.h"
63 #include "llvm/Transforms/Scalar/LoopPassManager.h"
6464 #include "llvm/Transforms/Utils/Local.h"
6565 #include "llvm/Transforms/Utils/LoopUtils.h"
6666 #include "llvm/Transforms/Utils/SSAUpdater.h"
0 //===- LoopAccessAnalysisPrinter.cpp - Loop Access Analysis Printer --------==//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/Transforms/Scalar/LoopAccessAnalysisPrinter.h"
10 #include "llvm/Analysis/LoopAccessAnalysis.h"
11 using namespace llvm;
12
13 #define DEBUG_TYPE "loop-accesses"
14
15 PreservedAnalyses
16 LoopAccessInfoPrinterPass::run(Loop &L, LoopAnalysisManager &AM,
17 LoopStandardAnalysisResults &AR, LPMUpdater &) {
18 Function &F = *L.getHeader()->getParent();
19 auto &LAI = AM.getResult(L, AR);
20 OS << "Loop access info in function '" << F.getName() << "':\n";
21 OS.indent(2) << L.getHeader()->getName() << ":\n";
22 LAI.print(OS, 4);
23 return PreservedAnalyses::all();
24 }
1818 #include "llvm/ADT/Statistic.h"
1919 #include "llvm/Analysis/GlobalsModRef.h"
2020 #include "llvm/Analysis/LoopPass.h"
21 #include "llvm/Analysis/LoopPassManager.h"
2221 #include "llvm/IR/Dominators.h"
2322 #include "llvm/Transforms/Scalar.h"
23 #include "llvm/Transforms/Scalar/LoopPassManager.h"
2424 #include "llvm/Transforms/Utils/LoopUtils.h"
2525 using namespace llvm;
2626
3030 #include "llvm/Analysis/GlobalsModRef.h"
3131 #include "llvm/Analysis/LoopAccessAnalysis.h"
3232 #include "llvm/Analysis/LoopInfo.h"
33 #include "llvm/Analysis/LoopPassManager.h"
3433 #include "llvm/Analysis/OptimizationDiagnosticInfo.h"
3534 #include "llvm/IR/DiagnosticInfo.h"
3635 #include "llvm/IR/Dominators.h"
3736 #include "llvm/Pass.h"
3837 #include "llvm/Support/CommandLine.h"
3938 #include "llvm/Support/Debug.h"
39 #include "llvm/Transforms/Scalar/LoopPassManager.h"
4040 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
4141 #include "llvm/Transforms/Utils/Cloning.h"
4242 #include "llvm/Transforms/Utils/LoopUtils.h"
4545 #include "llvm/Analysis/GlobalsModRef.h"
4646 #include "llvm/Analysis/LoopAccessAnalysis.h"
4747 #include "llvm/Analysis/LoopPass.h"
48 #include "llvm/Analysis/LoopPassManager.h"
4948 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
5049 #include "llvm/Analysis/ScalarEvolutionExpander.h"
5150 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
6059 #include "llvm/Support/Debug.h"
6160 #include "llvm/Support/raw_ostream.h"
6261 #include "llvm/Transforms/Scalar.h"
62 #include "llvm/Transforms/Scalar/LoopPassManager.h"
6363 #include "llvm/Transforms/Utils/BuildLibCalls.h"
6464 #include "llvm/Transforms/Utils/Local.h"
6565 #include "llvm/Transforms/Utils/LoopUtils.h"
1717 #include "llvm/Analysis/InstructionSimplify.h"
1818 #include "llvm/Analysis/LoopInfo.h"
1919 #include "llvm/Analysis/LoopPass.h"
20 #include "llvm/Analysis/LoopPassManager.h"
2120 #include "llvm/Analysis/ScalarEvolution.h"
2221 #include "llvm/Analysis/TargetLibraryInfo.h"
2322 #include "llvm/IR/DataLayout.h"
2524 #include "llvm/IR/Instructions.h"
2625 #include "llvm/Support/Debug.h"
2726 #include "llvm/Transforms/Scalar.h"
27 #include "llvm/Transforms/Scalar/LoopPassManager.h"
2828 #include "llvm/Transforms/Utils/Local.h"
2929 #include "llvm/Transforms/Utils/LoopUtils.h"
3030 using namespace llvm;
0 //===- LoopPassManager.cpp - Loop pass management -------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/Transforms/Scalar/LoopPassManager.h"
10 #include "llvm/Analysis/LoopInfo.h"
11
12 using namespace llvm;
13
14 // Explicit template instantiations and specialization defininitions for core
15 // template typedefs.
16 namespace llvm {
17 template class PassManager
18 LoopStandardAnalysisResults &, LPMUpdater &>;
19
20 /// Explicitly specialize the pass manager's run method to handle loop nest
21 /// structure updates.
22 template <>
23 PreservedAnalyses
24 PassManager
25 LPMUpdater &>::run(Loop &L, LoopAnalysisManager &AM,
26 LoopStandardAnalysisResults &AR, LPMUpdater &U) {
27 PreservedAnalyses PA = PreservedAnalyses::all();
28
29 if (DebugLogging)
30 dbgs() << "Starting Loop pass manager run.\n";
31
32 for (auto &Pass : Passes) {
33 if (DebugLogging)
34 dbgs() << "Running pass: " << Pass->name() << " on " << L;
35
36 PreservedAnalyses PassPA = Pass->run(L, AM, AR, U);
37
38 // If the loop was deleted, abort the run and return to the outer walk.
39 if (U.skipCurrentLoop()) {
40 PA.intersect(std::move(PassPA));
41 break;
42 }
43
44 // Update the analysis manager as each pass runs and potentially
45 // invalidates analyses.
46 AM.invalidate(L, PassPA);
47
48 // Finally, we intersect the final preserved analyses to compute the
49 // aggregate preserved set for this pass manager.
50 PA.intersect(std::move(PassPA));
51
52 // FIXME: Historically, the pass managers all called the LLVM context's
53 // yield function here. We don't have a generic way to acquire the
54 // context and it isn't yet clear what the right pattern is for yielding
55 // in the new pass manager so it is currently omitted.
56 // ...getContext().yield();
57 }
58
59 // Invalidation for the current loop should be handled above, and other loop
60 // analysis results shouldn't be impacted by runs over this loop. Therefore,
61 // the remaining analysis results in the AnalysisManager are preserved. We
62 // mark this with a set so that we don't need to inspect each one
63 // individually.
64 // FIXME: This isn't correct! This loop and all nested loops' analyses should
65 // be preserved, but unrolling should invalidate the parent loop's analyses.
66 PA.preserveSet>();
67
68 if (DebugLogging)
69 dbgs() << "Finished Loop pass manager run.\n";
70
71 return PA;
72 }
73 }
74
75 PrintLoopPass::PrintLoopPass() : OS(dbgs()) {}
76 PrintLoopPass::PrintLoopPass(raw_ostream &OS, const std::string &Banner)
77 : OS(OS), Banner(Banner) {}
78
79 PreservedAnalyses PrintLoopPass::run(Loop &L, LoopAnalysisManager &,
80 LoopStandardAnalysisResults &,
81 LPMUpdater &) {
82 printLoop(L, OS, Banner);
83 return PreservedAnalyses::all();
84 }
1313 #include "llvm/Transforms/Scalar/LoopRotation.h"
1414 #include "llvm/ADT/Statistic.h"
1515 #include "llvm/Analysis/AliasAnalysis.h"
16 #include "llvm/Analysis/AssumptionCache.h"
1617 #include "llvm/Analysis/BasicAliasAnalysis.h"
17 #include "llvm/Analysis/AssumptionCache.h"
1818 #include "llvm/Analysis/CodeMetrics.h"
19 #include "llvm/Analysis/GlobalsModRef.h"
1920 #include "llvm/Analysis/InstructionSimplify.h"
20 #include "llvm/Analysis/GlobalsModRef.h"
2121 #include "llvm/Analysis/LoopPass.h"
22 #include "llvm/Analysis/LoopPassManager.h"
2322 #include "llvm/Analysis/ScalarEvolution.h"
2423 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
2524 #include "llvm/Analysis/TargetTransformInfo.h"
3332 #include "llvm/Support/Debug.h"
3433 #include "llvm/Support/raw_ostream.h"
3534 #include "llvm/Transforms/Scalar.h"
35 #include "llvm/Transforms/Scalar/LoopPassManager.h"
3636 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
3737 #include "llvm/Transforms/Utils/Local.h"
3838 #include "llvm/Transforms/Utils/LoopUtils.h"
1717 #include "llvm/ADT/SmallVector.h"
1818 #include "llvm/ADT/Statistic.h"
1919 #include "llvm/Analysis/AliasAnalysis.h"
20 #include "llvm/Analysis/AssumptionCache.h"
2021 #include "llvm/Analysis/BasicAliasAnalysis.h"
21 #include "llvm/Analysis/AssumptionCache.h"
2222 #include "llvm/Analysis/DependenceAnalysis.h"
2323 #include "llvm/Analysis/GlobalsModRef.h"
2424 #include "llvm/Analysis/LoopInfo.h"
2525 #include "llvm/Analysis/LoopPass.h"
26 #include "llvm/Analysis/LoopPassManager.h"
2726 #include "llvm/Analysis/ScalarEvolution.h"
2827 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
2928 #include "llvm/Analysis/TargetTransformInfo.h"
3029 #include "llvm/IR/Dominators.h"
3130 #include "llvm/Transforms/Scalar.h"
31 #include "llvm/Transforms/Scalar/LoopPassManager.h"
3232 #include "llvm/Transforms/Utils/Local.h"
3333 #include "llvm/Transforms/Utils/LoopUtils.h"
3434 using namespace llvm;
3737 #include "llvm/Analysis/Loads.h"
3838 #include "llvm/Analysis/LoopInfo.h"
3939 #include "llvm/Analysis/LoopPass.h"
40 #include "llvm/Analysis/LoopPassManager.h"
4140 #include "llvm/Analysis/ScalarEvolution.h"
4241 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
4342 #include "llvm/IR/Dominators.h"
4645 #include "llvm/IR/Metadata.h"
4746 #include "llvm/Support/CommandLine.h"
4847 #include "llvm/Transforms/Scalar.h"
48 #include "llvm/Transforms/Scalar/LoopPassManager.h"
4949 #include "llvm/Transforms/Utils/Local.h"
5050 #include "llvm/Transforms/Utils/LoopUtils.h"
5151 using namespace llvm;
5858 #include "llvm/ADT/DenseSet.h"
5959 #include "llvm/ADT/Hashing.h"
6060 #include "llvm/ADT/PointerIntPair.h"
61 #include "llvm/ADT/STLExtras.h"
6162 #include "llvm/ADT/SetVector.h"
6263 #include "llvm/ADT/SmallBitVector.h"
6364 #include "llvm/ADT/SmallPtrSet.h"
6465 #include "llvm/ADT/SmallSet.h"
6566 #include "llvm/ADT/SmallVector.h"
66 #include "llvm/ADT/STLExtras.h"
6767 #include "llvm/Analysis/IVUsers.h"
6868 #include "llvm/Analysis/LoopInfo.h"
6969 #include "llvm/Analysis/LoopPass.h"
70 #include "llvm/Analysis/LoopPassManager.h"
7170 #include "llvm/Analysis/ScalarEvolution.h"
7271 #include "llvm/Analysis/ScalarEvolutionExpander.h"
7372 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
7978 #include "llvm/IR/DerivedTypes.h"
8079 #include "llvm/IR/Dominators.h"
8180 #include "llvm/IR/GlobalValue.h"
81 #include "llvm/IR/IRBuilder.h"
8282 #include "llvm/IR/Instruction.h"
8383 #include "llvm/IR/Instructions.h"
8484 #include "llvm/IR/IntrinsicInst.h"
85 #include "llvm/IR/IRBuilder.h"
85 #include "llvm/IR/Module.h"
8686 #include "llvm/IR/OperandTraits.h"
8787 #include "llvm/IR/Operator.h"
88 #include "llvm/IR/Module.h"
8988 #include "llvm/IR/Type.h"
9089 #include "llvm/IR/Value.h"
9190 #include "llvm/IR/ValueHandle.h"
9897 #include "llvm/Support/MathExtras.h"
9998 #include "llvm/Support/raw_ostream.h"
10099 #include "llvm/Transforms/Scalar.h"
100 #include "llvm/Transforms/Scalar/LoopPassManager.h"
101101 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
102102 #include "llvm/Transforms/Utils/Local.h"
103103 #include
1818 #include "llvm/Analysis/GlobalsModRef.h"
1919 #include "llvm/Analysis/InstructionSimplify.h"
2020 #include "llvm/Analysis/LoopPass.h"
21 #include "llvm/Analysis/LoopPassManager.h"
2221 #include "llvm/Analysis/LoopUnrollAnalyzer.h"
2322 #include "llvm/Analysis/OptimizationDiagnosticInfo.h"
2423 #include "llvm/Analysis/ScalarEvolution.h"
3231 #include "llvm/Support/Debug.h"
3332 #include "llvm/Support/raw_ostream.h"
3433 #include "llvm/Transforms/Scalar.h"
34 #include "llvm/Transforms/Scalar/LoopPassManager.h"
3535 #include "llvm/Transforms/Utils/LoopUtils.h"
3636 #include "llvm/Transforms/Utils/UnrollLoop.h"
3737 #include
1616 #include "llvm/ADT/StringRef.h"
1717 #include "llvm/Analysis/AliasAnalysis.h"
1818 #include "llvm/Analysis/CGSCCPassManager.h"
19 #include "llvm/Analysis/LoopPassManager.h"
2019 #include "llvm/Bitcode/BitcodeWriterPass.h"
2120 #include "llvm/IR/Dominators.h"
2221 #include "llvm/IR/IRPrintingPasses.h"
2928 #include "llvm/Support/ErrorHandling.h"
3029 #include "llvm/Support/ToolOutputFile.h"
3130 #include "llvm/Target/TargetMachine.h"
31 #include "llvm/Transforms/Scalar/LoopPassManager.h"
3232
3333 using namespace llvm;
3434 using namespace opt_tool;
1212 CFGTest.cpp
1313 CGSCCPassManagerTest.cpp
1414 LazyCallGraphTest.cpp
15 LoopPassManagerTest.cpp
1615 MemoryBuiltinsTest.cpp
1716 ScalarEvolutionTest.cpp
1817 TBAATest.cpp
+0
-1438
unittests/Analysis/LoopPassManagerTest.cpp less more
None //===- llvm/unittest/Analysis/LoopPassManagerTest.cpp - LPM tests ---------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/Analysis/AliasAnalysis.h"
10 #include "llvm/Analysis/AssumptionCache.h"
11 #include "llvm/Analysis/LoopPassManager.h"
12 #include "llvm/Analysis/ScalarEvolution.h"
13 #include "llvm/Analysis/TargetLibraryInfo.h"
14 #include "llvm/Analysis/TargetTransformInfo.h"
15 #include "llvm/AsmParser/Parser.h"
16 #include "llvm/IR/Dominators.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/LLVMContext.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/IR/PassManager.h"
21 #include "llvm/Support/SourceMgr.h"
22 #include "gmock/gmock.h"
23 #include "gtest/gtest.h"
24
25 using namespace llvm;
26
27 namespace {
28
29 using testing::DoDefault;
30 using testing::Return;
31 using testing::Expectation;
32 using testing::Invoke;
33 using testing::InvokeWithoutArgs;
34 using testing::_;
35
36 template
37 typename AnalysisManagerT = AnalysisManager,
38 typename... ExtraArgTs>
39 class MockAnalysisHandleBase {
40 public:
41 class Analysis : public AnalysisInfoMixin {
42 friend AnalysisInfoMixin;
43 friend MockAnalysisHandleBase;
44 static AnalysisKey Key;
45
46 DerivedT *Handle;
47
48 Analysis(DerivedT &Handle) : Handle(&Handle) {}
49
50 public:
51 class Result {
52 friend MockAnalysisHandleBase;
53
54 DerivedT *Handle;
55
56 Result(DerivedT &Handle) : Handle(&Handle) {}
57
58 public:
59 // Forward invalidation events to the mock handle.
60 bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA,
61 typename AnalysisManagerT::Invalidator &Inv) {
62 return Handle->invalidate(IR, PA, Inv);
63 }
64 };
65
66 Result run(IRUnitT &IR, AnalysisManagerT &AM, ExtraArgTs... ExtraArgs) {
67 return Handle->run(IR, AM, ExtraArgs...);
68 }
69 };
70
71 Analysis getAnalysis() { return Analysis(static_cast(*this)); }
72 typename Analysis::Result getResult() {
73 return typename Analysis::Result(static_cast(*this));
74 }
75
76 protected:
77 // FIXME: MSVC seems unable to handle a lambda argument to Invoke from within
78 // the template, so we use a boring static function.
79 static bool invalidateCallback(IRUnitT &IR, const PreservedAnalyses &PA,
80 typename AnalysisManagerT::Invalidator &Inv) {
81 auto PAC = PA.template getChecker();
82 return !PAC.preserved() &&
83 !PAC.template preservedSet>();
84 }
85
86 /// Derived classes should call this in their constructor to set up default
87 /// mock actions. (We can't do this in our constructor because this has to
88 /// run after the DerivedT is constructed.)
89 void setDefaults() {
90 ON_CALL(static_cast(*this),
91 run(_, _, testing::Matcher(_)...))
92 .WillByDefault(Return(this->getResult()));
93 ON_CALL(static_cast(*this), invalidate(_, _, _))
94 .WillByDefault(Invoke(&invalidateCallback));
95 }
96 };
97
98 template
99 typename... ExtraArgTs>
100 AnalysisKey MockAnalysisHandleBase
101 ExtraArgTs...>::Analysis::Key;
102
103 /// Mock handle for loop analyses.
104 ///
105 /// This is provided as a template accepting an (optional) integer. Because
106 /// analyses are identified and queried by type, this allows constructing
107 /// multiple handles with distinctly typed nested 'Analysis' types that can be
108 /// registered and queried. If you want to register multiple loop analysis
109 /// passes, you'll need to instantiate this type with different values for I.
110 /// For example:
111 ///
112 /// MockLoopAnalysisHandleTemplate<0> h0;
113 /// MockLoopAnalysisHandleTemplate<1> h1;
114 /// typedef decltype(h0)::Analysis Analysis0;
115 /// typedef decltype(h1)::Analysis Analysis1;
116 template (-1)>
117 struct MockLoopAnalysisHandleTemplate
118 : MockAnalysisHandleBase, Loop,
119 LoopAnalysisManager,
120 LoopStandardAnalysisResults &> {
121 typedef typename MockLoopAnalysisHandleTemplate::Analysis Analysis;
122
123 MOCK_METHOD3_T(run, typename Analysis::Result(Loop &, LoopAnalysisManager &,
124 LoopStandardAnalysisResults &));
125
126 MOCK_METHOD3_T(invalidate, bool(Loop &, const PreservedAnalyses &,
127 LoopAnalysisManager::Invalidator &));
128
129 MockLoopAnalysisHandleTemplate() { this->setDefaults(); }
130 };
131
132 typedef MockLoopAnalysisHandleTemplate<> MockLoopAnalysisHandle;
133
134 struct MockFunctionAnalysisHandle
135 : MockAnalysisHandleBase {
136 MOCK_METHOD2(run, Analysis::Result(Function &, FunctionAnalysisManager &));
137
138 MOCK_METHOD3(invalidate, bool(Function &, const PreservedAnalyses &,
139 FunctionAnalysisManager::Invalidator &));
140
141 MockFunctionAnalysisHandle() { setDefaults(); }
142 };
143
144 template
145 typename AnalysisManagerT = AnalysisManager,
146 typename... ExtraArgTs>
147 class MockPassHandleBase {
148 public:
149 class Pass : public PassInfoMixin {
150 friend MockPassHandleBase;
151
152 DerivedT *Handle;
153
154 Pass(DerivedT &Handle) : Handle(&Handle) {}
155
156 public:
157 PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM,
158 ExtraArgTs... ExtraArgs) {
159 return Handle->run(IR, AM, ExtraArgs...);
160 }
161 };
162
163 Pass getPass() { return Pass(static_cast(*this)); }
164
165 protected:
166 /// Derived classes should call this in their constructor to set up default
167 /// mock actions. (We can't do this in our constructor because this has to
168 /// run after the DerivedT is constructed.)
169 void setDefaults() {
170 ON_CALL(static_cast(*this),
171 run(_, _, testing::Matcher(_)...))
172 .WillByDefault(Return(PreservedAnalyses::all()));
173 }
174 };
175
176 struct MockLoopPassHandle
177 : MockPassHandleBase
178 LoopStandardAnalysisResults &, LPMUpdater &> {
179 MOCK_METHOD4(run,
180 PreservedAnalyses(Loop &, LoopAnalysisManager &,
181 LoopStandardAnalysisResults &, LPMUpdater &));
182 MockLoopPassHandle() { setDefaults(); }
183 };
184
185 struct MockFunctionPassHandle
186 : MockPassHandleBase {
187 MOCK_METHOD2(run, PreservedAnalyses(Function &, FunctionAnalysisManager &));
188
189 MockFunctionPassHandle() { setDefaults(); }
190 };
191
192 struct MockModulePassHandle : MockPassHandleBase {
193 MOCK_METHOD2(run, PreservedAnalyses(Module &, ModuleAnalysisManager &));
194
195 MockModulePassHandle() { setDefaults(); }
196 };
197
198 /// Define a custom matcher for objects which support a 'getName' method
199 /// returning a StringRef.
200 ///
201 /// LLVM often has IR objects or analysis objects which expose a StringRef name
202 /// and in tests it is convenient to match these by name for readability. This
203 /// matcher supports any type exposing a getName() method of this form.
204 ///
205 /// It should be used as:
206 ///
207 /// HasName("my_function")
208 ///
209 /// No namespace or other qualification is required.
210 MATCHER_P(HasName, Name, "") {
211 // The matcher's name and argument are printed in the case of failure, but we
212 // also want to print out the name of the argument. This uses an implicitly
213 // avaiable std::ostream, so we have to construct a std::string.
214 *result_listener << "has name '" << arg.getName().str() << "'";
215 return Name == arg.getName();
216 }
217
218 std::unique_ptr parseIR(LLVMContext &C, const char *IR) {
219 SMDiagnostic Err;
220 return parseAssemblyString(IR, Err, C);
221 }
222
223 class LoopPassManagerTest : public ::testing::Test {
224 protected:
225 LLVMContext Context;
226 std::unique_ptr M;
227
228 LoopAnalysisManager LAM;
229 FunctionAnalysisManager FAM;
230 ModuleAnalysisManager MAM;
231
232 MockLoopAnalysisHandle MLAHandle;
233 MockLoopPassHandle MLPHandle;
234 MockFunctionPassHandle MFPHandle;
235 MockModulePassHandle MMPHandle;
236
237 static PreservedAnalyses
238 getLoopAnalysisResult(Loop &L, LoopAnalysisManager &AM,
239 LoopStandardAnalysisResults &AR, LPMUpdater &) {
240 (void)AM.getResult(L, AR);
241 return PreservedAnalyses::all();
242 };
243
244 public:
245 LoopPassManagerTest()
246 : M(parseIR(Context, "define void @f() {\n"
247 "entry:\n"
248 " br label %loop.0\n"
249 "loop.0:\n"
250 " br i1 undef, label %loop.0.0, label %end\n"
251 "loop.0.0:\n"
252 " br i1 undef, label %loop.0.0, label %loop.0.1\n"
253 "loop.0.1:\n"
254 " br i1 undef, label %loop.0.1, label %loop.0\n"
255 "end:\n"
256 " ret void\n"
257 "}\n"
258 "\n"
259 "define void @g() {\n"
260 "entry:\n"
261 " br label %loop.g.0\n"
262 "loop.g.0:\n"
263 " br i1 undef, label %loop.g.0, label %end\n"
264 "end:\n"
265 " ret void\n"
266 "}\n")),
267 LAM(true), FAM(true), MAM(true) {
268 // Register our mock analysis.
269 LAM.registerPass([&] { return MLAHandle.getAnalysis(); });
270
271 // We need DominatorTreeAnalysis for LoopAnalysis.
272 FAM.registerPass([&] { return DominatorTreeAnalysis(); });
273 FAM.registerPass([&] { return LoopAnalysis(); });
274 // We also allow loop passes to assume a set of other analyses and so need
275 // those.
276 FAM.registerPass([&] { return AAManager(); });
277 FAM.registerPass([&] { return AssumptionAnalysis(); });
278 FAM.registerPass([&] { return ScalarEvolutionAnalysis(); });
279 FAM.registerPass([&] { return TargetLibraryAnalysis(); });
280 FAM.registerPass([&] { return TargetIRAnalysis(); });
281
282 // Cross-register proxies.
283 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
284 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
285 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
286 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
287 }
288 };
289
290 TEST_F(LoopPassManagerTest, Basic) {
291 ModulePassManager MPM(true);
292 ::testing::InSequence MakeExpectationsSequenced;
293
294 // First we just visit all the loops in all the functions and get their
295 // analysis results. This will run the analysis a total of four times,
296 // once for each loop.
297 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
298 .WillOnce(Invoke(getLoopAnalysisResult));
299 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
300 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _))
301 .WillOnce(Invoke(getLoopAnalysisResult));
302 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
303 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _))
304 .WillOnce(Invoke(getLoopAnalysisResult));
305 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
306 EXPECT_CALL(MLPHandle, run(HasName("loop.g.0"), _, _, _))
307 .WillOnce(Invoke(getLoopAnalysisResult));
308 EXPECT_CALL(MLAHandle, run(HasName("loop.g.0"), _, _));
309 // Wire the loop pass through pass managers into the module pipeline.
310 {
311 LoopPassManager LPM(true);
312 LPM.addPass(MLPHandle.getPass());
313 FunctionPassManager FPM(true);
314 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
315 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
316 }
317
318 // Next we run two passes over the loops. The first one invalidates the
319 // analyses for one loop, the second ones try to get the analysis results.
320 // This should force only one analysis to re-run within the loop PM, but will
321 // also invalidate everything after the loop pass manager finishes.
322 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
323 .WillOnce(DoDefault())
324 .WillOnce(Invoke(getLoopAnalysisResult));
325 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _))
326 .WillOnce(InvokeWithoutArgs([] { return PreservedAnalyses::none(); }))
327 .WillOnce(Invoke(getLoopAnalysisResult));
328 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
329 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _))
330 .WillOnce(DoDefault())
331 .WillOnce(Invoke(getLoopAnalysisResult));
332 EXPECT_CALL(MLPHandle, run(HasName("loop.g.0"), _, _, _))
333 .WillOnce(DoDefault())
334 .WillOnce(Invoke(getLoopAnalysisResult));
335 // Wire two loop pass runs into the module pipeline.
336 {
337 LoopPassManager LPM(true);
338 LPM.addPass(MLPHandle.getPass());
339 LPM.addPass(MLPHandle.getPass());
340 FunctionPassManager FPM(true);
341 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
342 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
343 }
344
345 // And now run the pipeline across the module.
346 MPM.run(*M, MAM);
347 }
348
349 TEST_F(LoopPassManagerTest, FunctionPassInvalidationOfLoopAnalyses) {
350 ModulePassManager MPM(true);
351 FunctionPassManager FPM(true);
352 // We process each function completely in sequence.
353 ::testing::Sequence FSequence, GSequence;
354
355 // First, force the analysis result to be computed for each loop.
356 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _))
357 .InSequence(FSequence)
358 .WillOnce(DoDefault());
359 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _))
360 .InSequence(FSequence)
361 .WillOnce(DoDefault());
362 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _))
363 .InSequence(FSequence)
364 .WillOnce(DoDefault());
365 EXPECT_CALL(MLAHandle, run(HasName("loop.g.0"), _, _))
366 .InSequence(GSequence)
367 .WillOnce(DoDefault());
368 FPM.addPass(createFunctionToLoopPassAdaptor(
369 RequireAnalysisLoopPass()));
370
371 // No need to re-run if we require again from a fresh loop pass manager.
372 FPM.addPass(createFunctionToLoopPassAdaptor(
373 RequireAnalysisLoopPass()));
374
375 // For 'f', preserve most things but not the specific loop analyses.
376 EXPECT_CALL(MFPHandle, run(HasName("f"), _))
377 .InSequence(FSequence)
378 .WillOnce(Return(getLoopPassPreservedAnalyses()));
379 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.0.0"), _, _))
380 .InSequence(FSequence)
381 .WillOnce(DoDefault());
382 // On one loop, skip the invalidation (as though we did an internal update).
383 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.0.1"), _, _))
384 .InSequence(FSequence)
385 .WillOnce(Return(false));
386 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.0"), _, _))
387 .InSequence(FSequence)
388 .WillOnce(DoDefault());
389 // Now two loops still have to be recomputed.
390 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _))
391 .InSequence(FSequence)
392 .WillOnce(DoDefault());
393 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _))
394 .InSequence(FSequence)
395 .WillOnce(DoDefault());
396 // Preserve things in the second function to ensure invalidation remains
397 // isolated to one function.
398 EXPECT_CALL(MFPHandle, run(HasName("g"), _))
399 .InSequence(GSequence)
400 .WillOnce(DoDefault());
401 FPM.addPass(MFPHandle.getPass());
402 FPM.addPass(createFunctionToLoopPassAdaptor(
403 RequireAnalysisLoopPass()));
404
405 EXPECT_CALL(MFPHandle, run(HasName("f"), _))
406 .InSequence(FSequence)
407 .WillOnce(DoDefault());
408 // For 'g', fail to preserve anything, causing the loops themselves to be
409 // cleared. We don't get an invalidation event here as the loop is gone, but
410 // we should still have to recompute the analysis.
411 EXPECT_CALL(MFPHandle, run(HasName("g"), _))
412 .InSequence(GSequence)
413 .WillOnce(Return(PreservedAnalyses::none()));
414 EXPECT_CALL(MLAHandle, run(HasName("loop.g.0"), _, _))
415 .InSequence(GSequence)
416 .WillOnce(DoDefault());
417 FPM.addPass(MFPHandle.getPass());
418 FPM.addPass(createFunctionToLoopPassAdaptor(
419 RequireAnalysisLoopPass()));
420
421 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
422
423 // Verify with a separate function pass run that we didn't mess up 'f's
424 // cache. No analysis runs should be necessary here.
425 MPM.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor(
426 RequireAnalysisLoopPass())));
427
428 MPM.run(*M, MAM);
429 }
430
431 TEST_F(LoopPassManagerTest, ModulePassInvalidationOfLoopAnalyses) {
432 ModulePassManager MPM(true);
433 ::testing::InSequence MakeExpectationsSequenced;
434
435 // First, force the analysis result to be computed for each loop.
436 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
437 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
438 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
439 EXPECT_CALL(MLAHandle, run(HasName("loop.g.0"), _, _));
440 MPM.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor(
441 RequireAnalysisLoopPass())));
442
443 // Walking all the way out and all the way back in doesn't re-run the
444 // analysis.
445 MPM.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor(
446 RequireAnalysisLoopPass())));
447
448 // But a module pass that doesn't preserve the actual mock loop analysis
449 // invalidates all the way down and forces recomputing.
450 EXPECT_CALL(MMPHandle, run(_, _)).WillOnce(InvokeWithoutArgs([] {
451 auto PA = getLoopPassPreservedAnalyses();
452 PA.preserve();
453 return PA;
454 }));
455 // All the loop analyses from both functions get invalidated before we
456 // recompute anything.
457 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.0.0"), _, _));
458 // On one loop, again skip the invalidation (as though we did an internal
459 // update).
460 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.0.1"), _, _))
461 .WillOnce(Return(false));
462 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.0"), _, _));
463 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.g.0"), _, _));
464 // Now all but one of the loops gets re-analyzed.
465 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
466 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
467 EXPECT_CALL(MLAHandle, run(HasName("loop.g.0"), _, _));
468 MPM.addPass(MMPHandle.getPass());
469 MPM.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor(
470 RequireAnalysisLoopPass())));
471
472 // Verify that the cached values persist.
473 MPM.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor(
474 RequireAnalysisLoopPass())));
475
476 // Now we fail to preserve the loop analysis and observe that the loop
477 // analyses are cleared (so no invalidation event) as the loops themselves
478 // are no longer valid.
479 EXPECT_CALL(MMPHandle, run(_, _)).WillOnce(InvokeWithoutArgs([] {
480 auto PA = PreservedAnalyses::none();
481 PA.preserve();
482 return PA;
483 }));
484 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
485 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
486 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
487 EXPECT_CALL(MLAHandle, run(HasName("loop.g.0"), _, _));
488 MPM.addPass(MMPHandle.getPass());
489 MPM.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor(
490 RequireAnalysisLoopPass())));
491
492 // Verify that the cached values persist.
493 MPM.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor(
494 RequireAnalysisLoopPass())));
495
496 // Next, check that even if we preserve everything within the function itelf,
497 // if the function's module pass proxy isn't preserved and the potential set
498 // of functions changes, the clear reaches the loop analyses as well. This
499 // will again trigger re-runs but not invalidation events.
500 EXPECT_CALL(MMPHandle, run(_, _)).WillOnce(InvokeWithoutArgs([] {
501 auto PA = PreservedAnalyses::none();
502 PA.preserveSet>();
503 PA.preserveSet>();
504 return PA;
505 }));
506 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
507 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
508 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
509 EXPECT_CALL(MLAHandle, run(HasName("loop.g.0"), _, _));
510 MPM.addPass(MMPHandle.getPass());
511 MPM.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor(
512 RequireAnalysisLoopPass())));
513
514 MPM.run(*M, MAM);
515 }
516
517 // Test that if any of the bundled analyses provided in the LPM's signature
518 // become invalid, the analysis proxy itself becomes invalid and we clear all
519 // loop analysis results.
520 TEST_F(LoopPassManagerTest, InvalidationOfBundledAnalyses) {
521 ModulePassManager MPM(true);
522 FunctionPassManager FPM(true);
523 ::testing::InSequence MakeExpectationsSequenced;
524
525 // First, force the analysis result to be computed for each loop.
526 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
527 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
528 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
529 FPM.addPass(createFunctionToLoopPassAdaptor(
530 RequireAnalysisLoopPass()));
531
532 // No need to re-run if we require again from a fresh loop pass manager.
533 FPM.addPass(createFunctionToLoopPassAdaptor(
534 RequireAnalysisLoopPass()));
535
536 // Preserving everything but the loop analyses themselves results in
537 // invalidation and running.
538 EXPECT_CALL(MFPHandle, run(HasName("f"), _))
539 .WillOnce(Return(getLoopPassPreservedAnalyses()));
540 EXPECT_CALL(MLAHandle, invalidate(_, _, _)).Times(3);
541 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
542 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
543 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
544 FPM.addPass(MFPHandle.getPass());
545 FPM.addPass(createFunctionToLoopPassAdaptor(
546 RequireAnalysisLoopPass()));
547
548 // The rest don't invalidate analyses, they only trigger re-runs because we
549 // clear the cache completely.
550 EXPECT_CALL(MFPHandle, run(HasName("f"), _)).WillOnce(InvokeWithoutArgs([] {
551 auto PA = PreservedAnalyses::none();
552 // Not preserving `AAManager`.
553 PA.preserve();
554 PA.preserve();
555 PA.preserve();
556 PA.preserve();
557 PA.preserve();
558 return PA;
559 }));
560 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
561 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
562 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
563 FPM.addPass(MFPHandle.getPass());
564 FPM.addPass(createFunctionToLoopPassAdaptor(
565 RequireAnalysisLoopPass()));
566
567 EXPECT_CALL(MFPHandle, run(HasName("f"), _)).WillOnce(InvokeWithoutArgs([] {
568 auto PA = PreservedAnalyses::none();
569 PA.preserve();
570 // Not preserving `AssumptionAnalysis`.
571 PA.preserve();
572 PA.preserve();
573 PA.preserve();
574 PA.preserve();
575 return PA;
576 }));
577 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
578 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
579 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
580 FPM.addPass(MFPHandle.getPass());
581 FPM.addPass(createFunctionToLoopPassAdaptor(
582 RequireAnalysisLoopPass()));
583
584 EXPECT_CALL(MFPHandle, run(HasName("f"), _)).WillOnce(InvokeWithoutArgs([] {
585 auto PA = PreservedAnalyses::none();
586 PA.preserve();
587 PA.preserve();
588 // Not preserving `DominatorTreeAnalysis`.
589 PA.preserve();
590 PA.preserve();
591 PA.preserve();
592 return PA;
593 }));
594 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
595 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
596 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
597 FPM.addPass(MFPHandle.getPass());
598 FPM.addPass(createFunctionToLoopPassAdaptor(
599 RequireAnalysisLoopPass()));
600
601 EXPECT_CALL(MFPHandle, run(HasName("f"), _)).WillOnce(InvokeWithoutArgs([] {
602 auto PA = PreservedAnalyses::none();
603 PA.preserve();
604 PA.preserve();
605 PA.preserve();
606 // Not preserving the `LoopAnalysis`.
607 PA.preserve();
608 PA.preserve();
609 return PA;
610 }));
611 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
612 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
613 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
614 FPM.addPass(MFPHandle.getPass());
615 FPM.addPass(createFunctionToLoopPassAdaptor(
616 RequireAnalysisLoopPass()));
617
618 EXPECT_CALL(MFPHandle, run(HasName("f"), _)).WillOnce(InvokeWithoutArgs([] {
619 auto PA = PreservedAnalyses::none();
620 PA.preserve();
621 PA.preserve();
622 PA.preserve();
623 PA.preserve();
624 // Not preserving the `LoopAnalysisManagerFunctionProxy`.
625 PA.preserve();
626 return PA;
627 }));
628 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
629 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
630 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
631 FPM.addPass(MFPHandle.getPass());
632 FPM.addPass(createFunctionToLoopPassAdaptor(
633 RequireAnalysisLoopPass()));
634
635 EXPECT_CALL(MFPHandle, run(HasName("f"), _)).WillOnce(InvokeWithoutArgs([] {
636 auto PA = PreservedAnalyses::none();
637 PA.preserve();
638 PA.preserve();
639 PA.preserve();
640 PA.preserve();
641 PA.preserve();
642 // Not preserving `ScalarEvolutionAnalysis`.
643 return PA;
644 }));
645 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
646 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
647 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
648 FPM.addPass(MFPHandle.getPass());
649 FPM.addPass(createFunctionToLoopPassAdaptor(
650 RequireAnalysisLoopPass()));
651
652 // After all the churn on 'f', we'll compute the loop analysis results for
653 // 'g' once with a requires pass and then run our mock pass over g a bunch
654 // but just get cached results each time.
655 EXPECT_CALL(MLAHandle, run(HasName("loop.g.0"), _, _));
656 EXPECT_CALL(MFPHandle, run(HasName("g"), _)).Times(7);
657
658 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
659 MPM.run(*M, MAM);
660 }
661
662 TEST_F(LoopPassManagerTest, IndirectInvalidation) {
663 // We need two distinct analysis types and handles.
664 enum { A, B };
665 MockLoopAnalysisHandleTemplate MLAHandleA;
666 MockLoopAnalysisHandleTemplate MLAHandleB;
667 LAM.registerPass([&] { return MLAHandleA.getAnalysis(); });
668 LAM.registerPass([&] { return MLAHandleB.getAnalysis(); });
669 typedef decltype(MLAHandleA)::Analysis AnalysisA;
670 typedef decltype(MLAHandleB)::Analysis AnalysisB;
671
672 // Set up AnalysisA to depend on our AnalysisB. For testing purposes we just
673 // need to get the AnalysisB results in AnalysisA's run method and check if
674 // AnalysisB gets invalidated in AnalysisA's invalidate method.
675 ON_CALL(MLAHandleA, run(_, _, _))
676 .WillByDefault(Invoke([&](Loop &L, LoopAnalysisManager &AM,
677 LoopStandardAnalysisResults &AR) {
678 (void)AM.getResult(L, AR);
679 return MLAHandleA.getResult();
680 }));
681 ON_CALL(MLAHandleA, invalidate(_, _, _))
682 .WillByDefault(Invoke([](Loop &L, const PreservedAnalyses &PA,
683 LoopAnalysisManager::Invalidator &Inv) {
684 auto PAC = PA.getChecker();
685 return !(PAC.preserved() || PAC.preservedSet>()) ||
686 Inv.invalidate(L, PA);
687 }));
688
689 ::testing::InSequence MakeExpectationsSequenced;
690
691 // Compute the analyses across all of 'f' first.
692 EXPECT_CALL(MLAHandleA, run(HasName("loop.0.0"), _, _));
693 EXPECT_CALL(MLAHandleB, run(HasName("loop.0.0"), _, _));
694 EXPECT_CALL(MLAHandleA, run(HasName("loop.0.1"), _, _));
695 EXPECT_CALL(MLAHandleB, run(HasName("loop.0.1"), _, _));
696 EXPECT_CALL(MLAHandleA, run(HasName("loop.0"), _, _));
697 EXPECT_CALL(MLAHandleB, run(HasName("loop.0"), _, _));
698
699 // Now we invalidate AnalysisB (but not AnalysisA) for one of the loops and
700 // preserve everything for the rest. This in turn triggers that one loop to
701 // recompute both AnalysisB *and* AnalysisA if indirect invalidation is
702 // working.
703 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
704 .WillOnce(InvokeWithoutArgs([] {
705 auto PA = getLoopPassPreservedAnalyses();
706 // Specifically preserve AnalysisA so that it would survive if it
707 // didn't depend on AnalysisB.
708 PA.preserve();
709 return PA;
710 }));
711 // It happens that AnalysisB is invalidated first. That shouldn't matter
712 // though, and we should still call AnalysisA's invalidation.
713 EXPECT_CALL(MLAHandleB, invalidate(HasName("loop.0.0"), _, _));
714 EXPECT_CALL(MLAHandleA, invalidate(HasName("loop.0.0"), _, _));
715 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
716 .WillOnce(Invoke([](Loop &L, LoopAnalysisManager &AM,
717 LoopStandardAnalysisResults &AR, LPMUpdater &) {
718 (void)AM.getResult(L, AR);
719 return PreservedAnalyses::all();
720 }));
721 EXPECT_CALL(MLAHandleA, run(HasName("loop.0.0"), _, _));
722 EXPECT_CALL(MLAHandleB, run(HasName("loop.0.0"), _, _));
723 // The rest of the loops should run and get cached results.
724 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _))
725 .Times(2)
726 .WillRepeatedly(Invoke([](Loop &L, LoopAnalysisManager &AM,
727 LoopStandardAnalysisResults &AR, LPMUpdater &) {
728 (void)AM.getResult(L, AR);
729 return PreservedAnalyses::all();
730 }));
731 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _))
732 .Times(2)
733 .WillRepeatedly(Invoke([](Loop &L, LoopAnalysisManager &AM,
734 LoopStandardAnalysisResults &AR, LPMUpdater &) {
735 (void)AM.getResult(L, AR);
736 return PreservedAnalyses::all();
737 }));
738
739 // The run over 'g' should be boring, with us just computing the analyses once
740 // up front and then running loop passes and getting cached results.
741 EXPECT_CALL(MLAHandleA, run(HasName("loop.g.0"), _, _));
742 EXPECT_CALL(MLAHandleB, run(HasName("loop.g.0"), _, _));
743 EXPECT_CALL(MLPHandle, run(HasName("loop.g.0"), _, _, _))
744 .Times(2)
745 .WillRepeatedly(Invoke([](Loop &L, LoopAnalysisManager &AM,
746 LoopStandardAnalysisResults &AR, LPMUpdater &) {
747 (void)AM.getResult(L, AR);
748 return PreservedAnalyses::all();
749 }));
750
751 // Build the pipeline and run it.
752 ModulePassManager MPM(true);
753 FunctionPassManager FPM(true);
754 FPM.addPass(
755 createFunctionToLoopPassAdaptor(RequireAnalysisLoopPass()));
756 LoopPassManager LPM(true);
757 LPM.addPass(MLPHandle.getPass());
758 LPM.addPass(MLPHandle.getPass());
759 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
760 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
761 MPM.run(*M, MAM);
762 }
763
764 TEST_F(LoopPassManagerTest, IndirectOuterPassInvalidation) {
765 typedef decltype(MLAHandle)::Analysis LoopAnalysis;
766
767 MockFunctionAnalysisHandle MFAHandle;
768 FAM.registerPass([&] { return MFAHandle.getAnalysis(); });
769 typedef decltype(MFAHandle)::Analysis FunctionAnalysis;
770
771 // Set up the loop analysis to depend on both the function and module
772 // analysis.
773 ON_CALL(MLAHandle, run(_, _, _))
774 .WillByDefault(Invoke([&](Loop &L, LoopAnalysisManager &AM,
775 LoopStandardAnalysisResults &AR) {
776 auto &FAMP = AM.getResult(L, AR);
777 auto &FAM = FAMP.getManager();
778 Function &F = *L.getHeader()->getParent();
779 if (auto *FA = FAM.getCachedResult(F))
780 FAMP.registerOuterAnalysisInvalidation
781 LoopAnalysis>();
782 return MLAHandle.getResult();
783 }));
784
785 ::testing::InSequence MakeExpectationsSequenced;
786
787 // Compute the analyses across all of 'f' first.
788 EXPECT_CALL(MFPHandle, run(HasName("f"), _))
789 .WillOnce(Invoke([](Function &F, FunctionAnalysisManager &AM) {
790 // Force the computing of the function analysis so it is available in
791 // this function.
792 (void)AM.getResult(F);
793 return PreservedAnalyses::all();
794 }));
795 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
796 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
797 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
798
799 // Now invalidate the function analysis but preserve the loop analyses.
800 // This should trigger immediate invalidation of the loop analyses, despite
801 // the fact that they were preserved.
802 EXPECT_CALL(MFPHandle, run(HasName("f"), _)).WillOnce(InvokeWithoutArgs([] {
803 auto PA = getLoopPassPreservedAnalyses();
804 PA.preserveSet>();
805 return PA;
806 }));
807 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.0.0"), _, _));
808 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.0.1"), _, _));
809 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.0"), _, _));
810
811 // And re-running a requires pass recomputes them.
812 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
813 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
814 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
815
816 // When we run over 'g' we don't populate the cache with the function
817 // analysis.
818 EXPECT_CALL(MFPHandle, run(HasName("g"), _))
819 .WillOnce(Return(PreservedAnalyses::all()));
820 EXPECT_CALL(MLAHandle, run(HasName("loop.g.0"), _, _));
821
822 // Which means that no extra invalidation occurs and cached values are used.
823 EXPECT_CALL(MFPHandle, run(HasName("g"), _)).WillOnce(InvokeWithoutArgs([] {
824 auto PA = getLoopPassPreservedAnalyses();
825 PA.preserveSet>();
826 return PA;
827 }));
828
829 // Build the pipeline and run it.
830 ModulePassManager MPM(true);
831 FunctionPassManager FPM(true);
832 FPM.addPass(MFPHandle.getPass());
833 FPM.addPass(
834 createFunctionToLoopPassAdaptor(RequireAnalysisLoopPass()));
835 FPM.addPass(MFPHandle.getPass());
836 FPM.addPass(
837 createFunctionToLoopPassAdaptor(RequireAnalysisLoopPass()));
838 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
839 MPM.run(*M, MAM);
840 }
841
842 TEST_F(LoopPassManagerTest, LoopChildInsertion) {
843 // Super boring module with three loops in a single loop nest.
844 M = parseIR(Context, "define void @f() {\n"
845 "entry:\n"
846 " br label %loop.0\n"
847 "loop.0:\n"
848 " br i1 undef, label %loop.0.0, label %end\n"
849 "loop.0.0:\n"
850 " br i1 undef, label %loop.0.0, label %loop.0.1\n"
851 "loop.0.1:\n"
852 " br i1 undef, label %loop.0.1, label %loop.0.2\n"
853 "loop.0.2:\n"
854 " br i1 undef, label %loop.0.2, label %loop.0\n"
855 "end:\n"
856 " ret void\n"
857 "}\n");
858
859 // Build up variables referring into the IR so we can rewrite it below
860 // easily.
861 Function &F = *M->begin();
862 ASSERT_THAT(F, HasName("f"));
863 auto BBI = F.begin();
864 BasicBlock &EntryBB = *BBI++;
865 ASSERT_THAT(EntryBB, HasName("entry"));
866 BasicBlock &Loop0BB = *BBI++;
867 ASSERT_THAT(Loop0BB, HasName("loop.0"));
868 BasicBlock &Loop00BB = *BBI++;
869 ASSERT_THAT(Loop00BB, HasName("loop.0.0"));
870 BasicBlock &Loop01BB = *BBI++;
871 ASSERT_THAT(Loop01BB, HasName("loop.0.1"));
872 BasicBlock &Loop02BB = *BBI++;
873 ASSERT_THAT(Loop02BB, HasName("loop.0.2"));
874 BasicBlock &EndBB = *BBI++;
875 ASSERT_THAT(EndBB, HasName("end"));
876 ASSERT_THAT(BBI, F.end());
877
878 // Build the pass managers and register our pipeline. We build a single loop
879 // pass pipeline consisting of three mock pass runs over each loop. After
880 // this we run both domtree and loop verification passes to make sure that
881 // the IR remained valid during our mutations.
882 ModulePassManager MPM(true);
883 FunctionPassManager FPM(true);
884 LoopPassManager LPM(true);
885 LPM.addPass(MLPHandle.getPass());
886 LPM.addPass(MLPHandle.getPass());
887 LPM.addPass(MLPHandle.getPass());
888 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
889 FPM.addPass(DominatorTreeVerifierPass());
890 FPM.addPass(LoopVerifierPass());
891 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
892
893 // All the visit orders are deterministic, so we use simple fully order
894 // expectations.
895 ::testing::InSequence MakeExpectationsSequenced;
896
897 // We run loop passes three times over each of the loops.
898 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
899 .WillOnce(Invoke(getLoopAnalysisResult));
900 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
901 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
902 .Times(2)
903 .WillRepeatedly(Invoke(getLoopAnalysisResult));
904
905 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _))
906 .WillOnce(Invoke(getLoopAnalysisResult));
907 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
908
909 // When running over the middle loop, the second run inserts two new child
910 // loops, inserting them and itself into the worklist.
911 BasicBlock *NewLoop010BB;
912 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _))
913 .WillOnce(Invoke([&](Loop &L, LoopAnalysisManager &AM,
914 LoopStandardAnalysisResults &AR,
915 LPMUpdater &Updater) {
916 auto *NewLoop = new Loop();
917 L.addChildLoop(NewLoop);
918 NewLoop010BB = BasicBlock::Create(Context, "loop.0.1.0", &F, &Loop02BB);
919 BranchInst::Create(&Loop01BB, NewLoop010BB,
920 UndefValue::get(Type::getInt1Ty(Context)),
921 NewLoop010BB);
922 Loop01BB.getTerminator()->replaceUsesOfWith(&Loop01BB, NewLoop010BB);
923 AR.DT.addNewBlock(NewLoop010BB, &Loop01BB);
924 NewLoop->addBasicBlockToLoop(NewLoop010BB, AR.LI);
925 Updater.addChildLoops({NewLoop});
926 return PreservedAnalyses::all();
927 }));
928
929 // We should immediately drop down to fully visit the new inner loop.
930 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1.0"), _, _, _))
931 .WillOnce(Invoke(getLoopAnalysisResult));
932 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1.0"), _, _));
933 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1.0"), _, _, _))
934 .Times(2)
935 .WillRepeatedly(Invoke(getLoopAnalysisResult));
936
937 // After visiting the inner loop, we should re-visit the second loop
938 // reflecting its new loop nest structure.
939 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _))
940 .WillOnce(Invoke(getLoopAnalysisResult));
941
942 // In the second run over the middle loop after we've visited the new child,
943 // we add another child to check that we can repeatedly add children, and add
944 // children to a loop that already has children.
945 BasicBlock *NewLoop011BB;
946 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _))
947 .WillOnce(Invoke([&](Loop &L, LoopAnalysisManager &AM,
948 LoopStandardAnalysisResults &AR,
949 LPMUpdater &Updater) {
950 auto *NewLoop = new Loop();
951 L.addChildLoop(NewLoop);
952 NewLoop011BB = BasicBlock::Create(Context, "loop.0.1.1", &F, &Loop02BB);
953 BranchInst::Create(&Loop01BB, NewLoop011BB,
954 UndefValue::get(Type::getInt1Ty(Context)),
955 NewLoop011BB);
956 NewLoop010BB->getTerminator()->replaceUsesOfWith(&Loop01BB,
957 NewLoop011BB);
958 AR.DT.addNewBlock(NewLoop011BB, NewLoop010BB);
959 NewLoop->addBasicBlockToLoop(NewLoop011BB, AR.LI);
960 Updater.addChildLoops({NewLoop});
961 return PreservedAnalyses::all();
962 }));
963
964 // Again, we should immediately drop down to visit the new, unvisited child
965 // loop. We don't need to revisit the other child though.
966 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1.1"), _, _, _))
967 .WillOnce(Invoke(getLoopAnalysisResult));
968 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1.1"), _, _));
969 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1.1"), _, _, _))
970 .Times(2)
971 .WillRepeatedly(Invoke(getLoopAnalysisResult));
972
973 // And now we should pop back up to the second loop and do a full pipeline of
974 // three passes on its current form.
975 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _))
976 .Times(3)
977 .WillRepeatedly(Invoke(getLoopAnalysisResult));
978
979 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2"), _, _, _))
980 .WillOnce(Invoke(getLoopAnalysisResult));
981 EXPECT_CALL(MLAHandle, run(HasName("loop.0.2"), _, _));
982 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2"), _, _, _))
983 .Times(2)
984 .WillRepeatedly(Invoke(getLoopAnalysisResult));
985
986 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _))
987 .WillOnce(Invoke(getLoopAnalysisResult));
988 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
989 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _))
990 .Times(2)
991 .WillRepeatedly(Invoke(getLoopAnalysisResult));
992
993 // Now that all the expected actions are registered, run the pipeline over
994 // our module. All of our expectations are verified when the test finishes.
995 MPM.run(*M, MAM);
996 }
997
998 TEST_F(LoopPassManagerTest, LoopPeerInsertion) {
999 // Super boring module with two loop nests and loop nest with two child
1000 // loops.
1001 M = parseIR(Context, "define void @f() {\n"
1002 "entry:\n"
1003 " br label %loop.0\n"
1004 "loop.0:\n"
1005 " br i1 undef, label %loop.0.0, label %loop.2\n"
1006 "loop.0.0:\n"
1007 " br i1 undef, label %loop.0.0, label %loop.0.2\n"
1008 "loop.0.2:\n"
1009 " br i1 undef, label %loop.0.2, label %loop.0\n"
1010 "loop.2:\n"
1011 " br i1 undef, label %loop.2, label %end\n"
1012 "end:\n"
1013 " ret void\n"
1014 "}\n");
1015
1016 // Build up variables referring into the IR so we can rewrite it below
1017 // easily.
1018 Function &F = *M->begin();
1019 ASSERT_THAT(F, HasName("f"));
1020 auto BBI = F.begin();
1021 BasicBlock &EntryBB = *BBI++;
1022 ASSERT_THAT(EntryBB, HasName("entry"));
1023 BasicBlock &Loop0BB = *BBI++;
1024 ASSERT_THAT(Loop0BB, HasName("loop.0"));
1025 BasicBlock &Loop00BB = *BBI++;
1026 ASSERT_THAT(Loop00BB, HasName("loop.0.0"));
1027 BasicBlock &Loop02BB = *BBI++;
1028 ASSERT_THAT(Loop02BB, HasName("loop.0.2"));
1029 BasicBlock &Loop2BB = *BBI++;
1030 ASSERT_THAT(Loop2BB, HasName("loop.2"));
1031 BasicBlock &EndBB = *BBI++;
1032 ASSERT_THAT(EndBB, HasName("end"));
1033 ASSERT_THAT(BBI, F.end());
1034 Constant *Undefi1 = UndefValue::get(Type::getInt1Ty(Context));
1035
1036 // Build the pass managers and register our pipeline. We build a single loop
1037 // pass pipeline consisting of three mock pass runs over each loop. After
1038 // this we run both domtree and loop verification passes to make sure that
1039 // the IR remained valid during our mutations.
1040 ModulePassManager MPM(true);
1041 FunctionPassManager FPM(true);
1042 LoopPassManager LPM(true);
1043 LPM.addPass(MLPHandle.getPass());
1044 LPM.addPass(MLPHandle.getPass());
1045 LPM.addPass(MLPHandle.getPass());
1046 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
1047 FPM.addPass(DominatorTreeVerifierPass());
1048 FPM.addPass(LoopVerifierPass());
1049 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1050
1051 // All the visit orders are deterministic, so we use simple fully order
1052 // expectations.
1053 ::testing::InSequence MakeExpectationsSequenced;
1054
1055 // We run loop passes three times over each of the loops.
1056 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
1057 .WillOnce(Invoke(getLoopAnalysisResult));
1058 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
1059
1060 // On the second run, we insert a sibling loop.
1061 BasicBlock *NewLoop01BB;
1062 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
1063 .WillOnce(Invoke([&](Loop &L, LoopAnalysisManager &AM,
1064 LoopStandardAnalysisResults &AR,
1065 LPMUpdater &Updater) {
1066 auto *NewLoop = new Loop();
1067 L.getParentLoop()->addChildLoop(NewLoop);
1068 NewLoop01BB = BasicBlock::Create(Context, "loop.0.1", &F, &Loop02BB);
1069 BranchInst::Create(&Loop02BB, NewLoop01BB, Undefi1, NewLoop01BB);
1070 Loop00BB.getTerminator()->replaceUsesOfWith(&Loop02BB, NewLoop01BB);
1071 auto *NewDTNode = AR.DT.addNewBlock(NewLoop01BB, &Loop00BB);
1072 AR.DT.changeImmediateDominator(AR.DT[&Loop02BB], NewDTNode);
1073 NewLoop->addBasicBlockToLoop(NewLoop01BB, AR.LI);
1074 Updater.addSiblingLoops({NewLoop});
1075 return PreservedAnalyses::all();
1076 }));
1077 // We finish processing this loop as sibling loops don't perturb the
1078 // postorder walk.
1079 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
1080 .WillOnce(Invoke(getLoopAnalysisResult));
1081
1082 // We visit the inserted sibling next.
1083 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _))
1084 .WillOnce(Invoke(getLoopAnalysisResult));
1085 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
1086 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _))
1087 .Times(2)
1088 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1089
1090 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2"), _, _, _))
1091 .WillOnce(Invoke(getLoopAnalysisResult));
1092 EXPECT_CALL(MLAHandle, run(HasName("loop.0.2"), _, _));
1093 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2"), _, _, _))
1094 .WillOnce(Invoke(getLoopAnalysisResult));
1095 // Next, on the third pass run on the last inner loop we add more new
1096 // siblings, more than one, and one with nested child loops. By doing this at
1097 // the end we make sure that edge case works well.
1098 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2"), _, _, _))
1099 .WillOnce(Invoke([&](Loop &L, LoopAnalysisManager &AM,
1100 LoopStandardAnalysisResults &AR,
1101 LPMUpdater &Updater) {
1102 Loop *NewLoops[] = {new Loop(), new Loop(), new Loop()};
1103 L.getParentLoop()->addChildLoop(NewLoops[0]);
1104 L.getParentLoop()->addChildLoop(NewLoops[1]);
1105 NewLoops[1]->addChildLoop(NewLoops[2]);
1106 auto *NewLoop03BB =
1107 BasicBlock::Create(Context, "loop.0.3", &F, &Loop2BB);
1108 auto *NewLoop04BB =
1109 BasicBlock::Create(Context, "loop.0.4", &F, &Loop2BB);
1110 auto *NewLoop040BB =
1111 BasicBlock::Create(Context, "loop.0.4.0", &F, &Loop2BB);
1112 Loop02BB.getTerminator()->replaceUsesOfWith(&Loop0BB, NewLoop03BB);
1113 BranchInst::Create(NewLoop04BB, NewLoop03BB, Undefi1, NewLoop03BB);
1114 BranchInst::Create(&Loop0BB, NewLoop040BB, Undefi1, NewLoop04BB);
1115 BranchInst::Create(NewLoop04BB, NewLoop040BB, Undefi1, NewLoop040BB);
1116 AR.DT.addNewBlock(NewLoop03BB, &Loop02BB);
1117 AR.DT.addNewBlock(NewLoop04BB, NewLoop03BB);
1118 AR.DT.addNewBlock(NewLoop040BB, NewLoop04BB);
1119 NewLoops[0]->addBasicBlockToLoop(NewLoop03BB, AR.LI);
1120 NewLoops[1]->addBasicBlockToLoop(NewLoop04BB, AR.LI);
1121 NewLoops[2]->addBasicBlockToLoop(NewLoop040BB, AR.LI);
1122 Updater.addSiblingLoops({NewLoops[0], NewLoops[1]});
1123 return PreservedAnalyses::all();
1124 }));
1125
1126 EXPECT_CALL(MLPHandle, run(HasName("loop.0.3"), _, _, _))
1127 .WillOnce(Invoke(getLoopAnalysisResult));
1128 EXPECT_CALL(MLAHandle, run(HasName("loop.0.3"), _, _));
1129 EXPECT_CALL(MLPHandle, run(HasName("loop.0.3"), _, _, _))
1130 .Times(2)
1131 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1132
1133 // Note that we need to visit the inner loop of this added sibling before the
1134 // sibling itself!
1135 EXPECT_CALL(MLPHandle, run(HasName("loop.0.4.0"), _, _, _))
1136 .WillOnce(Invoke(getLoopAnalysisResult));
1137 EXPECT_CALL(MLAHandle, run(HasName("loop.0.4.0"), _, _));
1138 EXPECT_CALL(MLPHandle, run(HasName("loop.0.4.0"), _, _, _))
1139 .Times(2)
1140 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1141
1142 EXPECT_CALL(MLPHandle, run(HasName("loop.0.4"), _, _, _))
1143 .WillOnce(Invoke(getLoopAnalysisResult));
1144 EXPECT_CALL(MLAHandle, run(HasName("loop.0.4"), _, _));
1145 EXPECT_CALL(MLPHandle, run(HasName("loop.0.4"), _, _, _))
1146 .Times(2)
1147 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1148
1149 // And only now do we visit the outermost loop of the nest.
1150 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _))
1151 .WillOnce(Invoke(getLoopAnalysisResult));
1152 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
1153 // On the second pass, we add sibling loops which become new top-level loops.
1154 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _))
1155 .WillOnce(Invoke([&](Loop &L, LoopAnalysisManager &AM,
1156 LoopStandardAnalysisResults &AR,
1157 LPMUpdater &Updater) {
1158 auto *NewLoop = new Loop();
1159 AR.LI.addTopLevelLoop(NewLoop);
1160 auto *NewLoop1BB = BasicBlock::Create(Context, "loop.1", &F, &Loop2BB);
1161 BranchInst::Create(&Loop2BB, NewLoop1BB, Undefi1, NewLoop1BB);
1162 Loop0BB.getTerminator()->replaceUsesOfWith(&Loop2BB, NewLoop1BB);
1163 auto *NewDTNode = AR.DT.addNewBlock(NewLoop1BB, &Loop0BB);
1164 AR.DT.changeImmediateDominator(AR.DT[&Loop2BB], NewDTNode);
1165 NewLoop->addBasicBlockToLoop(NewLoop1BB, AR.LI);
1166 Updater.addSiblingLoops({NewLoop});
1167 return PreservedAnalyses::all();
1168 }));
1169 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _))
1170 .WillOnce(Invoke(getLoopAnalysisResult));
1171
1172 EXPECT_CALL(MLPHandle, run(HasName("loop.1"), _, _, _))
1173 .WillOnce(Invoke(getLoopAnalysisResult));
1174 EXPECT_CALL(MLAHandle, run(HasName("loop.1"), _, _));
1175 EXPECT_CALL(MLPHandle, run(HasName("loop.1"), _, _, _))
1176 .Times(2)
1177 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1178
1179 EXPECT_CALL(MLPHandle, run(HasName("loop.2"), _, _, _))
1180 .WillOnce(Invoke(getLoopAnalysisResult));
1181 EXPECT_CALL(MLAHandle, run(HasName("loop.2"), _, _));
1182 EXPECT_CALL(MLPHandle, run(HasName("loop.2"), _, _, _))
1183 .Times(2)
1184 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1185
1186 // Now that all the expected actions are registered, run the pipeline over
1187 // our module. All of our expectations are verified when the test finishes.
1188 MPM.run(*M, MAM);
1189 }
1190
1191 TEST_F(LoopPassManagerTest, LoopDeletion) {
1192 // Build a module with a single loop nest that contains one outer loop with
1193 // three subloops, and one of those with its own subloop. We will
1194 // incrementally delete all of these to test different deletion scenarios.
1195 M = parseIR(Context, "define void @f() {\n"
1196 "entry:\n"
1197 " br label %loop.0\n"
1198 "loop.0:\n"
1199 " br i1 undef, label %loop.0.0, label %end\n"
1200 "loop.0.0:\n"
1201 " br i1 undef, label %loop.0.0, label %loop.0.1\n"
1202 "loop.0.1:\n"
1203 " br i1 undef, label %loop.0.1, label %loop.0.2\n"
1204 "loop.0.2:\n"
1205 " br i1 undef, label %loop.0.2.0, label %loop.0\n"
1206 "loop.0.2.0:\n"
1207 " br i1 undef, label %loop.0.2.0, label %loop.0.2\n"
1208 "end:\n"
1209 " ret void\n"
1210 "}\n");
1211
1212 // Build up variables referring into the IR so we can rewrite it below
1213 // easily.
1214 Function &F = *M->begin();
1215 ASSERT_THAT(F, HasName("f"));
1216 auto BBI = F.begin();
1217 BasicBlock &EntryBB = *BBI++;
1218 ASSERT_THAT(EntryBB, HasName("entry"));
1219 BasicBlock &Loop0BB = *BBI++;
1220 ASSERT_THAT(Loop0BB, HasName("loop.0"));
1221 BasicBlock &Loop00BB = *BBI++;
1222 ASSERT_THAT(Loop00BB, HasName("loop.0.0"));
1223 BasicBlock &Loop01BB = *BBI++;
1224 ASSERT_THAT(Loop01BB, HasName("loop.0.1"));
1225 BasicBlock &Loop02BB = *BBI++;
1226 ASSERT_THAT(Loop02BB, HasName("loop.0.2"));
1227 BasicBlock &Loop020BB = *BBI++;
1228 ASSERT_THAT(Loop020BB, HasName("loop.0.2.0"));
1229 BasicBlock &EndBB = *BBI++;
1230 ASSERT_THAT(EndBB, HasName("end"));
1231 ASSERT_THAT(BBI, F.end());
1232 Constant *Undefi1 = UndefValue::get(Type::getInt1Ty(Context));
1233
1234 // Helper to do the actual deletion of a loop. We directly encode this here
1235 // to isolate ourselves from the rest of LLVM and for simplicity. Here we can
1236 // egregiously cheat based on knowledge of the test case. For example, we
1237 // have no PHI nodes and there is always a single i-dom.
1238 auto DeleteLoopBlocks = [](Loop &L, BasicBlock &IDomBB,
1239 LoopStandardAnalysisResults &AR,
1240 LPMUpdater &Updater) {
1241 for (BasicBlock *LoopBB : L.blocks()) {
1242 SmallVector ChildNodes(AR.DT[LoopBB]->begin(),
1243 AR.DT[LoopBB]->end());
1244 for (DomTreeNode *ChildNode : ChildNodes)
1245 AR.DT.changeImmediateDominator(ChildNode, AR.DT[&IDomBB]);
1246 AR.DT.eraseNode(LoopBB);
1247 LoopBB->dropAllReferences();
1248 }
1249 SmallVector LoopBBs(L.block_begin(), L.block_end());
1250 Updater.markLoopAsDeleted(L);
1251 AR.LI.markAsRemoved(&L);
1252 for (BasicBlock *LoopBB : LoopBBs)
1253 LoopBB->eraseFromParent();
1254 };
1255
1256 // Build up the pass managers.
1257 ModulePassManager MPM(true);
1258 FunctionPassManager FPM(true);
1259 // We run several loop pass pipelines across the loop nest, but they all take
1260 // the same form of three mock pass runs in a loop pipeline followed by
1261 // domtree and loop verification. We use a lambda to stamp this out each
1262 // time.
1263 auto AddLoopPipelineAndVerificationPasses = [&] {
1264 LoopPassManager LPM(true);
1265 LPM.addPass(MLPHandle.getPass());
1266 LPM.addPass(MLPHandle.getPass());
1267 LPM.addPass(MLPHandle.getPass());
1268 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
1269 FPM.addPass(DominatorTreeVerifierPass());
1270 FPM.addPass(LoopVerifierPass());
1271 };
1272
1273 // All the visit orders are deterministic so we use simple fully order
1274 // expectations.
1275 ::testing::InSequence MakeExpectationsSequenced;
1276
1277 // We run the loop pipeline with three passes over each of the loops. When
1278 // running over the middle loop, the second pass in the pipeline deletes it.
1279 // This should prevent the third pass from visiting it but otherwise leave
1280 // the process unimpacted.
1281 AddLoopPipelineAndVerificationPasses();
1282 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
1283 .WillOnce(Invoke(getLoopAnalysisResult));
1284 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
1285 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
1286 .Times(2)
1287 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1288
1289 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _))
1290 .WillOnce(Invoke(getLoopAnalysisResult));
1291 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
1292 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _))
1293 .WillOnce(
1294 Invoke([&](Loop &L, LoopAnalysisManager &AM,
1295 LoopStandardAnalysisResults &AR, LPMUpdater &Updater) {
1296 AR.SE.forgetLoop(&L);
1297 Loop00BB.getTerminator()->replaceUsesOfWith(&Loop01BB, &Loop02BB);
1298 DeleteLoopBlocks(L, Loop00BB, AR, Updater);
1299 return PreservedAnalyses::all();
1300 }));
1301
1302 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2.0"), _, _, _))
1303 .WillOnce(Invoke(getLoopAnalysisResult));
1304 EXPECT_CALL(MLAHandle, run(HasName("loop.0.2.0"), _, _));
1305 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2.0"), _, _, _))
1306 .Times(2)
1307 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1308