blob: ffad7e70ce5d1ccb36152f70e6dbef0a1f1b0dbf [file] [log] [blame]
Hans Wennborgc9bd88e2014-01-14 19:35:09 +00001// RUN: %clang_cc1 -fno-rtti -emit-llvm -triple=i386-pc-win32 %s -o - | FileCheck %s --check-prefix=CHECK32
2// RUN: %clang_cc1 -fno-rtti -emit-llvm -triple=x86_64-pc-win32 %s -o - | FileCheck %s --check-prefix=CHECK64
Hans Wennborg88497d62013-11-15 17:24:45 +00003
4struct S {
5 int x, y, z;
6};
7
Reid Kleckner966abe72014-05-15 23:01:46 +00008// U is not trivially copyable, and requires inalloca to pass by value.
9struct U {
10 int u;
11 U();
12 ~U();
13 U(const U &);
14};
15
Hans Wennborg88497d62013-11-15 17:24:45 +000016struct C {
17 virtual void foo();
18 virtual int bar(int, double);
19 virtual S baz(int);
Reid Kleckner966abe72014-05-15 23:01:46 +000020 virtual S qux(U);
Reid Klecknerab2090d2014-07-26 01:34:32 +000021 virtual S __fastcall zed(U);
Hans Wennborg88497d62013-11-15 17:24:45 +000022};
23
24namespace {
25struct D {
26 virtual void foo();
27};
28}
29
30void f() {
31 void (C::*ptr)();
32 ptr = &C::foo;
33 ptr = &C::foo; // Don't crash trying to define the thunk twice :)
34
35 int (C::*ptr2)(int, double);
36 ptr2 = &C::bar;
37
38 S (C::*ptr3)(int);
39 ptr3 = &C::baz;
40
41 void (D::*ptr4)();
42 ptr4 = &D::foo;
43
Reid Kleckner966abe72014-05-15 23:01:46 +000044 S (C::*ptr5)(U);
45 ptr5 = &C::qux;
46
Reid Klecknerab2090d2014-07-26 01:34:32 +000047 S (__fastcall C::*ptr6)(U);
48 ptr6 = &C::zed;
49
Reid Kleckner966abe72014-05-15 23:01:46 +000050
Hans Wennborg88497d62013-11-15 17:24:45 +000051// CHECK32-LABEL: define void @"\01?f@@YAXXZ"()
52// CHECK32: store i8* bitcast (void (%struct.C*)* @"\01??_9C@@$BA@AE" to i8*), i8** %ptr
53// CHECK32: store i8* bitcast (i32 (%struct.C*, i32, double)* @"\01??_9C@@$B3AE" to i8*), i8** %ptr2
Reid Kleckner37abaca2014-05-09 22:46:15 +000054// CHECK32: store i8* bitcast (void (%struct.C*, %struct.S*, i32)* @"\01??_9C@@$B7AE" to i8*), i8** %ptr3
David Blaikieabe1a392014-04-02 05:58:29 +000055// CHECK32: store i8* bitcast (void (%"struct.(anonymous namespace)::D"*)* @"\01??_9D@?A@@$BA@AE" to i8*), i8** %ptr4
Hans Wennborg88497d62013-11-15 17:24:45 +000056// CHECK32: }
57//
58// CHECK64-LABEL: define void @"\01?f@@YAXXZ"()
59// CHECK64: store i8* bitcast (void (%struct.C*)* @"\01??_9C@@$BA@AA" to i8*), i8** %ptr
60// CHECK64: store i8* bitcast (i32 (%struct.C*, i32, double)* @"\01??_9C@@$B7AA" to i8*), i8** %ptr2
Reid Kleckner37abaca2014-05-09 22:46:15 +000061// CHECK64: store i8* bitcast (void (%struct.C*, %struct.S*, i32)* @"\01??_9C@@$BBA@AA" to i8*), i8** %ptr3
David Blaikieabe1a392014-04-02 05:58:29 +000062// CHECK64: store i8* bitcast (void (%"struct.(anonymous namespace)::D"*)* @"\01??_9D@?A@@$BA@AA" to i8*), i8** %ptr
Hans Wennborg88497d62013-11-15 17:24:45 +000063// CHECK64: }
64}
65
66
67// Thunk for calling the 1st virtual function in C with no parameters.
68// CHECK32-LABEL: define linkonce_odr x86_thiscallcc void @"\01??_9C@@$BA@AE"(%struct.C* %this) unnamed_addr
69// CHECK32: [[VPTR:%.*]] = getelementptr inbounds void (%struct.C*)** %{{.*}}, i64 0
70// CHECK32: [[CALLEE:%.*]] = load void (%struct.C*)** [[VPTR]]
Reid Klecknerab2090d2014-07-26 01:34:32 +000071// CHECK32: call x86_thiscallcc void [[CALLEE]](%struct.C* %{{.*}})
Hans Wennborg88497d62013-11-15 17:24:45 +000072// CHECK32: ret void
73// CHECK32: }
74//
75// CHECK64-LABEL: define linkonce_odr void @"\01??_9C@@$BA@AA"(%struct.C* %this) unnamed_addr
76// CHECK64: [[VPTR:%.*]] = getelementptr inbounds void (%struct.C*)** %{{.*}}, i64 0
77// CHECK64: [[CALLEE:%.*]] = load void (%struct.C*)** [[VPTR]]
Reid Klecknerab2090d2014-07-26 01:34:32 +000078// CHECK64: call void [[CALLEE]](%struct.C* %{{.*}})
Hans Wennborg88497d62013-11-15 17:24:45 +000079// CHECK64: ret void
80// CHECK64: }
81
82// Thunk for calling the 2nd virtual function in C, taking int and double as parameters, returning int.
83// CHECK32-LABEL: define linkonce_odr x86_thiscallcc i32 @"\01??_9C@@$B3AE"(%struct.C* %this, i32, double) unnamed_addr
84// CHECK32: [[VPTR:%.*]] = getelementptr inbounds i32 (%struct.C*, i32, double)** %{{.*}}, i64 1
85// CHECK32: [[CALLEE:%.*]] = load i32 (%struct.C*, i32, double)** [[VPTR]]
Reid Klecknerab2090d2014-07-26 01:34:32 +000086// CHECK32: [[CALL:%.*]] = call x86_thiscallcc i32 [[CALLEE]](%struct.C* %{{.*}}, i32 %{{.*}}, double %{{.*}})
Hans Wennborg88497d62013-11-15 17:24:45 +000087// CHECK32: ret i32 [[CALL]]
88// CHECK32: }
89//
90// CHECK64-LABEL: define linkonce_odr i32 @"\01??_9C@@$B7AA"(%struct.C* %this, i32, double) unnamed_addr
91// CHECK64: [[VPTR:%.*]] = getelementptr inbounds i32 (%struct.C*, i32, double)** %{{.*}}, i64 1
92// CHECK64: [[CALLEE:%.*]] = load i32 (%struct.C*, i32, double)** [[VPTR]]
Reid Klecknerab2090d2014-07-26 01:34:32 +000093// CHECK64: [[CALL:%.*]] = call i32 [[CALLEE]](%struct.C* %{{.*}}, i32 %{{.*}}, double %{{.*}})
Hans Wennborg88497d62013-11-15 17:24:45 +000094// CHECK64: ret i32 [[CALL]]
95// CHECK64: }
96
97// Thunk for calling the 3rd virtual function in C, taking an int parameter, returning a struct.
Reid Kleckner37abaca2014-05-09 22:46:15 +000098// CHECK32-LABEL: define linkonce_odr x86_thiscallcc void @"\01??_9C@@$B7AE"(%struct.C* %this, %struct.S* noalias sret %agg.result, i32) unnamed_addr
99// CHECK32: [[VPTR:%.*]] = getelementptr inbounds void (%struct.C*, %struct.S*, i32)** %{{.*}}, i64 2
100// CHECK32: [[CALLEE:%.*]] = load void (%struct.C*, %struct.S*, i32)** [[VPTR]]
Reid Klecknerab2090d2014-07-26 01:34:32 +0000101// CHECK32: call x86_thiscallcc void [[CALLEE]](%struct.C* %{{.*}}, %struct.S* sret %agg.result, i32 %{{.*}})
Hans Wennborg88497d62013-11-15 17:24:45 +0000102// CHECK32: ret void
103// CHECK32: }
104//
Reid Kleckner37abaca2014-05-09 22:46:15 +0000105// CHECK64-LABEL: define linkonce_odr void @"\01??_9C@@$BBA@AA"(%struct.C* %this, %struct.S* noalias sret %agg.result, i32) unnamed_addr
106// CHECK64: [[VPTR:%.*]] = getelementptr inbounds void (%struct.C*, %struct.S*, i32)** %{{.*}}, i64 2
107// CHECK64: [[CALLEE:%.*]] = load void (%struct.C*, %struct.S*, i32)** [[VPTR]]
Reid Klecknerab2090d2014-07-26 01:34:32 +0000108// CHECK64: call void [[CALLEE]](%struct.C* %{{.*}}, %struct.S* sret %agg.result, i32 %{{.*}})
Hans Wennborg88497d62013-11-15 17:24:45 +0000109// CHECK64: ret void
110// CHECK64: }
111
112// Thunk for calling the virtual function in internal class D.
David Blaikieabe1a392014-04-02 05:58:29 +0000113// CHECK32-LABEL: define internal x86_thiscallcc void @"\01??_9D@?A@@$BA@AE"(%"struct.(anonymous namespace)::D"* %this) unnamed_addr
114// CHECK32: [[VPTR:%.*]] = getelementptr inbounds void (%"struct.(anonymous namespace)::D"*)** %{{.*}}, i64 0
115// CHECK32: [[CALLEE:%.*]] = load void (%"struct.(anonymous namespace)::D"*)** [[VPTR]]
Reid Klecknerab2090d2014-07-26 01:34:32 +0000116// CHECK32: call x86_thiscallcc void [[CALLEE]](%"struct.(anonymous namespace)::D"* %{{.*}})
Hans Wennborg88497d62013-11-15 17:24:45 +0000117// CHECK32: ret void
118// CHECK32: }
119//
David Blaikieabe1a392014-04-02 05:58:29 +0000120// CHECK64-LABEL: define internal void @"\01??_9D@?A@@$BA@AA"(%"struct.(anonymous namespace)::D"* %this) unnamed_addr
121// CHECK64: [[VPTR:%.*]] = getelementptr inbounds void (%"struct.(anonymous namespace)::D"*)** %{{.*}}, i64 0
122// CHECK64: [[CALLEE:%.*]] = load void (%"struct.(anonymous namespace)::D"*)** [[VPTR]]
Reid Klecknerab2090d2014-07-26 01:34:32 +0000123// CHECK64: call void [[CALLEE]](%"struct.(anonymous namespace)::D"* %{{.*}})
Reid Kleckner966abe72014-05-15 23:01:46 +0000124// CHECK64: ret void
125// CHECK64: }
126
Reid Klecknerab2090d2014-07-26 01:34:32 +0000127// Thunk for calling the fourth virtual function in C, taking a struct parameter
128// and returning a struct.
Reid Kleckner966abe72014-05-15 23:01:46 +0000129// CHECK32-LABEL: define linkonce_odr x86_thiscallcc %struct.S* @"\01??_9C@@$BM@AE"(%struct.C* %this, <{ %struct.S*, %struct.U }>* inalloca) unnamed_addr
130// CHECK32: [[VPTR:%.*]] = getelementptr inbounds %struct.S* (%struct.C*, <{ %struct.S*, %struct.U }>*)** %{{.*}}, i64 3
131// CHECK32: [[CALLEE:%.*]] = load %struct.S* (%struct.C*, <{ %struct.S*, %struct.U }>*)** [[VPTR]]
Reid Klecknerab2090d2014-07-26 01:34:32 +0000132// CHECK32: [[CALL:%.*]] = musttail call x86_thiscallcc %struct.S* [[CALLEE]](%struct.C* %{{.*}}, <{ %struct.S*, %struct.U }>* inalloca %{{.*}})
133// CHECK32-NEXT: ret %struct.S* [[CALL]]
Reid Kleckner966abe72014-05-15 23:01:46 +0000134// CHECK32: }
135//
136// CHECK64-LABEL: define linkonce_odr void @"\01??_9C@@$BBI@AA"(%struct.C* %this, %struct.S* noalias sret %agg.result, %struct.U*) unnamed_addr
137// CHECK64: [[VPTR:%.*]] = getelementptr inbounds void (%struct.C*, %struct.S*, %struct.U*)** %{{.*}}, i64 3
138// CHECK64: [[CALLEE:%.*]] = load void (%struct.C*, %struct.S*, %struct.U*)** [[VPTR]]
Reid Klecknerab2090d2014-07-26 01:34:32 +0000139// CHECK64: call void [[CALLEE]](%struct.C* %{{.*}}, %struct.S* sret %agg.result, %struct.U* %{{.*}})
140// CHECK64: ret void
141// CHECK64: }
142
143// Thunk for calling the fifth virtual function in C, taking a struct parameter
144// and returning a struct.
145// CHECK32-LABEL: define linkonce_odr x86_fastcallcc void @"\01??_9C@@$BBA@AE"(%struct.C* inreg %this, %struct.S* inreg noalias sret %agg.result, <{ %struct.U }>* inalloca) unnamed_addr
146// CHECK32: [[VPTR:%.*]] = getelementptr inbounds void (%struct.C*, %struct.S*, <{ %struct.U }>*)** %{{.*}}, i64 4
147// CHECK32: [[CALLEE:%.*]] = load void (%struct.C*, %struct.S*, <{ %struct.U }>*)** [[VPTR]]
148// CHECK32: musttail call x86_fastcallcc void [[CALLEE]](%struct.C* inreg %{{.*}}, %struct.S* inreg sret %{{.*}}, <{ %struct.U }>* inalloca %{{.*}})
149// CHECK32-NEXT: ret void
150// CHECK32: }
151//
152// CHECK64-LABEL: define linkonce_odr void @"\01??_9C@@$BCA@AA"(%struct.C* %this, %struct.S* noalias sret %agg.result, %struct.U*) unnamed_addr
153// CHECK64: [[VPTR:%.*]] = getelementptr inbounds void (%struct.C*, %struct.S*, %struct.U*)** %{{.*}}, i64 4
154// CHECK64: [[CALLEE:%.*]] = load void (%struct.C*, %struct.S*, %struct.U*)** [[VPTR]]
155// CHECK64: call void [[CALLEE]](%struct.C* %{{.*}}, %struct.S* sret %agg.result, %struct.U* %{{.*}})
Hans Wennborg88497d62013-11-15 17:24:45 +0000156// CHECK64: ret void
157// CHECK64: }