Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / CodeGenCXX / partial-destruction.cpp
blob840e43880293b9fa0a7478b115b3ceb9376042c5
1 // RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -emit-llvm -o - -fcxx-exceptions -fexceptions -std=c++03 | FileCheck %s -check-prefixes=CHECK,CHECKv03
2 // RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -emit-llvm -o - -fcxx-exceptions -fexceptions -std=c++11 | FileCheck %s -check-prefixes=CHECK,CHECKv11
4 // Test IR generation for partial destruction of aggregates.
6 void opaque();
8 // Initializer lists.
9 namespace test0 {
10 struct A { A(int); A(); ~A(); void *v; };
11 void test() {
12 A as[10] = { 5, 7 };
13 opaque();
15 // CHECK-LABEL: define{{.*}} void @_ZN5test04testEv()
16 // CHECK-SAME: personality ptr @__gxx_personality_v0
17 // CHECK: [[AS:%.*]] = alloca [10 x [[A:%.*]]], align
18 // CHECK-NEXT: [[ENDVAR:%.*]] = alloca ptr
19 // CHECK-NEXT: [[EXN:%.*]] = alloca ptr
20 // CHECK-NEXT: [[SEL:%.*]] = alloca i32
22 // Initialize.
23 // CHECK-NEXT: [[E_BEGIN:%.*]] = getelementptr inbounds [10 x [[A]]], ptr [[AS]], i64 0, i64 0
24 // CHECK-NEXT: store ptr [[E_BEGIN]], ptr [[ENDVAR]]
25 // CHECK-NEXT: invoke void @_ZN5test01AC1Ei(ptr {{[^,]*}} [[E_BEGIN]], i32 noundef 5)
26 // CHECK: [[E1:%.*]] = getelementptr inbounds [[A]], ptr [[E_BEGIN]], i64 1
27 // CHECK-NEXT: store ptr [[E1]], ptr [[ENDVAR]]
28 // CHECK-NEXT: invoke void @_ZN5test01AC1Ei(ptr {{[^,]*}} [[E1]], i32 noundef 7)
29 // CHECK: [[E2:%.*]] = getelementptr inbounds [[A]], ptr [[E1]], i64 1
30 // CHECK-NEXT: store ptr [[E2]], ptr [[ENDVAR]]
31 // CHECK-NEXT: [[E_END:%.*]] = getelementptr inbounds [[A]], ptr [[E_BEGIN]], i64 10
32 // CHECK-NEXT: br label
33 // CHECK: [[E_CUR:%.*]] = phi ptr [ [[E2]], {{%.*}} ], [ [[E_NEXT:%.*]], {{%.*}} ]
34 // CHECK-NEXT: invoke void @_ZN5test01AC1Ev(ptr {{[^,]*}} [[E_CUR]])
35 // CHECK: [[E_NEXT]] = getelementptr inbounds [[A]], ptr [[E_CUR]], i64 1
36 // CHECK-NEXT: store ptr [[E_NEXT]], ptr [[ENDVAR]]
37 // CHECK-NEXT: [[T0:%.*]] = icmp eq ptr [[E_NEXT]], [[E_END]]
38 // CHECK-NEXT: br i1 [[T0]],
40 // Run.
41 // CHECK: invoke void @_Z6opaquev()
43 // Normal destroy.
44 // CHECK: [[ED_BEGIN:%.*]] = getelementptr inbounds [10 x [[A]]], ptr [[AS]], i32 0, i32 0
45 // CHECK-NEXT: [[ED_END:%.*]] = getelementptr inbounds [[A]], ptr [[ED_BEGIN]], i64 10
46 // CHECK-NEXT: br label
47 // CHECK: [[ED_AFTER:%.*]] = phi ptr [ [[ED_END]], {{%.*}} ], [ [[ED_CUR:%.*]], {{%.*}} ]
48 // CHECK-NEXT: [[ED_CUR]] = getelementptr inbounds [[A]], ptr [[ED_AFTER]], i64 -1
49 // CHECKv03-NEXT: invoke void @_ZN5test01AD1Ev(ptr {{[^,]*}} [[ED_CUR]])
50 // CHECKv11-NEXT: call void @_ZN5test01AD1Ev(ptr {{[^,]*}} [[ED_CUR]])
51 // CHECK: [[T0:%.*]] = icmp eq ptr [[ED_CUR]], [[ED_BEGIN]]
52 // CHECK-NEXT: br i1 [[T0]],
53 // CHECK: ret void
55 // Partial destroy for initialization.
56 // CHECK: landingpad { ptr, i32 }
57 // CHECK-NEXT: cleanup
58 // CHECK: [[PARTIAL_END:%.*]] = load ptr, ptr [[ENDVAR]]
59 // CHECK-NEXT: [[T0:%.*]] = icmp eq ptr [[E_BEGIN]], [[PARTIAL_END]]
60 // CHECK-NEXT: br i1 [[T0]],
61 // CHECK: [[E_AFTER:%.*]] = phi ptr [ [[PARTIAL_END]], {{%.*}} ], [ [[E_CUR:%.*]], {{%.*}} ]
62 // CHECK-NEXT: [[E_CUR]] = getelementptr inbounds [[A]], ptr [[E_AFTER]], i64 -1
63 // CHECKv03-NEXT: invoke void @_ZN5test01AD1Ev(ptr {{[^,]*}} [[E_CUR]])
64 // CHECKv11-NEXT: call void @_ZN5test01AD1Ev(ptr {{[^,]*}} [[E_CUR]])
65 // CHECK: [[T0:%.*]] = icmp eq ptr [[E_CUR]], [[E_BEGIN]]
66 // CHECK-NEXT: br i1 [[T0]],
68 // Primary EH destructor.
69 // CHECK: landingpad { ptr, i32 }
70 // CHECK-NEXT: cleanup
71 // CHECK: [[E0:%.*]] = getelementptr inbounds [10 x [[A]]], ptr [[AS]], i32 0, i32 0
72 // CHECK-NEXT: [[E_END:%.*]] = getelementptr inbounds [[A]], ptr [[E0]], i64 10
73 // CHECK-NEXT: br label
75 // Partial destructor for primary normal destructor.
76 // FIXME: There's some really bad block ordering here which causes
77 // the partial destroy for the primary normal destructor to fall
78 // within the primary EH destructor.
79 // CHECKv03: landingpad { ptr, i32 }
80 // CHECKv03-NEXT: cleanup
81 // CHECKv03: [[T0:%.*]] = icmp eq ptr [[ED_BEGIN]], [[ED_CUR]]
82 // CHECKv03-NEXT: br i1 [[T0]]
83 // CHECKv03: [[EDD_AFTER:%.*]] = phi ptr [ [[ED_CUR]], {{%.*}} ], [ [[EDD_CUR:%.*]], {{%.*}} ]
84 // CHECKv03-NEXT: [[EDD_CUR]] = getelementptr inbounds [[A]], ptr [[EDD_AFTER]], i64 -1
85 // CHECKv03-NEXT: invoke void @_ZN5test01AD1Ev(ptr {{[^,]*}} [[EDD_CUR]])
86 // CHECKv03: [[T0:%.*]] = icmp eq ptr [[EDD_CUR]], [[ED_BEGIN]]
87 // CHECKv03-NEXT: br i1 [[T0]]
89 // Back to the primary EH destructor.
90 // CHECK: [[E_AFTER:%.*]] = phi ptr [ [[E_END]], {{%.*}} ], [ [[E_CUR:%.*]], {{%.*}} ]
91 // CHECK-NEXT: [[E_CUR]] = getelementptr inbounds [[A]], ptr [[E_AFTER]], i64 -1
92 // CHECKv03-NEXT: invoke void @_ZN5test01AD1Ev(ptr {{[^,]*}} [[E_CUR]])
93 // CHECKv11-NEXT: call void @_ZN5test01AD1Ev(ptr {{[^,]*}} [[E_CUR]])
94 // CHECK: [[T0:%.*]] = icmp eq ptr [[E_CUR]], [[E0]]
95 // CHECK-NEXT: br i1 [[T0]],
99 namespace test1 {
100 struct A { A(); A(int); ~A(); };
101 struct B { A x, y, z; int w; };
103 void test() {
104 B v = { 5, 6, 7, 8 };
106 // CHECK-LABEL: define{{.*}} void @_ZN5test14testEv()
107 // CHECK-SAME: personality ptr @__gxx_personality_v0
108 // CHECK: [[V:%.*]] = alloca [[B:%.*]], align 4
109 // CHECK-NEXT: alloca ptr
110 // CHECK-NEXT: alloca i32
111 // CHECK-NEXT: [[X:%.*]] = getelementptr inbounds [[B]], ptr [[V]], i32 0, i32 0
112 // CHECK-NEXT: call void @_ZN5test11AC1Ei(ptr {{[^,]*}} [[X]], i32 noundef 5)
113 // CHECK-NEXT: [[Y:%.*]] = getelementptr inbounds [[B]], ptr [[V]], i32 0, i32 1
114 // CHECK-NEXT: invoke void @_ZN5test11AC1Ei(ptr {{[^,]*}} [[Y]], i32 noundef 6)
115 // CHECK: [[Z:%.*]] = getelementptr inbounds [[B]], ptr [[V]], i32 0, i32 2
116 // CHECK-NEXT: invoke void @_ZN5test11AC1Ei(ptr {{[^,]*}} [[Z]], i32 noundef 7)
117 // CHECK: [[W:%.*]] = getelementptr inbounds [[B]], ptr [[V]], i32 0, i32 3
118 // CHECK-NEXT: store i32 8, ptr [[W]], align 4
119 // CHECK-NEXT: call void @_ZN5test11BD1Ev(ptr {{[^,]*}} [[V]])
120 // CHECK-NEXT: ret void
122 // FIXME: again, the block ordering is pretty bad here
123 // CHECK: landingpad { ptr, i32 }
124 // CHECK-NEXT: cleanup
125 // CHECK: landingpad { ptr, i32 }
126 // CHECK-NEXT: cleanup
127 // CHECKv03: invoke void @_ZN5test11AD1Ev(ptr {{[^,]*}} [[Y]])
128 // CHECKv03: invoke void @_ZN5test11AD1Ev(ptr {{[^,]*}} [[X]])
129 // CHECKv11: call void @_ZN5test11AD1Ev(ptr {{[^,]*}} [[Y]])
130 // CHECKv11: call void @_ZN5test11AD1Ev(ptr {{[^,]*}} [[X]])
133 namespace test2 {
134 struct A { A(); ~A(); };
136 void test() {
137 A v[4][7];
139 // CHECK-LABEL: define{{.*}} void @_ZN5test24testEv()
140 // CHECK-SAME: personality ptr @__gxx_personality_v0
141 // CHECK: [[V:%.*]] = alloca [4 x [7 x [[A:%.*]]]], align 1
142 // CHECK-NEXT: alloca ptr
143 // CHECK-NEXT: alloca i32
145 // Main initialization loop.
146 // CHECK-NEXT: [[BEGIN:%.*]] = getelementptr inbounds [4 x [7 x [[A]]]], ptr [[V]], i32 0, i32 0, i32 0
147 // CHECK-NEXT: [[END:%.*]] = getelementptr inbounds [[A]], ptr [[BEGIN]], i64 28
148 // CHECK-NEXT: br label
149 // CHECK: [[CUR:%.*]] = phi ptr [ [[BEGIN]], {{%.*}} ], [ [[NEXT:%.*]], {{%.*}} ]
150 // CHECK-NEXT: invoke void @_ZN5test21AC1Ev(ptr {{[^,]*}} [[CUR]])
151 // CHECK: [[NEXT:%.*]] = getelementptr inbounds [[A]], ptr [[CUR]], i64 1
152 // CHECK-NEXT: [[DONE:%.*]] = icmp eq ptr [[NEXT]], [[END]]
153 // CHECK-NEXT: br i1 [[DONE]],
155 // Partial destruction landing pad.
156 // CHECK: landingpad { ptr, i32 }
157 // CHECK-NEXT: cleanup
158 // CHECK: [[EMPTY:%.*]] = icmp eq ptr [[BEGIN]], [[CUR]]
159 // CHECK-NEXT: br i1 [[EMPTY]],
160 // CHECK: [[PAST:%.*]] = phi ptr [ [[CUR]], {{%.*}} ], [ [[DEL:%.*]], {{%.*}} ]
161 // CHECK-NEXT: [[DEL]] = getelementptr inbounds [[A]], ptr [[PAST]], i64 -1
162 // CHECKv03-NEXT: invoke void @_ZN5test21AD1Ev(ptr {{[^,]*}} [[DEL]])
163 // CHECKv11-NEXT: call void @_ZN5test21AD1Ev(ptr {{[^,]*}} [[DEL]])
164 // CHECK: [[T0:%.*]] = icmp eq ptr [[DEL]], [[BEGIN]]
165 // CHECK-NEXT: br i1 [[T0]],
170 // PR10351
171 namespace test3 {
172 struct A { A(); ~A(); void *p; };
173 struct B {
174 B() {}
175 A a;
178 B *test() {
179 return new B[10];
180 // invoke void @_ZN5test31BD1Ev(
184 namespace test4 {
185 struct A { A(unsigned i); ~A(); };
186 void test() {
187 A v[2][3] = { { A(0), A(1), A(2) }, { A(3), A(4), A(5) } };
190 // CHECK-LABEL: define{{.*}} void @_ZN5test44testEv()
191 // CHECK: [[ARRAY:%.*]] = alloca [2 x [3 x [[A:%.*]]]], align
192 // CHECK: [[A0:%.*]] = getelementptr inbounds [2 x [3 x [[A]]]], ptr [[ARRAY]], i64 0, i64 0
193 // CHECK-NEXT: store ptr [[A0]],
194 // CHECK-NEXT: [[A00:%.*]] = getelementptr inbounds [3 x [[A]]], ptr [[A0]], i64 0, i64 0
195 // CHECK-NEXT: store ptr [[A00]],
196 // CHECK-NEXT: invoke void @_ZN5test41AC1Ej(ptr {{[^,]*}} [[A00]], i32 noundef 0)
197 // CHECK: [[A01:%.*]] = getelementptr inbounds [[A]], ptr [[A00]], i64 1
198 // CHECK-NEXT: store ptr [[A01]],
199 // CHECK-NEXT: invoke void @_ZN5test41AC1Ej(ptr {{[^,]*}} [[A01]], i32 noundef 1)
200 // CHECK: [[A02:%.*]] = getelementptr inbounds [[A]], ptr [[A01]], i64 1
201 // CHECK-NEXT: store ptr [[A02]],
202 // CHECK-NEXT: invoke void @_ZN5test41AC1Ej(ptr {{[^,]*}} [[A02]], i32 noundef 2)
203 // CHECK: [[A1:%.*]] = getelementptr inbounds [3 x [[A]]], ptr [[A0]], i64 1
204 // CHECK-NEXT: store ptr [[A1]],
205 // CHECK-NEXT: [[A10:%.*]] = getelementptr inbounds [3 x [[A]]], ptr [[A1]], i64 0, i64 0
206 // CHECK-NEXT: store ptr [[A10]],
207 // CHECK-NEXT: invoke void @_ZN5test41AC1Ej(ptr {{[^,]*}} [[A10]], i32 noundef 3)
208 // CHECK: [[A11:%.*]] = getelementptr inbounds [[A]], ptr [[A10]], i64 1
209 // CHECK-NEXT: store ptr [[A11]],
210 // CHECK-NEXT: invoke void @_ZN5test41AC1Ej(ptr {{[^,]*}} [[A11]], i32 noundef 4)
211 // CHECK: [[A12:%.*]] = getelementptr inbounds [[A]], ptr [[A11]], i64 1
212 // CHECK-NEXT: store ptr [[A12]],
213 // CHECK-NEXT: invoke void @_ZN5test41AC1Ej(ptr {{[^,]*}} [[A12]], i32 noundef 5)