blob: b7610550434649954c1dd79c0b427f63e463f885 [file] [log] [blame]
Charles Davis3a811f12010-05-25 19:52:27 +00001//===------- ItaniumCXXABI.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 Itanium C++ ABI. The class
Charles Davis3a811f12010-05-25 19:52:27 +000011// in this file generates structures that follow the Itanium C++ ABI, which is
12// documented at:
13// http://www.codesourcery.com/public/cxx-abi/abi.html
14// http://www.codesourcery.com/public/cxx-abi/abi-eh.html
John McCallee79a4c2010-08-21 22:46:04 +000015//
16// It also supports the closely-related ARM ABI, documented at:
17// http://infocenter.arm.com/help/topic/com.arm.doc.ihi0041c/IHI0041C_cppabi.pdf
18//
Charles Davis3a811f12010-05-25 19:52:27 +000019//===----------------------------------------------------------------------===//
20
21#include "CGCXXABI.h"
John McCall0bab0cd2010-08-23 01:21:21 +000022#include "CGRecordLayout.h"
Charles Davis9ee494f2012-06-23 23:44:00 +000023#include "CGVTables.h"
John McCall93d557b2010-08-22 00:05:51 +000024#include "CodeGenFunction.h"
Charles Davis3a811f12010-05-25 19:52:27 +000025#include "CodeGenModule.h"
Craig Topperba77cb92012-09-15 18:47:51 +000026#include "clang/AST/Mangle.h"
27#include "clang/AST/Type.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000028#include "llvm/IR/DataLayout.h"
29#include "llvm/IR/Intrinsics.h"
30#include "llvm/IR/Value.h"
Charles Davis3a811f12010-05-25 19:52:27 +000031
32using namespace clang;
John McCall93d557b2010-08-22 00:05:51 +000033using namespace CodeGen;
Charles Davis3a811f12010-05-25 19:52:27 +000034
35namespace {
Charles Davis071cc7d2010-08-16 03:33:14 +000036class ItaniumCXXABI : public CodeGen::CGCXXABI {
John McCall0bab0cd2010-08-23 01:21:21 +000037private:
Chris Lattner9cbe4f02011-07-09 17:41:47 +000038 llvm::IntegerType *PtrDiffTy;
John McCall93d557b2010-08-22 00:05:51 +000039protected:
John McCallbabc9a92010-08-22 00:59:17 +000040 bool IsARM;
John McCall0bab0cd2010-08-23 01:21:21 +000041
42 // It's a little silly for us to cache this.
Chris Lattner9cbe4f02011-07-09 17:41:47 +000043 llvm::IntegerType *getPtrDiffTy() {
John McCall0bab0cd2010-08-23 01:21:21 +000044 if (!PtrDiffTy) {
John McCall9cb2cee2010-09-02 10:25:57 +000045 QualType T = getContext().getPointerDiffType();
Chris Lattner9cbe4f02011-07-09 17:41:47 +000046 llvm::Type *Ty = CGM.getTypes().ConvertType(T);
John McCall0bab0cd2010-08-23 01:21:21 +000047 PtrDiffTy = cast<llvm::IntegerType>(Ty);
48 }
49 return PtrDiffTy;
50 }
51
Charles Davis3a811f12010-05-25 19:52:27 +000052public:
John McCallbabc9a92010-08-22 00:59:17 +000053 ItaniumCXXABI(CodeGen::CodeGenModule &CGM, bool IsARM = false) :
Peter Collingbourne14110472011-01-13 18:57:25 +000054 CGCXXABI(CGM), PtrDiffTy(0), IsARM(IsARM) { }
John McCall93d557b2010-08-22 00:05:51 +000055
John McCallf16aa102010-08-22 21:01:12 +000056 bool isZeroInitializable(const MemberPointerType *MPT);
John McCallcf2c85e2010-08-22 04:16:24 +000057
Chris Lattner9cbe4f02011-07-09 17:41:47 +000058 llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT);
John McCall0bab0cd2010-08-23 01:21:21 +000059
John McCall93d557b2010-08-22 00:05:51 +000060 llvm::Value *EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
61 llvm::Value *&This,
62 llvm::Value *MemFnPtr,
63 const MemberPointerType *MPT);
John McCall3023def2010-08-22 03:04:22 +000064
John McCall6c2ab1d2010-08-31 21:07:20 +000065 llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF,
66 llvm::Value *Base,
67 llvm::Value *MemPtr,
68 const MemberPointerType *MPT);
69
John McCall0bab0cd2010-08-23 01:21:21 +000070 llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
71 const CastExpr *E,
72 llvm::Value *Src);
John McCall4d4e5c12012-02-15 01:22:51 +000073 llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
74 llvm::Constant *Src);
John McCallcf2c85e2010-08-22 04:16:24 +000075
John McCall0bab0cd2010-08-23 01:21:21 +000076 llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
John McCallcf2c85e2010-08-22 04:16:24 +000077
John McCall755d8492011-04-12 00:42:48 +000078 llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
John McCall5808ce42011-02-03 08:15:49 +000079 llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
80 CharUnits offset);
Richard Smith2d6a5672012-01-14 04:30:29 +000081 llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT);
82 llvm::Constant *BuildMemberPointer(const CXXMethodDecl *MD,
83 CharUnits ThisAdjustment);
John McCall875ab102010-08-22 06:43:33 +000084
John McCall0bab0cd2010-08-23 01:21:21 +000085 llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF,
86 llvm::Value *L,
87 llvm::Value *R,
88 const MemberPointerType *MPT,
89 bool Inequality);
John McCalle9fd7eb2010-08-22 08:30:07 +000090
John McCall0bab0cd2010-08-23 01:21:21 +000091 llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
92 llvm::Value *Addr,
93 const MemberPointerType *MPT);
John McCall4c40d982010-08-31 07:33:07 +000094
John McCallecd03b42012-09-25 10:10:39 +000095 llvm::Value *adjustToCompleteObject(CodeGenFunction &CGF,
96 llvm::Value *ptr,
97 QualType type);
98
John McCall4c40d982010-08-31 07:33:07 +000099 void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
100 CXXCtorType T,
101 CanQualType &ResTy,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000102 SmallVectorImpl<CanQualType> &ArgTys);
John McCall4c40d982010-08-31 07:33:07 +0000103
104 void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
105 CXXDtorType T,
106 CanQualType &ResTy,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000107 SmallVectorImpl<CanQualType> &ArgTys);
John McCall4c40d982010-08-31 07:33:07 +0000108
109 void BuildInstanceFunctionParams(CodeGenFunction &CGF,
110 QualType &ResTy,
111 FunctionArgList &Params);
112
113 void EmitInstanceFunctionProlog(CodeGenFunction &CGF);
John McCall1e7fe752010-09-02 09:58:18 +0000114
Joao Matos285baac2012-07-17 17:10:11 +0000115 StringRef GetPureVirtualCallName() { return "__cxa_pure_virtual"; }
David Blaikie2eb9a952012-10-16 22:56:05 +0000116 StringRef GetDeletedVirtualCallName() { return "__cxa_deleted_virtual"; }
Joao Matos285baac2012-07-17 17:10:11 +0000117
John McCalle2b45e22012-05-01 05:23:51 +0000118 CharUnits getArrayCookieSizeImpl(QualType elementType);
John McCall1e7fe752010-09-02 09:58:18 +0000119 llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
120 llvm::Value *NewPtr,
121 llvm::Value *NumElements,
John McCall6ec278d2011-01-27 09:37:56 +0000122 const CXXNewExpr *expr,
John McCall1e7fe752010-09-02 09:58:18 +0000123 QualType ElementType);
John McCalle2b45e22012-05-01 05:23:51 +0000124 llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF,
125 llvm::Value *allocPtr,
126 CharUnits cookieSize);
John McCall5cd91b52010-09-08 01:44:27 +0000127
John McCall3030eb82010-11-06 09:44:32 +0000128 void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
Chandler Carruth0f30a122012-03-30 19:44:53 +0000129 llvm::GlobalVariable *DeclPtr, bool PerformInit);
John McCall20bb1752012-05-01 06:13:13 +0000130 void registerGlobalDtor(CodeGenFunction &CGF, llvm::Constant *dtor,
131 llvm::Constant *addr);
Charles Davis9ee494f2012-06-23 23:44:00 +0000132
133 void EmitVTables(const CXXRecordDecl *Class);
Charles Davis3a811f12010-05-25 19:52:27 +0000134};
John McCallee79a4c2010-08-21 22:46:04 +0000135
136class ARMCXXABI : public ItaniumCXXABI {
137public:
John McCallbabc9a92010-08-22 00:59:17 +0000138 ARMCXXABI(CodeGen::CodeGenModule &CGM) : ItaniumCXXABI(CGM, /*ARM*/ true) {}
John McCall4c40d982010-08-31 07:33:07 +0000139
140 void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
141 CXXCtorType T,
142 CanQualType &ResTy,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000143 SmallVectorImpl<CanQualType> &ArgTys);
John McCall4c40d982010-08-31 07:33:07 +0000144
145 void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
146 CXXDtorType T,
147 CanQualType &ResTy,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000148 SmallVectorImpl<CanQualType> &ArgTys);
John McCall4c40d982010-08-31 07:33:07 +0000149
150 void BuildInstanceFunctionParams(CodeGenFunction &CGF,
151 QualType &ResTy,
152 FunctionArgList &Params);
153
154 void EmitInstanceFunctionProlog(CodeGenFunction &CGF);
155
156 void EmitReturnFromThunk(CodeGenFunction &CGF, RValue RV, QualType ResTy);
157
John McCalle2b45e22012-05-01 05:23:51 +0000158 CharUnits getArrayCookieSizeImpl(QualType elementType);
John McCall1e7fe752010-09-02 09:58:18 +0000159 llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
160 llvm::Value *NewPtr,
161 llvm::Value *NumElements,
John McCall6ec278d2011-01-27 09:37:56 +0000162 const CXXNewExpr *expr,
John McCall1e7fe752010-09-02 09:58:18 +0000163 QualType ElementType);
John McCalle2b45e22012-05-01 05:23:51 +0000164 llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, llvm::Value *allocPtr,
165 CharUnits cookieSize);
John McCall4c40d982010-08-31 07:33:07 +0000166
167private:
168 /// \brief Returns true if the given instance method is one of the
169 /// kinds that the ARM ABI says returns 'this'.
170 static bool HasThisReturn(GlobalDecl GD) {
171 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
172 return ((isa<CXXDestructorDecl>(MD) && GD.getDtorType() != Dtor_Deleting) ||
173 (isa<CXXConstructorDecl>(MD)));
174 }
John McCallee79a4c2010-08-21 22:46:04 +0000175};
Charles Davis3a811f12010-05-25 19:52:27 +0000176}
177
Charles Davis071cc7d2010-08-16 03:33:14 +0000178CodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) {
Charles Davis3a811f12010-05-25 19:52:27 +0000179 return new ItaniumCXXABI(CGM);
180}
181
John McCallee79a4c2010-08-21 22:46:04 +0000182CodeGen::CGCXXABI *CodeGen::CreateARMCXXABI(CodeGenModule &CGM) {
183 return new ARMCXXABI(CGM);
184}
185
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000186llvm::Type *
John McCall0bab0cd2010-08-23 01:21:21 +0000187ItaniumCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) {
188 if (MPT->isMemberDataPointer())
189 return getPtrDiffTy();
Chris Lattner7650d952011-06-18 22:49:11 +0000190 return llvm::StructType::get(getPtrDiffTy(), getPtrDiffTy(), NULL);
John McCall875ab102010-08-22 06:43:33 +0000191}
192
John McCallbabc9a92010-08-22 00:59:17 +0000193/// In the Itanium and ARM ABIs, method pointers have the form:
194/// struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr;
195///
196/// In the Itanium ABI:
197/// - method pointers are virtual if (memptr.ptr & 1) is nonzero
198/// - the this-adjustment is (memptr.adj)
199/// - the virtual offset is (memptr.ptr - 1)
200///
201/// In the ARM ABI:
202/// - method pointers are virtual if (memptr.adj & 1) is nonzero
203/// - the this-adjustment is (memptr.adj >> 1)
204/// - the virtual offset is (memptr.ptr)
205/// ARM uses 'adj' for the virtual flag because Thumb functions
206/// may be only single-byte aligned.
207///
208/// If the member is virtual, the adjusted 'this' pointer points
209/// to a vtable pointer from which the virtual offset is applied.
210///
211/// If the member is non-virtual, memptr.ptr is the address of
212/// the function to call.
John McCall93d557b2010-08-22 00:05:51 +0000213llvm::Value *
214ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
215 llvm::Value *&This,
216 llvm::Value *MemFnPtr,
217 const MemberPointerType *MPT) {
218 CGBuilderTy &Builder = CGF.Builder;
219
220 const FunctionProtoType *FPT =
221 MPT->getPointeeType()->getAs<FunctionProtoType>();
222 const CXXRecordDecl *RD =
223 cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
224
Chris Lattner2acc6e32011-07-18 04:24:23 +0000225 llvm::FunctionType *FTy =
John McCallde5d3c72012-02-17 03:33:10 +0000226 CGM.getTypes().GetFunctionType(
227 CGM.getTypes().arrangeCXXMethodType(RD, FPT));
John McCall93d557b2010-08-22 00:05:51 +0000228
Chris Lattner2acc6e32011-07-18 04:24:23 +0000229 llvm::IntegerType *ptrdiff = getPtrDiffTy();
John McCallbabc9a92010-08-22 00:59:17 +0000230 llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(ptrdiff, 1);
John McCall93d557b2010-08-22 00:05:51 +0000231
John McCallbabc9a92010-08-22 00:59:17 +0000232 llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual");
233 llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual");
234 llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end");
235
John McCalld608cdb2010-08-22 10:59:02 +0000236 // Extract memptr.adj, which is in the second field.
237 llvm::Value *RawAdj = Builder.CreateExtractValue(MemFnPtr, 1, "memptr.adj");
John McCallbabc9a92010-08-22 00:59:17 +0000238
239 // Compute the true adjustment.
240 llvm::Value *Adj = RawAdj;
241 if (IsARM)
242 Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted");
John McCall93d557b2010-08-22 00:05:51 +0000243
244 // Apply the adjustment and cast back to the original struct type
245 // for consistency.
John McCallbabc9a92010-08-22 00:59:17 +0000246 llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy());
247 Ptr = Builder.CreateInBoundsGEP(Ptr, Adj);
248 This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted");
John McCall93d557b2010-08-22 00:05:51 +0000249
250 // Load the function pointer.
John McCalld608cdb2010-08-22 10:59:02 +0000251 llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr");
John McCall93d557b2010-08-22 00:05:51 +0000252
253 // If the LSB in the function pointer is 1, the function pointer points to
254 // a virtual function.
John McCallbabc9a92010-08-22 00:59:17 +0000255 llvm::Value *IsVirtual;
256 if (IsARM)
257 IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1);
258 else
259 IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1);
260 IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual");
John McCall93d557b2010-08-22 00:05:51 +0000261 Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
262
263 // In the virtual path, the adjustment left 'This' pointing to the
264 // vtable of the correct base subobject. The "function pointer" is an
John McCallbabc9a92010-08-22 00:59:17 +0000265 // offset within the vtable (+1 for the virtual flag on non-ARM).
John McCall93d557b2010-08-22 00:05:51 +0000266 CGF.EmitBlock(FnVirtual);
267
268 // Cast the adjusted this to a pointer to vtable pointer and load.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000269 llvm::Type *VTableTy = Builder.getInt8PtrTy();
John McCall93d557b2010-08-22 00:05:51 +0000270 llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy->getPointerTo());
John McCallbabc9a92010-08-22 00:59:17 +0000271 VTable = Builder.CreateLoad(VTable, "memptr.vtable");
John McCall93d557b2010-08-22 00:05:51 +0000272
273 // Apply the offset.
John McCallbabc9a92010-08-22 00:59:17 +0000274 llvm::Value *VTableOffset = FnAsInt;
275 if (!IsARM) VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1);
276 VTable = Builder.CreateGEP(VTable, VTableOffset);
John McCall93d557b2010-08-22 00:05:51 +0000277
278 // Load the virtual function to call.
279 VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo());
John McCallbabc9a92010-08-22 00:59:17 +0000280 llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "memptr.virtualfn");
John McCall93d557b2010-08-22 00:05:51 +0000281 CGF.EmitBranch(FnEnd);
282
283 // In the non-virtual path, the function pointer is actually a
284 // function pointer.
285 CGF.EmitBlock(FnNonVirtual);
286 llvm::Value *NonVirtualFn =
John McCallbabc9a92010-08-22 00:59:17 +0000287 Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn");
John McCall93d557b2010-08-22 00:05:51 +0000288
289 // We're done.
290 CGF.EmitBlock(FnEnd);
Jay Foadbbf3bac2011-03-30 11:28:58 +0000291 llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo(), 2);
John McCall93d557b2010-08-22 00:05:51 +0000292 Callee->addIncoming(VirtualFn, FnVirtual);
293 Callee->addIncoming(NonVirtualFn, FnNonVirtual);
294 return Callee;
295}
John McCall3023def2010-08-22 03:04:22 +0000296
John McCall6c2ab1d2010-08-31 21:07:20 +0000297/// Compute an l-value by applying the given pointer-to-member to a
298/// base object.
299llvm::Value *ItaniumCXXABI::EmitMemberDataPointerAddress(CodeGenFunction &CGF,
300 llvm::Value *Base,
301 llvm::Value *MemPtr,
302 const MemberPointerType *MPT) {
303 assert(MemPtr->getType() == getPtrDiffTy());
304
305 CGBuilderTy &Builder = CGF.Builder;
306
Micah Villmow956a5a12012-10-25 15:39:14 +0000307 unsigned AS = Base->getType()->getPointerAddressSpace();
John McCall6c2ab1d2010-08-31 21:07:20 +0000308
309 // Cast to char*.
310 Base = Builder.CreateBitCast(Base, Builder.getInt8Ty()->getPointerTo(AS));
311
312 // Apply the offset, which we assume is non-null.
313 llvm::Value *Addr = Builder.CreateInBoundsGEP(Base, MemPtr, "memptr.offset");
314
315 // Cast the address to the appropriate pointer type, adopting the
316 // address space of the base pointer.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000317 llvm::Type *PType
Douglas Gregoreede61a2010-09-02 17:38:50 +0000318 = CGF.ConvertTypeForMem(MPT->getPointeeType())->getPointerTo(AS);
John McCall6c2ab1d2010-08-31 21:07:20 +0000319 return Builder.CreateBitCast(Addr, PType);
320}
321
John McCall4d4e5c12012-02-15 01:22:51 +0000322/// Perform a bitcast, derived-to-base, or base-to-derived member pointer
323/// conversion.
324///
325/// Bitcast conversions are always a no-op under Itanium.
John McCall0bab0cd2010-08-23 01:21:21 +0000326///
327/// Obligatory offset/adjustment diagram:
328/// <-- offset --> <-- adjustment -->
329/// |--------------------------|----------------------|--------------------|
330/// ^Derived address point ^Base address point ^Member address point
331///
332/// So when converting a base member pointer to a derived member pointer,
333/// we add the offset to the adjustment because the address point has
334/// decreased; and conversely, when converting a derived MP to a base MP
335/// we subtract the offset from the adjustment because the address point
336/// has increased.
337///
338/// The standard forbids (at compile time) conversion to and from
339/// virtual bases, which is why we don't have to consider them here.
340///
341/// The standard forbids (at run time) casting a derived MP to a base
342/// MP when the derived MP does not point to a member of the base.
343/// This is why -1 is a reasonable choice for null data member
344/// pointers.
John McCalld608cdb2010-08-22 10:59:02 +0000345llvm::Value *
John McCall0bab0cd2010-08-23 01:21:21 +0000346ItaniumCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF,
347 const CastExpr *E,
John McCall4d4e5c12012-02-15 01:22:51 +0000348 llvm::Value *src) {
John McCall2de56d12010-08-25 11:45:40 +0000349 assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
John McCall4d4e5c12012-02-15 01:22:51 +0000350 E->getCastKind() == CK_BaseToDerivedMemberPointer ||
351 E->getCastKind() == CK_ReinterpretMemberPointer);
352
353 // Under Itanium, reinterprets don't require any additional processing.
354 if (E->getCastKind() == CK_ReinterpretMemberPointer) return src;
355
356 // Use constant emission if we can.
357 if (isa<llvm::Constant>(src))
358 return EmitMemberPointerConversion(E, cast<llvm::Constant>(src));
359
360 llvm::Constant *adj = getMemberPointerAdjustment(E);
361 if (!adj) return src;
John McCall3023def2010-08-22 03:04:22 +0000362
363 CGBuilderTy &Builder = CGF.Builder;
John McCall4d4e5c12012-02-15 01:22:51 +0000364 bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
John McCall3023def2010-08-22 03:04:22 +0000365
John McCall4d4e5c12012-02-15 01:22:51 +0000366 const MemberPointerType *destTy =
367 E->getType()->castAs<MemberPointerType>();
John McCall875ab102010-08-22 06:43:33 +0000368
John McCall0bab0cd2010-08-23 01:21:21 +0000369 // For member data pointers, this is just a matter of adding the
370 // offset if the source is non-null.
John McCall4d4e5c12012-02-15 01:22:51 +0000371 if (destTy->isMemberDataPointer()) {
372 llvm::Value *dst;
373 if (isDerivedToBase)
374 dst = Builder.CreateNSWSub(src, adj, "adj");
John McCall0bab0cd2010-08-23 01:21:21 +0000375 else
John McCall4d4e5c12012-02-15 01:22:51 +0000376 dst = Builder.CreateNSWAdd(src, adj, "adj");
John McCall0bab0cd2010-08-23 01:21:21 +0000377
378 // Null check.
John McCall4d4e5c12012-02-15 01:22:51 +0000379 llvm::Value *null = llvm::Constant::getAllOnesValue(src->getType());
380 llvm::Value *isNull = Builder.CreateICmpEQ(src, null, "memptr.isnull");
381 return Builder.CreateSelect(isNull, src, dst);
John McCall0bab0cd2010-08-23 01:21:21 +0000382 }
383
John McCalld608cdb2010-08-22 10:59:02 +0000384 // The this-adjustment is left-shifted by 1 on ARM.
385 if (IsARM) {
John McCall4d4e5c12012-02-15 01:22:51 +0000386 uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue();
387 offset <<= 1;
388 adj = llvm::ConstantInt::get(adj->getType(), offset);
John McCalld608cdb2010-08-22 10:59:02 +0000389 }
390
John McCall4d4e5c12012-02-15 01:22:51 +0000391 llvm::Value *srcAdj = Builder.CreateExtractValue(src, 1, "src.adj");
392 llvm::Value *dstAdj;
393 if (isDerivedToBase)
394 dstAdj = Builder.CreateNSWSub(srcAdj, adj, "adj");
John McCalld608cdb2010-08-22 10:59:02 +0000395 else
John McCall4d4e5c12012-02-15 01:22:51 +0000396 dstAdj = Builder.CreateNSWAdd(srcAdj, adj, "adj");
John McCalld608cdb2010-08-22 10:59:02 +0000397
John McCall4d4e5c12012-02-15 01:22:51 +0000398 return Builder.CreateInsertValue(src, dstAdj, 1);
399}
400
401llvm::Constant *
402ItaniumCXXABI::EmitMemberPointerConversion(const CastExpr *E,
403 llvm::Constant *src) {
404 assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
405 E->getCastKind() == CK_BaseToDerivedMemberPointer ||
406 E->getCastKind() == CK_ReinterpretMemberPointer);
407
408 // Under Itanium, reinterprets don't require any additional processing.
409 if (E->getCastKind() == CK_ReinterpretMemberPointer) return src;
410
411 // If the adjustment is trivial, we don't need to do anything.
412 llvm::Constant *adj = getMemberPointerAdjustment(E);
413 if (!adj) return src;
414
415 bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
416
417 const MemberPointerType *destTy =
418 E->getType()->castAs<MemberPointerType>();
419
420 // For member data pointers, this is just a matter of adding the
421 // offset if the source is non-null.
422 if (destTy->isMemberDataPointer()) {
423 // null maps to null.
424 if (src->isAllOnesValue()) return src;
425
426 if (isDerivedToBase)
427 return llvm::ConstantExpr::getNSWSub(src, adj);
428 else
429 return llvm::ConstantExpr::getNSWAdd(src, adj);
430 }
431
432 // The this-adjustment is left-shifted by 1 on ARM.
433 if (IsARM) {
434 uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue();
435 offset <<= 1;
436 adj = llvm::ConstantInt::get(adj->getType(), offset);
437 }
438
439 llvm::Constant *srcAdj = llvm::ConstantExpr::getExtractValue(src, 1);
440 llvm::Constant *dstAdj;
441 if (isDerivedToBase)
442 dstAdj = llvm::ConstantExpr::getNSWSub(srcAdj, adj);
443 else
444 dstAdj = llvm::ConstantExpr::getNSWAdd(srcAdj, adj);
445
446 return llvm::ConstantExpr::getInsertValue(src, dstAdj, 1);
John McCall3023def2010-08-22 03:04:22 +0000447}
John McCallcf2c85e2010-08-22 04:16:24 +0000448
449llvm::Constant *
John McCall0bab0cd2010-08-23 01:21:21 +0000450ItaniumCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) {
Chris Lattner2acc6e32011-07-18 04:24:23 +0000451 llvm::Type *ptrdiff_t = getPtrDiffTy();
John McCall0bab0cd2010-08-23 01:21:21 +0000452
453 // Itanium C++ ABI 2.3:
454 // A NULL pointer is represented as -1.
455 if (MPT->isMemberDataPointer())
456 return llvm::ConstantInt::get(ptrdiff_t, -1ULL, /*isSigned=*/true);
John McCalld608cdb2010-08-22 10:59:02 +0000457
458 llvm::Constant *Zero = llvm::ConstantInt::get(ptrdiff_t, 0);
459 llvm::Constant *Values[2] = { Zero, Zero };
Chris Lattnerc5cbb902011-06-20 04:01:35 +0000460 return llvm::ConstantStruct::getAnon(Values);
John McCallcf2c85e2010-08-22 04:16:24 +0000461}
462
John McCall5808ce42011-02-03 08:15:49 +0000463llvm::Constant *
464ItaniumCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT,
465 CharUnits offset) {
John McCall0bab0cd2010-08-23 01:21:21 +0000466 // Itanium C++ ABI 2.3:
467 // A pointer to data member is an offset from the base address of
468 // the class object containing it, represented as a ptrdiff_t
John McCall5808ce42011-02-03 08:15:49 +0000469 return llvm::ConstantInt::get(getPtrDiffTy(), offset.getQuantity());
John McCall0bab0cd2010-08-23 01:21:21 +0000470}
471
John McCall755d8492011-04-12 00:42:48 +0000472llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) {
Richard Smith2d6a5672012-01-14 04:30:29 +0000473 return BuildMemberPointer(MD, CharUnits::Zero());
474}
475
476llvm::Constant *ItaniumCXXABI::BuildMemberPointer(const CXXMethodDecl *MD,
477 CharUnits ThisAdjustment) {
John McCalld608cdb2010-08-22 10:59:02 +0000478 assert(MD->isInstance() && "Member function must not be static!");
479 MD = MD->getCanonicalDecl();
480
481 CodeGenTypes &Types = CGM.getTypes();
Chris Lattner2acc6e32011-07-18 04:24:23 +0000482 llvm::Type *ptrdiff_t = getPtrDiffTy();
John McCalld608cdb2010-08-22 10:59:02 +0000483
484 // Get the function pointer (or index if this is a virtual function).
485 llvm::Constant *MemPtr[2];
486 if (MD->isVirtual()) {
Peter Collingbourne1d2b3172011-09-26 01:56:30 +0000487 uint64_t Index = CGM.getVTableContext().getMethodVTableIndex(MD);
John McCalld608cdb2010-08-22 10:59:02 +0000488
Ken Dyck1246ba62011-04-09 01:30:02 +0000489 const ASTContext &Context = getContext();
490 CharUnits PointerWidth =
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000491 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
Ken Dyck1246ba62011-04-09 01:30:02 +0000492 uint64_t VTableOffset = (Index * PointerWidth.getQuantity());
John McCalld608cdb2010-08-22 10:59:02 +0000493
494 if (IsARM) {
495 // ARM C++ ABI 3.2.1:
496 // This ABI specifies that adj contains twice the this
497 // adjustment, plus 1 if the member function is virtual. The
498 // least significant bit of adj then makes exactly the same
499 // discrimination as the least significant bit of ptr does for
500 // Itanium.
501 MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset);
Richard Smith2d6a5672012-01-14 04:30:29 +0000502 MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t,
503 2 * ThisAdjustment.getQuantity() + 1);
John McCalld608cdb2010-08-22 10:59:02 +0000504 } else {
505 // Itanium C++ ABI 2.3:
506 // For a virtual function, [the pointer field] is 1 plus the
507 // virtual table offset (in bytes) of the function,
508 // represented as a ptrdiff_t.
509 MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset + 1);
Richard Smith2d6a5672012-01-14 04:30:29 +0000510 MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t,
511 ThisAdjustment.getQuantity());
John McCalld608cdb2010-08-22 10:59:02 +0000512 }
513 } else {
John McCall755d8492011-04-12 00:42:48 +0000514 const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
Chris Lattner2acc6e32011-07-18 04:24:23 +0000515 llvm::Type *Ty;
John McCall755d8492011-04-12 00:42:48 +0000516 // Check whether the function has a computable LLVM signature.
Chris Lattnerf742eb02011-07-10 00:18:59 +0000517 if (Types.isFuncTypeConvertible(FPT)) {
John McCall755d8492011-04-12 00:42:48 +0000518 // The function has a computable LLVM signature; use the correct type.
John McCallde5d3c72012-02-17 03:33:10 +0000519 Ty = Types.GetFunctionType(Types.arrangeCXXMethodDeclaration(MD));
John McCalld608cdb2010-08-22 10:59:02 +0000520 } else {
John McCall755d8492011-04-12 00:42:48 +0000521 // Use an arbitrary non-function type to tell GetAddrOfFunction that the
522 // function type is incomplete.
523 Ty = ptrdiff_t;
John McCalld608cdb2010-08-22 10:59:02 +0000524 }
John McCall755d8492011-04-12 00:42:48 +0000525 llvm::Constant *addr = CGM.GetAddrOfFunction(MD, Ty);
John McCalld608cdb2010-08-22 10:59:02 +0000526
John McCall379b5152011-04-11 07:02:50 +0000527 MemPtr[0] = llvm::ConstantExpr::getPtrToInt(addr, ptrdiff_t);
Richard Smith2d6a5672012-01-14 04:30:29 +0000528 MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, (IsARM ? 2 : 1) *
529 ThisAdjustment.getQuantity());
John McCalld608cdb2010-08-22 10:59:02 +0000530 }
John McCall875ab102010-08-22 06:43:33 +0000531
Chris Lattnerc5cbb902011-06-20 04:01:35 +0000532 return llvm::ConstantStruct::getAnon(MemPtr);
John McCall875ab102010-08-22 06:43:33 +0000533}
534
Richard Smith2d6a5672012-01-14 04:30:29 +0000535llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const APValue &MP,
536 QualType MPType) {
537 const MemberPointerType *MPT = MPType->castAs<MemberPointerType>();
538 const ValueDecl *MPD = MP.getMemberPointerDecl();
539 if (!MPD)
540 return EmitNullMemberPointer(MPT);
541
542 // Compute the this-adjustment.
543 CharUnits ThisAdjustment = CharUnits::Zero();
544 ArrayRef<const CXXRecordDecl*> Path = MP.getMemberPointerPath();
545 bool DerivedMember = MP.isMemberPointerToDerivedMember();
546 const CXXRecordDecl *RD = cast<CXXRecordDecl>(MPD->getDeclContext());
547 for (unsigned I = 0, N = Path.size(); I != N; ++I) {
548 const CXXRecordDecl *Base = RD;
549 const CXXRecordDecl *Derived = Path[I];
550 if (DerivedMember)
551 std::swap(Base, Derived);
552 ThisAdjustment +=
553 getContext().getASTRecordLayout(Derived).getBaseClassOffset(Base);
554 RD = Path[I];
555 }
556 if (DerivedMember)
557 ThisAdjustment = -ThisAdjustment;
558
559 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MPD))
560 return BuildMemberPointer(MD, ThisAdjustment);
561
562 CharUnits FieldOffset =
563 getContext().toCharUnitsFromBits(getContext().getFieldOffset(MPD));
564 return EmitMemberDataPointer(MPT, ThisAdjustment + FieldOffset);
565}
566
John McCalle9fd7eb2010-08-22 08:30:07 +0000567/// The comparison algorithm is pretty easy: the member pointers are
568/// the same if they're either bitwise identical *or* both null.
569///
570/// ARM is different here only because null-ness is more complicated.
571llvm::Value *
John McCall0bab0cd2010-08-23 01:21:21 +0000572ItaniumCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF,
573 llvm::Value *L,
574 llvm::Value *R,
575 const MemberPointerType *MPT,
576 bool Inequality) {
John McCalle9fd7eb2010-08-22 08:30:07 +0000577 CGBuilderTy &Builder = CGF.Builder;
578
John McCalle9fd7eb2010-08-22 08:30:07 +0000579 llvm::ICmpInst::Predicate Eq;
580 llvm::Instruction::BinaryOps And, Or;
581 if (Inequality) {
582 Eq = llvm::ICmpInst::ICMP_NE;
583 And = llvm::Instruction::Or;
584 Or = llvm::Instruction::And;
585 } else {
586 Eq = llvm::ICmpInst::ICMP_EQ;
587 And = llvm::Instruction::And;
588 Or = llvm::Instruction::Or;
589 }
590
John McCall0bab0cd2010-08-23 01:21:21 +0000591 // Member data pointers are easy because there's a unique null
592 // value, so it just comes down to bitwise equality.
593 if (MPT->isMemberDataPointer())
594 return Builder.CreateICmp(Eq, L, R);
595
596 // For member function pointers, the tautologies are more complex.
597 // The Itanium tautology is:
John McCallde719f72010-08-23 06:56:36 +0000598 // (L == R) <==> (L.ptr == R.ptr && (L.ptr == 0 || L.adj == R.adj))
John McCall0bab0cd2010-08-23 01:21:21 +0000599 // The ARM tautology is:
John McCallde719f72010-08-23 06:56:36 +0000600 // (L == R) <==> (L.ptr == R.ptr &&
601 // (L.adj == R.adj ||
602 // (L.ptr == 0 && ((L.adj|R.adj) & 1) == 0)))
John McCall0bab0cd2010-08-23 01:21:21 +0000603 // The inequality tautologies have exactly the same structure, except
604 // applying De Morgan's laws.
605
606 llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr");
607 llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr");
608
John McCalle9fd7eb2010-08-22 08:30:07 +0000609 // This condition tests whether L.ptr == R.ptr. This must always be
610 // true for equality to hold.
611 llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr");
612
613 // This condition, together with the assumption that L.ptr == R.ptr,
614 // tests whether the pointers are both null. ARM imposes an extra
615 // condition.
616 llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType());
617 llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null");
618
619 // This condition tests whether L.adj == R.adj. If this isn't
620 // true, the pointers are unequal unless they're both null.
John McCalld608cdb2010-08-22 10:59:02 +0000621 llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj");
622 llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj");
John McCalle9fd7eb2010-08-22 08:30:07 +0000623 llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj");
624
625 // Null member function pointers on ARM clear the low bit of Adj,
626 // so the zero condition has to check that neither low bit is set.
627 if (IsARM) {
628 llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1);
629
630 // Compute (l.adj | r.adj) & 1 and test it against zero.
631 llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj");
632 llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One);
633 llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero,
634 "cmp.or.adj");
635 EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero);
636 }
637
638 // Tie together all our conditions.
639 llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq);
640 Result = Builder.CreateBinOp(And, PtrEq, Result,
641 Inequality ? "memptr.ne" : "memptr.eq");
642 return Result;
643}
644
645llvm::Value *
John McCall0bab0cd2010-08-23 01:21:21 +0000646ItaniumCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
647 llvm::Value *MemPtr,
648 const MemberPointerType *MPT) {
John McCalle9fd7eb2010-08-22 08:30:07 +0000649 CGBuilderTy &Builder = CGF.Builder;
John McCall0bab0cd2010-08-23 01:21:21 +0000650
651 /// For member data pointers, this is just a check against -1.
652 if (MPT->isMemberDataPointer()) {
653 assert(MemPtr->getType() == getPtrDiffTy());
654 llvm::Value *NegativeOne =
655 llvm::Constant::getAllOnesValue(MemPtr->getType());
656 return Builder.CreateICmpNE(MemPtr, NegativeOne, "memptr.tobool");
657 }
John McCalle9fd7eb2010-08-22 08:30:07 +0000658
Daniel Dunbardb27b5f2011-04-19 23:10:47 +0000659 // In Itanium, a member function pointer is not null if 'ptr' is not null.
John McCalld608cdb2010-08-22 10:59:02 +0000660 llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr");
John McCalle9fd7eb2010-08-22 08:30:07 +0000661
662 llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0);
663 llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool");
664
Daniel Dunbardb27b5f2011-04-19 23:10:47 +0000665 // On ARM, a member function pointer is also non-null if the low bit of 'adj'
666 // (the virtual bit) is set.
John McCalle9fd7eb2010-08-22 08:30:07 +0000667 if (IsARM) {
668 llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1);
John McCalld608cdb2010-08-22 10:59:02 +0000669 llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj");
John McCalle9fd7eb2010-08-22 08:30:07 +0000670 llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit");
Daniel Dunbardb27b5f2011-04-19 23:10:47 +0000671 llvm::Value *IsVirtual = Builder.CreateICmpNE(VirtualBit, Zero,
672 "memptr.isvirtual");
673 Result = Builder.CreateOr(Result, IsVirtual);
John McCalle9fd7eb2010-08-22 08:30:07 +0000674 }
675
676 return Result;
677}
John McCall875ab102010-08-22 06:43:33 +0000678
John McCallf16aa102010-08-22 21:01:12 +0000679/// The Itanium ABI requires non-zero initialization only for data
680/// member pointers, for which '0' is a valid offset.
681bool ItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
682 return MPT->getPointeeType()->isFunctionType();
John McCallcf2c85e2010-08-22 04:16:24 +0000683}
John McCall4c40d982010-08-31 07:33:07 +0000684
John McCallecd03b42012-09-25 10:10:39 +0000685/// The Itanium ABI always places an offset to the complete object
686/// at entry -2 in the vtable.
687llvm::Value *ItaniumCXXABI::adjustToCompleteObject(CodeGenFunction &CGF,
688 llvm::Value *ptr,
689 QualType type) {
690 // Grab the vtable pointer as an intptr_t*.
691 llvm::Value *vtable = CGF.GetVTablePtr(ptr, CGF.IntPtrTy->getPointerTo());
692
693 // Track back to entry -2 and pull out the offset there.
694 llvm::Value *offsetPtr =
695 CGF.Builder.CreateConstInBoundsGEP1_64(vtable, -2, "complete-offset.ptr");
696 llvm::LoadInst *offset = CGF.Builder.CreateLoad(offsetPtr);
697 offset->setAlignment(CGF.PointerAlignInBytes);
698
699 // Apply the offset.
700 ptr = CGF.Builder.CreateBitCast(ptr, CGF.Int8PtrTy);
701 return CGF.Builder.CreateInBoundsGEP(ptr, offset);
702}
703
John McCall4c40d982010-08-31 07:33:07 +0000704/// The generic ABI passes 'this', plus a VTT if it's initializing a
705/// base subobject.
706void ItaniumCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor,
707 CXXCtorType Type,
708 CanQualType &ResTy,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000709 SmallVectorImpl<CanQualType> &ArgTys) {
John McCall9cb2cee2010-09-02 10:25:57 +0000710 ASTContext &Context = getContext();
John McCall4c40d982010-08-31 07:33:07 +0000711
712 // 'this' is already there.
713
714 // Check if we need to add a VTT parameter (which has type void **).
715 if (Type == Ctor_Base && Ctor->getParent()->getNumVBases() != 0)
716 ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
717}
718
719/// The ARM ABI does the same as the Itanium ABI, but returns 'this'.
720void ARMCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor,
721 CXXCtorType Type,
722 CanQualType &ResTy,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000723 SmallVectorImpl<CanQualType> &ArgTys) {
John McCall4c40d982010-08-31 07:33:07 +0000724 ItaniumCXXABI::BuildConstructorSignature(Ctor, Type, ResTy, ArgTys);
725 ResTy = ArgTys[0];
726}
727
728/// The generic ABI passes 'this', plus a VTT if it's destroying a
729/// base subobject.
730void ItaniumCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor,
731 CXXDtorType Type,
732 CanQualType &ResTy,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000733 SmallVectorImpl<CanQualType> &ArgTys) {
John McCall9cb2cee2010-09-02 10:25:57 +0000734 ASTContext &Context = getContext();
John McCall4c40d982010-08-31 07:33:07 +0000735
736 // 'this' is already there.
737
738 // Check if we need to add a VTT parameter (which has type void **).
739 if (Type == Dtor_Base && Dtor->getParent()->getNumVBases() != 0)
740 ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
741}
742
743/// The ARM ABI does the same as the Itanium ABI, but returns 'this'
744/// for non-deleting destructors.
745void ARMCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor,
746 CXXDtorType Type,
747 CanQualType &ResTy,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000748 SmallVectorImpl<CanQualType> &ArgTys) {
John McCall4c40d982010-08-31 07:33:07 +0000749 ItaniumCXXABI::BuildDestructorSignature(Dtor, Type, ResTy, ArgTys);
750
751 if (Type != Dtor_Deleting)
752 ResTy = ArgTys[0];
753}
754
755void ItaniumCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF,
756 QualType &ResTy,
757 FunctionArgList &Params) {
758 /// Create the 'this' variable.
759 BuildThisParam(CGF, Params);
760
761 const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl());
762 assert(MD->isInstance());
763
764 // Check if we need a VTT parameter as well.
765 if (CodeGenVTables::needsVTTParameter(CGF.CurGD)) {
John McCall9cb2cee2010-09-02 10:25:57 +0000766 ASTContext &Context = getContext();
John McCall4c40d982010-08-31 07:33:07 +0000767
768 // FIXME: avoid the fake decl
769 QualType T = Context.getPointerType(Context.VoidPtrTy);
770 ImplicitParamDecl *VTTDecl
771 = ImplicitParamDecl::Create(Context, 0, MD->getLocation(),
772 &Context.Idents.get("vtt"), T);
John McCalld26bc762011-03-09 04:27:21 +0000773 Params.push_back(VTTDecl);
John McCall4c40d982010-08-31 07:33:07 +0000774 getVTTDecl(CGF) = VTTDecl;
775 }
776}
777
778void ARMCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF,
779 QualType &ResTy,
780 FunctionArgList &Params) {
781 ItaniumCXXABI::BuildInstanceFunctionParams(CGF, ResTy, Params);
782
783 // Return 'this' from certain constructors and destructors.
784 if (HasThisReturn(CGF.CurGD))
John McCalld26bc762011-03-09 04:27:21 +0000785 ResTy = Params[0]->getType();
John McCall4c40d982010-08-31 07:33:07 +0000786}
787
788void ItaniumCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
789 /// Initialize the 'this' slot.
790 EmitThisParam(CGF);
791
792 /// Initialize the 'vtt' slot if needed.
793 if (getVTTDecl(CGF)) {
794 getVTTValue(CGF)
795 = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(getVTTDecl(CGF)),
796 "vtt");
797 }
798}
799
800void ARMCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
801 ItaniumCXXABI::EmitInstanceFunctionProlog(CGF);
802
803 /// Initialize the return slot to 'this' at the start of the
804 /// function.
805 if (HasThisReturn(CGF.CurGD))
Eli Friedmancec5ebd2012-02-11 02:57:39 +0000806 CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue);
John McCall4c40d982010-08-31 07:33:07 +0000807}
808
809void ARMCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF,
810 RValue RV, QualType ResultType) {
811 if (!isa<CXXDestructorDecl>(CGF.CurGD.getDecl()))
812 return ItaniumCXXABI::EmitReturnFromThunk(CGF, RV, ResultType);
813
814 // Destructor thunks in the ARM ABI have indeterminate results.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000815 llvm::Type *T =
John McCall4c40d982010-08-31 07:33:07 +0000816 cast<llvm::PointerType>(CGF.ReturnValue->getType())->getElementType();
817 RValue Undef = RValue::get(llvm::UndefValue::get(T));
818 return ItaniumCXXABI::EmitReturnFromThunk(CGF, Undef, ResultType);
819}
John McCall1e7fe752010-09-02 09:58:18 +0000820
821/************************** Array allocation cookies **************************/
822
John McCalle2b45e22012-05-01 05:23:51 +0000823CharUnits ItaniumCXXABI::getArrayCookieSizeImpl(QualType elementType) {
824 // The array cookie is a size_t; pad that up to the element alignment.
825 // The cookie is actually right-justified in that space.
826 return std::max(CharUnits::fromQuantity(CGM.SizeSizeInBytes),
827 CGM.getContext().getTypeAlignInChars(elementType));
John McCall1e7fe752010-09-02 09:58:18 +0000828}
829
830llvm::Value *ItaniumCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
831 llvm::Value *NewPtr,
832 llvm::Value *NumElements,
John McCall6ec278d2011-01-27 09:37:56 +0000833 const CXXNewExpr *expr,
John McCall1e7fe752010-09-02 09:58:18 +0000834 QualType ElementType) {
John McCalle2b45e22012-05-01 05:23:51 +0000835 assert(requiresArrayCookie(expr));
John McCall1e7fe752010-09-02 09:58:18 +0000836
Micah Villmow956a5a12012-10-25 15:39:14 +0000837 unsigned AS = NewPtr->getType()->getPointerAddressSpace();
John McCall1e7fe752010-09-02 09:58:18 +0000838
John McCall9cb2cee2010-09-02 10:25:57 +0000839 ASTContext &Ctx = getContext();
John McCall1e7fe752010-09-02 09:58:18 +0000840 QualType SizeTy = Ctx.getSizeType();
841 CharUnits SizeSize = Ctx.getTypeSizeInChars(SizeTy);
842
843 // The size of the cookie.
844 CharUnits CookieSize =
845 std::max(SizeSize, Ctx.getTypeAlignInChars(ElementType));
John McCalle2b45e22012-05-01 05:23:51 +0000846 assert(CookieSize == getArrayCookieSizeImpl(ElementType));
John McCall1e7fe752010-09-02 09:58:18 +0000847
848 // Compute an offset to the cookie.
849 llvm::Value *CookiePtr = NewPtr;
850 CharUnits CookieOffset = CookieSize - SizeSize;
851 if (!CookieOffset.isZero())
852 CookiePtr = CGF.Builder.CreateConstInBoundsGEP1_64(CookiePtr,
853 CookieOffset.getQuantity());
854
855 // Write the number of elements into the appropriate slot.
856 llvm::Value *NumElementsPtr
857 = CGF.Builder.CreateBitCast(CookiePtr,
858 CGF.ConvertType(SizeTy)->getPointerTo(AS));
859 CGF.Builder.CreateStore(NumElements, NumElementsPtr);
860
861 // Finally, compute a pointer to the actual data buffer by skipping
862 // over the cookie completely.
863 return CGF.Builder.CreateConstInBoundsGEP1_64(NewPtr,
864 CookieSize.getQuantity());
865}
866
John McCalle2b45e22012-05-01 05:23:51 +0000867llvm::Value *ItaniumCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
868 llvm::Value *allocPtr,
869 CharUnits cookieSize) {
870 // The element size is right-justified in the cookie.
871 llvm::Value *numElementsPtr = allocPtr;
872 CharUnits numElementsOffset =
873 cookieSize - CharUnits::fromQuantity(CGF.SizeSizeInBytes);
874 if (!numElementsOffset.isZero())
875 numElementsPtr =
876 CGF.Builder.CreateConstInBoundsGEP1_64(numElementsPtr,
877 numElementsOffset.getQuantity());
John McCall1e7fe752010-09-02 09:58:18 +0000878
Micah Villmow956a5a12012-10-25 15:39:14 +0000879 unsigned AS = allocPtr->getType()->getPointerAddressSpace();
John McCalle2b45e22012-05-01 05:23:51 +0000880 numElementsPtr =
881 CGF.Builder.CreateBitCast(numElementsPtr, CGF.SizeTy->getPointerTo(AS));
882 return CGF.Builder.CreateLoad(numElementsPtr);
John McCall1e7fe752010-09-02 09:58:18 +0000883}
884
John McCalle2b45e22012-05-01 05:23:51 +0000885CharUnits ARMCXXABI::getArrayCookieSizeImpl(QualType elementType) {
John McCall1e7fe752010-09-02 09:58:18 +0000886 // On ARM, the cookie is always:
887 // struct array_cookie {
888 // std::size_t element_size; // element_size != 0
889 // std::size_t element_count;
890 // };
891 // TODO: what should we do if the allocated type actually wants
892 // greater alignment?
John McCalle2b45e22012-05-01 05:23:51 +0000893 return CharUnits::fromQuantity(2 * CGM.SizeSizeInBytes);
John McCall1e7fe752010-09-02 09:58:18 +0000894}
895
896llvm::Value *ARMCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
897 llvm::Value *NewPtr,
898 llvm::Value *NumElements,
John McCall6ec278d2011-01-27 09:37:56 +0000899 const CXXNewExpr *expr,
John McCall1e7fe752010-09-02 09:58:18 +0000900 QualType ElementType) {
John McCalle2b45e22012-05-01 05:23:51 +0000901 assert(requiresArrayCookie(expr));
John McCall1e7fe752010-09-02 09:58:18 +0000902
903 // NewPtr is a char*.
904
Micah Villmow956a5a12012-10-25 15:39:14 +0000905 unsigned AS = NewPtr->getType()->getPointerAddressSpace();
John McCall1e7fe752010-09-02 09:58:18 +0000906
John McCall9cb2cee2010-09-02 10:25:57 +0000907 ASTContext &Ctx = getContext();
John McCall1e7fe752010-09-02 09:58:18 +0000908 CharUnits SizeSize = Ctx.getTypeSizeInChars(Ctx.getSizeType());
Chris Lattner2acc6e32011-07-18 04:24:23 +0000909 llvm::IntegerType *SizeTy =
John McCall1e7fe752010-09-02 09:58:18 +0000910 cast<llvm::IntegerType>(CGF.ConvertType(Ctx.getSizeType()));
911
912 // The cookie is always at the start of the buffer.
913 llvm::Value *CookiePtr = NewPtr;
914
915 // The first element is the element size.
916 CookiePtr = CGF.Builder.CreateBitCast(CookiePtr, SizeTy->getPointerTo(AS));
917 llvm::Value *ElementSize = llvm::ConstantInt::get(SizeTy,
918 Ctx.getTypeSizeInChars(ElementType).getQuantity());
919 CGF.Builder.CreateStore(ElementSize, CookiePtr);
920
921 // The second element is the element count.
922 CookiePtr = CGF.Builder.CreateConstInBoundsGEP1_32(CookiePtr, 1);
923 CGF.Builder.CreateStore(NumElements, CookiePtr);
924
925 // Finally, compute a pointer to the actual data buffer by skipping
926 // over the cookie completely.
927 CharUnits CookieSize = 2 * SizeSize;
928 return CGF.Builder.CreateConstInBoundsGEP1_64(NewPtr,
929 CookieSize.getQuantity());
930}
931
John McCalle2b45e22012-05-01 05:23:51 +0000932llvm::Value *ARMCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
933 llvm::Value *allocPtr,
934 CharUnits cookieSize) {
935 // The number of elements is at offset sizeof(size_t) relative to
936 // the allocated pointer.
937 llvm::Value *numElementsPtr
938 = CGF.Builder.CreateConstInBoundsGEP1_64(allocPtr, CGF.SizeSizeInBytes);
John McCall1e7fe752010-09-02 09:58:18 +0000939
Micah Villmow956a5a12012-10-25 15:39:14 +0000940 unsigned AS = allocPtr->getType()->getPointerAddressSpace();
John McCalle2b45e22012-05-01 05:23:51 +0000941 numElementsPtr =
942 CGF.Builder.CreateBitCast(numElementsPtr, CGF.SizeTy->getPointerTo(AS));
943 return CGF.Builder.CreateLoad(numElementsPtr);
John McCall1e7fe752010-09-02 09:58:18 +0000944}
945
John McCall5cd91b52010-09-08 01:44:27 +0000946/*********************** Static local initialization **************************/
947
948static llvm::Constant *getGuardAcquireFn(CodeGenModule &CGM,
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000949 llvm::PointerType *GuardPtrTy) {
John McCall5cd91b52010-09-08 01:44:27 +0000950 // int __cxa_guard_acquire(__guard *guard_object);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000951 llvm::FunctionType *FTy =
John McCall5cd91b52010-09-08 01:44:27 +0000952 llvm::FunctionType::get(CGM.getTypes().ConvertType(CGM.getContext().IntTy),
Jay Foadda549e82011-07-29 13:56:53 +0000953 GuardPtrTy, /*isVarArg=*/false);
Nick Lewyckye76872e2012-02-13 23:45:02 +0000954 return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_acquire",
Bill Wendling72390b32012-12-20 19:27:06 +0000955 llvm::Attribute::get(CGM.getLLVMContext(),
956 llvm::Attribute::NoUnwind));
John McCall5cd91b52010-09-08 01:44:27 +0000957}
958
959static llvm::Constant *getGuardReleaseFn(CodeGenModule &CGM,
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000960 llvm::PointerType *GuardPtrTy) {
John McCall5cd91b52010-09-08 01:44:27 +0000961 // void __cxa_guard_release(__guard *guard_object);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000962 llvm::FunctionType *FTy =
Chris Lattner8b418682012-02-07 00:39:47 +0000963 llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false);
Nick Lewyckye76872e2012-02-13 23:45:02 +0000964 return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_release",
Bill Wendling72390b32012-12-20 19:27:06 +0000965 llvm::Attribute::get(CGM.getLLVMContext(),
966 llvm::Attribute::NoUnwind));
John McCall5cd91b52010-09-08 01:44:27 +0000967}
968
969static llvm::Constant *getGuardAbortFn(CodeGenModule &CGM,
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000970 llvm::PointerType *GuardPtrTy) {
John McCall5cd91b52010-09-08 01:44:27 +0000971 // void __cxa_guard_abort(__guard *guard_object);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000972 llvm::FunctionType *FTy =
Chris Lattner8b418682012-02-07 00:39:47 +0000973 llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false);
Nick Lewyckye76872e2012-02-13 23:45:02 +0000974 return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_abort",
Bill Wendling72390b32012-12-20 19:27:06 +0000975 llvm::Attribute::get(CGM.getLLVMContext(),
976 llvm::Attribute::NoUnwind));
John McCall5cd91b52010-09-08 01:44:27 +0000977}
978
979namespace {
980 struct CallGuardAbort : EHScopeStack::Cleanup {
981 llvm::GlobalVariable *Guard;
Chandler Carruth0f30a122012-03-30 19:44:53 +0000982 CallGuardAbort(llvm::GlobalVariable *Guard) : Guard(Guard) {}
John McCall5cd91b52010-09-08 01:44:27 +0000983
John McCallad346f42011-07-12 20:27:29 +0000984 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCall5cd91b52010-09-08 01:44:27 +0000985 CGF.Builder.CreateCall(getGuardAbortFn(CGF.CGM, Guard->getType()), Guard)
986 ->setDoesNotThrow();
987 }
988 };
989}
990
991/// The ARM code here follows the Itanium code closely enough that we
992/// just special-case it at particular places.
John McCall3030eb82010-11-06 09:44:32 +0000993void ItaniumCXXABI::EmitGuardedInit(CodeGenFunction &CGF,
994 const VarDecl &D,
John McCall355bba72012-03-30 21:00:39 +0000995 llvm::GlobalVariable *var,
996 bool shouldPerformInit) {
John McCall5cd91b52010-09-08 01:44:27 +0000997 CGBuilderTy &Builder = CGF.Builder;
John McCall3030eb82010-11-06 09:44:32 +0000998
999 // We only need to use thread-safe statics for local variables;
1000 // global initialization is always single-threaded.
John McCall0502a222011-06-17 07:33:57 +00001001 bool threadsafe =
David Blaikie4e4d0842012-03-11 07:00:24 +00001002 (getContext().getLangOpts().ThreadsafeStatics && D.isLocalVarDecl());
Anders Carlsson173d5122011-04-27 04:37:08 +00001003
Anders Carlsson173d5122011-04-27 04:37:08 +00001004 // If we have a global variable with internal linkage and thread-safe statics
1005 // are disabled, we can just let the guard variable be of type i8.
John McCall355bba72012-03-30 21:00:39 +00001006 bool useInt8GuardVariable = !threadsafe && var->hasInternalLinkage();
1007
1008 llvm::IntegerType *guardTy;
John McCall0502a222011-06-17 07:33:57 +00001009 if (useInt8GuardVariable) {
John McCall355bba72012-03-30 21:00:39 +00001010 guardTy = CGF.Int8Ty;
John McCall0502a222011-06-17 07:33:57 +00001011 } else {
Anders Carlsson173d5122011-04-27 04:37:08 +00001012 // Guard variables are 64 bits in the generic ABI and 32 bits on ARM.
John McCall355bba72012-03-30 21:00:39 +00001013 guardTy = (IsARM ? CGF.Int32Ty : CGF.Int64Ty);
Anders Carlsson173d5122011-04-27 04:37:08 +00001014 }
John McCall355bba72012-03-30 21:00:39 +00001015 llvm::PointerType *guardPtrTy = guardTy->getPointerTo();
John McCall5cd91b52010-09-08 01:44:27 +00001016
John McCall355bba72012-03-30 21:00:39 +00001017 // Create the guard variable if we don't already have it (as we
1018 // might if we're double-emitting this function body).
1019 llvm::GlobalVariable *guard = CGM.getStaticLocalDeclGuardAddress(&D);
1020 if (!guard) {
1021 // Mangle the name for the guard.
1022 SmallString<256> guardName;
1023 {
1024 llvm::raw_svector_ostream out(guardName);
1025 getMangleContext().mangleItaniumGuardVariable(&D, out);
1026 out.flush();
1027 }
John McCall112c9672010-11-02 21:04:24 +00001028
John McCall355bba72012-03-30 21:00:39 +00001029 // Create the guard variable with a zero-initializer.
1030 // Just absorb linkage and visibility from the guarded variable.
1031 guard = new llvm::GlobalVariable(CGM.getModule(), guardTy,
1032 false, var->getLinkage(),
1033 llvm::ConstantInt::get(guardTy, 0),
1034 guardName.str());
1035 guard->setVisibility(var->getVisibility());
1036
1037 CGM.setStaticLocalDeclGuardAddress(&D, guard);
1038 }
John McCall49d26d22012-03-30 07:09:50 +00001039
John McCall5cd91b52010-09-08 01:44:27 +00001040 // Test whether the variable has completed initialization.
John McCall355bba72012-03-30 21:00:39 +00001041 llvm::Value *isInitialized;
John McCall5cd91b52010-09-08 01:44:27 +00001042
1043 // ARM C++ ABI 3.2.3.1:
1044 // To support the potential use of initialization guard variables
1045 // as semaphores that are the target of ARM SWP and LDREX/STREX
1046 // synchronizing instructions we define a static initialization
1047 // guard variable to be a 4-byte aligned, 4- byte word with the
1048 // following inline access protocol.
1049 // #define INITIALIZED 1
1050 // if ((obj_guard & INITIALIZED) != INITIALIZED) {
1051 // if (__cxa_guard_acquire(&obj_guard))
1052 // ...
1053 // }
John McCall0502a222011-06-17 07:33:57 +00001054 if (IsARM && !useInt8GuardVariable) {
John McCall355bba72012-03-30 21:00:39 +00001055 llvm::Value *V = Builder.CreateLoad(guard);
John McCall5cd91b52010-09-08 01:44:27 +00001056 V = Builder.CreateAnd(V, Builder.getInt32(1));
John McCall355bba72012-03-30 21:00:39 +00001057 isInitialized = Builder.CreateIsNull(V, "guard.uninitialized");
John McCall5cd91b52010-09-08 01:44:27 +00001058
1059 // Itanium C++ ABI 3.3.2:
1060 // The following is pseudo-code showing how these functions can be used:
1061 // if (obj_guard.first_byte == 0) {
1062 // if ( __cxa_guard_acquire (&obj_guard) ) {
1063 // try {
1064 // ... initialize the object ...;
1065 // } catch (...) {
1066 // __cxa_guard_abort (&obj_guard);
1067 // throw;
1068 // }
1069 // ... queue object destructor with __cxa_atexit() ...;
1070 // __cxa_guard_release (&obj_guard);
1071 // }
1072 // }
1073 } else {
1074 // Load the first byte of the guard variable.
Chandler Carruth0f30a122012-03-30 19:44:53 +00001075 llvm::LoadInst *LI =
John McCall355bba72012-03-30 21:00:39 +00001076 Builder.CreateLoad(Builder.CreateBitCast(guard, CGM.Int8PtrTy));
Chandler Carruth0f30a122012-03-30 19:44:53 +00001077 LI->setAlignment(1);
John McCall5cd91b52010-09-08 01:44:27 +00001078
Eli Friedmaneb43f4a2011-09-13 22:21:56 +00001079 // Itanium ABI:
1080 // An implementation supporting thread-safety on multiprocessor
1081 // systems must also guarantee that references to the initialized
1082 // object do not occur before the load of the initialization flag.
1083 //
1084 // In LLVM, we do this by marking the load Acquire.
1085 if (threadsafe)
Chandler Carruth0f30a122012-03-30 19:44:53 +00001086 LI->setAtomic(llvm::Acquire);
Eli Friedmaneb43f4a2011-09-13 22:21:56 +00001087
John McCall355bba72012-03-30 21:00:39 +00001088 isInitialized = Builder.CreateIsNull(LI, "guard.uninitialized");
John McCall5cd91b52010-09-08 01:44:27 +00001089 }
1090
1091 llvm::BasicBlock *InitCheckBlock = CGF.createBasicBlock("init.check");
1092 llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end");
1093
1094 // Check if the first byte of the guard variable is zero.
John McCall355bba72012-03-30 21:00:39 +00001095 Builder.CreateCondBr(isInitialized, InitCheckBlock, EndBlock);
John McCall5cd91b52010-09-08 01:44:27 +00001096
1097 CGF.EmitBlock(InitCheckBlock);
1098
1099 // Variables used when coping with thread-safe statics and exceptions.
John McCall0502a222011-06-17 07:33:57 +00001100 if (threadsafe) {
John McCall5cd91b52010-09-08 01:44:27 +00001101 // Call __cxa_guard_acquire.
1102 llvm::Value *V
John McCall355bba72012-03-30 21:00:39 +00001103 = Builder.CreateCall(getGuardAcquireFn(CGM, guardPtrTy), guard);
John McCall5cd91b52010-09-08 01:44:27 +00001104
1105 llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init");
1106
1107 Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"),
1108 InitBlock, EndBlock);
1109
1110 // Call __cxa_guard_abort along the exceptional edge.
John McCall355bba72012-03-30 21:00:39 +00001111 CGF.EHStack.pushCleanup<CallGuardAbort>(EHCleanup, guard);
John McCall5cd91b52010-09-08 01:44:27 +00001112
1113 CGF.EmitBlock(InitBlock);
1114 }
1115
1116 // Emit the initializer and add a global destructor if appropriate.
John McCall355bba72012-03-30 21:00:39 +00001117 CGF.EmitCXXGlobalVarDeclInit(D, var, shouldPerformInit);
John McCall5cd91b52010-09-08 01:44:27 +00001118
John McCall0502a222011-06-17 07:33:57 +00001119 if (threadsafe) {
John McCall5cd91b52010-09-08 01:44:27 +00001120 // Pop the guard-abort cleanup if we pushed one.
1121 CGF.PopCleanupBlock();
1122
1123 // Call __cxa_guard_release. This cannot throw.
John McCall355bba72012-03-30 21:00:39 +00001124 Builder.CreateCall(getGuardReleaseFn(CGM, guardPtrTy), guard);
John McCall5cd91b52010-09-08 01:44:27 +00001125 } else {
John McCall355bba72012-03-30 21:00:39 +00001126 Builder.CreateStore(llvm::ConstantInt::get(guardTy, 1), guard);
John McCall5cd91b52010-09-08 01:44:27 +00001127 }
1128
1129 CGF.EmitBlock(EndBlock);
1130}
John McCall20bb1752012-05-01 06:13:13 +00001131
1132/// Register a global destructor using __cxa_atexit.
1133static void emitGlobalDtorWithCXAAtExit(CodeGenFunction &CGF,
1134 llvm::Constant *dtor,
1135 llvm::Constant *addr) {
1136 // We're assuming that the destructor function is something we can
1137 // reasonably call with the default CC. Go ahead and cast it to the
1138 // right prototype.
1139 llvm::Type *dtorTy =
1140 llvm::FunctionType::get(CGF.VoidTy, CGF.Int8PtrTy, false)->getPointerTo();
1141
1142 // extern "C" int __cxa_atexit(void (*f)(void *), void *p, void *d);
1143 llvm::Type *paramTys[] = { dtorTy, CGF.Int8PtrTy, CGF.Int8PtrTy };
1144 llvm::FunctionType *atexitTy =
1145 llvm::FunctionType::get(CGF.IntTy, paramTys, false);
1146
1147 // Fetch the actual function.
1148 llvm::Constant *atexit =
1149 CGF.CGM.CreateRuntimeFunction(atexitTy, "__cxa_atexit");
1150 if (llvm::Function *fn = dyn_cast<llvm::Function>(atexit))
1151 fn->setDoesNotThrow();
1152
1153 // Create a variable that binds the atexit to this shared object.
1154 llvm::Constant *handle =
1155 CGF.CGM.CreateRuntimeVariable(CGF.Int8Ty, "__dso_handle");
1156
1157 llvm::Value *args[] = {
1158 llvm::ConstantExpr::getBitCast(dtor, dtorTy),
1159 llvm::ConstantExpr::getBitCast(addr, CGF.Int8PtrTy),
1160 handle
1161 };
1162 CGF.Builder.CreateCall(atexit, args)->setDoesNotThrow();
1163}
1164
1165/// Register a global destructor as best as we know how.
1166void ItaniumCXXABI::registerGlobalDtor(CodeGenFunction &CGF,
1167 llvm::Constant *dtor,
1168 llvm::Constant *addr) {
1169 // Use __cxa_atexit if available.
1170 if (CGM.getCodeGenOpts().CXAAtExit) {
1171 return emitGlobalDtorWithCXAAtExit(CGF, dtor, addr);
1172 }
1173
1174 // In Apple kexts, we want to add a global destructor entry.
1175 // FIXME: shouldn't this be guarded by some variable?
Richard Smith7edf9e32012-11-01 22:30:59 +00001176 if (CGM.getLangOpts().AppleKext) {
John McCall20bb1752012-05-01 06:13:13 +00001177 // Generate a global destructor entry.
1178 return CGM.AddCXXDtorEntry(dtor, addr);
1179 }
1180
1181 CGF.registerGlobalDtorWithAtExit(dtor, addr);
1182}
Charles Davis9ee494f2012-06-23 23:44:00 +00001183
1184/// Generate and emit virtual tables for the given class.
1185void ItaniumCXXABI::EmitVTables(const CXXRecordDecl *Class) {
1186 CGM.getVTables().GenerateClassData(CGM.getVTableLinkage(Class), Class);
1187}