1 // RUN: %clang_cc1 -std=c++14 -triple x86_64-unknown-unknown %s -emit-llvm -o - | FileCheck %s
2
3 typedef __typeof__(sizeof(0)) size_t;
4
5 // Declare an 'operator new' template to tickle a bug in __builtin_operator_new.
6 template<typename T> void *operator new(size_t, int (*)(T));
7
8 // Ensure that this declaration doesn't cause operator new to lose its
9 // 'noalias' attribute.
10 void *operator new[](size_t);
11
t1()12 void t1() {
13 delete new int;
14 delete [] new int [3];
15 }
16
17 // CHECK: declare nonnull i8* @_Znwm(i64) [[ATTR_NOBUILTIN:#[^ ]*]]
18 // CHECK: declare void @_ZdlPv(i8*) [[ATTR_NOBUILTIN_NOUNWIND:#[^ ]*]]
19 // CHECK: declare nonnull i8* @_Znam(i64) [[ATTR_NOBUILTIN]]
20 // CHECK: declare void @_ZdaPv(i8*) [[ATTR_NOBUILTIN_NOUNWIND]]
21
22 namespace std {
23 struct nothrow_t {};
24 }
25 std::nothrow_t nothrow;
26
27 // Declare the reserved placement operators.
28 void *operator new(size_t, void*) throw();
29 void operator delete(void*, void*) throw();
30 void *operator new[](size_t, void*) throw();
31 void operator delete[](void*, void*) throw();
32
33 // Declare the replaceable global allocation operators.
34 void *operator new(size_t, const std::nothrow_t &) throw();
35 void *operator new[](size_t, const std::nothrow_t &) throw();
36 void operator delete(void *, const std::nothrow_t &) throw();
37 void operator delete[](void *, const std::nothrow_t &) throw();
38
39 // Declare some other placemenet operators.
40 void *operator new(size_t, void*, bool) throw();
41 void *operator new[](size_t, void*, bool) throw();
42
t2(int * a)43 void t2(int* a) {
44 int* b = new (a) int;
45 }
46
47 struct S {
48 int a;
49 };
50
51 // POD types.
t3()52 void t3() {
53 int *a = new int(10);
54 _Complex int* b = new _Complex int(10i);
55
56 S s;
57 s.a = 10;
58 S *sp = new S(s);
59 }
60
61 // Non-POD
62 struct T {
63 T();
64 int a;
65 };
66
t4()67 void t4() {
68 // CHECK: call void @_ZN1TC1Ev
69 T *t = new T;
70 }
71
72 struct T2 {
73 int a;
74 T2(int, int);
75 };
76
t5()77 void t5() {
78 // CHECK: call void @_ZN2T2C1Eii
79 T2 *t2 = new T2(10, 10);
80 }
81
t6()82 int *t6() {
83 // Null check.
84 return new (0) int(10);
85 }
86
t7()87 void t7() {
88 new int();
89 }
90
91 struct U {
92 ~U();
93 };
94
t8(int n)95 void t8(int n) {
96 new int[10];
97 new int[n];
98
99 // Non-POD
100 new T[10];
101 new T[n];
102
103 // Cookie required
104 new U[10];
105 new U[n];
106 }
107
t9()108 void t9() {
109 bool b;
110
111 new bool(true);
112 new (&b) bool(true);
113 }
114
115 struct A {
116 void* operator new(__typeof(sizeof(int)), int, float, ...);
117 A();
118 };
119
t10()120 A* t10() {
121 // CHECK: @_ZN1AnwEmifz
122 return new(1, 2, 3.45, 100) A;
123 }
124
125 // CHECK-LABEL: define{{.*}} void @_Z3t11i
126 struct B { int a; };
127 struct Bmemptr { int Bmemptr::* memptr; int a; };
128
t11(int n)129 void t11(int n) {
130 // CHECK: call noalias nonnull i8* @_Znwm
131 // CHECK: call void @llvm.memset.p0i8.i64(
132 B* b = new B();
133
134 // CHECK: call noalias nonnull i8* @_Znam
135 // CHECK: {{call void.*llvm.memset.p0i8.i64.*i8 0, i64 %}}
136 B *b2 = new B[n]();
137
138 // CHECK: call noalias nonnull i8* @_Znam
139 // CHECK: call void @llvm.memcpy.p0i8.p0i8.i64
140 // CHECK: br
141 Bmemptr *b_memptr = new Bmemptr[n]();
142
143 // CHECK: ret void
144 }
145
146 struct Empty { };
147
148 // We don't need to initialize an empty class.
149 // CHECK-LABEL: define{{.*}} void @_Z3t12v
t12()150 void t12() {
151 // CHECK: call noalias nonnull i8* @_Znam
152 // CHECK-NOT: br
153 (void)new Empty[10];
154
155 // CHECK: call noalias nonnull i8* @_Znam
156 // CHECK-NOT: br
157 (void)new Empty[10]();
158
159 // CHECK: ret void
160 }
161
162 // Zero-initialization
163 // CHECK-LABEL: define{{.*}} void @_Z3t13i
t13(int n)164 void t13(int n) {
165 // CHECK: call noalias nonnull i8* @_Znwm
166 // CHECK: store i32 0, i32*
167 (void)new int();
168
169 // CHECK: call noalias nonnull i8* @_Znam
170 // CHECK: {{call void.*llvm.memset.p0i8.i64.*i8 0, i64 %}}
171 (void)new int[n]();
172
173 // CHECK-NEXT: ret void
174 }
175
176 struct Alloc{
177 int x;
178 void* operator new[](size_t size);
179 __attribute__((returns_nonnull)) void *operator new[](size_t size, const std::nothrow_t &) throw();
180 void operator delete[](void* p);
181 ~Alloc();
182 };
183
f()184 void f() {
185 // CHECK: call i8* @_ZN5AllocnaEm(i64 808)
186 // CHECK: store i64 200
187 // CHECK: call void @_ZN5AllocD1Ev(
188 // CHECK: call void @_ZN5AllocdaEPv(i8*
189 delete[] new Alloc[10][20];
190 // CHECK: [[P:%.*]] = call nonnull i8* @_ZN5AllocnaEmRKSt9nothrow_t(i64 808, {{.*}}) [[ATTR_NOUNWIND:#[^ ]*]]
191 // CHECK-NOT: icmp eq i8* [[P]], null
192 // CHECK: store i64 200
193 delete[] new (nothrow) Alloc[10][20];
194 // CHECK: call noalias nonnull i8* @_Znwm
195 // CHECK: call void @_ZdlPv(i8*
196 delete new bool;
197 // CHECK: ret void
198 }
199
200 namespace test15 {
201 struct A { A(); ~A(); };
202
203 // CHECK-LABEL: define{{.*}} void @_ZN6test156test0aEPv(
204 // CHECK: [[P:%.*]] = load i8*, i8**
205 // CHECK-NOT: icmp eq i8* [[P]], null
206 // CHECK-NOT: br i1
207 // CHECK: [[T0:%.*]] = bitcast i8* [[P]] to [[A:%.*]]*
208 // CHECK-NEXT: call void @_ZN6test151AC1Ev([[A]]* {{[^,]*}} [[T0]])
test0a(void * p)209 void test0a(void *p) {
210 new (p) A();
211 }
212
213 // CHECK-LABEL: define{{.*}} void @_ZN6test156test0bEPv(
214 // CHECK: [[P0:%.*]] = load i8*, i8**
215 // CHECK: [[P:%.*]] = call i8* @_ZnwmPvb(i64 1, i8* [[P0]]
216 // CHECK-NEXT: icmp eq i8* [[P]], null
217 // CHECK-NEXT: br i1
218 // CHECK: [[T0:%.*]] = bitcast i8* [[P]] to [[A:%.*]]*
219 // CHECK-NEXT: call void @_ZN6test151AC1Ev([[A]]* {{[^,]*}} [[T0]])
test0b(void * p)220 void test0b(void *p) {
221 new (p, true) A();
222 }
223
224 // CHECK-LABEL: define{{.*}} void @_ZN6test156test1aEPv(
225 // CHECK: [[P:%.*]] = load i8*, i8**
226 // CHECK-NOT: icmp eq i8* [[P]], null
227 // CHECK-NOT: br i1
228 // CHECK: [[BEGIN:%.*]] = bitcast i8* [[P]] to [[A:%.*]]*
229 // CHECK-NEXT: [[END:%.*]] = getelementptr inbounds [[A]], [[A]]* [[BEGIN]], i64 5
230 // CHECK-NEXT: br label
231 // CHECK: [[CUR:%.*]] = phi [[A]]* [ [[BEGIN]], {{%.*}} ], [ [[NEXT:%.*]], {{%.*}} ]
232 // CHECK-NEXT: call void @_ZN6test151AC1Ev([[A]]* {{[^,]*}} [[CUR]])
233 // CHECK-NEXT: [[NEXT]] = getelementptr inbounds [[A]], [[A]]* [[CUR]], i64 1
234 // CHECK-NEXT: [[DONE:%.*]] = icmp eq [[A]]* [[NEXT]], [[END]]
235 // CHECK-NEXT: br i1 [[DONE]]
test1a(void * p)236 void test1a(void *p) {
237 new (p) A[5];
238 }
239
240 // CHECK-LABEL: define{{.*}} void @_ZN6test156test1bEPv(
241 // CHECK: [[P0:%.*]] = load i8*, i8**
242 // CHECK: [[P:%.*]] = call i8* @_ZnamPvb(i64 13, i8* [[P0]]
243 // CHECK-NEXT: icmp eq i8* [[P]], null
244 // CHECK-NEXT: br i1
245 // CHECK: [[AFTER_COOKIE:%.*]] = getelementptr inbounds i8, i8* [[P]], i64 8
246 // CHECK: [[BEGIN:%.*]] = bitcast i8* [[AFTER_COOKIE]] to [[A:%.*]]*
247 // CHECK-NEXT: [[END:%.*]] = getelementptr inbounds [[A]], [[A]]* [[BEGIN]], i64 5
248 // CHECK-NEXT: br label
249 // CHECK: [[CUR:%.*]] = phi [[A]]* [ [[BEGIN]], {{%.*}} ], [ [[NEXT:%.*]], {{%.*}} ]
250 // CHECK-NEXT: call void @_ZN6test151AC1Ev([[A]]* {{[^,]*}} [[CUR]])
251 // CHECK-NEXT: [[NEXT]] = getelementptr inbounds [[A]], [[A]]* [[CUR]], i64 1
252 // CHECK-NEXT: [[DONE:%.*]] = icmp eq [[A]]* [[NEXT]], [[END]]
253 // CHECK-NEXT: br i1 [[DONE]]
test1b(void * p)254 void test1b(void *p) {
255 new (p, true) A[5];
256 }
257
258 // TODO: it's okay if all these size calculations get dropped.
259 // FIXME: maybe we should try to throw on overflow?
260 // CHECK-LABEL: define{{.*}} void @_ZN6test155test2EPvi(
261 // CHECK: [[N:%.*]] = load i32, i32*
262 // CHECK-NEXT: [[T0:%.*]] = sext i32 [[N]] to i64
263 // CHECK-NEXT: [[P:%.*]] = load i8*, i8**
264 // CHECK: [[BEGIN:%.*]] = bitcast i8* [[P]] to [[A:%.*]]*
265 // CHECK-NEXT: [[ISEMPTY:%.*]] = icmp eq i64 [[T0]], 0
266 // CHECK-NEXT: br i1 [[ISEMPTY]],
267 // CHECK: [[END:%.*]] = getelementptr inbounds [[A]], [[A]]* [[BEGIN]], i64 [[T0]]
268 // CHECK-NEXT: br label
269 // CHECK: [[CUR:%.*]] = phi [[A]]* [ [[BEGIN]],
270 // CHECK-NEXT: call void @_ZN6test151AC1Ev([[A]]* {{[^,]*}} [[CUR]])
test2(void * p,int n)271 void test2(void *p, int n) {
272 new (p) A[n];
273 }
274 }
275
276 namespace PR10197 {
277 // CHECK-LABEL: define weak_odr void @_ZN7PR101971fIiEEvv()
278 template<typename T>
f()279 void f() {
280 // CHECK: [[CALL:%.*]] = call noalias nonnull i8* @_Znwm
281 // CHECK-NEXT: [[CASTED:%.*]] = bitcast i8* [[CALL]] to
282 new T;
283 // CHECK-NEXT: ret void
284 }
285
286 template void f<int>();
287 }
288
289 namespace PR11523 {
290 class MyClass;
291 typedef int MyClass::* NewTy;
292 // CHECK-LABEL: define{{.*}} i64* @_ZN7PR115231fEv
293 // CHECK: store i64 -1
f()294 NewTy* f() { return new NewTy[2](); }
295 }
296
297 namespace PR11757 {
298 // Make sure we elide the copy construction.
299 struct X { X(); X(const X&); };
a(X * x)300 X* a(X* x) { return new X(X()); }
301 // CHECK: define {{.*}} @_ZN7PR117571aEPNS_1XE
302 // CHECK: [[CALL:%.*]] = call noalias nonnull i8* @_Znwm
303 // CHECK-NEXT: [[CASTED:%.*]] = bitcast i8* [[CALL]] to
304 // CHECK-NEXT: call void @_ZN7PR117571XC1Ev({{.*}}* {{[^,]*}} [[CASTED]])
305 // CHECK-NEXT: ret {{.*}} [[CASTED]]
306 }
307
308 namespace PR13380 {
APR13380::A309 struct A { A() {} };
310 struct B : public A { int x; };
311 // CHECK-LABEL: define{{.*}} i8* @_ZN7PR133801fEv
312 // CHECK: call noalias nonnull i8* @_Znam(
313 // CHECK: call void @llvm.memset.p0i8
314 // CHECK-NEXT: call void @_ZN7PR133801BC1Ev
f()315 void* f() { return new B[2](); }
316 }
317
318 struct MyPlacementType {} mpt;
319 void *operator new(size_t, MyPlacementType);
320
321 namespace N3664 {
322 struct S { S() throw(int); };
323
324 // CHECK-LABEL: define{{.*}} void @_ZN5N36641fEv
f()325 void f() {
326 // CHECK: call noalias nonnull i8* @_Znwm(i64 4) [[ATTR_BUILTIN_NEW:#[^ ]*]]
327 int *p = new int; // expected-note {{allocated with 'new' here}}
328 // CHECK: call void @_ZdlPv({{.*}}) [[ATTR_BUILTIN_DELETE:#[^ ]*]]
329 delete p;
330
331 // CHECK: call noalias nonnull i8* @_Znam(i64 12) [[ATTR_BUILTIN_NEW]]
332 int *q = new int[3];
333 // CHECK: call void @_ZdaPv({{.*}}) [[ATTR_BUILTIN_DELETE]]
334 delete[] p; // expected-warning {{'delete[]' applied to a pointer that was allocated with 'new'; did you mean 'delete'?}}
335
336 // CHECK: call noalias i8* @_ZnamRKSt9nothrow_t(i64 3, {{.*}}) [[ATTR_NOBUILTIN_NOUNWIND_ALLOCSIZE:#[^ ]*]]
337 (void) new (nothrow) S[3];
338
339 // CHECK: call i8* @_Znwm15MyPlacementType(i64 4){{$}}
340 (void) new (mpt) int;
341 }
342
343 // CHECK: declare i8* @_ZnamRKSt9nothrow_t(i64, {{.*}}) [[ATTR_NOBUILTIN_NOUNWIND_ALLOCSIZE:#[^ ]*]]
344
345 // CHECK-LABEL: define{{.*}} void @_ZN5N36641gEv
g()346 void g() {
347 // It's OK for there to be attributes here, so long as we don't have a
348 // 'builtin' attribute.
349 // CHECK: call noalias nonnull i8* @_Znwm(i64 4) {{#[^ ]*}}{{$}}
350 int *p = (int*)operator new(4);
351 // CHECK: call void @_ZdlPv({{.*}}) [[ATTR_NOUNWIND:#[^ ]*]]
352 operator delete(p);
353
354 // CHECK: call noalias nonnull i8* @_Znam(i64 12) {{#[^ ]*}}{{$}}
355 int *q = (int*)operator new[](12);
356 // CHECK: call void @_ZdaPv({{.*}}) [[ATTR_NOUNWIND]]
357 operator delete [](p);
358
359 // CHECK: call noalias i8* @_ZnamRKSt9nothrow_t(i64 3, {{.*}}) [[ATTR_NOUNWIND_ALLOCSIZE:#[^ ]*]]
360 (void) operator new[](3, nothrow);
361 }
362 }
363
364 namespace builtins {
365 // CHECK-LABEL: define{{.*}} void @_ZN8builtins1fEv
f()366 void f() {
367 // CHECK: call noalias nonnull i8* @_Znwm(i64 4) [[ATTR_BUILTIN_NEW]]
368 // CHECK: call void @_ZdlPv({{.*}}) [[ATTR_BUILTIN_DELETE]]
369 __builtin_operator_delete(__builtin_operator_new(4));
370 }
371 }
372
373 // CHECK-DAG: attributes [[ATTR_NOBUILTIN]] = {{[{].*}} nobuiltin allocsize(0) {{.*[}]}}
374 // CHECK-DAG: attributes [[ATTR_NOBUILTIN_NOUNWIND]] = {{[{].*}} nobuiltin nounwind {{.*[}]}}
375 // CHECK-DAG: attributes [[ATTR_NOBUILTIN_NOUNWIND_ALLOCSIZE]] = {{[{].*}} nobuiltin nounwind allocsize(0) {{.*[}]}}
376
377 // CHECK-DAG: attributes [[ATTR_BUILTIN_NEW]] = {{[{].*}} builtin {{.*[}]}}
378 // CHECK-DAG: attributes [[ATTR_BUILTIN_DELETE]] = {{[{].*}} builtin {{.*[}]}}
379
380 // The ([^b}|...) monstrosity is matching a character that's not the start of 'builtin'.
381 // Add more letters if this matches some other attribute.
382 // CHECK-DAG: attributes [[ATTR_NOUNWIND]] = {{([^b]|b[^u]|bu[^i]|bui[^l])*}} nounwind {{([^b]|b[^u]|bu[^i]|bui[^l])*$}}
383 // CHECK-DAG: attributes [[ATTR_NOUNWIND_ALLOCSIZE]] = {{([^b]|b[^u]|bu[^i]|bui[^l])*}} nounwind allocsize(0) {{([^b]|b[^u]|bu[^i]|bui[^l])*$}}
384