1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; Test that the wcslen library call simplifier works correctly.
4 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
6 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
8 declare i64 @wcslen(ptr)
10 !0 = !{i32 1, !"wchar_size", i32 4}
11 !llvm.module.flags = !{!0}
13 @hello = constant [6 x i32] [i32 104, i32 101, i32 108, i32 108, i32 111, i32 0]
14 @longer = constant [7 x i32] [i32 108, i32 111, i32 110, i32 103, i32 101, i32 114, i32 0]
15 @null = constant [1 x i32] zeroinitializer
16 @null_hello = constant [7 x i32] [i32 0, i32 104, i32 101, i32 108, i32 108, i32 111, i32 0]
17 @nullstring = constant i32 0
18 @a = common global [32 x i32] zeroinitializer, align 1
19 @null_hello_mid = constant [13 x i32] [i32 104, i32 101, i32 108, i32 108, i32 111, i32 32, i32 119, i32 111, i32 114, i32 0, i32 108, i32 100, i32 0]
21 define i64 @test_simplify1() {
22 ; CHECK-LABEL: @test_simplify1(
23 ; CHECK-NEXT: ret i64 5
25 %hello_l = call i64 @wcslen(ptr @hello)
29 define i64 @test_simplify2() {
30 ; CHECK-LABEL: @test_simplify2(
31 ; CHECK-NEXT: ret i64 0
33 %null_l = call i64 @wcslen(ptr @null)
37 define i64 @test_simplify3() {
38 ; CHECK-LABEL: @test_simplify3(
39 ; CHECK-NEXT: ret i64 0
41 %null_hello_l = call i64 @wcslen(ptr @null_hello)
45 define i64 @test_simplify4() {
46 ; CHECK-LABEL: @test_simplify4(
47 ; CHECK-NEXT: ret i64 0
49 %len = tail call i64 @wcslen(ptr @nullstring) nounwind
53 ; Check wcslen(x) == 0 --> *x == 0.
55 define i1 @test_simplify5() {
56 ; CHECK-LABEL: @test_simplify5(
57 ; CHECK-NEXT: ret i1 false
59 %hello_l = call i64 @wcslen(ptr @hello)
60 %eq_hello = icmp eq i64 %hello_l, 0
64 define i1 @test_simplify6(ptr %str_p) {
65 ; CHECK-LABEL: @test_simplify6(
66 ; CHECK-NEXT: [[CHAR0:%.*]] = load i32, ptr [[STR_P:%.*]], align 4
67 ; CHECK-NEXT: [[EQ_NULL:%.*]] = icmp eq i32 [[CHAR0]], 0
68 ; CHECK-NEXT: ret i1 [[EQ_NULL]]
70 %str_l = call i64 @wcslen(ptr %str_p)
71 %eq_null = icmp eq i64 %str_l, 0
75 ; Check wcslen(x) != 0 --> *x != 0.
77 define i1 @test_simplify7() {
78 ; CHECK-LABEL: @test_simplify7(
79 ; CHECK-NEXT: ret i1 true
81 %hello_l = call i64 @wcslen(ptr @hello)
82 %ne_hello = icmp ne i64 %hello_l, 0
86 define i1 @test_simplify8(ptr %str_p) {
87 ; CHECK-LABEL: @test_simplify8(
88 ; CHECK-NEXT: [[CHAR0:%.*]] = load i32, ptr [[STR_P:%.*]], align 4
89 ; CHECK-NEXT: [[NE_NULL:%.*]] = icmp ne i32 [[CHAR0]], 0
90 ; CHECK-NEXT: ret i1 [[NE_NULL]]
92 %str_l = call i64 @wcslen(ptr %str_p)
93 %ne_null = icmp ne i64 %str_l, 0
97 define i64 @test_simplify9(i1 %x) {
98 ; CHECK-LABEL: @test_simplify9(
99 ; CHECK-NEXT: [[L:%.*]] = select i1 [[X:%.*]], i64 5, i64 6
100 ; CHECK-NEXT: ret i64 [[L]]
102 %s = select i1 %x, ptr @hello, ptr @longer
103 %l = call i64 @wcslen(ptr %s)
107 ; Check the case that should be simplified to a sub instruction.
108 ; wcslen(@hello + x) --> 5 - x
110 define i64 @test_simplify10(i32 %x) {
111 ; CHECK-LABEL: @test_simplify10(
112 ; CHECK-NEXT: [[TMP1:%.*]] = sext i32 [[X:%.*]] to i64
113 ; CHECK-NEXT: [[HELLO_L:%.*]] = sub nsw i64 5, [[TMP1]]
114 ; CHECK-NEXT: ret i64 [[HELLO_L]]
116 %hello_p = getelementptr inbounds [6 x i32], ptr @hello, i32 0, i32 %x
117 %hello_l = call i64 @wcslen(ptr %hello_p)
121 ; wcslen(@null_hello_mid + (x & 7)) --> 9 - (x & 7)
123 define i64 @test_simplify11(i32 %x) {
124 ; CHECK-LABEL: @test_simplify11(
125 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 7
126 ; CHECK-NEXT: [[NARROW:%.*]] = sub nuw nsw i32 9, [[AND]]
127 ; CHECK-NEXT: [[HELLO_L:%.*]] = zext nneg i32 [[NARROW]] to i64
128 ; CHECK-NEXT: ret i64 [[HELLO_L]]
131 %hello_p = getelementptr inbounds [13 x i32], ptr @null_hello_mid, i32 0, i32 %and
132 %hello_l = call i64 @wcslen(ptr %hello_p)
136 ; Check cases that shouldn't be simplified.
138 define i64 @test_no_simplify1() {
139 ; CHECK-LABEL: @test_no_simplify1(
140 ; CHECK-NEXT: [[A_L:%.*]] = call i64 @wcslen(ptr nonnull @a)
141 ; CHECK-NEXT: ret i64 [[A_L]]
143 %a_l = call i64 @wcslen(ptr @a)
147 ; wcslen(@null_hello + x) should not be simplified to a sub instruction.
149 define i64 @test_no_simplify2(i32 %x) {
150 ; CHECK-LABEL: @test_no_simplify2(
151 ; CHECK-NEXT: [[TMP1:%.*]] = sext i32 [[X:%.*]] to i64
152 ; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds [7 x i32], ptr @null_hello, i64 0, i64 [[TMP1]]
153 ; CHECK-NEXT: [[HELLO_L:%.*]] = call i64 @wcslen(ptr nonnull [[HELLO_P]])
154 ; CHECK-NEXT: ret i64 [[HELLO_L]]
156 %hello_p = getelementptr inbounds [7 x i32], ptr @null_hello, i32 0, i32 %x
157 %hello_l = call i64 @wcslen(ptr %hello_p)
161 define i64 @test_no_simplify2_no_null_opt(i32 %x) #0 {
162 ; CHECK-LABEL: @test_no_simplify2_no_null_opt(
163 ; CHECK-NEXT: [[TMP1:%.*]] = sext i32 [[X:%.*]] to i64
164 ; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds [7 x i32], ptr @null_hello, i64 0, i64 [[TMP1]]
165 ; CHECK-NEXT: [[HELLO_L:%.*]] = call i64 @wcslen(ptr [[HELLO_P]])
166 ; CHECK-NEXT: ret i64 [[HELLO_L]]
168 %hello_p = getelementptr inbounds [7 x i32], ptr @null_hello, i32 0, i32 %x
169 %hello_l = call i64 @wcslen(ptr %hello_p)
173 ; wcslen(@null_hello_mid + (x & 15)) should not be simplified to a sub instruction.
175 define i64 @test_no_simplify3(i32 %x) {
176 ; CHECK-LABEL: @test_no_simplify3(
177 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 15
178 ; CHECK-NEXT: [[TMP1:%.*]] = zext nneg i32 [[AND]] to i64
179 ; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds [13 x i32], ptr @null_hello_mid, i64 0, i64 [[TMP1]]
180 ; CHECK-NEXT: [[HELLO_L:%.*]] = call i64 @wcslen(ptr nonnull [[HELLO_P]])
181 ; CHECK-NEXT: ret i64 [[HELLO_L]]
183 %and = and i32 %x, 15
184 %hello_p = getelementptr inbounds [13 x i32], ptr @null_hello_mid, i32 0, i32 %and
185 %hello_l = call i64 @wcslen(ptr %hello_p)
189 define i64 @test_no_simplify3_no_null_opt(i32 %x) #0 {
190 ; CHECK-LABEL: @test_no_simplify3_no_null_opt(
191 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 15
192 ; CHECK-NEXT: [[TMP1:%.*]] = zext nneg i32 [[AND]] to i64
193 ; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds [13 x i32], ptr @null_hello_mid, i64 0, i64 [[TMP1]]
194 ; CHECK-NEXT: [[HELLO_L:%.*]] = call i64 @wcslen(ptr [[HELLO_P]])
195 ; CHECK-NEXT: ret i64 [[HELLO_L]]
197 %and = and i32 %x, 15
198 %hello_p = getelementptr inbounds [13 x i32], ptr @null_hello_mid, i32 0, i32 %and
199 %hello_l = call i64 @wcslen(ptr %hello_p)
203 @str16 = constant [1 x i16] [i16 0]
205 ; Fold the invalid call to zero. This is safer than letting the undefined
206 ; library call take place even though it prevents sanitizers from detecting
209 define i64 @test_simplify12() {
210 ; CHECK-LABEL: @test_simplify12(
211 ; CHECK-NEXT: ret i64 0
213 %l = call i64 @wcslen(ptr @str16)
217 @ws = constant [10 x i32] [i32 9, i32 8, i32 7, i32 6, i32 5, i32 4, i32 3, i32 2, i32 1, i32 0]
219 ; Fold wcslen(ws + 2) => 7.
220 define i64 @fold_wcslen_1() {
221 ; CHECK-LABEL: @fold_wcslen_1(
222 ; CHECK-NEXT: ret i64 7
224 %p = getelementptr inbounds [10 x i32], ptr @ws, i64 0, i64 2
225 %len = tail call i64 @wcslen(ptr %p)
229 ; Should not crash on this, and no optimization expected (idea is to get into
230 ; llvm::getConstantDataArrayInfo looking for an array with 32-bit elements but
231 ; with an offset that isn't a multiple of the element size).
232 define i64 @no_fold_wcslen_1() {
233 ; CHECK-LABEL: @no_fold_wcslen_1(
234 ; CHECK-NEXT: [[LEN:%.*]] = tail call i64 @wcslen(ptr nonnull getelementptr inbounds (i8, ptr @ws, i64 3))
235 ; CHECK-NEXT: ret i64 [[LEN]]
237 %p = getelementptr [15 x i8], ptr @ws, i64 0, i64 3
238 %len = tail call i64 @wcslen(ptr %p)
242 @s8 = constant [10 x i8] [i8 9, i8 8, i8 7, i8 6, i8 5, i8 4, i8 3, i8 2, i8 1, i8 0]
244 ; Should not crash on this, and no optimization expected (idea is to get into
245 ; llvm::getConstantDataArrayInfo looking for an array with 32-bit elements but
246 ; with an offset that isn't a multiple of the element size).
247 define i64 @no_fold_wcslen_2() {
248 ; CHECK-LABEL: @no_fold_wcslen_2(
249 ; CHECK-NEXT: [[LEN:%.*]] = tail call i64 @wcslen(ptr nonnull getelementptr inbounds (i8, ptr @s8, i64 3))
250 ; CHECK-NEXT: ret i64 [[LEN]]
252 %p = getelementptr [10 x i8], ptr @s8, i64 0, i64 3
253 %len = tail call i64 @wcslen(ptr %p)
257 attributes #0 = { null_pointer_is_valid }