• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clang_cc1 -std=c++11 -Wno-uninitialized -fno-rtti -emit-llvm %s -o - -triple=i386-pc-win32 -fms-extensions | FileCheck -allow-deprecated-dag-overlap %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 pr37399 {
7 template <typename T>
8 struct Functor {
9   void (T::*PtrToMemberFunction)();
10 };
11 // CHECK-DAG: %"struct.pr37399::Functor" = type { i8* }
12 
13 template <typename SomeType>
14 class SimpleDerivedFunctor;
15 template <typename SomeType>
16 class SimpleDerivedFunctor : public Functor<SimpleDerivedFunctor<SomeType>> {};
17 // CHECK-DAG: %"class.pr37399::SimpleDerivedFunctor" = type { %"struct.pr37399::Functor" }
18 
19 SimpleDerivedFunctor<void> SimpleFunctor;
20 // CHECK-DAG: @"?SimpleFunctor@pr37399@@3V?$SimpleDerivedFunctor@X@1@A" = dso_local global %"class.pr37399::SimpleDerivedFunctor" zeroinitializer, align 4
21 
22 short Global = 0;
23 template <typename SomeType>
24 class DerivedFunctor;
25 template <typename SomeType>
26 class DerivedFunctor
27     : public Functor<DerivedFunctor<void>> {
28 public:
Foo()29   void Foo() {
30     Global = 42;
31   }
32 };
33 
34 class MultipleBase {
35 public:
MultipleBase()36   MultipleBase() : Value() {}
37   short Value;
38 };
39 // CHECK-DAG: %"class.pr37399::MultipleBase" = type { i16 }
40 
41 template <typename SomeType>
42 class MultiplyDerivedFunctor;
43 template <typename SomeType>
44 class MultiplyDerivedFunctor
45     : public Functor<MultiplyDerivedFunctor<void>>,
46       public MultipleBase {
47 public:
Foo()48   void Foo() {
49     MultipleBase::Value = 42*2;
50   }
51 };
52 
53 class VirtualBase {
54 public:
VirtualBase()55   VirtualBase() : Value() {}
56   short Value;
57 };
58 // CHECK-DAG: %"class.pr37399::VirtualBase" = type { i16 }
59 
60 template <typename SomeType>
61 class VirtBaseFunctor
62     : public Functor<SomeType>,
63       public virtual VirtualBase{};
64 template <typename SomeType>
65 class VirtuallyDerivedFunctor;
66 template <typename SomeType>
67 class VirtuallyDerivedFunctor
68     : public VirtBaseFunctor<VirtuallyDerivedFunctor<void>>,
69       public virtual VirtualBase {
70 public:
Foo()71   void Foo() {
72     VirtualBase::Value = 42*3;
73   }
74 };
75 } // namespace pr37399
76 
77 pr37399::DerivedFunctor<int>           BFunctor;
78 // CHECK-DAG: @"?BFunctor@@3V?$DerivedFunctor@H@pr37399@@A" = dso_local global %"[[BFUNCTOR:class.pr37399::DerivedFunctor(\.[0-9]+)?]]" zeroinitializer, align 8
79 // CHECK-DAG: %"[[BFUNCTOR]]" = type { %"[[BFUNCTORBASE:struct.pr37399::Functor(\.[0-9]+)?]]" }
80 // CHECK-DAG: %"[[BFUNCTORBASE]]" = type { { i8*, i32, i32, i32 } }
81 pr37399::DerivedFunctor<void>          AFunctor;
82 // CHECK-DAG: @"?AFunctor@@3V?$DerivedFunctor@X@pr37399@@A" = dso_local global %"[[AFUNCTOR:class.pr37399::DerivedFunctor(\.[0-9]+)?]]" zeroinitializer, align 8
83 // CHECK-DAG: %"[[AFUNCTOR]]" = type { %"[[AFUNCTORBASE:struct.pr37399::Functor(\.[0-9]+)?]]" }
84 // CHECK-DAG: %"[[AFUNCTORBASE]]" = type { { i8*, i32, i32, i32 } }
85 
86 pr37399::MultiplyDerivedFunctor<int>   DFunctor;
87 // CHECK-DAG: @"?DFunctor@@3V?$MultiplyDerivedFunctor@H@pr37399@@A" = dso_local global %"[[DFUNCTOR:class.pr37399::MultiplyDerivedFunctor(\.[0-9]+)?]]" zeroinitializer, align 8
88 // CHECK-DAG: %"[[DFUNCTOR]]" = type { %"[[DFUNCTORBASE:struct.pr37399::Functor(\.[0-9]+)?]]", %"class.pr37399::MultipleBase", [6 x i8] }
89 // CHECK-DAG: %"[[DFUNCTORBASE]]" = type { { i8*, i32, i32, i32 } }
90 pr37399::MultiplyDerivedFunctor<void>  CFunctor;
91 // CHECK-DAG: @"?CFunctor@@3V?$MultiplyDerivedFunctor@X@pr37399@@A" = dso_local global %"[[CFUNCTOR:class.pr37399::MultiplyDerivedFunctor(\.[0-9]+)?]]" zeroinitializer, align 8
92 // CHECK-DAG: %"[[CFUNCTOR]]" = type { %"[[CFUNCTORBASE:struct.pr37399::Functor(\.[0-9]+)?]]", %"class.pr37399::MultipleBase", [6 x i8] }
93 // CHECK-DAG: %"[[CFUNCTORBASE]]" = type { { i8*, i32, i32, i32 } }
94 
95 pr37399::VirtuallyDerivedFunctor<int>  FFunctor;
96 // CHECK-DAG: @"?FFunctor@@3V?$VirtuallyDerivedFunctor@H@pr37399@@A" = dso_local global %"[[FFUNCTOR:class.pr37399::VirtuallyDerivedFunctor(\.[0-9]+)?]]" zeroinitializer, align 8
97 // CHECK-DAG: %"[[FFUNCTOR]]" = type { %"class.pr37399::VirtBaseFunctor.base", %"class.pr37399::VirtualBase" }
98 pr37399::VirtuallyDerivedFunctor<void> EFunctor;
99 // CHECK-DAG: @"?EFunctor@@3V?$VirtuallyDerivedFunctor@X@pr37399@@A" = dso_local global %"[[EFUNCTOR:class.pr37399::VirtuallyDerivedFunctor(\.[0-9]+)?]]" zeroinitializer, align 8
100 // CHECK-DAG: %"[[EFUNCTOR]]" = type { %"class.pr37399::VirtBaseFunctor.base", %"class.pr37399::VirtualBase" }
101 
102 // CHECK-DAG: %"class.pr37399::VirtBaseFunctor.base" = type <{ %"[[VFUNCTORBASE:struct.pr37399::Functor(\.[0-9]+)?]]", i32*, [4 x i8] }>
103 // CHECK-DAG: %"[[VFUNCTORBASE]]" = type { { i8*, i32, i32, i32 } }
104 
105 namespace pr37399 {
SingleInheritanceFnPtrCall()106 void SingleInheritanceFnPtrCall() {
107   BFunctor.PtrToMemberFunction = &DerivedFunctor<void>::Foo;
108   (AFunctor.*(BFunctor.PtrToMemberFunction))();
109 }
MultipleInheritanceFnPtrCall()110 void MultipleInheritanceFnPtrCall() {
111   DFunctor.PtrToMemberFunction = &MultiplyDerivedFunctor<void>::Foo;
112   Global = CFunctor.MultipleBase::Value;
113   (CFunctor.*(DFunctor.PtrToMemberFunction))();
114   Global = CFunctor.MultipleBase::Value;
115 }
VirtualInheritanceFnPtrCall()116 void VirtualInheritanceFnPtrCall() {
117   FFunctor.PtrToMemberFunction = &VirtuallyDerivedFunctor<void>::Foo;
118   Global = EFunctor.VirtualBase::Value;
119   (EFunctor.*(FFunctor.PtrToMemberFunction))();
120   Global = EFunctor.VirtualBase::Value;
121 }
122 } // namespace pr37399
123 
124 namespace pr43803 {
125 // This case is interesting because it exercises conversion between member
126 // pointer types when emitting constants.
127 
128 struct B;
129 struct C { int B::*option; };
130 extern const C table[3];
131 struct A {
132   int x, y;
133   // Test the indirect case.
134   struct {
135     int z;
136   };
137 };
138 struct B : A {};
139 const C table[] = {
140     {&B::x},
141     {&B::y},
142     {&B::z},
143 };
144 
145 // CHECK: @"?table@pr43803@@3QBUC@1@B" = dso_local constant [3 x %"struct.pr43803::C"]
146 // CHECK-SAME: [%"struct.pr43803::C" { { i32, i32, i32 } zeroinitializer, [4 x i8] undef },
147 // CHECK-SAME:  %"struct.pr43803::C" { { i32, i32, i32 } { i32 4, i32 0, i32 0 }, [4 x i8] undef },
148 // CHECK-SAME:  %"struct.pr43803::C" { { i32, i32, i32 } { i32 8, i32 0, i32 0 }, [4 x i8] undef }]
149 }
150 
151 struct PR26313_Y;
152 typedef void (PR26313_Y::*PR26313_FUNC)();
153 struct PR26313_X {
154   PR26313_FUNC *ptr;
155   PR26313_X();
156 };
PR26313_X()157 PR26313_X::PR26313_X() {}
PR26313_f(PR26313_FUNC * p)158 void PR26313_f(PR26313_FUNC *p) { delete p; }
159 
160 struct PR26313_Z;
161 int PR26313_Z::**a = nullptr;
162 int PR26313_Z::*b = *a;
163 // CHECK-DAG: @"?a@@3PAPQPR26313_Z@@HA" = dso_local global %0* null, align 4
164 // CHECK-DAG: @"?b@@3PQPR26313_Z@@HQ1@" = dso_local global { i32, i32, i32 } { i32 0, i32 0, i32 -1 }, align 4
165 
166 namespace PR20947 {
167 struct A;
168 int A::**a = nullptr;
169 // CHECK-DAG: @"?a@PR20947@@3PAPQA@1@HA" = dso_local global %{{.*}}* null, align 4
170 
171 struct B;
172 int B::*&b = b;
173 // CHECK-DAG: @"?b@PR20947@@3AAPQB@1@HA" = dso_local global %{{.*}}* null, align 4
174 }
175 
176 namespace PR20017 {
177 template <typename T>
178 struct A {
m_fn1PR20017::A179   int T::*m_fn1() { return nullptr; }
180 };
181 struct B;
182 auto a = &A<B>::m_fn1;
183 // CHECK-DAG: @"?a@PR20017@@3P8?$A@UB@PR20017@@@1@AEPQB@1@HXZQ21@" = dso_local global i8* bitcast ({ i32, i32, i32 } ({{.*}}*)* @"?m_fn1@?$A@UB@PR20017@@@PR20017@@QAEPQB@2@HXZ" to i8*), align 4
184 }
185 
186 #ifndef INCOMPLETE_VIRTUAL
187 struct B1 {
188   void foo();
189   int b;
190 };
191 struct B2 {
192   int b2;
193   void foo();
194 };
195 struct Single : B1 {
196   void foo();
197 };
198 struct Multiple : B1, B2 {
199   int m;
200   void foo();
201 };
202 struct Virtual : virtual B1 {
203   int v;
204   void foo();
205 };
206 
207 struct POD {
208   int a;
209   int b;
210 };
211 
212 struct Polymorphic {
213   virtual void myVirtual();
214   int a;
215   int b;
216 };
217 
218 // This class uses the virtual inheritance model, yet its vbptr offset is not 0.
219 // We still use zero for the null field offset, despite it being a valid field
220 // offset.
221 struct NonZeroVBPtr : POD, Virtual {
222   int n;
223   void foo();
224 };
225 
226 struct Unspecified;
227 struct UnspecSingle;
228 
229 // Check that we can lower the LLVM types and get the null initializers right.
230 int Single     ::*s_d_memptr;
231 int Polymorphic::*p_d_memptr;
232 int Multiple   ::*m_d_memptr;
233 int Virtual    ::*v_d_memptr;
234 int NonZeroVBPtr::*n_d_memptr;
235 int Unspecified::*u_d_memptr;
236 int UnspecSingle::*us_d_memptr;
237 // CHECK: @"?s_d_memptr@@3PQSingle@@HQ1@" = dso_local global i32 -1, align 4
238 // CHECK: @"?p_d_memptr@@3PQPolymorphic@@HQ1@" = dso_local global i32 0, align 4
239 // CHECK: @"?m_d_memptr@@3PQMultiple@@HQ1@" = dso_local global i32 -1, align 4
240 // CHECK: @"?v_d_memptr@@3PQVirtual@@HQ1@" = dso_local global { i32, i32 }
241 // CHECK:   { i32 0, i32 -1 }, align 4
242 // CHECK: @"?n_d_memptr@@3PQNonZeroVBPtr@@HQ1@" = dso_local global { i32, i32 }
243 // CHECK:   { i32 0, i32 -1 }, align 4
244 // CHECK: @"?u_d_memptr@@3PQUnspecified@@HQ1@" = dso_local global { i32, i32, i32 }
245 // CHECK:   { i32 0, i32 0, i32 -1 }, align 4
246 // CHECK: @"?us_d_memptr@@3PQUnspecSingle@@HQ1@" = dso_local global { i32, i32, i32 }
247 // CHECK:   { i32 0, i32 0, i32 -1 }, align 4
248 
249 void (Single  ::*s_f_memptr)();
250 void (Multiple::*m_f_memptr)();
251 void (Virtual ::*v_f_memptr)();
252 // CHECK: @"?s_f_memptr@@3P8Single@@AEXXZQ1@" = dso_local global i8* null, align 4
253 // CHECK: @"?m_f_memptr@@3P8Multiple@@AEXXZQ1@" = dso_local global { i8*, i32 } zeroinitializer, align 4
254 // CHECK: @"?v_f_memptr@@3P8Virtual@@AEXXZQ1@" = dso_local global { i8*, i32, i32 } zeroinitializer, align 4
255 
256 // We can define Unspecified after locking in the inheritance model.
257 struct Unspecified : Multiple, Virtual {
258   void foo();
259   int u;
260 };
261 
262 struct UnspecSingle {
263   void foo();
264 };
265 
266 // Test memptr emission in a constant expression.
267 namespace Const {
268 void (Single     ::*s_f_mp)() = &Single::foo;
269 void (Multiple   ::*m_f_mp)() = &B2::foo;
270 void (Virtual    ::*v_f_mp)() = &Virtual::foo;
271 void (Unspecified::*u_f_mp)() = &Unspecified::foo;
272 void (UnspecSingle::*us_f_mp)() = &UnspecSingle::foo;
273 // CHECK: @"?s_f_mp@Const@@3P8Single@@AEXXZQ2@" =
274 // CHECK:   global i8* bitcast ({{.*}} @"?foo@Single@@QAEXXZ" to i8*), align 4
275 // CHECK: @"?m_f_mp@Const@@3P8Multiple@@AEXXZQ2@" =
276 // CHECK:   global { i8*, i32 } { i8* bitcast ({{.*}} @"?foo@B2@@QAEXXZ" to i8*), i32 4 }, align 4
277 // CHECK: @"?v_f_mp@Const@@3P8Virtual@@AEXXZQ2@" =
278 // CHECK:   global { i8*, i32, i32 } { i8* bitcast ({{.*}} @"?foo@Virtual@@QAEXXZ" to i8*), i32 0, i32 0 }, align 4
279 // CHECK: @"?u_f_mp@Const@@3P8Unspecified@@AEXXZQ2@" =
280 // CHECK:   global { i8*, i32, i32, i32 } { i8* bitcast ({{.*}} @"?foo@Unspecified@@QAEXXZ" to i8*), i32 0, i32 0, i32 0 }, align 4
281 // CHECK: @"?us_f_mp@Const@@3P8UnspecSingle@@AEXXZQ2@" =
282 // CHECK:   global { i8*, i32, i32, i32 } { i8* bitcast ({{.*}} @"?foo@UnspecSingle@@QAEXXZ" to i8*), i32 0, i32 0, i32 0 }, align 4
283 }
284 
285 namespace CastParam {
286 // This exercises ConstExprEmitter instead of ValueDecl::evaluateValue.  The
287 // extra reinterpret_cast for the parameter type requires more careful folding.
288 // FIXME: Or does it?  If reinterpret_casts are no-ops, we should be able to
289 // strip them in evaluateValue() and just proceed as normal with an APValue.
290 struct A {
291   int a;
292   void foo(A *p);
293 };
294 struct B { int b; };
295 struct C : B, A { int c; };
296 
297 void (A::*ptr1)(void *) = (void (A::*)(void *)) &A::foo;
298 // CHECK: @"?ptr1@CastParam@@3P8A@1@AEXPAX@ZQ21@" =
299 // CHECK:   global i8* bitcast (void ({{.*}})* @"?foo@A@CastParam@@QAEXPAU12@@Z" to i8*), align 4
300 
301 // Try a reinterpret_cast followed by a memptr conversion.
302 void (C::*ptr2)(void *) = (void (C::*)(void *)) (void (A::*)(void *)) &A::foo;
303 // CHECK: @"?ptr2@CastParam@@3P8C@1@AEXPAX@ZQ21@" =
304 // CHECK:   global { i8*, i32 } { i8* bitcast (void ({{.*}})* @"?foo@A@CastParam@@QAEXPAU12@@Z" to i8*), i32 4 }, align 4
305 
306 void (C::*ptr3)(void *) = (void (C::*)(void *)) (void (A::*)(void *)) (void (A::*)(A *)) 0;
307 // CHECK: @"?ptr3@CastParam@@3P8C@1@AEXPAX@ZQ21@" =
308 // CHECK:   global { i8*, i32 } zeroinitializer, align 4
309 
310 struct D : C {
311   virtual void isPolymorphic();
312   int d;
313 };
314 
315 // Try a cast that changes the inheritance model.  Null for D is 0, but null for
316 // C is -1.  We need the cast to long in order to hit the non-APValue path.
317 int C::*ptr4 = (int C::*) (int D::*) (long D::*) 0;
318 // CHECK: @"?ptr4@CastParam@@3PQC@1@HQ21@" = dso_local global i32 -1, align 4
319 
320 // MSVC rejects this but we accept it.
321 int C::*ptr5 = (int C::*) (long D::*) 0;
322 // CHECK: @"?ptr5@CastParam@@3PQC@1@HQ21@" = dso_local global i32 -1, align 4
323 }
324 
325 struct UnspecWithVBPtr;
326 int UnspecWithVBPtr::*forceUnspecWithVBPtr;
327 struct UnspecWithVBPtr : B1, virtual B2 {
328   int u;
329   void foo();
330 };
331 
332 // Test emitting non-virtual member pointers in a non-constexpr setting.
EmitNonVirtualMemberPointers()333 void EmitNonVirtualMemberPointers() {
334   void (Single     ::*s_f_memptr)() = &Single::foo;
335   void (Multiple   ::*m_f_memptr)() = &Multiple::foo;
336   void (Virtual    ::*v_f_memptr)() = &Virtual::foo;
337   void (Unspecified::*u_f_memptr)() = &Unspecified::foo;
338   void (UnspecWithVBPtr::*u2_f_memptr)() = &UnspecWithVBPtr::foo;
339 // CHECK: define dso_local void @"?EmitNonVirtualMemberPointers@@YAXXZ"() {{.*}} {
340 // CHECK:   alloca i8*, align 4
341 // CHECK:   alloca { i8*, i32 }, align 4
342 // CHECK:   alloca { i8*, i32, i32 }, align 4
343 // CHECK:   alloca { i8*, i32, i32, i32 }, align 4
344 // CHECK:   store i8* bitcast (void (%{{.*}}*)* @"?foo@Single@@QAEXXZ" to i8*), i8** %{{.*}}, align 4
345 // CHECK:   store { i8*, i32 }
346 // CHECK:     { i8* bitcast (void (%{{.*}}*)* @"?foo@Multiple@@QAEXXZ" to i8*), i32 0 },
347 // CHECK:     { i8*, i32 }* %{{.*}}, align 4
348 // CHECK:   store { i8*, i32, i32 }
349 // CHECK:     { i8* bitcast (void (%{{.*}}*)* @"?foo@Virtual@@QAEXXZ" to i8*), i32 0, i32 0 },
350 // CHECK:     { i8*, i32, i32 }* %{{.*}}, align 4
351 // CHECK:   store { i8*, i32, i32, i32 }
352 // CHECK:     { i8* bitcast (void (%{{.*}}*)* @"?foo@Unspecified@@QAEXXZ" to i8*), i32 0, i32 0, i32 0 },
353 // CHECK:     { i8*, i32, i32, i32 }* %{{.*}}, align 4
354 // CHECK:   store { i8*, i32, i32, i32 }
355 // CHECK:     { i8* bitcast (void (%{{.*}}*)* @"?foo@UnspecWithVBPtr@@QAEXXZ" to i8*),
356 // CHECK:       i32 0, i32 0, i32 0 },
357 // CHECK:     { i8*, i32, i32, i32 }* %{{.*}}, align 4
358 // CHECK:   ret void
359 // CHECK: }
360 }
361 
podMemPtrs()362 void podMemPtrs() {
363   int POD::*memptr;
364   memptr = &POD::a;
365   memptr = &POD::b;
366   if (memptr)
367     memptr = 0;
368 // Check that member pointers use the right offsets and that null is -1.
369 // CHECK:      define dso_local void @"?podMemPtrs@@YAXXZ"() {{.*}} {
370 // CHECK:        %[[memptr:.*]] = alloca i32, align 4
371 // CHECK-NEXT:   store i32 0, i32* %[[memptr]], align 4
372 // CHECK-NEXT:   store i32 4, i32* %[[memptr]], align 4
373 // CHECK-NEXT:   %[[memptr_val:.*]] = load i32, i32* %[[memptr]], align 4
374 // CHECK-NEXT:   %{{.*}} = icmp ne i32 %[[memptr_val]], -1
375 // CHECK-NEXT:   br i1 %{{.*}}, label %{{.*}}, label %{{.*}}
376 // CHECK:        store i32 -1, i32* %[[memptr]], align 4
377 // CHECK:        ret void
378 // CHECK:      }
379 }
380 
polymorphicMemPtrs()381 void polymorphicMemPtrs() {
382   int Polymorphic::*memptr;
383   memptr = &Polymorphic::a;
384   memptr = &Polymorphic::b;
385   if (memptr)
386     memptr = 0;
387 // Member pointers for polymorphic classes include the vtable slot in their
388 // offset and use 0 to represent null.
389 // CHECK:      define dso_local void @"?polymorphicMemPtrs@@YAXXZ"() {{.*}} {
390 // CHECK:        %[[memptr:.*]] = alloca i32, align 4
391 // CHECK-NEXT:   store i32 4, i32* %[[memptr]], align 4
392 // CHECK-NEXT:   store i32 8, i32* %[[memptr]], align 4
393 // CHECK-NEXT:   %[[memptr_val:.*]] = load i32, i32* %[[memptr]], align 4
394 // CHECK-NEXT:   %{{.*}} = icmp ne i32 %[[memptr_val]], 0
395 // CHECK-NEXT:   br i1 %{{.*}}, label %{{.*}}, label %{{.*}}
396 // CHECK:        store i32 0, i32* %[[memptr]], align 4
397 // CHECK:        ret void
398 // CHECK:      }
399 }
400 
nullTestDataUnspecified(int Unspecified::* mp)401 bool nullTestDataUnspecified(int Unspecified::*mp) {
402   return mp;
403 // CHECK: define dso_local zeroext i1 @"?nullTestDataUnspecified@@YA_NPQUnspecified@@H@Z"{{.*}} {
404 // CHECK:   %{{.*}} = load { i32, i32, i32 }, { i32, i32, i32 }* %{{.*}}, align 4
405 // CHECK:   store { i32, i32, i32 } {{.*}} align 4
406 // CHECK:   %[[mp:.*]] = load { i32, i32, i32 }, { i32, i32, i32 }* %{{.*}}, align 4
407 // CHECK:   %[[mp0:.*]] = extractvalue { i32, i32, i32 } %[[mp]], 0
408 // CHECK:   %[[cmp0:.*]] = icmp ne i32 %[[mp0]], 0
409 // CHECK:   %[[mp1:.*]] = extractvalue { i32, i32, i32 } %[[mp]], 1
410 // CHECK:   %[[cmp1:.*]] = icmp ne i32 %[[mp1]], 0
411 // CHECK:   %[[and0:.*]] = or i1 %[[cmp0]], %[[cmp1]]
412 // CHECK:   %[[mp2:.*]] = extractvalue { i32, i32, i32 } %[[mp]], 2
413 // CHECK:   %[[cmp2:.*]] = icmp ne i32 %[[mp2]], -1
414 // CHECK:   %[[and1:.*]] = or i1 %[[and0]], %[[cmp2]]
415 // CHECK:   ret i1 %[[and1]]
416 // CHECK: }
417 
418 // Pass this large type indirectly.
419 // X64-LABEL: define dso_local zeroext i1 @"?nullTestDataUnspecified@@
420 // X64:             ({ i32, i32, i32 }* %0)
421 }
422 
nullTestFunctionUnspecified(void (Unspecified::* mp)())423 bool nullTestFunctionUnspecified(void (Unspecified::*mp)()) {
424   return mp;
425 // CHECK: define dso_local zeroext i1 @"?nullTestFunctionUnspecified@@YA_NP8Unspecified@@AEXXZ@Z"{{.*}} {
426 // CHECK:   %{{.*}} = load { i8*, i32, i32, i32 }, { i8*, i32, i32, i32 }* %{{.*}}, align 4
427 // CHECK:   store { i8*, i32, i32, i32 } {{.*}} align 4
428 // CHECK:   %[[mp:.*]] = load { i8*, i32, i32, i32 }, { i8*, i32, i32, i32 }* %{{.*}}, align 4
429 // CHECK:   %[[mp0:.*]] = extractvalue { i8*, i32, i32, i32 } %[[mp]], 0
430 // CHECK:   %[[cmp0:.*]] = icmp ne i8* %[[mp0]], null
431 // CHECK:   ret i1 %[[cmp0]]
432 // CHECK: }
433 }
434 
loadDataMemberPointerVirtual(Virtual * o,int Virtual::* memptr)435 int loadDataMemberPointerVirtual(Virtual *o, int Virtual::*memptr) {
436   return o->*memptr;
437 // Test that we can unpack this aggregate member pointer and load the member
438 // data pointer.
439 // CHECK: define dso_local i32 @"?loadDataMemberPointerVirtual@@YAHPAUVirtual@@PQ1@H@Z"{{.*}} {
440 // CHECK:   %[[o:.*]] = load %{{.*}}*, %{{.*}}** %{{.*}}, align 4
441 // CHECK:   %[[memptr:.*]] = load { i32, i32 }, { i32, i32 }* %{{.*}}, align 4
442 // CHECK:   %[[memptr0:.*]] = extractvalue { i32, i32 } %[[memptr:.*]], 0
443 // CHECK:   %[[memptr1:.*]] = extractvalue { i32, i32 } %[[memptr:.*]], 1
444 // CHECK:   %[[v6:.*]] = bitcast %{{.*}}* %[[o]] to i8*
445 // CHECK:   %[[vbptr:.*]] = getelementptr inbounds i8, i8* %[[v6]], i32 0
446 // CHECK:   %[[vbptr_a:.*]] = bitcast i8* %[[vbptr]] to i32**
447 // CHECK:   %[[vbtable:.*]] = load i32*, i32** %[[vbptr_a:.*]]
448 // CHECK:   %[[memptr1_shr:.*]] = ashr exact i32 %[[memptr1]], 2
449 // CHECK:   %[[v7:.*]] = getelementptr inbounds i32, i32* %[[vbtable]], i32 %[[memptr1_shr]]
450 // CHECK:   %[[vbase_offs:.*]] = load i32, i32* %[[v7]]
451 // CHECK:   %[[v10:.*]] = getelementptr inbounds i8, i8* %[[vbptr]], i32 %[[vbase_offs]]
452 // CHECK:   %[[offset:.*]] = getelementptr inbounds i8, i8* %[[v10]], i32 %[[memptr0]]
453 // CHECK:   %[[v11:.*]] = bitcast i8* %[[offset]] to i32*
454 // CHECK:   %[[v12:.*]] = load i32, i32* %[[v11]]
455 // CHECK:   ret i32 %[[v12]]
456 // CHECK: }
457 
458 // A two-field data memptr on x64 gets coerced to i64 and is passed in a
459 // register or memory.
460 // X64-LABEL: define dso_local i32 @"?loadDataMemberPointerVirtual@@YAHPEAUVirtual@@PEQ1@H@Z"
461 // X64:             (%struct.Virtual* %o, i64 %memptr.coerce)
462 }
463 
loadDataMemberPointerUnspecified(Unspecified * o,int Unspecified::* memptr)464 int loadDataMemberPointerUnspecified(Unspecified *o, int Unspecified::*memptr) {
465   return o->*memptr;
466 // Test that we can unpack this aggregate member pointer and load the member
467 // data pointer.
468 // CHECK: define dso_local i32 @"?loadDataMemberPointerUnspecified@@YAHPAUUnspecified@@PQ1@H@Z"{{.*}} {
469 // CHECK:   %[[o:.*]] = load %{{.*}}*, %{{.*}}** %{{.*}}, align 4
470 // CHECK:   %[[memptr:.*]] = load { i32, i32, i32 }, { i32, i32, i32 }* %{{.*}}, align 4
471 // CHECK:   %[[memptr0:.*]] = extractvalue { i32, i32, i32 } %[[memptr:.*]], 0
472 // CHECK:   %[[memptr1:.*]] = extractvalue { i32, i32, i32 } %[[memptr:.*]], 1
473 // CHECK:   %[[memptr2:.*]] = extractvalue { i32, i32, i32 } %[[memptr:.*]], 2
474 // CHECK:   %[[base:.*]] = bitcast %{{.*}}* %[[o]] to i8*
475 // CHECK:   %[[is_vbase:.*]] = icmp ne i32 %[[memptr2]], 0
476 // CHECK:   br i1 %[[is_vbase]], label %[[vadjust:.*]], label %[[skip:.*]]
477 //
478 // CHECK: [[vadjust]]
479 // CHECK:   %[[vbptr:.*]] = getelementptr inbounds i8, i8* %[[base]], i32 %[[memptr1]]
480 // CHECK:   %[[vbptr_a:.*]] = bitcast i8* %[[vbptr]] to i32**
481 // CHECK:   %[[vbtable:.*]] = load i32*, i32** %[[vbptr_a:.*]]
482 // CHECK:   %[[memptr2_shr:.*]] = ashr exact i32 %[[memptr2]], 2
483 // CHECK:   %[[v7:.*]] = getelementptr inbounds i32, i32* %[[vbtable]], i32 %[[memptr2_shr]]
484 // CHECK:   %[[vbase_offs:.*]] = load i32, i32* %[[v7]]
485 // CHECK:   %[[base_adj:.*]] = getelementptr inbounds i8, i8* %[[vbptr]], i32 %[[vbase_offs]]
486 //
487 // CHECK: [[skip]]
488 // CHECK:   %[[new_base:.*]] = phi i8* [ %[[base]], %{{.*}} ], [ %[[base_adj]], %[[vadjust]] ]
489 // CHECK:   %[[offset:.*]] = getelementptr inbounds i8, i8* %[[new_base]], i32 %[[memptr0]]
490 // CHECK:   %[[v11:.*]] = bitcast i8* %[[offset]] to i32*
491 // CHECK:   %[[v12:.*]] = load i32, i32* %[[v11]]
492 // CHECK:   ret i32 %[[v12]]
493 // CHECK: }
494 }
495 
callMemberPointerSingle(Single * o,void (Single::* memptr)())496 void callMemberPointerSingle(Single *o, void (Single::*memptr)()) {
497   (o->*memptr)();
498 // Just look for an indirect thiscall.
499 // CHECK: define dso_local void @"?callMemberPointerSingle@@{{.*}} {{.*}} {
500 // CHECK:   call x86_thiscallcc void %{{.*}}(%{{.*}} %{{.*}})
501 // CHECK:   ret void
502 // CHECK: }
503 
504 // X64-LABEL: define dso_local void @"?callMemberPointerSingle@@
505 // X64:           (%struct.Single* %o, i8* %memptr)
506 // X64:   bitcast i8* %{{[^ ]*}} to void (%struct.Single*)*
507 // X64:   ret void
508 }
509 
callMemberPointerMultiple(Multiple * o,void (Multiple::* memptr)())510 void callMemberPointerMultiple(Multiple *o, void (Multiple::*memptr)()) {
511   (o->*memptr)();
512 // CHECK: define dso_local void @"?callMemberPointerMultiple@@{{.*}} {
513 // CHECK:   %[[memptr0:.*]] = extractvalue { i8*, i32 } %{{.*}}, 0
514 // CHECK:   %[[memptr1:.*]] = extractvalue { i8*, i32 } %{{.*}}, 1
515 // CHECK:   %[[this_adjusted:.*]] = getelementptr inbounds i8, i8* %{{.*}}, i32 %[[memptr1]]
516 // CHECK:   %[[this:.*]] = bitcast i8* %[[this_adjusted]] to {{.*}}
517 // CHECK:   %[[fptr:.*]] = bitcast i8* %[[memptr0]] to {{.*}}
518 // CHECK:   call x86_thiscallcc void %[[fptr]](%{{.*}} %[[this]])
519 // CHECK:   ret void
520 // CHECK: }
521 }
522 
callMemberPointerVirtualBase(Virtual * o,void (Virtual::* memptr)())523 void callMemberPointerVirtualBase(Virtual *o, void (Virtual::*memptr)()) {
524   (o->*memptr)();
525 // This shares a lot with virtual data member pointers.
526 // CHECK: define dso_local void @"?callMemberPointerVirtualBase@@{{.*}} {
527 // CHECK:   %[[memptr0:.*]] = extractvalue { i8*, i32, i32 } %{{.*}}, 0
528 // CHECK:   %[[memptr1:.*]] = extractvalue { i8*, i32, i32 } %{{.*}}, 1
529 // CHECK:   %[[memptr2:.*]] = extractvalue { i8*, i32, i32 } %{{.*}}, 2
530 // CHECK:   %[[vbptr:.*]] = getelementptr inbounds i8, i8* %{{.*}}, i32 0
531 // CHECK:   %[[vbptr_a:.*]] = bitcast i8* %[[vbptr]] to i32**
532 // CHECK:   %[[vbtable:.*]] = load i32*, i32** %[[vbptr_a:.*]]
533 // CHECK:   %[[memptr2_shr:.*]] = ashr exact i32 %[[memptr2]], 2
534 // CHECK:   %[[v7:.*]] = getelementptr inbounds i32, i32* %[[vbtable]], i32 %[[memptr2_shr]]
535 // CHECK:   %[[vbase_offs:.*]] = load i32, i32* %[[v7]]
536 // CHECK:   %[[v10:.*]] = getelementptr inbounds i8, i8* %[[vbptr]], i32 %[[vbase_offs]]
537 // CHECK:   %[[this_adjusted:.*]] = getelementptr inbounds i8, i8* %[[v10]], i32 %[[memptr1]]
538 // CHECK:   %[[fptr:.*]] = bitcast i8* %[[memptr0]] to void ({{.*}})
539 // CHECK:   %[[this:.*]] = bitcast i8* %[[this_adjusted]] to {{.*}}
540 // CHECK:   call x86_thiscallcc void %[[fptr]](%{{.*}} %[[this]])
541 // CHECK:   ret void
542 // CHECK: }
543 }
544 
compareSingleFunctionMemptr(void (Single::* l)(),void (Single::* r)())545 bool compareSingleFunctionMemptr(void (Single::*l)(), void (Single::*r)()) {
546   return l == r;
547 // Should only be one comparison here.
548 // CHECK: define dso_local zeroext i1 @"?compareSingleFunctionMemptr@@YA_NP8Single@@AEXXZ0@Z"{{.*}} {
549 // CHECK-NOT: icmp
550 // CHECK:   %[[r:.*]] = icmp eq
551 // CHECK-NOT: icmp
552 // CHECK:   ret i1 %[[r]]
553 // CHECK: }
554 
555 // X64-LABEL: define dso_local zeroext i1 @"?compareSingleFunctionMemptr@@
556 // X64:             (i8* %{{[^,]*}}, i8* %{{[^)]*}})
557 }
558 
compareNeqSingleFunctionMemptr(void (Single::* l)(),void (Single::* r)())559 bool compareNeqSingleFunctionMemptr(void (Single::*l)(), void (Single::*r)()) {
560   return l != r;
561 // Should only be one comparison here.
562 // CHECK: define dso_local zeroext i1 @"?compareNeqSingleFunctionMemptr@@YA_NP8Single@@AEXXZ0@Z"{{.*}} {
563 // CHECK-NOT: icmp
564 // CHECK:   %[[r:.*]] = icmp ne
565 // CHECK-NOT: icmp
566 // CHECK:   ret i1 %[[r]]
567 // CHECK: }
568 }
569 
unspecFuncMemptrEq(void (Unspecified::* l)(),void (Unspecified::* r)())570 bool unspecFuncMemptrEq(void (Unspecified::*l)(), void (Unspecified::*r)()) {
571   return l == r;
572 // CHECK: define dso_local zeroext i1 @"?unspecFuncMemptrEq@@YA_NP8Unspecified@@AEXXZ0@Z"{{.*}} {
573 // CHECK:   %[[lhs0:.*]] = extractvalue { i8*, i32, i32, i32 } %[[l:.*]], 0
574 // CHECK:   %{{.*}} = extractvalue { i8*, i32, i32, i32 } %[[r:.*]], 0
575 // CHECK:   %[[cmp0:.*]] = icmp eq i8* %[[lhs0]], %{{.*}}
576 // CHECK:   %{{.*}} = extractvalue { i8*, i32, i32, i32 } %[[l]], 1
577 // CHECK:   %{{.*}} = extractvalue { i8*, i32, i32, i32 } %[[r]], 1
578 // CHECK:   %[[cmp1:.*]] = icmp eq i32
579 // CHECK:   %{{.*}} = extractvalue { i8*, i32, i32, i32 } %[[l]], 2
580 // CHECK:   %{{.*}} = extractvalue { i8*, i32, i32, i32 } %[[r]], 2
581 // CHECK:   %[[cmp2:.*]] = icmp eq i32
582 // CHECK:   %[[res12:.*]] = and i1 %[[cmp1]], %[[cmp2]]
583 // CHECK:   %{{.*}} = extractvalue { i8*, i32, i32, i32 } %[[l]], 3
584 // CHECK:   %{{.*}} = extractvalue { i8*, i32, i32, i32 } %[[r]], 3
585 // CHECK:   %[[cmp3:.*]] = icmp eq i32
586 // CHECK:   %[[res123:.*]] = and i1 %[[res12]], %[[cmp3]]
587 // CHECK:   %[[iszero:.*]] = icmp eq i8* %[[lhs0]], null
588 // CHECK:   %[[bits_or_null:.*]] = or i1 %[[res123]], %[[iszero]]
589 // CHECK:   %{{.*}} = and i1 %[[bits_or_null]], %[[cmp0]]
590 // CHECK:   ret i1 %{{.*}}
591 // CHECK: }
592 
593 // X64-LABEL: define dso_local zeroext i1 @"?unspecFuncMemptrEq@@
594 // X64:             ({ i8*, i32, i32, i32 }* %0, { i8*, i32, i32, i32 }* %1)
595 }
596 
unspecFuncMemptrNeq(void (Unspecified::* l)(),void (Unspecified::* r)())597 bool unspecFuncMemptrNeq(void (Unspecified::*l)(), void (Unspecified::*r)()) {
598   return l != r;
599 // CHECK: define dso_local zeroext i1 @"?unspecFuncMemptrNeq@@YA_NP8Unspecified@@AEXXZ0@Z"{{.*}} {
600 // CHECK:   %[[lhs0:.*]] = extractvalue { i8*, i32, i32, i32 } %[[l:.*]], 0
601 // CHECK:   %{{.*}} = extractvalue { i8*, i32, i32, i32 } %[[r:.*]], 0
602 // CHECK:   %[[cmp0:.*]] = icmp ne i8* %[[lhs0]], %{{.*}}
603 // CHECK:   %{{.*}} = extractvalue { i8*, i32, i32, i32 } %[[l]], 1
604 // CHECK:   %{{.*}} = extractvalue { i8*, i32, i32, i32 } %[[r]], 1
605 // CHECK:   %[[cmp1:.*]] = icmp ne i32
606 // CHECK:   %{{.*}} = extractvalue { i8*, i32, i32, i32 } %[[l]], 2
607 // CHECK:   %{{.*}} = extractvalue { i8*, i32, i32, i32 } %[[r]], 2
608 // CHECK:   %[[cmp2:.*]] = icmp ne i32
609 // CHECK:   %[[res12:.*]] = or i1 %[[cmp1]], %[[cmp2]]
610 // CHECK:   %{{.*}} = extractvalue { i8*, i32, i32, i32 } %[[l]], 3
611 // CHECK:   %{{.*}} = extractvalue { i8*, i32, i32, i32 } %[[r]], 3
612 // CHECK:   %[[cmp3:.*]] = icmp ne i32
613 // CHECK:   %[[res123:.*]] = or i1 %[[res12]], %[[cmp3]]
614 // CHECK:   %[[iszero:.*]] = icmp ne i8* %[[lhs0]], null
615 // CHECK:   %[[bits_or_null:.*]] = and i1 %[[res123]], %[[iszero]]
616 // CHECK:   %{{.*}} = or i1 %[[bits_or_null]], %[[cmp0]]
617 // CHECK:   ret i1 %{{.*}}
618 // CHECK: }
619 }
620 
unspecDataMemptrEq(int Unspecified::* l,int Unspecified::* r)621 bool unspecDataMemptrEq(int Unspecified::*l, int Unspecified::*r) {
622   return l == r;
623 // CHECK: define dso_local zeroext i1 @"?unspecDataMemptrEq@@YA_NPQUnspecified@@H0@Z"{{.*}} {
624 // CHECK:   extractvalue { i32, i32, i32 } %{{.*}}, 0
625 // CHECK:   extractvalue { i32, i32, i32 } %{{.*}}, 0
626 // CHECK:   icmp eq i32
627 // CHECK:   extractvalue { i32, i32, i32 } %{{.*}}, 1
628 // CHECK:   extractvalue { i32, i32, i32 } %{{.*}}, 1
629 // CHECK:   icmp eq i32
630 // CHECK:   extractvalue { i32, i32, i32 } %{{.*}}, 2
631 // CHECK:   extractvalue { i32, i32, i32 } %{{.*}}, 2
632 // CHECK:   icmp eq i32
633 // CHECK:   and i1
634 // CHECK:   and i1
635 // CHECK:   ret i1
636 // CHECK: }
637 
638 // X64-LABEL: define dso_local zeroext i1 @"?unspecDataMemptrEq@@
639 // X64:             ({ i32, i32, i32 }* %0, { i32, i32, i32 }* %1)
640 }
641 
convertB2FuncToMultiple(void (B2::* mp)())642 void (Multiple::*convertB2FuncToMultiple(void (B2::*mp)()))() {
643   return mp;
644 // CHECK: define dso_local i64 @"?convertB2FuncToMultiple@@YAP8Multiple@@AEXXZP8B2@@AEXXZ@Z"{{.*}} {
645 // CHECK:   store
646 // CHECK:   %[[mp:.*]] = load i8*, i8** %{{.*}}, align 4
647 // CHECK:   icmp ne i8* %[[mp]], null
648 // CHECK:   br i1 %{{.*}} label %{{.*}}, label %{{.*}}
649 //
650 //        memptr.convert:                                   ; preds = %entry
651 // CHECK:   insertvalue { i8*, i32 } undef, i8* %[[mp]], 0
652 // CHECK:   insertvalue { i8*, i32 } %{{.*}}, i32 4, 1
653 // CHECK:   br label
654 //
655 //        memptr.converted:                                 ; preds = %memptr.convert, %entry
656 // CHECK:   phi { i8*, i32 } [ zeroinitializer, %{{.*}} ], [ {{.*}} ]
657 // CHECK: }
658 }
659 
convertMultipleFuncToB2(void (Multiple::* mp)())660 void (B2::*convertMultipleFuncToB2(void (Multiple::*mp)()))() {
661 // FIXME: cl emits warning C4407 on this code because of the representation
662 // change.  We might want to do the same.
663   return static_cast<void (B2::*)()>(mp);
664 // FIXME: We should return i8* instead of i32 here.  The ptrtoint cast prevents
665 // LLVM from optimizing away the branch.  This is likely a bug in
666 // lib/CodeGen/TargetInfo.cpp with how we classify memptr types for returns.
667 //
668 // CHECK: define dso_local i32 @"?convertMultipleFuncToB2@@YAP8B2@@AEXXZP8Multiple@@AEXXZ@Z"{{.*}} {
669 // CHECK:   store
670 // CHECK:   %[[src:.*]] = load { i8*, i32 }, { i8*, i32 }* %{{.*}}, align 4
671 // CHECK:   extractvalue { i8*, i32 } %[[src]], 0
672 // CHECK:   icmp ne i8* %{{.*}}, null
673 // CHECK:   br i1 %{{.*}}, label %{{.*}}, label %{{.*}}
674 //
675 //        memptr.convert:                                   ; preds = %entry
676 // CHECK:   %[[fp:.*]] = extractvalue { i8*, i32 } %[[src]], 0
677 // CHECK:   br label
678 //
679 //        memptr.converted:                                 ; preds = %memptr.convert, %entry
680 // CHECK:   phi i8* [ null, %{{.*}} ], [ %[[fp]], %{{.*}} ]
681 // CHECK: }
682 }
683 
684 namespace Test1 {
685 
686 struct A { int a; };
687 struct B { int b; };
688 struct C : virtual A { int c; };
689 struct D : B, C { int d; };
690 
convertCToD(void (C::* mp)())691 void (D::*convertCToD(void (C::*mp)()))() {
692   return mp;
693 // CHECK: define dso_local void @"?convertCToD@Test1@@YAP8D@1@AEXXZP8C@1@AEXXZ@Z"{{.*}} {
694 // CHECK:   store
695 // CHECK:   load { i8*, i32, i32 }, { i8*, i32, i32 }* %{{.*}}, align 4
696 // CHECK:   extractvalue { i8*, i32, i32 } %{{.*}}, 0
697 // CHECK:   icmp ne i8* %{{.*}}, null
698 // CHECK:   br i1 %{{.*}}, label %{{.*}}, label %{{.*}}
699 //
700 //        memptr.convert:                                   ; preds = %entry
701 // CHECK:   extractvalue { i8*, i32, i32 } %{{.*}}, 0
702 // CHECK:   %[[nvoff:.*]] = extractvalue { i8*, i32, i32 } %{{.*}}, 1
703 // CHECK:   %[[vbidx:.*]] = extractvalue { i8*, i32, i32 } %{{.*}}, 2
704 // CHECK:   %[[is_nvbase:.*]] = icmp eq i32 %[[vbidx]], 0
705 // CHECK:   %[[nv_disp:.*]] = add nsw i32 %[[nvoff]], 4
706 // CHECK:   %[[nv_adj:.*]] = select i1 %[[is_nvbase]], i32 %[[nv_disp]], i32 0
707 // CHECK:   %[[dst_adj:.*]] = select i1 %[[is_nvbase]], i32 4, i32 0
708 // CHECK:   %[[adj:.*]] = sub nsw i32 %[[nv_adj]], %[[dst_adj]]
709 // CHECK:   insertvalue { i8*, i32, i32 } undef, i8* {{.*}}, 0
710 // CHECK:   insertvalue { i8*, i32, i32 } {{.*}}, i32 %[[adj]], 1
711 // CHECK:   insertvalue { i8*, i32, i32 } {{.*}}, i32 {{.*}}, 2
712 // CHECK:   br label
713 //
714 //        memptr.converted:                                 ; preds = %memptr.convert, %entry
715 // CHECK:   phi { i8*, i32, i32 } [ { i8* null, i32 0, i32 -1 }, {{.*}} ], [ {{.*}} ]
716 // CHECK: }
717 }
718 
719 }
720 
721 namespace Test2 {
722 // Test that we dynamically convert between different null reps.
723 
724 struct A { int a; };
725 struct B : A { int b; };
726 struct C : A {
727   int c;
728   virtual void hasVfPtr();
729 };
730 
reinterpret(int B::* mp)731 int A::*reinterpret(int B::*mp) {
732   return reinterpret_cast<int A::*>(mp);
733 // CHECK: define dso_local i32 @"?reinterpret@Test2@@YAPQA@1@HPQB@1@H@Z"{{.*}}  {
734 // CHECK-NOT: select
735 // CHECK:   ret i32
736 // CHECK: }
737 }
738 
reinterpret(int C::* mp)739 int A::*reinterpret(int C::*mp) {
740   return reinterpret_cast<int A::*>(mp);
741 // CHECK: define dso_local i32 @"?reinterpret@Test2@@YAPQA@1@HPQC@1@H@Z"{{.*}}  {
742 // CHECK:   %[[mp:.*]] = load i32, i32*
743 // CHECK:   %[[cmp:.*]] = icmp ne i32 %[[mp]], 0
744 // CHECK:   select i1 %[[cmp]], i32 %[[mp]], i32 -1
745 // CHECK: }
746 }
747 
748 }
749 
750 namespace Test3 {
751 // Make sure we cast 'this' to i8* before using GEP.
752 
753 struct A {
754   int a;
755   int b;
756 };
757 
load_data(A * a,int A::* mp)758 int *load_data(A *a, int A::*mp) {
759   return &(a->*mp);
760 // CHECK-LABEL: define dso_local i32* @"?load_data@Test3@@YAPAHPAUA@1@PQ21@H@Z"{{.*}}  {
761 // CHECK:    %[[a:.*]] = load %"struct.Test3::A"*, %"struct.Test3::A"** %{{.*}}, align 4
762 // CHECK:    %[[mp:.*]] = load i32, i32* %{{.*}}, align 4
763 // CHECK:    %[[a_i8:.*]] = bitcast %"struct.Test3::A"* %[[a]] to i8*
764 // CHECK:    getelementptr inbounds i8, i8* %[[a_i8]], i32 %[[mp]]
765 // CHECK: }
766 }
767 
768 }
769 
770 namespace Test4 {
771 
772 struct A        { virtual void f(); };
773 struct B        { virtual void g(); };
774 struct C : A, B { virtual void g(); };
775 
getmp()776 void (C::*getmp())() {
777   return &C::g;
778 }
779 // CHECK-LABEL: define dso_local i64 @"?getmp@Test4@@YAP8C@1@AEXXZXZ"()
780 // CHECK: store { i8*, i32 } { i8* bitcast (void (%"struct.Test4::C"*, ...)* @"??_9C@Test4@@$BA@AE" to i8*), i32 4 }, { i8*, i32 }* %{{.*}}
781 //
782 
783 // CHECK-LABEL: define linkonce_odr x86_thiscallcc void @"??_9C@Test4@@$BA@AE"(%"struct.Test4::C"* %this, ...) {{.*}} comdat
784 // CHECK-NOT:  getelementptr
785 // CHECK:  load void (%"struct.Test4::C"*, ...)**, void (%"struct.Test4::C"*, ...)*** %{{.*}}
786 // CHECK:  getelementptr inbounds void (%"struct.Test4::C"*, ...)*, void (%"struct.Test4::C"*, ...)** %{{.*}}, i64 0
787 // CHECK-NOT:  getelementptr
788 // CHECK:  musttail call x86_thiscallcc void (%"struct.Test4::C"*, ...) %
789 
790 }
791 
792 namespace pr20007 {
793 struct A {
794   void f();
795   void f(int);
796 };
797 struct B : public A {};
test()798 void test() { void (B::*a)() = &B::f; }
799 // CHECK-LABEL: define dso_local void @"?test@pr20007@@YAXXZ"
800 // CHECK: store i8* bitcast (void (%"struct.pr20007::A"*)* @"?f@A@pr20007@@QAEXXZ" to i8*)
801 }
802 
803 namespace pr20007_kw {
804 struct A {
805   void f();
806   void f(int);
807 };
808 struct __single_inheritance B;
809 struct B : public A {};
test()810 void test() { void (B::*a)() = &B::f; }
811 // CHECK-LABEL: define dso_local void @"?test@pr20007_kw@@YAXXZ"
812 // CHECK: store i8* bitcast (void (%"struct.pr20007_kw::A"*)* @"?f@A@pr20007_kw@@QAEXXZ" to i8*)
813 }
814 
815 namespace pr20007_pragma {
816 struct A {
817   void f();
818   void f(int);
819 };
820 struct B : public A {};
test()821 void test() { (void)(void (B::*)()) &B::f; }
822 #pragma pointers_to_members(full_generality, virtual_inheritance)
823 static_assert(sizeof(int B::*) == 4, "");
824 static_assert(sizeof(int A::*) == 4, "");
825 #pragma pointers_to_members(best_case)
826 // CHECK-LABEL: define dso_local void @"?test@pr20007_pragma@@YAXXZ"
827 }
828 
829 namespace pr20007_pragma2 {
830 struct A {
831 };
832 struct B : public A {
833   void f();
834 };
test()835 void test() { (void)&B::f; }
836 #pragma pointers_to_members(full_generality, virtual_inheritance)
837 static_assert(sizeof(int B::*) == 4, "");
838 static_assert(sizeof(int A::*) == 12, "");
839 #pragma pointers_to_members(best_case)
840 // CHECK-LABEL: define dso_local void @"?test@pr20007_pragma2@@YAXXZ"
841 }
842 
843 namespace pr23823 {
844 struct Base { void Method(); };
845 struct Child : Base {};
846 void use(void (Child::*const &)());
f()847 void f() { use(&Child::Method); }
848 #pragma pointers_to_members(full_generality, virtual_inheritance)
849 static_assert(sizeof(int Base::*) == 4, "");
850 static_assert(sizeof(int Child::*) == 4, "");
851 #pragma pointers_to_members(best_case)
852 }
853 
854 namespace pr19987 {
855 template <typename T>
856 struct S {
857   int T::*x;
858 };
859 
860 struct U : S<U> {};
861 
862 static_assert(sizeof(S<U>::x) == 12, "");
863 }
864 
865 #else
866 struct __virtual_inheritance A;
867 #ifdef MEMFUN
foo(A * a,int (A::* mp)())868 int foo(A *a, int (A::*mp)()) {
869     return (a->*mp)(); // expected-error{{requires a complete class type}}
870 }
871 #else
foo(A * a,int A::* mp)872 int foo(A *a, int A::*mp) {
873     return a->*mp; // expected-error{{requires a complete class type}}
874 }
875 #endif
876 #endif
877 
878 namespace pr23878 {
879 struct A { virtual void g(); };
880 struct B { virtual void f(); };
881 struct C : virtual B { void f(); };
882 struct D : A, C {};
883 
884 typedef void (D::*DMemPtrTy)();
885 
886 // CHECK-LABEL: define dso_local void @"?get_memptr@pr23878@@YAP8D@1@AEXXZXZ"
887 // CHECK: @"??_9C@pr23878@@$BA@AE" to i8*), i32 0, i32 4
get_memptr()888 DMemPtrTy get_memptr() { return &D::f; }
889 }
890 
891 class C {};
892 
893 typedef void (C::*f)();
894 
895 class CA : public C {
896 public:
897   void OnHelp(void);
898   int OnHelp(int);
899 };
900 
901 // CHECK-LABEL: foo_fun
foo_fun()902 void foo_fun() {
903   // CHECK: store i8* bitcast (void (%class.CA*)* @"?OnHelp@CA@@QAEXXZ" to i8*), i8**
904   f func = (f)&CA::OnHelp;
905 }
906 namespace PR24703 {
907 struct S;
908 
f(int S::* & p)909 void f(int S::*&p) {}
910 // CHECK-LABEL: define dso_local void @"?f@PR24703@@YAXAAPQS@1@H@Z"(
911 }
912 
913 namespace ReferenceToMPTWithIncompleteClass {
914 struct S;
915 struct J;
916 struct K;
917 extern K *k;
918 
919 // CHECK-LABEL: @"?f@ReferenceToMPTWithIncompleteClass@@YAIAAPQS@1@H@Z"(
920 // CHECK: ret i32 12
f(int S::* & p)921 unsigned f(int S::*&p) { return sizeof p; }
922 
923 // CHECK-LABEL: @"?g@ReferenceToMPTWithIncompleteClass@@YA_NAAPQJ@1@H0@Z"(
g(int J::* & p,int J::* & q)924 bool g(int J::*&p, int J::*&q) { return p == q; }
925 
926 // CHECK-LABEL: @"?h@ReferenceToMPTWithIncompleteClass@@YAHAAPQK@1@H@Z"(
h(int K::* & p)927 int h(int K::*&p) { return k->*p; }
928 }
929 
930 namespace PMFInTemplateArgument {
931 template <class C, int (C::*M)(int)>
932 void JSMethod();
933 class A {
934   int printd(int);
935   void printd();
936 };
printd()937 void A::printd() { JSMethod<A, &A::printd>(); }
938 // CHECK-LABEL: @"??$JSMethod@VA@PMFInTemplateArgument@@$1?printd@12@AAEHH@Z@PMFInTemplateArgument@@YAXXZ"(
939 }
940