blob: 3188590d11eff8f920c4e45f21e96a6f97260dfd [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"
John McCall93d557b2010-08-22 00:05:51 +000023#include "CodeGenFunction.h"
Charles Davis3a811f12010-05-25 19:52:27 +000024#include "CodeGenModule.h"
Peter Collingbourne14110472011-01-13 18:57:25 +000025#include <clang/AST/Mangle.h>
John McCall93d557b2010-08-22 00:05:51 +000026#include <clang/AST/Type.h>
John McCall0502a222011-06-17 07:33:57 +000027#include <llvm/Intrinsics.h>
John McCall0bab0cd2010-08-23 01:21:21 +000028#include <llvm/Target/TargetData.h>
John McCall93d557b2010-08-22 00:05:51 +000029#include <llvm/Value.h>
Charles Davis3a811f12010-05-25 19:52:27 +000030
31using namespace clang;
John McCall93d557b2010-08-22 00:05:51 +000032using namespace CodeGen;
Charles Davis3a811f12010-05-25 19:52:27 +000033
34namespace {
Charles Davis071cc7d2010-08-16 03:33:14 +000035class ItaniumCXXABI : public CodeGen::CGCXXABI {
John McCall0bab0cd2010-08-23 01:21:21 +000036private:
Chris Lattner9cbe4f02011-07-09 17:41:47 +000037 llvm::IntegerType *PtrDiffTy;
John McCall93d557b2010-08-22 00:05:51 +000038protected:
John McCallbabc9a92010-08-22 00:59:17 +000039 bool IsARM;
John McCall0bab0cd2010-08-23 01:21:21 +000040
41 // It's a little silly for us to cache this.
Chris Lattner9cbe4f02011-07-09 17:41:47 +000042 llvm::IntegerType *getPtrDiffTy() {
John McCall0bab0cd2010-08-23 01:21:21 +000043 if (!PtrDiffTy) {
John McCall9cb2cee2010-09-02 10:25:57 +000044 QualType T = getContext().getPointerDiffType();
Chris Lattner9cbe4f02011-07-09 17:41:47 +000045 llvm::Type *Ty = CGM.getTypes().ConvertType(T);
John McCall0bab0cd2010-08-23 01:21:21 +000046 PtrDiffTy = cast<llvm::IntegerType>(Ty);
47 }
48 return PtrDiffTy;
49 }
50
Charles Davis3a811f12010-05-25 19:52:27 +000051public:
John McCallbabc9a92010-08-22 00:59:17 +000052 ItaniumCXXABI(CodeGen::CodeGenModule &CGM, bool IsARM = false) :
Peter Collingbourne14110472011-01-13 18:57:25 +000053 CGCXXABI(CGM), PtrDiffTy(0), IsARM(IsARM) { }
John McCall93d557b2010-08-22 00:05:51 +000054
John McCallf16aa102010-08-22 21:01:12 +000055 bool isZeroInitializable(const MemberPointerType *MPT);
John McCallcf2c85e2010-08-22 04:16:24 +000056
Chris Lattner9cbe4f02011-07-09 17:41:47 +000057 llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT);
John McCall0bab0cd2010-08-23 01:21:21 +000058
John McCall93d557b2010-08-22 00:05:51 +000059 llvm::Value *EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
60 llvm::Value *&This,
61 llvm::Value *MemFnPtr,
62 const MemberPointerType *MPT);
John McCall3023def2010-08-22 03:04:22 +000063
John McCall6c2ab1d2010-08-31 21:07:20 +000064 llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF,
65 llvm::Value *Base,
66 llvm::Value *MemPtr,
67 const MemberPointerType *MPT);
68
John McCall0bab0cd2010-08-23 01:21:21 +000069 llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
70 const CastExpr *E,
71 llvm::Value *Src);
John McCall4d4e5c12012-02-15 01:22:51 +000072 llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
73 llvm::Constant *Src);
John McCallcf2c85e2010-08-22 04:16:24 +000074
John McCall0bab0cd2010-08-23 01:21:21 +000075 llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
John McCallcf2c85e2010-08-22 04:16:24 +000076
John McCall755d8492011-04-12 00:42:48 +000077 llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
John McCall5808ce42011-02-03 08:15:49 +000078 llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
79 CharUnits offset);
Richard Smith2d6a5672012-01-14 04:30:29 +000080 llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT);
81 llvm::Constant *BuildMemberPointer(const CXXMethodDecl *MD,
82 CharUnits ThisAdjustment);
John McCall875ab102010-08-22 06:43:33 +000083
John McCall0bab0cd2010-08-23 01:21:21 +000084 llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF,
85 llvm::Value *L,
86 llvm::Value *R,
87 const MemberPointerType *MPT,
88 bool Inequality);
John McCalle9fd7eb2010-08-22 08:30:07 +000089
John McCall0bab0cd2010-08-23 01:21:21 +000090 llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
91 llvm::Value *Addr,
92 const MemberPointerType *MPT);
John McCall4c40d982010-08-31 07:33:07 +000093
94 void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
95 CXXCtorType T,
96 CanQualType &ResTy,
Chris Lattner5f9e2722011-07-23 10:55:15 +000097 SmallVectorImpl<CanQualType> &ArgTys);
John McCall4c40d982010-08-31 07:33:07 +000098
99 void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
100 CXXDtorType 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 BuildInstanceFunctionParams(CodeGenFunction &CGF,
105 QualType &ResTy,
106 FunctionArgList &Params);
107
108 void EmitInstanceFunctionProlog(CodeGenFunction &CGF);
John McCall1e7fe752010-09-02 09:58:18 +0000109
John McCalle2b45e22012-05-01 05:23:51 +0000110 CharUnits getArrayCookieSizeImpl(QualType elementType);
John McCall1e7fe752010-09-02 09:58:18 +0000111 llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
112 llvm::Value *NewPtr,
113 llvm::Value *NumElements,
John McCall6ec278d2011-01-27 09:37:56 +0000114 const CXXNewExpr *expr,
John McCall1e7fe752010-09-02 09:58:18 +0000115 QualType ElementType);
John McCalle2b45e22012-05-01 05:23:51 +0000116 llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF,
117 llvm::Value *allocPtr,
118 CharUnits cookieSize);
John McCall5cd91b52010-09-08 01:44:27 +0000119
John McCall3030eb82010-11-06 09:44:32 +0000120 void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
Chandler Carruth0f30a122012-03-30 19:44:53 +0000121 llvm::GlobalVariable *DeclPtr, bool PerformInit);
John McCall20bb1752012-05-01 06:13:13 +0000122 void registerGlobalDtor(CodeGenFunction &CGF, llvm::Constant *dtor,
123 llvm::Constant *addr);
Charles Davis3a811f12010-05-25 19:52:27 +0000124};
John McCallee79a4c2010-08-21 22:46:04 +0000125
126class ARMCXXABI : public ItaniumCXXABI {
127public:
John McCallbabc9a92010-08-22 00:59:17 +0000128 ARMCXXABI(CodeGen::CodeGenModule &CGM) : ItaniumCXXABI(CGM, /*ARM*/ true) {}
John McCall4c40d982010-08-31 07:33:07 +0000129
130 void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
131 CXXCtorType T,
132 CanQualType &ResTy,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000133 SmallVectorImpl<CanQualType> &ArgTys);
John McCall4c40d982010-08-31 07:33:07 +0000134
135 void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
136 CXXDtorType T,
137 CanQualType &ResTy,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000138 SmallVectorImpl<CanQualType> &ArgTys);
John McCall4c40d982010-08-31 07:33:07 +0000139
140 void BuildInstanceFunctionParams(CodeGenFunction &CGF,
141 QualType &ResTy,
142 FunctionArgList &Params);
143
144 void EmitInstanceFunctionProlog(CodeGenFunction &CGF);
145
146 void EmitReturnFromThunk(CodeGenFunction &CGF, RValue RV, QualType ResTy);
147
John McCalle2b45e22012-05-01 05:23:51 +0000148 CharUnits getArrayCookieSizeImpl(QualType elementType);
John McCall1e7fe752010-09-02 09:58:18 +0000149 llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
150 llvm::Value *NewPtr,
151 llvm::Value *NumElements,
John McCall6ec278d2011-01-27 09:37:56 +0000152 const CXXNewExpr *expr,
John McCall1e7fe752010-09-02 09:58:18 +0000153 QualType ElementType);
John McCalle2b45e22012-05-01 05:23:51 +0000154 llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, llvm::Value *allocPtr,
155 CharUnits cookieSize);
John McCall4c40d982010-08-31 07:33:07 +0000156
157private:
158 /// \brief Returns true if the given instance method is one of the
159 /// kinds that the ARM ABI says returns 'this'.
160 static bool HasThisReturn(GlobalDecl GD) {
161 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
162 return ((isa<CXXDestructorDecl>(MD) && GD.getDtorType() != Dtor_Deleting) ||
163 (isa<CXXConstructorDecl>(MD)));
164 }
John McCallee79a4c2010-08-21 22:46:04 +0000165};
Charles Davis3a811f12010-05-25 19:52:27 +0000166}
167
Charles Davis071cc7d2010-08-16 03:33:14 +0000168CodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) {
Charles Davis3a811f12010-05-25 19:52:27 +0000169 return new ItaniumCXXABI(CGM);
170}
171
John McCallee79a4c2010-08-21 22:46:04 +0000172CodeGen::CGCXXABI *CodeGen::CreateARMCXXABI(CodeGenModule &CGM) {
173 return new ARMCXXABI(CGM);
174}
175
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000176llvm::Type *
John McCall0bab0cd2010-08-23 01:21:21 +0000177ItaniumCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) {
178 if (MPT->isMemberDataPointer())
179 return getPtrDiffTy();
Chris Lattner7650d952011-06-18 22:49:11 +0000180 return llvm::StructType::get(getPtrDiffTy(), getPtrDiffTy(), NULL);
John McCall875ab102010-08-22 06:43:33 +0000181}
182
John McCallbabc9a92010-08-22 00:59:17 +0000183/// In the Itanium and ARM ABIs, method pointers have the form:
184/// struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr;
185///
186/// In the Itanium ABI:
187/// - method pointers are virtual if (memptr.ptr & 1) is nonzero
188/// - the this-adjustment is (memptr.adj)
189/// - the virtual offset is (memptr.ptr - 1)
190///
191/// In the ARM ABI:
192/// - method pointers are virtual if (memptr.adj & 1) is nonzero
193/// - the this-adjustment is (memptr.adj >> 1)
194/// - the virtual offset is (memptr.ptr)
195/// ARM uses 'adj' for the virtual flag because Thumb functions
196/// may be only single-byte aligned.
197///
198/// If the member is virtual, the adjusted 'this' pointer points
199/// to a vtable pointer from which the virtual offset is applied.
200///
201/// If the member is non-virtual, memptr.ptr is the address of
202/// the function to call.
John McCall93d557b2010-08-22 00:05:51 +0000203llvm::Value *
204ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
205 llvm::Value *&This,
206 llvm::Value *MemFnPtr,
207 const MemberPointerType *MPT) {
208 CGBuilderTy &Builder = CGF.Builder;
209
210 const FunctionProtoType *FPT =
211 MPT->getPointeeType()->getAs<FunctionProtoType>();
212 const CXXRecordDecl *RD =
213 cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
214
Chris Lattner2acc6e32011-07-18 04:24:23 +0000215 llvm::FunctionType *FTy =
John McCallde5d3c72012-02-17 03:33:10 +0000216 CGM.getTypes().GetFunctionType(
217 CGM.getTypes().arrangeCXXMethodType(RD, FPT));
John McCall93d557b2010-08-22 00:05:51 +0000218
Chris Lattner2acc6e32011-07-18 04:24:23 +0000219 llvm::IntegerType *ptrdiff = getPtrDiffTy();
John McCallbabc9a92010-08-22 00:59:17 +0000220 llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(ptrdiff, 1);
John McCall93d557b2010-08-22 00:05:51 +0000221
John McCallbabc9a92010-08-22 00:59:17 +0000222 llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual");
223 llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual");
224 llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end");
225
John McCalld608cdb2010-08-22 10:59:02 +0000226 // Extract memptr.adj, which is in the second field.
227 llvm::Value *RawAdj = Builder.CreateExtractValue(MemFnPtr, 1, "memptr.adj");
John McCallbabc9a92010-08-22 00:59:17 +0000228
229 // Compute the true adjustment.
230 llvm::Value *Adj = RawAdj;
231 if (IsARM)
232 Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted");
John McCall93d557b2010-08-22 00:05:51 +0000233
234 // Apply the adjustment and cast back to the original struct type
235 // for consistency.
John McCallbabc9a92010-08-22 00:59:17 +0000236 llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy());
237 Ptr = Builder.CreateInBoundsGEP(Ptr, Adj);
238 This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted");
John McCall93d557b2010-08-22 00:05:51 +0000239
240 // Load the function pointer.
John McCalld608cdb2010-08-22 10:59:02 +0000241 llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr");
John McCall93d557b2010-08-22 00:05:51 +0000242
243 // If the LSB in the function pointer is 1, the function pointer points to
244 // a virtual function.
John McCallbabc9a92010-08-22 00:59:17 +0000245 llvm::Value *IsVirtual;
246 if (IsARM)
247 IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1);
248 else
249 IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1);
250 IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual");
John McCall93d557b2010-08-22 00:05:51 +0000251 Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
252
253 // In the virtual path, the adjustment left 'This' pointing to the
254 // vtable of the correct base subobject. The "function pointer" is an
John McCallbabc9a92010-08-22 00:59:17 +0000255 // offset within the vtable (+1 for the virtual flag on non-ARM).
John McCall93d557b2010-08-22 00:05:51 +0000256 CGF.EmitBlock(FnVirtual);
257
258 // Cast the adjusted this to a pointer to vtable pointer and load.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000259 llvm::Type *VTableTy = Builder.getInt8PtrTy();
John McCall93d557b2010-08-22 00:05:51 +0000260 llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy->getPointerTo());
John McCallbabc9a92010-08-22 00:59:17 +0000261 VTable = Builder.CreateLoad(VTable, "memptr.vtable");
John McCall93d557b2010-08-22 00:05:51 +0000262
263 // Apply the offset.
John McCallbabc9a92010-08-22 00:59:17 +0000264 llvm::Value *VTableOffset = FnAsInt;
265 if (!IsARM) VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1);
266 VTable = Builder.CreateGEP(VTable, VTableOffset);
John McCall93d557b2010-08-22 00:05:51 +0000267
268 // Load the virtual function to call.
269 VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo());
John McCallbabc9a92010-08-22 00:59:17 +0000270 llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "memptr.virtualfn");
John McCall93d557b2010-08-22 00:05:51 +0000271 CGF.EmitBranch(FnEnd);
272
273 // In the non-virtual path, the function pointer is actually a
274 // function pointer.
275 CGF.EmitBlock(FnNonVirtual);
276 llvm::Value *NonVirtualFn =
John McCallbabc9a92010-08-22 00:59:17 +0000277 Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn");
John McCall93d557b2010-08-22 00:05:51 +0000278
279 // We're done.
280 CGF.EmitBlock(FnEnd);
Jay Foadbbf3bac2011-03-30 11:28:58 +0000281 llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo(), 2);
John McCall93d557b2010-08-22 00:05:51 +0000282 Callee->addIncoming(VirtualFn, FnVirtual);
283 Callee->addIncoming(NonVirtualFn, FnNonVirtual);
284 return Callee;
285}
John McCall3023def2010-08-22 03:04:22 +0000286
John McCall6c2ab1d2010-08-31 21:07:20 +0000287/// Compute an l-value by applying the given pointer-to-member to a
288/// base object.
289llvm::Value *ItaniumCXXABI::EmitMemberDataPointerAddress(CodeGenFunction &CGF,
290 llvm::Value *Base,
291 llvm::Value *MemPtr,
292 const MemberPointerType *MPT) {
293 assert(MemPtr->getType() == getPtrDiffTy());
294
295 CGBuilderTy &Builder = CGF.Builder;
296
297 unsigned AS = cast<llvm::PointerType>(Base->getType())->getAddressSpace();
298
299 // Cast to char*.
300 Base = Builder.CreateBitCast(Base, Builder.getInt8Ty()->getPointerTo(AS));
301
302 // Apply the offset, which we assume is non-null.
303 llvm::Value *Addr = Builder.CreateInBoundsGEP(Base, MemPtr, "memptr.offset");
304
305 // Cast the address to the appropriate pointer type, adopting the
306 // address space of the base pointer.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000307 llvm::Type *PType
Douglas Gregoreede61a2010-09-02 17:38:50 +0000308 = CGF.ConvertTypeForMem(MPT->getPointeeType())->getPointerTo(AS);
John McCall6c2ab1d2010-08-31 21:07:20 +0000309 return Builder.CreateBitCast(Addr, PType);
310}
311
John McCall4d4e5c12012-02-15 01:22:51 +0000312/// Perform a bitcast, derived-to-base, or base-to-derived member pointer
313/// conversion.
314///
315/// Bitcast conversions are always a no-op under Itanium.
John McCall0bab0cd2010-08-23 01:21:21 +0000316///
317/// Obligatory offset/adjustment diagram:
318/// <-- offset --> <-- adjustment -->
319/// |--------------------------|----------------------|--------------------|
320/// ^Derived address point ^Base address point ^Member address point
321///
322/// So when converting a base member pointer to a derived member pointer,
323/// we add the offset to the adjustment because the address point has
324/// decreased; and conversely, when converting a derived MP to a base MP
325/// we subtract the offset from the adjustment because the address point
326/// has increased.
327///
328/// The standard forbids (at compile time) conversion to and from
329/// virtual bases, which is why we don't have to consider them here.
330///
331/// The standard forbids (at run time) casting a derived MP to a base
332/// MP when the derived MP does not point to a member of the base.
333/// This is why -1 is a reasonable choice for null data member
334/// pointers.
John McCalld608cdb2010-08-22 10:59:02 +0000335llvm::Value *
John McCall0bab0cd2010-08-23 01:21:21 +0000336ItaniumCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF,
337 const CastExpr *E,
John McCall4d4e5c12012-02-15 01:22:51 +0000338 llvm::Value *src) {
John McCall2de56d12010-08-25 11:45:40 +0000339 assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
John McCall4d4e5c12012-02-15 01:22:51 +0000340 E->getCastKind() == CK_BaseToDerivedMemberPointer ||
341 E->getCastKind() == CK_ReinterpretMemberPointer);
342
343 // Under Itanium, reinterprets don't require any additional processing.
344 if (E->getCastKind() == CK_ReinterpretMemberPointer) return src;
345
346 // Use constant emission if we can.
347 if (isa<llvm::Constant>(src))
348 return EmitMemberPointerConversion(E, cast<llvm::Constant>(src));
349
350 llvm::Constant *adj = getMemberPointerAdjustment(E);
351 if (!adj) return src;
John McCall3023def2010-08-22 03:04:22 +0000352
353 CGBuilderTy &Builder = CGF.Builder;
John McCall4d4e5c12012-02-15 01:22:51 +0000354 bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
John McCall3023def2010-08-22 03:04:22 +0000355
John McCall4d4e5c12012-02-15 01:22:51 +0000356 const MemberPointerType *destTy =
357 E->getType()->castAs<MemberPointerType>();
John McCall875ab102010-08-22 06:43:33 +0000358
John McCall0bab0cd2010-08-23 01:21:21 +0000359 // For member data pointers, this is just a matter of adding the
360 // offset if the source is non-null.
John McCall4d4e5c12012-02-15 01:22:51 +0000361 if (destTy->isMemberDataPointer()) {
362 llvm::Value *dst;
363 if (isDerivedToBase)
364 dst = Builder.CreateNSWSub(src, adj, "adj");
John McCall0bab0cd2010-08-23 01:21:21 +0000365 else
John McCall4d4e5c12012-02-15 01:22:51 +0000366 dst = Builder.CreateNSWAdd(src, adj, "adj");
John McCall0bab0cd2010-08-23 01:21:21 +0000367
368 // Null check.
John McCall4d4e5c12012-02-15 01:22:51 +0000369 llvm::Value *null = llvm::Constant::getAllOnesValue(src->getType());
370 llvm::Value *isNull = Builder.CreateICmpEQ(src, null, "memptr.isnull");
371 return Builder.CreateSelect(isNull, src, dst);
John McCall0bab0cd2010-08-23 01:21:21 +0000372 }
373
John McCalld608cdb2010-08-22 10:59:02 +0000374 // The this-adjustment is left-shifted by 1 on ARM.
375 if (IsARM) {
John McCall4d4e5c12012-02-15 01:22:51 +0000376 uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue();
377 offset <<= 1;
378 adj = llvm::ConstantInt::get(adj->getType(), offset);
John McCalld608cdb2010-08-22 10:59:02 +0000379 }
380
John McCall4d4e5c12012-02-15 01:22:51 +0000381 llvm::Value *srcAdj = Builder.CreateExtractValue(src, 1, "src.adj");
382 llvm::Value *dstAdj;
383 if (isDerivedToBase)
384 dstAdj = Builder.CreateNSWSub(srcAdj, adj, "adj");
John McCalld608cdb2010-08-22 10:59:02 +0000385 else
John McCall4d4e5c12012-02-15 01:22:51 +0000386 dstAdj = Builder.CreateNSWAdd(srcAdj, adj, "adj");
John McCalld608cdb2010-08-22 10:59:02 +0000387
John McCall4d4e5c12012-02-15 01:22:51 +0000388 return Builder.CreateInsertValue(src, dstAdj, 1);
389}
390
391llvm::Constant *
392ItaniumCXXABI::EmitMemberPointerConversion(const CastExpr *E,
393 llvm::Constant *src) {
394 assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
395 E->getCastKind() == CK_BaseToDerivedMemberPointer ||
396 E->getCastKind() == CK_ReinterpretMemberPointer);
397
398 // Under Itanium, reinterprets don't require any additional processing.
399 if (E->getCastKind() == CK_ReinterpretMemberPointer) return src;
400
401 // If the adjustment is trivial, we don't need to do anything.
402 llvm::Constant *adj = getMemberPointerAdjustment(E);
403 if (!adj) return src;
404
405 bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
406
407 const MemberPointerType *destTy =
408 E->getType()->castAs<MemberPointerType>();
409
410 // For member data pointers, this is just a matter of adding the
411 // offset if the source is non-null.
412 if (destTy->isMemberDataPointer()) {
413 // null maps to null.
414 if (src->isAllOnesValue()) return src;
415
416 if (isDerivedToBase)
417 return llvm::ConstantExpr::getNSWSub(src, adj);
418 else
419 return llvm::ConstantExpr::getNSWAdd(src, adj);
420 }
421
422 // The this-adjustment is left-shifted by 1 on ARM.
423 if (IsARM) {
424 uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue();
425 offset <<= 1;
426 adj = llvm::ConstantInt::get(adj->getType(), offset);
427 }
428
429 llvm::Constant *srcAdj = llvm::ConstantExpr::getExtractValue(src, 1);
430 llvm::Constant *dstAdj;
431 if (isDerivedToBase)
432 dstAdj = llvm::ConstantExpr::getNSWSub(srcAdj, adj);
433 else
434 dstAdj = llvm::ConstantExpr::getNSWAdd(srcAdj, adj);
435
436 return llvm::ConstantExpr::getInsertValue(src, dstAdj, 1);
John McCall3023def2010-08-22 03:04:22 +0000437}
John McCallcf2c85e2010-08-22 04:16:24 +0000438
439llvm::Constant *
John McCall0bab0cd2010-08-23 01:21:21 +0000440ItaniumCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) {
Chris Lattner2acc6e32011-07-18 04:24:23 +0000441 llvm::Type *ptrdiff_t = getPtrDiffTy();
John McCall0bab0cd2010-08-23 01:21:21 +0000442
443 // Itanium C++ ABI 2.3:
444 // A NULL pointer is represented as -1.
445 if (MPT->isMemberDataPointer())
446 return llvm::ConstantInt::get(ptrdiff_t, -1ULL, /*isSigned=*/true);
John McCalld608cdb2010-08-22 10:59:02 +0000447
448 llvm::Constant *Zero = llvm::ConstantInt::get(ptrdiff_t, 0);
449 llvm::Constant *Values[2] = { Zero, Zero };
Chris Lattnerc5cbb902011-06-20 04:01:35 +0000450 return llvm::ConstantStruct::getAnon(Values);
John McCallcf2c85e2010-08-22 04:16:24 +0000451}
452
John McCall5808ce42011-02-03 08:15:49 +0000453llvm::Constant *
454ItaniumCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT,
455 CharUnits offset) {
John McCall0bab0cd2010-08-23 01:21:21 +0000456 // Itanium C++ ABI 2.3:
457 // A pointer to data member is an offset from the base address of
458 // the class object containing it, represented as a ptrdiff_t
John McCall5808ce42011-02-03 08:15:49 +0000459 return llvm::ConstantInt::get(getPtrDiffTy(), offset.getQuantity());
John McCall0bab0cd2010-08-23 01:21:21 +0000460}
461
John McCall755d8492011-04-12 00:42:48 +0000462llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) {
Richard Smith2d6a5672012-01-14 04:30:29 +0000463 return BuildMemberPointer(MD, CharUnits::Zero());
464}
465
466llvm::Constant *ItaniumCXXABI::BuildMemberPointer(const CXXMethodDecl *MD,
467 CharUnits ThisAdjustment) {
John McCalld608cdb2010-08-22 10:59:02 +0000468 assert(MD->isInstance() && "Member function must not be static!");
469 MD = MD->getCanonicalDecl();
470
471 CodeGenTypes &Types = CGM.getTypes();
Chris Lattner2acc6e32011-07-18 04:24:23 +0000472 llvm::Type *ptrdiff_t = getPtrDiffTy();
John McCalld608cdb2010-08-22 10:59:02 +0000473
474 // Get the function pointer (or index if this is a virtual function).
475 llvm::Constant *MemPtr[2];
476 if (MD->isVirtual()) {
Peter Collingbourne1d2b3172011-09-26 01:56:30 +0000477 uint64_t Index = CGM.getVTableContext().getMethodVTableIndex(MD);
John McCalld608cdb2010-08-22 10:59:02 +0000478
Ken Dyck1246ba62011-04-09 01:30:02 +0000479 const ASTContext &Context = getContext();
480 CharUnits PointerWidth =
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000481 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
Ken Dyck1246ba62011-04-09 01:30:02 +0000482 uint64_t VTableOffset = (Index * PointerWidth.getQuantity());
John McCalld608cdb2010-08-22 10:59:02 +0000483
484 if (IsARM) {
485 // ARM C++ ABI 3.2.1:
486 // This ABI specifies that adj contains twice the this
487 // adjustment, plus 1 if the member function is virtual. The
488 // least significant bit of adj then makes exactly the same
489 // discrimination as the least significant bit of ptr does for
490 // Itanium.
491 MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset);
Richard Smith2d6a5672012-01-14 04:30:29 +0000492 MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t,
493 2 * ThisAdjustment.getQuantity() + 1);
John McCalld608cdb2010-08-22 10:59:02 +0000494 } else {
495 // Itanium C++ ABI 2.3:
496 // For a virtual function, [the pointer field] is 1 plus the
497 // virtual table offset (in bytes) of the function,
498 // represented as a ptrdiff_t.
499 MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset + 1);
Richard Smith2d6a5672012-01-14 04:30:29 +0000500 MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t,
501 ThisAdjustment.getQuantity());
John McCalld608cdb2010-08-22 10:59:02 +0000502 }
503 } else {
John McCall755d8492011-04-12 00:42:48 +0000504 const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
Chris Lattner2acc6e32011-07-18 04:24:23 +0000505 llvm::Type *Ty;
John McCall755d8492011-04-12 00:42:48 +0000506 // Check whether the function has a computable LLVM signature.
Chris Lattnerf742eb02011-07-10 00:18:59 +0000507 if (Types.isFuncTypeConvertible(FPT)) {
John McCall755d8492011-04-12 00:42:48 +0000508 // The function has a computable LLVM signature; use the correct type.
John McCallde5d3c72012-02-17 03:33:10 +0000509 Ty = Types.GetFunctionType(Types.arrangeCXXMethodDeclaration(MD));
John McCalld608cdb2010-08-22 10:59:02 +0000510 } else {
John McCall755d8492011-04-12 00:42:48 +0000511 // Use an arbitrary non-function type to tell GetAddrOfFunction that the
512 // function type is incomplete.
513 Ty = ptrdiff_t;
John McCalld608cdb2010-08-22 10:59:02 +0000514 }
John McCall755d8492011-04-12 00:42:48 +0000515 llvm::Constant *addr = CGM.GetAddrOfFunction(MD, Ty);
John McCalld608cdb2010-08-22 10:59:02 +0000516
John McCall379b5152011-04-11 07:02:50 +0000517 MemPtr[0] = llvm::ConstantExpr::getPtrToInt(addr, ptrdiff_t);
Richard Smith2d6a5672012-01-14 04:30:29 +0000518 MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, (IsARM ? 2 : 1) *
519 ThisAdjustment.getQuantity());
John McCalld608cdb2010-08-22 10:59:02 +0000520 }
John McCall875ab102010-08-22 06:43:33 +0000521
Chris Lattnerc5cbb902011-06-20 04:01:35 +0000522 return llvm::ConstantStruct::getAnon(MemPtr);
John McCall875ab102010-08-22 06:43:33 +0000523}
524
Richard Smith2d6a5672012-01-14 04:30:29 +0000525llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const APValue &MP,
526 QualType MPType) {
527 const MemberPointerType *MPT = MPType->castAs<MemberPointerType>();
528 const ValueDecl *MPD = MP.getMemberPointerDecl();
529 if (!MPD)
530 return EmitNullMemberPointer(MPT);
531
532 // Compute the this-adjustment.
533 CharUnits ThisAdjustment = CharUnits::Zero();
534 ArrayRef<const CXXRecordDecl*> Path = MP.getMemberPointerPath();
535 bool DerivedMember = MP.isMemberPointerToDerivedMember();
536 const CXXRecordDecl *RD = cast<CXXRecordDecl>(MPD->getDeclContext());
537 for (unsigned I = 0, N = Path.size(); I != N; ++I) {
538 const CXXRecordDecl *Base = RD;
539 const CXXRecordDecl *Derived = Path[I];
540 if (DerivedMember)
541 std::swap(Base, Derived);
542 ThisAdjustment +=
543 getContext().getASTRecordLayout(Derived).getBaseClassOffset(Base);
544 RD = Path[I];
545 }
546 if (DerivedMember)
547 ThisAdjustment = -ThisAdjustment;
548
549 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MPD))
550 return BuildMemberPointer(MD, ThisAdjustment);
551
552 CharUnits FieldOffset =
553 getContext().toCharUnitsFromBits(getContext().getFieldOffset(MPD));
554 return EmitMemberDataPointer(MPT, ThisAdjustment + FieldOffset);
555}
556
John McCalle9fd7eb2010-08-22 08:30:07 +0000557/// The comparison algorithm is pretty easy: the member pointers are
558/// the same if they're either bitwise identical *or* both null.
559///
560/// ARM is different here only because null-ness is more complicated.
561llvm::Value *
John McCall0bab0cd2010-08-23 01:21:21 +0000562ItaniumCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF,
563 llvm::Value *L,
564 llvm::Value *R,
565 const MemberPointerType *MPT,
566 bool Inequality) {
John McCalle9fd7eb2010-08-22 08:30:07 +0000567 CGBuilderTy &Builder = CGF.Builder;
568
John McCalle9fd7eb2010-08-22 08:30:07 +0000569 llvm::ICmpInst::Predicate Eq;
570 llvm::Instruction::BinaryOps And, Or;
571 if (Inequality) {
572 Eq = llvm::ICmpInst::ICMP_NE;
573 And = llvm::Instruction::Or;
574 Or = llvm::Instruction::And;
575 } else {
576 Eq = llvm::ICmpInst::ICMP_EQ;
577 And = llvm::Instruction::And;
578 Or = llvm::Instruction::Or;
579 }
580
John McCall0bab0cd2010-08-23 01:21:21 +0000581 // Member data pointers are easy because there's a unique null
582 // value, so it just comes down to bitwise equality.
583 if (MPT->isMemberDataPointer())
584 return Builder.CreateICmp(Eq, L, R);
585
586 // For member function pointers, the tautologies are more complex.
587 // The Itanium tautology is:
John McCallde719f72010-08-23 06:56:36 +0000588 // (L == R) <==> (L.ptr == R.ptr && (L.ptr == 0 || L.adj == R.adj))
John McCall0bab0cd2010-08-23 01:21:21 +0000589 // The ARM tautology is:
John McCallde719f72010-08-23 06:56:36 +0000590 // (L == R) <==> (L.ptr == R.ptr &&
591 // (L.adj == R.adj ||
592 // (L.ptr == 0 && ((L.adj|R.adj) & 1) == 0)))
John McCall0bab0cd2010-08-23 01:21:21 +0000593 // The inequality tautologies have exactly the same structure, except
594 // applying De Morgan's laws.
595
596 llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr");
597 llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr");
598
John McCalle9fd7eb2010-08-22 08:30:07 +0000599 // This condition tests whether L.ptr == R.ptr. This must always be
600 // true for equality to hold.
601 llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr");
602
603 // This condition, together with the assumption that L.ptr == R.ptr,
604 // tests whether the pointers are both null. ARM imposes an extra
605 // condition.
606 llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType());
607 llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null");
608
609 // This condition tests whether L.adj == R.adj. If this isn't
610 // true, the pointers are unequal unless they're both null.
John McCalld608cdb2010-08-22 10:59:02 +0000611 llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj");
612 llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj");
John McCalle9fd7eb2010-08-22 08:30:07 +0000613 llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj");
614
615 // Null member function pointers on ARM clear the low bit of Adj,
616 // so the zero condition has to check that neither low bit is set.
617 if (IsARM) {
618 llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1);
619
620 // Compute (l.adj | r.adj) & 1 and test it against zero.
621 llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj");
622 llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One);
623 llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero,
624 "cmp.or.adj");
625 EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero);
626 }
627
628 // Tie together all our conditions.
629 llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq);
630 Result = Builder.CreateBinOp(And, PtrEq, Result,
631 Inequality ? "memptr.ne" : "memptr.eq");
632 return Result;
633}
634
635llvm::Value *
John McCall0bab0cd2010-08-23 01:21:21 +0000636ItaniumCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
637 llvm::Value *MemPtr,
638 const MemberPointerType *MPT) {
John McCalle9fd7eb2010-08-22 08:30:07 +0000639 CGBuilderTy &Builder = CGF.Builder;
John McCall0bab0cd2010-08-23 01:21:21 +0000640
641 /// For member data pointers, this is just a check against -1.
642 if (MPT->isMemberDataPointer()) {
643 assert(MemPtr->getType() == getPtrDiffTy());
644 llvm::Value *NegativeOne =
645 llvm::Constant::getAllOnesValue(MemPtr->getType());
646 return Builder.CreateICmpNE(MemPtr, NegativeOne, "memptr.tobool");
647 }
John McCalle9fd7eb2010-08-22 08:30:07 +0000648
Daniel Dunbardb27b5f2011-04-19 23:10:47 +0000649 // In Itanium, a member function pointer is not null if 'ptr' is not null.
John McCalld608cdb2010-08-22 10:59:02 +0000650 llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr");
John McCalle9fd7eb2010-08-22 08:30:07 +0000651
652 llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0);
653 llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool");
654
Daniel Dunbardb27b5f2011-04-19 23:10:47 +0000655 // On ARM, a member function pointer is also non-null if the low bit of 'adj'
656 // (the virtual bit) is set.
John McCalle9fd7eb2010-08-22 08:30:07 +0000657 if (IsARM) {
658 llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1);
John McCalld608cdb2010-08-22 10:59:02 +0000659 llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj");
John McCalle9fd7eb2010-08-22 08:30:07 +0000660 llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit");
Daniel Dunbardb27b5f2011-04-19 23:10:47 +0000661 llvm::Value *IsVirtual = Builder.CreateICmpNE(VirtualBit, Zero,
662 "memptr.isvirtual");
663 Result = Builder.CreateOr(Result, IsVirtual);
John McCalle9fd7eb2010-08-22 08:30:07 +0000664 }
665
666 return Result;
667}
John McCall875ab102010-08-22 06:43:33 +0000668
John McCallf16aa102010-08-22 21:01:12 +0000669/// The Itanium ABI requires non-zero initialization only for data
670/// member pointers, for which '0' is a valid offset.
671bool ItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
672 return MPT->getPointeeType()->isFunctionType();
John McCallcf2c85e2010-08-22 04:16:24 +0000673}
John McCall4c40d982010-08-31 07:33:07 +0000674
675/// The generic ABI passes 'this', plus a VTT if it's initializing a
676/// base subobject.
677void ItaniumCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor,
678 CXXCtorType Type,
679 CanQualType &ResTy,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000680 SmallVectorImpl<CanQualType> &ArgTys) {
John McCall9cb2cee2010-09-02 10:25:57 +0000681 ASTContext &Context = getContext();
John McCall4c40d982010-08-31 07:33:07 +0000682
683 // 'this' is already there.
684
685 // Check if we need to add a VTT parameter (which has type void **).
686 if (Type == Ctor_Base && Ctor->getParent()->getNumVBases() != 0)
687 ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
688}
689
690/// The ARM ABI does the same as the Itanium ABI, but returns 'this'.
691void ARMCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor,
692 CXXCtorType Type,
693 CanQualType &ResTy,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000694 SmallVectorImpl<CanQualType> &ArgTys) {
John McCall4c40d982010-08-31 07:33:07 +0000695 ItaniumCXXABI::BuildConstructorSignature(Ctor, Type, ResTy, ArgTys);
696 ResTy = ArgTys[0];
697}
698
699/// The generic ABI passes 'this', plus a VTT if it's destroying a
700/// base subobject.
701void ItaniumCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor,
702 CXXDtorType Type,
703 CanQualType &ResTy,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000704 SmallVectorImpl<CanQualType> &ArgTys) {
John McCall9cb2cee2010-09-02 10:25:57 +0000705 ASTContext &Context = getContext();
John McCall4c40d982010-08-31 07:33:07 +0000706
707 // 'this' is already there.
708
709 // Check if we need to add a VTT parameter (which has type void **).
710 if (Type == Dtor_Base && Dtor->getParent()->getNumVBases() != 0)
711 ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
712}
713
714/// The ARM ABI does the same as the Itanium ABI, but returns 'this'
715/// for non-deleting destructors.
716void ARMCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor,
717 CXXDtorType Type,
718 CanQualType &ResTy,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000719 SmallVectorImpl<CanQualType> &ArgTys) {
John McCall4c40d982010-08-31 07:33:07 +0000720 ItaniumCXXABI::BuildDestructorSignature(Dtor, Type, ResTy, ArgTys);
721
722 if (Type != Dtor_Deleting)
723 ResTy = ArgTys[0];
724}
725
726void ItaniumCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF,
727 QualType &ResTy,
728 FunctionArgList &Params) {
729 /// Create the 'this' variable.
730 BuildThisParam(CGF, Params);
731
732 const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl());
733 assert(MD->isInstance());
734
735 // Check if we need a VTT parameter as well.
736 if (CodeGenVTables::needsVTTParameter(CGF.CurGD)) {
John McCall9cb2cee2010-09-02 10:25:57 +0000737 ASTContext &Context = getContext();
John McCall4c40d982010-08-31 07:33:07 +0000738
739 // FIXME: avoid the fake decl
740 QualType T = Context.getPointerType(Context.VoidPtrTy);
741 ImplicitParamDecl *VTTDecl
742 = ImplicitParamDecl::Create(Context, 0, MD->getLocation(),
743 &Context.Idents.get("vtt"), T);
John McCalld26bc762011-03-09 04:27:21 +0000744 Params.push_back(VTTDecl);
John McCall4c40d982010-08-31 07:33:07 +0000745 getVTTDecl(CGF) = VTTDecl;
746 }
747}
748
749void ARMCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF,
750 QualType &ResTy,
751 FunctionArgList &Params) {
752 ItaniumCXXABI::BuildInstanceFunctionParams(CGF, ResTy, Params);
753
754 // Return 'this' from certain constructors and destructors.
755 if (HasThisReturn(CGF.CurGD))
John McCalld26bc762011-03-09 04:27:21 +0000756 ResTy = Params[0]->getType();
John McCall4c40d982010-08-31 07:33:07 +0000757}
758
759void ItaniumCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
760 /// Initialize the 'this' slot.
761 EmitThisParam(CGF);
762
763 /// Initialize the 'vtt' slot if needed.
764 if (getVTTDecl(CGF)) {
765 getVTTValue(CGF)
766 = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(getVTTDecl(CGF)),
767 "vtt");
768 }
769}
770
771void ARMCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
772 ItaniumCXXABI::EmitInstanceFunctionProlog(CGF);
773
774 /// Initialize the return slot to 'this' at the start of the
775 /// function.
776 if (HasThisReturn(CGF.CurGD))
Eli Friedmancec5ebd2012-02-11 02:57:39 +0000777 CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue);
John McCall4c40d982010-08-31 07:33:07 +0000778}
779
780void ARMCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF,
781 RValue RV, QualType ResultType) {
782 if (!isa<CXXDestructorDecl>(CGF.CurGD.getDecl()))
783 return ItaniumCXXABI::EmitReturnFromThunk(CGF, RV, ResultType);
784
785 // Destructor thunks in the ARM ABI have indeterminate results.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000786 llvm::Type *T =
John McCall4c40d982010-08-31 07:33:07 +0000787 cast<llvm::PointerType>(CGF.ReturnValue->getType())->getElementType();
788 RValue Undef = RValue::get(llvm::UndefValue::get(T));
789 return ItaniumCXXABI::EmitReturnFromThunk(CGF, Undef, ResultType);
790}
John McCall1e7fe752010-09-02 09:58:18 +0000791
792/************************** Array allocation cookies **************************/
793
John McCalle2b45e22012-05-01 05:23:51 +0000794CharUnits ItaniumCXXABI::getArrayCookieSizeImpl(QualType elementType) {
795 // The array cookie is a size_t; pad that up to the element alignment.
796 // The cookie is actually right-justified in that space.
797 return std::max(CharUnits::fromQuantity(CGM.SizeSizeInBytes),
798 CGM.getContext().getTypeAlignInChars(elementType));
John McCall1e7fe752010-09-02 09:58:18 +0000799}
800
801llvm::Value *ItaniumCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
802 llvm::Value *NewPtr,
803 llvm::Value *NumElements,
John McCall6ec278d2011-01-27 09:37:56 +0000804 const CXXNewExpr *expr,
John McCall1e7fe752010-09-02 09:58:18 +0000805 QualType ElementType) {
John McCalle2b45e22012-05-01 05:23:51 +0000806 assert(requiresArrayCookie(expr));
John McCall1e7fe752010-09-02 09:58:18 +0000807
808 unsigned AS = cast<llvm::PointerType>(NewPtr->getType())->getAddressSpace();
809
John McCall9cb2cee2010-09-02 10:25:57 +0000810 ASTContext &Ctx = getContext();
John McCall1e7fe752010-09-02 09:58:18 +0000811 QualType SizeTy = Ctx.getSizeType();
812 CharUnits SizeSize = Ctx.getTypeSizeInChars(SizeTy);
813
814 // The size of the cookie.
815 CharUnits CookieSize =
816 std::max(SizeSize, Ctx.getTypeAlignInChars(ElementType));
John McCalle2b45e22012-05-01 05:23:51 +0000817 assert(CookieSize == getArrayCookieSizeImpl(ElementType));
John McCall1e7fe752010-09-02 09:58:18 +0000818
819 // Compute an offset to the cookie.
820 llvm::Value *CookiePtr = NewPtr;
821 CharUnits CookieOffset = CookieSize - SizeSize;
822 if (!CookieOffset.isZero())
823 CookiePtr = CGF.Builder.CreateConstInBoundsGEP1_64(CookiePtr,
824 CookieOffset.getQuantity());
825
826 // Write the number of elements into the appropriate slot.
827 llvm::Value *NumElementsPtr
828 = CGF.Builder.CreateBitCast(CookiePtr,
829 CGF.ConvertType(SizeTy)->getPointerTo(AS));
830 CGF.Builder.CreateStore(NumElements, NumElementsPtr);
831
832 // Finally, compute a pointer to the actual data buffer by skipping
833 // over the cookie completely.
834 return CGF.Builder.CreateConstInBoundsGEP1_64(NewPtr,
835 CookieSize.getQuantity());
836}
837
John McCalle2b45e22012-05-01 05:23:51 +0000838llvm::Value *ItaniumCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
839 llvm::Value *allocPtr,
840 CharUnits cookieSize) {
841 // The element size is right-justified in the cookie.
842 llvm::Value *numElementsPtr = allocPtr;
843 CharUnits numElementsOffset =
844 cookieSize - CharUnits::fromQuantity(CGF.SizeSizeInBytes);
845 if (!numElementsOffset.isZero())
846 numElementsPtr =
847 CGF.Builder.CreateConstInBoundsGEP1_64(numElementsPtr,
848 numElementsOffset.getQuantity());
John McCall1e7fe752010-09-02 09:58:18 +0000849
John McCalle2b45e22012-05-01 05:23:51 +0000850 unsigned AS = cast<llvm::PointerType>(allocPtr->getType())->getAddressSpace();
851 numElementsPtr =
852 CGF.Builder.CreateBitCast(numElementsPtr, CGF.SizeTy->getPointerTo(AS));
853 return CGF.Builder.CreateLoad(numElementsPtr);
John McCall1e7fe752010-09-02 09:58:18 +0000854}
855
John McCalle2b45e22012-05-01 05:23:51 +0000856CharUnits ARMCXXABI::getArrayCookieSizeImpl(QualType elementType) {
John McCall1e7fe752010-09-02 09:58:18 +0000857 // On ARM, the cookie is always:
858 // struct array_cookie {
859 // std::size_t element_size; // element_size != 0
860 // std::size_t element_count;
861 // };
862 // TODO: what should we do if the allocated type actually wants
863 // greater alignment?
John McCalle2b45e22012-05-01 05:23:51 +0000864 return CharUnits::fromQuantity(2 * CGM.SizeSizeInBytes);
John McCall1e7fe752010-09-02 09:58:18 +0000865}
866
867llvm::Value *ARMCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
868 llvm::Value *NewPtr,
869 llvm::Value *NumElements,
John McCall6ec278d2011-01-27 09:37:56 +0000870 const CXXNewExpr *expr,
John McCall1e7fe752010-09-02 09:58:18 +0000871 QualType ElementType) {
John McCalle2b45e22012-05-01 05:23:51 +0000872 assert(requiresArrayCookie(expr));
John McCall1e7fe752010-09-02 09:58:18 +0000873
874 // NewPtr is a char*.
875
876 unsigned AS = cast<llvm::PointerType>(NewPtr->getType())->getAddressSpace();
877
John McCall9cb2cee2010-09-02 10:25:57 +0000878 ASTContext &Ctx = getContext();
John McCall1e7fe752010-09-02 09:58:18 +0000879 CharUnits SizeSize = Ctx.getTypeSizeInChars(Ctx.getSizeType());
Chris Lattner2acc6e32011-07-18 04:24:23 +0000880 llvm::IntegerType *SizeTy =
John McCall1e7fe752010-09-02 09:58:18 +0000881 cast<llvm::IntegerType>(CGF.ConvertType(Ctx.getSizeType()));
882
883 // The cookie is always at the start of the buffer.
884 llvm::Value *CookiePtr = NewPtr;
885
886 // The first element is the element size.
887 CookiePtr = CGF.Builder.CreateBitCast(CookiePtr, SizeTy->getPointerTo(AS));
888 llvm::Value *ElementSize = llvm::ConstantInt::get(SizeTy,
889 Ctx.getTypeSizeInChars(ElementType).getQuantity());
890 CGF.Builder.CreateStore(ElementSize, CookiePtr);
891
892 // The second element is the element count.
893 CookiePtr = CGF.Builder.CreateConstInBoundsGEP1_32(CookiePtr, 1);
894 CGF.Builder.CreateStore(NumElements, CookiePtr);
895
896 // Finally, compute a pointer to the actual data buffer by skipping
897 // over the cookie completely.
898 CharUnits CookieSize = 2 * SizeSize;
899 return CGF.Builder.CreateConstInBoundsGEP1_64(NewPtr,
900 CookieSize.getQuantity());
901}
902
John McCalle2b45e22012-05-01 05:23:51 +0000903llvm::Value *ARMCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
904 llvm::Value *allocPtr,
905 CharUnits cookieSize) {
906 // The number of elements is at offset sizeof(size_t) relative to
907 // the allocated pointer.
908 llvm::Value *numElementsPtr
909 = CGF.Builder.CreateConstInBoundsGEP1_64(allocPtr, CGF.SizeSizeInBytes);
John McCall1e7fe752010-09-02 09:58:18 +0000910
John McCalle2b45e22012-05-01 05:23:51 +0000911 unsigned AS = cast<llvm::PointerType>(allocPtr->getType())->getAddressSpace();
912 numElementsPtr =
913 CGF.Builder.CreateBitCast(numElementsPtr, CGF.SizeTy->getPointerTo(AS));
914 return CGF.Builder.CreateLoad(numElementsPtr);
John McCall1e7fe752010-09-02 09:58:18 +0000915}
916
John McCall5cd91b52010-09-08 01:44:27 +0000917/*********************** Static local initialization **************************/
918
919static llvm::Constant *getGuardAcquireFn(CodeGenModule &CGM,
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000920 llvm::PointerType *GuardPtrTy) {
John McCall5cd91b52010-09-08 01:44:27 +0000921 // int __cxa_guard_acquire(__guard *guard_object);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000922 llvm::FunctionType *FTy =
John McCall5cd91b52010-09-08 01:44:27 +0000923 llvm::FunctionType::get(CGM.getTypes().ConvertType(CGM.getContext().IntTy),
Jay Foadda549e82011-07-29 13:56:53 +0000924 GuardPtrTy, /*isVarArg=*/false);
John McCall5cd91b52010-09-08 01:44:27 +0000925
Nick Lewyckye76872e2012-02-13 23:45:02 +0000926 return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_acquire",
927 llvm::Attribute::NoUnwind);
John McCall5cd91b52010-09-08 01:44:27 +0000928}
929
930static llvm::Constant *getGuardReleaseFn(CodeGenModule &CGM,
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000931 llvm::PointerType *GuardPtrTy) {
John McCall5cd91b52010-09-08 01:44:27 +0000932 // void __cxa_guard_release(__guard *guard_object);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000933 llvm::FunctionType *FTy =
Chris Lattner8b418682012-02-07 00:39:47 +0000934 llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false);
John McCall5cd91b52010-09-08 01:44:27 +0000935
Nick Lewyckye76872e2012-02-13 23:45:02 +0000936 return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_release",
937 llvm::Attribute::NoUnwind);
John McCall5cd91b52010-09-08 01:44:27 +0000938}
939
940static llvm::Constant *getGuardAbortFn(CodeGenModule &CGM,
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000941 llvm::PointerType *GuardPtrTy) {
John McCall5cd91b52010-09-08 01:44:27 +0000942 // void __cxa_guard_abort(__guard *guard_object);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000943 llvm::FunctionType *FTy =
Chris Lattner8b418682012-02-07 00:39:47 +0000944 llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false);
John McCall5cd91b52010-09-08 01:44:27 +0000945
Nick Lewyckye76872e2012-02-13 23:45:02 +0000946 return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_abort",
947 llvm::Attribute::NoUnwind);
John McCall5cd91b52010-09-08 01:44:27 +0000948}
949
950namespace {
951 struct CallGuardAbort : EHScopeStack::Cleanup {
952 llvm::GlobalVariable *Guard;
Chandler Carruth0f30a122012-03-30 19:44:53 +0000953 CallGuardAbort(llvm::GlobalVariable *Guard) : Guard(Guard) {}
John McCall5cd91b52010-09-08 01:44:27 +0000954
John McCallad346f42011-07-12 20:27:29 +0000955 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCall5cd91b52010-09-08 01:44:27 +0000956 CGF.Builder.CreateCall(getGuardAbortFn(CGF.CGM, Guard->getType()), Guard)
957 ->setDoesNotThrow();
958 }
959 };
960}
961
962/// The ARM code here follows the Itanium code closely enough that we
963/// just special-case it at particular places.
John McCall3030eb82010-11-06 09:44:32 +0000964void ItaniumCXXABI::EmitGuardedInit(CodeGenFunction &CGF,
965 const VarDecl &D,
John McCall355bba72012-03-30 21:00:39 +0000966 llvm::GlobalVariable *var,
967 bool shouldPerformInit) {
John McCall5cd91b52010-09-08 01:44:27 +0000968 CGBuilderTy &Builder = CGF.Builder;
John McCall3030eb82010-11-06 09:44:32 +0000969
970 // We only need to use thread-safe statics for local variables;
971 // global initialization is always single-threaded.
John McCall0502a222011-06-17 07:33:57 +0000972 bool threadsafe =
David Blaikie4e4d0842012-03-11 07:00:24 +0000973 (getContext().getLangOpts().ThreadsafeStatics && D.isLocalVarDecl());
Anders Carlsson173d5122011-04-27 04:37:08 +0000974
Anders Carlsson173d5122011-04-27 04:37:08 +0000975 // If we have a global variable with internal linkage and thread-safe statics
976 // are disabled, we can just let the guard variable be of type i8.
John McCall355bba72012-03-30 21:00:39 +0000977 bool useInt8GuardVariable = !threadsafe && var->hasInternalLinkage();
978
979 llvm::IntegerType *guardTy;
John McCall0502a222011-06-17 07:33:57 +0000980 if (useInt8GuardVariable) {
John McCall355bba72012-03-30 21:00:39 +0000981 guardTy = CGF.Int8Ty;
John McCall0502a222011-06-17 07:33:57 +0000982 } else {
Anders Carlsson173d5122011-04-27 04:37:08 +0000983 // Guard variables are 64 bits in the generic ABI and 32 bits on ARM.
John McCall355bba72012-03-30 21:00:39 +0000984 guardTy = (IsARM ? CGF.Int32Ty : CGF.Int64Ty);
Anders Carlsson173d5122011-04-27 04:37:08 +0000985 }
John McCall355bba72012-03-30 21:00:39 +0000986 llvm::PointerType *guardPtrTy = guardTy->getPointerTo();
John McCall5cd91b52010-09-08 01:44:27 +0000987
John McCall355bba72012-03-30 21:00:39 +0000988 // Create the guard variable if we don't already have it (as we
989 // might if we're double-emitting this function body).
990 llvm::GlobalVariable *guard = CGM.getStaticLocalDeclGuardAddress(&D);
991 if (!guard) {
992 // Mangle the name for the guard.
993 SmallString<256> guardName;
994 {
995 llvm::raw_svector_ostream out(guardName);
996 getMangleContext().mangleItaniumGuardVariable(&D, out);
997 out.flush();
998 }
John McCall112c9672010-11-02 21:04:24 +0000999
John McCall355bba72012-03-30 21:00:39 +00001000 // Create the guard variable with a zero-initializer.
1001 // Just absorb linkage and visibility from the guarded variable.
1002 guard = new llvm::GlobalVariable(CGM.getModule(), guardTy,
1003 false, var->getLinkage(),
1004 llvm::ConstantInt::get(guardTy, 0),
1005 guardName.str());
1006 guard->setVisibility(var->getVisibility());
1007
1008 CGM.setStaticLocalDeclGuardAddress(&D, guard);
1009 }
John McCall49d26d22012-03-30 07:09:50 +00001010
John McCall5cd91b52010-09-08 01:44:27 +00001011 // Test whether the variable has completed initialization.
John McCall355bba72012-03-30 21:00:39 +00001012 llvm::Value *isInitialized;
John McCall5cd91b52010-09-08 01:44:27 +00001013
1014 // ARM C++ ABI 3.2.3.1:
1015 // To support the potential use of initialization guard variables
1016 // as semaphores that are the target of ARM SWP and LDREX/STREX
1017 // synchronizing instructions we define a static initialization
1018 // guard variable to be a 4-byte aligned, 4- byte word with the
1019 // following inline access protocol.
1020 // #define INITIALIZED 1
1021 // if ((obj_guard & INITIALIZED) != INITIALIZED) {
1022 // if (__cxa_guard_acquire(&obj_guard))
1023 // ...
1024 // }
John McCall0502a222011-06-17 07:33:57 +00001025 if (IsARM && !useInt8GuardVariable) {
John McCall355bba72012-03-30 21:00:39 +00001026 llvm::Value *V = Builder.CreateLoad(guard);
John McCall5cd91b52010-09-08 01:44:27 +00001027 V = Builder.CreateAnd(V, Builder.getInt32(1));
John McCall355bba72012-03-30 21:00:39 +00001028 isInitialized = Builder.CreateIsNull(V, "guard.uninitialized");
John McCall5cd91b52010-09-08 01:44:27 +00001029
1030 // Itanium C++ ABI 3.3.2:
1031 // The following is pseudo-code showing how these functions can be used:
1032 // if (obj_guard.first_byte == 0) {
1033 // if ( __cxa_guard_acquire (&obj_guard) ) {
1034 // try {
1035 // ... initialize the object ...;
1036 // } catch (...) {
1037 // __cxa_guard_abort (&obj_guard);
1038 // throw;
1039 // }
1040 // ... queue object destructor with __cxa_atexit() ...;
1041 // __cxa_guard_release (&obj_guard);
1042 // }
1043 // }
1044 } else {
1045 // Load the first byte of the guard variable.
Chandler Carruth0f30a122012-03-30 19:44:53 +00001046 llvm::LoadInst *LI =
John McCall355bba72012-03-30 21:00:39 +00001047 Builder.CreateLoad(Builder.CreateBitCast(guard, CGM.Int8PtrTy));
Chandler Carruth0f30a122012-03-30 19:44:53 +00001048 LI->setAlignment(1);
John McCall5cd91b52010-09-08 01:44:27 +00001049
Eli Friedmaneb43f4a2011-09-13 22:21:56 +00001050 // Itanium ABI:
1051 // An implementation supporting thread-safety on multiprocessor
1052 // systems must also guarantee that references to the initialized
1053 // object do not occur before the load of the initialization flag.
1054 //
1055 // In LLVM, we do this by marking the load Acquire.
1056 if (threadsafe)
Chandler Carruth0f30a122012-03-30 19:44:53 +00001057 LI->setAtomic(llvm::Acquire);
Eli Friedmaneb43f4a2011-09-13 22:21:56 +00001058
John McCall355bba72012-03-30 21:00:39 +00001059 isInitialized = Builder.CreateIsNull(LI, "guard.uninitialized");
John McCall5cd91b52010-09-08 01:44:27 +00001060 }
1061
1062 llvm::BasicBlock *InitCheckBlock = CGF.createBasicBlock("init.check");
1063 llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end");
1064
1065 // Check if the first byte of the guard variable is zero.
John McCall355bba72012-03-30 21:00:39 +00001066 Builder.CreateCondBr(isInitialized, InitCheckBlock, EndBlock);
John McCall5cd91b52010-09-08 01:44:27 +00001067
1068 CGF.EmitBlock(InitCheckBlock);
1069
1070 // Variables used when coping with thread-safe statics and exceptions.
John McCall0502a222011-06-17 07:33:57 +00001071 if (threadsafe) {
John McCall5cd91b52010-09-08 01:44:27 +00001072 // Call __cxa_guard_acquire.
1073 llvm::Value *V
John McCall355bba72012-03-30 21:00:39 +00001074 = Builder.CreateCall(getGuardAcquireFn(CGM, guardPtrTy), guard);
John McCall5cd91b52010-09-08 01:44:27 +00001075
1076 llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init");
1077
1078 Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"),
1079 InitBlock, EndBlock);
1080
1081 // Call __cxa_guard_abort along the exceptional edge.
John McCall355bba72012-03-30 21:00:39 +00001082 CGF.EHStack.pushCleanup<CallGuardAbort>(EHCleanup, guard);
John McCall5cd91b52010-09-08 01:44:27 +00001083
1084 CGF.EmitBlock(InitBlock);
1085 }
1086
1087 // Emit the initializer and add a global destructor if appropriate.
John McCall355bba72012-03-30 21:00:39 +00001088 CGF.EmitCXXGlobalVarDeclInit(D, var, shouldPerformInit);
John McCall5cd91b52010-09-08 01:44:27 +00001089
John McCall0502a222011-06-17 07:33:57 +00001090 if (threadsafe) {
John McCall5cd91b52010-09-08 01:44:27 +00001091 // Pop the guard-abort cleanup if we pushed one.
1092 CGF.PopCleanupBlock();
1093
1094 // Call __cxa_guard_release. This cannot throw.
John McCall355bba72012-03-30 21:00:39 +00001095 Builder.CreateCall(getGuardReleaseFn(CGM, guardPtrTy), guard);
John McCall5cd91b52010-09-08 01:44:27 +00001096 } else {
John McCall355bba72012-03-30 21:00:39 +00001097 Builder.CreateStore(llvm::ConstantInt::get(guardTy, 1), guard);
John McCall5cd91b52010-09-08 01:44:27 +00001098 }
1099
1100 CGF.EmitBlock(EndBlock);
1101}
John McCall20bb1752012-05-01 06:13:13 +00001102
1103/// Register a global destructor using __cxa_atexit.
1104static void emitGlobalDtorWithCXAAtExit(CodeGenFunction &CGF,
1105 llvm::Constant *dtor,
1106 llvm::Constant *addr) {
1107 // We're assuming that the destructor function is something we can
1108 // reasonably call with the default CC. Go ahead and cast it to the
1109 // right prototype.
1110 llvm::Type *dtorTy =
1111 llvm::FunctionType::get(CGF.VoidTy, CGF.Int8PtrTy, false)->getPointerTo();
1112
1113 // extern "C" int __cxa_atexit(void (*f)(void *), void *p, void *d);
1114 llvm::Type *paramTys[] = { dtorTy, CGF.Int8PtrTy, CGF.Int8PtrTy };
1115 llvm::FunctionType *atexitTy =
1116 llvm::FunctionType::get(CGF.IntTy, paramTys, false);
1117
1118 // Fetch the actual function.
1119 llvm::Constant *atexit =
1120 CGF.CGM.CreateRuntimeFunction(atexitTy, "__cxa_atexit");
1121 if (llvm::Function *fn = dyn_cast<llvm::Function>(atexit))
1122 fn->setDoesNotThrow();
1123
1124 // Create a variable that binds the atexit to this shared object.
1125 llvm::Constant *handle =
1126 CGF.CGM.CreateRuntimeVariable(CGF.Int8Ty, "__dso_handle");
1127
1128 llvm::Value *args[] = {
1129 llvm::ConstantExpr::getBitCast(dtor, dtorTy),
1130 llvm::ConstantExpr::getBitCast(addr, CGF.Int8PtrTy),
1131 handle
1132 };
1133 CGF.Builder.CreateCall(atexit, args)->setDoesNotThrow();
1134}
1135
1136/// Register a global destructor as best as we know how.
1137void ItaniumCXXABI::registerGlobalDtor(CodeGenFunction &CGF,
1138 llvm::Constant *dtor,
1139 llvm::Constant *addr) {
1140 // Use __cxa_atexit if available.
1141 if (CGM.getCodeGenOpts().CXAAtExit) {
1142 return emitGlobalDtorWithCXAAtExit(CGF, dtor, addr);
1143 }
1144
1145 // In Apple kexts, we want to add a global destructor entry.
1146 // FIXME: shouldn't this be guarded by some variable?
1147 if (CGM.getContext().getLangOpts().AppleKext) {
1148 // Generate a global destructor entry.
1149 return CGM.AddCXXDtorEntry(dtor, addr);
1150 }
1151
1152 CGF.registerGlobalDtorWithAtExit(dtor, addr);
1153}