1 // RUN: %clang_cc1 -std=c++11 -Wno-uninitialized -fno-rtti -emit-llvm %s -o - -triple=i386-pc-win32 -fms-extensions | FileCheck %s
2 // RUN: %clang_cc1 -std=c++11 -Wno-uninitialized -fno-rtti -emit-llvm %s -o - -triple=x86_64-pc-win32 -fms-extensions | FileCheck %s -check-prefix=X64
3 // RUN: %clang_cc1 -std=c++11 -Wno-uninitialized -fno-rtti -emit-llvm %s -o - -triple=i386-pc-win32 -DINCOMPLETE_VIRTUAL -fms-extensions -verify
4 // RUN: %clang_cc1 -std=c++11 -Wno-uninitialized -fno-rtti -emit-llvm %s -o - -triple=i386-pc-win32 -DINCOMPLETE_VIRTUAL -DMEMFUN -fms-extensions -verify
5 
6 namespace PR20947 {
7 struct A;
8 int A::**a = nullptr;
9 // CHECK: %[[opaque0:.*]] = type opaque
10 // CHECK: %[[opaque1:.*]] = type opaque
11 // CHECK: @"\01?a@PR20947@@3PAPQA@1@HA" = global %[[opaque0]]* null, align 4
12 
13 struct B;
14 int B::*&b = b;
15 // CHECK: @"\01?b@PR20947@@3AAPQB@1@HA" = global %[[opaque1]]* null, align 4
16 }
17 
18 namespace PR20017 {
19 template <typename T>
20 struct A {
m_fn1PR20017::A21   int T::*m_fn1() { return nullptr; }
22 };
23 struct B;
24 auto a = &A<B>::m_fn1;
25 // CHECK-DAG: @"\01?a@PR20017@@3P8?$A@UB@PR20017@@@1@AEPQB@1@HXZQ21@" = global i8* bitcast ({ i32, i32, i32 } ({{.*}}*)* @"\01?m_fn1@?$A@UB@PR20017@@@PR20017@@QAEPQB@2@HXZ" to i8*), align 4
26 }
27 
28 #ifndef INCOMPLETE_VIRTUAL
29 struct B1 {
30   void foo();
31   int b;
32 };
33 struct B2 {
34   int b2;
35   void foo();
36 };
37 struct Single : B1 {
38   void foo();
39 };
40 struct Multiple : B1, B2 {
41   int m;
42   void foo();
43 };
44 struct Virtual : virtual B1 {
45   int v;
46   void foo();
47 };
48 
49 struct POD {
50   int a;
51   int b;
52 };
53 
54 struct Polymorphic {
55   virtual void myVirtual();
56   int a;
57   int b;
58 };
59 
60 // This class uses the virtual inheritance model, yet its vbptr offset is not 0.
61 // We still use zero for the null field offset, despite it being a valid field
62 // offset.
63 struct NonZeroVBPtr : POD, Virtual {
64   int n;
65   void foo();
66 };
67 
68 struct Unspecified;
69 struct UnspecSingle;
70 
71 // Check that we can lower the LLVM types and get the null initializers right.
72 int Single     ::*s_d_memptr;
73 int Polymorphic::*p_d_memptr;
74 int Multiple   ::*m_d_memptr;
75 int Virtual    ::*v_d_memptr;
76 int NonZeroVBPtr::*n_d_memptr;
77 int Unspecified::*u_d_memptr;
78 int UnspecSingle::*us_d_memptr;
79 // CHECK: @"\01?s_d_memptr@@3PQSingle@@HQ1@" = global i32 -1, align 4
80 // CHECK: @"\01?p_d_memptr@@3PQPolymorphic@@HQ1@" = global i32 0, align 4
81 // CHECK: @"\01?m_d_memptr@@3PQMultiple@@HQ1@" = global i32 -1, align 4
82 // CHECK: @"\01?v_d_memptr@@3PQVirtual@@HQ1@" = global { i32, i32 }
83 // CHECK:   { i32 0, i32 -1 }, align 8
84 // CHECK: @"\01?n_d_memptr@@3PQNonZeroVBPtr@@HQ1@" = global { i32, i32 }
85 // CHECK:   { i32 0, i32 -1 }, align 8
86 // CHECK: @"\01?u_d_memptr@@3PQUnspecified@@HQ1@" = global { i32, i32, i32 }
87 // CHECK:   { i32 0, i32 0, i32 -1 }, align 8
88 // CHECK: @"\01?us_d_memptr@@3PQUnspecSingle@@HQ1@" = global { i32, i32, i32 }
89 // CHECK:   { i32 0, i32 0, i32 -1 }, align 8
90 
91 void (Single  ::*s_f_memptr)();
92 void (Multiple::*m_f_memptr)();
93 void (Virtual ::*v_f_memptr)();
94 // CHECK: @"\01?s_f_memptr@@3P8Single@@AEXXZQ1@" = global i8* null, align 4
95 // CHECK: @"\01?m_f_memptr@@3P8Multiple@@AEXXZQ1@" = global { i8*, i32 } zeroinitializer, align 8
96 // CHECK: @"\01?v_f_memptr@@3P8Virtual@@AEXXZQ1@" = global { i8*, i32, i32 } zeroinitializer, align 8
97 
98 // We can define Unspecified after locking in the inheritance model.
99 struct Unspecified : Multiple, Virtual {
100   void foo();
101   int u;
102 };
103 
104 struct UnspecSingle {
105   void foo();
106 };
107 
108 // Test memptr emission in a constant expression.
109 namespace Const {
110 void (Single     ::*s_f_mp)() = &Single::foo;
111 void (Multiple   ::*m_f_mp)() = &B2::foo;
112 void (Virtual    ::*v_f_mp)() = &Virtual::foo;
113 void (Unspecified::*u_f_mp)() = &Unspecified::foo;
114 void (UnspecSingle::*us_f_mp)() = &UnspecSingle::foo;
115 // CHECK: @"\01?s_f_mp@Const@@3P8Single@@AEXXZQ2@" =
116 // CHECK:   global i8* bitcast ({{.*}} @"\01?foo@Single@@QAEXXZ" to i8*), align 4
117 // CHECK: @"\01?m_f_mp@Const@@3P8Multiple@@AEXXZQ2@" =
118 // CHECK:   global { i8*, i32 } { i8* bitcast ({{.*}} @"\01?foo@B2@@QAEXXZ" to i8*), i32 4 }, align 8
119 // CHECK: @"\01?v_f_mp@Const@@3P8Virtual@@AEXXZQ2@" =
120 // CHECK:   global { i8*, i32, i32 } { i8* bitcast ({{.*}} @"\01?foo@Virtual@@QAEXXZ" to i8*), i32 0, i32 0 }, align 8
121 // CHECK: @"\01?u_f_mp@Const@@3P8Unspecified@@AEXXZQ2@" =
122 // CHECK:   global { i8*, i32, i32, i32 } { i8* bitcast ({{.*}} @"\01?foo@Unspecified@@QAEXXZ" to i8*), i32 0, i32 12, i32 0 }, align 8
123 // CHECK: @"\01?us_f_mp@Const@@3P8UnspecSingle@@AEXXZQ2@" =
124 // CHECK:   global { i8*, i32, i32, i32 } { i8* bitcast ({{.*}} @"\01?foo@UnspecSingle@@QAEXXZ" to i8*), i32 0, i32 0, i32 0 }, align 8
125 }
126 
127 namespace CastParam {
128 // This exercises ConstExprEmitter instead of ValueDecl::evaluateValue.  The
129 // extra reinterpret_cast for the parameter type requires more careful folding.
130 // FIXME: Or does it?  If reinterpret_casts are no-ops, we should be able to
131 // strip them in evaluateValue() and just proceed as normal with an APValue.
132 struct A {
133   int a;
134   void foo(A *p);
135 };
136 struct B { int b; };
137 struct C : B, A { int c; };
138 
139 void (A::*ptr1)(void *) = (void (A::*)(void *)) &A::foo;
140 // CHECK: @"\01?ptr1@CastParam@@3P8A@1@AEXPAX@ZQ21@" =
141 // CHECK:   global i8* bitcast (void ({{.*}})* @"\01?foo@A@CastParam@@QAEXPAU12@@Z" to i8*), align 4
142 
143 // Try a reinterpret_cast followed by a memptr conversion.
144 void (C::*ptr2)(void *) = (void (C::*)(void *)) (void (A::*)(void *)) &A::foo;
145 // CHECK: @"\01?ptr2@CastParam@@3P8C@1@AEXPAX@ZQ21@" =
146 // CHECK:   global { i8*, i32 } { i8* bitcast (void ({{.*}})* @"\01?foo@A@CastParam@@QAEXPAU12@@Z" to i8*), i32 4 }, align 8
147 
148 void (C::*ptr3)(void *) = (void (C::*)(void *)) (void (A::*)(void *)) (void (A::*)(A *)) 0;
149 // CHECK: @"\01?ptr3@CastParam@@3P8C@1@AEXPAX@ZQ21@" =
150 // CHECK:   global { i8*, i32 } zeroinitializer, align 8
151 
152 struct D : C {
153   virtual void isPolymorphic();
154   int d;
155 };
156 
157 // Try a cast that changes the inheritance model.  Null for D is 0, but null for
158 // C is -1.  We need the cast to long in order to hit the non-APValue path.
159 int C::*ptr4 = (int C::*) (int D::*) (long D::*) 0;
160 // CHECK: @"\01?ptr4@CastParam@@3PQC@1@HQ21@" = global i32 -1, align 4
161 
162 // MSVC rejects this but we accept it.
163 int C::*ptr5 = (int C::*) (long D::*) 0;
164 // CHECK: @"\01?ptr5@CastParam@@3PQC@1@HQ21@" = global i32 -1, align 4
165 }
166 
167 struct UnspecWithVBPtr;
168 int UnspecWithVBPtr::*forceUnspecWithVBPtr;
169 struct UnspecWithVBPtr : B1, virtual B2 {
170   int u;
171   void foo();
172 };
173 
174 // Test emitting non-virtual member pointers in a non-constexpr setting.
EmitNonVirtualMemberPointers()175 void EmitNonVirtualMemberPointers() {
176   void (Single     ::*s_f_memptr)() = &Single::foo;
177   void (Multiple   ::*m_f_memptr)() = &Multiple::foo;
178   void (Virtual    ::*v_f_memptr)() = &Virtual::foo;
179   void (Unspecified::*u_f_memptr)() = &Unspecified::foo;
180   void (UnspecWithVBPtr::*u2_f_memptr)() = &UnspecWithVBPtr::foo;
181 // CHECK: define void @"\01?EmitNonVirtualMemberPointers@@YAXXZ"() {{.*}} {
182 // CHECK:   alloca i8*, align 4
183 // CHECK:   alloca { i8*, i32 }, align 8
184 // CHECK:   alloca { i8*, i32, i32 }, align 8
185 // CHECK:   alloca { i8*, i32, i32, i32 }, align 8
186 // CHECK:   store i8* bitcast (void (%{{.*}}*)* @"\01?foo@Single@@QAEXXZ" to i8*), i8** %{{.*}}, align 4
187 // CHECK:   store { i8*, i32 }
188 // CHECK:     { i8* bitcast (void (%{{.*}}*)* @"\01?foo@Multiple@@QAEXXZ" to i8*), i32 0 },
189 // CHECK:     { i8*, i32 }* %{{.*}}, align 8
190 // CHECK:   store { i8*, i32, i32 }
191 // CHECK:     { i8* bitcast (void (%{{.*}}*)* @"\01?foo@Virtual@@QAEXXZ" to i8*), i32 0, i32 0 },
192 // CHECK:     { i8*, i32, i32 }* %{{.*}}, align 8
193 // CHECK:   store { i8*, i32, i32, i32 }
194 // CHECK:     { i8* bitcast (void (%{{.*}}*)* @"\01?foo@Unspecified@@QAEXXZ" to i8*), i32 0, i32 12, i32 0 },
195 // CHECK:     { i8*, i32, i32, i32 }* %{{.*}}, align 8
196 // CHECK:   store { i8*, i32, i32, i32 }
197 // CHECK:     { i8* bitcast (void (%{{.*}}*)* @"\01?foo@UnspecWithVBPtr@@QAEXXZ" to i8*),
198 // CHECK:       i32 0, i32 4, i32 0 },
199 // CHECK:     { i8*, i32, i32, i32 }* %{{.*}}, align 8
200 // CHECK:   ret void
201 // CHECK: }
202 }
203 
podMemPtrs()204 void podMemPtrs() {
205   int POD::*memptr;
206   memptr = &POD::a;
207   memptr = &POD::b;
208   if (memptr)
209     memptr = 0;
210 // Check that member pointers use the right offsets and that null is -1.
211 // CHECK:      define void @"\01?podMemPtrs@@YAXXZ"() {{.*}} {
212 // CHECK:        %[[memptr:.*]] = alloca i32, align 4
213 // CHECK-NEXT:   store i32 0, i32* %[[memptr]], align 4
214 // CHECK-NEXT:   store i32 4, i32* %[[memptr]], align 4
215 // CHECK-NEXT:   %[[memptr_val:.*]] = load i32, i32* %[[memptr]], align 4
216 // CHECK-NEXT:   %{{.*}} = icmp ne i32 %[[memptr_val]], -1
217 // CHECK-NEXT:   br i1 %{{.*}}, label %{{.*}}, label %{{.*}}
218 // CHECK:        store i32 -1, i32* %[[memptr]], align 4
219 // CHECK:        ret void
220 // CHECK:      }
221 }
222 
polymorphicMemPtrs()223 void polymorphicMemPtrs() {
224   int Polymorphic::*memptr;
225   memptr = &Polymorphic::a;
226   memptr = &Polymorphic::b;
227   if (memptr)
228     memptr = 0;
229 // Member pointers for polymorphic classes include the vtable slot in their
230 // offset and use 0 to represent null.
231 // CHECK:      define void @"\01?polymorphicMemPtrs@@YAXXZ"() {{.*}} {
232 // CHECK:        %[[memptr:.*]] = alloca i32, align 4
233 // CHECK-NEXT:   store i32 4, i32* %[[memptr]], align 4
234 // CHECK-NEXT:   store i32 8, i32* %[[memptr]], align 4
235 // CHECK-NEXT:   %[[memptr_val:.*]] = load i32, i32* %[[memptr]], align 4
236 // CHECK-NEXT:   %{{.*}} = icmp ne i32 %[[memptr_val]], 0
237 // CHECK-NEXT:   br i1 %{{.*}}, label %{{.*}}, label %{{.*}}
238 // CHECK:        store i32 0, i32* %[[memptr]], align 4
239 // CHECK:        ret void
240 // CHECK:      }
241 }
242 
nullTestDataUnspecified(int Unspecified::* mp)243 bool nullTestDataUnspecified(int Unspecified::*mp) {
244   return mp;
245 // CHECK: define zeroext i1 @"\01?nullTestDataUnspecified@@YA_NPQUnspecified@@H@Z"{{.*}} {
246 // CHECK:   %{{.*}} = load { i32, i32, i32 }, { i32, i32, i32 }* %{{.*}}, align 8
247 // CHECK:   store { i32, i32, i32 } {{.*}} align 8
248 // CHECK:   %[[mp:.*]] = load { i32, i32, i32 }, { i32, i32, i32 }* %{{.*}}, align 8
249 // CHECK:   %[[mp0:.*]] = extractvalue { i32, i32, i32 } %[[mp]], 0
250 // CHECK:   %[[cmp0:.*]] = icmp ne i32 %[[mp0]], 0
251 // CHECK:   %[[mp1:.*]] = extractvalue { i32, i32, i32 } %[[mp]], 1
252 // CHECK:   %[[cmp1:.*]] = icmp ne i32 %[[mp1]], 0
253 // CHECK:   %[[and0:.*]] = or i1 %[[cmp0]], %[[cmp1]]
254 // CHECK:   %[[mp2:.*]] = extractvalue { i32, i32, i32 } %[[mp]], 2
255 // CHECK:   %[[cmp2:.*]] = icmp ne i32 %[[mp2]], -1
256 // CHECK:   %[[and1:.*]] = or i1 %[[and0]], %[[cmp2]]
257 // CHECK:   ret i1 %[[and1]]
258 // CHECK: }
259 
260 // Pass this large type indirectly.
261 // X64-LABEL: define zeroext i1 @"\01?nullTestDataUnspecified@@
262 // X64:             ({ i32, i32, i32 }*)
263 }
264 
nullTestFunctionUnspecified(void (Unspecified::* mp)())265 bool nullTestFunctionUnspecified(void (Unspecified::*mp)()) {
266   return mp;
267 // CHECK: define zeroext i1 @"\01?nullTestFunctionUnspecified@@YA_NP8Unspecified@@AEXXZ@Z"{{.*}} {
268 // CHECK:   %{{.*}} = load { i8*, i32, i32, i32 }, { i8*, i32, i32, i32 }* %{{.*}}, align 8
269 // CHECK:   store { i8*, i32, i32, i32 } {{.*}} align 8
270 // CHECK:   %[[mp:.*]] = load { i8*, i32, i32, i32 }, { i8*, i32, i32, i32 }* %{{.*}}, align 8
271 // CHECK:   %[[mp0:.*]] = extractvalue { i8*, i32, i32, i32 } %[[mp]], 0
272 // CHECK:   %[[cmp0:.*]] = icmp ne i8* %[[mp0]], null
273 // CHECK:   ret i1 %[[cmp0]]
274 // CHECK: }
275 }
276 
loadDataMemberPointerVirtual(Virtual * o,int Virtual::* memptr)277 int loadDataMemberPointerVirtual(Virtual *o, int Virtual::*memptr) {
278   return o->*memptr;
279 // Test that we can unpack this aggregate member pointer and load the member
280 // data pointer.
281 // CHECK: define i32 @"\01?loadDataMemberPointerVirtual@@YAHPAUVirtual@@PQ1@H@Z"{{.*}} {
282 // CHECK:   %[[o:.*]] = load %{{.*}}*, %{{.*}}** %{{.*}}, align 4
283 // CHECK:   %[[memptr:.*]] = load { i32, i32 }, { i32, i32 }* %{{.*}}, align 8
284 // CHECK:   %[[memptr0:.*]] = extractvalue { i32, i32 } %[[memptr:.*]], 0
285 // CHECK:   %[[memptr1:.*]] = extractvalue { i32, i32 } %[[memptr:.*]], 1
286 // CHECK:   %[[v6:.*]] = bitcast %{{.*}}* %[[o]] to i8*
287 // CHECK:   %[[vbptr:.*]] = getelementptr inbounds i8, i8* %[[v6]], i32 0
288 // CHECK:   %[[vbptr_a:.*]] = bitcast i8* %[[vbptr]] to i32**
289 // CHECK:   %[[vbtable:.*]] = load i32*, i32** %[[vbptr_a:.*]]
290 // CHECK:   %[[memptr1_shr:.*]] = ashr exact i32 %[[memptr1]], 2
291 // CHECK:   %[[v7:.*]] = getelementptr inbounds i32, i32* %[[vbtable]], i32 %[[memptr1_shr]]
292 // CHECK:   %[[vbase_offs:.*]] = load i32, i32* %[[v7]]
293 // CHECK:   %[[v10:.*]] = getelementptr inbounds i8, i8* %[[vbptr]], i32 %[[vbase_offs]]
294 // CHECK:   %[[offset:.*]] = getelementptr inbounds i8, i8* %[[v10]], i32 %[[memptr0]]
295 // CHECK:   %[[v11:.*]] = bitcast i8* %[[offset]] to i32*
296 // CHECK:   %[[v12:.*]] = load i32, i32* %[[v11]]
297 // CHECK:   ret i32 %[[v12]]
298 // CHECK: }
299 
300 // A two-field data memptr on x64 gets coerced to i64 and is passed in a
301 // register or memory.
302 // X64-LABEL: define i32 @"\01?loadDataMemberPointerVirtual@@YAHPEAUVirtual@@PEQ1@H@Z"
303 // X64:             (%struct.Virtual* %o, i64 %memptr.coerce)
304 }
305 
loadDataMemberPointerUnspecified(Unspecified * o,int Unspecified::* memptr)306 int loadDataMemberPointerUnspecified(Unspecified *o, int Unspecified::*memptr) {
307   return o->*memptr;
308 // Test that we can unpack this aggregate member pointer and load the member
309 // data pointer.
310 // CHECK: define i32 @"\01?loadDataMemberPointerUnspecified@@YAHPAUUnspecified@@PQ1@H@Z"{{.*}} {
311 // CHECK:   %[[o:.*]] = load %{{.*}}*, %{{.*}}** %{{.*}}, align 4
312 // CHECK:   %[[memptr:.*]] = load { i32, i32, i32 }, { i32, i32, i32 }* %{{.*}}, align 8
313 // CHECK:   %[[memptr0:.*]] = extractvalue { i32, i32, i32 } %[[memptr:.*]], 0
314 // CHECK:   %[[memptr1:.*]] = extractvalue { i32, i32, i32 } %[[memptr:.*]], 1
315 // CHECK:   %[[memptr2:.*]] = extractvalue { i32, i32, i32 } %[[memptr:.*]], 2
316 // CHECK:   %[[base:.*]] = bitcast %{{.*}}* %[[o]] to i8*
317 // CHECK:   %[[is_vbase:.*]] = icmp ne i32 %[[memptr2]], 0
318 // CHECK:   br i1 %[[is_vbase]], label %[[vadjust:.*]], label %[[skip:.*]]
319 //
320 // CHECK: [[vadjust]]
321 // CHECK:   %[[vbptr:.*]] = getelementptr inbounds i8, i8* %[[base]], i32 %[[memptr1]]
322 // CHECK:   %[[vbptr_a:.*]] = bitcast i8* %[[vbptr]] to i32**
323 // CHECK:   %[[vbtable:.*]] = load i32*, i32** %[[vbptr_a:.*]]
324 // CHECK:   %[[memptr2_shr:.*]] = ashr exact i32 %[[memptr2]], 2
325 // CHECK:   %[[v7:.*]] = getelementptr inbounds i32, i32* %[[vbtable]], i32 %[[memptr2_shr]]
326 // CHECK:   %[[vbase_offs:.*]] = load i32, i32* %[[v7]]
327 // CHECK:   %[[base_adj:.*]] = getelementptr inbounds i8, i8* %[[vbptr]], i32 %[[vbase_offs]]
328 //
329 // CHECK: [[skip]]
330 // CHECK:   %[[new_base:.*]] = phi i8* [ %[[base]], %{{.*}} ], [ %[[base_adj]], %[[vadjust]] ]
331 // CHECK:   %[[offset:.*]] = getelementptr inbounds i8, i8* %[[new_base]], i32 %[[memptr0]]
332 // CHECK:   %[[v11:.*]] = bitcast i8* %[[offset]] to i32*
333 // CHECK:   %[[v12:.*]] = load i32, i32* %[[v11]]
334 // CHECK:   ret i32 %[[v12]]
335 // CHECK: }
336 }
337 
callMemberPointerSingle(Single * o,void (Single::* memptr)())338 void callMemberPointerSingle(Single *o, void (Single::*memptr)()) {
339   (o->*memptr)();
340 // Just look for an indirect thiscall.
341 // CHECK: define void @"\01?callMemberPointerSingle@@{{.*}} {{.*}} {
342 // CHECK:   call x86_thiscallcc void %{{.*}}(%{{.*}} %{{.*}})
343 // CHECK:   ret void
344 // CHECK: }
345 
346 // X64-LABEL: define void @"\01?callMemberPointerSingle@@
347 // X64:           (%struct.Single* %o, i8* %memptr)
348 // X64:   bitcast i8* %{{[^ ]*}} to void (%struct.Single*)*
349 // X64:   ret void
350 }
351 
callMemberPointerMultiple(Multiple * o,void (Multiple::* memptr)())352 void callMemberPointerMultiple(Multiple *o, void (Multiple::*memptr)()) {
353   (o->*memptr)();
354 // CHECK: define void @"\01?callMemberPointerMultiple@@{{.*}} {
355 // CHECK:   %[[memptr0:.*]] = extractvalue { i8*, i32 } %{{.*}}, 0
356 // CHECK:   %[[memptr1:.*]] = extractvalue { i8*, i32 } %{{.*}}, 1
357 // CHECK:   %[[this_adjusted:.*]] = getelementptr inbounds i8, i8* %{{.*}}, i32 %[[memptr1]]
358 // CHECK:   %[[this:.*]] = bitcast i8* %[[this_adjusted]] to {{.*}}
359 // CHECK:   %[[fptr:.*]] = bitcast i8* %[[memptr0]] to {{.*}}
360 // CHECK:   call x86_thiscallcc void %[[fptr]](%{{.*}} %[[this]])
361 // CHECK:   ret void
362 // CHECK: }
363 }
364 
callMemberPointerVirtualBase(Virtual * o,void (Virtual::* memptr)())365 void callMemberPointerVirtualBase(Virtual *o, void (Virtual::*memptr)()) {
366   (o->*memptr)();
367 // This shares a lot with virtual data member pointers.
368 // CHECK: define void @"\01?callMemberPointerVirtualBase@@{{.*}} {
369 // CHECK:   %[[memptr0:.*]] = extractvalue { i8*, i32, i32 } %{{.*}}, 0
370 // CHECK:   %[[memptr1:.*]] = extractvalue { i8*, i32, i32 } %{{.*}}, 1
371 // CHECK:   %[[memptr2:.*]] = extractvalue { i8*, i32, i32 } %{{.*}}, 2
372 // CHECK:   %[[vbptr:.*]] = getelementptr inbounds i8, i8* %{{.*}}, i32 0
373 // CHECK:   %[[vbptr_a:.*]] = bitcast i8* %[[vbptr]] to i32**
374 // CHECK:   %[[vbtable:.*]] = load i32*, i32** %[[vbptr_a:.*]]
375 // CHECK:   %[[memptr2_shr:.*]] = ashr exact i32 %[[memptr2]], 2
376 // CHECK:   %[[v7:.*]] = getelementptr inbounds i32, i32* %[[vbtable]], i32 %[[memptr2_shr]]
377 // CHECK:   %[[vbase_offs:.*]] = load i32, i32* %[[v7]]
378 // CHECK:   %[[v10:.*]] = getelementptr inbounds i8, i8* %[[vbptr]], i32 %[[vbase_offs]]
379 // CHECK:   %[[this_adjusted:.*]] = getelementptr inbounds i8, i8* %[[v10]], i32 %[[memptr1]]
380 // CHECK:   %[[fptr:.*]] = bitcast i8* %[[memptr0]] to void ({{.*}})
381 // CHECK:   %[[this:.*]] = bitcast i8* %[[this_adjusted]] to {{.*}}
382 // CHECK:   call x86_thiscallcc void %[[fptr]](%{{.*}} %[[this]])
383 // CHECK:   ret void
384 // CHECK: }
385 }
386 
compareSingleFunctionMemptr(void (Single::* l)(),void (Single::* r)())387 bool compareSingleFunctionMemptr(void (Single::*l)(), void (Single::*r)()) {
388   return l == r;
389 // Should only be one comparison here.
390 // CHECK: define zeroext i1 @"\01?compareSingleFunctionMemptr@@YA_NP8Single@@AEXXZ0@Z"{{.*}} {
391 // CHECK-NOT: icmp
392 // CHECK:   %[[r:.*]] = icmp eq
393 // CHECK-NOT: icmp
394 // CHECK:   ret i1 %[[r]]
395 // CHECK: }
396 
397 // X64-LABEL: define zeroext i1 @"\01?compareSingleFunctionMemptr@@
398 // X64:             (i8* %{{[^,]*}}, i8* %{{[^)]*}})
399 }
400 
compareNeqSingleFunctionMemptr(void (Single::* l)(),void (Single::* r)())401 bool compareNeqSingleFunctionMemptr(void (Single::*l)(), void (Single::*r)()) {
402   return l != r;
403 // Should only be one comparison here.
404 // CHECK: define zeroext i1 @"\01?compareNeqSingleFunctionMemptr@@YA_NP8Single@@AEXXZ0@Z"{{.*}} {
405 // CHECK-NOT: icmp
406 // CHECK:   %[[r:.*]] = icmp ne
407 // CHECK-NOT: icmp
408 // CHECK:   ret i1 %[[r]]
409 // CHECK: }
410 }
411 
unspecFuncMemptrEq(void (Unspecified::* l)(),void (Unspecified::* r)())412 bool unspecFuncMemptrEq(void (Unspecified::*l)(), void (Unspecified::*r)()) {
413   return l == r;
414 // CHECK: define zeroext i1 @"\01?unspecFuncMemptrEq@@YA_NP8Unspecified@@AEXXZ0@Z"{{.*}} {
415 // CHECK:   %[[lhs0:.*]] = extractvalue { i8*, i32, i32, i32 } %[[l:.*]], 0
416 // CHECK:   %{{.*}} = extractvalue { i8*, i32, i32, i32 } %[[r:.*]], 0
417 // CHECK:   %[[cmp0:.*]] = icmp eq i8* %[[lhs0]], %{{.*}}
418 // CHECK:   %{{.*}} = extractvalue { i8*, i32, i32, i32 } %[[l]], 1
419 // CHECK:   %{{.*}} = extractvalue { i8*, i32, i32, i32 } %[[r]], 1
420 // CHECK:   %[[cmp1:.*]] = icmp eq i32
421 // CHECK:   %{{.*}} = extractvalue { i8*, i32, i32, i32 } %[[l]], 2
422 // CHECK:   %{{.*}} = extractvalue { i8*, i32, i32, i32 } %[[r]], 2
423 // CHECK:   %[[cmp2:.*]] = icmp eq i32
424 // CHECK:   %[[res12:.*]] = and i1 %[[cmp1]], %[[cmp2]]
425 // CHECK:   %{{.*}} = extractvalue { i8*, i32, i32, i32 } %[[l]], 3
426 // CHECK:   %{{.*}} = extractvalue { i8*, i32, i32, i32 } %[[r]], 3
427 // CHECK:   %[[cmp3:.*]] = icmp eq i32
428 // CHECK:   %[[res123:.*]] = and i1 %[[res12]], %[[cmp3]]
429 // CHECK:   %[[iszero:.*]] = icmp eq i8* %[[lhs0]], null
430 // CHECK:   %[[bits_or_null:.*]] = or i1 %[[res123]], %[[iszero]]
431 // CHECK:   %{{.*}} = and i1 %[[bits_or_null]], %[[cmp0]]
432 // CHECK:   ret i1 %{{.*}}
433 // CHECK: }
434 
435 // X64-LABEL: define zeroext i1 @"\01?unspecFuncMemptrEq@@
436 // X64:             ({ i8*, i32, i32, i32 }*, { i8*, i32, i32, i32 }*)
437 }
438 
unspecFuncMemptrNeq(void (Unspecified::* l)(),void (Unspecified::* r)())439 bool unspecFuncMemptrNeq(void (Unspecified::*l)(), void (Unspecified::*r)()) {
440   return l != r;
441 // CHECK: define zeroext i1 @"\01?unspecFuncMemptrNeq@@YA_NP8Unspecified@@AEXXZ0@Z"{{.*}} {
442 // CHECK:   %[[lhs0:.*]] = extractvalue { i8*, i32, i32, i32 } %[[l:.*]], 0
443 // CHECK:   %{{.*}} = extractvalue { i8*, i32, i32, i32 } %[[r:.*]], 0
444 // CHECK:   %[[cmp0:.*]] = icmp ne i8* %[[lhs0]], %{{.*}}
445 // CHECK:   %{{.*}} = extractvalue { i8*, i32, i32, i32 } %[[l]], 1
446 // CHECK:   %{{.*}} = extractvalue { i8*, i32, i32, i32 } %[[r]], 1
447 // CHECK:   %[[cmp1:.*]] = icmp ne i32
448 // CHECK:   %{{.*}} = extractvalue { i8*, i32, i32, i32 } %[[l]], 2
449 // CHECK:   %{{.*}} = extractvalue { i8*, i32, i32, i32 } %[[r]], 2
450 // CHECK:   %[[cmp2:.*]] = icmp ne i32
451 // CHECK:   %[[res12:.*]] = or i1 %[[cmp1]], %[[cmp2]]
452 // CHECK:   %{{.*}} = extractvalue { i8*, i32, i32, i32 } %[[l]], 3
453 // CHECK:   %{{.*}} = extractvalue { i8*, i32, i32, i32 } %[[r]], 3
454 // CHECK:   %[[cmp3:.*]] = icmp ne i32
455 // CHECK:   %[[res123:.*]] = or i1 %[[res12]], %[[cmp3]]
456 // CHECK:   %[[iszero:.*]] = icmp ne i8* %[[lhs0]], null
457 // CHECK:   %[[bits_or_null:.*]] = and i1 %[[res123]], %[[iszero]]
458 // CHECK:   %{{.*}} = or i1 %[[bits_or_null]], %[[cmp0]]
459 // CHECK:   ret i1 %{{.*}}
460 // CHECK: }
461 }
462 
unspecDataMemptrEq(int Unspecified::* l,int Unspecified::* r)463 bool unspecDataMemptrEq(int Unspecified::*l, int Unspecified::*r) {
464   return l == r;
465 // CHECK: define zeroext i1 @"\01?unspecDataMemptrEq@@YA_NPQUnspecified@@H0@Z"{{.*}} {
466 // CHECK:   extractvalue { i32, i32, i32 } %{{.*}}, 0
467 // CHECK:   extractvalue { i32, i32, i32 } %{{.*}}, 0
468 // CHECK:   icmp eq i32
469 // CHECK:   extractvalue { i32, i32, i32 } %{{.*}}, 1
470 // CHECK:   extractvalue { i32, i32, i32 } %{{.*}}, 1
471 // CHECK:   icmp eq i32
472 // CHECK:   extractvalue { i32, i32, i32 } %{{.*}}, 2
473 // CHECK:   extractvalue { i32, i32, i32 } %{{.*}}, 2
474 // CHECK:   icmp eq i32
475 // CHECK:   and i1
476 // CHECK:   and i1
477 // CHECK:   ret i1
478 // CHECK: }
479 
480 // X64-LABEL: define zeroext i1 @"\01?unspecDataMemptrEq@@
481 // X64:             ({ i32, i32, i32 }*, { i32, i32, i32 }*)
482 }
483 
convertB2FuncToMultiple(void (B2::* mp)())484 void (Multiple::*convertB2FuncToMultiple(void (B2::*mp)()))() {
485   return mp;
486 // CHECK: define i64 @"\01?convertB2FuncToMultiple@@YAP8Multiple@@AEXXZP8B2@@AEXXZ@Z"{{.*}} {
487 // CHECK:   store
488 // CHECK:   %[[mp:.*]] = load i8*, i8** %{{.*}}, align 4
489 // CHECK:   icmp ne i8* %[[mp]], null
490 // CHECK:   br i1 %{{.*}} label %{{.*}}, label %{{.*}}
491 //
492 //        memptr.convert:                                   ; preds = %entry
493 // CHECK:   insertvalue { i8*, i32 } undef, i8* %[[mp]], 0
494 // CHECK:   insertvalue { i8*, i32 } %{{.*}}, i32 4, 1
495 // CHECK:   br label
496 //
497 //        memptr.converted:                                 ; preds = %memptr.convert, %entry
498 // CHECK:   phi { i8*, i32 } [ zeroinitializer, %{{.*}} ], [ {{.*}} ]
499 // CHECK: }
500 }
501 
convertMultipleFuncToB2(void (Multiple::* mp)())502 void (B2::*convertMultipleFuncToB2(void (Multiple::*mp)()))() {
503 // FIXME: cl emits warning C4407 on this code because of the representation
504 // change.  We might want to do the same.
505   return static_cast<void (B2::*)()>(mp);
506 // FIXME: We should return i8* instead of i32 here.  The ptrtoint cast prevents
507 // LLVM from optimizing away the branch.  This is likely a bug in
508 // lib/CodeGen/TargetInfo.cpp with how we classify memptr types for returns.
509 //
510 // CHECK: define i32 @"\01?convertMultipleFuncToB2@@YAP8B2@@AEXXZP8Multiple@@AEXXZ@Z"{{.*}} {
511 // CHECK:   store
512 // CHECK:   %[[src:.*]] = load { i8*, i32 }, { i8*, i32 }* %{{.*}}, align 8
513 // CHECK:   extractvalue { i8*, i32 } %[[src]], 0
514 // CHECK:   icmp ne i8* %{{.*}}, null
515 // CHECK:   br i1 %{{.*}}, label %{{.*}}, label %{{.*}}
516 //
517 //        memptr.convert:                                   ; preds = %entry
518 // CHECK:   %[[fp:.*]] = extractvalue { i8*, i32 } %[[src]], 0
519 // CHECK:   br label
520 //
521 //        memptr.converted:                                 ; preds = %memptr.convert, %entry
522 // CHECK:   phi i8* [ null, %{{.*}} ], [ %[[fp]], %{{.*}} ]
523 // CHECK: }
524 }
525 
526 namespace Test1 {
527 
528 struct A { int a; };
529 struct B { int b; };
530 struct C : virtual A { int c; };
531 struct D : B, C { int d; };
532 
convertCToD(void (C::* mp)())533 void (D::*convertCToD(void (C::*mp)()))() {
534   return mp;
535 // CHECK: define void @"\01?convertCToD@Test1@@YAP8D@1@AEXXZP8C@1@AEXXZ@Z"{{.*}} {
536 // CHECK:   store
537 // CHECK:   load { i8*, i32, i32 }, { i8*, i32, i32 }* %{{.*}}, align 8
538 // CHECK:   extractvalue { i8*, i32, i32 } %{{.*}}, 0
539 // CHECK:   icmp ne i8* %{{.*}}, null
540 // CHECK:   br i1 %{{.*}}, label %{{.*}}, label %{{.*}}
541 //
542 //        memptr.convert:                                   ; preds = %entry
543 // CHECK:   extractvalue { i8*, i32, i32 } %{{.*}}, 0
544 // CHECK:   extractvalue { i8*, i32, i32 } %{{.*}}, 1
545 // CHECK:   extractvalue { i8*, i32, i32 } %{{.*}}, 2
546 // CHECK:   %[[adj:.*]] = add nsw i32 %{{.*}}, 4
547 // CHECK:   insertvalue { i8*, i32, i32 } undef, i8* {{.*}}, 0
548 // CHECK:   insertvalue { i8*, i32, i32 } {{.*}}, i32 %[[adj]], 1
549 // CHECK:   insertvalue { i8*, i32, i32 } {{.*}}, i32 {{.*}}, 2
550 // CHECK:   br label
551 //
552 //        memptr.converted:                                 ; preds = %memptr.convert, %entry
553 // CHECK:   phi { i8*, i32, i32 } [ { i8* null, i32 0, i32 -1 }, {{.*}} ], [ {{.*}} ]
554 // CHECK: }
555 }
556 
557 }
558 
559 namespace Test2 {
560 // Test that we dynamically convert between different null reps.
561 
562 struct A { int a; };
563 struct B : A { int b; };
564 struct C : A {
565   int c;
566   virtual void hasVfPtr();
567 };
568 
reinterpret(int B::* mp)569 int A::*reinterpret(int B::*mp) {
570   return reinterpret_cast<int A::*>(mp);
571 // CHECK: define i32 @"\01?reinterpret@Test2@@YAPQA@1@HPQB@1@H@Z"{{.*}}  {
572 // CHECK-NOT: select
573 // CHECK:   ret i32
574 // CHECK: }
575 }
576 
reinterpret(int C::* mp)577 int A::*reinterpret(int C::*mp) {
578   return reinterpret_cast<int A::*>(mp);
579 // CHECK: define i32 @"\01?reinterpret@Test2@@YAPQA@1@HPQC@1@H@Z"{{.*}}  {
580 // CHECK:   %[[mp:.*]] = load i32, i32*
581 // CHECK:   %[[cmp:.*]] = icmp ne i32 %[[mp]], 0
582 // CHECK:   select i1 %[[cmp]], i32 %[[mp]], i32 -1
583 // CHECK: }
584 }
585 
586 }
587 
588 namespace Test3 {
589 // Make sure we cast 'this' to i8* before using GEP.
590 
591 struct A {
592   int a;
593   int b;
594 };
595 
load_data(A * a,int A::* mp)596 int *load_data(A *a, int A::*mp) {
597   return &(a->*mp);
598 // CHECK-LABEL: define i32* @"\01?load_data@Test3@@YAPAHPAUA@1@PQ21@H@Z"{{.*}}  {
599 // CHECK:    %[[a:.*]] = load %"struct.Test3::A"*, %"struct.Test3::A"** %{{.*}}, align 4
600 // CHECK:    %[[mp:.*]] = load i32, i32* %{{.*}}, align 4
601 // CHECK:    %[[a_i8:.*]] = bitcast %"struct.Test3::A"* %[[a]] to i8*
602 // CHECK:    getelementptr inbounds i8, i8* %[[a_i8]], i32 %[[mp]]
603 // CHECK: }
604 }
605 
606 }
607 
608 namespace Test4 {
609 
610 struct A        { virtual void f(); };
611 struct B        { virtual void g(); };
612 struct C : A, B { virtual void g(); };
613 
getmp()614 void (C::*getmp())() {
615   return &C::g;
616 }
617 // CHECK-LABEL: define i64 @"\01?getmp@Test4@@YAP8C@1@AEXXZXZ"()
618 // CHECK: store { i8*, i32 } { i8* bitcast (void (%"struct.Test4::C"*, ...)* @"\01??_9C@Test4@@$BA@AE" to i8*), i32 4 }, { i8*, i32 }* %{{.*}}
619 //
620 
621 // CHECK-LABEL: define linkonce_odr x86_thiscallcc void @"\01??_9C@Test4@@$BA@AE"(%"struct.Test4::C"* %this, ...) {{.*}} comdat
622 // CHECK-NOT:  getelementptr
623 // CHECK:  load void (%"struct.Test4::C"*, ...)**, void (%"struct.Test4::C"*, ...)*** %{{.*}}
624 // CHECK:  getelementptr inbounds void (%"struct.Test4::C"*, ...)*, void (%"struct.Test4::C"*, ...)** %{{.*}}, i64 0
625 // CHECK-NOT:  getelementptr
626 // CHECK:  musttail call x86_thiscallcc void (%"struct.Test4::C"*, ...) %
627 
628 }
629 
630 namespace pr20007 {
631 struct A {
632   void f();
633   void f(int);
634 };
635 struct B : public A {};
test()636 void test() { void (B::*a)() = &B::f; }
637 // CHECK-LABEL: define void @"\01?test@pr20007@@YAXXZ"
638 // CHECK: store i8* bitcast (void (%"struct.pr20007::A"*)* @"\01?f@A@pr20007@@QAEXXZ" to i8*)
639 }
640 
641 namespace pr20007_kw {
642 struct A {
643   void f();
644   void f(int);
645 };
646 struct __single_inheritance B;
647 struct B : public A {};
test()648 void test() { void (B::*a)() = &B::f; }
649 // CHECK-LABEL: define void @"\01?test@pr20007_kw@@YAXXZ"
650 // CHECK: store i8* bitcast (void (%"struct.pr20007_kw::A"*)* @"\01?f@A@pr20007_kw@@QAEXXZ" to i8*)
651 }
652 
653 namespace pr20007_pragma {
654 struct A {
655   void f();
656   void f(int);
657 };
658 struct B : public A {};
test()659 void test() { (void)(void (B::*)()) &B::f; }
660 #pragma pointers_to_members(full_generality, virtual_inheritance)
661 static_assert(sizeof(int B::*) == 4, "");
662 static_assert(sizeof(int A::*) == 4, "");
663 #pragma pointers_to_members(best_case)
664 // CHECK-LABEL: define void @"\01?test@pr20007_pragma@@YAXXZ"
665 }
666 
667 namespace pr20007_pragma2 {
668 struct A {
669 };
670 struct B : public A {
671   void f();
672 };
test()673 void test() { (void)&B::f; }
674 #pragma pointers_to_members(full_generality, virtual_inheritance)
675 static_assert(sizeof(int B::*) == 4, "");
676 static_assert(sizeof(int A::*) == 12, "");
677 #pragma pointers_to_members(best_case)
678 // CHECK-LABEL: define void @"\01?test@pr20007_pragma2@@YAXXZ"
679 }
680 
681 namespace pr19987 {
682 template <typename T>
683 struct S {
684   int T::*x;
685 };
686 
687 struct U : S<U> {};
688 
689 static_assert(sizeof(S<U>::x) == 12, "");
690 }
691 
692 #else
693 struct __virtual_inheritance A;
694 #ifdef MEMFUN
foo(A * a,int (A::* mp)())695 int foo(A *a, int (A::*mp)()) {
696     return (a->*mp)(); // expected-error{{requires a complete class type}}
697 }
698 #else
foo(A * a,int A::* mp)699 int foo(A *a, int A::*mp) {
700     return a->*mp; // expected-error{{requires a complete class type}}
701 }
702 #endif
703 #endif
704