Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 16 additions & 29 deletions llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1515,16 +1515,6 @@ bool LoopIdiomRecognize::runOnNoncountableLoop() {
recognizeShiftUntilLessThan() || recognizeAndInsertStrLen();
}

/// Check if a Value is either a nullptr or a constant int zero
static bool isZeroConstant(const Value *Val) {
if (isa<ConstantPointerNull>(Val))
return true;
const ConstantInt *CmpZero = dyn_cast<ConstantInt>(Val);
if (!CmpZero || !CmpZero->isZero())
return false;
return true;
}

/// Check if the given conditional branch is based on the comparison between
/// a variable and zero, and if the variable is non-zero or zero (JmpOnZero is
/// true), the control yields to the loop entry. If the branch matches the
Expand All @@ -1540,7 +1530,8 @@ static Value *matchCondition(BranchInst *BI, BasicBlock *LoopEntry,
if (!Cond)
return nullptr;

if (!isZeroConstant(Cond->getOperand(1)))
auto *CmpZero = dyn_cast<ConstantInt>(Cond->getOperand(1));
if (!CmpZero || !CmpZero->isZero())
return nullptr;

BasicBlock *TrueSucc = BI->getSuccessor(0);
Expand Down Expand Up @@ -1611,11 +1602,7 @@ class StrlenVerifier {
return false;
LoadBaseEv = LoadEv->getStart();

LLVM_DEBUG({
dbgs() << "pointer load scev: ";
LoadEv->print(outs());
dbgs() << "\n";
});
LLVM_DEBUG(dbgs() << "pointer load scev: " << *LoadEv << "\n");

const SCEVConstant *Step =
dyn_cast<SCEVConstant>(LoadEv->getStepRecurrence(*SE));
Expand Down Expand Up @@ -1656,11 +1643,7 @@ class StrlenVerifier {
if (!Ev)
return false;

LLVM_DEBUG({
dbgs() << "loop exit phi scev: ";
Ev->print(dbgs());
dbgs() << "\n";
});
LLVM_DEBUG(dbgs() << "loop exit phi scev: " << *Ev << "\n");

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

if (Verifier.OpWidth == 8) {
if (DisableLIRP::Strlen)
return false;
if (!isLibFuncEmittable(Preheader->getModule(), TLI, LibFunc_strlen))
return false;
} else {
if (DisableLIRP::Wcslen)
return false;
if (!isLibFuncEmittable(Preheader->getModule(), TLI, LibFunc_wcslen))
return false;
}

IRBuilder<> Builder(Preheader->getTerminator());
SCEVExpander Expander(*SE, Preheader->getModule()->getDataLayout(),
"strlen_idiom");
Expand All @@ -1772,16 +1767,8 @@ bool LoopIdiomRecognize::recognizeAndInsertStrLen() {

Value *StrLenFunc = nullptr;
if (Verifier.OpWidth == 8) {
if (DisableLIRP::Strlen)
return false;
if (!isLibFuncEmittable(Preheader->getModule(), TLI, LibFunc_strlen))
return false;
StrLenFunc = emitStrLen(MaterialzedBase, Builder, *DL, TLI);
} else {
if (DisableLIRP::Wcslen)
return false;
if (!isLibFuncEmittable(Preheader->getModule(), TLI, LibFunc_wcslen))
return false;
StrLenFunc = emitWcsLen(MaterialzedBase, Builder, *DL, TLI);
}
assert(StrLenFunc && "Failed to emit strlen function.");
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Transforms/LoopIdiom/strlen-not-emittable.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; RUN: opt -passes='loop(loop-idiom),verify' < %s -S | FileCheck %s
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why removing the verify?

Copy link
Member Author

@mustartt mustartt Mar 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its redundant:
#131412 (comment)

I also verified that verify is already scheduled at the end of the pipeline.

; RUN: opt -passes='loop(loop-idiom)' < %s -S | FileCheck %s

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"
target triple = "x86_64-unknown-linux-gnu"
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Transforms/LoopIdiom/strlen.ll
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt -passes='loop(loop-idiom),verify' < %s -S | FileCheck %s
; RUN: opt -passes='loop(loop-idiom)' < %s -S | FileCheck %s

declare void @other()
declare void @use(ptr)
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Transforms/LoopIdiom/wcslen16.ll
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt -passes='loop(loop-idiom),verify' < %s -S | FileCheck %s
; RUN: opt -passes='loop(loop-idiom)' < %s -S | FileCheck %s

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"
target triple = "x86_64-unknown-linux-gnu"
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Transforms/LoopIdiom/wcslen32.ll
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt -passes='loop(loop-idiom),verify' < %s -S | FileCheck %s
; RUN: opt -passes='loop(loop-idiom)' < %s -S | FileCheck %s

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"
target triple = "x86_64-unknown-linux-gnu"
Expand Down
Loading