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