1 ; Test a pile of objectsize bounds checking.
2 ; RUN: opt < %s -instcombine -S | FileCheck %s
3 ; We need target data to get the sizes of the arrays and structures.
4 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"
6 @a = private global [60 x i8] zeroinitializer, align 1 ; <[60 x i8]*>
7 @.str = private constant [8 x i8] c"abcdefg\00" ; <[8 x i8]*>
8 define i32 @foo() nounwind {
10 ; CHECK-NEXT: ret i32 60
11 %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)
15 define i8* @bar() nounwind {
19 %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)
20 %cmp = icmp ne i32 %0, -1
22 br i1 %cmp, label %cond.true, label %cond.false
25 %1 = load i8*, i8** %retval
29 %2 = load i8*, i8** %retval
33 define i32 @f() nounwind {
35 ; CHECK-NEXT: ret i32 0
36 %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)
40 @window = external global [0 x i8]
42 define i1 @baz() nounwind {
44 ; CHECK-NEXT: objectsize
45 %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)
46 %2 = icmp eq i32 %1, -1
50 define void @test1(i8* %q, i32 %x) nounwind noinline {
51 ; CHECK-LABEL: @test1(
52 ; CHECK: objectsize.i32.p0i8
54 %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]
55 %1 = icmp eq i32 %0, -1 ; <i1> [#uses=1]
56 br i1 %1, label %"47", label %"46"
58 "46": ; preds = %entry
61 "47": ; preds = %entry
65 @.str5 = private constant [9 x i32] [i32 97, i32 98, i32 99, i32 100, i32 0, i32
66 101, i32 102, i32 103, i32 0], align 4
67 define i32 @test2() nounwind {
68 ; CHECK-LABEL: @test2(
69 ; CHECK-NEXT: ret i32 34
70 %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)
75 @array = internal global [480 x float] zeroinitializer ; <[480 x float]*> [#uses=1]
77 declare i8* @__memcpy_chk(i8*, i8*, i32, i32) nounwind
79 declare i32 @llvm.objectsize.i32.p0i8(i8*, i1, i1, i1) nounwind readonly
81 declare i32 @llvm.objectsize.i32.p1i8(i8 addrspace(1)*, i1, i1, i1) nounwind readonly
83 declare i8* @__inline_memcpy_chk(i8*, i8*, i32) nounwind inlinehint
85 define void @test3() nounwind {
86 ; CHECK-LABEL: @test3(
88 br i1 undef, label %bb11, label %bb12
91 %0 = getelementptr inbounds float, float* getelementptr inbounds ([480 x float], [480 x float]* @array, i32 0, i32 128), i32 -127 ; <float*> [#uses=1]
92 %1 = bitcast float* %0 to i8* ; <i8*> [#uses=1]
93 %2 = call i32 @llvm.objectsize.i32.p0i8(i8* %1, i1 false, i1 false, i1 false) ; <i32> [#uses=1]
94 %3 = call i8* @__memcpy_chk(i8* undef, i8* undef, i32 512, i32 %2) nounwind ; <i8*> [#uses=0]
99 %4 = getelementptr inbounds float, float* getelementptr inbounds ([480 x float], [480 x float]* @array, i32 0, i32 128), i32 -127 ; <float*> [#uses=1]
100 %5 = bitcast float* %4 to i8* ; <i8*> [#uses=1]
101 %6 = call i8* @__inline_memcpy_chk(i8* %5, i8* undef, i32 512) nounwind inlinehint ; <i8*> [#uses=0]
102 ; CHECK: @__inline_memcpy_chk
108 %struct.data = type { [100 x i32], [100 x i32], [1024 x i8] }
110 define i32 @test4(i8** %esc) nounwind ssp {
111 ; CHECK-LABEL: @test4(
113 %0 = alloca %struct.data, align 8
114 %1 = bitcast %struct.data* %0 to i8*
115 %2 = call i32 @llvm.objectsize.i32.p0i8(i8* %1, i1 false, i1 false, i1 false) nounwind
116 ; CHECK-NOT: @llvm.objectsize
117 ; CHECK: @llvm.memset.p0i8.i32(i8* nonnull align 8 %1, i8 0, i32 1824, i1 false)
118 %3 = call i8* @__memset_chk(i8* %1, i32 0, i32 1824, i32 %2) nounwind
119 store i8* %1, i8** %esc
124 @s = external global i8*
126 define i8* @test5(i32 %n) nounwind ssp {
127 ; CHECK-LABEL: @test5(
129 %0 = tail call noalias i8* @malloc(i32 20) nounwind
130 %1 = tail call i32 @llvm.objectsize.i32.p0i8(i8* %0, i1 false, i1 false, i1 false)
131 %2 = load i8*, i8** @s, align 8
132 ; CHECK-NOT: @llvm.objectsize
133 ; CHECK: @llvm.memcpy.p0i8.p0i8.i32(i8* align 1 %0, i8* align 1 %1, i32 10, i1 false)
134 %3 = tail call i8* @__memcpy_chk(i8* %0, i8* %2, i32 10, i32 %1) nounwind
138 define void @test6(i32 %n) nounwind ssp {
139 ; CHECK-LABEL: @test6(
141 %0 = tail call noalias i8* @malloc(i32 20) nounwind
142 %1 = tail call i32 @llvm.objectsize.i32.p0i8(i8* %0, i1 false, i1 false, i1 false)
143 %2 = load i8*, i8** @s, align 8
144 ; CHECK-NOT: @llvm.objectsize
145 ; CHECK: @__memcpy_chk(i8* %0, i8* %1, i32 30, i32 20)
146 %3 = tail call i8* @__memcpy_chk(i8* %0, i8* %2, i32 30, i32 %1) nounwind
150 declare i8* @__memset_chk(i8*, i32, i32, i32) nounwind
152 declare noalias i8* @malloc(i32) nounwind
154 define i32 @test7(i8** %esc) {
155 ; CHECK-LABEL: @test7(
156 %alloc = call noalias i8* @malloc(i32 48) nounwind
157 store i8* %alloc, i8** %esc
158 %gep = getelementptr inbounds i8, i8* %alloc, i32 16
159 %objsize = call i32 @llvm.objectsize.i32.p0i8(i8* %gep, i1 false, i1 false, i1 false) nounwind readonly
164 declare noalias i8* @calloc(i32, i32) nounwind
166 define i32 @test8(i8** %esc) {
167 ; CHECK-LABEL: @test8(
168 %alloc = call noalias i8* @calloc(i32 5, i32 7) nounwind
169 store i8* %alloc, i8** %esc
170 %gep = getelementptr inbounds i8, i8* %alloc, i32 5
171 %objsize = call i32 @llvm.objectsize.i32.p0i8(i8* %gep, i1 false, i1 false, i1 false) nounwind readonly
176 declare noalias i8* @strdup(i8* nocapture) nounwind
177 declare noalias i8* @strndup(i8* nocapture, i32) nounwind
179 ; CHECK-LABEL: @test9(
180 define i32 @test9(i8** %esc) {
181 %call = tail call i8* @strdup(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0)) nounwind
182 store i8* %call, i8** %esc, align 8
183 %1 = tail call i32 @llvm.objectsize.i32.p0i8(i8* %call, i1 true, i1 false, i1 false)
188 ; CHECK-LABEL: @test10(
189 define i32 @test10(i8** %esc) {
190 %call = tail call i8* @strndup(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 3) nounwind
191 store i8* %call, i8** %esc, align 8
192 %1 = tail call i32 @llvm.objectsize.i32.p0i8(i8* %call, i1 true, i1 false, i1 false)
197 ; CHECK-LABEL: @test11(
198 define i32 @test11(i8** %esc) {
199 %call = tail call i8* @strndup(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 7) nounwind
200 store i8* %call, i8** %esc, align 8
201 %1 = tail call i32 @llvm.objectsize.i32.p0i8(i8* %call, i1 true, i1 false, i1 false)
206 ; CHECK-LABEL: @test12(
207 define i32 @test12(i8** %esc) {
208 %call = tail call i8* @strndup(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 8) nounwind
209 store i8* %call, i8** %esc, align 8
210 %1 = tail call i32 @llvm.objectsize.i32.p0i8(i8* %call, i1 true, i1 false, i1 false)
215 ; CHECK-LABEL: @test13(
216 define i32 @test13(i8** %esc) {
217 %call = tail call i8* @strndup(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 57) nounwind
218 store i8* %call, i8** %esc, align 8
219 %1 = tail call i32 @llvm.objectsize.i32.p0i8(i8* %call, i1 true, i1 false, i1 false)
224 @globalalias = internal alias [60 x i8], [60 x i8]* @a
226 ; CHECK-LABEL: @test18(
227 ; CHECK-NEXT: ret i32 60
228 define i32 @test18() {
229 %bc = bitcast [60 x i8]* @globalalias to i8*
230 %1 = call i32 @llvm.objectsize.i32.p0i8(i8* %bc, i1 false, i1 false, i1 false)
234 @globalalias2 = weak alias [60 x i8], [60 x i8]* @a
236 ; CHECK-LABEL: @test19(
237 ; CHECK: llvm.objectsize
238 define i32 @test19() {
239 %bc = bitcast [60 x i8]* @globalalias2 to i8*
240 %1 = call i32 @llvm.objectsize.i32.p0i8(i8* %bc, i1 false, i1 false, i1 false)
244 ; CHECK-LABEL: @test20(
246 define i32 @test20() {
247 %1 = call i32 @llvm.objectsize.i32.p0i8(i8* null, i1 false, i1 false, i1 false)
251 ; CHECK-LABEL: @test21(
253 define i32 @test21() {
254 %1 = call i32 @llvm.objectsize.i32.p0i8(i8* null, i1 true, i1 false, i1 false)
258 ; CHECK-LABEL: @test22(
259 ; CHECK: llvm.objectsize
260 define i32 @test22() {
261 %1 = call i32 @llvm.objectsize.i32.p0i8(i8* null, i1 false, i1 true, i1 false)
265 ; CHECK-LABEL: @test23(
266 ; CHECK: llvm.objectsize
267 define i32 @test23() {
268 %1 = call i32 @llvm.objectsize.i32.p0i8(i8* null, i1 true, i1 true, i1 false)
272 ; 1 is an arbitrary non-zero address space.
273 ; CHECK-LABEL: @test24(
274 ; CHECK: llvm.objectsize
275 define i32 @test24() {
276 %1 = call i32 @llvm.objectsize.i32.p1i8(i8 addrspace(1)* null, i1 false,
281 ; CHECK-LABEL: @test25(
282 ; CHECK: llvm.objectsize
283 define i32 @test25() {
284 %1 = call i32 @llvm.objectsize.i32.p1i8(i8 addrspace(1)* null, i1 true,
289 ; CHECK-LABEL: @test26(
290 ; CHECK: llvm.objectsize
291 define i32 @test26() {
292 %1 = call i32 @llvm.objectsize.i32.p1i8(i8 addrspace(1)* null, i1 false,
297 ; CHECK-LABEL: @test27(
298 ; CHECK: llvm.objectsize
299 define i32 @test27() {
300 %1 = call i32 @llvm.objectsize.i32.p1i8(i8 addrspace(1)* null, i1 true,