Home | History | Annotate | Download | only in CodeGenCXX
      1 // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -triple x86_64-apple-darwin -std=c++11 -emit-llvm %s -o %t.ll
      2 // RUN: FileCheck --input-file=%t.ll %s
      3 
      4 struct test1_D {
      5   double d;
      6 } d1;
      7 
      8 void test1() {
      9   throw d1;
     10 }
     11 
     12 // CHECK-LABEL:     define void @_Z5test1v()
     13 // CHECK:       [[EXNOBJ:%.*]] = call i8* @__cxa_allocate_exception(i64 8)
     14 // CHECK-NEXT:  [[EXN:%.*]] = bitcast i8* [[EXNOBJ]] to [[DSTAR:%[^*]*\*]]
     15 // CHECK-NEXT:  [[EXN2:%.*]] = bitcast [[DSTAR]] [[EXN]] to i8*
     16 // CHECK-NEXT:  call void @llvm.memcpy.p0i8.p0i8.i64(i8* [[EXN2]], i8* bitcast ([[DSTAR]] @d1 to i8*), i64 8, i32 8, i1 false)
     17 // CHECK-NEXT:  call void @__cxa_throw(i8* [[EXNOBJ]], i8* bitcast ({ i8*, i8* }* @_ZTI7test1_D to i8*), i8* null) [[NR:#[0-9]+]]
     18 // CHECK-NEXT:  unreachable
     19 
     20 
     21 struct test2_D {
     22   test2_D(const test2_D&o);
     23   test2_D();
     24   virtual void bar() { }
     25   int i; int j;
     26 } d2;
     27 
     28 void test2() {
     29   throw d2;
     30 }
     31 
     32 // CHECK-LABEL:     define void @_Z5test2v()
     33 // CHECK:       [[EXNVAR:%.*]] = alloca i8*
     34 // CHECK-NEXT:  [[SELECTORVAR:%.*]] = alloca i32
     35 // CHECK-NEXT:  [[EXNOBJ:%.*]] = call i8* @__cxa_allocate_exception(i64 16)
     36 // CHECK-NEXT:  [[EXN:%.*]] = bitcast i8* [[EXNOBJ]] to [[DSTAR:%[^*]*\*]]
     37 // CHECK-NEXT:  invoke void @_ZN7test2_DC1ERKS_([[DSTAR]] [[EXN]], [[DSTAR]] nonnull @d2)
     38 // CHECK-NEXT:     to label %[[CONT:.*]] unwind label %{{.*}}
     39 //      :     [[CONT]]:   (can't check this in Release-Asserts builds)
     40 // CHECK:       call void @__cxa_throw(i8* [[EXNOBJ]], i8* bitcast ({{.*}}* @_ZTI7test2_D to i8*), i8* null) [[NR]]
     41 // CHECK-NEXT:  unreachable
     42 
     43 
     44 struct test3_D {
     45   test3_D() { }
     46   test3_D(volatile test3_D&o);
     47   virtual void bar();
     48 };
     49 
     50 void test3() {
     51   throw (volatile test3_D *)0;
     52 }
     53 
     54 // CHECK-LABEL:     define void @_Z5test3v()
     55 // CHECK:       [[EXNOBJ:%.*]] = call i8* @__cxa_allocate_exception(i64 8)
     56 // CHECK-NEXT:  [[EXN:%.*]] = bitcast i8* [[EXNOBJ]] to [[D:%[^*]+]]**
     57 // CHECK-NEXT:  store [[D]]* null, [[D]]** [[EXN]]
     58 // CHECK-NEXT:  call void @__cxa_throw(i8* [[EXNOBJ]], i8* bitcast ({ i8*, i8*, i32, i8* }* @_ZTIPV7test3_D to i8*), i8* null) [[NR]]
     59 // CHECK-NEXT:  unreachable
     60 
     61 
     62 void test4() {
     63   throw;
     64 }
     65 
     66 // CHECK-LABEL:     define void @_Z5test4v()
     67 // CHECK:        call void @__cxa_rethrow() [[NR]]
     68 // CHECK-NEXT:   unreachable
     69 
     70 
     71 // rdar://problem/7696549
     72 namespace test5 {
     73   struct A {
     74     A();
     75     A(const A&);
     76     ~A();
     77   };
     78 
     79   void test() {
     80     try { throw A(); } catch (A &x) {}
     81   }
     82 // CHECK-LABEL:      define void @_ZN5test54testEv()
     83 // CHECK:      [[EXNOBJ:%.*]] = call i8* @__cxa_allocate_exception(i64 1)
     84 // CHECK:      [[EXNCAST:%.*]] = bitcast i8* [[EXNOBJ]] to [[A:%[^*]*]]*
     85 // CHECK-NEXT: invoke void @_ZN5test51AC1Ev([[A]]* [[EXNCAST]])
     86 // CHECK:      invoke void @__cxa_throw(i8* [[EXNOBJ]], i8* bitcast ({{.*}}* @_ZTIN5test51AE to i8*), i8* bitcast (void ([[A]]*)* @_ZN5test51AD1Ev to i8*)) [[NR]]
     87 // CHECK-NEXT:   to label {{%.*}} unwind label %[[HANDLER:[^ ]*]]
     88 //      :    [[HANDLER]]:  (can't check this in Release-Asserts builds)
     89 // CHECK:      {{%.*}} = call i32 @llvm.eh.typeid.for(i8* bitcast ({{.*}}* @_ZTIN5test51AE to i8*))
     90 }
     91 
     92 namespace test6 {
     93   template <class T> struct allocator {
     94     ~allocator() throw() { }
     95   };
     96 
     97   void foo() {
     98     allocator<int> a;
     99   }
    100 }
    101 
    102 // PR7127
    103 namespace test7 {
    104 // CHECK-LABEL:      define i32 @_ZN5test73fooEv()
    105   int foo() {
    106 // CHECK:      [[CAUGHTEXNVAR:%.*]] = alloca i8*
    107 // CHECK-NEXT: [[SELECTORVAR:%.*]] = alloca i32
    108 // CHECK-NEXT: [[INTCATCHVAR:%.*]] = alloca i32
    109     try {
    110       try {
    111 // CHECK-NEXT: [[EXNALLOC:%.*]] = call i8* @__cxa_allocate_exception
    112 // CHECK-NEXT: bitcast i8* [[EXNALLOC]] to i32*
    113 // CHECK-NEXT: store i32 1, i32*
    114 // CHECK-NEXT: invoke void @__cxa_throw(i8* [[EXNALLOC]], i8* bitcast (i8** @_ZTIi to i8*), i8* null
    115         throw 1;
    116       }
    117 
    118 // CHECK:      [[CAUGHTVAL:%.*]] = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*)
    119 // CHECK-NEXT:   catch i8* bitcast (i8** @_ZTIi to i8*)
    120 // CHECK-NEXT:   catch i8* null
    121 // CHECK-NEXT: [[CAUGHTEXN:%.*]] = extractvalue { i8*, i32 } [[CAUGHTVAL]], 0
    122 // CHECK-NEXT: store i8* [[CAUGHTEXN]], i8** [[CAUGHTEXNVAR]]
    123 // CHECK-NEXT: [[SELECTOR:%.*]] = extractvalue { i8*, i32 } [[CAUGHTVAL]], 1
    124 // CHECK-NEXT: store i32 [[SELECTOR]], i32* [[SELECTORVAR]]
    125 // CHECK-NEXT: br label
    126 // CHECK:      [[SELECTOR:%.*]] = load i32* [[SELECTORVAR]]
    127 // CHECK-NEXT: [[T0:%.*]] = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
    128 // CHECK-NEXT: icmp eq i32 [[SELECTOR]], [[T0]]
    129 // CHECK-NEXT: br i1
    130 // CHECK:      [[T0:%.*]] = load i8** [[CAUGHTEXNVAR]]
    131 // CHECK-NEXT: [[T1:%.*]] = call i8* @__cxa_begin_catch(i8* [[T0]])
    132 // CHECK-NEXT: [[T2:%.*]] = bitcast i8* [[T1]] to i32*
    133 // CHECK-NEXT: [[T3:%.*]] = load i32* [[T2]]
    134 // CHECK-NEXT: store i32 [[T3]], i32* {{%.*}}, align 4
    135 // CHECK-NEXT: invoke void @__cxa_rethrow
    136       catch (int) {
    137         throw;
    138       }
    139     }
    140 // CHECK:      [[CAUGHTVAL:%.*]] = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*)
    141 // CHECK-NEXT:   catch i8* null
    142 // CHECK-NEXT: [[CAUGHTEXN:%.*]] = extractvalue { i8*, i32 } [[CAUGHTVAL]], 0
    143 // CHECK-NEXT: store i8* [[CAUGHTEXN]], i8** [[CAUGHTEXNVAR]]
    144 // CHECK-NEXT: [[SELECTOR:%.*]] = extractvalue { i8*, i32 } [[CAUGHTVAL]], 1
    145 // CHECK-NEXT: store i32 [[SELECTOR]], i32* [[SELECTORVAR]]
    146 // CHECK-NEXT: call void @__cxa_end_catch()
    147 // CHECK-NEXT: br label
    148 // CHECK:      load i8** [[CAUGHTEXNVAR]]
    149 // CHECK-NEXT: call i8* @__cxa_begin_catch
    150 // CHECK-NEXT: call void @__cxa_end_catch
    151     catch (...) {
    152     }
    153 // CHECK:      ret i32 0
    154     return 0;
    155   }
    156 }
    157 
    158 // Ordering of destructors in a catch handler.
    159 namespace test8 {
    160   struct A { A(const A&); ~A(); };
    161   void bar();
    162 
    163   // CHECK-LABEL: define void @_ZN5test83fooEv()
    164   void foo() {
    165     try {
    166       // CHECK:      invoke void @_ZN5test83barEv()
    167       bar();
    168     } catch (A a) {
    169       // CHECK:      call i8* @__cxa_get_exception_ptr
    170       // CHECK-NEXT: bitcast
    171       // CHECK-NEXT: invoke void @_ZN5test81AC1ERKS0_(
    172       // CHECK:      call i8* @__cxa_begin_catch
    173       // CHECK-NEXT: call void @_ZN5test81AD1Ev(
    174       // CHECK:      call void @__cxa_end_catch()
    175       // CHECK:      ret void
    176     }
    177   }
    178 }
    179 
    180 // Constructor function-try-block must rethrow on fallthrough.
    181 // rdar://problem/7696603
    182 namespace test9 {
    183   void opaque();
    184 
    185   struct A { A(); };
    186 
    187 
    188   // CHECK-LABEL: define void @_ZN5test91AC2Ev(%"struct.test9::A"* %this) unnamed_addr
    189   A::A() try {
    190   // CHECK:      invoke void @_ZN5test96opaqueEv()
    191     opaque();
    192   } catch (int x) {
    193   // CHECK:      landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*)
    194   // CHECK-NEXT:   catch i8* bitcast (i8** @_ZTIi to i8*)
    195 
    196   // CHECK:      call i8* @__cxa_begin_catch
    197   // CHECK:      invoke void @_ZN5test96opaqueEv()
    198   // CHECK:      invoke void @__cxa_rethrow()
    199 
    200   // CHECK-LABEL:      define void @_ZN5test91AC1Ev(%"struct.test9::A"* %this) unnamed_addr
    201   // CHECK:      call void @_ZN5test91AC2Ev
    202   // CHECK-NEXT: ret void
    203     opaque();
    204   }
    205 }
    206 
    207 // __cxa_end_catch can throw for some kinds of caught exceptions.
    208 namespace test10 {
    209   void opaque();
    210 
    211   struct A { ~A(); };
    212   struct B { int x; };
    213 
    214   // CHECK-LABEL: define void @_ZN6test103fooEv()
    215   void foo() {
    216     A a; // force a cleanup context
    217 
    218     try {
    219     // CHECK:      invoke void @_ZN6test106opaqueEv()
    220       opaque();
    221     } catch (int i) {
    222     // CHECK:      call i8* @__cxa_begin_catch
    223     // CHECK-NEXT: bitcast
    224     // CHECK-NEXT: load i32*
    225     // CHECK-NEXT: store i32
    226     // CHECK-NEXT: call void @__cxa_end_catch() [[NUW:#[0-9]+]]
    227     } catch (B a) {
    228     // CHECK:      call i8* @__cxa_begin_catch
    229     // CHECK-NEXT: bitcast
    230     // CHECK-NEXT: bitcast
    231     // CHECK-NEXT: bitcast
    232     // CHECK-NEXT: call void @llvm.memcpy
    233     // CHECK-NEXT: invoke void @__cxa_end_catch()
    234     } catch (...) {
    235     // CHECK:      call i8* @__cxa_begin_catch
    236     // CHECK-NEXT: invoke void @__cxa_end_catch()
    237     }
    238 
    239     // CHECK: call void @_ZN6test101AD1Ev(
    240   }
    241 }
    242 
    243 // __cxa_begin_catch returns pointers by value, even when catching by reference
    244 // <rdar://problem/8212123>
    245 namespace test11 {
    246   void opaque();
    247 
    248   // CHECK-LABEL: define void @_ZN6test113fooEv()
    249   void foo() {
    250     try {
    251       // CHECK:      invoke void @_ZN6test116opaqueEv()
    252       opaque();
    253     } catch (int**&p) {
    254       // CHECK:      [[EXN:%.*]] = load i8**
    255       // CHECK-NEXT: call i8* @__cxa_begin_catch(i8* [[EXN]]) [[NUW]]
    256       // CHECK-NEXT: [[ADJ1:%.*]] = getelementptr i8* [[EXN]], i32 32
    257       // CHECK-NEXT: [[ADJ2:%.*]] = bitcast i8* [[ADJ1]] to i32***
    258       // CHECK-NEXT: store i32*** [[ADJ2]], i32**** [[P:%.*]]
    259       // CHECK-NEXT: call void @__cxa_end_catch() [[NUW]]
    260     }
    261   }
    262 
    263   struct A {};
    264 
    265   // CHECK-LABEL: define void @_ZN6test113barEv()
    266   void bar() {
    267     try {
    268       // CHECK:      [[EXNSLOT:%.*]] = alloca i8*
    269       // CHECK-NEXT: [[SELECTORSLOT:%.*]] = alloca i32
    270       // CHECK-NEXT: [[P:%.*]] = alloca [[A:%.*]]**,
    271       // CHECK-NEXT: [[TMP:%.*]] = alloca [[A]]*
    272       // CHECK-NEXT: invoke void @_ZN6test116opaqueEv()
    273       opaque();
    274     } catch (A*&p) {
    275       // CHECK:      [[EXN:%.*]] = load i8** [[EXNSLOT]]
    276       // CHECK-NEXT: [[ADJ1:%.*]] = call i8* @__cxa_begin_catch(i8* [[EXN]]) [[NUW]]
    277       // CHECK-NEXT: [[ADJ2:%.*]] = bitcast i8* [[ADJ1]] to [[A]]*
    278       // CHECK-NEXT: store [[A]]* [[ADJ2]], [[A]]** [[TMP]]
    279       // CHECK-NEXT: store [[A]]** [[TMP]], [[A]]*** [[P]]
    280       // CHECK-NEXT: call void @__cxa_end_catch() [[NUW]]
    281     }
    282   }
    283 }
    284 
    285 // PR7686
    286 namespace test12 {
    287   struct A { ~A() noexcept(false); };
    288   bool opaque(const A&);
    289 
    290   // CHECK-LABEL: define void @_ZN6test124testEv()
    291   void test() {
    292     // CHECK: [[X:%.*]] = alloca [[A:%.*]],
    293     // CHECK: [[EHCLEANUPDEST:%.*]] = alloca i32
    294     // CHECK: [[Y:%.*]] = alloca [[A]]
    295     // CHECK: [[Z:%.*]] = alloca [[A]]
    296     // CHECK: [[CLEANUPDEST:%.*]] = alloca i32
    297 
    298     A x;
    299     // CHECK: invoke zeroext i1 @_ZN6test126opaqueERKNS_1AE(
    300     if (opaque(x)) {
    301       A y;
    302       A z;
    303 
    304       // CHECK: invoke void @_ZN6test121AD1Ev([[A]]* [[Z]])
    305       // CHECK: invoke void @_ZN6test121AD1Ev([[A]]* [[Y]])
    306       // CHECK-NOT: switch
    307       goto success;
    308     }
    309 
    310   success:
    311     bool _ = true;
    312 
    313     // CHECK: call void @_ZN6test121AD1Ev([[A]]* [[X]])
    314     // CHECK-NEXT: ret void
    315   }
    316 }
    317 
    318 // Reduced from some TableGen code that was causing a self-host crash.
    319 namespace test13 {
    320   struct A { ~A(); };
    321 
    322   void test0(int x) {
    323     try {
    324       switch (x) {
    325       case 0:
    326         break;
    327       case 1:{
    328         A a;
    329         break;
    330       }
    331       default:
    332         return;
    333       }
    334       return;
    335     } catch (int x) {
    336     }
    337     return;
    338   }
    339 
    340   void test1(int x) {
    341     A y;
    342     try {
    343       switch (x) {
    344       default: break;
    345       }
    346     } catch (int x) {}
    347   }
    348 }
    349 
    350 // rdar://problem/8231514
    351 namespace test14 {
    352   struct A { ~A(); };
    353   struct B { ~B(); };
    354 
    355   B b();
    356   void opaque();
    357 
    358   void foo() {
    359     A a;
    360     try {
    361       B str = b();
    362       opaque();
    363     } catch (int x) {
    364     }
    365   }
    366 }
    367 
    368 // rdar://problem/8231514
    369 // JumpDests shouldn't get confused by scopes that aren't normal cleanups.
    370 namespace test15 {
    371   struct A { ~A(); };
    372 
    373   bool opaque(int);
    374 
    375   // CHECK-LABEL: define void @_ZN6test153fooEv()
    376   void foo() {
    377     A a;
    378 
    379     try {
    380       // CHECK:      [[X:%.*]] = alloca i32
    381       // CHECK:      store i32 10, i32* [[X]]
    382       // CHECK-NEXT: br label
    383       //   -> while.cond
    384       int x = 10;
    385 
    386       while (true) {
    387         // CHECK:      load i32* [[X]]
    388         // CHECK-NEXT: [[COND:%.*]] = invoke zeroext i1 @_ZN6test156opaqueEi
    389         // CHECK:      br i1 [[COND]]
    390         if (opaque(x))
    391         // CHECK:      br label
    392           break;
    393 
    394         // CHECK:      br label
    395       }
    396       // CHECK:      br label
    397     } catch (int x) { }
    398 
    399     // CHECK: call void @_ZN6test151AD1Ev
    400   }
    401 }
    402 
    403 namespace test16 {
    404   struct A { A(); ~A() noexcept(false); };
    405   struct B { int x; B(const A &); ~B() noexcept(false); };
    406   void foo();
    407   bool cond();
    408 
    409   // CHECK-LABEL: define void @_ZN6test163barEv()
    410   void bar() {
    411     // CHECK:      [[EXN_SAVE:%.*]] = alloca i8*
    412     // CHECK-NEXT: [[EXN_ACTIVE:%.*]] = alloca i1
    413     // CHECK-NEXT: [[TEMP:%.*]] = alloca [[A:%.*]],
    414     // CHECK-NEXT: [[EXNSLOT:%.*]] = alloca i8*
    415     // CHECK-NEXT: [[SELECTORSLOT:%.*]] = alloca i32
    416     // CHECK-NEXT: [[TEMP_ACTIVE:%.*]] = alloca i1
    417 
    418     cond() ? throw B(A()) : foo();
    419 
    420     // CHECK-NEXT: [[COND:%.*]] = call zeroext i1 @_ZN6test164condEv()
    421     // CHECK-NEXT: store i1 false, i1* [[EXN_ACTIVE]]
    422     // CHECK-NEXT: store i1 false, i1* [[TEMP_ACTIVE]]
    423     // CHECK-NEXT: br i1 [[COND]],
    424 
    425     // CHECK:      [[EXN:%.*]] = call i8* @__cxa_allocate_exception(i64 4)
    426     // CHECK-NEXT: store i8* [[EXN]], i8** [[EXN_SAVE]]
    427     // CHECK-NEXT: store i1 true, i1* [[EXN_ACTIVE]]
    428     // CHECK-NEXT: [[T0:%.*]] = bitcast i8* [[EXN]] to [[B:%.*]]*
    429     // CHECK-NEXT: invoke void @_ZN6test161AC1Ev([[A]]* [[TEMP]])
    430     // CHECK:      store i1 true, i1* [[TEMP_ACTIVE]]
    431     // CHECK-NEXT: invoke void @_ZN6test161BC1ERKNS_1AE([[B]]* [[T0]], [[A]]* nonnull [[TEMP]])
    432     // CHECK:      store i1 false, i1* [[EXN_ACTIVE]]
    433     // CHECK-NEXT: invoke void @__cxa_throw(i8* [[EXN]],
    434 
    435     // CHECK:      invoke void @_ZN6test163fooEv()
    436     // CHECK:      br label
    437 
    438     // CHECK:      invoke void @_ZN6test161AD1Ev([[A]]* [[TEMP]])
    439     // CHECK:      ret void
    440 
    441     // CHECK:      [[T0:%.*]] = load i1* [[EXN_ACTIVE]]
    442     // CHECK-NEXT: br i1 [[T0]]
    443     // CHECK:      [[T1:%.*]] = load i8** [[EXN_SAVE]]
    444     // CHECK-NEXT: call void @__cxa_free_exception(i8* [[T1]])
    445     // CHECK-NEXT: br label
    446   }
    447 }
    448 
    449 // CHECK: attributes [[NUW]] = { nounwind }
    450 // CHECK: attributes [[NR]] = { noreturn }
    451