llvm.org GIT mirror llvm / 8b3efeb
[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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@350775 91177308-0d34-0410-b5e6-96231b3b80d8 Eli Friedman 10 months ago
3 changed file(s) with 26 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
797797 Bitfield.setBit((unsigned char)C);
798798 Value *BitfieldC = B.getInt(Bitfield);
799799
800 // Adjust width of "C" to the bitfield width, then mask off the high bits.
801 Value *C = B.CreateZExtOrTrunc(CI->getArgOperand(1), BitfieldC->getType());
802 C = B.CreateAnd(C, B.getIntN(Width, 0xFF));
803
800804 // First check that the bit field access is within bounds.
801 Value *C = B.CreateZExtOrTrunc(CI->getArgOperand(1), BitfieldC->getType());
802805 Value *Bounds = B.CreateICmp(ICmpInst::ICMP_ULT, C, B.getIntN(Width, Width),
803806 "memchr.bounds");
804807
4949
5050 define void @test4(i32 %chr) {
5151 ; CHECK-LABEL: @test4(
52 ; CHECK-NEXT: [[DST:%.*]] = call i8* @memchr(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @hello, i32 0, i32 0), i32 %chr, i32 14)
52 ; CHECK-NEXT: [[DST:%.*]] = call i8* @memchr(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @hello, i32 0, i32 0), i32 [[CHR:%.*]], i32 14)
5353 ; CHECK-NEXT: store i8* [[DST]], i8** @chp, align 4
5454 ; CHECK-NEXT: ret void
5555 ;
130130 ; Check transformation memchr("\r\n", C, 2) != nullptr -> (C & 9216) != 0
131131 define i1 @test11(i32 %C) {
132132 ; CHECK-LABEL: @test11(
133 ; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 %C to i16
134 ; CHECK-NEXT: [[MEMCHR_BOUNDS:%.*]] = icmp ult i16 [[TMP1]], 16
135 ; CHECK-NEXT: [[TMP2:%.*]] = shl i16 1, [[TMP1]]
136 ; CHECK-NEXT: [[TMP3:%.*]] = and i16 [[TMP2]], 9216
137 ; CHECK-NEXT: [[MEMCHR_BITS:%.*]] = icmp ne i16 [[TMP3]], 0
138 ; CHECK-NEXT: [[MEMCHR:%.*]] = and i1 [[MEMCHR:%.*]].bounds, [[MEMCHR:%.*]].bits
133 ; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 [[C:%.*]] to i16
134 ; CHECK-NEXT: [[TMP2:%.*]] = and i16 [[TMP1]], 255
135 ; CHECK-NEXT: [[MEMCHR_BOUNDS:%.*]] = icmp ult i16 [[TMP2]], 16
136 ; CHECK-NEXT: [[TMP3:%.*]] = shl i16 1, [[TMP2]]
137 ; CHECK-NEXT: [[TMP4:%.*]] = and i16 [[TMP3]], 9216
138 ; CHECK-NEXT: [[MEMCHR_BITS:%.*]] = icmp ne i16 [[TMP4]], 0
139 ; CHECK-NEXT: [[MEMCHR:%.*]] = and i1 [[MEMCHR_BOUNDS]], [[MEMCHR_BITS]]
139140 ; CHECK-NEXT: ret i1 [[MEMCHR]]
140141 ;
141142 %dst = call i8* @memchr(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @newlines, i64 0, i64 0), i32 %C, i32 2)
146147 ; No 64 bits here
147148 define i1 @test12(i32 %C) {
148149 ; CHECK-LABEL: @test12(
149 ; CHECK-NEXT: [[DST:%.*]] = call i8* @memchr(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @spaces, i32 0, i32 0), i32 %C, i32 3)
150 ; CHECK-NEXT: [[DST:%.*]] = call i8* @memchr(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @spaces, i32 0, i32 0), i32 [[C:%.*]], i32 3)
150151 ; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8* [[DST]], null
151152 ; CHECK-NEXT: ret i1 [[CMP]]
152153 ;
157158
158159 define i1 @test13(i32 %C) {
159160 ; CHECK-LABEL: @test13(
160 ; CHECK-NEXT: [[MEMCHR_BOUNDS:%.*]] = icmp ult i32 %C, 32
161 ; CHECK-NEXT: [[TMP1:%.*]] = shl i32 1, %C
162 ; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP1]], -2147483647
163 ; CHECK-NEXT: [[MEMCHR_BITS:%.*]] = icmp ne i32 [[TMP2]], 0
164 ; CHECK-NEXT: [[MEMCHR:%.*]] = and i1 [[MEMCHR:%.*]].bounds, [[MEMCHR:%.*]].bits
161 ; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[C:%.*]], 255
162 ; CHECK-NEXT: [[MEMCHR_BOUNDS:%.*]] = icmp ult i32 [[TMP1]], 32
163 ; CHECK-NEXT: [[TMP2:%.*]] = shl i32 1, [[TMP1]]
164 ; CHECK-NEXT: [[TMP3:%.*]] = and i32 [[TMP2]], -2147483647
165 ; CHECK-NEXT: [[MEMCHR_BITS:%.*]] = icmp ne i32 [[TMP3]], 0
166 ; CHECK-NEXT: [[MEMCHR:%.*]] = and i1 [[MEMCHR_BOUNDS]], [[MEMCHR_BITS]]
165167 ; CHECK-NEXT: ret i1 [[MEMCHR]]
166168 ;
167169 %dst = call i8* @memchr(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @single, i64 0, i64 0), i32 %C, i32 2)
171173
172174 define i1 @test14(i32 %C) {
173175 ; CHECK-LABEL: @test14(
174 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 %C, 31
175 ; CHECK-NEXT: ret i1 [[TMP1]]
176 ; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[C:%.*]], 255
177 ; CHECK-NEXT: [[MEMCHR_BITS:%.*]] = icmp eq i32 [[TMP1]], 31
178 ; CHECK-NEXT: ret i1 [[MEMCHR_BITS]]
176179 ;
177180 %dst = call i8* @memchr(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @single, i64 0, i64 0), i32 %C, i32 1)
178181 %cmp = icmp ne i8* %dst, null
181184
182185 define i1 @test15(i32 %C) {
183186 ; CHECK-LABEL: @test15(
184 ; CHECK-NEXT: [[DST:%.*]] = call i8* @memchr(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @negative, i32 0, i32 0), i32 %C, i32 3)
187 ; CHECK-NEXT: [[DST:%.*]] = call i8* @memchr(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @negative, i32 0, i32 0), i32 [[C:%.*]], i32 3)
185188 ; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8* [[DST]], null
186189 ; CHECK-NEXT: ret i1 [[CMP]]
187190 ;
8181 define i1 @test_simplify7(i32 %C) {
8282 ; CHECK-LABEL: @test_simplify7
8383 ; CHECK-NEXT: [[TRUNC:%.*]] = trunc i32 %C to i16
84 ; CHECK-NEXT: %memchr.bounds = icmp ult i16 [[TRUNC]], 16
85 ; CHECK-NEXT: [[SHL:%.*]] = shl i16 1, [[TRUNC]]
84 ; CHECK-NEXT: [[TRUNC_AND:%.*]] = and i16 [[TRUNC]], 255
85 ; CHECK-NEXT: %memchr.bounds = icmp ult i16 [[TRUNC_AND]], 16
86 ; CHECK-NEXT: [[SHL:%.*]] = shl i16 1, [[TRUNC_AND]]
8687 ; CHECK-NEXT: [[AND:%.*]] = and i16 [[SHL]], 9217
8788 ; CHECK-NEXT: %memchr.bits = icmp ne i16 [[AND]], 0
8889 ; CHECK-NEXT: %memchr1 = and i1 %memchr.bounds, %memchr.bits