From bf6357f0f51eccc48b92a130afb51c0280d56180 Mon Sep 17 00:00:00 2001 From: Henry Jiang Date: Fri, 14 Mar 2025 18:56:34 -0400 Subject: [Transforms] LoopIdiomRecognize recognize strlen and wcslen (#108985) This PR continues the effort made in https://discourse.llvm.org/t/rfc-strlen-loop-idiom-recognition-folding/55848 and https://reviews.llvm.org/D83392 and https://reviews.llvm.org/D88460 to extend `LoopIdiomRecognize` to find and replace loops of the form ```c base = str; while (*str) ++str; ``` and transforming the `strlen` loop idiom into the appropriate `strlen` and `wcslen` library call which will give a small performance boost if replaced. ```c str = base + strlen(base) len = str - base ``` --------- Co-authored-by: Michael Kruse --- llvm/lib/Transforms/Utils/BuildLibCalls.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'llvm/lib/Transforms/Utils/BuildLibCalls.cpp') diff --git a/llvm/lib/Transforms/Utils/BuildLibCalls.cpp b/llvm/lib/Transforms/Utils/BuildLibCalls.cpp index 2301be6..24eefc9 100644 --- a/llvm/lib/Transforms/Utils/BuildLibCalls.cpp +++ b/llvm/lib/Transforms/Utils/BuildLibCalls.cpp @@ -1582,6 +1582,15 @@ Value *llvm::emitStrLen(Value *Ptr, IRBuilderBase &B, const DataLayout &DL, return emitLibCall(LibFunc_strlen, SizeTTy, CharPtrTy, Ptr, B, TLI); } +Value *llvm::emitWcsLen(Value *Ptr, IRBuilderBase &B, const DataLayout &DL, + const TargetLibraryInfo *TLI) { + assert(Ptr && Ptr->getType()->isPointerTy() && + "Argument to wcslen intrinsic must be a pointer."); + Type *PtrTy = B.getPtrTy(); + Type *SizeTTy = getSizeTTy(B, TLI); + return emitLibCall(LibFunc_wcslen, SizeTTy, PtrTy, Ptr, B, TLI); +} + Value *llvm::emitStrDup(Value *Ptr, IRBuilderBase &B, const TargetLibraryInfo *TLI) { Type *CharPtrTy = B.getPtrTy(); -- cgit v1.1