[llvm] r350775 - [SimplifyLibCalls] Fix memchr expansion for constant strings.
Eli Friedman via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 9 15:39:26 PST 2019
Author: efriedma
Date: Wed Jan 9 15:39:26 2019
New Revision: 350775
URL: http://llvm.org/viewvc/llvm-project?rev=350775&view=rev
Log:
[SimplifyLibCalls] Fix memchr expansion for constant strings.
The C standard says "The memchr function locates the first
occurrence of c (converted to an unsigned char)[...]". The expansion
was missing the conversion to unsigned char.
Fixes https://bugs.llvm.org/show_bug.cgi?id=39041 .
Differential Revision: https://reviews.llvm.org/D55947
Modified:
llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp
llvm/trunk/test/Transforms/InstCombine/memchr.ll
llvm/trunk/test/Transforms/InstCombine/strchr-1.ll
Modified: llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp?rev=350775&r1=350774&r2=350775&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp Wed Jan 9 15:39:26 2019
@@ -798,8 +798,11 @@ Value *LibCallSimplifier::optimizeMemChr
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");
Modified: llvm/trunk/test/Transforms/InstCombine/memchr.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/memchr.ll?rev=350775&r1=350774&r2=350775&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/memchr.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/memchr.ll Wed Jan 9 15:39:26 2019
@@ -50,7 +50,7 @@ define void @test3() {
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 @@ define void @test10() {
; 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 @@ define i1 @test11(i32 %C) {
; 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 @test12(i32 %C) {
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 @test13(i32 %C) {
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 @test14(i32 %C) {
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]]
;
Modified: llvm/trunk/test/Transforms/InstCombine/strchr-1.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/strchr-1.ll?rev=350775&r1=350774&r2=350775&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/strchr-1.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/strchr-1.ll Wed Jan 9 15:39:26 2019
@@ -82,8 +82,9 @@ define void @test_simplify6(i8* %str) {
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
More information about the llvm-commits
mailing list