Skip to content

Commit 1fdd8cb

Browse files
committed
Revert "[Transform] Clean up strlen loop idiom (#132421)"
This reverts commit 7c52886. Reverting this as I have to revert another preceding commit, ac9049d.
1 parent dd97324 commit 1fdd8cb

File tree

5 files changed

+33
-20
lines changed

5 files changed

+33
-20
lines changed

llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,6 +1515,16 @@ 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+
15181528
/// Check if the given conditional branch is based on the comparison between
15191529
/// a variable and zero, and if the variable is non-zero or zero (JmpOnZero is
15201530
/// true), the control yields to the loop entry. If the branch matches the
@@ -1530,8 +1540,7 @@ static Value *matchCondition(BranchInst *BI, BasicBlock *LoopEntry,
15301540
if (!Cond)
15311541
return nullptr;
15321542

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

15371546
BasicBlock *TrueSucc = BI->getSuccessor(0);
@@ -1602,7 +1611,11 @@ class StrlenVerifier {
16021611
return false;
16031612
LoadBaseEv = LoadEv->getStart();
16041613

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+
});
16061619

16071620
const SCEVConstant *Step =
16081621
dyn_cast<SCEVConstant>(LoadEv->getStepRecurrence(*SE));
@@ -1643,7 +1656,11 @@ class StrlenVerifier {
16431656
if (!Ev)
16441657
return false;
16451658

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+
});
16471664

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

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-
17611766
IRBuilder<> Builder(Preheader->getTerminator());
17621767
SCEVExpander Expander(*SE, Preheader->getModule()->getDataLayout(),
17631768
"strlen_idiom");
@@ -1767,8 +1772,16 @@ bool LoopIdiomRecognize::recognizeAndInsertStrLen() {
17671772

17681773
Value *StrLenFunc = nullptr;
17691774
if (Verifier.OpWidth == 8) {
1775+
if (DisableLIRP::Strlen)
1776+
return false;
1777+
if (!isLibFuncEmittable(Preheader->getModule(), TLI, LibFunc_strlen))
1778+
return false;
17701779
StrLenFunc = emitStrLen(MaterialzedBase, Builder, *DL, TLI);
17711780
} else {
1781+
if (DisableLIRP::Wcslen)
1782+
return false;
1783+
if (!isLibFuncEmittable(Preheader->getModule(), TLI, LibFunc_wcslen))
1784+
return false;
17721785
StrLenFunc = emitWcsLen(MaterialzedBase, Builder, *DL, TLI);
17731786
}
17741787
assert(StrLenFunc && "Failed to emit strlen function.");

llvm/test/Transforms/LoopIdiom/strlen-not-emittable.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: opt -passes='loop(loop-idiom)' < %s -S | FileCheck %s
1+
; RUN: opt -passes='loop(loop-idiom),verify' < %s -S | FileCheck %s
22

33
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
44
target triple = "x86_64-unknown-linux-gnu"

llvm/test/Transforms/LoopIdiom/strlen.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2-
; RUN: opt -passes='loop(loop-idiom)' < %s -S | FileCheck %s
2+
; RUN: opt -passes='loop(loop-idiom),verify' < %s -S | FileCheck %s
33

44
declare void @other()
55
declare void @use(ptr)

llvm/test/Transforms/LoopIdiom/wcslen16.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2-
; RUN: opt -passes='loop(loop-idiom)' < %s -S | FileCheck %s
2+
; RUN: opt -passes='loop(loop-idiom),verify' < %s -S | FileCheck %s
33

44
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
55
target triple = "x86_64-unknown-linux-gnu"

llvm/test/Transforms/LoopIdiom/wcslen32.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2-
; RUN: opt -passes='loop(loop-idiom)' < %s -S | FileCheck %s
2+
; RUN: opt -passes='loop(loop-idiom),verify' < %s -S | FileCheck %s
33

44
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
55
target triple = "x86_64-unknown-linux-gnu"

0 commit comments

Comments
 (0)