1// RUN: %clang_cc1 -triple x86_64-unknown-unknown %s -emit-llvm -o - | FileCheck %s
2
3typedef __typeof__(sizeof(0)) size_t;
4
5void t1() {
6  int* a = new int;
7}
8
9// 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();
14
15void t2(int* a) {
16  int* b = new (a) int;
17}
18
19struct S {
20  int a;
21};
22
23// POD types.
24void t3() {
25  int *a = new int(10);
26  _Complex int* b = new _Complex int(10i);
27
28  S s;
29  s.a = 10;
30  S *sp = new S(s);
31}
32
33// Non-POD
34struct T {
35  T();
36  int a;
37};
38
39void t4() {
40  // CHECK: call void @_ZN1TC1Ev
41  T *t = new T;
42}
43
44struct T2 {
45  int a;
46  T2(int, int);
47};
48
49void t5() {
50  // CHECK: call void @_ZN2T2C1Eii
51  T2 *t2 = new T2(10, 10);
52}
53
54int *t6() {
55  // Null check.
56  return new (0) int(10);
57}
58
59void t7() {
60  new int();
61}
62
63struct U {
64  ~U();
65};
66
67void t8(int n) {
68  new int[10];
69  new int[n];
70
71  // Non-POD
72  new T[10];
73  new T[n];
74
75  // Cookie required
76  new U[10];
77  new U[n];
78}
79
80// noalias
81// CHECK: declare noalias i8* @_Znam
82void *operator new[](size_t);
83
84void t9() {
85  bool b;
86
87  new bool(true);
88  new (&b) bool(true);
89}
90
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
101// CHECK: define void @_Z3t11i
102struct B { int a; };
103struct Bmemptr { int Bmemptr::* memptr; int a; };
104
105void t11(int n) {
106  // CHECK: call noalias i8* @_Znwm
107  // CHECK: call void @llvm.memset.p0i8.i64(
108  B* b = new B();
109
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
120}
121
122struct Empty { };
123
124// We don't need to initialize an empty class.
125// CHECK: define void @_Z3t12v
126void t12() {
127  // CHECK: call noalias i8* @_Znam
128  // CHECK-NOT: br
129  (void)new Empty[10];
130
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
150}
151
152struct Alloc{
153  int x;
154  void* operator new[](size_t size);
155  void operator delete[](void* p);
156  ~Alloc();
157};
158
159void f() {
160  // CHECK: call i8* @_ZN5AllocnaEm(i64 808)
161  // CHECK: store i64 200
162  // CHECK: call void @_ZN5AllocD1Ev(
163  // CHECK: call void @_ZN5AllocdaEPv(i8*
164  delete[] new Alloc[10][20];
165  // CHECK: call noalias i8* @_Znwm
166  // CHECK: call void @_ZdlPv(i8*
167  delete new bool;
168  // CHECK: ret void
169}
170
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(
185  // CHECK:      [[P:%.*]] = load i8**
186  // CHECK-NEXT: icmp eq i8* [[P]], null
187  // CHECK-NEXT: br i1
188  // 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]]
196  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
210  // 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]])
217  void test2(void *p, int n) {
218    new (p) A[n];
219  }
220}
221
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}
234
235namespace PR11523 {
236  class MyClass;
237  typedef int MyClass::* NewTy;
238  // CHECK: define i64* @_ZN7PR115231fEv
239  // CHECK: store i64 -1
240  NewTy* f() { return new NewTy[2](); }
241}
242
243namespace PR11757 {
244  // Make sure we elide the copy construction.
245  struct X { X(); X(const X&); };
246  X* a(X* x) { return new X(X()); }
247  // CHECK: define {{.*}} @_ZN7PR117571aEPNS_1XE
248  // CHECK: [[CALL:%.*]] = call noalias i8* @_Znwm
249  // CHECK-NEXT: [[CASTED:%.*]] = bitcast i8* [[CALL]] to
250  // CHECK-NEXT: call void @_ZN7PR117571XC1Ev({{.*}}* [[CASTED]])
251  // CHECK-NEXT: ret {{.*}} [[CASTED]]
252}
253