llvm.org GIT mirror llvm / d27a39a
[PM] Rewrite the loop pass manager to use a worklist and augmented run arguments much like the CGSCC pass manager. This is a major redesign following the pattern establish for the CGSCC layer to support updates to the set of loops during the traversal of the loop nest and to support invalidation of analyses. An additional significant burden in the loop PM is that so many passes require access to a large number of function analyses. Manually ensuring these are cached, available, and preserved has been a long-standing burden in LLVM even with the help of the automatic scheduling in the old pass manager. And it made the new pass manager extremely unweildy. With this design, we can package the common analyses up while in a function pass and make them immediately available to all the loop passes. While in some cases this is unnecessary, I think the simplicity afforded is worth it. This does not (yet) address loop simplified form or LCSSA form, but those are the next things on my radar and I have a clear plan for them. While the patch is very large, most of it is either mechanically updating loop passes to the new API or the new testing for the loop PM. The code for it is reasonably compact. I have not yet updated all of the loop passes to correctly leverage the update mechanisms demonstrated in the unittests. I'll do that in follow-up patches along with improved FileCheck tests for those passes that ensure things work in more realistic scenarios. In many cases, there isn't much we can do with these until the loop simplified form and LCSSA form are in place. Differential Revision: https://reviews.llvm.org/D28292 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@291651 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 2 years ago
34 changed file(s) with 2045 addition(s) and 417 deletion(s). Raw diff Collapse all Expand all
192192 public:
193193 typedef IVUsers Result;
194194
195 IVUsers run(Loop &L, LoopAnalysisManager &AM);
195 IVUsers run(Loop &L, LoopAnalysisManager &AM,
196 LoopStandardAnalysisResults &AR);
196197 };
197198
198199 /// Printer pass for the \c IVUsers for a loop.
201202
202203 public:
203204 explicit IVUsersPrinterPass(raw_ostream &OS) : OS(OS) {}
204 PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM);
205 PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
206 LoopStandardAnalysisResults &AR, LPMUpdater &U);
205207 };
206208 }
207209
752752
753753 public:
754754 typedef LoopAccessInfo Result;
755 Result run(Loop &, LoopAnalysisManager &);
756 static StringRef name() { return "LoopAccessAnalysis"; }
755
756 Result run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR);
757757 };
758758
759759 /// \brief Printer pass for the \c LoopAccessInfo results.
763763
764764 public:
765765 explicit LoopAccessInfoPrinterPass(raw_ostream &OS) : OS(OS) {}
766 PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM);
766 PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
767 LoopStandardAnalysisResults &AR, LPMUpdater &U);
767768 };
768769
769770 inline Instruction *MemoryDepChecker::Dependence::getSource(
852852 void getAnalysisUsage(AnalysisUsage &AU) const override;
853853 };
854854
855 /// \brief Pass for printing a loop's contents as LLVM's text IR assembly.
856 class PrintLoopPass : public PassInfoMixin {
857 raw_ostream &OS;
858 std::string Banner;
859
860 public:
861 PrintLoopPass();
862 PrintLoopPass(raw_ostream &OS, const std::string &Banner = "");
863
864 PreservedAnalyses run(Loop &L, AnalysisManager &);
865 };
855 /// Function to print a loop's contents as LLVM's text IR assembly.
856 void printLoop(Loop &L, raw_ostream &OS, const std::string &Banner = "");
866857
867858 } // End llvm namespace
868859
77 //===----------------------------------------------------------------------===//
88 /// \file
99 ///
10 /// This header provides classes for managing passes over loops in LLVM IR.
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.
1133 ///
1234 //===----------------------------------------------------------------------===//
1335
1436 #ifndef LLVM_ANALYSIS_LOOPPASSMANAGER_H
1537 #define LLVM_ANALYSIS_LOOPPASSMANAGER_H
1638
39 #include "llvm/ADT/PostOrderIterator.h"
40 #include "llvm/ADT/PriorityWorklist.h"
1741 #include "llvm/ADT/STLExtras.h"
1842 #include "llvm/Analysis/AliasAnalysis.h"
43 #include "llvm/Analysis/BasicAliasAnalysis.h"
44 #include "llvm/Analysis/GlobalsModRef.h"
1945 #include "llvm/Analysis/LoopInfo.h"
2046 #include "llvm/Analysis/ScalarEvolution.h"
47 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
2148 #include "llvm/Analysis/TargetLibraryInfo.h"
49 #include "llvm/Analysis/TargetTransformInfo.h"
2250 #include "llvm/IR/Dominators.h"
2351 #include "llvm/IR/PassManager.h"
2452
2553 namespace llvm {
2654
27 extern template class PassManager;
28 /// \brief The loop pass manager.
29 ///
30 /// See the documentation for the PassManager template for details. It runs a
31 /// sequency of loop passes over each loop that the manager is run over. This
32 /// typedef serves as a convenient way to refer to this construct.
33 typedef PassManager LoopPassManager;
34
35 extern template class AnalysisManager;
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;
3665 /// \brief The loop analysis manager.
3766 ///
3867 /// See the documentation for the AnalysisManager template for detail
3968 /// documentation. This typedef serves as a convenient way to refer to this
4069 /// construct in the adaptors and proxies used to integrate this into the larger
4170 /// pass manager infrastructure.
42 typedef AnalysisManager> LoopAnalysisManager;
71 typedef AnalysisManager, LoopStandardAnalysisResults &>
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 &>;
43116
44117 /// A proxy from a \c LoopAnalysisManager to a \c Function.
45118 typedef InnerAnalysisManagerProxy
46119 LoopAnalysisManagerFunctionProxy;
47120
48 /// Specialization of the invalidate method for the \c
49 /// LoopAnalysisManagerFunctionProxy's result.
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.
50177 template <>
51 bool LoopAnalysisManagerFunctionProxy::Result::invalidate(
52 Function &F, const PreservedAnalyses &PA,
53 FunctionAnalysisManager::Invalidator &Inv);
178 LoopAnalysisManagerFunctionProxy::Result
179 LoopAnalysisManagerFunctionProxy::run(Function &F, FunctionAnalysisManager &AM);
54180
55181 // Ensure the \c LoopAnalysisManagerFunctionProxy is provided as an extern
56182 // template.
57183 extern template class InnerAnalysisManagerProxy;
58184
59 extern template class OuterAnalysisManagerProxy>;
185 extern template class OuterAnalysisManagerProxy,
186 LoopStandardAnalysisResults &>;
60187 /// A proxy from a \c FunctionAnalysisManager to a \c Loop.
61 typedef OuterAnalysisManagerProxy>
188 typedef OuterAnalysisManagerProxy,
189 LoopStandardAnalysisResults &>
62190 FunctionAnalysisManagerLoopProxy;
63191
64192 /// Returns the minimum set of Analyses that all loop passes must preserve.
65193 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 };
66345
67346 /// \brief Adaptor that maps from a function to its loops.
68347 ///
86365 // Get the loop structure for this function
87366 LoopInfo &LI = AM.getResult(F);
88367
89 // Also precompute all of the function analyses used by loop passes.
90 // FIXME: These should be handed into the loop passes when the loop pass
91 // management layer is reworked to follow the design of CGSCC.
92 (void)AM.getResult(F);
93 (void)AM.getResult(F);
94 (void)AM.getResult(F);
95 (void)AM.getResult(F);
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)};
96380
97381 PreservedAnalyses PA = PreservedAnalyses::all();
98382
99 // We want to visit the loops in reverse post-order. We'll build the stack
100 // of loops to visit in Loops by first walking the loops in pre-order.
101 SmallVector Loops;
102 SmallVector WorkList(LI.begin(), LI.end());
103 while (!WorkList.empty()) {
104 Loop *L = WorkList.pop_back_val();
105 WorkList.insert(WorkList.end(), L->begin(), L->end());
106 Loops.push_back(L);
107 }
108
109 // Now pop each element off of the stack to visit the loops in reverse
110 // post-order.
111 for (auto *L : reverse(Loops)) {
112 PreservedAnalyses PassPA = Pass.run(*L, LAM);
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);
113409 // FIXME: We should verify the set of analyses relevant to Loop passes
114410 // are preserved.
115411
116 // We know that the loop pass couldn't have invalidated any other loop's
117 // analyses (that's the contract of a loop pass), so directly handle the
118 // loop analysis manager's invalidation here.
119 LAM.invalidate(*L, PassPA);
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);
120418
121419 // Then intersect the preserved set so that invalidation of module
122420 // analyses will eventually occur when the module pass completes.
123421 PA.intersect(std::move(PassPA));
124 }
422 } while (!Worklist.empty());
125423
126424 // By definition we preserve the proxy. We also preserve all analyses on
127425 // Loops. This precludes *any* invalidation of loop analyses by the proxy,
129427 // loop analysis manager incrementally above.
130428 PA.preserveSet>();
131429 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();
132441 return PA;
133442 }
134443
143452 createFunctionToLoopPassAdaptor(LoopPassT Pass) {
144453 return FunctionToLoopPassAdaptor(std::move(Pass));
145454 }
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 };
146468 }
147469
148470 #endif // LLVM_ANALYSIS_LOOPPASSMANAGER_H
2222
2323 class IndVarSimplifyPass : public PassInfoMixin {
2424 public:
25 PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM);
25 PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
26 LoopStandardAnalysisResults &AR, LPMUpdater &U);
2627 };
2728 }
2829
4141 /// Performs Loop Invariant Code Motion Pass.
4242 class LICMPass : public PassInfoMixin {
4343 public:
44 PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM);
44 PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
45 LoopStandardAnalysisResults &AR, LPMUpdater &U);
4546 };
4647 } // end namespace llvm
4748
2323 class LoopDeletionPass : public PassInfoMixin {
2424 public:
2525 LoopDeletionPass() {}
26 PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM);
26 PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
27 LoopStandardAnalysisResults &AR, LPMUpdater &U);
2728 bool runImpl(Loop *L, DominatorTree &DT, ScalarEvolution &SE,
2829 LoopInfo &loopInfo);
2930
2424 /// Performs Loop Idiom Recognize Pass.
2525 class LoopIdiomRecognizePass : public PassInfoMixin {
2626 public:
27 PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM);
27 PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
28 LoopStandardAnalysisResults &AR, LPMUpdater &U);
2829 };
2930 } // end namespace llvm
3031
2222 /// Performs Loop Inst Simplify Pass.
2323 class LoopInstSimplifyPass : public PassInfoMixin {
2424 public:
25 PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM);
25 PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
26 LoopStandardAnalysisResults &AR, LPMUpdater &U);
2627 };
2728 } // end namespace llvm
2829
2323 class LoopRotatePass : public PassInfoMixin {
2424 public:
2525 LoopRotatePass(bool EnableHeaderDuplication = true);
26 PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM);
26 PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
27 LoopStandardAnalysisResults &AR, LPMUpdater &U);
2728
2829 private:
2930 const bool EnableHeaderDuplication;
2525 /// Performs basic CFG simplifications to assist other loop passes.
2626 class LoopSimplifyCFGPass : public PassInfoMixin {
2727 public:
28 PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM);
28 PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
29 LoopStandardAnalysisResults &AR, LPMUpdater &U);
2930 };
3031 } // end namespace llvm
3132
3030 /// Performs Loop Strength Reduce Pass.
3131 class LoopStrengthReducePass : public PassInfoMixin {
3232 public:
33 PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM);
33 PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
34 LoopStandardAnalysisResults &AR, LPMUpdater &U);
3435 };
3536 } // end namespace llvm
3637
2222 Optional ProvidedRuntime;
2323 Optional ProvidedUpperBound;
2424
25 PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM);
25 PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
26 LoopStandardAnalysisResults &AR, LPMUpdater &U);
2627 };
2728 } // end namespace llvm
2829
3535
3636 AnalysisKey IVUsersAnalysis::Key;
3737
38 IVUsers IVUsersAnalysis::run(Loop &L, LoopAnalysisManager &AM) {
39 const auto &FAM =
40 AM.getResult(L).getManager();
41 Function *F = L.getHeader()->getParent();
42
43 return IVUsers(&L, FAM.getCachedResult(*F),
44 FAM.getCachedResult(*F),
45 FAM.getCachedResult(*F),
46 FAM.getCachedResult(*F));
47 }
48
49 PreservedAnalyses IVUsersPrinterPass::run(Loop &L, LoopAnalysisManager &AM) {
50 AM.getResult(L).print(OS);
38 IVUsers IVUsersAnalysis::run(Loop &L, LoopAnalysisManager &AM,
39 LoopStandardAnalysisResults &AR) {
40 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);
5147 return PreservedAnalyses::all();
5248 }
5349
21192119
21202120 AnalysisKey LoopAccessAnalysis::Key;
21212121
2122 LoopAccessInfo LoopAccessAnalysis::run(Loop &L, LoopAnalysisManager &AM) {
2123 const FunctionAnalysisManager &FAM =
2124 AM.getResult(L).getManager();
2122 LoopAccessInfo LoopAccessAnalysis::run(Loop &L, LoopAnalysisManager &AM,
2123 LoopStandardAnalysisResults &AR) {
2124 return LoopAccessInfo(&L, &AR.SE, &AR.TLI, &AR.AA, &AR.DT, &AR.LI);
2125 }
2126
2127 PreservedAnalyses
2128 LoopAccessInfoPrinterPass::run(Loop &L, LoopAnalysisManager &AM,
2129 LoopStandardAnalysisResults &AR, LPMUpdater &) {
21252130 Function &F = *L.getHeader()->getParent();
2126 auto *SE = FAM.getCachedResult(F);
2127 auto *TLI = FAM.getCachedResult(F);
2128 auto *AA = FAM.getCachedResult(F);
2129 auto *DT = FAM.getCachedResult(F);
2130 auto *LI = FAM.getCachedResult(F);
2131 if (!SE)
2132 report_fatal_error(
2133 "ScalarEvolution must have been cached at a higher level");
2134 if (!AA)
2135 report_fatal_error("AliasAnalysis must have been cached at a higher level");
2136 if (!DT)
2137 report_fatal_error("DominatorTree must have been cached at a higher level");
2138 if (!LI)
2139 report_fatal_error("LoopInfo must have been cached at a higher level");
2140 return LoopAccessInfo(&L, SE, TLI, AA, DT, LI);
2141 }
2142
2143 PreservedAnalyses LoopAccessInfoPrinterPass::run(Loop &L,
2144 LoopAnalysisManager &AM) {
2145 Function &F = *L.getHeader()->getParent();
2146 auto &LAI = AM.getResult(L);
2131 auto &LAI = AM.getResult(L, AR);
21472132 OS << "Loop access info in function '" << F.getName() << "':\n";
21482133 OS.indent(2) << L.getHeader()->getName() << ":\n";
21492134 LAI.print(OS, 4);
688688 return PreservedAnalyses::all();
689689 }
690690
691 PrintLoopPass::PrintLoopPass() : OS(dbgs()) {}
692 PrintLoopPass::PrintLoopPass(raw_ostream &OS, const std::string &Banner)
693 : OS(OS), Banner(Banner) {}
694
695 PreservedAnalyses PrintLoopPass::run(Loop &L, AnalysisManager &) {
691 void llvm::printLoop(Loop &L, raw_ostream &OS, const std::string &Banner) {
696692 OS << Banner;
697693 for (auto *Block : L.blocks())
698694 if (Block)
699695 Block->print(OS);
700696 else
701697 OS << "Printing block";
702 return PreservedAnalyses::all();
703698 }
704699
705700 //===----------------------------------------------------------------------===//
3131 /// PrintLoopPass - Print a Function corresponding to a Loop.
3232 ///
3333 class PrintLoopPassWrapper : public LoopPass {
34 PrintLoopPass P;
34 raw_ostream &OS;
35 std::string Banner;
3536
3637 public:
3738 static char ID;
38 PrintLoopPassWrapper() : LoopPass(ID) {}
39 PrintLoopPassWrapper() : LoopPass(ID), OS(dbgs()) {}
3940 PrintLoopPassWrapper(raw_ostream &OS, const std::string &Banner)
40 : LoopPass(ID), P(OS, Banner) {}
41 : LoopPass(ID), OS(OS), Banner(Banner) {}
4142
4243 void getAnalysisUsage(AnalysisUsage &AU) const override {
4344 AU.setPreservesAll();
4849 [](BasicBlock *BB) { return BB; });
4950 if (BBI != L->blocks().end() &&
5051 isFunctionInPrintList((*BBI)->getParent()->getName())) {
51 LoopAnalysisManager DummyLAM;
52 P.run(*L, DummyLAM);
52 printLoop(*L, OS, Banner);
5353 }
5454 return false;
5555 }
1919 // Explicit template instantiations and specialization defininitions for core
2020 // template typedefs.
2121 namespace llvm {
22 template class PassManager;
23 template class AnalysisManager;
22 template class AllAnalysesOn;
23 template class AnalysisManager;
24 template class PassManager
25 LoopStandardAnalysisResults &, LPMUpdater &>;
2426 template class InnerAnalysisManagerProxy;
25 template class OuterAnalysisManagerProxy;
26
27 template class OuterAnalysisManagerProxy
28 LoopStandardAnalysisResults &>;
29
30 /// Explicitly specialize the pass manager's run method to handle loop nest
31 /// structure updates.
2732 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
2884 bool LoopAnalysisManagerFunctionProxy::Result::invalidate(
2985 Function &F, const PreservedAnalyses &PA,
3086 FunctionAnalysisManager::Invalidator &Inv) {
31 // If this proxy isn't marked as preserved, the set of Function objects in
32 // the module may have changed. We therefore can't call
33 // InnerAM->invalidate(), because any pointers to Functions it has may be
34 // stale.
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.
35116 auto PAC = PA.getChecker();
36 if (!PAC.preserved() && !PAC.preservedSet>())
37 InnerAM->clear();
38
39 // FIXME: Proper suppor for invalidation isn't yet implemented for the LPM.
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 }
40188
41189 // Return false to indicate that this result is still a valid proxy.
42190 return false;
43191 }
192
193 template <>
194 LoopAnalysisManagerFunctionProxy::Result
195 LoopAnalysisManagerFunctionProxy::run(Function &F,
196 FunctionAnalysisManager &AM) {
197 return Result(*InnerAM, AM.getResult(F));
198 }
44199 }
45200
46201 PreservedAnalyses llvm::getLoopPassPreservedAnalyses() {
47202 PreservedAnalyses PA;
203 PA.preserve();
48204 PA.preserve();
49205 PA.preserve();
206 PA.preserve();
50207 PA.preserve();
51208 // TODO: What we really want to do here is preserve an AA category, but that
52209 // concept doesn't exist yet.
56213 PA.preserve();
57214 return PA;
58215 }
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 }
3737 #include "llvm/Analysis/LazyValueInfo.h"
3838 #include "llvm/Analysis/LoopAccessAnalysis.h"
3939 #include "llvm/Analysis/LoopInfo.h"
40 #include "llvm/Analysis/LoopPassManager.h"
4041 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
4142 #include "llvm/Analysis/ModuleSummaryAnalysis.h"
4243 #include "llvm/Analysis/OptimizationDiagnosticInfo.h"
219220
220221 /// \brief No-op loop pass which does nothing.
221222 struct NoOpLoopPass {
222 PreservedAnalyses run(Loop &L, LoopAnalysisManager &) {
223 PreservedAnalyses run(Loop &L, LoopAnalysisManager &,
224 LoopStandardAnalysisResults &, LPMUpdater &) {
223225 return PreservedAnalyses::all();
224226 }
225227 static StringRef name() { return "NoOpLoopPass"; }
232234
233235 public:
234236 struct Result {};
235 Result run(Loop &, LoopAnalysisManager &) { return Result(); }
237 Result run(Loop &, LoopAnalysisManager &, LoopStandardAnalysisResults &) {
238 return Result();
239 }
236240 static StringRef name() { return "NoOpLoopAnalysis"; }
237241 };
238242
10181022 #define LOOP_ANALYSIS(NAME, CREATE_PASS) \
10191023 if (Name == "require<" NAME ">") { \
10201024 LPM.addPass(RequireAnalysisPass< \
1021 std::remove_reference::type, Loop>()); \
1025 std::remove_reference::type, Loop, \
1026 LoopAnalysisManager, LoopStandardAnalysisResults &, \
1027 LPMUpdater &>()); \
10221028 return true; \
10231029 } \
10241030 if (Name == "invalidate<" NAME ">") { \
24812481 return Changed;
24822482 }
24832483
2484 PreservedAnalyses IndVarSimplifyPass::run(Loop &L, LoopAnalysisManager &AM) {
2485 auto &FAM = AM.getResult(L).getManager();
2484 PreservedAnalyses IndVarSimplifyPass::run(Loop &L, LoopAnalysisManager &AM,
2485 LoopStandardAnalysisResults &AR,
2486 LPMUpdater &) {
24862487 Function *F = L.getHeader()->getParent();
24872488 const DataLayout &DL = F->getParent()->getDataLayout();
24882489
2489 auto *LI = FAM.getCachedResult(*F);
2490 auto *SE = FAM.getCachedResult(*F);
2491 auto *DT = FAM.getCachedResult(*F);
2492
2493 assert((LI && SE && DT) &&
2494 "Analyses required for indvarsimplify not available!");
2495
2496 // Optional analyses.
2497 auto *TTI = FAM.getCachedResult(*F);
2498 auto *TLI = FAM.getCachedResult(*F);
2499
2500 IndVarSimplify IVS(LI, SE, DT, DL, TLI, TTI);
2490 IndVarSimplify IVS(&AR.LI, &AR.SE, &AR.DT, DL, &AR.TLI, &AR.TTI);
25012491 if (!IVS.run(&L))
25022492 return PreservedAnalyses::all();
25032493
184184 };
185185 }
186186
187 PreservedAnalyses LICMPass::run(Loop &L, LoopAnalysisManager &AM) {
187 PreservedAnalyses LICMPass::run(Loop &L, LoopAnalysisManager &AM,
188 LoopStandardAnalysisResults &AR, LPMUpdater &) {
188189 const auto &FAM =
189 AM.getResult(L).getManager();
190 AM.getResult(L, AR).getManager();
190191 Function *F = L.getHeader()->getParent();
191192
192 auto *AA = FAM.getCachedResult(*F);
193 auto *LI = FAM.getCachedResult(*F);
194 auto *DT = FAM.getCachedResult(*F);
195 auto *TLI = FAM.getCachedResult(*F);
196 auto *SE = FAM.getCachedResult(*F);
197193 auto *ORE = FAM.getCachedResult(*F);
198 assert((AA && LI && DT && TLI && SE && ORE) &&
199 "Analyses for LICM not available");
194 // FIXME: This should probably be optional rather than required.
195 if (!ORE)
196 report_fatal_error("LICM: OptimizationRemarkEmitterAnalysis not "
197 "cached at a higher level");
200198
201199 LoopInvariantCodeMotion LICM;
202
203 if (!LICM.runOnLoop(&L, AA, LI, DT, TLI, SE, ORE, true))
200 if (!LICM.runOnLoop(&L, &AR.AA, &AR.LI, &AR.DT, &AR.TLI, &AR.SE, ORE, true))
204201 return PreservedAnalyses::all();
205202
206203 // FIXME: There is no setPreservesCFG in the new PM. When that becomes
214214 return Changed;
215215 }
216216
217 PreservedAnalyses LoopDeletionPass::run(Loop &L, LoopAnalysisManager &AM) {
218 auto &FAM = AM.getResult(L).getManager();
219 Function *F = L.getHeader()->getParent();
220
221 auto &DT = *FAM.getCachedResult(*F);
222 auto &SE = *FAM.getCachedResult(*F);
223 auto &LI = *FAM.getCachedResult(*F);
224
225 bool Changed = runImpl(&L, DT, SE, LI);
217 PreservedAnalyses LoopDeletionPass::run(Loop &L, LoopAnalysisManager &AM,
218 LoopStandardAnalysisResults &AR,
219 LPMUpdater &) {
220 bool Changed = runImpl(&L, AR.DT, AR.SE, AR.LI);
226221 if (!Changed)
227222 return PreservedAnalyses::all();
228223
945945 auto &SE = AM.getResult(F);
946946 auto &ORE = AM.getResult(F);
947947
948 // We don't directly need these analyses but they're required for loop
949 // analyses so provide them below.
950 auto &AA = AM.getResult(F);
951 auto &AC = AM.getResult(F);
952 auto &TTI = AM.getResult(F);
953 auto &TLI = AM.getResult(F);
954
948955 auto &LAM = AM.getResult(F).getManager();
949956 std::function GetLAA =
950957 [&](Loop &L) -> const LoopAccessInfo & {
951 return LAM.getResult(L);
958 LoopStandardAnalysisResults AR = {AA, AC, DT, LI, SE, TLI, TTI};
959 return LAM.getResult(L, AR);
952960 };
953961
954962 bool Changed = runImpl(F, &LI, &DT, &SE, &ORE, GetLAA);
185185 };
186186 } // End anonymous namespace.
187187
188 PreservedAnalyses LoopIdiomRecognizePass::run(Loop &L,
189 LoopAnalysisManager &AM) {
190 const auto &FAM =
191 AM.getResult(L).getManager();
192 Function *F = L.getHeader()->getParent();
193
194 // Use getCachedResult because Loop pass cannot trigger a function analysis.
195 auto *AA = FAM.getCachedResult(*F);
196 auto *DT = FAM.getCachedResult(*F);
197 auto *LI = FAM.getCachedResult(*F);
198 auto *SE = FAM.getCachedResult(*F);
199 auto *TLI = FAM.getCachedResult(*F);
200 const auto *TTI = FAM.getCachedResult(*F);
188 PreservedAnalyses LoopIdiomRecognizePass::run(Loop &L, LoopAnalysisManager &AM,
189 LoopStandardAnalysisResults &AR,
190 LPMUpdater &) {
201191 const auto *DL = &L.getHeader()->getModule()->getDataLayout();
202 assert((AA && DT && LI && SE && TLI && TTI && DL) &&
203 "Analyses for Loop Idiom Recognition not available");
204
205 LoopIdiomRecognize LIR(AA, DT, LI, SE, TLI, TTI, DL);
192
193 LoopIdiomRecognize LIR(&AR.AA, &AR.DT, &AR.LI, &AR.SE, &AR.TLI, &AR.TTI, DL);
206194 if (!LIR.runOnLoop(&L))
207195 return PreservedAnalyses::all();
208196
182182 };
183183 }
184184
185 PreservedAnalyses LoopInstSimplifyPass::run(Loop &L,
186 LoopAnalysisManager &AM) {
187 const auto &FAM =
188 AM.getResult(L).getManager();
189 Function *F = L.getHeader()->getParent();
190
191 // Use getCachedResult because Loop pass cannot trigger a function analysis.
192 auto *DT = FAM.getCachedResult(*F);
193 auto *LI = FAM.getCachedResult(*F);
194 auto *AC = FAM.getCachedResult(*F);
195 const auto *TLI = FAM.getCachedResult(*F);
196 assert((LI && AC && TLI) && "Analyses for Loop Inst Simplify not available");
197
198 if (!SimplifyLoopInst(&L, DT, LI, AC, TLI))
185 PreservedAnalyses LoopInstSimplifyPass::run(Loop &L, LoopAnalysisManager &AM,
186 LoopStandardAnalysisResults &AR,
187 LPMUpdater &) {
188 if (!SimplifyLoopInst(&L, &AR.DT, &AR.LI, &AR.AC, &AR.TLI))
199189 return PreservedAnalyses::all();
200190
201191 return getLoopPassPreservedAnalyses();
624624 LoopRotatePass::LoopRotatePass(bool EnableHeaderDuplication)
625625 : EnableHeaderDuplication(EnableHeaderDuplication) {}
626626
627 PreservedAnalyses LoopRotatePass::run(Loop &L, LoopAnalysisManager &AM) {
628 auto &FAM = AM.getResult(L).getManager();
629 Function *F = L.getHeader()->getParent();
630
631 auto *LI = FAM.getCachedResult(*F);
632 const auto *TTI = FAM.getCachedResult(*F);
633 auto *AC = FAM.getCachedResult(*F);
634 assert((LI && TTI && AC) && "Analyses for loop rotation not available");
635
636 // Optional analyses.
637 auto *DT = FAM.getCachedResult(*F);
638 auto *SE = FAM.getCachedResult(*F);
627 PreservedAnalyses LoopRotatePass::run(Loop &L, LoopAnalysisManager &AM,
628 LoopStandardAnalysisResults &AR,
629 LPMUpdater &) {
639630 int Threshold = EnableHeaderDuplication ? DefaultRotationThreshold : 0;
640 LoopRotate LR(Threshold, LI, TTI, AC, DT, SE);
631 LoopRotate LR(Threshold, &AR.LI, &AR.TTI, &AR.AC, &AR.DT, &AR.SE);
641632
642633 bool Changed = LR.processLoop(&L);
643634 if (!Changed)
6363 return Changed;
6464 }
6565
66 PreservedAnalyses LoopSimplifyCFGPass::run(Loop &L, LoopAnalysisManager &AM) {
67 const auto &FAM =
68 AM.getResult(L).getManager();
69 Function *F = L.getHeader()->getParent();
70
71 auto *LI = FAM.getCachedResult(*F);
72 auto *DT = FAM.getCachedResult(*F);
73 assert((LI && DT) && "Analyses for LoopSimplifyCFG not available");
74
75 if (!simplifyLoopCFG(L, *DT, *LI))
66 PreservedAnalyses LoopSimplifyCFGPass::run(Loop &L, LoopAnalysisManager &AM,
67 LoopStandardAnalysisResults &AR,
68 LPMUpdater &) {
69 if (!simplifyLoopCFG(L, AR.DT, AR.LI))
7670 return PreservedAnalyses::all();
7771 return getLoopPassPreservedAnalyses();
7872 }
50515051 return ReduceLoopStrength(L, IU, SE, DT, LI, TTI);
50525052 }
50535053
5054 PreservedAnalyses LoopStrengthReducePass::run(Loop &L,
5055 LoopAnalysisManager &AM) {
5056 const auto &FAM =
5057 AM.getResult(L).getManager();
5058 Function *F = L.getHeader()->getParent();
5059
5060 auto &IU = AM.getResult(L);
5061 auto *SE = FAM.getCachedResult(*F);
5062 auto *DT = FAM.getCachedResult(*F);
5063 auto *LI = FAM.getCachedResult(*F);
5064 auto *TTI = FAM.getCachedResult(*F);
5065 assert((SE && DT && LI && TTI) &&
5066 "Analyses for Loop Strength Reduce not available");
5067
5068 if (!ReduceLoopStrength(&L, IU, *SE, *DT, *LI, *TTI))
5054 PreservedAnalyses LoopStrengthReducePass::run(Loop &L, LoopAnalysisManager &AM,
5055 LoopStandardAnalysisResults &AR,
5056 LPMUpdater &) {
5057 if (!ReduceLoopStrength(&L, AM.getResult(L, AR), AR.SE,
5058 AR.DT, AR.LI, AR.TTI))
50695059 return PreservedAnalyses::all();
50705060
50715061 return getLoopPassPreservedAnalyses();
11101110 return llvm::createLoopUnrollPass(-1, -1, 0, 0, 0);
11111111 }
11121112
1113 PreservedAnalyses LoopUnrollPass::run(Loop &L, LoopAnalysisManager &AM) {
1113 PreservedAnalyses LoopUnrollPass::run(Loop &L, LoopAnalysisManager &AM,
1114 LoopStandardAnalysisResults &AR,
1115 LPMUpdater &) {
11141116 const auto &FAM =
1115 AM.getResult(L).getManager();
1117 AM.getResult(L, AR).getManager();
11161118 Function *F = L.getHeader()->getParent();
11171119
1118
1119 DominatorTree *DT = FAM.getCachedResult(*F);
1120 LoopInfo *LI = FAM.getCachedResult(*F);
1121 ScalarEvolution *SE = FAM.getCachedResult(*F);
1122 auto *TTI = FAM.getCachedResult(*F);
1123 auto *AC = FAM.getCachedResult(*F);
11241120 auto *ORE = FAM.getCachedResult(*F);
1125 if (!DT)
1126 report_fatal_error(
1127 "LoopUnrollPass: DominatorTreeAnalysis not cached at a higher level");
1128 if (!LI)
1129 report_fatal_error(
1130 "LoopUnrollPass: LoopAnalysis not cached at a higher level");
1131 if (!SE)
1132 report_fatal_error(
1133 "LoopUnrollPass: ScalarEvolutionAnalysis not cached at a higher level");
1134 if (!TTI)
1135 report_fatal_error(
1136 "LoopUnrollPass: TargetIRAnalysis not cached at a higher level");
1137 if (!AC)
1138 report_fatal_error(
1139 "LoopUnrollPass: AssumptionAnalysis not cached at a higher level");
1121 // FIXME: This should probably be optional rather than required.
11401122 if (!ORE)
11411123 report_fatal_error("LoopUnrollPass: OptimizationRemarkEmitterAnalysis not "
11421124 "cached at a higher level");
11431125
1144 bool Changed =
1145 tryToUnrollLoop(&L, *DT, LI, SE, *TTI, *AC, *ORE, /*PreserveLCSSA*/ true,
1146 ProvidedCount, ProvidedThreshold, ProvidedAllowPartial,
1147 ProvidedRuntime, ProvidedUpperBound);
1126 bool Changed = tryToUnrollLoop(&L, AR.DT, &AR.LI, &AR.SE, AR.TTI, AR.AC, *ORE,
1127 /*PreserveLCSSA*/ true, ProvidedCount,
1128 ProvidedThreshold, ProvidedAllowPartial,
1129 ProvidedRuntime, ProvidedUpperBound);
11481130
11491131 if (!Changed)
11501132 return PreservedAnalyses::all();
76407640 auto &TTI = AM.getResult(F);
76417641 auto &DT = AM.getResult(F);
76427642 auto &BFI = AM.getResult(F);
7643 auto *TLI = AM.getCachedResult(F);
7643 auto &TLI = AM.getResult(F);
76447644 auto &AA = AM.getResult(F);
76457645 auto &AC = AM.getResult(F);
76467646 auto &DB = AM.getResult(F);
76497649 auto &LAM = AM.getResult(F).getManager();
76507650 std::function GetLAA =
76517651 [&](Loop &L) -> const LoopAccessInfo & {
7652 return LAM.getResult(L);
7652 LoopStandardAnalysisResults AR = {AA, AC, DT, LI, SE, TLI, TTI};
7653 return LAM.getResult(L, AR);
76537654 };
76547655 bool Changed =
7655 runImpl(F, SE, LI, TTI, DT, BFI, TLI, DB, AA, AC, GetLAA, ORE);
7656 runImpl(F, SE, LI, TTI, DT, BFI, &TLI, DB, AA, AC, GetLAA, ORE);
76567657 if (!Changed)
76577658 return PreservedAnalyses::all();
76587659 PreservedAnalyses PA;
77 ; / \ \
88 ; loop.0.0 loop.0.1 loop.1.0
99 ;
10 ; CHECK: Running pass: NoOpLoopPass on loop.1.0
11 ; CHECK: Running pass: NoOpLoopPass on loop.1
12 ; CHECK: Running pass: NoOpLoopPass on loop.0.0
13 ; CHECK: Running pass: NoOpLoopPass on loop.0.1
14 ; CHECK: Running pass: NoOpLoopPass on loop.0
10 ; CHECK: Running pass: NoOpLoopPass on Loop at depth 2 containing: %loop.0.0
11 ; CHECK: Running pass: NoOpLoopPass on Loop at depth 2 containing: %loop.0.1
12 ; CHECK: Running pass: NoOpLoopPass on Loop at depth 1 containing: %loop.0
13 ; CHECK: Running pass: NoOpLoopPass on Loop at depth 2 containing: %loop.1.0
14 ; CHECK: Running pass: NoOpLoopPass on Loop at depth 1 containing: %loop.1
15
1516 define void @f() {
1617 entry:
1718 br label %loop.0
432432 ; CHECK-O: Running pass: TailCallElimPass
433433 ; CHECK-O: Running pass: SimplifyCFGPass
434434 ; CHECK-O: Running pass: ReassociatePass
435 ; CHECK-O: Starting llvm::Loop pass manager run.
436 ; CHECK-O: Finished llvm::Loop pass manager run.
437 ; CHECK-O: Running pass: SimplifyCFGPass
438 ; CHECK-O: Running pass: InstCombinePass
439 ; CHECK-O: Starting llvm::Loop pass manager run.
440 ; CHECK-O: Finished llvm::Loop pass manager run.
435 ; CHECK-O: Starting Loop pass manager run.
436 ; CHECK-O: Finished Loop pass manager run.
437 ; CHECK-O: Running pass: SimplifyCFGPass
438 ; CHECK-O: Running pass: InstCombinePass
439 ; CHECK-O: Starting Loop pass manager run.
440 ; CHECK-O: Finished Loop pass manager run.
441441 ; CHECK-O: Running pass: MemCpyOptPass
442442 ; CHECK-O: Running pass: SCCPPass
443443 ; CHECK-O: Running pass: BDCEPass
543543 ; CHECK-REPEAT-LOOP-PASS-NEXT: Running analysis: DominatorTreeAnalysis
544544 ; CHECK-REPEAT-LOOP-PASS-NEXT: Running analysis: AAManager
545545 ; CHECK-REPEAT-LOOP-PASS-NEXT: Running analysis: TargetLibraryAnalysis
546 ; CHECK-REPEAT-LOOP-PASS-NEXT: Running analysis: AssumptionAnalysis
546547 ; CHECK-REPEAT-LOOP-PASS-NEXT: Running analysis: ScalarEvolutionAnalysis
547 ; CHECK-REPEAT-LOOP-PASS-NEXT: Running analysis: AssumptionAnalysis
548 ; CHECK-REPEAT-LOOP-PASS-NEXT: Starting llvm::Loop pass manager run
548 ; CHECK-REPEAT-LOOP-PASS-NEXT: Running analysis: TargetIRAnalysis
549 ; CHECK-REPEAT-LOOP-PASS-NEXT: Starting Loop pass manager run
549550 ; CHECK-REPEAT-LOOP-PASS-NEXT: Running pass: RepeatedPass
550 ; CHECK-REPEAT-LOOP-PASS-NEXT: Starting llvm::Loop pass manager run
551 ; CHECK-REPEAT-LOOP-PASS-NEXT: Starting Loop pass manager run
551552 ; CHECK-REPEAT-LOOP-PASS-NEXT: Running pass: NoOpLoopPass
552 ; CHECK-REPEAT-LOOP-PASS-NEXT: Finished llvm::Loop pass manager run
553 ; CHECK-REPEAT-LOOP-PASS-NEXT: Starting llvm::Loop pass manager run
553 ; CHECK-REPEAT-LOOP-PASS-NEXT: Finished Loop pass manager run
554 ; CHECK-REPEAT-LOOP-PASS-NEXT: Starting Loop pass manager run
554555 ; CHECK-REPEAT-LOOP-PASS-NEXT: Running pass: NoOpLoopPass
555 ; CHECK-REPEAT-LOOP-PASS-NEXT: Finished llvm::Loop pass manager run
556 ; CHECK-REPEAT-LOOP-PASS-NEXT: Starting llvm::Loop pass manager run
556 ; CHECK-REPEAT-LOOP-PASS-NEXT: Finished Loop pass manager run
557 ; CHECK-REPEAT-LOOP-PASS-NEXT: Starting Loop pass manager run
557558 ; CHECK-REPEAT-LOOP-PASS-NEXT: Running pass: NoOpLoopPass
558 ; CHECK-REPEAT-LOOP-PASS-NEXT: Finished llvm::Loop pass manager run
559 ; CHECK-REPEAT-LOOP-PASS-NEXT: Finished llvm::Loop pass manager run
559 ; CHECK-REPEAT-LOOP-PASS-NEXT: Finished Loop pass manager run
560 ; CHECK-REPEAT-LOOP-PASS-NEXT: Finished Loop pass manager run
560561 ; CHECK-REPEAT-LOOP-PASS-NEXT: Finished llvm::Function pass manager run
561562 ; CHECK-REPEAT-LOOP-PASS-NEXT: Finished llvm::Module pass manager run
562563
143143 ; CHECK-TWO-NOOP-LOOP: Running pass: ModuleToFunctionPassAdaptor
144144 ; CHECK-TWO-NOOP-LOOP: Starting llvm::Function pass manager run
145145 ; CHECK-TWO-NOOP-LOOP: Running pass: FunctionToLoopPassAdaptor
146 ; CHECK-TWO-NOOP-LOOP: Starting llvm::Loop pass manager run
146 ; CHECK-TWO-NOOP-LOOP: Starting Loop pass manager run
147147 ; CHECK-TWO-NOOP-LOOP: Running pass: NoOpLoopPass
148148 ; CHECK-TWO-NOOP-LOOP: Running pass: NoOpLoopPass
149 ; CHECK-TWO-NOOP-LOOP: Finished llvm::Loop pass manager run
149 ; CHECK-TWO-NOOP-LOOP: Finished Loop pass manager run
150150 ; CHECK-TWO-NOOP-LOOP: Finished llvm::Function pass manager run
151151 ; CHECK-TWO-NOOP-LOOP: Finished llvm::Module pass manager run
152152
166166 ; CHECK-NESTED-FP-LP: Running pass: ModuleToFunctionPassAdaptor
167167 ; CHECK-NESTED-FP-LP: Starting llvm::Function pass manager run
168168 ; CHECK-NESTED-FP-LP: Running pass: FunctionToLoopPassAdaptor
169 ; CHECK-NESTED-FP-LP: Starting llvm::Loop pass manager run
169 ; CHECK-NESTED-FP-LP: Starting Loop pass manager run
170170 ; CHECK-NESTED-FP-LP: Running pass: NoOpLoopPass
171 ; CHECK-NESTED-FP-LP: Finished llvm::Loop pass manager run
171 ; CHECK-NESTED-FP-LP: Finished Loop pass manager run
172172 ; CHECK-NESTED-FP-LP: Finished llvm::Function pass manager run
173173 ; CHECK-NESTED-FP-LP: Finished llvm::Module pass manager run
174174
1111 #include "llvm/Analysis/LoopPassManager.h"
1212 #include "llvm/Analysis/ScalarEvolution.h"
1313 #include "llvm/Analysis/TargetLibraryInfo.h"
14 #include "llvm/Analysis/TargetTransformInfo.h"
1415 #include "llvm/AsmParser/Parser.h"
1516 #include "llvm/IR/Dominators.h"
1617 #include "llvm/IR/Function.h"
1819 #include "llvm/IR/Module.h"
1920 #include "llvm/IR/PassManager.h"
2021 #include "llvm/Support/SourceMgr.h"
22 #include "gmock/gmock.h"
2123 #include "gtest/gtest.h"
2224
2325 using namespace llvm;
2426
2527 namespace {
2628
27 class TestLoopAnalysis : public AnalysisInfoMixin {
28 friend AnalysisInfoMixin;
29 static AnalysisKey Key;
30
31 int &Runs;
32
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 {
3340 public:
34 struct Result {
35 Result(int Count) : BlockCount(Count) {}
36 int BlockCount;
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 }
3769 };
3870
39 TestLoopAnalysis(int &Runs) : Runs(Runs) {}
40
41 /// \brief Run the analysis pass over the loop and return a result.
42 Result run(Loop &L, LoopAnalysisManager &AM) {
43 ++Runs;
44 int Count = 0;
45
46 for (auto I = L.block_begin(), E = L.block_end(); I != E; ++I)
47 ++Count;
48 return Result(Count);
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 /// Derived classes should call this in their constructor to set up default
78 /// mock actions. (We can't do this in our constructor because this has to
79 /// run after the DerivedT is constructed.)
80 void setDefaults() {
81 ON_CALL(static_cast(*this),
82 run(_, _, testing::Matcher(_)...))
83 .WillByDefault(Return(this->getResult()));
84 ON_CALL(static_cast(*this), invalidate(_, _, _))
85 .WillByDefault(Invoke([](IRUnitT &, const PreservedAnalyses &PA,
86 typename AnalysisManagerT::Invalidator &Inv) {
87 auto PAC = PA.getChecker();
88 return !PAC.preserved() &&
89 !PAC.template preservedSet>();
90 }));
4991 }
5092 };
5193
52 AnalysisKey TestLoopAnalysis::Key;
53
54 class TestLoopPass {
55 std::vector &VisitedLoops;
56 int &AnalyzedBlockCount;
57 bool OnlyUseCachedResults;
58
94 template
95 typename... ExtraArgTs>
96 AnalysisKey MockAnalysisHandleBase
97 ExtraArgTs...>::Analysis::Key;
98
99 /// Mock handle for loop analyses.
100 ///
101 /// This is provided as a template accepting an (optional) integer. Because
102 /// analyses are identified and queried by type, this allows constructing
103 /// multiple handles with distinctly typed nested 'Analysis' types that can be
104 /// registered and queried. If you want to register multiple loop analysis
105 /// passes, you'll need to instantiate this type with different values for I.
106 /// For example:
107 ///
108 /// MockLoopAnalysisHandleTemplate<0> h0;
109 /// MockLoopAnalysisHandleTemplate<1> h1;
110 /// typedef decltype(h0)::Analysis Analysis0;
111 /// typedef decltype(h1)::Analysis Analysis1;
112 template (-1)>
113 struct MockLoopAnalysisHandleTemplate
114 : MockAnalysisHandleBase, Loop,
115 LoopAnalysisManager,
116 LoopStandardAnalysisResults &> {
117 typedef typename MockLoopAnalysisHandleTemplate::Analysis Analysis;
118
119 MOCK_METHOD3_T(run, typename Analysis::Result(Loop &, LoopAnalysisManager &,
120 LoopStandardAnalysisResults &));
121
122 MOCK_METHOD3_T(invalidate, bool(Loop &, const PreservedAnalyses &,
123 LoopAnalysisManager::Invalidator &));
124
125 MockLoopAnalysisHandleTemplate() { this->setDefaults(); }
126 };
127
128 typedef MockLoopAnalysisHandleTemplate<> MockLoopAnalysisHandle;
129
130 struct MockFunctionAnalysisHandle
131 : MockAnalysisHandleBase {
132 MOCK_METHOD2(run, Analysis::Result(Function &, FunctionAnalysisManager &));
133
134 MOCK_METHOD3(invalidate, bool(Function &, const PreservedAnalyses &,
135 FunctionAnalysisManager::Invalidator &));
136
137 MockFunctionAnalysisHandle() { setDefaults(); }
138 };
139
140 template
141 typename AnalysisManagerT = AnalysisManager,
142 typename... ExtraArgTs>
143 class MockPassHandleBase {
59144 public:
60 TestLoopPass(std::vector &VisitedLoops, int &AnalyzedBlockCount,
61 bool OnlyUseCachedResults = false)
62 : VisitedLoops(VisitedLoops), AnalyzedBlockCount(AnalyzedBlockCount),
63 OnlyUseCachedResults(OnlyUseCachedResults) {}
64
65 PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM) {
66 VisitedLoops.push_back(L.getName());
67
68 if (OnlyUseCachedResults) {
69 // Hack to force the use of the cached interface.
70 if (auto *AR = AM.getCachedResult(L))
71 AnalyzedBlockCount += AR->BlockCount;
72 } else {
73 // Typical path just runs the analysis as needed.
74 auto &AR = AM.getResult(L);
75 AnalyzedBlockCount += AR.BlockCount;
145 class Pass : public PassInfoMixin {
146 friend MockPassHandleBase;
147
148 DerivedT *Handle;
149
150 Pass(DerivedT &Handle) : Handle(&Handle) {}
151
152 public:
153 PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM,
154 ExtraArgTs... ExtraArgs) {
155 return Handle->run(IR, AM, ExtraArgs...);
76156 }
77
78 return PreservedAnalyses::all();
157 };
158
159 Pass getPass() { return Pass(static_cast(*this)); }
160
161 protected:
162 /// Derived classes should call this in their constructor to set up default
163 /// mock actions. (We can't do this in our constructor because this has to
164 /// run after the DerivedT is constructed.)
165 void setDefaults() {
166 ON_CALL(static_cast(*this),
167 run(_, _, testing::Matcher(_)...))
168 .WillByDefault(Return(PreservedAnalyses::all()));
79169 }
80
81 static StringRef name() { return "TestLoopPass"; }
82170 };
83171
84 // A test loop pass that invalidates the analysis for loops with the given name.
85 class TestLoopInvalidatingPass {
86 StringRef Name;
87
88 public:
89 TestLoopInvalidatingPass(StringRef LoopName) : Name(LoopName) {}
90
91 PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM) {
92 return L.getName() == Name ? getLoopPassPreservedAnalyses()
93 : PreservedAnalyses::all();
94 }
95
96 static StringRef name() { return "TestLoopInvalidatingPass"; }
172 struct MockLoopPassHandle
173 : MockPassHandleBase
174 LoopStandardAnalysisResults &, LPMUpdater &> {
175 MOCK_METHOD4(run,
176 PreservedAnalyses(Loop &, LoopAnalysisManager &,
177 LoopStandardAnalysisResults &, LPMUpdater &));
178 MockLoopPassHandle() { setDefaults(); }
97179 };
180
181 struct MockFunctionPassHandle
182 : MockPassHandleBase {
183 MOCK_METHOD2(run, PreservedAnalyses(Function &, FunctionAnalysisManager &));
184
185 MockFunctionPassHandle() { setDefaults(); }
186 };
187
188 struct MockModulePassHandle : MockPassHandleBase {
189 MOCK_METHOD2(run, PreservedAnalyses(Module &, ModuleAnalysisManager &));
190
191 MockModulePassHandle() { setDefaults(); }
192 };
193
194 /// Define a custom matcher for objects which support a 'getName' method
195 /// returning a StringRef.
196 ///
197 /// LLVM often has IR objects or analysis objects which expose a StringRef name
198 /// and in tests it is convenient to match these by name for readability. This
199 /// matcher supports any type exposing a getName() method of this form.
200 ///
201 /// It should be used as:
202 ///
203 /// HasName("my_function")
204 ///
205 /// No namespace or other qualification is required.
206 MATCHER_P(HasName, Name, "") {
207 // The matcher's name and argument are printed in the case of failure, but we
208 // also want to print out the name of the argument. This uses an implicitly
209 // avaiable std::ostream, so we have to construct a std::string.
210 *result_listener << "has name '" << arg.getName().str() << "'";
211 return Name == arg.getName();
212 }
98213
99214 std::unique_ptr parseIR(LLVMContext &C, const char *IR) {
100215 SMDiagnostic Err;
105220 protected:
106221 LLVMContext Context;
107222 std::unique_ptr M;
223
224 LoopAnalysisManager LAM;
225 FunctionAnalysisManager FAM;
226 ModuleAnalysisManager MAM;
227
228 MockLoopAnalysisHandle MLAHandle;
229 MockLoopPassHandle MLPHandle;
230 MockFunctionPassHandle MFPHandle;
231 MockModulePassHandle MMPHandle;
232
233 static PreservedAnalyses
234 getLoopAnalysisResult(Loop &L, LoopAnalysisManager &AM,
235 LoopStandardAnalysisResults &AR, LPMUpdater &) {
236 (void)AM.getResult(L, AR);
237 return PreservedAnalyses::all();
238 };
108239
109240 public:
110241 LoopPassManagerTest()
128259 " br i1 undef, label %loop.g.0, label %end\n"
129260 "end:\n"
130261 " ret void\n"
131 "}\n")) {}
262 "}\n")),
263 LAM(true), FAM(true), MAM(true) {
264 // Register our mock analysis.
265 LAM.registerPass([&] { return MLAHandle.getAnalysis(); });
266
267 // We need DominatorTreeAnalysis for LoopAnalysis.
268 FAM.registerPass([&] { return DominatorTreeAnalysis(); });
269 FAM.registerPass([&] { return LoopAnalysis(); });
270 // We also allow loop passes to assume a set of other analyses and so need
271 // those.
272 FAM.registerPass([&] { return AAManager(); });
273 FAM.registerPass([&] { return AssumptionAnalysis(); });
274 FAM.registerPass([&] { return ScalarEvolutionAnalysis(); });
275 FAM.registerPass([&] { return TargetLibraryAnalysis(); });
276 FAM.registerPass([&] { return TargetIRAnalysis(); });
277
278 // Cross-register proxies.
279 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
280 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
281 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
282 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
283 }
132284 };
133285
134 #define EXPECT_N_ELEMENTS_EQ(N, EXPECTED, ACTUAL) \
135 do { \
136 EXPECT_EQ(N##UL, ACTUAL.size()); \
137 for (int I = 0; I < N; ++I) \
138 EXPECT_TRUE(EXPECTED[I] == ACTUAL[I]) << "Element " << I << " is " \
139 << ACTUAL[I] << ". Expected " \
140 << EXPECTED[I] << "."; \
141 } while (0)
142
143286 TEST_F(LoopPassManagerTest, Basic) {
144 LoopAnalysisManager LAM(true);
145 int LoopAnalysisRuns = 0;
146 LAM.registerPass([&] { return TestLoopAnalysis(LoopAnalysisRuns); });
147
148 FunctionAnalysisManager FAM(true);
149 // We need DominatorTreeAnalysis for LoopAnalysis.
150 FAM.registerPass([&] { return DominatorTreeAnalysis(); });
151 FAM.registerPass([&] { return LoopAnalysis(); });
152 // We also allow loop passes to assume a set of other analyses and so need
153 // those.
154 FAM.registerPass([&] { return AAManager(); });
155 FAM.registerPass([&] { return TargetLibraryAnalysis(); });
156 FAM.registerPass([&] { return ScalarEvolutionAnalysis(); });
157 FAM.registerPass([&] { return AssumptionAnalysis(); });
158 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
159 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
160
161 ModuleAnalysisManager MAM(true);
162 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
163 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
164
287 ModulePassManager MPM(true);
288 ::testing::InSequence MakeExpectationsSequenced;
289
290 // First we just visit all the loops in all the functions and get their
291 // analysis results. This will run the analysis a total of four times,
292 // once for each loop.
293 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
294 .WillOnce(Invoke(getLoopAnalysisResult));
295 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
296 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _))
297 .WillOnce(Invoke(getLoopAnalysisResult));
298 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
299 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _))
300 .WillOnce(Invoke(getLoopAnalysisResult));
301 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
302 EXPECT_CALL(MLPHandle, run(HasName("loop.g.0"), _, _, _))
303 .WillOnce(Invoke(getLoopAnalysisResult));
304 EXPECT_CALL(MLAHandle, run(HasName("loop.g.0"), _, _));
305 // Wire the loop pass through pass managers into the module pipeline.
306 {
307 LoopPassManager LPM(true);
308 LPM.addPass(MLPHandle.getPass());
309 FunctionPassManager FPM(true);
310 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
311 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
312 }
313
314 // Next we run two passes over the loops. The first one invalidates the
315 // analyses for one loop, the second ones try to get the analysis results.
316 // This should force only one analysis to re-run within the loop PM, but will
317 // also invalidate everything after the loop pass manager finishes.
318 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
319 .WillOnce(DoDefault())
320 .WillOnce(Invoke(getLoopAnalysisResult));
321 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _))
322 .WillOnce(InvokeWithoutArgs([] { return PreservedAnalyses::none(); }))
323 .WillOnce(Invoke(getLoopAnalysisResult));
324 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
325 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _))
326 .WillOnce(DoDefault())
327 .WillOnce(Invoke(getLoopAnalysisResult));
328 EXPECT_CALL(MLPHandle, run(HasName("loop.g.0"), _, _, _))
329 .WillOnce(DoDefault())
330 .WillOnce(Invoke(getLoopAnalysisResult));
331 // Wire two loop pass runs into the module pipeline.
332 {
333 LoopPassManager LPM(true);
334 LPM.addPass(MLPHandle.getPass());
335 LPM.addPass(MLPHandle.getPass());
336 FunctionPassManager FPM(true);
337 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
338 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
339 }
340
341 // And now run the pipeline across the module.
342 MPM.run(*M, MAM);
343 }
344
345 TEST_F(LoopPassManagerTest, FunctionPassInvalidationOfLoopAnalyses) {
165346 ModulePassManager MPM(true);
166347 FunctionPassManager FPM(true);
167
168 // Visit all of the loops.
169 std::vector VisitedLoops1;
170 int AnalyzedBlockCount1 = 0;
171 {
172 LoopPassManager LPM;
173 LPM.addPass(TestLoopPass(VisitedLoops1, AnalyzedBlockCount1));
174
175 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
176 }
177
178 // Only use cached analyses.
179 std::vector VisitedLoops2;
180 int AnalyzedBlockCount2 = 0;
181 {
182 LoopPassManager LPM;
183 LPM.addPass(TestLoopInvalidatingPass("loop.g.0"));
184 LPM.addPass(TestLoopPass(VisitedLoops2, AnalyzedBlockCount2,
185 /*OnlyUseCachedResults=*/true));
186
187 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
188 }
348 // We process each function completely in sequence.
349 ::testing::Sequence FSequence, GSequence;
350
351 // First, force the analysis result to be computed for each loop.
352 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _))
353 .InSequence(FSequence)
354 .WillOnce(DoDefault());
355 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _))
356 .InSequence(FSequence)
357 .WillOnce(DoDefault());
358 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _))
359 .InSequence(FSequence)
360 .WillOnce(DoDefault());
361 EXPECT_CALL(MLAHandle, run(HasName("loop.g.0"), _, _))
362 .InSequence(GSequence)
363 .WillOnce(DoDefault());
364 FPM.addPass(createFunctionToLoopPassAdaptor(
365 RequireAnalysisLoopPass()));
366
367 // No need to re-run if we require again from a fresh loop pass manager.
368 FPM.addPass(createFunctionToLoopPassAdaptor(
369 RequireAnalysisLoopPass()));
370
371 // For 'f', preserve most things but not the specific loop analyses.
372 EXPECT_CALL(MFPHandle, run(HasName("f"), _))
373 .InSequence(FSequence)
374 .WillOnce(Return(getLoopPassPreservedAnalyses()));
375 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.0.0"), _, _))
376 .InSequence(FSequence)
377 .WillOnce(DoDefault());
378 // On one loop, skip the invalidation (as though we did an internal update).
379 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.0.1"), _, _))
380 .InSequence(FSequence)
381 .WillOnce(Return(false));
382 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.0"), _, _))
383 .InSequence(FSequence)
384 .WillOnce(DoDefault());
385 // Now two loops still have to be recomputed.
386 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _))
387 .InSequence(FSequence)
388 .WillOnce(DoDefault());
389 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _))
390 .InSequence(FSequence)
391 .WillOnce(DoDefault());
392 // Preserve things in the second function to ensure invalidation remains
393 // isolated to one function.
394 EXPECT_CALL(MFPHandle, run(HasName("g"), _))
395 .InSequence(GSequence)
396 .WillOnce(DoDefault());
397 FPM.addPass(MFPHandle.getPass());
398 FPM.addPass(createFunctionToLoopPassAdaptor(
399 RequireAnalysisLoopPass()));
400
401 EXPECT_CALL(MFPHandle, run(HasName("f"), _))
402 .InSequence(FSequence)
403 .WillOnce(DoDefault());
404 // For 'g', fail to preserve anything, causing the loops themselves to be
405 // cleared. We don't get an invalidation event here as the loop is gone, but
406 // we should still have to recompute the analysis.
407 EXPECT_CALL(MFPHandle, run(HasName("g"), _))
408 .InSequence(GSequence)
409 .WillOnce(Return(PreservedAnalyses::none()));
410 EXPECT_CALL(MLAHandle, run(HasName("loop.g.0"), _, _))
411 .InSequence(GSequence)
412 .WillOnce(DoDefault());
413 FPM.addPass(MFPHandle.getPass());
414 FPM.addPass(createFunctionToLoopPassAdaptor(
415 RequireAnalysisLoopPass()));
416
417 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
418
419 // Verify with a separate function pass run that we didn't mess up 'f's
420 // cache. No analysis runs should be necessary here.
421 MPM.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor(
422 RequireAnalysisLoopPass())));
423
424 MPM.run(*M, MAM);
425 }
426
427 TEST_F(LoopPassManagerTest, ModulePassInvalidationOfLoopAnalyses) {
428 ModulePassManager MPM(true);
429 ::testing::InSequence MakeExpectationsSequenced;
430
431 // First, force the analysis result to be computed for each loop.
432 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
433 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
434 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
435 EXPECT_CALL(MLAHandle, run(HasName("loop.g.0"), _, _));
436 MPM.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor(
437 RequireAnalysisLoopPass())));
438
439 // Walking all the way out and all the way back in doesn't re-run the
440 // analysis.
441 MPM.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor(
442 RequireAnalysisLoopPass())));
443
444 // But a module pass that doesn't preserve the actual mock loop analysis
445 // invalidates all the way down and forces recomputing.
446 EXPECT_CALL(MMPHandle, run(_, _)).WillOnce(InvokeWithoutArgs([] {
447 auto PA = getLoopPassPreservedAnalyses();
448 PA.preserve();
449 return PA;
450 }));
451 // All the loop analyses from both functions get invalidated before we
452 // recompute anything.
453 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.0.0"), _, _));
454 // On one loop, again skip the invalidation (as though we did an internal
455 // update).
456 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.0.1"), _, _))
457 .WillOnce(Return(false));
458 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.0"), _, _));
459 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.g.0"), _, _));
460 // Now all but one of the loops gets re-analyzed.
461 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
462 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
463 EXPECT_CALL(MLAHandle, run(HasName("loop.g.0"), _, _));
464 MPM.addPass(MMPHandle.getPass());
465 MPM.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor(
466 RequireAnalysisLoopPass())));
467
468 // Verify that the cached values persist.
469 MPM.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor(
470 RequireAnalysisLoopPass())));
471
472 // Now we fail to preserve the loop analysis and observe that the loop
473 // analyses are cleared (so no invalidation event) as the loops themselves
474 // are no longer valid.
475 EXPECT_CALL(MMPHandle, run(_, _)).WillOnce(InvokeWithoutArgs([] {
476 auto PA = PreservedAnalyses::none();
477 PA.preserve();
478 return PA;
479 }));
480 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
481 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
482 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
483 EXPECT_CALL(MLAHandle, run(HasName("loop.g.0"), _, _));
484 MPM.addPass(MMPHandle.getPass());
485 MPM.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor(
486 RequireAnalysisLoopPass())));
487
488 // Verify that the cached values persist.
489 MPM.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor(
490 RequireAnalysisLoopPass())));
491
492 // Next, check that even if we preserve everything within the function itelf,
493 // if the function's module pass proxy isn't preserved and the potential set
494 // of functions changes, the clear reaches the loop analyses as well. This
495 // will again trigger re-runs but not invalidation events.
496 EXPECT_CALL(MMPHandle, run(_, _)).WillOnce(InvokeWithoutArgs([] {
497 auto PA = PreservedAnalyses::none();
498 PA.preserveSet>();
499 PA.preserveSet>();
500 return PA;
501 }));
502 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
503 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
504 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
505 EXPECT_CALL(MLAHandle, run(HasName("loop.g.0"), _, _));
506 MPM.addPass(MMPHandle.getPass());
507 MPM.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor(
508 RequireAnalysisLoopPass())));
509
510 MPM.run(*M, MAM);
511 }
512
513 // Test that if any of the bundled analyses provided in the LPM's signature
514 // become invalid, the analysis proxy itself becomes invalid and we clear all
515 // loop analysis results.
516 TEST_F(LoopPassManagerTest, InvalidationOfBundledAnalyses) {
517 ModulePassManager MPM(true);
518 FunctionPassManager FPM(true);
519 ::testing::InSequence MakeExpectationsSequenced;
520
521 // First, force the analysis result to be computed for each loop.
522 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
523 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
524 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
525 FPM.addPass(createFunctionToLoopPassAdaptor(
526 RequireAnalysisLoopPass()));
527
528 // No need to re-run if we require again from a fresh loop pass manager.
529 FPM.addPass(createFunctionToLoopPassAdaptor(
530 RequireAnalysisLoopPass()));
531
532 // Preserving everything but the loop analyses themselves results in
533 // invalidation and running.
534 EXPECT_CALL(MFPHandle, run(HasName("f"), _))
535 .WillOnce(Return(getLoopPassPreservedAnalyses()));
536 EXPECT_CALL(MLAHandle, invalidate(_, _, _)).Times(3);
537 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
538 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
539 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
540 FPM.addPass(MFPHandle.getPass());
541 FPM.addPass(createFunctionToLoopPassAdaptor(
542 RequireAnalysisLoopPass()));
543
544 // The rest don't invalidate analyses, they only trigger re-runs because we
545 // clear the cache completely.
546 EXPECT_CALL(MFPHandle, run(HasName("f"), _)).WillOnce(InvokeWithoutArgs([] {
547 auto PA = PreservedAnalyses::none();
548 // Not preserving `AAManager`.
549 PA.preserve();
550 PA.preserve();
551 PA.preserve();
552 PA.preserve();
553 PA.preserve();
554 return PA;
555 }));
556 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
557 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
558 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
559 FPM.addPass(MFPHandle.getPass());
560 FPM.addPass(createFunctionToLoopPassAdaptor(
561 RequireAnalysisLoopPass()));
562
563 EXPECT_CALL(MFPHandle, run(HasName("f"), _)).WillOnce(InvokeWithoutArgs([] {
564 auto PA = PreservedAnalyses::none();
565 PA.preserve();
566 // Not preserving `AssumptionAnalysis`.
567 PA.preserve();
568 PA.preserve();
569 PA.preserve();
570 PA.preserve();
571 return PA;
572 }));
573 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
574 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
575 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
576 FPM.addPass(MFPHandle.getPass());
577 FPM.addPass(createFunctionToLoopPassAdaptor(
578 RequireAnalysisLoopPass()));
579
580 EXPECT_CALL(MFPHandle, run(HasName("f"), _)).WillOnce(InvokeWithoutArgs([] {
581 auto PA = PreservedAnalyses::none();
582 PA.preserve();
583 PA.preserve();
584 // Not preserving `DominatorTreeAnalysis`.
585 PA.preserve();
586 PA.preserve();
587 PA.preserve();
588 return PA;
589 }));
590 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
591 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
592 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
593 FPM.addPass(MFPHandle.getPass());
594 FPM.addPass(createFunctionToLoopPassAdaptor(
595 RequireAnalysisLoopPass()));
596
597 EXPECT_CALL(MFPHandle, run(HasName("f"), _)).WillOnce(InvokeWithoutArgs([] {
598 auto PA = PreservedAnalyses::none();
599 PA.preserve();
600 PA.preserve();
601 PA.preserve();
602 // Not preserving the `LoopAnalysis`.
603 PA.preserve();
604 PA.preserve();
605 return PA;
606 }));
607 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
608 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
609 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
610 FPM.addPass(MFPHandle.getPass());
611 FPM.addPass(createFunctionToLoopPassAdaptor(
612 RequireAnalysisLoopPass()));
613
614 EXPECT_CALL(MFPHandle, run(HasName("f"), _)).WillOnce(InvokeWithoutArgs([] {
615 auto PA = PreservedAnalyses::none();
616 PA.preserve();
617 PA.preserve();
618 PA.preserve();
619 PA.preserve();
620 // Not preserving the `LoopAnalysisManagerFunctionProxy`.
621 PA.preserve();
622 return PA;
623 }));
624 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
625 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
626 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
627 FPM.addPass(MFPHandle.getPass());
628 FPM.addPass(createFunctionToLoopPassAdaptor(
629 RequireAnalysisLoopPass()));
630
631 EXPECT_CALL(MFPHandle, run(HasName("f"), _)).WillOnce(InvokeWithoutArgs([] {
632 auto PA = PreservedAnalyses::none();
633 PA.preserve();
634 PA.preserve();
635 PA.preserve();
636 PA.preserve();
637 PA.preserve();
638 // Not preserving `ScalarEvolutionAnalysis`.
639 return PA;
640 }));
641 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
642 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
643 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
644 FPM.addPass(MFPHandle.getPass());
645 FPM.addPass(createFunctionToLoopPassAdaptor(
646 RequireAnalysisLoopPass()));
647
648 // After all the churn on 'f', we'll compute the loop analysis results for
649 // 'g' once with a requires pass and then run our mock pass over g a bunch
650 // but just get cached results each time.
651 EXPECT_CALL(MLAHandle, run(HasName("loop.g.0"), _, _));
652 EXPECT_CALL(MFPHandle, run(HasName("g"), _)).Times(7);
189653
190654 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
191655 MPM.run(*M, MAM);
192
193 StringRef ExpectedLoops[] = {"loop.0.0", "loop.0.1", "loop.0", "loop.g.0"};
194
195 // Validate the counters and order of loops visited.
196 // loop.0 has 3 blocks whereas loop.0.0, loop.0.1, and loop.g.0 each have 1.
197 EXPECT_N_ELEMENTS_EQ(4, ExpectedLoops, VisitedLoops1);
198 EXPECT_EQ(6, AnalyzedBlockCount1);
199
200 EXPECT_N_ELEMENTS_EQ(4, ExpectedLoops, VisitedLoops2);
201 // The block from loop.g.0 won't be counted, since it wasn't cached.
202 EXPECT_EQ(5, AnalyzedBlockCount2);
203
204 // The first LPM runs the loop analysis for all four loops, the second uses
205 // cached results for everything.
206 EXPECT_EQ(4, LoopAnalysisRuns);
656 }
657
658 TEST_F(LoopPassManagerTest, IndirectInvalidation) {
659 // We need two distinct analysis types and handles.
660 enum { A, B };
661 MockLoopAnalysisHandleTemplate MLAHandleA;
662 MockLoopAnalysisHandleTemplate MLAHandleB;
663 LAM.registerPass([&] { return MLAHandleA.getAnalysis(); });
664 LAM.registerPass([&] { return MLAHandleB.getAnalysis(); });
665 typedef decltype(MLAHandleA)::Analysis AnalysisA;
666 typedef decltype(MLAHandleB)::Analysis AnalysisB;
667
668 // Set up AnalysisA to depend on our AnalysisB. For testing purposes we just
669 // need to get the AnalysisB results in AnalysisA's run method and check if
670 // AnalysisB gets invalidated in AnalysisA's invalidate method.
671 ON_CALL(MLAHandleA, run(_, _, _))
672 .WillByDefault(Invoke([&](Loop &L, LoopAnalysisManager &AM,
673 LoopStandardAnalysisResults &AR) {
674 (void)AM.getResult(L, AR);
675 return MLAHandleA.getResult();
676 }));
677 ON_CALL(MLAHandleA, invalidate(_, _, _))
678 .WillByDefault(Invoke([](Loop &L, const PreservedAnalyses &PA,
679 LoopAnalysisManager::Invalidator &Inv) {
680 auto PAC = PA.getChecker();
681 return !(PAC.preserved() || PAC.preservedSet>()) ||
682 Inv.invalidate(L, PA);
683 }));
684
685 ::testing::InSequence MakeExpectationsSequenced;
686
687 // Compute the analyses across all of 'f' first.
688 EXPECT_CALL(MLAHandleA, run(HasName("loop.0.0"), _, _));
689 EXPECT_CALL(MLAHandleB, run(HasName("loop.0.0"), _, _));
690 EXPECT_CALL(MLAHandleA, run(HasName("loop.0.1"), _, _));
691 EXPECT_CALL(MLAHandleB, run(HasName("loop.0.1"), _, _));
692 EXPECT_CALL(MLAHandleA, run(HasName("loop.0"), _, _));
693 EXPECT_CALL(MLAHandleB, run(HasName("loop.0"), _, _));
694
695 // Now we invalidate AnalysisB (but not AnalysisA) for one of the loops and
696 // preserve everything for the rest. This in turn triggers that one loop to
697 // recompute both AnalysisB *and* AnalysisA if indirect invalidation is
698 // working.
699 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
700 .WillOnce(InvokeWithoutArgs([] {
701 auto PA = getLoopPassPreservedAnalyses();
702 // Specifically preserve AnalysisA so that it would survive if it
703 // didn't depend on AnalysisB.
704 PA.preserve();
705 return PA;
706 }));
707 // It happens that AnalysisB is invalidated first. That shouldn't matter
708 // though, and we should still call AnalysisA's invalidation.
709 EXPECT_CALL(MLAHandleB, invalidate(HasName("loop.0.0"), _, _));
710 EXPECT_CALL(MLAHandleA, invalidate(HasName("loop.0.0"), _, _));
711 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
712 .WillOnce(Invoke([](Loop &L, LoopAnalysisManager &AM,
713 LoopStandardAnalysisResults &AR, LPMUpdater &) {
714 (void)AM.getResult(L, AR);
715 return PreservedAnalyses::all();
716 }));
717 EXPECT_CALL(MLAHandleA, run(HasName("loop.0.0"), _, _));
718 EXPECT_CALL(MLAHandleB, run(HasName("loop.0.0"), _, _));
719 // The rest of the loops should run and get cached results.
720 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _))
721 .Times(2)
722 .WillRepeatedly(Invoke([](Loop &L, LoopAnalysisManager &AM,
723 LoopStandardAnalysisResults &AR, LPMUpdater &) {
724 (void)AM.getResult(L, AR);
725 return PreservedAnalyses::all();
726 }));
727 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _))
728 .Times(2)
729 .WillRepeatedly(Invoke([](Loop &L, LoopAnalysisManager &AM,
730 LoopStandardAnalysisResults &AR, LPMUpdater &) {
731 (void)AM.getResult(L, AR);
732 return PreservedAnalyses::all();
733 }));
734
735 // The run over 'g' should be boring, with us just computing the analyses once
736 // up front and then running loop passes and getting cached results.
737 EXPECT_CALL(MLAHandleA, run(HasName("loop.g.0"), _, _));
738 EXPECT_CALL(MLAHandleB, run(HasName("loop.g.0"), _, _));
739 EXPECT_CALL(MLPHandle, run(HasName("loop.g.0"), _, _, _))
740 .Times(2)
741 .WillRepeatedly(Invoke([](Loop &L, LoopAnalysisManager &AM,
742 LoopStandardAnalysisResults &AR, LPMUpdater &) {
743 (void)AM.getResult(L, AR);
744 return PreservedAnalyses::all();
745 }));
746
747 // Build the pipeline and run it.
748 ModulePassManager MPM(true);
749 FunctionPassManager FPM(true);
750 FPM.addPass(
751 createFunctionToLoopPassAdaptor(RequireAnalysisLoopPass()));
752 LoopPassManager LPM(true);
753 LPM.addPass(MLPHandle.getPass());
754 LPM.addPass(MLPHandle.getPass());
755 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
756 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
757 MPM.run(*M, MAM);
758 }
759
760 TEST_F(LoopPassManagerTest, IndirectOuterPassInvalidation) {
761 typedef decltype(MLAHandle)::Analysis LoopAnalysis;
762
763 MockFunctionAnalysisHandle MFAHandle;
764 FAM.registerPass([&] { return MFAHandle.getAnalysis(); });
765 typedef decltype(MFAHandle)::Analysis FunctionAnalysis;
766
767 // Set up the loop analysis to depend on both the function and module
768 // analysis.
769 ON_CALL(MLAHandle, run(_, _, _))
770 .WillByDefault(Invoke([&](Loop &L, LoopAnalysisManager &AM,
771 LoopStandardAnalysisResults &AR) {
772 auto &FAMP = AM.getResult(L, AR);
773 auto &FAM = FAMP.getManager();
774 Function &F = *L.getHeader()->getParent();
775 if (auto *FA = FAM.getCachedResult(F))
776 FAMP.registerOuterAnalysisInvalidation
777 LoopAnalysis>();
778 return MLAHandle.getResult();
779 }));
780
781 ::testing::InSequence MakeExpectationsSequenced;
782
783 // Compute the analyses across all of 'f' first.
784 EXPECT_CALL(MFPHandle, run(HasName("f"), _))
785 .WillOnce(Invoke([](Function &F, FunctionAnalysisManager &AM) {
786 // Force the computing of the function analysis so it is available in
787 // this function.
788 (void)AM.getResult(F);
789 return PreservedAnalyses::all();
790 }));
791 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
792 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
793 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
794
795 // Now invalidate the function analysis but preserve the loop analyses.
796 // This should trigger immediate invalidation of the loop analyses, despite
797 // the fact that they were preserved.
798 EXPECT_CALL(MFPHandle, run(HasName("f"), _)).WillOnce(InvokeWithoutArgs([] {
799 auto PA = getLoopPassPreservedAnalyses();
800 PA.preserveSet>();
801 return PA;
802 }));
803 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.0.0"), _, _));
804 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.0.1"), _, _));
805 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.0"), _, _));
806
807 // And re-running a requires pass recomputes them.
808 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
809 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
810 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
811
812 // When we run over 'g' we don't populate the cache with the function
813 // analysis.
814 EXPECT_CALL(MFPHandle, run(HasName("g"), _))
815 .WillOnce(Return(PreservedAnalyses::all()));
816 EXPECT_CALL(MLAHandle, run(HasName("loop.g.0"), _, _));
817
818 // Which means that no extra invalidation occurs and cached values are used.
819 EXPECT_CALL(MFPHandle, run(HasName("g"), _)).WillOnce(InvokeWithoutArgs([] {
820 auto PA = getLoopPassPreservedAnalyses();
821 PA.preserveSet>();
822 return PA;
823 }));
824
825 // Build the pipeline and run it.
826 ModulePassManager MPM(true);
827 FunctionPassManager FPM(true);
828 FPM.addPass(MFPHandle.getPass());
829 FPM.addPass(
830 createFunctionToLoopPassAdaptor(RequireAnalysisLoopPass()));
831 FPM.addPass(MFPHandle.getPass());
832 FPM.addPass(
833 createFunctionToLoopPassAdaptor(RequireAnalysisLoopPass()));
834 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
835 MPM.run(*M, MAM);
836 }
837
838 TEST_F(LoopPassManagerTest, LoopChildInsertion) {
839 // Super boring module with three loops in a single loop nest.
840 M = parseIR(Context, "define void @f() {\n"
841 "entry:\n"
842 " br label %loop.0\n"
843 "loop.0:\n"
844 " br i1 undef, label %loop.0.0, label %end\n"
845 "loop.0.0:\n"
846 " br i1 undef, label %loop.0.0, label %loop.0.1\n"
847 "loop.0.1:\n"
848 " br i1 undef, label %loop.0.1, label %loop.0.2\n"
849 "loop.0.2:\n"
850 " br i1 undef, label %loop.0.2, label %loop.0\n"
851 "end:\n"
852 " ret void\n"
853 "}\n");
854
855 // Build up variables referring into the IR so we can rewrite it below
856 // easily.
857 Function &F = *M->begin();
858 ASSERT_THAT(F, HasName("f"));
859 auto BBI = F.begin();
860 BasicBlock &EntryBB = *BBI++;
861 ASSERT_THAT(EntryBB, HasName("entry"));
862 BasicBlock &Loop0BB = *BBI++;
863 ASSERT_THAT(Loop0BB, HasName("loop.0"));
864 BasicBlock &Loop00BB = *BBI++;
865 ASSERT_THAT(Loop00BB, HasName("loop.0.0"));
866 BasicBlock &Loop01BB = *BBI++;
867 ASSERT_THAT(Loop01BB, HasName("loop.0.1"));
868 BasicBlock &Loop02BB = *BBI++;
869 ASSERT_THAT(Loop02BB, HasName("loop.0.2"));
870 BasicBlock &EndBB = *BBI++;
871 ASSERT_THAT(EndBB, HasName("end"));
872 ASSERT_THAT(BBI, F.end());
873
874 // Build the pass managers and register our pipeline. We build a single loop
875 // pass pipeline consisting of three mock pass runs over each loop. After
876 // this we run both domtree and loop verification passes to make sure that
877 // the IR remained valid during our mutations.
878 ModulePassManager MPM(true);
879 FunctionPassManager FPM(true);
880 LoopPassManager LPM(true);
881 LPM.addPass(MLPHandle.getPass());
882 LPM.addPass(MLPHandle.getPass());
883 LPM.addPass(MLPHandle.getPass());
884 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
885 FPM.addPass(DominatorTreeVerifierPass());
886 FPM.addPass(LoopVerifierPass());
887 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
888
889 // All the visit orders are deterministic, so we use simple fully order
890 // expectations.
891 ::testing::InSequence MakeExpectationsSequenced;
892
893 // We run loop passes three times over each of the loops.
894 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
895 .WillOnce(Invoke(getLoopAnalysisResult));
896 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
897 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
898 .Times(2)
899 .WillRepeatedly(Invoke(getLoopAnalysisResult));
900
901 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _))
902 .WillOnce(Invoke(getLoopAnalysisResult));
903 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
904
905 // When running over the middle loop, the second run inserts two new child
906 // loops, inserting them and itself into the worklist.
907 BasicBlock *NewLoop010BB;
908 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _))
909 .WillOnce(Invoke([&](Loop &L, LoopAnalysisManager &AM,
910 LoopStandardAnalysisResults &AR,
911 LPMUpdater &Updater) {
912 auto *NewLoop = new Loop();
913 L.addChildLoop(NewLoop);
914 NewLoop010BB = BasicBlock::Create(Context, "loop.0.1.0", &F, &Loop02BB);
915 BranchInst::Create(&Loop01BB, NewLoop010BB,
916 UndefValue::get(Type::getInt1Ty(Context)),
917 NewLoop010BB);
918 Loop01BB.getTerminator()->replaceUsesOfWith(&Loop01BB, NewLoop010BB);
919 AR.DT.addNewBlock(NewLoop010BB, &Loop01BB);
920 NewLoop->addBasicBlockToLoop(NewLoop010BB, AR.LI);
921 Updater.addChildLoops({NewLoop});
922 return PreservedAnalyses::all();
923 }));
924
925 // We should immediately drop down to fully visit the new inner loop.
926 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1.0"), _, _, _))
927 .WillOnce(Invoke(getLoopAnalysisResult));
928 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1.0"), _, _));
929 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1.0"), _, _, _))
930 .Times(2)
931 .WillRepeatedly(Invoke(getLoopAnalysisResult));
932
933 // After visiting the inner loop, we should re-visit the second loop
934 // reflecting its new loop nest structure.
935 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _))
936 .WillOnce(Invoke(getLoopAnalysisResult));
937
938 // In the second run over the middle loop after we've visited the new child,
939 // we add another child to check that we can repeatedly add children, and add
940 // children to a loop that already has children.
941 BasicBlock *NewLoop011BB;
942 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _))
943 .WillOnce(Invoke([&](Loop &L, LoopAnalysisManager &AM,
944 LoopStandardAnalysisResults &AR,
945 LPMUpdater &Updater) {
946 auto *NewLoop = new Loop();
947 L.addChildLoop(NewLoop);
948 NewLoop011BB = BasicBlock::Create(Context, "loop.0.1.1", &F, &Loop02BB);
949 BranchInst::Create(&Loop01BB, NewLoop011BB,
950 UndefValue::get(Type::getInt1Ty(Context)),
951 NewLoop011BB);
952 NewLoop010BB->getTerminator()->replaceUsesOfWith(&Loop01BB,
953 NewLoop011BB);
954 AR.DT.addNewBlock(NewLoop011BB, NewLoop010BB);
955 NewLoop->addBasicBlockToLoop(NewLoop011BB, AR.LI);
956 Updater.addChildLoops({NewLoop});
957 return PreservedAnalyses::all();
958 }));
959
960 // Again, we should immediately drop down to visit the new, unvisited child
961 // loop. We don't need to revisit the other child though.
962 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1.1"), _, _, _))
963 .WillOnce(Invoke(getLoopAnalysisResult));
964 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1.1"), _, _));
965 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1.1"), _, _, _))
966 .Times(2)
967 .WillRepeatedly(Invoke(getLoopAnalysisResult));
968
969 // And now we should pop back up to the second loop and do a full pipeline of
970 // three passes on its current form.
971 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _))
972 .Times(3)
973 .WillRepeatedly(Invoke(getLoopAnalysisResult));
974
975 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2"), _, _, _))
976 .WillOnce(Invoke(getLoopAnalysisResult));
977 EXPECT_CALL(MLAHandle, run(HasName("loop.0.2"), _, _));
978 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2"), _, _, _))
979 .Times(2)
980 .WillRepeatedly(Invoke(getLoopAnalysisResult));
981
982 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _))
983 .WillOnce(Invoke(getLoopAnalysisResult));
984 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
985 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _))
986 .Times(2)
987 .WillRepeatedly(Invoke(getLoopAnalysisResult));
988
989 // Now that all the expected actions are registered, run the pipeline over
990 // our module. All of our expectations are verified when the test finishes.
991 MPM.run(*M, MAM);
992 }
993
994 TEST_F(LoopPassManagerTest, LoopPeerInsertion) {
995 // Super boring module with two loop nests and loop nest with two child
996 // loops.
997 M = parseIR(Context, "define void @f() {\n"
998 "entry:\n"
999 " br label %loop.0\n"
1000 "loop.0:\n"
1001 " br i1 undef, label %loop.0.0, label %loop.2\n"
1002 "loop.0.0:\n"
1003 " br i1 undef, label %loop.0.0, label %loop.0.2\n"
1004 "loop.0.2:\n"
1005 " br i1 undef, label %loop.0.2, label %loop.0\n"
1006 "loop.2:\n"
1007 " br i1 undef, label %loop.2, label %end\n"
1008 "end:\n"
1009 " ret void\n"
1010 "}\n");
1011
1012 // Build up variables referring into the IR so we can rewrite it below
1013 // easily.
1014 Function &F = *M->begin();
1015 ASSERT_THAT(F, HasName("f"));
1016 auto BBI = F.begin();
1017 BasicBlock &EntryBB = *BBI++;
1018 ASSERT_THAT(EntryBB, HasName("entry"));
1019 BasicBlock &Loop0BB = *BBI++;
1020 ASSERT_THAT(Loop0BB, HasName("loop.0"));
1021 BasicBlock &Loop00BB = *BBI++;
1022 ASSERT_THAT(Loop00BB, HasName("loop.0.0"));
1023 BasicBlock &Loop02BB = *BBI++;
1024 ASSERT_THAT(Loop02BB, HasName("loop.0.2"));
1025 BasicBlock &Loop2BB = *BBI++;
1026 ASSERT_THAT(Loop2BB, HasName("loop.2"));
1027 BasicBlock &EndBB = *BBI++;
1028 ASSERT_THAT(EndBB, HasName("end"));
1029 ASSERT_THAT(BBI, F.end());
1030 Constant *Undefi1 = UndefValue::get(Type::getInt1Ty(Context));
1031
1032 // Build the pass managers and register our pipeline. We build a single loop
1033 // pass pipeline consisting of three mock pass runs over each loop. After
1034 // this we run both domtree and loop verification passes to make sure that
1035 // the IR remained valid during our mutations.
1036 ModulePassManager MPM(true);
1037 FunctionPassManager FPM(true);
1038 LoopPassManager LPM(true);
1039 LPM.addPass(MLPHandle.getPass());
1040 LPM.addPass(MLPHandle.getPass());
1041 LPM.addPass(MLPHandle.getPass());
1042 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
1043 FPM.addPass(DominatorTreeVerifierPass());
1044 FPM.addPass(LoopVerifierPass());
1045 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1046
1047 // All the visit orders are deterministic, so we use simple fully order
1048 // expectations.
1049 ::testing::InSequence MakeExpectationsSequenced;
1050
1051 // We run loop passes three times over each of the loops.
1052 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
1053 .WillOnce(Invoke(getLoopAnalysisResult));
1054 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
1055
1056 // On the second run, we insert a sibling loop.
1057 BasicBlock *NewLoop01BB;
1058 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
1059 .WillOnce(Invoke([&](Loop &L, LoopAnalysisManager &AM,
1060 LoopStandardAnalysisResults &AR,
1061 LPMUpdater &Updater) {
1062 auto *NewLoop = new Loop();
1063 L.getParentLoop()->addChildLoop(NewLoop);
1064 NewLoop01BB = BasicBlock::Create(Context, "loop.0.1", &F, &Loop02BB);
1065 BranchInst::Create(&Loop02BB, NewLoop01BB, Undefi1, NewLoop01BB);
1066 Loop00BB.getTerminator()->replaceUsesOfWith(&Loop02BB, NewLoop01BB);
1067 auto *NewDTNode = AR.DT.addNewBlock(NewLoop01BB, &Loop00BB);
1068 AR.DT.changeImmediateDominator(AR.DT[&Loop02BB], NewDTNode);
1069 NewLoop->addBasicBlockToLoop(NewLoop01BB, AR.LI);
1070 Updater.addSiblingLoops({NewLoop});
1071 return PreservedAnalyses::all();
1072 }));
1073 // We finish processing this loop as sibling loops don't perturb the
1074 // postorder walk.
1075 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
1076 .WillOnce(Invoke(getLoopAnalysisResult));
1077
1078 // We visit the inserted sibling next.
1079 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _))
1080 .WillOnce(Invoke(getLoopAnalysisResult));
1081 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
1082 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _))
1083 .Times(2)
1084 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1085
1086 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2"), _, _, _))
1087 .WillOnce(Invoke(getLoopAnalysisResult));
1088 EXPECT_CALL(MLAHandle, run(HasName("loop.0.2"), _, _));
1089 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2"), _, _, _))
1090 .WillOnce(Invoke(getLoopAnalysisResult));
1091 // Next, on the third pass run on the last inner loop we add more new
1092 // siblings, more than one, and one with nested child loops. By doing this at
1093 // the end we make sure that edge case works well.
1094 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2"), _, _, _))
1095 .WillOnce(Invoke([&](Loop &L, LoopAnalysisManager &AM,
1096 LoopStandardAnalysisResults &AR,
1097 LPMUpdater &Updater) {
1098 Loop *NewLoops[] = {new Loop(), new Loop(), new Loop()};
1099 L.getParentLoop()->addChildLoop(NewLoops[0]);
1100 L.getParentLoop()->addChildLoop(NewLoops[1]);
1101 NewLoops[1]->addChildLoop(NewLoops[2]);
1102 auto *NewLoop03BB =
1103 BasicBlock::Create(Context, "loop.0.3", &F, &Loop2BB);
1104 auto *NewLoop04BB =
1105 BasicBlock::Create(Context, "loop.0.4", &F, &Loop2BB);
1106 auto *NewLoop040BB =
1107 BasicBlock::Create(Context, "loop.0.4.0", &F, &Loop2BB);
1108 Loop02BB.getTerminator()->replaceUsesOfWith(&Loop0BB, NewLoop03BB);
1109 BranchInst::Create(NewLoop04BB, NewLoop03BB, Undefi1, NewLoop03BB);
1110 BranchInst::Create(&Loop0BB, NewLoop040BB, Undefi1, NewLoop04BB);
1111 BranchInst::Create(NewLoop04BB, NewLoop040BB, Undefi1, NewLoop040BB);
1112 AR.DT.addNewBlock(NewLoop03BB, &Loop02BB);
1113 AR.DT.addNewBlock(NewLoop04BB, NewLoop03BB);
1114 AR.DT.addNewBlock(NewLoop040BB, NewLoop04BB);
1115 NewLoops[0]->addBasicBlockToLoop(NewLoop03BB, AR.LI);
1116 NewLoops[1]->addBasicBlockToLoop(NewLoop04BB, AR.LI);
1117 NewLoops[2]->addBasicBlockToLoop(NewLoop040BB, AR.LI);
1118 Updater.addSiblingLoops({NewLoops[0], NewLoops[1]});
1119 return PreservedAnalyses::all();
1120 }));
1121
1122 EXPECT_CALL(MLPHandle, run(HasName("loop.0.3"), _, _, _))
1123 .WillOnce(Invoke(getLoopAnalysisResult));
1124 EXPECT_CALL(MLAHandle, run(HasName("loop.0.3"), _, _));
1125 EXPECT_CALL(MLPHandle, run(HasName("loop.0.3"), _, _, _))
1126 .Times(2)
1127 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1128
1129 // Note that we need to visit the inner loop of this added sibling before the
1130 // sibling itself!
1131 EXPECT_CALL(MLPHandle, run(HasName("loop.0.4.0"), _, _, _))
1132 .WillOnce(Invoke(getLoopAnalysisResult));
1133 EXPECT_CALL(MLAHandle, run(HasName("loop.0.4.0"), _, _));
1134 EXPECT_CALL(MLPHandle, run(HasName("loop.0.4.0"), _, _, _))
1135 .Times(2)
1136 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1137
1138 EXPECT_CALL(MLPHandle, run(HasName("loop.0.4"), _, _, _))
1139 .WillOnce(Invoke(getLoopAnalysisResult));
1140 EXPECT_CALL(MLAHandle, run(HasName("loop.0.4"), _, _));
1141 EXPECT_CALL(MLPHandle, run(HasName("loop.0.4"), _, _, _))
1142 .Times(2)
1143 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1144
1145 // And only now do we visit the outermost loop of the nest.
1146 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _))
1147 .WillOnce(Invoke(getLoopAnalysisResult));
1148 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
1149 // On the second pass, we add sibling loops which become new top-level loops.
1150 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _))
1151 .WillOnce(Invoke([&](Loop &L, LoopAnalysisManager &AM,
1152 LoopStandardAnalysisResults &AR,
1153 LPMUpdater &Updater) {
1154 auto *NewLoop = new Loop();
1155 AR.LI.addTopLevelLoop(NewLoop);
1156 auto *NewLoop1BB = BasicBlock::Create(Context, "loop.1", &F, &Loop2BB);
1157 BranchInst::Create(&Loop2BB, NewLoop1BB, Undefi1, NewLoop1BB);
1158 Loop0BB.getTerminator()->replaceUsesOfWith(&Loop2BB, NewLoop1BB);
1159 auto *NewDTNode = AR.DT.addNewBlock(NewLoop1BB, &Loop0BB);
1160 AR.DT.changeImmediateDominator(AR.DT[&Loop2BB], NewDTNode);
1161 NewLoop->addBasicBlockToLoop(NewLoop1BB, AR.LI);
1162 Updater.addSiblingLoops({NewLoop});
1163 return PreservedAnalyses::all();
1164 }));
1165 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _))
1166 .WillOnce(Invoke(getLoopAnalysisResult));
1167
1168 EXPECT_CALL(MLPHandle, run(HasName("loop.1"), _, _, _))
1169 .WillOnce(Invoke(getLoopAnalysisResult));
1170 EXPECT_CALL(MLAHandle, run(HasName("loop.1"), _, _));
1171 EXPECT_CALL(MLPHandle, run(HasName("loop.1"), _, _, _))
1172 .Times(2)
1173 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1174
1175 EXPECT_CALL(MLPHandle, run(HasName("loop.2"), _, _, _))
1176 .WillOnce(Invoke(getLoopAnalysisResult));
1177 EXPECT_CALL(MLAHandle, run(HasName("loop.2"), _, _));
1178 EXPECT_CALL(MLPHandle, run(HasName("loop.2"), _, _, _))
1179 .Times(2)
1180 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1181
1182 // Now that all the expected actions are registered, run the pipeline over
1183 // our module. All of our expectations are verified when the test finishes.
1184 MPM.run(*M, MAM);
1185 }
1186
1187 TEST_F(LoopPassManagerTest, LoopDeletion) {
1188 // Build a module with a single loop nest that contains one outer loop with
1189 // three subloops, and one of those with its own subloop. We will
1190 // incrementally delete all of these to test different deletion scenarios.
1191 M = parseIR(Context, "define void @f() {\n"
1192 "entry:\n"
1193 " br label %loop.0\n"
1194 "loop.0:\n"
1195 " br i1 undef, label %loop.0.0, label %end\n"
1196 "loop.0.0:\n"
1197 " br i1 undef, label %loop.0.0, label %loop.0.1\n"
1198 "loop.0.1:\n"
1199 " br i1 undef, label %loop.0.1, label %loop.0.2\n"
1200 "loop.0.2:\n"
1201 " br i1 undef, label %loop.0.2.0, label %loop.0\n"
1202 "loop.0.2.0:\n"
1203 " br i1 undef, label %loop.0.2.0, label %loop.0.2\n"
1204 "end:\n"
1205 " ret void\n"
1206 "}\n");
1207
1208 // Build up variables referring into the IR so we can rewrite it below
1209 // easily.
1210 Function &F = *M->begin();
1211 ASSERT_THAT(F, HasName("f"));
1212 auto BBI = F.begin();
1213 BasicBlock &EntryBB = *BBI++;
1214 ASSERT_THAT(EntryBB, HasName("entry"));
1215 BasicBlock &Loop0BB = *BBI++;
1216 ASSERT_THAT(Loop0BB, HasName("loop.0"));
1217 BasicBlock &Loop00BB = *BBI++;
1218 ASSERT_THAT(Loop00BB, HasName("loop.0.0"));
1219 BasicBlock &Loop01BB = *BBI++;
1220 ASSERT_THAT(Loop01BB, HasName("loop.0.1"));
1221 BasicBlock &Loop02BB = *BBI++;
1222 ASSERT_THAT(Loop02BB, HasName("loop.0.2"));
1223 BasicBlock &Loop020BB = *BBI++;
1224 ASSERT_THAT(Loop020BB, HasName("loop.0.2.0"));
1225 BasicBlock &EndBB = *BBI++;
1226 ASSERT_THAT(EndBB, HasName("end"));
1227 ASSERT_THAT(BBI, F.end());
1228 Constant *Undefi1 = UndefValue::get(Type::getInt1Ty(Context));
1229
1230 // Helper to do the actual deletion of a loop. We directly encode this here
1231 // to isolate ourselves from the rest of LLVM and for simplicity. Here we can
1232 // egregiously cheat based on knowledge of the test case. For example, we
1233 // have no PHI nodes and there is always a single i-dom.
1234 auto DeleteLoopBlocks = [](Loop &L, BasicBlock &IDomBB,
1235 LoopStandardAnalysisResults &AR,
1236 LPMUpdater &Updater) {
1237 for (BasicBlock *LoopBB : L.blocks()) {
1238 SmallVector ChildNodes(AR.DT[LoopBB]->begin(),
1239 AR.DT[LoopBB]->end());
1240 for (DomTreeNode *ChildNode : ChildNodes)
1241 AR.DT.changeImmediateDominator(ChildNode, AR.DT[&IDomBB]);
1242 AR.DT.eraseNode(LoopBB);
1243 LoopBB->dropAllReferences();
1244 }
1245 SmallVector LoopBBs(L.block_begin(), L.block_end());
1246 Updater.markLoopAsDeleted(L);
1247 AR.LI.markAsRemoved(&L);
1248 for (BasicBlock *LoopBB : LoopBBs)
1249 LoopBB->eraseFromParent();
1250 };
1251
1252 // Build up the pass managers.
1253 ModulePassManager MPM(true);
1254 FunctionPassManager FPM(true);
1255 // We run several loop pass pipelines across the loop nest, but they all take
1256 // the same form of three mock pass runs in a loop pipeline followed by
1257 // domtree and loop verification. We use a lambda to stamp this out each
1258 // time.
1259 auto AddLoopPipelineAndVerificationPasses = [&] {
1260 LoopPassManager LPM(true);
1261 LPM.addPass(MLPHandle.getPass());
1262 LPM.addPass(MLPHandle.getPass());
1263 LPM.addPass(MLPHandle.getPass());
1264 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
1265 FPM.addPass(DominatorTreeVerifierPass());
1266 FPM.addPass(LoopVerifierPass());
1267 };
1268
1269 // All the visit orders are deterministic so we use simple fully order
1270 // expectations.
1271 ::testing::InSequence MakeExpectationsSequenced;
1272
1273 // We run the loop pipeline with three passes over each of the loops. When
1274 // running over the middle loop, the second pass in the pipeline deletes it.
1275 // This should prevent the third pass from visiting it but otherwise leave
1276 // the process unimpacted.
1277 AddLoopPipelineAndVerificationPasses();
1278 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
1279 .WillOnce(Invoke(getLoopAnalysisResult));
1280 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
1281 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
1282 .Times(2)
1283 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1284
1285 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _))
1286 .WillOnce(Invoke(getLoopAnalysisResult));
1287 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
1288 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _))
1289 .WillOnce(
1290 Invoke([&](Loop &L, LoopAnalysisManager &AM,
1291 LoopStandardAnalysisResults &AR, LPMUpdater &Updater) {
1292 AR.SE.forgetLoop(&L);
1293 Loop00BB.getTerminator()->replaceUsesOfWith(&Loop01BB, &Loop02BB);
1294 DeleteLoopBlocks(L, Loop00BB, AR, Updater);
1295 return PreservedAnalyses::all();
1296 }));
1297
1298 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2.0"), _, _, _))
1299 .WillOnce(Invoke(getLoopAnalysisResult));
1300 EXPECT_CALL(MLAHandle, run(HasName("loop.0.2.0"), _, _));
1301 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2.0"), _, _, _))
1302 .Times(2)
1303 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1304
1305 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2"), _, _, _))
1306 .WillOnce(Invoke(getLoopAnalysisResult));
1307 EXPECT_CALL(MLAHandle, run(HasName("loop.0.2"), _, _));
1308 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2"), _, _, _))
1309 .Times(2)
1310 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1311
1312 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _))
1313 .WillOnce(Invoke(getLoopAnalysisResult));
1314 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
1315 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _))
1316 .Times(2)
1317 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1318
1319 // Run the loop pipeline again. This time we delete the last loop, which
1320 // contains a nested loop within it, and we reuse its inner loop object to
1321 // insert a new loop into the nest. This makes sure that we don't reuse
1322 // cached analysis results for loop objects when removed just because their
1323 // pointers match, and that we can handle nested loop deletion.
1324 AddLoopPipelineAndVerificationPasses();
1325 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
1326 .Times(3)
1327 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1328
1329 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2.0"), _, _, _))
1330 .Times(3)
1331 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1332
1333 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2"), _, _, _))
1334 .WillOnce(Invoke(getLoopAnalysisResult));
1335 BasicBlock *NewLoop03BB;
1336 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2"), _, _, _))
1337 .WillOnce(
1338 Invoke([&](Loop &L, LoopAnalysisManager &AM,
1339 LoopStandardAnalysisResults &AR, LPMUpdater &Updater) {
1340 // Delete the inner loop first. we also do this manually because we
1341 // want to preserve the loop object and reuse it.
1342 AR.SE.forgetLoop(*L.begin());
1343 Loop02BB.getTerminator()->replaceUsesOfWith(&Loop020BB, &Loop02BB);
1344 assert(std::next((*L.begin())->block_begin()) ==
1345 (*L.begin())->block_end() &&
1346 "There should only be one block.");
1347 assert(AR.DT[&Loop020BB]->getNumChildren() == 0 &&
1348 "Cannot have children in the domtree!");
1349 AR.DT.eraseNode(&Loop020BB);
1350 Updater.markLoopAsDeleted(**L.begin());
1351 AR.LI.removeBlock(&Loop020BB);
1352 auto *OldL = L.removeChildLoop(L.begin());
1353 Loop020BB.eraseFromParent();
1354
1355 auto *ParentL = L.getParentLoop();
1356 AR.SE.forgetLoop(&L);
1357 Loop00BB.getTerminator()->replaceUsesOfWith(&Loop02BB, &Loop0BB);
1358 DeleteLoopBlocks(L, Loop00BB, AR, Updater);
1359
1360 // Now insert a new sibling loop, reusing a loop pointer.
1361 ParentL->addChildLoop(OldL);
1362 NewLoop03BB = BasicBlock::Create(Context, "loop.0.3", &F, &EndBB);
1363 BranchInst::Create(&Loop0BB, NewLoop03BB, Undefi1, NewLoop03BB);
1364 Loop00BB.getTerminator()->replaceUsesOfWith(&Loop0BB, NewLoop03BB);
1365 AR.DT.addNewBlock(NewLoop03BB, &Loop00BB);
1366 OldL->addBasicBlockToLoop(NewLoop03BB, AR.LI);
1367 Updater.addSiblingLoops({OldL});
1368 return PreservedAnalyses::all();
1369 }));
1370
1371 // To respect our inner-to-outer traversal order, we must visit the
1372 // newly-inserted sibling of the loop we just deleted before we visit the
1373 // outer loop. When we do so, this must compute a fresh analysis result, even
1374 // though our new loop has the same pointer value as the loop we deleted.
1375 EXPECT_CALL(MLPHandle, run(HasName("loop.0.3"), _, _, _))
1376 .WillOnce(Invoke(getLoopAnalysisResult));
1377 EXPECT_CALL(MLAHandle, run(HasName("loop.0.3"), _, _));
1378 EXPECT_CALL(MLPHandle, run(HasName("loop.0.3"), _, _, _))
1379 .Times(2)
1380 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1381
1382 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _))
1383 .Times(3)
1384 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1385
1386 // In the final loop pipeline run we delete every loop, including the last
1387 // loop of the nest. We do this again in the second pass in the pipeline, and
1388 // as a consequence we never make it to three runs on any loop. We also cover
1389 // deleting multiple loops in a single pipeline, deleting the first loop and
1390 // deleting the (last) top level loop.
1391 AddLoopPipelineAndVerificationPasses();
1392 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
1393 .WillOnce(Invoke(getLoopAnalysisResult));
1394 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
1395 .WillOnce(
1396 Invoke([&](Loop &L, LoopAnalysisManager &AM,
1397 LoopStandardAnalysisResults &AR, LPMUpdater &Updater) {
1398 AR.SE.forgetLoop(&L);
1399 Loop0BB.getTerminator()->replaceUsesOfWith(&Loop00BB, NewLoop03BB);
1400 DeleteLoopBlocks(L, Loop0BB, AR, Updater);
1401 return PreservedAnalyses::all();
1402 }));
1403
1404 EXPECT_CALL(MLPHandle, run(HasName("loop.0.3"), _, _, _))
1405 .WillOnce(Invoke(getLoopAnalysisResult));
1406 EXPECT_CALL(MLPHandle, run(HasName("loop.0.3"), _, _, _))
1407 .WillOnce(
1408 Invoke([&](Loop &L, LoopAnalysisManager &AM,
1409 LoopStandardAnalysisResults &AR, LPMUpdater &Updater) {
1410 AR.SE.forgetLoop(&L);
1411 Loop0BB.getTerminator()->replaceUsesOfWith(NewLoop03BB, &Loop0BB);
1412 DeleteLoopBlocks(L, Loop0BB, AR, Updater);
1413 return PreservedAnalyses::all();
1414 }));
1415
1416 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _))
1417 .WillOnce(Invoke(getLoopAnalysisResult));
1418 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _))
1419 .WillOnce(
1420 Invoke([&](Loop &L, LoopAnalysisManager &AM,
1421 LoopStandardAnalysisResults &AR, LPMUpdater &Updater) {
1422 AR.SE.forgetLoop(&L);
1423 EntryBB.getTerminator()->replaceUsesOfWith(&Loop0BB, &EndBB);
1424 DeleteLoopBlocks(L, EntryBB, AR, Updater);
1425 return PreservedAnalyses::all();
1426 }));
1427
1428 // Add the function pass pipeline now that it is fully built up and run it
1429 // over the module's one function.
1430 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1431 MPM.run(*M, MAM);
2071432 }
2081433 }