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