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   virtual S __fastcall zed(U);
22 };
23 
24 namespace {
25 struct D {
26   virtual void foo();
27 };
28 }
29 
30 void 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 
44   S (C::*ptr5)(U);
45   ptr5 = &C::qux;
46 
47   S (__fastcall C::*ptr6)(U);
48   ptr6 = &C::zed;
49 
50 
51 // 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
54 // CHECK32: store i8* bitcast (void (%struct.C*, %struct.S*, i32)* @"\01??_9C@@$B7AE" to i8*), i8** %ptr3
55 // CHECK32: store i8* bitcast (void (%"struct.(anonymous namespace)::D"*)* @"\01??_9D@?A@@$BA@AE" to i8*), i8** %ptr4
56 // 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
61 // CHECK64: store i8* bitcast (void (%struct.C*, %struct.S*, i32)* @"\01??_9C@@$BBA@AA" to i8*), i8** %ptr3
62 // CHECK64: store i8* bitcast (void (%"struct.(anonymous namespace)::D"*)* @"\01??_9D@?A@@$BA@AA" to i8*), i8** %ptr
63 // 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)
69 // CHECK32-NOT:         unnamed_addr
70 // CHECK32: [[VPTR:%.*]] = getelementptr inbounds void (%struct.C*)** %{{.*}}, i64 0
71 // CHECK32: [[CALLEE:%.*]] = load void (%struct.C*)** [[VPTR]]
72 // CHECK32: call x86_thiscallcc void [[CALLEE]](%struct.C* %{{.*}})
73 // CHECK32: ret void
74 // CHECK32: }
75 //
76 // CHECK64-LABEL: define linkonce_odr void @"\01??_9C@@$BA@AA"(%struct.C* %this)
77 // CHECK64-NOT:         unnamed_addr
78 // CHECK64: [[VPTR:%.*]] = getelementptr inbounds void (%struct.C*)** %{{.*}}, i64 0
79 // CHECK64: [[CALLEE:%.*]] = load void (%struct.C*)** [[VPTR]]
80 // CHECK64: call void [[CALLEE]](%struct.C* %{{.*}})
81 // CHECK64: ret void
82 // CHECK64: }
83 
84 // Thunk for calling the 2nd virtual function in C, taking int and double as parameters, returning int.
85 // CHECK32-LABEL: define linkonce_odr x86_thiscallcc i32 @"\01??_9C@@$B3AE"(%struct.C* %this, i32, double)
86 // CHECK32: [[VPTR:%.*]] = getelementptr inbounds i32 (%struct.C*, i32, double)** %{{.*}}, i64 1
87 // CHECK32: [[CALLEE:%.*]] = load i32 (%struct.C*, i32, double)** [[VPTR]]
88 // CHECK32: [[CALL:%.*]] = call x86_thiscallcc i32 [[CALLEE]](%struct.C* %{{.*}}, i32 %{{.*}}, double %{{.*}})
89 // CHECK32: ret i32 [[CALL]]
90 // CHECK32: }
91 //
92 // CHECK64-LABEL: define linkonce_odr i32 @"\01??_9C@@$B7AA"(%struct.C* %this, i32, double)
93 // CHECK64: [[VPTR:%.*]] = getelementptr inbounds i32 (%struct.C*, i32, double)** %{{.*}}, i64 1
94 // CHECK64: [[CALLEE:%.*]] = load i32 (%struct.C*, i32, double)** [[VPTR]]
95 // CHECK64: [[CALL:%.*]] = call i32 [[CALLEE]](%struct.C* %{{.*}}, i32 %{{.*}}, double %{{.*}})
96 // CHECK64: ret i32 [[CALL]]
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, %struct.S* noalias sret %agg.result, i32)
101 // CHECK32: [[VPTR:%.*]] = getelementptr inbounds void (%struct.C*, %struct.S*, i32)** %{{.*}}, i64 2
102 // CHECK32: [[CALLEE:%.*]] = load void (%struct.C*, %struct.S*, i32)** [[VPTR]]
103 // CHECK32: call x86_thiscallcc void [[CALLEE]](%struct.C* %{{.*}}, %struct.S* sret %agg.result, i32 %{{.*}})
104 // CHECK32: ret void
105 // CHECK32: }
106 //
107 // CHECK64-LABEL: define linkonce_odr void @"\01??_9C@@$BBA@AA"(%struct.C* %this, %struct.S* noalias sret %agg.result, i32)
108 // CHECK64: [[VPTR:%.*]] = getelementptr inbounds void (%struct.C*, %struct.S*, i32)** %{{.*}}, i64 2
109 // CHECK64: [[CALLEE:%.*]] = load void (%struct.C*, %struct.S*, i32)** [[VPTR]]
110 // CHECK64: call void [[CALLEE]](%struct.C* %{{.*}}, %struct.S* sret %agg.result, i32 %{{.*}})
111 // CHECK64: ret void
112 // CHECK64: }
113 
114 // Thunk for calling the virtual function in internal class D.
115 // CHECK32-LABEL: define internal x86_thiscallcc void @"\01??_9D@?A@@$BA@AE"(%"struct.(anonymous namespace)::D"* %this)
116 // CHECK32: [[VPTR:%.*]] = getelementptr inbounds void (%"struct.(anonymous namespace)::D"*)** %{{.*}}, i64 0
117 // CHECK32: [[CALLEE:%.*]] = load void (%"struct.(anonymous namespace)::D"*)** [[VPTR]]
118 // CHECK32: call x86_thiscallcc void [[CALLEE]](%"struct.(anonymous namespace)::D"* %{{.*}})
119 // CHECK32: ret void
120 // CHECK32: }
121 //
122 // CHECK64-LABEL: define internal void @"\01??_9D@?A@@$BA@AA"(%"struct.(anonymous namespace)::D"* %this)
123 // CHECK64: [[VPTR:%.*]] = getelementptr inbounds void (%"struct.(anonymous namespace)::D"*)** %{{.*}}, i64 0
124 // CHECK64: [[CALLEE:%.*]] = load void (%"struct.(anonymous namespace)::D"*)** [[VPTR]]
125 // CHECK64: call void [[CALLEE]](%"struct.(anonymous namespace)::D"* %{{.*}})
126 // CHECK64: ret void
127 // CHECK64: }
128 
129 // Thunk for calling the fourth virtual function in C, taking a struct parameter
130 // and returning a struct.
131 // CHECK32-LABEL: define linkonce_odr x86_thiscallcc %struct.S* @"\01??_9C@@$BM@AE"(%struct.C* %this, <{ %struct.S*, %struct.U }>* inalloca)
132 // CHECK32: [[VPTR:%.*]] = getelementptr inbounds %struct.S* (%struct.C*, <{ %struct.S*, %struct.U }>*)** %{{.*}}, i64 3
133 // CHECK32: [[CALLEE:%.*]] = load %struct.S* (%struct.C*, <{ %struct.S*, %struct.U }>*)** [[VPTR]]
134 // CHECK32: [[CALL:%.*]] = musttail call x86_thiscallcc %struct.S* [[CALLEE]](%struct.C* %{{.*}}, <{ %struct.S*, %struct.U }>* inalloca %{{.*}})
135 // CHECK32-NEXT: ret %struct.S* [[CALL]]
136 // CHECK32: }
137 //
138 // CHECK64-LABEL: define linkonce_odr void @"\01??_9C@@$BBI@AA"(%struct.C* %this, %struct.S* noalias sret %agg.result, %struct.U*)
139 // CHECK64: [[VPTR:%.*]] = getelementptr inbounds void (%struct.C*, %struct.S*, %struct.U*)** %{{.*}}, i64 3
140 // CHECK64: [[CALLEE:%.*]] = load void (%struct.C*, %struct.S*, %struct.U*)** [[VPTR]]
141 // CHECK64: call void [[CALLEE]](%struct.C* %{{.*}}, %struct.S* sret %agg.result, %struct.U* %{{.*}})
142 // CHECK64: ret void
143 // CHECK64: }
144 
145 // Thunk for calling the fifth virtual function in C, taking a struct parameter
146 // and returning a struct.
147 // 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)
148 // CHECK32: [[VPTR:%.*]] = getelementptr inbounds void (%struct.C*, %struct.S*, <{ %struct.U }>*)** %{{.*}}, i64 4
149 // CHECK32: [[CALLEE:%.*]] = load void (%struct.C*, %struct.S*, <{ %struct.U }>*)** [[VPTR]]
150 // CHECK32: musttail call x86_fastcallcc void [[CALLEE]](%struct.C* inreg %{{.*}}, %struct.S* inreg sret %{{.*}}, <{ %struct.U }>* inalloca %{{.*}})
151 // CHECK32-NEXT: ret void
152 // CHECK32: }
153 //
154 // CHECK64-LABEL: define linkonce_odr void @"\01??_9C@@$BCA@AA"(%struct.C* %this, %struct.S* noalias sret %agg.result, %struct.U*)
155 // CHECK64: [[VPTR:%.*]] = getelementptr inbounds void (%struct.C*, %struct.S*, %struct.U*)** %{{.*}}, i64 4
156 // CHECK64: [[CALLEE:%.*]] = load void (%struct.C*, %struct.S*, %struct.U*)** [[VPTR]]
157 // CHECK64: call void [[CALLEE]](%struct.C* %{{.*}}, %struct.S* sret %agg.result, %struct.U* %{{.*}})
158 // CHECK64: ret void
159 // CHECK64: }
160