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