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 -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(i32 %x) {
117 ; CHECK-LABEL: @test_simplify10(
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 ; strlen(@null_hello_mid + (x & 7)) --> 9 - (x & 7)
128 define i32 @test_simplify11(i32 %x) {
129 ; CHECK-LABEL: @test_simplify11(
130 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 7
131 ; CHECK-NEXT: [[TMP1:%.*]] = sub nuw nsw i32 9, [[AND]]
132 ; CHECK-NEXT: ret i32 [[TMP1]]
135 %hello_p = getelementptr inbounds [13 x i8], [13 x i8]* @null_hello_mid, i32 0, i32 %and
136 %hello_l = call i32 @strlen(i8* %hello_p)
140 ; Check cases that shouldn't be simplified.
142 define i32 @test_no_simplify1() {
143 ; CHECK-LABEL: @test_no_simplify1(
144 ; 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))
145 ; CHECK-NEXT: ret i32 [[A_L]]
147 %a_p = getelementptr [32 x i8], [32 x i8]* @a, i32 0, i32 0
148 %a_l = call i32 @strlen(i8* %a_p)
152 ; strlen(@null_hello + x) should not be simplified to a sub instruction.
154 define i32 @test_no_simplify2(i32 %x) {
155 ; CHECK-LABEL: @test_no_simplify2(
156 ; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds [7 x i8], [7 x i8]* @null_hello, i32 0, i32 [[X:%.*]]
157 ; CHECK-NEXT: [[HELLO_L:%.*]] = call i32 @strlen(i8* noundef nonnull [[HELLO_P]])
158 ; CHECK-NEXT: ret i32 [[HELLO_L]]
160 %hello_p = getelementptr inbounds [7 x i8], [7 x i8]* @null_hello, i32 0, i32 %x
161 %hello_l = call i32 @strlen(i8* %hello_p)
165 define i32 @test_no_simplify2_no_null_opt(i32 %x) #0 {
166 ; CHECK-LABEL: @test_no_simplify2_no_null_opt(
167 ; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds [7 x i8], [7 x i8]* @null_hello, i32 0, i32 [[X:%.*]]
168 ; CHECK-NEXT: [[HELLO_L:%.*]] = call i32 @strlen(i8* noundef [[HELLO_P]])
169 ; CHECK-NEXT: ret i32 [[HELLO_L]]
171 %hello_p = getelementptr inbounds [7 x i8], [7 x i8]* @null_hello, i32 0, i32 %x
172 %hello_l = call i32 @strlen(i8* %hello_p)
176 ; strlen(@null_hello_mid + (x & 15)) should not be simplified to a sub instruction.
178 define i32 @test_no_simplify3(i32 %x) {
179 ; CHECK-LABEL: @test_no_simplify3(
180 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 15
181 ; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds [13 x i8], [13 x i8]* @null_hello_mid, i32 0, i32 [[AND]]
182 ; CHECK-NEXT: [[HELLO_L:%.*]] = call i32 @strlen(i8* noundef nonnull [[HELLO_P]])
183 ; CHECK-NEXT: ret i32 [[HELLO_L]]
185 %and = and i32 %x, 15
186 %hello_p = getelementptr inbounds [13 x i8], [13 x i8]* @null_hello_mid, i32 0, i32 %and
187 %hello_l = call i32 @strlen(i8* %hello_p)
191 define i32 @test_no_simplify3_on_null_opt(i32 %x) #0 {
192 ; CHECK-LABEL: @test_no_simplify3_on_null_opt(
193 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 15
194 ; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds [13 x i8], [13 x i8]* @null_hello_mid, i32 0, i32 [[AND]]
195 ; CHECK-NEXT: [[HELLO_L:%.*]] = call i32 @strlen(i8* noundef [[HELLO_P]])
196 ; CHECK-NEXT: ret i32 [[HELLO_L]]
198 %and = and i32 %x, 15
199 %hello_p = getelementptr inbounds [13 x i8], [13 x i8]* @null_hello_mid, i32 0, i32 %and
200 %hello_l = call i32 @strlen(i8* %hello_p)
204 define i32 @test1(i8* %str) {
205 ; CHECK-LABEL: @test1(
206 ; CHECK-NEXT: [[LEN:%.*]] = tail call i32 @strlen(i8* noundef nonnull dereferenceable(1) [[STR:%.*]]) [[ATTR1:#.*]]
207 ; CHECK-NEXT: ret i32 [[LEN]]
209 %len = tail call i32 @strlen(i8* %str) nounwind
213 define i32 @test2(i8* %str) #0 {
214 ; CHECK-LABEL: @test2(
215 ; CHECK-NEXT: [[LEN:%.*]] = tail call i32 @strlen(i8* noundef [[STR:%.*]]) [[ATTR1]]
216 ; CHECK-NEXT: ret i32 [[LEN]]
218 %len = tail call i32 @strlen(i8* %str) nounwind
222 ; Test cases for PR47149.
223 define i1 @strlen0_after_write_to_first_byte_global() {
224 ; CHECK-LABEL: @strlen0_after_write_to_first_byte_global(
225 ; CHECK-NEXT: store i8 49, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @a, i32 0, i32 0), align 16
226 ; CHECK-NEXT: ret i1 false
228 store i8 49, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @a, i64 0, i64 0), align 16
229 %len = tail call i32 @strlen(i8* nonnull dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @a, i64 0, i64 0))
230 %cmp = icmp eq i32 %len, 0
234 define i1 @strlen0_after_write_to_second_byte_global() {
235 ; CHECK-LABEL: @strlen0_after_write_to_second_byte_global(
236 ; CHECK-NEXT: store i8 49, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @a, i32 0, i32 1), align 16
237 ; CHECK-NEXT: [[STRLENFIRST:%.*]] = load i8, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @a, i32 0, i32 0), align 1
238 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[STRLENFIRST]], 0
239 ; CHECK-NEXT: ret i1 [[CMP]]
241 store i8 49, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @a, i64 0, i64 1), align 16
242 %len = tail call i32 @strlen(i8* nonnull dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @a, i64 0, i64 0))
243 %cmp = icmp eq i32 %len, 0
247 define i1 @strlen0_after_write_to_first_byte(i8 *%ptr) {
248 ; CHECK-LABEL: @strlen0_after_write_to_first_byte(
249 ; CHECK-NEXT: store i8 49, i8* [[PTR:%.*]], align 1
250 ; CHECK-NEXT: ret i1 false
252 store i8 49, i8* %ptr
253 %len = tail call i32 @strlen(i8* nonnull dereferenceable(1) %ptr)
254 %cmp = icmp eq i32 %len, 0
258 define i1 @strlen0_after_write_to_second_byte(i8 *%ptr) {
259 ; CHECK-LABEL: @strlen0_after_write_to_second_byte(
260 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr i8, i8* [[PTR:%.*]], i32 1
261 ; CHECK-NEXT: store i8 49, i8* [[GEP]], align 1
262 ; CHECK-NEXT: [[STRLENFIRST:%.*]] = load i8, i8* [[PTR]], align 1
263 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[STRLENFIRST]], 0
264 ; CHECK-NEXT: ret i1 [[CMP]]
266 %gep = getelementptr i8, i8* %ptr, i64 1
267 store i8 49, i8* %gep
268 %len = tail call i32 @strlen(i8* nonnull dereferenceable(1) %ptr)
269 %cmp = icmp eq i32 %len, 0
273 attributes #0 = { null_pointer_is_valid }