1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; Test that the sprintf library call simplifier works correctly.
4 ; RUN: opt < %s -instcombine -S | FileCheck %s
5 ; RUN: opt < %s -mtriple xcore-xmos-elf -instcombine -S | FileCheck %s -check-prefixes=CHECK,CHECK-IPRINTF,WITHSTPCPY
6 ; RUN: opt < %s -mtriple=i386-pc-windows-msvc -instcombine -S | FileCheck %s --check-prefixes=CHECK,WIN
7 ; RUN: opt < %s -mtriple=i386-mingw32 -instcombine -S | FileCheck %s --check-prefixes=CHECK,WIN,NOSTPCPY
8 ; RUN: opt < %s -mtriple=armv7-none-linux-android16 -instcombine -S | FileCheck %s --check-prefixes=CHECK,NOSTPCPY
9 ; RUN: opt < %s -mtriple=armv7-none-linux-android21 -instcombine -S | FileCheck %s --check-prefixes=CHECK,WITHSTPCPY
10 ; RUN: opt < %s -mtriple=x86_64-scei-ps4 -instcombine -S | FileCheck %s --check-prefixes=CHECK,NOSTPCPY
12 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"
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"
23 declare i32 @sprintf(i8*, i8*, ...)
25 ; Check sprintf(dst, fmt) -> llvm.memcpy(str, fmt, strlen(fmt) + 1, 1).
27 define 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
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)
37 define 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
42 %fmt = getelementptr [1 x i8], [1 x i8]* @null, i32 0, i32 0
43 call i32 (i8*, i8*, ...) @sprintf(i8* %dst, i8* %fmt)
47 define 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
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)
57 ; Check sprintf(dst, "%c", chr) -> *(i8*)dst = chr; *((i8*)dst + 1) = 0.
59 define 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 i8, i8* [[DST]], i32 1
63 ; CHECK-NEXT: store i8 0, i8* [[NUL]], align 1
64 ; CHECK-NEXT: ret void
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)
71 ; Check sprintf(dst, "%s", str) -> strcpy(dst, "%s", str) if result is unused.
73 define 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
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)
83 ; Check sprintf(dst, format, ...) -> siprintf(str, format, ...) if no floating.
85 define 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
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)
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)
99 ; Check sprintf(dst, "%s", str) -> llvm.memcpy(dest, str, strlen(str) + 1, 1).
101 define 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]]
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]]
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)
120 ; Check sprintf(dst, "%s", str) -> llvm.memcpy(dest, str, strlen(str) + 1, 1).
121 define 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
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)
132 ; Check sprintf(dst, "%s", str) -> stpcpy(dest, str) - dest
134 define 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]]
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]]
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)
153 define 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
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)
163 define 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
168 call i32 (i8*, i8*, ...) @sprintf(i8* %dst, i8* %fmt, double %d)
172 define 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]]
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]]
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)