llvm.org GIT mirror llvm / f91f51f
[LoopInfo] Fix Loop::getLoopID() for loops with multiple latches The previous implementation traversed all loop blocks and bailed if one was not a latch block. Since we are only interested in latch blocks, we should only traverse those. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@341926 91177308-0d34-0410-b5e6-96231b3b80d8 Johannes Doerfert 1 year, 8 days ago
2 changed file(s) with 62 addition(s) and 13 deletion(s). Raw diff Collapse all Expand all
217217 } else {
218218 assert(!getLoopLatch() &&
219219 "The loop should have no single latch at this point");
220 // Go through each predecessor of the loop header and check the
221 // terminator for the metadata.
222 BasicBlock *H = getHeader();
223 for (BasicBlock *BB : this->blocks()) {
220 // Go through the latch blocks and check the terminator for the metadata.
221 SmallVector LatchesBlocks;
222 getLoopLatches(LatchesBlocks);
223 for (BasicBlock *BB : LatchesBlocks) {
224224 TerminatorInst *TI = BB->getTerminator();
225 MDNode *MD = nullptr;
226
227 // Check if this terminator branches to the loop header.
228 for (BasicBlock *Successor : successors(TI)) {
229 if (Successor == H) {
230 MD = TI->getMetadata(LLVMContext::MD_loop);
231 break;
232 }
233 }
225 MDNode *MD = TI->getMetadata(LLVMContext::MD_loop);
226
234227 if (!MD)
235228 return nullptr;
236229
7878 // We must have successfully found and set the loop id in the
7979 // only latch the loop has.
8080 EXPECT_TRUE(loopIDFoundAndSet);
81 });
82 }
83
84 // Test loop id handling for a loop with multiple latches.
85 TEST(LoopInfoTest, LoopWithMultipleLatches) {
86 const char *ModuleStr =
87 "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
88 "define void @foo(i32 %n) {\n"
89 "entry:\n"
90 " br i1 undef, label %for.cond, label %for.end\n"
91 "for.cond:\n"
92 " %i.0 = phi i32 [ 0, %entry ], [ %inc, %latch.1 ], [ %inc, %latch.2 ]\n"
93 " %inc = add nsw i32 %i.0, 1\n"
94 " %cmp = icmp slt i32 %i.0, %n\n"
95 " br i1 %cmp, label %latch.1, label %for.end\n"
96 "latch.1:\n"
97 " br i1 undef, label %for.cond, label %latch.2, !llvm.loop !0\n"
98 "latch.2:\n"
99 " br label %for.cond, !llvm.loop !0\n"
100 "for.end:\n"
101 " ret void\n"
102 "}\n"
103 "!0 = distinct !{!0, !1}\n"
104 "!1 = !{!\"llvm.loop.distribute.enable\", i1 true}\n";
105
106 // Parse the module.
107 LLVMContext Context;
108 std::unique_ptr M = makeLLVMModule(Context, ModuleStr);
109
110 runWithLoopInfo(*M, "foo", [&](Function &F, LoopInfo &LI) {
111 Function::iterator FI = F.begin();
112 F.dump();
113 // First basic block is entry - skip it.
114 BasicBlock *Header = &*(++FI);
115 assert(Header->getName() == "for.cond");
116 Loop *L = LI.getLoopFor(Header);
117 EXPECT_NE(L, nullptr);
118
119 // This loop is not in simplified form.
120 EXPECT_FALSE(L->isLoopSimplifyForm());
121
122 // Try to get and set the metadata id for the loop.
123 MDNode *OldLoopID = L->getLoopID();
124 EXPECT_NE(OldLoopID, nullptr);
125
126 MDNode *NewLoopID = MDNode::get(Context, {nullptr});
127 // Set operand 0 to refer to the loop id itself.
128 NewLoopID->replaceOperandWith(0, NewLoopID);
129
130 L->setLoopID(NewLoopID);
131 EXPECT_EQ(L->getLoopID(), NewLoopID);
132 EXPECT_NE(L->getLoopID(), OldLoopID);
133
134 L->setLoopID(OldLoopID);
135 EXPECT_EQ(L->getLoopID(), OldLoopID);
136 EXPECT_NE(L->getLoopID(), NewLoopID);
81137 });
82138 }
83139