clang API Documentation
00001 //===--- CGVTables.cpp - Emit LLVM Code for C++ vtables -------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This contains code dealing with C++ code generation of virtual tables. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "CodeGenFunction.h" 00015 #include "CGCXXABI.h" 00016 #include "CodeGenModule.h" 00017 #include "clang/AST/CXXInheritance.h" 00018 #include "clang/AST/RecordLayout.h" 00019 #include "clang/CodeGen/CGFunctionInfo.h" 00020 #include "clang/Frontend/CodeGenOptions.h" 00021 #include "llvm/ADT/DenseSet.h" 00022 #include "llvm/ADT/SetVector.h" 00023 #include "llvm/Support/Compiler.h" 00024 #include "llvm/Support/Format.h" 00025 #include "llvm/Transforms/Utils/Cloning.h" 00026 #include <algorithm> 00027 #include <cstdio> 00028 00029 using namespace clang; 00030 using namespace CodeGen; 00031 00032 CodeGenVTables::CodeGenVTables(CodeGenModule &CGM) 00033 : CGM(CGM), VTContext(CGM.getContext().getVTableContext()) {} 00034 00035 llvm::Constant *CodeGenModule::GetAddrOfThunk(GlobalDecl GD, 00036 const ThunkInfo &Thunk) { 00037 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 00038 00039 // Compute the mangled name. 00040 SmallString<256> Name; 00041 llvm::raw_svector_ostream Out(Name); 00042 if (const CXXDestructorDecl* DD = dyn_cast<CXXDestructorDecl>(MD)) 00043 getCXXABI().getMangleContext().mangleCXXDtorThunk(DD, GD.getDtorType(), 00044 Thunk.This, Out); 00045 else 00046 getCXXABI().getMangleContext().mangleThunk(MD, Thunk, Out); 00047 Out.flush(); 00048 00049 llvm::Type *Ty = getTypes().GetFunctionTypeForVTable(GD); 00050 return GetOrCreateLLVMFunction(Name, Ty, GD, /*ForVTable=*/true, 00051 /*DontDefer=*/true, /*IsThunk=*/true); 00052 } 00053 00054 static void setThunkVisibility(CodeGenModule &CGM, const CXXMethodDecl *MD, 00055 const ThunkInfo &Thunk, llvm::Function *Fn) { 00056 CGM.setGlobalVisibility(Fn, MD); 00057 } 00058 00059 #ifndef NDEBUG 00060 static bool similar(const ABIArgInfo &infoL, CanQualType typeL, 00061 const ABIArgInfo &infoR, CanQualType typeR) { 00062 return (infoL.getKind() == infoR.getKind() && 00063 (typeL == typeR || 00064 (isa<PointerType>(typeL) && isa<PointerType>(typeR)) || 00065 (isa<ReferenceType>(typeL) && isa<ReferenceType>(typeR)))); 00066 } 00067 #endif 00068 00069 static RValue PerformReturnAdjustment(CodeGenFunction &CGF, 00070 QualType ResultType, RValue RV, 00071 const ThunkInfo &Thunk) { 00072 // Emit the return adjustment. 00073 bool NullCheckValue = !ResultType->isReferenceType(); 00074 00075 llvm::BasicBlock *AdjustNull = nullptr; 00076 llvm::BasicBlock *AdjustNotNull = nullptr; 00077 llvm::BasicBlock *AdjustEnd = nullptr; 00078 00079 llvm::Value *ReturnValue = RV.getScalarVal(); 00080 00081 if (NullCheckValue) { 00082 AdjustNull = CGF.createBasicBlock("adjust.null"); 00083 AdjustNotNull = CGF.createBasicBlock("adjust.notnull"); 00084 AdjustEnd = CGF.createBasicBlock("adjust.end"); 00085 00086 llvm::Value *IsNull = CGF.Builder.CreateIsNull(ReturnValue); 00087 CGF.Builder.CreateCondBr(IsNull, AdjustNull, AdjustNotNull); 00088 CGF.EmitBlock(AdjustNotNull); 00089 } 00090 00091 ReturnValue = CGF.CGM.getCXXABI().performReturnAdjustment(CGF, ReturnValue, 00092 Thunk.Return); 00093 00094 if (NullCheckValue) { 00095 CGF.Builder.CreateBr(AdjustEnd); 00096 CGF.EmitBlock(AdjustNull); 00097 CGF.Builder.CreateBr(AdjustEnd); 00098 CGF.EmitBlock(AdjustEnd); 00099 00100 llvm::PHINode *PHI = CGF.Builder.CreatePHI(ReturnValue->getType(), 2); 00101 PHI->addIncoming(ReturnValue, AdjustNotNull); 00102 PHI->addIncoming(llvm::Constant::getNullValue(ReturnValue->getType()), 00103 AdjustNull); 00104 ReturnValue = PHI; 00105 } 00106 00107 return RValue::get(ReturnValue); 00108 } 00109 00110 // This function does roughly the same thing as GenerateThunk, but in a 00111 // very different way, so that va_start and va_end work correctly. 00112 // FIXME: This function assumes "this" is the first non-sret LLVM argument of 00113 // a function, and that there is an alloca built in the entry block 00114 // for all accesses to "this". 00115 // FIXME: This function assumes there is only one "ret" statement per function. 00116 // FIXME: Cloning isn't correct in the presence of indirect goto! 00117 // FIXME: This implementation of thunks bloats codesize by duplicating the 00118 // function definition. There are alternatives: 00119 // 1. Add some sort of stub support to LLVM for cases where we can 00120 // do a this adjustment, then a sibcall. 00121 // 2. We could transform the definition to take a va_list instead of an 00122 // actual variable argument list, then have the thunks (including a 00123 // no-op thunk for the regular definition) call va_start/va_end. 00124 // There's a bit of per-call overhead for this solution, but it's 00125 // better for codesize if the definition is long. 00126 void CodeGenFunction::GenerateVarArgsThunk( 00127 llvm::Function *Fn, 00128 const CGFunctionInfo &FnInfo, 00129 GlobalDecl GD, const ThunkInfo &Thunk) { 00130 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 00131 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); 00132 QualType ResultType = FPT->getReturnType(); 00133 00134 // Get the original function 00135 assert(FnInfo.isVariadic()); 00136 llvm::Type *Ty = CGM.getTypes().GetFunctionType(FnInfo); 00137 llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true); 00138 llvm::Function *BaseFn = cast<llvm::Function>(Callee); 00139 00140 // Clone to thunk. 00141 llvm::ValueToValueMapTy VMap; 00142 llvm::Function *NewFn = llvm::CloneFunction(BaseFn, VMap, 00143 /*ModuleLevelChanges=*/false); 00144 CGM.getModule().getFunctionList().push_back(NewFn); 00145 Fn->replaceAllUsesWith(NewFn); 00146 NewFn->takeName(Fn); 00147 Fn->eraseFromParent(); 00148 Fn = NewFn; 00149 00150 // "Initialize" CGF (minimally). 00151 CurFn = Fn; 00152 00153 // Get the "this" value 00154 llvm::Function::arg_iterator AI = Fn->arg_begin(); 00155 if (CGM.ReturnTypeUsesSRet(FnInfo)) 00156 ++AI; 00157 00158 // Find the first store of "this", which will be to the alloca associated 00159 // with "this". 00160 llvm::Value *ThisPtr = &*AI; 00161 llvm::BasicBlock *EntryBB = Fn->begin(); 00162 llvm::Instruction *ThisStore = nullptr; 00163 for (llvm::BasicBlock::iterator I = EntryBB->begin(), E = EntryBB->end(); 00164 I != E; I++) { 00165 if (isa<llvm::StoreInst>(I) && I->getOperand(0) == ThisPtr) { 00166 ThisStore = cast<llvm::StoreInst>(I); 00167 break; 00168 } 00169 } 00170 assert(ThisStore && "Store of this should be in entry block?"); 00171 // Adjust "this", if necessary. 00172 Builder.SetInsertPoint(ThisStore); 00173 llvm::Value *AdjustedThisPtr = 00174 CGM.getCXXABI().performThisAdjustment(*this, ThisPtr, Thunk.This); 00175 ThisStore->setOperand(0, AdjustedThisPtr); 00176 00177 if (!Thunk.Return.isEmpty()) { 00178 // Fix up the returned value, if necessary. 00179 for (llvm::Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++) { 00180 llvm::Instruction *T = I->getTerminator(); 00181 if (isa<llvm::ReturnInst>(T)) { 00182 RValue RV = RValue::get(T->getOperand(0)); 00183 T->eraseFromParent(); 00184 Builder.SetInsertPoint(&*I); 00185 RV = PerformReturnAdjustment(*this, ResultType, RV, Thunk); 00186 Builder.CreateRet(RV.getScalarVal()); 00187 break; 00188 } 00189 } 00190 } 00191 } 00192 00193 void CodeGenFunction::StartThunk(llvm::Function *Fn, GlobalDecl GD, 00194 const CGFunctionInfo &FnInfo) { 00195 assert(!CurGD.getDecl() && "CurGD was already set!"); 00196 CurGD = GD; 00197 CurFuncIsThunk = true; 00198 00199 // Build FunctionArgs. 00200 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 00201 QualType ThisType = MD->getThisType(getContext()); 00202 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); 00203 QualType ResultType = CGM.getCXXABI().HasThisReturn(GD) 00204 ? ThisType 00205 : CGM.getCXXABI().hasMostDerivedReturn(GD) 00206 ? CGM.getContext().VoidPtrTy 00207 : FPT->getReturnType(); 00208 FunctionArgList FunctionArgs; 00209 00210 // Create the implicit 'this' parameter declaration. 00211 CGM.getCXXABI().buildThisParam(*this, FunctionArgs); 00212 00213 // Add the rest of the parameters. 00214 FunctionArgs.append(MD->param_begin(), MD->param_end()); 00215 00216 if (isa<CXXDestructorDecl>(MD)) 00217 CGM.getCXXABI().addImplicitStructorParams(*this, ResultType, FunctionArgs); 00218 00219 // Start defining the function. 00220 StartFunction(GlobalDecl(), ResultType, Fn, FnInfo, FunctionArgs, 00221 MD->getLocation(), SourceLocation()); 00222 00223 // Since we didn't pass a GlobalDecl to StartFunction, do this ourselves. 00224 CGM.getCXXABI().EmitInstanceFunctionProlog(*this); 00225 CXXThisValue = CXXABIThisValue; 00226 } 00227 00228 void CodeGenFunction::EmitCallAndReturnForThunk(llvm::Value *Callee, 00229 const ThunkInfo *Thunk) { 00230 assert(isa<CXXMethodDecl>(CurGD.getDecl()) && 00231 "Please use a new CGF for this thunk"); 00232 const CXXMethodDecl *MD = cast<CXXMethodDecl>(CurGD.getDecl()); 00233 00234 // Adjust the 'this' pointer if necessary 00235 llvm::Value *AdjustedThisPtr = Thunk ? CGM.getCXXABI().performThisAdjustment( 00236 *this, LoadCXXThis(), Thunk->This) 00237 : LoadCXXThis(); 00238 00239 if (CurFnInfo->usesInAlloca()) { 00240 // We don't handle return adjusting thunks, because they require us to call 00241 // the copy constructor. For now, fall through and pretend the return 00242 // adjustment was empty so we don't crash. 00243 if (Thunk && !Thunk->Return.isEmpty()) { 00244 CGM.ErrorUnsupported( 00245 MD, "non-trivial argument copy for return-adjusting thunk"); 00246 } 00247 EmitMustTailThunk(MD, AdjustedThisPtr, Callee); 00248 return; 00249 } 00250 00251 // Start building CallArgs. 00252 CallArgList CallArgs; 00253 QualType ThisType = MD->getThisType(getContext()); 00254 CallArgs.add(RValue::get(AdjustedThisPtr), ThisType); 00255 00256 if (isa<CXXDestructorDecl>(MD)) 00257 CGM.getCXXABI().adjustCallArgsForDestructorThunk(*this, CurGD, CallArgs); 00258 00259 // Add the rest of the arguments. 00260 for (const ParmVarDecl *PD : MD->params()) 00261 EmitDelegateCallArg(CallArgs, PD, PD->getLocStart()); 00262 00263 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); 00264 00265 #ifndef NDEBUG 00266 const CGFunctionInfo &CallFnInfo = 00267 CGM.getTypes().arrangeCXXMethodCall(CallArgs, FPT, 00268 RequiredArgs::forPrototypePlus(FPT, 1)); 00269 assert(CallFnInfo.getRegParm() == CurFnInfo->getRegParm() && 00270 CallFnInfo.isNoReturn() == CurFnInfo->isNoReturn() && 00271 CallFnInfo.getCallingConvention() == CurFnInfo->getCallingConvention()); 00272 assert(isa<CXXDestructorDecl>(MD) || // ignore dtor return types 00273 similar(CallFnInfo.getReturnInfo(), CallFnInfo.getReturnType(), 00274 CurFnInfo->getReturnInfo(), CurFnInfo->getReturnType())); 00275 assert(CallFnInfo.arg_size() == CurFnInfo->arg_size()); 00276 for (unsigned i = 0, e = CurFnInfo->arg_size(); i != e; ++i) 00277 assert(similar(CallFnInfo.arg_begin()[i].info, 00278 CallFnInfo.arg_begin()[i].type, 00279 CurFnInfo->arg_begin()[i].info, 00280 CurFnInfo->arg_begin()[i].type)); 00281 #endif 00282 00283 // Determine whether we have a return value slot to use. 00284 QualType ResultType = CGM.getCXXABI().HasThisReturn(CurGD) 00285 ? ThisType 00286 : CGM.getCXXABI().hasMostDerivedReturn(CurGD) 00287 ? CGM.getContext().VoidPtrTy 00288 : FPT->getReturnType(); 00289 ReturnValueSlot Slot; 00290 if (!ResultType->isVoidType() && 00291 CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect && 00292 !hasScalarEvaluationKind(CurFnInfo->getReturnType())) 00293 Slot = ReturnValueSlot(ReturnValue, ResultType.isVolatileQualified()); 00294 00295 // Now emit our call. 00296 llvm::Instruction *CallOrInvoke; 00297 RValue RV = EmitCall(*CurFnInfo, Callee, Slot, CallArgs, MD, &CallOrInvoke); 00298 00299 // Consider return adjustment if we have ThunkInfo. 00300 if (Thunk && !Thunk->Return.isEmpty()) 00301 RV = PerformReturnAdjustment(*this, ResultType, RV, *Thunk); 00302 00303 // Emit return. 00304 if (!ResultType->isVoidType() && Slot.isNull()) 00305 CGM.getCXXABI().EmitReturnFromThunk(*this, RV, ResultType); 00306 00307 // Disable the final ARC autorelease. 00308 AutoreleaseResult = false; 00309 00310 FinishFunction(); 00311 } 00312 00313 void CodeGenFunction::EmitMustTailThunk(const CXXMethodDecl *MD, 00314 llvm::Value *AdjustedThisPtr, 00315 llvm::Value *Callee) { 00316 // Emitting a musttail call thunk doesn't use any of the CGCall.cpp machinery 00317 // to translate AST arguments into LLVM IR arguments. For thunks, we know 00318 // that the caller prototype more or less matches the callee prototype with 00319 // the exception of 'this'. 00320 SmallVector<llvm::Value *, 8> Args; 00321 for (llvm::Argument &A : CurFn->args()) 00322 Args.push_back(&A); 00323 00324 // Set the adjusted 'this' pointer. 00325 const ABIArgInfo &ThisAI = CurFnInfo->arg_begin()->info; 00326 if (ThisAI.isDirect()) { 00327 const ABIArgInfo &RetAI = CurFnInfo->getReturnInfo(); 00328 int ThisArgNo = RetAI.isIndirect() && !RetAI.isSRetAfterThis() ? 1 : 0; 00329 llvm::Type *ThisType = Args[ThisArgNo]->getType(); 00330 if (ThisType != AdjustedThisPtr->getType()) 00331 AdjustedThisPtr = Builder.CreateBitCast(AdjustedThisPtr, ThisType); 00332 Args[ThisArgNo] = AdjustedThisPtr; 00333 } else { 00334 assert(ThisAI.isInAlloca() && "this is passed directly or inalloca"); 00335 llvm::Value *ThisAddr = GetAddrOfLocalVar(CXXABIThisDecl); 00336 llvm::Type *ThisType = 00337 cast<llvm::PointerType>(ThisAddr->getType())->getElementType(); 00338 if (ThisType != AdjustedThisPtr->getType()) 00339 AdjustedThisPtr = Builder.CreateBitCast(AdjustedThisPtr, ThisType); 00340 Builder.CreateStore(AdjustedThisPtr, ThisAddr); 00341 } 00342 00343 // Emit the musttail call manually. Even if the prologue pushed cleanups, we 00344 // don't actually want to run them. 00345 llvm::CallInst *Call = Builder.CreateCall(Callee, Args); 00346 Call->setTailCallKind(llvm::CallInst::TCK_MustTail); 00347 00348 // Apply the standard set of call attributes. 00349 unsigned CallingConv; 00350 CodeGen::AttributeListType AttributeList; 00351 CGM.ConstructAttributeList(*CurFnInfo, MD, AttributeList, CallingConv, 00352 /*AttrOnCallSite=*/true); 00353 llvm::AttributeSet Attrs = 00354 llvm::AttributeSet::get(getLLVMContext(), AttributeList); 00355 Call->setAttributes(Attrs); 00356 Call->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv)); 00357 00358 if (Call->getType()->isVoidTy()) 00359 Builder.CreateRetVoid(); 00360 else 00361 Builder.CreateRet(Call); 00362 00363 // Finish the function to maintain CodeGenFunction invariants. 00364 // FIXME: Don't emit unreachable code. 00365 EmitBlock(createBasicBlock()); 00366 FinishFunction(); 00367 } 00368 00369 void CodeGenFunction::GenerateThunk(llvm::Function *Fn, 00370 const CGFunctionInfo &FnInfo, 00371 GlobalDecl GD, const ThunkInfo &Thunk) { 00372 StartThunk(Fn, GD, FnInfo); 00373 00374 // Get our callee. 00375 llvm::Type *Ty = 00376 CGM.getTypes().GetFunctionType(CGM.getTypes().arrangeGlobalDeclaration(GD)); 00377 llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true); 00378 00379 // Make the call and return the result. 00380 EmitCallAndReturnForThunk(Callee, &Thunk); 00381 00382 // Set the right linkage. 00383 CGM.setFunctionLinkage(GD, Fn); 00384 00385 // Set the right visibility. 00386 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 00387 setThunkVisibility(CGM, MD, Thunk, Fn); 00388 } 00389 00390 void CodeGenVTables::emitThunk(GlobalDecl GD, const ThunkInfo &Thunk, 00391 bool ForVTable) { 00392 const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeGlobalDeclaration(GD); 00393 00394 // FIXME: re-use FnInfo in this computation. 00395 llvm::Constant *C = CGM.GetAddrOfThunk(GD, Thunk); 00396 llvm::GlobalValue *Entry; 00397 00398 // Strip off a bitcast if we got one back. 00399 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(C)) { 00400 assert(CE->getOpcode() == llvm::Instruction::BitCast); 00401 Entry = cast<llvm::GlobalValue>(CE->getOperand(0)); 00402 } else { 00403 Entry = cast<llvm::GlobalValue>(C); 00404 } 00405 00406 // There's already a declaration with the same name, check if it has the same 00407 // type or if we need to replace it. 00408 if (Entry->getType()->getElementType() != 00409 CGM.getTypes().GetFunctionTypeForVTable(GD)) { 00410 llvm::GlobalValue *OldThunkFn = Entry; 00411 00412 // If the types mismatch then we have to rewrite the definition. 00413 assert(OldThunkFn->isDeclaration() && 00414 "Shouldn't replace non-declaration"); 00415 00416 // Remove the name from the old thunk function and get a new thunk. 00417 OldThunkFn->setName(StringRef()); 00418 Entry = cast<llvm::GlobalValue>(CGM.GetAddrOfThunk(GD, Thunk)); 00419 00420 // If needed, replace the old thunk with a bitcast. 00421 if (!OldThunkFn->use_empty()) { 00422 llvm::Constant *NewPtrForOldDecl = 00423 llvm::ConstantExpr::getBitCast(Entry, OldThunkFn->getType()); 00424 OldThunkFn->replaceAllUsesWith(NewPtrForOldDecl); 00425 } 00426 00427 // Remove the old thunk. 00428 OldThunkFn->eraseFromParent(); 00429 } 00430 00431 llvm::Function *ThunkFn = cast<llvm::Function>(Entry); 00432 bool ABIHasKeyFunctions = CGM.getTarget().getCXXABI().hasKeyFunctions(); 00433 bool UseAvailableExternallyLinkage = ForVTable && ABIHasKeyFunctions; 00434 00435 if (!ThunkFn->isDeclaration()) { 00436 if (!ABIHasKeyFunctions || UseAvailableExternallyLinkage) { 00437 // There is already a thunk emitted for this function, do nothing. 00438 return; 00439 } 00440 00441 // Change the linkage. 00442 CGM.setFunctionLinkage(GD, ThunkFn); 00443 return; 00444 } 00445 00446 CGM.SetLLVMFunctionAttributesForDefinition(GD.getDecl(), ThunkFn); 00447 00448 if (ThunkFn->isVarArg()) { 00449 // Varargs thunks are special; we can't just generate a call because 00450 // we can't copy the varargs. Our implementation is rather 00451 // expensive/sucky at the moment, so don't generate the thunk unless 00452 // we have to. 00453 // FIXME: Do something better here; GenerateVarArgsThunk is extremely ugly. 00454 if (!UseAvailableExternallyLinkage) { 00455 CodeGenFunction(CGM).GenerateVarArgsThunk(ThunkFn, FnInfo, GD, Thunk); 00456 CGM.getCXXABI().setThunkLinkage(ThunkFn, ForVTable, GD, 00457 !Thunk.Return.isEmpty()); 00458 } 00459 } else { 00460 // Normal thunk body generation. 00461 CodeGenFunction(CGM).GenerateThunk(ThunkFn, FnInfo, GD, Thunk); 00462 CGM.getCXXABI().setThunkLinkage(ThunkFn, ForVTable, GD, 00463 !Thunk.Return.isEmpty()); 00464 } 00465 } 00466 00467 void CodeGenVTables::maybeEmitThunkForVTable(GlobalDecl GD, 00468 const ThunkInfo &Thunk) { 00469 // If the ABI has key functions, only the TU with the key function should emit 00470 // the thunk. However, we can allow inlining of thunks if we emit them with 00471 // available_externally linkage together with vtables when optimizations are 00472 // enabled. 00473 if (CGM.getTarget().getCXXABI().hasKeyFunctions() && 00474 !CGM.getCodeGenOpts().OptimizationLevel) 00475 return; 00476 00477 // We can't emit thunks for member functions with incomplete types. 00478 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 00479 if (!CGM.getTypes().isFuncTypeConvertible( 00480 MD->getType()->castAs<FunctionType>())) 00481 return; 00482 00483 emitThunk(GD, Thunk, /*ForVTable=*/true); 00484 } 00485 00486 void CodeGenVTables::EmitThunks(GlobalDecl GD) 00487 { 00488 const CXXMethodDecl *MD = 00489 cast<CXXMethodDecl>(GD.getDecl())->getCanonicalDecl(); 00490 00491 // We don't need to generate thunks for the base destructor. 00492 if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base) 00493 return; 00494 00495 const VTableContextBase::ThunkInfoVectorTy *ThunkInfoVector = 00496 VTContext->getThunkInfo(GD); 00497 00498 if (!ThunkInfoVector) 00499 return; 00500 00501 for (unsigned I = 0, E = ThunkInfoVector->size(); I != E; ++I) 00502 emitThunk(GD, (*ThunkInfoVector)[I], /*ForVTable=*/false); 00503 } 00504 00505 llvm::Constant *CodeGenVTables::CreateVTableInitializer( 00506 const CXXRecordDecl *RD, const VTableComponent *Components, 00507 unsigned NumComponents, const VTableLayout::VTableThunkTy *VTableThunks, 00508 unsigned NumVTableThunks, llvm::Constant *RTTI) { 00509 SmallVector<llvm::Constant *, 64> Inits; 00510 00511 llvm::Type *Int8PtrTy = CGM.Int8PtrTy; 00512 00513 llvm::Type *PtrDiffTy = 00514 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType()); 00515 00516 unsigned NextVTableThunkIndex = 0; 00517 00518 llvm::Constant *PureVirtualFn = nullptr, *DeletedVirtualFn = nullptr; 00519 00520 for (unsigned I = 0; I != NumComponents; ++I) { 00521 VTableComponent Component = Components[I]; 00522 00523 llvm::Constant *Init = nullptr; 00524 00525 switch (Component.getKind()) { 00526 case VTableComponent::CK_VCallOffset: 00527 Init = llvm::ConstantInt::get(PtrDiffTy, 00528 Component.getVCallOffset().getQuantity()); 00529 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy); 00530 break; 00531 case VTableComponent::CK_VBaseOffset: 00532 Init = llvm::ConstantInt::get(PtrDiffTy, 00533 Component.getVBaseOffset().getQuantity()); 00534 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy); 00535 break; 00536 case VTableComponent::CK_OffsetToTop: 00537 Init = llvm::ConstantInt::get(PtrDiffTy, 00538 Component.getOffsetToTop().getQuantity()); 00539 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy); 00540 break; 00541 case VTableComponent::CK_RTTI: 00542 Init = llvm::ConstantExpr::getBitCast(RTTI, Int8PtrTy); 00543 break; 00544 case VTableComponent::CK_FunctionPointer: 00545 case VTableComponent::CK_CompleteDtorPointer: 00546 case VTableComponent::CK_DeletingDtorPointer: { 00547 GlobalDecl GD; 00548 00549 // Get the right global decl. 00550 switch (Component.getKind()) { 00551 default: 00552 llvm_unreachable("Unexpected vtable component kind"); 00553 case VTableComponent::CK_FunctionPointer: 00554 GD = Component.getFunctionDecl(); 00555 break; 00556 case VTableComponent::CK_CompleteDtorPointer: 00557 GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Complete); 00558 break; 00559 case VTableComponent::CK_DeletingDtorPointer: 00560 GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Deleting); 00561 break; 00562 } 00563 00564 if (cast<CXXMethodDecl>(GD.getDecl())->isPure()) { 00565 // We have a pure virtual member function. 00566 if (!PureVirtualFn) { 00567 llvm::FunctionType *Ty = 00568 llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false); 00569 StringRef PureCallName = CGM.getCXXABI().GetPureVirtualCallName(); 00570 PureVirtualFn = CGM.CreateRuntimeFunction(Ty, PureCallName); 00571 PureVirtualFn = llvm::ConstantExpr::getBitCast(PureVirtualFn, 00572 CGM.Int8PtrTy); 00573 } 00574 Init = PureVirtualFn; 00575 } else if (cast<CXXMethodDecl>(GD.getDecl())->isDeleted()) { 00576 if (!DeletedVirtualFn) { 00577 llvm::FunctionType *Ty = 00578 llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false); 00579 StringRef DeletedCallName = 00580 CGM.getCXXABI().GetDeletedVirtualCallName(); 00581 DeletedVirtualFn = CGM.CreateRuntimeFunction(Ty, DeletedCallName); 00582 DeletedVirtualFn = llvm::ConstantExpr::getBitCast(DeletedVirtualFn, 00583 CGM.Int8PtrTy); 00584 } 00585 Init = DeletedVirtualFn; 00586 } else { 00587 // Check if we should use a thunk. 00588 if (NextVTableThunkIndex < NumVTableThunks && 00589 VTableThunks[NextVTableThunkIndex].first == I) { 00590 const ThunkInfo &Thunk = VTableThunks[NextVTableThunkIndex].second; 00591 00592 maybeEmitThunkForVTable(GD, Thunk); 00593 Init = CGM.GetAddrOfThunk(GD, Thunk); 00594 00595 NextVTableThunkIndex++; 00596 } else { 00597 llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVTable(GD); 00598 00599 Init = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true); 00600 } 00601 00602 Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy); 00603 } 00604 break; 00605 } 00606 00607 case VTableComponent::CK_UnusedFunctionPointer: 00608 Init = llvm::ConstantExpr::getNullValue(Int8PtrTy); 00609 break; 00610 }; 00611 00612 Inits.push_back(Init); 00613 } 00614 00615 llvm::ArrayType *ArrayType = llvm::ArrayType::get(Int8PtrTy, NumComponents); 00616 return llvm::ConstantArray::get(ArrayType, Inits); 00617 } 00618 00619 llvm::GlobalVariable * 00620 CodeGenVTables::GenerateConstructionVTable(const CXXRecordDecl *RD, 00621 const BaseSubobject &Base, 00622 bool BaseIsVirtual, 00623 llvm::GlobalVariable::LinkageTypes Linkage, 00624 VTableAddressPointsMapTy& AddressPoints) { 00625 if (CGDebugInfo *DI = CGM.getModuleDebugInfo()) 00626 DI->completeClassData(Base.getBase()); 00627 00628 std::unique_ptr<VTableLayout> VTLayout( 00629 getItaniumVTableContext().createConstructionVTableLayout( 00630 Base.getBase(), Base.getBaseOffset(), BaseIsVirtual, RD)); 00631 00632 // Add the address points. 00633 AddressPoints = VTLayout->getAddressPoints(); 00634 00635 // Get the mangled construction vtable name. 00636 SmallString<256> OutName; 00637 llvm::raw_svector_ostream Out(OutName); 00638 cast<ItaniumMangleContext>(CGM.getCXXABI().getMangleContext()) 00639 .mangleCXXCtorVTable(RD, Base.getBaseOffset().getQuantity(), 00640 Base.getBase(), Out); 00641 Out.flush(); 00642 StringRef Name = OutName.str(); 00643 00644 llvm::ArrayType *ArrayType = 00645 llvm::ArrayType::get(CGM.Int8PtrTy, VTLayout->getNumVTableComponents()); 00646 00647 // Construction vtable symbols are not part of the Itanium ABI, so we cannot 00648 // guarantee that they actually will be available externally. Instead, when 00649 // emitting an available_externally VTT, we provide references to an internal 00650 // linkage construction vtable. The ABI only requires complete-object vtables 00651 // to be the same for all instances of a type, not construction vtables. 00652 if (Linkage == llvm::GlobalVariable::AvailableExternallyLinkage) 00653 Linkage = llvm::GlobalVariable::InternalLinkage; 00654 00655 // Create the variable that will hold the construction vtable. 00656 llvm::GlobalVariable *VTable = 00657 CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType, Linkage); 00658 CGM.setGlobalVisibility(VTable, RD); 00659 00660 // V-tables are always unnamed_addr. 00661 VTable->setUnnamedAddr(true); 00662 00663 llvm::Constant *RTTI = CGM.GetAddrOfRTTIDescriptor( 00664 CGM.getContext().getTagDeclType(Base.getBase())); 00665 00666 // Create and set the initializer. 00667 llvm::Constant *Init = CreateVTableInitializer( 00668 Base.getBase(), VTLayout->vtable_component_begin(), 00669 VTLayout->getNumVTableComponents(), VTLayout->vtable_thunk_begin(), 00670 VTLayout->getNumVTableThunks(), RTTI); 00671 VTable->setInitializer(Init); 00672 00673 return VTable; 00674 } 00675 00676 /// Compute the required linkage of the v-table for the given class. 00677 /// 00678 /// Note that we only call this at the end of the translation unit. 00679 llvm::GlobalVariable::LinkageTypes 00680 CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) { 00681 if (!RD->isExternallyVisible()) 00682 return llvm::GlobalVariable::InternalLinkage; 00683 00684 // We're at the end of the translation unit, so the current key 00685 // function is fully correct. 00686 const CXXMethodDecl *keyFunction = Context.getCurrentKeyFunction(RD); 00687 if (keyFunction && !RD->hasAttr<DLLImportAttr>()) { 00688 // If this class has a key function, use that to determine the 00689 // linkage of the vtable. 00690 const FunctionDecl *def = nullptr; 00691 if (keyFunction->hasBody(def)) 00692 keyFunction = cast<CXXMethodDecl>(def); 00693 00694 switch (keyFunction->getTemplateSpecializationKind()) { 00695 case TSK_Undeclared: 00696 case TSK_ExplicitSpecialization: 00697 assert(def && "Should not have been asked to emit this"); 00698 if (keyFunction->isInlined()) 00699 return !Context.getLangOpts().AppleKext ? 00700 llvm::GlobalVariable::LinkOnceODRLinkage : 00701 llvm::Function::InternalLinkage; 00702 00703 return llvm::GlobalVariable::ExternalLinkage; 00704 00705 case TSK_ImplicitInstantiation: 00706 return !Context.getLangOpts().AppleKext ? 00707 llvm::GlobalVariable::LinkOnceODRLinkage : 00708 llvm::Function::InternalLinkage; 00709 00710 case TSK_ExplicitInstantiationDefinition: 00711 return !Context.getLangOpts().AppleKext ? 00712 llvm::GlobalVariable::WeakODRLinkage : 00713 llvm::Function::InternalLinkage; 00714 00715 case TSK_ExplicitInstantiationDeclaration: 00716 llvm_unreachable("Should not have been asked to emit this"); 00717 } 00718 } 00719 00720 // -fapple-kext mode does not support weak linkage, so we must use 00721 // internal linkage. 00722 if (Context.getLangOpts().AppleKext) 00723 return llvm::Function::InternalLinkage; 00724 00725 llvm::GlobalVariable::LinkageTypes DiscardableODRLinkage = 00726 llvm::GlobalValue::LinkOnceODRLinkage; 00727 llvm::GlobalVariable::LinkageTypes NonDiscardableODRLinkage = 00728 llvm::GlobalValue::WeakODRLinkage; 00729 if (RD->hasAttr<DLLExportAttr>()) { 00730 // Cannot discard exported vtables. 00731 DiscardableODRLinkage = NonDiscardableODRLinkage; 00732 } else if (RD->hasAttr<DLLImportAttr>()) { 00733 // Imported vtables are available externally. 00734 DiscardableODRLinkage = llvm::GlobalVariable::AvailableExternallyLinkage; 00735 NonDiscardableODRLinkage = llvm::GlobalVariable::AvailableExternallyLinkage; 00736 } 00737 00738 switch (RD->getTemplateSpecializationKind()) { 00739 case TSK_Undeclared: 00740 case TSK_ExplicitSpecialization: 00741 case TSK_ImplicitInstantiation: 00742 return DiscardableODRLinkage; 00743 00744 case TSK_ExplicitInstantiationDeclaration: 00745 llvm_unreachable("Should not have been asked to emit this"); 00746 00747 case TSK_ExplicitInstantiationDefinition: 00748 return NonDiscardableODRLinkage; 00749 } 00750 00751 llvm_unreachable("Invalid TemplateSpecializationKind!"); 00752 } 00753 00754 /// This is a callback from Sema to tell us that it believes that a 00755 /// particular v-table is required to be emitted in this translation 00756 /// unit. 00757 /// 00758 /// The reason we don't simply trust this callback is because Sema 00759 /// will happily report that something is used even when it's used 00760 /// only in code that we don't actually have to emit. 00761 /// 00762 /// \param isRequired - if true, the v-table is mandatory, e.g. 00763 /// because the translation unit defines the key function 00764 void CodeGenModule::EmitVTable(CXXRecordDecl *theClass, bool isRequired) { 00765 if (!isRequired) return; 00766 00767 VTables.GenerateClassData(theClass); 00768 } 00769 00770 void 00771 CodeGenVTables::GenerateClassData(const CXXRecordDecl *RD) { 00772 if (CGDebugInfo *DI = CGM.getModuleDebugInfo()) 00773 DI->completeClassData(RD); 00774 00775 if (RD->getNumVBases()) 00776 CGM.getCXXABI().emitVirtualInheritanceTables(RD); 00777 00778 CGM.getCXXABI().emitVTableDefinitions(*this, RD); 00779 } 00780 00781 /// At this point in the translation unit, does it appear that can we 00782 /// rely on the vtable being defined elsewhere in the program? 00783 /// 00784 /// The response is really only definitive when called at the end of 00785 /// the translation unit. 00786 /// 00787 /// The only semantic restriction here is that the object file should 00788 /// not contain a v-table definition when that v-table is defined 00789 /// strongly elsewhere. Otherwise, we'd just like to avoid emitting 00790 /// v-tables when unnecessary. 00791 bool CodeGenVTables::isVTableExternal(const CXXRecordDecl *RD) { 00792 assert(RD->isDynamicClass() && "Non-dynamic classes have no VTable."); 00793 00794 // If we have an explicit instantiation declaration (and not a 00795 // definition), the v-table is defined elsewhere. 00796 TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind(); 00797 if (TSK == TSK_ExplicitInstantiationDeclaration) 00798 return true; 00799 00800 // Otherwise, if the class is an instantiated template, the 00801 // v-table must be defined here. 00802 if (TSK == TSK_ImplicitInstantiation || 00803 TSK == TSK_ExplicitInstantiationDefinition) 00804 return false; 00805 00806 // Otherwise, if the class doesn't have a key function (possibly 00807 // anymore), the v-table must be defined here. 00808 const CXXMethodDecl *keyFunction = CGM.getContext().getCurrentKeyFunction(RD); 00809 if (!keyFunction) 00810 return false; 00811 00812 // Otherwise, if we don't have a definition of the key function, the 00813 // v-table must be defined somewhere else. 00814 return !keyFunction->hasBody(); 00815 } 00816 00817 /// Given that we're currently at the end of the translation unit, and 00818 /// we've emitted a reference to the v-table for this class, should 00819 /// we define that v-table? 00820 static bool shouldEmitVTableAtEndOfTranslationUnit(CodeGenModule &CGM, 00821 const CXXRecordDecl *RD) { 00822 return !CGM.getVTables().isVTableExternal(RD); 00823 } 00824 00825 /// Given that at some point we emitted a reference to one or more 00826 /// v-tables, and that we are now at the end of the translation unit, 00827 /// decide whether we should emit them. 00828 void CodeGenModule::EmitDeferredVTables() { 00829 #ifndef NDEBUG 00830 // Remember the size of DeferredVTables, because we're going to assume 00831 // that this entire operation doesn't modify it. 00832 size_t savedSize = DeferredVTables.size(); 00833 #endif 00834 00835 typedef std::vector<const CXXRecordDecl *>::const_iterator const_iterator; 00836 for (const_iterator i = DeferredVTables.begin(), 00837 e = DeferredVTables.end(); i != e; ++i) { 00838 const CXXRecordDecl *RD = *i; 00839 if (shouldEmitVTableAtEndOfTranslationUnit(*this, RD)) 00840 VTables.GenerateClassData(RD); 00841 } 00842 00843 assert(savedSize == DeferredVTables.size() && 00844 "deferred extra v-tables during v-table emission?"); 00845 DeferredVTables.clear(); 00846 }