[llvm-branch-commits] [llvm] release/23.x: [SimplifyLibCalls] Fix ConstantInt construction based on char (#231159) (PR #231224)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Mon Oct 12 09:56:08 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: llvmbot
<details>
<summary>Changes</summary>
Backport b3fe8bb231fb43ae186c9ad38766bffb1e7f8e9a
Requested by: @<!-- -->nikic
---
Full diff: https://github.com/llvm/llvm-project/pull/231224.diff
3 Files Affected:
- (modified) llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp (+4-3)
- (modified) llvm/test/Transforms/InstCombine/memchr-6.ll (+20)
- (modified) llvm/test/Transforms/InstCombine/memrchr-4.ll (+14)
``````````diff
diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index b167fabdcf3ba..0ea3c3ff10b56 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -1299,7 +1299,8 @@ Value *LibCallSimplifier::optimizeMemRChr(CallInst *CI, IRBuilderBase &B) {
Value *NNeZ = B.CreateICmpNE(Size, ConstantInt::get(SizeTy, 0));
// Slice off the sought character's high end bits.
CharVal = B.CreateTrunc(CharVal, Int8Ty);
- Value *CEqS0 = B.CreateICmpEQ(ConstantInt::get(Int8Ty, Str[0]), CharVal);
+ Value *CEqS0 =
+ B.CreateICmpEQ(ConstantInt::get(Int8Ty, (unsigned char)Str[0]), CharVal);
Value *And = B.CreateLogicalAnd(NNeZ, CEqS0);
Value *SizeM1 = B.CreateSub(Size, ConstantInt::get(SizeTy, 1));
Value *SrcPlus =
@@ -1390,7 +1391,7 @@ Value *LibCallSimplifier::optimizeMemChr(CallInst *CI, IRBuilderBase &B) {
if (Pos != StringRef::npos) {
// Handle two consecutive sequences of the same characters.
Value *PosVal = ConstantInt::get(SizeTy, Pos);
- Value *StrPos = ConstantInt::get(Int8Ty, Str[Pos]);
+ Value *StrPos = ConstantInt::get(Int8Ty, (unsigned char)Str[Pos]);
Value *CEqSPos = B.CreateICmpEQ(CharVal, StrPos);
Value *NGtPos = B.CreateICmp(ICmpInst::ICMP_UGT, Size, PosVal);
Value *And = B.CreateAnd(CEqSPos, NGtPos);
@@ -1398,7 +1399,7 @@ Value *LibCallSimplifier::optimizeMemChr(CallInst *CI, IRBuilderBase &B) {
Sel1 = B.CreateSelect(And, SrcPlus, NullPtr, "memchr.sel1");
}
- Value *Str0 = ConstantInt::get(Int8Ty, Str[0]);
+ Value *Str0 = ConstantInt::get(Int8Ty, (unsigned char)Str[0]);
Value *CEqS0 = B.CreateICmpEQ(Str0, CharVal);
Value *NNeZ = B.CreateICmpNE(Size, ConstantInt::get(SizeTy, 0));
Value *And = B.CreateAnd(NNeZ, CEqS0);
diff --git a/llvm/test/Transforms/InstCombine/memchr-6.ll b/llvm/test/Transforms/InstCombine/memchr-6.ll
index 3c55f418cd8e0..7daf29f80b5d5 100644
--- a/llvm/test/Transforms/InstCombine/memchr-6.ll
+++ b/llvm/test/Transforms/InstCombine/memchr-6.ll
@@ -10,6 +10,7 @@ declare ptr @memchr(ptr, i32, i64)
@a11111 = constant [5 x i8] c"\01\01\01\01\01"
@a111122 = constant [6 x i8] c"\01\01\01\01\02\02"
@a1110111 = constant [7 x i8] c"\01\01\01\00\01\01\01"
+ at a_fe_ff = constant [6 x i8] c"\FE\FE\FE\FE\FF\FF"
; Fold memchr(a00000, C, 5) to *a00000 == C ? a00000 : null.
@@ -81,6 +82,25 @@ define ptr @fold_memchr_a111122_c_n(i32 %C, i64 %N) {
ret ptr %ret
}
+; Same as previous, but using characters with top bit set.
+
+define ptr @fold_memchr_a_fe_ff(i32 %C, i64 %N) !prof !0 {
+; CHECK-LABEL: @fold_memchr_a_fe_ff(
+; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 [[C:%.*]] to i8
+; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i8 [[TMP1]], -1
+; CHECK-NEXT: [[TMP3:%.*]] = icmp ugt i64 [[N:%.*]], 4
+; CHECK-NEXT: [[TMP4:%.*]] = and i1 [[TMP2]], [[TMP3]]
+; CHECK-NEXT: [[MEMCHR_SEL1:%.*]] = select i1 [[TMP4]], ptr getelementptr inbounds nuw (i8, ptr @a_fe_ff, i64 4), ptr null, !prof [[PROF1]]
+; CHECK-NEXT: [[TMP5:%.*]] = icmp eq i8 [[TMP1]], -2
+; CHECK-NEXT: [[TMP6:%.*]] = icmp ne i64 [[N]], 0
+; CHECK-NEXT: [[TMP7:%.*]] = and i1 [[TMP6]], [[TMP5]]
+; CHECK-NEXT: [[MEMCHR_SEL2:%.*]] = select i1 [[TMP7]], ptr @a_fe_ff, ptr [[MEMCHR_SEL1]], !prof [[PROF1]]
+; CHECK-NEXT: ret ptr [[MEMCHR_SEL2]]
+;
+
+ %ret = call ptr @memchr(ptr @a_fe_ff, i32 %C, i64 %N)
+ ret ptr %ret
+}
; Fold memchr(a1110111, C, 3) to a1110111[2] == C ? a1110111 : null.
diff --git a/llvm/test/Transforms/InstCombine/memrchr-4.ll b/llvm/test/Transforms/InstCombine/memrchr-4.ll
index 7362916e28fb9..4faaefde938e9 100644
--- a/llvm/test/Transforms/InstCombine/memrchr-4.ll
+++ b/llvm/test/Transforms/InstCombine/memrchr-4.ll
@@ -8,6 +8,7 @@ declare ptr @memrchr(ptr, i32, i64)
@a11111 = constant [5 x i8] c"\01\01\01\01\01"
@a1110111 = constant [7 x i8] c"\01\01\01\00\01\01\01"
+ at aff = constant [5 x i8] c"\FF\FF\FF\FF\FF"
; Fold memrchr(a11111, C, 5) to *a11111 == C ? a11111 + 5 - 1 : null.
@@ -24,6 +25,19 @@ define ptr @fold_memrchr_a11111_c_5(i32 %C) {
ret ptr %ret
}
+; Same as previous, but using characters with top bit set.
+
+define ptr @fold_memrchr_aff(i32 %C) !prof !0 {
+; CHECK-LABEL: @fold_memrchr_aff(
+; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 [[C:%.*]] to i8
+; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i8 [[TMP1]], -1
+; CHECK-NEXT: [[MEMRCHR_SEL:%.*]] = select i1 [[TMP2]], ptr getelementptr inbounds nuw (i8, ptr @aff, i64 4), ptr null, !prof [[PROF1]]
+; CHECK-NEXT: ret ptr [[MEMRCHR_SEL]]
+;
+ %ret = call ptr @memrchr(ptr @aff, i32 %C, i64 5)
+ ret ptr %ret
+}
+
; Fold memrchr(a11111, C, N) to N && *a11111 == C ? a11111 + N - 1 : null,
; on the assumption that N is in bounds.
``````````
</details>
https://github.com/llvm/llvm-project/pull/231224
More information about the llvm-branch-commits
mailing list