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