1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; Test a pile of objectsize bounds checking.
3 ; RUN: opt < %s -instcombine -S | FileCheck %s
4 ; We need target data to get the sizes of the arrays and structures.
5 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"
7 @a = private global [60 x i8] zeroinitializer, align 1 ; <[60 x i8]*>
8 @.str = private constant [8 x i8] c"abcdefg\00" ; <[8 x i8]*>
9 define i32 @foo() nounwind {
11 ; CHECK-NEXT: ret i32 60
13 %1 = call i32 @llvm.objectsize.i32.p0i8(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @a, i32 0, i32 0), i1 false, i1 false, i1 false)
17 define i8* @bar() nounwind {
20 ; CHECK-NEXT: [[RETVAL:%.*]] = alloca i8*, align 4
21 ; CHECK-NEXT: br i1 true, label [[COND_TRUE:%.*]], label [[COND_FALSE:%.*]]
23 ; CHECK-NEXT: [[TMP0:%.*]] = load i8*, i8** [[RETVAL]], align 4
24 ; CHECK-NEXT: ret i8* [[TMP0]]
26 ; CHECK-NEXT: ret i8* undef
30 %0 = call i32 @llvm.objectsize.i32.p0i8(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @a, i32 0, i32 0), i1 false, i1 false, i1 false)
31 %cmp = icmp ne i32 %0, -1
32 br i1 %cmp, label %cond.true, label %cond.false
35 %1 = load i8*, i8** %retval
39 %2 = load i8*, i8** %retval
43 define i32 @f() nounwind {
45 ; CHECK-NEXT: ret i32 0
47 %1 = call i32 @llvm.objectsize.i32.p0i8(i8* getelementptr ([60 x i8], [60 x i8]* @a, i32 1, i32 0), i1 false, i1 false, i1 false)
51 @window = external global [0 x i8]
53 define i1 @baz() nounwind {
55 ; CHECK-NEXT: [[TMP1:%.*]] = tail call i32 @llvm.objectsize.i32.p0i8(i8* getelementptr inbounds ([0 x i8], [0 x i8]* @window, i32 0, i32 0), i1 false, i1 false, i1 false)
56 ; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i32 [[TMP1]], -1
57 ; CHECK-NEXT: ret i1 [[TMP2]]
59 %1 = tail call i32 @llvm.objectsize.i32.p0i8(i8* getelementptr inbounds ([0 x i8], [0 x i8]* @window, i32 0, i32 0), i1 false, i1 false, i1 false)
60 %2 = icmp eq i32 %1, -1
64 define void @test1(i8* %q, i32 %x) nounwind noinline {
65 ; CHECK-LABEL: @test1(
67 ; CHECK-NEXT: [[TMP0:%.*]] = call i32 @llvm.objectsize.i32.p0i8(i8* getelementptr inbounds ([0 x i8], [0 x i8]* @window, i32 0, i32 10), i1 false, i1 false, i1 false)
68 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[TMP0]], -1
69 ; CHECK-NEXT: br i1 [[TMP1]], label %"47", label %"46"
71 ; CHECK-NEXT: unreachable
73 ; CHECK-NEXT: unreachable
76 %0 = call i32 @llvm.objectsize.i32.p0i8(i8* getelementptr inbounds ([0 x i8], [0 x i8]* @window, i32 0, i32 10), i1 false, i1 false, i1 false) ; <i64> [#uses=1]
77 %1 = icmp eq i32 %0, -1 ; <i1> [#uses=1]
78 br i1 %1, label %"47", label %"46"
80 "46": ; preds = %entry
83 "47": ; preds = %entry
87 @.str5 = private constant [9 x i32] [i32 97, i32 98, i32 99, i32 100, i32 0, i32
88 101, i32 102, i32 103, i32 0], align 4
89 define i32 @test2() nounwind {
90 ; CHECK-LABEL: @test2(
91 ; CHECK-NEXT: ret i32 34
93 %1 = call i32 @llvm.objectsize.i32.p0i8(i8* getelementptr (i8, i8* bitcast ([9 x i32]* @.str5 to i8*), i32 2), i1 false, i1 false, i1 false)
98 @array = internal global [480 x float] zeroinitializer ; <[480 x float]*> [#uses=1]
100 declare i8* @__memcpy_chk(i8*, i8*, i32, i32) nounwind
102 declare i32 @llvm.objectsize.i32.p0i8(i8*, i1, i1, i1) nounwind readonly
104 declare i32 @llvm.objectsize.i32.p1i8(i8 addrspace(1)*, i1, i1, i1) nounwind readonly
106 declare i8* @__inline_memcpy_chk(i8*, i8*, i32) nounwind inlinehint
108 define void @test3() nounwind {
109 ; CHECK-LABEL: @test3(
111 ; CHECK-NEXT: br i1 undef, label [[BB11:%.*]], label [[BB12:%.*]]
113 ; CHECK-NEXT: unreachable
115 ; CHECK-NEXT: [[TMP0:%.*]] = call i8* @__inline_memcpy_chk(i8* bitcast (float* getelementptr inbounds ([480 x float], [480 x float]* @array, i32 0, i32 1) to i8*), i8* undef, i32 512) #3
116 ; CHECK-NEXT: unreachable
119 br i1 undef, label %bb11, label %bb12
122 %0 = getelementptr inbounds float, float* getelementptr inbounds ([480 x float], [480 x float]* @array, i32 0, i32 128), i32 -127 ; <float*> [#uses=1]
123 %1 = bitcast float* %0 to i8* ; <i8*> [#uses=1]
124 %2 = call i32 @llvm.objectsize.i32.p0i8(i8* %1, i1 false, i1 false, i1 false) ; <i32> [#uses=1]
125 %3 = call i8* @__memcpy_chk(i8* undef, i8* undef, i32 512, i32 %2) nounwind ; <i8*> [#uses=0]
129 %4 = getelementptr inbounds float, float* getelementptr inbounds ([480 x float], [480 x float]* @array, i32 0, i32 128), i32 -127 ; <float*> [#uses=1]
130 %5 = bitcast float* %4 to i8* ; <i8*> [#uses=1]
131 %6 = call i8* @__inline_memcpy_chk(i8* %5, i8* undef, i32 512) nounwind inlinehint ; <i8*> [#uses=0]
137 %struct.data = type { [100 x i32], [100 x i32], [1024 x i8] }
139 define i32 @test4(i8** %esc) nounwind ssp {
140 ; CHECK-LABEL: @test4(
142 ; CHECK-NEXT: [[TMP0:%.*]] = alloca [[STRUCT_DATA:%.*]], align 8
143 ; CHECK-NEXT: [[TMP1:%.*]] = bitcast %struct.data* [[TMP0]] to i8*
144 ; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* nonnull align 8 dereferenceable(1824) [[TMP1]], i8 0, i32 1824, i1 false) #0
145 ; CHECK-NEXT: [[TMP2:%.*]] = bitcast i8** [[ESC:%.*]] to %struct.data**
146 ; CHECK-NEXT: store %struct.data* [[TMP0]], %struct.data** [[TMP2]], align 4
147 ; CHECK-NEXT: ret i32 0
150 %0 = alloca %struct.data, align 8
151 %1 = bitcast %struct.data* %0 to i8*
152 %2 = call i32 @llvm.objectsize.i32.p0i8(i8* %1, i1 false, i1 false, i1 false) nounwind
153 %3 = call i8* @__memset_chk(i8* %1, i32 0, i32 1824, i32 %2) nounwind
154 store i8* %1, i8** %esc
159 @s = external global i8*
161 define i8* @test5(i32 %n) nounwind ssp {
162 ; CHECK-LABEL: @test5(
164 ; CHECK-NEXT: [[TMP0:%.*]] = tail call noalias dereferenceable_or_null(20) i8* @malloc(i32 20) #0
165 ; CHECK-NEXT: [[TMP1:%.*]] = load i8*, i8** @s, align 8
166 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* nonnull align 1 dereferenceable(10) [[TMP0]], i8* nonnull align 1 dereferenceable(10) [[TMP1]], i32 10, i1 false) #0
167 ; CHECK-NEXT: ret i8* [[TMP0]]
170 %0 = tail call noalias i8* @malloc(i32 20) nounwind
171 %1 = tail call i32 @llvm.objectsize.i32.p0i8(i8* %0, i1 false, i1 false, i1 false)
172 %2 = load i8*, i8** @s, align 8
173 %3 = tail call i8* @__memcpy_chk(i8* %0, i8* %2, i32 10, i32 %1) nounwind
177 define void @test6(i32 %n) nounwind ssp {
178 ; CHECK-LABEL: @test6(
180 ; CHECK-NEXT: [[TMP0:%.*]] = tail call noalias dereferenceable_or_null(20) i8* @malloc(i32 20) #0
181 ; CHECK-NEXT: [[TMP1:%.*]] = load i8*, i8** @s, align 8
182 ; CHECK-NEXT: [[TMP2:%.*]] = tail call i8* @__memcpy_chk(i8* [[TMP0]], i8* [[TMP1]], i32 30, i32 20) #0
183 ; CHECK-NEXT: ret void
186 %0 = tail call noalias i8* @malloc(i32 20) nounwind
187 %1 = tail call i32 @llvm.objectsize.i32.p0i8(i8* %0, i1 false, i1 false, i1 false)
188 %2 = load i8*, i8** @s, align 8
189 %3 = tail call i8* @__memcpy_chk(i8* %0, i8* %2, i32 30, i32 %1) nounwind
193 declare i8* @__memset_chk(i8*, i32, i32, i32) nounwind
195 declare noalias i8* @malloc(i32) nounwind
197 define i32 @test7(i8** %esc) {
198 ; CHECK-LABEL: @test7(
199 ; CHECK-NEXT: [[ALLOC:%.*]] = call noalias dereferenceable_or_null(48) i8* @malloc(i32 48) #0
200 ; CHECK-NEXT: store i8* [[ALLOC]], i8** [[ESC:%.*]], align 4
201 ; CHECK-NEXT: ret i32 32
203 %alloc = call noalias i8* @malloc(i32 48) nounwind
204 store i8* %alloc, i8** %esc
205 %gep = getelementptr inbounds i8, i8* %alloc, i32 16
206 %objsize = call i32 @llvm.objectsize.i32.p0i8(i8* %gep, i1 false, i1 false, i1 false) nounwind readonly
210 declare noalias i8* @calloc(i32, i32) nounwind
212 define i32 @test8(i8** %esc) {
213 ; CHECK-LABEL: @test8(
214 ; CHECK-NEXT: [[ALLOC:%.*]] = call noalias dereferenceable_or_null(35) i8* @calloc(i32 5, i32 7) #0
215 ; CHECK-NEXT: store i8* [[ALLOC]], i8** [[ESC:%.*]], align 4
216 ; CHECK-NEXT: ret i32 30
218 %alloc = call noalias i8* @calloc(i32 5, i32 7) nounwind
219 store i8* %alloc, i8** %esc
220 %gep = getelementptr inbounds i8, i8* %alloc, i32 5
221 %objsize = call i32 @llvm.objectsize.i32.p0i8(i8* %gep, i1 false, i1 false, i1 false) nounwind readonly
225 declare noalias i8* @strdup(i8* nocapture) nounwind
226 declare noalias i8* @strndup(i8* nocapture, i32) nounwind
228 define i32 @test9(i8** %esc) {
229 ; CHECK-LABEL: @test9(
230 ; CHECK-NEXT: [[CALL:%.*]] = tail call dereferenceable_or_null(8) i8* @strdup(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i32 0, i32 0)) #0
231 ; CHECK-NEXT: store i8* [[CALL]], i8** [[ESC:%.*]], align 8
232 ; CHECK-NEXT: ret i32 8
234 %call = tail call i8* @strdup(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0)) nounwind
235 store i8* %call, i8** %esc, align 8
236 %1 = tail call i32 @llvm.objectsize.i32.p0i8(i8* %call, i1 true, i1 false, i1 false)
240 define i32 @test10(i8** %esc) {
241 ; CHECK-LABEL: @test10(
242 ; CHECK-NEXT: [[CALL:%.*]] = tail call dereferenceable_or_null(4) i8* @strndup(i8* dereferenceable(8) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i32 0, i32 0), i32 3) #0
243 ; CHECK-NEXT: store i8* [[CALL]], i8** [[ESC:%.*]], align 8
244 ; CHECK-NEXT: ret i32 4
246 %call = tail call i8* @strndup(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 3) nounwind
247 store i8* %call, i8** %esc, align 8
248 %1 = tail call i32 @llvm.objectsize.i32.p0i8(i8* %call, i1 true, i1 false, i1 false)
252 define i32 @test11(i8** %esc) {
253 ; CHECK-LABEL: @test11(
254 ; CHECK-NEXT: [[STRDUP:%.*]] = call dereferenceable_or_null(8) i8* @strdup(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i32 0, i32 0))
255 ; CHECK-NEXT: store i8* [[STRDUP]], i8** [[ESC:%.*]], align 8
256 ; CHECK-NEXT: ret i32 8
258 %call = tail call i8* @strndup(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 7) nounwind
259 store i8* %call, i8** %esc, align 8
260 %1 = tail call i32 @llvm.objectsize.i32.p0i8(i8* %call, i1 true, i1 false, i1 false)
264 define i32 @test12(i8** %esc) {
265 ; CHECK-LABEL: @test12(
266 ; CHECK-NEXT: [[STRDUP:%.*]] = call dereferenceable_or_null(8) i8* @strdup(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i32 0, i32 0))
267 ; CHECK-NEXT: store i8* [[STRDUP]], i8** [[ESC:%.*]], align 8
268 ; CHECK-NEXT: ret i32 8
270 %call = tail call i8* @strndup(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 8) nounwind
271 store i8* %call, i8** %esc, align 8
272 %1 = tail call i32 @llvm.objectsize.i32.p0i8(i8* %call, i1 true, i1 false, i1 false)
276 define i32 @test13(i8** %esc) {
277 ; CHECK-LABEL: @test13(
278 ; CHECK-NEXT: [[STRDUP:%.*]] = call dereferenceable_or_null(8) i8* @strdup(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i32 0, i32 0))
279 ; CHECK-NEXT: store i8* [[STRDUP]], i8** [[ESC:%.*]], align 8
280 ; CHECK-NEXT: ret i32 8
282 %call = tail call i8* @strndup(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 57) nounwind
283 store i8* %call, i8** %esc, align 8
284 %1 = tail call i32 @llvm.objectsize.i32.p0i8(i8* %call, i1 true, i1 false, i1 false)
288 @globalalias = internal alias [60 x i8], [60 x i8]* @a
290 define i32 @test18() {
291 ; CHECK-LABEL: @test18(
292 ; CHECK-NEXT: ret i32 60
294 %bc = bitcast [60 x i8]* @globalalias to i8*
295 %1 = call i32 @llvm.objectsize.i32.p0i8(i8* %bc, i1 false, i1 false, i1 false)
299 @globalalias2 = weak alias [60 x i8], [60 x i8]* @a
301 define i32 @test19() {
302 ; CHECK-LABEL: @test19(
303 ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.objectsize.i32.p0i8(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @globalalias2, i32 0, i32 0), i1 false, i1 false, i1 false)
304 ; CHECK-NEXT: ret i32 [[TMP1]]
306 %bc = bitcast [60 x i8]* @globalalias2 to i8*
307 %1 = call i32 @llvm.objectsize.i32.p0i8(i8* %bc, i1 false, i1 false, i1 false)
311 define i32 @test20() {
312 ; CHECK-LABEL: @test20(
313 ; CHECK-NEXT: ret i32 0
315 %1 = call i32 @llvm.objectsize.i32.p0i8(i8* null, i1 false, i1 false, i1 false)
319 define i32 @test21() {
320 ; CHECK-LABEL: @test21(
321 ; CHECK-NEXT: ret i32 0
323 %1 = call i32 @llvm.objectsize.i32.p0i8(i8* null, i1 true, i1 false, i1 false)
327 define i32 @test22() {
328 ; CHECK-LABEL: @test22(
329 ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.objectsize.i32.p0i8(i8* null, i1 false, i1 true, i1 false)
330 ; CHECK-NEXT: ret i32 [[TMP1]]
332 %1 = call i32 @llvm.objectsize.i32.p0i8(i8* null, i1 false, i1 true, i1 false)
336 define i32 @test23() {
337 ; CHECK-LABEL: @test23(
338 ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.objectsize.i32.p0i8(i8* null, i1 true, i1 true, i1 false)
339 ; CHECK-NEXT: ret i32 [[TMP1]]
341 %1 = call i32 @llvm.objectsize.i32.p0i8(i8* null, i1 true, i1 true, i1 false)
345 ; 1 is an arbitrary non-zero address space.
346 define i32 @test24() {
347 ; CHECK-LABEL: @test24(
348 ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.objectsize.i32.p1i8(i8 addrspace(1)* null, i1 false, i1 false, i1 false)
349 ; CHECK-NEXT: ret i32 [[TMP1]]
351 %1 = call i32 @llvm.objectsize.i32.p1i8(i8 addrspace(1)* null, i1 false,
356 define i32 @test25() {
357 ; CHECK-LABEL: @test25(
358 ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.objectsize.i32.p1i8(i8 addrspace(1)* null, i1 true, i1 false, i1 false)
359 ; CHECK-NEXT: ret i32 [[TMP1]]
361 %1 = call i32 @llvm.objectsize.i32.p1i8(i8 addrspace(1)* null, i1 true,
366 define i32 @test26() {
367 ; CHECK-LABEL: @test26(
368 ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.objectsize.i32.p1i8(i8 addrspace(1)* null, i1 false, i1 true, i1 false)
369 ; CHECK-NEXT: ret i32 [[TMP1]]
371 %1 = call i32 @llvm.objectsize.i32.p1i8(i8 addrspace(1)* null, i1 false,
376 define i32 @test27() {
377 ; CHECK-LABEL: @test27(
378 ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.objectsize.i32.p1i8(i8 addrspace(1)* null, i1 true, i1 true, i1 false)
379 ; CHECK-NEXT: ret i32 [[TMP1]]
381 %1 = call i32 @llvm.objectsize.i32.p1i8(i8 addrspace(1)* null, i1 true,