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-LABEL:    define void @_ZN5test04testEv()
15   // CHECK-SAME: personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*)
16   // CHECK:      [[AS:%.*]] = alloca [10 x [[A:%.*]]], align
17   // CHECK-NEXT: [[ENDVAR:%.*]] = alloca [[A]]*
18   // CHECK-NEXT: [[EXN:%.*]] = alloca i8*
19   // CHECK-NEXT: [[SEL:%.*]] = alloca i32
20 
21   // Initialize.
22   // CHECK-NEXT: [[E_BEGIN:%.*]] = getelementptr inbounds [10 x [[A]]], [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]], [[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]], [[A]]* [[E1]], i64 1
29   // CHECK-NEXT: store [[A]]* [[E2]], [[A]]** [[ENDVAR]]
30   // CHECK-NEXT: [[E_END:%.*]] = getelementptr inbounds [[A]], [[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]], [[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]]], [10 x [[A]]]* [[AS]], i32 0, i32 0
44   // CHECK-NEXT: [[ED_END:%.*]] = getelementptr inbounds [[A]], [[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]], [[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:      landingpad { i8*, i32 }
55   // CHECK-NEXT:   cleanup
56   // CHECK:      [[PARTIAL_END:%.*]] = load [[A]]*, [[A]]** [[ENDVAR]]
57   // CHECK-NEXT: [[T0:%.*]] = icmp eq [[A]]* [[E_BEGIN]], [[PARTIAL_END]]
58   // CHECK-NEXT: br i1 [[T0]],
59   // CHECK:      [[E_AFTER:%.*]] = phi [[A]]* [ [[PARTIAL_END]], {{%.*}} ], [ [[E_CUR:%.*]], {{%.*}} ]
60   // CHECK-NEXT: [[E_CUR]] = getelementptr inbounds [[A]], [[A]]* [[E_AFTER]], i64 -1
61   // CHECK-NEXT: invoke void @_ZN5test01AD1Ev([[A]]* [[E_CUR]])
62   // CHECK:      [[T0:%.*]] = icmp eq [[A]]* [[E_CUR]], [[E_BEGIN]]
63   // CHECK-NEXT: br i1 [[T0]],
64 
65   // Primary EH destructor.
66   // CHECK:      landingpad { i8*, i32 }
67   // CHECK-NEXT:   cleanup
68   // CHECK:      [[E0:%.*]] = getelementptr inbounds [10 x [[A]]], [10 x [[A]]]* [[AS]], i32 0, i32 0
69   // CHECK-NEXT: [[E_END:%.*]] = getelementptr inbounds [[A]], [[A]]* [[E0]], i64 10
70   // CHECK-NEXT: br label
71 
72   // Partial destructor for primary normal destructor.
73   // FIXME: There's some really bad block ordering here which causes
74   // the partial destroy for the primary normal destructor to fall
75   // within the primary EH destructor.
76   // CHECK:      landingpad { i8*, i32 }
77   // CHECK-NEXT:   cleanup
78   // CHECK:      [[T0:%.*]] = icmp eq [[A]]* [[ED_BEGIN]], [[ED_CUR]]
79   // CHECK-NEXT: br i1 [[T0]]
80   // CHECK:      [[EDD_AFTER:%.*]] = phi [[A]]* [ [[ED_CUR]], {{%.*}} ], [ [[EDD_CUR:%.*]], {{%.*}} ]
81   // CHECK-NEXT: [[EDD_CUR]] = getelementptr inbounds [[A]], [[A]]* [[EDD_AFTER]], i64 -1
82   // CHECK-NEXT: invoke void @_ZN5test01AD1Ev([[A]]* [[EDD_CUR]])
83   // CHECK:      [[T0:%.*]] = icmp eq [[A]]* [[EDD_CUR]], [[ED_BEGIN]]
84   // CHECK-NEXT: br i1 [[T0]]
85 
86   // Back to the primary EH destructor.
87   // CHECK:      [[E_AFTER:%.*]] = phi [[A]]* [ [[E_END]], {{%.*}} ], [ [[E_CUR:%.*]], {{%.*}} ]
88   // CHECK-NEXT: [[E_CUR]] = getelementptr inbounds [[A]], [[A]]* [[E_AFTER]], i64 -1
89   // CHECK-NEXT: invoke void @_ZN5test01AD1Ev([[A]]* [[E_CUR]])
90   // CHECK:      [[T0:%.*]] = icmp eq [[A]]* [[E_CUR]], [[E0]]
91   // CHECK-NEXT: br i1 [[T0]],
92 
93 }
94 
95 namespace test1 {
96   struct A { A(); A(int); ~A(); };
97   struct B { A x, y, z; int w; };
98 
99   void test() {
100     B v = { 5, 6, 7, 8 };
101   }
102   // CHECK-LABEL:    define void @_ZN5test14testEv()
103   // CHECK-SAME: personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*)
104   // CHECK:      [[V:%.*]] = alloca [[B:%.*]], align 4
105   // CHECK-NEXT: alloca i8*
106   // CHECK-NEXT: alloca i32
107   // CHECK-NEXT: [[X:%.*]] = getelementptr inbounds [[B]], [[B]]* [[V]], i32 0, i32 0
108   // CHECK-NEXT: call void @_ZN5test11AC1Ei([[A:%.*]]* [[X]], i32 5)
109   // CHECK-NEXT: [[Y:%.*]] = getelementptr inbounds [[B]], [[B]]* [[V]], i32 0, i32 1
110   // CHECK-NEXT: invoke void @_ZN5test11AC1Ei([[A]]* [[Y]], i32 6)
111   // CHECK:      [[Z:%.*]] = getelementptr inbounds [[B]], [[B]]* [[V]], i32 0, i32 2
112   // CHECK-NEXT: invoke void @_ZN5test11AC1Ei([[A]]* [[Z]], i32 7)
113   // CHECK:      [[W:%.*]] = getelementptr inbounds [[B]], [[B]]* [[V]], i32 0, i32 3
114   // CHECK-NEXT: store i32 8, i32* [[W]], align 4
115   // CHECK-NEXT: call void @_ZN5test11BD1Ev([[B]]* [[V]])
116   // CHECK-NEXT: ret void
117 
118   // FIXME: again, the block ordering is pretty bad here
119   // CHECK:      landingpad { i8*, i32 }
120   // CHECK-NEXT:   cleanup
121   // CHECK:      landingpad { i8*, i32 }
122   // CHECK-NEXT:   cleanup
123   // CHECK:      invoke void @_ZN5test11AD1Ev([[A]]* [[Y]])
124   // CHECK:      invoke void @_ZN5test11AD1Ev([[A]]* [[X]])
125 }
126 
127 namespace test2 {
128   struct A { A(); ~A(); };
129 
130   void test() {
131     A v[4][7];
132 
133     // CHECK-LABEL:    define void @_ZN5test24testEv()
134     // CHECK-SAME: personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*)
135     // CHECK:      [[V:%.*]] = alloca [4 x [7 x [[A:%.*]]]], align 1
136     // CHECK-NEXT: alloca i8*
137     // CHECK-NEXT: alloca i32
138 
139     // Main initialization loop.
140     // CHECK-NEXT: [[BEGIN:%.*]] = getelementptr inbounds [4 x [7 x [[A]]]], [4 x [7 x [[A]]]]* [[V]], i32 0, i32 0, i32 0
141     // CHECK-NEXT: [[END:%.*]] = getelementptr inbounds [[A]], [[A]]* [[BEGIN]], i64 28
142     // CHECK-NEXT: br label
143     // CHECK:      [[CUR:%.*]] = phi [[A]]* [ [[BEGIN]], {{%.*}} ], [ [[NEXT:%.*]], {{%.*}} ]
144     // CHECK-NEXT: invoke void @_ZN5test21AC1Ev([[A]]* [[CUR]])
145     // CHECK:      [[NEXT:%.*]] = getelementptr inbounds [[A]], [[A]]* [[CUR]], i64 1
146     // CHECK-NEXT: [[DONE:%.*]] = icmp eq [[A]]* [[NEXT]], [[END]]
147     // CHECK-NEXT: br i1 [[DONE]],
148 
149     // Partial destruction landing pad.
150     // CHECK:      landingpad { i8*, i32 }
151     // CHECK-NEXT:   cleanup
152     // CHECK:      [[EMPTY:%.*]] = icmp eq [[A]]* [[BEGIN]], [[CUR]]
153     // CHECK-NEXT: br i1 [[EMPTY]],
154     // CHECK:      [[PAST:%.*]] = phi [[A]]* [ [[CUR]], {{%.*}} ], [ [[DEL:%.*]], {{%.*}} ]
155     // CHECK-NEXT: [[DEL]] = getelementptr inbounds [[A]], [[A]]* [[PAST]], i64 -1
156     // CHECK-NEXT: invoke void @_ZN5test21AD1Ev([[A]]* [[DEL]])
157     // CHECK:      [[T0:%.*]] = icmp eq [[A]]* [[DEL]], [[BEGIN]]
158     // CHECK-NEXT: br i1 [[T0]],
159   }
160 
161 }
162 
163 // PR10351
164 namespace test3 {
165   struct A { A(); ~A(); void *p; };
166   struct B {
167     B() {}
168     A a;
169   };
170 
171   B *test() {
172     return new B[10];
173     // invoke void @_ZN5test31BD1Ev(
174   }
175 }
176 
177 namespace test4 {
178   struct A { A(unsigned i); ~A(); };
179   void test() {
180     A v[2][3] = { { A(0), A(1), A(2) }, { A(3), A(4), A(5) } };
181   }
182 }
183 // CHECK-LABEL: define void @_ZN5test44testEv()
184 // CHECK:       [[ARRAY:%.*]] = alloca [2 x [3 x [[A:%.*]]]], align
185 // CHECK:       [[A0:%.*]] = getelementptr inbounds [2 x [3 x [[A]]]], [2 x [3 x [[A]]]]* [[ARRAY]], i64 0, i64 0
186 // CHECK-NEXT:  store [3 x [[A]]]* [[A0]],
187 // CHECK-NEXT:  [[A00:%.*]] = getelementptr inbounds [3 x [[A]]], [3 x [[A]]]* [[A0]], i64 0, i64 0
188 // CHECK-NEXT:  store [[A]]* [[A00]],
189 // CHECK-NEXT:  invoke void @_ZN5test41AC1Ej([[A]]* [[A00]], i32 0)
190 // CHECK:       [[A01:%.*]] = getelementptr inbounds [[A]], [[A]]* [[A00]], i64 1
191 // CHECK-NEXT:  store [[A]]* [[A01]],
192 // CHECK-NEXT:  invoke void @_ZN5test41AC1Ej([[A]]* [[A01]], i32 1)
193 // CHECK:       [[A02:%.*]] = getelementptr inbounds [[A]], [[A]]* [[A01]], i64 1
194 // CHECK-NEXT:  store [[A]]* [[A02]],
195 // CHECK-NEXT:  invoke void @_ZN5test41AC1Ej([[A]]* [[A02]], i32 2)
196 // CHECK:       [[A1:%.*]] = getelementptr inbounds [3 x [[A]]], [3 x [[A]]]* [[A0]], i64 1
197 // CHECK-NEXT:  store [3 x [[A]]]* [[A1]],
198 // CHECK-NEXT:  [[A10:%.*]] = getelementptr inbounds [3 x [[A]]], [3 x [[A]]]* [[A1]], i64 0, i64 0
199 // CHECK-NEXT:  store [[A]]* [[A10]],
200 // CHECK-NEXT:  invoke void @_ZN5test41AC1Ej([[A]]* [[A10]], i32 3)
201 // CHECK:       [[A11:%.*]] = getelementptr inbounds [[A]], [[A]]* [[A10]], i64 1
202 // CHECK-NEXT:  store [[A]]* [[A11]],
203 // CHECK-NEXT:  invoke void @_ZN5test41AC1Ej([[A]]* [[A11]], i32 4)
204 // CHECK:       [[A12:%.*]] = getelementptr inbounds [[A]], [[A]]* [[A11]], i64 1
205 // CHECK-NEXT:  store [[A]]* [[A12]],
206 // CHECK-NEXT:  invoke void @_ZN5test41AC1Ej([[A]]* [[A12]], i32 5)
207