Index: lib/Analysis/LoopInfo.cpp =================================================================== --- lib/Analysis/LoopInfo.cpp +++ lib/Analysis/LoopInfo.cpp @@ -219,22 +219,20 @@ // Go through each predecessor of the loop header and check the // terminator for the metadata. BasicBlock *H = getHeader(); - for (BasicBlock *BB : this->blocks()) { - TerminatorInst *TI = BB->getTerminator(); - MDNode *MD = nullptr; - - // Check if this terminator branches to the loop header. - for (BasicBlock *Successor : TI->successors()) { - if (Successor == H) { - MD = TI->getMetadata(LLVMContext::MD_loop); - break; - } - } - if (!MD) + assert(H && "Loop must have a header"); + for (auto I = pred_begin(H), E = pred_end(H); I != E; ++I) { + // Ignore header's predecessor outside the loop. + if (!contains(*I)) + continue; + MDNode *MD = (*I)->getTerminator()->getMetadata(LLVMContext::MD_loop); + // Unable to find a metadata in the latch block. + if (!MD) return nullptr; + // Found a metadata. if (!LoopID) LoopID = MD; + // Found a non-null, but different metadata from the one seen before. else if (MD != LoopID) return nullptr; } Index: unittests/Analysis/LoopInfoTest.cpp =================================================================== --- unittests/Analysis/LoopInfoTest.cpp +++ unittests/Analysis/LoopInfoTest.cpp @@ -75,3 +75,94 @@ // only latch the loop has. EXPECT_TRUE(loopIDFoundAndSet); } + +// This test has 2 latches and both carry the same llvm.loop metadata. +TEST(LoopInfoTest, LoopWithMultiLatchesWithSameMD) { + 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.first.latch ], [ %inc2, %for.second.latch]\n" + " %cmp = icmp slt i32 %i.0, %n\n" + " br i1 %cmp, label %for.first.latch, label %for.end\n" + "for.first.latch:\n" + " %inc = add nsw i32 %i.0, 1\n" + " br i1 undef, label %for.second.latch, label %for.cond, !llvm.loop !0\n" + "for.second.latch:\n" + " %inc2 = add nsw i32 %inc, 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"; + + // Parse the module. + 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); + + // We must be able to get the metadata. + EXPECT_TRUE(L->getLoopID()); +} + +// This test has 2 latches, but only 1 carry the llvm.loop metadata. +TEST(LoopInfoTest, LoopWithMultiLatchesSomeWithMD) { + 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.first.latch ], [ %inc2, %for.second.latch]\n" + " %cmp = icmp slt i32 %i.0, %n\n" + " br i1 %cmp, label %for.first.latch, label %for.end\n" + "for.first.latch:\n" + " %inc = add nsw i32 %i.0, 1\n" + " br i1 undef, label %for.second.latch, label %for.cond, !llvm.loop !0\n" + "for.second.latch:\n" + " %inc2 = add nsw i32 %inc, 1\n" + " br label %for.cond\n" + "for.end:\n" + " ret void\n" + "}\n" + "!0 = distinct !{!0, !1}\n" + "!1 = !{!\"llvm.loop.distribute.enable\", i1 true}\n"; + + // Parse the module. + 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); + + // We must not be able to get the metadata from the loop as one of the + // latch does not have it. + EXPECT_FALSE(L->getLoopID()); +} + +