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