1; RUN: opt -codegenprepare -S  < %s | FileCheck %s
2
3; Ensure we act sanely on overflow.
4; CHECK-LABEL: define i32 @bar
5define i32 @bar() {
6entry:
7  ; CHECK: ret i32 -1
8  %az = alloca [2147483649 x i32], align 16
9  %a = alloca i8*, align 8
10  %arraydecay = getelementptr inbounds [2147483649 x i32], [2147483649 x i32]* %az, i32 0, i32 0
11  %0 = bitcast i32* %arraydecay to i8*
12  store i8* %0, i8** %a, align 8
13  %1 = load i8*, i8** %a, align 8
14  %2 = call i32 @llvm.objectsize.i32.p0i8(i8* %1, i1 false)
15  ret i32 %2
16}
17
18; CHECK-LABEL: define i32 @baz
19define i32 @baz(i32 %n) {
20entry:
21  ; CHECK: ret i32 -1
22  %az = alloca [1 x i32], align 16
23  %bz = alloca [4294967297 x i32], align 16
24  %tobool = icmp ne i32 %n, 0
25  %arraydecay = getelementptr inbounds [1 x i32], [1 x i32]* %az, i64 0, i64 0
26  %arraydecay1 = getelementptr inbounds [4294967297 x i32], [4294967297 x i32]* %bz, i64 0, i64 0
27  %cond = select i1 %tobool, i32* %arraydecay, i32* %arraydecay1
28  %0 = bitcast i32* %cond to i8*
29  %1 = call i32 @llvm.objectsize.i32.p0i8(i8* %0, i1 false)
30  ret i32 %1
31}
32
33declare i32 @llvm.objectsize.i32.p0i8(i8*, i1)
34
35; The following tests were generated by:
36; #include<stdlib.h>
37; #define STATIC_BUF_SIZE 10
38; #define LARGER_BUF_SIZE 30
39;
40; size_t foo1(int flag) {
41;   char *cptr;
42;   char chararray[LARGER_BUF_SIZE];
43;   char chararray2[STATIC_BUF_SIZE];
44;   if(flag)
45;     cptr = chararray2;
46;    else
47;     cptr = chararray;
48;
49;   return  __builtin_object_size(cptr, 2);
50; }
51;
52; size_t foo2(int n) {
53;   char Small[10];
54;   char Large[20];
55;   char *Ptr = n ? Small : Large + 19;
56;   return __builtin_object_size(Ptr, 0);
57; }
58;
59; void foo() {
60;   size_t ret;
61;   size_t ret1;
62;   ret = foo1(0);
63;   ret1 = foo2(0);
64;   printf("\n%d %d\n", ret, ret1);
65; }
66
67target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
68target triple = "x86_64-unknown-linux-gnu"
69
70@.str = private unnamed_addr constant [8 x i8] c"\0A%d %d\0A\00", align 1
71
72define i64 @foo1(i32 %flag) {
73entry:
74  %chararray = alloca [30 x i8], align 16
75  %chararray2 = alloca [10 x i8], align 1
76  %0 = getelementptr inbounds [30 x i8], [30 x i8]* %chararray, i64 0, i64 0
77  call void @llvm.lifetime.start.p0i8(i64 30, i8* %0)
78  %1 = getelementptr inbounds [10 x i8], [10 x i8]* %chararray2, i64 0, i64 0
79  call void @llvm.lifetime.start.p0i8(i64 10, i8* %1)
80  %tobool = icmp eq i32 %flag, 0
81  %cptr.0 = select i1 %tobool, i8* %0, i8* %1
82  %2 = call i64 @llvm.objectsize.i64.p0i8(i8* %cptr.0, i1 true)
83  call void @llvm.lifetime.end.p0i8(i64 10, i8* %1)
84  call void @llvm.lifetime.end.p0i8(i64 30, i8* %0)
85  ret i64 %2
86; CHECK-LABEL: foo1
87; CHECK:  ret i64 10
88}
89
90declare void @llvm.lifetime.start.p0i8(i64, i8* nocapture)
91
92declare i64 @llvm.objectsize.i64.p0i8(i8*, i1)
93
94declare void @llvm.lifetime.end.p0i8(i64, i8* nocapture)
95
96define i64 @foo2(i32 %n) {
97entry:
98  %Small = alloca [10 x i8], align 1
99  %Large = alloca [20 x i8], align 16
100  %0 = getelementptr inbounds [10 x i8], [10 x i8]* %Small, i64 0, i64 0
101  call void @llvm.lifetime.start.p0i8(i64 10, i8* %0)
102  %1 = getelementptr inbounds [20 x i8], [20 x i8]* %Large, i64 0, i64 0
103  call void @llvm.lifetime.start.p0i8(i64 20, i8* %1)
104  %tobool = icmp ne i32 %n, 0
105  %add.ptr = getelementptr inbounds [20 x i8], [20 x i8]* %Large, i64 0, i64 19
106  %cond = select i1 %tobool, i8* %0, i8* %add.ptr
107  %2 = call i64 @llvm.objectsize.i64.p0i8(i8* %cond, i1 false)
108  call void @llvm.lifetime.end.p0i8(i64 20, i8* %1)
109  call void @llvm.lifetime.end.p0i8(i64 10, i8* %0)
110  ret i64 %2
111; CHECK-LABEL: foo2
112; CHECK:  ret i64 10
113}
114
115define void @foo() {
116entry:
117  %call = tail call i64 @foo1(i32 0)
118  %call1 = tail call i64 @foo2(i32 0)
119  %call2 = tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i64 %call, i64 %call1)
120  ret void
121}
122
123declare i32 @printf(i8* nocapture readonly, ...)
124