Index: lib/Transforms/Utils/SimplifyLibCalls.cpp =================================================================== --- lib/Transforms/Utils/SimplifyLibCalls.cpp +++ lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -798,8 +798,11 @@ Bitfield.setBit((unsigned char)C); Value *BitfieldC = B.getInt(Bitfield); - // First check that the bit field access is within bounds. + // Adjust width of "C" to the bitfield width, then mask off the high bits. Value *C = B.CreateZExtOrTrunc(CI->getArgOperand(1), BitfieldC->getType()); + C = B.CreateAnd(C, B.getIntN(Width, 0xFF)); + + // First check that the bit field access is within bounds. Value *Bounds = B.CreateICmp(ICmpInst::ICMP_ULT, C, B.getIntN(Width, Width), "memchr.bounds"); Index: test/Transforms/InstCombine/memchr.ll =================================================================== --- test/Transforms/InstCombine/memchr.ll +++ test/Transforms/InstCombine/memchr.ll @@ -50,7 +50,7 @@ define void @test4(i32 %chr) { ; CHECK-LABEL: @test4( -; CHECK-NEXT: [[DST:%.*]] = call i8* @memchr(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @hello, i32 0, i32 0), i32 %chr, i32 14) +; CHECK-NEXT: [[DST:%.*]] = call i8* @memchr(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @hello, i32 0, i32 0), i32 [[CHR:%.*]], i32 14) ; CHECK-NEXT: store i8* [[DST]], i8** @chp, align 4 ; CHECK-NEXT: ret void ; @@ -131,12 +131,13 @@ ; Check transformation memchr("\r\n", C, 2) != nullptr -> (C & 9216) != 0 define i1 @test11(i32 %C) { ; CHECK-LABEL: @test11( -; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 %C to i16 -; CHECK-NEXT: [[MEMCHR_BOUNDS:%.*]] = icmp ult i16 [[TMP1]], 16 -; CHECK-NEXT: [[TMP2:%.*]] = shl i16 1, [[TMP1]] -; CHECK-NEXT: [[TMP3:%.*]] = and i16 [[TMP2]], 9216 -; CHECK-NEXT: [[MEMCHR_BITS:%.*]] = icmp ne i16 [[TMP3]], 0 -; CHECK-NEXT: [[MEMCHR:%.*]] = and i1 [[MEMCHR:%.*]].bounds, [[MEMCHR:%.*]].bits +; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 [[C:%.*]] to i16 +; CHECK-NEXT: [[TMP2:%.*]] = and i16 [[TMP1]], 255 +; CHECK-NEXT: [[MEMCHR_BOUNDS:%.*]] = icmp ult i16 [[TMP2]], 16 +; CHECK-NEXT: [[TMP3:%.*]] = shl i16 1, [[TMP2]] +; CHECK-NEXT: [[TMP4:%.*]] = and i16 [[TMP3]], 9216 +; CHECK-NEXT: [[MEMCHR_BITS:%.*]] = icmp ne i16 [[TMP4]], 0 +; CHECK-NEXT: [[MEMCHR:%.*]] = and i1 [[MEMCHR_BOUNDS]], [[MEMCHR_BITS]] ; CHECK-NEXT: ret i1 [[MEMCHR]] ; %dst = call i8* @memchr(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @newlines, i64 0, i64 0), i32 %C, i32 2) @@ -147,7 +148,7 @@ ; No 64 bits here define i1 @test12(i32 %C) { ; CHECK-LABEL: @test12( -; CHECK-NEXT: [[DST:%.*]] = call i8* @memchr(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @spaces, i32 0, i32 0), i32 %C, i32 3) +; CHECK-NEXT: [[DST:%.*]] = call i8* @memchr(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @spaces, i32 0, i32 0), i32 [[C:%.*]], i32 3) ; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8* [[DST]], null ; CHECK-NEXT: ret i1 [[CMP]] ; @@ -158,11 +159,12 @@ define i1 @test13(i32 %C) { ; CHECK-LABEL: @test13( -; CHECK-NEXT: [[MEMCHR_BOUNDS:%.*]] = icmp ult i32 %C, 32 -; CHECK-NEXT: [[TMP1:%.*]] = shl i32 1, %C -; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP1]], -2147483647 -; CHECK-NEXT: [[MEMCHR_BITS:%.*]] = icmp ne i32 [[TMP2]], 0 -; CHECK-NEXT: [[MEMCHR:%.*]] = and i1 [[MEMCHR:%.*]].bounds, [[MEMCHR:%.*]].bits +; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[C:%.*]], 255 +; CHECK-NEXT: [[MEMCHR_BOUNDS:%.*]] = icmp ult i32 [[TMP1]], 32 +; CHECK-NEXT: [[TMP2:%.*]] = shl i32 1, [[TMP1]] +; CHECK-NEXT: [[TMP3:%.*]] = and i32 [[TMP2]], -2147483647 +; CHECK-NEXT: [[MEMCHR_BITS:%.*]] = icmp ne i32 [[TMP3]], 0 +; CHECK-NEXT: [[MEMCHR:%.*]] = and i1 [[MEMCHR_BOUNDS]], [[MEMCHR_BITS]] ; CHECK-NEXT: ret i1 [[MEMCHR]] ; %dst = call i8* @memchr(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @single, i64 0, i64 0), i32 %C, i32 2) @@ -172,8 +174,9 @@ define i1 @test14(i32 %C) { ; CHECK-LABEL: @test14( -; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 %C, 31 -; CHECK-NEXT: ret i1 [[TMP1]] +; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[C:%.*]], 255 +; CHECK-NEXT: [[MEMCHR_BITS:%.*]] = icmp eq i32 [[TMP1]], 31 +; CHECK-NEXT: ret i1 [[MEMCHR_BITS]] ; %dst = call i8* @memchr(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @single, i64 0, i64 0), i32 %C, i32 1) %cmp = icmp ne i8* %dst, null @@ -182,7 +185,7 @@ define i1 @test15(i32 %C) { ; CHECK-LABEL: @test15( -; CHECK-NEXT: [[DST:%.*]] = call i8* @memchr(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @negative, i32 0, i32 0), i32 %C, i32 3) +; CHECK-NEXT: [[DST:%.*]] = call i8* @memchr(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @negative, i32 0, i32 0), i32 [[C:%.*]], i32 3) ; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8* [[DST]], null ; CHECK-NEXT: ret i1 [[CMP]] ; Index: test/Transforms/InstCombine/strchr-1.ll =================================================================== --- test/Transforms/InstCombine/strchr-1.ll +++ test/Transforms/InstCombine/strchr-1.ll @@ -82,8 +82,9 @@ define i1 @test_simplify7(i32 %C) { ; CHECK-LABEL: @test_simplify7 ; CHECK-NEXT: [[TRUNC:%.*]] = trunc i32 %C to i16 -; CHECK-NEXT: %memchr.bounds = icmp ult i16 [[TRUNC]], 16 -; CHECK-NEXT: [[SHL:%.*]] = shl i16 1, [[TRUNC]] +; CHECK-NEXT: [[TRUNC_AND:%.*]] = and i16 [[TRUNC]], 255 +; CHECK-NEXT: %memchr.bounds = icmp ult i16 [[TRUNC_AND]], 16 +; CHECK-NEXT: [[SHL:%.*]] = shl i16 1, [[TRUNC_AND]] ; CHECK-NEXT: [[AND:%.*]] = and i16 [[SHL]], 9217 ; CHECK-NEXT: %memchr.bits = icmp ne i16 [[AND]], 0 ; CHECK-NEXT: %memchr1 = and i1 %memchr.bounds, %memchr.bits