blob: 3a72bb84e7f65300563e17a234e069920eff35fe [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -triple x86_64-unknown-unknown %s -emit-llvm -o - | FileCheck %s
John McCallb1c98a32011-05-16 01:05:12 +00002
3typedef __typeof__(sizeof(0)) size_t;
Anders Carlssoned4e3672009-05-31 20:21:44 +00004
5void t1() {
6 int* a = new int;
7}
Anders Carlssond9583892009-05-31 20:26:12 +00008
John McCallb1c98a32011-05-16 01:05:12 +00009// Declare the reserved placement operators.
10void *operator new(size_t, void*) throw();
11void operator delete(void*, void*) throw();
12void *operator new[](size_t, void*) throw();
13void operator delete[](void*, void*) throw();
Anders Carlssond9583892009-05-31 20:26:12 +000014
15void t2(int* a) {
16 int* b = new (a) int;
17}
Anders Carlsson6d0ffad2009-05-31 20:56:36 +000018
Anders Carlsson627a3e52009-05-31 21:12:26 +000019struct S {
20 int a;
21};
22
Anders Carlssond3fd6ba2009-05-31 21:53:59 +000023// POD types.
Anders Carlsson6d0ffad2009-05-31 20:56:36 +000024void t3() {
25 int *a = new int(10);
Anders Carlsson3923e952009-05-31 21:07:58 +000026 _Complex int* b = new _Complex int(10i);
27
Anders Carlsson627a3e52009-05-31 21:12:26 +000028 S s;
29 s.a = 10;
30 S *sp = new S(s);
Anders Carlsson6d0ffad2009-05-31 20:56:36 +000031}
Anders Carlssond3fd6ba2009-05-31 21:53:59 +000032
33// Non-POD
34struct T {
35 T();
36 int a;
37};
38
39void t4() {
Anders Carlssona4d4c012009-09-23 16:07:23 +000040 // CHECK: call void @_ZN1TC1Ev
Anders Carlssond3fd6ba2009-05-31 21:53:59 +000041 T *t = new T;
42}
43
44struct T2 {
45 int a;
46 T2(int, int);
47};
48
49void t5() {
Anders Carlssona4d4c012009-09-23 16:07:23 +000050 // CHECK: call void @_ZN2T2C1Eii
Anders Carlssond3fd6ba2009-05-31 21:53:59 +000051 T2 *t2 = new T2(10, 10);
52}
Anders Carlssonf1108532009-06-01 00:05:16 +000053
54int *t6() {
55 // Null check.
56 return new (0) int(10);
57}
Anders Carlsson215bd202009-06-01 00:26:14 +000058
59void t7() {
60 new int();
61}
Anders Carlssona4d4c012009-09-23 16:07:23 +000062
Anders Carlsson6ac5fc42009-09-23 18:59:48 +000063struct U {
64 ~U();
65};
66
Anders Carlssona4d4c012009-09-23 16:07:23 +000067void t8(int n) {
68 new int[10];
69 new int[n];
70
71 // Non-POD
72 new T[10];
73 new T[n];
Anders Carlsson6ac5fc42009-09-23 18:59:48 +000074
75 // Cookie required
76 new U[10];
77 new U[n];
Anders Carlssona4d4c012009-09-23 16:07:23 +000078}
Anders Carlsson09edb9c2009-11-22 19:25:33 +000079
Douglas Gregor7b868622010-08-18 15:06:25 +000080// noalias
81// CHECK: declare noalias i8* @_Znam
82void *operator new[](size_t);
83
Anders Carlsson09edb9c2009-11-22 19:25:33 +000084void t9() {
85 bool b;
86
87 new bool(true);
88 new (&b) bool(true);
89}
Fariborz Jahanian048f52a2009-11-24 18:29:37 +000090
91struct A {
92 void* operator new(__typeof(sizeof(int)), int, float, ...);
93 A();
94};
95
96A* t10() {
97 // CHECK: @_ZN1AnwEmifz
98 return new(1, 2, 3.45, 100) A;
99}
100
Douglas Gregor59174c02010-07-21 01:10:17 +0000101// CHECK: define void @_Z3t11i
Anders Carlsson0d7c5832010-05-03 01:20:20 +0000102struct B { int a; };
Douglas Gregor59174c02010-07-21 01:10:17 +0000103struct Bmemptr { int Bmemptr::* memptr; int a; };
104
105void t11(int n) {
Nuno Lopesfc284482009-12-16 16:59:22 +0000106 // CHECK: call noalias i8* @_Znwm
Mon P Wang3ecd7852010-04-04 03:10:52 +0000107 // CHECK: call void @llvm.memset.p0i8.i64(
Anders Carlsson30311fa2009-12-16 06:57:54 +0000108 B* b = new B();
Douglas Gregor59174c02010-07-21 01:10:17 +0000109
110 // CHECK: call noalias i8* @_Znam
111 // CHECK: {{call void.*llvm.memset.p0i8.i64.*i8 0, i64 %}}
112 B *b2 = new B[n]();
113
114 // CHECK: call noalias i8* @_Znam
115 // CHECK: call void @llvm.memcpy.p0i8.p0i8.i64
116 // CHECK: br
117 Bmemptr *b_memptr = new Bmemptr[n]();
118
119 // CHECK: ret void
Anders Carlsson30311fa2009-12-16 06:57:54 +0000120}
Anders Carlssone99bdb62010-05-03 15:09:17 +0000121
122struct Empty { };
123
124// We don't need to initialize an empty class.
Douglas Gregor59174c02010-07-21 01:10:17 +0000125// CHECK: define void @_Z3t12v
Anders Carlssone99bdb62010-05-03 15:09:17 +0000126void t12() {
Douglas Gregor59174c02010-07-21 01:10:17 +0000127 // CHECK: call noalias i8* @_Znam
128 // CHECK-NOT: br
Anders Carlssone99bdb62010-05-03 15:09:17 +0000129 (void)new Empty[10];
Douglas Gregor59174c02010-07-21 01:10:17 +0000130
131 // CHECK: call noalias i8* @_Znam
132 // CHECK-NOT: br
133 (void)new Empty[10]();
134
135 // CHECK: ret void
136}
137
138// Zero-initialization
139// CHECK: define void @_Z3t13i
140void t13(int n) {
141 // CHECK: call noalias i8* @_Znwm
142 // CHECK: store i32 0, i32*
143 (void)new int();
144
145 // CHECK: call noalias i8* @_Znam
146 // CHECK: {{call void.*llvm.memset.p0i8.i64.*i8 0, i64 %}}
147 (void)new int[n]();
148
149 // CHECK-NEXT: ret void
Anders Carlssone99bdb62010-05-03 15:09:17 +0000150}
Argyrios Kyrtzidisd2932982010-08-25 23:14:56 +0000151
152struct Alloc{
Argyrios Kyrtzidise7ab92e2010-08-26 15:23:38 +0000153 int x;
Argyrios Kyrtzidisd2932982010-08-25 23:14:56 +0000154 void* operator new[](size_t size);
155 void operator delete[](void* p);
Argyrios Kyrtzidise7ab92e2010-08-26 15:23:38 +0000156 ~Alloc();
Argyrios Kyrtzidisd2932982010-08-25 23:14:56 +0000157};
158
159void f() {
Argyrios Kyrtzidise7ab92e2010-08-26 15:23:38 +0000160 // CHECK: call i8* @_ZN5AllocnaEm(i64 808)
161 // CHECK: store i64 200
John McCall1e7fe752010-09-02 09:58:18 +0000162 // CHECK: call void @_ZN5AllocD1Ev(
Argyrios Kyrtzidisd2932982010-08-25 23:14:56 +0000163 // CHECK: call void @_ZN5AllocdaEPv(i8*
164 delete[] new Alloc[10][20];
Douglas Gregorcc09c022010-09-02 23:24:14 +0000165 // CHECK: call noalias i8* @_Znwm
166 // CHECK: call void @_ZdlPv(i8*
167 delete new bool;
168 // CHECK: ret void
Argyrios Kyrtzidisd2932982010-08-25 23:14:56 +0000169}
John McCallb1c98a32011-05-16 01:05:12 +0000170
171namespace test15 {
172 struct A { A(); ~A(); };
173
174 // CHECK: define void @_ZN6test155test0EPv(
175 // CHECK: [[P:%.*]] = load i8*
176 // CHECK-NEXT: icmp eq i8* [[P]], null
177 // CHECK-NEXT: br i1
178 // CHECK: [[T0:%.*]] = bitcast i8* [[P]] to [[A:%.*]]*
179 // CHECK-NEXT: call void @_ZN6test151AC1Ev([[A]]* [[T0]])
180 void test0(void *p) {
181 new (p) A();
182 }
183
184 // CHECK: define void @_ZN6test155test1EPv(
John McCallc3c07662011-07-13 06:10:41 +0000185 // CHECK: [[P:%.*]] = load i8**
John McCallb1c98a32011-05-16 01:05:12 +0000186 // CHECK-NEXT: icmp eq i8* [[P]], null
187 // CHECK-NEXT: br i1
John McCallc3c07662011-07-13 06:10:41 +0000188 // CHECK: [[BEGIN:%.*]] = bitcast i8* [[P]] to [[A:%.*]]*
189 // CHECK-NEXT: [[END:%.*]] = getelementptr inbounds [[A]]* [[BEGIN]], i64 5
190 // CHECK-NEXT: br label
191 // CHECK: [[CUR:%.*]] = phi [[A]]* [ [[BEGIN]], {{%.*}} ], [ [[NEXT:%.*]], {{%.*}} ]
192 // CHECK-NEXT: call void @_ZN6test151AC1Ev([[A]]* [[CUR]])
193 // CHECK-NEXT: [[NEXT]] = getelementptr inbounds [[A]]* [[CUR]], i64 1
194 // CHECK-NEXT: [[DONE:%.*]] = icmp eq [[A]]* [[NEXT]], [[END]]
195 // CHECK-NEXT: br i1 [[DONE]]
John McCallb1c98a32011-05-16 01:05:12 +0000196 void test1(void *p) {
197 new (p) A[5];
198 }
199
200 // TODO: it's okay if all these size calculations get dropped.
201 // FIXME: maybe we should try to throw on overflow?
202 // CHECK: define void @_ZN6test155test2EPvi(
203 // CHECK: [[N:%.*]] = load i32*
204 // CHECK-NEXT: [[T0:%.*]] = sext i32 [[N]] to i64
205 // CHECK-NEXT: [[T1:%.*]] = icmp slt i64 [[T0]], 0
206 // CHECK-NEXT: [[T2:%.*]] = select i1 [[T1]], i64 -1, i64 [[T0]]
207 // CHECK-NEXT: [[P:%.*]] = load i8*
208 // CHECK-NEXT: icmp eq i8* [[P]], null
209 // CHECK-NEXT: br i1
John McCallc3c07662011-07-13 06:10:41 +0000210 // CHECK: [[BEGIN:%.*]] = bitcast i8* [[P]] to [[A:%.*]]*
211 // CHECK-NEXT: [[ISEMPTY:%.*]] = icmp eq i64 [[T0]], 0
212 // CHECK-NEXT: br i1 [[ISEMPTY]],
213 // CHECK: [[END:%.*]] = getelementptr inbounds [[A]]* [[BEGIN]], i64 [[T0]]
214 // CHECK-NEXT: br label
215 // CHECK: [[CUR:%.*]] = phi [[A]]* [ [[BEGIN]],
216 // CHECK-NEXT: call void @_ZN6test151AC1Ev([[A]]* [[CUR]])
John McCallb1c98a32011-05-16 01:05:12 +0000217 void test2(void *p, int n) {
218 new (p) A[n];
219 }
220}
Douglas Gregord0e8b782011-06-27 16:55:54 +0000221
222namespace PR10197 {
223 // CHECK: define weak_odr void @_ZN7PR101971fIiEEvv()
224 template<typename T>
225 void f() {
226 // CHECK: [[CALL:%.*]] = call noalias i8* @_Znwm
227 // CHECK-NEXT: [[CASTED:%.*]] = bitcast i8* [[CALL]] to
228 new T;
229 // CHECK-NEXT: ret void
230 }
231
232 template void f<int>();
233}