@@ -1515,6 +1515,16 @@ bool LoopIdiomRecognize::runOnNoncountableLoop() {
1515
1515
recognizeShiftUntilLessThan () || recognizeAndInsertStrLen ();
1516
1516
}
1517
1517
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
+
1518
1528
// / Check if the given conditional branch is based on the comparison between
1519
1529
// / a variable and zero, and if the variable is non-zero or zero (JmpOnZero is
1520
1530
// / true), the control yields to the loop entry. If the branch matches the
@@ -1530,8 +1540,7 @@ static Value *matchCondition(BranchInst *BI, BasicBlock *LoopEntry,
1530
1540
if (!Cond)
1531
1541
return nullptr ;
1532
1542
1533
- auto *CmpZero = dyn_cast<ConstantInt>(Cond->getOperand (1 ));
1534
- if (!CmpZero || !CmpZero->isZero ())
1543
+ if (!isZeroConstant (Cond->getOperand (1 )))
1535
1544
return nullptr ;
1536
1545
1537
1546
BasicBlock *TrueSucc = BI->getSuccessor (0 );
@@ -1602,7 +1611,11 @@ class StrlenVerifier {
1602
1611
return false ;
1603
1612
LoadBaseEv = LoadEv->getStart ();
1604
1613
1605
- LLVM_DEBUG (dbgs () << " pointer load scev: " << *LoadEv << " \n " );
1614
+ LLVM_DEBUG ({
1615
+ dbgs () << " pointer load scev: " ;
1616
+ LoadEv->print (outs ());
1617
+ dbgs () << " \n " ;
1618
+ });
1606
1619
1607
1620
const SCEVConstant *Step =
1608
1621
dyn_cast<SCEVConstant>(LoadEv->getStepRecurrence (*SE));
@@ -1643,7 +1656,11 @@ class StrlenVerifier {
1643
1656
if (!Ev)
1644
1657
return false ;
1645
1658
1646
- LLVM_DEBUG (dbgs () << " loop exit phi scev: " << *Ev << " \n " );
1659
+ LLVM_DEBUG ({
1660
+ dbgs () << " loop exit phi scev: " ;
1661
+ Ev->print (dbgs ());
1662
+ dbgs () << " \n " ;
1663
+ });
1647
1664
1648
1665
// Since we verified that the loop trip count will be a valid strlen
1649
1666
// idiom, we can expand all lcssa phi with {n,+,1} as (n + strlen) and use
@@ -1746,18 +1763,6 @@ bool LoopIdiomRecognize::recognizeAndInsertStrLen() {
1746
1763
BasicBlock *Preheader = CurLoop->getLoopPreheader ();
1747
1764
BasicBlock *LoopExitBB = CurLoop->getExitBlock ();
1748
1765
1749
- if (Verifier.OpWidth == 8 ) {
1750
- if (DisableLIRP::Strlen)
1751
- return false ;
1752
- if (!isLibFuncEmittable (Preheader->getModule (), TLI, LibFunc_strlen))
1753
- return false ;
1754
- } else {
1755
- if (DisableLIRP::Wcslen)
1756
- return false ;
1757
- if (!isLibFuncEmittable (Preheader->getModule (), TLI, LibFunc_wcslen))
1758
- return false ;
1759
- }
1760
-
1761
1766
IRBuilder<> Builder (Preheader->getTerminator ());
1762
1767
SCEVExpander Expander (*SE, Preheader->getModule ()->getDataLayout (),
1763
1768
" strlen_idiom" );
@@ -1767,8 +1772,16 @@ bool LoopIdiomRecognize::recognizeAndInsertStrLen() {
1767
1772
1768
1773
Value *StrLenFunc = nullptr ;
1769
1774
if (Verifier.OpWidth == 8 ) {
1775
+ if (DisableLIRP::Strlen)
1776
+ return false ;
1777
+ if (!isLibFuncEmittable (Preheader->getModule (), TLI, LibFunc_strlen))
1778
+ return false ;
1770
1779
StrLenFunc = emitStrLen (MaterialzedBase, Builder, *DL, TLI);
1771
1780
} else {
1781
+ if (DisableLIRP::Wcslen)
1782
+ return false ;
1783
+ if (!isLibFuncEmittable (Preheader->getModule (), TLI, LibFunc_wcslen))
1784
+ return false ;
1772
1785
StrLenFunc = emitWcsLen (MaterialzedBase, Builder, *DL, TLI);
1773
1786
}
1774
1787
assert (StrLenFunc && " Failed to emit strlen function." );
0 commit comments