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 -instcombine -S | FileCheck %s
6 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
8 declare i64 @wcslen(i32*)
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_p = getelementptr [6 x i32], [6 x i32]* @hello, i64 0, i64 0
26 %hello_l = call i64 @wcslen(i32* %hello_p)
30 define i64 @test_simplify2() {
31 ; CHECK-LABEL: @test_simplify2(
32 ; CHECK-NEXT: ret i64 0
34 %null_p = getelementptr [1 x i32], [1 x i32]* @null, i64 0, i64 0
35 %null_l = call i64 @wcslen(i32* %null_p)
39 define i64 @test_simplify3() {
40 ; CHECK-LABEL: @test_simplify3(
41 ; CHECK-NEXT: ret i64 0
43 %null_hello_p = getelementptr [7 x i32], [7 x i32]* @null_hello, i64 0, i64 0
44 %null_hello_l = call i64 @wcslen(i32* %null_hello_p)
48 define i64 @test_simplify4() {
49 ; CHECK-LABEL: @test_simplify4(
50 ; CHECK-NEXT: ret i64 0
52 %len = tail call i64 @wcslen(i32* @nullstring) nounwind
56 ; Check wcslen(x) == 0 --> *x == 0.
58 define i1 @test_simplify5() {
59 ; CHECK-LABEL: @test_simplify5(
60 ; CHECK-NEXT: ret i1 false
62 %hello_p = getelementptr [6 x i32], [6 x i32]* @hello, i64 0, i64 0
63 %hello_l = call i64 @wcslen(i32* %hello_p)
64 %eq_hello = icmp eq i64 %hello_l, 0
68 define i1 @test_simplify6(i32* %str_p) {
69 ; CHECK-LABEL: @test_simplify6(
70 ; CHECK-NEXT: [[STRLENFIRST:%.*]] = load i32, i32* [[STR_P:%.*]], align 4
71 ; CHECK-NEXT: [[EQ_NULL:%.*]] = icmp eq i32 [[STRLENFIRST]], 0
72 ; CHECK-NEXT: ret i1 [[EQ_NULL]]
74 %str_l = call i64 @wcslen(i32* %str_p)
75 %eq_null = icmp eq i64 %str_l, 0
79 ; Check wcslen(x) != 0 --> *x != 0.
81 define i1 @test_simplify7() {
82 ; CHECK-LABEL: @test_simplify7(
83 ; CHECK-NEXT: ret i1 true
85 %hello_p = getelementptr [6 x i32], [6 x i32]* @hello, i64 0, i64 0
86 %hello_l = call i64 @wcslen(i32* %hello_p)
87 %ne_hello = icmp ne i64 %hello_l, 0
91 define i1 @test_simplify8(i32* %str_p) {
92 ; CHECK-LABEL: @test_simplify8(
93 ; CHECK-NEXT: [[STRLENFIRST:%.*]] = load i32, i32* [[STR_P:%.*]], align 4
94 ; CHECK-NEXT: [[NE_NULL:%.*]] = icmp ne i32 [[STRLENFIRST]], 0
95 ; CHECK-NEXT: ret i1 [[NE_NULL]]
97 %str_l = call i64 @wcslen(i32* %str_p)
98 %ne_null = icmp ne i64 %str_l, 0
102 define i64 @test_simplify9(i1 %x) {
103 ; CHECK-LABEL: @test_simplify9(
104 ; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[X:%.*]], i64 5, i64 6
105 ; CHECK-NEXT: ret i64 [[TMP1]]
107 %hello = getelementptr [6 x i32], [6 x i32]* @hello, i64 0, i64 0
108 %longer = getelementptr [7 x i32], [7 x i32]* @longer, i64 0, i64 0
109 %s = select i1 %x, i32* %hello, i32* %longer
110 %l = call i64 @wcslen(i32* %s)
114 ; Check the case that should be simplified to a sub instruction.
115 ; wcslen(@hello + x) --> 5 - x
117 define i64 @test_simplify10(i32 %x) {
118 ; CHECK-LABEL: @test_simplify10(
119 ; CHECK-NEXT: [[TMP1:%.*]] = sext i32 [[X:%.*]] to i64
120 ; CHECK-NEXT: [[TMP2:%.*]] = sub nsw i64 5, [[TMP1]]
121 ; CHECK-NEXT: ret i64 [[TMP2]]
123 %hello_p = getelementptr inbounds [6 x i32], [6 x i32]* @hello, i32 0, i32 %x
124 %hello_l = call i64 @wcslen(i32* %hello_p)
128 ; wcslen(@null_hello_mid + (x & 7)) --> 9 - (x & 7)
130 define i64 @test_simplify11(i32 %x) {
131 ; CHECK-LABEL: @test_simplify11(
132 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 7
133 ; CHECK-NEXT: [[NARROW:%.*]] = sub nuw nsw i32 9, [[AND]]
134 ; CHECK-NEXT: [[TMP1:%.*]] = zext i32 [[NARROW]] to i64
135 ; CHECK-NEXT: ret i64 [[TMP1]]
138 %hello_p = getelementptr inbounds [13 x i32], [13 x i32]* @null_hello_mid, i32 0, i32 %and
139 %hello_l = call i64 @wcslen(i32* %hello_p)
143 ; Check cases that shouldn't be simplified.
145 define i64 @test_no_simplify1() {
146 ; CHECK-LABEL: @test_no_simplify1(
147 ; CHECK-NEXT: [[A_L:%.*]] = call i64 @wcslen(i32* getelementptr inbounds ([32 x i32], [32 x i32]* @a, i64 0, i64 0))
148 ; CHECK-NEXT: ret i64 [[A_L]]
150 %a_p = getelementptr [32 x i32], [32 x i32]* @a, i64 0, i64 0
151 %a_l = call i64 @wcslen(i32* %a_p)
155 ; wcslen(@null_hello + x) should not be simplified to a sub instruction.
157 define i64 @test_no_simplify2(i32 %x) {
158 ; CHECK-LABEL: @test_no_simplify2(
159 ; CHECK-NEXT: [[TMP1:%.*]] = sext i32 [[X:%.*]] to i64
160 ; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds [7 x i32], [7 x i32]* @null_hello, i64 0, i64 [[TMP1]]
161 ; CHECK-NEXT: [[HELLO_L:%.*]] = call i64 @wcslen(i32* nonnull [[HELLO_P]])
162 ; CHECK-NEXT: ret i64 [[HELLO_L]]
164 %hello_p = getelementptr inbounds [7 x i32], [7 x i32]* @null_hello, i32 0, i32 %x
165 %hello_l = call i64 @wcslen(i32* %hello_p)
169 define i64 @test_no_simplify2_no_null_opt(i32 %x) #0 {
170 ; CHECK-LABEL: @test_no_simplify2_no_null_opt(
171 ; CHECK-NEXT: [[TMP1:%.*]] = sext i32 [[X:%.*]] to i64
172 ; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds [7 x i32], [7 x i32]* @null_hello, i64 0, i64 [[TMP1]]
173 ; CHECK-NEXT: [[HELLO_L:%.*]] = call i64 @wcslen(i32* [[HELLO_P]])
174 ; CHECK-NEXT: ret i64 [[HELLO_L]]
176 %hello_p = getelementptr inbounds [7 x i32], [7 x i32]* @null_hello, i32 0, i32 %x
177 %hello_l = call i64 @wcslen(i32* %hello_p)
181 ; wcslen(@null_hello_mid + (x & 15)) should not be simplified to a sub instruction.
183 define i64 @test_no_simplify3(i32 %x) {
184 ; CHECK-LABEL: @test_no_simplify3(
185 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 15
186 ; CHECK-NEXT: [[TMP1:%.*]] = zext i32 [[AND]] to i64
187 ; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds [13 x i32], [13 x i32]* @null_hello_mid, i64 0, i64 [[TMP1]]
188 ; CHECK-NEXT: [[HELLO_L:%.*]] = call i64 @wcslen(i32* nonnull [[HELLO_P]])
189 ; CHECK-NEXT: ret i64 [[HELLO_L]]
191 %and = and i32 %x, 15
192 %hello_p = getelementptr inbounds [13 x i32], [13 x i32]* @null_hello_mid, i32 0, i32 %and
193 %hello_l = call i64 @wcslen(i32* %hello_p)
197 define i64 @test_no_simplify3_no_null_opt(i32 %x) #0 {
198 ; CHECK-LABEL: @test_no_simplify3_no_null_opt(
199 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 15
200 ; CHECK-NEXT: [[TMP1:%.*]] = zext i32 [[AND]] to i64
201 ; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds [13 x i32], [13 x i32]* @null_hello_mid, i64 0, i64 [[TMP1]]
202 ; CHECK-NEXT: [[HELLO_L:%.*]] = call i64 @wcslen(i32* [[HELLO_P]])
203 ; CHECK-NEXT: ret i64 [[HELLO_L]]
205 %and = and i32 %x, 15
206 %hello_p = getelementptr inbounds [13 x i32], [13 x i32]* @null_hello_mid, i32 0, i32 %and
207 %hello_l = call i64 @wcslen(i32* %hello_p)
211 @str16 = constant [1 x i16] [i16 0]
213 define i64 @test_no_simplify4() {
214 ; CHECK-LABEL: @test_no_simplify4(
215 ; CHECK-NEXT: [[L:%.*]] = call i64 @wcslen(i32* bitcast ([1 x i16]* @str16 to i32*))
216 ; CHECK-NEXT: ret i64 [[L]]
218 %l = call i64 @wcslen(i32* bitcast ([1 x i16]* @str16 to i32*))
222 attributes #0 = { "null-pointer-is-valid"="true" }