xref: /llvm-project/llvm/test/Transforms/InstCombine/wcslen-1.ll (revision 23a239267e8a1d20ed10d3545feaf2a2bb70b085)
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; Test that the wcslen library call simplifier works correctly.
3;
4; RUN: opt < %s -passes=instcombine -S | FileCheck %s
5
6target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
7
8declare i64 @wcslen(ptr)
9
10!0 = !{i32 1, !"wchar_size", i32 4}
11!llvm.module.flags = !{!0}
12
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]
20
21define i64 @test_simplify1() {
22; CHECK-LABEL: @test_simplify1(
23; CHECK-NEXT:    ret i64 5
24;
25  %hello_l = call i64 @wcslen(ptr @hello)
26  ret i64 %hello_l
27}
28
29define i64 @test_simplify2() {
30; CHECK-LABEL: @test_simplify2(
31; CHECK-NEXT:    ret i64 0
32;
33  %null_l = call i64 @wcslen(ptr @null)
34  ret i64 %null_l
35}
36
37define i64 @test_simplify3() {
38; CHECK-LABEL: @test_simplify3(
39; CHECK-NEXT:    ret i64 0
40;
41  %null_hello_l = call i64 @wcslen(ptr @null_hello)
42  ret i64 %null_hello_l
43}
44
45define i64 @test_simplify4() {
46; CHECK-LABEL: @test_simplify4(
47; CHECK-NEXT:    ret i64 0
48;
49  %len = tail call i64 @wcslen(ptr @nullstring) nounwind
50  ret i64 %len
51}
52
53; Check wcslen(x) == 0 --> *x == 0.
54
55define i1 @test_simplify5() {
56; CHECK-LABEL: @test_simplify5(
57; CHECK-NEXT:    ret i1 false
58;
59  %hello_l = call i64 @wcslen(ptr @hello)
60  %eq_hello = icmp eq i64 %hello_l, 0
61  ret i1 %eq_hello
62}
63
64define 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]]
69;
70  %str_l = call i64 @wcslen(ptr %str_p)
71  %eq_null = icmp eq i64 %str_l, 0
72  ret i1 %eq_null
73}
74
75; Check wcslen(x) != 0 --> *x != 0.
76
77define i1 @test_simplify7() {
78; CHECK-LABEL: @test_simplify7(
79; CHECK-NEXT:    ret i1 true
80;
81  %hello_l = call i64 @wcslen(ptr @hello)
82  %ne_hello = icmp ne i64 %hello_l, 0
83  ret i1 %ne_hello
84}
85
86define 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]]
91;
92  %str_l = call i64 @wcslen(ptr %str_p)
93  %ne_null = icmp ne i64 %str_l, 0
94  ret i1 %ne_null
95}
96
97define 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]]
101;
102  %s = select i1 %x, ptr @hello, ptr @longer
103  %l = call i64 @wcslen(ptr %s)
104  ret i64 %l
105}
106
107; Check the case that should be simplified to a sub instruction.
108; wcslen(@hello + x) --> 5 - x
109
110define 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]]
115;
116  %hello_p = getelementptr inbounds [6 x i32], ptr @hello, i32 0, i32 %x
117  %hello_l = call i64 @wcslen(ptr %hello_p)
118  ret i64 %hello_l
119}
120
121; wcslen(@null_hello_mid + (x & 7)) --> 9 - (x & 7)
122
123define 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]]
129;
130  %and = and i32 %x, 7
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)
133  ret i64 %hello_l
134}
135
136; Check cases that shouldn't be simplified.
137
138define 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]]
142;
143  %a_l = call i64 @wcslen(ptr @a)
144  ret i64 %a_l
145}
146
147; wcslen(@null_hello + x) should not be simplified to a sub instruction.
148
149define 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]]
155;
156  %hello_p = getelementptr inbounds [7 x i32], ptr @null_hello, i32 0, i32 %x
157  %hello_l = call i64 @wcslen(ptr %hello_p)
158  ret i64 %hello_l
159}
160
161define 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]]
167;
168  %hello_p = getelementptr inbounds [7 x i32], ptr @null_hello, i32 0, i32 %x
169  %hello_l = call i64 @wcslen(ptr %hello_p)
170  ret i64 %hello_l
171}
172
173; wcslen(@null_hello_mid + (x & 15)) should not be simplified to a sub instruction.
174
175define 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 nuw [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]]
182;
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)
186  ret i64 %hello_l
187}
188
189define 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 nuw [13 x i32], ptr @null_hello_mid, i64 0, i64 [[TMP1]]
194; CHECK-NEXT:    [[HELLO_L:%.*]] = call i64 @wcslen(ptr nonnull [[HELLO_P]])
195; CHECK-NEXT:    ret i64 [[HELLO_L]]
196;
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)
200  ret i64 %hello_l
201}
202
203@str16 = constant [1 x i16] [i16 0]
204
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
207; it.
208
209define i64 @test_simplify12() {
210; CHECK-LABEL: @test_simplify12(
211; CHECK-NEXT:    ret i64 0
212;
213  %l = call i64 @wcslen(ptr @str16)
214  ret i64 %l
215}
216
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]
218
219; Fold wcslen(ws + 2) => 7.
220define i64 @fold_wcslen_1() {
221; CHECK-LABEL: @fold_wcslen_1(
222; CHECK-NEXT:    ret i64 7
223;
224  %p = getelementptr inbounds [10 x i32], ptr @ws, i64 0, i64 2
225  %len = tail call i64 @wcslen(ptr %p)
226  ret i64 %len
227}
228
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).
232define i64 @no_fold_wcslen_1() {
233; CHECK-LABEL: @no_fold_wcslen_1(
234; CHECK-NEXT:    [[LEN:%.*]] = tail call i64 @wcslen(ptr nonnull getelementptr inbounds nuw (i8, ptr @ws, i64 3))
235; CHECK-NEXT:    ret i64 [[LEN]]
236;
237  %p = getelementptr [15 x i8], ptr @ws, i64 0, i64 3
238  %len = tail call i64 @wcslen(ptr %p)
239  ret i64 %len
240}
241
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]
243
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).
247define i64 @no_fold_wcslen_2() {
248; CHECK-LABEL: @no_fold_wcslen_2(
249; CHECK-NEXT:    [[LEN:%.*]] = tail call i64 @wcslen(ptr nonnull getelementptr inbounds nuw (i8, ptr @s8, i64 3))
250; CHECK-NEXT:    ret i64 [[LEN]]
251;
252  %p = getelementptr [10 x i8], ptr @s8, i64 0, i64 3
253  %len = tail call i64 @wcslen(ptr %p)
254  ret i64 %len
255}
256
257attributes #0 = { null_pointer_is_valid }
258