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