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 @chp = global i8* zeroinitializer
14 declare i8* @memchr(i8*, i32, i32)
16 define void @test1() {
18 ; CHECK: store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @hello, i32 0, i32 6)
19 ; CHECK-NOT: call i8* @memchr
22 %str = getelementptr [14 x i8], [14 x i8]* @hello, i32 0, i32 0
23 %dst = call i8* @memchr(i8* %str, i32 119, i32 14)
24 store i8* %dst, i8** @chp
28 define void @test2() {
30 ; CHECK: store i8* null, i8** @chp, align 4
31 ; CHECK-NOT: call i8* @memchr
34 %str = getelementptr [1 x i8], [1 x i8]* @null, i32 0, i32 0
35 %dst = call i8* @memchr(i8* %str, i32 119, i32 1)
36 store i8* %dst, i8** @chp
40 define void @test3() {
42 ; CHECK: store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @hello, i32 0, i32 13)
43 ; CHECK-NOT: call i8* @memchr
46 %src = getelementptr [14 x i8], [14 x i8]* @hello, i32 0, i32 0
47 %dst = call i8* @memchr(i8* %src, i32 0, i32 14)
48 store i8* %dst, i8** @chp
52 define void @test4(i32 %chr) {
54 ; CHECK: call i8* @memchr
55 ; CHECK-NOT: call i8* @memchr
58 %src = getelementptr [14 x i8], [14 x i8]* @hello, i32 0, i32 0
59 %dst = call i8* @memchr(i8* %src, i32 %chr, i32 14)
60 store i8* %dst, i8** @chp
64 define void @test5() {
66 ; CHECK: store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @hello, i32 0, i32 13)
67 ; CHECK-NOT: call i8* @memchr
70 %src = getelementptr [14 x i8], [14 x i8]* @hello, i32 0, i32 0
71 %dst = call i8* @memchr(i8* %src, i32 65280, i32 14)
72 store i8* %dst, i8** @chp
76 define void @test6() {
78 ; CHECK: store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @hello, i32 0, i32 6)
79 ; CHECK-NOT: call i8* @memchr
82 %src = getelementptr [14 x i8], [14 x i8]* @hello, i32 0, i32 0
83 ; Overflow, but we still find the right thing.
84 %dst = call i8* @memchr(i8* %src, i32 119, i32 100)
85 store i8* %dst, i8** @chp
89 define void @test7() {
91 ; CHECK: store i8* null, i8** @chp, align 4
92 ; CHECK-NOT: call i8* @memchr
95 %src = getelementptr [14 x i8], [14 x i8]* @hello, i32 0, i32 0
97 %dst = call i8* @memchr(i8* %src, i32 120, i32 100)
98 store i8* %dst, i8** @chp
102 define void @test8() {
103 ; CHECK-LABEL: @test8
104 ; CHECK: store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @hellonull, i32 0, i32 6)
105 ; CHECK-NOT: call i8* @memchr
108 %str = getelementptr [14 x i8], [14 x i8]* @hellonull, i32 0, i32 0
109 %dst = call i8* @memchr(i8* %str, i32 119, i32 14)
110 store i8* %dst, i8** @chp
114 define void @test9() {
115 ; CHECK-LABEL: @test9
116 ; CHECK: store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @hellonull, i32 0, i32 6)
117 ; CHECK-NOT: call i8* @memchr
120 %str = getelementptr [14 x i8], [14 x i8]* @hellonull, i32 0, i32 2
121 %dst = call i8* @memchr(i8* %str, i32 119, i32 12)
122 store i8* %dst, i8** @chp
126 define void @test10() {
127 ; CHECK-LABEL: @test10
128 ; CHECK: store i8* null, i8** @chp, align 4
129 ; CHECK-NOT: call i8* @memchr
132 %str = getelementptr [14 x i8], [14 x i8]* @hello, i32 0, i32 0
133 %dst = call i8* @memchr(i8* %str, i32 119, i32 6)
134 store i8* %dst, i8** @chp
138 ; Check transformation memchr("\r\n", C, 2) != nullptr -> (C & 9216) != 0
139 define i1 @test11(i32 %C) {
140 ; CHECK-LABEL: @test11
141 ; CHECK-NEXT: [[TRUNC:%.*]] = trunc i32 %C to i16
142 ; CHECK-NEXT: %memchr.bounds = icmp ult i16 [[TRUNC]], 16
143 ; CHECK-NEXT: [[SHL:%.*]] = shl i16 1, [[TRUNC]]
144 ; CHECK-NEXT: [[AND:%.*]] = and i16 [[SHL]], 9216
145 ; CHECK-NEXT: %memchr.bits = icmp ne i16 [[AND]], 0
146 ; CHECK-NEXT: %memchr = and i1 %memchr.bounds, %memchr.bits
147 ; CHECK-NEXT: ret i1 %memchr
149 %dst = call i8* @memchr(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @newlines, i64 0, i64 0), i32 %C, i32 2)
150 %cmp = icmp ne i8* %dst, null
155 define i1 @test12(i32 %C) {
156 ; CHECK-LABEL: @test12
157 ; CHECK-NEXT: %dst = call i8* @memchr(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @spaces, i32 0, i32 0), i32 %C, i32 3)
158 ; CHECK-NEXT: %cmp = icmp ne i8* %dst, null
159 ; CHECK-NEXT: ret i1 %cmp
161 %dst = call i8* @memchr(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @spaces, i64 0, i64 0), i32 %C, i32 3)
162 %cmp = icmp ne i8* %dst, null
166 define i1 @test13(i32 %C) {
167 ; CHECK-LABEL: @test13
168 ; CHECK-NEXT: %memchr.bounds = icmp ult i32 %C, 32
169 ; CHECK-NEXT: [[SHL:%.*]] = shl i32 1, %C
170 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[SHL]], -2147483647
171 ; CHECK-NEXT: %memchr.bits = icmp ne i32 [[AND]], 0
172 ; CHECK-NEXT: %memchr = and i1 %memchr.bounds, %memchr.bits
173 ; CHECK-NEXT: ret i1 %memchr
175 %dst = call i8* @memchr(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @single, i64 0, i64 0), i32 %C, i32 2)
176 %cmp = icmp ne i8* %dst, null
180 define i1 @test14(i32 %C) {
181 ; CHECK-LABEL: @test14
182 ; CHECK-NEXT: icmp eq i32 %C, 31
185 %dst = call i8* @memchr(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @single, i64 0, i64 0), i32 %C, i32 1)
186 %cmp = icmp ne i8* %dst, null