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