Skip to content

Commit 53dc525

Browse files
author
duanbo.db
committed
[LoopInfo] Fix function getInductionVariable
The way function gets the induction variable is by judging whether StepInst or IndVar in the phi statement is one of the operands of CMP. But if the LatchCmpOp0/LatchCmpOp1 is a constant, the subsequent comparison may result in null == null, which is meaningless. This patch fixes the typo. Reviewed By: Whitney Differential Revision: https://reviews.llvm.org/D112980
1 parent 0b39ec8 commit 53dc525

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

llvm/lib/Analysis/LoopInfo.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,15 +301,16 @@ PHINode *Loop::getInductionVariable(ScalarEvolution &SE) const {
301301
if (!CmpInst)
302302
return nullptr;
303303

304-
Instruction *LatchCmpOp0 = dyn_cast<Instruction>(CmpInst->getOperand(0));
305-
Instruction *LatchCmpOp1 = dyn_cast<Instruction>(CmpInst->getOperand(1));
304+
Value *LatchCmpOp0 = CmpInst->getOperand(0);
305+
Value *LatchCmpOp1 = CmpInst->getOperand(1);
306306

307307
for (PHINode &IndVar : Header->phis()) {
308308
InductionDescriptor IndDesc;
309309
if (!InductionDescriptor::isInductionPHI(&IndVar, this, &SE, IndDesc))
310310
continue;
311311

312-
Instruction *StepInst = IndDesc.getInductionBinOp();
312+
BasicBlock *Latch = getLoopLatch();
313+
Value *StepInst = IndVar.getIncomingValueForBlock(Latch);
313314

314315
// case 1:
315316
// IndVar = phi[{InitialValue, preheader}, {StepInst, latch}]

llvm/unittests/Analysis/LoopInfoTest.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,3 +1547,39 @@ TEST(LoopInfoTest, LoopUserBranch) {
15471547
EXPECT_EQ(L->getLoopGuardBranch(), nullptr);
15481548
});
15491549
}
1550+
1551+
TEST(LoopInfoTest, LoopInductionVariable) {
1552+
const char *ModuleStr =
1553+
"define i32 @foo(i32* %addr) {\n"
1554+
"entry:\n"
1555+
" br label %for.body\n"
1556+
"for.body:\n"
1557+
" %sum.08 = phi i32 [ 0, %entry ], [ %add, %for.body ]\n"
1558+
" %addr.addr.06 = phi i32* [ %addr, %entry ], [ %incdec.ptr, %for.body "
1559+
"]\n"
1560+
" %count.07 = phi i32 [ 6000, %entry ], [ %dec, %for.body ]\n"
1561+
" %0 = load i32, i32* %addr.addr.06, align 4\n"
1562+
" %add = add nsw i32 %0, %sum.08\n"
1563+
" %dec = add nsw i32 %count.07, -1\n"
1564+
" %incdec.ptr = getelementptr inbounds i32, i32* %addr.addr.06, i64 1\n"
1565+
" %cmp = icmp ugt i32 %count.07, 1\n"
1566+
" br i1 %cmp, label %for.body, label %for.end\n"
1567+
"for.end:\n"
1568+
" %cmp1 = icmp eq i32 %add, -1\n"
1569+
" %conv = zext i1 %cmp1 to i32\n"
1570+
" ret i32 %conv\n"
1571+
"}\n";
1572+
1573+
// Parse the module.
1574+
LLVMContext Context;
1575+
std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
1576+
1577+
runWithLoopInfoPlus(
1578+
*M, "foo", [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
1579+
Function::iterator FI = F.begin();
1580+
BasicBlock *Header = &*(++FI);
1581+
Loop *L = LI.getLoopFor(Header);
1582+
EXPECT_NE(L, nullptr);
1583+
EXPECT_EQ(L->getInductionVariable(SE)->getName(), "count.07");
1584+
});
1585+
}

0 commit comments

Comments
 (0)