1 // 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
3 
4 struct S {
5   int x, y, z;
6 };
7 
8 // U is not trivially copyable, and requires inalloca to pass by value.
9 struct U {
10   int u;
11   U();
12   ~U();
13   U(const U &);
14 };
15 
16 struct C {
17   virtual void foo();
18   virtual int bar(int, double);
19   virtual S baz(int);
20   virtual S qux(U);
21 };
22 
23 namespace {
24 struct D {
25   virtual void foo();
26 };
27 }
28 
f()29 void f() {
30   void (C::*ptr)();
31   ptr = &C::foo;
32   ptr = &C::foo; // Don't crash trying to define the thunk twice :)
33 
34   int (C::*ptr2)(int, double);
35   ptr2 = &C::bar;
36 
37   S (C::*ptr3)(int);
38   ptr3 = &C::baz;
39 
40   void (D::*ptr4)();
41   ptr4 = &D::foo;
42 
43   S (C::*ptr5)(U);
44   ptr5 = &C::qux;
45 
46 
47 // CHECK32-LABEL: define void @"\01?f@@YAXXZ"()
48 // CHECK32: store i8* bitcast (void (%struct.C*, ...)* @"\01??_9C@@$BA@AE" to i8*), i8** %ptr
49 // CHECK32: store i8* bitcast (void (%struct.C*, ...)* @"\01??_9C@@$B3AE" to i8*), i8** %ptr2
50 // CHECK32: store i8* bitcast (void (%struct.C*, ...)* @"\01??_9C@@$B7AE" to i8*), i8** %ptr3
51 // CHECK32: store i8* bitcast (void (%"struct.(anonymous namespace)::D"*, ...)* @"\01??_9D@?A@@$BA@AE" to i8*), i8** %ptr4
52 // CHECK32: }
53 //
54 // CHECK64-LABEL: define void @"\01?f@@YAXXZ"()
55 // CHECK64: store i8* bitcast (void (%struct.C*, ...)* @"\01??_9C@@$BA@AA" to i8*), i8** %ptr
56 // CHECK64: store i8* bitcast (void (%struct.C*, ...)* @"\01??_9C@@$B7AA" to i8*), i8** %ptr2
57 // CHECK64: store i8* bitcast (void (%struct.C*, ...)* @"\01??_9C@@$BBA@AA" to i8*), i8** %ptr3
58 // CHECK64: store i8* bitcast (void (%"struct.(anonymous namespace)::D"*, ...)* @"\01??_9D@?A@@$BA@AA" to i8*), i8** %ptr
59 // CHECK64: }
60 }
61 
62 
63 // Thunk for calling the 1st virtual function in C with no parameters.
64 // CHECK32-LABEL: define linkonce_odr x86_thiscallcc void @"\01??_9C@@$BA@AE"(%struct.C* %this, ...)
65 // CHECK32: #[[ATTR:[0-9]+]]
66 // CHECK32-NOT:             unnamed_addr
67 // CHECK32: [[VPTR:%.*]] = getelementptr inbounds void (%struct.C*, ...)** %{{.*}}, i64 0
68 // CHECK32: [[CALLEE:%.*]] = load void (%struct.C*, ...)** [[VPTR]]
69 // CHECK32: musttail call x86_thiscallcc void (%struct.C*, ...)* [[CALLEE]](%struct.C* %{{.*}}, ...)
70 // CHECK32-NEXT: ret void
71 // CHECK32: }
72 //
73 // CHECK64-LABEL: define linkonce_odr void @"\01??_9C@@$BA@AA"(%struct.C* %this, ...)
74 // CHECK64: #[[ATTR:[0-9]+]]
75 // CHECK64-NOT:             unnamed_addr
76 // CHECK64: [[VPTR:%.*]] = getelementptr inbounds void (%struct.C*, ...)** %{{.*}}, i64 0
77 // CHECK64: [[CALLEE:%.*]] = load void (%struct.C*, ...)** [[VPTR]]
78 // CHECK64: musttail call void (%struct.C*, ...)* [[CALLEE]](%struct.C* %{{.*}}, ...)
79 // CHECK64-NEXT: 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 void @"\01??_9C@@$B3AE"(%struct.C* %this, ...)
84 // CHECK32: #[[ATTR]]
85 // CHECK32: [[VPTR:%.*]] = getelementptr inbounds void (%struct.C*, ...)** %{{.*}}, i64 1
86 // CHECK32: [[CALLEE:%.*]] = load void (%struct.C*, ...)** [[VPTR]]
87 // CHECK32: musttail call x86_thiscallcc void (%struct.C*, ...)* [[CALLEE]](%struct.C* %{{.*}}, ...)
88 // CHECK32-NEXT: ret void
89 // CHECK32: }
90 //
91 // CHECK64-LABEL: define linkonce_odr void @"\01??_9C@@$B7AA"(%struct.C* %this, ...)
92 // CHECK64: #[[ATTR]]
93 // CHECK64: [[VPTR:%.*]] = getelementptr inbounds void (%struct.C*, ...)** %{{.*}}, i64 1
94 // CHECK64: [[CALLEE:%.*]] = load void (%struct.C*, ...)** [[VPTR]]
95 // CHECK64: musttail call void (%struct.C*, ...)* [[CALLEE]](%struct.C* %{{.*}}, ...)
96 // CHECK64-NEXT: ret void
97 // CHECK64: }
98 
99 // Thunk for calling the 3rd virtual function in C, taking an int parameter, returning a struct.
100 // CHECK32-LABEL: define linkonce_odr x86_thiscallcc void @"\01??_9C@@$B7AE"(%struct.C* %this, ...)
101 // CHECK32: #[[ATTR]]
102 // CHECK32: [[VPTR:%.*]] = getelementptr inbounds void (%struct.C*, ...)** %{{.*}}, i64 2
103 // CHECK32: [[CALLEE:%.*]] = load void (%struct.C*, ...)** [[VPTR]]
104 // CHECK32: musttail call x86_thiscallcc void (%struct.C*, ...)* [[CALLEE]](%struct.C* %{{.*}}, ...)
105 // CHECK32-NEXT: ret void
106 // CHECK32: }
107 //
108 // CHECK64-LABEL: define linkonce_odr void @"\01??_9C@@$BBA@AA"(%struct.C* %this, ...)
109 // CHECK64: #[[ATTR]]
110 // CHECK64: [[VPTR:%.*]] = getelementptr inbounds void (%struct.C*, ...)** %{{.*}}, i64 2
111 // CHECK64: [[CALLEE:%.*]] = load void (%struct.C*, ...)** [[VPTR]]
112 // CHECK64: musttail call void (%struct.C*, ...)* [[CALLEE]](%struct.C* %{{.*}}, ...)
113 // CHECK64-NEXT: ret void
114 // CHECK64: }
115 
116 // Thunk for calling the virtual function in internal class D.
117 // CHECK32-LABEL: define internal x86_thiscallcc void @"\01??_9D@?A@@$BA@AE"(%"struct.(anonymous namespace)::D"* %this, ...)
118 // CHECK32: #[[ATTR]]
119 // CHECK32: [[VPTR:%.*]] = getelementptr inbounds void (%"struct.(anonymous namespace)::D"*, ...)** %{{.*}}, i64 0
120 // CHECK32: [[CALLEE:%.*]] = load void (%"struct.(anonymous namespace)::D"*, ...)** [[VPTR]]
121 // CHECK32: musttail call x86_thiscallcc void (%"struct.(anonymous namespace)::D"*, ...)* [[CALLEE]](%"struct.(anonymous namespace)::D"* %{{.*}}, ...)
122 // CHECK32-NEXT: ret void
123 // CHECK32: }
124 //
125 // CHECK64-LABEL: define internal void @"\01??_9D@?A@@$BA@AA"(%"struct.(anonymous namespace)::D"* %this, ...)
126 // CHECK64: #[[ATTR]]
127 // CHECK64: [[VPTR:%.*]] = getelementptr inbounds void (%"struct.(anonymous namespace)::D"*, ...)** %{{.*}}, i64 0
128 // CHECK64: [[CALLEE:%.*]] = load void (%"struct.(anonymous namespace)::D"*, ...)** [[VPTR]]
129 // CHECK64: musttail call void (%"struct.(anonymous namespace)::D"*, ...)* [[CALLEE]](%"struct.(anonymous namespace)::D"* %{{.*}}, ...)
130 // CHECK64-NEXT: ret void
131 // CHECK64: }
132 
133 // Thunk for calling the fourth virtual function in C, taking a struct parameter
134 // and returning a struct.
135 // CHECK32-LABEL: define linkonce_odr x86_thiscallcc void @"\01??_9C@@$BM@AE"(%struct.C* %this, ...)
136 // CHECK32: [[VPTR:%.*]] = getelementptr inbounds void (%struct.C*, ...)** %{{.*}}, i64 3
137 // CHECK32: [[CALLEE:%.*]] = load void (%struct.C*, ...)** [[VPTR]]
138 // CHECK32: musttail call x86_thiscallcc void (%struct.C*, ...)* [[CALLEE]](%struct.C* %{{.*}}, ...)
139 // CHECK32-NEXT: ret void
140 // CHECK32: }
141 //
142 // CHECK64-LABEL: define linkonce_odr void @"\01??_9C@@$BBI@AA"(%struct.C* %this, ...)
143 // CHECK64: [[VPTR:%.*]] = getelementptr inbounds void (%struct.C*, ...)** %{{.*}}, i64 3
144 // CHECK64: [[CALLEE:%.*]] = load void (%struct.C*, ...)** [[VPTR]]
145 // CHECK64: musttail call void (%struct.C*, ...)* [[CALLEE]](%struct.C* %{{.*}}, ...)
146 // CHECK64: ret void
147 // CHECK64: }
148 
149 // CHECK32: #[[ATTR]] = {{{.*}}"thunk"{{.*}}}
150