1 //===- llvm/unittest/Analysis/LoopPassManagerTest.cpp - LPM tests ---------===//
3 // 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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Transforms/Scalar/LoopPassManager.h"
10 #include "llvm/Analysis/AliasAnalysis.h"
11 #include "llvm/Analysis/AssumptionCache.h"
12 #include "llvm/Analysis/ScalarEvolution.h"
13 #include "llvm/Analysis/TargetLibraryInfo.h"
14 #include "llvm/Analysis/TargetTransformInfo.h"
15 #include "llvm/AsmParser/Parser.h"
16 #include "llvm/IR/Dominators.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/LLVMContext.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/IR/PassManager.h"
21 #include "llvm/Support/SourceMgr.h"
23 #include "gmock/gmock.h"
24 #include "gtest/gtest.h"
30 using testing::DoDefault
;
31 using testing::Return
;
32 using testing::Expectation
;
33 using testing::Invoke
;
34 using testing::InvokeWithoutArgs
;
37 template <typename DerivedT
, typename IRUnitT
,
38 typename AnalysisManagerT
= AnalysisManager
<IRUnitT
>,
39 typename
... ExtraArgTs
>
40 class MockAnalysisHandleBase
{
42 class Analysis
: public AnalysisInfoMixin
<Analysis
> {
43 friend AnalysisInfoMixin
<Analysis
>;
44 friend MockAnalysisHandleBase
;
45 static AnalysisKey Key
;
49 Analysis(DerivedT
&Handle
) : Handle(&Handle
) {
50 static_assert(std::is_base_of
<MockAnalysisHandleBase
, DerivedT
>::value
,
51 "Must pass the derived type to this template!");
56 friend MockAnalysisHandleBase
;
60 Result(DerivedT
&Handle
) : Handle(&Handle
) {}
63 // Forward invalidation events to the mock handle.
64 bool invalidate(IRUnitT
&IR
, const PreservedAnalyses
&PA
,
65 typename
AnalysisManagerT::Invalidator
&Inv
) {
66 return Handle
->invalidate(IR
, PA
, Inv
);
70 Result
run(IRUnitT
&IR
, AnalysisManagerT
&AM
, ExtraArgTs
... ExtraArgs
) {
71 return Handle
->run(IR
, AM
, ExtraArgs
...);
75 Analysis
getAnalysis() { return Analysis(static_cast<DerivedT
&>(*this)); }
76 typename
Analysis::Result
getResult() {
77 return typename
Analysis::Result(static_cast<DerivedT
&>(*this));
81 // FIXME: MSVC seems unable to handle a lambda argument to Invoke from within
82 // the template, so we use a boring static function.
83 static bool invalidateCallback(IRUnitT
&IR
, const PreservedAnalyses
&PA
,
84 typename
AnalysisManagerT::Invalidator
&Inv
) {
85 auto PAC
= PA
.template getChecker
<Analysis
>();
86 return !PAC
.preserved() &&
87 !PAC
.template preservedSet
<AllAnalysesOn
<IRUnitT
>>();
90 /// Derived classes should call this in their constructor to set up default
91 /// mock actions. (We can't do this in our constructor because this has to
92 /// run after the DerivedT is constructed.)
94 ON_CALL(static_cast<DerivedT
&>(*this),
95 run(_
, _
, testing::Matcher
<ExtraArgTs
>(_
)...))
96 .WillByDefault(Return(this->getResult()));
97 ON_CALL(static_cast<DerivedT
&>(*this), invalidate(_
, _
, _
))
98 .WillByDefault(Invoke(&invalidateCallback
));
102 template <typename DerivedT
, typename IRUnitT
, typename AnalysisManagerT
,
103 typename
... ExtraArgTs
>
104 AnalysisKey MockAnalysisHandleBase
<DerivedT
, IRUnitT
, AnalysisManagerT
,
105 ExtraArgTs
...>::Analysis::Key
;
107 /// Mock handle for loop analyses.
109 /// This is provided as a template accepting an (optional) integer. Because
110 /// analyses are identified and queried by type, this allows constructing
111 /// multiple handles with distinctly typed nested 'Analysis' types that can be
112 /// registered and queried. If you want to register multiple loop analysis
113 /// passes, you'll need to instantiate this type with different values for I.
116 /// MockLoopAnalysisHandleTemplate<0> h0;
117 /// MockLoopAnalysisHandleTemplate<1> h1;
118 /// typedef decltype(h0)::Analysis Analysis0;
119 /// typedef decltype(h1)::Analysis Analysis1;
120 template <size_t I
= static_cast<size_t>(-1)>
121 struct MockLoopAnalysisHandleTemplate
122 : MockAnalysisHandleBase
<MockLoopAnalysisHandleTemplate
<I
>, Loop
,
124 LoopStandardAnalysisResults
&> {
125 typedef typename
MockLoopAnalysisHandleTemplate::Analysis Analysis
;
127 MOCK_METHOD3_T(run
, typename
Analysis::Result(Loop
&, LoopAnalysisManager
&,
128 LoopStandardAnalysisResults
&));
130 MOCK_METHOD3_T(invalidate
, bool(Loop
&, const PreservedAnalyses
&,
131 LoopAnalysisManager::Invalidator
&));
133 MockLoopAnalysisHandleTemplate() { this->setDefaults(); }
136 typedef MockLoopAnalysisHandleTemplate
<> MockLoopAnalysisHandle
;
138 struct MockFunctionAnalysisHandle
139 : MockAnalysisHandleBase
<MockFunctionAnalysisHandle
, Function
> {
140 MOCK_METHOD2(run
, Analysis::Result(Function
&, FunctionAnalysisManager
&));
142 MOCK_METHOD3(invalidate
, bool(Function
&, const PreservedAnalyses
&,
143 FunctionAnalysisManager::Invalidator
&));
145 MockFunctionAnalysisHandle() { setDefaults(); }
148 template <typename DerivedT
, typename IRUnitT
,
149 typename AnalysisManagerT
= AnalysisManager
<IRUnitT
>,
150 typename
... ExtraArgTs
>
151 class MockPassHandleBase
{
153 class Pass
: public PassInfoMixin
<Pass
> {
154 friend MockPassHandleBase
;
158 Pass(DerivedT
&Handle
) : Handle(&Handle
) {
159 static_assert(std::is_base_of
<MockPassHandleBase
, DerivedT
>::value
,
160 "Must pass the derived type to this template!");
164 PreservedAnalyses
run(IRUnitT
&IR
, AnalysisManagerT
&AM
,
165 ExtraArgTs
... ExtraArgs
) {
166 return Handle
->run(IR
, AM
, ExtraArgs
...);
170 Pass
getPass() { return Pass(static_cast<DerivedT
&>(*this)); }
173 /// Derived classes should call this in their constructor to set up default
174 /// mock actions. (We can't do this in our constructor because this has to
175 /// run after the DerivedT is constructed.)
177 ON_CALL(static_cast<DerivedT
&>(*this),
178 run(_
, _
, testing::Matcher
<ExtraArgTs
>(_
)...))
179 .WillByDefault(Return(PreservedAnalyses::all()));
183 struct MockLoopPassHandle
184 : MockPassHandleBase
<MockLoopPassHandle
, Loop
, LoopAnalysisManager
,
185 LoopStandardAnalysisResults
&, LPMUpdater
&> {
187 PreservedAnalyses(Loop
&, LoopAnalysisManager
&,
188 LoopStandardAnalysisResults
&, LPMUpdater
&));
189 MockLoopPassHandle() { setDefaults(); }
192 struct MockFunctionPassHandle
193 : MockPassHandleBase
<MockFunctionPassHandle
, Function
> {
194 MOCK_METHOD2(run
, PreservedAnalyses(Function
&, FunctionAnalysisManager
&));
196 MockFunctionPassHandle() { setDefaults(); }
199 struct MockModulePassHandle
: MockPassHandleBase
<MockModulePassHandle
, Module
> {
200 MOCK_METHOD2(run
, PreservedAnalyses(Module
&, ModuleAnalysisManager
&));
202 MockModulePassHandle() { setDefaults(); }
205 /// Define a custom matcher for objects which support a 'getName' method
206 /// returning a StringRef.
208 /// LLVM often has IR objects or analysis objects which expose a StringRef name
209 /// and in tests it is convenient to match these by name for readability. This
210 /// matcher supports any type exposing a getName() method of this form.
212 /// It should be used as:
214 /// HasName("my_function")
216 /// No namespace or other qualification is required.
217 MATCHER_P(HasName
, Name
, "") {
218 // The matcher's name and argument are printed in the case of failure, but we
219 // also want to print out the name of the argument. This uses an implicitly
220 // avaiable std::ostream, so we have to construct a std::string.
221 *result_listener
<< "has name '" << arg
.getName().str() << "'";
222 return Name
== arg
.getName();
225 std::unique_ptr
<Module
> parseIR(LLVMContext
&C
, const char *IR
) {
227 return parseAssemblyString(IR
, Err
, C
);
230 class LoopPassManagerTest
: public ::testing::Test
{
233 std::unique_ptr
<Module
> M
;
235 LoopAnalysisManager LAM
;
236 FunctionAnalysisManager FAM
;
237 ModuleAnalysisManager MAM
;
239 MockLoopAnalysisHandle MLAHandle
;
240 MockLoopPassHandle MLPHandle
;
241 MockFunctionPassHandle MFPHandle
;
242 MockModulePassHandle MMPHandle
;
244 static PreservedAnalyses
245 getLoopAnalysisResult(Loop
&L
, LoopAnalysisManager
&AM
,
246 LoopStandardAnalysisResults
&AR
, LPMUpdater
&) {
247 (void)AM
.getResult
<MockLoopAnalysisHandle::Analysis
>(L
, AR
);
248 return PreservedAnalyses::all();
252 LoopPassManagerTest()
254 "define void @f(i1* %ptr) {\n"
256 " br label %loop.0\n"
258 " %cond.0 = load volatile i1, i1* %ptr\n"
259 " br i1 %cond.0, label %loop.0.0.ph, label %end\n"
261 " br label %loop.0.0\n"
263 " %cond.0.0 = load volatile i1, i1* %ptr\n"
264 " br i1 %cond.0.0, label %loop.0.0, label %loop.0.1.ph\n"
266 " br label %loop.0.1\n"
268 " %cond.0.1 = load volatile i1, i1* %ptr\n"
269 " br i1 %cond.0.1, label %loop.0.1, label %loop.0.latch\n"
271 " br label %loop.0\n"
276 "define void @g(i1* %ptr) {\n"
278 " br label %loop.g.0\n"
280 " %cond.0 = load volatile i1, i1* %ptr\n"
281 " br i1 %cond.0, label %loop.g.0, label %end\n"
285 LAM(true), FAM(true), MAM(true) {
286 // Register our mock analysis.
287 LAM
.registerPass([&] { return MLAHandle
.getAnalysis(); });
289 // We need DominatorTreeAnalysis for LoopAnalysis.
290 FAM
.registerPass([&] { return DominatorTreeAnalysis(); });
291 FAM
.registerPass([&] { return LoopAnalysis(); });
292 // We also allow loop passes to assume a set of other analyses and so need
294 FAM
.registerPass([&] { return AAManager(); });
295 FAM
.registerPass([&] { return AssumptionAnalysis(); });
296 if (EnableMSSALoopDependency
)
297 FAM
.registerPass([&] { return MemorySSAAnalysis(); });
298 FAM
.registerPass([&] { return ScalarEvolutionAnalysis(); });
299 FAM
.registerPass([&] { return TargetLibraryAnalysis(); });
300 FAM
.registerPass([&] { return TargetIRAnalysis(); });
302 // Register required pass instrumentation analysis.
303 LAM
.registerPass([&] { return PassInstrumentationAnalysis(); });
304 FAM
.registerPass([&] { return PassInstrumentationAnalysis(); });
305 MAM
.registerPass([&] { return PassInstrumentationAnalysis(); });
307 // Cross-register proxies.
308 LAM
.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM
); });
309 FAM
.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM
); });
310 FAM
.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM
); });
311 MAM
.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM
); });
315 TEST_F(LoopPassManagerTest
, Basic
) {
316 ModulePassManager
MPM(true);
317 ::testing::InSequence MakeExpectationsSequenced
;
319 // First we just visit all the loops in all the functions and get their
320 // analysis results. This will run the analysis a total of four times,
321 // once for each loop.
322 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.0"), _
, _
, _
))
323 .WillOnce(Invoke(getLoopAnalysisResult
));
324 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.0"), _
, _
));
325 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.1"), _
, _
, _
))
326 .WillOnce(Invoke(getLoopAnalysisResult
));
327 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.1"), _
, _
));
328 EXPECT_CALL(MLPHandle
, run(HasName("loop.0"), _
, _
, _
))
329 .WillOnce(Invoke(getLoopAnalysisResult
));
330 EXPECT_CALL(MLAHandle
, run(HasName("loop.0"), _
, _
));
331 EXPECT_CALL(MLPHandle
, run(HasName("loop.g.0"), _
, _
, _
))
332 .WillOnce(Invoke(getLoopAnalysisResult
));
333 EXPECT_CALL(MLAHandle
, run(HasName("loop.g.0"), _
, _
));
334 // Wire the loop pass through pass managers into the module pipeline.
336 LoopPassManager
LPM(true);
337 LPM
.addPass(MLPHandle
.getPass());
338 FunctionPassManager
FPM(true);
339 FPM
.addPass(createFunctionToLoopPassAdaptor(std::move(LPM
)));
340 MPM
.addPass(createModuleToFunctionPassAdaptor(std::move(FPM
)));
343 // Next we run two passes over the loops. The first one invalidates the
344 // analyses for one loop, the second ones try to get the analysis results.
345 // This should force only one analysis to re-run within the loop PM, but will
346 // also invalidate everything after the loop pass manager finishes.
347 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.0"), _
, _
, _
))
348 .WillOnce(DoDefault())
349 .WillOnce(Invoke(getLoopAnalysisResult
));
350 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.1"), _
, _
, _
))
351 .WillOnce(InvokeWithoutArgs([] { return PreservedAnalyses::none(); }))
352 .WillOnce(Invoke(getLoopAnalysisResult
));
353 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.1"), _
, _
));
354 EXPECT_CALL(MLPHandle
, run(HasName("loop.0"), _
, _
, _
))
355 .WillOnce(DoDefault())
356 .WillOnce(Invoke(getLoopAnalysisResult
));
357 EXPECT_CALL(MLPHandle
, run(HasName("loop.g.0"), _
, _
, _
))
358 .WillOnce(DoDefault())
359 .WillOnce(Invoke(getLoopAnalysisResult
));
360 // Wire two loop pass runs into the module pipeline.
362 LoopPassManager
LPM(true);
363 LPM
.addPass(MLPHandle
.getPass());
364 LPM
.addPass(MLPHandle
.getPass());
365 FunctionPassManager
FPM(true);
366 FPM
.addPass(createFunctionToLoopPassAdaptor(std::move(LPM
)));
367 MPM
.addPass(createModuleToFunctionPassAdaptor(std::move(FPM
)));
370 // And now run the pipeline across the module.
374 TEST_F(LoopPassManagerTest
, FunctionPassInvalidationOfLoopAnalyses
) {
375 ModulePassManager
MPM(true);
376 FunctionPassManager
FPM(true);
377 // We process each function completely in sequence.
378 ::testing::Sequence FSequence
, GSequence
;
380 // First, force the analysis result to be computed for each loop.
381 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.0"), _
, _
))
382 .InSequence(FSequence
)
383 .WillOnce(DoDefault());
384 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.1"), _
, _
))
385 .InSequence(FSequence
)
386 .WillOnce(DoDefault());
387 EXPECT_CALL(MLAHandle
, run(HasName("loop.0"), _
, _
))
388 .InSequence(FSequence
)
389 .WillOnce(DoDefault());
390 EXPECT_CALL(MLAHandle
, run(HasName("loop.g.0"), _
, _
))
391 .InSequence(GSequence
)
392 .WillOnce(DoDefault());
393 FPM
.addPass(createFunctionToLoopPassAdaptor(
394 RequireAnalysisLoopPass
<MockLoopAnalysisHandle::Analysis
>()));
396 // No need to re-run if we require again from a fresh loop pass manager.
397 FPM
.addPass(createFunctionToLoopPassAdaptor(
398 RequireAnalysisLoopPass
<MockLoopAnalysisHandle::Analysis
>()));
399 // For 'f', preserve most things but not the specific loop analyses.
400 auto PA
= getLoopPassPreservedAnalyses();
401 if (EnableMSSALoopDependency
)
402 PA
.preserve
<MemorySSAAnalysis
>();
403 EXPECT_CALL(MFPHandle
, run(HasName("f"), _
))
404 .InSequence(FSequence
)
405 .WillOnce(Return(PA
));
406 EXPECT_CALL(MLAHandle
, invalidate(HasName("loop.0.0"), _
, _
))
407 .InSequence(FSequence
)
408 .WillOnce(DoDefault());
409 // On one loop, skip the invalidation (as though we did an internal update).
410 EXPECT_CALL(MLAHandle
, invalidate(HasName("loop.0.1"), _
, _
))
411 .InSequence(FSequence
)
412 .WillOnce(Return(false));
413 EXPECT_CALL(MLAHandle
, invalidate(HasName("loop.0"), _
, _
))
414 .InSequence(FSequence
)
415 .WillOnce(DoDefault());
416 // Now two loops still have to be recomputed.
417 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.0"), _
, _
))
418 .InSequence(FSequence
)
419 .WillOnce(DoDefault());
420 EXPECT_CALL(MLAHandle
, run(HasName("loop.0"), _
, _
))
421 .InSequence(FSequence
)
422 .WillOnce(DoDefault());
423 // Preserve things in the second function to ensure invalidation remains
424 // isolated to one function.
425 EXPECT_CALL(MFPHandle
, run(HasName("g"), _
))
426 .InSequence(GSequence
)
427 .WillOnce(DoDefault());
428 FPM
.addPass(MFPHandle
.getPass());
429 FPM
.addPass(createFunctionToLoopPassAdaptor(
430 RequireAnalysisLoopPass
<MockLoopAnalysisHandle::Analysis
>()));
432 EXPECT_CALL(MFPHandle
, run(HasName("f"), _
))
433 .InSequence(FSequence
)
434 .WillOnce(DoDefault());
435 // For 'g', fail to preserve anything, causing the loops themselves to be
436 // cleared. We don't get an invalidation event here as the loop is gone, but
437 // we should still have to recompute the analysis.
438 EXPECT_CALL(MFPHandle
, run(HasName("g"), _
))
439 .InSequence(GSequence
)
440 .WillOnce(Return(PreservedAnalyses::none()));
441 EXPECT_CALL(MLAHandle
, run(HasName("loop.g.0"), _
, _
))
442 .InSequence(GSequence
)
443 .WillOnce(DoDefault());
444 FPM
.addPass(MFPHandle
.getPass());
445 FPM
.addPass(createFunctionToLoopPassAdaptor(
446 RequireAnalysisLoopPass
<MockLoopAnalysisHandle::Analysis
>()));
448 MPM
.addPass(createModuleToFunctionPassAdaptor(std::move(FPM
)));
450 // Verify with a separate function pass run that we didn't mess up 'f's
451 // cache. No analysis runs should be necessary here.
452 MPM
.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor(
453 RequireAnalysisLoopPass
<MockLoopAnalysisHandle::Analysis
>())));
458 TEST_F(LoopPassManagerTest
, ModulePassInvalidationOfLoopAnalyses
) {
459 ModulePassManager
MPM(true);
460 ::testing::InSequence MakeExpectationsSequenced
;
462 // First, force the analysis result to be computed for each loop.
463 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.0"), _
, _
));
464 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.1"), _
, _
));
465 EXPECT_CALL(MLAHandle
, run(HasName("loop.0"), _
, _
));
466 EXPECT_CALL(MLAHandle
, run(HasName("loop.g.0"), _
, _
));
467 MPM
.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor(
468 RequireAnalysisLoopPass
<MockLoopAnalysisHandle::Analysis
>())));
470 // Walking all the way out and all the way back in doesn't re-run the
472 MPM
.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor(
473 RequireAnalysisLoopPass
<MockLoopAnalysisHandle::Analysis
>())));
475 // But a module pass that doesn't preserve the actual mock loop analysis
476 // invalidates all the way down and forces recomputing.
477 EXPECT_CALL(MMPHandle
, run(_
, _
)).WillOnce(InvokeWithoutArgs([] {
478 auto PA
= getLoopPassPreservedAnalyses();
479 PA
.preserve
<FunctionAnalysisManagerModuleProxy
>();
480 if (EnableMSSALoopDependency
)
481 PA
.preserve
<MemorySSAAnalysis
>();
484 // All the loop analyses from both functions get invalidated before we
485 // recompute anything.
486 EXPECT_CALL(MLAHandle
, invalidate(HasName("loop.0.0"), _
, _
));
487 // On one loop, again skip the invalidation (as though we did an internal
489 EXPECT_CALL(MLAHandle
, invalidate(HasName("loop.0.1"), _
, _
))
490 .WillOnce(Return(false));
491 EXPECT_CALL(MLAHandle
, invalidate(HasName("loop.0"), _
, _
));
492 EXPECT_CALL(MLAHandle
, invalidate(HasName("loop.g.0"), _
, _
));
493 // Now all but one of the loops gets re-analyzed.
494 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.0"), _
, _
));
495 EXPECT_CALL(MLAHandle
, run(HasName("loop.0"), _
, _
));
496 EXPECT_CALL(MLAHandle
, run(HasName("loop.g.0"), _
, _
));
497 MPM
.addPass(MMPHandle
.getPass());
498 MPM
.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor(
499 RequireAnalysisLoopPass
<MockLoopAnalysisHandle::Analysis
>())));
501 // Verify that the cached values persist.
502 MPM
.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor(
503 RequireAnalysisLoopPass
<MockLoopAnalysisHandle::Analysis
>())));
505 // Now we fail to preserve the loop analysis and observe that the loop
506 // analyses are cleared (so no invalidation event) as the loops themselves
507 // are no longer valid.
508 EXPECT_CALL(MMPHandle
, run(_
, _
)).WillOnce(InvokeWithoutArgs([] {
509 auto PA
= PreservedAnalyses::none();
510 PA
.preserve
<FunctionAnalysisManagerModuleProxy
>();
513 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.0"), _
, _
));
514 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.1"), _
, _
));
515 EXPECT_CALL(MLAHandle
, run(HasName("loop.0"), _
, _
));
516 EXPECT_CALL(MLAHandle
, run(HasName("loop.g.0"), _
, _
));
517 MPM
.addPass(MMPHandle
.getPass());
518 MPM
.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor(
519 RequireAnalysisLoopPass
<MockLoopAnalysisHandle::Analysis
>())));
521 // Verify that the cached values persist.
522 MPM
.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor(
523 RequireAnalysisLoopPass
<MockLoopAnalysisHandle::Analysis
>())));
525 // Next, check that even if we preserve everything within the function itelf,
526 // if the function's module pass proxy isn't preserved and the potential set
527 // of functions changes, the clear reaches the loop analyses as well. This
528 // will again trigger re-runs but not invalidation events.
529 EXPECT_CALL(MMPHandle
, run(_
, _
)).WillOnce(InvokeWithoutArgs([] {
530 auto PA
= PreservedAnalyses::none();
531 PA
.preserveSet
<AllAnalysesOn
<Function
>>();
532 PA
.preserveSet
<AllAnalysesOn
<Loop
>>();
535 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.0"), _
, _
));
536 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.1"), _
, _
));
537 EXPECT_CALL(MLAHandle
, run(HasName("loop.0"), _
, _
));
538 EXPECT_CALL(MLAHandle
, run(HasName("loop.g.0"), _
, _
));
539 MPM
.addPass(MMPHandle
.getPass());
540 MPM
.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor(
541 RequireAnalysisLoopPass
<MockLoopAnalysisHandle::Analysis
>())));
546 // Test that if any of the bundled analyses provided in the LPM's signature
547 // become invalid, the analysis proxy itself becomes invalid and we clear all
548 // loop analysis results.
549 TEST_F(LoopPassManagerTest
, InvalidationOfBundledAnalyses
) {
550 ModulePassManager
MPM(true);
551 FunctionPassManager
FPM(true);
552 ::testing::InSequence MakeExpectationsSequenced
;
554 // First, force the analysis result to be computed for each loop.
555 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.0"), _
, _
));
556 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.1"), _
, _
));
557 EXPECT_CALL(MLAHandle
, run(HasName("loop.0"), _
, _
));
558 FPM
.addPass(createFunctionToLoopPassAdaptor(
559 RequireAnalysisLoopPass
<MockLoopAnalysisHandle::Analysis
>()));
561 // No need to re-run if we require again from a fresh loop pass manager.
562 FPM
.addPass(createFunctionToLoopPassAdaptor(
563 RequireAnalysisLoopPass
<MockLoopAnalysisHandle::Analysis
>()));
565 // Preserving everything but the loop analyses themselves results in
566 // invalidation and running.
567 EXPECT_CALL(MFPHandle
, run(HasName("f"), _
))
568 .WillOnce(Return(getLoopPassPreservedAnalyses()));
569 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.0"), _
, _
));
570 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.1"), _
, _
));
571 EXPECT_CALL(MLAHandle
, run(HasName("loop.0"), _
, _
));
572 FPM
.addPass(MFPHandle
.getPass());
573 FPM
.addPass(createFunctionToLoopPassAdaptor(
574 RequireAnalysisLoopPass
<MockLoopAnalysisHandle::Analysis
>()));
576 // The rest don't invalidate analyses, they only trigger re-runs because we
577 // clear the cache completely.
578 EXPECT_CALL(MFPHandle
, run(HasName("f"), _
)).WillOnce(InvokeWithoutArgs([] {
579 auto PA
= PreservedAnalyses::none();
580 // Not preserving `AAManager`.
581 PA
.preserve
<DominatorTreeAnalysis
>();
582 PA
.preserve
<LoopAnalysis
>();
583 PA
.preserve
<LoopAnalysisManagerFunctionProxy
>();
584 PA
.preserve
<ScalarEvolutionAnalysis
>();
587 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.0"), _
, _
));
588 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.1"), _
, _
));
589 EXPECT_CALL(MLAHandle
, run(HasName("loop.0"), _
, _
));
590 FPM
.addPass(MFPHandle
.getPass());
591 FPM
.addPass(createFunctionToLoopPassAdaptor(
592 RequireAnalysisLoopPass
<MockLoopAnalysisHandle::Analysis
>()));
594 EXPECT_CALL(MFPHandle
, run(HasName("f"), _
)).WillOnce(InvokeWithoutArgs([] {
595 auto PA
= PreservedAnalyses::none();
596 PA
.preserve
<AAManager
>();
597 // Not preserving `DominatorTreeAnalysis`.
598 PA
.preserve
<LoopAnalysis
>();
599 PA
.preserve
<LoopAnalysisManagerFunctionProxy
>();
600 PA
.preserve
<ScalarEvolutionAnalysis
>();
603 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.0"), _
, _
));
604 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.1"), _
, _
));
605 EXPECT_CALL(MLAHandle
, run(HasName("loop.0"), _
, _
));
606 FPM
.addPass(MFPHandle
.getPass());
607 FPM
.addPass(createFunctionToLoopPassAdaptor(
608 RequireAnalysisLoopPass
<MockLoopAnalysisHandle::Analysis
>()));
610 EXPECT_CALL(MFPHandle
, run(HasName("f"), _
)).WillOnce(InvokeWithoutArgs([] {
611 auto PA
= PreservedAnalyses::none();
612 PA
.preserve
<AAManager
>();
613 PA
.preserve
<DominatorTreeAnalysis
>();
614 // Not preserving the `LoopAnalysis`.
615 PA
.preserve
<LoopAnalysisManagerFunctionProxy
>();
616 PA
.preserve
<ScalarEvolutionAnalysis
>();
619 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.0"), _
, _
));
620 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.1"), _
, _
));
621 EXPECT_CALL(MLAHandle
, run(HasName("loop.0"), _
, _
));
622 FPM
.addPass(MFPHandle
.getPass());
623 FPM
.addPass(createFunctionToLoopPassAdaptor(
624 RequireAnalysisLoopPass
<MockLoopAnalysisHandle::Analysis
>()));
626 EXPECT_CALL(MFPHandle
, run(HasName("f"), _
)).WillOnce(InvokeWithoutArgs([] {
627 auto PA
= PreservedAnalyses::none();
628 PA
.preserve
<AAManager
>();
629 PA
.preserve
<DominatorTreeAnalysis
>();
630 PA
.preserve
<LoopAnalysis
>();
631 // Not preserving the `LoopAnalysisManagerFunctionProxy`.
632 PA
.preserve
<ScalarEvolutionAnalysis
>();
635 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.0"), _
, _
));
636 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.1"), _
, _
));
637 EXPECT_CALL(MLAHandle
, run(HasName("loop.0"), _
, _
));
638 FPM
.addPass(MFPHandle
.getPass());
639 FPM
.addPass(createFunctionToLoopPassAdaptor(
640 RequireAnalysisLoopPass
<MockLoopAnalysisHandle::Analysis
>()));
642 EXPECT_CALL(MFPHandle
, run(HasName("f"), _
)).WillOnce(InvokeWithoutArgs([] {
643 auto PA
= PreservedAnalyses::none();
644 PA
.preserve
<AAManager
>();
645 PA
.preserve
<DominatorTreeAnalysis
>();
646 PA
.preserve
<LoopAnalysis
>();
647 PA
.preserve
<LoopAnalysisManagerFunctionProxy
>();
648 // Not preserving `ScalarEvolutionAnalysis`.
651 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.0"), _
, _
));
652 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.1"), _
, _
));
653 EXPECT_CALL(MLAHandle
, run(HasName("loop.0"), _
, _
));
654 FPM
.addPass(MFPHandle
.getPass());
655 FPM
.addPass(createFunctionToLoopPassAdaptor(
656 RequireAnalysisLoopPass
<MockLoopAnalysisHandle::Analysis
>()));
658 // After all the churn on 'f', we'll compute the loop analysis results for
659 // 'g' once with a requires pass and then run our mock pass over g a bunch
660 // but just get cached results each time.
661 EXPECT_CALL(MLAHandle
, run(HasName("loop.g.0"), _
, _
));
662 EXPECT_CALL(MFPHandle
, run(HasName("g"), _
)).Times(6);
664 MPM
.addPass(createModuleToFunctionPassAdaptor(std::move(FPM
)));
668 TEST_F(LoopPassManagerTest
, IndirectInvalidation
) {
669 // We need two distinct analysis types and handles.
671 MockLoopAnalysisHandleTemplate
<A
> MLAHandleA
;
672 MockLoopAnalysisHandleTemplate
<B
> MLAHandleB
;
673 LAM
.registerPass([&] { return MLAHandleA
.getAnalysis(); });
674 LAM
.registerPass([&] { return MLAHandleB
.getAnalysis(); });
675 typedef decltype(MLAHandleA
)::Analysis AnalysisA
;
676 typedef decltype(MLAHandleB
)::Analysis AnalysisB
;
678 // Set up AnalysisA to depend on our AnalysisB. For testing purposes we just
679 // need to get the AnalysisB results in AnalysisA's run method and check if
680 // AnalysisB gets invalidated in AnalysisA's invalidate method.
681 ON_CALL(MLAHandleA
, run(_
, _
, _
))
682 .WillByDefault(Invoke([&](Loop
&L
, LoopAnalysisManager
&AM
,
683 LoopStandardAnalysisResults
&AR
) {
684 (void)AM
.getResult
<AnalysisB
>(L
, AR
);
685 return MLAHandleA
.getResult();
687 ON_CALL(MLAHandleA
, invalidate(_
, _
, _
))
688 .WillByDefault(Invoke([](Loop
&L
, const PreservedAnalyses
&PA
,
689 LoopAnalysisManager::Invalidator
&Inv
) {
690 auto PAC
= PA
.getChecker
<AnalysisA
>();
691 return !(PAC
.preserved() || PAC
.preservedSet
<AllAnalysesOn
<Loop
>>()) ||
692 Inv
.invalidate
<AnalysisB
>(L
, PA
);
695 ::testing::InSequence MakeExpectationsSequenced
;
697 // Compute the analyses across all of 'f' first.
698 EXPECT_CALL(MLAHandleA
, run(HasName("loop.0.0"), _
, _
));
699 EXPECT_CALL(MLAHandleB
, run(HasName("loop.0.0"), _
, _
));
700 EXPECT_CALL(MLAHandleA
, run(HasName("loop.0.1"), _
, _
));
701 EXPECT_CALL(MLAHandleB
, run(HasName("loop.0.1"), _
, _
));
702 EXPECT_CALL(MLAHandleA
, run(HasName("loop.0"), _
, _
));
703 EXPECT_CALL(MLAHandleB
, run(HasName("loop.0"), _
, _
));
705 // Now we invalidate AnalysisB (but not AnalysisA) for one of the loops and
706 // preserve everything for the rest. This in turn triggers that one loop to
707 // recompute both AnalysisB *and* AnalysisA if indirect invalidation is
709 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.0"), _
, _
, _
))
710 .WillOnce(InvokeWithoutArgs([] {
711 auto PA
= getLoopPassPreservedAnalyses();
712 // Specifically preserve AnalysisA so that it would survive if it
713 // didn't depend on AnalysisB.
714 PA
.preserve
<AnalysisA
>();
717 // It happens that AnalysisB is invalidated first. That shouldn't matter
718 // though, and we should still call AnalysisA's invalidation.
719 EXPECT_CALL(MLAHandleB
, invalidate(HasName("loop.0.0"), _
, _
));
720 EXPECT_CALL(MLAHandleA
, invalidate(HasName("loop.0.0"), _
, _
));
721 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.0"), _
, _
, _
))
722 .WillOnce(Invoke([](Loop
&L
, LoopAnalysisManager
&AM
,
723 LoopStandardAnalysisResults
&AR
, LPMUpdater
&) {
724 (void)AM
.getResult
<AnalysisA
>(L
, AR
);
725 return PreservedAnalyses::all();
727 EXPECT_CALL(MLAHandleA
, run(HasName("loop.0.0"), _
, _
));
728 EXPECT_CALL(MLAHandleB
, run(HasName("loop.0.0"), _
, _
));
729 // The rest of the loops should run and get cached results.
730 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.1"), _
, _
, _
))
732 .WillRepeatedly(Invoke([](Loop
&L
, LoopAnalysisManager
&AM
,
733 LoopStandardAnalysisResults
&AR
, LPMUpdater
&) {
734 (void)AM
.getResult
<AnalysisA
>(L
, AR
);
735 return PreservedAnalyses::all();
737 EXPECT_CALL(MLPHandle
, run(HasName("loop.0"), _
, _
, _
))
739 .WillRepeatedly(Invoke([](Loop
&L
, LoopAnalysisManager
&AM
,
740 LoopStandardAnalysisResults
&AR
, LPMUpdater
&) {
741 (void)AM
.getResult
<AnalysisA
>(L
, AR
);
742 return PreservedAnalyses::all();
745 // The run over 'g' should be boring, with us just computing the analyses once
746 // up front and then running loop passes and getting cached results.
747 EXPECT_CALL(MLAHandleA
, run(HasName("loop.g.0"), _
, _
));
748 EXPECT_CALL(MLAHandleB
, run(HasName("loop.g.0"), _
, _
));
749 EXPECT_CALL(MLPHandle
, run(HasName("loop.g.0"), _
, _
, _
))
751 .WillRepeatedly(Invoke([](Loop
&L
, LoopAnalysisManager
&AM
,
752 LoopStandardAnalysisResults
&AR
, LPMUpdater
&) {
753 (void)AM
.getResult
<AnalysisA
>(L
, AR
);
754 return PreservedAnalyses::all();
757 // Build the pipeline and run it.
758 ModulePassManager
MPM(true);
759 FunctionPassManager
FPM(true);
761 createFunctionToLoopPassAdaptor(RequireAnalysisLoopPass
<AnalysisA
>()));
762 LoopPassManager
LPM(true);
763 LPM
.addPass(MLPHandle
.getPass());
764 LPM
.addPass(MLPHandle
.getPass());
765 FPM
.addPass(createFunctionToLoopPassAdaptor(std::move(LPM
)));
766 MPM
.addPass(createModuleToFunctionPassAdaptor(std::move(FPM
)));
770 TEST_F(LoopPassManagerTest
, IndirectOuterPassInvalidation
) {
771 typedef decltype(MLAHandle
)::Analysis LoopAnalysis
;
773 MockFunctionAnalysisHandle MFAHandle
;
774 FAM
.registerPass([&] { return MFAHandle
.getAnalysis(); });
775 typedef decltype(MFAHandle
)::Analysis FunctionAnalysis
;
777 // Set up the loop analysis to depend on both the function and module
779 ON_CALL(MLAHandle
, run(_
, _
, _
))
780 .WillByDefault(Invoke([&](Loop
&L
, LoopAnalysisManager
&AM
,
781 LoopStandardAnalysisResults
&AR
) {
782 auto &FAMP
= AM
.getResult
<FunctionAnalysisManagerLoopProxy
>(L
, AR
);
783 auto &FAM
= FAMP
.getManager();
784 Function
&F
= *L
.getHeader()->getParent();
785 if (FAM
.getCachedResult
<FunctionAnalysis
>(F
))
786 FAMP
.registerOuterAnalysisInvalidation
<FunctionAnalysis
,
788 return MLAHandle
.getResult();
791 ::testing::InSequence MakeExpectationsSequenced
;
793 // Compute the analyses across all of 'f' first.
794 EXPECT_CALL(MFPHandle
, run(HasName("f"), _
))
795 .WillOnce(Invoke([](Function
&F
, FunctionAnalysisManager
&AM
) {
796 // Force the computing of the function analysis so it is available in
798 (void)AM
.getResult
<FunctionAnalysis
>(F
);
799 return PreservedAnalyses::all();
801 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.0"), _
, _
));
802 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.1"), _
, _
));
803 EXPECT_CALL(MLAHandle
, run(HasName("loop.0"), _
, _
));
805 // Now invalidate the function analysis but preserve the loop analyses.
806 // This should trigger immediate invalidation of the loop analyses, despite
807 // the fact that they were preserved.
808 EXPECT_CALL(MFPHandle
, run(HasName("f"), _
)).WillOnce(InvokeWithoutArgs([] {
809 auto PA
= getLoopPassPreservedAnalyses();
810 if (EnableMSSALoopDependency
)
811 PA
.preserve
<MemorySSAAnalysis
>();
812 PA
.preserveSet
<AllAnalysesOn
<Loop
>>();
815 EXPECT_CALL(MLAHandle
, invalidate(HasName("loop.0.0"), _
, _
));
816 EXPECT_CALL(MLAHandle
, invalidate(HasName("loop.0.1"), _
, _
));
817 EXPECT_CALL(MLAHandle
, invalidate(HasName("loop.0"), _
, _
));
819 // And re-running a requires pass recomputes them.
820 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.0"), _
, _
));
821 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.1"), _
, _
));
822 EXPECT_CALL(MLAHandle
, run(HasName("loop.0"), _
, _
));
824 // When we run over 'g' we don't populate the cache with the function
826 EXPECT_CALL(MFPHandle
, run(HasName("g"), _
))
827 .WillOnce(Return(PreservedAnalyses::all()));
828 EXPECT_CALL(MLAHandle
, run(HasName("loop.g.0"), _
, _
));
830 // Which means that no extra invalidation occurs and cached values are used.
831 EXPECT_CALL(MFPHandle
, run(HasName("g"), _
)).WillOnce(InvokeWithoutArgs([] {
832 auto PA
= getLoopPassPreservedAnalyses();
833 if (EnableMSSALoopDependency
)
834 PA
.preserve
<MemorySSAAnalysis
>();
835 PA
.preserveSet
<AllAnalysesOn
<Loop
>>();
839 // Build the pipeline and run it.
840 ModulePassManager
MPM(true);
841 FunctionPassManager
FPM(true);
842 FPM
.addPass(MFPHandle
.getPass());
844 createFunctionToLoopPassAdaptor(RequireAnalysisLoopPass
<LoopAnalysis
>()));
845 FPM
.addPass(MFPHandle
.getPass());
847 createFunctionToLoopPassAdaptor(RequireAnalysisLoopPass
<LoopAnalysis
>()));
848 MPM
.addPass(createModuleToFunctionPassAdaptor(std::move(FPM
)));
852 TEST_F(LoopPassManagerTest
, LoopChildInsertion
) {
853 // Super boring module with three loops in a single loop nest.
854 M
= parseIR(Context
, "define void @f(i1* %ptr) {\n"
856 " br label %loop.0\n"
858 " %cond.0 = load volatile i1, i1* %ptr\n"
859 " br i1 %cond.0, label %loop.0.0.ph, label %end\n"
861 " br label %loop.0.0\n"
863 " %cond.0.0 = load volatile i1, i1* %ptr\n"
864 " br i1 %cond.0.0, label %loop.0.0, label %loop.0.1.ph\n"
866 " br label %loop.0.1\n"
868 " %cond.0.1 = load volatile i1, i1* %ptr\n"
869 " br i1 %cond.0.1, label %loop.0.1, label %loop.0.2.ph\n"
871 " br label %loop.0.2\n"
873 " %cond.0.2 = load volatile i1, i1* %ptr\n"
874 " br i1 %cond.0.2, label %loop.0.2, label %loop.0.latch\n"
876 " br label %loop.0\n"
881 // Build up variables referring into the IR so we can rewrite it below
883 Function
&F
= *M
->begin();
884 ASSERT_THAT(F
, HasName("f"));
885 Argument
&Ptr
= *F
.arg_begin();
886 auto BBI
= F
.begin();
887 BasicBlock
&EntryBB
= *BBI
++;
888 ASSERT_THAT(EntryBB
, HasName("entry"));
889 BasicBlock
&Loop0BB
= *BBI
++;
890 ASSERT_THAT(Loop0BB
, HasName("loop.0"));
891 BasicBlock
&Loop00PHBB
= *BBI
++;
892 ASSERT_THAT(Loop00PHBB
, HasName("loop.0.0.ph"));
893 BasicBlock
&Loop00BB
= *BBI
++;
894 ASSERT_THAT(Loop00BB
, HasName("loop.0.0"));
895 BasicBlock
&Loop01PHBB
= *BBI
++;
896 ASSERT_THAT(Loop01PHBB
, HasName("loop.0.1.ph"));
897 BasicBlock
&Loop01BB
= *BBI
++;
898 ASSERT_THAT(Loop01BB
, HasName("loop.0.1"));
899 BasicBlock
&Loop02PHBB
= *BBI
++;
900 ASSERT_THAT(Loop02PHBB
, HasName("loop.0.2.ph"));
901 BasicBlock
&Loop02BB
= *BBI
++;
902 ASSERT_THAT(Loop02BB
, HasName("loop.0.2"));
903 BasicBlock
&Loop0LatchBB
= *BBI
++;
904 ASSERT_THAT(Loop0LatchBB
, HasName("loop.0.latch"));
905 BasicBlock
&EndBB
= *BBI
++;
906 ASSERT_THAT(EndBB
, HasName("end"));
907 ASSERT_THAT(BBI
, F
.end());
908 auto CreateCondBr
= [&](BasicBlock
*TrueBB
, BasicBlock
*FalseBB
,
909 const char *Name
, BasicBlock
*BB
) {
910 auto *Cond
= new LoadInst(Type::getInt1Ty(Context
), &Ptr
, Name
,
911 /*isVolatile*/ true, BB
);
912 BranchInst::Create(TrueBB
, FalseBB
, Cond
, BB
);
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
)));
930 // All the visit orders are deterministic, so we use simple fully order
932 ::testing::InSequence MakeExpectationsSequenced
;
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"), _
, _
, _
))
940 .WillRepeatedly(Invoke(getLoopAnalysisResult
));
942 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.1"), _
, _
, _
))
943 .WillOnce(Invoke(getLoopAnalysisResult
));
944 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.1"), _
, _
));
946 // When running over the middle loop, the second run inserts two new child
947 // loops, inserting them and itself into the worklist.
948 BasicBlock
*NewLoop010BB
, *NewLoop01LatchBB
;
949 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.1"), _
, _
, _
))
950 .WillOnce(Invoke([&](Loop
&L
, LoopAnalysisManager
&AM
,
951 LoopStandardAnalysisResults
&AR
,
952 LPMUpdater
&Updater
) {
953 auto *NewLoop
= AR
.LI
.AllocateLoop();
954 L
.addChildLoop(NewLoop
);
955 auto *NewLoop010PHBB
=
956 BasicBlock::Create(Context
, "loop.0.1.0.ph", &F
, &Loop02PHBB
);
958 BasicBlock::Create(Context
, "loop.0.1.0", &F
, &Loop02PHBB
);
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",
965 BranchInst::Create(&Loop01BB
, NewLoop01LatchBB
);
966 AR
.DT
.addNewBlock(NewLoop010PHBB
, &Loop01BB
);
967 AR
.DT
.addNewBlock(NewLoop010BB
, NewLoop010PHBB
);
968 AR
.DT
.addNewBlock(NewLoop01LatchBB
, NewLoop010BB
);
969 EXPECT_TRUE(AR
.DT
.verify());
970 L
.addBasicBlockToLoop(NewLoop010PHBB
, AR
.LI
);
971 NewLoop
->addBasicBlockToLoop(NewLoop010BB
, AR
.LI
);
972 L
.addBasicBlockToLoop(NewLoop01LatchBB
, AR
.LI
);
973 NewLoop
->verifyLoop();
975 Updater
.addChildLoops({NewLoop
});
976 return PreservedAnalyses::all();
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"), _
, _
, _
))
985 .WillRepeatedly(Invoke(getLoopAnalysisResult
));
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
));
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.
995 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.1"), _
, _
, _
))
996 .WillOnce(Invoke([&](Loop
&L
, LoopAnalysisManager
&AM
,
997 LoopStandardAnalysisResults
&AR
,
998 LPMUpdater
&Updater
) {
999 auto *NewLoop
= AR
.LI
.AllocateLoop();
1000 L
.addChildLoop(NewLoop
);
1001 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
,
1005 BranchInst::Create(NewLoop011BB
, NewLoop011PHBB
);
1006 CreateCondBr(NewLoop01LatchBB
, NewLoop011BB
, "cond.0.1.1",
1008 AR
.DT
.addNewBlock(NewLoop011PHBB
, NewLoop010BB
);
1009 auto *NewDTNode
= AR
.DT
.addNewBlock(NewLoop011BB
, NewLoop011PHBB
);
1010 AR
.DT
.changeImmediateDominator(AR
.DT
[NewLoop01LatchBB
], NewDTNode
);
1011 EXPECT_TRUE(AR
.DT
.verify());
1012 L
.addBasicBlockToLoop(NewLoop011PHBB
, AR
.LI
);
1013 NewLoop
->addBasicBlockToLoop(NewLoop011BB
, AR
.LI
);
1014 NewLoop
->verifyLoop();
1016 Updater
.addChildLoops({NewLoop
});
1017 return PreservedAnalyses::all();
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"), _
, _
, _
))
1027 .WillRepeatedly(Invoke(getLoopAnalysisResult
));
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"), _
, _
, _
))
1033 .WillRepeatedly(Invoke(getLoopAnalysisResult
));
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"), _
, _
, _
))
1040 .WillRepeatedly(Invoke(getLoopAnalysisResult
));
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"), _
, _
, _
))
1047 .WillRepeatedly(Invoke(getLoopAnalysisResult
));
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.
1054 TEST_F(LoopPassManagerTest
, LoopPeerInsertion
) {
1055 // Super boring module with two loop nests and loop nest with two child
1057 M
= parseIR(Context
, "define void @f(i1* %ptr) {\n"
1059 " br label %loop.0\n"
1061 " %cond.0 = load volatile i1, i1* %ptr\n"
1062 " br i1 %cond.0, label %loop.0.0.ph, label %loop.2.ph\n"
1064 " br label %loop.0.0\n"
1066 " %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"
1069 " br label %loop.0.2\n"
1071 " %cond.0.2 = load volatile i1, i1* %ptr\n"
1072 " br i1 %cond.0.2, label %loop.0.2, label %loop.0.latch\n"
1074 " br label %loop.0\n"
1076 " br label %loop.2\n"
1078 " %cond.2 = load volatile i1, i1* %ptr\n"
1079 " br i1 %cond.2, label %loop.2, label %end\n"
1084 // Build up variables referring into the IR so we can rewrite it below
1086 Function
&F
= *M
->begin();
1087 ASSERT_THAT(F
, HasName("f"));
1088 Argument
&Ptr
= *F
.arg_begin();
1089 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"));
1094 BasicBlock
&Loop00PHBB
= *BBI
++;
1095 ASSERT_THAT(Loop00PHBB
, HasName("loop.0.0.ph"));
1096 BasicBlock
&Loop00BB
= *BBI
++;
1097 ASSERT_THAT(Loop00BB
, HasName("loop.0.0"));
1098 BasicBlock
&Loop02PHBB
= *BBI
++;
1099 ASSERT_THAT(Loop02PHBB
, HasName("loop.0.2.ph"));
1100 BasicBlock
&Loop02BB
= *BBI
++;
1101 ASSERT_THAT(Loop02BB
, HasName("loop.0.2"));
1102 BasicBlock
&Loop0LatchBB
= *BBI
++;
1103 ASSERT_THAT(Loop0LatchBB
, HasName("loop.0.latch"));
1104 BasicBlock
&Loop2PHBB
= *BBI
++;
1105 ASSERT_THAT(Loop2PHBB
, HasName("loop.2.ph"));
1106 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());
1111 auto CreateCondBr
= [&](BasicBlock
*TrueBB
, BasicBlock
*FalseBB
,
1112 const char *Name
, BasicBlock
*BB
) {
1113 auto *Cond
= new LoadInst(Type::getInt1Ty(Context
), &Ptr
, Name
,
1114 /*isVolatile*/ true, BB
);
1115 BranchInst::Create(TrueBB
, FalseBB
, Cond
, BB
);
1118 // Build the pass managers and register our pipeline. We build a single loop
1119 // pass pipeline consisting of three mock pass runs over each loop. After
1120 // this we run both domtree and loop verification passes to make sure that
1121 // the IR remained valid during our mutations.
1122 ModulePassManager
MPM(true);
1123 FunctionPassManager
FPM(true);
1124 LoopPassManager
LPM(true);
1125 LPM
.addPass(MLPHandle
.getPass());
1126 LPM
.addPass(MLPHandle
.getPass());
1127 LPM
.addPass(MLPHandle
.getPass());
1128 FPM
.addPass(createFunctionToLoopPassAdaptor(std::move(LPM
)));
1129 FPM
.addPass(DominatorTreeVerifierPass());
1130 FPM
.addPass(LoopVerifierPass());
1131 MPM
.addPass(createModuleToFunctionPassAdaptor(std::move(FPM
)));
1133 // All the visit orders are deterministic, so we use simple fully order
1135 ::testing::InSequence MakeExpectationsSequenced
;
1137 // We run loop passes three times over each of the loops.
1138 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.0"), _
, _
, _
))
1139 .WillOnce(Invoke(getLoopAnalysisResult
));
1140 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.0"), _
, _
));
1142 // On the second run, we insert a sibling loop.
1143 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.0"), _
, _
, _
))
1144 .WillOnce(Invoke([&](Loop
&L
, LoopAnalysisManager
&AM
,
1145 LoopStandardAnalysisResults
&AR
,
1146 LPMUpdater
&Updater
) {
1147 auto *NewLoop
= AR
.LI
.AllocateLoop();
1148 L
.getParentLoop()->addChildLoop(NewLoop
);
1149 auto *NewLoop01PHBB
= BasicBlock::Create(Context
, "loop.0.1.ph", &F
, &Loop02PHBB
);
1150 auto *NewLoop01BB
= BasicBlock::Create(Context
, "loop.0.1", &F
, &Loop02PHBB
);
1151 BranchInst::Create(NewLoop01BB
, NewLoop01PHBB
);
1152 CreateCondBr(&Loop02PHBB
, NewLoop01BB
, "cond.0.1", NewLoop01BB
);
1153 Loop00BB
.getTerminator()->replaceUsesOfWith(&Loop02PHBB
, NewLoop01PHBB
);
1154 AR
.DT
.addNewBlock(NewLoop01PHBB
, &Loop00BB
);
1155 auto *NewDTNode
= AR
.DT
.addNewBlock(NewLoop01BB
, NewLoop01PHBB
);
1156 AR
.DT
.changeImmediateDominator(AR
.DT
[&Loop02PHBB
], NewDTNode
);
1157 EXPECT_TRUE(AR
.DT
.verify());
1158 L
.getParentLoop()->addBasicBlockToLoop(NewLoop01PHBB
, AR
.LI
);
1159 NewLoop
->addBasicBlockToLoop(NewLoop01BB
, AR
.LI
);
1160 L
.getParentLoop()->verifyLoop();
1161 Updater
.addSiblingLoops({NewLoop
});
1162 return PreservedAnalyses::all();
1164 // We finish processing this loop as sibling loops don't perturb the
1166 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.0"), _
, _
, _
))
1167 .WillOnce(Invoke(getLoopAnalysisResult
));
1169 // We visit the inserted sibling next.
1170 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.1"), _
, _
, _
))
1171 .WillOnce(Invoke(getLoopAnalysisResult
));
1172 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.1"), _
, _
));
1173 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.1"), _
, _
, _
))
1175 .WillRepeatedly(Invoke(getLoopAnalysisResult
));
1177 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.2"), _
, _
, _
))
1178 .WillOnce(Invoke(getLoopAnalysisResult
));
1179 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.2"), _
, _
));
1180 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.2"), _
, _
, _
))
1181 .WillOnce(Invoke(getLoopAnalysisResult
));
1182 // Next, on the third pass run on the last inner loop we add more new
1183 // siblings, more than one, and one with nested child loops. By doing this at
1184 // the end we make sure that edge case works well.
1185 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.2"), _
, _
, _
))
1186 .WillOnce(Invoke([&](Loop
&L
, LoopAnalysisManager
&AM
,
1187 LoopStandardAnalysisResults
&AR
,
1188 LPMUpdater
&Updater
) {
1189 Loop
*NewLoops
[] = {AR
.LI
.AllocateLoop(), AR
.LI
.AllocateLoop(),
1190 AR
.LI
.AllocateLoop()};
1191 L
.getParentLoop()->addChildLoop(NewLoops
[0]);
1192 L
.getParentLoop()->addChildLoop(NewLoops
[1]);
1193 NewLoops
[1]->addChildLoop(NewLoops
[2]);
1194 auto *NewLoop03PHBB
=
1195 BasicBlock::Create(Context
, "loop.0.3.ph", &F
, &Loop0LatchBB
);
1197 BasicBlock::Create(Context
, "loop.0.3", &F
, &Loop0LatchBB
);
1198 auto *NewLoop04PHBB
=
1199 BasicBlock::Create(Context
, "loop.0.4.ph", &F
, &Loop0LatchBB
);
1201 BasicBlock::Create(Context
, "loop.0.4", &F
, &Loop0LatchBB
);
1202 auto *NewLoop040PHBB
=
1203 BasicBlock::Create(Context
, "loop.0.4.0.ph", &F
, &Loop0LatchBB
);
1204 auto *NewLoop040BB
=
1205 BasicBlock::Create(Context
, "loop.0.4.0", &F
, &Loop0LatchBB
);
1206 auto *NewLoop04LatchBB
=
1207 BasicBlock::Create(Context
, "loop.0.4.latch", &F
, &Loop0LatchBB
);
1208 Loop02BB
.getTerminator()->replaceUsesOfWith(&Loop0LatchBB
, NewLoop03PHBB
);
1209 BranchInst::Create(NewLoop03BB
, NewLoop03PHBB
);
1210 CreateCondBr(NewLoop04PHBB
, NewLoop03BB
, "cond.0.3", NewLoop03BB
);
1211 BranchInst::Create(NewLoop04BB
, NewLoop04PHBB
);
1212 CreateCondBr(&Loop0LatchBB
, NewLoop040PHBB
, "cond.0.4", NewLoop04BB
);
1213 BranchInst::Create(NewLoop040BB
, NewLoop040PHBB
);
1214 CreateCondBr(NewLoop04LatchBB
, NewLoop040BB
, "cond.0.4.0", NewLoop040BB
);
1215 BranchInst::Create(NewLoop04BB
, NewLoop04LatchBB
);
1216 AR
.DT
.addNewBlock(NewLoop03PHBB
, &Loop02BB
);
1217 AR
.DT
.addNewBlock(NewLoop03BB
, NewLoop03PHBB
);
1218 AR
.DT
.addNewBlock(NewLoop04PHBB
, NewLoop03BB
);
1219 auto *NewDTNode
= AR
.DT
.addNewBlock(NewLoop04BB
, NewLoop04PHBB
);
1220 AR
.DT
.changeImmediateDominator(AR
.DT
[&Loop0LatchBB
], NewDTNode
);
1221 AR
.DT
.addNewBlock(NewLoop040PHBB
, NewLoop04BB
);
1222 AR
.DT
.addNewBlock(NewLoop040BB
, NewLoop040PHBB
);
1223 AR
.DT
.addNewBlock(NewLoop04LatchBB
, NewLoop040BB
);
1224 EXPECT_TRUE(AR
.DT
.verify());
1225 L
.getParentLoop()->addBasicBlockToLoop(NewLoop03PHBB
, AR
.LI
);
1226 NewLoops
[0]->addBasicBlockToLoop(NewLoop03BB
, AR
.LI
);
1227 L
.getParentLoop()->addBasicBlockToLoop(NewLoop04PHBB
, AR
.LI
);
1228 NewLoops
[1]->addBasicBlockToLoop(NewLoop04BB
, AR
.LI
);
1229 NewLoops
[1]->addBasicBlockToLoop(NewLoop040PHBB
, AR
.LI
);
1230 NewLoops
[2]->addBasicBlockToLoop(NewLoop040BB
, AR
.LI
);
1231 NewLoops
[1]->addBasicBlockToLoop(NewLoop04LatchBB
, AR
.LI
);
1232 L
.getParentLoop()->verifyLoop();
1233 Updater
.addSiblingLoops({NewLoops
[0], NewLoops
[1]});
1234 return PreservedAnalyses::all();
1237 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.3"), _
, _
, _
))
1238 .WillOnce(Invoke(getLoopAnalysisResult
));
1239 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.3"), _
, _
));
1240 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.3"), _
, _
, _
))
1242 .WillRepeatedly(Invoke(getLoopAnalysisResult
));
1244 // Note that we need to visit the inner loop of this added sibling before the
1246 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.4.0"), _
, _
, _
))
1247 .WillOnce(Invoke(getLoopAnalysisResult
));
1248 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.4.0"), _
, _
));
1249 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.4.0"), _
, _
, _
))
1251 .WillRepeatedly(Invoke(getLoopAnalysisResult
));
1253 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.4"), _
, _
, _
))
1254 .WillOnce(Invoke(getLoopAnalysisResult
));
1255 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.4"), _
, _
));
1256 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.4"), _
, _
, _
))
1258 .WillRepeatedly(Invoke(getLoopAnalysisResult
));
1260 // And only now do we visit the outermost loop of the nest.
1261 EXPECT_CALL(MLPHandle
, run(HasName("loop.0"), _
, _
, _
))
1262 .WillOnce(Invoke(getLoopAnalysisResult
));
1263 EXPECT_CALL(MLAHandle
, run(HasName("loop.0"), _
, _
));
1264 // On the second pass, we add sibling loops which become new top-level loops.
1265 EXPECT_CALL(MLPHandle
, run(HasName("loop.0"), _
, _
, _
))
1266 .WillOnce(Invoke([&](Loop
&L
, LoopAnalysisManager
&AM
,
1267 LoopStandardAnalysisResults
&AR
,
1268 LPMUpdater
&Updater
) {
1269 auto *NewLoop
= AR
.LI
.AllocateLoop();
1270 AR
.LI
.addTopLevelLoop(NewLoop
);
1271 auto *NewLoop1PHBB
= BasicBlock::Create(Context
, "loop.1.ph", &F
, &Loop2BB
);
1272 auto *NewLoop1BB
= BasicBlock::Create(Context
, "loop.1", &F
, &Loop2BB
);
1273 BranchInst::Create(NewLoop1BB
, NewLoop1PHBB
);
1274 CreateCondBr(&Loop2PHBB
, NewLoop1BB
, "cond.1", NewLoop1BB
);
1275 Loop0BB
.getTerminator()->replaceUsesOfWith(&Loop2PHBB
, NewLoop1PHBB
);
1276 AR
.DT
.addNewBlock(NewLoop1PHBB
, &Loop0BB
);
1277 auto *NewDTNode
= AR
.DT
.addNewBlock(NewLoop1BB
, NewLoop1PHBB
);
1278 AR
.DT
.changeImmediateDominator(AR
.DT
[&Loop2PHBB
], NewDTNode
);
1279 EXPECT_TRUE(AR
.DT
.verify());
1280 NewLoop
->addBasicBlockToLoop(NewLoop1BB
, AR
.LI
);
1281 NewLoop
->verifyLoop();
1282 Updater
.addSiblingLoops({NewLoop
});
1283 return PreservedAnalyses::all();
1285 EXPECT_CALL(MLPHandle
, run(HasName("loop.0"), _
, _
, _
))
1286 .WillOnce(Invoke(getLoopAnalysisResult
));
1288 EXPECT_CALL(MLPHandle
, run(HasName("loop.1"), _
, _
, _
))
1289 .WillOnce(Invoke(getLoopAnalysisResult
));
1290 EXPECT_CALL(MLAHandle
, run(HasName("loop.1"), _
, _
));
1291 EXPECT_CALL(MLPHandle
, run(HasName("loop.1"), _
, _
, _
))
1293 .WillRepeatedly(Invoke(getLoopAnalysisResult
));
1295 EXPECT_CALL(MLPHandle
, run(HasName("loop.2"), _
, _
, _
))
1296 .WillOnce(Invoke(getLoopAnalysisResult
));
1297 EXPECT_CALL(MLAHandle
, run(HasName("loop.2"), _
, _
));
1298 EXPECT_CALL(MLPHandle
, run(HasName("loop.2"), _
, _
, _
))
1300 .WillRepeatedly(Invoke(getLoopAnalysisResult
));
1302 // Now that all the expected actions are registered, run the pipeline over
1303 // our module. All of our expectations are verified when the test finishes.
1307 TEST_F(LoopPassManagerTest
, LoopDeletion
) {
1308 // Build a module with a single loop nest that contains one outer loop with
1309 // three subloops, and one of those with its own subloop. We will
1310 // incrementally delete all of these to test different deletion scenarios.
1311 M
= parseIR(Context
, "define void @f(i1* %ptr) {\n"
1313 " br label %loop.0\n"
1315 " %cond.0 = load volatile i1, i1* %ptr\n"
1316 " br i1 %cond.0, label %loop.0.0.ph, label %end\n"
1318 " br label %loop.0.0\n"
1320 " %cond.0.0 = load volatile i1, i1* %ptr\n"
1321 " br i1 %cond.0.0, label %loop.0.0, label %loop.0.1.ph\n"
1323 " br label %loop.0.1\n"
1325 " %cond.0.1 = load volatile i1, i1* %ptr\n"
1326 " br i1 %cond.0.1, label %loop.0.1, label %loop.0.2.ph\n"
1328 " br label %loop.0.2\n"
1330 " %cond.0.2 = load volatile i1, i1* %ptr\n"
1331 " br i1 %cond.0.2, label %loop.0.2.0.ph, label %loop.0.latch\n"
1333 " br label %loop.0.2.0\n"
1335 " %cond.0.2.0 = load volatile i1, i1* %ptr\n"
1336 " br i1 %cond.0.2.0, label %loop.0.2.0, label %loop.0.2.latch\n"
1338 " br label %loop.0.2\n"
1340 " br label %loop.0\n"
1345 // Build up variables referring into the IR so we can rewrite it below
1347 Function
&F
= *M
->begin();
1348 ASSERT_THAT(F
, HasName("f"));
1349 Argument
&Ptr
= *F
.arg_begin();
1350 auto BBI
= F
.begin();
1351 BasicBlock
&EntryBB
= *BBI
++;
1352 ASSERT_THAT(EntryBB
, HasName("entry"));
1353 BasicBlock
&Loop0BB
= *BBI
++;
1354 ASSERT_THAT(Loop0BB
, HasName("loop.0"));
1355 BasicBlock
&Loop00PHBB
= *BBI
++;
1356 ASSERT_THAT(Loop00PHBB
, HasName("loop.0.0.ph"));
1357 BasicBlock
&Loop00BB
= *BBI
++;
1358 ASSERT_THAT(Loop00BB
, HasName("loop.0.0"));
1359 BasicBlock
&Loop01PHBB
= *BBI
++;
1360 ASSERT_THAT(Loop01PHBB
, HasName("loop.0.1.ph"));
1361 BasicBlock
&Loop01BB
= *BBI
++;
1362 ASSERT_THAT(Loop01BB
, HasName("loop.0.1"));
1363 BasicBlock
&Loop02PHBB
= *BBI
++;
1364 ASSERT_THAT(Loop02PHBB
, HasName("loop.0.2.ph"));
1365 BasicBlock
&Loop02BB
= *BBI
++;
1366 ASSERT_THAT(Loop02BB
, HasName("loop.0.2"));
1367 BasicBlock
&Loop020PHBB
= *BBI
++;
1368 ASSERT_THAT(Loop020PHBB
, HasName("loop.0.2.0.ph"));
1369 BasicBlock
&Loop020BB
= *BBI
++;
1370 ASSERT_THAT(Loop020BB
, HasName("loop.0.2.0"));
1371 BasicBlock
&Loop02LatchBB
= *BBI
++;
1372 ASSERT_THAT(Loop02LatchBB
, HasName("loop.0.2.latch"));
1373 BasicBlock
&Loop0LatchBB
= *BBI
++;
1374 ASSERT_THAT(Loop0LatchBB
, HasName("loop.0.latch"));
1375 BasicBlock
&EndBB
= *BBI
++;
1376 ASSERT_THAT(EndBB
, HasName("end"));
1377 ASSERT_THAT(BBI
, F
.end());
1379 // Helper to do the actual deletion of a loop. We directly encode this here
1380 // to isolate ourselves from the rest of LLVM and for simplicity. Here we can
1381 // egregiously cheat based on knowledge of the test case. For example, we
1382 // have no PHI nodes and there is always a single i-dom.
1383 auto EraseLoop
= [](Loop
&L
, BasicBlock
&IDomBB
,
1384 LoopStandardAnalysisResults
&AR
, LPMUpdater
&Updater
) {
1385 assert(L
.empty() && "Can only delete leaf loops with this routine!");
1386 SmallVector
<BasicBlock
*, 4> LoopBBs(L
.block_begin(), L
.block_end());
1387 Updater
.markLoopAsDeleted(L
, L
.getName());
1388 IDomBB
.getTerminator()->replaceUsesOfWith(L
.getHeader(),
1389 L
.getUniqueExitBlock());
1390 for (BasicBlock
*LoopBB
: LoopBBs
) {
1391 SmallVector
<DomTreeNode
*, 4> ChildNodes(AR
.DT
[LoopBB
]->begin(),
1392 AR
.DT
[LoopBB
]->end());
1393 for (DomTreeNode
*ChildNode
: ChildNodes
)
1394 AR
.DT
.changeImmediateDominator(ChildNode
, AR
.DT
[&IDomBB
]);
1395 AR
.DT
.eraseNode(LoopBB
);
1396 AR
.LI
.removeBlock(LoopBB
);
1397 LoopBB
->dropAllReferences();
1399 for (BasicBlock
*LoopBB
: LoopBBs
)
1400 LoopBB
->eraseFromParent();
1405 // Build up the pass managers.
1406 ModulePassManager
MPM(true);
1407 FunctionPassManager
FPM(true);
1408 // We run several loop pass pipelines across the loop nest, but they all take
1409 // the same form of three mock pass runs in a loop pipeline followed by
1410 // domtree and loop verification. We use a lambda to stamp this out each
1412 auto AddLoopPipelineAndVerificationPasses
= [&] {
1413 LoopPassManager
LPM(true);
1414 LPM
.addPass(MLPHandle
.getPass());
1415 LPM
.addPass(MLPHandle
.getPass());
1416 LPM
.addPass(MLPHandle
.getPass());
1417 FPM
.addPass(createFunctionToLoopPassAdaptor(std::move(LPM
)));
1418 FPM
.addPass(DominatorTreeVerifierPass());
1419 FPM
.addPass(LoopVerifierPass());
1422 // All the visit orders are deterministic so we use simple fully order
1424 ::testing::InSequence MakeExpectationsSequenced
;
1426 // We run the loop pipeline with three passes over each of the loops. When
1427 // running over the middle loop, the second pass in the pipeline deletes it.
1428 // This should prevent the third pass from visiting it but otherwise leave
1429 // the process unimpacted.
1430 AddLoopPipelineAndVerificationPasses();
1431 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.0"), _
, _
, _
))
1432 .WillOnce(Invoke(getLoopAnalysisResult
));
1433 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.0"), _
, _
));
1434 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.0"), _
, _
, _
))
1436 .WillRepeatedly(Invoke(getLoopAnalysisResult
));
1438 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.1"), _
, _
, _
))
1439 .WillOnce(Invoke(getLoopAnalysisResult
));
1440 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.1"), _
, _
));
1441 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.1"), _
, _
, _
))
1443 Invoke([&](Loop
&L
, LoopAnalysisManager
&AM
,
1444 LoopStandardAnalysisResults
&AR
, LPMUpdater
&Updater
) {
1445 Loop
*ParentL
= L
.getParentLoop();
1446 AR
.SE
.forgetLoop(&L
);
1447 EraseLoop(L
, Loop01PHBB
, AR
, Updater
);
1448 ParentL
->verifyLoop();
1449 return PreservedAnalyses::all();
1452 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.2.0"), _
, _
, _
))
1453 .WillOnce(Invoke(getLoopAnalysisResult
));
1454 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.2.0"), _
, _
));
1455 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.2.0"), _
, _
, _
))
1457 .WillRepeatedly(Invoke(getLoopAnalysisResult
));
1459 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.2"), _
, _
, _
))
1460 .WillOnce(Invoke(getLoopAnalysisResult
));
1461 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.2"), _
, _
));
1462 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.2"), _
, _
, _
))
1464 .WillRepeatedly(Invoke(getLoopAnalysisResult
));
1466 EXPECT_CALL(MLPHandle
, run(HasName("loop.0"), _
, _
, _
))
1467 .WillOnce(Invoke(getLoopAnalysisResult
));
1468 EXPECT_CALL(MLAHandle
, run(HasName("loop.0"), _
, _
));
1469 EXPECT_CALL(MLPHandle
, run(HasName("loop.0"), _
, _
, _
))
1471 .WillRepeatedly(Invoke(getLoopAnalysisResult
));
1473 // Run the loop pipeline again. This time we delete the last loop, which
1474 // contains a nested loop within it and insert a new loop into the nest. This
1475 // makes sure we can handle nested loop deletion.
1476 AddLoopPipelineAndVerificationPasses();
1477 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.0"), _
, _
, _
))
1479 .WillRepeatedly(Invoke(getLoopAnalysisResult
));
1481 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.2.0"), _
, _
, _
))
1483 .WillRepeatedly(Invoke(getLoopAnalysisResult
));
1485 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.2"), _
, _
, _
))
1486 .WillOnce(Invoke(getLoopAnalysisResult
));
1487 BasicBlock
*NewLoop03PHBB
;
1488 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.2"), _
, _
, _
))
1490 Invoke([&](Loop
&L
, LoopAnalysisManager
&AM
,
1491 LoopStandardAnalysisResults
&AR
, LPMUpdater
&Updater
) {
1492 AR
.SE
.forgetLoop(*L
.begin());
1493 EraseLoop(**L
.begin(), Loop020PHBB
, AR
, Updater
);
1495 auto *ParentL
= L
.getParentLoop();
1496 AR
.SE
.forgetLoop(&L
);
1497 EraseLoop(L
, Loop02PHBB
, AR
, Updater
);
1499 // Now insert a new sibling loop.
1500 auto *NewSibling
= AR
.LI
.AllocateLoop();
1501 ParentL
->addChildLoop(NewSibling
);
1503 BasicBlock::Create(Context
, "loop.0.3.ph", &F
, &Loop0LatchBB
);
1505 BasicBlock::Create(Context
, "loop.0.3", &F
, &Loop0LatchBB
);
1506 BranchInst::Create(NewLoop03BB
, NewLoop03PHBB
);
1508 new LoadInst(Type::getInt1Ty(Context
), &Ptr
, "cond.0.3",
1509 /*isVolatile*/ true, NewLoop03BB
);
1510 BranchInst::Create(&Loop0LatchBB
, NewLoop03BB
, Cond
, NewLoop03BB
);
1511 Loop02PHBB
.getTerminator()->replaceUsesOfWith(&Loop0LatchBB
,
1513 AR
.DT
.addNewBlock(NewLoop03PHBB
, &Loop02PHBB
);
1514 AR
.DT
.addNewBlock(NewLoop03BB
, NewLoop03PHBB
);
1515 AR
.DT
.changeImmediateDominator(AR
.DT
[&Loop0LatchBB
],
1516 AR
.DT
[NewLoop03BB
]);
1517 EXPECT_TRUE(AR
.DT
.verify());
1518 ParentL
->addBasicBlockToLoop(NewLoop03PHBB
, AR
.LI
);
1519 NewSibling
->addBasicBlockToLoop(NewLoop03BB
, AR
.LI
);
1520 NewSibling
->verifyLoop();
1521 ParentL
->verifyLoop();
1522 Updater
.addSiblingLoops({NewSibling
});
1523 return PreservedAnalyses::all();
1526 // To respect our inner-to-outer traversal order, we must visit the
1527 // newly-inserted sibling of the loop we just deleted before we visit the
1528 // outer loop. When we do so, this must compute a fresh analysis result, even
1529 // though our new loop has the same pointer value as the loop we deleted.
1530 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.3"), _
, _
, _
))
1531 .WillOnce(Invoke(getLoopAnalysisResult
));
1532 EXPECT_CALL(MLAHandle
, run(HasName("loop.0.3"), _
, _
));
1533 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.3"), _
, _
, _
))
1535 .WillRepeatedly(Invoke(getLoopAnalysisResult
));
1537 EXPECT_CALL(MLPHandle
, run(HasName("loop.0"), _
, _
, _
))
1539 .WillRepeatedly(Invoke(getLoopAnalysisResult
));
1541 // In the final loop pipeline run we delete every loop, including the last
1542 // loop of the nest. We do this again in the second pass in the pipeline, and
1543 // as a consequence we never make it to three runs on any loop. We also cover
1544 // deleting multiple loops in a single pipeline, deleting the first loop and
1545 // deleting the (last) top level loop.
1546 AddLoopPipelineAndVerificationPasses();
1547 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.0"), _
, _
, _
))
1548 .WillOnce(Invoke(getLoopAnalysisResult
));
1549 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.0"), _
, _
, _
))
1551 Invoke([&](Loop
&L
, LoopAnalysisManager
&AM
,
1552 LoopStandardAnalysisResults
&AR
, LPMUpdater
&Updater
) {
1553 AR
.SE
.forgetLoop(&L
);
1554 EraseLoop(L
, Loop00PHBB
, AR
, Updater
);
1555 return PreservedAnalyses::all();
1558 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.3"), _
, _
, _
))
1559 .WillOnce(Invoke(getLoopAnalysisResult
));
1560 EXPECT_CALL(MLPHandle
, run(HasName("loop.0.3"), _
, _
, _
))
1562 Invoke([&](Loop
&L
, LoopAnalysisManager
&AM
,
1563 LoopStandardAnalysisResults
&AR
, LPMUpdater
&Updater
) {
1564 AR
.SE
.forgetLoop(&L
);
1565 EraseLoop(L
, *NewLoop03PHBB
, AR
, Updater
);
1566 return PreservedAnalyses::all();
1569 EXPECT_CALL(MLPHandle
, run(HasName("loop.0"), _
, _
, _
))
1570 .WillOnce(Invoke(getLoopAnalysisResult
));
1571 EXPECT_CALL(MLPHandle
, run(HasName("loop.0"), _
, _
, _
))
1573 Invoke([&](Loop
&L
, LoopAnalysisManager
&AM
,
1574 LoopStandardAnalysisResults
&AR
, LPMUpdater
&Updater
) {
1575 AR
.SE
.forgetLoop(&L
);
1576 EraseLoop(L
, EntryBB
, AR
, Updater
);
1577 return PreservedAnalyses::all();
1580 // Add the function pass pipeline now that it is fully built up and run it
1581 // over the module's one function.
1582 MPM
.addPass(createModuleToFunctionPassAdaptor(std::move(FPM
)));