Index: lib/Analysis/LoopInfo.cpp =================================================================== --- lib/Analysis/LoopInfo.cpp +++ lib/Analysis/LoopInfo.cpp @@ -211,9 +211,11 @@ MDNode *Loop::getLoopID() const { MDNode *LoopID = nullptr; - if (isLoopSimplifyForm()) { - LoopID = getLoopLatch()->getTerminator()->getMetadata(LLVMContext::MD_loop); + if (BasicBlock *Latch = getLoopLatch()) { + LoopID = Latch->getTerminator()->getMetadata(LLVMContext::MD_loop); } else { + assert(!getLoopLatch() && + "The loop should have no single latch at this point"); // Go through each predecessor of the loop header and check the // terminator for the metadata. BasicBlock *H = getHeader(); @@ -248,11 +250,12 @@ assert(LoopID->getNumOperands() > 0 && "Loop ID needs at least one operand"); assert(LoopID->getOperand(0) == LoopID && "Loop ID should refer to itself"); - if (isLoopSimplifyForm()) { - getLoopLatch()->getTerminator()->setMetadata(LLVMContext::MD_loop, LoopID); + if (BasicBlock *Latch = getLoopLatch()) { + Latch->getTerminator()->setMetadata(LLVMContext::MD_loop, LoopID); return; } + assert(!getLoopLatch() && "The loop should have no single latch at this point"); BasicBlock *H = getHeader(); for (BasicBlock *BB : this->blocks()) { TerminatorInst *TI = BB->getTerminator(); Index: unittests/Analysis/CMakeLists.txt =================================================================== --- unittests/Analysis/CMakeLists.txt +++ unittests/Analysis/CMakeLists.txt @@ -13,6 +13,7 @@ CFGTest.cpp CGSCCPassManagerTest.cpp LazyCallGraphTest.cpp + LoopInfoTest.cpp MemoryBuiltinsTest.cpp ProfileSummaryInfoTest.cpp ScalarEvolutionTest.cpp Index: unittests/Analysis/LoopInfoTest.cpp =================================================================== --- /dev/null +++ unittests/Analysis/LoopInfoTest.cpp @@ -0,0 +1,97 @@ +//===- LoopInfoTest.cpp - LoopInfo unit tests -----------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Analysis/LoopInfo.h" +#include "llvm/AsmParser/Parser.h" +#include "llvm/IR/LegacyPassManager.h" +#include "llvm/Support/SourceMgr.h" +#include "gtest/gtest.h" + +using namespace llvm; +namespace llvm { +void initializeLoopInfoTestPass(PassRegistry &); + +static SmallVector LoopIDVector; + +namespace { +struct LoopInfoTest : public FunctionPass { + static char ID; + bool runOnFunction(Function &F) override { + LoopInfo *LI = &getAnalysis().getLoopInfo(); + + Function::iterator FI = F.begin(); + FI++; // First basic block is entry - skip it. + BasicBlock *Header = &*FI++; + Loop *L = LI->getLoopFor(Header); + + // Try to get and set the metadata ID for the loop. + if (MDNode *D = L->getLoopID()) { + L->setLoopID(D); + LoopIDVector.push_back(L->getLoopID()); + } + return false; + } + void getAnalysisUsage(AnalysisUsage &AU) const override { + AU.addRequired(); + AU.setPreservesAll(); + } + LoopInfoTest() : FunctionPass(ID) { + initializeLoopInfoTestPass(*PassRegistry::getPassRegistry()); + } +}; +} + +char LoopInfoTest::ID = 0; + +std::unique_ptr makeLLVMModule(LLVMContext &Context, + const char *ModuleStr) { + SMDiagnostic Err; + return parseAssemblyString(ModuleStr, Err, Context); +} + +TEST(LoopInfoTest, LoopWithSingleLatch) { + // Make sure we find the loop id metadata for this loop. We can take the + // getLoopLatch()->getTerminator() path in getLoopID() even this loop is + // not in simplified form. + const char *ModuleStr = + "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n" + "define void @foo(i32 %n) {\n" + "entry:\n" + " br i1 undef, label %for.cond, label %for.end\n" + "for.cond:\n" + " %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.inc ]\n" + " %cmp = icmp slt i32 %i.0, %n\n" + " br i1 %cmp, label %for.inc, label %for.end\n" + "for.inc:\n" + " %inc = add nsw i32 %i.0, 1\n" + " br label %for.cond, !llvm.loop !0\n" + "for.end:\n" + " ret void\n" + "}\n" + "!0 = distinct !{!0, !1}\n" + "!1 = !{!\"llvm.loop.distribute.enable\", i1 true}\n"; + + LoopInfoTest *P = new LoopInfoTest(); + LLVMContext Context; + std::unique_ptr M = makeLLVMModule(Context, ModuleStr); + legacy::PassManager Passes; + Passes.add(P); + Passes.run(*M); + + // Perform checks. + EXPECT_TRUE(LoopIDVector.begin() != LoopIDVector.end()); +} + +} // end namespace llvm + +INITIALIZE_PASS_BEGIN(LoopInfoTest, "loopinfotestpass", "loopinfotestpass", + false, false) +INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) +INITIALIZE_PASS_END(LoopInfoTest, "loopinfotestpass", "loopinfotestpass", + false, false)