1 ; Test that the memchr library call simplifier works correctly.
2 ; RUN: opt < %s -instcombine -S | FileCheck %s
4 target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128-n8:16:32"
6 @hello = constant [14 x i8] c"hello world\5Cn\00"
7 @hellonull = constant [14 x i8] c"hello\00world\5Cn\00"
8 @null = constant [1 x i8] zeroinitializer
9 @newlines = constant [3 x i8] c"\0D\0A\00"
10 @single = constant [2 x i8] c"\1F\00"
11 @spaces = constant [4 x i8] c" \0D\0A\00"
12 @negative = constant [3 x i8] c"\FF\FE\00"
13 @chp = global i8* zeroinitializer
15 declare i8* @memchr(i8*, i32, i32)
17 define void @test1() {
19 ; CHECK: store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @hello, i32 0, i32 6)
20 ; CHECK-NOT: call i8* @memchr
23 %str = getelementptr [14 x i8], [14 x i8]* @hello, i32 0, i32 0
24 %dst = call i8* @memchr(i8* %str, i32 119, i32 14)
25 store i8* %dst, i8** @chp
29 define void @test2() {
31 ; CHECK: store i8* null, i8** @chp, align 4
32 ; CHECK-NOT: call i8* @memchr
35 %str = getelementptr [1 x i8], [1 x i8]* @null, i32 0, i32 0
36 %dst = call i8* @memchr(i8* %str, i32 119, i32 1)
37 store i8* %dst, i8** @chp
41 define void @test3() {
43 ; CHECK: store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @hello, i32 0, i32 13)
44 ; CHECK-NOT: call i8* @memchr
47 %src = getelementptr [14 x i8], [14 x i8]* @hello, i32 0, i32 0
48 %dst = call i8* @memchr(i8* %src, i32 0, i32 14)
49 store i8* %dst, i8** @chp
53 define void @test4(i32 %chr) {
55 ; CHECK: call i8* @memchr
56 ; CHECK-NOT: call i8* @memchr
59 %src = getelementptr [14 x i8], [14 x i8]* @hello, i32 0, i32 0
60 %dst = call i8* @memchr(i8* %src, i32 %chr, i32 14)
61 store i8* %dst, i8** @chp
65 define void @test5() {
67 ; CHECK: store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @hello, i32 0, i32 13)
68 ; CHECK-NOT: call i8* @memchr
71 %src = getelementptr [14 x i8], [14 x i8]* @hello, i32 0, i32 0
72 %dst = call i8* @memchr(i8* %src, i32 65280, i32 14)
73 store i8* %dst, i8** @chp
77 define void @test6() {
79 ; CHECK: store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @hello, i32 0, i32 6)
80 ; CHECK-NOT: call i8* @memchr
83 %src = getelementptr [14 x i8], [14 x i8]* @hello, i32 0, i32 0
84 ; Overflow, but we still find the right thing.
85 %dst = call i8* @memchr(i8* %src, i32 119, i32 100)
86 store i8* %dst, i8** @chp
90 define void @test7() {
92 ; CHECK: store i8* null, i8** @chp, align 4
93 ; CHECK-NOT: call i8* @memchr
96 %src = getelementptr [14 x i8], [14 x i8]* @hello, i32 0, i32 0
98 %dst = call i8* @memchr(i8* %src, i32 120, i32 100)
99 store i8* %dst, i8** @chp
103 define void @test8() {
104 ; CHECK-LABEL: @test8
105 ; CHECK: store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @hellonull, i32 0, i32 6)
106 ; CHECK-NOT: call i8* @memchr
109 %str = getelementptr [14 x i8], [14 x i8]* @hellonull, i32 0, i32 0
110 %dst = call i8* @memchr(i8* %str, i32 119, i32 14)
111 store i8* %dst, i8** @chp
115 define void @test9() {
116 ; CHECK-LABEL: @test9
117 ; CHECK: store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @hellonull, i32 0, i32 6)
118 ; CHECK-NOT: call i8* @memchr
121 %str = getelementptr [14 x i8], [14 x i8]* @hellonull, i32 0, i32 2
122 %dst = call i8* @memchr(i8* %str, i32 119, i32 12)
123 store i8* %dst, i8** @chp
127 define void @test10() {
128 ; CHECK-LABEL: @test10
129 ; CHECK: store i8* null, i8** @chp, align 4
130 ; CHECK-NOT: call i8* @memchr
133 %str = getelementptr [14 x i8], [14 x i8]* @hello, i32 0, i32 0
134 %dst = call i8* @memchr(i8* %str, i32 119, i32 6)
135 store i8* %dst, i8** @chp
139 ; Check transformation memchr("\r\n", C, 2) != nullptr -> (C & 9216) != 0
140 define i1 @test11(i32 %C) {
141 ; CHECK-LABEL: @test11
142 ; CHECK-NEXT: [[TRUNC:%.*]] = trunc i32 %C to i16
143 ; CHECK-NEXT: %memchr.bounds = icmp ult i16 [[TRUNC]], 16
144 ; CHECK-NEXT: [[SHL:%.*]] = shl i16 1, [[TRUNC]]
145 ; CHECK-NEXT: [[AND:%.*]] = and i16 [[SHL]], 9216
146 ; CHECK-NEXT: %memchr.bits = icmp ne i16 [[AND]], 0
147 ; CHECK-NEXT: %memchr = and i1 %memchr.bounds, %memchr.bits
148 ; CHECK-NEXT: ret i1 %memchr
150 %dst = call i8* @memchr(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @newlines, i64 0, i64 0), i32 %C, i32 2)
151 %cmp = icmp ne i8* %dst, null
156 define i1 @test12(i32 %C) {
157 ; CHECK-LABEL: @test12
158 ; CHECK-NEXT: %dst = call i8* @memchr(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @spaces, i32 0, i32 0), i32 %C, i32 3)
159 ; CHECK-NEXT: %cmp = icmp ne i8* %dst, null
160 ; CHECK-NEXT: ret i1 %cmp
162 %dst = call i8* @memchr(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @spaces, i64 0, i64 0), i32 %C, i32 3)
163 %cmp = icmp ne i8* %dst, null
167 define i1 @test13(i32 %C) {
168 ; CHECK-LABEL: @test13
169 ; CHECK-NEXT: %memchr.bounds = icmp ult i32 %C, 32
170 ; CHECK-NEXT: [[SHL:%.*]] = shl i32 1, %C
171 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[SHL]], -2147483647
172 ; CHECK-NEXT: %memchr.bits = icmp ne i32 [[AND]], 0
173 ; CHECK-NEXT: %memchr = and i1 %memchr.bounds, %memchr.bits
174 ; CHECK-NEXT: ret i1 %memchr
176 %dst = call i8* @memchr(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @single, i64 0, i64 0), i32 %C, i32 2)
177 %cmp = icmp ne i8* %dst, null
181 define i1 @test14(i32 %C) {
182 ; CHECK-LABEL: @test14
183 ; CHECK-NEXT: icmp eq i32 %C, 31
186 %dst = call i8* @memchr(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @single, i64 0, i64 0), i32 %C, i32 1)
187 %cmp = icmp ne i8* %dst, null
191 define i1 @test15(i32 %C) {
192 ; CHECK-LABEL: @test15
193 ; CHECK-NEXT: %dst = call i8* @memchr(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @negative, i32 0, i32 0), i32 %C, i32 3)
194 ; CHECK-NEXT: %cmp = icmp ne i8* %dst, null
195 ; CHECK-NEXT: ret i1 %cmp
197 %dst = call i8* @memchr(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @negative, i64 0, i64 0), i32 %C, i32 3)
198 %cmp = icmp ne i8* %dst, null