1 // RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -emit-llvm -o - -fcxx-exceptions -fexceptions | FileCheck %s 2 3 // Test IR generation for partial destruction of aggregates. 4 5 void opaque(); 6 7 // Initializer lists. 8 namespace test0 { 9 struct A { A(int); A(); ~A(); void *v; }; 10 void test() { 11 A as[10] = { 5, 7 }; 12 opaque(); 13 } 14 // CHECK: define void @_ZN5test04testEv() 15 // CHECK: [[AS:%.*]] = alloca [10 x [[A:%.*]]], align 16 // CHECK-NEXT: [[ENDVAR:%.*]] = alloca [[A]]* 17 // CHECK-NEXT: [[EXN:%.*]] = alloca i8* 18 // CHECK-NEXT: [[SEL:%.*]] = alloca i32 19 // CHECK-NEXT: [[CLEANUP:%.*]] = alloca i32 20 21 // Initialize. 22 // CHECK-NEXT: [[E_BEGIN:%.*]] = getelementptr inbounds [10 x [[A]]]* [[AS]], i64 0, i64 0 23 // CHECK-NEXT: store [[A]]* [[E_BEGIN]], [[A]]** [[ENDVAR]] 24 // CHECK-NEXT: invoke void @_ZN5test01AC1Ei([[A]]* [[E_BEGIN]], i32 5) 25 // CHECK: [[E1:%.*]] = getelementptr inbounds [[A]]* [[E_BEGIN]], i64 1 26 // CHECK-NEXT: store [[A]]* [[E1]], [[A]]** [[ENDVAR]] 27 // CHECK-NEXT: invoke void @_ZN5test01AC1Ei([[A]]* [[E1]], i32 7) 28 // CHECK: [[E2:%.*]] = getelementptr inbounds [[A]]* [[E1]], i64 1 29 // CHECK-NEXT: store [[A]]* [[E2]], [[A]]** [[ENDVAR]] 30 // CHECK-NEXT: [[E_END:%.*]] = getelementptr inbounds [[A]]* [[E_BEGIN]], i64 10 31 // CHECK-NEXT: br label 32 // CHECK: [[E_CUR:%.*]] = phi [[A]]* [ [[E2]], {{%.*}} ], [ [[E_NEXT:%.*]], {{%.*}} ] 33 // CHECK-NEXT: invoke void @_ZN5test01AC1Ev([[A]]* [[E_CUR]]) 34 // CHECK: [[E_NEXT]] = getelementptr inbounds [[A]]* [[E_CUR]], i64 1 35 // CHECK-NEXT: store [[A]]* [[E_NEXT]], [[A]]** [[ENDVAR]] 36 // CHECK-NEXT: [[T0:%.*]] = icmp eq [[A]]* [[E_NEXT]], [[E_END]] 37 // CHECK-NEXT: br i1 [[T0]], 38 39 // Run. 40 // CHECK: invoke void @_Z6opaquev() 41 42 // Normal destroy. 43 // CHECK: [[ED_BEGIN:%.*]] = getelementptr inbounds [10 x [[A]]]* [[AS]], i32 0, i32 0 44 // CHECK-NEXT: [[ED_END:%.*]] = getelementptr inbounds [[A]]* [[ED_BEGIN]], i64 10 45 // CHECK-NEXT: br label 46 // CHECK: [[ED_AFTER:%.*]] = phi [[A]]* [ [[ED_END]], {{%.*}} ], [ [[ED_CUR:%.*]], {{%.*}} ] 47 // CHECK-NEXT: [[ED_CUR]] = getelementptr inbounds [[A]]* [[ED_AFTER]], i64 -1 48 // CHECK-NEXT: invoke void @_ZN5test01AD1Ev([[A]]* [[ED_CUR]]) 49 // CHECK: [[T0:%.*]] = icmp eq [[A]]* [[ED_CUR]], [[ED_BEGIN]] 50 // CHECK-NEXT: br i1 [[T0]], 51 // CHECK: ret void 52 53 // Partial destroy for initialization. 54 // CHECK: llvm.eh.selector({{.*}}, i32 0) 55 // CHECK: [[PARTIAL_END:%.*]] = load [[A]]** [[ENDVAR]] 56 // CHECK-NEXT: [[T0:%.*]] = icmp eq [[A]]* [[E_BEGIN]], [[PARTIAL_END]] 57 // CHECK-NEXT: br i1 [[T0]], 58 // CHECK: [[E_AFTER:%.*]] = phi [[A]]* [ [[PARTIAL_END]], {{%.*}} ], [ [[E_CUR:%.*]], {{%.*}} ] 59 // CHECK-NEXT: [[E_CUR]] = getelementptr inbounds [[A]]* [[E_AFTER]], i64 -1 60 // CHECK-NEXT: invoke void @_ZN5test01AD1Ev([[A]]* [[E_CUR]]) 61 // CHECK: [[T0:%.*]] = icmp eq [[A]]* [[E_CUR]], [[E_BEGIN]] 62 // CHECK-NEXT: br i1 [[T0]], 63 64 // Primary EH destructor. 65 // CHECK: llvm.eh.selector({{.*}}, i32 0) 66 // CHECK: [[E0:%.*]] = getelementptr inbounds [10 x [[A]]]* [[AS]], i32 0, i32 0 67 // CHECK-NEXT: [[E_END:%.*]] = getelementptr inbounds [[A]]* [[E0]], i64 10 68 // CHECK-NEXT: br label 69 70 // Partial destructor for primary normal destructor. 71 // FIXME: There's some really bad block ordering here which causes 72 // the partial destroy for the primary normal destructor to fall 73 // within the primary EH destructor. 74 // CHECK: llvm.eh.selector({{.*}}, i32 0) 75 // CHECK: [[T0:%.*]] = icmp eq [[A]]* [[ED_BEGIN]], [[ED_CUR]] 76 // CHECK-NEXT: br i1 [[T0]] 77 // CHECK: [[EDD_AFTER:%.*]] = phi [[A]]* [ [[ED_CUR]], {{%.*}} ], [ [[EDD_CUR:%.*]], {{%.*}} ] 78 // CHECK-NEXT: [[EDD_CUR]] = getelementptr inbounds [[A]]* [[EDD_AFTER]], i64 -1 79 // CHECK-NEXT: invoke void @_ZN5test01AD1Ev([[A]]* [[EDD_CUR]]) 80 // CHECK: [[T0:%.*]] = icmp eq [[A]]* [[EDD_CUR]], [[ED_BEGIN]] 81 // CHECK-NEXT: br i1 [[T0]] 82 83 // Back to the primary EH destructor. 84 // CHECK: [[E_AFTER:%.*]] = phi [[A]]* [ [[E_END]], {{%.*}} ], [ [[E_CUR:%.*]], {{%.*}} ] 85 // CHECK-NEXT: [[E_CUR]] = getelementptr inbounds [[A]]* [[E_AFTER]], i64 -1 86 // CHECK-NEXT: invoke void @_ZN5test01AD1Ev([[A]]* [[E_CUR]]) 87 // CHECK: [[T0:%.*]] = icmp eq [[A]]* [[E_CUR]], [[E0]] 88 // CHECK-NEXT: br i1 [[T0]], 89 90 } 91 92 namespace test1 { 93 struct A { A(); A(int); ~A(); }; 94 struct B { A x, y, z; int w; }; 95 96 void test() { 97 B v = { 5, 6, 7, 8 }; 98 } 99 // CHECK: define void @_ZN5test14testEv() 100 // CHECK: [[V:%.*]] = alloca [[B:%.*]], align 4 101 // CHECK-NEXT: alloca i8* 102 // CHECK-NEXT: alloca i32 103 // CHECK-NEXT: alloca i32 104 // CHECK-NEXT: [[X:%.*]] = getelementptr inbounds [[B]]* [[V]], i32 0, i32 0 105 // CHECK-NEXT: call void @_ZN5test11AC1Ei([[A:%.*]]* [[X]], i32 5) 106 // CHECK-NEXT: [[Y:%.*]] = getelementptr inbounds [[B]]* [[V]], i32 0, i32 1 107 // CHECK-NEXT: invoke void @_ZN5test11AC1Ei([[A]]* [[Y]], i32 6) 108 // CHECK: [[Z:%.*]] = getelementptr inbounds [[B]]* [[V]], i32 0, i32 2 109 // CHECK-NEXT: invoke void @_ZN5test11AC1Ei([[A]]* [[Z]], i32 7) 110 // CHECK: [[W:%.*]] = getelementptr inbounds [[B]]* [[V]], i32 0, i32 3 111 // CHECK-NEXT: store i32 8, i32* [[W]], align 4 112 // CHECK-NEXT: call void @_ZN5test11BD1Ev([[B]]* [[V]]) 113 // CHECK-NEXT: ret void 114 115 // FIXME: again, the block ordering is pretty bad here 116 // CHECK: eh.selector({{.*}}, i32 0) 117 // CHECK: eh.selector({{.*}}, i32 0) 118 // CHECK: invoke void @_ZN5test11AD1Ev([[A]]* [[Y]]) 119 // CHECK: invoke void @_ZN5test11AD1Ev([[A]]* [[X]]) 120 } 121 122 namespace test2 { 123 struct A { A(); ~A(); }; 124 125 void test() { 126 A v[4][7]; 127 128 // CHECK: define void @_ZN5test24testEv() 129 // CHECK: [[V:%.*]] = alloca [4 x [7 x [[A:%.*]]]], align 1 130 // CHECK-NEXT: alloca i8* 131 // CHECK-NEXT: alloca i32 132 // CHECK-NEXT: alloca i32 133 134 // Main initialization loop. 135 // CHECK-NEXT: [[BEGIN:%.*]] = getelementptr inbounds [4 x [7 x [[A]]]]* [[V]], i32 0, i32 0, i32 0 136 // CHECK-NEXT: [[END:%.*]] = getelementptr inbounds [[A]]* [[BEGIN]], i64 28 137 // CHECK-NEXT: br label 138 // CHECK: [[CUR:%.*]] = phi [[A]]* [ [[BEGIN]], {{%.*}} ], [ [[NEXT:%.*]], {{%.*}} ] 139 // CHECK-NEXT: invoke void @_ZN5test21AC1Ev([[A]]* [[CUR]]) 140 // CHECK: [[NEXT:%.*]] = getelementptr inbounds [[A]]* [[CUR]], i64 1 141 // CHECK-NEXT: [[DONE:%.*]] = icmp eq [[A]]* [[NEXT]], [[END]] 142 // CHECK-NEXT: br i1 [[DONE]], 143 144 // Partial destruction landing pad. 145 // CHECK: llvm.eh.exception() 146 // CHECK: [[EMPTY:%.*]] = icmp eq [[A]]* [[BEGIN]], [[CUR]] 147 // CHECK-NEXT: br i1 [[EMPTY]], 148 // CHECK: [[PAST:%.*]] = phi [[A]]* [ [[CUR]], {{%.*}} ], [ [[DEL:%.*]], {{%.*}} ] 149 // CHECK-NEXT: [[DEL]] = getelementptr inbounds [[A]]* [[PAST]], i64 -1 150 // CHECK-NEXT: invoke void @_ZN5test21AD1Ev([[A]]* [[DEL]]) 151 // CHECK: [[T0:%.*]] = icmp eq [[A]]* [[DEL]], [[BEGIN]] 152 // CHECK-NEXT: br i1 [[T0]], 153 } 154 155 } 156 157 // PR10351 158 namespace test3 { 159 struct A { A(); ~A(); void *p; }; 160 struct B { 161 B() {} 162 A a; 163 }; 164 165 B *test() { 166 return new B[10]; 167 // invoke void @_ZN5test31BD1Ev( 168 } 169 } 170