1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; Test that the strlen library call simplifier works correctly.
4 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
6 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"
8 @hello = constant [6 x i8] c"hello\00"
9 @longer = constant [7 x i8] c"longer\00"
10 @null = constant [1 x i8] zeroinitializer
11 @null_hello = constant [7 x i8] c"\00hello\00"
12 @nullstring = constant i8 0
13 @a = common global [32 x i8] zeroinitializer, align 1
14 @null_hello_mid = constant [13 x i8] c"hello wor\00ld\00"
16 declare i32 @strlen(ptr)
18 ; Check strlen(string constant) -> integer constant.
20 define i32 @test_simplify1() {
21 ; CHECK-LABEL: @test_simplify1(
22 ; CHECK-NEXT: ret i32 5
24 %hello_l = call i32 @strlen(ptr @hello)
28 define i32 @test_simplify2() {
29 ; CHECK-LABEL: @test_simplify2(
30 ; CHECK-NEXT: ret i32 0
32 %null_l = call i32 @strlen(ptr @null)
36 define i32 @test_simplify3() {
37 ; CHECK-LABEL: @test_simplify3(
38 ; CHECK-NEXT: ret i32 0
40 %null_hello_l = call i32 @strlen(ptr @null_hello)
44 define i32 @test_simplify4() {
45 ; CHECK-LABEL: @test_simplify4(
46 ; CHECK-NEXT: ret i32 0
48 %len = tail call i32 @strlen(ptr @nullstring) nounwind
52 ; Check strlen(x) == 0 --> *x == 0.
54 define i1 @test_simplify5() {
55 ; CHECK-LABEL: @test_simplify5(
56 ; CHECK-NEXT: ret i1 false
58 %hello_l = call i32 @strlen(ptr @hello)
59 %eq_hello = icmp eq i32 %hello_l, 0
63 define i1 @test_simplify6(ptr %str_p) {
64 ; CHECK-LABEL: @test_simplify6(
65 ; CHECK-NEXT: [[CHAR0:%.*]] = load i8, ptr [[STR_P:%.*]], align 1
66 ; CHECK-NEXT: [[EQ_NULL:%.*]] = icmp eq i8 [[CHAR0]], 0
67 ; CHECK-NEXT: ret i1 [[EQ_NULL]]
69 %str_l = call i32 @strlen(ptr %str_p)
70 %eq_null = icmp eq i32 %str_l, 0
74 ; Check strlen(x) != 0 --> *x != 0.
76 define i1 @test_simplify7() {
77 ; CHECK-LABEL: @test_simplify7(
78 ; CHECK-NEXT: ret i1 true
80 %hello_l = call i32 @strlen(ptr @hello)
81 %ne_hello = icmp ne i32 %hello_l, 0
85 define i1 @test_simplify8(ptr %str_p) {
86 ; CHECK-LABEL: @test_simplify8(
87 ; CHECK-NEXT: [[CHAR0:%.*]] = load i8, ptr [[STR_P:%.*]], align 1
88 ; CHECK-NEXT: [[NE_NULL:%.*]] = icmp ne i8 [[CHAR0]], 0
89 ; CHECK-NEXT: ret i1 [[NE_NULL]]
91 %str_l = call i32 @strlen(ptr %str_p)
92 %ne_null = icmp ne i32 %str_l, 0
96 define i32 @test_simplify9(i1 %x) {
97 ; CHECK-LABEL: @test_simplify9(
98 ; CHECK-NEXT: [[L:%.*]] = select i1 [[X:%.*]], i32 5, i32 6
99 ; CHECK-NEXT: ret i32 [[L]]
101 %s = select i1 %x, ptr @hello, ptr @longer
102 %l = call i32 @strlen(ptr %s)
106 ; Check the case that should be simplified to a sub instruction.
107 ; strlen(@hello + x) --> 5 - x
109 define i32 @test_simplify10_inbounds(i32 %x) {
110 ; CHECK-LABEL: @test_simplify10_inbounds(
111 ; CHECK-NEXT: [[HELLO_L:%.*]] = sub i32 5, [[X:%.*]]
112 ; CHECK-NEXT: ret i32 [[HELLO_L]]
114 %hello_p = getelementptr inbounds [6 x i8], ptr @hello, i32 0, i32 %x
115 %hello_l = call i32 @strlen(ptr %hello_p)
119 define i32 @test_simplify10_no_inbounds(i32 %x) {
120 ; CHECK-LABEL: @test_simplify10_no_inbounds(
121 ; CHECK-NEXT: [[HELLO_L:%.*]] = sub i32 5, [[X:%.*]]
122 ; CHECK-NEXT: ret i32 [[HELLO_L]]
124 %hello_p = getelementptr [6 x i8], ptr @hello, i32 0, i32 %x
125 %hello_l = call i32 @strlen(ptr %hello_p)
129 ; strlen(@null_hello_mid + (x & 7)) --> 9 - (x & 7)
131 define i32 @test_simplify11(i32 %x) {
132 ; CHECK-LABEL: @test_simplify11(
133 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 7
134 ; CHECK-NEXT: [[HELLO_L:%.*]] = sub nuw nsw i32 9, [[AND]]
135 ; CHECK-NEXT: ret i32 [[HELLO_L]]
138 %hello_p = getelementptr inbounds [13 x i8], ptr @null_hello_mid, i32 0, i32 %and
139 %hello_l = call i32 @strlen(ptr %hello_p)
143 ; Check cases that shouldn't be simplified.
145 define i32 @test_no_simplify1() {
146 ; CHECK-LABEL: @test_no_simplify1(
147 ; CHECK-NEXT: [[A_L:%.*]] = call i32 @strlen(ptr noundef nonnull dereferenceable(1) @a)
148 ; CHECK-NEXT: ret i32 [[A_L]]
150 %a_l = call i32 @strlen(ptr @a)
154 ; strlen(@null_hello + x) should not be simplified to a sub instruction.
156 define i32 @test_no_simplify2(i32 %x) {
157 ; CHECK-LABEL: @test_no_simplify2(
158 ; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds [7 x i8], ptr @null_hello, i32 0, i32 [[X:%.*]]
159 ; CHECK-NEXT: [[HELLO_L:%.*]] = call i32 @strlen(ptr noundef nonnull dereferenceable(1) [[HELLO_P]])
160 ; CHECK-NEXT: ret i32 [[HELLO_L]]
162 %hello_p = getelementptr inbounds [7 x i8], ptr @null_hello, i32 0, i32 %x
163 %hello_l = call i32 @strlen(ptr %hello_p)
167 define i32 @test_no_simplify2_no_null_opt(i32 %x) #0 {
168 ; CHECK-LABEL: @test_no_simplify2_no_null_opt(
169 ; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds [7 x i8], ptr @null_hello, i32 0, i32 [[X:%.*]]
170 ; CHECK-NEXT: [[HELLO_L:%.*]] = call i32 @strlen(ptr noundef [[HELLO_P]])
171 ; CHECK-NEXT: ret i32 [[HELLO_L]]
173 %hello_p = getelementptr inbounds [7 x i8], ptr @null_hello, i32 0, i32 %x
174 %hello_l = call i32 @strlen(ptr %hello_p)
178 ; strlen(@null_hello_mid + (x & 15)) should not be simplified to a sub instruction.
180 define i32 @test_no_simplify3(i32 %x) {
181 ; CHECK-LABEL: @test_no_simplify3(
182 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 15
183 ; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds nuw [13 x i8], ptr @null_hello_mid, i32 0, i32 [[AND]]
184 ; CHECK-NEXT: [[HELLO_L:%.*]] = call i32 @strlen(ptr noundef nonnull dereferenceable(1) [[HELLO_P]])
185 ; CHECK-NEXT: ret i32 [[HELLO_L]]
187 %and = and i32 %x, 15
188 %hello_p = getelementptr inbounds [13 x i8], ptr @null_hello_mid, i32 0, i32 %and
189 %hello_l = call i32 @strlen(ptr %hello_p)
193 define i32 @test_no_simplify3_on_null_opt(i32 %x) #0 {
194 ; CHECK-LABEL: @test_no_simplify3_on_null_opt(
195 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 15
196 ; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds nuw [13 x i8], ptr @null_hello_mid, i32 0, i32 [[AND]]
197 ; CHECK-NEXT: [[HELLO_L:%.*]] = call i32 @strlen(ptr noundef nonnull dereferenceable(1) [[HELLO_P]])
198 ; CHECK-NEXT: ret i32 [[HELLO_L]]
200 %and = and i32 %x, 15
201 %hello_p = getelementptr inbounds [13 x i8], ptr @null_hello_mid, i32 0, i32 %and
202 %hello_l = call i32 @strlen(ptr %hello_p)
206 define i32 @test1(ptr %str) {
207 ; CHECK-LABEL: @test1(
208 ; CHECK-NEXT: [[LEN:%.*]] = tail call i32 @strlen(ptr noundef nonnull dereferenceable(1) [[STR:%.*]]) #[[ATTR1:[0-9]+]]
209 ; CHECK-NEXT: ret i32 [[LEN]]
211 %len = tail call i32 @strlen(ptr %str) nounwind
215 define i32 @test2(ptr %str) #0 {
216 ; CHECK-LABEL: @test2(
217 ; CHECK-NEXT: [[LEN:%.*]] = tail call i32 @strlen(ptr noundef [[STR:%.*]]) #[[ATTR1]]
218 ; CHECK-NEXT: ret i32 [[LEN]]
220 %len = tail call i32 @strlen(ptr %str) nounwind
224 ; Test cases for PR47149.
225 define i1 @strlen0_after_write_to_first_byte_global() {
226 ; CHECK-LABEL: @strlen0_after_write_to_first_byte_global(
227 ; CHECK-NEXT: store i8 49, ptr @a, align 16
228 ; CHECK-NEXT: ret i1 false
230 store i8 49, ptr @a, align 16
231 %len = tail call i32 @strlen(ptr nonnull dereferenceable(1) @a)
232 %cmp = icmp eq i32 %len, 0
236 define i1 @strlen0_after_write_to_second_byte_global() {
237 ; CHECK-LABEL: @strlen0_after_write_to_second_byte_global(
238 ; CHECK-NEXT: store i8 49, ptr getelementptr inbounds nuw (i8, ptr @a, i32 1), align 16
239 ; CHECK-NEXT: [[CHAR0:%.*]] = load i8, ptr @a, align 1
240 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[CHAR0]], 0
241 ; CHECK-NEXT: ret i1 [[CMP]]
243 store i8 49, ptr getelementptr inbounds ([32 x i8], ptr @a, i64 0, i64 1), align 16
244 %len = tail call i32 @strlen(ptr nonnull dereferenceable(1) @a)
245 %cmp = icmp eq i32 %len, 0
249 define i1 @strlen0_after_write_to_first_byte(ptr %ptr) {
250 ; CHECK-LABEL: @strlen0_after_write_to_first_byte(
251 ; CHECK-NEXT: store i8 49, ptr [[PTR:%.*]], align 1
252 ; CHECK-NEXT: ret i1 false
254 store i8 49, ptr %ptr
255 %len = tail call i32 @strlen(ptr nonnull dereferenceable(1) %ptr)
256 %cmp = icmp eq i32 %len, 0
260 define i1 @strlen0_after_write_to_second_byte(ptr %ptr) {
261 ; CHECK-LABEL: @strlen0_after_write_to_second_byte(
262 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr i8, ptr [[PTR:%.*]], i32 1
263 ; CHECK-NEXT: store i8 49, ptr [[GEP]], align 1
264 ; CHECK-NEXT: [[CHAR0:%.*]] = load i8, ptr [[PTR]], align 1
265 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[CHAR0]], 0
266 ; CHECK-NEXT: ret i1 [[CMP]]
268 %gep = getelementptr i8, ptr %ptr, i64 1
269 store i8 49, ptr %gep
270 %len = tail call i32 @strlen(ptr nonnull dereferenceable(1) %ptr)
271 %cmp = icmp eq i32 %len, 0
275 attributes #0 = { null_pointer_is_valid }