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