Home | History | Annotate | Download | only in CodeGenCXX
      1 // RUN: %clang_cc1 %s -triple=thumbv7-apple-ios3.0 -fno-use-cxa-atexit -target-abi apcs-gnu -emit-llvm -o - -fexceptions | FileCheck %s
      2 
      3 // CHECK: @_ZZN5test74testEvE1x = internal global i32 0, align 4
      4 // CHECK: @_ZGVZN5test74testEvE1x = internal global i32 0
      5 // CHECK: @_ZZN5test84testEvE1x = internal global [[TEST8A:.*]] zeroinitializer, align 1
      6 // CHECK: @_ZGVZN5test84testEvE1x = internal global i32 0
      7 
      8 typedef typeof(sizeof(int)) size_t;
      9 
     10 class foo {
     11 public:
     12     foo();
     13     virtual ~foo();
     14 };
     15 
     16 class bar : public foo {
     17 public:
     18 	bar();
     19 };
     20 
     21 // The global dtor needs the right calling conv with -fno-use-cxa-atexit
     22 // rdar://7817590
     23 bar baz;
     24 
     25 // PR9593
     26 // Make sure atexit(3) is used for global dtors.
     27 
     28 // CHECK:      call [[BAR:%.*]]* @_ZN3barC1Ev(
     29 // CHECK-NEXT: call i32 @atexit(void ()* @__dtor_baz)
     30 
     31 // CHECK: define internal void @__dtor_baz()
     32 // CHECK: call [[BAR]]* @_ZN3barD1Ev([[BAR]]* @baz)
     33 
     34 // Destructors and constructors must return this.
     35 namespace test1 {
     36   void foo();
     37 
     38   struct A {
     39     A(int i) { foo(); }
     40     ~A() { foo(); }
     41     void bar() { foo(); }
     42   };
     43 
     44   // CHECK: define void @_ZN5test14testEv()
     45   void test() {
     46     // CHECK: [[AV:%.*]] = alloca [[A:%.*]], align 1
     47     // CHECK: call [[A]]* @_ZN5test11AC1Ei([[A]]* [[AV]], i32 10)
     48     // CHECK: invoke void @_ZN5test11A3barEv([[A]]* [[AV]])
     49     // CHECK: call [[A]]* @_ZN5test11AD1Ev([[A]]* [[AV]])
     50     // CHECK: ret void
     51     A a = 10;
     52     a.bar();
     53   }
     54 
     55   // CHECK: define linkonce_odr [[A]]* @_ZN5test11AC1Ei([[A]]* returned %this, i32 %i) unnamed_addr
     56   // CHECK:   [[THIS:%.*]] = alloca [[A]]*, align 4
     57   // CHECK:   store [[A]]* {{.*}}, [[A]]** [[THIS]]
     58   // CHECK:   [[THIS1:%.*]] = load [[A]]** [[THIS]]
     59   // CHECK:   {{%.*}} = call [[A]]* @_ZN5test11AC2Ei(
     60   // CHECK:   ret [[A]]* [[THIS1]]
     61 
     62   // CHECK: define linkonce_odr [[A]]* @_ZN5test11AD1Ev([[A]]* returned %this) unnamed_addr
     63   // CHECK:   [[THIS:%.*]] = alloca [[A]]*, align 4
     64   // CHECK:   store [[A]]* {{.*}}, [[A]]** [[THIS]]
     65   // CHECK:   [[THIS1:%.*]] = load [[A]]** [[THIS]]
     66   // CHECK:   {{%.*}} = call [[A]]* @_ZN5test11AD2Ev(
     67   // CHECK:   ret [[A]]* [[THIS1]]
     68 }
     69 
     70 // Awkward virtual cases.
     71 namespace test2 {
     72   void foo();
     73 
     74   struct A {
     75     int x;
     76 
     77     A(int);
     78     virtual ~A() { foo(); }
     79   };
     80 
     81   struct B {
     82     int y;
     83     int z;
     84 
     85     B(int);
     86     virtual ~B() { foo(); }
     87   };
     88 
     89   struct C : A, virtual B {
     90     int q;
     91 
     92     C(int i) : A(i), B(i) { foo(); }
     93     ~C() { foo(); }
     94   };
     95 
     96   void test() {
     97     C c = 10;
     98   }
     99 
    100   // Tests at eof
    101 }
    102 
    103 namespace test3 {
    104   struct A {
    105     int x;
    106     ~A();
    107   };
    108 
    109   void a() {
    110     // CHECK: define void @_ZN5test31aEv()
    111     // CHECK: call noalias i8* @_Znam(i32 48)
    112     // CHECK: store i32 4
    113     // CHECK: store i32 10
    114     A *x = new A[10];
    115   }
    116 
    117   void b(int n) {
    118     // CHECK: define void @_ZN5test31bEi(
    119     // CHECK: [[N:%.*]] = load i32*
    120     // CHECK: @llvm.umul.with.overflow.i32(i32 [[N]], i32 4)
    121     // CHECK: @llvm.uadd.with.overflow.i32(i32 {{.*}}, i32 8)
    122     // CHECK: [[OR:%.*]] = or i1
    123     // CHECK: [[SZ:%.*]] = select i1 [[OR]]
    124     // CHECK: call noalias i8* @_Znam(i32 [[SZ]])
    125     // CHECK: store i32 4
    126     // CHECK: store i32 [[N]]
    127     A *x = new A[n];
    128   }
    129 
    130   void c() {
    131     // CHECK: define void @_ZN5test31cEv()
    132     // CHECK: call  noalias i8* @_Znam(i32 808)
    133     // CHECK: store i32 4
    134     // CHECK: store i32 200
    135     A (*x)[20] = new A[10][20];
    136   }
    137 
    138   void d(int n) {
    139     // CHECK: define void @_ZN5test31dEi(
    140     // CHECK: [[N:%.*]] = load i32*
    141     // CHECK: @llvm.umul.with.overflow.i32(i32 [[N]], i32 80)
    142     // CHECK: [[NE:%.*]] = mul i32 [[N]], 20
    143     // CHECK: @llvm.uadd.with.overflow.i32(i32 {{.*}}, i32 8)
    144     // CHECK: [[SZ:%.*]] = select
    145     // CHECK: call noalias i8* @_Znam(i32 [[SZ]])
    146     // CHECK: store i32 4
    147     // CHECK: store i32 [[NE]]
    148     A (*x)[20] = new A[n][20];
    149   }
    150 
    151   void e(A *x) {
    152     // CHECK: define void @_ZN5test31eEPNS_1AE(
    153     // CHECK: icmp eq {{.*}}, null
    154     // CHECK: getelementptr {{.*}}, i64 -8
    155     // CHECK: getelementptr {{.*}}, i64 4
    156     // CHECK: bitcast {{.*}} to i32*
    157     // CHECK: load
    158     // CHECK: invoke {{.*}} @_ZN5test31AD1Ev
    159     // CHECK: call void @_ZdaPv
    160     delete [] x;
    161   }
    162 
    163   void f(A (*x)[20]) {
    164     // CHECK: define void @_ZN5test31fEPA20_NS_1AE(
    165     // CHECK: icmp eq {{.*}}, null
    166     // CHECK: getelementptr {{.*}}, i64 -8
    167     // CHECK: getelementptr {{.*}}, i64 4
    168     // CHECK: bitcast {{.*}} to i32*
    169     // CHECK: load
    170     // CHECK: invoke {{.*}} @_ZN5test31AD1Ev
    171     // CHECK: call void @_ZdaPv
    172     delete [] x;
    173   }
    174 }
    175 
    176 namespace test4 {
    177   struct A {
    178     int x;
    179     void operator delete[](void *, size_t sz);
    180   };
    181 
    182   void a() {
    183     // CHECK: define void @_ZN5test41aEv()
    184     // CHECK: call noalias i8* @_Znam(i32 48)
    185     // CHECK: store i32 4
    186     // CHECK: store i32 10
    187     A *x = new A[10];
    188   }
    189 
    190   void b(int n) {
    191     // CHECK: define void @_ZN5test41bEi(
    192     // CHECK: [[N:%.*]] = load i32*
    193     // CHECK: @llvm.umul.with.overflow.i32(i32 [[N]], i32 4)
    194     // CHECK: @llvm.uadd.with.overflow.i32(i32 {{.*}}, i32 8)
    195     // CHECK: [[SZ:%.*]] = select
    196     // CHECK: call noalias i8* @_Znam(i32 [[SZ]])
    197     // CHECK: store i32 4
    198     // CHECK: store i32 [[N]]
    199     A *x = new A[n];
    200   }
    201 
    202   void c() {
    203     // CHECK: define void @_ZN5test41cEv()
    204     // CHECK: call  noalias i8* @_Znam(i32 808)
    205     // CHECK: store i32 4
    206     // CHECK: store i32 200
    207     A (*x)[20] = new A[10][20];
    208   }
    209 
    210   void d(int n) {
    211     // CHECK: define void @_ZN5test41dEi(
    212     // CHECK: [[N:%.*]] = load i32*
    213     // CHECK: @llvm.umul.with.overflow.i32(i32 [[N]], i32 80)
    214     // CHECK: [[NE:%.*]] = mul i32 [[N]], 20
    215     // CHECK: @llvm.uadd.with.overflow.i32(i32 {{.*}}, i32 8)
    216     // CHECK: [[SZ:%.*]] = select
    217     // CHECK: call noalias i8* @_Znam(i32 [[SZ]])
    218     // CHECK: store i32 4
    219     // CHECK: store i32 [[NE]]
    220     A (*x)[20] = new A[n][20];
    221   }
    222 
    223   void e(A *x) {
    224     // CHECK: define void @_ZN5test41eEPNS_1AE(
    225     // CHECK: [[ALLOC:%.*]] = getelementptr inbounds {{.*}}, i64 -8
    226     // CHECK: getelementptr inbounds {{.*}}, i64 4
    227     // CHECK: bitcast
    228     // CHECK: [[T0:%.*]] = load i32*
    229     // CHECK: [[T1:%.*]] = mul i32 4, [[T0]]
    230     // CHECK: [[T2:%.*]] = add i32 [[T1]], 8
    231     // CHECK: call void @_ZN5test41AdaEPvm(i8* [[ALLOC]], i32 [[T2]])
    232     delete [] x;
    233   }
    234 
    235   void f(A (*x)[20]) {
    236     // CHECK: define void @_ZN5test41fEPA20_NS_1AE(
    237     // CHECK: [[ALLOC:%.*]] = getelementptr inbounds {{.*}}, i64 -8
    238     // CHECK: getelementptr inbounds {{.*}}, i64 4
    239     // CHECK: bitcast
    240     // CHECK: [[T0:%.*]] = load i32*
    241     // CHECK: [[T1:%.*]] = mul i32 4, [[T0]]
    242     // CHECK: [[T2:%.*]] = add i32 [[T1]], 8
    243     // CHECK: call void @_ZN5test41AdaEPvm(i8* [[ALLOC]], i32 [[T2]])
    244     delete [] x;
    245   }
    246 }
    247 
    248 // <rdar://problem/8386802>: don't crash
    249 namespace test5 {
    250   struct A {
    251     ~A();
    252   };
    253 
    254   // CHECK: define void @_ZN5test54testEPNS_1AE
    255   void test(A *a) {
    256     // CHECK:      [[PTR:%.*]] = alloca [[A:%.*]]*, align 4
    257     // CHECK-NEXT: store [[A]]* {{.*}}, [[A]]** [[PTR]], align 4
    258     // CHECK-NEXT: [[TMP:%.*]] = load [[A]]** [[PTR]], align 4
    259     // CHECK-NEXT: call [[A]]* @_ZN5test51AD1Ev([[A]]* [[TMP]])
    260     // CHECK-NEXT: ret void
    261     a->~A();
    262   }
    263 }
    264 
    265 namespace test6 {
    266   struct A {
    267     virtual ~A();
    268   };
    269 
    270   // CHECK: define void @_ZN5test64testEPNS_1AE
    271   void test(A *a) {
    272     // CHECK:      [[AVAR:%.*]] = alloca [[A:%.*]]*, align 4
    273     // CHECK-NEXT: store [[A]]* {{.*}}, [[A]]** [[AVAR]], align 4
    274     // CHECK-NEXT: [[V:%.*]] = load [[A]]** [[AVAR]], align 4
    275     // CHECK-NEXT: [[ISNULL:%.*]] = icmp eq [[A]]* [[V]], null
    276     // CHECK-NEXT: br i1 [[ISNULL]]
    277     // CHECK:      [[T0:%.*]] = bitcast [[A]]* [[V]] to void ([[A]]*)***
    278     // CHECK-NEXT: [[T1:%.*]] = load void ([[A]]*)*** [[T0]]
    279     // CHECK-NEXT: [[T2:%.*]] = getelementptr inbounds void ([[A]]*)** [[T1]], i64 1
    280     // CHECK-NEXT: [[T3:%.*]] = load void ([[A]]*)** [[T2]]
    281     // CHECK-NEXT: call void [[T3]]([[A]]* [[V]])
    282     // CHECK-NEXT: br label
    283     // CHECK:      ret void
    284     delete a;
    285   }
    286 }
    287 
    288 namespace test7 {
    289   int foo();
    290 
    291   // Static and guard tested at top of file
    292 
    293   // CHECK: define void @_ZN5test74testEv()
    294   void test() {
    295     // CHECK:      [[T0:%.*]] = load i32* @_ZGVZN5test74testEvE1x
    296     // CHECK-NEXT: [[T1:%.*]] = and i32 [[T0]], 1
    297     // CHECK-NEXT: [[T2:%.*]] = icmp eq i32 [[T1]], 0
    298     // CHECK-NEXT: br i1 [[T2]]
    299     //   -> fallthrough, end
    300     // CHECK:      [[T3:%.*]] = call i32 @__cxa_guard_acquire(i32* @_ZGVZN5test74testEvE1x)
    301     // CHECK-NEXT: [[T4:%.*]] = icmp ne i32 [[T3]], 0
    302     // CHECK-NEXT: br i1 [[T4]]
    303     //   -> fallthrough, end
    304     // CHECK:      [[INIT:%.*]] = invoke i32 @_ZN5test73fooEv()
    305     // CHECK:      store i32 [[INIT]], i32* @_ZZN5test74testEvE1x, align 4
    306     // CHECK-NEXT: call void @__cxa_guard_release(i32* @_ZGVZN5test74testEvE1x)
    307     // CHECK-NEXT: br label
    308     //   -> end
    309     // end:
    310     // CHECK:      ret void
    311     static int x = foo();
    312 
    313     // CHECK:      landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*)
    314     // CHECK-NEXT:   cleanup
    315     // CHECK:      call void @__cxa_guard_abort(i32* @_ZGVZN5test74testEvE1x)
    316     // CHECK:      resume { i8*, i32 }
    317   }
    318 }
    319 
    320 namespace test8 {
    321   struct A {
    322     A();
    323     ~A();
    324   };
    325 
    326   // Static and guard tested at top of file
    327 
    328   // CHECK: define void @_ZN5test84testEv()
    329   void test() {
    330     // CHECK:      [[T0:%.*]] = load i32* @_ZGVZN5test84testEvE1x
    331     // CHECK-NEXT: [[T1:%.*]] = and i32 [[T0]], 1
    332     // CHECK-NEXT: [[T2:%.*]] = icmp eq i32 [[T1]], 0
    333     // CHECK-NEXT: br i1 [[T2]]
    334     //   -> fallthrough, end
    335     // CHECK:      [[T3:%.*]] = call i32 @__cxa_guard_acquire(i32* @_ZGVZN5test84testEvE1x)
    336     // CHECK-NEXT: [[T4:%.*]] = icmp ne i32 [[T3]], 0
    337     // CHECK-NEXT: br i1 [[T4]]
    338     //   -> fallthrough, end
    339     // CHECK:      [[INIT:%.*]] = invoke [[TEST8A]]* @_ZN5test81AC1Ev([[TEST8A]]* @_ZZN5test84testEvE1x)
    340 
    341     // FIXME: Here we register a global destructor that
    342     // unconditionally calls the destructor.  That's what we've always
    343     // done for -fno-use-cxa-atexit here, but that's really not
    344     // semantically correct at all.
    345 
    346     // CHECK:      call void @__cxa_guard_release(i32* @_ZGVZN5test84testEvE1x)
    347     // CHECK-NEXT: br label
    348     //   -> end
    349     // end:
    350     // CHECK:      ret void
    351     static A x;
    352 
    353     // CHECK:      landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*)
    354     // CHECK-NEXT:   cleanup
    355     // CHECK:      call void @__cxa_guard_abort(i32* @_ZGVZN5test84testEvE1x)
    356     // CHECK:      resume { i8*, i32 }
    357   }
    358 }
    359 
    360 //   rdar://12836470
    361 // Use a larger-than-mandated array cookie when allocating an
    362 // array whose type is overaligned.
    363 namespace test9 {
    364   class __attribute__((aligned(16))) A {
    365     float data[4];
    366   public:
    367     A();
    368     ~A();
    369   };
    370 
    371   A *testNew(unsigned n) {
    372     return new A[n];
    373   }
    374 // CHECK:    define [[TEST9:%.*]]* @_ZN5test97testNewEj(i32
    375 // CHECK:      [[N_VAR:%.*]] = alloca i32, align 4
    376 // CHECK:      [[N:%.*]] = load i32* [[N_VAR]], align 4
    377 // CHECK-NEXT: [[T0:%.*]] = call { i32, i1 } @llvm.umul.with.overflow.i32(i32 [[N]], i32 16)
    378 // CHECK-NEXT: [[O0:%.*]] = extractvalue { i32, i1 } [[T0]], 1
    379 // CHECK-NEXT: [[T1:%.*]] = extractvalue { i32, i1 } [[T0]], 0
    380 // CHECK-NEXT: [[T2:%.*]] = call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 [[T1]], i32 16)
    381 // CHECK-NEXT: [[O1:%.*]] = extractvalue { i32, i1 } [[T2]], 1
    382 // CHECK-NEXT: [[OVERFLOW:%.*]] = or i1 [[O0]], [[O1]]
    383 // CHECK-NEXT: [[T3:%.*]] = extractvalue { i32, i1 } [[T2]], 0
    384 // CHECK-NEXT: [[T4:%.*]] = select i1 [[OVERFLOW]], i32 -1, i32 [[T3]]
    385 // CHECK-NEXT: [[ALLOC:%.*]] = call noalias i8* @_Znam(i32 [[T4]])
    386 // CHECK-NEXT: [[T0:%.*]] = bitcast i8* [[ALLOC]] to i32*
    387 // CHECK-NEXT: store i32 16, i32* [[T0]]
    388 // CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds i32* [[T0]], i32 1
    389 // CHECK-NEXT: store i32 [[N]], i32* [[T1]]
    390 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds i8* [[ALLOC]], i64 16
    391 // CHECK-NEXT: bitcast i8* [[T0]] to [[TEST9]]*
    392 //   Array allocation follows.
    393 
    394   void testDelete(A *array) {
    395     delete[] array;
    396   }
    397 // CHECK:    define void @_ZN5test910testDeleteEPNS_1AE(
    398 // CHECK:      [[BEGIN:%.*]] = load [[TEST9]]**
    399 // CHECK-NEXT: [[T0:%.*]] = icmp eq [[TEST9]]* [[BEGIN]], null
    400 // CHECK-NEXT: br i1 [[T0]],
    401 // CHECK:      [[T0:%.*]] = bitcast [[TEST9]]* [[BEGIN]] to i8*
    402 // CHECK-NEXT: [[ALLOC:%.*]] = getelementptr inbounds i8* [[T0]], i64 -16
    403 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds i8* [[ALLOC]], i64 4
    404 // CHECK-NEXT: [[T1:%.*]] = bitcast i8* [[T0]] to i32*
    405 // CHECK-NEXT: [[N:%.*]] = load i32* [[T1]]
    406 // CHECK-NEXT: [[END:%.*]] = getelementptr inbounds [[TEST9]]* [[BEGIN]], i32 [[N]]
    407 // CHECK-NEXT: [[T0:%.*]] = icmp eq [[TEST9]]* [[BEGIN]], [[END]]
    408 // CHECK-NEXT: br i1 [[T0]],
    409 //   Array deallocation follows.
    410 }
    411 
    412   // CHECK: define linkonce_odr [[C:%.*]]* @_ZTv0_n12_N5test21CD1Ev(
    413   // CHECK:   call [[C]]* @_ZN5test21CD1Ev(
    414   // CHECK:   ret [[C]]* undef
    415 
    416   // CHECK: define linkonce_odr void @_ZTv0_n12_N5test21CD0Ev(
    417   // CHECK:   call void @_ZN5test21CD0Ev(
    418   // CHECK:   ret void
    419 
    420 // CH_ECK: @_GLOBAL__D_a()
    421 // CH_ECK: call %class.bar* @_ZN3barD1Ev(%class.bar* @baz)
    422