blob: af930c84720bcef7ddb140a26294f97489ec825e [file] [log] [blame]
David Majnemere0e228a2015-06-11 08:12:44 +00001// RUN: %clang_cc1 -std=c++11 -fno-rtti -emit-llvm -triple=i386-pc-win32 %s -o - | FileCheck %s --check-prefix=CHECK32
2// RUN: %clang_cc1 -std=c++11 -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
David Majnemere0e228a2015-06-11 08:12:44 +000016struct B;
17
Hans Wennborg88497d62013-11-15 17:24:45 +000018struct C {
19 virtual void foo();
20 virtual int bar(int, double);
21 virtual S baz(int);
Reid Kleckner966abe72014-05-15 23:01:46 +000022 virtual S qux(U);
David Majnemerbc02d322015-03-14 06:34:41 +000023 virtual void thud(...);
David Majnemere0e228a2015-06-11 08:12:44 +000024 virtual void (B::*plugh())();
Hans Wennborg88497d62013-11-15 17:24:45 +000025};
26
27namespace {
28struct D {
29 virtual void foo();
30};
31}
32
33void f() {
34 void (C::*ptr)();
35 ptr = &C::foo;
36 ptr = &C::foo; // Don't crash trying to define the thunk twice :)
37
38 int (C::*ptr2)(int, double);
39 ptr2 = &C::bar;
40
41 S (C::*ptr3)(int);
42 ptr3 = &C::baz;
43
44 void (D::*ptr4)();
45 ptr4 = &D::foo;
46
Reid Kleckner966abe72014-05-15 23:01:46 +000047 S (C::*ptr5)(U);
48 ptr5 = &C::qux;
49
David Majnemerbc02d322015-03-14 06:34:41 +000050 void (C::*ptr6)(...);
51 ptr6 = &C::thud;
52
David Majnemere0e228a2015-06-11 08:12:44 +000053 auto ptr7 = &C::plugh;
54
Reid Kleckner966abe72014-05-15 23:01:46 +000055
Hans Wennborg88497d62013-11-15 17:24:45 +000056// CHECK32-LABEL: define void @"\01?f@@YAXXZ"()
Reid Klecknerc3473512014-08-29 21:43:29 +000057// CHECK32: store i8* bitcast (void (%struct.C*, ...)* @"\01??_9C@@$BA@AE" to i8*), i8** %ptr
58// CHECK32: store i8* bitcast (void (%struct.C*, ...)* @"\01??_9C@@$B3AE" to i8*), i8** %ptr2
59// CHECK32: store i8* bitcast (void (%struct.C*, ...)* @"\01??_9C@@$B7AE" to i8*), i8** %ptr3
60// 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 +000061// CHECK32: }
62//
63// CHECK64-LABEL: define void @"\01?f@@YAXXZ"()
Reid Klecknerc3473512014-08-29 21:43:29 +000064// CHECK64: store i8* bitcast (void (%struct.C*, ...)* @"\01??_9C@@$BA@AA" to i8*), i8** %ptr
65// CHECK64: store i8* bitcast (void (%struct.C*, ...)* @"\01??_9C@@$B7AA" to i8*), i8** %ptr2
66// CHECK64: store i8* bitcast (void (%struct.C*, ...)* @"\01??_9C@@$BBA@AA" to i8*), i8** %ptr3
67// 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 +000068// CHECK64: }
69}
70
71
72// Thunk for calling the 1st virtual function in C with no parameters.
Reid Klecknerc3473512014-08-29 21:43:29 +000073// CHECK32-LABEL: define linkonce_odr x86_thiscallcc void @"\01??_9C@@$BA@AE"(%struct.C* %this, ...)
Reid Kleckner9da94482015-01-21 22:18:17 +000074// CHECK32: #[[ATTR:[0-9]+]]
Reid Kleckner76176f22014-08-29 22:06:20 +000075// CHECK32-NOT: unnamed_addr
David Majnemer8c9cdb62015-01-21 01:21:31 +000076// CHECK32: comdat
David Blaikie218b7832015-02-27 19:18:17 +000077// CHECK32: [[VPTR:%.*]] = getelementptr inbounds void (%struct.C*, ...)*, void (%struct.C*, ...)** %{{.*}}, i64 0
David Blaikiea953f282015-02-27 21:19:58 +000078// CHECK32: [[CALLEE:%.*]] = load void (%struct.C*, ...)*, void (%struct.C*, ...)** [[VPTR]]
David Blaikied6c88ec2015-04-16 23:25:00 +000079// CHECK32: musttail call x86_thiscallcc void (%struct.C*, ...) [[CALLEE]](%struct.C* %{{.*}}, ...)
Reid Klecknerc3473512014-08-29 21:43:29 +000080// CHECK32-NEXT: ret void
Hans Wennborg88497d62013-11-15 17:24:45 +000081// CHECK32: }
82//
Reid Klecknerc3473512014-08-29 21:43:29 +000083// CHECK64-LABEL: define linkonce_odr void @"\01??_9C@@$BA@AA"(%struct.C* %this, ...)
Reid Kleckner9da94482015-01-21 22:18:17 +000084// CHECK64: #[[ATTR:[0-9]+]]
Reid Kleckner76176f22014-08-29 22:06:20 +000085// CHECK64-NOT: unnamed_addr
David Majnemer8c9cdb62015-01-21 01:21:31 +000086// CHECK64: comdat
David Blaikie218b7832015-02-27 19:18:17 +000087// CHECK64: [[VPTR:%.*]] = getelementptr inbounds void (%struct.C*, ...)*, void (%struct.C*, ...)** %{{.*}}, i64 0
David Blaikiea953f282015-02-27 21:19:58 +000088// CHECK64: [[CALLEE:%.*]] = load void (%struct.C*, ...)*, void (%struct.C*, ...)** [[VPTR]]
David Blaikied6c88ec2015-04-16 23:25:00 +000089// CHECK64: musttail call void (%struct.C*, ...) [[CALLEE]](%struct.C* %{{.*}}, ...)
Reid Klecknerc3473512014-08-29 21:43:29 +000090// CHECK64-NEXT: ret void
Hans Wennborg88497d62013-11-15 17:24:45 +000091// CHECK64: }
92
93// Thunk for calling the 2nd virtual function in C, taking int and double as parameters, returning int.
Reid Kleckner9da94482015-01-21 22:18:17 +000094// CHECK32-LABEL: define linkonce_odr x86_thiscallcc void @"\01??_9C@@$B3AE"(%struct.C* %this, ...)
95// CHECK32: #[[ATTR]] comdat
David Blaikie218b7832015-02-27 19:18:17 +000096// CHECK32: [[VPTR:%.*]] = getelementptr inbounds void (%struct.C*, ...)*, void (%struct.C*, ...)** %{{.*}}, i64 1
David Blaikiea953f282015-02-27 21:19:58 +000097// CHECK32: [[CALLEE:%.*]] = load void (%struct.C*, ...)*, void (%struct.C*, ...)** [[VPTR]]
David Blaikied6c88ec2015-04-16 23:25:00 +000098// CHECK32: musttail call x86_thiscallcc void (%struct.C*, ...) [[CALLEE]](%struct.C* %{{.*}}, ...)
Reid Klecknerc3473512014-08-29 21:43:29 +000099// CHECK32-NEXT: ret void
Hans Wennborg88497d62013-11-15 17:24:45 +0000100// CHECK32: }
101//
Reid Kleckner9da94482015-01-21 22:18:17 +0000102// CHECK64-LABEL: define linkonce_odr void @"\01??_9C@@$B7AA"(%struct.C* %this, ...)
103// CHECK64: #[[ATTR]] comdat
David Blaikie218b7832015-02-27 19:18:17 +0000104// CHECK64: [[VPTR:%.*]] = getelementptr inbounds void (%struct.C*, ...)*, void (%struct.C*, ...)** %{{.*}}, i64 1
David Blaikiea953f282015-02-27 21:19:58 +0000105// CHECK64: [[CALLEE:%.*]] = load void (%struct.C*, ...)*, void (%struct.C*, ...)** [[VPTR]]
David Blaikied6c88ec2015-04-16 23:25:00 +0000106// CHECK64: musttail call void (%struct.C*, ...) [[CALLEE]](%struct.C* %{{.*}}, ...)
Reid Klecknerc3473512014-08-29 21:43:29 +0000107// CHECK64-NEXT: ret void
Hans Wennborg88497d62013-11-15 17:24:45 +0000108// CHECK64: }
109
110// Thunk for calling the 3rd virtual function in C, taking an int parameter, returning a struct.
Reid Kleckner9da94482015-01-21 22:18:17 +0000111// CHECK32-LABEL: define linkonce_odr x86_thiscallcc void @"\01??_9C@@$B7AE"(%struct.C* %this, ...)
112// CHECK32: #[[ATTR]] comdat
David Blaikie218b7832015-02-27 19:18:17 +0000113// CHECK32: [[VPTR:%.*]] = getelementptr inbounds void (%struct.C*, ...)*, void (%struct.C*, ...)** %{{.*}}, i64 2
David Blaikiea953f282015-02-27 21:19:58 +0000114// CHECK32: [[CALLEE:%.*]] = load void (%struct.C*, ...)*, void (%struct.C*, ...)** [[VPTR]]
David Blaikied6c88ec2015-04-16 23:25:00 +0000115// CHECK32: musttail call x86_thiscallcc void (%struct.C*, ...) [[CALLEE]](%struct.C* %{{.*}}, ...)
Reid Klecknerc3473512014-08-29 21:43:29 +0000116// CHECK32-NEXT: ret void
Hans Wennborg88497d62013-11-15 17:24:45 +0000117// CHECK32: }
118//
Reid Kleckner9da94482015-01-21 22:18:17 +0000119// CHECK64-LABEL: define linkonce_odr void @"\01??_9C@@$BBA@AA"(%struct.C* %this, ...)
120// CHECK64: #[[ATTR]] comdat
David Blaikie218b7832015-02-27 19:18:17 +0000121// CHECK64: [[VPTR:%.*]] = getelementptr inbounds void (%struct.C*, ...)*, void (%struct.C*, ...)** %{{.*}}, i64 2
David Blaikiea953f282015-02-27 21:19:58 +0000122// CHECK64: [[CALLEE:%.*]] = load void (%struct.C*, ...)*, void (%struct.C*, ...)** [[VPTR]]
David Blaikied6c88ec2015-04-16 23:25:00 +0000123// CHECK64: musttail call void (%struct.C*, ...) [[CALLEE]](%struct.C* %{{.*}}, ...)
Reid Klecknerc3473512014-08-29 21:43:29 +0000124// CHECK64-NEXT: ret void
Hans Wennborg88497d62013-11-15 17:24:45 +0000125// CHECK64: }
126
127// Thunk for calling the virtual function in internal class D.
Reid Klecknerc3473512014-08-29 21:43:29 +0000128// CHECK32-LABEL: define internal x86_thiscallcc void @"\01??_9D@?A@@$BA@AE"(%"struct.(anonymous namespace)::D"* %this, ...)
Reid Kleckner9da94482015-01-21 22:18:17 +0000129// CHECK32: #[[ATTR]]
David Blaikie218b7832015-02-27 19:18:17 +0000130// CHECK32: [[VPTR:%.*]] = getelementptr inbounds void (%"struct.(anonymous namespace)::D"*, ...)*, void (%"struct.(anonymous namespace)::D"*, ...)** %{{.*}}, i64 0
David Blaikiea953f282015-02-27 21:19:58 +0000131// CHECK32: [[CALLEE:%.*]] = load void (%"struct.(anonymous namespace)::D"*, ...)*, void (%"struct.(anonymous namespace)::D"*, ...)** [[VPTR]]
David Blaikied6c88ec2015-04-16 23:25:00 +0000132// CHECK32: musttail call x86_thiscallcc void (%"struct.(anonymous namespace)::D"*, ...) [[CALLEE]](%"struct.(anonymous namespace)::D"* %{{.*}}, ...)
Reid Klecknerc3473512014-08-29 21:43:29 +0000133// CHECK32-NEXT: ret void
Hans Wennborg88497d62013-11-15 17:24:45 +0000134// CHECK32: }
135//
Reid Klecknerc3473512014-08-29 21:43:29 +0000136// CHECK64-LABEL: define internal void @"\01??_9D@?A@@$BA@AA"(%"struct.(anonymous namespace)::D"* %this, ...)
Reid Kleckner9da94482015-01-21 22:18:17 +0000137// CHECK64: #[[ATTR]]
David Blaikie218b7832015-02-27 19:18:17 +0000138// CHECK64: [[VPTR:%.*]] = getelementptr inbounds void (%"struct.(anonymous namespace)::D"*, ...)*, void (%"struct.(anonymous namespace)::D"*, ...)** %{{.*}}, i64 0
David Blaikiea953f282015-02-27 21:19:58 +0000139// CHECK64: [[CALLEE:%.*]] = load void (%"struct.(anonymous namespace)::D"*, ...)*, void (%"struct.(anonymous namespace)::D"*, ...)** [[VPTR]]
David Blaikied6c88ec2015-04-16 23:25:00 +0000140// CHECK64: musttail call void (%"struct.(anonymous namespace)::D"*, ...) [[CALLEE]](%"struct.(anonymous namespace)::D"* %{{.*}}, ...)
Reid Klecknerc3473512014-08-29 21:43:29 +0000141// CHECK64-NEXT: ret void
Reid Kleckner966abe72014-05-15 23:01:46 +0000142// CHECK64: }
143
Reid Klecknerab2090d2014-07-26 01:34:32 +0000144// Thunk for calling the fourth virtual function in C, taking a struct parameter
145// and returning a struct.
David Majnemer8c9cdb62015-01-21 01:21:31 +0000146// CHECK32-LABEL: define linkonce_odr x86_thiscallcc void @"\01??_9C@@$BM@AE"(%struct.C* %this, ...) {{.*}} comdat
David Blaikie218b7832015-02-27 19:18:17 +0000147// CHECK32: [[VPTR:%.*]] = getelementptr inbounds void (%struct.C*, ...)*, void (%struct.C*, ...)** %{{.*}}, i64 3
David Blaikiea953f282015-02-27 21:19:58 +0000148// CHECK32: [[CALLEE:%.*]] = load void (%struct.C*, ...)*, void (%struct.C*, ...)** [[VPTR]]
David Blaikied6c88ec2015-04-16 23:25:00 +0000149// CHECK32: musttail call x86_thiscallcc void (%struct.C*, ...) [[CALLEE]](%struct.C* %{{.*}}, ...)
Reid Klecknerc3473512014-08-29 21:43:29 +0000150// CHECK32-NEXT: ret void
Reid Kleckner966abe72014-05-15 23:01:46 +0000151// CHECK32: }
152//
David Majnemer8c9cdb62015-01-21 01:21:31 +0000153// CHECK64-LABEL: define linkonce_odr void @"\01??_9C@@$BBI@AA"(%struct.C* %this, ...) {{.*}} comdat
David Blaikie218b7832015-02-27 19:18:17 +0000154// CHECK64: [[VPTR:%.*]] = getelementptr inbounds void (%struct.C*, ...)*, void (%struct.C*, ...)** %{{.*}}, i64 3
David Blaikiea953f282015-02-27 21:19:58 +0000155// CHECK64: [[CALLEE:%.*]] = load void (%struct.C*, ...)*, void (%struct.C*, ...)** [[VPTR]]
David Blaikied6c88ec2015-04-16 23:25:00 +0000156// CHECK64: musttail call void (%struct.C*, ...) [[CALLEE]](%struct.C* %{{.*}}, ...)
Reid Klecknerab2090d2014-07-26 01:34:32 +0000157// CHECK64: ret void
158// CHECK64: }
Reid Kleckner9da94482015-01-21 22:18:17 +0000159
David Majnemerbc02d322015-03-14 06:34:41 +0000160// Thunk for calling the fifth virtual function in C which uses the __cdecl calling convention.
161// CHECK32-LABEL: define linkonce_odr void @"\01??_9C@@$BBA@AA"(%struct.C* %this, ...) {{.*}} comdat align 2 {
162// CHECK32: [[VPTR:%.*]] = getelementptr inbounds void (%struct.C*, ...)*, void (%struct.C*, ...)** %{{.*}}, i64 4
163// CHECK32: [[CALLEE:%.*]] = load void (%struct.C*, ...)*, void (%struct.C*, ...)** [[VPTR]]
David Blaikied6c88ec2015-04-16 23:25:00 +0000164// CHECK32: musttail call void (%struct.C*, ...) [[CALLEE]](%struct.C* %{{.*}}, ...)
David Majnemerbc02d322015-03-14 06:34:41 +0000165// CHECK32: ret void
166// CHECK32: }
167//
168// CHECK64-LABEL: define linkonce_odr void @"\01??_9C@@$BCA@AA"(%struct.C* %this, ...) {{.*}} comdat align 2 {
169// CHECK64: [[VPTR:%.*]] = getelementptr inbounds void (%struct.C*, ...)*, void (%struct.C*, ...)** %{{.*}}, i64 4
170// CHECK64: [[CALLEE:%.*]] = load void (%struct.C*, ...)*, void (%struct.C*, ...)** [[VPTR]]
David Blaikied6c88ec2015-04-16 23:25:00 +0000171// CHECK64: musttail call void (%struct.C*, ...) [[CALLEE]](%struct.C* %{{.*}}, ...)
David Majnemerbc02d322015-03-14 06:34:41 +0000172// CHECK64: ret void
173// CHECK64: }
174
David Majnemere0e228a2015-06-11 08:12:44 +0000175// CHECK32: define linkonce_odr x86_thiscallcc void @"\01??_9C@@$BBE@AE"(%struct.C* %this, ...) {{.*}} comdat align 2 {
176// CHECK32: [[VPTR:%.*]] = getelementptr inbounds void (%struct.C*, ...)*, void (%struct.C*, ...)** %{{.*}}, i64 5
177// CHECK32: [[CALLEE:%.*]] = load void (%struct.C*, ...)*, void (%struct.C*, ...)** [[VPTR]]
178// CHECK32: musttail call x86_thiscallcc void (%struct.C*, ...) [[CALLEE]](%struct.C* %{{.*}}, ...)
179// CHECK32: ret void
180// CHECK32: }
181
182// CHECK64: define linkonce_odr void @"\01??_9C@@$BCI@AA"(%struct.C* %this, ...) {{.*}} comdat align 2 {
183// CHECK64: [[VPTR:%.*]] = getelementptr inbounds void (%struct.C*, ...)*, void (%struct.C*, ...)** %{{.*}}, i64 5
184// CHECK64: [[CALLEE:%.*]] = load void (%struct.C*, ...)*, void (%struct.C*, ...)** [[VPTR]]
185// CHECK64: musttail call void (%struct.C*, ...) [[CALLEE]](%struct.C* %{{.*}}, ...)
186// CHECK64: ret void
187// CHECK64: }
188
Reid Kleckner9da94482015-01-21 22:18:17 +0000189// CHECK32: #[[ATTR]] = {{{.*}}"thunk"{{.*}}}