[llvm] Memchr inline (PR #130525)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Mar 9 16:47:17 PDT 2025
github-actions[bot] wrote:
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:warning: C/C++ code formatter, clang-format found issues in your code. :warning:
<details>
<summary>
You can test this locally with the following command:
</summary>
``````````bash
git-clang-format --diff 6a7687c455125f6597a9719227a0efcb7f71e572 8d916c2aac81813d8431135493eaf6c2a1fa1201 --extensions cpp -- AggressiveInstCombine.cpp llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp
``````````
</details>
<details>
<summary>
View the diff from clang-format here.
</summary>
``````````diff
diff --git a/AggressiveInstCombine.cpp b/AggressiveInstCombine.cpp
index 10b27ca2ca8..efd74f30168 100644
--- a/AggressiveInstCombine.cpp
+++ b/AggressiveInstCombine.cpp
@@ -19,13 +19,16 @@ static bool foldMemChr(CallInst *Call, DomTreeUpdater *DTU,
Value *ByteVal = IRB.CreateTrunc(Val, IRB.getInt8Ty());
// Initialize result to null
- Value *Result = ConstantPointerNull::get(cast<PointerType>(Call->getType()));
+ Value *Result =
+ ConstantPointerNull::get(cast<PointerType>(Call->getType()));
// For each byte up to Length
for (unsigned i = 0; i < Length; i++) {
- Value *CurPtr = i == 0 ? Ptr :
- IRB.CreateGEP(IRB.getInt8Ty(), Ptr,
- ConstantInt::get(DL.getIndexType(Call->getType()), i));
+ Value *CurPtr =
+ i == 0 ? Ptr
+ : IRB.CreateGEP(
+ IRB.getInt8Ty(), Ptr,
+ ConstantInt::get(DL.getIndexType(Call->getType()), i));
Value *CurByte = IRB.CreateLoad(IRB.getInt8Ty(), CurPtr);
Value *CmpRes = IRB.CreateICmpEQ(CurByte, ByteVal);
Result = IRB.CreateSelect(CmpRes, CurPtr, Result);
@@ -40,4 +43,4 @@ static bool foldMemChr(CallInst *Call, DomTreeUpdater *DTU,
return false;
}
-// ... rest of the file unchanged ...
\ No newline at end of file
+// ... rest of the file unchanged ...
\ No newline at end of file
diff --git a/llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp b/llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp
index bda5981a3c5..6b0c80abca9 100644
--- a/llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp
+++ b/llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp
@@ -54,9 +54,9 @@ static cl::opt<unsigned> StrNCmpInlineThreshold(
cl::desc("The maximum length of a constant string for a builtin string cmp "
"call eligible for inlining. The default value is 3."));
-static cl::opt<unsigned> MemChrInlineThreshold(
- "memchr-inline-threshold", cl::init(6), cl::Hidden,
- cl::desc("Size threshold for inlining memchr calls"));
+static cl::opt<unsigned>
+ MemChrInlineThreshold("memchr-inline-threshold", cl::init(6), cl::Hidden,
+ cl::desc("Size threshold for inlining memchr calls"));
/// Match a pattern for a bitwise funnel/rotate operation that partially guards
/// against undefined behavior by branching around the funnel-shift/rotation
@@ -1110,34 +1110,37 @@ static bool foldMemChr(CallInst *Call, DomTreeUpdater *DTU,
Value *Ptr = Call->getArgOperand(0);
Value *Val = Call->getArgOperand(1);
Value *Len = Call->getArgOperand(2);
-
+
// If length is not a constant, we can't do the optimization
auto *LenC = dyn_cast<ConstantInt>(Len);
if (!LenC)
return false;
-
+
uint64_t Length = LenC->getZExtValue();
-
+
// Check if this is a small memchr we should inline
if (Length <= MemChrInlineThreshold) {
IRBuilder<> IRB(Call);
-
+
// Truncate the search value to i8
Value *ByteVal = IRB.CreateTrunc(Val, IRB.getInt8Ty());
-
+
// Initialize result to null
- Value *Result = ConstantPointerNull::get(cast<PointerType>(Call->getType()));
-
+ Value *Result =
+ ConstantPointerNull::get(cast<PointerType>(Call->getType()));
+
// For each byte up to Length
for (unsigned i = 0; i < Length; i++) {
- Value *CurPtr = i == 0 ? Ptr :
- IRB.CreateGEP(IRB.getInt8Ty(), Ptr,
- ConstantInt::get(DL.getIndexType(Call->getType()), i));
+ Value *CurPtr =
+ i == 0 ? Ptr
+ : IRB.CreateGEP(
+ IRB.getInt8Ty(), Ptr,
+ ConstantInt::get(DL.getIndexType(Call->getType()), i));
Value *CurByte = IRB.CreateLoad(IRB.getInt8Ty(), CurPtr);
Value *CmpRes = IRB.CreateICmpEQ(CurByte, ByteVal);
Result = IRB.CreateSelect(CmpRes, CurPtr, Result);
}
-
+
// Replace the call with our expanded version
Call->replaceAllUsesWith(Result);
Call->eraseFromParent();
@@ -1149,7 +1152,7 @@ static bool foldMemChr(CallInst *Call, DomTreeUpdater *DTU,
Value *Base = Call->getArgOperand(0);
if (!getConstantStringInfo(Base, Str, /*TrimAtNul=*/false))
return false;
-
+
// ... rest of the existing constant string handling ...
// ... existing code ...
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/130525
More information about the llvm-commits
mailing list