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 @@ -868,8 +868,29 @@ } Value *LibCallSimplifier::optimizeMemRChr(CallInst *CI, IRBuilderBase &B) { - if (isKnownNonZero(CI->getOperand(2), DL)) - annotateNonNullNoUndefBasedOnAccess(CI, 0); + Value *SrcStr = CI->getArgOperand(0); + Value *Size = CI->getArgOperand(2); + annotateNonNullAndDereferenceable(CI, 0, Size, DL); + Value *CharVal = CI->getArgOperand(1); + ConstantInt *LenC = dyn_cast(Size); + + if (LenC) { + if (LenC->isZero()) + // Fold memrchr(x, y, 0) --> null. + return Constant::getNullValue(CI->getType()); + + if (LenC->isOne()) { + // Fold memrchr(x, y, 1) --> *x == y ? x : null for any x and y, + // constant or otherwise. + Value *Val = B.CreateLoad(B.getInt8Ty(), SrcStr, "memrchr.char0"); + // Slice off the character's high end bits. + CharVal = B.CreateTrunc(CharVal, B.getInt8Ty()); + Value *Cmp = B.CreateICmpEQ(Val, CharVal, "memrchr.char0cmp"); + Value *NullPtr = Constant::getNullValue(CI->getType()); + return B.CreateSelect(Cmp, SrcStr, NullPtr, "memrchr.sel"); + } + } + return nullptr; } diff --git a/llvm/test/Transforms/InstCombine/memrchr-2.ll b/llvm/test/Transforms/InstCombine/memrchr-2.ll --- a/llvm/test/Transforms/InstCombine/memrchr-2.ll +++ b/llvm/test/Transforms/InstCombine/memrchr-2.ll @@ -15,10 +15,8 @@ define i8* @fold_memrchr_a12345_1_ui32max_p1(i32 %0) { ; CHECK-LABEL: @fold_memrchr_a12345_1_ui32max_p1( -; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @a12345, i64 0, i64 0), i32 [[TMP0:%.*]], i64 4294967296) +; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(4294967296) getelementptr inbounds ([5 x i8], [5 x i8]* @a12345, i64 0, i64 0), i32 [[TMP0:%.*]], i64 4294967296) ; CHECK-NEXT: ret i8* [[RET]] -; -; CHECK : ret i8* null %ptr = getelementptr [5 x i8], [5 x i8]* @a12345, i64 0, i64 0 %ret = call i8* @memrchr(i8* %ptr, i32 %0, i64 4294967296) @@ -30,10 +28,8 @@ define i8* @fold_memrchr_ax1_1_ui32max_p2(i32 %0) { ; CHECK-LABEL: @fold_memrchr_ax1_1_ui32max_p2( -; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([1 x i8], [1 x i8]* @ax1, i64 0, i64 0), i32 [[TMP0:%.*]], i64 4294967297) +; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(4294967297) getelementptr inbounds ([1 x i8], [1 x i8]* @ax1, i64 0, i64 0), i32 [[TMP0:%.*]], i64 4294967297) ; CHECK-NEXT: ret i8* [[RET]] -; -; CHECK : ret i8* null %ptr = getelementptr [1 x i8], [1 x i8]* @ax1, i64 0, i64 0 %ret = call i8* @memrchr(i8* %ptr, i32 %0, i64 4294967297) @@ -45,7 +41,7 @@ define i8* @fold_memrchr_ax_1_ui32max_p2(i32 %0) { ; CHECK-LABEL: @fold_memrchr_ax_1_ui32max_p2( -; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @a12345, i64 0, i64 0), i32 [[TMP0:%.*]], i64 4294967297) +; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(4294967297) getelementptr inbounds ([5 x i8], [5 x i8]* @a12345, i64 0, i64 0), i32 [[TMP0:%.*]], i64 4294967297) ; CHECK-NEXT: ret i8* [[RET]] ; @@ -59,10 +55,8 @@ define i8* @fold_memrchr_a12345_c_6(i32 %0) { ; CHECK-LABEL: @fold_memrchr_a12345_c_6( -; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @a12345, i64 0, i64 0), i32 [[TMP0:%.*]], i64 6) +; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(6) getelementptr inbounds ([5 x i8], [5 x i8]* @a12345, i64 0, i64 0), i32 [[TMP0:%.*]], i64 6) ; CHECK-NEXT: ret i8* [[RET]] -; -; CHECK : ret i8* null %ptr = getelementptr [5 x i8], [5 x i8]* @a12345, i64 0, i64 0 %ret = call i8* @memrchr(i8* %ptr, i32 %0, i64 6) @@ -74,10 +68,8 @@ define i8* @fold_memrchr_a12345_c_szmax(i32 %0) { ; CHECK-LABEL: @fold_memrchr_a12345_c_szmax( -; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @a12345, i64 0, i64 0), i32 [[TMP0:%.*]], i64 -1) +; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(18446744073709551615) getelementptr inbounds ([5 x i8], [5 x i8]* @a12345, i64 0, i64 0), i32 [[TMP0:%.*]], i64 -1) ; CHECK-NEXT: ret i8* [[RET]] -; -; CHECK : ret i8* null %ptr = getelementptr [5 x i8], [5 x i8]* @a12345, i64 0, i64 0 %ret = call i8* @memrchr(i8* %ptr, i32 %0, i64 18446744073709551615) diff --git a/llvm/test/Transforms/InstCombine/memrchr-3.ll b/llvm/test/Transforms/InstCombine/memrchr-3.ll --- a/llvm/test/Transforms/InstCombine/memrchr-3.ll +++ b/llvm/test/Transforms/InstCombine/memrchr-3.ll @@ -15,8 +15,7 @@ define i8* @fold_memrchr_ax_c_0(i32 %0) { ; CHECK-LABEL: @fold_memrchr_ax_c_0( -; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* getelementptr inbounds ([0 x i8], [0 x i8]* @ax, i64 0, i64 0), i32 [[TMP0:%.*]], i64 0) -; CHECK-NEXT: ret i8* [[RET]] +; CHECK-NEXT: ret i8* null ; %ptr = getelementptr [0 x i8], [0 x i8]* @ax, i32 0, i32 0 @@ -29,8 +28,7 @@ define i8* @fold_memrchr_a12345_3_0() { ; CHECK-LABEL: @fold_memrchr_a12345_3_0( -; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @a12345, i64 0, i64 0), i32 3, i64 0) -; CHECK-NEXT: ret i8* [[RET]] +; CHECK-NEXT: ret i8* null ; %ptr = getelementptr [5 x i8], [5 x i8]* @a12345, i32 0, i32 0 @@ -43,8 +41,7 @@ define i8* @fold_memrchr_a12345_1_1() { ; CHECK-LABEL: @fold_memrchr_a12345_1_1( -; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @a12345, i64 0, i64 0), i32 1, i64 1) -; CHECK-NEXT: ret i8* [[RET]] +; CHECK-NEXT: ret i8* getelementptr inbounds ([5 x i8], [5 x i8]* @a12345, i64 0, i64 0) ; %ptr = getelementptr [5 x i8], [5 x i8]* @a12345, i32 0, i32 0 %ret = call i8* @memrchr(i8* %ptr, i32 1, i64 1) @@ -56,8 +53,7 @@ define i8* @fold_memrchr_a12345_5_1() { ; CHECK-LABEL: @fold_memrchr_a12345_5_1( -; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @a12345, i64 0, i64 0), i32 5, i64 1) -; CHECK-NEXT: ret i8* [[RET]] +; CHECK-NEXT: ret i8* null ; %ptr = getelementptr [5 x i8], [5 x i8]* @a12345, i32 0, i32 0 %ret = call i8* @memrchr(i8* %ptr, i32 5, i64 1) @@ -69,8 +65,7 @@ define i8* @fold_memrchr_a123123_1_1() { ; CHECK-LABEL: @fold_memrchr_a123123_1_1( -; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @a123123, i64 0, i64 0), i32 1, i64 1) -; CHECK-NEXT: ret i8* [[RET]] +; CHECK-NEXT: ret i8* getelementptr inbounds ([6 x i8], [6 x i8]* @a123123, i64 0, i64 0) ; %ptr = getelementptr [6 x i8], [6 x i8]* @a123123, i32 0, i32 0 %ret = call i8* @memrchr(i8* %ptr, i32 1, i64 1) @@ -82,8 +77,7 @@ define i8* @fold_memrchr_a123123_3_1() { ; CHECK-LABEL: @fold_memrchr_a123123_3_1( -; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @a123123, i64 0, i64 0), i32 3, i64 1) -; CHECK-NEXT: ret i8* [[RET]] +; CHECK-NEXT: ret i8* null ; %ptr = getelementptr [6 x i8], [6 x i8]* @a123123, i32 0, i32 0 %ret = call i8* @memrchr(i8* %ptr, i32 3, i64 1) @@ -95,8 +89,11 @@ define i8* @fold_memrchr_ax_c_1(i32 %0) { ; CHECK-LABEL: @fold_memrchr_ax_c_1( -; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([0 x i8], [0 x i8]* @ax, i64 0, i64 0), i32 [[TMP0:%.*]], i64 1) -; CHECK-NEXT: ret i8* [[RET]] +; CHECK-NEXT: [[MEMRCHR_CHAR0:%.*]] = load i8, i8* getelementptr inbounds ([0 x i8], [0 x i8]* @ax, i64 0, i64 0), align 1 +; CHECK-NEXT: [[TMP2:%.*]] = trunc i32 [[TMP0:%.*]] to i8 +; CHECK-NEXT: [[MEMRCHR_CHAR0CMP:%.*]] = icmp eq i8 [[MEMRCHR_CHAR0]], [[TMP2]] +; CHECK-NEXT: [[MEMRCHR_SEL:%.*]] = select i1 [[MEMRCHR_CHAR0CMP]], i8* getelementptr inbounds ([0 x i8], [0 x i8]* @ax, i64 0, i64 0), i8* null +; CHECK-NEXT: ret i8* [[MEMRCHR_SEL]] ; %ptr = getelementptr [0 x i8], [0 x i8]* @ax, i32 0, i32 0 %ret = call i8* @memrchr(i8* %ptr, i32 %0, i64 1) @@ -108,7 +105,7 @@ define i8* @fold_memrchr_a12345_5_5() { ; CHECK-LABEL: @fold_memrchr_a12345_5_5( -; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @a12345, i64 0, i64 0), i32 5, i64 5) +; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @a12345, i64 0, i64 0), i32 5, i64 5) ; CHECK-NEXT: ret i8* [[RET]] ; @@ -122,7 +119,7 @@ define i8* @fold_memrchr_a12345_5_4() { ; CHECK-LABEL: @fold_memrchr_a12345_5_4( -; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @a12345, i64 0, i64 0), i32 5, i64 4) +; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(4) getelementptr inbounds ([5 x i8], [5 x i8]* @a12345, i64 0, i64 0), i32 5, i64 4) ; CHECK-NEXT: ret i8* [[RET]] ; @@ -136,7 +133,7 @@ define i8* @fold_memrchr_a12345_4_5() { ; CHECK-LABEL: @fold_memrchr_a12345_4_5( -; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @a12345, i64 0, i64 0), i32 4, i64 5) +; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @a12345, i64 0, i64 0), i32 4, i64 5) ; CHECK-NEXT: ret i8* [[RET]] ; @@ -150,7 +147,7 @@ define i8* @fold_memrchr_a12345p1_1_4() { ; CHECK-LABEL: @fold_memrchr_a12345p1_1_4( -; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @a12345, i64 0, i64 1), i32 5, i64 4) +; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(4) getelementptr inbounds ([5 x i8], [5 x i8]* @a12345, i64 0, i64 1), i32 5, i64 4) ; CHECK-NEXT: ret i8* [[RET]] ; @@ -164,7 +161,7 @@ define i8* @fold_memrchr_a12345_2_5() { ; CHECK-LABEL: @fold_memrchr_a12345_2_5( -; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @a12345, i64 0, i64 0), i32 2, i64 5) +; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @a12345, i64 0, i64 0), i32 2, i64 5) ; CHECK-NEXT: ret i8* [[RET]] ; @@ -192,7 +189,7 @@ define i8* @fold_memrchr_a123123_3_5() { ; CHECK-LABEL: @fold_memrchr_a123123_3_5( -; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @a123123, i64 0, i64 0), i32 3, i64 5) +; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(5) getelementptr inbounds ([6 x i8], [6 x i8]* @a123123, i64 0, i64 0), i32 3, i64 5) ; CHECK-NEXT: ret i8* [[RET]] ; @@ -206,7 +203,7 @@ define i8* @fold_memrchr_a123123_3_6() { ; CHECK-LABEL: @fold_memrchr_a123123_3_6( -; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @a123123, i64 0, i64 0), i32 3, i64 6) +; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(6) getelementptr inbounds ([6 x i8], [6 x i8]* @a123123, i64 0, i64 0), i32 3, i64 6) ; CHECK-NEXT: ret i8* [[RET]] ; @@ -219,7 +216,7 @@ define i8* @fold_memrchr_a123123_2_6() { ; CHECK-LABEL: @fold_memrchr_a123123_2_6( -; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @a123123, i64 0, i64 0), i32 2, i64 6) +; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(6) getelementptr inbounds ([6 x i8], [6 x i8]* @a123123, i64 0, i64 0), i32 2, i64 6) ; CHECK-NEXT: ret i8* [[RET]] ; @@ -232,7 +229,7 @@ define i8* @fold_memrchr_a123123_1_6() { ; CHECK-LABEL: @fold_memrchr_a123123_1_6( -; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @a123123, i64 0, i64 0), i32 1, i64 6) +; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(6) getelementptr inbounds ([6 x i8], [6 x i8]* @a123123, i64 0, i64 0), i32 1, i64 6) ; CHECK-NEXT: ret i8* [[RET]] ; @@ -246,7 +243,7 @@ define i8* @fold_memrchr_a123123_0_6() { ; CHECK-LABEL: @fold_memrchr_a123123_0_6( -; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @a123123, i64 0, i64 0), i32 0, i64 6) +; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(6) getelementptr inbounds ([6 x i8], [6 x i8]* @a123123, i64 0, i64 0), i32 0, i64 6) ; CHECK-NEXT: ret i8* [[RET]] ; 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 @@ -14,7 +14,7 @@ define i8* @fold_memrchr_a11111_c_5(i32 %0) { ; CHECK-LABEL: @fold_memrchr_a11111_c_5( -; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @a11111, i64 0, i64 0), i32 [[TMP0:%.*]], i64 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 [[TMP0:%.*]], i64 5) ; CHECK-NEXT: ret i8* [[RET]] ; @@ -28,7 +28,7 @@ define i8* @fold_memrchr_a1110111_c_3(i32 %0) { ; CHECK-LABEL: @fold_memrchr_a1110111_c_3( -; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @a1110111, i64 0, i64 0), i32 [[TMP0:%.*]], i64 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 [[TMP0:%.*]], i64 3) ; CHECK-NEXT: ret i8* [[RET]] ; @@ -42,7 +42,7 @@ define i8* @call_memrchr_a1110111_c_4(i32 %0) { ; CHECK-LABEL: @call_memrchr_a1110111_c_4( -; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @a1110111, i64 0, i64 0), i32 [[TMP0:%.*]], i64 4) +; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(4) getelementptr inbounds ([7 x i8], [7 x i8]* @a1110111, i64 0, i64 0), i32 [[TMP0:%.*]], i64 4) ; CHECK-NEXT: ret i8* [[RET]] ; @@ -56,7 +56,7 @@ define i8* @call_memrchr_a11111_c_7(i32 %0) { ; CHECK-LABEL: @call_memrchr_a11111_c_7( -; CHECK-NEXT: [[RET:%.*]] = call i8* @memrchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @a1110111, i64 0, i64 0), i32 [[TMP0:%.*]], i64 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 [[TMP0:%.*]], i64 7) ; CHECK-NEXT: ret i8* [[RET]] ;