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 | |
| 14 | #include "CodeGenModule.h" |
| 15 | #include "CodeGenFunction.h" |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 16 | #include "CGCXXABI.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) |
| 32 | : CGM(CGM), VTContext(CGM.getContext()) { } |
| 33 | |
Argyrios Kyrtzidis | d2c47bd | 2010-10-11 03:25:57 +0000 | [diff] [blame] | 34 | bool CodeGenVTables::ShouldEmitVTableInThisTU(const CXXRecordDecl *RD) { |
| 35 | assert(RD->isDynamicClass() && "Non dynamic classes have no VTable."); |
| 36 | |
| 37 | TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind(); |
| 38 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
| 39 | return false; |
| 40 | |
| 41 | const CXXMethodDecl *KeyFunction = CGM.getContext().getKeyFunction(RD); |
| 42 | if (!KeyFunction) |
| 43 | return true; |
| 44 | |
| 45 | // Itanium C++ ABI, 5.2.6 Instantiated Templates: |
| 46 | // An instantiation of a class template requires: |
| 47 | // - In the object where instantiated, the virtual table... |
| 48 | if (TSK == TSK_ImplicitInstantiation || |
| 49 | TSK == TSK_ExplicitInstantiationDefinition) |
| 50 | return true; |
| 51 | |
Anders Carlsson | 6d7f847 | 2011-01-30 20:45:54 +0000 | [diff] [blame] | 52 | // If we're building with optimization, we always emit VTables since that |
| 53 | // allows for virtual function calls to be devirtualized. |
| 54 | // (We don't want to do this in -fapple-kext mode however). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 55 | if (CGM.getCodeGenOpts().OptimizationLevel && !CGM.getLangOpts().AppleKext) |
Anders Carlsson | 6d7f847 | 2011-01-30 20:45:54 +0000 | [diff] [blame] | 56 | return true; |
| 57 | |
Argyrios Kyrtzidis | d2c47bd | 2010-10-11 03:25:57 +0000 | [diff] [blame] | 58 | return KeyFunction->hasBody(); |
| 59 | } |
| 60 | |
Anders Carlsson | 19879c9 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 61 | llvm::Constant *CodeGenModule::GetAddrOfThunk(GlobalDecl GD, |
Anders Carlsson | 84c49e4 | 2011-02-06 17:15:43 +0000 | [diff] [blame] | 62 | const ThunkInfo &Thunk) { |
Anders Carlsson | 19879c9 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 63 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 64 | |
| 65 | // Compute the mangled name. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 66 | SmallString<256> Name; |
Rafael Espindola | f0be979 | 2011-02-11 02:52:17 +0000 | [diff] [blame] | 67 | llvm::raw_svector_ostream Out(Name); |
Anders Carlsson | 19879c9 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 68 | if (const CXXDestructorDecl* DD = dyn_cast<CXXDestructorDecl>(MD)) |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 69 | getCXXABI().getMangleContext().mangleCXXDtorThunk(DD, GD.getDtorType(), |
Rafael Espindola | f0be979 | 2011-02-11 02:52:17 +0000 | [diff] [blame] | 70 | Thunk.This, Out); |
Anders Carlsson | 19879c9 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 71 | else |
Rafael Espindola | f0be979 | 2011-02-11 02:52:17 +0000 | [diff] [blame] | 72 | getCXXABI().getMangleContext().mangleThunk(MD, Thunk, Out); |
| 73 | Out.flush(); |
| 74 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 75 | llvm::Type *Ty = getTypes().GetFunctionTypeForVTable(GD); |
Anders Carlsson | 84c49e4 | 2011-02-06 17:15:43 +0000 | [diff] [blame] | 76 | return GetOrCreateLLVMFunction(Name, Ty, GD, /*ForVTable=*/true); |
Anders Carlsson | 19879c9 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 79 | static llvm::Value *PerformTypeAdjustment(CodeGenFunction &CGF, |
| 80 | llvm::Value *Ptr, |
| 81 | int64_t NonVirtualAdjustment, |
Eli Friedman | 82bad6b | 2012-09-14 01:45:09 +0000 | [diff] [blame] | 82 | int64_t VirtualAdjustment, |
| 83 | bool IsReturnAdjustment) { |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 84 | if (!NonVirtualAdjustment && !VirtualAdjustment) |
| 85 | return Ptr; |
| 86 | |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 87 | llvm::Type *Int8PtrTy = CGF.Int8PtrTy; |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 88 | llvm::Value *V = CGF.Builder.CreateBitCast(Ptr, Int8PtrTy); |
| 89 | |
Eli Friedman | 82bad6b | 2012-09-14 01:45:09 +0000 | [diff] [blame] | 90 | if (NonVirtualAdjustment && !IsReturnAdjustment) { |
| 91 | // Perform the non-virtual adjustment for a base-to-derived cast. |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 92 | V = CGF.Builder.CreateConstInBoundsGEP1_64(V, NonVirtualAdjustment); |
| 93 | } |
| 94 | |
| 95 | if (VirtualAdjustment) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 96 | llvm::Type *PtrDiffTy = |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 97 | CGF.ConvertType(CGF.getContext().getPointerDiffType()); |
| 98 | |
Eli Friedman | 82bad6b | 2012-09-14 01:45:09 +0000 | [diff] [blame] | 99 | // Perform the virtual adjustment. |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 100 | llvm::Value *VTablePtrPtr = |
| 101 | CGF.Builder.CreateBitCast(V, Int8PtrTy->getPointerTo()); |
| 102 | |
| 103 | llvm::Value *VTablePtr = CGF.Builder.CreateLoad(VTablePtrPtr); |
| 104 | |
| 105 | llvm::Value *OffsetPtr = |
| 106 | CGF.Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment); |
| 107 | |
| 108 | OffsetPtr = CGF.Builder.CreateBitCast(OffsetPtr, PtrDiffTy->getPointerTo()); |
| 109 | |
| 110 | // Load the adjustment offset from the vtable. |
| 111 | llvm::Value *Offset = CGF.Builder.CreateLoad(OffsetPtr); |
| 112 | |
| 113 | // Adjust our pointer. |
| 114 | V = CGF.Builder.CreateInBoundsGEP(V, Offset); |
| 115 | } |
| 116 | |
Eli Friedman | 82bad6b | 2012-09-14 01:45:09 +0000 | [diff] [blame] | 117 | if (NonVirtualAdjustment && IsReturnAdjustment) { |
| 118 | // Perform the non-virtual adjustment for a derived-to-base cast. |
| 119 | V = CGF.Builder.CreateConstInBoundsGEP1_64(V, NonVirtualAdjustment); |
| 120 | } |
| 121 | |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 122 | // Cast back to the original type. |
| 123 | return CGF.Builder.CreateBitCast(V, Ptr->getType()); |
| 124 | } |
| 125 | |
John McCall | 6500553 | 2010-08-04 23:46:35 +0000 | [diff] [blame] | 126 | static void setThunkVisibility(CodeGenModule &CGM, const CXXMethodDecl *MD, |
| 127 | const ThunkInfo &Thunk, llvm::Function *Fn) { |
Anders Carlsson | 0ffeaad | 2011-01-29 19:39:23 +0000 | [diff] [blame] | 128 | CGM.setGlobalVisibility(Fn, MD); |
John McCall | 6500553 | 2010-08-04 23:46:35 +0000 | [diff] [blame] | 129 | |
John McCall | 279b5eb | 2010-08-12 23:36:15 +0000 | [diff] [blame] | 130 | if (!CGM.getCodeGenOpts().HiddenWeakVTables) |
| 131 | return; |
| 132 | |
John McCall | 6500553 | 2010-08-04 23:46:35 +0000 | [diff] [blame] | 133 | // If the thunk has weak/linkonce linkage, but the function must be |
| 134 | // emitted in every translation unit that references it, then we can |
| 135 | // emit its thunks with hidden visibility, since its thunks must be |
| 136 | // emitted when the function is. |
| 137 | |
John McCall | 7a53690 | 2010-08-05 20:39:18 +0000 | [diff] [blame] | 138 | // This follows CodeGenModule::setTypeVisibility; see the comments |
| 139 | // there for explanation. |
John McCall | 6500553 | 2010-08-04 23:46:35 +0000 | [diff] [blame] | 140 | |
| 141 | if ((Fn->getLinkage() != llvm::GlobalVariable::LinkOnceODRLinkage && |
| 142 | Fn->getLinkage() != llvm::GlobalVariable::WeakODRLinkage) || |
| 143 | Fn->getVisibility() != llvm::GlobalVariable::DefaultVisibility) |
| 144 | return; |
| 145 | |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 146 | if (MD->getExplicitVisibility()) |
John McCall | 6500553 | 2010-08-04 23:46:35 +0000 | [diff] [blame] | 147 | return; |
| 148 | |
| 149 | switch (MD->getTemplateSpecializationKind()) { |
John McCall | 6500553 | 2010-08-04 23:46:35 +0000 | [diff] [blame] | 150 | case TSK_ExplicitInstantiationDefinition: |
| 151 | case TSK_ExplicitInstantiationDeclaration: |
| 152 | return; |
| 153 | |
John McCall | 6500553 | 2010-08-04 23:46:35 +0000 | [diff] [blame] | 154 | case TSK_Undeclared: |
| 155 | break; |
| 156 | |
John McCall | 7a53690 | 2010-08-05 20:39:18 +0000 | [diff] [blame] | 157 | case TSK_ExplicitSpecialization: |
John McCall | 6500553 | 2010-08-04 23:46:35 +0000 | [diff] [blame] | 158 | case TSK_ImplicitInstantiation: |
John McCall | 279b5eb | 2010-08-12 23:36:15 +0000 | [diff] [blame] | 159 | if (!CGM.getCodeGenOpts().HiddenWeakTemplateVTables) |
John McCall | 7a53690 | 2010-08-05 20:39:18 +0000 | [diff] [blame] | 160 | return; |
John McCall | 6500553 | 2010-08-04 23:46:35 +0000 | [diff] [blame] | 161 | break; |
| 162 | } |
| 163 | |
| 164 | // If there's an explicit definition, and that definition is |
| 165 | // out-of-line, then we can't assume that all users will have a |
| 166 | // definition to emit. |
| 167 | const FunctionDecl *Def = 0; |
| 168 | if (MD->hasBody(Def) && Def->isOutOfLine()) |
| 169 | return; |
| 170 | |
| 171 | Fn->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 172 | } |
| 173 | |
John McCall | 311b442 | 2011-03-09 07:12:35 +0000 | [diff] [blame] | 174 | #ifndef NDEBUG |
| 175 | static bool similar(const ABIArgInfo &infoL, CanQualType typeL, |
| 176 | const ABIArgInfo &infoR, CanQualType typeR) { |
| 177 | return (infoL.getKind() == infoR.getKind() && |
| 178 | (typeL == typeR || |
| 179 | (isa<PointerType>(typeL) && isa<PointerType>(typeR)) || |
| 180 | (isa<ReferenceType>(typeL) && isa<ReferenceType>(typeR)))); |
| 181 | } |
| 182 | #endif |
| 183 | |
Eli Friedman | 7dcdf5b | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 184 | static RValue PerformReturnAdjustment(CodeGenFunction &CGF, |
| 185 | QualType ResultType, RValue RV, |
| 186 | const ThunkInfo &Thunk) { |
| 187 | // Emit the return adjustment. |
| 188 | bool NullCheckValue = !ResultType->isReferenceType(); |
| 189 | |
| 190 | llvm::BasicBlock *AdjustNull = 0; |
| 191 | llvm::BasicBlock *AdjustNotNull = 0; |
| 192 | llvm::BasicBlock *AdjustEnd = 0; |
| 193 | |
| 194 | llvm::Value *ReturnValue = RV.getScalarVal(); |
| 195 | |
| 196 | if (NullCheckValue) { |
| 197 | AdjustNull = CGF.createBasicBlock("adjust.null"); |
| 198 | AdjustNotNull = CGF.createBasicBlock("adjust.notnull"); |
| 199 | AdjustEnd = CGF.createBasicBlock("adjust.end"); |
| 200 | |
| 201 | llvm::Value *IsNull = CGF.Builder.CreateIsNull(ReturnValue); |
| 202 | CGF.Builder.CreateCondBr(IsNull, AdjustNull, AdjustNotNull); |
| 203 | CGF.EmitBlock(AdjustNotNull); |
| 204 | } |
| 205 | |
| 206 | ReturnValue = PerformTypeAdjustment(CGF, ReturnValue, |
| 207 | Thunk.Return.NonVirtual, |
Eli Friedman | 82bad6b | 2012-09-14 01:45:09 +0000 | [diff] [blame] | 208 | Thunk.Return.VBaseOffsetOffset, |
| 209 | /*IsReturnAdjustment*/true); |
Eli Friedman | 7dcdf5b | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 210 | |
| 211 | if (NullCheckValue) { |
| 212 | CGF.Builder.CreateBr(AdjustEnd); |
| 213 | CGF.EmitBlock(AdjustNull); |
| 214 | CGF.Builder.CreateBr(AdjustEnd); |
| 215 | CGF.EmitBlock(AdjustEnd); |
| 216 | |
| 217 | llvm::PHINode *PHI = CGF.Builder.CreatePHI(ReturnValue->getType(), 2); |
| 218 | PHI->addIncoming(ReturnValue, AdjustNotNull); |
| 219 | PHI->addIncoming(llvm::Constant::getNullValue(ReturnValue->getType()), |
| 220 | AdjustNull); |
| 221 | ReturnValue = PHI; |
| 222 | } |
| 223 | |
| 224 | return RValue::get(ReturnValue); |
| 225 | } |
| 226 | |
| 227 | // This function does roughly the same thing as GenerateThunk, but in a |
| 228 | // very different way, so that va_start and va_end work correctly. |
| 229 | // FIXME: This function assumes "this" is the first non-sret LLVM argument of |
| 230 | // a function, and that there is an alloca built in the entry block |
| 231 | // for all accesses to "this". |
| 232 | // FIXME: This function assumes there is only one "ret" statement per function. |
| 233 | // FIXME: Cloning isn't correct in the presence of indirect goto! |
| 234 | // FIXME: This implementation of thunks bloats codesize by duplicating the |
| 235 | // function definition. There are alternatives: |
| 236 | // 1. Add some sort of stub support to LLVM for cases where we can |
| 237 | // do a this adjustment, then a sibcall. |
| 238 | // 2. We could transform the definition to take a va_list instead of an |
| 239 | // actual variable argument list, then have the thunks (including a |
| 240 | // no-op thunk for the regular definition) call va_start/va_end. |
| 241 | // There's a bit of per-call overhead for this solution, but it's |
| 242 | // better for codesize if the definition is long. |
| 243 | void CodeGenFunction::GenerateVarArgsThunk( |
| 244 | llvm::Function *Fn, |
| 245 | const CGFunctionInfo &FnInfo, |
| 246 | GlobalDecl GD, const ThunkInfo &Thunk) { |
| 247 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 248 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
| 249 | QualType ResultType = FPT->getResultType(); |
| 250 | |
| 251 | // Get the original function |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 252 | assert(FnInfo.isVariadic()); |
| 253 | llvm::Type *Ty = CGM.getTypes().GetFunctionType(FnInfo); |
Eli Friedman | 7dcdf5b | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 254 | llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true); |
| 255 | llvm::Function *BaseFn = cast<llvm::Function>(Callee); |
| 256 | |
| 257 | // Clone to thunk. |
Benjamin Kramer | 9b5ede5 | 2012-09-19 13:13:52 +0000 | [diff] [blame] | 258 | llvm::ValueToValueMapTy VMap; |
| 259 | llvm::Function *NewFn = llvm::CloneFunction(BaseFn, VMap, |
| 260 | /*ModuleLevelChanges=*/false); |
Eli Friedman | 7dcdf5b | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 261 | CGM.getModule().getFunctionList().push_back(NewFn); |
| 262 | Fn->replaceAllUsesWith(NewFn); |
| 263 | NewFn->takeName(Fn); |
| 264 | Fn->eraseFromParent(); |
| 265 | Fn = NewFn; |
| 266 | |
| 267 | // "Initialize" CGF (minimally). |
| 268 | CurFn = Fn; |
| 269 | |
| 270 | // Get the "this" value |
| 271 | llvm::Function::arg_iterator AI = Fn->arg_begin(); |
| 272 | if (CGM.ReturnTypeUsesSRet(FnInfo)) |
| 273 | ++AI; |
| 274 | |
| 275 | // Find the first store of "this", which will be to the alloca associated |
| 276 | // with "this". |
| 277 | llvm::Value *ThisPtr = &*AI; |
| 278 | llvm::BasicBlock *EntryBB = Fn->begin(); |
| 279 | llvm::Instruction *ThisStore = 0; |
| 280 | for (llvm::BasicBlock::iterator I = EntryBB->begin(), E = EntryBB->end(); |
| 281 | I != E; I++) { |
| 282 | if (isa<llvm::StoreInst>(I) && I->getOperand(0) == ThisPtr) { |
| 283 | ThisStore = cast<llvm::StoreInst>(I); |
| 284 | break; |
| 285 | } |
| 286 | } |
| 287 | assert(ThisStore && "Store of this should be in entry block?"); |
| 288 | // Adjust "this", if necessary. |
| 289 | Builder.SetInsertPoint(ThisStore); |
| 290 | llvm::Value *AdjustedThisPtr = |
| 291 | PerformTypeAdjustment(*this, ThisPtr, |
| 292 | Thunk.This.NonVirtual, |
Eli Friedman | 82bad6b | 2012-09-14 01:45:09 +0000 | [diff] [blame] | 293 | Thunk.This.VCallOffsetOffset, |
| 294 | /*IsReturnAdjustment*/false); |
Eli Friedman | 7dcdf5b | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 295 | ThisStore->setOperand(0, AdjustedThisPtr); |
| 296 | |
| 297 | if (!Thunk.Return.isEmpty()) { |
| 298 | // Fix up the returned value, if necessary. |
| 299 | for (llvm::Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++) { |
| 300 | llvm::Instruction *T = I->getTerminator(); |
| 301 | if (isa<llvm::ReturnInst>(T)) { |
| 302 | RValue RV = RValue::get(T->getOperand(0)); |
| 303 | T->eraseFromParent(); |
| 304 | Builder.SetInsertPoint(&*I); |
| 305 | RV = PerformReturnAdjustment(*this, ResultType, RV, Thunk); |
| 306 | Builder.CreateRet(RV.getScalarVal()); |
| 307 | break; |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 313 | void CodeGenFunction::GenerateThunk(llvm::Function *Fn, |
| 314 | const CGFunctionInfo &FnInfo, |
| 315 | GlobalDecl GD, const ThunkInfo &Thunk) { |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 316 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 317 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
| 318 | QualType ResultType = FPT->getResultType(); |
| 319 | QualType ThisType = MD->getThisType(getContext()); |
| 320 | |
| 321 | FunctionArgList FunctionArgs; |
| 322 | |
| 323 | // FIXME: It would be nice if more of this code could be shared with |
| 324 | // CodeGenFunction::GenerateCode. |
| 325 | |
| 326 | // Create the implicit 'this' parameter declaration. |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 327 | CurGD = GD; |
| 328 | CGM.getCXXABI().BuildInstanceFunctionParams(*this, ResultType, FunctionArgs); |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 329 | |
| 330 | // Add the rest of the parameters. |
| 331 | for (FunctionDecl::param_const_iterator I = MD->param_begin(), |
| 332 | E = MD->param_end(); I != E; ++I) { |
| 333 | ParmVarDecl *Param = *I; |
| 334 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 335 | FunctionArgs.push_back(Param); |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 336 | } |
Tilmann Scheller | 9c6082f | 2011-03-02 21:36:49 +0000 | [diff] [blame] | 337 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 338 | StartFunction(GlobalDecl(), ResultType, Fn, FnInfo, FunctionArgs, |
| 339 | SourceLocation()); |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 340 | |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 341 | CGM.getCXXABI().EmitInstanceFunctionProlog(*this); |
Eli Friedman | cec5ebd | 2012-02-11 02:57:39 +0000 | [diff] [blame] | 342 | CXXThisValue = CXXABIThisValue; |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 343 | |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 344 | // Adjust the 'this' pointer if necessary. |
| 345 | llvm::Value *AdjustedThisPtr = |
| 346 | PerformTypeAdjustment(*this, LoadCXXThis(), |
| 347 | Thunk.This.NonVirtual, |
Eli Friedman | 82bad6b | 2012-09-14 01:45:09 +0000 | [diff] [blame] | 348 | Thunk.This.VCallOffsetOffset, |
| 349 | /*IsReturnAdjustment*/false); |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 350 | |
| 351 | CallArgList CallArgs; |
| 352 | |
| 353 | // Add our adjusted 'this' pointer. |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 354 | CallArgs.add(RValue::get(AdjustedThisPtr), ThisType); |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 355 | |
| 356 | // Add the rest of the parameters. |
| 357 | for (FunctionDecl::param_const_iterator I = MD->param_begin(), |
| 358 | E = MD->param_end(); I != E; ++I) { |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 359 | ParmVarDecl *param = *I; |
| 360 | EmitDelegateCallArg(CallArgs, param); |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | // Get our callee. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 364 | llvm::Type *Ty = |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 365 | CGM.getTypes().GetFunctionType(CGM.getTypes().arrangeGlobalDeclaration(GD)); |
Anders Carlsson | 84c49e4 | 2011-02-06 17:15:43 +0000 | [diff] [blame] | 366 | llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true); |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 367 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 368 | #ifndef NDEBUG |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 369 | const CGFunctionInfo &CallFnInfo = |
| 370 | CGM.getTypes().arrangeCXXMethodCall(CallArgs, FPT, |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 371 | RequiredArgs::forPrototypePlus(FPT, 1)); |
John McCall | 311b442 | 2011-03-09 07:12:35 +0000 | [diff] [blame] | 372 | assert(CallFnInfo.getRegParm() == FnInfo.getRegParm() && |
| 373 | CallFnInfo.isNoReturn() == FnInfo.isNoReturn() && |
| 374 | CallFnInfo.getCallingConvention() == FnInfo.getCallingConvention()); |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 375 | assert(isa<CXXDestructorDecl>(MD) || // ignore dtor return types |
| 376 | similar(CallFnInfo.getReturnInfo(), CallFnInfo.getReturnType(), |
John McCall | 311b442 | 2011-03-09 07:12:35 +0000 | [diff] [blame] | 377 | FnInfo.getReturnInfo(), FnInfo.getReturnType())); |
| 378 | assert(CallFnInfo.arg_size() == FnInfo.arg_size()); |
| 379 | for (unsigned i = 0, e = FnInfo.arg_size(); i != e; ++i) |
| 380 | assert(similar(CallFnInfo.arg_begin()[i].info, |
| 381 | CallFnInfo.arg_begin()[i].type, |
| 382 | FnInfo.arg_begin()[i].info, FnInfo.arg_begin()[i].type)); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 383 | #endif |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 384 | |
Douglas Gregor | cb359df | 2010-05-20 05:54:35 +0000 | [diff] [blame] | 385 | // Determine whether we have a return value slot to use. |
| 386 | ReturnValueSlot Slot; |
| 387 | if (!ResultType->isVoidType() && |
| 388 | FnInfo.getReturnInfo().getKind() == ABIArgInfo::Indirect && |
| 389 | hasAggregateLLVMType(CurFnInfo->getReturnType())) |
| 390 | Slot = ReturnValueSlot(ReturnValue, ResultType.isVolatileQualified()); |
| 391 | |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 392 | // Now emit our call. |
Douglas Gregor | cb359df | 2010-05-20 05:54:35 +0000 | [diff] [blame] | 393 | RValue RV = EmitCall(FnInfo, Callee, Slot, CallArgs, MD); |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 394 | |
Eli Friedman | 7dcdf5b | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 395 | if (!Thunk.Return.isEmpty()) |
| 396 | RV = PerformReturnAdjustment(*this, ResultType, RV, Thunk); |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 397 | |
Douglas Gregor | cb359df | 2010-05-20 05:54:35 +0000 | [diff] [blame] | 398 | if (!ResultType->isVoidType() && Slot.isNull()) |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 399 | CGM.getCXXABI().EmitReturnFromThunk(*this, RV, ResultType); |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 400 | |
John McCall | bd9b65a | 2012-07-31 00:33:55 +0000 | [diff] [blame] | 401 | // Disable the final ARC autorelease. |
| 402 | AutoreleaseResult = false; |
| 403 | |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 404 | FinishFunction(); |
| 405 | |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 406 | // Set the right linkage. |
John McCall | 8b24233 | 2010-05-25 04:30:21 +0000 | [diff] [blame] | 407 | CGM.setFunctionLinkage(MD, Fn); |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 408 | |
| 409 | // Set the right visibility. |
John McCall | 6500553 | 2010-08-04 23:46:35 +0000 | [diff] [blame] | 410 | setThunkVisibility(CGM, MD, Thunk, Fn); |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Anders Carlsson | 14e82fd | 2011-02-06 18:31:40 +0000 | [diff] [blame] | 413 | void CodeGenVTables::EmitThunk(GlobalDecl GD, const ThunkInfo &Thunk, |
| 414 | bool UseAvailableExternallyLinkage) |
Anders Carlsson | fbf6ed4 | 2010-03-23 16:36:50 +0000 | [diff] [blame] | 415 | { |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 416 | const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeGlobalDeclaration(GD); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 417 | |
| 418 | // FIXME: re-use FnInfo in this computation. |
Anders Carlsson | 84c49e4 | 2011-02-06 17:15:43 +0000 | [diff] [blame] | 419 | llvm::Constant *Entry = CGM.GetAddrOfThunk(GD, Thunk); |
Anders Carlsson | 19879c9 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 420 | |
Anders Carlsson | 7986ad5 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 421 | // Strip off a bitcast if we got one back. |
Anders Carlsson | 13d6898 | 2010-03-24 00:35:44 +0000 | [diff] [blame] | 422 | if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { |
Anders Carlsson | 7986ad5 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 423 | assert(CE->getOpcode() == llvm::Instruction::BitCast); |
Anders Carlsson | 13d6898 | 2010-03-24 00:35:44 +0000 | [diff] [blame] | 424 | Entry = CE->getOperand(0); |
Anders Carlsson | 7986ad5 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Anders Carlsson | 7986ad5 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 427 | // There's already a declaration with the same name, check if it has the same |
| 428 | // type or if we need to replace it. |
Anders Carlsson | 13d6898 | 2010-03-24 00:35:44 +0000 | [diff] [blame] | 429 | if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() != |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 430 | CGM.getTypes().GetFunctionTypeForVTable(GD)) { |
Anders Carlsson | 13d6898 | 2010-03-24 00:35:44 +0000 | [diff] [blame] | 431 | llvm::GlobalValue *OldThunkFn = cast<llvm::GlobalValue>(Entry); |
Anders Carlsson | 7986ad5 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 432 | |
| 433 | // If the types mismatch then we have to rewrite the definition. |
| 434 | assert(OldThunkFn->isDeclaration() && |
| 435 | "Shouldn't replace non-declaration"); |
| 436 | |
| 437 | // 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] | 438 | OldThunkFn->setName(StringRef()); |
Anders Carlsson | 84c49e4 | 2011-02-06 17:15:43 +0000 | [diff] [blame] | 439 | Entry = CGM.GetAddrOfThunk(GD, Thunk); |
Anders Carlsson | 7986ad5 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 440 | |
| 441 | // If needed, replace the old thunk with a bitcast. |
| 442 | if (!OldThunkFn->use_empty()) { |
| 443 | llvm::Constant *NewPtrForOldDecl = |
Anders Carlsson | 13d6898 | 2010-03-24 00:35:44 +0000 | [diff] [blame] | 444 | llvm::ConstantExpr::getBitCast(Entry, OldThunkFn->getType()); |
Anders Carlsson | 7986ad5 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 445 | OldThunkFn->replaceAllUsesWith(NewPtrForOldDecl); |
| 446 | } |
| 447 | |
| 448 | // Remove the old thunk. |
| 449 | OldThunkFn->eraseFromParent(); |
| 450 | } |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 451 | |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 452 | llvm::Function *ThunkFn = cast<llvm::Function>(Entry); |
Anders Carlsson | 14e82fd | 2011-02-06 18:31:40 +0000 | [diff] [blame] | 453 | |
| 454 | if (!ThunkFn->isDeclaration()) { |
| 455 | if (UseAvailableExternallyLinkage) { |
| 456 | // There is already a thunk emitted for this function, do nothing. |
| 457 | return; |
| 458 | } |
| 459 | |
Anders Carlsson | 22df7b1 | 2011-02-06 20:09:44 +0000 | [diff] [blame] | 460 | // If a function has a body, it should have available_externally linkage. |
| 461 | assert(ThunkFn->hasAvailableExternallyLinkage() && |
| 462 | "Function should have available_externally linkage!"); |
| 463 | |
| 464 | // Change the linkage. |
| 465 | CGM.setFunctionLinkage(cast<CXXMethodDecl>(GD.getDecl()), ThunkFn); |
| 466 | return; |
Anders Carlsson | 14e82fd | 2011-02-06 18:31:40 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Rafael Espindola | 022301b | 2012-09-21 20:39:32 +0000 | [diff] [blame] | 469 | CGM.SetLLVMFunctionAttributesForDefinition(GD.getDecl(), ThunkFn); |
| 470 | |
Eli Friedman | 7dcdf5b | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 471 | if (ThunkFn->isVarArg()) { |
| 472 | // Varargs thunks are special; we can't just generate a call because |
| 473 | // we can't copy the varargs. Our implementation is rather |
| 474 | // expensive/sucky at the moment, so don't generate the thunk unless |
| 475 | // we have to. |
| 476 | // FIXME: Do something better here; GenerateVarArgsThunk is extremely ugly. |
| 477 | if (!UseAvailableExternallyLinkage) |
| 478 | CodeGenFunction(CGM).GenerateVarArgsThunk(ThunkFn, FnInfo, GD, Thunk); |
| 479 | } else { |
| 480 | // Normal thunk body generation. |
| 481 | CodeGenFunction(CGM).GenerateThunk(ThunkFn, FnInfo, GD, Thunk); |
| 482 | } |
Anders Carlsson | 14e82fd | 2011-02-06 18:31:40 +0000 | [diff] [blame] | 483 | |
| 484 | if (UseAvailableExternallyLinkage) |
| 485 | ThunkFn->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage); |
| 486 | } |
| 487 | |
| 488 | void CodeGenVTables::MaybeEmitThunkAvailableExternally(GlobalDecl GD, |
| 489 | const ThunkInfo &Thunk) { |
| 490 | // We only want to do this when building with optimizations. |
| 491 | if (!CGM.getCodeGenOpts().OptimizationLevel) |
| 492 | return; |
| 493 | |
| 494 | // We can't emit thunks for member functions with incomplete types. |
| 495 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
Chris Lattner | f742eb0 | 2011-07-10 00:18:59 +0000 | [diff] [blame] | 496 | if (!CGM.getTypes().isFuncTypeConvertible( |
| 497 | cast<FunctionType>(MD->getType().getTypePtr()))) |
Anders Carlsson | 14e82fd | 2011-02-06 18:31:40 +0000 | [diff] [blame] | 498 | return; |
| 499 | |
| 500 | EmitThunk(GD, Thunk, /*UseAvailableExternallyLinkage=*/true); |
Anders Carlsson | fbf6ed4 | 2010-03-23 16:36:50 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Anders Carlsson | ee5ab9f | 2010-03-23 04:59:02 +0000 | [diff] [blame] | 503 | void CodeGenVTables::EmitThunks(GlobalDecl GD) |
| 504 | { |
Anders Carlsson | fbf6ed4 | 2010-03-23 16:36:50 +0000 | [diff] [blame] | 505 | const CXXMethodDecl *MD = |
| 506 | cast<CXXMethodDecl>(GD.getDecl())->getCanonicalDecl(); |
| 507 | |
| 508 | // We don't need to generate thunks for the base destructor. |
| 509 | if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base) |
| 510 | return; |
| 511 | |
Peter Collingbourne | 84fcc48 | 2011-09-26 01:56:41 +0000 | [diff] [blame] | 512 | const VTableContext::ThunkInfoVectorTy *ThunkInfoVector = |
| 513 | VTContext.getThunkInfo(MD); |
| 514 | if (!ThunkInfoVector) |
Anders Carlsson | ccd83d7 | 2010-03-24 16:42:11 +0000 | [diff] [blame] | 515 | return; |
Anders Carlsson | ccd83d7 | 2010-03-24 16:42:11 +0000 | [diff] [blame] | 516 | |
Peter Collingbourne | 84fcc48 | 2011-09-26 01:56:41 +0000 | [diff] [blame] | 517 | for (unsigned I = 0, E = ThunkInfoVector->size(); I != E; ++I) |
| 518 | EmitThunk(GD, (*ThunkInfoVector)[I], |
| 519 | /*UseAvailableExternallyLinkage=*/false); |
Anders Carlsson | ee5ab9f | 2010-03-23 04:59:02 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 522 | llvm::Constant * |
| 523 | CodeGenVTables::CreateVTableInitializer(const CXXRecordDecl *RD, |
Peter Collingbourne | e09cdf4 | 2011-09-26 01:56:50 +0000 | [diff] [blame] | 524 | const VTableComponent *Components, |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 525 | unsigned NumComponents, |
Peter Collingbourne | e09cdf4 | 2011-09-26 01:56:50 +0000 | [diff] [blame] | 526 | const VTableLayout::VTableThunkTy *VTableThunks, |
| 527 | unsigned NumVTableThunks) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 528 | SmallVector<llvm::Constant *, 64> Inits; |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 529 | |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 530 | llvm::Type *Int8PtrTy = CGM.Int8PtrTy; |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 531 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 532 | llvm::Type *PtrDiffTy = |
Anders Carlsson | 6a5ab5d | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 533 | CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType()); |
| 534 | |
| 535 | QualType ClassType = CGM.getContext().getTagDeclType(RD); |
| 536 | llvm::Constant *RTTI = CGM.GetAddrOfRTTIDescriptor(ClassType); |
| 537 | |
| 538 | unsigned NextVTableThunkIndex = 0; |
| 539 | |
Anders Carlsson | 67d568a | 2010-03-29 05:40:50 +0000 | [diff] [blame] | 540 | llvm::Constant* PureVirtualFn = 0; |
| 541 | |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 542 | for (unsigned I = 0; I != NumComponents; ++I) { |
Peter Collingbourne | e09cdf4 | 2011-09-26 01:56:50 +0000 | [diff] [blame] | 543 | VTableComponent Component = Components[I]; |
Anders Carlsson | 6a5ab5d | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 544 | |
| 545 | llvm::Constant *Init = 0; |
| 546 | |
| 547 | switch (Component.getKind()) { |
Anders Carlsson | 9446481 | 2010-04-10 19:13:06 +0000 | [diff] [blame] | 548 | case VTableComponent::CK_VCallOffset: |
Ken Dyck | c40a3fd | 2011-04-02 01:14:48 +0000 | [diff] [blame] | 549 | Init = llvm::ConstantInt::get(PtrDiffTy, |
| 550 | Component.getVCallOffset().getQuantity()); |
Anders Carlsson | 6a5ab5d | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 551 | Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy); |
| 552 | break; |
Anders Carlsson | 9446481 | 2010-04-10 19:13:06 +0000 | [diff] [blame] | 553 | case VTableComponent::CK_VBaseOffset: |
Ken Dyck | c40a3fd | 2011-04-02 01:14:48 +0000 | [diff] [blame] | 554 | Init = llvm::ConstantInt::get(PtrDiffTy, |
| 555 | Component.getVBaseOffset().getQuantity()); |
Anders Carlsson | 6a5ab5d | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 556 | Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy); |
| 557 | break; |
Anders Carlsson | 9446481 | 2010-04-10 19:13:06 +0000 | [diff] [blame] | 558 | case VTableComponent::CK_OffsetToTop: |
Ken Dyck | c40a3fd | 2011-04-02 01:14:48 +0000 | [diff] [blame] | 559 | Init = llvm::ConstantInt::get(PtrDiffTy, |
| 560 | Component.getOffsetToTop().getQuantity()); |
Anders Carlsson | 6a5ab5d | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 561 | Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy); |
| 562 | break; |
Anders Carlsson | 9446481 | 2010-04-10 19:13:06 +0000 | [diff] [blame] | 563 | case VTableComponent::CK_RTTI: |
Anders Carlsson | 6a5ab5d | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 564 | Init = llvm::ConstantExpr::getBitCast(RTTI, Int8PtrTy); |
| 565 | break; |
Anders Carlsson | 9446481 | 2010-04-10 19:13:06 +0000 | [diff] [blame] | 566 | case VTableComponent::CK_FunctionPointer: |
| 567 | case VTableComponent::CK_CompleteDtorPointer: |
| 568 | case VTableComponent::CK_DeletingDtorPointer: { |
Anders Carlsson | 6a5ab5d | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 569 | GlobalDecl GD; |
| 570 | |
| 571 | // Get the right global decl. |
| 572 | switch (Component.getKind()) { |
| 573 | default: |
| 574 | llvm_unreachable("Unexpected vtable component kind"); |
Anders Carlsson | 9446481 | 2010-04-10 19:13:06 +0000 | [diff] [blame] | 575 | case VTableComponent::CK_FunctionPointer: |
Anders Carlsson | 6a5ab5d | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 576 | GD = Component.getFunctionDecl(); |
| 577 | break; |
Anders Carlsson | 9446481 | 2010-04-10 19:13:06 +0000 | [diff] [blame] | 578 | case VTableComponent::CK_CompleteDtorPointer: |
Anders Carlsson | 6a5ab5d | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 579 | GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Complete); |
| 580 | break; |
Anders Carlsson | 9446481 | 2010-04-10 19:13:06 +0000 | [diff] [blame] | 581 | case VTableComponent::CK_DeletingDtorPointer: |
Anders Carlsson | 6a5ab5d | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 582 | GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Deleting); |
| 583 | break; |
| 584 | } |
| 585 | |
Anders Carlsson | 67d568a | 2010-03-29 05:40:50 +0000 | [diff] [blame] | 586 | if (cast<CXXMethodDecl>(GD.getDecl())->isPure()) { |
| 587 | // We have a pure virtual member function. |
Joao Matos | e9af3e6 | 2012-07-17 19:17:58 +0000 | [diff] [blame] | 588 | if (!PureVirtualFn) { |
Eli Friedman | cf15f17 | 2012-09-14 01:19:01 +0000 | [diff] [blame] | 589 | llvm::FunctionType *Ty = |
| 590 | llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false); |
| 591 | StringRef PureCallName = CGM.getCXXABI().GetPureVirtualCallName(); |
| 592 | PureVirtualFn = CGM.CreateRuntimeFunction(Ty, PureCallName); |
| 593 | PureVirtualFn = llvm::ConstantExpr::getBitCast(PureVirtualFn, |
Joao Matos | e9af3e6 | 2012-07-17 19:17:58 +0000 | [diff] [blame] | 594 | CGM.Int8PtrTy); |
Anders Carlsson | 67d568a | 2010-03-29 05:40:50 +0000 | [diff] [blame] | 595 | } |
Anders Carlsson | 67d568a | 2010-03-29 05:40:50 +0000 | [diff] [blame] | 596 | Init = PureVirtualFn; |
Anders Carlsson | 6a5ab5d | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 597 | } else { |
Anders Carlsson | 67d568a | 2010-03-29 05:40:50 +0000 | [diff] [blame] | 598 | // Check if we should use a thunk. |
Peter Collingbourne | e09cdf4 | 2011-09-26 01:56:50 +0000 | [diff] [blame] | 599 | if (NextVTableThunkIndex < NumVTableThunks && |
Anders Carlsson | 67d568a | 2010-03-29 05:40:50 +0000 | [diff] [blame] | 600 | VTableThunks[NextVTableThunkIndex].first == I) { |
| 601 | const ThunkInfo &Thunk = VTableThunks[NextVTableThunkIndex].second; |
Anders Carlsson | 6a5ab5d | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 602 | |
Anders Carlsson | 14e82fd | 2011-02-06 18:31:40 +0000 | [diff] [blame] | 603 | MaybeEmitThunkAvailableExternally(GD, Thunk); |
Benjamin Kramer | fce8009 | 2012-03-20 20:18:13 +0000 | [diff] [blame] | 604 | Init = CGM.GetAddrOfThunk(GD, Thunk); |
Anders Carlsson | 14e82fd | 2011-02-06 18:31:40 +0000 | [diff] [blame] | 605 | |
Anders Carlsson | 67d568a | 2010-03-29 05:40:50 +0000 | [diff] [blame] | 606 | NextVTableThunkIndex++; |
| 607 | } else { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 608 | llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVTable(GD); |
Anders Carlsson | 67d568a | 2010-03-29 05:40:50 +0000 | [diff] [blame] | 609 | |
Anders Carlsson | 1faa89f | 2011-02-05 04:35:53 +0000 | [diff] [blame] | 610 | Init = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true); |
Anders Carlsson | 67d568a | 2010-03-29 05:40:50 +0000 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy); |
Anders Carlsson | 6a5ab5d | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 614 | } |
Anders Carlsson | 6a5ab5d | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 615 | break; |
| 616 | } |
| 617 | |
Anders Carlsson | 9446481 | 2010-04-10 19:13:06 +0000 | [diff] [blame] | 618 | case VTableComponent::CK_UnusedFunctionPointer: |
Anders Carlsson | 6a5ab5d | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 619 | Init = llvm::ConstantExpr::getNullValue(Int8PtrTy); |
| 620 | break; |
| 621 | }; |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 622 | |
| 623 | Inits.push_back(Init); |
| 624 | } |
| 625 | |
| 626 | llvm::ArrayType *ArrayType = llvm::ArrayType::get(Int8PtrTy, NumComponents); |
Jay Foad | 9735760 | 2011-06-22 09:24:39 +0000 | [diff] [blame] | 627 | return llvm::ConstantArray::get(ArrayType, Inits); |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Anders Carlsson | 9dc338a | 2010-03-30 03:35:35 +0000 | [diff] [blame] | 630 | llvm::GlobalVariable *CodeGenVTables::GetAddrOfVTable(const CXXRecordDecl *RD) { |
Peter Collingbourne | bf1c5ae | 2011-09-26 01:56:36 +0000 | [diff] [blame] | 631 | llvm::GlobalVariable *&VTable = VTables[RD]; |
| 632 | if (VTable) |
| 633 | return VTable; |
| 634 | |
| 635 | // We may need to generate a definition for this vtable. |
| 636 | if (ShouldEmitVTableInThisTU(RD)) |
| 637 | CGM.DeferredVTables.push_back(RD); |
| 638 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 639 | SmallString<256> OutName; |
Rafael Espindola | f0be979 | 2011-02-11 02:52:17 +0000 | [diff] [blame] | 640 | llvm::raw_svector_ostream Out(OutName); |
| 641 | CGM.getCXXABI().getMangleContext().mangleCXXVTable(RD, Out); |
| 642 | Out.flush(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 643 | StringRef Name = OutName.str(); |
Mike Stump | 85615df | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 644 | |
Anders Carlsson | ccd83d7 | 2010-03-24 16:42:11 +0000 | [diff] [blame] | 645 | llvm::ArrayType *ArrayType = |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 646 | llvm::ArrayType::get(CGM.Int8PtrTy, |
Peter Collingbourne | e09cdf4 | 2011-09-26 01:56:50 +0000 | [diff] [blame] | 647 | VTContext.getVTableLayout(RD).getNumVTableComponents()); |
Anders Carlsson | a7cde3b | 2010-03-29 03:38:52 +0000 | [diff] [blame] | 648 | |
Peter Collingbourne | bf1c5ae | 2011-09-26 01:56:36 +0000 | [diff] [blame] | 649 | VTable = |
Anders Carlsson | 96eaf29 | 2011-01-29 18:25:07 +0000 | [diff] [blame] | 650 | CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType, |
| 651 | llvm::GlobalValue::ExternalLinkage); |
Peter Collingbourne | bf1c5ae | 2011-09-26 01:56:36 +0000 | [diff] [blame] | 652 | VTable->setUnnamedAddr(true); |
| 653 | return VTable; |
Mike Stump | 380dd75 | 2009-11-10 07:44:33 +0000 | [diff] [blame] | 654 | } |
Mike Stump | 8cfcb52 | 2009-11-11 20:26:26 +0000 | [diff] [blame] | 655 | |
Anders Carlsson | a7cde3b | 2010-03-29 03:38:52 +0000 | [diff] [blame] | 656 | void |
| 657 | CodeGenVTables::EmitVTableDefinition(llvm::GlobalVariable *VTable, |
| 658 | llvm::GlobalVariable::LinkageTypes Linkage, |
| 659 | const CXXRecordDecl *RD) { |
Peter Collingbourne | e09cdf4 | 2011-09-26 01:56:50 +0000 | [diff] [blame] | 660 | const VTableLayout &VTLayout = VTContext.getVTableLayout(RD); |
| 661 | |
Anders Carlsson | a7cde3b | 2010-03-29 03:38:52 +0000 | [diff] [blame] | 662 | // Create and set the initializer. |
| 663 | llvm::Constant *Init = |
Peter Collingbourne | e09cdf4 | 2011-09-26 01:56:50 +0000 | [diff] [blame] | 664 | CreateVTableInitializer(RD, |
| 665 | VTLayout.vtable_component_begin(), |
| 666 | VTLayout.getNumVTableComponents(), |
| 667 | VTLayout.vtable_thunk_begin(), |
| 668 | VTLayout.getNumVTableThunks()); |
Anders Carlsson | a7cde3b | 2010-03-29 03:38:52 +0000 | [diff] [blame] | 669 | VTable->setInitializer(Init); |
Anders Carlsson | 67d568a | 2010-03-29 05:40:50 +0000 | [diff] [blame] | 670 | |
| 671 | // Set the correct linkage. |
| 672 | VTable->setLinkage(Linkage); |
Douglas Gregor | c66bcfd | 2010-06-14 23:41:45 +0000 | [diff] [blame] | 673 | |
| 674 | // Set the right visibility. |
Anders Carlsson | fa2e99f | 2011-01-29 20:24:48 +0000 | [diff] [blame] | 675 | CGM.setTypeVisibility(VTable, RD, CodeGenModule::TVK_ForVTable); |
Anders Carlsson | a7cde3b | 2010-03-29 03:38:52 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Anders Carlsson | ff143f8 | 2010-03-25 00:35:49 +0000 | [diff] [blame] | 678 | llvm::GlobalVariable * |
| 679 | CodeGenVTables::GenerateConstructionVTable(const CXXRecordDecl *RD, |
Anders Carlsson | 2c822f1 | 2010-03-26 03:56:54 +0000 | [diff] [blame] | 680 | const BaseSubobject &Base, |
| 681 | bool BaseIsVirtual, |
John McCall | bda0d6b | 2011-03-27 09:00:25 +0000 | [diff] [blame] | 682 | llvm::GlobalVariable::LinkageTypes Linkage, |
Anders Carlsson | 2c822f1 | 2010-03-26 03:56:54 +0000 | [diff] [blame] | 683 | VTableAddressPointsMapTy& AddressPoints) { |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 684 | OwningPtr<VTableLayout> VTLayout( |
Peter Collingbourne | ab172b5 | 2011-09-26 01:57:04 +0000 | [diff] [blame] | 685 | VTContext.createConstructionVTableLayout(Base.getBase(), |
| 686 | Base.getBaseOffset(), |
| 687 | BaseIsVirtual, RD)); |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 688 | |
Anders Carlsson | 6a5ab5d | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 689 | // Add the address points. |
Peter Collingbourne | ab172b5 | 2011-09-26 01:57:04 +0000 | [diff] [blame] | 690 | AddressPoints = VTLayout->getAddressPoints(); |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 691 | |
| 692 | // Get the mangled construction vtable name. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 693 | SmallString<256> OutName; |
Rafael Espindola | f0be979 | 2011-02-11 02:52:17 +0000 | [diff] [blame] | 694 | llvm::raw_svector_ostream Out(OutName); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 695 | CGM.getCXXABI().getMangleContext(). |
Ken Dyck | 4230d52 | 2011-03-24 01:21:01 +0000 | [diff] [blame] | 696 | mangleCXXCtorVTable(RD, Base.getBaseOffset().getQuantity(), Base.getBase(), |
| 697 | Out); |
Rafael Espindola | f0be979 | 2011-02-11 02:52:17 +0000 | [diff] [blame] | 698 | Out.flush(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 699 | StringRef Name = OutName.str(); |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 700 | |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 701 | llvm::ArrayType *ArrayType = |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 702 | llvm::ArrayType::get(CGM.Int8PtrTy, VTLayout->getNumVTableComponents()); |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 703 | |
| 704 | // Create the variable that will hold the construction vtable. |
| 705 | llvm::GlobalVariable *VTable = |
John McCall | bda0d6b | 2011-03-27 09:00:25 +0000 | [diff] [blame] | 706 | CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType, Linkage); |
| 707 | CGM.setTypeVisibility(VTable, RD, CodeGenModule::TVK_ForConstructionVTable); |
| 708 | |
| 709 | // V-tables are always unnamed_addr. |
| 710 | VTable->setUnnamedAddr(true); |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 711 | |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 712 | // Create and set the initializer. |
| 713 | llvm::Constant *Init = |
| 714 | CreateVTableInitializer(Base.getBase(), |
Peter Collingbourne | ab172b5 | 2011-09-26 01:57:04 +0000 | [diff] [blame] | 715 | VTLayout->vtable_component_begin(), |
| 716 | VTLayout->getNumVTableComponents(), |
| 717 | VTLayout->vtable_thunk_begin(), |
| 718 | VTLayout->getNumVTableThunks()); |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 719 | VTable->setInitializer(Init); |
| 720 | |
Anders Carlsson | ff143f8 | 2010-03-25 00:35:49 +0000 | [diff] [blame] | 721 | return VTable; |
| 722 | } |
| 723 | |
Anders Carlsson | a7cde3b | 2010-03-29 03:38:52 +0000 | [diff] [blame] | 724 | void |
| 725 | CodeGenVTables::GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage, |
| 726 | const CXXRecordDecl *RD) { |
Peter Collingbourne | bf1c5ae | 2011-09-26 01:56:36 +0000 | [diff] [blame] | 727 | llvm::GlobalVariable *VTable = GetAddrOfVTable(RD); |
| 728 | if (VTable->hasInitializer()) |
Anders Carlsson | a7cde3b | 2010-03-29 03:38:52 +0000 | [diff] [blame] | 729 | return; |
Anders Carlsson | a7cde3b | 2010-03-29 03:38:52 +0000 | [diff] [blame] | 730 | |
Anders Carlsson | 9dc338a | 2010-03-30 03:35:35 +0000 | [diff] [blame] | 731 | EmitVTableDefinition(VTable, Linkage, RD); |
| 732 | |
Anders Carlsson | 1cbce12 | 2011-01-29 19:16:51 +0000 | [diff] [blame] | 733 | if (RD->getNumVBases()) { |
| 734 | llvm::GlobalVariable *VTT = GetAddrOfVTT(RD); |
| 735 | EmitVTTDefinition(VTT, Linkage, RD); |
| 736 | } |
Douglas Gregor | 1e201b4 | 2010-04-08 15:52:03 +0000 | [diff] [blame] | 737 | |
| 738 | // If this is the magic class __cxxabiv1::__fundamental_type_info, |
| 739 | // we will emit the typeinfo for the fundamental types. This is the |
| 740 | // same behaviour as GCC. |
| 741 | const DeclContext *DC = RD->getDeclContext(); |
| 742 | if (RD->getIdentifier() && |
| 743 | RD->getIdentifier()->isStr("__fundamental_type_info") && |
| 744 | isa<NamespaceDecl>(DC) && |
| 745 | cast<NamespaceDecl>(DC)->getIdentifier() && |
| 746 | cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__cxxabiv1") && |
| 747 | DC->getParent()->isTranslationUnit()) |
| 748 | CGM.EmitFundamentalRTTIDescriptors(); |
Anders Carlsson | a7cde3b | 2010-03-29 03:38:52 +0000 | [diff] [blame] | 749 | } |