Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 1 | //===--- CGVTables.cpp - Emit LLVM Code for C++ vtables -------------------===// |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 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 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 14 | #include "CodeGenFunction.h" |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 15 | #include "CGCXXABI.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "CodeGenModule.h" |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 17 | #include "clang/AST/CXXInheritance.h" |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 18 | #include "clang/AST/RecordLayout.h" |
John McCall | 7a53690 | 2010-08-05 20:39:18 +0000 | [diff] [blame] | 19 | #include "clang/Frontend/CodeGenOptions.h" |
Anders Carlsson | 5dd730a | 2009-11-26 19:32:45 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/DenseSet.h" |
Anders Carlsson | b9021e9 | 2010-02-27 16:18:19 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SetVector.h" |
Chandler Carruth | e087f07 | 2010-02-13 10:38:52 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Compiler.h" |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Format.h" |
Eli Friedman | 7dcdf5b | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 24 | #include "llvm/Transforms/Utils/Cloning.h" |
Anders Carlsson | 5e454aa | 2010-03-17 20:06:32 +0000 | [diff] [blame] | 25 | #include <algorithm> |
Zhongxing Xu | 7fe26ac | 2009-11-13 05:46:16 +0000 | [diff] [blame] | 26 | #include <cstdio> |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace clang; |
| 29 | using namespace CodeGen; |
| 30 | |
Peter Collingbourne | 1d2b317 | 2011-09-26 01:56:30 +0000 | [diff] [blame] | 31 | CodeGenVTables::CodeGenVTables(CodeGenModule &CGM) |
Timur Iskhodzhanov | 635de28 | 2013-07-30 09:46:19 +0000 | [diff] [blame^] | 32 | : CGM(CGM), VTContext(CGM.getContext()) { |
| 33 | if (CGM.getTarget().getCXXABI().isMicrosoft()) { |
| 34 | // FIXME: Eventually, we should only have one of V*TContexts available. |
| 35 | // Today we use both in the Microsoft ABI as MicrosoftVFTableContext |
| 36 | // is not completely supported in CodeGen yet. |
| 37 | VFTContext.reset(new MicrosoftVFTableContext(CGM.getContext())); |
| 38 | } |
| 39 | } |
Peter Collingbourne | 1d2b317 | 2011-09-26 01:56:30 +0000 | [diff] [blame] | 40 | |
Anders Carlsson | 19879c9 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 41 | llvm::Constant *CodeGenModule::GetAddrOfThunk(GlobalDecl GD, |
Anders Carlsson | 84c49e4 | 2011-02-06 17:15:43 +0000 | [diff] [blame] | 42 | const ThunkInfo &Thunk) { |
Anders Carlsson | 19879c9 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 43 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 44 | |
| 45 | // Compute the mangled name. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 46 | SmallString<256> Name; |
Rafael Espindola | f0be979 | 2011-02-11 02:52:17 +0000 | [diff] [blame] | 47 | llvm::raw_svector_ostream Out(Name); |
Anders Carlsson | 19879c9 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 48 | if (const CXXDestructorDecl* DD = dyn_cast<CXXDestructorDecl>(MD)) |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 49 | getCXXABI().getMangleContext().mangleCXXDtorThunk(DD, GD.getDtorType(), |
Rafael Espindola | f0be979 | 2011-02-11 02:52:17 +0000 | [diff] [blame] | 50 | Thunk.This, Out); |
Anders Carlsson | 19879c9 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 51 | else |
Rafael Espindola | f0be979 | 2011-02-11 02:52:17 +0000 | [diff] [blame] | 52 | getCXXABI().getMangleContext().mangleThunk(MD, Thunk, Out); |
| 53 | Out.flush(); |
| 54 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 55 | llvm::Type *Ty = getTypes().GetFunctionTypeForVTable(GD); |
Anders Carlsson | 84c49e4 | 2011-02-06 17:15:43 +0000 | [diff] [blame] | 56 | return GetOrCreateLLVMFunction(Name, Ty, GD, /*ForVTable=*/true); |
Anders Carlsson | 19879c9 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 59 | static llvm::Value *PerformTypeAdjustment(CodeGenFunction &CGF, |
| 60 | llvm::Value *Ptr, |
| 61 | int64_t NonVirtualAdjustment, |
Eli Friedman | 82bad6b | 2012-09-14 01:45:09 +0000 | [diff] [blame] | 62 | int64_t VirtualAdjustment, |
| 63 | bool IsReturnAdjustment) { |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 64 | if (!NonVirtualAdjustment && !VirtualAdjustment) |
| 65 | return Ptr; |
| 66 | |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 67 | llvm::Type *Int8PtrTy = CGF.Int8PtrTy; |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 68 | llvm::Value *V = CGF.Builder.CreateBitCast(Ptr, Int8PtrTy); |
| 69 | |
Eli Friedman | 82bad6b | 2012-09-14 01:45:09 +0000 | [diff] [blame] | 70 | if (NonVirtualAdjustment && !IsReturnAdjustment) { |
| 71 | // Perform the non-virtual adjustment for a base-to-derived cast. |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 72 | V = CGF.Builder.CreateConstInBoundsGEP1_64(V, NonVirtualAdjustment); |
| 73 | } |
| 74 | |
| 75 | if (VirtualAdjustment) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 76 | llvm::Type *PtrDiffTy = |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 77 | CGF.ConvertType(CGF.getContext().getPointerDiffType()); |
| 78 | |
Eli Friedman | 82bad6b | 2012-09-14 01:45:09 +0000 | [diff] [blame] | 79 | // Perform the virtual adjustment. |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 80 | llvm::Value *VTablePtrPtr = |
| 81 | CGF.Builder.CreateBitCast(V, Int8PtrTy->getPointerTo()); |
| 82 | |
| 83 | llvm::Value *VTablePtr = CGF.Builder.CreateLoad(VTablePtrPtr); |
| 84 | |
| 85 | llvm::Value *OffsetPtr = |
| 86 | CGF.Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment); |
| 87 | |
| 88 | OffsetPtr = CGF.Builder.CreateBitCast(OffsetPtr, PtrDiffTy->getPointerTo()); |
| 89 | |
| 90 | // Load the adjustment offset from the vtable. |
| 91 | llvm::Value *Offset = CGF.Builder.CreateLoad(OffsetPtr); |
| 92 | |
| 93 | // Adjust our pointer. |
| 94 | V = CGF.Builder.CreateInBoundsGEP(V, Offset); |
| 95 | } |
| 96 | |
Eli Friedman | 82bad6b | 2012-09-14 01:45:09 +0000 | [diff] [blame] | 97 | if (NonVirtualAdjustment && IsReturnAdjustment) { |
| 98 | // Perform the non-virtual adjustment for a derived-to-base cast. |
| 99 | V = CGF.Builder.CreateConstInBoundsGEP1_64(V, NonVirtualAdjustment); |
| 100 | } |
| 101 | |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 102 | // Cast back to the original type. |
| 103 | return CGF.Builder.CreateBitCast(V, Ptr->getType()); |
| 104 | } |
| 105 | |
John McCall | 6500553 | 2010-08-04 23:46:35 +0000 | [diff] [blame] | 106 | static void setThunkVisibility(CodeGenModule &CGM, const CXXMethodDecl *MD, |
| 107 | const ThunkInfo &Thunk, llvm::Function *Fn) { |
Anders Carlsson | 0ffeaad | 2011-01-29 19:39:23 +0000 | [diff] [blame] | 108 | CGM.setGlobalVisibility(Fn, MD); |
John McCall | 6500553 | 2010-08-04 23:46:35 +0000 | [diff] [blame] | 109 | |
John McCall | 279b5eb | 2010-08-12 23:36:15 +0000 | [diff] [blame] | 110 | if (!CGM.getCodeGenOpts().HiddenWeakVTables) |
| 111 | return; |
| 112 | |
John McCall | 6500553 | 2010-08-04 23:46:35 +0000 | [diff] [blame] | 113 | // If the thunk has weak/linkonce linkage, but the function must be |
| 114 | // emitted in every translation unit that references it, then we can |
| 115 | // emit its thunks with hidden visibility, since its thunks must be |
| 116 | // emitted when the function is. |
| 117 | |
John McCall | 7a53690 | 2010-08-05 20:39:18 +0000 | [diff] [blame] | 118 | // This follows CodeGenModule::setTypeVisibility; see the comments |
| 119 | // there for explanation. |
John McCall | 6500553 | 2010-08-04 23:46:35 +0000 | [diff] [blame] | 120 | |
| 121 | if ((Fn->getLinkage() != llvm::GlobalVariable::LinkOnceODRLinkage && |
| 122 | Fn->getLinkage() != llvm::GlobalVariable::WeakODRLinkage) || |
| 123 | Fn->getVisibility() != llvm::GlobalVariable::DefaultVisibility) |
| 124 | return; |
| 125 | |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 126 | if (MD->getExplicitVisibility(ValueDecl::VisibilityForValue)) |
John McCall | 6500553 | 2010-08-04 23:46:35 +0000 | [diff] [blame] | 127 | return; |
| 128 | |
| 129 | switch (MD->getTemplateSpecializationKind()) { |
John McCall | 6500553 | 2010-08-04 23:46:35 +0000 | [diff] [blame] | 130 | case TSK_ExplicitInstantiationDefinition: |
| 131 | case TSK_ExplicitInstantiationDeclaration: |
| 132 | return; |
| 133 | |
John McCall | 6500553 | 2010-08-04 23:46:35 +0000 | [diff] [blame] | 134 | case TSK_Undeclared: |
| 135 | break; |
| 136 | |
John McCall | 7a53690 | 2010-08-05 20:39:18 +0000 | [diff] [blame] | 137 | case TSK_ExplicitSpecialization: |
John McCall | 6500553 | 2010-08-04 23:46:35 +0000 | [diff] [blame] | 138 | case TSK_ImplicitInstantiation: |
Douglas Gregor | aafd111 | 2012-10-24 14:11:55 +0000 | [diff] [blame] | 139 | return; |
John McCall | 6500553 | 2010-08-04 23:46:35 +0000 | [diff] [blame] | 140 | break; |
| 141 | } |
| 142 | |
| 143 | // If there's an explicit definition, and that definition is |
| 144 | // out-of-line, then we can't assume that all users will have a |
| 145 | // definition to emit. |
| 146 | const FunctionDecl *Def = 0; |
| 147 | if (MD->hasBody(Def) && Def->isOutOfLine()) |
| 148 | return; |
| 149 | |
| 150 | Fn->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 151 | } |
| 152 | |
John McCall | 311b442 | 2011-03-09 07:12:35 +0000 | [diff] [blame] | 153 | #ifndef NDEBUG |
| 154 | static bool similar(const ABIArgInfo &infoL, CanQualType typeL, |
| 155 | const ABIArgInfo &infoR, CanQualType typeR) { |
| 156 | return (infoL.getKind() == infoR.getKind() && |
| 157 | (typeL == typeR || |
| 158 | (isa<PointerType>(typeL) && isa<PointerType>(typeR)) || |
| 159 | (isa<ReferenceType>(typeL) && isa<ReferenceType>(typeR)))); |
| 160 | } |
| 161 | #endif |
| 162 | |
Eli Friedman | 7dcdf5b | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 163 | static RValue PerformReturnAdjustment(CodeGenFunction &CGF, |
| 164 | QualType ResultType, RValue RV, |
| 165 | const ThunkInfo &Thunk) { |
| 166 | // Emit the return adjustment. |
| 167 | bool NullCheckValue = !ResultType->isReferenceType(); |
| 168 | |
| 169 | llvm::BasicBlock *AdjustNull = 0; |
| 170 | llvm::BasicBlock *AdjustNotNull = 0; |
| 171 | llvm::BasicBlock *AdjustEnd = 0; |
| 172 | |
| 173 | llvm::Value *ReturnValue = RV.getScalarVal(); |
| 174 | |
| 175 | if (NullCheckValue) { |
| 176 | AdjustNull = CGF.createBasicBlock("adjust.null"); |
| 177 | AdjustNotNull = CGF.createBasicBlock("adjust.notnull"); |
| 178 | AdjustEnd = CGF.createBasicBlock("adjust.end"); |
| 179 | |
| 180 | llvm::Value *IsNull = CGF.Builder.CreateIsNull(ReturnValue); |
| 181 | CGF.Builder.CreateCondBr(IsNull, AdjustNull, AdjustNotNull); |
| 182 | CGF.EmitBlock(AdjustNotNull); |
| 183 | } |
| 184 | |
| 185 | ReturnValue = PerformTypeAdjustment(CGF, ReturnValue, |
| 186 | Thunk.Return.NonVirtual, |
Eli Friedman | 82bad6b | 2012-09-14 01:45:09 +0000 | [diff] [blame] | 187 | Thunk.Return.VBaseOffsetOffset, |
| 188 | /*IsReturnAdjustment*/true); |
Eli Friedman | 7dcdf5b | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 189 | |
| 190 | if (NullCheckValue) { |
| 191 | CGF.Builder.CreateBr(AdjustEnd); |
| 192 | CGF.EmitBlock(AdjustNull); |
| 193 | CGF.Builder.CreateBr(AdjustEnd); |
| 194 | CGF.EmitBlock(AdjustEnd); |
| 195 | |
| 196 | llvm::PHINode *PHI = CGF.Builder.CreatePHI(ReturnValue->getType(), 2); |
| 197 | PHI->addIncoming(ReturnValue, AdjustNotNull); |
| 198 | PHI->addIncoming(llvm::Constant::getNullValue(ReturnValue->getType()), |
| 199 | AdjustNull); |
| 200 | ReturnValue = PHI; |
| 201 | } |
| 202 | |
| 203 | return RValue::get(ReturnValue); |
| 204 | } |
| 205 | |
| 206 | // This function does roughly the same thing as GenerateThunk, but in a |
| 207 | // very different way, so that va_start and va_end work correctly. |
| 208 | // FIXME: This function assumes "this" is the first non-sret LLVM argument of |
| 209 | // a function, and that there is an alloca built in the entry block |
| 210 | // for all accesses to "this". |
| 211 | // FIXME: This function assumes there is only one "ret" statement per function. |
| 212 | // FIXME: Cloning isn't correct in the presence of indirect goto! |
| 213 | // FIXME: This implementation of thunks bloats codesize by duplicating the |
| 214 | // function definition. There are alternatives: |
| 215 | // 1. Add some sort of stub support to LLVM for cases where we can |
| 216 | // do a this adjustment, then a sibcall. |
| 217 | // 2. We could transform the definition to take a va_list instead of an |
| 218 | // actual variable argument list, then have the thunks (including a |
| 219 | // no-op thunk for the regular definition) call va_start/va_end. |
| 220 | // There's a bit of per-call overhead for this solution, but it's |
| 221 | // better for codesize if the definition is long. |
| 222 | void CodeGenFunction::GenerateVarArgsThunk( |
| 223 | llvm::Function *Fn, |
| 224 | const CGFunctionInfo &FnInfo, |
| 225 | GlobalDecl GD, const ThunkInfo &Thunk) { |
| 226 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 227 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
| 228 | QualType ResultType = FPT->getResultType(); |
| 229 | |
| 230 | // Get the original function |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 231 | assert(FnInfo.isVariadic()); |
| 232 | llvm::Type *Ty = CGM.getTypes().GetFunctionType(FnInfo); |
Eli Friedman | 7dcdf5b | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 233 | llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true); |
| 234 | llvm::Function *BaseFn = cast<llvm::Function>(Callee); |
| 235 | |
| 236 | // Clone to thunk. |
Benjamin Kramer | 9b5ede5 | 2012-09-19 13:13:52 +0000 | [diff] [blame] | 237 | llvm::ValueToValueMapTy VMap; |
| 238 | llvm::Function *NewFn = llvm::CloneFunction(BaseFn, VMap, |
| 239 | /*ModuleLevelChanges=*/false); |
Eli Friedman | 7dcdf5b | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 240 | CGM.getModule().getFunctionList().push_back(NewFn); |
| 241 | Fn->replaceAllUsesWith(NewFn); |
| 242 | NewFn->takeName(Fn); |
| 243 | Fn->eraseFromParent(); |
| 244 | Fn = NewFn; |
| 245 | |
| 246 | // "Initialize" CGF (minimally). |
| 247 | CurFn = Fn; |
| 248 | |
| 249 | // Get the "this" value |
| 250 | llvm::Function::arg_iterator AI = Fn->arg_begin(); |
| 251 | if (CGM.ReturnTypeUsesSRet(FnInfo)) |
| 252 | ++AI; |
| 253 | |
| 254 | // Find the first store of "this", which will be to the alloca associated |
| 255 | // with "this". |
| 256 | llvm::Value *ThisPtr = &*AI; |
| 257 | llvm::BasicBlock *EntryBB = Fn->begin(); |
| 258 | llvm::Instruction *ThisStore = 0; |
| 259 | for (llvm::BasicBlock::iterator I = EntryBB->begin(), E = EntryBB->end(); |
| 260 | I != E; I++) { |
| 261 | if (isa<llvm::StoreInst>(I) && I->getOperand(0) == ThisPtr) { |
| 262 | ThisStore = cast<llvm::StoreInst>(I); |
| 263 | break; |
| 264 | } |
| 265 | } |
| 266 | assert(ThisStore && "Store of this should be in entry block?"); |
| 267 | // Adjust "this", if necessary. |
| 268 | Builder.SetInsertPoint(ThisStore); |
| 269 | llvm::Value *AdjustedThisPtr = |
| 270 | PerformTypeAdjustment(*this, ThisPtr, |
| 271 | Thunk.This.NonVirtual, |
Eli Friedman | 82bad6b | 2012-09-14 01:45:09 +0000 | [diff] [blame] | 272 | Thunk.This.VCallOffsetOffset, |
| 273 | /*IsReturnAdjustment*/false); |
Eli Friedman | 7dcdf5b | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 274 | ThisStore->setOperand(0, AdjustedThisPtr); |
| 275 | |
| 276 | if (!Thunk.Return.isEmpty()) { |
| 277 | // Fix up the returned value, if necessary. |
| 278 | for (llvm::Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++) { |
| 279 | llvm::Instruction *T = I->getTerminator(); |
| 280 | if (isa<llvm::ReturnInst>(T)) { |
| 281 | RValue RV = RValue::get(T->getOperand(0)); |
| 282 | T->eraseFromParent(); |
| 283 | Builder.SetInsertPoint(&*I); |
| 284 | RV = PerformReturnAdjustment(*this, ResultType, RV, Thunk); |
| 285 | Builder.CreateRet(RV.getScalarVal()); |
| 286 | break; |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 292 | void CodeGenFunction::GenerateThunk(llvm::Function *Fn, |
| 293 | const CGFunctionInfo &FnInfo, |
| 294 | GlobalDecl GD, const ThunkInfo &Thunk) { |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 295 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 296 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 297 | QualType ThisType = MD->getThisType(getContext()); |
Stephen Lin | 3b50e8d | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 298 | QualType ResultType = |
| 299 | CGM.getCXXABI().HasThisReturn(GD) ? ThisType : FPT->getResultType(); |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 300 | |
| 301 | FunctionArgList FunctionArgs; |
| 302 | |
| 303 | // FIXME: It would be nice if more of this code could be shared with |
| 304 | // CodeGenFunction::GenerateCode. |
| 305 | |
| 306 | // Create the implicit 'this' parameter declaration. |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 307 | CurGD = GD; |
| 308 | CGM.getCXXABI().BuildInstanceFunctionParams(*this, ResultType, FunctionArgs); |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 309 | |
| 310 | // Add the rest of the parameters. |
| 311 | for (FunctionDecl::param_const_iterator I = MD->param_begin(), |
| 312 | E = MD->param_end(); I != E; ++I) { |
| 313 | ParmVarDecl *Param = *I; |
| 314 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 315 | FunctionArgs.push_back(Param); |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 316 | } |
Alexey Samsonov | 34b41f8 | 2012-10-25 10:18:50 +0000 | [diff] [blame] | 317 | |
| 318 | // Initialize debug info if needed. |
| 319 | maybeInitializeDebugInfo(); |
| 320 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 321 | StartFunction(GlobalDecl(), ResultType, Fn, FnInfo, FunctionArgs, |
| 322 | SourceLocation()); |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 323 | |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 324 | CGM.getCXXABI().EmitInstanceFunctionProlog(*this); |
Eli Friedman | cec5ebd | 2012-02-11 02:57:39 +0000 | [diff] [blame] | 325 | CXXThisValue = CXXABIThisValue; |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 326 | |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 327 | // Adjust the 'this' pointer if necessary. |
| 328 | llvm::Value *AdjustedThisPtr = |
| 329 | PerformTypeAdjustment(*this, LoadCXXThis(), |
| 330 | Thunk.This.NonVirtual, |
Eli Friedman | 82bad6b | 2012-09-14 01:45:09 +0000 | [diff] [blame] | 331 | Thunk.This.VCallOffsetOffset, |
| 332 | /*IsReturnAdjustment*/false); |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 333 | |
| 334 | CallArgList CallArgs; |
| 335 | |
| 336 | // Add our adjusted 'this' pointer. |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 337 | CallArgs.add(RValue::get(AdjustedThisPtr), ThisType); |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 338 | |
| 339 | // Add the rest of the parameters. |
| 340 | for (FunctionDecl::param_const_iterator I = MD->param_begin(), |
| 341 | E = MD->param_end(); I != E; ++I) { |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 342 | ParmVarDecl *param = *I; |
| 343 | EmitDelegateCallArg(CallArgs, param); |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | // Get our callee. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 347 | llvm::Type *Ty = |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 348 | CGM.getTypes().GetFunctionType(CGM.getTypes().arrangeGlobalDeclaration(GD)); |
Anders Carlsson | 84c49e4 | 2011-02-06 17:15:43 +0000 | [diff] [blame] | 349 | llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true); |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 350 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 351 | #ifndef NDEBUG |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 352 | const CGFunctionInfo &CallFnInfo = |
| 353 | CGM.getTypes().arrangeCXXMethodCall(CallArgs, FPT, |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 354 | RequiredArgs::forPrototypePlus(FPT, 1)); |
John McCall | 311b442 | 2011-03-09 07:12:35 +0000 | [diff] [blame] | 355 | assert(CallFnInfo.getRegParm() == FnInfo.getRegParm() && |
| 356 | CallFnInfo.isNoReturn() == FnInfo.isNoReturn() && |
| 357 | CallFnInfo.getCallingConvention() == FnInfo.getCallingConvention()); |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 358 | assert(isa<CXXDestructorDecl>(MD) || // ignore dtor return types |
| 359 | similar(CallFnInfo.getReturnInfo(), CallFnInfo.getReturnType(), |
John McCall | 311b442 | 2011-03-09 07:12:35 +0000 | [diff] [blame] | 360 | FnInfo.getReturnInfo(), FnInfo.getReturnType())); |
| 361 | assert(CallFnInfo.arg_size() == FnInfo.arg_size()); |
| 362 | for (unsigned i = 0, e = FnInfo.arg_size(); i != e; ++i) |
| 363 | assert(similar(CallFnInfo.arg_begin()[i].info, |
| 364 | CallFnInfo.arg_begin()[i].type, |
| 365 | FnInfo.arg_begin()[i].info, FnInfo.arg_begin()[i].type)); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 366 | #endif |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 367 | |
Douglas Gregor | cb359df | 2010-05-20 05:54:35 +0000 | [diff] [blame] | 368 | // Determine whether we have a return value slot to use. |
| 369 | ReturnValueSlot Slot; |
| 370 | if (!ResultType->isVoidType() && |
| 371 | FnInfo.getReturnInfo().getKind() == ABIArgInfo::Indirect && |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 372 | !hasScalarEvaluationKind(CurFnInfo->getReturnType())) |
Douglas Gregor | cb359df | 2010-05-20 05:54:35 +0000 | [diff] [blame] | 373 | Slot = ReturnValueSlot(ReturnValue, ResultType.isVolatileQualified()); |
| 374 | |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 375 | // Now emit our call. |
Douglas Gregor | cb359df | 2010-05-20 05:54:35 +0000 | [diff] [blame] | 376 | RValue RV = EmitCall(FnInfo, Callee, Slot, CallArgs, MD); |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 377 | |
Eli Friedman | 7dcdf5b | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 378 | if (!Thunk.Return.isEmpty()) |
| 379 | RV = PerformReturnAdjustment(*this, ResultType, RV, Thunk); |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 380 | |
Douglas Gregor | cb359df | 2010-05-20 05:54:35 +0000 | [diff] [blame] | 381 | if (!ResultType->isVoidType() && Slot.isNull()) |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 382 | CGM.getCXXABI().EmitReturnFromThunk(*this, RV, ResultType); |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 383 | |
John McCall | bd9b65a | 2012-07-31 00:33:55 +0000 | [diff] [blame] | 384 | // Disable the final ARC autorelease. |
| 385 | AutoreleaseResult = false; |
| 386 | |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 387 | FinishFunction(); |
| 388 | |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 389 | // Set the right linkage. |
Peter Collingbourne | 144a31f | 2013-06-05 17:49:37 +0000 | [diff] [blame] | 390 | CGM.setFunctionLinkage(GD, Fn); |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 391 | |
| 392 | // Set the right visibility. |
John McCall | 6500553 | 2010-08-04 23:46:35 +0000 | [diff] [blame] | 393 | setThunkVisibility(CGM, MD, Thunk, Fn); |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Anders Carlsson | 14e82fd | 2011-02-06 18:31:40 +0000 | [diff] [blame] | 396 | void CodeGenVTables::EmitThunk(GlobalDecl GD, const ThunkInfo &Thunk, |
| 397 | bool UseAvailableExternallyLinkage) |
Anders Carlsson | fbf6ed4 | 2010-03-23 16:36:50 +0000 | [diff] [blame] | 398 | { |
Timur Iskhodzhanov | 635de28 | 2013-07-30 09:46:19 +0000 | [diff] [blame^] | 399 | if (CGM.getTarget().getCXXABI().isMicrosoft()) { |
| 400 | // Emission of thunks is not supported yet in Microsoft ABI. |
| 401 | return; |
| 402 | } |
| 403 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 404 | const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeGlobalDeclaration(GD); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 405 | |
| 406 | // FIXME: re-use FnInfo in this computation. |
Anders Carlsson | 84c49e4 | 2011-02-06 17:15:43 +0000 | [diff] [blame] | 407 | llvm::Constant *Entry = CGM.GetAddrOfThunk(GD, Thunk); |
Anders Carlsson | 19879c9 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 408 | |
Anders Carlsson | 7986ad5 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 409 | // Strip off a bitcast if we got one back. |
Anders Carlsson | 13d6898 | 2010-03-24 00:35:44 +0000 | [diff] [blame] | 410 | if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { |
Anders Carlsson | 7986ad5 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 411 | assert(CE->getOpcode() == llvm::Instruction::BitCast); |
Anders Carlsson | 13d6898 | 2010-03-24 00:35:44 +0000 | [diff] [blame] | 412 | Entry = CE->getOperand(0); |
Anders Carlsson | 7986ad5 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Anders Carlsson | 7986ad5 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 415 | // There's already a declaration with the same name, check if it has the same |
| 416 | // type or if we need to replace it. |
Anders Carlsson | 13d6898 | 2010-03-24 00:35:44 +0000 | [diff] [blame] | 417 | if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() != |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 418 | CGM.getTypes().GetFunctionTypeForVTable(GD)) { |
Anders Carlsson | 13d6898 | 2010-03-24 00:35:44 +0000 | [diff] [blame] | 419 | llvm::GlobalValue *OldThunkFn = cast<llvm::GlobalValue>(Entry); |
Anders Carlsson | 7986ad5 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 420 | |
| 421 | // If the types mismatch then we have to rewrite the definition. |
| 422 | assert(OldThunkFn->isDeclaration() && |
| 423 | "Shouldn't replace non-declaration"); |
| 424 | |
| 425 | // Remove the name from the old thunk function and get a new thunk. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 426 | OldThunkFn->setName(StringRef()); |
Anders Carlsson | 84c49e4 | 2011-02-06 17:15:43 +0000 | [diff] [blame] | 427 | Entry = CGM.GetAddrOfThunk(GD, Thunk); |
Anders Carlsson | 7986ad5 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 428 | |
| 429 | // If needed, replace the old thunk with a bitcast. |
| 430 | if (!OldThunkFn->use_empty()) { |
| 431 | llvm::Constant *NewPtrForOldDecl = |
Anders Carlsson | 13d6898 | 2010-03-24 00:35:44 +0000 | [diff] [blame] | 432 | llvm::ConstantExpr::getBitCast(Entry, OldThunkFn->getType()); |
Anders Carlsson | 7986ad5 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 433 | OldThunkFn->replaceAllUsesWith(NewPtrForOldDecl); |
| 434 | } |
| 435 | |
| 436 | // Remove the old thunk. |
| 437 | OldThunkFn->eraseFromParent(); |
| 438 | } |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 439 | |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 440 | llvm::Function *ThunkFn = cast<llvm::Function>(Entry); |
Anders Carlsson | 14e82fd | 2011-02-06 18:31:40 +0000 | [diff] [blame] | 441 | |
| 442 | if (!ThunkFn->isDeclaration()) { |
| 443 | if (UseAvailableExternallyLinkage) { |
| 444 | // There is already a thunk emitted for this function, do nothing. |
| 445 | return; |
| 446 | } |
| 447 | |
Anders Carlsson | 22df7b1 | 2011-02-06 20:09:44 +0000 | [diff] [blame] | 448 | // If a function has a body, it should have available_externally linkage. |
| 449 | assert(ThunkFn->hasAvailableExternallyLinkage() && |
| 450 | "Function should have available_externally linkage!"); |
| 451 | |
| 452 | // Change the linkage. |
Peter Collingbourne | 144a31f | 2013-06-05 17:49:37 +0000 | [diff] [blame] | 453 | CGM.setFunctionLinkage(GD, ThunkFn); |
Anders Carlsson | 22df7b1 | 2011-02-06 20:09:44 +0000 | [diff] [blame] | 454 | return; |
Anders Carlsson | 14e82fd | 2011-02-06 18:31:40 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Rafael Espindola | 022301b | 2012-09-21 20:39:32 +0000 | [diff] [blame] | 457 | CGM.SetLLVMFunctionAttributesForDefinition(GD.getDecl(), ThunkFn); |
| 458 | |
Eli Friedman | 7dcdf5b | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 459 | if (ThunkFn->isVarArg()) { |
| 460 | // Varargs thunks are special; we can't just generate a call because |
| 461 | // we can't copy the varargs. Our implementation is rather |
| 462 | // expensive/sucky at the moment, so don't generate the thunk unless |
| 463 | // we have to. |
| 464 | // FIXME: Do something better here; GenerateVarArgsThunk is extremely ugly. |
| 465 | if (!UseAvailableExternallyLinkage) |
| 466 | CodeGenFunction(CGM).GenerateVarArgsThunk(ThunkFn, FnInfo, GD, Thunk); |
| 467 | } else { |
| 468 | // Normal thunk body generation. |
| 469 | CodeGenFunction(CGM).GenerateThunk(ThunkFn, FnInfo, GD, Thunk); |
| 470 | } |
Anders Carlsson | 14e82fd | 2011-02-06 18:31:40 +0000 | [diff] [blame] | 471 | |
| 472 | if (UseAvailableExternallyLinkage) |
| 473 | ThunkFn->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage); |
| 474 | } |
| 475 | |
| 476 | void CodeGenVTables::MaybeEmitThunkAvailableExternally(GlobalDecl GD, |
| 477 | const ThunkInfo &Thunk) { |
| 478 | // We only want to do this when building with optimizations. |
| 479 | if (!CGM.getCodeGenOpts().OptimizationLevel) |
| 480 | return; |
| 481 | |
| 482 | // We can't emit thunks for member functions with incomplete types. |
| 483 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
Chris Lattner | f742eb0 | 2011-07-10 00:18:59 +0000 | [diff] [blame] | 484 | if (!CGM.getTypes().isFuncTypeConvertible( |
| 485 | cast<FunctionType>(MD->getType().getTypePtr()))) |
Anders Carlsson | 14e82fd | 2011-02-06 18:31:40 +0000 | [diff] [blame] | 486 | return; |
| 487 | |
| 488 | EmitThunk(GD, Thunk, /*UseAvailableExternallyLinkage=*/true); |
Anders Carlsson | fbf6ed4 | 2010-03-23 16:36:50 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Anders Carlsson | ee5ab9f | 2010-03-23 04:59:02 +0000 | [diff] [blame] | 491 | void CodeGenVTables::EmitThunks(GlobalDecl GD) |
| 492 | { |
Anders Carlsson | fbf6ed4 | 2010-03-23 16:36:50 +0000 | [diff] [blame] | 493 | const CXXMethodDecl *MD = |
| 494 | cast<CXXMethodDecl>(GD.getDecl())->getCanonicalDecl(); |
| 495 | |
| 496 | // We don't need to generate thunks for the base destructor. |
| 497 | if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base) |
| 498 | return; |
| 499 | |
Timur Iskhodzhanov | 635de28 | 2013-07-30 09:46:19 +0000 | [diff] [blame^] | 500 | if (VFTContext.isValid()) { |
| 501 | // FIXME: This is a temporary solution to force generation of vftables in |
| 502 | // Microsoft ABI. Remove when we thread VFTableContext through CodeGen. |
| 503 | VFTContext->getVFPtrOffsets(MD->getParent()); |
| 504 | } |
| 505 | |
Peter Collingbourne | 84fcc48 | 2011-09-26 01:56:41 +0000 | [diff] [blame] | 506 | const VTableContext::ThunkInfoVectorTy *ThunkInfoVector = |
| 507 | VTContext.getThunkInfo(MD); |
| 508 | if (!ThunkInfoVector) |
Anders Carlsson | ccd83d7 | 2010-03-24 16:42:11 +0000 | [diff] [blame] | 509 | return; |
Anders Carlsson | ccd83d7 | 2010-03-24 16:42:11 +0000 | [diff] [blame] | 510 | |
Peter Collingbourne | 84fcc48 | 2011-09-26 01:56:41 +0000 | [diff] [blame] | 511 | for (unsigned I = 0, E = ThunkInfoVector->size(); I != E; ++I) |
| 512 | EmitThunk(GD, (*ThunkInfoVector)[I], |
| 513 | /*UseAvailableExternallyLinkage=*/false); |
Anders Carlsson | ee5ab9f | 2010-03-23 04:59:02 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 516 | llvm::Constant * |
| 517 | CodeGenVTables::CreateVTableInitializer(const CXXRecordDecl *RD, |
Peter Collingbourne | e09cdf4 | 2011-09-26 01:56:50 +0000 | [diff] [blame] | 518 | const VTableComponent *Components, |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 519 | unsigned NumComponents, |
Peter Collingbourne | e09cdf4 | 2011-09-26 01:56:50 +0000 | [diff] [blame] | 520 | const VTableLayout::VTableThunkTy *VTableThunks, |
| 521 | unsigned NumVTableThunks) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 522 | SmallVector<llvm::Constant *, 64> Inits; |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 523 | |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 524 | llvm::Type *Int8PtrTy = CGM.Int8PtrTy; |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 525 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 526 | llvm::Type *PtrDiffTy = |
Anders Carlsson | 6a5ab5d | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 527 | CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType()); |
| 528 | |
| 529 | QualType ClassType = CGM.getContext().getTagDeclType(RD); |
| 530 | llvm::Constant *RTTI = CGM.GetAddrOfRTTIDescriptor(ClassType); |
| 531 | |
| 532 | unsigned NextVTableThunkIndex = 0; |
| 533 | |
David Blaikie | 2eb9a95 | 2012-10-16 22:56:05 +0000 | [diff] [blame] | 534 | llvm::Constant *PureVirtualFn = 0, *DeletedVirtualFn = 0; |
Anders Carlsson | 67d568a | 2010-03-29 05:40:50 +0000 | [diff] [blame] | 535 | |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 536 | for (unsigned I = 0; I != NumComponents; ++I) { |
Peter Collingbourne | e09cdf4 | 2011-09-26 01:56:50 +0000 | [diff] [blame] | 537 | VTableComponent Component = Components[I]; |
Anders Carlsson | 6a5ab5d | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 538 | |
| 539 | llvm::Constant *Init = 0; |
| 540 | |
| 541 | switch (Component.getKind()) { |
Anders Carlsson | 9446481 | 2010-04-10 19:13:06 +0000 | [diff] [blame] | 542 | case VTableComponent::CK_VCallOffset: |
Ken Dyck | c40a3fd | 2011-04-02 01:14:48 +0000 | [diff] [blame] | 543 | Init = llvm::ConstantInt::get(PtrDiffTy, |
| 544 | Component.getVCallOffset().getQuantity()); |
Anders Carlsson | 6a5ab5d | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 545 | Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy); |
| 546 | break; |
Anders Carlsson | 9446481 | 2010-04-10 19:13:06 +0000 | [diff] [blame] | 547 | case VTableComponent::CK_VBaseOffset: |
Ken Dyck | c40a3fd | 2011-04-02 01:14:48 +0000 | [diff] [blame] | 548 | Init = llvm::ConstantInt::get(PtrDiffTy, |
| 549 | Component.getVBaseOffset().getQuantity()); |
Anders Carlsson | 6a5ab5d | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 550 | Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy); |
| 551 | break; |
Anders Carlsson | 9446481 | 2010-04-10 19:13:06 +0000 | [diff] [blame] | 552 | case VTableComponent::CK_OffsetToTop: |
Ken Dyck | c40a3fd | 2011-04-02 01:14:48 +0000 | [diff] [blame] | 553 | Init = llvm::ConstantInt::get(PtrDiffTy, |
| 554 | Component.getOffsetToTop().getQuantity()); |
Anders Carlsson | 6a5ab5d | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 555 | Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy); |
| 556 | break; |
Anders Carlsson | 9446481 | 2010-04-10 19:13:06 +0000 | [diff] [blame] | 557 | case VTableComponent::CK_RTTI: |
Anders Carlsson | 6a5ab5d | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 558 | Init = llvm::ConstantExpr::getBitCast(RTTI, Int8PtrTy); |
| 559 | break; |
Anders Carlsson | 9446481 | 2010-04-10 19:13:06 +0000 | [diff] [blame] | 560 | case VTableComponent::CK_FunctionPointer: |
| 561 | case VTableComponent::CK_CompleteDtorPointer: |
| 562 | case VTableComponent::CK_DeletingDtorPointer: { |
Anders Carlsson | 6a5ab5d | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 563 | GlobalDecl GD; |
| 564 | |
| 565 | // Get the right global decl. |
| 566 | switch (Component.getKind()) { |
| 567 | default: |
| 568 | llvm_unreachable("Unexpected vtable component kind"); |
Anders Carlsson | 9446481 | 2010-04-10 19:13:06 +0000 | [diff] [blame] | 569 | case VTableComponent::CK_FunctionPointer: |
Anders Carlsson | 6a5ab5d | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 570 | GD = Component.getFunctionDecl(); |
| 571 | break; |
Anders Carlsson | 9446481 | 2010-04-10 19:13:06 +0000 | [diff] [blame] | 572 | case VTableComponent::CK_CompleteDtorPointer: |
Anders Carlsson | 6a5ab5d | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 573 | GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Complete); |
| 574 | break; |
Anders Carlsson | 9446481 | 2010-04-10 19:13:06 +0000 | [diff] [blame] | 575 | case VTableComponent::CK_DeletingDtorPointer: |
Anders Carlsson | 6a5ab5d | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 576 | GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Deleting); |
| 577 | break; |
| 578 | } |
| 579 | |
Anders Carlsson | 67d568a | 2010-03-29 05:40:50 +0000 | [diff] [blame] | 580 | if (cast<CXXMethodDecl>(GD.getDecl())->isPure()) { |
| 581 | // We have a pure virtual member function. |
Joao Matos | e9af3e6 | 2012-07-17 19:17:58 +0000 | [diff] [blame] | 582 | if (!PureVirtualFn) { |
Eli Friedman | cf15f17 | 2012-09-14 01:19:01 +0000 | [diff] [blame] | 583 | llvm::FunctionType *Ty = |
| 584 | llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false); |
| 585 | StringRef PureCallName = CGM.getCXXABI().GetPureVirtualCallName(); |
| 586 | PureVirtualFn = CGM.CreateRuntimeFunction(Ty, PureCallName); |
| 587 | PureVirtualFn = llvm::ConstantExpr::getBitCast(PureVirtualFn, |
Joao Matos | e9af3e6 | 2012-07-17 19:17:58 +0000 | [diff] [blame] | 588 | CGM.Int8PtrTy); |
Anders Carlsson | 67d568a | 2010-03-29 05:40:50 +0000 | [diff] [blame] | 589 | } |
Anders Carlsson | 67d568a | 2010-03-29 05:40:50 +0000 | [diff] [blame] | 590 | Init = PureVirtualFn; |
David Blaikie | 2eb9a95 | 2012-10-16 22:56:05 +0000 | [diff] [blame] | 591 | } else if (cast<CXXMethodDecl>(GD.getDecl())->isDeleted()) { |
| 592 | if (!DeletedVirtualFn) { |
| 593 | llvm::FunctionType *Ty = |
| 594 | llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false); |
| 595 | StringRef DeletedCallName = |
| 596 | CGM.getCXXABI().GetDeletedVirtualCallName(); |
| 597 | DeletedVirtualFn = CGM.CreateRuntimeFunction(Ty, DeletedCallName); |
| 598 | DeletedVirtualFn = llvm::ConstantExpr::getBitCast(DeletedVirtualFn, |
| 599 | CGM.Int8PtrTy); |
| 600 | } |
| 601 | Init = DeletedVirtualFn; |
Anders Carlsson | 6a5ab5d | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 602 | } else { |
Anders Carlsson | 67d568a | 2010-03-29 05:40:50 +0000 | [diff] [blame] | 603 | // Check if we should use a thunk. |
Peter Collingbourne | e09cdf4 | 2011-09-26 01:56:50 +0000 | [diff] [blame] | 604 | if (NextVTableThunkIndex < NumVTableThunks && |
Anders Carlsson | 67d568a | 2010-03-29 05:40:50 +0000 | [diff] [blame] | 605 | VTableThunks[NextVTableThunkIndex].first == I) { |
| 606 | const ThunkInfo &Thunk = VTableThunks[NextVTableThunkIndex].second; |
Anders Carlsson | 6a5ab5d | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 607 | |
Anders Carlsson | 14e82fd | 2011-02-06 18:31:40 +0000 | [diff] [blame] | 608 | MaybeEmitThunkAvailableExternally(GD, Thunk); |
Benjamin Kramer | fce8009 | 2012-03-20 20:18:13 +0000 | [diff] [blame] | 609 | Init = CGM.GetAddrOfThunk(GD, Thunk); |
Anders Carlsson | 14e82fd | 2011-02-06 18:31:40 +0000 | [diff] [blame] | 610 | |
Anders Carlsson | 67d568a | 2010-03-29 05:40:50 +0000 | [diff] [blame] | 611 | NextVTableThunkIndex++; |
| 612 | } else { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 613 | llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVTable(GD); |
Anders Carlsson | 67d568a | 2010-03-29 05:40:50 +0000 | [diff] [blame] | 614 | |
Anders Carlsson | 1faa89f | 2011-02-05 04:35:53 +0000 | [diff] [blame] | 615 | Init = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true); |
Anders Carlsson | 67d568a | 2010-03-29 05:40:50 +0000 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy); |
Anders Carlsson | 6a5ab5d | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 619 | } |
Anders Carlsson | 6a5ab5d | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 620 | break; |
| 621 | } |
| 622 | |
Anders Carlsson | 9446481 | 2010-04-10 19:13:06 +0000 | [diff] [blame] | 623 | case VTableComponent::CK_UnusedFunctionPointer: |
Anders Carlsson | 6a5ab5d | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 624 | Init = llvm::ConstantExpr::getNullValue(Int8PtrTy); |
| 625 | break; |
| 626 | }; |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 627 | |
| 628 | Inits.push_back(Init); |
| 629 | } |
| 630 | |
| 631 | llvm::ArrayType *ArrayType = llvm::ArrayType::get(Int8PtrTy, NumComponents); |
Jay Foad | 9735760 | 2011-06-22 09:24:39 +0000 | [diff] [blame] | 632 | return llvm::ConstantArray::get(ArrayType, Inits); |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Anders Carlsson | 9dc338a | 2010-03-30 03:35:35 +0000 | [diff] [blame] | 635 | llvm::GlobalVariable *CodeGenVTables::GetAddrOfVTable(const CXXRecordDecl *RD) { |
Peter Collingbourne | bf1c5ae | 2011-09-26 01:56:36 +0000 | [diff] [blame] | 636 | llvm::GlobalVariable *&VTable = VTables[RD]; |
| 637 | if (VTable) |
| 638 | return VTable; |
| 639 | |
John McCall | d5617ee | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 640 | // Queue up this v-table for possible deferred emission. |
| 641 | CGM.addDeferredVTable(RD); |
Peter Collingbourne | bf1c5ae | 2011-09-26 01:56:36 +0000 | [diff] [blame] | 642 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 643 | SmallString<256> OutName; |
Rafael Espindola | f0be979 | 2011-02-11 02:52:17 +0000 | [diff] [blame] | 644 | llvm::raw_svector_ostream Out(OutName); |
| 645 | CGM.getCXXABI().getMangleContext().mangleCXXVTable(RD, Out); |
| 646 | Out.flush(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 647 | StringRef Name = OutName.str(); |
Mike Stump | 85615df | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 648 | |
Anders Carlsson | ccd83d7 | 2010-03-24 16:42:11 +0000 | [diff] [blame] | 649 | llvm::ArrayType *ArrayType = |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 650 | llvm::ArrayType::get(CGM.Int8PtrTy, |
Peter Collingbourne | e09cdf4 | 2011-09-26 01:56:50 +0000 | [diff] [blame] | 651 | VTContext.getVTableLayout(RD).getNumVTableComponents()); |
Anders Carlsson | a7cde3b | 2010-03-29 03:38:52 +0000 | [diff] [blame] | 652 | |
Peter Collingbourne | bf1c5ae | 2011-09-26 01:56:36 +0000 | [diff] [blame] | 653 | VTable = |
Anders Carlsson | 96eaf29 | 2011-01-29 18:25:07 +0000 | [diff] [blame] | 654 | CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType, |
| 655 | llvm::GlobalValue::ExternalLinkage); |
Peter Collingbourne | bf1c5ae | 2011-09-26 01:56:36 +0000 | [diff] [blame] | 656 | VTable->setUnnamedAddr(true); |
| 657 | return VTable; |
Mike Stump | 380dd75 | 2009-11-10 07:44:33 +0000 | [diff] [blame] | 658 | } |
Mike Stump | 8cfcb52 | 2009-11-11 20:26:26 +0000 | [diff] [blame] | 659 | |
Anders Carlsson | a7cde3b | 2010-03-29 03:38:52 +0000 | [diff] [blame] | 660 | void |
| 661 | CodeGenVTables::EmitVTableDefinition(llvm::GlobalVariable *VTable, |
| 662 | llvm::GlobalVariable::LinkageTypes Linkage, |
| 663 | const CXXRecordDecl *RD) { |
Peter Collingbourne | e09cdf4 | 2011-09-26 01:56:50 +0000 | [diff] [blame] | 664 | const VTableLayout &VTLayout = VTContext.getVTableLayout(RD); |
| 665 | |
Anders Carlsson | a7cde3b | 2010-03-29 03:38:52 +0000 | [diff] [blame] | 666 | // Create and set the initializer. |
| 667 | llvm::Constant *Init = |
Peter Collingbourne | e09cdf4 | 2011-09-26 01:56:50 +0000 | [diff] [blame] | 668 | CreateVTableInitializer(RD, |
| 669 | VTLayout.vtable_component_begin(), |
| 670 | VTLayout.getNumVTableComponents(), |
| 671 | VTLayout.vtable_thunk_begin(), |
| 672 | VTLayout.getNumVTableThunks()); |
Anders Carlsson | a7cde3b | 2010-03-29 03:38:52 +0000 | [diff] [blame] | 673 | VTable->setInitializer(Init); |
Anders Carlsson | 67d568a | 2010-03-29 05:40:50 +0000 | [diff] [blame] | 674 | |
| 675 | // Set the correct linkage. |
| 676 | VTable->setLinkage(Linkage); |
Douglas Gregor | c66bcfd | 2010-06-14 23:41:45 +0000 | [diff] [blame] | 677 | |
| 678 | // Set the right visibility. |
Anders Carlsson | fa2e99f | 2011-01-29 20:24:48 +0000 | [diff] [blame] | 679 | CGM.setTypeVisibility(VTable, RD, CodeGenModule::TVK_ForVTable); |
Anders Carlsson | a7cde3b | 2010-03-29 03:38:52 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Anders Carlsson | ff143f8 | 2010-03-25 00:35:49 +0000 | [diff] [blame] | 682 | llvm::GlobalVariable * |
| 683 | CodeGenVTables::GenerateConstructionVTable(const CXXRecordDecl *RD, |
Anders Carlsson | 2c822f1 | 2010-03-26 03:56:54 +0000 | [diff] [blame] | 684 | const BaseSubobject &Base, |
| 685 | bool BaseIsVirtual, |
John McCall | bda0d6b | 2011-03-27 09:00:25 +0000 | [diff] [blame] | 686 | llvm::GlobalVariable::LinkageTypes Linkage, |
Anders Carlsson | 2c822f1 | 2010-03-26 03:56:54 +0000 | [diff] [blame] | 687 | VTableAddressPointsMapTy& AddressPoints) { |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 688 | OwningPtr<VTableLayout> VTLayout( |
Peter Collingbourne | ab172b5 | 2011-09-26 01:57:04 +0000 | [diff] [blame] | 689 | VTContext.createConstructionVTableLayout(Base.getBase(), |
| 690 | Base.getBaseOffset(), |
| 691 | BaseIsVirtual, RD)); |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 692 | |
Anders Carlsson | 6a5ab5d | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 693 | // Add the address points. |
Peter Collingbourne | ab172b5 | 2011-09-26 01:57:04 +0000 | [diff] [blame] | 694 | AddressPoints = VTLayout->getAddressPoints(); |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 695 | |
| 696 | // Get the mangled construction vtable name. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 697 | SmallString<256> OutName; |
Rafael Espindola | f0be979 | 2011-02-11 02:52:17 +0000 | [diff] [blame] | 698 | llvm::raw_svector_ostream Out(OutName); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 699 | CGM.getCXXABI().getMangleContext(). |
Ken Dyck | 4230d52 | 2011-03-24 01:21:01 +0000 | [diff] [blame] | 700 | mangleCXXCtorVTable(RD, Base.getBaseOffset().getQuantity(), Base.getBase(), |
| 701 | Out); |
Rafael Espindola | f0be979 | 2011-02-11 02:52:17 +0000 | [diff] [blame] | 702 | Out.flush(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 703 | StringRef Name = OutName.str(); |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 704 | |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 705 | llvm::ArrayType *ArrayType = |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 706 | llvm::ArrayType::get(CGM.Int8PtrTy, VTLayout->getNumVTableComponents()); |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 707 | |
Richard Smith | b4127a2 | 2013-02-16 00:51:21 +0000 | [diff] [blame] | 708 | // Construction vtable symbols are not part of the Itanium ABI, so we cannot |
| 709 | // guarantee that they actually will be available externally. Instead, when |
| 710 | // emitting an available_externally VTT, we provide references to an internal |
| 711 | // linkage construction vtable. The ABI only requires complete-object vtables |
| 712 | // to be the same for all instances of a type, not construction vtables. |
| 713 | if (Linkage == llvm::GlobalVariable::AvailableExternallyLinkage) |
| 714 | Linkage = llvm::GlobalVariable::InternalLinkage; |
| 715 | |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 716 | // Create the variable that will hold the construction vtable. |
| 717 | llvm::GlobalVariable *VTable = |
John McCall | bda0d6b | 2011-03-27 09:00:25 +0000 | [diff] [blame] | 718 | CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType, Linkage); |
| 719 | CGM.setTypeVisibility(VTable, RD, CodeGenModule::TVK_ForConstructionVTable); |
| 720 | |
| 721 | // V-tables are always unnamed_addr. |
| 722 | VTable->setUnnamedAddr(true); |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 723 | |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 724 | // Create and set the initializer. |
| 725 | llvm::Constant *Init = |
| 726 | CreateVTableInitializer(Base.getBase(), |
Peter Collingbourne | ab172b5 | 2011-09-26 01:57:04 +0000 | [diff] [blame] | 727 | VTLayout->vtable_component_begin(), |
| 728 | VTLayout->getNumVTableComponents(), |
| 729 | VTLayout->vtable_thunk_begin(), |
| 730 | VTLayout->getNumVTableThunks()); |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 731 | VTable->setInitializer(Init); |
| 732 | |
Anders Carlsson | ff143f8 | 2010-03-25 00:35:49 +0000 | [diff] [blame] | 733 | return VTable; |
| 734 | } |
| 735 | |
John McCall | d5617ee | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 736 | /// Compute the required linkage of the v-table for the given class. |
| 737 | /// |
| 738 | /// Note that we only call this at the end of the translation unit. |
| 739 | llvm::GlobalVariable::LinkageTypes |
| 740 | CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) { |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 741 | if (!RD->isExternallyVisible()) |
John McCall | d5617ee | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 742 | return llvm::GlobalVariable::InternalLinkage; |
| 743 | |
| 744 | // We're at the end of the translation unit, so the current key |
| 745 | // function is fully correct. |
| 746 | if (const CXXMethodDecl *keyFunction = Context.getCurrentKeyFunction(RD)) { |
| 747 | // If this class has a key function, use that to determine the |
| 748 | // linkage of the vtable. |
| 749 | const FunctionDecl *def = 0; |
| 750 | if (keyFunction->hasBody(def)) |
| 751 | keyFunction = cast<CXXMethodDecl>(def); |
| 752 | |
| 753 | switch (keyFunction->getTemplateSpecializationKind()) { |
| 754 | case TSK_Undeclared: |
| 755 | case TSK_ExplicitSpecialization: |
| 756 | // When compiling with optimizations turned on, we emit all vtables, |
| 757 | // even if the key function is not defined in the current translation |
| 758 | // unit. If this is the case, use available_externally linkage. |
| 759 | if (!def && CodeGenOpts.OptimizationLevel) |
| 760 | return llvm::GlobalVariable::AvailableExternallyLinkage; |
| 761 | |
| 762 | if (keyFunction->isInlined()) |
| 763 | return !Context.getLangOpts().AppleKext ? |
| 764 | llvm::GlobalVariable::LinkOnceODRLinkage : |
| 765 | llvm::Function::InternalLinkage; |
| 766 | |
| 767 | return llvm::GlobalVariable::ExternalLinkage; |
| 768 | |
| 769 | case TSK_ImplicitInstantiation: |
| 770 | return !Context.getLangOpts().AppleKext ? |
| 771 | llvm::GlobalVariable::LinkOnceODRLinkage : |
| 772 | llvm::Function::InternalLinkage; |
| 773 | |
| 774 | case TSK_ExplicitInstantiationDefinition: |
| 775 | return !Context.getLangOpts().AppleKext ? |
| 776 | llvm::GlobalVariable::WeakODRLinkage : |
| 777 | llvm::Function::InternalLinkage; |
| 778 | |
| 779 | case TSK_ExplicitInstantiationDeclaration: |
John McCall | d5617ee | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 780 | return !Context.getLangOpts().AppleKext ? |
Richard Smith | b4127a2 | 2013-02-16 00:51:21 +0000 | [diff] [blame] | 781 | llvm::GlobalVariable::AvailableExternallyLinkage : |
John McCall | d5617ee | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 782 | llvm::Function::InternalLinkage; |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | // -fapple-kext mode does not support weak linkage, so we must use |
| 787 | // internal linkage. |
| 788 | if (Context.getLangOpts().AppleKext) |
| 789 | return llvm::Function::InternalLinkage; |
| 790 | |
| 791 | switch (RD->getTemplateSpecializationKind()) { |
| 792 | case TSK_Undeclared: |
| 793 | case TSK_ExplicitSpecialization: |
| 794 | case TSK_ImplicitInstantiation: |
| 795 | return llvm::GlobalVariable::LinkOnceODRLinkage; |
| 796 | |
| 797 | case TSK_ExplicitInstantiationDeclaration: |
Richard Smith | b4127a2 | 2013-02-16 00:51:21 +0000 | [diff] [blame] | 798 | return llvm::GlobalVariable::AvailableExternallyLinkage; |
John McCall | d5617ee | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 799 | |
| 800 | case TSK_ExplicitInstantiationDefinition: |
| 801 | return llvm::GlobalVariable::WeakODRLinkage; |
| 802 | } |
| 803 | |
| 804 | llvm_unreachable("Invalid TemplateSpecializationKind!"); |
| 805 | } |
| 806 | |
| 807 | /// This is a callback from Sema to tell us that it believes that a |
| 808 | /// particular v-table is required to be emitted in this translation |
| 809 | /// unit. |
| 810 | /// |
| 811 | /// The reason we don't simply trust this callback is because Sema |
| 812 | /// will happily report that something is used even when it's used |
| 813 | /// only in code that we don't actually have to emit. |
| 814 | /// |
| 815 | /// \param isRequired - if true, the v-table is mandatory, e.g. |
| 816 | /// because the translation unit defines the key function |
| 817 | void CodeGenModule::EmitVTable(CXXRecordDecl *theClass, bool isRequired) { |
| 818 | if (!isRequired) return; |
| 819 | |
| 820 | VTables.GenerateClassData(theClass); |
| 821 | } |
| 822 | |
Anders Carlsson | a7cde3b | 2010-03-29 03:38:52 +0000 | [diff] [blame] | 823 | void |
John McCall | d5617ee | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 824 | CodeGenVTables::GenerateClassData(const CXXRecordDecl *RD) { |
Timur Iskhodzhanov | 635de28 | 2013-07-30 09:46:19 +0000 | [diff] [blame^] | 825 | if (VFTContext.isValid()) { |
| 826 | // FIXME: This is a temporary solution to force generation of vftables in |
| 827 | // Microsoft ABI. Remove when we thread VFTableContext through CodeGen. |
| 828 | VFTContext->getVFPtrOffsets(RD); |
| 829 | } |
| 830 | |
John McCall | d5617ee | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 831 | // First off, check whether we've already emitted the v-table and |
| 832 | // associated stuff. |
Peter Collingbourne | bf1c5ae | 2011-09-26 01:56:36 +0000 | [diff] [blame] | 833 | llvm::GlobalVariable *VTable = GetAddrOfVTable(RD); |
| 834 | if (VTable->hasInitializer()) |
Anders Carlsson | a7cde3b | 2010-03-29 03:38:52 +0000 | [diff] [blame] | 835 | return; |
Anders Carlsson | a7cde3b | 2010-03-29 03:38:52 +0000 | [diff] [blame] | 836 | |
John McCall | d5617ee | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 837 | llvm::GlobalVariable::LinkageTypes Linkage = CGM.getVTableLinkage(RD); |
Anders Carlsson | 9dc338a | 2010-03-30 03:35:35 +0000 | [diff] [blame] | 838 | EmitVTableDefinition(VTable, Linkage, RD); |
| 839 | |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 840 | if (RD->getNumVBases()) |
| 841 | CGM.getCXXABI().EmitVirtualInheritanceTables(Linkage, RD); |
Douglas Gregor | 1e201b4 | 2010-04-08 15:52:03 +0000 | [diff] [blame] | 842 | |
| 843 | // If this is the magic class __cxxabiv1::__fundamental_type_info, |
| 844 | // we will emit the typeinfo for the fundamental types. This is the |
| 845 | // same behaviour as GCC. |
| 846 | const DeclContext *DC = RD->getDeclContext(); |
| 847 | if (RD->getIdentifier() && |
| 848 | RD->getIdentifier()->isStr("__fundamental_type_info") && |
| 849 | isa<NamespaceDecl>(DC) && |
| 850 | cast<NamespaceDecl>(DC)->getIdentifier() && |
| 851 | cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__cxxabiv1") && |
| 852 | DC->getParent()->isTranslationUnit()) |
| 853 | CGM.EmitFundamentalRTTIDescriptors(); |
Anders Carlsson | a7cde3b | 2010-03-29 03:38:52 +0000 | [diff] [blame] | 854 | } |
John McCall | d5617ee | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 855 | |
| 856 | /// At this point in the translation unit, does it appear that can we |
| 857 | /// rely on the vtable being defined elsewhere in the program? |
| 858 | /// |
| 859 | /// The response is really only definitive when called at the end of |
| 860 | /// the translation unit. |
| 861 | /// |
| 862 | /// The only semantic restriction here is that the object file should |
| 863 | /// not contain a v-table definition when that v-table is defined |
| 864 | /// strongly elsewhere. Otherwise, we'd just like to avoid emitting |
| 865 | /// v-tables when unnecessary. |
| 866 | bool CodeGenVTables::isVTableExternal(const CXXRecordDecl *RD) { |
| 867 | assert(RD->isDynamicClass() && "Non dynamic classes have no VTable."); |
| 868 | |
| 869 | // If we have an explicit instantiation declaration (and not a |
| 870 | // definition), the v-table is defined elsewhere. |
| 871 | TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind(); |
| 872 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
| 873 | return true; |
| 874 | |
| 875 | // Otherwise, if the class is an instantiated template, the |
| 876 | // v-table must be defined here. |
| 877 | if (TSK == TSK_ImplicitInstantiation || |
| 878 | TSK == TSK_ExplicitInstantiationDefinition) |
| 879 | return false; |
| 880 | |
| 881 | // Otherwise, if the class doesn't have a key function (possibly |
| 882 | // anymore), the v-table must be defined here. |
| 883 | const CXXMethodDecl *keyFunction = CGM.getContext().getCurrentKeyFunction(RD); |
| 884 | if (!keyFunction) |
| 885 | return false; |
| 886 | |
| 887 | // Otherwise, if we don't have a definition of the key function, the |
| 888 | // v-table must be defined somewhere else. |
| 889 | return !keyFunction->hasBody(); |
| 890 | } |
| 891 | |
| 892 | /// Given that we're currently at the end of the translation unit, and |
| 893 | /// we've emitted a reference to the v-table for this class, should |
| 894 | /// we define that v-table? |
| 895 | static bool shouldEmitVTableAtEndOfTranslationUnit(CodeGenModule &CGM, |
| 896 | const CXXRecordDecl *RD) { |
| 897 | // If we're building with optimization, we always emit v-tables |
| 898 | // since that allows for virtual function calls to be devirtualized. |
| 899 | // If the v-table is defined strongly elsewhere, this definition |
| 900 | // will be emitted available_externally. |
| 901 | // |
| 902 | // However, we don't want to do this in -fapple-kext mode, because |
| 903 | // kext mode does not permit devirtualization. |
| 904 | if (CGM.getCodeGenOpts().OptimizationLevel && !CGM.getLangOpts().AppleKext) |
| 905 | return true; |
| 906 | |
| 907 | return !CGM.getVTables().isVTableExternal(RD); |
| 908 | } |
| 909 | |
| 910 | /// Given that at some point we emitted a reference to one or more |
| 911 | /// v-tables, and that we are now at the end of the translation unit, |
| 912 | /// decide whether we should emit them. |
| 913 | void CodeGenModule::EmitDeferredVTables() { |
| 914 | #ifndef NDEBUG |
| 915 | // Remember the size of DeferredVTables, because we're going to assume |
| 916 | // that this entire operation doesn't modify it. |
| 917 | size_t savedSize = DeferredVTables.size(); |
| 918 | #endif |
| 919 | |
| 920 | typedef std::vector<const CXXRecordDecl *>::const_iterator const_iterator; |
| 921 | for (const_iterator i = DeferredVTables.begin(), |
| 922 | e = DeferredVTables.end(); i != e; ++i) { |
| 923 | const CXXRecordDecl *RD = *i; |
| 924 | if (shouldEmitVTableAtEndOfTranslationUnit(*this, RD)) |
| 925 | VTables.GenerateClassData(RD); |
| 926 | } |
| 927 | |
| 928 | assert(savedSize == DeferredVTables.size() && |
| 929 | "deferred extra v-tables during v-table emission?"); |
| 930 | DeferredVTables.clear(); |
| 931 | } |