1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 %s -fno-rtti -triple=i386-pc-win32 -emit-llvm -o %t
2f4a2713aSLionel Sambuc // RUN: FileCheck %s < %t
3f4a2713aSLionel Sambuc // RUN: FileCheck --check-prefix=CHECK2 %s < %t
4f4a2713aSLionel Sambuc
5f4a2713aSLionel Sambuc // For now, just make sure x86_64 doesn't crash.
6*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 %s -fno-rtti -triple=x86_64-pc-win32 -emit-llvm -o %t
7f4a2713aSLionel Sambuc
8f4a2713aSLionel Sambuc struct VBase {
9f4a2713aSLionel Sambuc virtual ~VBase();
10f4a2713aSLionel Sambuc virtual void foo();
11f4a2713aSLionel Sambuc virtual void bar();
12f4a2713aSLionel Sambuc int field;
13f4a2713aSLionel Sambuc };
14f4a2713aSLionel Sambuc
15f4a2713aSLionel Sambuc struct B : virtual VBase {
16f4a2713aSLionel Sambuc B();
17f4a2713aSLionel Sambuc virtual ~B();
18f4a2713aSLionel Sambuc virtual void foo();
19f4a2713aSLionel Sambuc virtual void bar();
20f4a2713aSLionel Sambuc };
21f4a2713aSLionel Sambuc
B()22f4a2713aSLionel Sambuc B::B() {
23f4a2713aSLionel Sambuc // CHECK-LABEL: define x86_thiscallcc %struct.B* @"\01??0B@@QAE@XZ"
24f4a2713aSLionel Sambuc // CHECK: %[[THIS:.*]] = load %struct.B**
25f4a2713aSLionel Sambuc // CHECK: br i1 %{{.*}}, label %[[INIT_VBASES:.*]], label %[[SKIP_VBASES:.*]]
26f4a2713aSLionel Sambuc
27f4a2713aSLionel Sambuc // Don't check the INIT_VBASES case as it's covered by the ctor tests.
28f4a2713aSLionel Sambuc
29f4a2713aSLionel Sambuc // CHECK: %[[SKIP_VBASES]]
30f4a2713aSLionel Sambuc // CHECK: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8*
31f4a2713aSLionel Sambuc // CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8* %[[THIS_i8]], i32 0
32f4a2713aSLionel Sambuc // ...
33f4a2713aSLionel Sambuc // CHECK: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8*
34f4a2713aSLionel Sambuc // CHECK: %[[VFPTR_i8:.*]] = getelementptr inbounds i8* %[[THIS_i8]], i32 %{{.*}}
35*0a6a1f1dSLionel Sambuc // CHECK: %[[VFPTR:.*]] = bitcast i8* %[[VFPTR_i8]] to i32 (...)***
36*0a6a1f1dSLionel Sambuc // CHECK: store i32 (...)** bitcast ([3 x i8*]* @"\01??_7B@@6B@" to i32 (...)**), i32 (...)*** %[[VFPTR]]
37f4a2713aSLionel Sambuc
38f4a2713aSLionel Sambuc // Initialize vtorDisp:
39f4a2713aSLionel Sambuc // CHECK: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8*
40f4a2713aSLionel Sambuc // CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8* %[[THIS_i8]], i32 0
41f4a2713aSLionel Sambuc // ...
42f4a2713aSLionel Sambuc // CHECK: %[[VBASE_OFFSET:.*]] = add nsw i32 0, %{{.*}}
43f4a2713aSLionel Sambuc // CHECK: %[[VTORDISP_VAL:.*]] = sub i32 %[[VBASE_OFFSET]], 8
44f4a2713aSLionel Sambuc // CHECK: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8*
45f4a2713aSLionel Sambuc // CHECK: %[[VBASE_i8:.*]] = getelementptr inbounds i8* %[[THIS_i8]], i32 %[[VBASE_OFFSET]]
46f4a2713aSLionel Sambuc // CHECK: %[[VTORDISP_i8:.*]] = getelementptr i8* %[[VBASE_i8]], i32 -4
47f4a2713aSLionel Sambuc // CHECK: %[[VTORDISP_PTR:.*]] = bitcast i8* %[[VTORDISP_i8]] to i32*
48f4a2713aSLionel Sambuc // CHECK: store i32 %[[VTORDISP_VAL]], i32* %[[VTORDISP_PTR]]
49f4a2713aSLionel Sambuc
50f4a2713aSLionel Sambuc // CHECK: ret
51f4a2713aSLionel Sambuc }
52f4a2713aSLionel Sambuc
~B()53f4a2713aSLionel Sambuc B::~B() {
54f4a2713aSLionel Sambuc // CHECK-LABEL: define x86_thiscallcc void @"\01??1B@@UAE@XZ"
55f4a2713aSLionel Sambuc // Adjust the this parameter:
56f4a2713aSLionel Sambuc // CHECK: %[[THIS_PARAM_i8:.*]] = bitcast %struct.B* {{.*}} to i8*
57f4a2713aSLionel Sambuc // CHECK: %[[THIS_i8:.*]] = getelementptr inbounds i8* %[[THIS_PARAM_i8]], i32 -8
58f4a2713aSLionel Sambuc // CHECK: %[[THIS:.*]] = bitcast i8* %[[THIS_i8]] to %struct.B*
59f4a2713aSLionel Sambuc // CHECK: store %struct.B* %[[THIS]], %struct.B** %[[THIS_ADDR:.*]], align 4
60f4a2713aSLionel Sambuc // CHECK: %[[THIS:.*]] = load %struct.B** %[[THIS_ADDR]]
61f4a2713aSLionel Sambuc
62f4a2713aSLionel Sambuc // Restore the vfptr that could have been changed by a subclass.
63f4a2713aSLionel Sambuc // CHECK: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8*
64f4a2713aSLionel Sambuc // CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8* %[[THIS_i8]], i32 0
65f4a2713aSLionel Sambuc // ...
66f4a2713aSLionel Sambuc // CHECK: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8*
67f4a2713aSLionel Sambuc // CHECK: %[[VFPTR_i8:.*]] = getelementptr inbounds i8* %[[THIS_i8]], i32 %{{.*}}
68*0a6a1f1dSLionel Sambuc // CHECK: %[[VFPTR:.*]] = bitcast i8* %[[VFPTR_i8]] to i32 (...)***
69*0a6a1f1dSLionel Sambuc // CHECK: store i32 (...)** bitcast ([3 x i8*]* @"\01??_7B@@6B@" to i32 (...)**), i32 (...)*** %[[VFPTR]]
70f4a2713aSLionel Sambuc
71f4a2713aSLionel Sambuc // Initialize vtorDisp:
72f4a2713aSLionel Sambuc // CHECK: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8*
73f4a2713aSLionel Sambuc // CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8* %[[THIS_i8]], i32 0
74f4a2713aSLionel Sambuc // ...
75f4a2713aSLionel Sambuc // CHECK: %[[VBASE_OFFSET:.*]] = add nsw i32 0, %{{.*}}
76f4a2713aSLionel Sambuc // CHECK: %[[VTORDISP_VAL:.*]] = sub i32 %[[VBASE_OFFSET]], 8
77f4a2713aSLionel Sambuc // CHECK: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8*
78f4a2713aSLionel Sambuc // CHECK: %[[VBASE_i8:.*]] = getelementptr inbounds i8* %[[THIS_i8]], i32 %[[VBASE_OFFSET]]
79f4a2713aSLionel Sambuc // CHECK: %[[VTORDISP_i8:.*]] = getelementptr i8* %[[VBASE_i8]], i32 -4
80f4a2713aSLionel Sambuc // CHECK: %[[VTORDISP_PTR:.*]] = bitcast i8* %[[VTORDISP_i8]] to i32*
81f4a2713aSLionel Sambuc // CHECK: store i32 %[[VTORDISP_VAL]], i32* %[[VTORDISP_PTR]]
82f4a2713aSLionel Sambuc
83f4a2713aSLionel Sambuc foo(); // Avoid the "trivial destructor" optimization.
84f4a2713aSLionel Sambuc
85f4a2713aSLionel Sambuc // CHECK: ret
86f4a2713aSLionel Sambuc
87f4a2713aSLionel Sambuc // CHECK2-LABEL: define linkonce_odr x86_thiscallcc void @"\01??_DB@@UAE@XZ"(%struct.B*
88f4a2713aSLionel Sambuc // CHECK2: %[[THIS:.*]] = load %struct.B** {{.*}}
89f4a2713aSLionel Sambuc // CHECK2: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8*
90f4a2713aSLionel Sambuc // CHECK2: %[[B_i8:.*]] = getelementptr i8* %[[THIS_i8]], i32 8
91f4a2713aSLionel Sambuc // CHECK2: %[[B:.*]] = bitcast i8* %[[B_i8]] to %struct.B*
92f4a2713aSLionel Sambuc // CHECK2: call x86_thiscallcc void @"\01??1B@@UAE@XZ"(%struct.B* %[[B]])
93f4a2713aSLionel Sambuc // CHECK2: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8*
94f4a2713aSLionel Sambuc // CHECK2: %[[VBASE_i8:.*]] = getelementptr inbounds i8* %[[THIS_i8]], i64 8
95f4a2713aSLionel Sambuc // CHECK2: %[[VBASE:.*]] = bitcast i8* %[[VBASE_i8]] to %struct.VBase*
96f4a2713aSLionel Sambuc // CHECK2: call x86_thiscallcc void @"\01??1VBase@@UAE@XZ"(%struct.VBase* %[[VBASE]])
97f4a2713aSLionel Sambuc // CHECK2: ret
98f4a2713aSLionel Sambuc
99*0a6a1f1dSLionel Sambuc // CHECK2-LABEL: define linkonce_odr x86_thiscallcc i8* @"\01??_GB@@UAEPAXI@Z"
100f4a2713aSLionel Sambuc // CHECK2: %[[THIS_PARAM_i8:.*]] = bitcast %struct.B* {{.*}} to i8*
101f4a2713aSLionel Sambuc // CHECK2: %[[THIS_i8:.*]] = getelementptr inbounds i8* %[[THIS_PARAM_i8:.*]], i32 -8
102f4a2713aSLionel Sambuc // CHECK2: %[[THIS:.*]] = bitcast i8* %[[THIS_i8]] to %struct.B*
103f4a2713aSLionel Sambuc // CHECK2: store %struct.B* %[[THIS]], %struct.B** %[[THIS_ADDR:.*]], align 4
104f4a2713aSLionel Sambuc // CHECK2: %[[THIS:.*]] = load %struct.B** %[[THIS_ADDR]]
105f4a2713aSLionel Sambuc // CHECK2: call x86_thiscallcc void @"\01??_DB@@UAE@XZ"(%struct.B* %[[THIS]])
106f4a2713aSLionel Sambuc // ...
107f4a2713aSLionel Sambuc // CHECK2: ret
108f4a2713aSLionel Sambuc }
109f4a2713aSLionel Sambuc
foo()110f4a2713aSLionel Sambuc void B::foo() {
111f4a2713aSLionel Sambuc // CHECK-LABEL: define x86_thiscallcc void @"\01?foo@B@@UAEXXZ"(i8*
112f4a2713aSLionel Sambuc //
113f4a2713aSLionel Sambuc // B::foo gets 'this' cast to VBase* in ECX (i.e. this+8) so we
114f4a2713aSLionel Sambuc // need to adjust 'this' before use.
115f4a2713aSLionel Sambuc //
116f4a2713aSLionel Sambuc // CHECK: %[[THIS_ADDR:.*]] = alloca %struct.B*, align 4
117f4a2713aSLionel Sambuc // CHECK: %[[THIS_i8:.*]] = getelementptr inbounds i8* %[[ECX:.*]], i32 -8
118f4a2713aSLionel Sambuc // CHECK: %[[THIS:.*]] = bitcast i8* %[[THIS_i8]] to %struct.B*
119f4a2713aSLionel Sambuc // CHECK: store %struct.B* %[[THIS]], %struct.B** %[[THIS_ADDR]], align 4
120f4a2713aSLionel Sambuc
121f4a2713aSLionel Sambuc field = 42;
122f4a2713aSLionel Sambuc // CHECK: %[[THIS:.*]] = load %struct.B** %[[THIS_ADDR]]
123f4a2713aSLionel Sambuc // CHECK: %[[THIS8:.*]] = bitcast %struct.B* %[[THIS]] to i8*
124f4a2713aSLionel Sambuc // CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8* %[[THIS8]], i32 0
125*0a6a1f1dSLionel Sambuc // CHECK: %[[VBPTR8:.*]] = bitcast i8* %[[VBPTR]] to i32**
126*0a6a1f1dSLionel Sambuc // CHECK: %[[VBTABLE:.*]] = load i32** %[[VBPTR8]]
127*0a6a1f1dSLionel Sambuc // CHECK: %[[VBENTRY:.*]] = getelementptr inbounds i32* %[[VBTABLE]], i32 1
128f4a2713aSLionel Sambuc // CHECK: %[[VBOFFSET32:.*]] = load i32* %[[VBENTRY]]
129f4a2713aSLionel Sambuc // CHECK: %[[VBOFFSET:.*]] = add nsw i32 0, %[[VBOFFSET32]]
130f4a2713aSLionel Sambuc // CHECK: %[[THIS8:.*]] = bitcast %struct.B* %[[THIS]] to i8*
131f4a2713aSLionel Sambuc // CHECK: %[[VBASE_i8:.*]] = getelementptr inbounds i8* %[[THIS8]], i32 %[[VBOFFSET]]
132f4a2713aSLionel Sambuc // CHECK: %[[VBASE:.*]] = bitcast i8* %[[VBASE_i8]] to %struct.VBase*
133f4a2713aSLionel Sambuc // CHECK: %[[FIELD:.*]] = getelementptr inbounds %struct.VBase* %[[VBASE]], i32 0, i32 1
134f4a2713aSLionel Sambuc // CHECK: store i32 42, i32* %[[FIELD]], align 4
135f4a2713aSLionel Sambuc //
136f4a2713aSLionel Sambuc // CHECK: ret void
137f4a2713aSLionel Sambuc }
138f4a2713aSLionel Sambuc
call_vbase_bar(B * obj)139f4a2713aSLionel Sambuc void call_vbase_bar(B *obj) {
140f4a2713aSLionel Sambuc // CHECK-LABEL: define void @"\01?call_vbase_bar@@YAXPAUB@@@Z"(%struct.B* %obj)
141f4a2713aSLionel Sambuc // CHECK: %[[OBJ:.*]] = load %struct.B
142f4a2713aSLionel Sambuc
143f4a2713aSLionel Sambuc obj->bar();
144f4a2713aSLionel Sambuc // When calling a vbase's virtual method, one needs to adjust 'this'
145f4a2713aSLionel Sambuc // at the caller site.
146f4a2713aSLionel Sambuc //
147f4a2713aSLionel Sambuc // CHECK: %[[OBJ_i8:.*]] = bitcast %struct.B* %[[OBJ]] to i8*
148f4a2713aSLionel Sambuc // CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8* %[[OBJ_i8]], i32 0
149*0a6a1f1dSLionel Sambuc // CHECK: %[[VBPTR8:.*]] = bitcast i8* %[[VBPTR]] to i32**
150*0a6a1f1dSLionel Sambuc // CHECK: %[[VBTABLE:.*]] = load i32** %[[VBPTR8]]
151*0a6a1f1dSLionel Sambuc // CHECK: %[[VBENTRY:.*]] = getelementptr inbounds i32* %[[VBTABLE]], i32 1
152f4a2713aSLionel Sambuc // CHECK: %[[VBOFFSET32:.*]] = load i32* %[[VBENTRY]]
153f4a2713aSLionel Sambuc // CHECK: %[[VBOFFSET:.*]] = add nsw i32 0, %[[VBOFFSET32]]
154f4a2713aSLionel Sambuc // CHECK: %[[VBASE_i8:.*]] = getelementptr inbounds i8* %[[OBJ_i8]], i32 %[[VBOFFSET]]
155f4a2713aSLionel Sambuc // CHECK: %[[VFPTR:.*]] = bitcast i8* %[[VBASE_i8]] to void (i8*)***
156f4a2713aSLionel Sambuc // CHECK: %[[VFTABLE:.*]] = load void (i8*)*** %[[VFPTR]]
157f4a2713aSLionel Sambuc // CHECK: %[[VFUN:.*]] = getelementptr inbounds void (i8*)** %[[VFTABLE]], i64 2
158f4a2713aSLionel Sambuc // CHECK: %[[VFUN_VALUE:.*]] = load void (i8*)** %[[VFUN]]
159f4a2713aSLionel Sambuc //
160f4a2713aSLionel Sambuc // CHECK: %[[OBJ_i8:.*]] = bitcast %struct.B* %[[OBJ]] to i8*
161f4a2713aSLionel Sambuc // CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8* %[[OBJ_i8]], i32 0
162*0a6a1f1dSLionel Sambuc // CHECK: %[[VBPTR8:.*]] = bitcast i8* %[[VBPTR]] to i32**
163*0a6a1f1dSLionel Sambuc // CHECK: %[[VBTABLE:.*]] = load i32** %[[VBPTR8]]
164*0a6a1f1dSLionel Sambuc // CHECK: %[[VBENTRY:.*]] = getelementptr inbounds i32* %[[VBTABLE]], i32 1
165f4a2713aSLionel Sambuc // CHECK: %[[VBOFFSET32:.*]] = load i32* %[[VBENTRY]]
166f4a2713aSLionel Sambuc // CHECK: %[[VBOFFSET:.*]] = add nsw i32 0, %[[VBOFFSET32]]
167f4a2713aSLionel Sambuc // CHECK: %[[VBASE:.*]] = getelementptr inbounds i8* %[[OBJ_i8]], i32 %[[VBOFFSET]]
168f4a2713aSLionel Sambuc //
169f4a2713aSLionel Sambuc // CHECK: call x86_thiscallcc void %[[VFUN_VALUE]](i8* %[[VBASE]])
170f4a2713aSLionel Sambuc //
171f4a2713aSLionel Sambuc // CHECK: ret void
172f4a2713aSLionel Sambuc }
173f4a2713aSLionel Sambuc
delete_B(B * obj)174f4a2713aSLionel Sambuc void delete_B(B *obj) {
175f4a2713aSLionel Sambuc // CHECK-LABEL: define void @"\01?delete_B@@YAXPAUB@@@Z"(%struct.B* %obj)
176f4a2713aSLionel Sambuc // CHECK: %[[OBJ:.*]] = load %struct.B
177f4a2713aSLionel Sambuc
178f4a2713aSLionel Sambuc delete obj;
179f4a2713aSLionel Sambuc // CHECK: %[[OBJ_i8:.*]] = bitcast %struct.B* %[[OBJ]] to i8*
180f4a2713aSLionel Sambuc // CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8* %[[OBJ_i8]], i32 0
181*0a6a1f1dSLionel Sambuc // CHECK: %[[VBPTR8:.*]] = bitcast i8* %[[VBPTR]] to i32**
182*0a6a1f1dSLionel Sambuc // CHECK: %[[VBTABLE:.*]] = load i32** %[[VBPTR8]]
183*0a6a1f1dSLionel Sambuc // CHECK: %[[VBENTRY:.*]] = getelementptr inbounds i32* %[[VBTABLE]], i32 1
184f4a2713aSLionel Sambuc // CHECK: %[[VBOFFSET32:.*]] = load i32* %[[VBENTRY]]
185f4a2713aSLionel Sambuc // CHECK: %[[VBOFFSET:.*]] = add nsw i32 0, %[[VBOFFSET32]]
186f4a2713aSLionel Sambuc // CHECK: %[[VBASE_i8:.*]] = getelementptr inbounds i8* %[[OBJ_i8]], i32 %[[VBOFFSET]]
187*0a6a1f1dSLionel Sambuc // CHECK: %[[VFPTR:.*]] = bitcast i8* %[[VBASE_i8]] to i8* (%struct.B*, i32)***
188*0a6a1f1dSLionel Sambuc // CHECK: %[[VFTABLE:.*]] = load i8* (%struct.B*, i32)*** %[[VFPTR]]
189*0a6a1f1dSLionel Sambuc // CHECK: %[[VFUN:.*]] = getelementptr inbounds i8* (%struct.B*, i32)** %[[VFTABLE]], i64 0
190*0a6a1f1dSLionel Sambuc // CHECK: %[[VFUN_VALUE:.*]] = load i8* (%struct.B*, i32)** %[[VFUN]]
191f4a2713aSLionel Sambuc //
192f4a2713aSLionel Sambuc // CHECK: %[[OBJ_i8:.*]] = bitcast %struct.B* %[[OBJ]] to i8*
193f4a2713aSLionel Sambuc // CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8* %[[OBJ_i8]], i32 0
194*0a6a1f1dSLionel Sambuc // CHECK: %[[VBPTR8:.*]] = bitcast i8* %[[VBPTR]] to i32**
195*0a6a1f1dSLionel Sambuc // CHECK: %[[VBTABLE:.*]] = load i32** %[[VBPTR8]]
196*0a6a1f1dSLionel Sambuc // CHECK: %[[VBENTRY:.*]] = getelementptr inbounds i32* %[[VBTABLE]], i32 1
197f4a2713aSLionel Sambuc // CHECK: %[[VBOFFSET32:.*]] = load i32* %[[VBENTRY]]
198f4a2713aSLionel Sambuc // CHECK: %[[VBOFFSET:.*]] = add nsw i32 0, %[[VBOFFSET32]]
199f4a2713aSLionel Sambuc // CHECK: %[[VBASE_i8:.*]] = getelementptr inbounds i8* %[[OBJ_i8]], i32 %[[VBOFFSET]]
200f4a2713aSLionel Sambuc // CHECK: %[[VBASE:.*]] = bitcast i8* %[[VBASE_i8]] to %struct.B*
201f4a2713aSLionel Sambuc //
202*0a6a1f1dSLionel Sambuc // CHECK: call x86_thiscallcc i8* %[[VFUN_VALUE]](%struct.B* %[[VBASE]], i32 1)
203f4a2713aSLionel Sambuc // CHECK: ret void
204f4a2713aSLionel Sambuc }
205f4a2713aSLionel Sambuc
call_complete_dtor()206f4a2713aSLionel Sambuc void call_complete_dtor() {
207f4a2713aSLionel Sambuc // CHECK-LABEL: define void @"\01?call_complete_dtor@@YAXXZ"
208f4a2713aSLionel Sambuc B b;
209f4a2713aSLionel Sambuc // CHECK: call x86_thiscallcc %struct.B* @"\01??0B@@QAE@XZ"(%struct.B* %[[B:.*]], i32 1)
210f4a2713aSLionel Sambuc // CHECK-NOT: getelementptr
211f4a2713aSLionel Sambuc // CHECK: call x86_thiscallcc void @"\01??_DB@@UAE@XZ"(%struct.B* %[[B]])
212f4a2713aSLionel Sambuc // CHECK: ret
213f4a2713aSLionel Sambuc }
214f4a2713aSLionel Sambuc
215f4a2713aSLionel Sambuc struct C : B {
216f4a2713aSLionel Sambuc C();
217f4a2713aSLionel Sambuc // has an implicit vdtor.
218f4a2713aSLionel Sambuc };
219f4a2713aSLionel Sambuc
220f4a2713aSLionel Sambuc // Used to crash on an assertion.
C()221f4a2713aSLionel Sambuc C::C() {
222f4a2713aSLionel Sambuc // CHECK-LABEL: define x86_thiscallcc %struct.C* @"\01??0C@@QAE@XZ"
223f4a2713aSLionel Sambuc }
224f4a2713aSLionel Sambuc
225f4a2713aSLionel Sambuc namespace multiple_vbases {
226f4a2713aSLionel Sambuc struct A {
227f4a2713aSLionel Sambuc virtual void a();
228f4a2713aSLionel Sambuc };
229f4a2713aSLionel Sambuc
230f4a2713aSLionel Sambuc struct B {
231f4a2713aSLionel Sambuc virtual void b();
232f4a2713aSLionel Sambuc };
233f4a2713aSLionel Sambuc
234f4a2713aSLionel Sambuc struct C {
235f4a2713aSLionel Sambuc virtual void c();
236f4a2713aSLionel Sambuc };
237f4a2713aSLionel Sambuc
238f4a2713aSLionel Sambuc struct D : virtual A, virtual B, virtual C {
239f4a2713aSLionel Sambuc virtual void a();
240f4a2713aSLionel Sambuc virtual void b();
241f4a2713aSLionel Sambuc virtual void c();
242f4a2713aSLionel Sambuc D();
243f4a2713aSLionel Sambuc };
244f4a2713aSLionel Sambuc
D()245f4a2713aSLionel Sambuc D::D() {
246f4a2713aSLionel Sambuc // CHECK-LABEL: define x86_thiscallcc %"struct.multiple_vbases::D"* @"\01??0D@multiple_vbases@@QAE@XZ"
247f4a2713aSLionel Sambuc // Just make sure we emit 3 vtordisps after initializing vfptrs.
248*0a6a1f1dSLionel Sambuc // CHECK: store i32 (...)** bitcast ([1 x i8*]* @"\01??_7D@multiple_vbases@@6BA@1@@" to i32 (...)**), i32 (...)*** %{{.*}}
249*0a6a1f1dSLionel Sambuc // CHECK: store i32 (...)** bitcast ([1 x i8*]* @"\01??_7D@multiple_vbases@@6BB@1@@" to i32 (...)**), i32 (...)*** %{{.*}}
250*0a6a1f1dSLionel Sambuc // CHECK: store i32 (...)** bitcast ([1 x i8*]* @"\01??_7D@multiple_vbases@@6BC@1@@" to i32 (...)**), i32 (...)*** %{{.*}}
251f4a2713aSLionel Sambuc // ...
252f4a2713aSLionel Sambuc // CHECK: store i32 %{{.*}}, i32* %{{.*}}
253f4a2713aSLionel Sambuc // CHECK: store i32 %{{.*}}, i32* %{{.*}}
254f4a2713aSLionel Sambuc // CHECK: store i32 %{{.*}}, i32* %{{.*}}
255f4a2713aSLionel Sambuc // CHECK: ret
256f4a2713aSLionel Sambuc }
257f4a2713aSLionel Sambuc }
258f4a2713aSLionel Sambuc
259f4a2713aSLionel Sambuc namespace diamond {
260f4a2713aSLionel Sambuc struct A {
261f4a2713aSLionel Sambuc A();
262f4a2713aSLionel Sambuc virtual ~A();
263f4a2713aSLionel Sambuc };
264f4a2713aSLionel Sambuc
265f4a2713aSLionel Sambuc struct B : virtual A {
266f4a2713aSLionel Sambuc B();
267f4a2713aSLionel Sambuc ~B();
268f4a2713aSLionel Sambuc };
269f4a2713aSLionel Sambuc
270f4a2713aSLionel Sambuc struct C : virtual A {
271f4a2713aSLionel Sambuc C();
272f4a2713aSLionel Sambuc ~C();
273f4a2713aSLionel Sambuc int c1, c2, c3;
274f4a2713aSLionel Sambuc };
275f4a2713aSLionel Sambuc
276f4a2713aSLionel Sambuc struct Z {
277f4a2713aSLionel Sambuc int z;
278f4a2713aSLionel Sambuc };
279f4a2713aSLionel Sambuc
280f4a2713aSLionel Sambuc struct D : virtual Z, B, C {
281f4a2713aSLionel Sambuc D();
282f4a2713aSLionel Sambuc ~D();
283f4a2713aSLionel Sambuc } d;
284f4a2713aSLionel Sambuc
~D()285f4a2713aSLionel Sambuc D::~D() {
286f4a2713aSLionel Sambuc // CHECK-LABEL: define x86_thiscallcc void @"\01??1D@diamond@@UAE@XZ"(%"struct.diamond::D"*)
287f4a2713aSLionel Sambuc // CHECK: %[[ARG_i8:.*]] = bitcast %"struct.diamond::D"* %{{.*}} to i8*
288f4a2713aSLionel Sambuc // CHECK: %[[THIS_i8:.*]] = getelementptr inbounds i8* %[[ARG_i8]], i32 -24
289f4a2713aSLionel Sambuc // CHECK: %[[THIS:.*]] = bitcast i8* %[[THIS_i8]] to %"struct.diamond::D"*
290f4a2713aSLionel Sambuc // CHECK: store %"struct.diamond::D"* %[[THIS]], %"struct.diamond::D"** %[[THIS_VAL:.*]], align 4
291f4a2713aSLionel Sambuc // CHECK: %[[THIS:.*]] = load %"struct.diamond::D"** %[[THIS_VAL]]
292f4a2713aSLionel Sambuc // CHECK: %[[D_i8:.*]] = bitcast %"struct.diamond::D"* %[[THIS]] to i8*
293f4a2713aSLionel Sambuc // CHECK: %[[C_i8:.*]] = getelementptr inbounds i8* %[[D_i8]], i64 4
294f4a2713aSLionel Sambuc // CHECK: %[[C:.*]] = bitcast i8* %[[C_i8]] to %"struct.diamond::C"*
295f4a2713aSLionel Sambuc // CHECK: %[[C_i8:.*]] = bitcast %"struct.diamond::C"* %[[C]] to i8*
296f4a2713aSLionel Sambuc // CHECK: %[[ARG_i8:.*]] = getelementptr i8* %{{.*}}, i32 16
297f4a2713aSLionel Sambuc // FIXME: We might consider changing the dtor this parameter type to i8*.
298f4a2713aSLionel Sambuc // CHECK: %[[ARG:.*]] = bitcast i8* %[[ARG_i8]] to %"struct.diamond::C"*
299f4a2713aSLionel Sambuc // CHECK: call x86_thiscallcc void @"\01??1C@diamond@@UAE@XZ"(%"struct.diamond::C"* %[[ARG]])
300f4a2713aSLionel Sambuc
301f4a2713aSLionel Sambuc // CHECK: %[[B:.*]] = bitcast %"struct.diamond::D"* %[[THIS]] to %"struct.diamond::B"*
302f4a2713aSLionel Sambuc // CHECK: %[[B_i8:.*]] = bitcast %"struct.diamond::B"* %[[B]] to i8*
303f4a2713aSLionel Sambuc // CHECK: %[[ARG_i8:.*]] = getelementptr i8* %[[B_i8]], i32 4
304f4a2713aSLionel Sambuc // CHECK: %[[ARG:.*]] = bitcast i8* %[[ARG_i8]] to %"struct.diamond::B"*
305f4a2713aSLionel Sambuc // CHECK: call x86_thiscallcc void @"\01??1B@diamond@@UAE@XZ"(%"struct.diamond::B"* %[[ARG]])
306f4a2713aSLionel Sambuc // CHECK: ret void
307f4a2713aSLionel Sambuc }
308f4a2713aSLionel Sambuc
309f4a2713aSLionel Sambuc }
310*0a6a1f1dSLionel Sambuc
311*0a6a1f1dSLionel Sambuc namespace test2 {
312*0a6a1f1dSLionel Sambuc struct A { A(); };
Btest2::B313*0a6a1f1dSLionel Sambuc struct B : virtual A { B() {} };
Ctest2::C314*0a6a1f1dSLionel Sambuc struct C : B, A { C() {} };
315*0a6a1f1dSLionel Sambuc
316*0a6a1f1dSLionel Sambuc // PR18435: Order mattered here. We were generating code for the delegating
317*0a6a1f1dSLionel Sambuc // call to B() from C().
callC()318*0a6a1f1dSLionel Sambuc void callC() { C x; }
319*0a6a1f1dSLionel Sambuc
320*0a6a1f1dSLionel Sambuc // CHECK-LABEL: define linkonce_odr x86_thiscallcc %"struct.test2::C"* @"\01??0C@test2@@QAE@XZ"
321*0a6a1f1dSLionel Sambuc // CHECK: (%"struct.test2::C"* returned %this, i32 %is_most_derived)
322*0a6a1f1dSLionel Sambuc // CHECK: br i1
323*0a6a1f1dSLionel Sambuc // Virtual bases
324*0a6a1f1dSLionel Sambuc // CHECK: call x86_thiscallcc %"struct.test2::A"* @"\01??0A@test2@@QAE@XZ"(%"struct.test2::A"* %{{.*}})
325*0a6a1f1dSLionel Sambuc // CHECK: br label
326*0a6a1f1dSLionel Sambuc // Non-virtual bases
327*0a6a1f1dSLionel Sambuc // CHECK: call x86_thiscallcc %"struct.test2::B"* @"\01??0B@test2@@QAE@XZ"(%"struct.test2::B"* %{{.*}}, i32 0)
328*0a6a1f1dSLionel Sambuc // CHECK: call x86_thiscallcc %"struct.test2::A"* @"\01??0A@test2@@QAE@XZ"(%"struct.test2::A"* %{{.*}})
329*0a6a1f1dSLionel Sambuc // CHECK: ret
330*0a6a1f1dSLionel Sambuc
331*0a6a1f1dSLionel Sambuc // CHECK2-LABEL: define linkonce_odr x86_thiscallcc %"struct.test2::B"* @"\01??0B@test2@@QAE@XZ"
332*0a6a1f1dSLionel Sambuc // CHECK2: (%"struct.test2::B"* returned %this, i32 %is_most_derived)
333*0a6a1f1dSLionel Sambuc // CHECK2: call x86_thiscallcc %"struct.test2::A"* @"\01??0A@test2@@QAE@XZ"(%"struct.test2::A"* %{{.*}})
334*0a6a1f1dSLionel Sambuc // CHECK2: ret
335*0a6a1f1dSLionel Sambuc
336*0a6a1f1dSLionel Sambuc }
337*0a6a1f1dSLionel Sambuc
338*0a6a1f1dSLionel Sambuc namespace test3 {
339*0a6a1f1dSLionel Sambuc // PR19104: A non-virtual call of a virtual method doesn't use vftable thunks,
340*0a6a1f1dSLionel Sambuc // so requires only static adjustment which is different to the one used
341*0a6a1f1dSLionel Sambuc // for virtual calls.
342*0a6a1f1dSLionel Sambuc struct A {
343*0a6a1f1dSLionel Sambuc virtual void foo();
344*0a6a1f1dSLionel Sambuc };
345*0a6a1f1dSLionel Sambuc
346*0a6a1f1dSLionel Sambuc struct B : virtual A {
347*0a6a1f1dSLionel Sambuc virtual void bar();
348*0a6a1f1dSLionel Sambuc };
349*0a6a1f1dSLionel Sambuc
350*0a6a1f1dSLionel Sambuc struct C : virtual A {
351*0a6a1f1dSLionel Sambuc virtual void foo();
352*0a6a1f1dSLionel Sambuc };
353*0a6a1f1dSLionel Sambuc
354*0a6a1f1dSLionel Sambuc struct D : B, C {
355*0a6a1f1dSLionel Sambuc virtual void bar();
356*0a6a1f1dSLionel Sambuc int field; // Laid out between C and A subobjects in D.
357*0a6a1f1dSLionel Sambuc };
358*0a6a1f1dSLionel Sambuc
bar()359*0a6a1f1dSLionel Sambuc void D::bar() {
360*0a6a1f1dSLionel Sambuc // CHECK-LABEL: define x86_thiscallcc void @"\01?bar@D@test3@@UAEXXZ"(%"struct.test3::D"* %this)
361*0a6a1f1dSLionel Sambuc
362*0a6a1f1dSLionel Sambuc C::foo();
363*0a6a1f1dSLionel Sambuc // Shouldn't need any vbtable lookups. All we have to do is adjust to C*,
364*0a6a1f1dSLionel Sambuc // then compensate for the adjustment performed in the C::foo() prologue.
365*0a6a1f1dSLionel Sambuc // CHECK-NOT: load i8**
366*0a6a1f1dSLionel Sambuc // CHECK: %[[OBJ_i8:.*]] = bitcast %"struct.test3::D"* %{{.*}} to i8*
367*0a6a1f1dSLionel Sambuc // CHECK: %[[C_i8:.*]] = getelementptr inbounds i8* %[[OBJ_i8]], i32 8
368*0a6a1f1dSLionel Sambuc // CHECK: %[[C:.*]] = bitcast i8* %[[C_i8]] to %"struct.test3::C"*
369*0a6a1f1dSLionel Sambuc // CHECK: %[[C_i8:.*]] = bitcast %"struct.test3::C"* %[[C]] to i8*
370*0a6a1f1dSLionel Sambuc // CHECK: %[[ARG:.*]] = getelementptr i8* %[[C_i8]], i32 4
371*0a6a1f1dSLionel Sambuc // CHECK: call x86_thiscallcc void @"\01?foo@C@test3@@UAEXXZ"(i8* %[[ARG]])
372*0a6a1f1dSLionel Sambuc // CHECK: ret
373*0a6a1f1dSLionel Sambuc }
374*0a6a1f1dSLionel Sambuc }
375*0a6a1f1dSLionel Sambuc
376*0a6a1f1dSLionel Sambuc namespace test4{
377*0a6a1f1dSLionel Sambuc // PR19172: We used to merge method vftable locations wrong.
378*0a6a1f1dSLionel Sambuc
379*0a6a1f1dSLionel Sambuc struct A {
~Atest4::A380*0a6a1f1dSLionel Sambuc virtual ~A() {}
381*0a6a1f1dSLionel Sambuc };
382*0a6a1f1dSLionel Sambuc
383*0a6a1f1dSLionel Sambuc struct B {
~Btest4::B384*0a6a1f1dSLionel Sambuc virtual ~B() {}
385*0a6a1f1dSLionel Sambuc };
386*0a6a1f1dSLionel Sambuc
387*0a6a1f1dSLionel Sambuc struct C : virtual A, B {
388*0a6a1f1dSLionel Sambuc virtual ~C();
389*0a6a1f1dSLionel Sambuc };
390*0a6a1f1dSLionel Sambuc
391*0a6a1f1dSLionel Sambuc void foo(void*);
392*0a6a1f1dSLionel Sambuc
~C()393*0a6a1f1dSLionel Sambuc C::~C() {
394*0a6a1f1dSLionel Sambuc // CHECK-LABEL: define x86_thiscallcc void @"\01??1C@test4@@UAE@XZ"(%"struct.test4::C"* %this)
395*0a6a1f1dSLionel Sambuc
396*0a6a1f1dSLionel Sambuc // In this case "this" points to the most derived class, so no GEPs needed.
397*0a6a1f1dSLionel Sambuc // CHECK-NOT: getelementptr
398*0a6a1f1dSLionel Sambuc // CHECK-NOT: bitcast
399*0a6a1f1dSLionel Sambuc // CHECK: %[[VFPTR_i8:.*]] = bitcast %"struct.test4::C"* %{{.*}} to i32 (...)***
400*0a6a1f1dSLionel Sambuc // CHECK: store i32 (...)** bitcast ([1 x i8*]* @"\01??_7C@test4@@6BB@1@@" to i32 (...)**), i32 (...)*** %[[VFPTR_i8]]
401*0a6a1f1dSLionel Sambuc
402*0a6a1f1dSLionel Sambuc foo(this);
403*0a6a1f1dSLionel Sambuc // CHECK: ret
404*0a6a1f1dSLionel Sambuc }
405*0a6a1f1dSLionel Sambuc
destroy(C * obj)406*0a6a1f1dSLionel Sambuc void destroy(C *obj) {
407*0a6a1f1dSLionel Sambuc // CHECK-LABEL: define void @"\01?destroy@test4@@YAXPAUC@1@@Z"(%"struct.test4::C"* %obj)
408*0a6a1f1dSLionel Sambuc
409*0a6a1f1dSLionel Sambuc delete obj;
410*0a6a1f1dSLionel Sambuc // CHECK: %[[VPTR:.*]] = bitcast %"struct.test4::C"* %[[OBJ:.*]] to i8* (%"struct.test4::C"*, i32)***
411*0a6a1f1dSLionel Sambuc // CHECK: %[[VFTABLE:.*]] = load i8* (%"struct.test4::C"*, i32)*** %[[VPTR]]
412*0a6a1f1dSLionel Sambuc // CHECK: %[[VFTENTRY:.*]] = getelementptr inbounds i8* (%"struct.test4::C"*, i32)** %[[VFTABLE]], i64 0
413*0a6a1f1dSLionel Sambuc // CHECK: %[[VFUN:.*]] = load i8* (%"struct.test4::C"*, i32)** %[[VFTENTRY]]
414*0a6a1f1dSLionel Sambuc // CHECK: call x86_thiscallcc i8* %[[VFUN]](%"struct.test4::C"* %[[OBJ]], i32 1)
415*0a6a1f1dSLionel Sambuc // CHECK: ret
416*0a6a1f1dSLionel Sambuc }
417*0a6a1f1dSLionel Sambuc
418*0a6a1f1dSLionel Sambuc struct D {
419*0a6a1f1dSLionel Sambuc virtual void d();
420*0a6a1f1dSLionel Sambuc };
421*0a6a1f1dSLionel Sambuc
422*0a6a1f1dSLionel Sambuc // The first non-virtual base doesn't have a vdtor,
423*0a6a1f1dSLionel Sambuc // but "this adjustment" is not needed.
424*0a6a1f1dSLionel Sambuc struct E : D, B, virtual A {
425*0a6a1f1dSLionel Sambuc virtual ~E();
426*0a6a1f1dSLionel Sambuc };
427*0a6a1f1dSLionel Sambuc
~E()428*0a6a1f1dSLionel Sambuc E::~E() {
429*0a6a1f1dSLionel Sambuc // CHECK-LABEL: define x86_thiscallcc void @"\01??1E@test4@@UAE@XZ"(%"struct.test4::E"* %this)
430*0a6a1f1dSLionel Sambuc
431*0a6a1f1dSLionel Sambuc // In this case "this" points to the most derived class, so no GEPs needed.
432*0a6a1f1dSLionel Sambuc // CHECK-NOT: getelementptr
433*0a6a1f1dSLionel Sambuc // CHECK-NOT: bitcast
434*0a6a1f1dSLionel Sambuc // CHECK: %[[VFPTR_i8:.*]] = bitcast %"struct.test4::E"* %{{.*}} to i32 (...)***
435*0a6a1f1dSLionel Sambuc // CHECK: store i32 (...)** bitcast ([1 x i8*]* @"\01??_7E@test4@@6BD@1@@" to i32 (...)**), i32 (...)*** %[[VFPTR_i8]]
436*0a6a1f1dSLionel Sambuc foo(this);
437*0a6a1f1dSLionel Sambuc }
438*0a6a1f1dSLionel Sambuc
destroy(E * obj)439*0a6a1f1dSLionel Sambuc void destroy(E *obj) {
440*0a6a1f1dSLionel Sambuc // CHECK-LABEL: define void @"\01?destroy@test4@@YAXPAUE@1@@Z"(%"struct.test4::E"* %obj)
441*0a6a1f1dSLionel Sambuc
442*0a6a1f1dSLionel Sambuc // CHECK-NOT: getelementptr
443*0a6a1f1dSLionel Sambuc // CHECK: %[[OBJ_i8:.*]] = bitcast %"struct.test4::E"* %[[OBJ:.*]] to i8*
444*0a6a1f1dSLionel Sambuc // CHECK: %[[B_i8:.*]] = getelementptr inbounds i8* %[[OBJ_i8]], i32 4
445*0a6a1f1dSLionel Sambuc // CHECK: %[[VPTR:.*]] = bitcast i8* %[[B_i8]] to i8* (%"struct.test4::E"*, i32)***
446*0a6a1f1dSLionel Sambuc // CHECK: %[[VFTABLE:.*]] = load i8* (%"struct.test4::E"*, i32)*** %[[VPTR]]
447*0a6a1f1dSLionel Sambuc // CHECK: %[[VFTENTRY:.*]] = getelementptr inbounds i8* (%"struct.test4::E"*, i32)** %[[VFTABLE]], i64 0
448*0a6a1f1dSLionel Sambuc // CHECK: %[[VFUN:.*]] = load i8* (%"struct.test4::E"*, i32)** %[[VFTENTRY]]
449*0a6a1f1dSLionel Sambuc // CHECK: %[[OBJ_i8:.*]] = bitcast %"struct.test4::E"* %[[OBJ]] to i8*
450*0a6a1f1dSLionel Sambuc // CHECK: %[[B_i8:.*]] = getelementptr inbounds i8* %[[OBJ_i8]], i32 4
451*0a6a1f1dSLionel Sambuc // FIXME: in fact, the call should take i8* and the bitcast is redundant.
452*0a6a1f1dSLionel Sambuc // CHECK: %[[B_as_E:.*]] = bitcast i8* %[[B_i8]] to %"struct.test4::E"*
453*0a6a1f1dSLionel Sambuc // CHECK: call x86_thiscallcc i8* %[[VFUN]](%"struct.test4::E"* %[[B_as_E]], i32 1)
454*0a6a1f1dSLionel Sambuc delete obj;
455*0a6a1f1dSLionel Sambuc }
456*0a6a1f1dSLionel Sambuc
457*0a6a1f1dSLionel Sambuc }
458