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