Index: unittests/Analysis/LoopInfoTest.cpp =================================================================== --- unittests/Analysis/LoopInfoTest.cpp +++ unittests/Analysis/LoopInfoTest.cpp @@ -15,6 +15,18 @@ using namespace llvm; + +/// Build the loop info for the function and run the Test. +static void runWithLoopInfo(Module &M, StringRef FuncName, + function_ref Test) { + auto *F = M.getFunction(FuncName); + ASSERT_NE(F, nullptr) << "Could not find " << FuncName; + // Compute the dominator tree and the loop info for the function. + DominatorTree DT(*F); + LoopInfo LI(DT); + Test(*F, LI); +} + static std::unique_ptr makeLLVMModule(LLVMContext &Context, const char *ModuleStr) { SMDiagnostic Err; @@ -46,31 +58,26 @@ LLVMContext Context; std::unique_ptr M = makeLLVMModule(Context, ModuleStr); - // Build the dominator tree and loop info. - DominatorTree DT; - DT.recalculate(*M->begin()); - LoopInfo LI; - LI.analyze(DT); - - Function &F = *M->begin(); - Function::iterator FI = F.begin(); - FI++; // First basic block is entry - skip it. - BasicBlock *Header = &*FI++; - assert(Header->getName() == "for.cond"); - Loop *L = LI.getLoopFor(Header); + runWithLoopInfo(*M, "foo", [&](Function &F, LoopInfo &LI) { + Function::iterator FI = F.begin(); + // First basic block is entry - skip it. + BasicBlock *Header = &*(++FI); + assert(Header->getName() == "for.cond"); + Loop *L = LI.getLoopFor(Header); - // This loop is not in simplified form. - EXPECT_FALSE(L->isLoopSimplifyForm()); + // This loop is not in simplified form. + EXPECT_FALSE(L->isLoopSimplifyForm()); - // Analyze the loop metadata id. - bool loopIDFoundAndSet = false; - // Try to get and set the metadata id for the loop. - if (MDNode *D = L->getLoopID()) { - L->setLoopID(D); - loopIDFoundAndSet = true; - } + // Analyze the loop metadata id. + bool loopIDFoundAndSet = false; + // Try to get and set the metadata id for the loop. + if (MDNode *D = L->getLoopID()) { + L->setLoopID(D); + loopIDFoundAndSet = true; + } - // We must have successfully found and set the loop id in the - // only latch the loop has. - EXPECT_TRUE(loopIDFoundAndSet); + // We must have successfully found and set the loop id in the + // only latch the loop has. + EXPECT_TRUE(loopIDFoundAndSet); + }); }