• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1; Test that the strchr library call simplifier works correctly.
2; RUN: opt < %s -instcombine -S | FileCheck %s
3
4target 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"
5
6@hello = constant [14 x i8] c"hello world\5Cn\00"
7@null = constant [1 x i8] zeroinitializer
8@chp = global i8* zeroinitializer
9
10declare i8* @strchr(i8*, i32)
11
12define void @test_simplify1() {
13; CHECK: store i8* getelementptr inbounds ([14 x i8]* @hello, i32 0, i32 6)
14; CHECK-NOT: call i8* @strchr
15; CHECK: ret void
16
17  %str = getelementptr [14 x i8]* @hello, i32 0, i32 0
18  %dst = call i8* @strchr(i8* %str, i32 119)
19  store i8* %dst, i8** @chp
20  ret void
21}
22
23define void @test_simplify2() {
24; CHECK: store i8* null, i8** @chp, align 4
25; CHECK-NOT: call i8* @strchr
26; CHECK: ret void
27
28  %str = getelementptr [1 x i8]* @null, i32 0, i32 0
29  %dst = call i8* @strchr(i8* %str, i32 119)
30  store i8* %dst, i8** @chp
31  ret void
32}
33
34define void @test_simplify3() {
35; CHECK: store i8* getelementptr inbounds ([14 x i8]* @hello, i32 0, i32 13)
36; CHECK-NOT: call i8* @strchr
37; CHECK: ret void
38
39  %src = getelementptr [14 x i8]* @hello, i32 0, i32 0
40  %dst = call i8* @strchr(i8* %src, i32 0)
41  store i8* %dst, i8** @chp
42  ret void
43}
44
45define void @test_simplify4(i32 %chr) {
46; CHECK: call i8* @memchr
47; CHECK-NOT: call i8* @strchr
48; CHECK: ret void
49
50  %src = getelementptr [14 x i8]* @hello, i32 0, i32 0
51  %dst = call i8* @strchr(i8* %src, i32 %chr)
52  store i8* %dst, i8** @chp
53  ret void
54}
55
56define void @test_simplify5() {
57; CHECK: store i8* getelementptr inbounds ([14 x i8]* @hello, i32 0, i32 13)
58; CHECK-NOT: call i8* @strchr
59; CHECK: ret void
60
61  %src = getelementptr [14 x i8]* @hello, i32 0, i32 0
62  %dst = call i8* @strchr(i8* %src, i32 65280)
63  store i8* %dst, i8** @chp
64  ret void
65}
66
67; Check transformation strchr(p, 0) -> p + strlen(p)
68define void @test_simplify6(i8* %str) {
69; CHECK: %strlen = call i32 @strlen(i8* %str)
70; CHECK-NOT: call i8* @strchr
71; CHECK: %strchr = getelementptr i8* %str, i32 %strlen
72; CHECK: store i8* %strchr, i8** @chp, align 4
73; CHECK: ret void
74
75  %dst = call i8* @strchr(i8* %str, i32 0)
76  store i8* %dst, i8** @chp
77  ret void
78}
79