1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
4 @.str = private unnamed_addr constant [4 x i8] c"str\00", align 1
5 @.str.1 = private unnamed_addr constant [3 x i8] c"%%\00", align 1
6 @.str.2 = private unnamed_addr constant [3 x i8] c"%c\00", align 1
7 @.str.3 = private unnamed_addr constant [3 x i8] c"%s\00", align 1
8 @.str.4 = private unnamed_addr constant [1 x i8] zeroinitializer, align 1
11 declare i32 @snprintf(ptr, i64, ptr, ...) #1
13 define void @test_not_const_fmt(ptr %buf, ptr %fmt) #0 {
14 ; CHECK-LABEL: @test_not_const_fmt(
15 ; CHECK-NEXT: [[CALL:%.*]] = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef nonnull dereferenceable(1) [[BUF:%.*]], i64 32, ptr [[FMT:%.*]])
16 ; CHECK-NEXT: ret void
18 %call = call i32 (ptr, i64, ptr, ...) @snprintf(ptr %buf, i64 32, ptr %fmt) #2
22 ; size is '0', do not add nonnull attribute
23 define void @test_not_const_fmt_zero_size_return_value(ptr %buf, ptr %fmt) #0 {
24 ; CHECK-LABEL: @test_not_const_fmt_zero_size_return_value(
25 ; CHECK-NEXT: [[CALL:%.*]] = call i32 (ptr, i64, ptr, ...) @snprintf(ptr [[BUF:%.*]], i64 0, ptr [[FMT:%.*]])
26 ; CHECK-NEXT: ret void
28 %call = call i32 (ptr, i64, ptr, ...) @snprintf(ptr %buf, i64 0, ptr %fmt) #2
32 define void @test_not_const_size(ptr %buf, i64 %size) #0 {
33 ; CHECK-LABEL: @test_not_const_size(
34 ; CHECK-NEXT: [[CALL:%.*]] = call i32 (ptr, i64, ptr, ...) @snprintf(ptr [[BUF:%.*]], i64 [[SIZE:%.*]], ptr nonnull @.str)
35 ; CHECK-NEXT: ret void
37 %call = call i32 (ptr, i64, ptr, ...) @snprintf(ptr %buf, i64 %size, ptr @.str) #2
42 define i32 @test_return_value(ptr %buf) #0 {
43 ; CHECK-LABEL: @test_return_value(
44 ; CHECK-NEXT: ret i32 3
46 %call = call i32 (ptr, i64, ptr, ...) @snprintf(ptr %buf, i64 0, ptr @.str) #2
50 define void @test_percentage(ptr %buf) #0 {
51 ; CHECK-LABEL: @test_percentage(
52 ; CHECK-NEXT: [[CALL:%.*]] = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef nonnull dereferenceable(1) [[BUF:%.*]], i64 32, ptr nonnull @.str.1)
53 ; CHECK-NEXT: ret void
55 %call = call i32 (ptr, i64, ptr, ...) @snprintf(ptr %buf, i64 32, ptr @.str.1) #2
59 define i32 @test_null_buf_return_value() #0 {
60 ; CHECK-LABEL: @test_null_buf_return_value(
61 ; CHECK-NEXT: ret i32 3
63 %call = call i32 (ptr, i64, ptr, ...) @snprintf(ptr null, i64 0, ptr @.str) #2
67 define i32 @test_percentage_return_value() #0 {
68 ; CHECK-LABEL: @test_percentage_return_value(
69 ; CHECK-NEXT: [[CALL:%.*]] = call i32 (ptr, i64, ptr, ...) @snprintf(ptr null, i64 0, ptr nonnull @.str.1)
70 ; CHECK-NEXT: ret i32 [[CALL]]
72 %call = call i32 (ptr, i64, ptr, ...) @snprintf(ptr null, i64 0, ptr @.str.1) #3
77 define void @test_correct_copy(ptr %buf) #0 {
78 ; CHECK-LABEL: @test_correct_copy(
79 ; CHECK-NEXT: store i32 7500915, ptr [[BUF:%.*]], align 1
80 ; CHECK-NEXT: ret void
82 %call = call i32 (ptr, i64, ptr, ...) @snprintf(ptr %buf, i64 32, ptr @.str) #2
86 define i32 @test_char_zero_size(ptr %buf) #0 {
87 ; CHECK-LABEL: @test_char_zero_size(
88 ; CHECK-NEXT: ret i32 1
90 %call = call i32 (ptr, i64, ptr, ...) @snprintf(ptr %buf, i64 0, ptr @.str.2, i32 65) #2
94 define i32 @test_char_small_size(ptr %buf) #0 {
95 ; CHECK-LABEL: @test_char_small_size(
96 ; CHECK-NEXT: store i8 0, ptr [[BUF:%.*]], align 1
97 ; CHECK-NEXT: ret i32 1
99 %call = call i32 (ptr, i64, ptr, ...) @snprintf(ptr %buf, i64 1, ptr @.str.2, i32 65) #2
103 define i32 @test_char_ok_size(ptr %buf) #0 {
104 ; CHECK-LABEL: @test_char_ok_size(
105 ; CHECK-NEXT: store i8 65, ptr [[BUF:%.*]], align 1
106 ; CHECK-NEXT: [[NUL:%.*]] = getelementptr inbounds i8, ptr [[BUF]], i64 1
107 ; CHECK-NEXT: store i8 0, ptr [[NUL]], align 1
108 ; CHECK-NEXT: ret i32 1
110 %call = call i32 (ptr, i64, ptr, ...) @snprintf(ptr %buf, i64 32, ptr @.str.2, i32 65) #2
114 define i32 @test_str_zero_size(ptr %buf) #0 {
115 ; CHECK-LABEL: @test_str_zero_size(
116 ; CHECK-NEXT: ret i32 3
118 %call = call i32 (ptr, i64, ptr, ...) @snprintf(ptr %buf, i64 0, ptr @.str.3, ptr @.str) #2
122 define i32 @test_str_small_size(ptr %buf) #0 {
123 ; CHECK-LABEL: @test_str_small_size(
124 ; CHECK-NEXT: store i8 0, ptr [[BUF:%.*]], align 1
125 ; CHECK-NEXT: ret i32 3
127 %call = call i32 (ptr, i64, ptr, ...) @snprintf(ptr %buf, i64 1, ptr @.str.3, ptr @.str) #2
131 define i32 @test_str_ok_size(ptr %buf) #0 {
132 ; CHECK-LABEL: @test_str_ok_size(
133 ; CHECK-NEXT: store i32 7500915, ptr [[BUF:%.*]], align 1
134 ; CHECK-NEXT: ret i32 3
136 %call = call i32 (ptr, i64, ptr, ...) @snprintf(ptr %buf, i64 32, ptr @.str.3, ptr @.str) #2
140 ; snprintf(buf, 32, "") -> memcpy -> store
141 define i32 @test_str_ok_size_tail(ptr %buf) {
142 ; CHECK-LABEL: @test_str_ok_size_tail(
143 ; CHECK-NEXT: store i8 0, ptr [[BUF:%.*]], align 1
144 ; CHECK-NEXT: ret i32 0
146 %1 = tail call i32 (ptr, i64, ptr, ...) @snprintf(ptr %buf, i64 8, ptr @.str.4)
150 define i32 @test_str_ok_size_musttail(ptr %buf, i64 %x, ptr %y, ...) {
151 ; CHECK-LABEL: @test_str_ok_size_musttail(
152 ; CHECK-NEXT: [[TMP1:%.*]] = musttail call i32 (ptr, i64, ptr, ...) @snprintf(ptr [[BUF:%.*]], i64 8, ptr nonnull @.str.4, ...)
153 ; CHECK-NEXT: ret i32 [[TMP1]]
155 %1 = musttail call i32 (ptr, i64, ptr, ...) @snprintf(ptr %buf, i64 8, ptr @.str.4, ...)
159 ; snprintf(buf, 32, "%s", "str") -> memcpy -> store
160 define i32 @test_str_ok_size_tail2(ptr %buf) {
161 ; CHECK-LABEL: @test_str_ok_size_tail2(
162 ; CHECK-NEXT: store i32 7500915, ptr [[BUF:%.*]], align 1
163 ; CHECK-NEXT: ret i32 3
165 %1 = tail call i32 (ptr, i64, ptr, ...) @snprintf(ptr %buf, i64 8, ptr @.str.3, ptr @.str)
169 define i32 @test_str_ok_size_musttail2(ptr %buf, i64 %x, ptr %y, ...) {
170 ; CHECK-LABEL: @test_str_ok_size_musttail2(
171 ; CHECK-NEXT: [[TMP1:%.*]] = musttail call i32 (ptr, i64, ptr, ...) @snprintf(ptr [[BUF:%.*]], i64 8, ptr nonnull @.str.3, ptr nonnull @.str, ...)
172 ; CHECK-NEXT: ret i32 [[TMP1]]
174 %1 = musttail call i32 (ptr, i64, ptr, ...) @snprintf(ptr %buf, i64 8, ptr @.str.3, ptr @.str, ...)