Anders Carlsson | 11e5140 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 1 | //===--- CGVTables.cpp - Emit LLVM Code for C++ vtables -------------------===// |
Anders Carlsson | 2bb27f5 | 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 | |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 14 | #include "CGCXXABI.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 15 | #include "CodeGenFunction.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "CodeGenModule.h" |
Anders Carlsson | f942ee0 | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 17 | #include "clang/AST/CXXInheritance.h" |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 18 | #include "clang/AST/RecordLayout.h" |
Mark Lacey | a8e7df3 | 2013-10-30 21:53:58 +0000 | [diff] [blame] | 19 | #include "clang/CodeGen/CGFunctionInfo.h" |
Wolfgang Pieb | a347c47 | 2017-10-31 22:49:48 +0000 | [diff] [blame] | 20 | #include "clang/CodeGen/ConstantInitBuilder.h" |
Saleem Abdulrasool | 10a4972 | 2016-04-08 16:52:00 +0000 | [diff] [blame] | 21 | #include "clang/Frontend/CodeGenOptions.h" |
Wolfgang Pieb | a347c47 | 2017-10-31 22:49:48 +0000 | [diff] [blame] | 22 | #include "llvm/IR/IntrinsicInst.h" |
Anders Carlsson | 5d40c6f | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Format.h" |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 24 | #include "llvm/Transforms/Utils/Cloning.h" |
Anders Carlsson | 5644614 | 2010-03-17 20:06:32 +0000 | [diff] [blame] | 25 | #include <algorithm> |
Zhongxing Xu | 1721ef7 | 2009-11-13 05:46:16 +0000 | [diff] [blame] | 26 | #include <cstdio> |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace clang; |
| 29 | using namespace CodeGen; |
| 30 | |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 31 | CodeGenVTables::CodeGenVTables(CodeGenModule &CGM) |
| 32 | : CGM(CGM), VTContext(CGM.getContext().getVTableContext()) {} |
Peter Collingbourne | a834166 | 2011-09-26 01:56:30 +0000 | [diff] [blame] | 33 | |
Simon Pilgrim | 48c32b1 | 2016-09-08 09:59:58 +0000 | [diff] [blame] | 34 | llvm::Constant *CodeGenModule::GetAddrOfThunk(GlobalDecl GD, |
Anders Carlsson | fe8a993 | 2011-02-06 17:15:43 +0000 | [diff] [blame] | 35 | const ThunkInfo &Thunk) { |
Anders Carlsson | cd836f0 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 36 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 37 | |
| 38 | // Compute the mangled name. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 39 | SmallString<256> Name; |
Rafael Espindola | 3968cd0 | 2011-02-11 02:52:17 +0000 | [diff] [blame] | 40 | llvm::raw_svector_ostream Out(Name); |
Anders Carlsson | cd836f0 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 41 | if (const CXXDestructorDecl* DD = dyn_cast<CXXDestructorDecl>(MD)) |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 42 | getCXXABI().getMangleContext().mangleCXXDtorThunk(DD, GD.getDtorType(), |
Rafael Espindola | 3968cd0 | 2011-02-11 02:52:17 +0000 | [diff] [blame] | 43 | Thunk.This, Out); |
Anders Carlsson | cd836f0 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 44 | else |
Rafael Espindola | 3968cd0 | 2011-02-11 02:52:17 +0000 | [diff] [blame] | 45 | getCXXABI().getMangleContext().mangleThunk(MD, Thunk, Out); |
Rafael Espindola | 3968cd0 | 2011-02-11 02:52:17 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 47 | llvm::Type *Ty = getTypes().GetFunctionTypeForVTable(GD); |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 48 | return GetOrCreateLLVMFunction(Name, Ty, GD, /*ForVTable=*/true, |
David Majnemer | b9bd6fb | 2014-11-01 05:42:23 +0000 | [diff] [blame] | 49 | /*DontDefer=*/true, /*IsThunk=*/true); |
Anders Carlsson | cd836f0 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 50 | } |
| 51 | |
John McCall | c8bd9c2 | 2010-08-04 23:46:35 +0000 | [diff] [blame] | 52 | static void setThunkVisibility(CodeGenModule &CGM, const CXXMethodDecl *MD, |
| 53 | const ThunkInfo &Thunk, llvm::Function *Fn) { |
Rafael Espindola | 880c3b2 | 2018-02-07 19:44:15 +0000 | [diff] [blame] | 54 | CGM.setGlobalVisibility(Fn, MD); |
John McCall | c8bd9c2 | 2010-08-04 23:46:35 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Rafael Espindola | 6bedf4a | 2015-07-15 14:48:06 +0000 | [diff] [blame] | 57 | static void setThunkProperties(CodeGenModule &CGM, const ThunkInfo &Thunk, |
| 58 | llvm::Function *ThunkFn, bool ForVTable, |
| 59 | GlobalDecl GD) { |
| 60 | CGM.setFunctionLinkage(GD, ThunkFn); |
| 61 | CGM.getCXXABI().setThunkLinkage(ThunkFn, ForVTable, GD, |
| 62 | !Thunk.Return.isEmpty()); |
| 63 | |
| 64 | // Set the right visibility. |
| 65 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 66 | setThunkVisibility(CGM, MD, Thunk, ThunkFn); |
| 67 | |
| 68 | if (CGM.supportsCOMDAT() && ThunkFn->isWeakForLinker()) |
| 69 | ThunkFn->setComdat(CGM.getModule().getOrInsertComdat(ThunkFn->getName())); |
| 70 | } |
| 71 | |
John McCall | 5fe0096 | 2011-03-09 07:12:35 +0000 | [diff] [blame] | 72 | #ifndef NDEBUG |
| 73 | static bool similar(const ABIArgInfo &infoL, CanQualType typeL, |
| 74 | const ABIArgInfo &infoR, CanQualType typeR) { |
| 75 | return (infoL.getKind() == infoR.getKind() && |
| 76 | (typeL == typeR || |
| 77 | (isa<PointerType>(typeL) && isa<PointerType>(typeR)) || |
| 78 | (isa<ReferenceType>(typeL) && isa<ReferenceType>(typeR)))); |
| 79 | } |
| 80 | #endif |
| 81 | |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 82 | static RValue PerformReturnAdjustment(CodeGenFunction &CGF, |
| 83 | QualType ResultType, RValue RV, |
| 84 | const ThunkInfo &Thunk) { |
| 85 | // Emit the return adjustment. |
| 86 | bool NullCheckValue = !ResultType->isReferenceType(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 87 | |
| 88 | llvm::BasicBlock *AdjustNull = nullptr; |
| 89 | llvm::BasicBlock *AdjustNotNull = nullptr; |
| 90 | llvm::BasicBlock *AdjustEnd = nullptr; |
| 91 | |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 92 | llvm::Value *ReturnValue = RV.getScalarVal(); |
| 93 | |
| 94 | if (NullCheckValue) { |
| 95 | AdjustNull = CGF.createBasicBlock("adjust.null"); |
| 96 | AdjustNotNull = CGF.createBasicBlock("adjust.notnull"); |
| 97 | AdjustEnd = CGF.createBasicBlock("adjust.end"); |
Simon Pilgrim | 48c32b1 | 2016-09-08 09:59:58 +0000 | [diff] [blame] | 98 | |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 99 | llvm::Value *IsNull = CGF.Builder.CreateIsNull(ReturnValue); |
| 100 | CGF.Builder.CreateCondBr(IsNull, AdjustNull, AdjustNotNull); |
| 101 | CGF.EmitBlock(AdjustNotNull); |
| 102 | } |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 103 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 104 | auto ClassDecl = ResultType->getPointeeType()->getAsCXXRecordDecl(); |
| 105 | auto ClassAlign = CGF.CGM.getClassPointerAlignment(ClassDecl); |
| 106 | ReturnValue = CGF.CGM.getCXXABI().performReturnAdjustment(CGF, |
| 107 | Address(ReturnValue, ClassAlign), |
| 108 | Thunk.Return); |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 109 | |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 110 | if (NullCheckValue) { |
| 111 | CGF.Builder.CreateBr(AdjustEnd); |
| 112 | CGF.EmitBlock(AdjustNull); |
| 113 | CGF.Builder.CreateBr(AdjustEnd); |
| 114 | CGF.EmitBlock(AdjustEnd); |
Simon Pilgrim | 48c32b1 | 2016-09-08 09:59:58 +0000 | [diff] [blame] | 115 | |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 116 | llvm::PHINode *PHI = CGF.Builder.CreatePHI(ReturnValue->getType(), 2); |
| 117 | PHI->addIncoming(ReturnValue, AdjustNotNull); |
Simon Pilgrim | 48c32b1 | 2016-09-08 09:59:58 +0000 | [diff] [blame] | 118 | PHI->addIncoming(llvm::Constant::getNullValue(ReturnValue->getType()), |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 119 | AdjustNull); |
| 120 | ReturnValue = PHI; |
| 121 | } |
Simon Pilgrim | 48c32b1 | 2016-09-08 09:59:58 +0000 | [diff] [blame] | 122 | |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 123 | return RValue::get(ReturnValue); |
| 124 | } |
| 125 | |
Wolfgang Pieb | a347c47 | 2017-10-31 22:49:48 +0000 | [diff] [blame] | 126 | /// This function clones a function's DISubprogram node and enters it into |
| 127 | /// a value map with the intent that the map can be utilized by the cloner |
| 128 | /// to short-circuit Metadata node mapping. |
| 129 | /// Furthermore, the function resolves any DILocalVariable nodes referenced |
| 130 | /// by dbg.value intrinsics so they can be properly mapped during cloning. |
| 131 | static void resolveTopLevelMetadata(llvm::Function *Fn, |
| 132 | llvm::ValueToValueMapTy &VMap) { |
| 133 | // Clone the DISubprogram node and put it into the Value map. |
| 134 | auto *DIS = Fn->getSubprogram(); |
| 135 | if (!DIS) |
| 136 | return; |
| 137 | auto *NewDIS = DIS->replaceWithDistinct(DIS->clone()); |
| 138 | VMap.MD()[DIS].reset(NewDIS); |
| 139 | |
| 140 | // Find all llvm.dbg.declare intrinsics and resolve the DILocalVariable nodes |
| 141 | // they are referencing. |
| 142 | for (auto &BB : Fn->getBasicBlockList()) { |
| 143 | for (auto &I : BB) { |
| 144 | if (auto *DII = dyn_cast<llvm::DbgInfoIntrinsic>(&I)) { |
| 145 | auto *DILocal = DII->getVariable(); |
| 146 | if (!DILocal->isResolved()) |
| 147 | DILocal->resolve(); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 153 | // This function does roughly the same thing as GenerateThunk, but in a |
| 154 | // very different way, so that va_start and va_end work correctly. |
| 155 | // FIXME: This function assumes "this" is the first non-sret LLVM argument of |
| 156 | // a function, and that there is an alloca built in the entry block |
| 157 | // for all accesses to "this". |
| 158 | // FIXME: This function assumes there is only one "ret" statement per function. |
| 159 | // FIXME: Cloning isn't correct in the presence of indirect goto! |
| 160 | // FIXME: This implementation of thunks bloats codesize by duplicating the |
| 161 | // function definition. There are alternatives: |
| 162 | // 1. Add some sort of stub support to LLVM for cases where we can |
| 163 | // do a this adjustment, then a sibcall. |
| 164 | // 2. We could transform the definition to take a va_list instead of an |
| 165 | // actual variable argument list, then have the thunks (including a |
| 166 | // no-op thunk for the regular definition) call va_start/va_end. |
| 167 | // There's a bit of per-call overhead for this solution, but it's |
| 168 | // better for codesize if the definition is long. |
Peter Collingbourne | e286b0e | 2015-06-30 22:08:44 +0000 | [diff] [blame] | 169 | llvm::Function * |
| 170 | CodeGenFunction::GenerateVarArgsThunk(llvm::Function *Fn, |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 171 | const CGFunctionInfo &FnInfo, |
| 172 | GlobalDecl GD, const ThunkInfo &Thunk) { |
| 173 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 174 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 175 | QualType ResultType = FPT->getReturnType(); |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 176 | |
| 177 | // Get the original function |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 178 | assert(FnInfo.isVariadic()); |
| 179 | llvm::Type *Ty = CGM.getTypes().GetFunctionType(FnInfo); |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 180 | llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true); |
| 181 | llvm::Function *BaseFn = cast<llvm::Function>(Callee); |
| 182 | |
| 183 | // Clone to thunk. |
Benjamin Kramer | 6ca4210 | 2012-09-19 13:13:52 +0000 | [diff] [blame] | 184 | llvm::ValueToValueMapTy VMap; |
Wolfgang Pieb | a347c47 | 2017-10-31 22:49:48 +0000 | [diff] [blame] | 185 | |
| 186 | // We are cloning a function while some Metadata nodes are still unresolved. |
| 187 | // Ensure that the value mapper does not encounter any of them. |
| 188 | resolveTopLevelMetadata(BaseFn, VMap); |
Peter Collingbourne | 7d6e81d | 2016-05-10 20:23:29 +0000 | [diff] [blame] | 189 | llvm::Function *NewFn = llvm::CloneFunction(BaseFn, VMap); |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 190 | Fn->replaceAllUsesWith(NewFn); |
| 191 | NewFn->takeName(Fn); |
| 192 | Fn->eraseFromParent(); |
| 193 | Fn = NewFn; |
| 194 | |
| 195 | // "Initialize" CGF (minimally). |
| 196 | CurFn = Fn; |
| 197 | |
| 198 | // Get the "this" value |
| 199 | llvm::Function::arg_iterator AI = Fn->arg_begin(); |
| 200 | if (CGM.ReturnTypeUsesSRet(FnInfo)) |
| 201 | ++AI; |
| 202 | |
| 203 | // Find the first store of "this", which will be to the alloca associated |
| 204 | // with "this". |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 205 | Address ThisPtr(&*AI, CGM.getClassPointerAlignment(MD->getParent())); |
Duncan P. N. Exon Smith | 9f5260a | 2015-11-06 23:00:41 +0000 | [diff] [blame] | 206 | llvm::BasicBlock *EntryBB = &Fn->front(); |
| 207 | llvm::BasicBlock::iterator ThisStore = |
David Blaikie | a629c0f | 2014-12-29 22:39:45 +0000 | [diff] [blame] | 208 | std::find_if(EntryBB->begin(), EntryBB->end(), [&](llvm::Instruction &I) { |
Duncan P. N. Exon Smith | 9f5260a | 2015-11-06 23:00:41 +0000 | [diff] [blame] | 209 | return isa<llvm::StoreInst>(I) && |
| 210 | I.getOperand(0) == ThisPtr.getPointer(); |
| 211 | }); |
| 212 | assert(ThisStore != EntryBB->end() && |
| 213 | "Store of this should be in entry block?"); |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 214 | // Adjust "this", if necessary. |
Duncan P. N. Exon Smith | 9f5260a | 2015-11-06 23:00:41 +0000 | [diff] [blame] | 215 | Builder.SetInsertPoint(&*ThisStore); |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 216 | llvm::Value *AdjustedThisPtr = |
| 217 | CGM.getCXXABI().performThisAdjustment(*this, ThisPtr, Thunk.This); |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 218 | ThisStore->setOperand(0, AdjustedThisPtr); |
| 219 | |
| 220 | if (!Thunk.Return.isEmpty()) { |
| 221 | // Fix up the returned value, if necessary. |
Piotr Padlewski | 44b4ce8 | 2015-07-28 16:10:58 +0000 | [diff] [blame] | 222 | for (llvm::BasicBlock &BB : *Fn) { |
| 223 | llvm::Instruction *T = BB.getTerminator(); |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 224 | if (isa<llvm::ReturnInst>(T)) { |
| 225 | RValue RV = RValue::get(T->getOperand(0)); |
| 226 | T->eraseFromParent(); |
Piotr Padlewski | 44b4ce8 | 2015-07-28 16:10:58 +0000 | [diff] [blame] | 227 | Builder.SetInsertPoint(&BB); |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 228 | RV = PerformReturnAdjustment(*this, ResultType, RV, Thunk); |
| 229 | Builder.CreateRet(RV.getScalarVal()); |
| 230 | break; |
| 231 | } |
| 232 | } |
| 233 | } |
Peter Collingbourne | e286b0e | 2015-06-30 22:08:44 +0000 | [diff] [blame] | 234 | |
| 235 | return Fn; |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 238 | void CodeGenFunction::StartThunk(llvm::Function *Fn, GlobalDecl GD, |
| 239 | const CGFunctionInfo &FnInfo) { |
| 240 | assert(!CurGD.getDecl() && "CurGD was already set!"); |
| 241 | CurGD = GD; |
Reid Kleckner | 1981944 | 2014-07-25 21:39:46 +0000 | [diff] [blame] | 242 | CurFuncIsThunk = true; |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 243 | |
| 244 | // Build FunctionArgs. |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 245 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 246 | QualType ThisType = MD->getThisType(getContext()); |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 247 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 248 | QualType ResultType = CGM.getCXXABI().HasThisReturn(GD) |
| 249 | ? ThisType |
| 250 | : CGM.getCXXABI().hasMostDerivedReturn(GD) |
| 251 | ? CGM.getContext().VoidPtrTy |
| 252 | : FPT->getReturnType(); |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 253 | FunctionArgList FunctionArgs; |
| 254 | |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 255 | // Create the implicit 'this' parameter declaration. |
Reid Kleckner | 89077a1 | 2013-12-17 19:46:40 +0000 | [diff] [blame] | 256 | CGM.getCXXABI().buildThisParam(*this, FunctionArgs); |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 257 | |
| 258 | // Add the rest of the parameters. |
Alexey Samsonov | 3551e31 | 2014-08-13 20:06:24 +0000 | [diff] [blame] | 259 | FunctionArgs.append(MD->param_begin(), MD->param_end()); |
Alexey Samsonov | 9b502e5 | 2012-10-25 10:18:50 +0000 | [diff] [blame] | 260 | |
Reid Kleckner | 89077a1 | 2013-12-17 19:46:40 +0000 | [diff] [blame] | 261 | if (isa<CXXDestructorDecl>(MD)) |
| 262 | CGM.getCXXABI().addImplicitStructorParams(*this, ResultType, FunctionArgs); |
| 263 | |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 264 | // Start defining the function. |
Adrian Prantl | db76357 | 2016-11-09 21:43:51 +0000 | [diff] [blame] | 265 | auto NL = ApplyDebugLocation::CreateEmpty(*this); |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 266 | StartFunction(GlobalDecl(), ResultType, Fn, FnInfo, FunctionArgs, |
Adrian Prantl | db76357 | 2016-11-09 21:43:51 +0000 | [diff] [blame] | 267 | MD->getLocation()); |
| 268 | // Create a scope with an artificial location for the body of this function. |
| 269 | auto AL = ApplyDebugLocation::CreateArtificial(*this); |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 270 | |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 271 | // Since we didn't pass a GlobalDecl to StartFunction, do this ourselves. |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 272 | CGM.getCXXABI().EmitInstanceFunctionProlog(*this); |
Eli Friedman | 9fbeba0 | 2012-02-11 02:57:39 +0000 | [diff] [blame] | 273 | CXXThisValue = CXXABIThisValue; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 274 | CurCodeDecl = MD; |
| 275 | CurFuncDecl = MD; |
| 276 | } |
| 277 | |
| 278 | void CodeGenFunction::FinishThunk() { |
| 279 | // Clear these to restore the invariants expected by |
| 280 | // StartFunction/FinishFunction. |
| 281 | CurCodeDecl = nullptr; |
| 282 | CurFuncDecl = nullptr; |
| 283 | |
| 284 | FinishFunction(); |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 285 | } |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 286 | |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 287 | void CodeGenFunction::EmitCallAndReturnForThunk(llvm::Constant *CalleePtr, |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 288 | const ThunkInfo *Thunk) { |
| 289 | assert(isa<CXXMethodDecl>(CurGD.getDecl()) && |
| 290 | "Please use a new CGF for this thunk"); |
Reid Kleckner | 3f76ac7 | 2014-07-26 01:30:05 +0000 | [diff] [blame] | 291 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(CurGD.getDecl()); |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 292 | |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 293 | // Adjust the 'this' pointer if necessary |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 294 | llvm::Value *AdjustedThisPtr = |
| 295 | Thunk ? CGM.getCXXABI().performThisAdjustment( |
| 296 | *this, LoadCXXThisAddress(), Thunk->This) |
| 297 | : LoadCXXThis(); |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 298 | |
Reid Kleckner | ab2090d | 2014-07-26 01:34:32 +0000 | [diff] [blame] | 299 | if (CurFnInfo->usesInAlloca()) { |
| 300 | // We don't handle return adjusting thunks, because they require us to call |
| 301 | // the copy constructor. For now, fall through and pretend the return |
| 302 | // adjustment was empty so we don't crash. |
| 303 | if (Thunk && !Thunk->Return.isEmpty()) { |
| 304 | CGM.ErrorUnsupported( |
| 305 | MD, "non-trivial argument copy for return-adjusting thunk"); |
| 306 | } |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 307 | EmitMustTailThunk(MD, AdjustedThisPtr, CalleePtr); |
Reid Kleckner | ab2090d | 2014-07-26 01:34:32 +0000 | [diff] [blame] | 308 | return; |
| 309 | } |
| 310 | |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 311 | // Start building CallArgs. |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 312 | CallArgList CallArgs; |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 313 | QualType ThisType = MD->getThisType(getContext()); |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 314 | CallArgs.add(RValue::get(AdjustedThisPtr), ThisType); |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 315 | |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 316 | if (isa<CXXDestructorDecl>(MD)) |
Reid Kleckner | 3f76ac7 | 2014-07-26 01:30:05 +0000 | [diff] [blame] | 317 | CGM.getCXXABI().adjustCallArgsForDestructorThunk(*this, CurGD, CallArgs); |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 318 | |
Benjamin Kramer | d12317e | 2017-02-23 22:47:56 +0000 | [diff] [blame] | 319 | #ifndef NDEBUG |
George Burgess IV | d0a9e80 | 2017-02-23 22:07:35 +0000 | [diff] [blame] | 320 | unsigned PrefixArgs = CallArgs.size() - 1; |
Benjamin Kramer | d12317e | 2017-02-23 22:47:56 +0000 | [diff] [blame] | 321 | #endif |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 322 | // Add the rest of the arguments. |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 323 | for (const ParmVarDecl *PD : MD->parameters()) |
Adrian Prantl | db76357 | 2016-11-09 21:43:51 +0000 | [diff] [blame] | 324 | EmitDelegateCallArg(CallArgs, PD, SourceLocation()); |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 325 | |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 326 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 327 | |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 328 | #ifndef NDEBUG |
George Burgess IV | 419996c | 2016-06-16 23:06:04 +0000 | [diff] [blame] | 329 | const CGFunctionInfo &CallFnInfo = CGM.getTypes().arrangeCXXMethodCall( |
George Burgess IV | d0a9e80 | 2017-02-23 22:07:35 +0000 | [diff] [blame] | 330 | CallArgs, FPT, RequiredArgs::forPrototypePlus(FPT, 1, MD), PrefixArgs); |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 331 | assert(CallFnInfo.getRegParm() == CurFnInfo->getRegParm() && |
| 332 | CallFnInfo.isNoReturn() == CurFnInfo->isNoReturn() && |
| 333 | CallFnInfo.getCallingConvention() == CurFnInfo->getCallingConvention()); |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 334 | assert(isa<CXXDestructorDecl>(MD) || // ignore dtor return types |
| 335 | similar(CallFnInfo.getReturnInfo(), CallFnInfo.getReturnType(), |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 336 | CurFnInfo->getReturnInfo(), CurFnInfo->getReturnType())); |
| 337 | assert(CallFnInfo.arg_size() == CurFnInfo->arg_size()); |
| 338 | for (unsigned i = 0, e = CurFnInfo->arg_size(); i != e; ++i) |
John McCall | 5fe0096 | 2011-03-09 07:12:35 +0000 | [diff] [blame] | 339 | assert(similar(CallFnInfo.arg_begin()[i].info, |
| 340 | CallFnInfo.arg_begin()[i].type, |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 341 | CurFnInfo->arg_begin()[i].info, |
| 342 | CurFnInfo->arg_begin()[i].type)); |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 343 | #endif |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 344 | |
Douglas Gregor | aa2ac80 | 2010-05-20 05:54:35 +0000 | [diff] [blame] | 345 | // Determine whether we have a return value slot to use. |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 346 | QualType ResultType = CGM.getCXXABI().HasThisReturn(CurGD) |
| 347 | ? ThisType |
| 348 | : CGM.getCXXABI().hasMostDerivedReturn(CurGD) |
| 349 | ? CGM.getContext().VoidPtrTy |
| 350 | : FPT->getReturnType(); |
Douglas Gregor | aa2ac80 | 2010-05-20 05:54:35 +0000 | [diff] [blame] | 351 | ReturnValueSlot Slot; |
| 352 | if (!ResultType->isVoidType() && |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 353 | CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect && |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 354 | !hasScalarEvaluationKind(CurFnInfo->getReturnType())) |
Douglas Gregor | aa2ac80 | 2010-05-20 05:54:35 +0000 | [diff] [blame] | 355 | Slot = ReturnValueSlot(ReturnValue, ResultType.isVolatileQualified()); |
Simon Pilgrim | 48c32b1 | 2016-09-08 09:59:58 +0000 | [diff] [blame] | 356 | |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 357 | // Now emit our call. |
Reid Kleckner | ab2090d | 2014-07-26 01:34:32 +0000 | [diff] [blame] | 358 | llvm::Instruction *CallOrInvoke; |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 359 | CGCallee Callee = CGCallee::forDirect(CalleePtr, MD); |
| 360 | RValue RV = EmitCall(*CurFnInfo, Callee, Slot, CallArgs, &CallOrInvoke); |
Reid Kleckner | ab2090d | 2014-07-26 01:34:32 +0000 | [diff] [blame] | 361 | |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 362 | // Consider return adjustment if we have ThunkInfo. |
| 363 | if (Thunk && !Thunk->Return.isEmpty()) |
| 364 | RV = PerformReturnAdjustment(*this, ResultType, RV, *Thunk); |
Michael Kuperstein | 819ad33 | 2015-08-06 11:57:15 +0000 | [diff] [blame] | 365 | else if (llvm::CallInst* Call = dyn_cast<llvm::CallInst>(CallOrInvoke)) |
| 366 | Call->setTailCallKind(llvm::CallInst::TCK_Tail); |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 367 | |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 368 | // Emit return. |
Douglas Gregor | aa2ac80 | 2010-05-20 05:54:35 +0000 | [diff] [blame] | 369 | if (!ResultType->isVoidType() && Slot.isNull()) |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 370 | CGM.getCXXABI().EmitReturnFromThunk(*this, RV, ResultType); |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 371 | |
John McCall | ff755cd | 2012-07-31 00:33:55 +0000 | [diff] [blame] | 372 | // Disable the final ARC autorelease. |
| 373 | AutoreleaseResult = false; |
| 374 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 375 | FinishThunk(); |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Reid Kleckner | ab2090d | 2014-07-26 01:34:32 +0000 | [diff] [blame] | 378 | void CodeGenFunction::EmitMustTailThunk(const CXXMethodDecl *MD, |
| 379 | llvm::Value *AdjustedThisPtr, |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 380 | llvm::Value *CalleePtr) { |
Reid Kleckner | ab2090d | 2014-07-26 01:34:32 +0000 | [diff] [blame] | 381 | // Emitting a musttail call thunk doesn't use any of the CGCall.cpp machinery |
| 382 | // to translate AST arguments into LLVM IR arguments. For thunks, we know |
| 383 | // that the caller prototype more or less matches the callee prototype with |
| 384 | // the exception of 'this'. |
| 385 | SmallVector<llvm::Value *, 8> Args; |
| 386 | for (llvm::Argument &A : CurFn->args()) |
| 387 | Args.push_back(&A); |
| 388 | |
| 389 | // Set the adjusted 'this' pointer. |
| 390 | const ABIArgInfo &ThisAI = CurFnInfo->arg_begin()->info; |
| 391 | if (ThisAI.isDirect()) { |
| 392 | const ABIArgInfo &RetAI = CurFnInfo->getReturnInfo(); |
| 393 | int ThisArgNo = RetAI.isIndirect() && !RetAI.isSRetAfterThis() ? 1 : 0; |
| 394 | llvm::Type *ThisType = Args[ThisArgNo]->getType(); |
| 395 | if (ThisType != AdjustedThisPtr->getType()) |
| 396 | AdjustedThisPtr = Builder.CreateBitCast(AdjustedThisPtr, ThisType); |
| 397 | Args[ThisArgNo] = AdjustedThisPtr; |
| 398 | } else { |
| 399 | assert(ThisAI.isInAlloca() && "this is passed directly or inalloca"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 400 | Address ThisAddr = GetAddrOfLocalVar(CXXABIThisDecl); |
| 401 | llvm::Type *ThisType = ThisAddr.getElementType(); |
Reid Kleckner | ab2090d | 2014-07-26 01:34:32 +0000 | [diff] [blame] | 402 | if (ThisType != AdjustedThisPtr->getType()) |
| 403 | AdjustedThisPtr = Builder.CreateBitCast(AdjustedThisPtr, ThisType); |
| 404 | Builder.CreateStore(AdjustedThisPtr, ThisAddr); |
| 405 | } |
| 406 | |
| 407 | // Emit the musttail call manually. Even if the prologue pushed cleanups, we |
| 408 | // don't actually want to run them. |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 409 | llvm::CallInst *Call = Builder.CreateCall(CalleePtr, Args); |
Reid Kleckner | ab2090d | 2014-07-26 01:34:32 +0000 | [diff] [blame] | 410 | Call->setTailCallKind(llvm::CallInst::TCK_MustTail); |
| 411 | |
| 412 | // Apply the standard set of call attributes. |
| 413 | unsigned CallingConv; |
Reid Kleckner | cdd2679 | 2017-04-18 23:50:03 +0000 | [diff] [blame] | 414 | llvm::AttributeList Attrs; |
| 415 | CGM.ConstructAttributeList(CalleePtr->getName(), *CurFnInfo, MD, Attrs, |
Chad Rosier | 7dbc9cf | 2016-01-06 14:35:46 +0000 | [diff] [blame] | 416 | CallingConv, /*AttrOnCallSite=*/true); |
Reid Kleckner | ab2090d | 2014-07-26 01:34:32 +0000 | [diff] [blame] | 417 | Call->setAttributes(Attrs); |
| 418 | Call->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv)); |
| 419 | |
| 420 | if (Call->getType()->isVoidTy()) |
| 421 | Builder.CreateRetVoid(); |
| 422 | else |
| 423 | Builder.CreateRet(Call); |
| 424 | |
| 425 | // Finish the function to maintain CodeGenFunction invariants. |
| 426 | // FIXME: Don't emit unreachable code. |
| 427 | EmitBlock(createBasicBlock()); |
| 428 | FinishFunction(); |
| 429 | } |
| 430 | |
Rafael Espindola | d6e6694 | 2015-07-13 06:07:58 +0000 | [diff] [blame] | 431 | void CodeGenFunction::generateThunk(llvm::Function *Fn, |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 432 | const CGFunctionInfo &FnInfo, |
| 433 | GlobalDecl GD, const ThunkInfo &Thunk) { |
| 434 | StartThunk(Fn, GD, FnInfo); |
Adrian Prantl | db76357 | 2016-11-09 21:43:51 +0000 | [diff] [blame] | 435 | // Create a scope with an artificial location for the body of this function. |
| 436 | auto AL = ApplyDebugLocation::CreateArtificial(*this); |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 437 | |
| 438 | // Get our callee. |
| 439 | llvm::Type *Ty = |
| 440 | CGM.getTypes().GetFunctionType(CGM.getTypes().arrangeGlobalDeclaration(GD)); |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 441 | llvm::Constant *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true); |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 442 | |
| 443 | // Make the call and return the result. |
Reid Kleckner | 3f76ac7 | 2014-07-26 01:30:05 +0000 | [diff] [blame] | 444 | EmitCallAndReturnForThunk(Callee, &Thunk); |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 447 | void CodeGenVTables::emitThunk(GlobalDecl GD, const ThunkInfo &Thunk, |
| 448 | bool ForVTable) { |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 449 | const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeGlobalDeclaration(GD); |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 450 | |
| 451 | // FIXME: re-use FnInfo in this computation. |
Rafael Espindola | bf6e67f | 2014-05-08 15:44:45 +0000 | [diff] [blame] | 452 | llvm::Constant *C = CGM.GetAddrOfThunk(GD, Thunk); |
| 453 | llvm::GlobalValue *Entry; |
| 454 | |
Anders Carlsson | 55e89f8 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 455 | // Strip off a bitcast if we got one back. |
Rafael Espindola | bf6e67f | 2014-05-08 15:44:45 +0000 | [diff] [blame] | 456 | if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(C)) { |
Anders Carlsson | 55e89f8 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 457 | assert(CE->getOpcode() == llvm::Instruction::BitCast); |
Rafael Espindola | bf6e67f | 2014-05-08 15:44:45 +0000 | [diff] [blame] | 458 | Entry = cast<llvm::GlobalValue>(CE->getOperand(0)); |
| 459 | } else { |
| 460 | Entry = cast<llvm::GlobalValue>(C); |
Anders Carlsson | 55e89f8 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 461 | } |
Rafael Espindola | bf6e67f | 2014-05-08 15:44:45 +0000 | [diff] [blame] | 462 | |
Anders Carlsson | 55e89f8 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 463 | // There's already a declaration with the same name, check if it has the same |
| 464 | // type or if we need to replace it. |
Rafael Espindola | bf6e67f | 2014-05-08 15:44:45 +0000 | [diff] [blame] | 465 | if (Entry->getType()->getElementType() != |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 466 | CGM.getTypes().GetFunctionTypeForVTable(GD)) { |
Rafael Espindola | bf6e67f | 2014-05-08 15:44:45 +0000 | [diff] [blame] | 467 | llvm::GlobalValue *OldThunkFn = Entry; |
| 468 | |
Anders Carlsson | 55e89f8 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 469 | // If the types mismatch then we have to rewrite the definition. |
| 470 | assert(OldThunkFn->isDeclaration() && |
| 471 | "Shouldn't replace non-declaration"); |
| 472 | |
| 473 | // Remove the name from the old thunk function and get a new thunk. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 474 | OldThunkFn->setName(StringRef()); |
Rafael Espindola | bf6e67f | 2014-05-08 15:44:45 +0000 | [diff] [blame] | 475 | Entry = cast<llvm::GlobalValue>(CGM.GetAddrOfThunk(GD, Thunk)); |
Simon Pilgrim | 48c32b1 | 2016-09-08 09:59:58 +0000 | [diff] [blame] | 476 | |
Anders Carlsson | 55e89f8 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 477 | // If needed, replace the old thunk with a bitcast. |
| 478 | if (!OldThunkFn->use_empty()) { |
| 479 | llvm::Constant *NewPtrForOldDecl = |
Anders Carlsson | 4a3cdf5 | 2010-03-24 00:35:44 +0000 | [diff] [blame] | 480 | llvm::ConstantExpr::getBitCast(Entry, OldThunkFn->getType()); |
Anders Carlsson | 55e89f8 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 481 | OldThunkFn->replaceAllUsesWith(NewPtrForOldDecl); |
| 482 | } |
Simon Pilgrim | 48c32b1 | 2016-09-08 09:59:58 +0000 | [diff] [blame] | 483 | |
Anders Carlsson | 55e89f8 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 484 | // Remove the old thunk. |
| 485 | OldThunkFn->eraseFromParent(); |
| 486 | } |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 487 | |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 488 | llvm::Function *ThunkFn = cast<llvm::Function>(Entry); |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 489 | bool ABIHasKeyFunctions = CGM.getTarget().getCXXABI().hasKeyFunctions(); |
| 490 | bool UseAvailableExternallyLinkage = ForVTable && ABIHasKeyFunctions; |
Anders Carlsson | 8b02183 | 2011-02-06 18:31:40 +0000 | [diff] [blame] | 491 | |
| 492 | if (!ThunkFn->isDeclaration()) { |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 493 | if (!ABIHasKeyFunctions || UseAvailableExternallyLinkage) { |
Anders Carlsson | 8b02183 | 2011-02-06 18:31:40 +0000 | [diff] [blame] | 494 | // There is already a thunk emitted for this function, do nothing. |
| 495 | return; |
| 496 | } |
| 497 | |
Rafael Espindola | 6bedf4a | 2015-07-15 14:48:06 +0000 | [diff] [blame] | 498 | setThunkProperties(CGM, Thunk, ThunkFn, ForVTable, GD); |
Anders Carlsson | e866d44 | 2011-02-06 20:09:44 +0000 | [diff] [blame] | 499 | return; |
Anders Carlsson | 8b02183 | 2011-02-06 18:31:40 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Rafael Espindola | 8679243 | 2012-09-21 20:39:32 +0000 | [diff] [blame] | 502 | CGM.SetLLVMFunctionAttributesForDefinition(GD.getDecl(), ThunkFn); |
| 503 | |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 504 | if (ThunkFn->isVarArg()) { |
| 505 | // Varargs thunks are special; we can't just generate a call because |
| 506 | // we can't copy the varargs. Our implementation is rather |
| 507 | // expensive/sucky at the moment, so don't generate the thunk unless |
| 508 | // we have to. |
| 509 | // FIXME: Do something better here; GenerateVarArgsThunk is extremely ugly. |
Peter Collingbourne | 45a2401 | 2015-06-30 19:07:26 +0000 | [diff] [blame] | 510 | if (UseAvailableExternallyLinkage) |
| 511 | return; |
Peter Collingbourne | e286b0e | 2015-06-30 22:08:44 +0000 | [diff] [blame] | 512 | ThunkFn = |
| 513 | CodeGenFunction(CGM).GenerateVarArgsThunk(ThunkFn, FnInfo, GD, Thunk); |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 514 | } else { |
| 515 | // Normal thunk body generation. |
Rafael Espindola | d6e6694 | 2015-07-13 06:07:58 +0000 | [diff] [blame] | 516 | CodeGenFunction(CGM).generateThunk(ThunkFn, FnInfo, GD, Thunk); |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 517 | } |
Peter Collingbourne | 45a2401 | 2015-06-30 19:07:26 +0000 | [diff] [blame] | 518 | |
Rafael Espindola | 6bedf4a | 2015-07-15 14:48:06 +0000 | [diff] [blame] | 519 | setThunkProperties(CGM, Thunk, ThunkFn, ForVTable, GD); |
Anders Carlsson | 8b02183 | 2011-02-06 18:31:40 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 522 | void CodeGenVTables::maybeEmitThunkForVTable(GlobalDecl GD, |
| 523 | const ThunkInfo &Thunk) { |
| 524 | // If the ABI has key functions, only the TU with the key function should emit |
| 525 | // the thunk. However, we can allow inlining of thunks if we emit them with |
| 526 | // available_externally linkage together with vtables when optimizations are |
| 527 | // enabled. |
| 528 | if (CGM.getTarget().getCXXABI().hasKeyFunctions() && |
| 529 | !CGM.getCodeGenOpts().OptimizationLevel) |
Anders Carlsson | 8b02183 | 2011-02-06 18:31:40 +0000 | [diff] [blame] | 530 | return; |
| 531 | |
| 532 | // We can't emit thunks for member functions with incomplete types. |
| 533 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
Chris Lattner | 8806e32 | 2011-07-10 00:18:59 +0000 | [diff] [blame] | 534 | if (!CGM.getTypes().isFuncTypeConvertible( |
Reid Kleckner | fe56be5 | 2013-10-11 20:46:27 +0000 | [diff] [blame] | 535 | MD->getType()->castAs<FunctionType>())) |
Anders Carlsson | 8b02183 | 2011-02-06 18:31:40 +0000 | [diff] [blame] | 536 | return; |
| 537 | |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 538 | emitThunk(GD, Thunk, /*ForVTable=*/true); |
Anders Carlsson | 5c5abad | 2010-03-23 16:36:50 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Anders Carlsson | 917229c | 2010-03-23 04:59:02 +0000 | [diff] [blame] | 541 | void CodeGenVTables::EmitThunks(GlobalDecl GD) |
| 542 | { |
Simon Pilgrim | 48c32b1 | 2016-09-08 09:59:58 +0000 | [diff] [blame] | 543 | const CXXMethodDecl *MD = |
Anders Carlsson | 5c5abad | 2010-03-23 16:36:50 +0000 | [diff] [blame] | 544 | cast<CXXMethodDecl>(GD.getDecl())->getCanonicalDecl(); |
| 545 | |
| 546 | // We don't need to generate thunks for the base destructor. |
| 547 | if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base) |
| 548 | return; |
| 549 | |
Reid Kleckner | b60a3d5 | 2013-12-20 23:58:52 +0000 | [diff] [blame] | 550 | const VTableContextBase::ThunkInfoVectorTy *ThunkInfoVector = |
| 551 | VTContext->getThunkInfo(GD); |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 552 | |
Peter Collingbourne | 5ee9ee4 | 2011-09-26 01:56:41 +0000 | [diff] [blame] | 553 | if (!ThunkInfoVector) |
Anders Carlsson | e90954d | 2010-03-24 16:42:11 +0000 | [diff] [blame] | 554 | return; |
Anders Carlsson | e90954d | 2010-03-24 16:42:11 +0000 | [diff] [blame] | 555 | |
Yaron Keren | ede6030 | 2015-08-01 19:11:36 +0000 | [diff] [blame] | 556 | for (const ThunkInfo& Thunk : *ThunkInfoVector) |
| 557 | emitThunk(GD, Thunk, /*ForVTable=*/false); |
Anders Carlsson | 917229c | 2010-03-23 04:59:02 +0000 | [diff] [blame] | 558 | } |
| 559 | |
John McCall | 9c6cb76 | 2016-11-28 22:18:33 +0000 | [diff] [blame] | 560 | void CodeGenVTables::addVTableComponent( |
| 561 | ConstantArrayBuilder &builder, const VTableLayout &layout, |
| 562 | unsigned idx, llvm::Constant *rtti, unsigned &nextVTableThunkIndex) { |
| 563 | auto &component = layout.vtable_components()[idx]; |
Anders Carlsson | a414714 | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 564 | |
John McCall | 9c6cb76 | 2016-11-28 22:18:33 +0000 | [diff] [blame] | 565 | auto addOffsetConstant = [&](CharUnits offset) { |
| 566 | builder.add(llvm::ConstantExpr::getIntToPtr( |
| 567 | llvm::ConstantInt::get(CGM.PtrDiffTy, offset.getQuantity()), |
| 568 | CGM.Int8PtrTy)); |
Peter Collingbourne | e53683f | 2016-09-08 01:14:39 +0000 | [diff] [blame] | 569 | }; |
Anders Carlsson | a5736bd | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 570 | |
John McCall | 9c6cb76 | 2016-11-28 22:18:33 +0000 | [diff] [blame] | 571 | switch (component.getKind()) { |
Peter Collingbourne | e53683f | 2016-09-08 01:14:39 +0000 | [diff] [blame] | 572 | case VTableComponent::CK_VCallOffset: |
John McCall | 9c6cb76 | 2016-11-28 22:18:33 +0000 | [diff] [blame] | 573 | return addOffsetConstant(component.getVCallOffset()); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 574 | |
Peter Collingbourne | e53683f | 2016-09-08 01:14:39 +0000 | [diff] [blame] | 575 | case VTableComponent::CK_VBaseOffset: |
John McCall | 9c6cb76 | 2016-11-28 22:18:33 +0000 | [diff] [blame] | 576 | return addOffsetConstant(component.getVBaseOffset()); |
Anders Carlsson | cb6207f | 2010-03-29 05:40:50 +0000 | [diff] [blame] | 577 | |
Peter Collingbourne | e53683f | 2016-09-08 01:14:39 +0000 | [diff] [blame] | 578 | case VTableComponent::CK_OffsetToTop: |
John McCall | 9c6cb76 | 2016-11-28 22:18:33 +0000 | [diff] [blame] | 579 | return addOffsetConstant(component.getOffsetToTop()); |
Anders Carlsson | a5736bd | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 580 | |
Peter Collingbourne | e53683f | 2016-09-08 01:14:39 +0000 | [diff] [blame] | 581 | case VTableComponent::CK_RTTI: |
John McCall | 9c6cb76 | 2016-11-28 22:18:33 +0000 | [diff] [blame] | 582 | return builder.add(llvm::ConstantExpr::getBitCast(rtti, CGM.Int8PtrTy)); |
Anders Carlsson | a5736bd | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 583 | |
Peter Collingbourne | e53683f | 2016-09-08 01:14:39 +0000 | [diff] [blame] | 584 | case VTableComponent::CK_FunctionPointer: |
| 585 | case VTableComponent::CK_CompleteDtorPointer: |
| 586 | case VTableComponent::CK_DeletingDtorPointer: { |
| 587 | GlobalDecl GD; |
| 588 | |
| 589 | // Get the right global decl. |
John McCall | 9c6cb76 | 2016-11-28 22:18:33 +0000 | [diff] [blame] | 590 | switch (component.getKind()) { |
Peter Collingbourne | e53683f | 2016-09-08 01:14:39 +0000 | [diff] [blame] | 591 | default: |
| 592 | llvm_unreachable("Unexpected vtable component kind"); |
Anders Carlsson | be1b9cb | 2010-04-10 19:13:06 +0000 | [diff] [blame] | 593 | case VTableComponent::CK_FunctionPointer: |
John McCall | 9c6cb76 | 2016-11-28 22:18:33 +0000 | [diff] [blame] | 594 | GD = component.getFunctionDecl(); |
Peter Collingbourne | e53683f | 2016-09-08 01:14:39 +0000 | [diff] [blame] | 595 | break; |
Anders Carlsson | be1b9cb | 2010-04-10 19:13:06 +0000 | [diff] [blame] | 596 | case VTableComponent::CK_CompleteDtorPointer: |
John McCall | 9c6cb76 | 2016-11-28 22:18:33 +0000 | [diff] [blame] | 597 | GD = GlobalDecl(component.getDestructorDecl(), Dtor_Complete); |
Peter Collingbourne | e53683f | 2016-09-08 01:14:39 +0000 | [diff] [blame] | 598 | break; |
| 599 | case VTableComponent::CK_DeletingDtorPointer: |
John McCall | 9c6cb76 | 2016-11-28 22:18:33 +0000 | [diff] [blame] | 600 | GD = GlobalDecl(component.getDestructorDecl(), Dtor_Deleting); |
Anders Carlsson | a5736bd | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 601 | break; |
| 602 | } |
| 603 | |
Peter Collingbourne | e53683f | 2016-09-08 01:14:39 +0000 | [diff] [blame] | 604 | if (CGM.getLangOpts().CUDA) { |
| 605 | // Emit NULL for methods we can't codegen on this |
| 606 | // side. Otherwise we'd end up with vtable with unresolved |
| 607 | // references. |
| 608 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 609 | // OK on device side: functions w/ __device__ attribute |
| 610 | // OK on host side: anything except __device__-only functions. |
| 611 | bool CanEmitMethod = |
| 612 | CGM.getLangOpts().CUDAIsDevice |
| 613 | ? MD->hasAttr<CUDADeviceAttr>() |
| 614 | : (MD->hasAttr<CUDAHostAttr>() || !MD->hasAttr<CUDADeviceAttr>()); |
| 615 | if (!CanEmitMethod) |
John McCall | 9c6cb76 | 2016-11-28 22:18:33 +0000 | [diff] [blame] | 616 | return builder.addNullPointer(CGM.Int8PtrTy); |
Peter Collingbourne | e53683f | 2016-09-08 01:14:39 +0000 | [diff] [blame] | 617 | // Method is acceptable, continue processing as usual. |
| 618 | } |
| 619 | |
John McCall | 9c6cb76 | 2016-11-28 22:18:33 +0000 | [diff] [blame] | 620 | auto getSpecialVirtualFn = [&](StringRef name) { |
| 621 | llvm::FunctionType *fnTy = |
| 622 | llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false); |
| 623 | llvm::Constant *fn = CGM.CreateRuntimeFunction(fnTy, name); |
| 624 | if (auto f = dyn_cast<llvm::Function>(fn)) |
| 625 | f->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
| 626 | return llvm::ConstantExpr::getBitCast(fn, CGM.Int8PtrTy); |
Anders Carlsson | a5736bd | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 627 | }; |
Peter Collingbourne | e53683f | 2016-09-08 01:14:39 +0000 | [diff] [blame] | 628 | |
John McCall | 9c6cb76 | 2016-11-28 22:18:33 +0000 | [diff] [blame] | 629 | llvm::Constant *fnPtr; |
Peter Collingbourne | e53683f | 2016-09-08 01:14:39 +0000 | [diff] [blame] | 630 | |
John McCall | 9c6cb76 | 2016-11-28 22:18:33 +0000 | [diff] [blame] | 631 | // Pure virtual member functions. |
| 632 | if (cast<CXXMethodDecl>(GD.getDecl())->isPure()) { |
| 633 | if (!PureVirtualFn) |
| 634 | PureVirtualFn = |
| 635 | getSpecialVirtualFn(CGM.getCXXABI().GetPureVirtualCallName()); |
| 636 | fnPtr = PureVirtualFn; |
Peter Collingbourne | e53683f | 2016-09-08 01:14:39 +0000 | [diff] [blame] | 637 | |
John McCall | 9c6cb76 | 2016-11-28 22:18:33 +0000 | [diff] [blame] | 638 | // Deleted virtual member functions. |
| 639 | } else if (cast<CXXMethodDecl>(GD.getDecl())->isDeleted()) { |
| 640 | if (!DeletedVirtualFn) |
| 641 | DeletedVirtualFn = |
| 642 | getSpecialVirtualFn(CGM.getCXXABI().GetDeletedVirtualCallName()); |
| 643 | fnPtr = DeletedVirtualFn; |
Peter Collingbourne | e53683f | 2016-09-08 01:14:39 +0000 | [diff] [blame] | 644 | |
John McCall | 9c6cb76 | 2016-11-28 22:18:33 +0000 | [diff] [blame] | 645 | // Thunks. |
| 646 | } else if (nextVTableThunkIndex < layout.vtable_thunks().size() && |
| 647 | layout.vtable_thunks()[nextVTableThunkIndex].first == idx) { |
| 648 | auto &thunkInfo = layout.vtable_thunks()[nextVTableThunkIndex].second; |
| 649 | |
| 650 | maybeEmitThunkForVTable(GD, thunkInfo); |
| 651 | nextVTableThunkIndex++; |
| 652 | fnPtr = CGM.GetAddrOfThunk(GD, thunkInfo); |
| 653 | |
| 654 | // Otherwise we can use the method definition directly. |
| 655 | } else { |
| 656 | llvm::Type *fnTy = CGM.getTypes().GetFunctionTypeForVTable(GD); |
| 657 | fnPtr = CGM.GetAddrOfFunction(GD, fnTy, /*ForVTable=*/true); |
Peter Collingbourne | e53683f | 2016-09-08 01:14:39 +0000 | [diff] [blame] | 658 | } |
| 659 | |
John McCall | 9c6cb76 | 2016-11-28 22:18:33 +0000 | [diff] [blame] | 660 | fnPtr = llvm::ConstantExpr::getBitCast(fnPtr, CGM.Int8PtrTy); |
| 661 | builder.add(fnPtr); |
| 662 | return; |
Anders Carlsson | a414714 | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 663 | } |
Peter Collingbourne | e53683f | 2016-09-08 01:14:39 +0000 | [diff] [blame] | 664 | |
| 665 | case VTableComponent::CK_UnusedFunctionPointer: |
John McCall | 9c6cb76 | 2016-11-28 22:18:33 +0000 | [diff] [blame] | 666 | return builder.addNullPointer(CGM.Int8PtrTy); |
Peter Collingbourne | e53683f | 2016-09-08 01:14:39 +0000 | [diff] [blame] | 667 | } |
Simon Pilgrim | 4acc49e | 2016-09-08 11:03:41 +0000 | [diff] [blame] | 668 | |
| 669 | llvm_unreachable("Unexpected vtable component kind"); |
Peter Collingbourne | e53683f | 2016-09-08 01:14:39 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Peter Collingbourne | 2849c4e | 2016-12-13 20:40:39 +0000 | [diff] [blame] | 672 | llvm::Type *CodeGenVTables::getVTableType(const VTableLayout &layout) { |
| 673 | SmallVector<llvm::Type *, 4> tys; |
| 674 | for (unsigned i = 0, e = layout.getNumVTables(); i != e; ++i) { |
| 675 | tys.push_back(llvm::ArrayType::get(CGM.Int8PtrTy, layout.getVTableSize(i))); |
| 676 | } |
| 677 | |
| 678 | return llvm::StructType::get(CGM.getLLVMContext(), tys); |
| 679 | } |
| 680 | |
| 681 | void CodeGenVTables::createVTableInitializer(ConstantStructBuilder &builder, |
John McCall | 9c6cb76 | 2016-11-28 22:18:33 +0000 | [diff] [blame] | 682 | const VTableLayout &layout, |
| 683 | llvm::Constant *rtti) { |
| 684 | unsigned nextVTableThunkIndex = 0; |
Peter Collingbourne | 2849c4e | 2016-12-13 20:40:39 +0000 | [diff] [blame] | 685 | for (unsigned i = 0, e = layout.getNumVTables(); i != e; ++i) { |
| 686 | auto vtableElem = builder.beginArray(CGM.Int8PtrTy); |
| 687 | size_t thisIndex = layout.getVTableOffset(i); |
| 688 | size_t nextIndex = thisIndex + layout.getVTableSize(i); |
| 689 | for (unsigned i = thisIndex; i != nextIndex; ++i) { |
| 690 | addVTableComponent(vtableElem, layout, i, rtti, nextVTableThunkIndex); |
| 691 | } |
| 692 | vtableElem.finishAndAddTo(builder); |
Peter Collingbourne | e53683f | 2016-09-08 01:14:39 +0000 | [diff] [blame] | 693 | } |
Anders Carlsson | a414714 | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Anders Carlsson | 0534b02 | 2010-03-25 00:35:49 +0000 | [diff] [blame] | 696 | llvm::GlobalVariable * |
Simon Pilgrim | 48c32b1 | 2016-09-08 09:59:58 +0000 | [diff] [blame] | 697 | CodeGenVTables::GenerateConstructionVTable(const CXXRecordDecl *RD, |
| 698 | const BaseSubobject &Base, |
| 699 | bool BaseIsVirtual, |
John McCall | 358d056 | 2011-03-27 09:00:25 +0000 | [diff] [blame] | 700 | llvm::GlobalVariable::LinkageTypes Linkage, |
Anders Carlsson | a208b39 | 2010-03-26 03:56:54 +0000 | [diff] [blame] | 701 | VTableAddressPointsMapTy& AddressPoints) { |
David Blaikie | d89b99d | 2013-08-22 15:23:05 +0000 | [diff] [blame] | 702 | if (CGDebugInfo *DI = CGM.getModuleDebugInfo()) |
| 703 | DI->completeClassData(Base.getBase()); |
| 704 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 705 | std::unique_ptr<VTableLayout> VTLayout( |
Reid Kleckner | b60a3d5 | 2013-12-20 23:58:52 +0000 | [diff] [blame] | 706 | getItaniumVTableContext().createConstructionVTableLayout( |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 707 | Base.getBase(), Base.getBaseOffset(), BaseIsVirtual, RD)); |
Anders Carlsson | a414714 | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 708 | |
Anders Carlsson | a5736bd | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 709 | // Add the address points. |
Peter Collingbourne | 1c593c6 | 2011-09-26 01:57:04 +0000 | [diff] [blame] | 710 | AddressPoints = VTLayout->getAddressPoints(); |
Anders Carlsson | a414714 | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 711 | |
| 712 | // Get the mangled construction vtable name. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 713 | SmallString<256> OutName; |
Rafael Espindola | 3968cd0 | 2011-02-11 02:52:17 +0000 | [diff] [blame] | 714 | llvm::raw_svector_ostream Out(OutName); |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 715 | cast<ItaniumMangleContext>(CGM.getCXXABI().getMangleContext()) |
| 716 | .mangleCXXCtorVTable(RD, Base.getBaseOffset().getQuantity(), |
| 717 | Base.getBase(), Out); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 718 | StringRef Name = OutName.str(); |
Anders Carlsson | a414714 | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 719 | |
Peter Collingbourne | 2849c4e | 2016-12-13 20:40:39 +0000 | [diff] [blame] | 720 | llvm::Type *VTType = getVTableType(*VTLayout); |
Anders Carlsson | a414714 | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 721 | |
Richard Smith | 65fd2a4 | 2013-02-16 00:51:21 +0000 | [diff] [blame] | 722 | // Construction vtable symbols are not part of the Itanium ABI, so we cannot |
| 723 | // guarantee that they actually will be available externally. Instead, when |
| 724 | // emitting an available_externally VTT, we provide references to an internal |
| 725 | // linkage construction vtable. The ABI only requires complete-object vtables |
| 726 | // to be the same for all instances of a type, not construction vtables. |
| 727 | if (Linkage == llvm::GlobalVariable::AvailableExternallyLinkage) |
| 728 | Linkage = llvm::GlobalVariable::InternalLinkage; |
| 729 | |
Anders Carlsson | a414714 | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 730 | // Create the variable that will hold the construction vtable. |
Simon Pilgrim | 48c32b1 | 2016-09-08 09:59:58 +0000 | [diff] [blame] | 731 | llvm::GlobalVariable *VTable = |
Peter Collingbourne | 2849c4e | 2016-12-13 20:40:39 +0000 | [diff] [blame] | 732 | CGM.CreateOrReplaceCXXRuntimeVariable(Name, VTType, Linkage); |
Rafael Espindola | 880c3b2 | 2018-02-07 19:44:15 +0000 | [diff] [blame] | 733 | CGM.setGlobalVisibility(VTable, RD); |
John McCall | 358d056 | 2011-03-27 09:00:25 +0000 | [diff] [blame] | 734 | |
| 735 | // V-tables are always unnamed_addr. |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 736 | VTable->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Anders Carlsson | a414714 | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 737 | |
David Majnemer | d905da4 | 2014-07-01 20:30:31 +0000 | [diff] [blame] | 738 | llvm::Constant *RTTI = CGM.GetAddrOfRTTIDescriptor( |
| 739 | CGM.getContext().getTagDeclType(Base.getBase())); |
| 740 | |
Anders Carlsson | a414714 | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 741 | // Create and set the initializer. |
John McCall | 9c6cb76 | 2016-11-28 22:18:33 +0000 | [diff] [blame] | 742 | ConstantInitBuilder builder(CGM); |
Peter Collingbourne | 2849c4e | 2016-12-13 20:40:39 +0000 | [diff] [blame] | 743 | auto components = builder.beginStruct(); |
John McCall | 9c6cb76 | 2016-11-28 22:18:33 +0000 | [diff] [blame] | 744 | createVTableInitializer(components, *VTLayout, RTTI); |
| 745 | components.finishAndSetAsInitializer(VTable); |
Simon Pilgrim | 48c32b1 | 2016-09-08 09:59:58 +0000 | [diff] [blame] | 746 | |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 747 | CGM.EmitVTableTypeMetadata(VTable, *VTLayout.get()); |
Peter Collingbourne | a4ccff3 | 2015-02-20 20:30:56 +0000 | [diff] [blame] | 748 | |
Anders Carlsson | 0534b02 | 2010-03-25 00:35:49 +0000 | [diff] [blame] | 749 | return VTable; |
| 750 | } |
| 751 | |
Piotr Padlewski | a68a787 | 2015-07-24 04:04:49 +0000 | [diff] [blame] | 752 | static bool shouldEmitAvailableExternallyVTable(const CodeGenModule &CGM, |
| 753 | const CXXRecordDecl *RD) { |
| 754 | return CGM.getCodeGenOpts().OptimizationLevel > 0 && |
Piotr Padlewski | d679d7e | 2015-09-15 00:37:06 +0000 | [diff] [blame] | 755 | CGM.getCXXABI().canSpeculativelyEmitVTable(RD); |
Piotr Padlewski | a68a787 | 2015-07-24 04:04:49 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Eric Christopher | d160c50 | 2016-01-29 01:35:53 +0000 | [diff] [blame] | 758 | /// Compute the required linkage of the vtable for the given class. |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 759 | /// |
| 760 | /// Note that we only call this at the end of the translation unit. |
Simon Pilgrim | 48c32b1 | 2016-09-08 09:59:58 +0000 | [diff] [blame] | 761 | llvm::GlobalVariable::LinkageTypes |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 762 | CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) { |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 763 | if (!RD->isExternallyVisible()) |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 764 | return llvm::GlobalVariable::InternalLinkage; |
| 765 | |
| 766 | // We're at the end of the translation unit, so the current key |
| 767 | // function is fully correct. |
Hans Wennborg | ec53c29 | 2014-10-23 22:40:46 +0000 | [diff] [blame] | 768 | const CXXMethodDecl *keyFunction = Context.getCurrentKeyFunction(RD); |
| 769 | if (keyFunction && !RD->hasAttr<DLLImportAttr>()) { |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 770 | // If this class has a key function, use that to determine the |
| 771 | // linkage of the vtable. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 772 | const FunctionDecl *def = nullptr; |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 773 | if (keyFunction->hasBody(def)) |
| 774 | keyFunction = cast<CXXMethodDecl>(def); |
Simon Pilgrim | 48c32b1 | 2016-09-08 09:59:58 +0000 | [diff] [blame] | 775 | |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 776 | switch (keyFunction->getTemplateSpecializationKind()) { |
| 777 | case TSK_Undeclared: |
| 778 | case TSK_ExplicitSpecialization: |
David Blaikie | b11c873 | 2017-01-30 06:36:08 +0000 | [diff] [blame] | 779 | assert((def || CodeGenOpts.OptimizationLevel > 0 || |
| 780 | CodeGenOpts.getDebugInfo() != codegenoptions::NoDebugInfo) && |
| 781 | "Shouldn't query vtable linkage without key function, " |
| 782 | "optimizations, or debug info"); |
Piotr Padlewski | a68a787 | 2015-07-24 04:04:49 +0000 | [diff] [blame] | 783 | if (!def && CodeGenOpts.OptimizationLevel > 0) |
| 784 | return llvm::GlobalVariable::AvailableExternallyLinkage; |
| 785 | |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 786 | if (keyFunction->isInlined()) |
| 787 | return !Context.getLangOpts().AppleKext ? |
| 788 | llvm::GlobalVariable::LinkOnceODRLinkage : |
| 789 | llvm::Function::InternalLinkage; |
Simon Pilgrim | 48c32b1 | 2016-09-08 09:59:58 +0000 | [diff] [blame] | 790 | |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 791 | return llvm::GlobalVariable::ExternalLinkage; |
Yaron Keren | 07d4496a | 2015-07-02 14:44:35 +0000 | [diff] [blame] | 792 | |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 793 | case TSK_ImplicitInstantiation: |
| 794 | return !Context.getLangOpts().AppleKext ? |
| 795 | llvm::GlobalVariable::LinkOnceODRLinkage : |
| 796 | llvm::Function::InternalLinkage; |
| 797 | |
| 798 | case TSK_ExplicitInstantiationDefinition: |
| 799 | return !Context.getLangOpts().AppleKext ? |
| 800 | llvm::GlobalVariable::WeakODRLinkage : |
| 801 | llvm::Function::InternalLinkage; |
Simon Pilgrim | 48c32b1 | 2016-09-08 09:59:58 +0000 | [diff] [blame] | 802 | |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 803 | case TSK_ExplicitInstantiationDeclaration: |
Rafael Espindola | ee6aa0c | 2013-09-03 21:05:13 +0000 | [diff] [blame] | 804 | llvm_unreachable("Should not have been asked to emit this"); |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 805 | } |
| 806 | } |
| 807 | |
| 808 | // -fapple-kext mode does not support weak linkage, so we must use |
| 809 | // internal linkage. |
| 810 | if (Context.getLangOpts().AppleKext) |
| 811 | return llvm::Function::InternalLinkage; |
Hans Wennborg | 853ae94 | 2014-05-30 16:59:42 +0000 | [diff] [blame] | 812 | |
| 813 | llvm::GlobalVariable::LinkageTypes DiscardableODRLinkage = |
| 814 | llvm::GlobalValue::LinkOnceODRLinkage; |
| 815 | llvm::GlobalVariable::LinkageTypes NonDiscardableODRLinkage = |
| 816 | llvm::GlobalValue::WeakODRLinkage; |
| 817 | if (RD->hasAttr<DLLExportAttr>()) { |
| 818 | // Cannot discard exported vtables. |
| 819 | DiscardableODRLinkage = NonDiscardableODRLinkage; |
| 820 | } else if (RD->hasAttr<DLLImportAttr>()) { |
| 821 | // Imported vtables are available externally. |
| 822 | DiscardableODRLinkage = llvm::GlobalVariable::AvailableExternallyLinkage; |
| 823 | NonDiscardableODRLinkage = llvm::GlobalVariable::AvailableExternallyLinkage; |
| 824 | } |
| 825 | |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 826 | switch (RD->getTemplateSpecializationKind()) { |
Piotr Padlewski | a68a787 | 2015-07-24 04:04:49 +0000 | [diff] [blame] | 827 | case TSK_Undeclared: |
| 828 | case TSK_ExplicitSpecialization: |
| 829 | case TSK_ImplicitInstantiation: |
| 830 | return DiscardableODRLinkage; |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 831 | |
Piotr Padlewski | a68a787 | 2015-07-24 04:04:49 +0000 | [diff] [blame] | 832 | case TSK_ExplicitInstantiationDeclaration: |
Reid Kleckner | ad1e22b | 2016-06-29 18:29:21 +0000 | [diff] [blame] | 833 | // Explicit instantiations in MSVC do not provide vtables, so we must emit |
| 834 | // our own. |
| 835 | if (getTarget().getCXXABI().isMicrosoft()) |
| 836 | return DiscardableODRLinkage; |
Piotr Padlewski | a68a787 | 2015-07-24 04:04:49 +0000 | [diff] [blame] | 837 | return shouldEmitAvailableExternallyVTable(*this, RD) |
| 838 | ? llvm::GlobalVariable::AvailableExternallyLinkage |
| 839 | : llvm::GlobalVariable::ExternalLinkage; |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 840 | |
Piotr Padlewski | a68a787 | 2015-07-24 04:04:49 +0000 | [diff] [blame] | 841 | case TSK_ExplicitInstantiationDefinition: |
| 842 | return NonDiscardableODRLinkage; |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | llvm_unreachable("Invalid TemplateSpecializationKind!"); |
| 846 | } |
| 847 | |
Eric Christopher | d160c50 | 2016-01-29 01:35:53 +0000 | [diff] [blame] | 848 | /// This is a callback from Sema to tell us that that a particular vtable is |
Nico Weber | b6a5d05 | 2015-01-15 04:07:35 +0000 | [diff] [blame] | 849 | /// required to be emitted in this translation unit. |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 850 | /// |
Nico Weber | b6a5d05 | 2015-01-15 04:07:35 +0000 | [diff] [blame] | 851 | /// This is only called for vtables that _must_ be emitted (mainly due to key |
| 852 | /// functions). For weak vtables, CodeGen tracks when they are needed and |
| 853 | /// emits them as-needed. |
| 854 | void CodeGenModule::EmitVTable(CXXRecordDecl *theClass) { |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 855 | VTables.GenerateClassData(theClass); |
| 856 | } |
| 857 | |
Simon Pilgrim | 48c32b1 | 2016-09-08 09:59:58 +0000 | [diff] [blame] | 858 | void |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 859 | CodeGenVTables::GenerateClassData(const CXXRecordDecl *RD) { |
David Blaikie | d89b99d | 2013-08-22 15:23:05 +0000 | [diff] [blame] | 860 | if (CGDebugInfo *DI = CGM.getModuleDebugInfo()) |
| 861 | DI->completeClassData(RD); |
| 862 | |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 863 | if (RD->getNumVBases()) |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 864 | CGM.getCXXABI().emitVirtualInheritanceTables(RD); |
Douglas Gregor | eadd3ca | 2010-04-08 15:52:03 +0000 | [diff] [blame] | 865 | |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 866 | CGM.getCXXABI().emitVTableDefinitions(*this, RD); |
Anders Carlsson | a627ac7e | 2010-03-29 03:38:52 +0000 | [diff] [blame] | 867 | } |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 868 | |
| 869 | /// At this point in the translation unit, does it appear that can we |
| 870 | /// rely on the vtable being defined elsewhere in the program? |
| 871 | /// |
| 872 | /// The response is really only definitive when called at the end of |
| 873 | /// the translation unit. |
| 874 | /// |
| 875 | /// The only semantic restriction here is that the object file should |
Eric Christopher | d160c50 | 2016-01-29 01:35:53 +0000 | [diff] [blame] | 876 | /// not contain a vtable definition when that vtable is defined |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 877 | /// strongly elsewhere. Otherwise, we'd just like to avoid emitting |
Eric Christopher | d160c50 | 2016-01-29 01:35:53 +0000 | [diff] [blame] | 878 | /// vtables when unnecessary. |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 879 | bool CodeGenVTables::isVTableExternal(const CXXRecordDecl *RD) { |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 880 | assert(RD->isDynamicClass() && "Non-dynamic classes have no VTable."); |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 881 | |
Reid Kleckner | ad1e22b | 2016-06-29 18:29:21 +0000 | [diff] [blame] | 882 | // We always synthesize vtables if they are needed in the MS ABI. MSVC doesn't |
| 883 | // emit them even if there is an explicit template instantiation. |
| 884 | if (CGM.getTarget().getCXXABI().isMicrosoft()) |
David Majnemer | 2d8b200 | 2016-02-11 17:49:28 +0000 | [diff] [blame] | 885 | return false; |
| 886 | |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 887 | // If we have an explicit instantiation declaration (and not a |
Eric Christopher | d160c50 | 2016-01-29 01:35:53 +0000 | [diff] [blame] | 888 | // definition), the vtable is defined elsewhere. |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 889 | TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind(); |
| 890 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
| 891 | return true; |
| 892 | |
| 893 | // Otherwise, if the class is an instantiated template, the |
Eric Christopher | d160c50 | 2016-01-29 01:35:53 +0000 | [diff] [blame] | 894 | // vtable must be defined here. |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 895 | if (TSK == TSK_ImplicitInstantiation || |
| 896 | TSK == TSK_ExplicitInstantiationDefinition) |
| 897 | return false; |
| 898 | |
| 899 | // Otherwise, if the class doesn't have a key function (possibly |
Eric Christopher | d160c50 | 2016-01-29 01:35:53 +0000 | [diff] [blame] | 900 | // anymore), the vtable must be defined here. |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 901 | const CXXMethodDecl *keyFunction = CGM.getContext().getCurrentKeyFunction(RD); |
| 902 | if (!keyFunction) |
| 903 | return false; |
| 904 | |
| 905 | // Otherwise, if we don't have a definition of the key function, the |
Eric Christopher | d160c50 | 2016-01-29 01:35:53 +0000 | [diff] [blame] | 906 | // vtable must be defined somewhere else. |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 907 | return !keyFunction->hasBody(); |
| 908 | } |
| 909 | |
| 910 | /// Given that we're currently at the end of the translation unit, and |
Eric Christopher | d160c50 | 2016-01-29 01:35:53 +0000 | [diff] [blame] | 911 | /// we've emitted a reference to the vtable for this class, should |
| 912 | /// we define that vtable? |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 913 | static bool shouldEmitVTableAtEndOfTranslationUnit(CodeGenModule &CGM, |
| 914 | const CXXRecordDecl *RD) { |
Piotr Padlewski | d679d7e | 2015-09-15 00:37:06 +0000 | [diff] [blame] | 915 | // If vtable is internal then it has to be done. |
Piotr Padlewski | a68a787 | 2015-07-24 04:04:49 +0000 | [diff] [blame] | 916 | if (!CGM.getVTables().isVTableExternal(RD)) |
| 917 | return true; |
| 918 | |
Piotr Padlewski | d679d7e | 2015-09-15 00:37:06 +0000 | [diff] [blame] | 919 | // If it's external then maybe we will need it as available_externally. |
Piotr Padlewski | a68a787 | 2015-07-24 04:04:49 +0000 | [diff] [blame] | 920 | return shouldEmitAvailableExternallyVTable(CGM, RD); |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | /// Given that at some point we emitted a reference to one or more |
Eric Christopher | d160c50 | 2016-01-29 01:35:53 +0000 | [diff] [blame] | 924 | /// vtables, and that we are now at the end of the translation unit, |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 925 | /// decide whether we should emit them. |
| 926 | void CodeGenModule::EmitDeferredVTables() { |
| 927 | #ifndef NDEBUG |
| 928 | // Remember the size of DeferredVTables, because we're going to assume |
| 929 | // that this entire operation doesn't modify it. |
| 930 | size_t savedSize = DeferredVTables.size(); |
| 931 | #endif |
| 932 | |
Piotr Padlewski | 44b4ce8 | 2015-07-28 16:10:58 +0000 | [diff] [blame] | 933 | for (const CXXRecordDecl *RD : DeferredVTables) |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 934 | if (shouldEmitVTableAtEndOfTranslationUnit(*this, RD)) |
| 935 | VTables.GenerateClassData(RD); |
Piotr Padlewski | d3b1cbd | 2017-06-01 08:04:05 +0000 | [diff] [blame] | 936 | else if (shouldOpportunisticallyEmitVTables()) |
| 937 | OpportunisticVTables.push_back(RD); |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 938 | |
| 939 | assert(savedSize == DeferredVTables.size() && |
Eric Christopher | d160c50 | 2016-01-29 01:35:53 +0000 | [diff] [blame] | 940 | "deferred extra vtables during vtable emission?"); |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 941 | DeferredVTables.clear(); |
| 942 | } |
Peter Collingbourne | a4ccff3 | 2015-02-20 20:30:56 +0000 | [diff] [blame] | 943 | |
Peter Collingbourne | 3afb266 | 2016-04-28 17:09:37 +0000 | [diff] [blame] | 944 | bool CodeGenModule::HasHiddenLTOVisibility(const CXXRecordDecl *RD) { |
| 945 | LinkageInfo LV = RD->getLinkageAndVisibility(); |
| 946 | if (!isExternallyVisible(LV.getLinkage())) |
| 947 | return true; |
Peter Collingbourne | 6fccf95 | 2015-07-15 12:15:56 +0000 | [diff] [blame] | 948 | |
Peter Collingbourne | 3afb266 | 2016-04-28 17:09:37 +0000 | [diff] [blame] | 949 | if (RD->hasAttr<LTOVisibilityPublicAttr>() || RD->hasAttr<UuidAttr>()) |
| 950 | return false; |
Peter Collingbourne | fb532b9 | 2016-02-24 20:46:36 +0000 | [diff] [blame] | 951 | |
Peter Collingbourne | 3afb266 | 2016-04-28 17:09:37 +0000 | [diff] [blame] | 952 | if (getTriple().isOSBinFormatCOFF()) { |
| 953 | if (RD->hasAttr<DLLExportAttr>() || RD->hasAttr<DLLImportAttr>()) |
| 954 | return false; |
| 955 | } else { |
| 956 | if (LV.getVisibility() != HiddenVisibility) |
| 957 | return false; |
| 958 | } |
Peter Collingbourne | fb532b9 | 2016-02-24 20:46:36 +0000 | [diff] [blame] | 959 | |
Peter Collingbourne | 3afb266 | 2016-04-28 17:09:37 +0000 | [diff] [blame] | 960 | if (getCodeGenOpts().LTOVisibilityPublicStd) { |
| 961 | const DeclContext *DC = RD; |
| 962 | while (1) { |
| 963 | auto *D = cast<Decl>(DC); |
| 964 | DC = DC->getParent(); |
| 965 | if (isa<TranslationUnitDecl>(DC->getRedeclContext())) { |
| 966 | if (auto *ND = dyn_cast<NamespaceDecl>(D)) |
| 967 | if (const IdentifierInfo *II = ND->getIdentifier()) |
| 968 | if (II->isStr("std") || II->isStr("stdext")) |
| 969 | return false; |
| 970 | break; |
| 971 | } |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | return true; |
Peter Collingbourne | e570644 | 2015-07-09 19:56:14 +0000 | [diff] [blame] | 976 | } |
| 977 | |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 978 | void CodeGenModule::EmitVTableTypeMetadata(llvm::GlobalVariable *VTable, |
| 979 | const VTableLayout &VTLayout) { |
Peter Collingbourne | 1e1475a | 2017-01-18 23:55:27 +0000 | [diff] [blame] | 980 | if (!getCodeGenOpts().LTOUnit) |
Peter Collingbourne | a4ccff3 | 2015-02-20 20:30:56 +0000 | [diff] [blame] | 981 | return; |
| 982 | |
Peter Collingbourne | 86d34a7 | 2015-06-17 19:08:05 +0000 | [diff] [blame] | 983 | CharUnits PointerWidth = |
| 984 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0)); |
Peter Collingbourne | a4ccff3 | 2015-02-20 20:30:56 +0000 | [diff] [blame] | 985 | |
Peter Collingbourne | 2c7f7e3 | 2015-09-10 02:17:40 +0000 | [diff] [blame] | 986 | typedef std::pair<const CXXRecordDecl *, unsigned> BSEntry; |
| 987 | std::vector<BSEntry> BitsetEntries; |
Peter Collingbourne | a4ccff3 | 2015-02-20 20:30:56 +0000 | [diff] [blame] | 988 | // Create a bit set entry for each address point. |
Peter Collingbourne | 3afb266 | 2016-04-28 17:09:37 +0000 | [diff] [blame] | 989 | for (auto &&AP : VTLayout.getAddressPoints()) |
Peter Collingbourne | 2849c4e | 2016-12-13 20:40:39 +0000 | [diff] [blame] | 990 | BitsetEntries.push_back( |
| 991 | std::make_pair(AP.first.getBase(), |
| 992 | VTLayout.getVTableOffset(AP.second.VTableIndex) + |
| 993 | AP.second.AddressPointIndex)); |
Peter Collingbourne | a4ccff3 | 2015-02-20 20:30:56 +0000 | [diff] [blame] | 994 | |
| 995 | // Sort the bit set entries for determinism. |
Peter Collingbourne | 2c7f7e3 | 2015-09-10 02:17:40 +0000 | [diff] [blame] | 996 | std::sort(BitsetEntries.begin(), BitsetEntries.end(), |
| 997 | [this](const BSEntry &E1, const BSEntry &E2) { |
| 998 | if (&E1 == &E2) |
Peter Collingbourne | 4794190 | 2015-02-24 01:12:53 +0000 | [diff] [blame] | 999 | return false; |
| 1000 | |
Peter Collingbourne | 2c7f7e3 | 2015-09-10 02:17:40 +0000 | [diff] [blame] | 1001 | std::string S1; |
| 1002 | llvm::raw_string_ostream O1(S1); |
| 1003 | getCXXABI().getMangleContext().mangleTypeName( |
| 1004 | QualType(E1.first->getTypeForDecl(), 0), O1); |
| 1005 | O1.flush(); |
| 1006 | |
| 1007 | std::string S2; |
| 1008 | llvm::raw_string_ostream O2(S2); |
| 1009 | getCXXABI().getMangleContext().mangleTypeName( |
| 1010 | QualType(E2.first->getTypeForDecl(), 0), O2); |
| 1011 | O2.flush(); |
| 1012 | |
Peter Collingbourne | a4ccff3 | 2015-02-20 20:30:56 +0000 | [diff] [blame] | 1013 | if (S1 < S2) |
| 1014 | return true; |
| 1015 | if (S1 != S2) |
| 1016 | return false; |
| 1017 | |
Peter Collingbourne | 2c7f7e3 | 2015-09-10 02:17:40 +0000 | [diff] [blame] | 1018 | return E1.second < E2.second; |
Peter Collingbourne | a4ccff3 | 2015-02-20 20:30:56 +0000 | [diff] [blame] | 1019 | }); |
| 1020 | |
Peter Collingbourne | a4ccff3 | 2015-02-20 20:30:56 +0000 | [diff] [blame] | 1021 | for (auto BitsetEntry : BitsetEntries) |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 1022 | AddVTableTypeMetadata(VTable, PointerWidth * BitsetEntry.second, |
| 1023 | BitsetEntry.first); |
Peter Collingbourne | a4ccff3 | 2015-02-20 20:30:56 +0000 | [diff] [blame] | 1024 | } |