blob: d9c08f060a68158604f705b93875846dff0a480e [file] [log] [blame]
Justin Bognereecc3c82016-02-25 07:23:08 +00001//===- llvm/unittest/Analysis/LoopPassManagerTest.cpp - LPM tests ---------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Justin Bognereecc3c82016-02-25 07:23:08 +00006//
7//===----------------------------------------------------------------------===//
8
Chandler Carruth9a67b072017-06-06 11:06:56 +00009#include "llvm/Transforms/Scalar/LoopPassManager.h"
Chandler Carruthe3f50642016-12-22 06:59:15 +000010#include "llvm/Analysis/AliasAnalysis.h"
11#include "llvm/Analysis/AssumptionCache.h"
Chandler Carruthe3f50642016-12-22 06:59:15 +000012#include "llvm/Analysis/ScalarEvolution.h"
13#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruth410eaeb2017-01-11 06:23:21 +000014#include "llvm/Analysis/TargetTransformInfo.h"
Justin Bognereecc3c82016-02-25 07:23:08 +000015#include "llvm/AsmParser/Parser.h"
16#include "llvm/IR/Dominators.h"
17#include "llvm/IR/Function.h"
18#include "llvm/IR/LLVMContext.h"
19#include "llvm/IR/Module.h"
20#include "llvm/IR/PassManager.h"
21#include "llvm/Support/SourceMgr.h"
Galina Kistanova465c2c22017-06-14 17:30:35 +000022
Eric Christopher7f04e872017-11-16 03:18:13 +000023// Workaround for the gcc 6.1 bug PR80916.
24#if defined(__GNUC__) && __GNUC__ > 5
Galina Kistanova465c2c22017-06-14 17:30:35 +000025# pragma GCC diagnostic push
26# pragma GCC diagnostic ignored "-Wunused-function"
27#endif
28
Chandler Carruth410eaeb2017-01-11 06:23:21 +000029#include "gmock/gmock.h"
Chandler Carruthe3f50642016-12-22 06:59:15 +000030#include "gtest/gtest.h"
Justin Bognereecc3c82016-02-25 07:23:08 +000031
Eric Christopher7f04e872017-11-16 03:18:13 +000032#if defined(__GNUC__) && __GNUC__ > 5
Galina Kistanova465c2c22017-06-14 17:30:35 +000033# pragma GCC diagnostic pop
34#endif
35
Justin Bognereecc3c82016-02-25 07:23:08 +000036using namespace llvm;
37
38namespace {
39
Chandler Carruth410eaeb2017-01-11 06:23:21 +000040using testing::DoDefault;
41using testing::Return;
42using testing::Expectation;
43using testing::Invoke;
44using testing::InvokeWithoutArgs;
45using testing::_;
Justin Bognereecc3c82016-02-25 07:23:08 +000046
Chandler Carruth410eaeb2017-01-11 06:23:21 +000047template <typename DerivedT, typename IRUnitT,
48 typename AnalysisManagerT = AnalysisManager<IRUnitT>,
49 typename... ExtraArgTs>
50class MockAnalysisHandleBase {
Justin Bognereecc3c82016-02-25 07:23:08 +000051public:
Chandler Carruth410eaeb2017-01-11 06:23:21 +000052 class Analysis : public AnalysisInfoMixin<Analysis> {
53 friend AnalysisInfoMixin<Analysis>;
54 friend MockAnalysisHandleBase;
55 static AnalysisKey Key;
56
57 DerivedT *Handle;
58
Chandler Carruth84f24702017-02-07 03:34:08 +000059 Analysis(DerivedT &Handle) : Handle(&Handle) {
60 static_assert(std::is_base_of<MockAnalysisHandleBase, DerivedT>::value,
61 "Must pass the derived type to this template!");
62 }
Chandler Carruth410eaeb2017-01-11 06:23:21 +000063
64 public:
65 class Result {
66 friend MockAnalysisHandleBase;
67
68 DerivedT *Handle;
69
70 Result(DerivedT &Handle) : Handle(&Handle) {}
71
72 public:
73 // Forward invalidation events to the mock handle.
74 bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA,
75 typename AnalysisManagerT::Invalidator &Inv) {
76 return Handle->invalidate(IR, PA, Inv);
77 }
78 };
79
80 Result run(IRUnitT &IR, AnalysisManagerT &AM, ExtraArgTs... ExtraArgs) {
81 return Handle->run(IR, AM, ExtraArgs...);
82 }
Justin Bognereecc3c82016-02-25 07:23:08 +000083 };
84
Chandler Carruth410eaeb2017-01-11 06:23:21 +000085 Analysis getAnalysis() { return Analysis(static_cast<DerivedT &>(*this)); }
86 typename Analysis::Result getResult() {
87 return typename Analysis::Result(static_cast<DerivedT &>(*this));
88 }
Justin Bognereecc3c82016-02-25 07:23:08 +000089
Chandler Carruth410eaeb2017-01-11 06:23:21 +000090protected:
Chandler Carruth3410eb22017-01-11 09:20:24 +000091 // FIXME: MSVC seems unable to handle a lambda argument to Invoke from within
92 // the template, so we use a boring static function.
93 static bool invalidateCallback(IRUnitT &IR, const PreservedAnalyses &PA,
94 typename AnalysisManagerT::Invalidator &Inv) {
95 auto PAC = PA.template getChecker<Analysis>();
96 return !PAC.preserved() &&
97 !PAC.template preservedSet<AllAnalysesOn<IRUnitT>>();
98 }
99
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000100 /// Derived classes should call this in their constructor to set up default
101 /// mock actions. (We can't do this in our constructor because this has to
102 /// run after the DerivedT is constructed.)
103 void setDefaults() {
104 ON_CALL(static_cast<DerivedT &>(*this),
105 run(_, _, testing::Matcher<ExtraArgTs>(_)...))
106 .WillByDefault(Return(this->getResult()));
107 ON_CALL(static_cast<DerivedT &>(*this), invalidate(_, _, _))
Chandler Carruth3410eb22017-01-11 09:20:24 +0000108 .WillByDefault(Invoke(&invalidateCallback));
Justin Bognereecc3c82016-02-25 07:23:08 +0000109 }
110};
111
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000112template <typename DerivedT, typename IRUnitT, typename AnalysisManagerT,
113 typename... ExtraArgTs>
114AnalysisKey MockAnalysisHandleBase<DerivedT, IRUnitT, AnalysisManagerT,
115 ExtraArgTs...>::Analysis::Key;
Justin Bognereecc3c82016-02-25 07:23:08 +0000116
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000117/// Mock handle for loop analyses.
118///
119/// This is provided as a template accepting an (optional) integer. Because
120/// analyses are identified and queried by type, this allows constructing
121/// multiple handles with distinctly typed nested 'Analysis' types that can be
122/// registered and queried. If you want to register multiple loop analysis
123/// passes, you'll need to instantiate this type with different values for I.
124/// For example:
125///
126/// MockLoopAnalysisHandleTemplate<0> h0;
127/// MockLoopAnalysisHandleTemplate<1> h1;
128/// typedef decltype(h0)::Analysis Analysis0;
129/// typedef decltype(h1)::Analysis Analysis1;
130template <size_t I = static_cast<size_t>(-1)>
131struct MockLoopAnalysisHandleTemplate
132 : MockAnalysisHandleBase<MockLoopAnalysisHandleTemplate<I>, Loop,
133 LoopAnalysisManager,
134 LoopStandardAnalysisResults &> {
135 typedef typename MockLoopAnalysisHandleTemplate::Analysis Analysis;
Justin Bognereecc3c82016-02-25 07:23:08 +0000136
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000137 MOCK_METHOD3_T(run, typename Analysis::Result(Loop &, LoopAnalysisManager &,
138 LoopStandardAnalysisResults &));
139
140 MOCK_METHOD3_T(invalidate, bool(Loop &, const PreservedAnalyses &,
141 LoopAnalysisManager::Invalidator &));
142
143 MockLoopAnalysisHandleTemplate() { this->setDefaults(); }
144};
145
146typedef MockLoopAnalysisHandleTemplate<> MockLoopAnalysisHandle;
147
148struct MockFunctionAnalysisHandle
149 : MockAnalysisHandleBase<MockFunctionAnalysisHandle, Function> {
150 MOCK_METHOD2(run, Analysis::Result(Function &, FunctionAnalysisManager &));
151
152 MOCK_METHOD3(invalidate, bool(Function &, const PreservedAnalyses &,
153 FunctionAnalysisManager::Invalidator &));
154
155 MockFunctionAnalysisHandle() { setDefaults(); }
156};
157
158template <typename DerivedT, typename IRUnitT,
159 typename AnalysisManagerT = AnalysisManager<IRUnitT>,
160 typename... ExtraArgTs>
161class MockPassHandleBase {
Justin Bognereecc3c82016-02-25 07:23:08 +0000162public:
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000163 class Pass : public PassInfoMixin<Pass> {
164 friend MockPassHandleBase;
Justin Bognereecc3c82016-02-25 07:23:08 +0000165
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000166 DerivedT *Handle;
Justin Bognereecc3c82016-02-25 07:23:08 +0000167
Chandler Carruth84f24702017-02-07 03:34:08 +0000168 Pass(DerivedT &Handle) : Handle(&Handle) {
169 static_assert(std::is_base_of<MockPassHandleBase, DerivedT>::value,
170 "Must pass the derived type to this template!");
171 }
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000172
173 public:
174 PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM,
175 ExtraArgTs... ExtraArgs) {
176 return Handle->run(IR, AM, ExtraArgs...);
Justin Bognereecc3c82016-02-25 07:23:08 +0000177 }
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000178 };
Justin Bognereecc3c82016-02-25 07:23:08 +0000179
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000180 Pass getPass() { return Pass(static_cast<DerivedT &>(*this)); }
181
182protected:
183 /// Derived classes should call this in their constructor to set up default
184 /// mock actions. (We can't do this in our constructor because this has to
185 /// run after the DerivedT is constructed.)
186 void setDefaults() {
187 ON_CALL(static_cast<DerivedT &>(*this),
188 run(_, _, testing::Matcher<ExtraArgTs>(_)...))
189 .WillByDefault(Return(PreservedAnalyses::all()));
Justin Bognereecc3c82016-02-25 07:23:08 +0000190 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000191};
192
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000193struct MockLoopPassHandle
194 : MockPassHandleBase<MockLoopPassHandle, Loop, LoopAnalysisManager,
195 LoopStandardAnalysisResults &, LPMUpdater &> {
196 MOCK_METHOD4(run,
197 PreservedAnalyses(Loop &, LoopAnalysisManager &,
198 LoopStandardAnalysisResults &, LPMUpdater &));
199 MockLoopPassHandle() { setDefaults(); }
Justin Bognereecc3c82016-02-25 07:23:08 +0000200};
201
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000202struct MockFunctionPassHandle
203 : MockPassHandleBase<MockFunctionPassHandle, Function> {
204 MOCK_METHOD2(run, PreservedAnalyses(Function &, FunctionAnalysisManager &));
205
206 MockFunctionPassHandle() { setDefaults(); }
207};
208
209struct MockModulePassHandle : MockPassHandleBase<MockModulePassHandle, Module> {
210 MOCK_METHOD2(run, PreservedAnalyses(Module &, ModuleAnalysisManager &));
211
212 MockModulePassHandle() { setDefaults(); }
213};
214
215/// Define a custom matcher for objects which support a 'getName' method
216/// returning a StringRef.
217///
218/// LLVM often has IR objects or analysis objects which expose a StringRef name
219/// and in tests it is convenient to match these by name for readability. This
220/// matcher supports any type exposing a getName() method of this form.
221///
222/// It should be used as:
223///
224/// HasName("my_function")
225///
226/// No namespace or other qualification is required.
227MATCHER_P(HasName, Name, "") {
228 // The matcher's name and argument are printed in the case of failure, but we
229 // also want to print out the name of the argument. This uses an implicitly
230 // avaiable std::ostream, so we have to construct a std::string.
231 *result_listener << "has name '" << arg.getName().str() << "'";
232 return Name == arg.getName();
233}
234
Mehdi Amini03b42e42016-04-14 21:59:01 +0000235std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) {
Justin Bognereecc3c82016-02-25 07:23:08 +0000236 SMDiagnostic Err;
237 return parseAssemblyString(IR, Err, C);
238}
239
240class LoopPassManagerTest : public ::testing::Test {
241protected:
Mehdi Amini03b42e42016-04-14 21:59:01 +0000242 LLVMContext Context;
Justin Bognereecc3c82016-02-25 07:23:08 +0000243 std::unique_ptr<Module> M;
244
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000245 LoopAnalysisManager LAM;
246 FunctionAnalysisManager FAM;
247 ModuleAnalysisManager MAM;
248
249 MockLoopAnalysisHandle MLAHandle;
250 MockLoopPassHandle MLPHandle;
251 MockFunctionPassHandle MFPHandle;
252 MockModulePassHandle MMPHandle;
253
254 static PreservedAnalyses
255 getLoopAnalysisResult(Loop &L, LoopAnalysisManager &AM,
256 LoopStandardAnalysisResults &AR, LPMUpdater &) {
257 (void)AM.getResult<MockLoopAnalysisHandle::Analysis>(L, AR);
258 return PreservedAnalyses::all();
259 };
260
Justin Bognereecc3c82016-02-25 07:23:08 +0000261public:
262 LoopPassManagerTest()
Chandler Carruth17350de2017-01-21 03:48:51 +0000263 : M(parseIR(Context,
264 "define void @f(i1* %ptr) {\n"
265 "entry:\n"
266 " br label %loop.0\n"
267 "loop.0:\n"
268 " %cond.0 = load volatile i1, i1* %ptr\n"
269 " br i1 %cond.0, label %loop.0.0.ph, label %end\n"
270 "loop.0.0.ph:\n"
271 " br label %loop.0.0\n"
272 "loop.0.0:\n"
273 " %cond.0.0 = load volatile i1, i1* %ptr\n"
274 " br i1 %cond.0.0, label %loop.0.0, label %loop.0.1.ph\n"
275 "loop.0.1.ph:\n"
276 " br label %loop.0.1\n"
277 "loop.0.1:\n"
278 " %cond.0.1 = load volatile i1, i1* %ptr\n"
279 " br i1 %cond.0.1, label %loop.0.1, label %loop.0.latch\n"
280 "loop.0.latch:\n"
281 " br label %loop.0\n"
282 "end:\n"
283 " ret void\n"
284 "}\n"
285 "\n"
286 "define void @g(i1* %ptr) {\n"
287 "entry:\n"
288 " br label %loop.g.0\n"
289 "loop.g.0:\n"
290 " %cond.0 = load volatile i1, i1* %ptr\n"
291 " br i1 %cond.0, label %loop.g.0, label %end\n"
292 "end:\n"
293 " ret void\n"
294 "}\n")),
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000295 LAM(true), FAM(true), MAM(true) {
296 // Register our mock analysis.
297 LAM.registerPass([&] { return MLAHandle.getAnalysis(); });
298
299 // We need DominatorTreeAnalysis for LoopAnalysis.
300 FAM.registerPass([&] { return DominatorTreeAnalysis(); });
301 FAM.registerPass([&] { return LoopAnalysis(); });
302 // We also allow loop passes to assume a set of other analyses and so need
303 // those.
304 FAM.registerPass([&] { return AAManager(); });
305 FAM.registerPass([&] { return AssumptionAnalysis(); });
306 FAM.registerPass([&] { return ScalarEvolutionAnalysis(); });
307 FAM.registerPass([&] { return TargetLibraryAnalysis(); });
308 FAM.registerPass([&] { return TargetIRAnalysis(); });
309
Fedor Sergeevee8d31c2018-09-20 17:08:45 +0000310 // Register required pass instrumentation analysis.
311 LAM.registerPass([&] { return PassInstrumentationAnalysis(); });
312 FAM.registerPass([&] { return PassInstrumentationAnalysis(); });
313 MAM.registerPass([&] { return PassInstrumentationAnalysis(); });
314
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000315 // Cross-register proxies.
316 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
317 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
318 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
319 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
320 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000321};
322
Justin Bognereecc3c82016-02-25 07:23:08 +0000323TEST_F(LoopPassManagerTest, Basic) {
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000324 ModulePassManager MPM(true);
325 ::testing::InSequence MakeExpectationsSequenced;
Justin Bognereecc3c82016-02-25 07:23:08 +0000326
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000327 // First we just visit all the loops in all the functions and get their
328 // analysis results. This will run the analysis a total of four times,
329 // once for each loop.
330 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
331 .WillOnce(Invoke(getLoopAnalysisResult));
332 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
333 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _))
334 .WillOnce(Invoke(getLoopAnalysisResult));
335 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
336 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _))
337 .WillOnce(Invoke(getLoopAnalysisResult));
338 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
339 EXPECT_CALL(MLPHandle, run(HasName("loop.g.0"), _, _, _))
340 .WillOnce(Invoke(getLoopAnalysisResult));
341 EXPECT_CALL(MLAHandle, run(HasName("loop.g.0"), _, _));
342 // Wire the loop pass through pass managers into the module pipeline.
343 {
344 LoopPassManager LPM(true);
345 LPM.addPass(MLPHandle.getPass());
346 FunctionPassManager FPM(true);
347 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
348 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
349 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000350
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000351 // Next we run two passes over the loops. The first one invalidates the
352 // analyses for one loop, the second ones try to get the analysis results.
353 // This should force only one analysis to re-run within the loop PM, but will
354 // also invalidate everything after the loop pass manager finishes.
355 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
356 .WillOnce(DoDefault())
357 .WillOnce(Invoke(getLoopAnalysisResult));
358 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _))
359 .WillOnce(InvokeWithoutArgs([] { return PreservedAnalyses::none(); }))
360 .WillOnce(Invoke(getLoopAnalysisResult));
361 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
362 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _))
363 .WillOnce(DoDefault())
364 .WillOnce(Invoke(getLoopAnalysisResult));
365 EXPECT_CALL(MLPHandle, run(HasName("loop.g.0"), _, _, _))
366 .WillOnce(DoDefault())
367 .WillOnce(Invoke(getLoopAnalysisResult));
368 // Wire two loop pass runs into the module pipeline.
369 {
370 LoopPassManager LPM(true);
371 LPM.addPass(MLPHandle.getPass());
372 LPM.addPass(MLPHandle.getPass());
373 FunctionPassManager FPM(true);
374 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
375 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
376 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000377
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000378 // And now run the pipeline across the module.
379 MPM.run(*M, MAM);
380}
381
382TEST_F(LoopPassManagerTest, FunctionPassInvalidationOfLoopAnalyses) {
Justin Bognereecc3c82016-02-25 07:23:08 +0000383 ModulePassManager MPM(true);
384 FunctionPassManager FPM(true);
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000385 // We process each function completely in sequence.
386 ::testing::Sequence FSequence, GSequence;
Justin Bognereecc3c82016-02-25 07:23:08 +0000387
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000388 // First, force the analysis result to be computed for each loop.
389 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _))
390 .InSequence(FSequence)
391 .WillOnce(DoDefault());
392 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _))
393 .InSequence(FSequence)
394 .WillOnce(DoDefault());
395 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _))
396 .InSequence(FSequence)
397 .WillOnce(DoDefault());
398 EXPECT_CALL(MLAHandle, run(HasName("loop.g.0"), _, _))
399 .InSequence(GSequence)
400 .WillOnce(DoDefault());
401 FPM.addPass(createFunctionToLoopPassAdaptor(
402 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>()));
Justin Bognereecc3c82016-02-25 07:23:08 +0000403
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000404 // No need to re-run if we require again from a fresh loop pass manager.
405 FPM.addPass(createFunctionToLoopPassAdaptor(
406 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>()));
Justin Bognereecc3c82016-02-25 07:23:08 +0000407
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000408 // For 'f', preserve most things but not the specific loop analyses.
409 EXPECT_CALL(MFPHandle, run(HasName("f"), _))
410 .InSequence(FSequence)
411 .WillOnce(Return(getLoopPassPreservedAnalyses()));
412 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.0.0"), _, _))
413 .InSequence(FSequence)
414 .WillOnce(DoDefault());
415 // On one loop, skip the invalidation (as though we did an internal update).
416 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.0.1"), _, _))
417 .InSequence(FSequence)
418 .WillOnce(Return(false));
419 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.0"), _, _))
420 .InSequence(FSequence)
421 .WillOnce(DoDefault());
422 // Now two loops still have to be recomputed.
423 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _))
424 .InSequence(FSequence)
425 .WillOnce(DoDefault());
426 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _))
427 .InSequence(FSequence)
428 .WillOnce(DoDefault());
429 // Preserve things in the second function to ensure invalidation remains
430 // isolated to one function.
431 EXPECT_CALL(MFPHandle, run(HasName("g"), _))
432 .InSequence(GSequence)
433 .WillOnce(DoDefault());
434 FPM.addPass(MFPHandle.getPass());
435 FPM.addPass(createFunctionToLoopPassAdaptor(
436 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>()));
Justin Bognereecc3c82016-02-25 07:23:08 +0000437
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000438 EXPECT_CALL(MFPHandle, run(HasName("f"), _))
439 .InSequence(FSequence)
440 .WillOnce(DoDefault());
441 // For 'g', fail to preserve anything, causing the loops themselves to be
442 // cleared. We don't get an invalidation event here as the loop is gone, but
443 // we should still have to recompute the analysis.
444 EXPECT_CALL(MFPHandle, run(HasName("g"), _))
445 .InSequence(GSequence)
446 .WillOnce(Return(PreservedAnalyses::none()));
447 EXPECT_CALL(MLAHandle, run(HasName("loop.g.0"), _, _))
448 .InSequence(GSequence)
449 .WillOnce(DoDefault());
450 FPM.addPass(MFPHandle.getPass());
451 FPM.addPass(createFunctionToLoopPassAdaptor(
452 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>()));
453
454 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
455
456 // Verify with a separate function pass run that we didn't mess up 'f's
457 // cache. No analysis runs should be necessary here.
458 MPM.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor(
459 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>())));
460
461 MPM.run(*M, MAM);
462}
463
464TEST_F(LoopPassManagerTest, ModulePassInvalidationOfLoopAnalyses) {
465 ModulePassManager MPM(true);
466 ::testing::InSequence MakeExpectationsSequenced;
467
468 // First, force the analysis result to be computed for each loop.
469 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
470 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
471 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
472 EXPECT_CALL(MLAHandle, run(HasName("loop.g.0"), _, _));
473 MPM.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor(
474 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>())));
475
476 // Walking all the way out and all the way back in doesn't re-run the
477 // analysis.
478 MPM.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor(
479 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>())));
480
481 // But a module pass that doesn't preserve the actual mock loop analysis
482 // invalidates all the way down and forces recomputing.
483 EXPECT_CALL(MMPHandle, run(_, _)).WillOnce(InvokeWithoutArgs([] {
484 auto PA = getLoopPassPreservedAnalyses();
485 PA.preserve<FunctionAnalysisManagerModuleProxy>();
486 return PA;
487 }));
488 // All the loop analyses from both functions get invalidated before we
489 // recompute anything.
490 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.0.0"), _, _));
491 // On one loop, again skip the invalidation (as though we did an internal
492 // update).
493 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.0.1"), _, _))
494 .WillOnce(Return(false));
495 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.0"), _, _));
496 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.g.0"), _, _));
497 // Now all but one of the loops gets re-analyzed.
498 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
499 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
500 EXPECT_CALL(MLAHandle, run(HasName("loop.g.0"), _, _));
501 MPM.addPass(MMPHandle.getPass());
502 MPM.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor(
503 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>())));
504
505 // Verify that the cached values persist.
506 MPM.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor(
507 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>())));
508
509 // Now we fail to preserve the loop analysis and observe that the loop
510 // analyses are cleared (so no invalidation event) as the loops themselves
511 // are no longer valid.
512 EXPECT_CALL(MMPHandle, run(_, _)).WillOnce(InvokeWithoutArgs([] {
513 auto PA = PreservedAnalyses::none();
514 PA.preserve<FunctionAnalysisManagerModuleProxy>();
515 return PA;
516 }));
517 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
518 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
519 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
520 EXPECT_CALL(MLAHandle, run(HasName("loop.g.0"), _, _));
521 MPM.addPass(MMPHandle.getPass());
522 MPM.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor(
523 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>())));
524
525 // Verify that the cached values persist.
526 MPM.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor(
527 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>())));
528
529 // Next, check that even if we preserve everything within the function itelf,
530 // if the function's module pass proxy isn't preserved and the potential set
531 // of functions changes, the clear reaches the loop analyses as well. This
532 // will again trigger re-runs but not invalidation events.
533 EXPECT_CALL(MMPHandle, run(_, _)).WillOnce(InvokeWithoutArgs([] {
534 auto PA = PreservedAnalyses::none();
535 PA.preserveSet<AllAnalysesOn<Function>>();
536 PA.preserveSet<AllAnalysesOn<Loop>>();
537 return PA;
538 }));
539 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
540 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
541 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
542 EXPECT_CALL(MLAHandle, run(HasName("loop.g.0"), _, _));
543 MPM.addPass(MMPHandle.getPass());
544 MPM.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor(
545 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>())));
546
547 MPM.run(*M, MAM);
548}
549
550// Test that if any of the bundled analyses provided in the LPM's signature
551// become invalid, the analysis proxy itself becomes invalid and we clear all
552// loop analysis results.
553TEST_F(LoopPassManagerTest, InvalidationOfBundledAnalyses) {
554 ModulePassManager MPM(true);
555 FunctionPassManager FPM(true);
556 ::testing::InSequence MakeExpectationsSequenced;
557
558 // First, force the analysis result to be computed for each loop.
559 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
560 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
561 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
562 FPM.addPass(createFunctionToLoopPassAdaptor(
563 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>()));
564
565 // No need to re-run if we require again from a fresh loop pass manager.
566 FPM.addPass(createFunctionToLoopPassAdaptor(
567 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>()));
568
569 // Preserving everything but the loop analyses themselves results in
570 // invalidation and running.
571 EXPECT_CALL(MFPHandle, run(HasName("f"), _))
572 .WillOnce(Return(getLoopPassPreservedAnalyses()));
573 EXPECT_CALL(MLAHandle, invalidate(_, _, _)).Times(3);
574 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
575 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
576 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
577 FPM.addPass(MFPHandle.getPass());
578 FPM.addPass(createFunctionToLoopPassAdaptor(
579 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>()));
580
581 // The rest don't invalidate analyses, they only trigger re-runs because we
582 // clear the cache completely.
583 EXPECT_CALL(MFPHandle, run(HasName("f"), _)).WillOnce(InvokeWithoutArgs([] {
584 auto PA = PreservedAnalyses::none();
585 // Not preserving `AAManager`.
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000586 PA.preserve<DominatorTreeAnalysis>();
587 PA.preserve<LoopAnalysis>();
588 PA.preserve<LoopAnalysisManagerFunctionProxy>();
589 PA.preserve<ScalarEvolutionAnalysis>();
590 return PA;
591 }));
592 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
593 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
594 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
595 FPM.addPass(MFPHandle.getPass());
596 FPM.addPass(createFunctionToLoopPassAdaptor(
597 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>()));
598
599 EXPECT_CALL(MFPHandle, run(HasName("f"), _)).WillOnce(InvokeWithoutArgs([] {
600 auto PA = PreservedAnalyses::none();
601 PA.preserve<AAManager>();
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000602 // Not preserving `DominatorTreeAnalysis`.
603 PA.preserve<LoopAnalysis>();
604 PA.preserve<LoopAnalysisManagerFunctionProxy>();
605 PA.preserve<ScalarEvolutionAnalysis>();
606 return PA;
607 }));
608 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
609 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
610 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
611 FPM.addPass(MFPHandle.getPass());
612 FPM.addPass(createFunctionToLoopPassAdaptor(
613 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>()));
614
615 EXPECT_CALL(MFPHandle, run(HasName("f"), _)).WillOnce(InvokeWithoutArgs([] {
616 auto PA = PreservedAnalyses::none();
617 PA.preserve<AAManager>();
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000618 PA.preserve<DominatorTreeAnalysis>();
619 // Not preserving the `LoopAnalysis`.
620 PA.preserve<LoopAnalysisManagerFunctionProxy>();
621 PA.preserve<ScalarEvolutionAnalysis>();
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<MockLoopAnalysisHandle::Analysis>()));
630
631 EXPECT_CALL(MFPHandle, run(HasName("f"), _)).WillOnce(InvokeWithoutArgs([] {
632 auto PA = PreservedAnalyses::none();
633 PA.preserve<AAManager>();
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000634 PA.preserve<DominatorTreeAnalysis>();
635 PA.preserve<LoopAnalysis>();
636 // Not preserving the `LoopAnalysisManagerFunctionProxy`.
637 PA.preserve<ScalarEvolutionAnalysis>();
638 return PA;
639 }));
640 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
641 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
642 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
643 FPM.addPass(MFPHandle.getPass());
644 FPM.addPass(createFunctionToLoopPassAdaptor(
645 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>()));
646
647 EXPECT_CALL(MFPHandle, run(HasName("f"), _)).WillOnce(InvokeWithoutArgs([] {
648 auto PA = PreservedAnalyses::none();
649 PA.preserve<AAManager>();
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000650 PA.preserve<DominatorTreeAnalysis>();
651 PA.preserve<LoopAnalysis>();
652 PA.preserve<LoopAnalysisManagerFunctionProxy>();
653 // Not preserving `ScalarEvolutionAnalysis`.
654 return PA;
655 }));
656 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
657 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
658 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
659 FPM.addPass(MFPHandle.getPass());
660 FPM.addPass(createFunctionToLoopPassAdaptor(
661 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>()));
662
663 // After all the churn on 'f', we'll compute the loop analysis results for
664 // 'g' once with a requires pass and then run our mock pass over g a bunch
665 // but just get cached results each time.
666 EXPECT_CALL(MLAHandle, run(HasName("loop.g.0"), _, _));
Chandler Carruth2f19a322017-01-15 00:26:18 +0000667 EXPECT_CALL(MFPHandle, run(HasName("g"), _)).Times(6);
Justin Bognereecc3c82016-02-25 07:23:08 +0000668
669 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruthb47f8012016-03-11 11:05:24 +0000670 MPM.run(*M, MAM);
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000671}
Justin Bognereecc3c82016-02-25 07:23:08 +0000672
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000673TEST_F(LoopPassManagerTest, IndirectInvalidation) {
674 // We need two distinct analysis types and handles.
675 enum { A, B };
676 MockLoopAnalysisHandleTemplate<A> MLAHandleA;
677 MockLoopAnalysisHandleTemplate<B> MLAHandleB;
678 LAM.registerPass([&] { return MLAHandleA.getAnalysis(); });
679 LAM.registerPass([&] { return MLAHandleB.getAnalysis(); });
680 typedef decltype(MLAHandleA)::Analysis AnalysisA;
681 typedef decltype(MLAHandleB)::Analysis AnalysisB;
Justin Bognereecc3c82016-02-25 07:23:08 +0000682
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000683 // Set up AnalysisA to depend on our AnalysisB. For testing purposes we just
684 // need to get the AnalysisB results in AnalysisA's run method and check if
685 // AnalysisB gets invalidated in AnalysisA's invalidate method.
686 ON_CALL(MLAHandleA, run(_, _, _))
687 .WillByDefault(Invoke([&](Loop &L, LoopAnalysisManager &AM,
688 LoopStandardAnalysisResults &AR) {
689 (void)AM.getResult<AnalysisB>(L, AR);
690 return MLAHandleA.getResult();
691 }));
692 ON_CALL(MLAHandleA, invalidate(_, _, _))
693 .WillByDefault(Invoke([](Loop &L, const PreservedAnalyses &PA,
694 LoopAnalysisManager::Invalidator &Inv) {
695 auto PAC = PA.getChecker<AnalysisA>();
696 return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Loop>>()) ||
697 Inv.invalidate<AnalysisB>(L, PA);
698 }));
Justin Bognereecc3c82016-02-25 07:23:08 +0000699
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000700 ::testing::InSequence MakeExpectationsSequenced;
Justin Bognereecc3c82016-02-25 07:23:08 +0000701
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000702 // Compute the analyses across all of 'f' first.
703 EXPECT_CALL(MLAHandleA, run(HasName("loop.0.0"), _, _));
704 EXPECT_CALL(MLAHandleB, run(HasName("loop.0.0"), _, _));
705 EXPECT_CALL(MLAHandleA, run(HasName("loop.0.1"), _, _));
706 EXPECT_CALL(MLAHandleB, run(HasName("loop.0.1"), _, _));
707 EXPECT_CALL(MLAHandleA, run(HasName("loop.0"), _, _));
708 EXPECT_CALL(MLAHandleB, run(HasName("loop.0"), _, _));
709
710 // Now we invalidate AnalysisB (but not AnalysisA) for one of the loops and
711 // preserve everything for the rest. This in turn triggers that one loop to
712 // recompute both AnalysisB *and* AnalysisA if indirect invalidation is
713 // working.
714 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
715 .WillOnce(InvokeWithoutArgs([] {
716 auto PA = getLoopPassPreservedAnalyses();
717 // Specifically preserve AnalysisA so that it would survive if it
718 // didn't depend on AnalysisB.
719 PA.preserve<AnalysisA>();
720 return PA;
721 }));
722 // It happens that AnalysisB is invalidated first. That shouldn't matter
723 // though, and we should still call AnalysisA's invalidation.
724 EXPECT_CALL(MLAHandleB, invalidate(HasName("loop.0.0"), _, _));
725 EXPECT_CALL(MLAHandleA, invalidate(HasName("loop.0.0"), _, _));
726 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
727 .WillOnce(Invoke([](Loop &L, LoopAnalysisManager &AM,
728 LoopStandardAnalysisResults &AR, LPMUpdater &) {
729 (void)AM.getResult<AnalysisA>(L, AR);
730 return PreservedAnalyses::all();
731 }));
732 EXPECT_CALL(MLAHandleA, run(HasName("loop.0.0"), _, _));
733 EXPECT_CALL(MLAHandleB, run(HasName("loop.0.0"), _, _));
734 // The rest of the loops should run and get cached results.
735 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _))
736 .Times(2)
737 .WillRepeatedly(Invoke([](Loop &L, LoopAnalysisManager &AM,
738 LoopStandardAnalysisResults &AR, LPMUpdater &) {
739 (void)AM.getResult<AnalysisA>(L, AR);
740 return PreservedAnalyses::all();
741 }));
742 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _))
743 .Times(2)
744 .WillRepeatedly(Invoke([](Loop &L, LoopAnalysisManager &AM,
745 LoopStandardAnalysisResults &AR, LPMUpdater &) {
746 (void)AM.getResult<AnalysisA>(L, AR);
747 return PreservedAnalyses::all();
748 }));
749
750 // The run over 'g' should be boring, with us just computing the analyses once
751 // up front and then running loop passes and getting cached results.
752 EXPECT_CALL(MLAHandleA, run(HasName("loop.g.0"), _, _));
753 EXPECT_CALL(MLAHandleB, run(HasName("loop.g.0"), _, _));
754 EXPECT_CALL(MLPHandle, run(HasName("loop.g.0"), _, _, _))
755 .Times(2)
756 .WillRepeatedly(Invoke([](Loop &L, LoopAnalysisManager &AM,
757 LoopStandardAnalysisResults &AR, LPMUpdater &) {
758 (void)AM.getResult<AnalysisA>(L, AR);
759 return PreservedAnalyses::all();
760 }));
761
762 // Build the pipeline and run it.
763 ModulePassManager MPM(true);
764 FunctionPassManager FPM(true);
765 FPM.addPass(
766 createFunctionToLoopPassAdaptor(RequireAnalysisLoopPass<AnalysisA>()));
767 LoopPassManager LPM(true);
768 LPM.addPass(MLPHandle.getPass());
769 LPM.addPass(MLPHandle.getPass());
770 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
771 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
772 MPM.run(*M, MAM);
773}
774
775TEST_F(LoopPassManagerTest, IndirectOuterPassInvalidation) {
776 typedef decltype(MLAHandle)::Analysis LoopAnalysis;
777
778 MockFunctionAnalysisHandle MFAHandle;
779 FAM.registerPass([&] { return MFAHandle.getAnalysis(); });
780 typedef decltype(MFAHandle)::Analysis FunctionAnalysis;
781
782 // Set up the loop analysis to depend on both the function and module
783 // analysis.
784 ON_CALL(MLAHandle, run(_, _, _))
785 .WillByDefault(Invoke([&](Loop &L, LoopAnalysisManager &AM,
786 LoopStandardAnalysisResults &AR) {
787 auto &FAMP = AM.getResult<FunctionAnalysisManagerLoopProxy>(L, AR);
788 auto &FAM = FAMP.getManager();
789 Function &F = *L.getHeader()->getParent();
Simon Pilgrim2ded2522017-01-11 10:40:33 +0000790 if (FAM.getCachedResult<FunctionAnalysis>(F))
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000791 FAMP.registerOuterAnalysisInvalidation<FunctionAnalysis,
792 LoopAnalysis>();
793 return MLAHandle.getResult();
794 }));
795
796 ::testing::InSequence MakeExpectationsSequenced;
797
798 // Compute the analyses across all of 'f' first.
799 EXPECT_CALL(MFPHandle, run(HasName("f"), _))
800 .WillOnce(Invoke([](Function &F, FunctionAnalysisManager &AM) {
801 // Force the computing of the function analysis so it is available in
802 // this function.
803 (void)AM.getResult<FunctionAnalysis>(F);
804 return PreservedAnalyses::all();
805 }));
806 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
807 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
808 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
809
810 // Now invalidate the function analysis but preserve the loop analyses.
811 // This should trigger immediate invalidation of the loop analyses, despite
812 // the fact that they were preserved.
813 EXPECT_CALL(MFPHandle, run(HasName("f"), _)).WillOnce(InvokeWithoutArgs([] {
814 auto PA = getLoopPassPreservedAnalyses();
815 PA.preserveSet<AllAnalysesOn<Loop>>();
816 return PA;
817 }));
818 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.0.0"), _, _));
819 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.0.1"), _, _));
820 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.0"), _, _));
821
822 // And re-running a requires pass recomputes them.
823 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
824 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
825 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
826
827 // When we run over 'g' we don't populate the cache with the function
828 // analysis.
829 EXPECT_CALL(MFPHandle, run(HasName("g"), _))
830 .WillOnce(Return(PreservedAnalyses::all()));
831 EXPECT_CALL(MLAHandle, run(HasName("loop.g.0"), _, _));
832
833 // Which means that no extra invalidation occurs and cached values are used.
834 EXPECT_CALL(MFPHandle, run(HasName("g"), _)).WillOnce(InvokeWithoutArgs([] {
835 auto PA = getLoopPassPreservedAnalyses();
836 PA.preserveSet<AllAnalysesOn<Loop>>();
837 return PA;
838 }));
839
840 // Build the pipeline and run it.
841 ModulePassManager MPM(true);
842 FunctionPassManager FPM(true);
843 FPM.addPass(MFPHandle.getPass());
844 FPM.addPass(
845 createFunctionToLoopPassAdaptor(RequireAnalysisLoopPass<LoopAnalysis>()));
846 FPM.addPass(MFPHandle.getPass());
847 FPM.addPass(
848 createFunctionToLoopPassAdaptor(RequireAnalysisLoopPass<LoopAnalysis>()));
849 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
850 MPM.run(*M, MAM);
851}
852
853TEST_F(LoopPassManagerTest, LoopChildInsertion) {
854 // Super boring module with three loops in a single loop nest.
Chandler Carruth17350de2017-01-21 03:48:51 +0000855 M = parseIR(Context, "define void @f(i1* %ptr) {\n"
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000856 "entry:\n"
857 " br label %loop.0\n"
858 "loop.0:\n"
Chandler Carruth17350de2017-01-21 03:48:51 +0000859 " %cond.0 = load volatile i1, i1* %ptr\n"
860 " br i1 %cond.0, label %loop.0.0.ph, label %end\n"
861 "loop.0.0.ph:\n"
862 " br label %loop.0.0\n"
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000863 "loop.0.0:\n"
Chandler Carruth17350de2017-01-21 03:48:51 +0000864 " %cond.0.0 = load volatile i1, i1* %ptr\n"
865 " br i1 %cond.0.0, label %loop.0.0, label %loop.0.1.ph\n"
866 "loop.0.1.ph:\n"
867 " br label %loop.0.1\n"
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000868 "loop.0.1:\n"
Chandler Carruth17350de2017-01-21 03:48:51 +0000869 " %cond.0.1 = load volatile i1, i1* %ptr\n"
870 " br i1 %cond.0.1, label %loop.0.1, label %loop.0.2.ph\n"
871 "loop.0.2.ph:\n"
872 " br label %loop.0.2\n"
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000873 "loop.0.2:\n"
Chandler Carruth17350de2017-01-21 03:48:51 +0000874 " %cond.0.2 = load volatile i1, i1* %ptr\n"
875 " br i1 %cond.0.2, label %loop.0.2, label %loop.0.latch\n"
876 "loop.0.latch:\n"
877 " br label %loop.0\n"
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000878 "end:\n"
879 " ret void\n"
880 "}\n");
881
882 // Build up variables referring into the IR so we can rewrite it below
883 // easily.
884 Function &F = *M->begin();
885 ASSERT_THAT(F, HasName("f"));
Chandler Carruth17350de2017-01-21 03:48:51 +0000886 Argument &Ptr = *F.arg_begin();
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000887 auto BBI = F.begin();
888 BasicBlock &EntryBB = *BBI++;
889 ASSERT_THAT(EntryBB, HasName("entry"));
890 BasicBlock &Loop0BB = *BBI++;
891 ASSERT_THAT(Loop0BB, HasName("loop.0"));
Chandler Carruth17350de2017-01-21 03:48:51 +0000892 BasicBlock &Loop00PHBB = *BBI++;
893 ASSERT_THAT(Loop00PHBB, HasName("loop.0.0.ph"));
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000894 BasicBlock &Loop00BB = *BBI++;
895 ASSERT_THAT(Loop00BB, HasName("loop.0.0"));
Chandler Carruth17350de2017-01-21 03:48:51 +0000896 BasicBlock &Loop01PHBB = *BBI++;
897 ASSERT_THAT(Loop01PHBB, HasName("loop.0.1.ph"));
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000898 BasicBlock &Loop01BB = *BBI++;
899 ASSERT_THAT(Loop01BB, HasName("loop.0.1"));
Chandler Carruth17350de2017-01-21 03:48:51 +0000900 BasicBlock &Loop02PHBB = *BBI++;
901 ASSERT_THAT(Loop02PHBB, HasName("loop.0.2.ph"));
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000902 BasicBlock &Loop02BB = *BBI++;
903 ASSERT_THAT(Loop02BB, HasName("loop.0.2"));
Chandler Carruth17350de2017-01-21 03:48:51 +0000904 BasicBlock &Loop0LatchBB = *BBI++;
905 ASSERT_THAT(Loop0LatchBB, HasName("loop.0.latch"));
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000906 BasicBlock &EndBB = *BBI++;
907 ASSERT_THAT(EndBB, HasName("end"));
908 ASSERT_THAT(BBI, F.end());
Chandler Carruth17350de2017-01-21 03:48:51 +0000909 auto CreateCondBr = [&](BasicBlock *TrueBB, BasicBlock *FalseBB,
910 const char *Name, BasicBlock *BB) {
911 auto *Cond = new LoadInst(&Ptr, Name, /*isVolatile*/ true, BB);
912 BranchInst::Create(TrueBB, FalseBB, Cond, BB);
913 };
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000914
915 // Build the pass managers and register our pipeline. We build a single loop
916 // pass pipeline consisting of three mock pass runs over each loop. After
917 // this we run both domtree and loop verification passes to make sure that
918 // the IR remained valid during our mutations.
919 ModulePassManager MPM(true);
920 FunctionPassManager FPM(true);
921 LoopPassManager LPM(true);
922 LPM.addPass(MLPHandle.getPass());
923 LPM.addPass(MLPHandle.getPass());
924 LPM.addPass(MLPHandle.getPass());
925 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
926 FPM.addPass(DominatorTreeVerifierPass());
927 FPM.addPass(LoopVerifierPass());
928 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
929
930 // All the visit orders are deterministic, so we use simple fully order
931 // expectations.
932 ::testing::InSequence MakeExpectationsSequenced;
933
934 // We run loop passes three times over each of the loops.
935 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
936 .WillOnce(Invoke(getLoopAnalysisResult));
937 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
938 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
939 .Times(2)
940 .WillRepeatedly(Invoke(getLoopAnalysisResult));
941
942 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _))
943 .WillOnce(Invoke(getLoopAnalysisResult));
944 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
945
946 // When running over the middle loop, the second run inserts two new child
947 // loops, inserting them and itself into the worklist.
Chandler Carruth17350de2017-01-21 03:48:51 +0000948 BasicBlock *NewLoop010BB, *NewLoop01LatchBB;
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000949 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _))
950 .WillOnce(Invoke([&](Loop &L, LoopAnalysisManager &AM,
951 LoopStandardAnalysisResults &AR,
952 LPMUpdater &Updater) {
Sanjoy Dasdef17292017-09-28 02:45:42 +0000953 auto *NewLoop = AR.LI.AllocateLoop();
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000954 L.addChildLoop(NewLoop);
Chandler Carruth17350de2017-01-21 03:48:51 +0000955 auto *NewLoop010PHBB =
956 BasicBlock::Create(Context, "loop.0.1.0.ph", &F, &Loop02PHBB);
957 NewLoop010BB =
958 BasicBlock::Create(Context, "loop.0.1.0", &F, &Loop02PHBB);
959 NewLoop01LatchBB =
960 BasicBlock::Create(Context, "loop.0.1.latch", &F, &Loop02PHBB);
961 Loop01BB.getTerminator()->replaceUsesOfWith(&Loop01BB, NewLoop010PHBB);
962 BranchInst::Create(NewLoop010BB, NewLoop010PHBB);
963 CreateCondBr(NewLoop01LatchBB, NewLoop010BB, "cond.0.1.0",
964 NewLoop010BB);
965 BranchInst::Create(&Loop01BB, NewLoop01LatchBB);
966 AR.DT.addNewBlock(NewLoop010PHBB, &Loop01BB);
967 AR.DT.addNewBlock(NewLoop010BB, NewLoop010PHBB);
968 AR.DT.addNewBlock(NewLoop01LatchBB, NewLoop010BB);
David Green7c35de12018-02-28 11:00:08 +0000969 EXPECT_TRUE(AR.DT.verify());
Chandler Carruth17350de2017-01-21 03:48:51 +0000970 L.addBasicBlockToLoop(NewLoop010PHBB, AR.LI);
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000971 NewLoop->addBasicBlockToLoop(NewLoop010BB, AR.LI);
Chandler Carruth17350de2017-01-21 03:48:51 +0000972 L.addBasicBlockToLoop(NewLoop01LatchBB, AR.LI);
973 NewLoop->verifyLoop();
974 L.verifyLoop();
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000975 Updater.addChildLoops({NewLoop});
976 return PreservedAnalyses::all();
977 }));
978
979 // We should immediately drop down to fully visit the new inner loop.
980 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1.0"), _, _, _))
981 .WillOnce(Invoke(getLoopAnalysisResult));
982 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1.0"), _, _));
983 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1.0"), _, _, _))
984 .Times(2)
985 .WillRepeatedly(Invoke(getLoopAnalysisResult));
986
987 // After visiting the inner loop, we should re-visit the second loop
988 // reflecting its new loop nest structure.
989 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _))
990 .WillOnce(Invoke(getLoopAnalysisResult));
991
992 // In the second run over the middle loop after we've visited the new child,
993 // we add another child to check that we can repeatedly add children, and add
994 // children to a loop that already has children.
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000995 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _))
996 .WillOnce(Invoke([&](Loop &L, LoopAnalysisManager &AM,
997 LoopStandardAnalysisResults &AR,
998 LPMUpdater &Updater) {
Sanjoy Dasdef17292017-09-28 02:45:42 +0000999 auto *NewLoop = AR.LI.AllocateLoop();
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001000 L.addChildLoop(NewLoop);
Chandler Carruth17350de2017-01-21 03:48:51 +00001001 auto *NewLoop011PHBB = BasicBlock::Create(Context, "loop.0.1.1.ph", &F, NewLoop01LatchBB);
1002 auto *NewLoop011BB = BasicBlock::Create(Context, "loop.0.1.1", &F, NewLoop01LatchBB);
1003 NewLoop010BB->getTerminator()->replaceUsesOfWith(NewLoop01LatchBB,
1004 NewLoop011PHBB);
1005 BranchInst::Create(NewLoop011BB, NewLoop011PHBB);
1006 CreateCondBr(NewLoop01LatchBB, NewLoop011BB, "cond.0.1.1",
1007 NewLoop011BB);
1008 AR.DT.addNewBlock(NewLoop011PHBB, NewLoop010BB);
1009 auto *NewDTNode = AR.DT.addNewBlock(NewLoop011BB, NewLoop011PHBB);
1010 AR.DT.changeImmediateDominator(AR.DT[NewLoop01LatchBB], NewDTNode);
David Green7c35de12018-02-28 11:00:08 +00001011 EXPECT_TRUE(AR.DT.verify());
Chandler Carruth17350de2017-01-21 03:48:51 +00001012 L.addBasicBlockToLoop(NewLoop011PHBB, AR.LI);
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001013 NewLoop->addBasicBlockToLoop(NewLoop011BB, AR.LI);
Chandler Carruth17350de2017-01-21 03:48:51 +00001014 NewLoop->verifyLoop();
1015 L.verifyLoop();
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001016 Updater.addChildLoops({NewLoop});
1017 return PreservedAnalyses::all();
1018 }));
1019
1020 // Again, we should immediately drop down to visit the new, unvisited child
1021 // loop. We don't need to revisit the other child though.
1022 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1.1"), _, _, _))
1023 .WillOnce(Invoke(getLoopAnalysisResult));
1024 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1.1"), _, _));
1025 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1.1"), _, _, _))
1026 .Times(2)
1027 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1028
1029 // And now we should pop back up to the second loop and do a full pipeline of
1030 // three passes on its current form.
1031 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _))
1032 .Times(3)
1033 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1034
1035 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2"), _, _, _))
1036 .WillOnce(Invoke(getLoopAnalysisResult));
1037 EXPECT_CALL(MLAHandle, run(HasName("loop.0.2"), _, _));
1038 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2"), _, _, _))
1039 .Times(2)
1040 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1041
1042 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _))
1043 .WillOnce(Invoke(getLoopAnalysisResult));
1044 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
1045 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _))
1046 .Times(2)
1047 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1048
1049 // Now that all the expected actions are registered, run the pipeline over
1050 // our module. All of our expectations are verified when the test finishes.
1051 MPM.run(*M, MAM);
1052}
1053
1054TEST_F(LoopPassManagerTest, LoopPeerInsertion) {
1055 // Super boring module with two loop nests and loop nest with two child
1056 // loops.
Chandler Carruth17350de2017-01-21 03:48:51 +00001057 M = parseIR(Context, "define void @f(i1* %ptr) {\n"
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001058 "entry:\n"
1059 " br label %loop.0\n"
1060 "loop.0:\n"
Chandler Carruth17350de2017-01-21 03:48:51 +00001061 " %cond.0 = load volatile i1, i1* %ptr\n"
1062 " br i1 %cond.0, label %loop.0.0.ph, label %loop.2.ph\n"
1063 "loop.0.0.ph:\n"
1064 " br label %loop.0.0\n"
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001065 "loop.0.0:\n"
Chandler Carruth17350de2017-01-21 03:48:51 +00001066 " %cond.0.0 = load volatile i1, i1* %ptr\n"
1067 " br i1 %cond.0.0, label %loop.0.0, label %loop.0.2.ph\n"
1068 "loop.0.2.ph:\n"
1069 " br label %loop.0.2\n"
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001070 "loop.0.2:\n"
Chandler Carruth17350de2017-01-21 03:48:51 +00001071 " %cond.0.2 = load volatile i1, i1* %ptr\n"
1072 " br i1 %cond.0.2, label %loop.0.2, label %loop.0.latch\n"
1073 "loop.0.latch:\n"
1074 " br label %loop.0\n"
1075 "loop.2.ph:\n"
1076 " br label %loop.2\n"
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001077 "loop.2:\n"
Chandler Carruth17350de2017-01-21 03:48:51 +00001078 " %cond.2 = load volatile i1, i1* %ptr\n"
1079 " br i1 %cond.2, label %loop.2, label %end\n"
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001080 "end:\n"
1081 " ret void\n"
1082 "}\n");
1083
1084 // Build up variables referring into the IR so we can rewrite it below
1085 // easily.
1086 Function &F = *M->begin();
1087 ASSERT_THAT(F, HasName("f"));
Chandler Carruth17350de2017-01-21 03:48:51 +00001088 Argument &Ptr = *F.arg_begin();
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001089 auto BBI = F.begin();
1090 BasicBlock &EntryBB = *BBI++;
1091 ASSERT_THAT(EntryBB, HasName("entry"));
1092 BasicBlock &Loop0BB = *BBI++;
1093 ASSERT_THAT(Loop0BB, HasName("loop.0"));
Chandler Carruth17350de2017-01-21 03:48:51 +00001094 BasicBlock &Loop00PHBB = *BBI++;
1095 ASSERT_THAT(Loop00PHBB, HasName("loop.0.0.ph"));
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001096 BasicBlock &Loop00BB = *BBI++;
1097 ASSERT_THAT(Loop00BB, HasName("loop.0.0"));
Chandler Carruth17350de2017-01-21 03:48:51 +00001098 BasicBlock &Loop02PHBB = *BBI++;
1099 ASSERT_THAT(Loop02PHBB, HasName("loop.0.2.ph"));
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001100 BasicBlock &Loop02BB = *BBI++;
1101 ASSERT_THAT(Loop02BB, HasName("loop.0.2"));
Chandler Carruth17350de2017-01-21 03:48:51 +00001102 BasicBlock &Loop0LatchBB = *BBI++;
1103 ASSERT_THAT(Loop0LatchBB, HasName("loop.0.latch"));
1104 BasicBlock &Loop2PHBB = *BBI++;
1105 ASSERT_THAT(Loop2PHBB, HasName("loop.2.ph"));
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001106 BasicBlock &Loop2BB = *BBI++;
1107 ASSERT_THAT(Loop2BB, HasName("loop.2"));
1108 BasicBlock &EndBB = *BBI++;
1109 ASSERT_THAT(EndBB, HasName("end"));
1110 ASSERT_THAT(BBI, F.end());
Chandler Carruth17350de2017-01-21 03:48:51 +00001111 auto CreateCondBr = [&](BasicBlock *TrueBB, BasicBlock *FalseBB,
1112 const char *Name, BasicBlock *BB) {
1113 auto *Cond = new LoadInst(&Ptr, Name, /*isVolatile*/ true, BB);
1114 BranchInst::Create(TrueBB, FalseBB, Cond, BB);
1115 };
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001116
1117 // Build the pass managers and register our pipeline. We build a single loop
1118 // pass pipeline consisting of three mock pass runs over each loop. After
1119 // this we run both domtree and loop verification passes to make sure that
1120 // the IR remained valid during our mutations.
1121 ModulePassManager MPM(true);
1122 FunctionPassManager FPM(true);
1123 LoopPassManager LPM(true);
1124 LPM.addPass(MLPHandle.getPass());
1125 LPM.addPass(MLPHandle.getPass());
1126 LPM.addPass(MLPHandle.getPass());
1127 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
1128 FPM.addPass(DominatorTreeVerifierPass());
1129 FPM.addPass(LoopVerifierPass());
1130 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1131
1132 // All the visit orders are deterministic, so we use simple fully order
1133 // expectations.
1134 ::testing::InSequence MakeExpectationsSequenced;
1135
1136 // We run loop passes three times over each of the loops.
1137 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
1138 .WillOnce(Invoke(getLoopAnalysisResult));
1139 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
1140
1141 // On the second run, we insert a sibling loop.
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001142 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
1143 .WillOnce(Invoke([&](Loop &L, LoopAnalysisManager &AM,
1144 LoopStandardAnalysisResults &AR,
1145 LPMUpdater &Updater) {
Sanjoy Dasdef17292017-09-28 02:45:42 +00001146 auto *NewLoop = AR.LI.AllocateLoop();
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001147 L.getParentLoop()->addChildLoop(NewLoop);
Chandler Carruth17350de2017-01-21 03:48:51 +00001148 auto *NewLoop01PHBB = BasicBlock::Create(Context, "loop.0.1.ph", &F, &Loop02PHBB);
1149 auto *NewLoop01BB = BasicBlock::Create(Context, "loop.0.1", &F, &Loop02PHBB);
1150 BranchInst::Create(NewLoop01BB, NewLoop01PHBB);
1151 CreateCondBr(&Loop02PHBB, NewLoop01BB, "cond.0.1", NewLoop01BB);
1152 Loop00BB.getTerminator()->replaceUsesOfWith(&Loop02PHBB, NewLoop01PHBB);
1153 AR.DT.addNewBlock(NewLoop01PHBB, &Loop00BB);
1154 auto *NewDTNode = AR.DT.addNewBlock(NewLoop01BB, NewLoop01PHBB);
1155 AR.DT.changeImmediateDominator(AR.DT[&Loop02PHBB], NewDTNode);
David Green7c35de12018-02-28 11:00:08 +00001156 EXPECT_TRUE(AR.DT.verify());
Chandler Carruth17350de2017-01-21 03:48:51 +00001157 L.getParentLoop()->addBasicBlockToLoop(NewLoop01PHBB, AR.LI);
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001158 NewLoop->addBasicBlockToLoop(NewLoop01BB, AR.LI);
Chandler Carruth17350de2017-01-21 03:48:51 +00001159 L.getParentLoop()->verifyLoop();
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001160 Updater.addSiblingLoops({NewLoop});
1161 return PreservedAnalyses::all();
1162 }));
1163 // We finish processing this loop as sibling loops don't perturb the
1164 // postorder walk.
1165 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
1166 .WillOnce(Invoke(getLoopAnalysisResult));
1167
1168 // We visit the inserted sibling next.
1169 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _))
1170 .WillOnce(Invoke(getLoopAnalysisResult));
1171 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
1172 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _))
1173 .Times(2)
1174 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1175
1176 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2"), _, _, _))
1177 .WillOnce(Invoke(getLoopAnalysisResult));
1178 EXPECT_CALL(MLAHandle, run(HasName("loop.0.2"), _, _));
1179 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2"), _, _, _))
1180 .WillOnce(Invoke(getLoopAnalysisResult));
1181 // Next, on the third pass run on the last inner loop we add more new
1182 // siblings, more than one, and one with nested child loops. By doing this at
1183 // the end we make sure that edge case works well.
1184 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2"), _, _, _))
1185 .WillOnce(Invoke([&](Loop &L, LoopAnalysisManager &AM,
1186 LoopStandardAnalysisResults &AR,
1187 LPMUpdater &Updater) {
Sanjoy Dasdef17292017-09-28 02:45:42 +00001188 Loop *NewLoops[] = {AR.LI.AllocateLoop(), AR.LI.AllocateLoop(),
1189 AR.LI.AllocateLoop()};
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001190 L.getParentLoop()->addChildLoop(NewLoops[0]);
1191 L.getParentLoop()->addChildLoop(NewLoops[1]);
1192 NewLoops[1]->addChildLoop(NewLoops[2]);
Chandler Carruth17350de2017-01-21 03:48:51 +00001193 auto *NewLoop03PHBB =
1194 BasicBlock::Create(Context, "loop.0.3.ph", &F, &Loop0LatchBB);
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001195 auto *NewLoop03BB =
Chandler Carruth17350de2017-01-21 03:48:51 +00001196 BasicBlock::Create(Context, "loop.0.3", &F, &Loop0LatchBB);
1197 auto *NewLoop04PHBB =
1198 BasicBlock::Create(Context, "loop.0.4.ph", &F, &Loop0LatchBB);
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001199 auto *NewLoop04BB =
Chandler Carruth17350de2017-01-21 03:48:51 +00001200 BasicBlock::Create(Context, "loop.0.4", &F, &Loop0LatchBB);
1201 auto *NewLoop040PHBB =
1202 BasicBlock::Create(Context, "loop.0.4.0.ph", &F, &Loop0LatchBB);
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001203 auto *NewLoop040BB =
Chandler Carruth17350de2017-01-21 03:48:51 +00001204 BasicBlock::Create(Context, "loop.0.4.0", &F, &Loop0LatchBB);
1205 auto *NewLoop04LatchBB =
1206 BasicBlock::Create(Context, "loop.0.4.latch", &F, &Loop0LatchBB);
1207 Loop02BB.getTerminator()->replaceUsesOfWith(&Loop0LatchBB, NewLoop03PHBB);
1208 BranchInst::Create(NewLoop03BB, NewLoop03PHBB);
1209 CreateCondBr(NewLoop04PHBB, NewLoop03BB, "cond.0.3", NewLoop03BB);
1210 BranchInst::Create(NewLoop04BB, NewLoop04PHBB);
1211 CreateCondBr(&Loop0LatchBB, NewLoop040PHBB, "cond.0.4", NewLoop04BB);
1212 BranchInst::Create(NewLoop040BB, NewLoop040PHBB);
1213 CreateCondBr(NewLoop04LatchBB, NewLoop040BB, "cond.0.4.0", NewLoop040BB);
1214 BranchInst::Create(NewLoop04BB, NewLoop04LatchBB);
1215 AR.DT.addNewBlock(NewLoop03PHBB, &Loop02BB);
1216 AR.DT.addNewBlock(NewLoop03BB, NewLoop03PHBB);
1217 AR.DT.addNewBlock(NewLoop04PHBB, NewLoop03BB);
1218 auto *NewDTNode = AR.DT.addNewBlock(NewLoop04BB, NewLoop04PHBB);
1219 AR.DT.changeImmediateDominator(AR.DT[&Loop0LatchBB], NewDTNode);
1220 AR.DT.addNewBlock(NewLoop040PHBB, NewLoop04BB);
1221 AR.DT.addNewBlock(NewLoop040BB, NewLoop040PHBB);
1222 AR.DT.addNewBlock(NewLoop04LatchBB, NewLoop040BB);
David Green7c35de12018-02-28 11:00:08 +00001223 EXPECT_TRUE(AR.DT.verify());
Chandler Carruth17350de2017-01-21 03:48:51 +00001224 L.getParentLoop()->addBasicBlockToLoop(NewLoop03PHBB, AR.LI);
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001225 NewLoops[0]->addBasicBlockToLoop(NewLoop03BB, AR.LI);
Chandler Carruth17350de2017-01-21 03:48:51 +00001226 L.getParentLoop()->addBasicBlockToLoop(NewLoop04PHBB, AR.LI);
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001227 NewLoops[1]->addBasicBlockToLoop(NewLoop04BB, AR.LI);
Chandler Carruth17350de2017-01-21 03:48:51 +00001228 NewLoops[1]->addBasicBlockToLoop(NewLoop040PHBB, AR.LI);
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001229 NewLoops[2]->addBasicBlockToLoop(NewLoop040BB, AR.LI);
Chandler Carruth17350de2017-01-21 03:48:51 +00001230 NewLoops[1]->addBasicBlockToLoop(NewLoop04LatchBB, AR.LI);
1231 L.getParentLoop()->verifyLoop();
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001232 Updater.addSiblingLoops({NewLoops[0], NewLoops[1]});
1233 return PreservedAnalyses::all();
1234 }));
1235
1236 EXPECT_CALL(MLPHandle, run(HasName("loop.0.3"), _, _, _))
1237 .WillOnce(Invoke(getLoopAnalysisResult));
1238 EXPECT_CALL(MLAHandle, run(HasName("loop.0.3"), _, _));
1239 EXPECT_CALL(MLPHandle, run(HasName("loop.0.3"), _, _, _))
1240 .Times(2)
1241 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1242
1243 // Note that we need to visit the inner loop of this added sibling before the
1244 // sibling itself!
1245 EXPECT_CALL(MLPHandle, run(HasName("loop.0.4.0"), _, _, _))
1246 .WillOnce(Invoke(getLoopAnalysisResult));
1247 EXPECT_CALL(MLAHandle, run(HasName("loop.0.4.0"), _, _));
1248 EXPECT_CALL(MLPHandle, run(HasName("loop.0.4.0"), _, _, _))
1249 .Times(2)
1250 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1251
1252 EXPECT_CALL(MLPHandle, run(HasName("loop.0.4"), _, _, _))
1253 .WillOnce(Invoke(getLoopAnalysisResult));
1254 EXPECT_CALL(MLAHandle, run(HasName("loop.0.4"), _, _));
1255 EXPECT_CALL(MLPHandle, run(HasName("loop.0.4"), _, _, _))
1256 .Times(2)
1257 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1258
1259 // And only now do we visit the outermost loop of the nest.
1260 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _))
1261 .WillOnce(Invoke(getLoopAnalysisResult));
1262 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
1263 // On the second pass, we add sibling loops which become new top-level loops.
1264 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _))
1265 .WillOnce(Invoke([&](Loop &L, LoopAnalysisManager &AM,
1266 LoopStandardAnalysisResults &AR,
1267 LPMUpdater &Updater) {
Sanjoy Dasdef17292017-09-28 02:45:42 +00001268 auto *NewLoop = AR.LI.AllocateLoop();
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001269 AR.LI.addTopLevelLoop(NewLoop);
Chandler Carruth17350de2017-01-21 03:48:51 +00001270 auto *NewLoop1PHBB = BasicBlock::Create(Context, "loop.1.ph", &F, &Loop2BB);
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001271 auto *NewLoop1BB = BasicBlock::Create(Context, "loop.1", &F, &Loop2BB);
Chandler Carruth17350de2017-01-21 03:48:51 +00001272 BranchInst::Create(NewLoop1BB, NewLoop1PHBB);
1273 CreateCondBr(&Loop2PHBB, NewLoop1BB, "cond.1", NewLoop1BB);
1274 Loop0BB.getTerminator()->replaceUsesOfWith(&Loop2PHBB, NewLoop1PHBB);
1275 AR.DT.addNewBlock(NewLoop1PHBB, &Loop0BB);
1276 auto *NewDTNode = AR.DT.addNewBlock(NewLoop1BB, NewLoop1PHBB);
1277 AR.DT.changeImmediateDominator(AR.DT[&Loop2PHBB], NewDTNode);
David Green7c35de12018-02-28 11:00:08 +00001278 EXPECT_TRUE(AR.DT.verify());
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001279 NewLoop->addBasicBlockToLoop(NewLoop1BB, AR.LI);
Chandler Carruth17350de2017-01-21 03:48:51 +00001280 NewLoop->verifyLoop();
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001281 Updater.addSiblingLoops({NewLoop});
1282 return PreservedAnalyses::all();
1283 }));
1284 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _))
1285 .WillOnce(Invoke(getLoopAnalysisResult));
1286
1287 EXPECT_CALL(MLPHandle, run(HasName("loop.1"), _, _, _))
1288 .WillOnce(Invoke(getLoopAnalysisResult));
1289 EXPECT_CALL(MLAHandle, run(HasName("loop.1"), _, _));
1290 EXPECT_CALL(MLPHandle, run(HasName("loop.1"), _, _, _))
1291 .Times(2)
1292 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1293
1294 EXPECT_CALL(MLPHandle, run(HasName("loop.2"), _, _, _))
1295 .WillOnce(Invoke(getLoopAnalysisResult));
1296 EXPECT_CALL(MLAHandle, run(HasName("loop.2"), _, _));
1297 EXPECT_CALL(MLPHandle, run(HasName("loop.2"), _, _, _))
1298 .Times(2)
1299 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1300
1301 // Now that all the expected actions are registered, run the pipeline over
1302 // our module. All of our expectations are verified when the test finishes.
1303 MPM.run(*M, MAM);
1304}
1305
1306TEST_F(LoopPassManagerTest, LoopDeletion) {
1307 // Build a module with a single loop nest that contains one outer loop with
1308 // three subloops, and one of those with its own subloop. We will
1309 // incrementally delete all of these to test different deletion scenarios.
Chandler Carruth17350de2017-01-21 03:48:51 +00001310 M = parseIR(Context, "define void @f(i1* %ptr) {\n"
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001311 "entry:\n"
1312 " br label %loop.0\n"
1313 "loop.0:\n"
Chandler Carruth17350de2017-01-21 03:48:51 +00001314 " %cond.0 = load volatile i1, i1* %ptr\n"
1315 " br i1 %cond.0, label %loop.0.0.ph, label %end\n"
1316 "loop.0.0.ph:\n"
1317 " br label %loop.0.0\n"
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001318 "loop.0.0:\n"
Chandler Carruth17350de2017-01-21 03:48:51 +00001319 " %cond.0.0 = load volatile i1, i1* %ptr\n"
1320 " br i1 %cond.0.0, label %loop.0.0, label %loop.0.1.ph\n"
1321 "loop.0.1.ph:\n"
1322 " br label %loop.0.1\n"
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001323 "loop.0.1:\n"
Chandler Carruth17350de2017-01-21 03:48:51 +00001324 " %cond.0.1 = load volatile i1, i1* %ptr\n"
1325 " br i1 %cond.0.1, label %loop.0.1, label %loop.0.2.ph\n"
1326 "loop.0.2.ph:\n"
1327 " br label %loop.0.2\n"
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001328 "loop.0.2:\n"
Chandler Carruth17350de2017-01-21 03:48:51 +00001329 " %cond.0.2 = load volatile i1, i1* %ptr\n"
1330 " br i1 %cond.0.2, label %loop.0.2.0.ph, label %loop.0.latch\n"
1331 "loop.0.2.0.ph:\n"
1332 " br label %loop.0.2.0\n"
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001333 "loop.0.2.0:\n"
Chandler Carruth17350de2017-01-21 03:48:51 +00001334 " %cond.0.2.0 = load volatile i1, i1* %ptr\n"
1335 " br i1 %cond.0.2.0, label %loop.0.2.0, label %loop.0.2.latch\n"
1336 "loop.0.2.latch:\n"
1337 " br label %loop.0.2\n"
1338 "loop.0.latch:\n"
1339 " br label %loop.0\n"
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001340 "end:\n"
1341 " ret void\n"
1342 "}\n");
1343
1344 // Build up variables referring into the IR so we can rewrite it below
1345 // easily.
1346 Function &F = *M->begin();
1347 ASSERT_THAT(F, HasName("f"));
Chandler Carruth17350de2017-01-21 03:48:51 +00001348 Argument &Ptr = *F.arg_begin();
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001349 auto BBI = F.begin();
1350 BasicBlock &EntryBB = *BBI++;
1351 ASSERT_THAT(EntryBB, HasName("entry"));
1352 BasicBlock &Loop0BB = *BBI++;
1353 ASSERT_THAT(Loop0BB, HasName("loop.0"));
Chandler Carruth17350de2017-01-21 03:48:51 +00001354 BasicBlock &Loop00PHBB = *BBI++;
1355 ASSERT_THAT(Loop00PHBB, HasName("loop.0.0.ph"));
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001356 BasicBlock &Loop00BB = *BBI++;
1357 ASSERT_THAT(Loop00BB, HasName("loop.0.0"));
Chandler Carruth17350de2017-01-21 03:48:51 +00001358 BasicBlock &Loop01PHBB = *BBI++;
1359 ASSERT_THAT(Loop01PHBB, HasName("loop.0.1.ph"));
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001360 BasicBlock &Loop01BB = *BBI++;
1361 ASSERT_THAT(Loop01BB, HasName("loop.0.1"));
Chandler Carruth17350de2017-01-21 03:48:51 +00001362 BasicBlock &Loop02PHBB = *BBI++;
1363 ASSERT_THAT(Loop02PHBB, HasName("loop.0.2.ph"));
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001364 BasicBlock &Loop02BB = *BBI++;
1365 ASSERT_THAT(Loop02BB, HasName("loop.0.2"));
Chandler Carruth17350de2017-01-21 03:48:51 +00001366 BasicBlock &Loop020PHBB = *BBI++;
1367 ASSERT_THAT(Loop020PHBB, HasName("loop.0.2.0.ph"));
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001368 BasicBlock &Loop020BB = *BBI++;
1369 ASSERT_THAT(Loop020BB, HasName("loop.0.2.0"));
Chandler Carruth17350de2017-01-21 03:48:51 +00001370 BasicBlock &Loop02LatchBB = *BBI++;
1371 ASSERT_THAT(Loop02LatchBB, HasName("loop.0.2.latch"));
1372 BasicBlock &Loop0LatchBB = *BBI++;
1373 ASSERT_THAT(Loop0LatchBB, HasName("loop.0.latch"));
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001374 BasicBlock &EndBB = *BBI++;
1375 ASSERT_THAT(EndBB, HasName("end"));
1376 ASSERT_THAT(BBI, F.end());
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001377
1378 // Helper to do the actual deletion of a loop. We directly encode this here
1379 // to isolate ourselves from the rest of LLVM and for simplicity. Here we can
1380 // egregiously cheat based on knowledge of the test case. For example, we
1381 // have no PHI nodes and there is always a single i-dom.
Sanjoy Das76ab2322017-09-19 23:19:00 +00001382 auto EraseLoop = [](Loop &L, BasicBlock &IDomBB,
1383 LoopStandardAnalysisResults &AR, LPMUpdater &Updater) {
Chandler Carruth17350de2017-01-21 03:48:51 +00001384 assert(L.empty() && "Can only delete leaf loops with this routine!");
1385 SmallVector<BasicBlock *, 4> LoopBBs(L.block_begin(), L.block_end());
Sanjoy Dasdef17292017-09-28 02:45:42 +00001386 Updater.markLoopAsDeleted(L, L.getName());
Chandler Carruth17350de2017-01-21 03:48:51 +00001387 IDomBB.getTerminator()->replaceUsesOfWith(L.getHeader(),
1388 L.getUniqueExitBlock());
1389 for (BasicBlock *LoopBB : LoopBBs) {
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001390 SmallVector<DomTreeNode *, 4> ChildNodes(AR.DT[LoopBB]->begin(),
1391 AR.DT[LoopBB]->end());
1392 for (DomTreeNode *ChildNode : ChildNodes)
1393 AR.DT.changeImmediateDominator(ChildNode, AR.DT[&IDomBB]);
1394 AR.DT.eraseNode(LoopBB);
Chandler Carruth17350de2017-01-21 03:48:51 +00001395 AR.LI.removeBlock(LoopBB);
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001396 LoopBB->dropAllReferences();
1397 }
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001398 for (BasicBlock *LoopBB : LoopBBs)
1399 LoopBB->eraseFromParent();
Chandler Carruth17350de2017-01-21 03:48:51 +00001400
Sanjoy Das388b0122017-09-22 01:47:41 +00001401 AR.LI.erase(&L);
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001402 };
1403
1404 // Build up the pass managers.
1405 ModulePassManager MPM(true);
1406 FunctionPassManager FPM(true);
1407 // We run several loop pass pipelines across the loop nest, but they all take
1408 // the same form of three mock pass runs in a loop pipeline followed by
1409 // domtree and loop verification. We use a lambda to stamp this out each
1410 // time.
1411 auto AddLoopPipelineAndVerificationPasses = [&] {
1412 LoopPassManager LPM(true);
1413 LPM.addPass(MLPHandle.getPass());
1414 LPM.addPass(MLPHandle.getPass());
1415 LPM.addPass(MLPHandle.getPass());
1416 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
1417 FPM.addPass(DominatorTreeVerifierPass());
1418 FPM.addPass(LoopVerifierPass());
1419 };
1420
1421 // All the visit orders are deterministic so we use simple fully order
1422 // expectations.
1423 ::testing::InSequence MakeExpectationsSequenced;
1424
1425 // We run the loop pipeline with three passes over each of the loops. When
1426 // running over the middle loop, the second pass in the pipeline deletes it.
1427 // This should prevent the third pass from visiting it but otherwise leave
1428 // the process unimpacted.
1429 AddLoopPipelineAndVerificationPasses();
1430 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
1431 .WillOnce(Invoke(getLoopAnalysisResult));
1432 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _));
1433 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
1434 .Times(2)
1435 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1436
1437 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _))
1438 .WillOnce(Invoke(getLoopAnalysisResult));
1439 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _));
1440 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _))
1441 .WillOnce(
1442 Invoke([&](Loop &L, LoopAnalysisManager &AM,
1443 LoopStandardAnalysisResults &AR, LPMUpdater &Updater) {
Chandler Carruth17350de2017-01-21 03:48:51 +00001444 Loop *ParentL = L.getParentLoop();
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001445 AR.SE.forgetLoop(&L);
Sanjoy Das76ab2322017-09-19 23:19:00 +00001446 EraseLoop(L, Loop01PHBB, AR, Updater);
Chandler Carruth17350de2017-01-21 03:48:51 +00001447 ParentL->verifyLoop();
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001448 return PreservedAnalyses::all();
1449 }));
1450
1451 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2.0"), _, _, _))
1452 .WillOnce(Invoke(getLoopAnalysisResult));
1453 EXPECT_CALL(MLAHandle, run(HasName("loop.0.2.0"), _, _));
1454 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2.0"), _, _, _))
1455 .Times(2)
1456 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1457
1458 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2"), _, _, _))
1459 .WillOnce(Invoke(getLoopAnalysisResult));
1460 EXPECT_CALL(MLAHandle, run(HasName("loop.0.2"), _, _));
1461 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2"), _, _, _))
1462 .Times(2)
1463 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1464
1465 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _))
1466 .WillOnce(Invoke(getLoopAnalysisResult));
1467 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _));
1468 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _))
1469 .Times(2)
1470 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1471
1472 // Run the loop pipeline again. This time we delete the last loop, which
Sanjoy Das76ab2322017-09-19 23:19:00 +00001473 // contains a nested loop within it and insert a new loop into the nest. This
1474 // makes sure we can handle nested loop deletion.
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001475 AddLoopPipelineAndVerificationPasses();
1476 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
1477 .Times(3)
1478 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1479
1480 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2.0"), _, _, _))
1481 .Times(3)
1482 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1483
1484 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2"), _, _, _))
1485 .WillOnce(Invoke(getLoopAnalysisResult));
Chandler Carruth17350de2017-01-21 03:48:51 +00001486 BasicBlock *NewLoop03PHBB;
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001487 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2"), _, _, _))
1488 .WillOnce(
1489 Invoke([&](Loop &L, LoopAnalysisManager &AM,
1490 LoopStandardAnalysisResults &AR, LPMUpdater &Updater) {
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001491 AR.SE.forgetLoop(*L.begin());
Sanjoy Das76ab2322017-09-19 23:19:00 +00001492 EraseLoop(**L.begin(), Loop020PHBB, AR, Updater);
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001493
1494 auto *ParentL = L.getParentLoop();
1495 AR.SE.forgetLoop(&L);
Sanjoy Das76ab2322017-09-19 23:19:00 +00001496 EraseLoop(L, Loop02PHBB, AR, Updater);
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001497
Sanjoy Das76ab2322017-09-19 23:19:00 +00001498 // Now insert a new sibling loop.
Sanjoy Dasdef17292017-09-28 02:45:42 +00001499 auto *NewSibling = AR.LI.AllocateLoop();
Sanjoy Das76ab2322017-09-19 23:19:00 +00001500 ParentL->addChildLoop(NewSibling);
Chandler Carruth17350de2017-01-21 03:48:51 +00001501 NewLoop03PHBB =
1502 BasicBlock::Create(Context, "loop.0.3.ph", &F, &Loop0LatchBB);
1503 auto *NewLoop03BB =
1504 BasicBlock::Create(Context, "loop.0.3", &F, &Loop0LatchBB);
1505 BranchInst::Create(NewLoop03BB, NewLoop03PHBB);
1506 auto *Cond = new LoadInst(&Ptr, "cond.0.3", /*isVolatile*/ true,
1507 NewLoop03BB);
1508 BranchInst::Create(&Loop0LatchBB, NewLoop03BB, Cond, NewLoop03BB);
1509 Loop02PHBB.getTerminator()->replaceUsesOfWith(&Loop0LatchBB,
1510 NewLoop03PHBB);
1511 AR.DT.addNewBlock(NewLoop03PHBB, &Loop02PHBB);
1512 AR.DT.addNewBlock(NewLoop03BB, NewLoop03PHBB);
1513 AR.DT.changeImmediateDominator(AR.DT[&Loop0LatchBB],
1514 AR.DT[NewLoop03BB]);
David Green7c35de12018-02-28 11:00:08 +00001515 EXPECT_TRUE(AR.DT.verify());
Chandler Carruth17350de2017-01-21 03:48:51 +00001516 ParentL->addBasicBlockToLoop(NewLoop03PHBB, AR.LI);
Sanjoy Das76ab2322017-09-19 23:19:00 +00001517 NewSibling->addBasicBlockToLoop(NewLoop03BB, AR.LI);
1518 NewSibling->verifyLoop();
Chandler Carruth17350de2017-01-21 03:48:51 +00001519 ParentL->verifyLoop();
Sanjoy Das76ab2322017-09-19 23:19:00 +00001520 Updater.addSiblingLoops({NewSibling});
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001521 return PreservedAnalyses::all();
1522 }));
1523
1524 // To respect our inner-to-outer traversal order, we must visit the
1525 // newly-inserted sibling of the loop we just deleted before we visit the
1526 // outer loop. When we do so, this must compute a fresh analysis result, even
1527 // though our new loop has the same pointer value as the loop we deleted.
1528 EXPECT_CALL(MLPHandle, run(HasName("loop.0.3"), _, _, _))
1529 .WillOnce(Invoke(getLoopAnalysisResult));
1530 EXPECT_CALL(MLAHandle, run(HasName("loop.0.3"), _, _));
1531 EXPECT_CALL(MLPHandle, run(HasName("loop.0.3"), _, _, _))
1532 .Times(2)
1533 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1534
1535 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _))
1536 .Times(3)
1537 .WillRepeatedly(Invoke(getLoopAnalysisResult));
1538
1539 // In the final loop pipeline run we delete every loop, including the last
1540 // loop of the nest. We do this again in the second pass in the pipeline, and
1541 // as a consequence we never make it to three runs on any loop. We also cover
1542 // deleting multiple loops in a single pipeline, deleting the first loop and
1543 // deleting the (last) top level loop.
1544 AddLoopPipelineAndVerificationPasses();
1545 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
1546 .WillOnce(Invoke(getLoopAnalysisResult));
1547 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _))
1548 .WillOnce(
1549 Invoke([&](Loop &L, LoopAnalysisManager &AM,
1550 LoopStandardAnalysisResults &AR, LPMUpdater &Updater) {
1551 AR.SE.forgetLoop(&L);
Sanjoy Das76ab2322017-09-19 23:19:00 +00001552 EraseLoop(L, Loop00PHBB, AR, Updater);
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001553 return PreservedAnalyses::all();
1554 }));
1555
1556 EXPECT_CALL(MLPHandle, run(HasName("loop.0.3"), _, _, _))
1557 .WillOnce(Invoke(getLoopAnalysisResult));
1558 EXPECT_CALL(MLPHandle, run(HasName("loop.0.3"), _, _, _))
1559 .WillOnce(
1560 Invoke([&](Loop &L, LoopAnalysisManager &AM,
1561 LoopStandardAnalysisResults &AR, LPMUpdater &Updater) {
1562 AR.SE.forgetLoop(&L);
Sanjoy Das76ab2322017-09-19 23:19:00 +00001563 EraseLoop(L, *NewLoop03PHBB, AR, Updater);
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001564 return PreservedAnalyses::all();
1565 }));
1566
1567 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _))
1568 .WillOnce(Invoke(getLoopAnalysisResult));
1569 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _))
1570 .WillOnce(
1571 Invoke([&](Loop &L, LoopAnalysisManager &AM,
1572 LoopStandardAnalysisResults &AR, LPMUpdater &Updater) {
1573 AR.SE.forgetLoop(&L);
Sanjoy Das76ab2322017-09-19 23:19:00 +00001574 EraseLoop(L, EntryBB, AR, Updater);
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001575 return PreservedAnalyses::all();
1576 }));
1577
1578 // Add the function pass pipeline now that it is fully built up and run it
1579 // over the module's one function.
1580 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1581 MPM.run(*M, MAM);
Justin Bognereecc3c82016-02-25 07:23:08 +00001582}
1583}