diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp --- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -974,7 +974,24 @@ } } - return nullptr; + // Truncate the string to search at most EndOff characters. + Str = Str.substr(0, EndOff); + if (Str.find_first_not_of(Str[0]) != StringRef::npos) + return nullptr; + + // If the source array consists of all equal characters, then for any + // C and N (whether in bounds or not), fold memrchr(S, C, N) to + // N != 0 && *S == C ? S + N - 1 : null + Type *SizeTy = Size->getType(); + Type *Int8Ty = B.getInt8Ty(); + 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 *And = B.CreateLogicalAnd(NNeZ, CEqS0); + Value *SizeM1 = B.CreateSub(Size, ConstantInt::get(SizeTy, 1)); + Value *SrcPlus = B.CreateGEP(Int8Ty, SrcStr, SizeM1, "memrchr.ptr_plus"); + return B.CreateSelect(And, SrcPlus, NullPtr, "memrchr.sel"); } Value *LibCallSimplifier::optimizeMemChr(CallInst *CI, IRBuilderBase &B) { diff --git a/llvm/test/Transforms/InstCombine/memrchr-4.ll b/llvm/test/Transforms/InstCombine/memrchr-4.ll --- a/llvm/test/Transforms/InstCombine/memrchr-4.ll +++ b/llvm/test/Transforms/InstCombine/memrchr-4.ll @@ -2,7 +2,7 @@ ; RUN: opt < %s -passes=instcombine -S | FileCheck %s ; ; Verify that memrchr calls with a string consisting of all the same -; characters are folded. +; characters are folded and those with mixed strings are not. declare i8* @memrchr(i8*, i32, i64) @@ -14,8 +14,10 @@ define i8* @fold_memrchr_a11111_c_5(i32 %C) { ; CHECK-LABEL: @fold_memrchr_a11111_c_5( -; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @a11111, i64 0, i64 0), i32 [[C:%.*]], i64 5) -; CHECK-NEXT: ret i8* [[RET]] +; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 [[C:%.*]] to i8 +; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i8 [[TMP1]], 1 +; CHECK-NEXT: [[MEMRCHR_SEL:%.*]] = select i1 [[TMP2]], i8* getelementptr inbounds ([5 x i8], [5 x i8]* @a11111, i64 0, i64 4), i8* null +; CHECK-NEXT: ret i8* [[MEMRCHR_SEL]] ; %ptr = getelementptr [5 x i8], [5 x i8]* @a11111, i64 0, i64 0 @@ -24,12 +26,35 @@ } +; Fold memrchr(a11111, C, N) to N && *a11111 == C ? a11111 + N - 1 : null, +; on the assumption that N is in bounds. + +define i8* @fold_memrchr_a11111_c_n(i32 %C, i64 %N) { +; CHECK-LABEL: @fold_memrchr_a11111_c_n( +; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i64 [[N:%.*]], 0 +; CHECK-NEXT: [[TMP2:%.*]] = trunc i32 [[C:%.*]] to i8 +; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i8 [[TMP2]], 1 +; CHECK-NEXT: [[TMP4:%.*]] = select i1 [[TMP1]], i1 [[TMP3]], i1 false +; CHECK-NEXT: [[TMP5:%.*]] = add i64 [[N]], -1 +; CHECK-NEXT: [[MEMRCHR_PTR_PLUS:%.*]] = getelementptr [5 x i8], [5 x i8]* @a11111, i64 0, i64 [[TMP5]] +; CHECK-NEXT: [[MEMRCHR_SEL:%.*]] = select i1 [[TMP4]], i8* [[MEMRCHR_PTR_PLUS]], i8* null +; CHECK-NEXT: ret i8* [[MEMRCHR_SEL]] +; + + %ptr = getelementptr [5 x i8], [5 x i8]* @a11111, i64 0, i64 0 + %ret = call i8* @memrchr(i8* %ptr, i32 %C, i64 %N) + ret i8* %ret +} + + ; Fold memrchr(a1110111, C, 3) to a1110111[2] == C ? a1110111 + 2 : null. define i8* @fold_memrchr_a1110111_c_3(i32 %C) { ; CHECK-LABEL: @fold_memrchr_a1110111_c_3( -; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(3) getelementptr inbounds ([7 x i8], [7 x i8]* @a1110111, i64 0, i64 0), i32 [[C:%.*]], i64 3) -; CHECK-NEXT: ret i8* [[RET]] +; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 [[C:%.*]] to i8 +; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i8 [[TMP1]], 1 +; CHECK-NEXT: [[MEMRCHR_SEL:%.*]] = select i1 [[TMP2]], i8* getelementptr inbounds ([7 x i8], [7 x i8]* @a1110111, i64 0, i64 2), i8* null +; CHECK-NEXT: ret i8* [[MEMRCHR_SEL]] ; %ptr = getelementptr [7 x i8], [7 x i8]* @a1110111, i64 0, i64 0 @@ -54,8 +79,8 @@ ; Don't fold memrchr(a1110111, C, 7). -define i8* @call_memrchr_a11111_c_7(i32 %C) { -; CHECK-LABEL: @call_memrchr_a11111_c_7( +define i8* @call_memrchr_a1110111_c_7(i32 %C) { +; CHECK-LABEL: @call_memrchr_a1110111_c_7( ; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(7) getelementptr inbounds ([7 x i8], [7 x i8]* @a1110111, i64 0, i64 0), i32 [[C:%.*]], i64 7) ; CHECK-NEXT: ret i8* [[RET]] ; @@ -64,3 +89,17 @@ %ret = call i8* @memrchr(i8* %ptr, i32 %C, i64 7) ret i8* %ret } + + +; Don't fold memrchr(a1110111, C, N). + +define i8* @call_memrchr_a1110111_c_n(i32 %C, i64 %N) { +; CHECK-LABEL: @call_memrchr_a1110111_c_n( +; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @a1110111, i64 0, i64 0), i32 [[C:%.*]], i64 [[N:%.*]]) +; CHECK-NEXT: ret i8* [[RET]] +; + + %ptr = getelementptr [7 x i8], [7 x i8]* @a1110111, i64 0, i64 0 + %ret = call i8* @memrchr(i8* %ptr, i32 %C, i64 %N) + ret i8* %ret +}