1 //===--- CGVTables.cpp - Emit LLVM Code for C++ vtables -------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This contains code dealing with C++ code generation of virtual tables. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "CodeGenFunction.h" 15 #include "CGCXXABI.h" 16 #include "CodeGenModule.h" 17 #include "clang/AST/CXXInheritance.h" 18 #include "clang/AST/RecordLayout.h" 19 #include "clang/CodeGen/CGFunctionInfo.h" 20 #include "clang/Frontend/CodeGenOptions.h" 21 #include "llvm/ADT/DenseSet.h" 22 #include "llvm/ADT/SetVector.h" 23 #include "llvm/Support/Compiler.h" 24 #include "llvm/Support/Format.h" 25 #include "llvm/Transforms/Utils/Cloning.h" 26 #include <algorithm> 27 #include <cstdio> 28 29 using namespace clang; 30 using namespace CodeGen; 31 32 CodeGenVTables::CodeGenVTables(CodeGenModule &CGM) 33 : CGM(CGM), VTContext(CGM.getContext().getVTableContext()) {} 34 35 llvm::Constant *CodeGenModule::GetAddrOfThunk(GlobalDecl GD, 36 const ThunkInfo &Thunk) { 37 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 38 39 // Compute the mangled name. 40 SmallString<256> Name; 41 llvm::raw_svector_ostream Out(Name); 42 if (const CXXDestructorDecl* DD = dyn_cast<CXXDestructorDecl>(MD)) 43 getCXXABI().getMangleContext().mangleCXXDtorThunk(DD, GD.getDtorType(), 44 Thunk.This, Out); 45 else 46 getCXXABI().getMangleContext().mangleThunk(MD, Thunk, Out); 47 Out.flush(); 48 49 llvm::Type *Ty = getTypes().GetFunctionTypeForVTable(GD); 50 return GetOrCreateLLVMFunction(Name, Ty, GD, /*ForVTable=*/true, 51 /*DontDefer*/ true); 52 } 53 54 static void setThunkVisibility(CodeGenModule &CGM, const CXXMethodDecl *MD, 55 const ThunkInfo &Thunk, llvm::Function *Fn) { 56 CGM.setGlobalVisibility(Fn, MD); 57 } 58 59 #ifndef NDEBUG 60 static bool similar(const ABIArgInfo &infoL, CanQualType typeL, 61 const ABIArgInfo &infoR, CanQualType typeR) { 62 return (infoL.getKind() == infoR.getKind() && 63 (typeL == typeR || 64 (isa<PointerType>(typeL) && isa<PointerType>(typeR)) || 65 (isa<ReferenceType>(typeL) && isa<ReferenceType>(typeR)))); 66 } 67 #endif 68 69 static RValue PerformReturnAdjustment(CodeGenFunction &CGF, 70 QualType ResultType, RValue RV, 71 const ThunkInfo &Thunk) { 72 // Emit the return adjustment. 73 bool NullCheckValue = !ResultType->isReferenceType(); 74 75 llvm::BasicBlock *AdjustNull = 0; 76 llvm::BasicBlock *AdjustNotNull = 0; 77 llvm::BasicBlock *AdjustEnd = 0; 78 79 llvm::Value *ReturnValue = RV.getScalarVal(); 80 81 if (NullCheckValue) { 82 AdjustNull = CGF.createBasicBlock("adjust.null"); 83 AdjustNotNull = CGF.createBasicBlock("adjust.notnull"); 84 AdjustEnd = CGF.createBasicBlock("adjust.end"); 85 86 llvm::Value *IsNull = CGF.Builder.CreateIsNull(ReturnValue); 87 CGF.Builder.CreateCondBr(IsNull, AdjustNull, AdjustNotNull); 88 CGF.EmitBlock(AdjustNotNull); 89 } 90 91 ReturnValue = CGF.CGM.getCXXABI().performReturnAdjustment(CGF, ReturnValue, 92 Thunk.Return); 93 94 if (NullCheckValue) { 95 CGF.Builder.CreateBr(AdjustEnd); 96 CGF.EmitBlock(AdjustNull); 97 CGF.Builder.CreateBr(AdjustEnd); 98 CGF.EmitBlock(AdjustEnd); 99 100 llvm::PHINode *PHI = CGF.Builder.CreatePHI(ReturnValue->getType(), 2); 101 PHI->addIncoming(ReturnValue, AdjustNotNull); 102 PHI->addIncoming(llvm::Constant::getNullValue(ReturnValue->getType()), 103 AdjustNull); 104 ReturnValue = PHI; 105 } 106 107 return RValue::get(ReturnValue); 108 } 109 110 // This function does roughly the same thing as GenerateThunk, but in a 111 // very different way, so that va_start and va_end work correctly. 112 // FIXME: This function assumes "this" is the first non-sret LLVM argument of 113 // a function, and that there is an alloca built in the entry block 114 // for all accesses to "this". 115 // FIXME: This function assumes there is only one "ret" statement per function. 116 // FIXME: Cloning isn't correct in the presence of indirect goto! 117 // FIXME: This implementation of thunks bloats codesize by duplicating the 118 // function definition. There are alternatives: 119 // 1. Add some sort of stub support to LLVM for cases where we can 120 // do a this adjustment, then a sibcall. 121 // 2. We could transform the definition to take a va_list instead of an 122 // actual variable argument list, then have the thunks (including a 123 // no-op thunk for the regular definition) call va_start/va_end. 124 // There's a bit of per-call overhead for this solution, but it's 125 // better for codesize if the definition is long. 126 void CodeGenFunction::GenerateVarArgsThunk( 127 llvm::Function *Fn, 128 const CGFunctionInfo &FnInfo, 129 GlobalDecl GD, const ThunkInfo &Thunk) { 130 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 131 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); 132 QualType ResultType = FPT->getReturnType(); 133 134 // Get the original function 135 assert(FnInfo.isVariadic()); 136 llvm::Type *Ty = CGM.getTypes().GetFunctionType(FnInfo); 137 llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true); 138 llvm::Function *BaseFn = cast<llvm::Function>(Callee); 139 140 // Clone to thunk. 141 llvm::ValueToValueMapTy VMap; 142 llvm::Function *NewFn = llvm::CloneFunction(BaseFn, VMap, 143 /*ModuleLevelChanges=*/false); 144 CGM.getModule().getFunctionList().push_back(NewFn); 145 Fn->replaceAllUsesWith(NewFn); 146 NewFn->takeName(Fn); 147 Fn->eraseFromParent(); 148 Fn = NewFn; 149 150 // "Initialize" CGF (minimally). 151 CurFn = Fn; 152 153 // Get the "this" value 154 llvm::Function::arg_iterator AI = Fn->arg_begin(); 155 if (CGM.ReturnTypeUsesSRet(FnInfo)) 156 ++AI; 157 158 // Find the first store of "this", which will be to the alloca associated 159 // with "this". 160 llvm::Value *ThisPtr = &*AI; 161 llvm::BasicBlock *EntryBB = Fn->begin(); 162 llvm::Instruction *ThisStore = 0; 163 for (llvm::BasicBlock::iterator I = EntryBB->begin(), E = EntryBB->end(); 164 I != E; I++) { 165 if (isa<llvm::StoreInst>(I) && I->getOperand(0) == ThisPtr) { 166 ThisStore = cast<llvm::StoreInst>(I); 167 break; 168 } 169 } 170 assert(ThisStore && "Store of this should be in entry block?"); 171 // Adjust "this", if necessary. 172 Builder.SetInsertPoint(ThisStore); 173 llvm::Value *AdjustedThisPtr = 174 CGM.getCXXABI().performThisAdjustment(*this, ThisPtr, Thunk.This); 175 ThisStore->setOperand(0, AdjustedThisPtr); 176 177 if (!Thunk.Return.isEmpty()) { 178 // Fix up the returned value, if necessary. 179 for (llvm::Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++) { 180 llvm::Instruction *T = I->getTerminator(); 181 if (isa<llvm::ReturnInst>(T)) { 182 RValue RV = RValue::get(T->getOperand(0)); 183 T->eraseFromParent(); 184 Builder.SetInsertPoint(&*I); 185 RV = PerformReturnAdjustment(*this, ResultType, RV, Thunk); 186 Builder.CreateRet(RV.getScalarVal()); 187 break; 188 } 189 } 190 } 191 } 192 193 void CodeGenFunction::StartThunk(llvm::Function *Fn, GlobalDecl GD, 194 const CGFunctionInfo &FnInfo) { 195 assert(!CurGD.getDecl() && "CurGD was already set!"); 196 CurGD = GD; 197 198 // Build FunctionArgs. 199 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 200 QualType ThisType = MD->getThisType(getContext()); 201 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); 202 QualType ResultType = 203 CGM.getCXXABI().HasThisReturn(GD) ? ThisType : FPT->getReturnType(); 204 FunctionArgList FunctionArgs; 205 206 // Create the implicit 'this' parameter declaration. 207 CGM.getCXXABI().buildThisParam(*this, FunctionArgs); 208 209 // Add the rest of the parameters. 210 for (FunctionDecl::param_const_iterator I = MD->param_begin(), 211 E = MD->param_end(); 212 I != E; ++I) 213 FunctionArgs.push_back(*I); 214 215 if (isa<CXXDestructorDecl>(MD)) 216 CGM.getCXXABI().addImplicitStructorParams(*this, ResultType, FunctionArgs); 217 218 // Start defining the function. 219 StartFunction(GlobalDecl(), ResultType, Fn, FnInfo, FunctionArgs, 220 SourceLocation()); 221 222 // Since we didn't pass a GlobalDecl to StartFunction, do this ourselves. 223 CGM.getCXXABI().EmitInstanceFunctionProlog(*this); 224 CXXThisValue = CXXABIThisValue; 225 } 226 227 void CodeGenFunction::EmitCallAndReturnForThunk(GlobalDecl GD, 228 llvm::Value *Callee, 229 const ThunkInfo *Thunk) { 230 assert(isa<CXXMethodDecl>(CurGD.getDecl()) && 231 "Please use a new CGF for this thunk"); 232 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 233 234 // Adjust the 'this' pointer if necessary 235 llvm::Value *AdjustedThisPtr = Thunk ? CGM.getCXXABI().performThisAdjustment( 236 *this, LoadCXXThis(), Thunk->This) 237 : LoadCXXThis(); 238 239 // Start building CallArgs. 240 CallArgList CallArgs; 241 QualType ThisType = MD->getThisType(getContext()); 242 CallArgs.add(RValue::get(AdjustedThisPtr), ThisType); 243 244 if (isa<CXXDestructorDecl>(MD)) 245 CGM.getCXXABI().adjustCallArgsForDestructorThunk(*this, GD, CallArgs); 246 247 // Add the rest of the arguments. 248 for (FunctionDecl::param_const_iterator I = MD->param_begin(), 249 E = MD->param_end(); I != E; ++I) 250 EmitDelegateCallArg(CallArgs, *I, (*I)->getLocStart()); 251 252 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); 253 254 #ifndef NDEBUG 255 const CGFunctionInfo &CallFnInfo = 256 CGM.getTypes().arrangeCXXMethodCall(CallArgs, FPT, 257 RequiredArgs::forPrototypePlus(FPT, 1)); 258 assert(CallFnInfo.getRegParm() == CurFnInfo->getRegParm() && 259 CallFnInfo.isNoReturn() == CurFnInfo->isNoReturn() && 260 CallFnInfo.getCallingConvention() == CurFnInfo->getCallingConvention()); 261 assert(isa<CXXDestructorDecl>(MD) || // ignore dtor return types 262 similar(CallFnInfo.getReturnInfo(), CallFnInfo.getReturnType(), 263 CurFnInfo->getReturnInfo(), CurFnInfo->getReturnType())); 264 assert(CallFnInfo.arg_size() == CurFnInfo->arg_size()); 265 for (unsigned i = 0, e = CurFnInfo->arg_size(); i != e; ++i) 266 assert(similar(CallFnInfo.arg_begin()[i].info, 267 CallFnInfo.arg_begin()[i].type, 268 CurFnInfo->arg_begin()[i].info, 269 CurFnInfo->arg_begin()[i].type)); 270 #endif 271 272 // Determine whether we have a return value slot to use. 273 QualType ResultType = 274 CGM.getCXXABI().HasThisReturn(GD) ? ThisType : FPT->getReturnType(); 275 ReturnValueSlot Slot; 276 if (!ResultType->isVoidType() && 277 CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect && 278 !hasScalarEvaluationKind(CurFnInfo->getReturnType())) 279 Slot = ReturnValueSlot(ReturnValue, ResultType.isVolatileQualified()); 280 281 // Now emit our call. 282 RValue RV = EmitCall(*CurFnInfo, Callee, Slot, CallArgs, MD); 283 284 // Consider return adjustment if we have ThunkInfo. 285 if (Thunk && !Thunk->Return.isEmpty()) 286 RV = PerformReturnAdjustment(*this, ResultType, RV, *Thunk); 287 288 // Emit return. 289 if (!ResultType->isVoidType() && Slot.isNull()) 290 CGM.getCXXABI().EmitReturnFromThunk(*this, RV, ResultType); 291 292 // Disable the final ARC autorelease. 293 AutoreleaseResult = false; 294 295 FinishFunction(); 296 } 297 298 void CodeGenFunction::GenerateThunk(llvm::Function *Fn, 299 const CGFunctionInfo &FnInfo, 300 GlobalDecl GD, const ThunkInfo &Thunk) { 301 StartThunk(Fn, GD, FnInfo); 302 303 // Get our callee. 304 llvm::Type *Ty = 305 CGM.getTypes().GetFunctionType(CGM.getTypes().arrangeGlobalDeclaration(GD)); 306 llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true); 307 308 // Make the call and return the result. 309 EmitCallAndReturnForThunk(GD, Callee, &Thunk); 310 311 // Set the right linkage. 312 CGM.setFunctionLinkage(GD, Fn); 313 314 // Set the right visibility. 315 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 316 setThunkVisibility(CGM, MD, Thunk, Fn); 317 } 318 319 void CodeGenVTables::emitThunk(GlobalDecl GD, const ThunkInfo &Thunk, 320 bool ForVTable) { 321 const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeGlobalDeclaration(GD); 322 323 // FIXME: re-use FnInfo in this computation. 324 llvm::Constant *Entry = CGM.GetAddrOfThunk(GD, Thunk); 325 326 // Strip off a bitcast if we got one back. 327 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { 328 assert(CE->getOpcode() == llvm::Instruction::BitCast); 329 Entry = CE->getOperand(0); 330 } 331 332 // There's already a declaration with the same name, check if it has the same 333 // type or if we need to replace it. 334 if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() != 335 CGM.getTypes().GetFunctionTypeForVTable(GD)) { 336 llvm::GlobalValue *OldThunkFn = cast<llvm::GlobalValue>(Entry); 337 338 // If the types mismatch then we have to rewrite the definition. 339 assert(OldThunkFn->isDeclaration() && 340 "Shouldn't replace non-declaration"); 341 342 // Remove the name from the old thunk function and get a new thunk. 343 OldThunkFn->setName(StringRef()); 344 Entry = CGM.GetAddrOfThunk(GD, Thunk); 345 346 // If needed, replace the old thunk with a bitcast. 347 if (!OldThunkFn->use_empty()) { 348 llvm::Constant *NewPtrForOldDecl = 349 llvm::ConstantExpr::getBitCast(Entry, OldThunkFn->getType()); 350 OldThunkFn->replaceAllUsesWith(NewPtrForOldDecl); 351 } 352 353 // Remove the old thunk. 354 OldThunkFn->eraseFromParent(); 355 } 356 357 llvm::Function *ThunkFn = cast<llvm::Function>(Entry); 358 bool ABIHasKeyFunctions = CGM.getTarget().getCXXABI().hasKeyFunctions(); 359 bool UseAvailableExternallyLinkage = ForVTable && ABIHasKeyFunctions; 360 361 if (!ThunkFn->isDeclaration()) { 362 if (!ABIHasKeyFunctions || UseAvailableExternallyLinkage) { 363 // There is already a thunk emitted for this function, do nothing. 364 return; 365 } 366 367 // Change the linkage. 368 CGM.setFunctionLinkage(GD, ThunkFn); 369 return; 370 } 371 372 CGM.SetLLVMFunctionAttributesForDefinition(GD.getDecl(), ThunkFn); 373 374 if (ThunkFn->isVarArg()) { 375 // Varargs thunks are special; we can't just generate a call because 376 // we can't copy the varargs. Our implementation is rather 377 // expensive/sucky at the moment, so don't generate the thunk unless 378 // we have to. 379 // FIXME: Do something better here; GenerateVarArgsThunk is extremely ugly. 380 if (!UseAvailableExternallyLinkage) { 381 CodeGenFunction(CGM).GenerateVarArgsThunk(ThunkFn, FnInfo, GD, Thunk); 382 CGM.getCXXABI().setThunkLinkage(ThunkFn, ForVTable); 383 } 384 } else { 385 // Normal thunk body generation. 386 CodeGenFunction(CGM).GenerateThunk(ThunkFn, FnInfo, GD, Thunk); 387 CGM.getCXXABI().setThunkLinkage(ThunkFn, ForVTable); 388 } 389 } 390 391 void CodeGenVTables::maybeEmitThunkForVTable(GlobalDecl GD, 392 const ThunkInfo &Thunk) { 393 // If the ABI has key functions, only the TU with the key function should emit 394 // the thunk. However, we can allow inlining of thunks if we emit them with 395 // available_externally linkage together with vtables when optimizations are 396 // enabled. 397 if (CGM.getTarget().getCXXABI().hasKeyFunctions() && 398 !CGM.getCodeGenOpts().OptimizationLevel) 399 return; 400 401 // We can't emit thunks for member functions with incomplete types. 402 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 403 if (!CGM.getTypes().isFuncTypeConvertible( 404 MD->getType()->castAs<FunctionType>())) 405 return; 406 407 emitThunk(GD, Thunk, /*ForVTable=*/true); 408 } 409 410 void CodeGenVTables::EmitThunks(GlobalDecl GD) 411 { 412 const CXXMethodDecl *MD = 413 cast<CXXMethodDecl>(GD.getDecl())->getCanonicalDecl(); 414 415 // We don't need to generate thunks for the base destructor. 416 if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base) 417 return; 418 419 const VTableContextBase::ThunkInfoVectorTy *ThunkInfoVector = 420 VTContext->getThunkInfo(GD); 421 422 if (!ThunkInfoVector) 423 return; 424 425 for (unsigned I = 0, E = ThunkInfoVector->size(); I != E; ++I) 426 emitThunk(GD, (*ThunkInfoVector)[I], /*ForVTable=*/false); 427 } 428 429 llvm::Constant * 430 CodeGenVTables::CreateVTableInitializer(const CXXRecordDecl *RD, 431 const VTableComponent *Components, 432 unsigned NumComponents, 433 const VTableLayout::VTableThunkTy *VTableThunks, 434 unsigned NumVTableThunks) { 435 SmallVector<llvm::Constant *, 64> Inits; 436 437 llvm::Type *Int8PtrTy = CGM.Int8PtrTy; 438 439 llvm::Type *PtrDiffTy = 440 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType()); 441 442 QualType ClassType = CGM.getContext().getTagDeclType(RD); 443 llvm::Constant *RTTI = CGM.GetAddrOfRTTIDescriptor(ClassType); 444 445 unsigned NextVTableThunkIndex = 0; 446 447 llvm::Constant *PureVirtualFn = 0, *DeletedVirtualFn = 0; 448 449 for (unsigned I = 0; I != NumComponents; ++I) { 450 VTableComponent Component = Components[I]; 451 452 llvm::Constant *Init = 0; 453 454 switch (Component.getKind()) { 455 case VTableComponent::CK_VCallOffset: 456 Init = llvm::ConstantInt::get(PtrDiffTy, 457 Component.getVCallOffset().getQuantity()); 458 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy); 459 break; 460 case VTableComponent::CK_VBaseOffset: 461 Init = llvm::ConstantInt::get(PtrDiffTy, 462 Component.getVBaseOffset().getQuantity()); 463 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy); 464 break; 465 case VTableComponent::CK_OffsetToTop: 466 Init = llvm::ConstantInt::get(PtrDiffTy, 467 Component.getOffsetToTop().getQuantity()); 468 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy); 469 break; 470 case VTableComponent::CK_RTTI: 471 Init = llvm::ConstantExpr::getBitCast(RTTI, Int8PtrTy); 472 break; 473 case VTableComponent::CK_FunctionPointer: 474 case VTableComponent::CK_CompleteDtorPointer: 475 case VTableComponent::CK_DeletingDtorPointer: { 476 GlobalDecl GD; 477 478 // Get the right global decl. 479 switch (Component.getKind()) { 480 default: 481 llvm_unreachable("Unexpected vtable component kind"); 482 case VTableComponent::CK_FunctionPointer: 483 GD = Component.getFunctionDecl(); 484 break; 485 case VTableComponent::CK_CompleteDtorPointer: 486 GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Complete); 487 break; 488 case VTableComponent::CK_DeletingDtorPointer: 489 GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Deleting); 490 break; 491 } 492 493 if (cast<CXXMethodDecl>(GD.getDecl())->isPure()) { 494 // We have a pure virtual member function. 495 if (!PureVirtualFn) { 496 llvm::FunctionType *Ty = 497 llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false); 498 StringRef PureCallName = CGM.getCXXABI().GetPureVirtualCallName(); 499 PureVirtualFn = CGM.CreateRuntimeFunction(Ty, PureCallName); 500 PureVirtualFn = llvm::ConstantExpr::getBitCast(PureVirtualFn, 501 CGM.Int8PtrTy); 502 } 503 Init = PureVirtualFn; 504 } else if (cast<CXXMethodDecl>(GD.getDecl())->isDeleted()) { 505 if (!DeletedVirtualFn) { 506 llvm::FunctionType *Ty = 507 llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false); 508 StringRef DeletedCallName = 509 CGM.getCXXABI().GetDeletedVirtualCallName(); 510 DeletedVirtualFn = CGM.CreateRuntimeFunction(Ty, DeletedCallName); 511 DeletedVirtualFn = llvm::ConstantExpr::getBitCast(DeletedVirtualFn, 512 CGM.Int8PtrTy); 513 } 514 Init = DeletedVirtualFn; 515 } else { 516 // Check if we should use a thunk. 517 if (NextVTableThunkIndex < NumVTableThunks && 518 VTableThunks[NextVTableThunkIndex].first == I) { 519 const ThunkInfo &Thunk = VTableThunks[NextVTableThunkIndex].second; 520 521 maybeEmitThunkForVTable(GD, Thunk); 522 Init = CGM.GetAddrOfThunk(GD, Thunk); 523 524 NextVTableThunkIndex++; 525 } else { 526 llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVTable(GD); 527 528 Init = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true); 529 } 530 531 Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy); 532 } 533 break; 534 } 535 536 case VTableComponent::CK_UnusedFunctionPointer: 537 Init = llvm::ConstantExpr::getNullValue(Int8PtrTy); 538 break; 539 }; 540 541 Inits.push_back(Init); 542 } 543 544 llvm::ArrayType *ArrayType = llvm::ArrayType::get(Int8PtrTy, NumComponents); 545 return llvm::ConstantArray::get(ArrayType, Inits); 546 } 547 548 llvm::GlobalVariable * 549 CodeGenVTables::GenerateConstructionVTable(const CXXRecordDecl *RD, 550 const BaseSubobject &Base, 551 bool BaseIsVirtual, 552 llvm::GlobalVariable::LinkageTypes Linkage, 553 VTableAddressPointsMapTy& AddressPoints) { 554 if (CGDebugInfo *DI = CGM.getModuleDebugInfo()) 555 DI->completeClassData(Base.getBase()); 556 557 std::unique_ptr<VTableLayout> VTLayout( 558 getItaniumVTableContext().createConstructionVTableLayout( 559 Base.getBase(), Base.getBaseOffset(), BaseIsVirtual, RD)); 560 561 // Add the address points. 562 AddressPoints = VTLayout->getAddressPoints(); 563 564 // Get the mangled construction vtable name. 565 SmallString<256> OutName; 566 llvm::raw_svector_ostream Out(OutName); 567 cast<ItaniumMangleContext>(CGM.getCXXABI().getMangleContext()) 568 .mangleCXXCtorVTable(RD, Base.getBaseOffset().getQuantity(), 569 Base.getBase(), Out); 570 Out.flush(); 571 StringRef Name = OutName.str(); 572 573 llvm::ArrayType *ArrayType = 574 llvm::ArrayType::get(CGM.Int8PtrTy, VTLayout->getNumVTableComponents()); 575 576 // Construction vtable symbols are not part of the Itanium ABI, so we cannot 577 // guarantee that they actually will be available externally. Instead, when 578 // emitting an available_externally VTT, we provide references to an internal 579 // linkage construction vtable. The ABI only requires complete-object vtables 580 // to be the same for all instances of a type, not construction vtables. 581 if (Linkage == llvm::GlobalVariable::AvailableExternallyLinkage) 582 Linkage = llvm::GlobalVariable::InternalLinkage; 583 584 // Create the variable that will hold the construction vtable. 585 llvm::GlobalVariable *VTable = 586 CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType, Linkage); 587 CGM.setGlobalVisibility(VTable, RD); 588 589 // V-tables are always unnamed_addr. 590 VTable->setUnnamedAddr(true); 591 592 // Create and set the initializer. 593 llvm::Constant *Init = 594 CreateVTableInitializer(Base.getBase(), 595 VTLayout->vtable_component_begin(), 596 VTLayout->getNumVTableComponents(), 597 VTLayout->vtable_thunk_begin(), 598 VTLayout->getNumVTableThunks()); 599 VTable->setInitializer(Init); 600 601 return VTable; 602 } 603 604 /// Compute the required linkage of the v-table for the given class. 605 /// 606 /// Note that we only call this at the end of the translation unit. 607 llvm::GlobalVariable::LinkageTypes 608 CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) { 609 if (!RD->isExternallyVisible()) 610 return llvm::GlobalVariable::InternalLinkage; 611 612 // We're at the end of the translation unit, so the current key 613 // function is fully correct. 614 if (const CXXMethodDecl *keyFunction = Context.getCurrentKeyFunction(RD)) { 615 // If this class has a key function, use that to determine the 616 // linkage of the vtable. 617 const FunctionDecl *def = 0; 618 if (keyFunction->hasBody(def)) 619 keyFunction = cast<CXXMethodDecl>(def); 620 621 switch (keyFunction->getTemplateSpecializationKind()) { 622 case TSK_Undeclared: 623 case TSK_ExplicitSpecialization: 624 assert(def && "Should not have been asked to emit this"); 625 if (keyFunction->isInlined()) 626 return !Context.getLangOpts().AppleKext ? 627 llvm::GlobalVariable::LinkOnceODRLinkage : 628 llvm::Function::InternalLinkage; 629 630 return llvm::GlobalVariable::ExternalLinkage; 631 632 case TSK_ImplicitInstantiation: 633 return !Context.getLangOpts().AppleKext ? 634 llvm::GlobalVariable::LinkOnceODRLinkage : 635 llvm::Function::InternalLinkage; 636 637 case TSK_ExplicitInstantiationDefinition: 638 return !Context.getLangOpts().AppleKext ? 639 llvm::GlobalVariable::WeakODRLinkage : 640 llvm::Function::InternalLinkage; 641 642 case TSK_ExplicitInstantiationDeclaration: 643 llvm_unreachable("Should not have been asked to emit this"); 644 } 645 } 646 647 // -fapple-kext mode does not support weak linkage, so we must use 648 // internal linkage. 649 if (Context.getLangOpts().AppleKext) 650 return llvm::Function::InternalLinkage; 651 652 switch (RD->getTemplateSpecializationKind()) { 653 case TSK_Undeclared: 654 case TSK_ExplicitSpecialization: 655 case TSK_ImplicitInstantiation: 656 return llvm::GlobalVariable::LinkOnceODRLinkage; 657 658 case TSK_ExplicitInstantiationDeclaration: 659 llvm_unreachable("Should not have been asked to emit this"); 660 661 case TSK_ExplicitInstantiationDefinition: 662 return llvm::GlobalVariable::WeakODRLinkage; 663 } 664 665 llvm_unreachable("Invalid TemplateSpecializationKind!"); 666 } 667 668 /// This is a callback from Sema to tell us that it believes that a 669 /// particular v-table is required to be emitted in this translation 670 /// unit. 671 /// 672 /// The reason we don't simply trust this callback is because Sema 673 /// will happily report that something is used even when it's used 674 /// only in code that we don't actually have to emit. 675 /// 676 /// \param isRequired - if true, the v-table is mandatory, e.g. 677 /// because the translation unit defines the key function 678 void CodeGenModule::EmitVTable(CXXRecordDecl *theClass, bool isRequired) { 679 if (!isRequired) return; 680 681 VTables.GenerateClassData(theClass); 682 } 683 684 void 685 CodeGenVTables::GenerateClassData(const CXXRecordDecl *RD) { 686 if (CGDebugInfo *DI = CGM.getModuleDebugInfo()) 687 DI->completeClassData(RD); 688 689 if (RD->getNumVBases()) 690 CGM.getCXXABI().emitVirtualInheritanceTables(RD); 691 692 CGM.getCXXABI().emitVTableDefinitions(*this, RD); 693 } 694 695 /// At this point in the translation unit, does it appear that can we 696 /// rely on the vtable being defined elsewhere in the program? 697 /// 698 /// The response is really only definitive when called at the end of 699 /// the translation unit. 700 /// 701 /// The only semantic restriction here is that the object file should 702 /// not contain a v-table definition when that v-table is defined 703 /// strongly elsewhere. Otherwise, we'd just like to avoid emitting 704 /// v-tables when unnecessary. 705 bool CodeGenVTables::isVTableExternal(const CXXRecordDecl *RD) { 706 assert(RD->isDynamicClass() && "Non-dynamic classes have no VTable."); 707 708 // If we have an explicit instantiation declaration (and not a 709 // definition), the v-table is defined elsewhere. 710 TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind(); 711 if (TSK == TSK_ExplicitInstantiationDeclaration) 712 return true; 713 714 // Otherwise, if the class is an instantiated template, the 715 // v-table must be defined here. 716 if (TSK == TSK_ImplicitInstantiation || 717 TSK == TSK_ExplicitInstantiationDefinition) 718 return false; 719 720 // Otherwise, if the class doesn't have a key function (possibly 721 // anymore), the v-table must be defined here. 722 const CXXMethodDecl *keyFunction = CGM.getContext().getCurrentKeyFunction(RD); 723 if (!keyFunction) 724 return false; 725 726 // Otherwise, if we don't have a definition of the key function, the 727 // v-table must be defined somewhere else. 728 return !keyFunction->hasBody(); 729 } 730 731 /// Given that we're currently at the end of the translation unit, and 732 /// we've emitted a reference to the v-table for this class, should 733 /// we define that v-table? 734 static bool shouldEmitVTableAtEndOfTranslationUnit(CodeGenModule &CGM, 735 const CXXRecordDecl *RD) { 736 return !CGM.getVTables().isVTableExternal(RD); 737 } 738 739 /// Given that at some point we emitted a reference to one or more 740 /// v-tables, and that we are now at the end of the translation unit, 741 /// decide whether we should emit them. 742 void CodeGenModule::EmitDeferredVTables() { 743 #ifndef NDEBUG 744 // Remember the size of DeferredVTables, because we're going to assume 745 // that this entire operation doesn't modify it. 746 size_t savedSize = DeferredVTables.size(); 747 #endif 748 749 typedef std::vector<const CXXRecordDecl *>::const_iterator const_iterator; 750 for (const_iterator i = DeferredVTables.begin(), 751 e = DeferredVTables.end(); i != e; ++i) { 752 const CXXRecordDecl *RD = *i; 753 if (shouldEmitVTableAtEndOfTranslationUnit(*this, RD)) 754 VTables.GenerateClassData(RD); 755 } 756 757 assert(savedSize == DeferredVTables.size() && 758 "deferred extra v-tables during v-table emission?"); 759 DeferredVTables.clear(); 760 } 761