https://github.com/nikic created https://github.com/llvm/llvm-project/pull/231401
char has system-dependent signedness. If it is signed, then these ConstantInt::get() uses would assert because the value is not a valid 8 bit unsigned value. Cast to unsigned char to make sure we get a valid value on all platforms. Fixes https://github.com/llvm/llvm-project/issues/228213. (cherry picked from commit b3fe8bb231fb43ae186c9ad38766bffb1e7f8e9a) >From 5eb9f612db474670678cec43f39018c1cbd6a855 Mon Sep 17 00:00:00 2001 From: Nikita Popov <[email protected]> Date: Mon, 12 Oct 2026 18:46:54 +0200 Subject: [PATCH] [SimplifyLibCalls] Fix ConstantInt construction based on char (#231159) char has system-dependent signedness. If it is signed, then these ConstantInt::get() uses would assert because the value is not a valid 8 bit unsigned value. Cast to unsigned char to make sure we get a valid value on all platforms. Fixes https://github.com/llvm/llvm-project/issues/228213. (cherry picked from commit b3fe8bb231fb43ae186c9ad38766bffb1e7f8e9a) --- .../lib/Transforms/Utils/SimplifyLibCalls.cpp | 7 ++++--- llvm/test/Transforms/InstCombine/memchr-6.ll | 20 +++++++++++++++++++ llvm/test/Transforms/InstCombine/memrchr-4.ll | 14 +++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) 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..98ba9bd829aac 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" +@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) { +; 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 +; 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]] +; 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..1c7138d643564 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" +@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) { +; 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 +; 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. _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
