Daniel Dunbar | a572887 | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -triple x86_64-unknown-unknown %s -emit-llvm -o - | FileCheck %s |
John McCall | b1c98a3 | 2011-05-16 01:05:12 +0000 | [diff] [blame] | 2 | |
| 3 | typedef __typeof__(sizeof(0)) size_t; |
Anders Carlsson | ed4e367 | 2009-05-31 20:21:44 +0000 | [diff] [blame] | 4 | |
| 5 | void t1() { |
| 6 | int* a = new int; |
| 7 | } |
Anders Carlsson | d958389 | 2009-05-31 20:26:12 +0000 | [diff] [blame] | 8 | |
John McCall | b1c98a3 | 2011-05-16 01:05:12 +0000 | [diff] [blame] | 9 | // Declare the reserved placement operators. |
| 10 | void *operator new(size_t, void*) throw(); |
| 11 | void operator delete(void*, void*) throw(); |
| 12 | void *operator new[](size_t, void*) throw(); |
| 13 | void operator delete[](void*, void*) throw(); |
Anders Carlsson | d958389 | 2009-05-31 20:26:12 +0000 | [diff] [blame] | 14 | |
| 15 | void t2(int* a) { |
| 16 | int* b = new (a) int; |
| 17 | } |
Anders Carlsson | 6d0ffad | 2009-05-31 20:56:36 +0000 | [diff] [blame] | 18 | |
Anders Carlsson | 627a3e5 | 2009-05-31 21:12:26 +0000 | [diff] [blame] | 19 | struct S { |
| 20 | int a; |
| 21 | }; |
| 22 | |
Anders Carlsson | d3fd6ba | 2009-05-31 21:53:59 +0000 | [diff] [blame] | 23 | // POD types. |
Anders Carlsson | 6d0ffad | 2009-05-31 20:56:36 +0000 | [diff] [blame] | 24 | void t3() { |
| 25 | int *a = new int(10); |
Anders Carlsson | 3923e95 | 2009-05-31 21:07:58 +0000 | [diff] [blame] | 26 | _Complex int* b = new _Complex int(10i); |
| 27 | |
Anders Carlsson | 627a3e5 | 2009-05-31 21:12:26 +0000 | [diff] [blame] | 28 | S s; |
| 29 | s.a = 10; |
| 30 | S *sp = new S(s); |
Anders Carlsson | 6d0ffad | 2009-05-31 20:56:36 +0000 | [diff] [blame] | 31 | } |
Anders Carlsson | d3fd6ba | 2009-05-31 21:53:59 +0000 | [diff] [blame] | 32 | |
| 33 | // Non-POD |
| 34 | struct T { |
| 35 | T(); |
| 36 | int a; |
| 37 | }; |
| 38 | |
| 39 | void t4() { |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 40 | // CHECK: call void @_ZN1TC1Ev |
Anders Carlsson | d3fd6ba | 2009-05-31 21:53:59 +0000 | [diff] [blame] | 41 | T *t = new T; |
| 42 | } |
| 43 | |
| 44 | struct T2 { |
| 45 | int a; |
| 46 | T2(int, int); |
| 47 | }; |
| 48 | |
| 49 | void t5() { |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 50 | // CHECK: call void @_ZN2T2C1Eii |
Anders Carlsson | d3fd6ba | 2009-05-31 21:53:59 +0000 | [diff] [blame] | 51 | T2 *t2 = new T2(10, 10); |
| 52 | } |
Anders Carlsson | f110853 | 2009-06-01 00:05:16 +0000 | [diff] [blame] | 53 | |
| 54 | int *t6() { |
| 55 | // Null check. |
| 56 | return new (0) int(10); |
| 57 | } |
Anders Carlsson | 215bd20 | 2009-06-01 00:26:14 +0000 | [diff] [blame] | 58 | |
| 59 | void t7() { |
| 60 | new int(); |
| 61 | } |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 62 | |
Anders Carlsson | 6ac5fc4 | 2009-09-23 18:59:48 +0000 | [diff] [blame] | 63 | struct U { |
| 64 | ~U(); |
| 65 | }; |
| 66 | |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 67 | void t8(int n) { |
| 68 | new int[10]; |
| 69 | new int[n]; |
| 70 | |
| 71 | // Non-POD |
| 72 | new T[10]; |
| 73 | new T[n]; |
Anders Carlsson | 6ac5fc4 | 2009-09-23 18:59:48 +0000 | [diff] [blame] | 74 | |
| 75 | // Cookie required |
| 76 | new U[10]; |
| 77 | new U[n]; |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 78 | } |
Anders Carlsson | 09edb9c | 2009-11-22 19:25:33 +0000 | [diff] [blame] | 79 | |
Douglas Gregor | 7b86862 | 2010-08-18 15:06:25 +0000 | [diff] [blame] | 80 | // noalias |
| 81 | // CHECK: declare noalias i8* @_Znam |
| 82 | void *operator new[](size_t); |
| 83 | |
Anders Carlsson | 09edb9c | 2009-11-22 19:25:33 +0000 | [diff] [blame] | 84 | void t9() { |
| 85 | bool b; |
| 86 | |
| 87 | new bool(true); |
| 88 | new (&b) bool(true); |
| 89 | } |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 90 | |
| 91 | struct A { |
| 92 | void* operator new(__typeof(sizeof(int)), int, float, ...); |
| 93 | A(); |
| 94 | }; |
| 95 | |
| 96 | A* t10() { |
| 97 | // CHECK: @_ZN1AnwEmifz |
| 98 | return new(1, 2, 3.45, 100) A; |
| 99 | } |
| 100 | |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 101 | // CHECK: define void @_Z3t11i |
Anders Carlsson | 0d7c583 | 2010-05-03 01:20:20 +0000 | [diff] [blame] | 102 | struct B { int a; }; |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 103 | struct Bmemptr { int Bmemptr::* memptr; int a; }; |
| 104 | |
| 105 | void t11(int n) { |
Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 106 | // CHECK: call noalias i8* @_Znwm |
Mon P Wang | 3ecd785 | 2010-04-04 03:10:52 +0000 | [diff] [blame] | 107 | // CHECK: call void @llvm.memset.p0i8.i64( |
Anders Carlsson | 30311fa | 2009-12-16 06:57:54 +0000 | [diff] [blame] | 108 | B* b = new B(); |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 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 |
Anders Carlsson | 30311fa | 2009-12-16 06:57:54 +0000 | [diff] [blame] | 120 | } |
Anders Carlsson | e99bdb6 | 2010-05-03 15:09:17 +0000 | [diff] [blame] | 121 | |
| 122 | struct Empty { }; |
| 123 | |
| 124 | // We don't need to initialize an empty class. |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 125 | // CHECK: define void @_Z3t12v |
Anders Carlsson | e99bdb6 | 2010-05-03 15:09:17 +0000 | [diff] [blame] | 126 | void t12() { |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 127 | // CHECK: call noalias i8* @_Znam |
| 128 | // CHECK-NOT: br |
Anders Carlsson | e99bdb6 | 2010-05-03 15:09:17 +0000 | [diff] [blame] | 129 | (void)new Empty[10]; |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 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 |
| 140 | void 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 Carlsson | e99bdb6 | 2010-05-03 15:09:17 +0000 | [diff] [blame] | 150 | } |
Argyrios Kyrtzidis | d293298 | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 151 | |
| 152 | struct Alloc{ |
Argyrios Kyrtzidis | e7ab92e | 2010-08-26 15:23:38 +0000 | [diff] [blame] | 153 | int x; |
Argyrios Kyrtzidis | d293298 | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 154 | void* operator new[](size_t size); |
| 155 | void operator delete[](void* p); |
Argyrios Kyrtzidis | e7ab92e | 2010-08-26 15:23:38 +0000 | [diff] [blame] | 156 | ~Alloc(); |
Argyrios Kyrtzidis | d293298 | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | void f() { |
Argyrios Kyrtzidis | e7ab92e | 2010-08-26 15:23:38 +0000 | [diff] [blame] | 160 | // CHECK: call i8* @_ZN5AllocnaEm(i64 808) |
| 161 | // CHECK: store i64 200 |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 162 | // CHECK: call void @_ZN5AllocD1Ev( |
Argyrios Kyrtzidis | d293298 | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 163 | // CHECK: call void @_ZN5AllocdaEPv(i8* |
| 164 | delete[] new Alloc[10][20]; |
Douglas Gregor | cc09c02 | 2010-09-02 23:24:14 +0000 | [diff] [blame] | 165 | // CHECK: call noalias i8* @_Znwm |
| 166 | // CHECK: call void @_ZdlPv(i8* |
| 167 | delete new bool; |
| 168 | // CHECK: ret void |
Argyrios Kyrtzidis | d293298 | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 169 | } |
John McCall | b1c98a3 | 2011-05-16 01:05:12 +0000 | [diff] [blame] | 170 | |
| 171 | namespace 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 McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 185 | // CHECK: [[P:%.*]] = load i8** |
John McCall | b1c98a3 | 2011-05-16 01:05:12 +0000 | [diff] [blame] | 186 | // CHECK-NEXT: icmp eq i8* [[P]], null |
| 187 | // CHECK-NEXT: br i1 |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 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]] |
John McCall | b1c98a3 | 2011-05-16 01:05:12 +0000 | [diff] [blame] | 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 |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 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]]) |
John McCall | b1c98a3 | 2011-05-16 01:05:12 +0000 | [diff] [blame] | 217 | void test2(void *p, int n) { |
| 218 | new (p) A[n]; |
| 219 | } |
| 220 | } |
Douglas Gregor | d0e8b78 | 2011-06-27 16:55:54 +0000 | [diff] [blame] | 221 | |
| 222 | namespace 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 | } |
Eli Friedman | 40563cd | 2011-12-09 23:05:37 +0000 | [diff] [blame] | 234 | |
| 235 | namespace 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 | } |
Eli Friedman | 22cfaf5 | 2012-02-16 22:45:48 +0000 | [diff] [blame] | 242 | |
| 243 | namespace 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 | } |
Eli Friedman | b41ba1a | 2012-08-25 07:11:29 +0000 | [diff] [blame] | 253 | |
| 254 | namespace PR13380 { |
| 255 | struct A { A() {} }; |
| 256 | struct B : public A { int x; }; |
| 257 | // CHECK: define i8* @_ZN7PR133801fEv |
| 258 | // CHECK: call noalias i8* @_Znam( |
| 259 | // CHECK: call void @llvm.memset.p0i8 |
| 260 | // CHECK-NEXT: call void @_ZN7PR133801BC1Ev |
| 261 | void* f() { return new B[2](); } |
| 262 | } |