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(i8*)
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_p = getelementptr [6 x i8], [6 x i8]* @hello, i32 0, i32 0
25 %hello_l = call i32 @strlen(i8* %hello_p)
29 define i32 @test_simplify2() {
30 ; CHECK-LABEL: @test_simplify2(
31 ; CHECK-NEXT: ret i32 0
33 %null_p = getelementptr [1 x i8], [1 x i8]* @null, i32 0, i32 0
34 %null_l = call i32 @strlen(i8* %null_p)
38 define i32 @test_simplify3() {
39 ; CHECK-LABEL: @test_simplify3(
40 ; CHECK-NEXT: ret i32 0
42 %null_hello_p = getelementptr [7 x i8], [7 x i8]* @null_hello, i32 0, i32 0
43 %null_hello_l = call i32 @strlen(i8* %null_hello_p)
47 define i32 @test_simplify4() {
48 ; CHECK-LABEL: @test_simplify4(
49 ; CHECK-NEXT: ret i32 0
51 %len = tail call i32 @strlen(i8* @nullstring) nounwind
55 ; Check strlen(x) == 0 --> *x == 0.
57 define i1 @test_simplify5() {
58 ; CHECK-LABEL: @test_simplify5(
59 ; CHECK-NEXT: ret i1 false
61 %hello_p = getelementptr [6 x i8], [6 x i8]* @hello, i32 0, i32 0
62 %hello_l = call i32 @strlen(i8* %hello_p)
63 %eq_hello = icmp eq i32 %hello_l, 0
67 define i1 @test_simplify6(i8* %str_p) {
68 ; CHECK-LABEL: @test_simplify6(
69 ; CHECK-NEXT: [[STRLENFIRST:%.*]] = load i8, i8* [[STR_P:%.*]], align 1
70 ; CHECK-NEXT: [[EQ_NULL:%.*]] = icmp eq i8 [[STRLENFIRST]], 0
71 ; CHECK-NEXT: ret i1 [[EQ_NULL]]
73 %str_l = call i32 @strlen(i8* %str_p)
74 %eq_null = icmp eq i32 %str_l, 0
78 ; Check strlen(x) != 0 --> *x != 0.
80 define i1 @test_simplify7() {
81 ; CHECK-LABEL: @test_simplify7(
82 ; CHECK-NEXT: ret i1 true
84 %hello_p = getelementptr [6 x i8], [6 x i8]* @hello, i32 0, i32 0
85 %hello_l = call i32 @strlen(i8* %hello_p)
86 %ne_hello = icmp ne i32 %hello_l, 0
90 define i1 @test_simplify8(i8* %str_p) {
91 ; CHECK-LABEL: @test_simplify8(
92 ; CHECK-NEXT: [[STRLENFIRST:%.*]] = load i8, i8* [[STR_P:%.*]], align 1
93 ; CHECK-NEXT: [[NE_NULL:%.*]] = icmp ne i8 [[STRLENFIRST]], 0
94 ; CHECK-NEXT: ret i1 [[NE_NULL]]
96 %str_l = call i32 @strlen(i8* %str_p)
97 %ne_null = icmp ne i32 %str_l, 0
101 define i32 @test_simplify9(i1 %x) {
102 ; CHECK-LABEL: @test_simplify9(
103 ; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[X:%.*]], i32 5, i32 6
104 ; CHECK-NEXT: ret i32 [[TMP1]]
106 %hello = getelementptr [6 x i8], [6 x i8]* @hello, i32 0, i32 0
107 %longer = getelementptr [7 x i8], [7 x i8]* @longer, i32 0, i32 0
108 %s = select i1 %x, i8* %hello, i8* %longer
109 %l = call i32 @strlen(i8* %s)
113 ; Check the case that should be simplified to a sub instruction.
114 ; strlen(@hello + x) --> 5 - x
116 define i32 @test_simplify10_inbounds(i32 %x) {
117 ; CHECK-LABEL: @test_simplify10_inbounds(
118 ; CHECK-NEXT: [[TMP1:%.*]] = sub i32 5, [[X:%.*]]
119 ; CHECK-NEXT: ret i32 [[TMP1]]
121 %hello_p = getelementptr inbounds [6 x i8], [6 x i8]* @hello, i32 0, i32 %x
122 %hello_l = call i32 @strlen(i8* %hello_p)
126 define i32 @test_simplify10_no_inbounds(i32 %x) {
127 ; CHECK-LABEL: @test_simplify10_no_inbounds(
128 ; CHECK-NEXT: [[TMP1:%.*]] = sub i32 5, [[X:%.*]]
129 ; CHECK-NEXT: ret i32 [[TMP1]]
131 %hello_p = getelementptr [6 x i8], [6 x i8]* @hello, i32 0, i32 %x
132 %hello_l = call i32 @strlen(i8* %hello_p)
136 ; strlen(@null_hello_mid + (x & 7)) --> 9 - (x & 7)
138 define i32 @test_simplify11(i32 %x) {
139 ; CHECK-LABEL: @test_simplify11(
140 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 7
141 ; CHECK-NEXT: [[TMP1:%.*]] = sub nuw nsw i32 9, [[AND]]
142 ; CHECK-NEXT: ret i32 [[TMP1]]
145 %hello_p = getelementptr inbounds [13 x i8], [13 x i8]* @null_hello_mid, i32 0, i32 %and
146 %hello_l = call i32 @strlen(i8* %hello_p)
150 ; Check cases that shouldn't be simplified.
152 define i32 @test_no_simplify1() {
153 ; CHECK-LABEL: @test_no_simplify1(
154 ; CHECK-NEXT: [[A_L:%.*]] = call i32 @strlen(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @a, i32 0, i32 0))
155 ; CHECK-NEXT: ret i32 [[A_L]]
157 %a_p = getelementptr [32 x i8], [32 x i8]* @a, i32 0, i32 0
158 %a_l = call i32 @strlen(i8* %a_p)
162 ; strlen(@null_hello + x) should not be simplified to a sub instruction.
164 define i32 @test_no_simplify2(i32 %x) {
165 ; CHECK-LABEL: @test_no_simplify2(
166 ; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds [7 x i8], [7 x i8]* @null_hello, i32 0, i32 [[X:%.*]]
167 ; CHECK-NEXT: [[HELLO_L:%.*]] = call i32 @strlen(i8* noundef nonnull [[HELLO_P]])
168 ; CHECK-NEXT: ret i32 [[HELLO_L]]
170 %hello_p = getelementptr inbounds [7 x i8], [7 x i8]* @null_hello, i32 0, i32 %x
171 %hello_l = call i32 @strlen(i8* %hello_p)
175 define i32 @test_no_simplify2_no_null_opt(i32 %x) #0 {
176 ; CHECK-LABEL: @test_no_simplify2_no_null_opt(
177 ; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds [7 x i8], [7 x i8]* @null_hello, i32 0, i32 [[X:%.*]]
178 ; CHECK-NEXT: [[HELLO_L:%.*]] = call i32 @strlen(i8* noundef [[HELLO_P]])
179 ; CHECK-NEXT: ret i32 [[HELLO_L]]
181 %hello_p = getelementptr inbounds [7 x i8], [7 x i8]* @null_hello, i32 0, i32 %x
182 %hello_l = call i32 @strlen(i8* %hello_p)
186 ; strlen(@null_hello_mid + (x & 15)) should not be simplified to a sub instruction.
188 define i32 @test_no_simplify3(i32 %x) {
189 ; CHECK-LABEL: @test_no_simplify3(
190 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 15
191 ; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds [13 x i8], [13 x i8]* @null_hello_mid, i32 0, i32 [[AND]]
192 ; CHECK-NEXT: [[HELLO_L:%.*]] = call i32 @strlen(i8* noundef nonnull [[HELLO_P]])
193 ; CHECK-NEXT: ret i32 [[HELLO_L]]
195 %and = and i32 %x, 15
196 %hello_p = getelementptr inbounds [13 x i8], [13 x i8]* @null_hello_mid, i32 0, i32 %and
197 %hello_l = call i32 @strlen(i8* %hello_p)
201 define i32 @test_no_simplify3_on_null_opt(i32 %x) #0 {
202 ; CHECK-LABEL: @test_no_simplify3_on_null_opt(
203 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 15
204 ; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds [13 x i8], [13 x i8]* @null_hello_mid, i32 0, i32 [[AND]]
205 ; CHECK-NEXT: [[HELLO_L:%.*]] = call i32 @strlen(i8* noundef [[HELLO_P]])
206 ; CHECK-NEXT: ret i32 [[HELLO_L]]
208 %and = and i32 %x, 15
209 %hello_p = getelementptr inbounds [13 x i8], [13 x i8]* @null_hello_mid, i32 0, i32 %and
210 %hello_l = call i32 @strlen(i8* %hello_p)
214 define i32 @test1(i8* %str) {
215 ; CHECK-LABEL: @test1(
216 ; CHECK-NEXT: [[LEN:%.*]] = tail call i32 @strlen(i8* noundef nonnull dereferenceable(1) [[STR:%.*]]) #[[ATTR1:[0-9]+]]
217 ; CHECK-NEXT: ret i32 [[LEN]]
219 %len = tail call i32 @strlen(i8* %str) nounwind
223 define i32 @test2(i8* %str) #0 {
224 ; CHECK-LABEL: @test2(
225 ; CHECK-NEXT: [[LEN:%.*]] = tail call i32 @strlen(i8* noundef [[STR:%.*]]) #[[ATTR1]]
226 ; CHECK-NEXT: ret i32 [[LEN]]
228 %len = tail call i32 @strlen(i8* %str) nounwind
232 ; Test cases for PR47149.
233 define i1 @strlen0_after_write_to_first_byte_global() {
234 ; CHECK-LABEL: @strlen0_after_write_to_first_byte_global(
235 ; CHECK-NEXT: store i8 49, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @a, i32 0, i32 0), align 16
236 ; CHECK-NEXT: ret i1 false
238 store i8 49, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @a, i64 0, i64 0), align 16
239 %len = tail call i32 @strlen(i8* nonnull dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @a, i64 0, i64 0))
240 %cmp = icmp eq i32 %len, 0
244 define i1 @strlen0_after_write_to_second_byte_global() {
245 ; CHECK-LABEL: @strlen0_after_write_to_second_byte_global(
246 ; CHECK-NEXT: store i8 49, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @a, i32 0, i32 1), align 16
247 ; CHECK-NEXT: [[STRLENFIRST:%.*]] = load i8, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @a, i32 0, i32 0), align 1
248 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[STRLENFIRST]], 0
249 ; CHECK-NEXT: ret i1 [[CMP]]
251 store i8 49, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @a, i64 0, i64 1), align 16
252 %len = tail call i32 @strlen(i8* nonnull dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @a, i64 0, i64 0))
253 %cmp = icmp eq i32 %len, 0
257 define i1 @strlen0_after_write_to_first_byte(i8 *%ptr) {
258 ; CHECK-LABEL: @strlen0_after_write_to_first_byte(
259 ; CHECK-NEXT: store i8 49, i8* [[PTR:%.*]], align 1
260 ; CHECK-NEXT: ret i1 false
262 store i8 49, i8* %ptr
263 %len = tail call i32 @strlen(i8* nonnull dereferenceable(1) %ptr)
264 %cmp = icmp eq i32 %len, 0
268 define i1 @strlen0_after_write_to_second_byte(i8 *%ptr) {
269 ; CHECK-LABEL: @strlen0_after_write_to_second_byte(
270 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr i8, i8* [[PTR:%.*]], i32 1
271 ; CHECK-NEXT: store i8 49, i8* [[GEP]], align 1
272 ; CHECK-NEXT: [[STRLENFIRST:%.*]] = load i8, i8* [[PTR]], align 1
273 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[STRLENFIRST]], 0
274 ; CHECK-NEXT: ret i1 [[CMP]]
276 %gep = getelementptr i8, i8* %ptr, i64 1
277 store i8 49, i8* %gep
278 %len = tail call i32 @strlen(i8* nonnull dereferenceable(1) %ptr)
279 %cmp = icmp eq i32 %len, 0
283 attributes #0 = { null_pointer_is_valid }