blob: 659ed0a30505107443368cf03efb780dcf0fcfce [file] [log] [blame]
Charles Davisc3926642010-06-09 23:25:41 +00001//===--- MicrosoftCXXABI.cpp - Emit LLVM Code from ASTs for a Module ------===//
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//
Chris Lattnerfc8f0e12011-04-15 05:22:18 +000010// This provides C++ code generation targeting the Microsoft Visual C++ ABI.
Charles Davisc3926642010-06-09 23:25:41 +000011// The class in this file generates structures that follow the Microsoft
12// Visual C++ ABI, which is actually not very well documented at all outside
13// of Microsoft.
14//
15//===----------------------------------------------------------------------===//
16
17#include "CGCXXABI.h"
Reid Kleckner90633022013-06-19 15:20:38 +000018#include "CGVTables.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070019#include "CodeGenModule.h"
Charles Davisc3926642010-06-09 23:25:41 +000020#include "clang/AST/Decl.h"
21#include "clang/AST/DeclCXX.h"
Timur Iskhodzhanov635de282013-07-30 09:46:19 +000022#include "clang/AST/VTableBuilder.h"
Stephen Hinesc568f1e2014-07-21 00:47:37 -070023#include "llvm/ADT/StringExtras.h"
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +000024#include "llvm/ADT/StringSet.h"
Stephen Hinesc568f1e2014-07-21 00:47:37 -070025#include "llvm/IR/CallSite.h"
Charles Davisc3926642010-06-09 23:25:41 +000026
27using namespace clang;
28using namespace CodeGen;
29
30namespace {
31
Stephen Hines651f13c2014-04-23 16:59:28 -070032/// Holds all the vbtable globals for a given class.
33struct VBTableGlobals {
34 const VPtrInfoVector *VBTables;
35 SmallVector<llvm::GlobalVariable *, 2> Globals;
36};
37
Charles Davis071cc7d2010-08-16 03:33:14 +000038class MicrosoftCXXABI : public CGCXXABI {
Charles Davisc3926642010-06-09 23:25:41 +000039public:
Stephen Hinesc568f1e2014-07-21 00:47:37 -070040 MicrosoftCXXABI(CodeGenModule &CGM)
41 : CGCXXABI(CGM), BaseClassDescriptorType(nullptr),
42 ClassHierarchyDescriptorType(nullptr),
43 CompleteObjectLocatorType(nullptr) {}
John McCall4c40d982010-08-31 07:33:07 +000044
Stephen Hines651f13c2014-04-23 16:59:28 -070045 bool HasThisReturn(GlobalDecl GD) const override;
Stephen Hines176edba2014-12-01 14:53:08 -080046 bool hasMostDerivedReturn(GlobalDecl GD) const override;
Stephen Lin3b50e8d2013-06-30 20:40:16 +000047
Stephen Hines6bcf27b2014-05-29 04:14:42 -070048 bool classifyReturnType(CGFunctionInfo &FI) const override;
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +000049
Stephen Hines6bcf27b2014-05-29 04:14:42 -070050 RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const override;
51
52 bool isSRetParameterAfterThis() const override { return true; }
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +000053
Stephen Hines176edba2014-12-01 14:53:08 -080054 size_t getSrcArgforCopyCtor(const CXXConstructorDecl *CD,
55 FunctionArgList &Args) const override {
56 assert(Args.size() >= 2 &&
57 "expected the arglist to have at least two args!");
58 // The 'most_derived' parameter goes second if the ctor is variadic and
59 // has v-bases.
60 if (CD->getParent()->getNumVBases() > 0 &&
61 CD->getType()->castAs<FunctionProtoType>()->isVariadic())
62 return 2;
63 return 1;
64 }
65
Stephen Hines651f13c2014-04-23 16:59:28 -070066 StringRef GetPureVirtualCallName() override { return "_purecall"; }
Stephen Hines651f13c2014-04-23 16:59:28 -070067 StringRef GetDeletedVirtualCallName() override { return "_purecall"; }
Joao Matos285baac2012-07-17 17:10:11 +000068
Stephen Hines176edba2014-12-01 14:53:08 -080069 void emitVirtualObjectDelete(CodeGenFunction &CGF, const CXXDeleteExpr *DE,
70 llvm::Value *Ptr, QualType ElementType,
71 const CXXDestructorDecl *Dtor) override;
John McCallecd03b42012-09-25 10:10:39 +000072
Stephen Hinesc568f1e2014-07-21 00:47:37 -070073 llvm::GlobalVariable *getMSCompleteObjectLocator(const CXXRecordDecl *RD,
74 const VPtrInfo *Info);
75
76 llvm::Constant *getAddrOfRTTIDescriptor(QualType Ty) override;
77
78 bool shouldTypeidBeNullChecked(bool IsDeref, QualType SrcRecordTy) override;
79 void EmitBadTypeidCall(CodeGenFunction &CGF) override;
80 llvm::Value *EmitTypeid(CodeGenFunction &CGF, QualType SrcRecordTy,
81 llvm::Value *ThisPtr,
82 llvm::Type *StdTypeInfoPtrTy) override;
83
84 bool shouldDynamicCastCallBeNullChecked(bool SrcIsPtr,
85 QualType SrcRecordTy) override;
86
87 llvm::Value *EmitDynamicCastCall(CodeGenFunction &CGF, llvm::Value *Value,
88 QualType SrcRecordTy, QualType DestTy,
89 QualType DestRecordTy,
90 llvm::BasicBlock *CastEnd) override;
91
92 llvm::Value *EmitDynamicCastToVoid(CodeGenFunction &CGF, llvm::Value *Value,
93 QualType SrcRecordTy,
94 QualType DestTy) override;
95
96 bool EmitBadCastCall(CodeGenFunction &CGF) override;
97
Stephen Hines651f13c2014-04-23 16:59:28 -070098 llvm::Value *
99 GetVirtualBaseClassOffset(CodeGenFunction &CGF, llvm::Value *This,
100 const CXXRecordDecl *ClassDecl,
101 const CXXRecordDecl *BaseClassDecl) override;
Reid Klecknerb0f533e2013-05-29 18:02:47 +0000102
Stephen Hines651f13c2014-04-23 16:59:28 -0700103 llvm::BasicBlock *
104 EmitCtorCompleteObjectHandler(CodeGenFunction &CGF,
105 const CXXRecordDecl *RD) override;
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000106
Timur Iskhodzhanov5bd0d442013-10-09 18:16:58 +0000107 void initializeHiddenVirtualInheritanceMembers(CodeGenFunction &CGF,
Stephen Hines651f13c2014-04-23 16:59:28 -0700108 const CXXRecordDecl *RD) override;
Timur Iskhodzhanov5bd0d442013-10-09 18:16:58 +0000109
Stephen Hines651f13c2014-04-23 16:59:28 -0700110 void EmitCXXConstructors(const CXXConstructorDecl *D) override;
Timur Iskhodzhanovbb1b7972013-08-04 17:30:04 +0000111
Reid Klecknera4130ba2013-07-22 13:51:44 +0000112 // Background on MSVC destructors
113 // ==============================
114 //
115 // Both Itanium and MSVC ABIs have destructor variants. The variant names
116 // roughly correspond in the following way:
117 // Itanium Microsoft
118 // Base -> no name, just ~Class
119 // Complete -> vbase destructor
120 // Deleting -> scalar deleting destructor
121 // vector deleting destructor
122 //
123 // The base and complete destructors are the same as in Itanium, although the
124 // complete destructor does not accept a VTT parameter when there are virtual
125 // bases. A separate mechanism involving vtordisps is used to ensure that
126 // virtual methods of destroyed subobjects are not called.
127 //
128 // The deleting destructors accept an i32 bitfield as a second parameter. Bit
129 // 1 indicates if the memory should be deleted. Bit 2 indicates if the this
130 // pointer points to an array. The scalar deleting destructor assumes that
131 // bit 2 is zero, and therefore does not contain a loop.
132 //
133 // For virtual destructors, only one entry is reserved in the vftable, and it
134 // always points to the vector deleting destructor. The vector deleting
135 // destructor is the most general, so it can be used to destroy objects in
136 // place, delete single heap objects, or delete arrays.
137 //
138 // A TU defining a non-inline destructor is only guaranteed to emit a base
139 // destructor, and all of the other variants are emitted on an as-needed basis
140 // in COMDATs. Because a non-base destructor can be emitted in a TU that
141 // lacks a definition for the destructor, non-base destructors must always
142 // delegate to or alias the base destructor.
143
Stephen Hines176edba2014-12-01 14:53:08 -0800144 void buildStructorSignature(const CXXMethodDecl *MD, StructorType T,
145 SmallVectorImpl<CanQualType> &ArgTys) override;
John McCall4c40d982010-08-31 07:33:07 +0000146
Reid Klecknera4130ba2013-07-22 13:51:44 +0000147 /// Non-base dtors should be emitted as delegating thunks in this ABI.
148 bool useThunkForDtorVariant(const CXXDestructorDecl *Dtor,
Stephen Hines651f13c2014-04-23 16:59:28 -0700149 CXXDtorType DT) const override {
Reid Klecknera4130ba2013-07-22 13:51:44 +0000150 return DT != Dtor_Base;
151 }
152
Stephen Hines651f13c2014-04-23 16:59:28 -0700153 void EmitCXXDestructors(const CXXDestructorDecl *D) override;
Reid Klecknera4130ba2013-07-22 13:51:44 +0000154
Stephen Hines651f13c2014-04-23 16:59:28 -0700155 const CXXRecordDecl *
156 getThisArgumentTypeForMethod(const CXXMethodDecl *MD) override {
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +0000157 MD = MD->getCanonicalDecl();
158 if (MD->isVirtual() && !isa<CXXDestructorDecl>(MD)) {
Timur Iskhodzhanov5f0db582013-11-05 15:54:58 +0000159 MicrosoftVTableContext::MethodVFTableLocation ML =
160 CGM.getMicrosoftVTableContext().getMethodVFTableLocation(MD);
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +0000161 // The vbases might be ordered differently in the final overrider object
162 // and the complete object, so the "this" argument may sometimes point to
163 // memory that has no particular type (e.g. past the complete object).
164 // In this case, we just use a generic pointer type.
165 // FIXME: might want to have a more precise type in the non-virtual
166 // multiple inheritance case.
Timur Iskhodzhanov40aa3662013-11-07 13:34:02 +0000167 if (ML.VBase || !ML.VFPtrOffset.isZero())
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700168 return nullptr;
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +0000169 }
170 return MD->getParent();
171 }
172
Stephen Hines651f13c2014-04-23 16:59:28 -0700173 llvm::Value *
174 adjustThisArgumentForVirtualFunctionCall(CodeGenFunction &CGF, GlobalDecl GD,
175 llvm::Value *This,
176 bool VirtualCall) override;
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +0000177
Stephen Hines651f13c2014-04-23 16:59:28 -0700178 void addImplicitStructorParams(CodeGenFunction &CGF, QualType &ResTy,
179 FunctionArgList &Params) override;
John McCall4c40d982010-08-31 07:33:07 +0000180
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +0000181 llvm::Value *adjustThisParameterInVirtualFunctionPrologue(
Stephen Hines651f13c2014-04-23 16:59:28 -0700182 CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This) override;
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +0000183
Stephen Hines651f13c2014-04-23 16:59:28 -0700184 void EmitInstanceFunctionProlog(CodeGenFunction &CGF) override;
John McCallfd708262011-01-27 02:46:02 +0000185
Stephen Hines651f13c2014-04-23 16:59:28 -0700186 unsigned addImplicitConstructorArgs(CodeGenFunction &CGF,
187 const CXXConstructorDecl *D,
188 CXXCtorType Type, bool ForVirtualBase,
189 bool Delegating,
190 CallArgList &Args) override;
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +0000191
Stephen Hines651f13c2014-04-23 16:59:28 -0700192 void EmitDestructorCall(CodeGenFunction &CGF, const CXXDestructorDecl *DD,
193 CXXDtorType Type, bool ForVirtualBase,
194 bool Delegating, llvm::Value *This) override;
195
196 void emitVTableDefinitions(CodeGenVTables &CGVT,
197 const CXXRecordDecl *RD) override;
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +0000198
199 llvm::Value *getVTableAddressPointInStructor(
200 CodeGenFunction &CGF, const CXXRecordDecl *VTableClass,
201 BaseSubobject Base, const CXXRecordDecl *NearestVBase,
Stephen Hines651f13c2014-04-23 16:59:28 -0700202 bool &NeedsVirtualOffset) override;
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +0000203
204 llvm::Constant *
205 getVTableAddressPointForConstExpr(BaseSubobject Base,
Stephen Hines651f13c2014-04-23 16:59:28 -0700206 const CXXRecordDecl *VTableClass) override;
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +0000207
208 llvm::GlobalVariable *getAddrOfVTable(const CXXRecordDecl *RD,
Stephen Hines651f13c2014-04-23 16:59:28 -0700209 CharUnits VPtrOffset) override;
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +0000210
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +0000211 llvm::Value *getVirtualFunctionPointer(CodeGenFunction &CGF, GlobalDecl GD,
Stephen Hines651f13c2014-04-23 16:59:28 -0700212 llvm::Value *This,
213 llvm::Type *Ty) override;
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +0000214
Stephen Hines176edba2014-12-01 14:53:08 -0800215 llvm::Value *EmitVirtualDestructorCall(CodeGenFunction &CGF,
216 const CXXDestructorDecl *Dtor,
217 CXXDtorType DtorType,
218 llvm::Value *This,
219 const CXXMemberCallExpr *CE) override;
Timur Iskhodzhanov0f9827f2013-02-15 14:45:22 +0000220
Timur Iskhodzhanov2cb17a02013-10-09 09:23:58 +0000221 void adjustCallArgsForDestructorThunk(CodeGenFunction &CGF, GlobalDecl GD,
Stephen Hines651f13c2014-04-23 16:59:28 -0700222 CallArgList &CallArgs) override {
Timur Iskhodzhanov2cb17a02013-10-09 09:23:58 +0000223 assert(GD.getDtorType() == Dtor_Deleting &&
224 "Only deleting destructor thunks are available in this ABI");
225 CallArgs.add(RValue::get(getStructorImplicitParamValue(CGF)),
226 CGM.getContext().IntTy);
227 }
228
Stephen Hines651f13c2014-04-23 16:59:28 -0700229 void emitVirtualInheritanceTables(const CXXRecordDecl *RD) override;
Reid Kleckner90633022013-06-19 15:20:38 +0000230
Stephen Hines651f13c2014-04-23 16:59:28 -0700231 llvm::GlobalVariable *
232 getAddrOfVBTable(const VPtrInfo &VBT, const CXXRecordDecl *RD,
233 llvm::GlobalVariable::LinkageTypes Linkage);
234
235 void emitVBTableDefinition(const VPtrInfo &VBT, const CXXRecordDecl *RD,
236 llvm::GlobalVariable *GV) const;
237
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700238 void setThunkLinkage(llvm::Function *Thunk, bool ForVTable,
239 GlobalDecl GD, bool ReturnAdjustment) override {
240 // Never dllimport/dllexport thunks.
241 Thunk->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
242
243 GVALinkage Linkage =
244 getContext().GetGVALinkageForFunction(cast<FunctionDecl>(GD.getDecl()));
245
246 if (Linkage == GVA_Internal)
247 Thunk->setLinkage(llvm::GlobalValue::InternalLinkage);
248 else if (ReturnAdjustment)
249 Thunk->setLinkage(llvm::GlobalValue::WeakODRLinkage);
250 else
251 Thunk->setLinkage(llvm::GlobalValue::LinkOnceODRLinkage);
Timur Iskhodzhanov2cb17a02013-10-09 09:23:58 +0000252 }
253
Timur Iskhodzhanovc70cc5d2013-10-30 11:55:43 +0000254 llvm::Value *performThisAdjustment(CodeGenFunction &CGF, llvm::Value *This,
Stephen Hines651f13c2014-04-23 16:59:28 -0700255 const ThisAdjustment &TA) override;
Timur Iskhodzhanovc70cc5d2013-10-30 11:55:43 +0000256
257 llvm::Value *performReturnAdjustment(CodeGenFunction &CGF, llvm::Value *Ret,
Stephen Hines651f13c2014-04-23 16:59:28 -0700258 const ReturnAdjustment &RA) override;
Timur Iskhodzhanovc70cc5d2013-10-30 11:55:43 +0000259
Stephen Hines176edba2014-12-01 14:53:08 -0800260 void EmitThreadLocalInitFuncs(
261 CodeGenModule &CGM,
262 ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *>>
263 CXXThreadLocals,
264 ArrayRef<llvm::Function *> CXXThreadLocalInits,
265 ArrayRef<llvm::GlobalVariable *> CXXThreadLocalInitVars) override;
266
267 bool usesThreadWrapperFunction() const override { return false; }
268 LValue EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF, const VarDecl *VD,
269 QualType LValType) override;
270
John McCall20bb1752012-05-01 06:13:13 +0000271 void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
272 llvm::GlobalVariable *DeclPtr,
Stephen Hines651f13c2014-04-23 16:59:28 -0700273 bool PerformInit) override;
Stephen Hines176edba2014-12-01 14:53:08 -0800274 void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
275 llvm::Constant *Dtor, llvm::Constant *Addr) override;
John McCall20bb1752012-05-01 06:13:13 +0000276
John McCallfd708262011-01-27 02:46:02 +0000277 // ==== Notes on array cookies =========
278 //
279 // MSVC seems to only use cookies when the class has a destructor; a
280 // two-argument usual array deallocation function isn't sufficient.
281 //
282 // For example, this code prints "100" and "1":
283 // struct A {
284 // char x;
285 // void *operator new[](size_t sz) {
286 // printf("%u\n", sz);
287 // return malloc(sz);
288 // }
289 // void operator delete[](void *p, size_t sz) {
290 // printf("%u\n", sz);
291 // free(p);
292 // }
293 // };
294 // int main() {
295 // A *p = new A[100];
296 // delete[] p;
297 // }
298 // Whereas it prints "104" and "104" if you give A a destructor.
John McCalle2b45e22012-05-01 05:23:51 +0000299
Stephen Hines651f13c2014-04-23 16:59:28 -0700300 bool requiresArrayCookie(const CXXDeleteExpr *expr,
301 QualType elementType) override;
302 bool requiresArrayCookie(const CXXNewExpr *expr) override;
303 CharUnits getArrayCookieSizeImpl(QualType type) override;
John McCalle2b45e22012-05-01 05:23:51 +0000304 llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
305 llvm::Value *NewPtr,
306 llvm::Value *NumElements,
307 const CXXNewExpr *expr,
Stephen Hines651f13c2014-04-23 16:59:28 -0700308 QualType ElementType) override;
John McCalle2b45e22012-05-01 05:23:51 +0000309 llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF,
310 llvm::Value *allocPtr,
Stephen Hines651f13c2014-04-23 16:59:28 -0700311 CharUnits cookieSize) override;
Reid Klecknera8a0f762013-03-22 19:02:54 +0000312
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700313 friend struct MSRTTIBuilder;
314
315 bool isImageRelative() const {
316 return CGM.getTarget().getPointerWidth(/*AddressSpace=*/0) == 64;
317 }
318
319 // 5 routines for constructing the llvm types for MS RTTI structs.
320 llvm::StructType *getTypeDescriptorType(StringRef TypeInfoString) {
321 llvm::SmallString<32> TDTypeName("rtti.TypeDescriptor");
322 TDTypeName += llvm::utostr(TypeInfoString.size());
323 llvm::StructType *&TypeDescriptorType =
324 TypeDescriptorTypeMap[TypeInfoString.size()];
325 if (TypeDescriptorType)
326 return TypeDescriptorType;
327 llvm::Type *FieldTypes[] = {
328 CGM.Int8PtrPtrTy,
329 CGM.Int8PtrTy,
330 llvm::ArrayType::get(CGM.Int8Ty, TypeInfoString.size() + 1)};
331 TypeDescriptorType =
332 llvm::StructType::create(CGM.getLLVMContext(), FieldTypes, TDTypeName);
333 return TypeDescriptorType;
334 }
335
336 llvm::Type *getImageRelativeType(llvm::Type *PtrType) {
337 if (!isImageRelative())
338 return PtrType;
339 return CGM.IntTy;
340 }
341
342 llvm::StructType *getBaseClassDescriptorType() {
343 if (BaseClassDescriptorType)
344 return BaseClassDescriptorType;
345 llvm::Type *FieldTypes[] = {
346 getImageRelativeType(CGM.Int8PtrTy),
347 CGM.IntTy,
348 CGM.IntTy,
349 CGM.IntTy,
350 CGM.IntTy,
351 CGM.IntTy,
352 getImageRelativeType(getClassHierarchyDescriptorType()->getPointerTo()),
353 };
354 BaseClassDescriptorType = llvm::StructType::create(
355 CGM.getLLVMContext(), FieldTypes, "rtti.BaseClassDescriptor");
356 return BaseClassDescriptorType;
357 }
358
359 llvm::StructType *getClassHierarchyDescriptorType() {
360 if (ClassHierarchyDescriptorType)
361 return ClassHierarchyDescriptorType;
362 // Forward-declare RTTIClassHierarchyDescriptor to break a cycle.
363 ClassHierarchyDescriptorType = llvm::StructType::create(
364 CGM.getLLVMContext(), "rtti.ClassHierarchyDescriptor");
365 llvm::Type *FieldTypes[] = {
366 CGM.IntTy,
367 CGM.IntTy,
368 CGM.IntTy,
369 getImageRelativeType(
370 getBaseClassDescriptorType()->getPointerTo()->getPointerTo()),
371 };
372 ClassHierarchyDescriptorType->setBody(FieldTypes);
373 return ClassHierarchyDescriptorType;
374 }
375
376 llvm::StructType *getCompleteObjectLocatorType() {
377 if (CompleteObjectLocatorType)
378 return CompleteObjectLocatorType;
379 CompleteObjectLocatorType = llvm::StructType::create(
380 CGM.getLLVMContext(), "rtti.CompleteObjectLocator");
381 llvm::Type *FieldTypes[] = {
382 CGM.IntTy,
383 CGM.IntTy,
384 CGM.IntTy,
385 getImageRelativeType(CGM.Int8PtrTy),
386 getImageRelativeType(getClassHierarchyDescriptorType()->getPointerTo()),
387 getImageRelativeType(CompleteObjectLocatorType),
388 };
389 llvm::ArrayRef<llvm::Type *> FieldTypesRef(FieldTypes);
390 if (!isImageRelative())
391 FieldTypesRef = FieldTypesRef.drop_back();
392 CompleteObjectLocatorType->setBody(FieldTypesRef);
393 return CompleteObjectLocatorType;
394 }
395
396 llvm::GlobalVariable *getImageBase() {
397 StringRef Name = "__ImageBase";
398 if (llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name))
399 return GV;
400
401 return new llvm::GlobalVariable(CGM.getModule(), CGM.Int8Ty,
402 /*isConstant=*/true,
403 llvm::GlobalValue::ExternalLinkage,
404 /*Initializer=*/nullptr, Name);
405 }
406
407 llvm::Constant *getImageRelativeConstant(llvm::Constant *PtrVal) {
408 if (!isImageRelative())
409 return PtrVal;
410
411 llvm::Constant *ImageBaseAsInt =
412 llvm::ConstantExpr::getPtrToInt(getImageBase(), CGM.IntPtrTy);
413 llvm::Constant *PtrValAsInt =
414 llvm::ConstantExpr::getPtrToInt(PtrVal, CGM.IntPtrTy);
415 llvm::Constant *Diff =
416 llvm::ConstantExpr::getSub(PtrValAsInt, ImageBaseAsInt,
417 /*HasNUW=*/true, /*HasNSW=*/true);
418 return llvm::ConstantExpr::getTrunc(Diff, CGM.IntTy);
419 }
420
Reid Klecknera8a0f762013-03-22 19:02:54 +0000421private:
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +0000422 MicrosoftMangleContext &getMangleContext() {
423 return cast<MicrosoftMangleContext>(CodeGen::CGCXXABI::getMangleContext());
424 }
425
Reid Klecknera3609b02013-04-11 18:13:19 +0000426 llvm::Constant *getZeroInt() {
427 return llvm::ConstantInt::get(CGM.IntTy, 0);
Reid Klecknera8a0f762013-03-22 19:02:54 +0000428 }
429
Reid Klecknera3609b02013-04-11 18:13:19 +0000430 llvm::Constant *getAllOnesInt() {
431 return llvm::Constant::getAllOnesValue(CGM.IntTy);
Reid Klecknera8a0f762013-03-22 19:02:54 +0000432 }
433
Reid Klecknerf6327302013-05-09 21:01:17 +0000434 llvm::Constant *getConstantOrZeroInt(llvm::Constant *C) {
435 return C ? C : getZeroInt();
436 }
437
438 llvm::Value *getValueOrZeroInt(llvm::Value *C) {
439 return C ? C : getZeroInt();
440 }
441
Stephen Hines651f13c2014-04-23 16:59:28 -0700442 CharUnits getVirtualFunctionPrologueThisAdjustment(GlobalDecl GD);
443
Reid Klecknera3609b02013-04-11 18:13:19 +0000444 void
445 GetNullMemberPointerFields(const MemberPointerType *MPT,
446 llvm::SmallVectorImpl<llvm::Constant *> &fields);
447
Reid Klecknerb0f533e2013-05-29 18:02:47 +0000448 /// \brief Shared code for virtual base adjustment. Returns the offset from
449 /// the vbptr to the virtual base. Optionally returns the address of the
450 /// vbptr itself.
451 llvm::Value *GetVBaseOffsetFromVBPtr(CodeGenFunction &CGF,
452 llvm::Value *Base,
453 llvm::Value *VBPtrOffset,
454 llvm::Value *VBTableOffset,
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700455 llvm::Value **VBPtr = nullptr);
Reid Klecknerb0f533e2013-05-29 18:02:47 +0000456
Timur Iskhodzhanovc70cc5d2013-10-30 11:55:43 +0000457 llvm::Value *GetVBaseOffsetFromVBPtr(CodeGenFunction &CGF,
458 llvm::Value *Base,
459 int32_t VBPtrOffset,
460 int32_t VBTableOffset,
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700461 llvm::Value **VBPtr = nullptr) {
Stephen Hines176edba2014-12-01 14:53:08 -0800462 assert(VBTableOffset % 4 == 0 && "should be byte offset into table of i32s");
Timur Iskhodzhanovc70cc5d2013-10-30 11:55:43 +0000463 llvm::Value *VBPOffset = llvm::ConstantInt::get(CGM.IntTy, VBPtrOffset),
464 *VBTOffset = llvm::ConstantInt::get(CGM.IntTy, VBTableOffset);
465 return GetVBaseOffsetFromVBPtr(CGF, Base, VBPOffset, VBTOffset, VBPtr);
466 }
467
Reid Klecknerb0f533e2013-05-29 18:02:47 +0000468 /// \brief Performs a full virtual base adjustment. Used to dereference
469 /// pointers to members of virtual bases.
Stephen Hines651f13c2014-04-23 16:59:28 -0700470 llvm::Value *AdjustVirtualBase(CodeGenFunction &CGF, const Expr *E,
471 const CXXRecordDecl *RD, llvm::Value *Base,
Reid Klecknera3609b02013-04-11 18:13:19 +0000472 llvm::Value *VirtualBaseAdjustmentOffset,
473 llvm::Value *VBPtrOffset /* optional */);
474
Reid Kleckner79e02912013-05-03 01:15:11 +0000475 /// \brief Emits a full member pointer with the fields common to data and
476 /// function member pointers.
477 llvm::Constant *EmitFullMemberPointer(llvm::Constant *FirstField,
478 bool IsMemberFunction,
Reid Klecknerf6327302013-05-09 21:01:17 +0000479 const CXXRecordDecl *RD,
480 CharUnits NonVirtualBaseAdjustment);
481
482 llvm::Constant *BuildMemberPointer(const CXXRecordDecl *RD,
483 const CXXMethodDecl *MD,
484 CharUnits NonVirtualBaseAdjustment);
485
486 bool MemberPointerConstantIsNull(const MemberPointerType *MPT,
487 llvm::Constant *MP);
Reid Kleckner79e02912013-05-03 01:15:11 +0000488
Reid Kleckner90633022013-06-19 15:20:38 +0000489 /// \brief - Initialize all vbptrs of 'this' with RD as the complete type.
490 void EmitVBPtrStores(CodeGenFunction &CGF, const CXXRecordDecl *RD);
491
492 /// \brief Caching wrapper around VBTableBuilder::enumerateVBTables().
Stephen Hines651f13c2014-04-23 16:59:28 -0700493 const VBTableGlobals &enumerateVBTables(const CXXRecordDecl *RD);
Reid Kleckner90633022013-06-19 15:20:38 +0000494
Hans Wennborg93b717a2013-11-15 17:24:45 +0000495 /// \brief Generate a thunk for calling a virtual member function MD.
Stephen Hines651f13c2014-04-23 16:59:28 -0700496 llvm::Function *EmitVirtualMemPtrThunk(
497 const CXXMethodDecl *MD,
498 const MicrosoftVTableContext::MethodVFTableLocation &ML);
Hans Wennborg93b717a2013-11-15 17:24:45 +0000499
Reid Klecknera8a0f762013-03-22 19:02:54 +0000500public:
Stephen Hines651f13c2014-04-23 16:59:28 -0700501 llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT) override;
Reid Klecknera3609b02013-04-11 18:13:19 +0000502
Stephen Hines651f13c2014-04-23 16:59:28 -0700503 bool isZeroInitializable(const MemberPointerType *MPT) override;
Reid Klecknera3609b02013-04-11 18:13:19 +0000504
Stephen Hines176edba2014-12-01 14:53:08 -0800505 bool isMemberPointerConvertible(const MemberPointerType *MPT) const override {
506 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
507 return RD->hasAttr<MSInheritanceAttr>();
508 }
509
510 bool isTypeInfoCalculable(QualType Ty) const override {
511 if (!CGCXXABI::isTypeInfoCalculable(Ty))
512 return false;
513 if (const auto *MPT = Ty->getAs<MemberPointerType>()) {
514 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
515 if (!RD->hasAttr<MSInheritanceAttr>())
516 return false;
517 }
518 return true;
519 }
520
Stephen Hines651f13c2014-04-23 16:59:28 -0700521 llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT) override;
Reid Klecknera8a0f762013-03-22 19:02:54 +0000522
Stephen Hines651f13c2014-04-23 16:59:28 -0700523 llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
524 CharUnits offset) override;
525 llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD) override;
526 llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT) override;
Reid Klecknera8a0f762013-03-22 19:02:54 +0000527
Stephen Hines651f13c2014-04-23 16:59:28 -0700528 llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF,
529 llvm::Value *L,
530 llvm::Value *R,
531 const MemberPointerType *MPT,
532 bool Inequality) override;
Reid Kleckner3d2f0002013-04-30 20:15:14 +0000533
Stephen Hines651f13c2014-04-23 16:59:28 -0700534 llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
535 llvm::Value *MemPtr,
536 const MemberPointerType *MPT) override;
Reid Klecknera8a0f762013-03-22 19:02:54 +0000537
Stephen Hines651f13c2014-04-23 16:59:28 -0700538 llvm::Value *
539 EmitMemberDataPointerAddress(CodeGenFunction &CGF, const Expr *E,
540 llvm::Value *Base, llvm::Value *MemPtr,
541 const MemberPointerType *MPT) override;
Reid Klecknera8a0f762013-03-22 19:02:54 +0000542
Stephen Hines651f13c2014-04-23 16:59:28 -0700543 llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
544 const CastExpr *E,
545 llvm::Value *Src) override;
Reid Klecknerf6327302013-05-09 21:01:17 +0000546
Stephen Hines651f13c2014-04-23 16:59:28 -0700547 llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
548 llvm::Constant *Src) override;
Reid Klecknerf6327302013-05-09 21:01:17 +0000549
Stephen Hines651f13c2014-04-23 16:59:28 -0700550 llvm::Value *
551 EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, const Expr *E,
552 llvm::Value *&This, llvm::Value *MemPtr,
553 const MemberPointerType *MPT) override;
Reid Klecknera3609b02013-04-11 18:13:19 +0000554
Stephen Hines176edba2014-12-01 14:53:08 -0800555 void emitCXXStructor(const CXXMethodDecl *MD, StructorType Type) override;
556
Reid Kleckner90633022013-06-19 15:20:38 +0000557private:
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +0000558 typedef std::pair<const CXXRecordDecl *, CharUnits> VFTableIdTy;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700559 typedef llvm::DenseMap<VFTableIdTy, llvm::GlobalVariable *> VTablesMapTy;
560 typedef llvm::DenseMap<VFTableIdTy, llvm::GlobalValue *> VFTablesMapTy;
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +0000561 /// \brief All the vftables that have been referenced.
562 VFTablesMapTy VFTablesMap;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700563 VTablesMapTy VTablesMap;
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +0000564
565 /// \brief This set holds the record decls we've deferred vtable emission for.
566 llvm::SmallPtrSet<const CXXRecordDecl *, 4> DeferredVFTables;
567
568
569 /// \brief All the vbtables which have been referenced.
Stephen Hines651f13c2014-04-23 16:59:28 -0700570 llvm::DenseMap<const CXXRecordDecl *, VBTableGlobals> VBTablesMap;
Reid Kleckner942f9fe2013-09-10 20:14:30 +0000571
572 /// Info on the global variable used to guard initialization of static locals.
573 /// The BitIndex field is only used for externally invisible declarations.
574 struct GuardInfo {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700575 GuardInfo() : Guard(nullptr), BitIndex(0) {}
Reid Kleckner942f9fe2013-09-10 20:14:30 +0000576 llvm::GlobalVariable *Guard;
577 unsigned BitIndex;
578 };
579
580 /// Map from DeclContext to the current guard variable. We assume that the
581 /// AST is visited in source code order.
582 llvm::DenseMap<const DeclContext *, GuardInfo> GuardVariableMap;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700583
584 llvm::DenseMap<size_t, llvm::StructType *> TypeDescriptorTypeMap;
585 llvm::StructType *BaseClassDescriptorType;
586 llvm::StructType *ClassHierarchyDescriptorType;
587 llvm::StructType *CompleteObjectLocatorType;
Charles Davisc3926642010-06-09 23:25:41 +0000588};
589
590}
591
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700592CGCXXABI::RecordArgABI
593MicrosoftCXXABI::getRecordArgABI(const CXXRecordDecl *RD) const {
594 switch (CGM.getTarget().getTriple().getArch()) {
595 default:
596 // FIXME: Implement for other architectures.
597 return RAA_Default;
598
599 case llvm::Triple::x86:
600 // All record arguments are passed in memory on x86. Decide whether to
601 // construct the object directly in argument memory, or to construct the
602 // argument elsewhere and copy the bytes during the call.
603
604 // If C++ prohibits us from making a copy, construct the arguments directly
605 // into argument memory.
606 if (!canCopyArgument(RD))
607 return RAA_DirectInMemory;
608
609 // Otherwise, construct the argument into a temporary and copy the bytes
610 // into the outgoing argument memory.
611 return RAA_Default;
612
613 case llvm::Triple::x86_64:
614 // Win64 passes objects with non-trivial copy ctors indirectly.
615 if (RD->hasNonTrivialCopyConstructor())
616 return RAA_Indirect;
617
Stephen Hines176edba2014-12-01 14:53:08 -0800618 // If an object has a destructor, we'd really like to pass it indirectly
619 // because it allows us to elide copies. Unfortunately, MSVC makes that
620 // impossible for small types, which it will pass in a single register or
621 // stack slot. Most objects with dtors are large-ish, so handle that early.
622 // We can't call out all large objects as being indirect because there are
623 // multiple x64 calling conventions and the C++ ABI code shouldn't dictate
624 // how we pass large POD types.
625 if (RD->hasNonTrivialDestructor() &&
626 getContext().getTypeSize(RD->getTypeForDecl()) > 64)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700627 return RAA_Indirect;
628
629 // We have a trivial copy constructor or no copy constructors, but we have
630 // to make sure it isn't deleted.
631 bool CopyDeleted = false;
632 for (const CXXConstructorDecl *CD : RD->ctors()) {
633 if (CD->isCopyConstructor()) {
634 assert(CD->isTrivial());
635 // We had at least one undeleted trivial copy ctor. Return directly.
636 if (!CD->isDeleted())
637 return RAA_Default;
638 CopyDeleted = true;
639 }
640 }
641
642 // The trivial copy constructor was deleted. Return indirectly.
643 if (CopyDeleted)
644 return RAA_Indirect;
645
646 // There were no copy ctors. Return in RAX.
647 return RAA_Default;
648 }
649
650 llvm_unreachable("invalid enum");
651}
652
Stephen Hines176edba2014-12-01 14:53:08 -0800653void MicrosoftCXXABI::emitVirtualObjectDelete(CodeGenFunction &CGF,
654 const CXXDeleteExpr *DE,
655 llvm::Value *Ptr,
656 QualType ElementType,
657 const CXXDestructorDecl *Dtor) {
658 // FIXME: Provide a source location here even though there's no
659 // CXXMemberCallExpr for dtor call.
660 bool UseGlobalDelete = DE->isGlobalDelete();
661 CXXDtorType DtorType = UseGlobalDelete ? Dtor_Complete : Dtor_Deleting;
662 llvm::Value *MDThis =
663 EmitVirtualDestructorCall(CGF, Dtor, DtorType, Ptr, /*CE=*/nullptr);
664 if (UseGlobalDelete)
665 CGF.EmitDeleteCall(DE->getOperatorDelete(), MDThis, ElementType);
John McCallecd03b42012-09-25 10:10:39 +0000666}
667
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700668/// \brief Gets the offset to the virtual base that contains the vfptr for
669/// MS-ABI polymorphic types.
670static llvm::Value *getPolymorphicOffset(CodeGenFunction &CGF,
671 const CXXRecordDecl *RD,
672 llvm::Value *Value) {
673 const ASTContext &Context = RD->getASTContext();
674 for (const CXXBaseSpecifier &Base : RD->vbases())
675 if (Context.getASTRecordLayout(Base.getType()->getAsCXXRecordDecl())
676 .hasExtendableVFPtr())
677 return CGF.CGM.getCXXABI().GetVirtualBaseClassOffset(
678 CGF, Value, RD, Base.getType()->getAsCXXRecordDecl());
679 llvm_unreachable("One of our vbases should be polymorphic.");
680}
681
682static std::pair<llvm::Value *, llvm::Value *>
683performBaseAdjustment(CodeGenFunction &CGF, llvm::Value *Value,
684 QualType SrcRecordTy) {
685 Value = CGF.Builder.CreateBitCast(Value, CGF.Int8PtrTy);
686 const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl();
687
688 if (CGF.getContext().getASTRecordLayout(SrcDecl).hasExtendableVFPtr())
689 return std::make_pair(Value, llvm::ConstantInt::get(CGF.Int32Ty, 0));
690
691 // Perform a base adjustment.
692 llvm::Value *Offset = getPolymorphicOffset(CGF, SrcDecl, Value);
693 Value = CGF.Builder.CreateInBoundsGEP(Value, Offset);
694 Offset = CGF.Builder.CreateTrunc(Offset, CGF.Int32Ty);
695 return std::make_pair(Value, Offset);
696}
697
698bool MicrosoftCXXABI::shouldTypeidBeNullChecked(bool IsDeref,
699 QualType SrcRecordTy) {
700 const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl();
701 return IsDeref &&
702 !CGM.getContext().getASTRecordLayout(SrcDecl).hasExtendableVFPtr();
703}
704
705static llvm::CallSite emitRTtypeidCall(CodeGenFunction &CGF,
706 llvm::Value *Argument) {
707 llvm::Type *ArgTypes[] = {CGF.Int8PtrTy};
708 llvm::FunctionType *FTy =
709 llvm::FunctionType::get(CGF.Int8PtrTy, ArgTypes, false);
710 llvm::Value *Args[] = {Argument};
711 llvm::Constant *Fn = CGF.CGM.CreateRuntimeFunction(FTy, "__RTtypeid");
712 return CGF.EmitRuntimeCallOrInvoke(Fn, Args);
713}
714
715void MicrosoftCXXABI::EmitBadTypeidCall(CodeGenFunction &CGF) {
716 llvm::CallSite Call =
717 emitRTtypeidCall(CGF, llvm::Constant::getNullValue(CGM.VoidPtrTy));
718 Call.setDoesNotReturn();
719 CGF.Builder.CreateUnreachable();
720}
721
722llvm::Value *MicrosoftCXXABI::EmitTypeid(CodeGenFunction &CGF,
723 QualType SrcRecordTy,
724 llvm::Value *ThisPtr,
725 llvm::Type *StdTypeInfoPtrTy) {
726 llvm::Value *Offset;
727 std::tie(ThisPtr, Offset) = performBaseAdjustment(CGF, ThisPtr, SrcRecordTy);
728 return CGF.Builder.CreateBitCast(
729 emitRTtypeidCall(CGF, ThisPtr).getInstruction(), StdTypeInfoPtrTy);
730}
731
732bool MicrosoftCXXABI::shouldDynamicCastCallBeNullChecked(bool SrcIsPtr,
733 QualType SrcRecordTy) {
734 const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl();
735 return SrcIsPtr &&
736 !CGM.getContext().getASTRecordLayout(SrcDecl).hasExtendableVFPtr();
737}
738
739llvm::Value *MicrosoftCXXABI::EmitDynamicCastCall(
740 CodeGenFunction &CGF, llvm::Value *Value, QualType SrcRecordTy,
741 QualType DestTy, QualType DestRecordTy, llvm::BasicBlock *CastEnd) {
742 llvm::Type *DestLTy = CGF.ConvertType(DestTy);
743
744 llvm::Value *SrcRTTI =
745 CGF.CGM.GetAddrOfRTTIDescriptor(SrcRecordTy.getUnqualifiedType());
746 llvm::Value *DestRTTI =
747 CGF.CGM.GetAddrOfRTTIDescriptor(DestRecordTy.getUnqualifiedType());
748
749 llvm::Value *Offset;
750 std::tie(Value, Offset) = performBaseAdjustment(CGF, Value, SrcRecordTy);
751
752 // PVOID __RTDynamicCast(
753 // PVOID inptr,
754 // LONG VfDelta,
755 // PVOID SrcType,
756 // PVOID TargetType,
757 // BOOL isReference)
758 llvm::Type *ArgTypes[] = {CGF.Int8PtrTy, CGF.Int32Ty, CGF.Int8PtrTy,
759 CGF.Int8PtrTy, CGF.Int32Ty};
760 llvm::Constant *Function = CGF.CGM.CreateRuntimeFunction(
761 llvm::FunctionType::get(CGF.Int8PtrTy, ArgTypes, false),
762 "__RTDynamicCast");
763 llvm::Value *Args[] = {
764 Value, Offset, SrcRTTI, DestRTTI,
765 llvm::ConstantInt::get(CGF.Int32Ty, DestTy->isReferenceType())};
766 Value = CGF.EmitRuntimeCallOrInvoke(Function, Args).getInstruction();
767 return CGF.Builder.CreateBitCast(Value, DestLTy);
768}
769
770llvm::Value *
771MicrosoftCXXABI::EmitDynamicCastToVoid(CodeGenFunction &CGF, llvm::Value *Value,
772 QualType SrcRecordTy,
773 QualType DestTy) {
774 llvm::Value *Offset;
775 std::tie(Value, Offset) = performBaseAdjustment(CGF, Value, SrcRecordTy);
776
777 // PVOID __RTCastToVoid(
778 // PVOID inptr)
779 llvm::Type *ArgTypes[] = {CGF.Int8PtrTy};
780 llvm::Constant *Function = CGF.CGM.CreateRuntimeFunction(
781 llvm::FunctionType::get(CGF.Int8PtrTy, ArgTypes, false),
782 "__RTCastToVoid");
783 llvm::Value *Args[] = {Value};
784 return CGF.EmitRuntimeCall(Function, Args);
785}
786
787bool MicrosoftCXXABI::EmitBadCastCall(CodeGenFunction &CGF) {
788 return false;
789}
790
Stephen Hines176edba2014-12-01 14:53:08 -0800791llvm::Value *MicrosoftCXXABI::GetVirtualBaseClassOffset(
792 CodeGenFunction &CGF, llvm::Value *This, const CXXRecordDecl *ClassDecl,
793 const CXXRecordDecl *BaseClassDecl) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700794 int64_t VBPtrChars =
795 getContext().getASTRecordLayout(ClassDecl).getVBPtrOffset().getQuantity();
Reid Klecknerb0f533e2013-05-29 18:02:47 +0000796 llvm::Value *VBPtrOffset = llvm::ConstantInt::get(CGM.PtrDiffTy, VBPtrChars);
Reid Klecknerb0f533e2013-05-29 18:02:47 +0000797 CharUnits IntSize = getContext().getTypeSizeInChars(getContext().IntTy);
Timur Iskhodzhanov5f0db582013-11-05 15:54:58 +0000798 CharUnits VBTableChars =
799 IntSize *
800 CGM.getMicrosoftVTableContext().getVBTableIndex(ClassDecl, BaseClassDecl);
Reid Klecknerb0f533e2013-05-29 18:02:47 +0000801 llvm::Value *VBTableOffset =
Stephen Hines176edba2014-12-01 14:53:08 -0800802 llvm::ConstantInt::get(CGM.IntTy, VBTableChars.getQuantity());
Reid Klecknerb0f533e2013-05-29 18:02:47 +0000803
804 llvm::Value *VBPtrToNewBase =
Stephen Hines176edba2014-12-01 14:53:08 -0800805 GetVBaseOffsetFromVBPtr(CGF, This, VBPtrOffset, VBTableOffset);
Reid Klecknerb0f533e2013-05-29 18:02:47 +0000806 VBPtrToNewBase =
Stephen Hines176edba2014-12-01 14:53:08 -0800807 CGF.Builder.CreateSExtOrBitCast(VBPtrToNewBase, CGM.PtrDiffTy);
Reid Klecknerb0f533e2013-05-29 18:02:47 +0000808 return CGF.Builder.CreateNSWAdd(VBPtrOffset, VBPtrToNewBase);
809}
810
Stephen Lin3b50e8d2013-06-30 20:40:16 +0000811bool MicrosoftCXXABI::HasThisReturn(GlobalDecl GD) const {
812 return isa<CXXConstructorDecl>(GD.getDecl());
John McCallbd315742012-09-25 08:00:39 +0000813}
814
Stephen Hines176edba2014-12-01 14:53:08 -0800815static bool isDeletingDtor(GlobalDecl GD) {
816 return isa<CXXDestructorDecl>(GD.getDecl()) &&
817 GD.getDtorType() == Dtor_Deleting;
818}
819
820bool MicrosoftCXXABI::hasMostDerivedReturn(GlobalDecl GD) const {
821 return isDeletingDtor(GD);
822}
823
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700824bool MicrosoftCXXABI::classifyReturnType(CGFunctionInfo &FI) const {
825 const CXXRecordDecl *RD = FI.getReturnType()->getAsCXXRecordDecl();
826 if (!RD)
827 return false;
828
829 if (FI.isInstanceMethod()) {
830 // If it's an instance method, aggregates are always returned indirectly via
831 // the second parameter.
832 FI.getReturnInfo() = ABIArgInfo::getIndirect(0, /*ByVal=*/false);
833 FI.getReturnInfo().setSRetAfterThis(FI.isInstanceMethod());
834 return true;
835 } else if (!RD->isPOD()) {
836 // If it's a free function, non-POD types are returned indirectly.
837 FI.getReturnInfo() = ABIArgInfo::getIndirect(0, /*ByVal=*/false);
838 return true;
839 }
840
841 // Otherwise, use the C ABI rules.
842 return false;
843}
844
Reid Kleckner90633022013-06-19 15:20:38 +0000845llvm::BasicBlock *
846MicrosoftCXXABI::EmitCtorCompleteObjectHandler(CodeGenFunction &CGF,
847 const CXXRecordDecl *RD) {
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000848 llvm::Value *IsMostDerivedClass = getStructorImplicitParamValue(CGF);
849 assert(IsMostDerivedClass &&
850 "ctor for a class with virtual bases must have an implicit parameter");
Reid Kleckner90633022013-06-19 15:20:38 +0000851 llvm::Value *IsCompleteObject =
852 CGF.Builder.CreateIsNotNull(IsMostDerivedClass, "is_complete_object");
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000853
854 llvm::BasicBlock *CallVbaseCtorsBB = CGF.createBasicBlock("ctor.init_vbases");
855 llvm::BasicBlock *SkipVbaseCtorsBB = CGF.createBasicBlock("ctor.skip_vbases");
856 CGF.Builder.CreateCondBr(IsCompleteObject,
857 CallVbaseCtorsBB, SkipVbaseCtorsBB);
858
859 CGF.EmitBlock(CallVbaseCtorsBB);
Reid Kleckner90633022013-06-19 15:20:38 +0000860
861 // Fill in the vbtable pointers here.
862 EmitVBPtrStores(CGF, RD);
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000863
864 // CGF will put the base ctor calls in this basic block for us later.
865
866 return SkipVbaseCtorsBB;
John McCallbd315742012-09-25 08:00:39 +0000867}
868
Timur Iskhodzhanov5bd0d442013-10-09 18:16:58 +0000869void MicrosoftCXXABI::initializeHiddenVirtualInheritanceMembers(
870 CodeGenFunction &CGF, const CXXRecordDecl *RD) {
871 // In most cases, an override for a vbase virtual method can adjust
872 // the "this" parameter by applying a constant offset.
873 // However, this is not enough while a constructor or a destructor of some
874 // class X is being executed if all the following conditions are met:
875 // - X has virtual bases, (1)
876 // - X overrides a virtual method M of a vbase Y, (2)
877 // - X itself is a vbase of the most derived class.
878 //
879 // If (1) and (2) are true, the vtorDisp for vbase Y is a hidden member of X
880 // which holds the extra amount of "this" adjustment we must do when we use
881 // the X vftables (i.e. during X ctor or dtor).
882 // Outside the ctors and dtors, the values of vtorDisps are zero.
883
884 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
885 typedef ASTRecordLayout::VBaseOffsetsMapTy VBOffsets;
886 const VBOffsets &VBaseMap = Layout.getVBaseOffsetsMap();
887 CGBuilderTy &Builder = CGF.Builder;
888
889 unsigned AS =
890 cast<llvm::PointerType>(getThisValue(CGF)->getType())->getAddressSpace();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700891 llvm::Value *Int8This = nullptr; // Initialize lazily.
Timur Iskhodzhanov5bd0d442013-10-09 18:16:58 +0000892
893 for (VBOffsets::const_iterator I = VBaseMap.begin(), E = VBaseMap.end();
894 I != E; ++I) {
895 if (!I->second.hasVtorDisp())
896 continue;
897
Timur Iskhodzhanov42e68d52013-11-13 16:03:43 +0000898 llvm::Value *VBaseOffset =
899 GetVirtualBaseClassOffset(CGF, getThisValue(CGF), RD, I->first);
Timur Iskhodzhanov5bd0d442013-10-09 18:16:58 +0000900 // FIXME: it doesn't look right that we SExt in GetVirtualBaseClassOffset()
901 // just to Trunc back immediately.
902 VBaseOffset = Builder.CreateTruncOrBitCast(VBaseOffset, CGF.Int32Ty);
903 uint64_t ConstantVBaseOffset =
904 Layout.getVBaseClassOffset(I->first).getQuantity();
905
906 // vtorDisp_for_vbase = vbptr[vbase_idx] - offsetof(RD, vbase).
907 llvm::Value *VtorDispValue = Builder.CreateSub(
908 VBaseOffset, llvm::ConstantInt::get(CGM.Int32Ty, ConstantVBaseOffset),
909 "vtordisp.value");
910
911 if (!Int8This)
912 Int8This = Builder.CreateBitCast(getThisValue(CGF),
913 CGF.Int8Ty->getPointerTo(AS));
914 llvm::Value *VtorDispPtr = Builder.CreateInBoundsGEP(Int8This, VBaseOffset);
915 // vtorDisp is always the 32-bits before the vbase in the class layout.
916 VtorDispPtr = Builder.CreateConstGEP1_32(VtorDispPtr, -4);
917 VtorDispPtr = Builder.CreateBitCast(
918 VtorDispPtr, CGF.Int32Ty->getPointerTo(AS), "vtordisp.ptr");
919
920 Builder.CreateStore(VtorDispValue, VtorDispPtr);
921 }
922}
923
Timur Iskhodzhanovbb1b7972013-08-04 17:30:04 +0000924void MicrosoftCXXABI::EmitCXXConstructors(const CXXConstructorDecl *D) {
925 // There's only one constructor type in this ABI.
926 CGM.EmitGlobal(GlobalDecl(D, Ctor_Complete));
927}
928
Reid Kleckner90633022013-06-19 15:20:38 +0000929void MicrosoftCXXABI::EmitVBPtrStores(CodeGenFunction &CGF,
930 const CXXRecordDecl *RD) {
931 llvm::Value *ThisInt8Ptr =
932 CGF.Builder.CreateBitCast(getThisValue(CGF), CGM.Int8PtrTy, "this.int8");
Stephen Hines651f13c2014-04-23 16:59:28 -0700933 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Reid Kleckner90633022013-06-19 15:20:38 +0000934
Stephen Hines651f13c2014-04-23 16:59:28 -0700935 const VBTableGlobals &VBGlobals = enumerateVBTables(RD);
936 for (unsigned I = 0, E = VBGlobals.VBTables->size(); I != E; ++I) {
937 const VPtrInfo *VBT = (*VBGlobals.VBTables)[I];
938 llvm::GlobalVariable *GV = VBGlobals.Globals[I];
Reid Kleckner90633022013-06-19 15:20:38 +0000939 const ASTRecordLayout &SubobjectLayout =
Stephen Hines651f13c2014-04-23 16:59:28 -0700940 CGM.getContext().getASTRecordLayout(VBT->BaseWithVPtr);
941 CharUnits Offs = VBT->NonVirtualOffset;
942 Offs += SubobjectLayout.getVBPtrOffset();
943 if (VBT->getVBaseWithVPtr())
944 Offs += Layout.getVBaseClassOffset(VBT->getVBaseWithVPtr());
Reid Kleckner90633022013-06-19 15:20:38 +0000945 llvm::Value *VBPtr =
Stephen Hines651f13c2014-04-23 16:59:28 -0700946 CGF.Builder.CreateConstInBoundsGEP1_64(ThisInt8Ptr, Offs.getQuantity());
Stephen Hines176edba2014-12-01 14:53:08 -0800947 llvm::Value *GVPtr = CGF.Builder.CreateConstInBoundsGEP2_32(GV, 0, 0);
948 VBPtr = CGF.Builder.CreateBitCast(VBPtr, GVPtr->getType()->getPointerTo(0),
Stephen Hines651f13c2014-04-23 16:59:28 -0700949 "vbptr." + VBT->ReusingBase->getName());
Stephen Hines176edba2014-12-01 14:53:08 -0800950 CGF.Builder.CreateStore(GVPtr, VBPtr);
Reid Kleckner90633022013-06-19 15:20:38 +0000951 }
952}
953
Stephen Hines176edba2014-12-01 14:53:08 -0800954void
955MicrosoftCXXABI::buildStructorSignature(const CXXMethodDecl *MD, StructorType T,
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000956 SmallVectorImpl<CanQualType> &ArgTys) {
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000957 // TODO: 'for base' flag
Stephen Hines176edba2014-12-01 14:53:08 -0800958 if (T == StructorType::Deleting) {
Timur Iskhodzhanov1f71f392013-08-27 10:38:19 +0000959 // The scalar deleting destructor takes an implicit int parameter.
960 ArgTys.push_back(CGM.getContext().IntTy);
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000961 }
Stephen Hines176edba2014-12-01 14:53:08 -0800962 auto *CD = dyn_cast<CXXConstructorDecl>(MD);
963 if (!CD)
964 return;
965
966 // All parameters are already in place except is_most_derived, which goes
967 // after 'this' if it's variadic and last if it's not.
968
969 const CXXRecordDecl *Class = CD->getParent();
970 const FunctionProtoType *FPT = CD->getType()->castAs<FunctionProtoType>();
971 if (Class->getNumVBases()) {
972 if (FPT->isVariadic())
973 ArgTys.insert(ArgTys.begin() + 1, CGM.getContext().IntTy);
974 else
975 ArgTys.push_back(CGM.getContext().IntTy);
976 }
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +0000977}
978
Reid Klecknera4130ba2013-07-22 13:51:44 +0000979void MicrosoftCXXABI::EmitCXXDestructors(const CXXDestructorDecl *D) {
980 // The TU defining a dtor is only guaranteed to emit a base destructor. All
981 // other destructor variants are delegating thunks.
982 CGM.EmitGlobal(GlobalDecl(D, Dtor_Base));
983}
984
Stephen Hines651f13c2014-04-23 16:59:28 -0700985CharUnits
986MicrosoftCXXABI::getVirtualFunctionPrologueThisAdjustment(GlobalDecl GD) {
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +0000987 GD = GD.getCanonicalDecl();
988 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Stephen Hines651f13c2014-04-23 16:59:28 -0700989
990 GlobalDecl LookupGD = GD;
991 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
992 // Complete destructors take a pointer to the complete object as a
993 // parameter, thus don't need this adjustment.
994 if (GD.getDtorType() == Dtor_Complete)
995 return CharUnits();
996
997 // There's no Dtor_Base in vftable but it shares the this adjustment with
998 // the deleting one, so look it up instead.
999 LookupGD = GlobalDecl(DD, Dtor_Deleting);
1000 }
1001
1002 MicrosoftVTableContext::MethodVFTableLocation ML =
1003 CGM.getMicrosoftVTableContext().getMethodVFTableLocation(LookupGD);
1004 CharUnits Adjustment = ML.VFPtrOffset;
1005
1006 // Normal virtual instance methods need to adjust from the vfptr that first
1007 // defined the virtual method to the virtual base subobject, but destructors
1008 // do not. The vector deleting destructor thunk applies this adjustment for
1009 // us if necessary.
1010 if (isa<CXXDestructorDecl>(MD))
1011 Adjustment = CharUnits::Zero();
1012
1013 if (ML.VBase) {
1014 const ASTRecordLayout &DerivedLayout =
1015 CGM.getContext().getASTRecordLayout(MD->getParent());
1016 Adjustment += DerivedLayout.getVBaseClassOffset(ML.VBase);
1017 }
1018
1019 return Adjustment;
1020}
1021
1022llvm::Value *MicrosoftCXXABI::adjustThisArgumentForVirtualFunctionCall(
1023 CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This, bool VirtualCall) {
1024 if (!VirtualCall) {
1025 // If the call of a virtual function is not virtual, we just have to
1026 // compensate for the adjustment the virtual function does in its prologue.
1027 CharUnits Adjustment = getVirtualFunctionPrologueThisAdjustment(GD);
1028 if (Adjustment.isZero())
1029 return This;
1030
1031 unsigned AS = cast<llvm::PointerType>(This->getType())->getAddressSpace();
1032 llvm::Type *charPtrTy = CGF.Int8Ty->getPointerTo(AS);
1033 This = CGF.Builder.CreateBitCast(This, charPtrTy);
1034 assert(Adjustment.isPositive());
1035 return CGF.Builder.CreateConstGEP1_32(This, Adjustment.getQuantity());
1036 }
1037
1038 GD = GD.getCanonicalDecl();
1039 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +00001040
Timur Iskhodzhanov82552742013-10-16 18:24:06 +00001041 GlobalDecl LookupGD = GD;
1042 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
1043 // Complete dtors take a pointer to the complete object,
1044 // thus don't need adjustment.
1045 if (GD.getDtorType() == Dtor_Complete)
1046 return This;
1047
1048 // There's only Dtor_Deleting in vftable but it shares the this adjustment
1049 // with the base one, so look up the deleting one instead.
1050 LookupGD = GlobalDecl(DD, Dtor_Deleting);
1051 }
Timur Iskhodzhanov5f0db582013-11-05 15:54:58 +00001052 MicrosoftVTableContext::MethodVFTableLocation ML =
1053 CGM.getMicrosoftVTableContext().getMethodVFTableLocation(LookupGD);
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +00001054
1055 unsigned AS = cast<llvm::PointerType>(This->getType())->getAddressSpace();
1056 llvm::Type *charPtrTy = CGF.Int8Ty->getPointerTo(AS);
Timur Iskhodzhanov40aa3662013-11-07 13:34:02 +00001057 CharUnits StaticOffset = ML.VFPtrOffset;
Stephen Hines651f13c2014-04-23 16:59:28 -07001058
1059 // Base destructors expect 'this' to point to the beginning of the base
1060 // subobject, not the first vfptr that happens to contain the virtual dtor.
1061 // However, we still need to apply the virtual base adjustment.
1062 if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
1063 StaticOffset = CharUnits::Zero();
1064
Timur Iskhodzhanov82552742013-10-16 18:24:06 +00001065 if (ML.VBase) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001066 This = CGF.Builder.CreateBitCast(This, charPtrTy);
1067 llvm::Value *VBaseOffset =
1068 GetVirtualBaseClassOffset(CGF, This, MD->getParent(), ML.VBase);
1069 This = CGF.Builder.CreateInBoundsGEP(This, VBaseOffset);
Timur Iskhodzhanov82552742013-10-16 18:24:06 +00001070 }
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +00001071 if (!StaticOffset.isZero()) {
1072 assert(StaticOffset.isPositive());
1073 This = CGF.Builder.CreateBitCast(This, charPtrTy);
Timur Iskhodzhanov6d87e652013-10-22 18:15:24 +00001074 if (ML.VBase) {
1075 // Non-virtual adjustment might result in a pointer outside the allocated
1076 // object, e.g. if the final overrider class is laid out after the virtual
1077 // base that declares a method in the most derived class.
1078 // FIXME: Update the code that emits this adjustment in thunks prologues.
1079 This = CGF.Builder.CreateConstGEP1_32(This, StaticOffset.getQuantity());
1080 } else {
1081 This = CGF.Builder.CreateConstInBoundsGEP1_32(This,
1082 StaticOffset.getQuantity());
1083 }
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +00001084 }
1085 return This;
1086}
1087
Stephen Hines651f13c2014-04-23 16:59:28 -07001088void MicrosoftCXXABI::addImplicitStructorParams(CodeGenFunction &CGF,
1089 QualType &ResTy,
1090 FunctionArgList &Params) {
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +00001091 ASTContext &Context = getContext();
1092 const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl());
Stephen Hines651f13c2014-04-23 16:59:28 -07001093 assert(isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD));
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +00001094 if (isa<CXXConstructorDecl>(MD) && MD->getParent()->getNumVBases()) {
1095 ImplicitParamDecl *IsMostDerived
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001096 = ImplicitParamDecl::Create(Context, nullptr,
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +00001097 CGF.CurGD.getDecl()->getLocation(),
1098 &Context.Idents.get("is_most_derived"),
1099 Context.IntTy);
Stephen Hines651f13c2014-04-23 16:59:28 -07001100 // The 'most_derived' parameter goes second if the ctor is variadic and last
1101 // if it's not. Dtors can't be variadic.
1102 const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
1103 if (FPT->isVariadic())
1104 Params.insert(Params.begin() + 1, IsMostDerived);
1105 else
1106 Params.push_back(IsMostDerived);
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +00001107 getStructorImplicitParamDecl(CGF) = IsMostDerived;
Stephen Hines176edba2014-12-01 14:53:08 -08001108 } else if (isDeletingDtor(CGF.CurGD)) {
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001109 ImplicitParamDecl *ShouldDelete
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001110 = ImplicitParamDecl::Create(Context, nullptr,
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001111 CGF.CurGD.getDecl()->getLocation(),
1112 &Context.Idents.get("should_call_delete"),
Timur Iskhodzhanov1f71f392013-08-27 10:38:19 +00001113 Context.IntTy);
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001114 Params.push_back(ShouldDelete);
1115 getStructorImplicitParamDecl(CGF) = ShouldDelete;
1116 }
John McCallbd315742012-09-25 08:00:39 +00001117}
1118
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +00001119llvm::Value *MicrosoftCXXABI::adjustThisParameterInVirtualFunctionPrologue(
1120 CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This) {
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +00001121 // In this ABI, every virtual function takes a pointer to one of the
1122 // subobjects that first defines it as the 'this' parameter, rather than a
Stephen Hines651f13c2014-04-23 16:59:28 -07001123 // pointer to the final overrider subobject. Thus, we need to adjust it back
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +00001124 // to the final overrider subobject before use.
1125 // See comments in the MicrosoftVFTableContext implementation for the details.
Stephen Hines651f13c2014-04-23 16:59:28 -07001126 CharUnits Adjustment = getVirtualFunctionPrologueThisAdjustment(GD);
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +00001127 if (Adjustment.isZero())
1128 return This;
1129
1130 unsigned AS = cast<llvm::PointerType>(This->getType())->getAddressSpace();
1131 llvm::Type *charPtrTy = CGF.Int8Ty->getPointerTo(AS),
1132 *thisTy = This->getType();
1133
1134 This = CGF.Builder.CreateBitCast(This, charPtrTy);
1135 assert(Adjustment.isPositive());
Timur Iskhodzhanov6d87e652013-10-22 18:15:24 +00001136 This =
1137 CGF.Builder.CreateConstInBoundsGEP1_32(This, -Adjustment.getQuantity());
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +00001138 return CGF.Builder.CreateBitCast(This, thisTy);
1139}
1140
John McCallbd315742012-09-25 08:00:39 +00001141void MicrosoftCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
1142 EmitThisParam(CGF);
Stephen Lin3b50e8d2013-06-30 20:40:16 +00001143
1144 /// If this is a function that the ABI specifies returns 'this', initialize
1145 /// the return slot to 'this' at the start of the function.
1146 ///
1147 /// Unlike the setting of return types, this is done within the ABI
1148 /// implementation instead of by clients of CGCXXABI because:
1149 /// 1) getThisValue is currently protected
1150 /// 2) in theory, an ABI could implement 'this' returns some other way;
1151 /// HasThisReturn only specifies a contract, not the implementation
1152 if (HasThisReturn(CGF.CurGD))
John McCallbd315742012-09-25 08:00:39 +00001153 CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue);
Stephen Hines176edba2014-12-01 14:53:08 -08001154 else if (hasMostDerivedReturn(CGF.CurGD))
1155 CGF.Builder.CreateStore(CGF.EmitCastToVoidPtr(getThisValue(CGF)),
1156 CGF.ReturnValue);
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +00001157
1158 const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl());
1159 if (isa<CXXConstructorDecl>(MD) && MD->getParent()->getNumVBases()) {
1160 assert(getStructorImplicitParamDecl(CGF) &&
1161 "no implicit parameter for a constructor with virtual bases?");
1162 getStructorImplicitParamValue(CGF)
1163 = CGF.Builder.CreateLoad(
1164 CGF.GetAddrOfLocalVar(getStructorImplicitParamDecl(CGF)),
1165 "is_most_derived");
1166 }
1167
Stephen Hines176edba2014-12-01 14:53:08 -08001168 if (isDeletingDtor(CGF.CurGD)) {
Timur Iskhodzhanov59660c22013-02-13 08:37:51 +00001169 assert(getStructorImplicitParamDecl(CGF) &&
1170 "no implicit parameter for a deleting destructor?");
1171 getStructorImplicitParamValue(CGF)
1172 = CGF.Builder.CreateLoad(
1173 CGF.GetAddrOfLocalVar(getStructorImplicitParamDecl(CGF)),
1174 "should_call_delete");
1175 }
John McCallbd315742012-09-25 08:00:39 +00001176}
1177
Stephen Hines651f13c2014-04-23 16:59:28 -07001178unsigned MicrosoftCXXABI::addImplicitConstructorArgs(
1179 CodeGenFunction &CGF, const CXXConstructorDecl *D, CXXCtorType Type,
1180 bool ForVirtualBase, bool Delegating, CallArgList &Args) {
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +00001181 assert(Type == Ctor_Complete || Type == Ctor_Base);
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +00001182
Stephen Hines651f13c2014-04-23 16:59:28 -07001183 // Check if we need a 'most_derived' parameter.
1184 if (!D->getParent()->getNumVBases())
1185 return 0;
1186
1187 // Add the 'most_derived' argument second if we are variadic or last if not.
1188 const FunctionProtoType *FPT = D->getType()->castAs<FunctionProtoType>();
1189 llvm::Value *MostDerivedArg =
1190 llvm::ConstantInt::get(CGM.Int32Ty, Type == Ctor_Complete);
1191 RValue RV = RValue::get(MostDerivedArg);
1192 if (MostDerivedArg) {
1193 if (FPT->isVariadic())
1194 Args.insert(Args.begin() + 1,
1195 CallArg(RV, getContext().IntTy, /*needscopy=*/false));
1196 else
1197 Args.add(RV, getContext().IntTy);
1198 }
1199
1200 return 1; // Added one arg.
1201}
1202
1203void MicrosoftCXXABI::EmitDestructorCall(CodeGenFunction &CGF,
1204 const CXXDestructorDecl *DD,
1205 CXXDtorType Type, bool ForVirtualBase,
1206 bool Delegating, llvm::Value *This) {
Stephen Hines176edba2014-12-01 14:53:08 -08001207 llvm::Value *Callee = CGM.getAddrOfCXXStructor(DD, getFromDtorType(Type));
Stephen Hines651f13c2014-04-23 16:59:28 -07001208
1209 if (DD->isVirtual()) {
1210 assert(Type != CXXDtorType::Dtor_Deleting &&
1211 "The deleting destructor should only be called via a virtual call");
1212 This = adjustThisArgumentForVirtualFunctionCall(CGF, GlobalDecl(DD, Type),
1213 This, false);
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +00001214 }
1215
Stephen Hines176edba2014-12-01 14:53:08 -08001216 CGF.EmitCXXStructorCall(DD, Callee, ReturnValueSlot(), This,
1217 /*ImplicitParam=*/nullptr,
1218 /*ImplicitParamTy=*/QualType(), nullptr,
1219 getFromDtorType(Type));
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +00001220}
1221
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001222void MicrosoftCXXABI::emitVTableDefinitions(CodeGenVTables &CGVT,
1223 const CXXRecordDecl *RD) {
Timur Iskhodzhanov5f0db582013-11-05 15:54:58 +00001224 MicrosoftVTableContext &VFTContext = CGM.getMicrosoftVTableContext();
Stephen Hines176edba2014-12-01 14:53:08 -08001225 const VPtrInfoVector &VFPtrs = VFTContext.getVFPtrOffsets(RD);
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001226
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001227 for (VPtrInfo *Info : VFPtrs) {
1228 llvm::GlobalVariable *VTable = getAddrOfVTable(RD, Info->FullOffsetInMDC);
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001229 if (VTable->hasInitializer())
1230 continue;
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001231
Stephen Hines176edba2014-12-01 14:53:08 -08001232 llvm::Constant *RTTI = getContext().getLangOpts().RTTIData
1233 ? getMSCompleteObjectLocator(RD, Info)
1234 : nullptr;
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001235
1236 const VTableLayout &VTLayout =
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001237 VFTContext.getVFTableLayout(RD, Info->FullOffsetInMDC);
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001238 llvm::Constant *Init = CGVT.CreateVTableInitializer(
1239 RD, VTLayout.vtable_component_begin(),
1240 VTLayout.getNumVTableComponents(), VTLayout.vtable_thunk_begin(),
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001241 VTLayout.getNumVTableThunks(), RTTI);
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001242
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001243 VTable->setInitializer(Init);
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001244 }
1245}
1246
1247llvm::Value *MicrosoftCXXABI::getVTableAddressPointInStructor(
1248 CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, BaseSubobject Base,
1249 const CXXRecordDecl *NearestVBase, bool &NeedsVirtualOffset) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001250 NeedsVirtualOffset = (NearestVBase != nullptr);
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001251
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001252 (void)getAddrOfVTable(VTableClass, Base.getBaseOffset());
1253 VFTableIdTy ID(VTableClass, Base.getBaseOffset());
1254 llvm::GlobalValue *VTableAddressPoint = VFTablesMap[ID];
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001255 if (!VTableAddressPoint) {
1256 assert(Base.getBase()->getNumVBases() &&
1257 !CGM.getContext().getASTRecordLayout(Base.getBase()).hasOwnVFPtr());
1258 }
1259 return VTableAddressPoint;
1260}
1261
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001262static void mangleVFTableName(MicrosoftMangleContext &MangleContext,
Stephen Hines651f13c2014-04-23 16:59:28 -07001263 const CXXRecordDecl *RD, const VPtrInfo *VFPtr,
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001264 SmallString<256> &Name) {
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001265 llvm::raw_svector_ostream Out(Name);
Stephen Hines651f13c2014-04-23 16:59:28 -07001266 MangleContext.mangleCXXVFTable(RD, VFPtr->MangledPath, Out);
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001267}
1268
1269llvm::Constant *MicrosoftCXXABI::getVTableAddressPointForConstExpr(
1270 BaseSubobject Base, const CXXRecordDecl *VTableClass) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001271 (void)getAddrOfVTable(VTableClass, Base.getBaseOffset());
1272 VFTableIdTy ID(VTableClass, Base.getBaseOffset());
1273 llvm::GlobalValue *VFTable = VFTablesMap[ID];
1274 assert(VFTable && "Couldn't find a vftable for the given base?");
1275 return VFTable;
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001276}
1277
1278llvm::GlobalVariable *MicrosoftCXXABI::getAddrOfVTable(const CXXRecordDecl *RD,
1279 CharUnits VPtrOffset) {
1280 // getAddrOfVTable may return 0 if asked to get an address of a vtable which
1281 // shouldn't be used in the given record type. We want to cache this result in
1282 // VFTablesMap, thus a simple zero check is not sufficient.
1283 VFTableIdTy ID(RD, VPtrOffset);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001284 VTablesMapTy::iterator I;
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001285 bool Inserted;
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001286 std::tie(I, Inserted) = VTablesMap.insert(std::make_pair(ID, nullptr));
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001287 if (!Inserted)
1288 return I->second;
1289
1290 llvm::GlobalVariable *&VTable = I->second;
1291
Timur Iskhodzhanov5f0db582013-11-05 15:54:58 +00001292 MicrosoftVTableContext &VTContext = CGM.getMicrosoftVTableContext();
Stephen Hines651f13c2014-04-23 16:59:28 -07001293 const VPtrInfoVector &VFPtrs = VTContext.getVFPtrOffsets(RD);
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001294
Stephen Hines176edba2014-12-01 14:53:08 -08001295 if (DeferredVFTables.insert(RD).second) {
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001296 // We haven't processed this record type before.
1297 // Queue up this v-table for possible deferred emission.
1298 CGM.addDeferredVTable(RD);
1299
1300#ifndef NDEBUG
1301 // Create all the vftables at once in order to make sure each vftable has
1302 // a unique mangled name.
1303 llvm::StringSet<> ObservedMangledNames;
1304 for (size_t J = 0, F = VFPtrs.size(); J != F; ++J) {
1305 SmallString<256> Name;
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001306 mangleVFTableName(getMangleContext(), RD, VFPtrs[J], Name);
Stephen Hines176edba2014-12-01 14:53:08 -08001307 if (!ObservedMangledNames.insert(Name.str()).second)
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001308 llvm_unreachable("Already saw this mangling before?");
1309 }
1310#endif
1311 }
1312
1313 for (size_t J = 0, F = VFPtrs.size(); J != F; ++J) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001314 if (VFPtrs[J]->FullOffsetInMDC != VPtrOffset)
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001315 continue;
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001316 SmallString<256> VFTableName;
1317 mangleVFTableName(getMangleContext(), RD, VFPtrs[J], VFTableName);
1318 StringRef VTableName = VFTableName;
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001319
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001320 uint64_t NumVTableSlots =
Stephen Hines651f13c2014-04-23 16:59:28 -07001321 VTContext.getVFTableLayout(RD, VFPtrs[J]->FullOffsetInMDC)
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001322 .getNumVTableComponents();
1323 llvm::GlobalValue::LinkageTypes VTableLinkage =
1324 llvm::GlobalValue::ExternalLinkage;
1325 llvm::ArrayType *VTableType =
1326 llvm::ArrayType::get(CGM.Int8PtrTy, NumVTableSlots);
1327 if (getContext().getLangOpts().RTTIData) {
1328 VTableLinkage = llvm::GlobalValue::PrivateLinkage;
1329 VTableName = "";
1330 }
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001331
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001332 VTable = CGM.getModule().getNamedGlobal(VFTableName);
1333 if (!VTable) {
1334 // Create a backing variable for the contents of VTable. The VTable may
1335 // or may not include space for a pointer to RTTI data.
1336 llvm::GlobalValue *VFTable = VTable = new llvm::GlobalVariable(
1337 CGM.getModule(), VTableType, /*isConstant=*/true, VTableLinkage,
1338 /*Initializer=*/nullptr, VTableName);
1339 VTable->setUnnamedAddr(true);
1340
1341 // Only insert a pointer into the VFTable for RTTI data if we are not
1342 // importing it. We never reference the RTTI data directly so there is no
1343 // need to make room for it.
1344 if (getContext().getLangOpts().RTTIData &&
1345 !RD->hasAttr<DLLImportAttr>()) {
1346 llvm::Value *GEPIndices[] = {llvm::ConstantInt::get(CGM.IntTy, 0),
1347 llvm::ConstantInt::get(CGM.IntTy, 1)};
1348 // Create a GEP which points just after the first entry in the VFTable,
1349 // this should be the location of the first virtual method.
1350 llvm::Constant *VTableGEP =
1351 llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, GEPIndices);
1352 // The symbol for the VFTable is an alias to the GEP. It is
1353 // transparent, to other modules, what the nature of this symbol is; all
1354 // that matters is that the alias be the address of the first virtual
1355 // method.
1356 VFTable = llvm::GlobalAlias::create(
1357 cast<llvm::SequentialType>(VTableGEP->getType())->getElementType(),
1358 /*AddressSpace=*/0, llvm::GlobalValue::ExternalLinkage,
1359 VFTableName.str(), VTableGEP, &CGM.getModule());
1360 } else {
1361 // We don't need a GlobalAlias to be a symbol for the VTable if we won't
1362 // be referencing any RTTI data. The GlobalVariable will end up being
1363 // an appropriate definition of the VFTable.
1364 VTable->setName(VFTableName.str());
1365 }
1366
1367 VFTable->setUnnamedAddr(true);
1368 if (RD->hasAttr<DLLImportAttr>())
1369 VFTable->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
1370 else if (RD->hasAttr<DLLExportAttr>())
1371 VFTable->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
1372
1373 llvm::GlobalValue::LinkageTypes VFTableLinkage = CGM.getVTableLinkage(RD);
1374 if (VFTable != VTable) {
1375 if (llvm::GlobalValue::isAvailableExternallyLinkage(VFTableLinkage)) {
1376 // AvailableExternally implies that we grabbed the data from another
1377 // executable. No need to stick the alias in a Comdat.
Stephen Hines176edba2014-12-01 14:53:08 -08001378 } else if (llvm::GlobalValue::isInternalLinkage(VFTableLinkage) ||
1379 llvm::GlobalValue::isWeakODRLinkage(VFTableLinkage) ||
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001380 llvm::GlobalValue::isLinkOnceODRLinkage(VFTableLinkage)) {
1381 // The alias is going to be dropped into a Comdat, no need to make it
1382 // weak.
Stephen Hines176edba2014-12-01 14:53:08 -08001383 if (!llvm::GlobalValue::isInternalLinkage(VFTableLinkage))
1384 VFTableLinkage = llvm::GlobalValue::ExternalLinkage;
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001385 llvm::Comdat *C =
1386 CGM.getModule().getOrInsertComdat(VFTable->getName());
1387 // We must indicate which VFTable is larger to support linking between
1388 // translation units which do and do not have RTTI data. The largest
1389 // VFTable contains the RTTI data; translation units which reference
1390 // the smaller VFTable always reference it relative to the first
1391 // virtual method.
1392 C->setSelectionKind(llvm::Comdat::Largest);
1393 VTable->setComdat(C);
1394 } else {
1395 llvm_unreachable("unexpected linkage for vftable!");
1396 }
1397 }
1398 VFTable->setLinkage(VFTableLinkage);
1399 CGM.setGlobalVisibility(VFTable, RD);
1400 VFTablesMap[ID] = VFTable;
1401 }
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001402 break;
1403 }
1404
1405 return VTable;
1406}
1407
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +00001408llvm::Value *MicrosoftCXXABI::getVirtualFunctionPointer(CodeGenFunction &CGF,
1409 GlobalDecl GD,
1410 llvm::Value *This,
1411 llvm::Type *Ty) {
1412 GD = GD.getCanonicalDecl();
1413 CGBuilderTy &Builder = CGF.Builder;
1414
1415 Ty = Ty->getPointerTo()->getPointerTo();
Stephen Hines651f13c2014-04-23 16:59:28 -07001416 llvm::Value *VPtr =
1417 adjustThisArgumentForVirtualFunctionCall(CGF, GD, This, true);
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +00001418 llvm::Value *VTable = CGF.GetVTablePtr(VPtr, Ty);
1419
Timur Iskhodzhanov5f0db582013-11-05 15:54:58 +00001420 MicrosoftVTableContext::MethodVFTableLocation ML =
1421 CGM.getMicrosoftVTableContext().getMethodVFTableLocation(GD);
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +00001422 llvm::Value *VFuncPtr =
1423 Builder.CreateConstInBoundsGEP1_64(VTable, ML.Index, "vfn");
1424 return Builder.CreateLoad(VFuncPtr);
1425}
1426
Stephen Hines176edba2014-12-01 14:53:08 -08001427llvm::Value *MicrosoftCXXABI::EmitVirtualDestructorCall(
1428 CodeGenFunction &CGF, const CXXDestructorDecl *Dtor, CXXDtorType DtorType,
1429 llvm::Value *This, const CXXMemberCallExpr *CE) {
1430 assert(CE == nullptr || CE->arg_begin() == CE->arg_end());
Timur Iskhodzhanov0f9827f2013-02-15 14:45:22 +00001431 assert(DtorType == Dtor_Deleting || DtorType == Dtor_Complete);
1432
1433 // We have only one destructor in the vftable but can get both behaviors
Timur Iskhodzhanov1f71f392013-08-27 10:38:19 +00001434 // by passing an implicit int parameter.
Timur Iskhodzhanov82552742013-10-16 18:24:06 +00001435 GlobalDecl GD(Dtor, Dtor_Deleting);
Stephen Hines176edba2014-12-01 14:53:08 -08001436 const CGFunctionInfo *FInfo = &CGM.getTypes().arrangeCXXStructorDeclaration(
1437 Dtor, StructorType::Deleting);
Timur Iskhodzhanov0f9827f2013-02-15 14:45:22 +00001438 llvm::Type *Ty = CGF.CGM.getTypes().GetFunctionType(*FInfo);
Timur Iskhodzhanov82552742013-10-16 18:24:06 +00001439 llvm::Value *Callee = getVirtualFunctionPointer(CGF, GD, This, Ty);
Timur Iskhodzhanov0f9827f2013-02-15 14:45:22 +00001440
1441 ASTContext &Context = CGF.getContext();
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +00001442 llvm::Value *ImplicitParam =
Timur Iskhodzhanov1f71f392013-08-27 10:38:19 +00001443 llvm::ConstantInt::get(llvm::IntegerType::getInt32Ty(CGF.getLLVMContext()),
Timur Iskhodzhanov0f9827f2013-02-15 14:45:22 +00001444 DtorType == Dtor_Deleting);
1445
Stephen Hines651f13c2014-04-23 16:59:28 -07001446 This = adjustThisArgumentForVirtualFunctionCall(CGF, GD, This, true);
Stephen Hines176edba2014-12-01 14:53:08 -08001447 RValue RV = CGF.EmitCXXStructorCall(Dtor, Callee, ReturnValueSlot(), This,
1448 ImplicitParam, Context.IntTy, CE,
1449 StructorType::Deleting);
1450 return RV.getScalarVal();
Timur Iskhodzhanov0f9827f2013-02-15 14:45:22 +00001451}
1452
Stephen Hines651f13c2014-04-23 16:59:28 -07001453const VBTableGlobals &
1454MicrosoftCXXABI::enumerateVBTables(const CXXRecordDecl *RD) {
Reid Kleckner90633022013-06-19 15:20:38 +00001455 // At this layer, we can key the cache off of a single class, which is much
Stephen Hines651f13c2014-04-23 16:59:28 -07001456 // easier than caching each vbtable individually.
1457 llvm::DenseMap<const CXXRecordDecl*, VBTableGlobals>::iterator Entry;
1458 bool Added;
1459 std::tie(Entry, Added) =
1460 VBTablesMap.insert(std::make_pair(RD, VBTableGlobals()));
1461 VBTableGlobals &VBGlobals = Entry->second;
1462 if (!Added)
1463 return VBGlobals;
Reid Kleckner90633022013-06-19 15:20:38 +00001464
Stephen Hines651f13c2014-04-23 16:59:28 -07001465 MicrosoftVTableContext &Context = CGM.getMicrosoftVTableContext();
1466 VBGlobals.VBTables = &Context.enumerateVBTables(RD);
Reid Kleckner90633022013-06-19 15:20:38 +00001467
Stephen Hines651f13c2014-04-23 16:59:28 -07001468 // Cache the globals for all vbtables so we don't have to recompute the
1469 // mangled names.
1470 llvm::GlobalVariable::LinkageTypes Linkage = CGM.getVTableLinkage(RD);
1471 for (VPtrInfoVector::const_iterator I = VBGlobals.VBTables->begin(),
1472 E = VBGlobals.VBTables->end();
1473 I != E; ++I) {
1474 VBGlobals.Globals.push_back(getAddrOfVBTable(**I, RD, Linkage));
1475 }
1476
1477 return VBGlobals;
Reid Kleckner90633022013-06-19 15:20:38 +00001478}
1479
Stephen Hines651f13c2014-04-23 16:59:28 -07001480llvm::Function *MicrosoftCXXABI::EmitVirtualMemPtrThunk(
1481 const CXXMethodDecl *MD,
1482 const MicrosoftVTableContext::MethodVFTableLocation &ML) {
Stephen Hines176edba2014-12-01 14:53:08 -08001483 assert(!isa<CXXConstructorDecl>(MD) && !isa<CXXDestructorDecl>(MD) &&
1484 "can't form pointers to ctors or virtual dtors");
1485
Stephen Hines651f13c2014-04-23 16:59:28 -07001486 // Calculate the mangled name.
1487 SmallString<256> ThunkName;
1488 llvm::raw_svector_ostream Out(ThunkName);
1489 getMangleContext().mangleVirtualMemPtrThunk(MD, Out);
1490 Out.flush();
1491
Hans Wennborg93b717a2013-11-15 17:24:45 +00001492 // If the thunk has been generated previously, just return it.
1493 if (llvm::GlobalValue *GV = CGM.getModule().getNamedValue(ThunkName))
1494 return cast<llvm::Function>(GV);
1495
1496 // Create the llvm::Function.
Stephen Hines176edba2014-12-01 14:53:08 -08001497 const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeMSMemberPointerThunk(MD);
Hans Wennborg93b717a2013-11-15 17:24:45 +00001498 llvm::FunctionType *ThunkTy = CGM.getTypes().GetFunctionType(FnInfo);
1499 llvm::Function *ThunkFn =
1500 llvm::Function::Create(ThunkTy, llvm::Function::ExternalLinkage,
1501 ThunkName.str(), &CGM.getModule());
1502 assert(ThunkFn->getName() == ThunkName && "name was uniqued!");
1503
Hans Wennborg93b717a2013-11-15 17:24:45 +00001504 ThunkFn->setLinkage(MD->isExternallyVisible()
1505 ? llvm::GlobalValue::LinkOnceODRLinkage
1506 : llvm::GlobalValue::InternalLinkage);
1507
1508 CGM.SetLLVMFunctionAttributes(MD, FnInfo, ThunkFn);
1509 CGM.SetLLVMFunctionAttributesForDefinition(MD, ThunkFn);
1510
Stephen Hines176edba2014-12-01 14:53:08 -08001511 // These thunks can be compared, so they are not unnamed.
1512 ThunkFn->setUnnamedAddr(false);
1513
Hans Wennborg93b717a2013-11-15 17:24:45 +00001514 // Start codegen.
1515 CodeGenFunction CGF(CGM);
Stephen Hines176edba2014-12-01 14:53:08 -08001516 CGF.CurGD = GlobalDecl(MD);
1517 CGF.CurFuncIsThunk = true;
1518
1519 // Build FunctionArgs, but only include the implicit 'this' parameter
1520 // declaration.
1521 FunctionArgList FunctionArgs;
1522 buildThisParam(CGF, FunctionArgs);
1523
1524 // Start defining the function.
1525 CGF.StartFunction(GlobalDecl(), FnInfo.getReturnType(), ThunkFn, FnInfo,
1526 FunctionArgs, MD->getLocation(), SourceLocation());
1527 EmitThisParam(CGF);
Hans Wennborg93b717a2013-11-15 17:24:45 +00001528
Stephen Hines651f13c2014-04-23 16:59:28 -07001529 // Load the vfptr and then callee from the vftable. The callee should have
1530 // adjusted 'this' so that the vfptr is at offset zero.
Stephen Hines176edba2014-12-01 14:53:08 -08001531 llvm::Value *VTable = CGF.GetVTablePtr(
1532 getThisValue(CGF), ThunkTy->getPointerTo()->getPointerTo());
Stephen Hines651f13c2014-04-23 16:59:28 -07001533 llvm::Value *VFuncPtr =
1534 CGF.Builder.CreateConstInBoundsGEP1_64(VTable, ML.Index, "vfn");
1535 llvm::Value *Callee = CGF.Builder.CreateLoad(VFuncPtr);
Hans Wennborg93b717a2013-11-15 17:24:45 +00001536
Stephen Hines176edba2014-12-01 14:53:08 -08001537 CGF.EmitMustTailThunk(MD, getThisValue(CGF), Callee);
Hans Wennborg93b717a2013-11-15 17:24:45 +00001538
1539 return ThunkFn;
1540}
1541
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001542void MicrosoftCXXABI::emitVirtualInheritanceTables(const CXXRecordDecl *RD) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001543 const VBTableGlobals &VBGlobals = enumerateVBTables(RD);
1544 for (unsigned I = 0, E = VBGlobals.VBTables->size(); I != E; ++I) {
1545 const VPtrInfo *VBT = (*VBGlobals.VBTables)[I];
1546 llvm::GlobalVariable *GV = VBGlobals.Globals[I];
1547 emitVBTableDefinition(*VBT, RD, GV);
Reid Kleckner90633022013-06-19 15:20:38 +00001548 }
1549}
1550
Stephen Hines651f13c2014-04-23 16:59:28 -07001551llvm::GlobalVariable *
1552MicrosoftCXXABI::getAddrOfVBTable(const VPtrInfo &VBT, const CXXRecordDecl *RD,
1553 llvm::GlobalVariable::LinkageTypes Linkage) {
1554 SmallString<256> OutName;
1555 llvm::raw_svector_ostream Out(OutName);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001556 getMangleContext().mangleCXXVBTable(RD, VBT.MangledPath, Out);
Stephen Hines651f13c2014-04-23 16:59:28 -07001557 Out.flush();
1558 StringRef Name = OutName.str();
1559
1560 llvm::ArrayType *VBTableType =
1561 llvm::ArrayType::get(CGM.IntTy, 1 + VBT.ReusingBase->getNumVBases());
1562
1563 assert(!CGM.getModule().getNamedGlobal(Name) &&
1564 "vbtable with this name already exists: mangling bug?");
1565 llvm::GlobalVariable *GV =
1566 CGM.CreateOrReplaceCXXRuntimeVariable(Name, VBTableType, Linkage);
1567 GV->setUnnamedAddr(true);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001568
1569 if (RD->hasAttr<DLLImportAttr>())
1570 GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
1571 else if (RD->hasAttr<DLLExportAttr>())
1572 GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
1573
Stephen Hines651f13c2014-04-23 16:59:28 -07001574 return GV;
1575}
1576
1577void MicrosoftCXXABI::emitVBTableDefinition(const VPtrInfo &VBT,
1578 const CXXRecordDecl *RD,
1579 llvm::GlobalVariable *GV) const {
1580 const CXXRecordDecl *ReusingBase = VBT.ReusingBase;
1581
1582 assert(RD->getNumVBases() && ReusingBase->getNumVBases() &&
1583 "should only emit vbtables for classes with vbtables");
1584
1585 const ASTRecordLayout &BaseLayout =
1586 CGM.getContext().getASTRecordLayout(VBT.BaseWithVPtr);
1587 const ASTRecordLayout &DerivedLayout =
1588 CGM.getContext().getASTRecordLayout(RD);
1589
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001590 SmallVector<llvm::Constant *, 4> Offsets(1 + ReusingBase->getNumVBases(),
1591 nullptr);
Stephen Hines651f13c2014-04-23 16:59:28 -07001592
1593 // The offset from ReusingBase's vbptr to itself always leads.
1594 CharUnits VBPtrOffset = BaseLayout.getVBPtrOffset();
1595 Offsets[0] = llvm::ConstantInt::get(CGM.IntTy, -VBPtrOffset.getQuantity());
1596
1597 MicrosoftVTableContext &Context = CGM.getMicrosoftVTableContext();
1598 for (const auto &I : ReusingBase->vbases()) {
1599 const CXXRecordDecl *VBase = I.getType()->getAsCXXRecordDecl();
1600 CharUnits Offset = DerivedLayout.getVBaseClassOffset(VBase);
1601 assert(!Offset.isNegative());
1602
1603 // Make it relative to the subobject vbptr.
1604 CharUnits CompleteVBPtrOffset = VBT.NonVirtualOffset + VBPtrOffset;
1605 if (VBT.getVBaseWithVPtr())
1606 CompleteVBPtrOffset +=
1607 DerivedLayout.getVBaseClassOffset(VBT.getVBaseWithVPtr());
1608 Offset -= CompleteVBPtrOffset;
1609
1610 unsigned VBIndex = Context.getVBTableIndex(ReusingBase, VBase);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001611 assert(Offsets[VBIndex] == nullptr && "The same vbindex seen twice?");
Stephen Hines651f13c2014-04-23 16:59:28 -07001612 Offsets[VBIndex] = llvm::ConstantInt::get(CGM.IntTy, Offset.getQuantity());
1613 }
1614
1615 assert(Offsets.size() ==
1616 cast<llvm::ArrayType>(cast<llvm::PointerType>(GV->getType())
1617 ->getElementType())->getNumElements());
1618 llvm::ArrayType *VBTableType =
1619 llvm::ArrayType::get(CGM.IntTy, Offsets.size());
1620 llvm::Constant *Init = llvm::ConstantArray::get(VBTableType, Offsets);
1621 GV->setInitializer(Init);
1622
1623 // Set the right visibility.
1624 CGM.setGlobalVisibility(GV, RD);
1625}
1626
Timur Iskhodzhanovc70cc5d2013-10-30 11:55:43 +00001627llvm::Value *MicrosoftCXXABI::performThisAdjustment(CodeGenFunction &CGF,
1628 llvm::Value *This,
1629 const ThisAdjustment &TA) {
1630 if (TA.isEmpty())
1631 return This;
1632
1633 llvm::Value *V = CGF.Builder.CreateBitCast(This, CGF.Int8PtrTy);
1634
Timur Iskhodzhanov58b6db72013-11-06 06:24:31 +00001635 if (!TA.Virtual.isEmpty()) {
1636 assert(TA.Virtual.Microsoft.VtordispOffset < 0);
1637 // Adjust the this argument based on the vtordisp value.
1638 llvm::Value *VtorDispPtr =
1639 CGF.Builder.CreateConstGEP1_32(V, TA.Virtual.Microsoft.VtordispOffset);
1640 VtorDispPtr =
1641 CGF.Builder.CreateBitCast(VtorDispPtr, CGF.Int32Ty->getPointerTo());
1642 llvm::Value *VtorDisp = CGF.Builder.CreateLoad(VtorDispPtr, "vtordisp");
1643 V = CGF.Builder.CreateGEP(V, CGF.Builder.CreateNeg(VtorDisp));
1644
1645 if (TA.Virtual.Microsoft.VBPtrOffset) {
1646 // If the final overrider is defined in a virtual base other than the one
1647 // that holds the vfptr, we have to use a vtordispex thunk which looks up
1648 // the vbtable of the derived class.
1649 assert(TA.Virtual.Microsoft.VBPtrOffset > 0);
1650 assert(TA.Virtual.Microsoft.VBOffsetOffset >= 0);
1651 llvm::Value *VBPtr;
1652 llvm::Value *VBaseOffset =
1653 GetVBaseOffsetFromVBPtr(CGF, V, -TA.Virtual.Microsoft.VBPtrOffset,
1654 TA.Virtual.Microsoft.VBOffsetOffset, &VBPtr);
1655 V = CGF.Builder.CreateInBoundsGEP(VBPtr, VBaseOffset);
1656 }
1657 }
Timur Iskhodzhanovc70cc5d2013-10-30 11:55:43 +00001658
1659 if (TA.NonVirtual) {
1660 // Non-virtual adjustment might result in a pointer outside the allocated
1661 // object, e.g. if the final overrider class is laid out after the virtual
1662 // base that declares a method in the most derived class.
1663 V = CGF.Builder.CreateConstGEP1_32(V, TA.NonVirtual);
1664 }
1665
1666 // Don't need to bitcast back, the call CodeGen will handle this.
1667 return V;
1668}
1669
1670llvm::Value *
1671MicrosoftCXXABI::performReturnAdjustment(CodeGenFunction &CGF, llvm::Value *Ret,
1672 const ReturnAdjustment &RA) {
1673 if (RA.isEmpty())
1674 return Ret;
1675
1676 llvm::Value *V = CGF.Builder.CreateBitCast(Ret, CGF.Int8PtrTy);
1677
1678 if (RA.Virtual.Microsoft.VBIndex) {
1679 assert(RA.Virtual.Microsoft.VBIndex > 0);
1680 int32_t IntSize =
1681 getContext().getTypeSizeInChars(getContext().IntTy).getQuantity();
1682 llvm::Value *VBPtr;
1683 llvm::Value *VBaseOffset =
1684 GetVBaseOffsetFromVBPtr(CGF, V, RA.Virtual.Microsoft.VBPtrOffset,
1685 IntSize * RA.Virtual.Microsoft.VBIndex, &VBPtr);
1686 V = CGF.Builder.CreateInBoundsGEP(VBPtr, VBaseOffset);
1687 }
1688
1689 if (RA.NonVirtual)
1690 V = CGF.Builder.CreateConstInBoundsGEP1_32(V, RA.NonVirtual);
1691
1692 // Cast back to the original type.
1693 return CGF.Builder.CreateBitCast(V, Ret->getType());
1694}
1695
John McCalle2b45e22012-05-01 05:23:51 +00001696bool MicrosoftCXXABI::requiresArrayCookie(const CXXDeleteExpr *expr,
1697 QualType elementType) {
1698 // Microsoft seems to completely ignore the possibility of a
1699 // two-argument usual deallocation function.
1700 return elementType.isDestructedType();
1701}
1702
1703bool MicrosoftCXXABI::requiresArrayCookie(const CXXNewExpr *expr) {
1704 // Microsoft seems to completely ignore the possibility of a
1705 // two-argument usual deallocation function.
1706 return expr->getAllocatedType().isDestructedType();
1707}
1708
1709CharUnits MicrosoftCXXABI::getArrayCookieSizeImpl(QualType type) {
1710 // The array cookie is always a size_t; we then pad that out to the
1711 // alignment of the element type.
1712 ASTContext &Ctx = getContext();
1713 return std::max(Ctx.getTypeSizeInChars(Ctx.getSizeType()),
1714 Ctx.getTypeAlignInChars(type));
1715}
1716
1717llvm::Value *MicrosoftCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
1718 llvm::Value *allocPtr,
1719 CharUnits cookieSize) {
Micah Villmow956a5a12012-10-25 15:39:14 +00001720 unsigned AS = allocPtr->getType()->getPointerAddressSpace();
John McCalle2b45e22012-05-01 05:23:51 +00001721 llvm::Value *numElementsPtr =
1722 CGF.Builder.CreateBitCast(allocPtr, CGF.SizeTy->getPointerTo(AS));
1723 return CGF.Builder.CreateLoad(numElementsPtr);
1724}
1725
1726llvm::Value* MicrosoftCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
1727 llvm::Value *newPtr,
1728 llvm::Value *numElements,
1729 const CXXNewExpr *expr,
1730 QualType elementType) {
1731 assert(requiresArrayCookie(expr));
1732
1733 // The size of the cookie.
1734 CharUnits cookieSize = getArrayCookieSizeImpl(elementType);
1735
1736 // Compute an offset to the cookie.
1737 llvm::Value *cookiePtr = newPtr;
1738
1739 // Write the number of elements into the appropriate slot.
Micah Villmow956a5a12012-10-25 15:39:14 +00001740 unsigned AS = newPtr->getType()->getPointerAddressSpace();
John McCalle2b45e22012-05-01 05:23:51 +00001741 llvm::Value *numElementsPtr
1742 = CGF.Builder.CreateBitCast(cookiePtr, CGF.SizeTy->getPointerTo(AS));
1743 CGF.Builder.CreateStore(numElements, numElementsPtr);
1744
1745 // Finally, compute a pointer to the actual data buffer by skipping
1746 // over the cookie completely.
1747 return CGF.Builder.CreateConstInBoundsGEP1_64(newPtr,
1748 cookieSize.getQuantity());
1749}
1750
Stephen Hines176edba2014-12-01 14:53:08 -08001751static void emitGlobalDtorWithTLRegDtor(CodeGenFunction &CGF, const VarDecl &VD,
1752 llvm::Constant *Dtor,
1753 llvm::Constant *Addr) {
1754 // Create a function which calls the destructor.
1755 llvm::Constant *DtorStub = CGF.createAtExitStub(VD, Dtor, Addr);
1756
1757 // extern "C" int __tlregdtor(void (*f)(void));
1758 llvm::FunctionType *TLRegDtorTy = llvm::FunctionType::get(
1759 CGF.IntTy, DtorStub->getType(), /*IsVarArg=*/false);
1760
1761 llvm::Constant *TLRegDtor =
1762 CGF.CGM.CreateRuntimeFunction(TLRegDtorTy, "__tlregdtor");
1763 if (llvm::Function *TLRegDtorFn = dyn_cast<llvm::Function>(TLRegDtor))
1764 TLRegDtorFn->setDoesNotThrow();
1765
1766 CGF.EmitNounwindRuntimeCall(TLRegDtor, DtorStub);
1767}
1768
1769void MicrosoftCXXABI::registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
1770 llvm::Constant *Dtor,
1771 llvm::Constant *Addr) {
1772 if (D.getTLSKind())
1773 return emitGlobalDtorWithTLRegDtor(CGF, D, Dtor, Addr);
1774
1775 // The default behavior is to use atexit.
1776 CGF.registerGlobalDtorWithAtExit(D, Dtor, Addr);
1777}
1778
1779void MicrosoftCXXABI::EmitThreadLocalInitFuncs(
1780 CodeGenModule &CGM,
1781 ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *>>
1782 CXXThreadLocals,
1783 ArrayRef<llvm::Function *> CXXThreadLocalInits,
1784 ArrayRef<llvm::GlobalVariable *> CXXThreadLocalInitVars) {
1785 // This will create a GV in the .CRT$XDU section. It will point to our
1786 // initialization function. The CRT will call all of these function
1787 // pointers at start-up time and, eventually, at thread-creation time.
1788 auto AddToXDU = [&CGM](llvm::Function *InitFunc) {
1789 llvm::GlobalVariable *InitFuncPtr = new llvm::GlobalVariable(
1790 CGM.getModule(), InitFunc->getType(), /*IsConstant=*/true,
1791 llvm::GlobalVariable::InternalLinkage, InitFunc,
1792 Twine(InitFunc->getName(), "$initializer$"));
1793 InitFuncPtr->setSection(".CRT$XDU");
1794 // This variable has discardable linkage, we have to add it to @llvm.used to
1795 // ensure it won't get discarded.
1796 CGM.addUsedGlobal(InitFuncPtr);
1797 return InitFuncPtr;
1798 };
1799
1800 std::vector<llvm::Function *> NonComdatInits;
1801 for (size_t I = 0, E = CXXThreadLocalInitVars.size(); I != E; ++I) {
1802 llvm::GlobalVariable *GV = CXXThreadLocalInitVars[I];
1803 llvm::Function *F = CXXThreadLocalInits[I];
1804
1805 // If the GV is already in a comdat group, then we have to join it.
1806 llvm::Comdat *C = GV->getComdat();
1807
1808 // LinkOnce and Weak linkage are lowered down to a single-member comdat
1809 // group.
1810 // Make an explicit group so we can join it.
1811 if (!C && (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage())) {
1812 C = CGM.getModule().getOrInsertComdat(GV->getName());
1813 GV->setComdat(C);
1814 AddToXDU(F)->setComdat(C);
1815 } else {
1816 NonComdatInits.push_back(F);
1817 }
1818 }
1819
1820 if (!NonComdatInits.empty()) {
1821 llvm::FunctionType *FTy =
1822 llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
1823 llvm::Function *InitFunc = CGM.CreateGlobalInitOrDestructFunction(
1824 FTy, "__tls_init", SourceLocation(),
1825 /*TLS=*/true);
1826 CodeGenFunction(CGM).GenerateCXXGlobalInitFunc(InitFunc, NonComdatInits);
1827
1828 AddToXDU(InitFunc);
1829 }
1830}
1831
1832LValue MicrosoftCXXABI::EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF,
1833 const VarDecl *VD,
1834 QualType LValType) {
1835 CGF.CGM.ErrorUnsupported(VD, "thread wrappers");
1836 return LValue();
1837}
1838
John McCall20bb1752012-05-01 06:13:13 +00001839void MicrosoftCXXABI::EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
Reid Kleckner942f9fe2013-09-10 20:14:30 +00001840 llvm::GlobalVariable *GV,
John McCall20bb1752012-05-01 06:13:13 +00001841 bool PerformInit) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001842 // MSVC only uses guards for static locals.
1843 if (!D.isStaticLocal()) {
1844 assert(GV->hasWeakLinkage() || GV->hasLinkOnceLinkage());
1845 // GlobalOpt is allowed to discard the initializer, so use linkonce_odr.
1846 CGF.CurFn->setLinkage(llvm::GlobalValue::LinkOnceODRLinkage);
1847 CGF.EmitCXXGlobalVarDeclInit(D, GV, PerformInit);
1848 return;
1849 }
1850
Reid Kleckner942f9fe2013-09-10 20:14:30 +00001851 // MSVC always uses an i32 bitfield to guard initialization, which is *not*
1852 // threadsafe. Since the user may be linking in inline functions compiled by
1853 // cl.exe, there's no reason to provide a false sense of security by using
1854 // critical sections here.
John McCall20bb1752012-05-01 06:13:13 +00001855
Richard Smith04e51762013-04-14 23:01:42 +00001856 if (D.getTLSKind())
1857 CGM.ErrorUnsupported(&D, "dynamic TLS initialization");
1858
Reid Kleckner942f9fe2013-09-10 20:14:30 +00001859 CGBuilderTy &Builder = CGF.Builder;
1860 llvm::IntegerType *GuardTy = CGF.Int32Ty;
1861 llvm::ConstantInt *Zero = llvm::ConstantInt::get(GuardTy, 0);
1862
1863 // Get the guard variable for this function if we have one already.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001864 GuardInfo *GI = &GuardVariableMap[D.getDeclContext()];
Reid Kleckner942f9fe2013-09-10 20:14:30 +00001865
1866 unsigned BitIndex;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001867 if (D.isStaticLocal() && D.isExternallyVisible()) {
Reid Kleckner942f9fe2013-09-10 20:14:30 +00001868 // Externally visible variables have to be numbered in Sema to properly
1869 // handle unreachable VarDecls.
Stephen Hines651f13c2014-04-23 16:59:28 -07001870 BitIndex = getContext().getStaticLocalNumber(&D);
Reid Kleckner942f9fe2013-09-10 20:14:30 +00001871 assert(BitIndex > 0);
1872 BitIndex--;
1873 } else {
1874 // Non-externally visible variables are numbered here in CodeGen.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001875 BitIndex = GI->BitIndex++;
Reid Kleckner942f9fe2013-09-10 20:14:30 +00001876 }
1877
1878 if (BitIndex >= 32) {
1879 if (D.isExternallyVisible())
1880 ErrorUnsupportedABI(CGF, "more than 32 guarded initializations");
1881 BitIndex %= 32;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001882 GI->Guard = nullptr;
Reid Kleckner942f9fe2013-09-10 20:14:30 +00001883 }
1884
1885 // Lazily create the i32 bitfield for this function.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001886 if (!GI->Guard) {
Reid Kleckner942f9fe2013-09-10 20:14:30 +00001887 // Mangle the name for the guard.
1888 SmallString<256> GuardName;
1889 {
1890 llvm::raw_svector_ostream Out(GuardName);
1891 getMangleContext().mangleStaticGuardVariable(&D, Out);
1892 Out.flush();
1893 }
1894
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001895 // Create the guard variable with a zero-initializer. Just absorb linkage,
1896 // visibility and dll storage class from the guarded variable.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001897 GI->Guard =
1898 new llvm::GlobalVariable(CGM.getModule(), GuardTy, false,
1899 GV->getLinkage(), Zero, GuardName.str());
1900 GI->Guard->setVisibility(GV->getVisibility());
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001901 GI->Guard->setDLLStorageClass(GV->getDLLStorageClass());
Reid Kleckner942f9fe2013-09-10 20:14:30 +00001902 } else {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001903 assert(GI->Guard->getLinkage() == GV->getLinkage() &&
Reid Kleckner942f9fe2013-09-10 20:14:30 +00001904 "static local from the same function had different linkage");
1905 }
1906
1907 // Pseudo code for the test:
1908 // if (!(GuardVar & MyGuardBit)) {
1909 // GuardVar |= MyGuardBit;
1910 // ... initialize the object ...;
1911 // }
1912
1913 // Test our bit from the guard variable.
1914 llvm::ConstantInt *Bit = llvm::ConstantInt::get(GuardTy, 1U << BitIndex);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001915 llvm::LoadInst *LI = Builder.CreateLoad(GI->Guard);
Reid Kleckner942f9fe2013-09-10 20:14:30 +00001916 llvm::Value *IsInitialized =
1917 Builder.CreateICmpNE(Builder.CreateAnd(LI, Bit), Zero);
1918 llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init");
1919 llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end");
1920 Builder.CreateCondBr(IsInitialized, EndBlock, InitBlock);
1921
1922 // Set our bit in the guard variable and emit the initializer and add a global
1923 // destructor if appropriate.
1924 CGF.EmitBlock(InitBlock);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001925 Builder.CreateStore(Builder.CreateOr(LI, Bit), GI->Guard);
Reid Kleckner942f9fe2013-09-10 20:14:30 +00001926 CGF.EmitCXXGlobalVarDeclInit(D, GV, PerformInit);
1927 Builder.CreateBr(EndBlock);
1928
1929 // Continue.
1930 CGF.EmitBlock(EndBlock);
John McCall20bb1752012-05-01 06:13:13 +00001931}
1932
Reid Klecknera3609b02013-04-11 18:13:19 +00001933bool MicrosoftCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
1934 // Null-ness for function memptrs only depends on the first field, which is
1935 // the function pointer. The rest don't matter, so we can zero initialize.
1936 if (MPT->isMemberFunctionPointer())
1937 return true;
1938
1939 // The virtual base adjustment field is always -1 for null, so if we have one
1940 // we can't zero initialize. The field offset is sometimes also -1 if 0 is a
1941 // valid field offset.
Stephen Hines651f13c2014-04-23 16:59:28 -07001942 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
1943 MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel();
1944 return (!MSInheritanceAttr::hasVBTableOffsetField(Inheritance) &&
1945 RD->nullFieldOffsetIsZero());
Reid Klecknera3609b02013-04-11 18:13:19 +00001946}
1947
1948llvm::Type *
1949MicrosoftCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001950 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
1951 MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel();
Reid Klecknera3609b02013-04-11 18:13:19 +00001952 llvm::SmallVector<llvm::Type *, 4> fields;
1953 if (MPT->isMemberFunctionPointer())
1954 fields.push_back(CGM.VoidPtrTy); // FunctionPointerOrVirtualThunk
1955 else
1956 fields.push_back(CGM.IntTy); // FieldOffset
1957
Stephen Hines651f13c2014-04-23 16:59:28 -07001958 if (MSInheritanceAttr::hasNVOffsetField(MPT->isMemberFunctionPointer(),
1959 Inheritance))
Reid Klecknera3609b02013-04-11 18:13:19 +00001960 fields.push_back(CGM.IntTy);
Stephen Hines651f13c2014-04-23 16:59:28 -07001961 if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance))
Reid Klecknera3609b02013-04-11 18:13:19 +00001962 fields.push_back(CGM.IntTy);
Stephen Hines651f13c2014-04-23 16:59:28 -07001963 if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance))
Reid Klecknera3609b02013-04-11 18:13:19 +00001964 fields.push_back(CGM.IntTy); // VirtualBaseAdjustmentOffset
1965
1966 if (fields.size() == 1)
1967 return fields[0];
1968 return llvm::StructType::get(CGM.getLLVMContext(), fields);
1969}
1970
1971void MicrosoftCXXABI::
1972GetNullMemberPointerFields(const MemberPointerType *MPT,
1973 llvm::SmallVectorImpl<llvm::Constant *> &fields) {
1974 assert(fields.empty());
Stephen Hines651f13c2014-04-23 16:59:28 -07001975 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
1976 MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel();
Reid Klecknera3609b02013-04-11 18:13:19 +00001977 if (MPT->isMemberFunctionPointer()) {
1978 // FunctionPointerOrVirtualThunk
1979 fields.push_back(llvm::Constant::getNullValue(CGM.VoidPtrTy));
1980 } else {
Stephen Hines651f13c2014-04-23 16:59:28 -07001981 if (RD->nullFieldOffsetIsZero())
Reid Klecknera3609b02013-04-11 18:13:19 +00001982 fields.push_back(getZeroInt()); // FieldOffset
1983 else
1984 fields.push_back(getAllOnesInt()); // FieldOffset
Reid Klecknera8a0f762013-03-22 19:02:54 +00001985 }
Reid Klecknera3609b02013-04-11 18:13:19 +00001986
Stephen Hines651f13c2014-04-23 16:59:28 -07001987 if (MSInheritanceAttr::hasNVOffsetField(MPT->isMemberFunctionPointer(),
1988 Inheritance))
Reid Klecknera3609b02013-04-11 18:13:19 +00001989 fields.push_back(getZeroInt());
Stephen Hines651f13c2014-04-23 16:59:28 -07001990 if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance))
Reid Klecknera3609b02013-04-11 18:13:19 +00001991 fields.push_back(getZeroInt());
Stephen Hines651f13c2014-04-23 16:59:28 -07001992 if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance))
Reid Klecknera3609b02013-04-11 18:13:19 +00001993 fields.push_back(getAllOnesInt());
Reid Klecknera8a0f762013-03-22 19:02:54 +00001994}
1995
1996llvm::Constant *
1997MicrosoftCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) {
Reid Klecknera3609b02013-04-11 18:13:19 +00001998 llvm::SmallVector<llvm::Constant *, 4> fields;
1999 GetNullMemberPointerFields(MPT, fields);
2000 if (fields.size() == 1)
2001 return fields[0];
2002 llvm::Constant *Res = llvm::ConstantStruct::getAnon(fields);
2003 assert(Res->getType() == ConvertMemberPointerType(MPT));
2004 return Res;
Reid Klecknera8a0f762013-03-22 19:02:54 +00002005}
2006
2007llvm::Constant *
Reid Kleckner79e02912013-05-03 01:15:11 +00002008MicrosoftCXXABI::EmitFullMemberPointer(llvm::Constant *FirstField,
2009 bool IsMemberFunction,
Reid Klecknerf6327302013-05-09 21:01:17 +00002010 const CXXRecordDecl *RD,
2011 CharUnits NonVirtualBaseAdjustment)
Reid Kleckner79e02912013-05-03 01:15:11 +00002012{
Stephen Hines651f13c2014-04-23 16:59:28 -07002013 MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel();
Reid Kleckner79e02912013-05-03 01:15:11 +00002014
2015 // Single inheritance class member pointer are represented as scalars instead
2016 // of aggregates.
Stephen Hines651f13c2014-04-23 16:59:28 -07002017 if (MSInheritanceAttr::hasOnlyOneField(IsMemberFunction, Inheritance))
Reid Kleckner79e02912013-05-03 01:15:11 +00002018 return FirstField;
2019
Reid Klecknera3609b02013-04-11 18:13:19 +00002020 llvm::SmallVector<llvm::Constant *, 4> fields;
Reid Kleckner79e02912013-05-03 01:15:11 +00002021 fields.push_back(FirstField);
2022
Stephen Hines651f13c2014-04-23 16:59:28 -07002023 if (MSInheritanceAttr::hasNVOffsetField(IsMemberFunction, Inheritance))
Reid Klecknerf6327302013-05-09 21:01:17 +00002024 fields.push_back(llvm::ConstantInt::get(
2025 CGM.IntTy, NonVirtualBaseAdjustment.getQuantity()));
Reid Kleckner79e02912013-05-03 01:15:11 +00002026
Stephen Hines651f13c2014-04-23 16:59:28 -07002027 if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance)) {
Reid Kleckner42090d62013-10-15 01:18:02 +00002028 CharUnits Offs = CharUnits::Zero();
2029 if (RD->getNumVBases())
Stephen Hines651f13c2014-04-23 16:59:28 -07002030 Offs = getContext().getASTRecordLayout(RD).getVBPtrOffset();
Reid Kleckner42090d62013-10-15 01:18:02 +00002031 fields.push_back(llvm::ConstantInt::get(CGM.IntTy, Offs.getQuantity()));
Reid Klecknera3609b02013-04-11 18:13:19 +00002032 }
Reid Kleckner79e02912013-05-03 01:15:11 +00002033
2034 // The rest of the fields are adjusted by conversions to a more derived class.
Stephen Hines651f13c2014-04-23 16:59:28 -07002035 if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance))
Reid Klecknera3609b02013-04-11 18:13:19 +00002036 fields.push_back(getZeroInt());
Reid Kleckner79e02912013-05-03 01:15:11 +00002037
Reid Klecknera3609b02013-04-11 18:13:19 +00002038 return llvm::ConstantStruct::getAnon(fields);
Reid Klecknera8a0f762013-03-22 19:02:54 +00002039}
2040
Reid Kleckner79e02912013-05-03 01:15:11 +00002041llvm::Constant *
2042MicrosoftCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT,
2043 CharUnits offset) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002044 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
Reid Kleckner79e02912013-05-03 01:15:11 +00002045 llvm::Constant *FirstField =
2046 llvm::ConstantInt::get(CGM.IntTy, offset.getQuantity());
Reid Klecknerf6327302013-05-09 21:01:17 +00002047 return EmitFullMemberPointer(FirstField, /*IsMemberFunction=*/false, RD,
2048 CharUnits::Zero());
2049}
2050
2051llvm::Constant *MicrosoftCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) {
2052 return BuildMemberPointer(MD->getParent(), MD, CharUnits::Zero());
2053}
2054
2055llvm::Constant *MicrosoftCXXABI::EmitMemberPointer(const APValue &MP,
2056 QualType MPType) {
2057 const MemberPointerType *MPT = MPType->castAs<MemberPointerType>();
2058 const ValueDecl *MPD = MP.getMemberPointerDecl();
2059 if (!MPD)
2060 return EmitNullMemberPointer(MPT);
2061
2062 CharUnits ThisAdjustment = getMemberPointerPathAdjustment(MP);
2063
2064 // FIXME PR15713: Support virtual inheritance paths.
2065
2066 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MPD))
Stephen Hines651f13c2014-04-23 16:59:28 -07002067 return BuildMemberPointer(MPT->getMostRecentCXXRecordDecl(), MD,
2068 ThisAdjustment);
Reid Klecknerf6327302013-05-09 21:01:17 +00002069
2070 CharUnits FieldOffset =
2071 getContext().toCharUnitsFromBits(getContext().getFieldOffset(MPD));
2072 return EmitMemberDataPointer(MPT, ThisAdjustment + FieldOffset);
Reid Kleckner79e02912013-05-03 01:15:11 +00002073}
2074
2075llvm::Constant *
Reid Klecknerf6327302013-05-09 21:01:17 +00002076MicrosoftCXXABI::BuildMemberPointer(const CXXRecordDecl *RD,
2077 const CXXMethodDecl *MD,
2078 CharUnits NonVirtualBaseAdjustment) {
Reid Kleckner79e02912013-05-03 01:15:11 +00002079 assert(MD->isInstance() && "Member function must not be static!");
2080 MD = MD->getCanonicalDecl();
Stephen Hines651f13c2014-04-23 16:59:28 -07002081 RD = RD->getMostRecentDecl();
Reid Kleckner79e02912013-05-03 01:15:11 +00002082 CodeGenTypes &Types = CGM.getTypes();
2083
2084 llvm::Constant *FirstField;
Stephen Hines176edba2014-12-01 14:53:08 -08002085 const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
Hans Wennborg93b717a2013-11-15 17:24:45 +00002086 if (!MD->isVirtual()) {
Reid Kleckner79e02912013-05-03 01:15:11 +00002087 llvm::Type *Ty;
2088 // Check whether the function has a computable LLVM signature.
2089 if (Types.isFuncTypeConvertible(FPT)) {
2090 // The function has a computable LLVM signature; use the correct type.
2091 Ty = Types.GetFunctionType(Types.arrangeCXXMethodDeclaration(MD));
2092 } else {
2093 // Use an arbitrary non-function type to tell GetAddrOfFunction that the
2094 // function type is incomplete.
2095 Ty = CGM.PtrDiffTy;
2096 }
2097 FirstField = CGM.GetAddrOfFunction(MD, Ty);
2098 FirstField = llvm::ConstantExpr::getBitCast(FirstField, CGM.VoidPtrTy);
Hans Wennborg93b717a2013-11-15 17:24:45 +00002099 } else {
2100 MicrosoftVTableContext::MethodVFTableLocation ML =
2101 CGM.getMicrosoftVTableContext().getMethodVFTableLocation(MD);
Stephen Hines176edba2014-12-01 14:53:08 -08002102 if (!CGM.getTypes().isFuncTypeConvertible(
2103 MD->getType()->castAs<FunctionType>())) {
Hans Wennborg93b717a2013-11-15 17:24:45 +00002104 CGM.ErrorUnsupported(MD, "pointer to virtual member function with "
2105 "incomplete return or parameter type");
2106 FirstField = llvm::Constant::getNullValue(CGM.VoidPtrTy);
Stephen Hines176edba2014-12-01 14:53:08 -08002107 } else if (FPT->getCallConv() == CC_X86FastCall) {
2108 CGM.ErrorUnsupported(MD, "pointer to fastcall virtual member function");
2109 FirstField = llvm::Constant::getNullValue(CGM.VoidPtrTy);
Hans Wennborg93b717a2013-11-15 17:24:45 +00002110 } else if (ML.VBase) {
2111 CGM.ErrorUnsupported(MD, "pointer to virtual member function overriding "
2112 "member function in virtual base class");
2113 FirstField = llvm::Constant::getNullValue(CGM.VoidPtrTy);
2114 } else {
Stephen Hines651f13c2014-04-23 16:59:28 -07002115 llvm::Function *Thunk = EmitVirtualMemPtrThunk(MD, ML);
Hans Wennborg93b717a2013-11-15 17:24:45 +00002116 FirstField = llvm::ConstantExpr::getBitCast(Thunk, CGM.VoidPtrTy);
Stephen Hines651f13c2014-04-23 16:59:28 -07002117 // Include the vfptr adjustment if the method is in a non-primary vftable.
2118 NonVirtualBaseAdjustment += ML.VFPtrOffset;
Hans Wennborg93b717a2013-11-15 17:24:45 +00002119 }
Reid Kleckner79e02912013-05-03 01:15:11 +00002120 }
2121
2122 // The rest of the fields are common with data member pointers.
Reid Klecknerf6327302013-05-09 21:01:17 +00002123 return EmitFullMemberPointer(FirstField, /*IsMemberFunction=*/true, RD,
2124 NonVirtualBaseAdjustment);
Reid Kleckner79e02912013-05-03 01:15:11 +00002125}
2126
Reid Kleckner3d2f0002013-04-30 20:15:14 +00002127/// Member pointers are the same if they're either bitwise identical *or* both
2128/// null. Null-ness for function members is determined by the first field,
2129/// while for data member pointers we must compare all fields.
2130llvm::Value *
2131MicrosoftCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF,
2132 llvm::Value *L,
2133 llvm::Value *R,
2134 const MemberPointerType *MPT,
2135 bool Inequality) {
2136 CGBuilderTy &Builder = CGF.Builder;
2137
2138 // Handle != comparisons by switching the sense of all boolean operations.
2139 llvm::ICmpInst::Predicate Eq;
2140 llvm::Instruction::BinaryOps And, Or;
2141 if (Inequality) {
2142 Eq = llvm::ICmpInst::ICMP_NE;
2143 And = llvm::Instruction::Or;
2144 Or = llvm::Instruction::And;
2145 } else {
2146 Eq = llvm::ICmpInst::ICMP_EQ;
2147 And = llvm::Instruction::And;
2148 Or = llvm::Instruction::Or;
2149 }
2150
2151 // If this is a single field member pointer (single inheritance), this is a
2152 // single icmp.
Stephen Hines651f13c2014-04-23 16:59:28 -07002153 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
2154 MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel();
2155 if (MSInheritanceAttr::hasOnlyOneField(MPT->isMemberFunctionPointer(),
2156 Inheritance))
Reid Kleckner3d2f0002013-04-30 20:15:14 +00002157 return Builder.CreateICmp(Eq, L, R);
2158
2159 // Compare the first field.
2160 llvm::Value *L0 = Builder.CreateExtractValue(L, 0, "lhs.0");
2161 llvm::Value *R0 = Builder.CreateExtractValue(R, 0, "rhs.0");
2162 llvm::Value *Cmp0 = Builder.CreateICmp(Eq, L0, R0, "memptr.cmp.first");
2163
2164 // Compare everything other than the first field.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002165 llvm::Value *Res = nullptr;
Reid Kleckner3d2f0002013-04-30 20:15:14 +00002166 llvm::StructType *LType = cast<llvm::StructType>(L->getType());
2167 for (unsigned I = 1, E = LType->getNumElements(); I != E; ++I) {
2168 llvm::Value *LF = Builder.CreateExtractValue(L, I);
2169 llvm::Value *RF = Builder.CreateExtractValue(R, I);
2170 llvm::Value *Cmp = Builder.CreateICmp(Eq, LF, RF, "memptr.cmp.rest");
2171 if (Res)
2172 Res = Builder.CreateBinOp(And, Res, Cmp);
2173 else
2174 Res = Cmp;
2175 }
2176
2177 // Check if the first field is 0 if this is a function pointer.
2178 if (MPT->isMemberFunctionPointer()) {
2179 // (l1 == r1 && ...) || l0 == 0
2180 llvm::Value *Zero = llvm::Constant::getNullValue(L0->getType());
2181 llvm::Value *IsZero = Builder.CreateICmp(Eq, L0, Zero, "memptr.cmp.iszero");
2182 Res = Builder.CreateBinOp(Or, Res, IsZero);
2183 }
2184
2185 // Combine the comparison of the first field, which must always be true for
2186 // this comparison to succeeed.
2187 return Builder.CreateBinOp(And, Res, Cmp0, "memptr.cmp");
2188}
2189
Reid Klecknera8a0f762013-03-22 19:02:54 +00002190llvm::Value *
2191MicrosoftCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
2192 llvm::Value *MemPtr,
2193 const MemberPointerType *MPT) {
2194 CGBuilderTy &Builder = CGF.Builder;
Reid Klecknera3609b02013-04-11 18:13:19 +00002195 llvm::SmallVector<llvm::Constant *, 4> fields;
2196 // We only need one field for member functions.
2197 if (MPT->isMemberFunctionPointer())
2198 fields.push_back(llvm::Constant::getNullValue(CGM.VoidPtrTy));
2199 else
2200 GetNullMemberPointerFields(MPT, fields);
2201 assert(!fields.empty());
2202 llvm::Value *FirstField = MemPtr;
2203 if (MemPtr->getType()->isStructTy())
2204 FirstField = Builder.CreateExtractValue(MemPtr, 0);
2205 llvm::Value *Res = Builder.CreateICmpNE(FirstField, fields[0], "memptr.cmp0");
Reid Klecknera8a0f762013-03-22 19:02:54 +00002206
Reid Klecknera3609b02013-04-11 18:13:19 +00002207 // For function member pointers, we only need to test the function pointer
2208 // field. The other fields if any can be garbage.
2209 if (MPT->isMemberFunctionPointer())
2210 return Res;
2211
2212 // Otherwise, emit a series of compares and combine the results.
2213 for (int I = 1, E = fields.size(); I < E; ++I) {
2214 llvm::Value *Field = Builder.CreateExtractValue(MemPtr, I);
2215 llvm::Value *Next = Builder.CreateICmpNE(Field, fields[I], "memptr.cmp");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002216 Res = Builder.CreateOr(Res, Next, "memptr.tobool");
Reid Klecknera3609b02013-04-11 18:13:19 +00002217 }
2218 return Res;
2219}
2220
Reid Klecknerf6327302013-05-09 21:01:17 +00002221bool MicrosoftCXXABI::MemberPointerConstantIsNull(const MemberPointerType *MPT,
2222 llvm::Constant *Val) {
2223 // Function pointers are null if the pointer in the first field is null.
2224 if (MPT->isMemberFunctionPointer()) {
2225 llvm::Constant *FirstField = Val->getType()->isStructTy() ?
2226 Val->getAggregateElement(0U) : Val;
2227 return FirstField->isNullValue();
2228 }
2229
2230 // If it's not a function pointer and it's zero initializable, we can easily
2231 // check zero.
2232 if (isZeroInitializable(MPT) && Val->isNullValue())
2233 return true;
2234
2235 // Otherwise, break down all the fields for comparison. Hopefully these
2236 // little Constants are reused, while a big null struct might not be.
2237 llvm::SmallVector<llvm::Constant *, 4> Fields;
2238 GetNullMemberPointerFields(MPT, Fields);
2239 if (Fields.size() == 1) {
2240 assert(Val->getType()->isIntegerTy());
2241 return Val == Fields[0];
2242 }
2243
2244 unsigned I, E;
2245 for (I = 0, E = Fields.size(); I != E; ++I) {
2246 if (Val->getAggregateElement(I) != Fields[I])
2247 break;
2248 }
2249 return I == E;
2250}
2251
Reid Klecknerb0f533e2013-05-29 18:02:47 +00002252llvm::Value *
2253MicrosoftCXXABI::GetVBaseOffsetFromVBPtr(CodeGenFunction &CGF,
2254 llvm::Value *This,
Reid Klecknerb0f533e2013-05-29 18:02:47 +00002255 llvm::Value *VBPtrOffset,
Timur Iskhodzhanov0e4f5592013-10-27 17:10:27 +00002256 llvm::Value *VBTableOffset,
Reid Klecknerb0f533e2013-05-29 18:02:47 +00002257 llvm::Value **VBPtrOut) {
2258 CGBuilderTy &Builder = CGF.Builder;
2259 // Load the vbtable pointer from the vbptr in the instance.
2260 This = Builder.CreateBitCast(This, CGM.Int8PtrTy);
2261 llvm::Value *VBPtr =
2262 Builder.CreateInBoundsGEP(This, VBPtrOffset, "vbptr");
2263 if (VBPtrOut) *VBPtrOut = VBPtr;
Stephen Hines176edba2014-12-01 14:53:08 -08002264 VBPtr = Builder.CreateBitCast(VBPtr,
2265 CGM.Int32Ty->getPointerTo(0)->getPointerTo(0));
Reid Klecknerb0f533e2013-05-29 18:02:47 +00002266 llvm::Value *VBTable = Builder.CreateLoad(VBPtr, "vbtable");
2267
Stephen Hines176edba2014-12-01 14:53:08 -08002268 // Translate from byte offset to table index. It improves analyzability.
2269 llvm::Value *VBTableIndex = Builder.CreateAShr(
2270 VBTableOffset, llvm::ConstantInt::get(VBTableOffset->getType(), 2),
2271 "vbtindex", /*isExact=*/true);
2272
Reid Klecknerb0f533e2013-05-29 18:02:47 +00002273 // Load an i32 offset from the vb-table.
Stephen Hines176edba2014-12-01 14:53:08 -08002274 llvm::Value *VBaseOffs = Builder.CreateInBoundsGEP(VBTable, VBTableIndex);
Reid Klecknerb0f533e2013-05-29 18:02:47 +00002275 VBaseOffs = Builder.CreateBitCast(VBaseOffs, CGM.Int32Ty->getPointerTo(0));
2276 return Builder.CreateLoad(VBaseOffs, "vbase_offs");
2277}
2278
Reid Klecknera3609b02013-04-11 18:13:19 +00002279// Returns an adjusted base cast to i8*, since we do more address arithmetic on
2280// it.
Stephen Hines651f13c2014-04-23 16:59:28 -07002281llvm::Value *MicrosoftCXXABI::AdjustVirtualBase(
2282 CodeGenFunction &CGF, const Expr *E, const CXXRecordDecl *RD,
2283 llvm::Value *Base, llvm::Value *VBTableOffset, llvm::Value *VBPtrOffset) {
Reid Klecknera3609b02013-04-11 18:13:19 +00002284 CGBuilderTy &Builder = CGF.Builder;
2285 Base = Builder.CreateBitCast(Base, CGM.Int8PtrTy);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002286 llvm::BasicBlock *OriginalBB = nullptr;
2287 llvm::BasicBlock *SkipAdjustBB = nullptr;
2288 llvm::BasicBlock *VBaseAdjustBB = nullptr;
Reid Klecknera3609b02013-04-11 18:13:19 +00002289
2290 // In the unspecified inheritance model, there might not be a vbtable at all,
2291 // in which case we need to skip the virtual base lookup. If there is a
2292 // vbtable, the first entry is a no-op entry that gives back the original
2293 // base, so look for a virtual base adjustment offset of zero.
2294 if (VBPtrOffset) {
2295 OriginalBB = Builder.GetInsertBlock();
2296 VBaseAdjustBB = CGF.createBasicBlock("memptr.vadjust");
2297 SkipAdjustBB = CGF.createBasicBlock("memptr.skip_vadjust");
2298 llvm::Value *IsVirtual =
Reid Klecknerb0f533e2013-05-29 18:02:47 +00002299 Builder.CreateICmpNE(VBTableOffset, getZeroInt(),
Reid Klecknera3609b02013-04-11 18:13:19 +00002300 "memptr.is_vbase");
2301 Builder.CreateCondBr(IsVirtual, VBaseAdjustBB, SkipAdjustBB);
2302 CGF.EmitBlock(VBaseAdjustBB);
Reid Klecknera8a0f762013-03-22 19:02:54 +00002303 }
2304
Reid Klecknera3609b02013-04-11 18:13:19 +00002305 // If we weren't given a dynamic vbptr offset, RD should be complete and we'll
2306 // know the vbptr offset.
2307 if (!VBPtrOffset) {
Reid Klecknerb0f533e2013-05-29 18:02:47 +00002308 CharUnits offs = CharUnits::Zero();
Stephen Hines651f13c2014-04-23 16:59:28 -07002309 if (!RD->hasDefinition()) {
2310 DiagnosticsEngine &Diags = CGF.CGM.getDiags();
2311 unsigned DiagID = Diags.getCustomDiagID(
2312 DiagnosticsEngine::Error,
2313 "member pointer representation requires a "
2314 "complete class type for %0 to perform this expression");
2315 Diags.Report(E->getExprLoc(), DiagID) << RD << E->getSourceRange();
2316 } else if (RD->getNumVBases())
2317 offs = getContext().getASTRecordLayout(RD).getVBPtrOffset();
Reid Klecknera3609b02013-04-11 18:13:19 +00002318 VBPtrOffset = llvm::ConstantInt::get(CGM.IntTy, offs.getQuantity());
2319 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002320 llvm::Value *VBPtr = nullptr;
Reid Klecknera3609b02013-04-11 18:13:19 +00002321 llvm::Value *VBaseOffs =
Timur Iskhodzhanov0e4f5592013-10-27 17:10:27 +00002322 GetVBaseOffsetFromVBPtr(CGF, Base, VBPtrOffset, VBTableOffset, &VBPtr);
Reid Klecknera3609b02013-04-11 18:13:19 +00002323 llvm::Value *AdjustedBase = Builder.CreateInBoundsGEP(VBPtr, VBaseOffs);
2324
2325 // Merge control flow with the case where we didn't have to adjust.
2326 if (VBaseAdjustBB) {
2327 Builder.CreateBr(SkipAdjustBB);
2328 CGF.EmitBlock(SkipAdjustBB);
2329 llvm::PHINode *Phi = Builder.CreatePHI(CGM.Int8PtrTy, 2, "memptr.base");
2330 Phi->addIncoming(Base, OriginalBB);
2331 Phi->addIncoming(AdjustedBase, VBaseAdjustBB);
2332 return Phi;
2333 }
2334 return AdjustedBase;
Reid Klecknera8a0f762013-03-22 19:02:54 +00002335}
2336
Stephen Hines651f13c2014-04-23 16:59:28 -07002337llvm::Value *MicrosoftCXXABI::EmitMemberDataPointerAddress(
2338 CodeGenFunction &CGF, const Expr *E, llvm::Value *Base, llvm::Value *MemPtr,
2339 const MemberPointerType *MPT) {
Reid Klecknera3609b02013-04-11 18:13:19 +00002340 assert(MPT->isMemberDataPointer());
Reid Klecknera8a0f762013-03-22 19:02:54 +00002341 unsigned AS = Base->getType()->getPointerAddressSpace();
2342 llvm::Type *PType =
2343 CGF.ConvertTypeForMem(MPT->getPointeeType())->getPointerTo(AS);
2344 CGBuilderTy &Builder = CGF.Builder;
Stephen Hines651f13c2014-04-23 16:59:28 -07002345 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
2346 MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel();
Reid Klecknera8a0f762013-03-22 19:02:54 +00002347
Reid Klecknera3609b02013-04-11 18:13:19 +00002348 // Extract the fields we need, regardless of model. We'll apply them if we
2349 // have them.
2350 llvm::Value *FieldOffset = MemPtr;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002351 llvm::Value *VirtualBaseAdjustmentOffset = nullptr;
2352 llvm::Value *VBPtrOffset = nullptr;
Reid Klecknera3609b02013-04-11 18:13:19 +00002353 if (MemPtr->getType()->isStructTy()) {
2354 // We need to extract values.
2355 unsigned I = 0;
2356 FieldOffset = Builder.CreateExtractValue(MemPtr, I++);
Stephen Hines651f13c2014-04-23 16:59:28 -07002357 if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance))
Reid Klecknera3609b02013-04-11 18:13:19 +00002358 VBPtrOffset = Builder.CreateExtractValue(MemPtr, I++);
Stephen Hines651f13c2014-04-23 16:59:28 -07002359 if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance))
Reid Klecknera3609b02013-04-11 18:13:19 +00002360 VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(MemPtr, I++);
Reid Klecknera8a0f762013-03-22 19:02:54 +00002361 }
2362
Reid Klecknera3609b02013-04-11 18:13:19 +00002363 if (VirtualBaseAdjustmentOffset) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002364 Base = AdjustVirtualBase(CGF, E, RD, Base, VirtualBaseAdjustmentOffset,
Reid Klecknera3609b02013-04-11 18:13:19 +00002365 VBPtrOffset);
Reid Klecknera8a0f762013-03-22 19:02:54 +00002366 }
Stephen Hines651f13c2014-04-23 16:59:28 -07002367
2368 // Cast to char*.
2369 Base = Builder.CreateBitCast(Base, Builder.getInt8Ty()->getPointerTo(AS));
2370
2371 // Apply the offset, which we assume is non-null.
Reid Klecknera3609b02013-04-11 18:13:19 +00002372 llvm::Value *Addr =
2373 Builder.CreateInBoundsGEP(Base, FieldOffset, "memptr.offset");
Reid Klecknera8a0f762013-03-22 19:02:54 +00002374
2375 // Cast the address to the appropriate pointer type, adopting the address
2376 // space of the base pointer.
2377 return Builder.CreateBitCast(Addr, PType);
2378}
2379
Stephen Hines651f13c2014-04-23 16:59:28 -07002380static MSInheritanceAttr::Spelling
Reid Klecknerf6327302013-05-09 21:01:17 +00002381getInheritanceFromMemptr(const MemberPointerType *MPT) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002382 return MPT->getMostRecentCXXRecordDecl()->getMSInheritanceModel();
Reid Klecknerf6327302013-05-09 21:01:17 +00002383}
2384
2385llvm::Value *
2386MicrosoftCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF,
2387 const CastExpr *E,
2388 llvm::Value *Src) {
2389 assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
2390 E->getCastKind() == CK_BaseToDerivedMemberPointer ||
2391 E->getCastKind() == CK_ReinterpretMemberPointer);
2392
2393 // Use constant emission if we can.
2394 if (isa<llvm::Constant>(Src))
2395 return EmitMemberPointerConversion(E, cast<llvm::Constant>(Src));
2396
2397 // We may be adding or dropping fields from the member pointer, so we need
2398 // both types and the inheritance models of both records.
2399 const MemberPointerType *SrcTy =
2400 E->getSubExpr()->getType()->castAs<MemberPointerType>();
2401 const MemberPointerType *DstTy = E->getType()->castAs<MemberPointerType>();
Reid Klecknerf6327302013-05-09 21:01:17 +00002402 bool IsFunc = SrcTy->isMemberFunctionPointer();
2403
2404 // If the classes use the same null representation, reinterpret_cast is a nop.
2405 bool IsReinterpret = E->getCastKind() == CK_ReinterpretMemberPointer;
Stephen Hines651f13c2014-04-23 16:59:28 -07002406 if (IsReinterpret && IsFunc)
2407 return Src;
2408
2409 CXXRecordDecl *SrcRD = SrcTy->getMostRecentCXXRecordDecl();
2410 CXXRecordDecl *DstRD = DstTy->getMostRecentCXXRecordDecl();
2411 if (IsReinterpret &&
2412 SrcRD->nullFieldOffsetIsZero() == DstRD->nullFieldOffsetIsZero())
Reid Klecknerf6327302013-05-09 21:01:17 +00002413 return Src;
2414
2415 CGBuilderTy &Builder = CGF.Builder;
2416
2417 // Branch past the conversion if Src is null.
2418 llvm::Value *IsNotNull = EmitMemberPointerIsNotNull(CGF, Src, SrcTy);
2419 llvm::Constant *DstNull = EmitNullMemberPointer(DstTy);
2420
2421 // C++ 5.2.10p9: The null member pointer value is converted to the null member
2422 // pointer value of the destination type.
2423 if (IsReinterpret) {
2424 // For reinterpret casts, sema ensures that src and dst are both functions
2425 // or data and have the same size, which means the LLVM types should match.
2426 assert(Src->getType() == DstNull->getType());
2427 return Builder.CreateSelect(IsNotNull, Src, DstNull);
2428 }
2429
2430 llvm::BasicBlock *OriginalBB = Builder.GetInsertBlock();
2431 llvm::BasicBlock *ConvertBB = CGF.createBasicBlock("memptr.convert");
2432 llvm::BasicBlock *ContinueBB = CGF.createBasicBlock("memptr.converted");
2433 Builder.CreateCondBr(IsNotNull, ConvertBB, ContinueBB);
2434 CGF.EmitBlock(ConvertBB);
2435
2436 // Decompose src.
2437 llvm::Value *FirstField = Src;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002438 llvm::Value *NonVirtualBaseAdjustment = nullptr;
2439 llvm::Value *VirtualBaseAdjustmentOffset = nullptr;
2440 llvm::Value *VBPtrOffset = nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07002441 MSInheritanceAttr::Spelling SrcInheritance = SrcRD->getMSInheritanceModel();
2442 if (!MSInheritanceAttr::hasOnlyOneField(IsFunc, SrcInheritance)) {
Reid Klecknerf6327302013-05-09 21:01:17 +00002443 // We need to extract values.
2444 unsigned I = 0;
2445 FirstField = Builder.CreateExtractValue(Src, I++);
Stephen Hines651f13c2014-04-23 16:59:28 -07002446 if (MSInheritanceAttr::hasNVOffsetField(IsFunc, SrcInheritance))
Reid Klecknerf6327302013-05-09 21:01:17 +00002447 NonVirtualBaseAdjustment = Builder.CreateExtractValue(Src, I++);
Stephen Hines651f13c2014-04-23 16:59:28 -07002448 if (MSInheritanceAttr::hasVBPtrOffsetField(SrcInheritance))
Reid Klecknerf6327302013-05-09 21:01:17 +00002449 VBPtrOffset = Builder.CreateExtractValue(Src, I++);
Stephen Hines651f13c2014-04-23 16:59:28 -07002450 if (MSInheritanceAttr::hasVBTableOffsetField(SrcInheritance))
Reid Klecknerf6327302013-05-09 21:01:17 +00002451 VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(Src, I++);
2452 }
2453
2454 // For data pointers, we adjust the field offset directly. For functions, we
2455 // have a separate field.
2456 llvm::Constant *Adj = getMemberPointerAdjustment(E);
2457 if (Adj) {
2458 Adj = llvm::ConstantExpr::getTruncOrBitCast(Adj, CGM.IntTy);
2459 llvm::Value *&NVAdjustField = IsFunc ? NonVirtualBaseAdjustment : FirstField;
2460 bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
2461 if (!NVAdjustField) // If this field didn't exist in src, it's zero.
2462 NVAdjustField = getZeroInt();
2463 if (isDerivedToBase)
2464 NVAdjustField = Builder.CreateNSWSub(NVAdjustField, Adj, "adj");
2465 else
2466 NVAdjustField = Builder.CreateNSWAdd(NVAdjustField, Adj, "adj");
2467 }
2468
2469 // FIXME PR15713: Support conversions through virtually derived classes.
2470
2471 // Recompose dst from the null struct and the adjusted fields from src.
Stephen Hines651f13c2014-04-23 16:59:28 -07002472 MSInheritanceAttr::Spelling DstInheritance = DstRD->getMSInheritanceModel();
Reid Klecknerf6327302013-05-09 21:01:17 +00002473 llvm::Value *Dst;
Stephen Hines651f13c2014-04-23 16:59:28 -07002474 if (MSInheritanceAttr::hasOnlyOneField(IsFunc, DstInheritance)) {
Reid Klecknerf6327302013-05-09 21:01:17 +00002475 Dst = FirstField;
2476 } else {
2477 Dst = llvm::UndefValue::get(DstNull->getType());
2478 unsigned Idx = 0;
2479 Dst = Builder.CreateInsertValue(Dst, FirstField, Idx++);
Stephen Hines651f13c2014-04-23 16:59:28 -07002480 if (MSInheritanceAttr::hasNVOffsetField(IsFunc, DstInheritance))
Reid Klecknerf6327302013-05-09 21:01:17 +00002481 Dst = Builder.CreateInsertValue(
2482 Dst, getValueOrZeroInt(NonVirtualBaseAdjustment), Idx++);
Stephen Hines651f13c2014-04-23 16:59:28 -07002483 if (MSInheritanceAttr::hasVBPtrOffsetField(DstInheritance))
Reid Klecknerf6327302013-05-09 21:01:17 +00002484 Dst = Builder.CreateInsertValue(
2485 Dst, getValueOrZeroInt(VBPtrOffset), Idx++);
Stephen Hines651f13c2014-04-23 16:59:28 -07002486 if (MSInheritanceAttr::hasVBTableOffsetField(DstInheritance))
Reid Klecknerf6327302013-05-09 21:01:17 +00002487 Dst = Builder.CreateInsertValue(
2488 Dst, getValueOrZeroInt(VirtualBaseAdjustmentOffset), Idx++);
2489 }
2490 Builder.CreateBr(ContinueBB);
2491
2492 // In the continuation, choose between DstNull and Dst.
2493 CGF.EmitBlock(ContinueBB);
2494 llvm::PHINode *Phi = Builder.CreatePHI(DstNull->getType(), 2, "memptr.converted");
2495 Phi->addIncoming(DstNull, OriginalBB);
2496 Phi->addIncoming(Dst, ConvertBB);
2497 return Phi;
2498}
2499
2500llvm::Constant *
2501MicrosoftCXXABI::EmitMemberPointerConversion(const CastExpr *E,
2502 llvm::Constant *Src) {
2503 const MemberPointerType *SrcTy =
2504 E->getSubExpr()->getType()->castAs<MemberPointerType>();
2505 const MemberPointerType *DstTy = E->getType()->castAs<MemberPointerType>();
2506
2507 // If src is null, emit a new null for dst. We can't return src because dst
2508 // might have a new representation.
2509 if (MemberPointerConstantIsNull(SrcTy, Src))
2510 return EmitNullMemberPointer(DstTy);
2511
2512 // We don't need to do anything for reinterpret_casts of non-null member
2513 // pointers. We should only get here when the two type representations have
2514 // the same size.
2515 if (E->getCastKind() == CK_ReinterpretMemberPointer)
2516 return Src;
2517
Stephen Hines651f13c2014-04-23 16:59:28 -07002518 MSInheritanceAttr::Spelling SrcInheritance = getInheritanceFromMemptr(SrcTy);
2519 MSInheritanceAttr::Spelling DstInheritance = getInheritanceFromMemptr(DstTy);
Reid Klecknerf6327302013-05-09 21:01:17 +00002520
2521 // Decompose src.
2522 llvm::Constant *FirstField = Src;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002523 llvm::Constant *NonVirtualBaseAdjustment = nullptr;
2524 llvm::Constant *VirtualBaseAdjustmentOffset = nullptr;
2525 llvm::Constant *VBPtrOffset = nullptr;
Reid Klecknerf6327302013-05-09 21:01:17 +00002526 bool IsFunc = SrcTy->isMemberFunctionPointer();
Stephen Hines651f13c2014-04-23 16:59:28 -07002527 if (!MSInheritanceAttr::hasOnlyOneField(IsFunc, SrcInheritance)) {
Reid Klecknerf6327302013-05-09 21:01:17 +00002528 // We need to extract values.
2529 unsigned I = 0;
2530 FirstField = Src->getAggregateElement(I++);
Stephen Hines651f13c2014-04-23 16:59:28 -07002531 if (MSInheritanceAttr::hasNVOffsetField(IsFunc, SrcInheritance))
Reid Klecknerf6327302013-05-09 21:01:17 +00002532 NonVirtualBaseAdjustment = Src->getAggregateElement(I++);
Stephen Hines651f13c2014-04-23 16:59:28 -07002533 if (MSInheritanceAttr::hasVBPtrOffsetField(SrcInheritance))
Reid Klecknerf6327302013-05-09 21:01:17 +00002534 VBPtrOffset = Src->getAggregateElement(I++);
Stephen Hines651f13c2014-04-23 16:59:28 -07002535 if (MSInheritanceAttr::hasVBTableOffsetField(SrcInheritance))
Reid Klecknerf6327302013-05-09 21:01:17 +00002536 VirtualBaseAdjustmentOffset = Src->getAggregateElement(I++);
2537 }
2538
2539 // For data pointers, we adjust the field offset directly. For functions, we
2540 // have a separate field.
2541 llvm::Constant *Adj = getMemberPointerAdjustment(E);
2542 if (Adj) {
2543 Adj = llvm::ConstantExpr::getTruncOrBitCast(Adj, CGM.IntTy);
2544 llvm::Constant *&NVAdjustField =
2545 IsFunc ? NonVirtualBaseAdjustment : FirstField;
2546 bool IsDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
2547 if (!NVAdjustField) // If this field didn't exist in src, it's zero.
2548 NVAdjustField = getZeroInt();
2549 if (IsDerivedToBase)
2550 NVAdjustField = llvm::ConstantExpr::getNSWSub(NVAdjustField, Adj);
2551 else
2552 NVAdjustField = llvm::ConstantExpr::getNSWAdd(NVAdjustField, Adj);
2553 }
2554
2555 // FIXME PR15713: Support conversions through virtually derived classes.
2556
2557 // Recompose dst from the null struct and the adjusted fields from src.
Stephen Hines651f13c2014-04-23 16:59:28 -07002558 if (MSInheritanceAttr::hasOnlyOneField(IsFunc, DstInheritance))
Reid Klecknerf6327302013-05-09 21:01:17 +00002559 return FirstField;
2560
2561 llvm::SmallVector<llvm::Constant *, 4> Fields;
2562 Fields.push_back(FirstField);
Stephen Hines651f13c2014-04-23 16:59:28 -07002563 if (MSInheritanceAttr::hasNVOffsetField(IsFunc, DstInheritance))
Reid Klecknerf6327302013-05-09 21:01:17 +00002564 Fields.push_back(getConstantOrZeroInt(NonVirtualBaseAdjustment));
Stephen Hines651f13c2014-04-23 16:59:28 -07002565 if (MSInheritanceAttr::hasVBPtrOffsetField(DstInheritance))
Reid Klecknerf6327302013-05-09 21:01:17 +00002566 Fields.push_back(getConstantOrZeroInt(VBPtrOffset));
Stephen Hines651f13c2014-04-23 16:59:28 -07002567 if (MSInheritanceAttr::hasVBTableOffsetField(DstInheritance))
Reid Klecknerf6327302013-05-09 21:01:17 +00002568 Fields.push_back(getConstantOrZeroInt(VirtualBaseAdjustmentOffset));
2569 return llvm::ConstantStruct::getAnon(Fields);
2570}
2571
Stephen Hines651f13c2014-04-23 16:59:28 -07002572llvm::Value *MicrosoftCXXABI::EmitLoadOfMemberFunctionPointer(
2573 CodeGenFunction &CGF, const Expr *E, llvm::Value *&This,
2574 llvm::Value *MemPtr, const MemberPointerType *MPT) {
Reid Klecknera3609b02013-04-11 18:13:19 +00002575 assert(MPT->isMemberFunctionPointer());
2576 const FunctionProtoType *FPT =
2577 MPT->getPointeeType()->castAs<FunctionProtoType>();
Stephen Hines651f13c2014-04-23 16:59:28 -07002578 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
Reid Klecknera3609b02013-04-11 18:13:19 +00002579 llvm::FunctionType *FTy =
2580 CGM.getTypes().GetFunctionType(
2581 CGM.getTypes().arrangeCXXMethodType(RD, FPT));
2582 CGBuilderTy &Builder = CGF.Builder;
2583
Stephen Hines651f13c2014-04-23 16:59:28 -07002584 MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel();
Reid Klecknera3609b02013-04-11 18:13:19 +00002585
2586 // Extract the fields we need, regardless of model. We'll apply them if we
2587 // have them.
2588 llvm::Value *FunctionPointer = MemPtr;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002589 llvm::Value *NonVirtualBaseAdjustment = nullptr;
2590 llvm::Value *VirtualBaseAdjustmentOffset = nullptr;
2591 llvm::Value *VBPtrOffset = nullptr;
Reid Klecknera3609b02013-04-11 18:13:19 +00002592 if (MemPtr->getType()->isStructTy()) {
2593 // We need to extract values.
2594 unsigned I = 0;
2595 FunctionPointer = Builder.CreateExtractValue(MemPtr, I++);
Stephen Hines651f13c2014-04-23 16:59:28 -07002596 if (MSInheritanceAttr::hasNVOffsetField(MPT, Inheritance))
Reid Klecknera3609b02013-04-11 18:13:19 +00002597 NonVirtualBaseAdjustment = Builder.CreateExtractValue(MemPtr, I++);
Stephen Hines651f13c2014-04-23 16:59:28 -07002598 if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance))
Reid Kleckner79e02912013-05-03 01:15:11 +00002599 VBPtrOffset = Builder.CreateExtractValue(MemPtr, I++);
Stephen Hines651f13c2014-04-23 16:59:28 -07002600 if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance))
Reid Klecknera3609b02013-04-11 18:13:19 +00002601 VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(MemPtr, I++);
2602 }
2603
2604 if (VirtualBaseAdjustmentOffset) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002605 This = AdjustVirtualBase(CGF, E, RD, This, VirtualBaseAdjustmentOffset,
Reid Klecknera3609b02013-04-11 18:13:19 +00002606 VBPtrOffset);
2607 }
2608
2609 if (NonVirtualBaseAdjustment) {
2610 // Apply the adjustment and cast back to the original struct type.
2611 llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy());
2612 Ptr = Builder.CreateInBoundsGEP(Ptr, NonVirtualBaseAdjustment);
2613 This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted");
2614 }
2615
2616 return Builder.CreateBitCast(FunctionPointer, FTy->getPointerTo());
2617}
2618
Charles Davis071cc7d2010-08-16 03:33:14 +00002619CGCXXABI *clang::CodeGen::CreateMicrosoftCXXABI(CodeGenModule &CGM) {
Charles Davisc3926642010-06-09 23:25:41 +00002620 return new MicrosoftCXXABI(CGM);
2621}
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002622
2623// MS RTTI Overview:
2624// The run time type information emitted by cl.exe contains 5 distinct types of
2625// structures. Many of them reference each other.
2626//
2627// TypeInfo: Static classes that are returned by typeid.
2628//
2629// CompleteObjectLocator: Referenced by vftables. They contain information
2630// required for dynamic casting, including OffsetFromTop. They also contain
2631// a reference to the TypeInfo for the type and a reference to the
2632// CompleteHierarchyDescriptor for the type.
2633//
2634// ClassHieararchyDescriptor: Contains information about a class hierarchy.
2635// Used during dynamic_cast to walk a class hierarchy. References a base
2636// class array and the size of said array.
2637//
2638// BaseClassArray: Contains a list of classes in a hierarchy. BaseClassArray is
2639// somewhat of a misnomer because the most derived class is also in the list
2640// as well as multiple copies of virtual bases (if they occur multiple times
2641// in the hiearchy.) The BaseClassArray contains one BaseClassDescriptor for
2642// every path in the hierarchy, in pre-order depth first order. Note, we do
2643// not declare a specific llvm type for BaseClassArray, it's merely an array
2644// of BaseClassDescriptor pointers.
2645//
2646// BaseClassDescriptor: Contains information about a class in a class hierarchy.
2647// BaseClassDescriptor is also somewhat of a misnomer for the same reason that
2648// BaseClassArray is. It contains information about a class within a
2649// hierarchy such as: is this base is ambiguous and what is its offset in the
2650// vbtable. The names of the BaseClassDescriptors have all of their fields
2651// mangled into them so they can be aggressively deduplicated by the linker.
2652
2653static llvm::GlobalVariable *getTypeInfoVTable(CodeGenModule &CGM) {
2654 StringRef MangledName("\01??_7type_info@@6B@");
2655 if (auto VTable = CGM.getModule().getNamedGlobal(MangledName))
2656 return VTable;
2657 return new llvm::GlobalVariable(CGM.getModule(), CGM.Int8PtrTy,
2658 /*Constant=*/true,
2659 llvm::GlobalVariable::ExternalLinkage,
2660 /*Initializer=*/nullptr, MangledName);
2661}
2662
2663namespace {
2664
2665/// \brief A Helper struct that stores information about a class in a class
2666/// hierarchy. The information stored in these structs struct is used during
2667/// the generation of ClassHierarchyDescriptors and BaseClassDescriptors.
2668// During RTTI creation, MSRTTIClasses are stored in a contiguous array with
2669// implicit depth first pre-order tree connectivity. getFirstChild and
2670// getNextSibling allow us to walk the tree efficiently.
2671struct MSRTTIClass {
2672 enum {
2673 IsPrivateOnPath = 1 | 8,
2674 IsAmbiguous = 2,
2675 IsPrivate = 4,
2676 IsVirtual = 16,
2677 HasHierarchyDescriptor = 64
2678 };
2679 MSRTTIClass(const CXXRecordDecl *RD) : RD(RD) {}
2680 uint32_t initialize(const MSRTTIClass *Parent,
2681 const CXXBaseSpecifier *Specifier);
2682
2683 MSRTTIClass *getFirstChild() { return this + 1; }
2684 static MSRTTIClass *getNextChild(MSRTTIClass *Child) {
2685 return Child + 1 + Child->NumBases;
2686 }
2687
2688 const CXXRecordDecl *RD, *VirtualRoot;
2689 uint32_t Flags, NumBases, OffsetInVBase;
2690};
2691
2692/// \brief Recursively initialize the base class array.
2693uint32_t MSRTTIClass::initialize(const MSRTTIClass *Parent,
2694 const CXXBaseSpecifier *Specifier) {
2695 Flags = HasHierarchyDescriptor;
2696 if (!Parent) {
2697 VirtualRoot = nullptr;
2698 OffsetInVBase = 0;
2699 } else {
2700 if (Specifier->getAccessSpecifier() != AS_public)
2701 Flags |= IsPrivate | IsPrivateOnPath;
2702 if (Specifier->isVirtual()) {
2703 Flags |= IsVirtual;
2704 VirtualRoot = RD;
2705 OffsetInVBase = 0;
2706 } else {
2707 if (Parent->Flags & IsPrivateOnPath)
2708 Flags |= IsPrivateOnPath;
2709 VirtualRoot = Parent->VirtualRoot;
2710 OffsetInVBase = Parent->OffsetInVBase + RD->getASTContext()
2711 .getASTRecordLayout(Parent->RD).getBaseClassOffset(RD).getQuantity();
2712 }
2713 }
2714 NumBases = 0;
2715 MSRTTIClass *Child = getFirstChild();
2716 for (const CXXBaseSpecifier &Base : RD->bases()) {
2717 NumBases += Child->initialize(this, &Base) + 1;
2718 Child = getNextChild(Child);
2719 }
2720 return NumBases;
2721}
2722
2723static llvm::GlobalValue::LinkageTypes getLinkageForRTTI(QualType Ty) {
2724 switch (Ty->getLinkage()) {
2725 case NoLinkage:
2726 case InternalLinkage:
2727 case UniqueExternalLinkage:
2728 return llvm::GlobalValue::InternalLinkage;
2729
2730 case VisibleNoLinkage:
2731 case ExternalLinkage:
2732 return llvm::GlobalValue::LinkOnceODRLinkage;
2733 }
2734 llvm_unreachable("Invalid linkage!");
2735}
2736
2737/// \brief An ephemeral helper class for building MS RTTI types. It caches some
2738/// calls to the module and information about the most derived class in a
2739/// hierarchy.
2740struct MSRTTIBuilder {
2741 enum {
2742 HasBranchingHierarchy = 1,
2743 HasVirtualBranchingHierarchy = 2,
2744 HasAmbiguousBases = 4
2745 };
2746
2747 MSRTTIBuilder(MicrosoftCXXABI &ABI, const CXXRecordDecl *RD)
2748 : CGM(ABI.CGM), Context(CGM.getContext()),
2749 VMContext(CGM.getLLVMContext()), Module(CGM.getModule()), RD(RD),
2750 Linkage(getLinkageForRTTI(CGM.getContext().getTagDeclType(RD))),
2751 ABI(ABI) {}
2752
2753 llvm::GlobalVariable *getBaseClassDescriptor(const MSRTTIClass &Classes);
2754 llvm::GlobalVariable *
2755 getBaseClassArray(SmallVectorImpl<MSRTTIClass> &Classes);
2756 llvm::GlobalVariable *getClassHierarchyDescriptor();
2757 llvm::GlobalVariable *getCompleteObjectLocator(const VPtrInfo *Info);
2758
2759 CodeGenModule &CGM;
2760 ASTContext &Context;
2761 llvm::LLVMContext &VMContext;
2762 llvm::Module &Module;
2763 const CXXRecordDecl *RD;
2764 llvm::GlobalVariable::LinkageTypes Linkage;
2765 MicrosoftCXXABI &ABI;
2766};
2767
2768} // namespace
2769
2770/// \brief Recursively serializes a class hierarchy in pre-order depth first
2771/// order.
2772static void serializeClassHierarchy(SmallVectorImpl<MSRTTIClass> &Classes,
2773 const CXXRecordDecl *RD) {
2774 Classes.push_back(MSRTTIClass(RD));
2775 for (const CXXBaseSpecifier &Base : RD->bases())
2776 serializeClassHierarchy(Classes, Base.getType()->getAsCXXRecordDecl());
2777}
2778
2779/// \brief Find ambiguity among base classes.
2780static void
2781detectAmbiguousBases(SmallVectorImpl<MSRTTIClass> &Classes) {
2782 llvm::SmallPtrSet<const CXXRecordDecl *, 8> VirtualBases;
2783 llvm::SmallPtrSet<const CXXRecordDecl *, 8> UniqueBases;
2784 llvm::SmallPtrSet<const CXXRecordDecl *, 8> AmbiguousBases;
2785 for (MSRTTIClass *Class = &Classes.front(); Class <= &Classes.back();) {
2786 if ((Class->Flags & MSRTTIClass::IsVirtual) &&
Stephen Hines176edba2014-12-01 14:53:08 -08002787 !VirtualBases.insert(Class->RD).second) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002788 Class = MSRTTIClass::getNextChild(Class);
2789 continue;
2790 }
Stephen Hines176edba2014-12-01 14:53:08 -08002791 if (!UniqueBases.insert(Class->RD).second)
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002792 AmbiguousBases.insert(Class->RD);
2793 Class++;
2794 }
2795 if (AmbiguousBases.empty())
2796 return;
2797 for (MSRTTIClass &Class : Classes)
2798 if (AmbiguousBases.count(Class.RD))
2799 Class.Flags |= MSRTTIClass::IsAmbiguous;
2800}
2801
2802llvm::GlobalVariable *MSRTTIBuilder::getClassHierarchyDescriptor() {
2803 SmallString<256> MangledName;
2804 {
2805 llvm::raw_svector_ostream Out(MangledName);
2806 ABI.getMangleContext().mangleCXXRTTIClassHierarchyDescriptor(RD, Out);
2807 }
2808
2809 // Check to see if we've already declared this ClassHierarchyDescriptor.
2810 if (auto CHD = Module.getNamedGlobal(MangledName))
2811 return CHD;
2812
2813 // Serialize the class hierarchy and initialize the CHD Fields.
2814 SmallVector<MSRTTIClass, 8> Classes;
2815 serializeClassHierarchy(Classes, RD);
2816 Classes.front().initialize(/*Parent=*/nullptr, /*Specifier=*/nullptr);
2817 detectAmbiguousBases(Classes);
2818 int Flags = 0;
2819 for (auto Class : Classes) {
2820 if (Class.RD->getNumBases() > 1)
2821 Flags |= HasBranchingHierarchy;
2822 // Note: cl.exe does not calculate "HasAmbiguousBases" correctly. We
2823 // believe the field isn't actually used.
2824 if (Class.Flags & MSRTTIClass::IsAmbiguous)
2825 Flags |= HasAmbiguousBases;
2826 }
2827 if ((Flags & HasBranchingHierarchy) && RD->getNumVBases() != 0)
2828 Flags |= HasVirtualBranchingHierarchy;
2829 // These gep indices are used to get the address of the first element of the
2830 // base class array.
2831 llvm::Value *GEPIndices[] = {llvm::ConstantInt::get(CGM.IntTy, 0),
2832 llvm::ConstantInt::get(CGM.IntTy, 0)};
2833
2834 // Forward-declare the class hierarchy descriptor
2835 auto Type = ABI.getClassHierarchyDescriptorType();
2836 auto CHD = new llvm::GlobalVariable(Module, Type, /*Constant=*/true, Linkage,
2837 /*Initializer=*/nullptr,
2838 MangledName.c_str());
2839
2840 // Initialize the base class ClassHierarchyDescriptor.
2841 llvm::Constant *Fields[] = {
2842 llvm::ConstantInt::get(CGM.IntTy, 0), // Unknown
2843 llvm::ConstantInt::get(CGM.IntTy, Flags),
2844 llvm::ConstantInt::get(CGM.IntTy, Classes.size()),
2845 ABI.getImageRelativeConstant(llvm::ConstantExpr::getInBoundsGetElementPtr(
2846 getBaseClassArray(Classes),
2847 llvm::ArrayRef<llvm::Value *>(GEPIndices))),
2848 };
2849 CHD->setInitializer(llvm::ConstantStruct::get(Type, Fields));
2850 return CHD;
2851}
2852
2853llvm::GlobalVariable *
2854MSRTTIBuilder::getBaseClassArray(SmallVectorImpl<MSRTTIClass> &Classes) {
2855 SmallString<256> MangledName;
2856 {
2857 llvm::raw_svector_ostream Out(MangledName);
2858 ABI.getMangleContext().mangleCXXRTTIBaseClassArray(RD, Out);
2859 }
2860
2861 // Forward-declare the base class array.
2862 // cl.exe pads the base class array with 1 (in 32 bit mode) or 4 (in 64 bit
2863 // mode) bytes of padding. We provide a pointer sized amount of padding by
2864 // adding +1 to Classes.size(). The sections have pointer alignment and are
2865 // marked pick-any so it shouldn't matter.
2866 llvm::Type *PtrType = ABI.getImageRelativeType(
2867 ABI.getBaseClassDescriptorType()->getPointerTo());
2868 auto *ArrType = llvm::ArrayType::get(PtrType, Classes.size() + 1);
2869 auto *BCA = new llvm::GlobalVariable(
2870 Module, ArrType,
2871 /*Constant=*/true, Linkage, /*Initializer=*/nullptr, MangledName.c_str());
2872
2873 // Initialize the BaseClassArray.
2874 SmallVector<llvm::Constant *, 8> BaseClassArrayData;
2875 for (MSRTTIClass &Class : Classes)
2876 BaseClassArrayData.push_back(
2877 ABI.getImageRelativeConstant(getBaseClassDescriptor(Class)));
2878 BaseClassArrayData.push_back(llvm::Constant::getNullValue(PtrType));
2879 BCA->setInitializer(llvm::ConstantArray::get(ArrType, BaseClassArrayData));
2880 return BCA;
2881}
2882
2883llvm::GlobalVariable *
2884MSRTTIBuilder::getBaseClassDescriptor(const MSRTTIClass &Class) {
2885 // Compute the fields for the BaseClassDescriptor. They are computed up front
2886 // because they are mangled into the name of the object.
2887 uint32_t OffsetInVBTable = 0;
2888 int32_t VBPtrOffset = -1;
2889 if (Class.VirtualRoot) {
2890 auto &VTableContext = CGM.getMicrosoftVTableContext();
2891 OffsetInVBTable = VTableContext.getVBTableIndex(RD, Class.VirtualRoot) * 4;
2892 VBPtrOffset = Context.getASTRecordLayout(RD).getVBPtrOffset().getQuantity();
2893 }
2894
2895 SmallString<256> MangledName;
2896 {
2897 llvm::raw_svector_ostream Out(MangledName);
2898 ABI.getMangleContext().mangleCXXRTTIBaseClassDescriptor(
2899 Class.RD, Class.OffsetInVBase, VBPtrOffset, OffsetInVBTable,
2900 Class.Flags, Out);
2901 }
2902
2903 // Check to see if we've already declared this object.
2904 if (auto BCD = Module.getNamedGlobal(MangledName))
2905 return BCD;
2906
2907 // Forward-declare the base class descriptor.
2908 auto Type = ABI.getBaseClassDescriptorType();
2909 auto BCD = new llvm::GlobalVariable(Module, Type, /*Constant=*/true, Linkage,
2910 /*Initializer=*/nullptr,
2911 MangledName.c_str());
2912
2913 // Initialize the BaseClassDescriptor.
2914 llvm::Constant *Fields[] = {
2915 ABI.getImageRelativeConstant(
2916 ABI.getAddrOfRTTIDescriptor(Context.getTypeDeclType(Class.RD))),
2917 llvm::ConstantInt::get(CGM.IntTy, Class.NumBases),
2918 llvm::ConstantInt::get(CGM.IntTy, Class.OffsetInVBase),
2919 llvm::ConstantInt::get(CGM.IntTy, VBPtrOffset),
2920 llvm::ConstantInt::get(CGM.IntTy, OffsetInVBTable),
2921 llvm::ConstantInt::get(CGM.IntTy, Class.Flags),
2922 ABI.getImageRelativeConstant(
2923 MSRTTIBuilder(ABI, Class.RD).getClassHierarchyDescriptor()),
2924 };
2925 BCD->setInitializer(llvm::ConstantStruct::get(Type, Fields));
2926 return BCD;
2927}
2928
2929llvm::GlobalVariable *
2930MSRTTIBuilder::getCompleteObjectLocator(const VPtrInfo *Info) {
2931 SmallString<256> MangledName;
2932 {
2933 llvm::raw_svector_ostream Out(MangledName);
2934 ABI.getMangleContext().mangleCXXRTTICompleteObjectLocator(RD, Info->MangledPath, Out);
2935 }
2936
2937 // Check to see if we've already computed this complete object locator.
2938 if (auto COL = Module.getNamedGlobal(MangledName))
2939 return COL;
2940
2941 // Compute the fields of the complete object locator.
2942 int OffsetToTop = Info->FullOffsetInMDC.getQuantity();
2943 int VFPtrOffset = 0;
2944 // The offset includes the vtordisp if one exists.
2945 if (const CXXRecordDecl *VBase = Info->getVBaseWithVPtr())
2946 if (Context.getASTRecordLayout(RD)
2947 .getVBaseOffsetsMap()
2948 .find(VBase)
2949 ->second.hasVtorDisp())
2950 VFPtrOffset = Info->NonVirtualOffset.getQuantity() + 4;
2951
2952 // Forward-declare the complete object locator.
2953 llvm::StructType *Type = ABI.getCompleteObjectLocatorType();
2954 auto COL = new llvm::GlobalVariable(Module, Type, /*Constant=*/true, Linkage,
2955 /*Initializer=*/nullptr, MangledName.c_str());
2956
2957 // Initialize the CompleteObjectLocator.
2958 llvm::Constant *Fields[] = {
2959 llvm::ConstantInt::get(CGM.IntTy, ABI.isImageRelative()),
2960 llvm::ConstantInt::get(CGM.IntTy, OffsetToTop),
2961 llvm::ConstantInt::get(CGM.IntTy, VFPtrOffset),
2962 ABI.getImageRelativeConstant(
2963 CGM.GetAddrOfRTTIDescriptor(Context.getTypeDeclType(RD))),
2964 ABI.getImageRelativeConstant(getClassHierarchyDescriptor()),
2965 ABI.getImageRelativeConstant(COL),
2966 };
2967 llvm::ArrayRef<llvm::Constant *> FieldsRef(Fields);
2968 if (!ABI.isImageRelative())
2969 FieldsRef = FieldsRef.drop_back();
2970 COL->setInitializer(llvm::ConstantStruct::get(Type, FieldsRef));
2971 return COL;
2972}
2973
2974/// \brief Gets a TypeDescriptor. Returns a llvm::Constant * rather than a
2975/// llvm::GlobalVariable * because different type descriptors have different
2976/// types, and need to be abstracted. They are abstracting by casting the
2977/// address to an Int8PtrTy.
2978llvm::Constant *MicrosoftCXXABI::getAddrOfRTTIDescriptor(QualType Type) {
2979 SmallString<256> MangledName, TypeInfoString;
2980 {
2981 llvm::raw_svector_ostream Out(MangledName);
2982 getMangleContext().mangleCXXRTTI(Type, Out);
2983 }
2984
2985 // Check to see if we've already declared this TypeDescriptor.
2986 if (llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(MangledName))
2987 return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy);
2988
2989 // Compute the fields for the TypeDescriptor.
2990 {
2991 llvm::raw_svector_ostream Out(TypeInfoString);
2992 getMangleContext().mangleCXXRTTIName(Type, Out);
2993 }
2994
2995 // Declare and initialize the TypeDescriptor.
2996 llvm::Constant *Fields[] = {
2997 getTypeInfoVTable(CGM), // VFPtr
2998 llvm::ConstantPointerNull::get(CGM.Int8PtrTy), // Runtime data
2999 llvm::ConstantDataArray::getString(CGM.getLLVMContext(), TypeInfoString)};
3000 llvm::StructType *TypeDescriptorType =
3001 getTypeDescriptorType(TypeInfoString);
3002 return llvm::ConstantExpr::getBitCast(
3003 new llvm::GlobalVariable(
3004 CGM.getModule(), TypeDescriptorType, /*Constant=*/false,
3005 getLinkageForRTTI(Type),
3006 llvm::ConstantStruct::get(TypeDescriptorType, Fields),
3007 MangledName.c_str()),
3008 CGM.Int8PtrTy);
3009}
3010
3011/// \brief Gets or a creates a Microsoft CompleteObjectLocator.
3012llvm::GlobalVariable *
3013MicrosoftCXXABI::getMSCompleteObjectLocator(const CXXRecordDecl *RD,
3014 const VPtrInfo *Info) {
3015 return MSRTTIBuilder(*this, RD).getCompleteObjectLocator(Info);
3016}
Stephen Hines176edba2014-12-01 14:53:08 -08003017
3018static void emitCXXConstructor(CodeGenModule &CGM,
3019 const CXXConstructorDecl *ctor,
3020 StructorType ctorType) {
3021 // There are no constructor variants, always emit the complete destructor.
3022 CGM.codegenCXXStructor(ctor, StructorType::Complete);
3023}
3024
3025static void emitCXXDestructor(CodeGenModule &CGM, const CXXDestructorDecl *dtor,
3026 StructorType dtorType) {
3027 // The complete destructor is equivalent to the base destructor for
3028 // classes with no virtual bases, so try to emit it as an alias.
3029 if (!dtor->getParent()->getNumVBases() &&
3030 (dtorType == StructorType::Complete || dtorType == StructorType::Base)) {
3031 bool ProducedAlias = !CGM.TryEmitDefinitionAsAlias(
3032 GlobalDecl(dtor, Dtor_Complete), GlobalDecl(dtor, Dtor_Base), true);
3033 if (ProducedAlias) {
3034 if (dtorType == StructorType::Complete)
3035 return;
3036 if (dtor->isVirtual())
3037 CGM.getVTables().EmitThunks(GlobalDecl(dtor, Dtor_Complete));
3038 }
3039 }
3040
3041 // The base destructor is equivalent to the base destructor of its
3042 // base class if there is exactly one non-virtual base class with a
3043 // non-trivial destructor, there are no fields with a non-trivial
3044 // destructor, and the body of the destructor is trivial.
3045 if (dtorType == StructorType::Base && !CGM.TryEmitBaseDestructorAsAlias(dtor))
3046 return;
3047
3048 CGM.codegenCXXStructor(dtor, dtorType);
3049}
3050
3051void MicrosoftCXXABI::emitCXXStructor(const CXXMethodDecl *MD,
3052 StructorType Type) {
3053 if (auto *CD = dyn_cast<CXXConstructorDecl>(MD)) {
3054 emitCXXConstructor(CGM, CD, Type);
3055 return;
3056 }
3057 emitCXXDestructor(CGM, cast<CXXDestructorDecl>(MD), Type);
3058}