Skip to content

Commit b212a29

Browse files
committed
bail out before modifing cfg
1 parent c4eec9e commit b212a29

File tree

1 file changed

+15
-24
lines changed

1 file changed

+15
-24
lines changed

llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,16 +1515,6 @@ bool LoopIdiomRecognize::runOnNoncountableLoop() {
15151515
recognizeShiftUntilLessThan() || recognizeAndInsertStrLen();
15161516
}
15171517

1518-
/// Check if a Value is either a nullptr or a constant int zero
1519-
static bool isZeroConstant(const Value *Val) {
1520-
if (isa<ConstantPointerNull>(Val))
1521-
return true;
1522-
const ConstantInt *CmpZero = dyn_cast<ConstantInt>(Val);
1523-
if (!CmpZero || !CmpZero->isZero())
1524-
return false;
1525-
return true;
1526-
}
1527-
15281518
/// Check if the given conditional branch is based on the comparison between
15291519
/// a variable and zero, and if the variable is non-zero or zero (JmpOnZero is
15301520
/// true), the control yields to the loop entry. If the branch matches the
@@ -1540,7 +1530,8 @@ static Value *matchCondition(BranchInst *BI, BasicBlock *LoopEntry,
15401530
if (!Cond)
15411531
return nullptr;
15421532

1543-
if (!isZeroConstant(Cond->getOperand(1)))
1533+
const ConstantInt *CmpZero = dyn_cast<ConstantInt>(Cond->getOperand(1));
1534+
if (!CmpZero || !CmpZero->isZero())
15441535
return nullptr;
15451536

15461537
BasicBlock *TrueSucc = BI->getSuccessor(0);
@@ -1656,11 +1647,7 @@ class StrlenVerifier {
16561647
if (!Ev)
16571648
return false;
16581649

1659-
LLVM_DEBUG({
1660-
dbgs() << "loop exit phi scev: ";
1661-
Ev->print(dbgs());
1662-
dbgs() << "\n";
1663-
});
1650+
LLVM_DEBUG(dbgs() << "loop exit phi scev: " << *Ev << "\n");
16641651

16651652
// Since we verified that the loop trip count will be a valid strlen
16661653
// idiom, we can expand all lcssa phi with {n,+,1} as (n + strlen) and use
@@ -1763,6 +1750,18 @@ bool LoopIdiomRecognize::recognizeAndInsertStrLen() {
17631750
BasicBlock *Preheader = CurLoop->getLoopPreheader();
17641751
BasicBlock *LoopExitBB = CurLoop->getExitBlock();
17651752

1753+
if (Verifier.OpWidth == 8) {
1754+
if (DisableLIRP::Strlen)
1755+
return false;
1756+
if (!isLibFuncEmittable(Preheader->getModule(), TLI, LibFunc_strlen))
1757+
return false;
1758+
} else {
1759+
if (DisableLIRP::Wcslen)
1760+
return false;
1761+
if (!isLibFuncEmittable(Preheader->getModule(), TLI, LibFunc_wcslen))
1762+
return false;
1763+
}
1764+
17661765
IRBuilder<> Builder(Preheader->getTerminator());
17671766
SCEVExpander Expander(*SE, Preheader->getModule()->getDataLayout(),
17681767
"strlen_idiom");
@@ -1772,16 +1771,8 @@ bool LoopIdiomRecognize::recognizeAndInsertStrLen() {
17721771

17731772
Value *StrLenFunc = nullptr;
17741773
if (Verifier.OpWidth == 8) {
1775-
if (DisableLIRP::Strlen)
1776-
return false;
1777-
if (!isLibFuncEmittable(Preheader->getModule(), TLI, LibFunc_strlen))
1778-
return false;
17791774
StrLenFunc = emitStrLen(MaterialzedBase, Builder, *DL, TLI);
17801775
} else {
1781-
if (DisableLIRP::Wcslen)
1782-
return false;
1783-
if (!isLibFuncEmittable(Preheader->getModule(), TLI, LibFunc_wcslen))
1784-
return false;
17851776
StrLenFunc = emitWcsLen(MaterialzedBase, Builder, *DL, TLI);
17861777
}
17871778
assert(StrLenFunc && "Failed to emit strlen function.");

0 commit comments

Comments
 (0)