[RISCV] Add shrinkwrap test cases showing gaps in current impl
[llvm-project.git] / clang / test / CodeGenCXX / new.cpp
blobaf225529c494e625c408fed5255c980dfd384dfa
1 // RUN: %clang_cc1 -std=c++14 -triple x86_64-unknown-unknown %s -emit-llvm -o - | FileCheck %s
3 typedef __typeof__(sizeof(0)) size_t;
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));
8 // Ensure that this declaration doesn't cause operator new to lose its
9 // 'noalias' attribute.
10 void *operator new[](size_t);
12 void t1() {
13 delete new int;
14 delete [] new int [3];
17 // CHECK: declare noundef nonnull ptr @_Znwm(i64 noundef) [[ATTR_NOBUILTIN:#[^ ]*]]
18 // CHECK: declare void @_ZdlPvm(ptr noundef, i64 noundef) [[ATTR_NOBUILTIN_NOUNWIND:#[^ ]*]]
19 // CHECK: declare noundef nonnull ptr @_Znam(i64 noundef) [[ATTR_NOBUILTIN]]
20 // CHECK: declare void @_ZdaPv(ptr noundef) [[ATTR_NOBUILTIN_NOUNWIND]]
22 namespace std {
23 struct nothrow_t {};
25 std::nothrow_t nothrow;
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();
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();
39 // Declare some other placemenet operators.
40 void *operator new(size_t, void*, bool) throw();
41 void *operator new[](size_t, void*, bool) throw();
43 void t2(int* a) {
44 int* b = new (a) int;
47 struct S {
48 int a;
51 // POD types.
52 void t3() {
53 int *a = new int(10);
54 _Complex int* b = new _Complex int(10i);
56 S s;
57 s.a = 10;
58 S *sp = new S(s);
61 // Non-POD
62 struct T {
63 T();
64 int a;
67 void t4() {
68 // CHECK: call void @_ZN1TC1Ev
69 T *t = new T;
72 struct T2 {
73 int a;
74 T2(int, int);
77 void t5() {
78 // CHECK: call void @_ZN2T2C1Eii
79 T2 *t2 = new T2(10, 10);
82 int *t6() {
83 // Null check.
84 return new (0) int(10);
87 void t7() {
88 new int();
91 struct U {
92 ~U();
95 void t8(int n) {
96 new int[10];
97 new int[n];
99 // Non-POD
100 new T[10];
101 new T[n];
103 // Cookie required
104 new U[10];
105 new U[n];
108 void t9() {
109 bool b;
111 new bool(true);
112 new (&b) bool(true);
115 struct A {
116 void* operator new(__typeof(sizeof(int)), int, float, ...);
117 A();
120 A* t10() {
121 // CHECK: @_ZN1AnwEmifz
122 return new(1, 2, 3.45, 100) A;
125 // CHECK-LABEL: define{{.*}} void @_Z3t11i
126 struct B { int a; };
127 struct Bmemptr { int Bmemptr::* memptr; int a; };
129 void t11(int n) {
130 // CHECK: call noalias noundef nonnull ptr @_Znwm
131 // CHECK: call void @llvm.memset.p0.i64(
132 B* b = new B();
134 // CHECK: call noalias noundef nonnull ptr @_Znam
135 // CHECK: {{call void.*llvm.memset.p0.i64.*i8 0, i64 %}}
136 B *b2 = new B[n]();
138 // CHECK: call noalias noundef nonnull ptr @_Znam
139 // CHECK: call void @llvm.memcpy.p0.p0.i64
140 // CHECK: br
141 Bmemptr *b_memptr = new Bmemptr[n]();
143 // CHECK: ret void
146 struct Empty { };
148 // We don't need to initialize an empty class.
149 // CHECK-LABEL: define{{.*}} void @_Z3t12v
150 void t12() {
151 // CHECK: call noalias noundef nonnull ptr @_Znam
152 // CHECK-NOT: br
153 (void)new Empty[10];
155 // CHECK: call noalias noundef nonnull ptr @_Znam
156 // CHECK-NOT: br
157 (void)new Empty[10]();
159 // CHECK: ret void
162 // Zero-initialization
163 // CHECK-LABEL: define{{.*}} void @_Z3t13i
164 void t13(int n) {
165 // CHECK: call noalias noundef nonnull ptr @_Znwm
166 // CHECK: store i32 0, ptr
167 (void)new int();
169 // CHECK: call noalias noundef nonnull ptr @_Znam
170 // CHECK: {{call void.*llvm.memset.p0.i64.*i8 0, i64 %}}
171 (void)new int[n]();
173 // CHECK-NEXT: ret void
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();
184 void f() {
185 // CHECK: call noundef ptr @_ZN5AllocnaEm(i64 noundef 808)
186 // CHECK: store i64 200
187 // CHECK: call void @_ZN5AllocD1Ev(
188 // CHECK: call void @_ZN5AllocdaEPv(ptr
189 delete[] new Alloc[10][20];
190 // CHECK: [[P:%.*]] = call noundef nonnull ptr @_ZN5AllocnaEmRKSt9nothrow_t(i64 noundef 808, {{.*}}) [[ATTR_NOUNWIND:#[^ ]*]]
191 // CHECK-NOT: icmp eq ptr [[P]], null
192 // CHECK: store i64 200
193 delete[] new (nothrow) Alloc[10][20];
194 // CHECK: call noalias noundef nonnull ptr @_Znwm
195 // CHECK: call void @_ZdlPvm(ptr noundef {{%.*}}, i64 noundef 1)
196 delete new bool;
197 // CHECK: ret void
200 namespace test15 {
201 struct A { A(); ~A(); };
203 // CHECK-LABEL: define{{.*}} void @_ZN6test156test0aEPv(
204 // CHECK: [[P:%.*]] = load ptr, ptr
205 // CHECK-NOT: icmp eq ptr [[P]], null
206 // CHECK-NOT: br i1
207 // CHECK-NEXT: call void @_ZN6test151AC1Ev(ptr {{[^,]*}} [[P]])
208 void test0a(void *p) {
209 new (p) A();
212 // CHECK-LABEL: define{{.*}} void @_ZN6test156test0bEPv(
213 // CHECK: [[P0:%.*]] = load ptr, ptr
214 // CHECK: [[P:%.*]] = call noundef ptr @_ZnwmPvb(i64 noundef 1, ptr noundef [[P0]]
215 // CHECK-NEXT: icmp eq ptr [[P]], null
216 // CHECK-NEXT: br i1
217 // CHECK: call void @_ZN6test151AC1Ev(ptr {{[^,]*}} [[P]])
218 void test0b(void *p) {
219 new (p, true) A();
222 // CHECK-LABEL: define{{.*}} void @_ZN6test156test1aEPv(
223 // CHECK: [[P:%.*]] = load ptr, ptr
224 // CHECK-NOT: icmp eq ptr [[P]], null
225 // CHECK-NOT: br i1
226 // CHECK-NEXT: [[END:%.*]] = getelementptr inbounds [[A:.*]], ptr [[P]], i64 5
227 // CHECK-NEXT: br label
228 // CHECK: [[CUR:%.*]] = phi ptr [ [[P]], {{%.*}} ], [ [[NEXT:%.*]], {{%.*}} ]
229 // CHECK-NEXT: call void @_ZN6test151AC1Ev(ptr {{[^,]*}} [[CUR]])
230 // CHECK-NEXT: [[NEXT]] = getelementptr inbounds [[A]], ptr [[CUR]], i64 1
231 // CHECK-NEXT: [[DONE:%.*]] = icmp eq ptr [[NEXT]], [[END]]
232 // CHECK-NEXT: br i1 [[DONE]]
233 void test1a(void *p) {
234 new (p) A[5];
237 // CHECK-LABEL: define{{.*}} void @_ZN6test156test1bEPv(
238 // CHECK: [[P0:%.*]] = load ptr, ptr
239 // CHECK: [[P:%.*]] = call noundef ptr @_ZnamPvb(i64 noundef 13, ptr noundef [[P0]]
240 // CHECK-NEXT: icmp eq ptr [[P]], null
241 // CHECK-NEXT: br i1
242 // CHECK: [[AFTER_COOKIE:%.*]] = getelementptr inbounds i8, ptr [[P]], i64 8
243 // CHECK-NEXT: [[END:%.*]] = getelementptr inbounds [[A]], ptr [[AFTER_COOKIE]], i64 5
244 // CHECK-NEXT: br label
245 // CHECK: [[CUR:%.*]] = phi ptr [ [[AFTER_COOKIE]], {{%.*}} ], [ [[NEXT:%.*]], {{%.*}} ]
246 // CHECK-NEXT: call void @_ZN6test151AC1Ev(ptr {{[^,]*}} [[CUR]])
247 // CHECK-NEXT: [[NEXT]] = getelementptr inbounds [[A]], ptr [[CUR]], i64 1
248 // CHECK-NEXT: [[DONE:%.*]] = icmp eq ptr [[NEXT]], [[END]]
249 // CHECK-NEXT: br i1 [[DONE]]
250 void test1b(void *p) {
251 new (p, true) A[5];
254 // TODO: it's okay if all these size calculations get dropped.
255 // FIXME: maybe we should try to throw on overflow?
256 // CHECK-LABEL: define{{.*}} void @_ZN6test155test2EPvi(
257 // CHECK: [[N:%.*]] = load i32, ptr
258 // CHECK-NEXT: [[T0:%.*]] = sext i32 [[N]] to i64
259 // CHECK-NEXT: [[P:%.*]] = load ptr, ptr
260 // CHECK-NEXT: [[ISEMPTY:%.*]] = icmp eq i64 [[T0]], 0
261 // CHECK-NEXT: br i1 [[ISEMPTY]],
262 // CHECK: [[END:%.*]] = getelementptr inbounds [[A]], ptr [[P]], i64 [[T0]]
263 // CHECK-NEXT: br label
264 // CHECK: [[CUR:%.*]] = phi ptr [ [[P]],
265 // CHECK-NEXT: call void @_ZN6test151AC1Ev(ptr {{[^,]*}} [[CUR]])
266 void test2(void *p, int n) {
267 new (p) A[n];
271 namespace PR10197 {
272 // CHECK-LABEL: define weak_odr void @_ZN7PR101971fIiEEvv()
273 template<typename T>
274 void f() {
275 // CHECK: [[CALL:%.*]] = call noalias noundef nonnull ptr @_Znwm
276 new T;
277 // CHECK-NEXT: ret void
280 template void f<int>();
283 namespace PR11523 {
284 class MyClass;
285 typedef int MyClass::* NewTy;
286 // CHECK-LABEL: define{{.*}} ptr @_ZN7PR115231fEv
287 // CHECK: store i64 -1
288 NewTy* f() { return new NewTy[2](); }
291 namespace PR11757 {
292 // Make sure we elide the copy construction.
293 struct X { X(); X(const X&); };
294 X* a(X* x) { return new X(X()); }
295 // CHECK: define {{.*}} @_ZN7PR117571aEPNS_1XE
296 // CHECK: [[CALL:%.*]] = call noalias noundef nonnull ptr @_Znwm
297 // CHECK: ret {{.*}} [[CALL]]
300 namespace PR13380 {
301 struct A { A() {} };
302 struct B : public A { int x; };
303 // CHECK-LABEL: define{{.*}} ptr @_ZN7PR133801fEv
304 // CHECK: call noalias noundef nonnull ptr @_Znam(
305 // CHECK: call void @llvm.memset.p0
306 // CHECK-NEXT: call void @_ZN7PR133801BC1Ev
307 void* f() { return new B[2](); }
310 struct MyPlacementType {} mpt;
311 void *operator new(size_t, MyPlacementType);
313 namespace N3664 {
314 struct S { S() throw(int); };
316 // CHECK-LABEL: define{{.*}} void @_ZN5N36641fEv
317 void f() {
318 // CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 4) [[ATTR_BUILTIN_NEW:#[^ ]*]]
319 int *p = new int; // expected-note {{allocated with 'new' here}}
320 // CHECK: call void @_ZdlPvm({{.*}}) [[ATTR_BUILTIN_DELETE:#[^ ]*]]
321 delete p;
323 // CHECK: call noalias noundef nonnull ptr @_Znam(i64 noundef 12) [[ATTR_BUILTIN_NEW]]
324 int *q = new int[3];
325 // CHECK: call void @_ZdaPv({{.*}}) [[ATTR_BUILTIN_DELETE]]
326 delete[] p; // expected-warning {{'delete[]' applied to a pointer that was allocated with 'new'; did you mean 'delete'?}}
328 // CHECK: call noalias noundef ptr @_ZnamRKSt9nothrow_t(i64 noundef 3, {{.*}}) [[ATTR_NOBUILTIN_NOUNWIND_ALLOCSIZE:#[^ ]*]]
329 (void) new (nothrow) S[3];
331 // CHECK: call noundef ptr @_Znwm15MyPlacementType(i64 noundef 4){{$}}
332 (void) new (mpt) int;
335 // CHECK: declare noundef ptr @_ZnamRKSt9nothrow_t(i64 noundef, {{.*}}) [[ATTR_NOBUILTIN_NOUNWIND_ALLOCSIZE:#[^ ]*]]
337 // CHECK-LABEL: define{{.*}} void @_ZN5N36641gEv
338 void g() {
339 // It's OK for there to be attributes here, so long as we don't have a
340 // 'builtin' attribute.
341 // CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 4) {{#[^ ]*}}{{$}}
342 int *p = (int*)operator new(4);
343 // CHECK: call void @_ZdlPv({{.*}}) [[ATTR_NOUNWIND:#[^ ]*]]
344 operator delete(p);
346 // CHECK: call noalias noundef nonnull ptr @_Znam(i64 noundef 12) {{#[^ ]*}}{{$}}
347 int *q = (int*)operator new[](12);
348 // CHECK: call void @_ZdaPv({{.*}}) [[ATTR_NOUNWIND]]
349 operator delete [](p);
351 // CHECK: call noalias noundef ptr @_ZnamRKSt9nothrow_t(i64 noundef 3, {{.*}}) [[ATTR_NOUNWIND_ALLOCSIZE:#[^ ]*]]
352 (void) operator new[](3, nothrow);
356 namespace builtins {
357 // CHECK-LABEL: define{{.*}} void @_ZN8builtins1fEv
358 void f() {
359 // CHECK: call noalias noundef nonnull ptr @_Znwm(i64 noundef 4) [[ATTR_BUILTIN_NEW]]
360 // CHECK: call void @_ZdlPv({{.*}}) [[ATTR_BUILTIN_DELETE]]
361 __builtin_operator_delete(__builtin_operator_new(4));
365 // CHECK-DAG: attributes [[ATTR_NOBUILTIN]] = {{[{].*}} nobuiltin allocsize(0) {{.*[}]}}
366 // CHECK-DAG: attributes [[ATTR_NOBUILTIN_NOUNWIND]] = {{[{].*}} nobuiltin nounwind {{.*[}]}}
367 // CHECK-DAG: attributes [[ATTR_NOBUILTIN_NOUNWIND_ALLOCSIZE]] = {{[{].*}} nobuiltin nounwind allocsize(0) {{.*[}]}}
369 // CHECK-DAG: attributes [[ATTR_BUILTIN_NEW]] = {{[{].*}} builtin {{.*[}]}}
370 // CHECK-DAG: attributes [[ATTR_BUILTIN_DELETE]] = {{[{].*}} builtin {{.*[}]}}
372 // The ([^b}|...) monstrosity is matching a character that's not the start of 'builtin'.
373 // Add more letters if this matches some other attribute.
374 // CHECK-DAG: attributes [[ATTR_NOUNWIND]] = {{([^b]|b[^u]|bu[^i]|bui[^l])*}} nounwind {{([^b]|b[^u]|bu[^i]|bui[^l])*$}}
375 // CHECK-DAG: attributes [[ATTR_NOUNWIND_ALLOCSIZE]] = {{([^b]|b[^u]|bu[^i]|bui[^l])*}} nounwind allocsize(0) {{([^b]|b[^u]|bu[^i]|bui[^l])*$}}