1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; Test that the sprintf library call simplifier works correctly.
3;
4; RUN: opt < %s -passes=instcombine -S | FileCheck %s
5; RUN: opt < %s -mtriple xcore-xmos-elf -passes=instcombine -S | FileCheck %s -check-prefixes=CHECK,CHECK-IPRINTF,WITHSTPCPY
6; RUN: opt < %s -mtriple=i386-pc-windows-msvc -passes=instcombine -S | FileCheck %s --check-prefixes=CHECK,WIN
7; RUN: opt < %s -mtriple=i386-mingw32 -passes=instcombine -S | FileCheck %s --check-prefixes=CHECK,WIN,NOSTPCPY
8; RUN: opt < %s -mtriple=armv7-none-linux-android16 -passes=instcombine -S | FileCheck %s --check-prefixes=CHECK,NOSTPCPY
9; RUN: opt < %s -mtriple=armv7-none-linux-android21 -passes=instcombine -S | FileCheck %s --check-prefixes=CHECK,WITHSTPCPY
10; RUN: opt < %s -mtriple=x86_64-scei-ps4 -passes=instcombine -S | FileCheck %s --check-prefixes=CHECK,NOSTPCPY
11
12target 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"
13
14@hello_world = constant [13 x i8] c"hello world\0A\00"
15@null = constant [1 x i8] zeroinitializer
16@null_hello = constant [7 x i8] c"\00hello\00"
17@h = constant [2 x i8] c"h\00"
18@percent_c = constant [3 x i8] c"%c\00"
19@percent_d = constant [3 x i8] c"%d\00"
20@percent_f = constant [3 x i8] c"%f\00"
21@percent_s = constant [3 x i8] c"%s\00"
22
23declare i32 @sprintf(i8*, i8*, ...)
24
25; Check sprintf(dst, fmt) -> llvm.memcpy(str, fmt, strlen(fmt) + 1, 1).
26
27define void @test_simplify1(i8* %dst) {
28; CHECK-LABEL: @test_simplify1(
29; CHECK-NEXT:    call void @llvm.memcpy.p0i8.p0i8.i32(i8* noundef nonnull align 1 dereferenceable(13) [[DST:%.*]], i8* noundef nonnull align 1 dereferenceable(13) getelementptr inbounds ([13 x i8], [13 x i8]* @hello_world, i32 0, i32 0), i32 13, i1 false)
30; CHECK-NEXT:    ret void
31;
32  %fmt = getelementptr [13 x i8], [13 x i8]* @hello_world, i32 0, i32 0
33  call i32 (i8*, i8*, ...) @sprintf(i8* %dst, i8* %fmt)
34  ret void
35}
36
37define void @test_simplify2(i8* %dst) {
38; CHECK-LABEL: @test_simplify2(
39; CHECK-NEXT:    store i8 0, i8* [[DST:%.*]], align 1
40; CHECK-NEXT:    ret void
41;
42  %fmt = getelementptr [1 x i8], [1 x i8]* @null, i32 0, i32 0
43  call i32 (i8*, i8*, ...) @sprintf(i8* %dst, i8* %fmt)
44  ret void
45}
46
47define void @test_simplify3(i8* %dst) {
48; CHECK-LABEL: @test_simplify3(
49; CHECK-NEXT:    store i8 0, i8* [[DST:%.*]], align 1
50; CHECK-NEXT:    ret void
51;
52  %fmt = getelementptr [7 x i8], [7 x i8]* @null_hello, i32 0, i32 0
53  call i32 (i8*, i8*, ...) @sprintf(i8* %dst, i8* %fmt)
54  ret void
55}
56
57; Check sprintf(dst, "%c", chr) -> *(i8*)dst = chr; *((i8*)dst + 1) = 0.
58
59define void @test_simplify4(i8* %dst) {
60; CHECK-LABEL: @test_simplify4(
61; CHECK-NEXT:    store i8 104, i8* [[DST:%.*]], align 1
62; CHECK-NEXT:    [[NUL:%.*]] = getelementptr inbounds i8, i8* [[DST]], i32 1
63; CHECK-NEXT:    store i8 0, i8* [[NUL]], align 1
64; CHECK-NEXT:    ret void
65;
66  %fmt = getelementptr [3 x i8], [3 x i8]* @percent_c, i32 0, i32 0
67  call i32 (i8*, i8*, ...) @sprintf(i8* %dst, i8* %fmt, i8 104)
68  ret void
69}
70
71; Check sprintf(dst, "%s", str) -> strcpy(dst, "%s", str) if result is unused.
72
73define void @test_simplify5(i8* %dst, i8* %str) {
74; CHECK-LABEL: @test_simplify5(
75; CHECK-NEXT:    [[STRCPY:%.*]] = call i8* @strcpy(i8* noundef nonnull dereferenceable(1) [[DST:%.*]], i8* noundef nonnull dereferenceable(1) [[STR:%.*]])
76; CHECK-NEXT:    ret void
77;
78  %fmt = getelementptr [3 x i8], [3 x i8]* @percent_s, i32 0, i32 0
79  call i32 (i8*, i8*, ...) @sprintf(i8* %dst, i8* %fmt, i8* %str)
80  ret void
81}
82
83; Check sprintf(dst, format, ...) -> siprintf(str, format, ...) if no floating.
84
85define void @test_simplify6(i8* %dst) {
86; CHECK-IPRINTF-LABEL: @test_simplify6(
87; CHECK-IPRINTF-NEXT:    [[TMP1:%.*]] = call i32 (i8*, i8*, ...) @siprintf(i8* [[DST:%.*]], i8* getelementptr inbounds ([3 x i8], [3 x i8]* @percent_d, i32 0, i32 0), i32 187)
88; CHECK-IPRINTF-NEXT:    ret void
89;
90; WIN-LABEL: @test_simplify6(
91; WIN-NEXT:    [[TMP1:%.*]] = call i32 (i8*, i8*, ...) @sprintf(i8* noundef nonnull dereferenceable(1) [[DST:%.*]], i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @percent_d, i32 0, i32 0), i32 187)
92; WIN-NEXT:    ret void
93;
94  %fmt = getelementptr [3 x i8], [3 x i8]* @percent_d, i32 0, i32 0
95  call i32 (i8*, i8*, ...) @sprintf(i8* %dst, i8* %fmt, i32 187)
96  ret void
97}
98
99; Check sprintf(dst, "%s", str) -> llvm.memcpy(dest, str, strlen(str) + 1, 1).
100
101define i32 @test_simplify7(i8* %dst, i8* %str) {
102; WITHSTPCPY-LABEL: @test_simplify7(
103; WITHSTPCPY-NEXT:    [[STPCPY:%.*]] = call i8* @stpcpy(i8* [[DST:%.*]], i8* [[STR:%.*]])
104; WITHSTPCPY-NEXT:    [[TMP1:%.*]] = ptrtoint i8* [[STPCPY]] to i32
105; WITHSTPCPY-NEXT:    [[TMP2:%.*]] = ptrtoint i8* [[DST]] to i32
106; WITHSTPCPY-NEXT:    [[TMP3:%.*]] = sub i32 [[TMP1]], [[TMP2]]
107; WITHSTPCPY-NEXT:    ret i32 [[TMP3]]
108;
109; NOSTPCPY-LABEL: @test_simplify7(
110; NOSTPCPY-NEXT:    [[STRLEN:%.*]] = call i32 @strlen(i8* noundef nonnull dereferenceable(1) [[STR:%.*]])
111; NOSTPCPY-NEXT:    [[LENINC:%.*]] = add i32 [[STRLEN]], 1
112; NOSTPCPY-NEXT:    call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 1 [[DST:%.*]], i8* align 1 [[STR]], i32 [[LENINC]], i1 false)
113; NOSTPCPY-NEXT:    ret i32 [[STRLEN]]
114;
115  %fmt = getelementptr [3 x i8], [3 x i8]* @percent_s, i32 0, i32 0
116  %r = call i32 (i8*, i8*, ...) @sprintf(i8* %dst, i8* %fmt, i8* %str)
117  ret i32 %r
118}
119
120; Check sprintf(dst, "%s", str) -> llvm.memcpy(dest, str, strlen(str) + 1, 1).
121define i32 @test_simplify8(i8* %dst) {
122; CHECK-LABEL: @test_simplify8(
123; CHECK-NEXT:    call void @llvm.memcpy.p0i8.p0i8.i32(i8* noundef nonnull align 1 dereferenceable(13) [[DST:%.*]], i8* noundef nonnull align 1 dereferenceable(13) getelementptr inbounds ([13 x i8], [13 x i8]* @hello_world, i32 0, i32 0), i32 13, i1 false)
124; CHECK-NEXT:    ret i32 12
125;
126  %fmt = getelementptr [3 x i8], [3 x i8]* @percent_s, i32 0, i32 0
127  %str = getelementptr [13 x i8], [13 x i8]* @hello_world, i32 0, i32 0
128  %r = call i32 (i8*, i8*, ...) @sprintf(i8* %dst, i8* %fmt, i8* %str)
129  ret i32 %r
130}
131
132; Check sprintf(dst, "%s", str) -> stpcpy(dest, str) - dest
133
134define i32 @test_simplify9(i8* %dst, i8* %str) {
135; CHECK-IPRINTF-LABEL: @test_simplify9(
136; CHECK-IPRINTF-NEXT:    [[STPCPY:%.*]] = call i8* @stpcpy(i8* [[DST:%.*]], i8* [[STR:%.*]])
137; CHECK-IPRINTF-NEXT:    [[TMP1:%.*]] = ptrtoint i8* [[STPCPY]] to i32
138; CHECK-IPRINTF-NEXT:    [[TMP2:%.*]] = ptrtoint i8* [[DST]] to i32
139; CHECK-IPRINTF-NEXT:    [[TMP3:%.*]] = sub i32 [[TMP1]], [[TMP2]]
140; CHECK-IPRINTF-NEXT:    ret i32 [[TMP3]]
141;
142; WIN-LABEL: @test_simplify9(
143; WIN-NEXT:    [[STRLEN:%.*]] = call i32 @strlen(i8* noundef nonnull dereferenceable(1) [[STR:%.*]])
144; WIN-NEXT:    [[LENINC:%.*]] = add i32 [[STRLEN]], 1
145; WIN-NEXT:    call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 1 [[DST:%.*]], i8* align 1 [[STR]], i32 [[LENINC]], i1 false)
146; WIN-NEXT:    ret i32 [[STRLEN]]
147;
148  %fmt = getelementptr [3 x i8], [3 x i8]* @percent_s, i32 0, i32 0
149  %r = call i32 (i8*, i8*, ...) @sprintf(i8* %dst, i8* %fmt, i8* %str)
150  ret i32 %r
151}
152
153define void @test_no_simplify1(i8* %dst) {
154; CHECK-LABEL: @test_no_simplify1(
155; CHECK-NEXT:    [[TMP1:%.*]] = call i32 (i8*, i8*, ...) @sprintf(i8* noundef nonnull dereferenceable(1) [[DST:%.*]], i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @percent_f, i32 0, i32 0), double 1.870000e+00)
156; CHECK-NEXT:    ret void
157;
158  %fmt = getelementptr [3 x i8], [3 x i8]* @percent_f, i32 0, i32 0
159  call i32 (i8*, i8*, ...) @sprintf(i8* %dst, i8* %fmt, double 1.87)
160  ret void
161}
162
163define void @test_no_simplify2(i8* %dst, i8* %fmt, double %d) {
164; CHECK-LABEL: @test_no_simplify2(
165; CHECK-NEXT:    [[TMP1:%.*]] = call i32 (i8*, i8*, ...) @sprintf(i8* noundef nonnull dereferenceable(1) [[DST:%.*]], i8* noundef nonnull dereferenceable(1) [[FMT:%.*]], double [[D:%.*]])
166; CHECK-NEXT:    ret void
167;
168  call i32 (i8*, i8*, ...) @sprintf(i8* %dst, i8* %fmt, double %d)
169  ret void
170}
171
172define i32 @test_no_simplify3(i8* %dst, i8* %str) minsize {
173; CHECK-IPRINTF-LABEL: @test_no_simplify3(
174; CHECK-IPRINTF-NEXT:    [[STPCPY:%.*]] = call i8* @stpcpy(i8* [[DST:%.*]], i8* [[STR:%.*]])
175; CHECK-IPRINTF-NEXT:    [[TMP1:%.*]] = ptrtoint i8* [[STPCPY]] to i32
176; CHECK-IPRINTF-NEXT:    [[TMP2:%.*]] = ptrtoint i8* [[DST]] to i32
177; CHECK-IPRINTF-NEXT:    [[TMP3:%.*]] = sub i32 [[TMP1]], [[TMP2]]
178; CHECK-IPRINTF-NEXT:    ret i32 [[TMP3]]
179;
180; WIN-LABEL: @test_no_simplify3(
181; WIN-NEXT:    [[R:%.*]] = call i32 (i8*, i8*, ...) @sprintf(i8* noundef nonnull dereferenceable(1) [[DST:%.*]], i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @percent_s, i32 0, i32 0), i8* [[STR:%.*]])
182; WIN-NEXT:    ret i32 [[R]]
183;
184  %fmt = getelementptr [3 x i8], [3 x i8]* @percent_s, i32 0, i32 0
185  %r = call i32 (i8*, i8*, ...) @sprintf(i8* %dst, i8* %fmt, i8* %str)
186  ret i32 %r
187}
188