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