1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; Test a pile of objectsize bounds checking.
3 ; RUN: opt < %s -passes=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 ; <ptr>
8 @.str = private constant [8 x i8] c"abcdefg\00" ; <ptr>
9 define i32 @foo() nounwind {
11 ; CHECK-NEXT: ret i32 60
13 %1 = call i32 @llvm.objectsize.i32.p0(ptr @a, i1 false, i1 false, i1 false)
17 define ptr @bar() nounwind {
20 ; CHECK-NEXT: [[RETVAL:%.*]] = alloca ptr, align 4
21 ; CHECK-NEXT: br i1 true, label [[COND_TRUE:%.*]], label [[COND_FALSE:%.*]]
23 ; CHECK-NEXT: [[TMP0:%.*]] = load ptr, ptr [[RETVAL]], align 4
24 ; CHECK-NEXT: ret ptr [[TMP0]]
26 ; CHECK-NEXT: ret ptr poison
30 %0 = call i32 @llvm.objectsize.i32.p0(ptr @a, 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 ptr, ptr %retval
39 %2 = load ptr, ptr %retval
43 define i32 @f() nounwind {
45 ; CHECK-NEXT: ret i32 0
47 %1 = call i32 @llvm.objectsize.i32.p0(ptr getelementptr ([60 x i8], ptr @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.p0(ptr @window, 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.p0(ptr @window, i1 false, i1 false, i1 false)
60 %2 = icmp eq i32 %1, -1
64 define void @test1(ptr %q, i32 %x) nounwind noinline {
65 ; CHECK-LABEL: @test1(
67 ; CHECK-NEXT: [[TMP0:%.*]] = call i32 @llvm.objectsize.i32.p0(ptr getelementptr inbounds ([0 x i8], ptr @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.p0(ptr getelementptr inbounds ([0 x i8], ptr @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.p0(ptr getelementptr (i8, ptr @.str5, i32 2), i1 false, i1 false, i1 false)
98 @array = internal global [480 x float] zeroinitializer ; <ptr> [#uses=1]
100 declare ptr @__memcpy_chk(ptr, ptr, i32, i32) nounwind
102 declare i32 @llvm.objectsize.i32.p0(ptr, i1, i1, i1) nounwind readonly
104 declare i32 @llvm.objectsize.i32.p1(ptr addrspace(1), i1, i1, i1) nounwind readonly
106 declare ptr @__inline_memcpy_chk(ptr, ptr, i32) nounwind inlinehint
108 define void @test3(i1 %c1, ptr %ptr1, ptr %ptr2, ptr %ptr3) nounwind {
109 ; CHECK-LABEL: @test3(
111 ; CHECK-NEXT: br i1 [[C1:%.*]], label [[BB11:%.*]], label [[BB12:%.*]]
113 ; CHECK-NEXT: unreachable
115 ; CHECK-NEXT: [[TMP0:%.*]] = call ptr @__inline_memcpy_chk(ptr nonnull getelementptr inbounds ([480 x float], ptr @array, i32 0, i32 1), ptr [[PTR3:%.*]], i32 512) #[[ATTR3:[0-9]+]]
116 ; CHECK-NEXT: unreachable
119 br i1 %c1, label %bb11, label %bb12
122 %0 = getelementptr inbounds float, ptr getelementptr inbounds ([480 x float], ptr @array, i32 0, i32 128), i32 -127 ; <ptr> [#uses=1]
123 %1 = call i32 @llvm.objectsize.i32.p0(ptr %0, i1 false, i1 false, i1 false) ; <i32> [#uses=1]
124 %2 = call ptr @__memcpy_chk(ptr %ptr1, ptr %ptr2, i32 512, i32 %1) nounwind ; <ptr> [#uses=0]
128 %3 = getelementptr inbounds float, ptr getelementptr inbounds ([480 x float], ptr @array, i32 0, i32 128), i32 -127 ; <ptr> [#uses=1]
129 %4 = call ptr @__inline_memcpy_chk(ptr %3, ptr %ptr3, i32 512) nounwind inlinehint ; <ptr> [#uses=0]
135 %struct.data = type { [100 x i32], [100 x i32], [1024 x i8] }
137 define i32 @test4(ptr %esc) nounwind ssp {
138 ; CHECK-LABEL: @test4(
140 ; CHECK-NEXT: [[TMP0:%.*]] = alloca [[STRUCT_DATA:%.*]], align 8
141 ; CHECK-NEXT: call void @llvm.memset.p0.i32(ptr noundef nonnull align 8 dereferenceable(1824) [[TMP0]], i8 0, i32 1824, i1 false) #[[ATTR0:[0-9]+]]
142 ; CHECK-NEXT: store ptr [[TMP0]], ptr [[ESC:%.*]], align 4
143 ; CHECK-NEXT: ret i32 0
146 %0 = alloca %struct.data, align 8
147 %1 = call i32 @llvm.objectsize.i32.p0(ptr %0, i1 false, i1 false, i1 false) nounwind
148 %2 = call ptr @__memset_chk(ptr %0, i32 0, i32 1824, i32 %1) nounwind
149 store ptr %0, ptr %esc
154 @s = external global ptr
156 define ptr @test5(i32 %n) nounwind ssp {
157 ; CHECK-LABEL: @test5(
159 ; CHECK-NEXT: [[TMP0:%.*]] = tail call noalias dereferenceable_or_null(20) ptr @malloc(i32 20) #[[ATTR0]]
160 ; CHECK-NEXT: [[TMP1:%.*]] = load ptr, ptr @s, align 8
161 ; CHECK-NEXT: tail call void @llvm.memcpy.p0.p0.i32(ptr noundef nonnull align 1 dereferenceable(10) [[TMP0]], ptr noundef nonnull align 1 dereferenceable(10) [[TMP1]], i32 10, i1 false) #[[ATTR0]]
162 ; CHECK-NEXT: ret ptr [[TMP0]]
165 %0 = tail call noalias ptr @malloc(i32 20) nounwind
166 %1 = tail call i32 @llvm.objectsize.i32.p0(ptr %0, i1 false, i1 false, i1 false)
167 %2 = load ptr, ptr @s, align 8
168 %3 = tail call ptr @__memcpy_chk(ptr %0, ptr %2, i32 10, i32 %1) nounwind
172 define void @test6(i32 %n) nounwind ssp {
173 ; CHECK-LABEL: @test6(
175 ; CHECK-NEXT: [[TMP0:%.*]] = tail call noalias dereferenceable_or_null(20) ptr @malloc(i32 20) #[[ATTR0]]
176 ; CHECK-NEXT: [[TMP1:%.*]] = load ptr, ptr @s, align 8
177 ; CHECK-NEXT: [[TMP2:%.*]] = tail call ptr @__memcpy_chk(ptr [[TMP0]], ptr [[TMP1]], i32 30, i32 20) #[[ATTR0]]
178 ; CHECK-NEXT: ret void
181 %0 = tail call noalias ptr @malloc(i32 20) nounwind
182 %1 = tail call i32 @llvm.objectsize.i32.p0(ptr %0, i1 false, i1 false, i1 false)
183 %2 = load ptr, ptr @s, align 8
184 %3 = tail call ptr @__memcpy_chk(ptr %0, ptr %2, i32 30, i32 %1) nounwind
188 declare ptr @__memset_chk(ptr, i32, i32, i32) nounwind
190 declare noalias ptr @malloc(i32) nounwind allockind("alloc,uninitialized") allocsize(0)
192 define i32 @test7(ptr %esc) {
193 ; CHECK-LABEL: @test7(
194 ; CHECK-NEXT: [[ALLOC:%.*]] = call noalias dereferenceable_or_null(48) ptr @malloc(i32 48) #[[ATTR0]]
195 ; CHECK-NEXT: store ptr [[ALLOC]], ptr [[ESC:%.*]], align 4
196 ; CHECK-NEXT: ret i32 32
198 %alloc = call noalias ptr @malloc(i32 48) nounwind
199 store ptr %alloc, ptr %esc
200 %gep = getelementptr inbounds i8, ptr %alloc, i32 16
201 %objsize = call i32 @llvm.objectsize.i32.p0(ptr %gep, i1 false, i1 false, i1 false) nounwind readonly
205 declare noalias ptr @calloc(i32, i32) nounwind allockind("alloc,zeroed") allocsize(0,1)
207 define i32 @test8(ptr %esc) {
208 ; CHECK-LABEL: @test8(
209 ; CHECK-NEXT: [[ALLOC:%.*]] = call noalias dereferenceable_or_null(35) ptr @calloc(i32 5, i32 7) #[[ATTR0]]
210 ; CHECK-NEXT: store ptr [[ALLOC]], ptr [[ESC:%.*]], align 4
211 ; CHECK-NEXT: ret i32 30
213 %alloc = call noalias ptr @calloc(i32 5, i32 7) nounwind
214 store ptr %alloc, ptr %esc
215 %gep = getelementptr inbounds i8, ptr %alloc, i32 5
216 %objsize = call i32 @llvm.objectsize.i32.p0(ptr %gep, i1 false, i1 false, i1 false) nounwind readonly
220 declare noalias ptr @strdup(ptr nocapture) nounwind
221 declare noalias ptr @strndup(ptr nocapture, i32) nounwind
223 define i32 @test9(ptr %esc) {
224 ; CHECK-LABEL: @test9(
225 ; CHECK-NEXT: [[CALL:%.*]] = tail call dereferenceable_or_null(8) ptr @strdup(ptr nonnull @.str) #[[ATTR0]]
226 ; CHECK-NEXT: store ptr [[CALL]], ptr [[ESC:%.*]], align 8
227 ; CHECK-NEXT: ret i32 8
229 %call = tail call ptr @strdup(ptr @.str) nounwind
230 store ptr %call, ptr %esc, align 8
231 %1 = tail call i32 @llvm.objectsize.i32.p0(ptr %call, i1 true, i1 false, i1 false)
235 define i32 @test10(ptr %esc) {
236 ; CHECK-LABEL: @test10(
237 ; CHECK-NEXT: [[CALL:%.*]] = tail call dereferenceable_or_null(4) ptr @strndup(ptr nonnull dereferenceable(8) @.str, i32 3) #[[ATTR0]]
238 ; CHECK-NEXT: store ptr [[CALL]], ptr [[ESC:%.*]], align 8
239 ; CHECK-NEXT: ret i32 4
241 %call = tail call ptr @strndup(ptr @.str, i32 3) nounwind
242 store ptr %call, ptr %esc, align 8
243 %1 = tail call i32 @llvm.objectsize.i32.p0(ptr %call, i1 true, i1 false, i1 false)
247 define i32 @test11(ptr %esc) {
248 ; CHECK-LABEL: @test11(
249 ; CHECK-NEXT: [[STRDUP:%.*]] = tail call dereferenceable_or_null(8) ptr @strdup(ptr nonnull @.str)
250 ; CHECK-NEXT: store ptr [[STRDUP]], ptr [[ESC:%.*]], align 8
251 ; CHECK-NEXT: ret i32 8
253 %call = tail call ptr @strndup(ptr @.str, i32 7) nounwind
254 store ptr %call, ptr %esc, align 8
255 %1 = tail call i32 @llvm.objectsize.i32.p0(ptr %call, i1 true, i1 false, i1 false)
259 define i32 @test12(ptr %esc) {
260 ; CHECK-LABEL: @test12(
261 ; CHECK-NEXT: [[STRDUP:%.*]] = tail call dereferenceable_or_null(8) ptr @strdup(ptr nonnull @.str)
262 ; CHECK-NEXT: store ptr [[STRDUP]], ptr [[ESC:%.*]], align 8
263 ; CHECK-NEXT: ret i32 8
265 %call = tail call ptr @strndup(ptr @.str, i32 8) nounwind
266 store ptr %call, ptr %esc, align 8
267 %1 = tail call i32 @llvm.objectsize.i32.p0(ptr %call, i1 true, i1 false, i1 false)
271 define i32 @test13(ptr %esc) {
272 ; CHECK-LABEL: @test13(
273 ; CHECK-NEXT: [[STRDUP:%.*]] = tail call dereferenceable_or_null(8) ptr @strdup(ptr nonnull @.str)
274 ; CHECK-NEXT: store ptr [[STRDUP]], ptr [[ESC:%.*]], align 8
275 ; CHECK-NEXT: ret i32 8
277 %call = tail call ptr @strndup(ptr @.str, i32 57) nounwind
278 store ptr %call, ptr %esc, align 8
279 %1 = tail call i32 @llvm.objectsize.i32.p0(ptr %call, i1 true, i1 false, i1 false)
283 @globalalias = internal alias [60 x i8], ptr @a
285 define i32 @test18() {
286 ; CHECK-LABEL: @test18(
287 ; CHECK-NEXT: ret i32 60
289 %1 = call i32 @llvm.objectsize.i32.p0(ptr @globalalias, i1 false, i1 false, i1 false)
293 @globalalias2 = weak alias [60 x i8], ptr @a
295 define i32 @test19() {
296 ; CHECK-LABEL: @test19(
297 ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.objectsize.i32.p0(ptr @globalalias2, i1 false, i1 false, i1 false)
298 ; CHECK-NEXT: ret i32 [[TMP1]]
300 %1 = call i32 @llvm.objectsize.i32.p0(ptr @globalalias2, i1 false, i1 false, i1 false)
304 define i32 @test20() {
305 ; CHECK-LABEL: @test20(
306 ; CHECK-NEXT: ret i32 0
308 %1 = call i32 @llvm.objectsize.i32.p0(ptr null, i1 false, i1 false, i1 false)
312 define i32 @test21() {
313 ; CHECK-LABEL: @test21(
314 ; CHECK-NEXT: ret i32 0
316 %1 = call i32 @llvm.objectsize.i32.p0(ptr null, i1 true, i1 false, i1 false)
320 define i32 @test22() {
321 ; CHECK-LABEL: @test22(
322 ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.objectsize.i32.p0(ptr null, i1 false, i1 true, i1 false)
323 ; CHECK-NEXT: ret i32 [[TMP1]]
325 %1 = call i32 @llvm.objectsize.i32.p0(ptr null, i1 false, i1 true, i1 false)
329 define i32 @test23() {
330 ; CHECK-LABEL: @test23(
331 ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.objectsize.i32.p0(ptr null, i1 true, i1 true, i1 false)
332 ; CHECK-NEXT: ret i32 [[TMP1]]
334 %1 = call i32 @llvm.objectsize.i32.p0(ptr null, i1 true, i1 true, i1 false)
338 ; 1 is an arbitrary non-zero address space.
339 define i32 @test24() {
340 ; CHECK-LABEL: @test24(
341 ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.objectsize.i32.p1(ptr addrspace(1) null, i1 false, i1 false, i1 false)
342 ; CHECK-NEXT: ret i32 [[TMP1]]
344 %1 = call i32 @llvm.objectsize.i32.p1(ptr addrspace(1) null, i1 false,
349 define i32 @test25() {
350 ; CHECK-LABEL: @test25(
351 ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.objectsize.i32.p1(ptr addrspace(1) null, i1 true, i1 false, i1 false)
352 ; CHECK-NEXT: ret i32 [[TMP1]]
354 %1 = call i32 @llvm.objectsize.i32.p1(ptr addrspace(1) null, i1 true,
359 define i32 @test26() {
360 ; CHECK-LABEL: @test26(
361 ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.objectsize.i32.p1(ptr addrspace(1) null, i1 false, i1 true, i1 false)
362 ; CHECK-NEXT: ret i32 [[TMP1]]
364 %1 = call i32 @llvm.objectsize.i32.p1(ptr addrspace(1) null, i1 false,
369 define i32 @test27() {
370 ; CHECK-LABEL: @test27(
371 ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.objectsize.i32.p1(ptr addrspace(1) null, i1 true, i1 true, i1 false)
372 ; CHECK-NEXT: ret i32 [[TMP1]]
374 %1 = call i32 @llvm.objectsize.i32.p1(ptr addrspace(1) null, i1 true,