1 //===- llvm/unittest/Analysis/LoopPassManagerTest.cpp - LPM tests ---------===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 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" 22 23 #include "gmock/gmock.h" 24 #include "gtest/gtest.h" 25 26 using namespace llvm; 27 28 namespace { 29 30 using testing::DoDefault; 31 using testing::Return; 32 using testing::Expectation; 33 using testing::Invoke; 34 using testing::InvokeWithoutArgs; 35 using testing::_; 36 37 template <typename DerivedT, typename IRUnitT, 38 typename AnalysisManagerT = AnalysisManager<IRUnitT>, 39 typename... ExtraArgTs> 40 class MockAnalysisHandleBase { 41 public: 42 class Analysis : public AnalysisInfoMixin<Analysis> { 43 friend AnalysisInfoMixin<Analysis>; 44 friend MockAnalysisHandleBase; 45 static AnalysisKey Key; 46 47 DerivedT *Handle; 48 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!"); 52 } 53 54 public: 55 class Result { 56 friend MockAnalysisHandleBase; 57 58 DerivedT *Handle; 59 60 Result(DerivedT &Handle) : Handle(&Handle) {} 61 62 public: 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); 67 } 68 }; 69 70 Result run(IRUnitT &IR, AnalysisManagerT &AM, ExtraArgTs... ExtraArgs) { 71 return Handle->run(IR, AM, ExtraArgs...); 72 } 73 }; 74 75 Analysis getAnalysis() { return Analysis(static_cast<DerivedT &>(*this)); } 76 typename Analysis::Result getResult() { 77 return typename Analysis::Result(static_cast<DerivedT &>(*this)); 78 } 79 80 protected: 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>>(); 88 } 89 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.) 93 void setDefaults() { 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)); 99 } 100 }; 101 102 template <typename DerivedT, typename IRUnitT, typename AnalysisManagerT, 103 typename... ExtraArgTs> 104 AnalysisKey MockAnalysisHandleBase<DerivedT, IRUnitT, AnalysisManagerT, 105 ExtraArgTs...>::Analysis::Key; 106 107 /// Mock handle for loop analyses. 108 /// 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. 114 /// For example: 115 /// 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, 123 LoopAnalysisManager, 124 LoopStandardAnalysisResults &> { 125 typedef typename MockLoopAnalysisHandleTemplate::Analysis Analysis; 126 127 MOCK_METHOD3_T(run, typename Analysis::Result(Loop &, LoopAnalysisManager &, 128 LoopStandardAnalysisResults &)); 129 130 MOCK_METHOD3_T(invalidate, bool(Loop &, const PreservedAnalyses &, 131 LoopAnalysisManager::Invalidator &)); 132 133 MockLoopAnalysisHandleTemplate() { this->setDefaults(); } 134 }; 135 136 typedef MockLoopAnalysisHandleTemplate<> MockLoopAnalysisHandle; 137 138 struct MockFunctionAnalysisHandle 139 : MockAnalysisHandleBase<MockFunctionAnalysisHandle, Function> { 140 MOCK_METHOD2(run, Analysis::Result(Function &, FunctionAnalysisManager &)); 141 142 MOCK_METHOD3(invalidate, bool(Function &, const PreservedAnalyses &, 143 FunctionAnalysisManager::Invalidator &)); 144 145 MockFunctionAnalysisHandle() { setDefaults(); } 146 }; 147 148 template <typename DerivedT, typename IRUnitT, 149 typename AnalysisManagerT = AnalysisManager<IRUnitT>, 150 typename... ExtraArgTs> 151 class MockPassHandleBase { 152 public: 153 class Pass : public PassInfoMixin<Pass> { 154 friend MockPassHandleBase; 155 156 DerivedT *Handle; 157 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!"); 161 } 162 163 public: 164 PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM, 165 ExtraArgTs... ExtraArgs) { 166 return Handle->run(IR, AM, ExtraArgs...); 167 } 168 }; 169 170 Pass getPass() { return Pass(static_cast<DerivedT &>(*this)); } 171 172 protected: 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.) 176 void setDefaults() { 177 ON_CALL(static_cast<DerivedT &>(*this), 178 run(_, _, testing::Matcher<ExtraArgTs>(_)...)) 179 .WillByDefault(Return(PreservedAnalyses::all())); 180 } 181 }; 182 183 struct MockLoopPassHandle 184 : MockPassHandleBase<MockLoopPassHandle, Loop, LoopAnalysisManager, 185 LoopStandardAnalysisResults &, LPMUpdater &> { 186 MOCK_METHOD4(run, 187 PreservedAnalyses(Loop &, LoopAnalysisManager &, 188 LoopStandardAnalysisResults &, LPMUpdater &)); 189 MockLoopPassHandle() { setDefaults(); } 190 }; 191 192 struct MockFunctionPassHandle 193 : MockPassHandleBase<MockFunctionPassHandle, Function> { 194 MOCK_METHOD2(run, PreservedAnalyses(Function &, FunctionAnalysisManager &)); 195 196 MockFunctionPassHandle() { setDefaults(); } 197 }; 198 199 struct MockModulePassHandle : MockPassHandleBase<MockModulePassHandle, Module> { 200 MOCK_METHOD2(run, PreservedAnalyses(Module &, ModuleAnalysisManager &)); 201 202 MockModulePassHandle() { setDefaults(); } 203 }; 204 205 /// Define a custom matcher for objects which support a 'getName' method 206 /// returning a StringRef. 207 /// 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. 211 /// 212 /// It should be used as: 213 /// 214 /// HasName("my_function") 215 /// 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(); 223 } 224 225 std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) { 226 SMDiagnostic Err; 227 return parseAssemblyString(IR, Err, C); 228 } 229 230 class LoopPassManagerTest : public ::testing::Test { 231 protected: 232 LLVMContext Context; 233 std::unique_ptr<Module> M; 234 235 LoopAnalysisManager LAM; 236 FunctionAnalysisManager FAM; 237 ModuleAnalysisManager MAM; 238 239 MockLoopAnalysisHandle MLAHandle; 240 MockLoopPassHandle MLPHandle; 241 MockFunctionPassHandle MFPHandle; 242 MockModulePassHandle MMPHandle; 243 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(); 249 }; 250 251 public: 252 LoopPassManagerTest() 253 : M(parseIR(Context, 254 "define void @f(i1* %ptr) {\n" 255 "entry:\n" 256 " br label %loop.0\n" 257 "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" 260 "loop.0.0.ph:\n" 261 " br label %loop.0.0\n" 262 "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" 265 "loop.0.1.ph:\n" 266 " br label %loop.0.1\n" 267 "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" 270 "loop.0.latch:\n" 271 " br label %loop.0\n" 272 "end:\n" 273 " ret void\n" 274 "}\n" 275 "\n" 276 "define void @g(i1* %ptr) {\n" 277 "entry:\n" 278 " br label %loop.g.0\n" 279 "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" 282 "end:\n" 283 " ret void\n" 284 "}\n")), 285 LAM(true), FAM(true), MAM(true) { 286 // Register our mock analysis. 287 LAM.registerPass([&] { return MLAHandle.getAnalysis(); }); 288 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 293 // those. 294 FAM.registerPass([&] { return AAManager(); }); 295 FAM.registerPass([&] { return AssumptionAnalysis(); }); 296 FAM.registerPass([&] { return MemorySSAAnalysis(); }); 297 FAM.registerPass([&] { return ScalarEvolutionAnalysis(); }); 298 FAM.registerPass([&] { return TargetLibraryAnalysis(); }); 299 FAM.registerPass([&] { return TargetIRAnalysis(); }); 300 301 // Register required pass instrumentation analysis. 302 LAM.registerPass([&] { return PassInstrumentationAnalysis(); }); 303 FAM.registerPass([&] { return PassInstrumentationAnalysis(); }); 304 MAM.registerPass([&] { return PassInstrumentationAnalysis(); }); 305 306 // Cross-register proxies. 307 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); }); 308 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); }); 309 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); }); 310 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); }); 311 } 312 }; 313 314 TEST_F(LoopPassManagerTest, Basic) { 315 ModulePassManager MPM(true); 316 ::testing::InSequence MakeExpectationsSequenced; 317 318 // First we just visit all the loops in all the functions and get their 319 // analysis results. This will run the analysis a total of four times, 320 // once for each loop. 321 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _)) 322 .WillOnce(Invoke(getLoopAnalysisResult)); 323 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _)); 324 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _)) 325 .WillOnce(Invoke(getLoopAnalysisResult)); 326 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _)); 327 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _)) 328 .WillOnce(Invoke(getLoopAnalysisResult)); 329 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _)); 330 EXPECT_CALL(MLPHandle, run(HasName("loop.g.0"), _, _, _)) 331 .WillOnce(Invoke(getLoopAnalysisResult)); 332 EXPECT_CALL(MLAHandle, run(HasName("loop.g.0"), _, _)); 333 // Wire the loop pass through pass managers into the module pipeline. 334 { 335 LoopPassManager LPM(true); 336 LPM.addPass(MLPHandle.getPass()); 337 FunctionPassManager FPM(true); 338 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM))); 339 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); 340 } 341 342 // Next we run two passes over the loops. The first one invalidates the 343 // analyses for one loop, the second ones try to get the analysis results. 344 // This should force only one analysis to re-run within the loop PM, but will 345 // also invalidate everything after the loop pass manager finishes. 346 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _)) 347 .WillOnce(DoDefault()) 348 .WillOnce(Invoke(getLoopAnalysisResult)); 349 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _)) 350 .WillOnce(InvokeWithoutArgs([] { return PreservedAnalyses::none(); })) 351 .WillOnce(Invoke(getLoopAnalysisResult)); 352 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _)); 353 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _)) 354 .WillOnce(DoDefault()) 355 .WillOnce(Invoke(getLoopAnalysisResult)); 356 EXPECT_CALL(MLPHandle, run(HasName("loop.g.0"), _, _, _)) 357 .WillOnce(DoDefault()) 358 .WillOnce(Invoke(getLoopAnalysisResult)); 359 // Wire two loop pass runs into the module pipeline. 360 { 361 LoopPassManager LPM(true); 362 LPM.addPass(MLPHandle.getPass()); 363 LPM.addPass(MLPHandle.getPass()); 364 FunctionPassManager FPM(true); 365 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM))); 366 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); 367 } 368 369 // And now run the pipeline across the module. 370 MPM.run(*M, MAM); 371 } 372 373 TEST_F(LoopPassManagerTest, FunctionPassInvalidationOfLoopAnalyses) { 374 ModulePassManager MPM(true); 375 FunctionPassManager FPM(true); 376 // We process each function completely in sequence. 377 ::testing::Sequence FSequence, GSequence; 378 379 // First, force the analysis result to be computed for each loop. 380 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _)) 381 .InSequence(FSequence) 382 .WillOnce(DoDefault()); 383 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _)) 384 .InSequence(FSequence) 385 .WillOnce(DoDefault()); 386 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _)) 387 .InSequence(FSequence) 388 .WillOnce(DoDefault()); 389 EXPECT_CALL(MLAHandle, run(HasName("loop.g.0"), _, _)) 390 .InSequence(GSequence) 391 .WillOnce(DoDefault()); 392 FPM.addPass(createFunctionToLoopPassAdaptor( 393 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>())); 394 395 // No need to re-run if we require again from a fresh loop pass manager. 396 FPM.addPass(createFunctionToLoopPassAdaptor( 397 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>())); 398 // For 'f', preserve most things but not the specific loop analyses. 399 auto PA = getLoopPassPreservedAnalyses(); 400 if (EnableMSSALoopDependency) 401 PA.preserve<MemorySSAAnalysis>(); 402 EXPECT_CALL(MFPHandle, run(HasName("f"), _)) 403 .InSequence(FSequence) 404 .WillOnce(Return(PA)); 405 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.0.0"), _, _)) 406 .InSequence(FSequence) 407 .WillOnce(DoDefault()); 408 // On one loop, skip the invalidation (as though we did an internal update). 409 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.0.1"), _, _)) 410 .InSequence(FSequence) 411 .WillOnce(Return(false)); 412 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.0"), _, _)) 413 .InSequence(FSequence) 414 .WillOnce(DoDefault()); 415 // Now two loops still have to be recomputed. 416 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _)) 417 .InSequence(FSequence) 418 .WillOnce(DoDefault()); 419 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _)) 420 .InSequence(FSequence) 421 .WillOnce(DoDefault()); 422 // Preserve things in the second function to ensure invalidation remains 423 // isolated to one function. 424 EXPECT_CALL(MFPHandle, run(HasName("g"), _)) 425 .InSequence(GSequence) 426 .WillOnce(DoDefault()); 427 FPM.addPass(MFPHandle.getPass()); 428 FPM.addPass(createFunctionToLoopPassAdaptor( 429 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>())); 430 431 EXPECT_CALL(MFPHandle, run(HasName("f"), _)) 432 .InSequence(FSequence) 433 .WillOnce(DoDefault()); 434 // For 'g', fail to preserve anything, causing the loops themselves to be 435 // cleared. We don't get an invalidation event here as the loop is gone, but 436 // we should still have to recompute the analysis. 437 EXPECT_CALL(MFPHandle, run(HasName("g"), _)) 438 .InSequence(GSequence) 439 .WillOnce(Return(PreservedAnalyses::none())); 440 EXPECT_CALL(MLAHandle, run(HasName("loop.g.0"), _, _)) 441 .InSequence(GSequence) 442 .WillOnce(DoDefault()); 443 FPM.addPass(MFPHandle.getPass()); 444 FPM.addPass(createFunctionToLoopPassAdaptor( 445 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>())); 446 447 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); 448 449 // Verify with a separate function pass run that we didn't mess up 'f's 450 // cache. No analysis runs should be necessary here. 451 MPM.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor( 452 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>()))); 453 454 MPM.run(*M, MAM); 455 } 456 457 TEST_F(LoopPassManagerTest, ModulePassInvalidationOfLoopAnalyses) { 458 ModulePassManager MPM(true); 459 ::testing::InSequence MakeExpectationsSequenced; 460 461 // First, force the analysis result to be computed for each loop. 462 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _)); 463 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _)); 464 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _)); 465 EXPECT_CALL(MLAHandle, run(HasName("loop.g.0"), _, _)); 466 MPM.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor( 467 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>()))); 468 469 // Walking all the way out and all the way back in doesn't re-run the 470 // analysis. 471 MPM.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor( 472 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>()))); 473 474 // But a module pass that doesn't preserve the actual mock loop analysis 475 // invalidates all the way down and forces recomputing. 476 EXPECT_CALL(MMPHandle, run(_, _)).WillOnce(InvokeWithoutArgs([] { 477 auto PA = getLoopPassPreservedAnalyses(); 478 PA.preserve<FunctionAnalysisManagerModuleProxy>(); 479 if (EnableMSSALoopDependency) 480 PA.preserve<MemorySSAAnalysis>(); 481 return PA; 482 })); 483 // All the loop analyses from both functions get invalidated before we 484 // recompute anything. 485 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.0.0"), _, _)); 486 // On one loop, again skip the invalidation (as though we did an internal 487 // update). 488 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.0.1"), _, _)) 489 .WillOnce(Return(false)); 490 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.0"), _, _)); 491 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.g.0"), _, _)); 492 // Now all but one of the loops gets re-analyzed. 493 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _)); 494 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _)); 495 EXPECT_CALL(MLAHandle, run(HasName("loop.g.0"), _, _)); 496 MPM.addPass(MMPHandle.getPass()); 497 MPM.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor( 498 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>()))); 499 500 // Verify that the cached values persist. 501 MPM.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor( 502 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>()))); 503 504 // Now we fail to preserve the loop analysis and observe that the loop 505 // analyses are cleared (so no invalidation event) as the loops themselves 506 // are no longer valid. 507 EXPECT_CALL(MMPHandle, run(_, _)).WillOnce(InvokeWithoutArgs([] { 508 auto PA = PreservedAnalyses::none(); 509 PA.preserve<FunctionAnalysisManagerModuleProxy>(); 510 return PA; 511 })); 512 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _)); 513 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _)); 514 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _)); 515 EXPECT_CALL(MLAHandle, run(HasName("loop.g.0"), _, _)); 516 MPM.addPass(MMPHandle.getPass()); 517 MPM.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor( 518 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>()))); 519 520 // Verify that the cached values persist. 521 MPM.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor( 522 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>()))); 523 524 // Next, check that even if we preserve everything within the function itelf, 525 // if the function's module pass proxy isn't preserved and the potential set 526 // of functions changes, the clear reaches the loop analyses as well. This 527 // will again trigger re-runs but not invalidation events. 528 EXPECT_CALL(MMPHandle, run(_, _)).WillOnce(InvokeWithoutArgs([] { 529 auto PA = PreservedAnalyses::none(); 530 PA.preserveSet<AllAnalysesOn<Function>>(); 531 PA.preserveSet<AllAnalysesOn<Loop>>(); 532 return PA; 533 })); 534 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _)); 535 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _)); 536 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _)); 537 EXPECT_CALL(MLAHandle, run(HasName("loop.g.0"), _, _)); 538 MPM.addPass(MMPHandle.getPass()); 539 MPM.addPass(createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor( 540 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>()))); 541 542 MPM.run(*M, MAM); 543 } 544 545 // Test that if any of the bundled analyses provided in the LPM's signature 546 // become invalid, the analysis proxy itself becomes invalid and we clear all 547 // loop analysis results. 548 TEST_F(LoopPassManagerTest, InvalidationOfBundledAnalyses) { 549 ModulePassManager MPM(true); 550 FunctionPassManager FPM(true); 551 ::testing::InSequence MakeExpectationsSequenced; 552 553 // First, force the analysis result to be computed for each loop. 554 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _)); 555 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _)); 556 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _)); 557 FPM.addPass(createFunctionToLoopPassAdaptor( 558 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>())); 559 560 // No need to re-run if we require again from a fresh loop pass manager. 561 FPM.addPass(createFunctionToLoopPassAdaptor( 562 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>())); 563 564 // Preserving everything but the loop analyses themselves results in 565 // invalidation and running. 566 EXPECT_CALL(MFPHandle, run(HasName("f"), _)) 567 .WillOnce(Return(getLoopPassPreservedAnalyses())); 568 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _)); 569 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _)); 570 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _)); 571 FPM.addPass(MFPHandle.getPass()); 572 FPM.addPass(createFunctionToLoopPassAdaptor( 573 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>())); 574 575 // The rest don't invalidate analyses, they only trigger re-runs because we 576 // clear the cache completely. 577 EXPECT_CALL(MFPHandle, run(HasName("f"), _)).WillOnce(InvokeWithoutArgs([] { 578 auto PA = PreservedAnalyses::none(); 579 // Not preserving `AAManager`. 580 PA.preserve<DominatorTreeAnalysis>(); 581 PA.preserve<LoopAnalysis>(); 582 PA.preserve<LoopAnalysisManagerFunctionProxy>(); 583 PA.preserve<ScalarEvolutionAnalysis>(); 584 return PA; 585 })); 586 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _)); 587 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _)); 588 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _)); 589 FPM.addPass(MFPHandle.getPass()); 590 FPM.addPass(createFunctionToLoopPassAdaptor( 591 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>())); 592 593 EXPECT_CALL(MFPHandle, run(HasName("f"), _)).WillOnce(InvokeWithoutArgs([] { 594 auto PA = PreservedAnalyses::none(); 595 PA.preserve<AAManager>(); 596 // Not preserving `DominatorTreeAnalysis`. 597 PA.preserve<LoopAnalysis>(); 598 PA.preserve<LoopAnalysisManagerFunctionProxy>(); 599 PA.preserve<ScalarEvolutionAnalysis>(); 600 return PA; 601 })); 602 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _)); 603 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _)); 604 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _)); 605 FPM.addPass(MFPHandle.getPass()); 606 FPM.addPass(createFunctionToLoopPassAdaptor( 607 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>())); 608 609 EXPECT_CALL(MFPHandle, run(HasName("f"), _)).WillOnce(InvokeWithoutArgs([] { 610 auto PA = PreservedAnalyses::none(); 611 PA.preserve<AAManager>(); 612 PA.preserve<DominatorTreeAnalysis>(); 613 // Not preserving the `LoopAnalysis`. 614 PA.preserve<LoopAnalysisManagerFunctionProxy>(); 615 PA.preserve<ScalarEvolutionAnalysis>(); 616 return PA; 617 })); 618 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _)); 619 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _)); 620 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _)); 621 FPM.addPass(MFPHandle.getPass()); 622 FPM.addPass(createFunctionToLoopPassAdaptor( 623 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>())); 624 625 EXPECT_CALL(MFPHandle, run(HasName("f"), _)).WillOnce(InvokeWithoutArgs([] { 626 auto PA = PreservedAnalyses::none(); 627 PA.preserve<AAManager>(); 628 PA.preserve<DominatorTreeAnalysis>(); 629 PA.preserve<LoopAnalysis>(); 630 // Not preserving the `LoopAnalysisManagerFunctionProxy`. 631 PA.preserve<ScalarEvolutionAnalysis>(); 632 return PA; 633 })); 634 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _)); 635 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _)); 636 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _)); 637 FPM.addPass(MFPHandle.getPass()); 638 FPM.addPass(createFunctionToLoopPassAdaptor( 639 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>())); 640 641 EXPECT_CALL(MFPHandle, run(HasName("f"), _)).WillOnce(InvokeWithoutArgs([] { 642 auto PA = PreservedAnalyses::none(); 643 PA.preserve<AAManager>(); 644 PA.preserve<DominatorTreeAnalysis>(); 645 PA.preserve<LoopAnalysis>(); 646 PA.preserve<LoopAnalysisManagerFunctionProxy>(); 647 // Not preserving `ScalarEvolutionAnalysis`. 648 return PA; 649 })); 650 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _)); 651 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _)); 652 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _)); 653 FPM.addPass(MFPHandle.getPass()); 654 FPM.addPass(createFunctionToLoopPassAdaptor( 655 RequireAnalysisLoopPass<MockLoopAnalysisHandle::Analysis>())); 656 657 // After all the churn on 'f', we'll compute the loop analysis results for 658 // 'g' once with a requires pass and then run our mock pass over g a bunch 659 // but just get cached results each time. 660 EXPECT_CALL(MLAHandle, run(HasName("loop.g.0"), _, _)); 661 EXPECT_CALL(MFPHandle, run(HasName("g"), _)).Times(6); 662 663 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); 664 MPM.run(*M, MAM); 665 } 666 667 TEST_F(LoopPassManagerTest, IndirectInvalidation) { 668 // We need two distinct analysis types and handles. 669 enum { A, B }; 670 MockLoopAnalysisHandleTemplate<A> MLAHandleA; 671 MockLoopAnalysisHandleTemplate<B> MLAHandleB; 672 LAM.registerPass([&] { return MLAHandleA.getAnalysis(); }); 673 LAM.registerPass([&] { return MLAHandleB.getAnalysis(); }); 674 typedef decltype(MLAHandleA)::Analysis AnalysisA; 675 typedef decltype(MLAHandleB)::Analysis AnalysisB; 676 677 // Set up AnalysisA to depend on our AnalysisB. For testing purposes we just 678 // need to get the AnalysisB results in AnalysisA's run method and check if 679 // AnalysisB gets invalidated in AnalysisA's invalidate method. 680 ON_CALL(MLAHandleA, run(_, _, _)) 681 .WillByDefault(Invoke([&](Loop &L, LoopAnalysisManager &AM, 682 LoopStandardAnalysisResults &AR) { 683 (void)AM.getResult<AnalysisB>(L, AR); 684 return MLAHandleA.getResult(); 685 })); 686 ON_CALL(MLAHandleA, invalidate(_, _, _)) 687 .WillByDefault(Invoke([](Loop &L, const PreservedAnalyses &PA, 688 LoopAnalysisManager::Invalidator &Inv) { 689 auto PAC = PA.getChecker<AnalysisA>(); 690 return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Loop>>()) || 691 Inv.invalidate<AnalysisB>(L, PA); 692 })); 693 694 ::testing::InSequence MakeExpectationsSequenced; 695 696 // Compute the analyses across all of 'f' first. 697 EXPECT_CALL(MLAHandleA, run(HasName("loop.0.0"), _, _)); 698 EXPECT_CALL(MLAHandleB, run(HasName("loop.0.0"), _, _)); 699 EXPECT_CALL(MLAHandleA, run(HasName("loop.0.1"), _, _)); 700 EXPECT_CALL(MLAHandleB, run(HasName("loop.0.1"), _, _)); 701 EXPECT_CALL(MLAHandleA, run(HasName("loop.0"), _, _)); 702 EXPECT_CALL(MLAHandleB, run(HasName("loop.0"), _, _)); 703 704 // Now we invalidate AnalysisB (but not AnalysisA) for one of the loops and 705 // preserve everything for the rest. This in turn triggers that one loop to 706 // recompute both AnalysisB *and* AnalysisA if indirect invalidation is 707 // working. 708 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _)) 709 .WillOnce(InvokeWithoutArgs([] { 710 auto PA = getLoopPassPreservedAnalyses(); 711 // Specifically preserve AnalysisA so that it would survive if it 712 // didn't depend on AnalysisB. 713 PA.preserve<AnalysisA>(); 714 return PA; 715 })); 716 // It happens that AnalysisB is invalidated first. That shouldn't matter 717 // though, and we should still call AnalysisA's invalidation. 718 EXPECT_CALL(MLAHandleB, invalidate(HasName("loop.0.0"), _, _)); 719 EXPECT_CALL(MLAHandleA, invalidate(HasName("loop.0.0"), _, _)); 720 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _)) 721 .WillOnce(Invoke([](Loop &L, LoopAnalysisManager &AM, 722 LoopStandardAnalysisResults &AR, LPMUpdater &) { 723 (void)AM.getResult<AnalysisA>(L, AR); 724 return PreservedAnalyses::all(); 725 })); 726 EXPECT_CALL(MLAHandleA, run(HasName("loop.0.0"), _, _)); 727 EXPECT_CALL(MLAHandleB, run(HasName("loop.0.0"), _, _)); 728 // The rest of the loops should run and get cached results. 729 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _)) 730 .Times(2) 731 .WillRepeatedly(Invoke([](Loop &L, LoopAnalysisManager &AM, 732 LoopStandardAnalysisResults &AR, LPMUpdater &) { 733 (void)AM.getResult<AnalysisA>(L, AR); 734 return PreservedAnalyses::all(); 735 })); 736 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _)) 737 .Times(2) 738 .WillRepeatedly(Invoke([](Loop &L, LoopAnalysisManager &AM, 739 LoopStandardAnalysisResults &AR, LPMUpdater &) { 740 (void)AM.getResult<AnalysisA>(L, AR); 741 return PreservedAnalyses::all(); 742 })); 743 744 // The run over 'g' should be boring, with us just computing the analyses once 745 // up front and then running loop passes and getting cached results. 746 EXPECT_CALL(MLAHandleA, run(HasName("loop.g.0"), _, _)); 747 EXPECT_CALL(MLAHandleB, run(HasName("loop.g.0"), _, _)); 748 EXPECT_CALL(MLPHandle, run(HasName("loop.g.0"), _, _, _)) 749 .Times(2) 750 .WillRepeatedly(Invoke([](Loop &L, LoopAnalysisManager &AM, 751 LoopStandardAnalysisResults &AR, LPMUpdater &) { 752 (void)AM.getResult<AnalysisA>(L, AR); 753 return PreservedAnalyses::all(); 754 })); 755 756 // Build the pipeline and run it. 757 ModulePassManager MPM(true); 758 FunctionPassManager FPM(true); 759 FPM.addPass( 760 createFunctionToLoopPassAdaptor(RequireAnalysisLoopPass<AnalysisA>())); 761 LoopPassManager LPM(true); 762 LPM.addPass(MLPHandle.getPass()); 763 LPM.addPass(MLPHandle.getPass()); 764 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM))); 765 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); 766 MPM.run(*M, MAM); 767 } 768 769 TEST_F(LoopPassManagerTest, IndirectOuterPassInvalidation) { 770 typedef decltype(MLAHandle)::Analysis LoopAnalysis; 771 772 MockFunctionAnalysisHandle MFAHandle; 773 FAM.registerPass([&] { return MFAHandle.getAnalysis(); }); 774 typedef decltype(MFAHandle)::Analysis FunctionAnalysis; 775 776 // Set up the loop analysis to depend on both the function and module 777 // analysis. 778 ON_CALL(MLAHandle, run(_, _, _)) 779 .WillByDefault(Invoke([&](Loop &L, LoopAnalysisManager &AM, 780 LoopStandardAnalysisResults &AR) { 781 auto &FAMP = AM.getResult<FunctionAnalysisManagerLoopProxy>(L, AR); 782 Function &F = *L.getHeader()->getParent(); 783 // This call will assert when trying to get the actual analysis if the 784 // FunctionAnalysis can be invalidated. Only check its existence. 785 // Alternatively, use FAM above, for the purposes of this unittest. 786 if (FAMP.cachedResultExists<FunctionAnalysis>(F)) 787 FAMP.registerOuterAnalysisInvalidation<FunctionAnalysis, 788 LoopAnalysis>(); 789 return MLAHandle.getResult(); 790 })); 791 792 ::testing::InSequence MakeExpectationsSequenced; 793 794 // Compute the analyses across all of 'f' first. 795 EXPECT_CALL(MFPHandle, run(HasName("f"), _)) 796 .WillOnce(Invoke([](Function &F, FunctionAnalysisManager &AM) { 797 // Force the computing of the function analysis so it is available in 798 // this function. 799 (void)AM.getResult<FunctionAnalysis>(F); 800 return PreservedAnalyses::all(); 801 })); 802 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _)); 803 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _)); 804 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _)); 805 806 // Now invalidate the function analysis but preserve the loop analyses. 807 // This should trigger immediate invalidation of the loop analyses, despite 808 // the fact that they were preserved. 809 EXPECT_CALL(MFPHandle, run(HasName("f"), _)).WillOnce(InvokeWithoutArgs([] { 810 auto PA = getLoopPassPreservedAnalyses(); 811 if (EnableMSSALoopDependency) 812 PA.preserve<MemorySSAAnalysis>(); 813 PA.preserveSet<AllAnalysesOn<Loop>>(); 814 return PA; 815 })); 816 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.0.0"), _, _)); 817 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.0.1"), _, _)); 818 EXPECT_CALL(MLAHandle, invalidate(HasName("loop.0"), _, _)); 819 820 // And re-running a requires pass recomputes them. 821 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _)); 822 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _)); 823 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _)); 824 825 // When we run over 'g' we don't populate the cache with the function 826 // analysis. 827 EXPECT_CALL(MFPHandle, run(HasName("g"), _)) 828 .WillOnce(Return(PreservedAnalyses::all())); 829 EXPECT_CALL(MLAHandle, run(HasName("loop.g.0"), _, _)); 830 831 // Which means that no extra invalidation occurs and cached values are used. 832 EXPECT_CALL(MFPHandle, run(HasName("g"), _)).WillOnce(InvokeWithoutArgs([] { 833 auto PA = getLoopPassPreservedAnalyses(); 834 if (EnableMSSALoopDependency) 835 PA.preserve<MemorySSAAnalysis>(); 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 853 TEST_F(LoopPassManagerTest, LoopChildInsertion) { 854 // Super boring module with three loops in a single loop nest. 855 M = parseIR(Context, "define void @f(i1* %ptr) {\n" 856 "entry:\n" 857 " br label %loop.0\n" 858 "loop.0:\n" 859 " %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" 863 "loop.0.0:\n" 864 " %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" 868 "loop.0.1:\n" 869 " %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" 873 "loop.0.2:\n" 874 " %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" 878 "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")); 886 Argument &Ptr = *F.arg_begin(); 887 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")); 892 BasicBlock &Loop00PHBB = *BBI++; 893 ASSERT_THAT(Loop00PHBB, HasName("loop.0.0.ph")); 894 BasicBlock &Loop00BB = *BBI++; 895 ASSERT_THAT(Loop00BB, HasName("loop.0.0")); 896 BasicBlock &Loop01PHBB = *BBI++; 897 ASSERT_THAT(Loop01PHBB, HasName("loop.0.1.ph")); 898 BasicBlock &Loop01BB = *BBI++; 899 ASSERT_THAT(Loop01BB, HasName("loop.0.1")); 900 BasicBlock &Loop02PHBB = *BBI++; 901 ASSERT_THAT(Loop02PHBB, HasName("loop.0.2.ph")); 902 BasicBlock &Loop02BB = *BBI++; 903 ASSERT_THAT(Loop02BB, HasName("loop.0.2")); 904 BasicBlock &Loop0LatchBB = *BBI++; 905 ASSERT_THAT(Loop0LatchBB, HasName("loop.0.latch")); 906 BasicBlock &EndBB = *BBI++; 907 ASSERT_THAT(EndBB, HasName("end")); 908 ASSERT_THAT(BBI, F.end()); 909 auto CreateCondBr = [&](BasicBlock *TrueBB, BasicBlock *FalseBB, 910 const char *Name, BasicBlock *BB) { 911 auto *Cond = new LoadInst(Type::getInt1Ty(Context), &Ptr, Name, 912 /*isVolatile*/ true, BB); 913 BranchInst::Create(TrueBB, FalseBB, Cond, BB); 914 }; 915 916 // Build the pass managers and register our pipeline. We build a single loop 917 // pass pipeline consisting of three mock pass runs over each loop. After 918 // this we run both domtree and loop verification passes to make sure that 919 // the IR remained valid during our mutations. 920 ModulePassManager MPM(true); 921 FunctionPassManager FPM(true); 922 LoopPassManager LPM(true); 923 LPM.addPass(MLPHandle.getPass()); 924 LPM.addPass(MLPHandle.getPass()); 925 LPM.addPass(MLPHandle.getPass()); 926 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM))); 927 FPM.addPass(DominatorTreeVerifierPass()); 928 FPM.addPass(LoopVerifierPass()); 929 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); 930 931 // All the visit orders are deterministic, so we use simple fully order 932 // expectations. 933 ::testing::InSequence MakeExpectationsSequenced; 934 935 // We run loop passes three times over each of the loops. 936 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _)) 937 .WillOnce(Invoke(getLoopAnalysisResult)); 938 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _)); 939 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _)) 940 .Times(2) 941 .WillRepeatedly(Invoke(getLoopAnalysisResult)); 942 943 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _)) 944 .WillOnce(Invoke(getLoopAnalysisResult)); 945 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _)); 946 947 // When running over the middle loop, the second run inserts two new child 948 // loops, inserting them and itself into the worklist. 949 BasicBlock *NewLoop010BB, *NewLoop01LatchBB; 950 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _)) 951 .WillOnce(Invoke([&](Loop &L, LoopAnalysisManager &AM, 952 LoopStandardAnalysisResults &AR, 953 LPMUpdater &Updater) { 954 auto *NewLoop = AR.LI.AllocateLoop(); 955 L.addChildLoop(NewLoop); 956 auto *NewLoop010PHBB = 957 BasicBlock::Create(Context, "loop.0.1.0.ph", &F, &Loop02PHBB); 958 NewLoop010BB = 959 BasicBlock::Create(Context, "loop.0.1.0", &F, &Loop02PHBB); 960 NewLoop01LatchBB = 961 BasicBlock::Create(Context, "loop.0.1.latch", &F, &Loop02PHBB); 962 Loop01BB.getTerminator()->replaceUsesOfWith(&Loop01BB, NewLoop010PHBB); 963 BranchInst::Create(NewLoop010BB, NewLoop010PHBB); 964 CreateCondBr(NewLoop01LatchBB, NewLoop010BB, "cond.0.1.0", 965 NewLoop010BB); 966 BranchInst::Create(&Loop01BB, NewLoop01LatchBB); 967 AR.DT.addNewBlock(NewLoop010PHBB, &Loop01BB); 968 AR.DT.addNewBlock(NewLoop010BB, NewLoop010PHBB); 969 AR.DT.addNewBlock(NewLoop01LatchBB, NewLoop010BB); 970 EXPECT_TRUE(AR.DT.verify()); 971 L.addBasicBlockToLoop(NewLoop010PHBB, AR.LI); 972 NewLoop->addBasicBlockToLoop(NewLoop010BB, AR.LI); 973 L.addBasicBlockToLoop(NewLoop01LatchBB, AR.LI); 974 NewLoop->verifyLoop(); 975 L.verifyLoop(); 976 Updater.addChildLoops({NewLoop}); 977 return PreservedAnalyses::all(); 978 })); 979 980 // We should immediately drop down to fully visit the new inner loop. 981 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1.0"), _, _, _)) 982 .WillOnce(Invoke(getLoopAnalysisResult)); 983 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1.0"), _, _)); 984 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1.0"), _, _, _)) 985 .Times(2) 986 .WillRepeatedly(Invoke(getLoopAnalysisResult)); 987 988 // After visiting the inner loop, we should re-visit the second loop 989 // reflecting its new loop nest structure. 990 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _)) 991 .WillOnce(Invoke(getLoopAnalysisResult)); 992 993 // In the second run over the middle loop after we've visited the new child, 994 // we add another child to check that we can repeatedly add children, and add 995 // children to a loop that already has children. 996 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _)) 997 .WillOnce(Invoke([&](Loop &L, LoopAnalysisManager &AM, 998 LoopStandardAnalysisResults &AR, 999 LPMUpdater &Updater) { 1000 auto *NewLoop = AR.LI.AllocateLoop(); 1001 L.addChildLoop(NewLoop); 1002 auto *NewLoop011PHBB = BasicBlock::Create(Context, "loop.0.1.1.ph", &F, NewLoop01LatchBB); 1003 auto *NewLoop011BB = BasicBlock::Create(Context, "loop.0.1.1", &F, NewLoop01LatchBB); 1004 NewLoop010BB->getTerminator()->replaceUsesOfWith(NewLoop01LatchBB, 1005 NewLoop011PHBB); 1006 BranchInst::Create(NewLoop011BB, NewLoop011PHBB); 1007 CreateCondBr(NewLoop01LatchBB, NewLoop011BB, "cond.0.1.1", 1008 NewLoop011BB); 1009 AR.DT.addNewBlock(NewLoop011PHBB, NewLoop010BB); 1010 auto *NewDTNode = AR.DT.addNewBlock(NewLoop011BB, NewLoop011PHBB); 1011 AR.DT.changeImmediateDominator(AR.DT[NewLoop01LatchBB], NewDTNode); 1012 EXPECT_TRUE(AR.DT.verify()); 1013 L.addBasicBlockToLoop(NewLoop011PHBB, AR.LI); 1014 NewLoop->addBasicBlockToLoop(NewLoop011BB, AR.LI); 1015 NewLoop->verifyLoop(); 1016 L.verifyLoop(); 1017 Updater.addChildLoops({NewLoop}); 1018 return PreservedAnalyses::all(); 1019 })); 1020 1021 // Again, we should immediately drop down to visit the new, unvisited child 1022 // loop. We don't need to revisit the other child though. 1023 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1.1"), _, _, _)) 1024 .WillOnce(Invoke(getLoopAnalysisResult)); 1025 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1.1"), _, _)); 1026 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1.1"), _, _, _)) 1027 .Times(2) 1028 .WillRepeatedly(Invoke(getLoopAnalysisResult)); 1029 1030 // And now we should pop back up to the second loop and do a full pipeline of 1031 // three passes on its current form. 1032 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _)) 1033 .Times(3) 1034 .WillRepeatedly(Invoke(getLoopAnalysisResult)); 1035 1036 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2"), _, _, _)) 1037 .WillOnce(Invoke(getLoopAnalysisResult)); 1038 EXPECT_CALL(MLAHandle, run(HasName("loop.0.2"), _, _)); 1039 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2"), _, _, _)) 1040 .Times(2) 1041 .WillRepeatedly(Invoke(getLoopAnalysisResult)); 1042 1043 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _)) 1044 .WillOnce(Invoke(getLoopAnalysisResult)); 1045 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _)); 1046 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _)) 1047 .Times(2) 1048 .WillRepeatedly(Invoke(getLoopAnalysisResult)); 1049 1050 // Now that all the expected actions are registered, run the pipeline over 1051 // our module. All of our expectations are verified when the test finishes. 1052 MPM.run(*M, MAM); 1053 } 1054 1055 TEST_F(LoopPassManagerTest, LoopPeerInsertion) { 1056 // Super boring module with two loop nests and loop nest with two child 1057 // loops. 1058 M = parseIR(Context, "define void @f(i1* %ptr) {\n" 1059 "entry:\n" 1060 " br label %loop.0\n" 1061 "loop.0:\n" 1062 " %cond.0 = load volatile i1, i1* %ptr\n" 1063 " br i1 %cond.0, label %loop.0.0.ph, label %loop.2.ph\n" 1064 "loop.0.0.ph:\n" 1065 " br label %loop.0.0\n" 1066 "loop.0.0:\n" 1067 " %cond.0.0 = load volatile i1, i1* %ptr\n" 1068 " br i1 %cond.0.0, label %loop.0.0, label %loop.0.2.ph\n" 1069 "loop.0.2.ph:\n" 1070 " br label %loop.0.2\n" 1071 "loop.0.2:\n" 1072 " %cond.0.2 = load volatile i1, i1* %ptr\n" 1073 " br i1 %cond.0.2, label %loop.0.2, label %loop.0.latch\n" 1074 "loop.0.latch:\n" 1075 " br label %loop.0\n" 1076 "loop.2.ph:\n" 1077 " br label %loop.2\n" 1078 "loop.2:\n" 1079 " %cond.2 = load volatile i1, i1* %ptr\n" 1080 " br i1 %cond.2, label %loop.2, label %end\n" 1081 "end:\n" 1082 " ret void\n" 1083 "}\n"); 1084 1085 // Build up variables referring into the IR so we can rewrite it below 1086 // easily. 1087 Function &F = *M->begin(); 1088 ASSERT_THAT(F, HasName("f")); 1089 Argument &Ptr = *F.arg_begin(); 1090 auto BBI = F.begin(); 1091 BasicBlock &EntryBB = *BBI++; 1092 ASSERT_THAT(EntryBB, HasName("entry")); 1093 BasicBlock &Loop0BB = *BBI++; 1094 ASSERT_THAT(Loop0BB, HasName("loop.0")); 1095 BasicBlock &Loop00PHBB = *BBI++; 1096 ASSERT_THAT(Loop00PHBB, HasName("loop.0.0.ph")); 1097 BasicBlock &Loop00BB = *BBI++; 1098 ASSERT_THAT(Loop00BB, HasName("loop.0.0")); 1099 BasicBlock &Loop02PHBB = *BBI++; 1100 ASSERT_THAT(Loop02PHBB, HasName("loop.0.2.ph")); 1101 BasicBlock &Loop02BB = *BBI++; 1102 ASSERT_THAT(Loop02BB, HasName("loop.0.2")); 1103 BasicBlock &Loop0LatchBB = *BBI++; 1104 ASSERT_THAT(Loop0LatchBB, HasName("loop.0.latch")); 1105 BasicBlock &Loop2PHBB = *BBI++; 1106 ASSERT_THAT(Loop2PHBB, HasName("loop.2.ph")); 1107 BasicBlock &Loop2BB = *BBI++; 1108 ASSERT_THAT(Loop2BB, HasName("loop.2")); 1109 BasicBlock &EndBB = *BBI++; 1110 ASSERT_THAT(EndBB, HasName("end")); 1111 ASSERT_THAT(BBI, F.end()); 1112 auto CreateCondBr = [&](BasicBlock *TrueBB, BasicBlock *FalseBB, 1113 const char *Name, BasicBlock *BB) { 1114 auto *Cond = new LoadInst(Type::getInt1Ty(Context), &Ptr, Name, 1115 /*isVolatile*/ true, BB); 1116 BranchInst::Create(TrueBB, FalseBB, Cond, BB); 1117 }; 1118 1119 // Build the pass managers and register our pipeline. We build a single loop 1120 // pass pipeline consisting of three mock pass runs over each loop. After 1121 // this we run both domtree and loop verification passes to make sure that 1122 // the IR remained valid during our mutations. 1123 ModulePassManager MPM(true); 1124 FunctionPassManager FPM(true); 1125 LoopPassManager LPM(true); 1126 LPM.addPass(MLPHandle.getPass()); 1127 LPM.addPass(MLPHandle.getPass()); 1128 LPM.addPass(MLPHandle.getPass()); 1129 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM))); 1130 FPM.addPass(DominatorTreeVerifierPass()); 1131 FPM.addPass(LoopVerifierPass()); 1132 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); 1133 1134 // All the visit orders are deterministic, so we use simple fully order 1135 // expectations. 1136 ::testing::InSequence MakeExpectationsSequenced; 1137 1138 // We run loop passes three times over each of the loops. 1139 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _)) 1140 .WillOnce(Invoke(getLoopAnalysisResult)); 1141 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _)); 1142 1143 // On the second run, we insert a sibling loop. 1144 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _)) 1145 .WillOnce(Invoke([&](Loop &L, LoopAnalysisManager &AM, 1146 LoopStandardAnalysisResults &AR, 1147 LPMUpdater &Updater) { 1148 auto *NewLoop = AR.LI.AllocateLoop(); 1149 L.getParentLoop()->addChildLoop(NewLoop); 1150 auto *NewLoop01PHBB = BasicBlock::Create(Context, "loop.0.1.ph", &F, &Loop02PHBB); 1151 auto *NewLoop01BB = BasicBlock::Create(Context, "loop.0.1", &F, &Loop02PHBB); 1152 BranchInst::Create(NewLoop01BB, NewLoop01PHBB); 1153 CreateCondBr(&Loop02PHBB, NewLoop01BB, "cond.0.1", NewLoop01BB); 1154 Loop00BB.getTerminator()->replaceUsesOfWith(&Loop02PHBB, NewLoop01PHBB); 1155 AR.DT.addNewBlock(NewLoop01PHBB, &Loop00BB); 1156 auto *NewDTNode = AR.DT.addNewBlock(NewLoop01BB, NewLoop01PHBB); 1157 AR.DT.changeImmediateDominator(AR.DT[&Loop02PHBB], NewDTNode); 1158 EXPECT_TRUE(AR.DT.verify()); 1159 L.getParentLoop()->addBasicBlockToLoop(NewLoop01PHBB, AR.LI); 1160 NewLoop->addBasicBlockToLoop(NewLoop01BB, AR.LI); 1161 L.getParentLoop()->verifyLoop(); 1162 Updater.addSiblingLoops({NewLoop}); 1163 return PreservedAnalyses::all(); 1164 })); 1165 // We finish processing this loop as sibling loops don't perturb the 1166 // postorder walk. 1167 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _)) 1168 .WillOnce(Invoke(getLoopAnalysisResult)); 1169 1170 // We visit the inserted sibling next. 1171 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _)) 1172 .WillOnce(Invoke(getLoopAnalysisResult)); 1173 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _)); 1174 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _)) 1175 .Times(2) 1176 .WillRepeatedly(Invoke(getLoopAnalysisResult)); 1177 1178 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2"), _, _, _)) 1179 .WillOnce(Invoke(getLoopAnalysisResult)); 1180 EXPECT_CALL(MLAHandle, run(HasName("loop.0.2"), _, _)); 1181 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2"), _, _, _)) 1182 .WillOnce(Invoke(getLoopAnalysisResult)); 1183 // Next, on the third pass run on the last inner loop we add more new 1184 // siblings, more than one, and one with nested child loops. By doing this at 1185 // the end we make sure that edge case works well. 1186 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2"), _, _, _)) 1187 .WillOnce(Invoke([&](Loop &L, LoopAnalysisManager &AM, 1188 LoopStandardAnalysisResults &AR, 1189 LPMUpdater &Updater) { 1190 Loop *NewLoops[] = {AR.LI.AllocateLoop(), AR.LI.AllocateLoop(), 1191 AR.LI.AllocateLoop()}; 1192 L.getParentLoop()->addChildLoop(NewLoops[0]); 1193 L.getParentLoop()->addChildLoop(NewLoops[1]); 1194 NewLoops[1]->addChildLoop(NewLoops[2]); 1195 auto *NewLoop03PHBB = 1196 BasicBlock::Create(Context, "loop.0.3.ph", &F, &Loop0LatchBB); 1197 auto *NewLoop03BB = 1198 BasicBlock::Create(Context, "loop.0.3", &F, &Loop0LatchBB); 1199 auto *NewLoop04PHBB = 1200 BasicBlock::Create(Context, "loop.0.4.ph", &F, &Loop0LatchBB); 1201 auto *NewLoop04BB = 1202 BasicBlock::Create(Context, "loop.0.4", &F, &Loop0LatchBB); 1203 auto *NewLoop040PHBB = 1204 BasicBlock::Create(Context, "loop.0.4.0.ph", &F, &Loop0LatchBB); 1205 auto *NewLoop040BB = 1206 BasicBlock::Create(Context, "loop.0.4.0", &F, &Loop0LatchBB); 1207 auto *NewLoop04LatchBB = 1208 BasicBlock::Create(Context, "loop.0.4.latch", &F, &Loop0LatchBB); 1209 Loop02BB.getTerminator()->replaceUsesOfWith(&Loop0LatchBB, NewLoop03PHBB); 1210 BranchInst::Create(NewLoop03BB, NewLoop03PHBB); 1211 CreateCondBr(NewLoop04PHBB, NewLoop03BB, "cond.0.3", NewLoop03BB); 1212 BranchInst::Create(NewLoop04BB, NewLoop04PHBB); 1213 CreateCondBr(&Loop0LatchBB, NewLoop040PHBB, "cond.0.4", NewLoop04BB); 1214 BranchInst::Create(NewLoop040BB, NewLoop040PHBB); 1215 CreateCondBr(NewLoop04LatchBB, NewLoop040BB, "cond.0.4.0", NewLoop040BB); 1216 BranchInst::Create(NewLoop04BB, NewLoop04LatchBB); 1217 AR.DT.addNewBlock(NewLoop03PHBB, &Loop02BB); 1218 AR.DT.addNewBlock(NewLoop03BB, NewLoop03PHBB); 1219 AR.DT.addNewBlock(NewLoop04PHBB, NewLoop03BB); 1220 auto *NewDTNode = AR.DT.addNewBlock(NewLoop04BB, NewLoop04PHBB); 1221 AR.DT.changeImmediateDominator(AR.DT[&Loop0LatchBB], NewDTNode); 1222 AR.DT.addNewBlock(NewLoop040PHBB, NewLoop04BB); 1223 AR.DT.addNewBlock(NewLoop040BB, NewLoop040PHBB); 1224 AR.DT.addNewBlock(NewLoop04LatchBB, NewLoop040BB); 1225 EXPECT_TRUE(AR.DT.verify()); 1226 L.getParentLoop()->addBasicBlockToLoop(NewLoop03PHBB, AR.LI); 1227 NewLoops[0]->addBasicBlockToLoop(NewLoop03BB, AR.LI); 1228 L.getParentLoop()->addBasicBlockToLoop(NewLoop04PHBB, AR.LI); 1229 NewLoops[1]->addBasicBlockToLoop(NewLoop04BB, AR.LI); 1230 NewLoops[1]->addBasicBlockToLoop(NewLoop040PHBB, AR.LI); 1231 NewLoops[2]->addBasicBlockToLoop(NewLoop040BB, AR.LI); 1232 NewLoops[1]->addBasicBlockToLoop(NewLoop04LatchBB, AR.LI); 1233 L.getParentLoop()->verifyLoop(); 1234 Updater.addSiblingLoops({NewLoops[0], NewLoops[1]}); 1235 return PreservedAnalyses::all(); 1236 })); 1237 1238 EXPECT_CALL(MLPHandle, run(HasName("loop.0.3"), _, _, _)) 1239 .WillOnce(Invoke(getLoopAnalysisResult)); 1240 EXPECT_CALL(MLAHandle, run(HasName("loop.0.3"), _, _)); 1241 EXPECT_CALL(MLPHandle, run(HasName("loop.0.3"), _, _, _)) 1242 .Times(2) 1243 .WillRepeatedly(Invoke(getLoopAnalysisResult)); 1244 1245 // Note that we need to visit the inner loop of this added sibling before the 1246 // sibling itself! 1247 EXPECT_CALL(MLPHandle, run(HasName("loop.0.4.0"), _, _, _)) 1248 .WillOnce(Invoke(getLoopAnalysisResult)); 1249 EXPECT_CALL(MLAHandle, run(HasName("loop.0.4.0"), _, _)); 1250 EXPECT_CALL(MLPHandle, run(HasName("loop.0.4.0"), _, _, _)) 1251 .Times(2) 1252 .WillRepeatedly(Invoke(getLoopAnalysisResult)); 1253 1254 EXPECT_CALL(MLPHandle, run(HasName("loop.0.4"), _, _, _)) 1255 .WillOnce(Invoke(getLoopAnalysisResult)); 1256 EXPECT_CALL(MLAHandle, run(HasName("loop.0.4"), _, _)); 1257 EXPECT_CALL(MLPHandle, run(HasName("loop.0.4"), _, _, _)) 1258 .Times(2) 1259 .WillRepeatedly(Invoke(getLoopAnalysisResult)); 1260 1261 // And only now do we visit the outermost loop of the nest. 1262 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _)) 1263 .WillOnce(Invoke(getLoopAnalysisResult)); 1264 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _)); 1265 // On the second pass, we add sibling loops which become new top-level loops. 1266 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _)) 1267 .WillOnce(Invoke([&](Loop &L, LoopAnalysisManager &AM, 1268 LoopStandardAnalysisResults &AR, 1269 LPMUpdater &Updater) { 1270 auto *NewLoop = AR.LI.AllocateLoop(); 1271 AR.LI.addTopLevelLoop(NewLoop); 1272 auto *NewLoop1PHBB = BasicBlock::Create(Context, "loop.1.ph", &F, &Loop2BB); 1273 auto *NewLoop1BB = BasicBlock::Create(Context, "loop.1", &F, &Loop2BB); 1274 BranchInst::Create(NewLoop1BB, NewLoop1PHBB); 1275 CreateCondBr(&Loop2PHBB, NewLoop1BB, "cond.1", NewLoop1BB); 1276 Loop0BB.getTerminator()->replaceUsesOfWith(&Loop2PHBB, NewLoop1PHBB); 1277 AR.DT.addNewBlock(NewLoop1PHBB, &Loop0BB); 1278 auto *NewDTNode = AR.DT.addNewBlock(NewLoop1BB, NewLoop1PHBB); 1279 AR.DT.changeImmediateDominator(AR.DT[&Loop2PHBB], NewDTNode); 1280 EXPECT_TRUE(AR.DT.verify()); 1281 NewLoop->addBasicBlockToLoop(NewLoop1BB, AR.LI); 1282 NewLoop->verifyLoop(); 1283 Updater.addSiblingLoops({NewLoop}); 1284 return PreservedAnalyses::all(); 1285 })); 1286 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _)) 1287 .WillOnce(Invoke(getLoopAnalysisResult)); 1288 1289 EXPECT_CALL(MLPHandle, run(HasName("loop.1"), _, _, _)) 1290 .WillOnce(Invoke(getLoopAnalysisResult)); 1291 EXPECT_CALL(MLAHandle, run(HasName("loop.1"), _, _)); 1292 EXPECT_CALL(MLPHandle, run(HasName("loop.1"), _, _, _)) 1293 .Times(2) 1294 .WillRepeatedly(Invoke(getLoopAnalysisResult)); 1295 1296 EXPECT_CALL(MLPHandle, run(HasName("loop.2"), _, _, _)) 1297 .WillOnce(Invoke(getLoopAnalysisResult)); 1298 EXPECT_CALL(MLAHandle, run(HasName("loop.2"), _, _)); 1299 EXPECT_CALL(MLPHandle, run(HasName("loop.2"), _, _, _)) 1300 .Times(2) 1301 .WillRepeatedly(Invoke(getLoopAnalysisResult)); 1302 1303 // Now that all the expected actions are registered, run the pipeline over 1304 // our module. All of our expectations are verified when the test finishes. 1305 MPM.run(*M, MAM); 1306 } 1307 1308 TEST_F(LoopPassManagerTest, LoopDeletion) { 1309 // Build a module with a single loop nest that contains one outer loop with 1310 // three subloops, and one of those with its own subloop. We will 1311 // incrementally delete all of these to test different deletion scenarios. 1312 M = parseIR(Context, "define void @f(i1* %ptr) {\n" 1313 "entry:\n" 1314 " br label %loop.0\n" 1315 "loop.0:\n" 1316 " %cond.0 = load volatile i1, i1* %ptr\n" 1317 " br i1 %cond.0, label %loop.0.0.ph, label %end\n" 1318 "loop.0.0.ph:\n" 1319 " br label %loop.0.0\n" 1320 "loop.0.0:\n" 1321 " %cond.0.0 = load volatile i1, i1* %ptr\n" 1322 " br i1 %cond.0.0, label %loop.0.0, label %loop.0.1.ph\n" 1323 "loop.0.1.ph:\n" 1324 " br label %loop.0.1\n" 1325 "loop.0.1:\n" 1326 " %cond.0.1 = load volatile i1, i1* %ptr\n" 1327 " br i1 %cond.0.1, label %loop.0.1, label %loop.0.2.ph\n" 1328 "loop.0.2.ph:\n" 1329 " br label %loop.0.2\n" 1330 "loop.0.2:\n" 1331 " %cond.0.2 = load volatile i1, i1* %ptr\n" 1332 " br i1 %cond.0.2, label %loop.0.2.0.ph, label %loop.0.latch\n" 1333 "loop.0.2.0.ph:\n" 1334 " br label %loop.0.2.0\n" 1335 "loop.0.2.0:\n" 1336 " %cond.0.2.0 = load volatile i1, i1* %ptr\n" 1337 " br i1 %cond.0.2.0, label %loop.0.2.0, label %loop.0.2.latch\n" 1338 "loop.0.2.latch:\n" 1339 " br label %loop.0.2\n" 1340 "loop.0.latch:\n" 1341 " br label %loop.0\n" 1342 "end:\n" 1343 " ret void\n" 1344 "}\n"); 1345 1346 // Build up variables referring into the IR so we can rewrite it below 1347 // easily. 1348 Function &F = *M->begin(); 1349 ASSERT_THAT(F, HasName("f")); 1350 Argument &Ptr = *F.arg_begin(); 1351 auto BBI = F.begin(); 1352 BasicBlock &EntryBB = *BBI++; 1353 ASSERT_THAT(EntryBB, HasName("entry")); 1354 BasicBlock &Loop0BB = *BBI++; 1355 ASSERT_THAT(Loop0BB, HasName("loop.0")); 1356 BasicBlock &Loop00PHBB = *BBI++; 1357 ASSERT_THAT(Loop00PHBB, HasName("loop.0.0.ph")); 1358 BasicBlock &Loop00BB = *BBI++; 1359 ASSERT_THAT(Loop00BB, HasName("loop.0.0")); 1360 BasicBlock &Loop01PHBB = *BBI++; 1361 ASSERT_THAT(Loop01PHBB, HasName("loop.0.1.ph")); 1362 BasicBlock &Loop01BB = *BBI++; 1363 ASSERT_THAT(Loop01BB, HasName("loop.0.1")); 1364 BasicBlock &Loop02PHBB = *BBI++; 1365 ASSERT_THAT(Loop02PHBB, HasName("loop.0.2.ph")); 1366 BasicBlock &Loop02BB = *BBI++; 1367 ASSERT_THAT(Loop02BB, HasName("loop.0.2")); 1368 BasicBlock &Loop020PHBB = *BBI++; 1369 ASSERT_THAT(Loop020PHBB, HasName("loop.0.2.0.ph")); 1370 BasicBlock &Loop020BB = *BBI++; 1371 ASSERT_THAT(Loop020BB, HasName("loop.0.2.0")); 1372 BasicBlock &Loop02LatchBB = *BBI++; 1373 ASSERT_THAT(Loop02LatchBB, HasName("loop.0.2.latch")); 1374 BasicBlock &Loop0LatchBB = *BBI++; 1375 ASSERT_THAT(Loop0LatchBB, HasName("loop.0.latch")); 1376 BasicBlock &EndBB = *BBI++; 1377 ASSERT_THAT(EndBB, HasName("end")); 1378 ASSERT_THAT(BBI, F.end()); 1379 1380 // Helper to do the actual deletion of a loop. We directly encode this here 1381 // to isolate ourselves from the rest of LLVM and for simplicity. Here we can 1382 // egregiously cheat based on knowledge of the test case. For example, we 1383 // have no PHI nodes and there is always a single i-dom. 1384 auto EraseLoop = [](Loop &L, BasicBlock &IDomBB, 1385 LoopStandardAnalysisResults &AR, LPMUpdater &Updater) { 1386 assert(L.empty() && "Can only delete leaf loops with this routine!"); 1387 SmallVector<BasicBlock *, 4> LoopBBs(L.block_begin(), L.block_end()); 1388 Updater.markLoopAsDeleted(L, L.getName()); 1389 IDomBB.getTerminator()->replaceUsesOfWith(L.getHeader(), 1390 L.getUniqueExitBlock()); 1391 for (BasicBlock *LoopBB : LoopBBs) { 1392 SmallVector<DomTreeNode *, 4> ChildNodes(AR.DT[LoopBB]->begin(), 1393 AR.DT[LoopBB]->end()); 1394 for (DomTreeNode *ChildNode : ChildNodes) 1395 AR.DT.changeImmediateDominator(ChildNode, AR.DT[&IDomBB]); 1396 AR.DT.eraseNode(LoopBB); 1397 AR.LI.removeBlock(LoopBB); 1398 LoopBB->dropAllReferences(); 1399 } 1400 for (BasicBlock *LoopBB : LoopBBs) 1401 LoopBB->eraseFromParent(); 1402 1403 AR.LI.erase(&L); 1404 }; 1405 1406 // Build up the pass managers. 1407 ModulePassManager MPM(true); 1408 FunctionPassManager FPM(true); 1409 // We run several loop pass pipelines across the loop nest, but they all take 1410 // the same form of three mock pass runs in a loop pipeline followed by 1411 // domtree and loop verification. We use a lambda to stamp this out each 1412 // time. 1413 auto AddLoopPipelineAndVerificationPasses = [&] { 1414 LoopPassManager LPM(true); 1415 LPM.addPass(MLPHandle.getPass()); 1416 LPM.addPass(MLPHandle.getPass()); 1417 LPM.addPass(MLPHandle.getPass()); 1418 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM))); 1419 FPM.addPass(DominatorTreeVerifierPass()); 1420 FPM.addPass(LoopVerifierPass()); 1421 }; 1422 1423 // All the visit orders are deterministic so we use simple fully order 1424 // expectations. 1425 ::testing::InSequence MakeExpectationsSequenced; 1426 1427 // We run the loop pipeline with three passes over each of the loops. When 1428 // running over the middle loop, the second pass in the pipeline deletes it. 1429 // This should prevent the third pass from visiting it but otherwise leave 1430 // the process unimpacted. 1431 AddLoopPipelineAndVerificationPasses(); 1432 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _)) 1433 .WillOnce(Invoke(getLoopAnalysisResult)); 1434 EXPECT_CALL(MLAHandle, run(HasName("loop.0.0"), _, _)); 1435 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _)) 1436 .Times(2) 1437 .WillRepeatedly(Invoke(getLoopAnalysisResult)); 1438 1439 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _)) 1440 .WillOnce(Invoke(getLoopAnalysisResult)); 1441 EXPECT_CALL(MLAHandle, run(HasName("loop.0.1"), _, _)); 1442 EXPECT_CALL(MLPHandle, run(HasName("loop.0.1"), _, _, _)) 1443 .WillOnce( 1444 Invoke([&](Loop &L, LoopAnalysisManager &AM, 1445 LoopStandardAnalysisResults &AR, LPMUpdater &Updater) { 1446 Loop *ParentL = L.getParentLoop(); 1447 AR.SE.forgetLoop(&L); 1448 EraseLoop(L, Loop01PHBB, AR, Updater); 1449 ParentL->verifyLoop(); 1450 return PreservedAnalyses::all(); 1451 })); 1452 1453 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2.0"), _, _, _)) 1454 .WillOnce(Invoke(getLoopAnalysisResult)); 1455 EXPECT_CALL(MLAHandle, run(HasName("loop.0.2.0"), _, _)); 1456 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2.0"), _, _, _)) 1457 .Times(2) 1458 .WillRepeatedly(Invoke(getLoopAnalysisResult)); 1459 1460 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2"), _, _, _)) 1461 .WillOnce(Invoke(getLoopAnalysisResult)); 1462 EXPECT_CALL(MLAHandle, run(HasName("loop.0.2"), _, _)); 1463 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2"), _, _, _)) 1464 .Times(2) 1465 .WillRepeatedly(Invoke(getLoopAnalysisResult)); 1466 1467 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _)) 1468 .WillOnce(Invoke(getLoopAnalysisResult)); 1469 EXPECT_CALL(MLAHandle, run(HasName("loop.0"), _, _)); 1470 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _)) 1471 .Times(2) 1472 .WillRepeatedly(Invoke(getLoopAnalysisResult)); 1473 1474 // Run the loop pipeline again. This time we delete the last loop, which 1475 // contains a nested loop within it and insert a new loop into the nest. This 1476 // makes sure we can handle nested loop deletion. 1477 AddLoopPipelineAndVerificationPasses(); 1478 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _)) 1479 .Times(3) 1480 .WillRepeatedly(Invoke(getLoopAnalysisResult)); 1481 1482 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2.0"), _, _, _)) 1483 .Times(3) 1484 .WillRepeatedly(Invoke(getLoopAnalysisResult)); 1485 1486 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2"), _, _, _)) 1487 .WillOnce(Invoke(getLoopAnalysisResult)); 1488 BasicBlock *NewLoop03PHBB; 1489 EXPECT_CALL(MLPHandle, run(HasName("loop.0.2"), _, _, _)) 1490 .WillOnce( 1491 Invoke([&](Loop &L, LoopAnalysisManager &AM, 1492 LoopStandardAnalysisResults &AR, LPMUpdater &Updater) { 1493 AR.SE.forgetLoop(*L.begin()); 1494 EraseLoop(**L.begin(), Loop020PHBB, AR, Updater); 1495 1496 auto *ParentL = L.getParentLoop(); 1497 AR.SE.forgetLoop(&L); 1498 EraseLoop(L, Loop02PHBB, AR, Updater); 1499 1500 // Now insert a new sibling loop. 1501 auto *NewSibling = AR.LI.AllocateLoop(); 1502 ParentL->addChildLoop(NewSibling); 1503 NewLoop03PHBB = 1504 BasicBlock::Create(Context, "loop.0.3.ph", &F, &Loop0LatchBB); 1505 auto *NewLoop03BB = 1506 BasicBlock::Create(Context, "loop.0.3", &F, &Loop0LatchBB); 1507 BranchInst::Create(NewLoop03BB, NewLoop03PHBB); 1508 auto *Cond = 1509 new LoadInst(Type::getInt1Ty(Context), &Ptr, "cond.0.3", 1510 /*isVolatile*/ true, NewLoop03BB); 1511 BranchInst::Create(&Loop0LatchBB, NewLoop03BB, Cond, NewLoop03BB); 1512 Loop02PHBB.getTerminator()->replaceUsesOfWith(&Loop0LatchBB, 1513 NewLoop03PHBB); 1514 AR.DT.addNewBlock(NewLoop03PHBB, &Loop02PHBB); 1515 AR.DT.addNewBlock(NewLoop03BB, NewLoop03PHBB); 1516 AR.DT.changeImmediateDominator(AR.DT[&Loop0LatchBB], 1517 AR.DT[NewLoop03BB]); 1518 EXPECT_TRUE(AR.DT.verify()); 1519 ParentL->addBasicBlockToLoop(NewLoop03PHBB, AR.LI); 1520 NewSibling->addBasicBlockToLoop(NewLoop03BB, AR.LI); 1521 NewSibling->verifyLoop(); 1522 ParentL->verifyLoop(); 1523 Updater.addSiblingLoops({NewSibling}); 1524 return PreservedAnalyses::all(); 1525 })); 1526 1527 // To respect our inner-to-outer traversal order, we must visit the 1528 // newly-inserted sibling of the loop we just deleted before we visit the 1529 // outer loop. When we do so, this must compute a fresh analysis result, even 1530 // though our new loop has the same pointer value as the loop we deleted. 1531 EXPECT_CALL(MLPHandle, run(HasName("loop.0.3"), _, _, _)) 1532 .WillOnce(Invoke(getLoopAnalysisResult)); 1533 EXPECT_CALL(MLAHandle, run(HasName("loop.0.3"), _, _)); 1534 EXPECT_CALL(MLPHandle, run(HasName("loop.0.3"), _, _, _)) 1535 .Times(2) 1536 .WillRepeatedly(Invoke(getLoopAnalysisResult)); 1537 1538 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _)) 1539 .Times(3) 1540 .WillRepeatedly(Invoke(getLoopAnalysisResult)); 1541 1542 // In the final loop pipeline run we delete every loop, including the last 1543 // loop of the nest. We do this again in the second pass in the pipeline, and 1544 // as a consequence we never make it to three runs on any loop. We also cover 1545 // deleting multiple loops in a single pipeline, deleting the first loop and 1546 // deleting the (last) top level loop. 1547 AddLoopPipelineAndVerificationPasses(); 1548 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _)) 1549 .WillOnce(Invoke(getLoopAnalysisResult)); 1550 EXPECT_CALL(MLPHandle, run(HasName("loop.0.0"), _, _, _)) 1551 .WillOnce( 1552 Invoke([&](Loop &L, LoopAnalysisManager &AM, 1553 LoopStandardAnalysisResults &AR, LPMUpdater &Updater) { 1554 AR.SE.forgetLoop(&L); 1555 EraseLoop(L, Loop00PHBB, AR, Updater); 1556 return PreservedAnalyses::all(); 1557 })); 1558 1559 EXPECT_CALL(MLPHandle, run(HasName("loop.0.3"), _, _, _)) 1560 .WillOnce(Invoke(getLoopAnalysisResult)); 1561 EXPECT_CALL(MLPHandle, run(HasName("loop.0.3"), _, _, _)) 1562 .WillOnce( 1563 Invoke([&](Loop &L, LoopAnalysisManager &AM, 1564 LoopStandardAnalysisResults &AR, LPMUpdater &Updater) { 1565 AR.SE.forgetLoop(&L); 1566 EraseLoop(L, *NewLoop03PHBB, AR, Updater); 1567 return PreservedAnalyses::all(); 1568 })); 1569 1570 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _)) 1571 .WillOnce(Invoke(getLoopAnalysisResult)); 1572 EXPECT_CALL(MLPHandle, run(HasName("loop.0"), _, _, _)) 1573 .WillOnce( 1574 Invoke([&](Loop &L, LoopAnalysisManager &AM, 1575 LoopStandardAnalysisResults &AR, LPMUpdater &Updater) { 1576 AR.SE.forgetLoop(&L); 1577 EraseLoop(L, EntryBB, AR, Updater); 1578 return PreservedAnalyses::all(); 1579 })); 1580 1581 // Add the function pass pipeline now that it is fully built up and run it 1582 // over the module's one function. 1583 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); 1584 MPM.run(*M, MAM); 1585 } 1586 } 1587