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