1 //===------- ItaniumCXXABI.cpp - Emit LLVM Code from ASTs for a Module ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This provides C++ code generation targeting the Itanium C++ ABI. The class
11 // in this file generates structures that follow the Itanium C++ ABI, which is
13 // http://www.codesourcery.com/public/cxx-abi/abi.html
14 // http://www.codesourcery.com/public/cxx-abi/abi-eh.html
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
19 //===----------------------------------------------------------------------===//
22 #include "CGRecordLayout.h"
23 #include "CodeGenFunction.h"
24 #include "CodeGenModule.h"
25 #include <clang/AST/Mangle.h>
26 #include <clang/AST/Type.h>
27 #include <llvm/Intrinsics.h>
28 #include <llvm/Target/TargetData.h>
29 #include <llvm/Value.h>
31 using namespace clang;
32 using namespace CodeGen;
35 class ItaniumCXXABI : public CodeGen::CGCXXABI {
37 llvm::IntegerType *PtrDiffTy;
41 // It's a little silly for us to cache this.
42 llvm::IntegerType *getPtrDiffTy() {
44 QualType T = getContext().getPointerDiffType();
45 llvm::Type *Ty = CGM.getTypes().ConvertType(T);
46 PtrDiffTy = cast<llvm::IntegerType>(Ty);
51 bool NeedsArrayCookie(const CXXNewExpr *expr);
52 bool NeedsArrayCookie(const CXXDeleteExpr *expr,
53 QualType elementType);
56 ItaniumCXXABI(CodeGen::CodeGenModule &CGM, bool IsARM = false) :
57 CGCXXABI(CGM), PtrDiffTy(0), IsARM(IsARM) { }
59 bool isZeroInitializable(const MemberPointerType *MPT);
61 llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT);
63 llvm::Value *EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
65 llvm::Value *MemFnPtr,
66 const MemberPointerType *MPT);
68 llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF,
71 const MemberPointerType *MPT);
73 llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
76 llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
79 llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
81 llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
82 llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
84 llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT);
85 llvm::Constant *BuildMemberPointer(const CXXMethodDecl *MD,
86 CharUnits ThisAdjustment);
88 llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF,
91 const MemberPointerType *MPT,
94 llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
96 const MemberPointerType *MPT);
98 void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
101 SmallVectorImpl<CanQualType> &ArgTys);
103 void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
106 SmallVectorImpl<CanQualType> &ArgTys);
108 void BuildInstanceFunctionParams(CodeGenFunction &CGF,
110 FunctionArgList &Params);
112 void EmitInstanceFunctionProlog(CodeGenFunction &CGF);
114 CharUnits GetArrayCookieSize(const CXXNewExpr *expr);
115 llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
117 llvm::Value *NumElements,
118 const CXXNewExpr *expr,
119 QualType ElementType);
120 void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr,
121 const CXXDeleteExpr *expr,
122 QualType ElementType, llvm::Value *&NumElements,
123 llvm::Value *&AllocPtr, CharUnits &CookieSize);
125 void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
126 llvm::GlobalVariable *DeclPtr, bool PerformInit);
129 class ARMCXXABI : public ItaniumCXXABI {
131 ARMCXXABI(CodeGen::CodeGenModule &CGM) : ItaniumCXXABI(CGM, /*ARM*/ true) {}
133 void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
136 SmallVectorImpl<CanQualType> &ArgTys);
138 void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
141 SmallVectorImpl<CanQualType> &ArgTys);
143 void BuildInstanceFunctionParams(CodeGenFunction &CGF,
145 FunctionArgList &Params);
147 void EmitInstanceFunctionProlog(CodeGenFunction &CGF);
149 void EmitReturnFromThunk(CodeGenFunction &CGF, RValue RV, QualType ResTy);
151 CharUnits GetArrayCookieSize(const CXXNewExpr *expr);
152 llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
154 llvm::Value *NumElements,
155 const CXXNewExpr *expr,
156 QualType ElementType);
157 void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr,
158 const CXXDeleteExpr *expr,
159 QualType ElementType, llvm::Value *&NumElements,
160 llvm::Value *&AllocPtr, CharUnits &CookieSize);
163 /// \brief Returns true if the given instance method is one of the
164 /// kinds that the ARM ABI says returns 'this'.
165 static bool HasThisReturn(GlobalDecl GD) {
166 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
167 return ((isa<CXXDestructorDecl>(MD) && GD.getDtorType() != Dtor_Deleting) ||
168 (isa<CXXConstructorDecl>(MD)));
173 CodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) {
174 return new ItaniumCXXABI(CGM);
177 CodeGen::CGCXXABI *CodeGen::CreateARMCXXABI(CodeGenModule &CGM) {
178 return new ARMCXXABI(CGM);
182 ItaniumCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) {
183 if (MPT->isMemberDataPointer())
184 return getPtrDiffTy();
185 return llvm::StructType::get(getPtrDiffTy(), getPtrDiffTy(), NULL);
188 /// In the Itanium and ARM ABIs, method pointers have the form:
189 /// struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr;
191 /// In the Itanium ABI:
192 /// - method pointers are virtual if (memptr.ptr & 1) is nonzero
193 /// - the this-adjustment is (memptr.adj)
194 /// - the virtual offset is (memptr.ptr - 1)
197 /// - method pointers are virtual if (memptr.adj & 1) is nonzero
198 /// - the this-adjustment is (memptr.adj >> 1)
199 /// - the virtual offset is (memptr.ptr)
200 /// ARM uses 'adj' for the virtual flag because Thumb functions
201 /// may be only single-byte aligned.
203 /// If the member is virtual, the adjusted 'this' pointer points
204 /// to a vtable pointer from which the virtual offset is applied.
206 /// If the member is non-virtual, memptr.ptr is the address of
207 /// the function to call.
209 ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
211 llvm::Value *MemFnPtr,
212 const MemberPointerType *MPT) {
213 CGBuilderTy &Builder = CGF.Builder;
215 const FunctionProtoType *FPT =
216 MPT->getPointeeType()->getAs<FunctionProtoType>();
217 const CXXRecordDecl *RD =
218 cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
220 llvm::FunctionType *FTy =
221 CGM.getTypes().GetFunctionType(
222 CGM.getTypes().arrangeCXXMethodType(RD, FPT));
224 llvm::IntegerType *ptrdiff = getPtrDiffTy();
225 llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(ptrdiff, 1);
227 llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual");
228 llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual");
229 llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end");
231 // Extract memptr.adj, which is in the second field.
232 llvm::Value *RawAdj = Builder.CreateExtractValue(MemFnPtr, 1, "memptr.adj");
234 // Compute the true adjustment.
235 llvm::Value *Adj = RawAdj;
237 Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted");
239 // Apply the adjustment and cast back to the original struct type
241 llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy());
242 Ptr = Builder.CreateInBoundsGEP(Ptr, Adj);
243 This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted");
245 // Load the function pointer.
246 llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr");
248 // If the LSB in the function pointer is 1, the function pointer points to
249 // a virtual function.
250 llvm::Value *IsVirtual;
252 IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1);
254 IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1);
255 IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual");
256 Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
258 // In the virtual path, the adjustment left 'This' pointing to the
259 // vtable of the correct base subobject. The "function pointer" is an
260 // offset within the vtable (+1 for the virtual flag on non-ARM).
261 CGF.EmitBlock(FnVirtual);
263 // Cast the adjusted this to a pointer to vtable pointer and load.
264 llvm::Type *VTableTy = Builder.getInt8PtrTy();
265 llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy->getPointerTo());
266 VTable = Builder.CreateLoad(VTable, "memptr.vtable");
269 llvm::Value *VTableOffset = FnAsInt;
270 if (!IsARM) VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1);
271 VTable = Builder.CreateGEP(VTable, VTableOffset);
273 // Load the virtual function to call.
274 VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo());
275 llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "memptr.virtualfn");
276 CGF.EmitBranch(FnEnd);
278 // In the non-virtual path, the function pointer is actually a
280 CGF.EmitBlock(FnNonVirtual);
281 llvm::Value *NonVirtualFn =
282 Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn");
285 CGF.EmitBlock(FnEnd);
286 llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo(), 2);
287 Callee->addIncoming(VirtualFn, FnVirtual);
288 Callee->addIncoming(NonVirtualFn, FnNonVirtual);
292 /// Compute an l-value by applying the given pointer-to-member to a
294 llvm::Value *ItaniumCXXABI::EmitMemberDataPointerAddress(CodeGenFunction &CGF,
297 const MemberPointerType *MPT) {
298 assert(MemPtr->getType() == getPtrDiffTy());
300 CGBuilderTy &Builder = CGF.Builder;
302 unsigned AS = cast<llvm::PointerType>(Base->getType())->getAddressSpace();
305 Base = Builder.CreateBitCast(Base, Builder.getInt8Ty()->getPointerTo(AS));
307 // Apply the offset, which we assume is non-null.
308 llvm::Value *Addr = Builder.CreateInBoundsGEP(Base, MemPtr, "memptr.offset");
310 // Cast the address to the appropriate pointer type, adopting the
311 // address space of the base pointer.
313 = CGF.ConvertTypeForMem(MPT->getPointeeType())->getPointerTo(AS);
314 return Builder.CreateBitCast(Addr, PType);
317 /// Perform a bitcast, derived-to-base, or base-to-derived member pointer
320 /// Bitcast conversions are always a no-op under Itanium.
322 /// Obligatory offset/adjustment diagram:
323 /// <-- offset --> <-- adjustment -->
324 /// |--------------------------|----------------------|--------------------|
325 /// ^Derived address point ^Base address point ^Member address point
327 /// So when converting a base member pointer to a derived member pointer,
328 /// we add the offset to the adjustment because the address point has
329 /// decreased; and conversely, when converting a derived MP to a base MP
330 /// we subtract the offset from the adjustment because the address point
333 /// The standard forbids (at compile time) conversion to and from
334 /// virtual bases, which is why we don't have to consider them here.
336 /// The standard forbids (at run time) casting a derived MP to a base
337 /// MP when the derived MP does not point to a member of the base.
338 /// This is why -1 is a reasonable choice for null data member
341 ItaniumCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF,
344 assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
345 E->getCastKind() == CK_BaseToDerivedMemberPointer ||
346 E->getCastKind() == CK_ReinterpretMemberPointer);
348 // Under Itanium, reinterprets don't require any additional processing.
349 if (E->getCastKind() == CK_ReinterpretMemberPointer) return src;
351 // Use constant emission if we can.
352 if (isa<llvm::Constant>(src))
353 return EmitMemberPointerConversion(E, cast<llvm::Constant>(src));
355 llvm::Constant *adj = getMemberPointerAdjustment(E);
356 if (!adj) return src;
358 CGBuilderTy &Builder = CGF.Builder;
359 bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
361 const MemberPointerType *destTy =
362 E->getType()->castAs<MemberPointerType>();
364 // For member data pointers, this is just a matter of adding the
365 // offset if the source is non-null.
366 if (destTy->isMemberDataPointer()) {
369 dst = Builder.CreateNSWSub(src, adj, "adj");
371 dst = Builder.CreateNSWAdd(src, adj, "adj");
374 llvm::Value *null = llvm::Constant::getAllOnesValue(src->getType());
375 llvm::Value *isNull = Builder.CreateICmpEQ(src, null, "memptr.isnull");
376 return Builder.CreateSelect(isNull, src, dst);
379 // The this-adjustment is left-shifted by 1 on ARM.
381 uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue();
383 adj = llvm::ConstantInt::get(adj->getType(), offset);
386 llvm::Value *srcAdj = Builder.CreateExtractValue(src, 1, "src.adj");
389 dstAdj = Builder.CreateNSWSub(srcAdj, adj, "adj");
391 dstAdj = Builder.CreateNSWAdd(srcAdj, adj, "adj");
393 return Builder.CreateInsertValue(src, dstAdj, 1);
397 ItaniumCXXABI::EmitMemberPointerConversion(const CastExpr *E,
398 llvm::Constant *src) {
399 assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
400 E->getCastKind() == CK_BaseToDerivedMemberPointer ||
401 E->getCastKind() == CK_ReinterpretMemberPointer);
403 // Under Itanium, reinterprets don't require any additional processing.
404 if (E->getCastKind() == CK_ReinterpretMemberPointer) return src;
406 // If the adjustment is trivial, we don't need to do anything.
407 llvm::Constant *adj = getMemberPointerAdjustment(E);
408 if (!adj) return src;
410 bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
412 const MemberPointerType *destTy =
413 E->getType()->castAs<MemberPointerType>();
415 // For member data pointers, this is just a matter of adding the
416 // offset if the source is non-null.
417 if (destTy->isMemberDataPointer()) {
418 // null maps to null.
419 if (src->isAllOnesValue()) return src;
422 return llvm::ConstantExpr::getNSWSub(src, adj);
424 return llvm::ConstantExpr::getNSWAdd(src, adj);
427 // The this-adjustment is left-shifted by 1 on ARM.
429 uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue();
431 adj = llvm::ConstantInt::get(adj->getType(), offset);
434 llvm::Constant *srcAdj = llvm::ConstantExpr::getExtractValue(src, 1);
435 llvm::Constant *dstAdj;
437 dstAdj = llvm::ConstantExpr::getNSWSub(srcAdj, adj);
439 dstAdj = llvm::ConstantExpr::getNSWAdd(srcAdj, adj);
441 return llvm::ConstantExpr::getInsertValue(src, dstAdj, 1);
445 ItaniumCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) {
446 llvm::Type *ptrdiff_t = getPtrDiffTy();
448 // Itanium C++ ABI 2.3:
449 // A NULL pointer is represented as -1.
450 if (MPT->isMemberDataPointer())
451 return llvm::ConstantInt::get(ptrdiff_t, -1ULL, /*isSigned=*/true);
453 llvm::Constant *Zero = llvm::ConstantInt::get(ptrdiff_t, 0);
454 llvm::Constant *Values[2] = { Zero, Zero };
455 return llvm::ConstantStruct::getAnon(Values);
459 ItaniumCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT,
461 // Itanium C++ ABI 2.3:
462 // A pointer to data member is an offset from the base address of
463 // the class object containing it, represented as a ptrdiff_t
464 return llvm::ConstantInt::get(getPtrDiffTy(), offset.getQuantity());
467 llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) {
468 return BuildMemberPointer(MD, CharUnits::Zero());
471 llvm::Constant *ItaniumCXXABI::BuildMemberPointer(const CXXMethodDecl *MD,
472 CharUnits ThisAdjustment) {
473 assert(MD->isInstance() && "Member function must not be static!");
474 MD = MD->getCanonicalDecl();
476 CodeGenTypes &Types = CGM.getTypes();
477 llvm::Type *ptrdiff_t = getPtrDiffTy();
479 // Get the function pointer (or index if this is a virtual function).
480 llvm::Constant *MemPtr[2];
481 if (MD->isVirtual()) {
482 uint64_t Index = CGM.getVTableContext().getMethodVTableIndex(MD);
484 const ASTContext &Context = getContext();
485 CharUnits PointerWidth =
486 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
487 uint64_t VTableOffset = (Index * PointerWidth.getQuantity());
490 // ARM C++ ABI 3.2.1:
491 // This ABI specifies that adj contains twice the this
492 // adjustment, plus 1 if the member function is virtual. The
493 // least significant bit of adj then makes exactly the same
494 // discrimination as the least significant bit of ptr does for
496 MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset);
497 MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t,
498 2 * ThisAdjustment.getQuantity() + 1);
500 // Itanium C++ ABI 2.3:
501 // For a virtual function, [the pointer field] is 1 plus the
502 // virtual table offset (in bytes) of the function,
503 // represented as a ptrdiff_t.
504 MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset + 1);
505 MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t,
506 ThisAdjustment.getQuantity());
509 const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
511 // Check whether the function has a computable LLVM signature.
512 if (Types.isFuncTypeConvertible(FPT)) {
513 // The function has a computable LLVM signature; use the correct type.
514 Ty = Types.GetFunctionType(Types.arrangeCXXMethodDeclaration(MD));
516 // Use an arbitrary non-function type to tell GetAddrOfFunction that the
517 // function type is incomplete.
520 llvm::Constant *addr = CGM.GetAddrOfFunction(MD, Ty);
522 MemPtr[0] = llvm::ConstantExpr::getPtrToInt(addr, ptrdiff_t);
523 MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, (IsARM ? 2 : 1) *
524 ThisAdjustment.getQuantity());
527 return llvm::ConstantStruct::getAnon(MemPtr);
530 llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const APValue &MP,
532 const MemberPointerType *MPT = MPType->castAs<MemberPointerType>();
533 const ValueDecl *MPD = MP.getMemberPointerDecl();
535 return EmitNullMemberPointer(MPT);
537 // Compute the this-adjustment.
538 CharUnits ThisAdjustment = CharUnits::Zero();
539 ArrayRef<const CXXRecordDecl*> Path = MP.getMemberPointerPath();
540 bool DerivedMember = MP.isMemberPointerToDerivedMember();
541 const CXXRecordDecl *RD = cast<CXXRecordDecl>(MPD->getDeclContext());
542 for (unsigned I = 0, N = Path.size(); I != N; ++I) {
543 const CXXRecordDecl *Base = RD;
544 const CXXRecordDecl *Derived = Path[I];
546 std::swap(Base, Derived);
548 getContext().getASTRecordLayout(Derived).getBaseClassOffset(Base);
552 ThisAdjustment = -ThisAdjustment;
554 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MPD))
555 return BuildMemberPointer(MD, ThisAdjustment);
557 CharUnits FieldOffset =
558 getContext().toCharUnitsFromBits(getContext().getFieldOffset(MPD));
559 return EmitMemberDataPointer(MPT, ThisAdjustment + FieldOffset);
562 /// The comparison algorithm is pretty easy: the member pointers are
563 /// the same if they're either bitwise identical *or* both null.
565 /// ARM is different here only because null-ness is more complicated.
567 ItaniumCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF,
570 const MemberPointerType *MPT,
572 CGBuilderTy &Builder = CGF.Builder;
574 llvm::ICmpInst::Predicate Eq;
575 llvm::Instruction::BinaryOps And, Or;
577 Eq = llvm::ICmpInst::ICMP_NE;
578 And = llvm::Instruction::Or;
579 Or = llvm::Instruction::And;
581 Eq = llvm::ICmpInst::ICMP_EQ;
582 And = llvm::Instruction::And;
583 Or = llvm::Instruction::Or;
586 // Member data pointers are easy because there's a unique null
587 // value, so it just comes down to bitwise equality.
588 if (MPT->isMemberDataPointer())
589 return Builder.CreateICmp(Eq, L, R);
591 // For member function pointers, the tautologies are more complex.
592 // The Itanium tautology is:
593 // (L == R) <==> (L.ptr == R.ptr && (L.ptr == 0 || L.adj == R.adj))
594 // The ARM tautology is:
595 // (L == R) <==> (L.ptr == R.ptr &&
596 // (L.adj == R.adj ||
597 // (L.ptr == 0 && ((L.adj|R.adj) & 1) == 0)))
598 // The inequality tautologies have exactly the same structure, except
599 // applying De Morgan's laws.
601 llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr");
602 llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr");
604 // This condition tests whether L.ptr == R.ptr. This must always be
605 // true for equality to hold.
606 llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr");
608 // This condition, together with the assumption that L.ptr == R.ptr,
609 // tests whether the pointers are both null. ARM imposes an extra
611 llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType());
612 llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null");
614 // This condition tests whether L.adj == R.adj. If this isn't
615 // true, the pointers are unequal unless they're both null.
616 llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj");
617 llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj");
618 llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj");
620 // Null member function pointers on ARM clear the low bit of Adj,
621 // so the zero condition has to check that neither low bit is set.
623 llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1);
625 // Compute (l.adj | r.adj) & 1 and test it against zero.
626 llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj");
627 llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One);
628 llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero,
630 EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero);
633 // Tie together all our conditions.
634 llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq);
635 Result = Builder.CreateBinOp(And, PtrEq, Result,
636 Inequality ? "memptr.ne" : "memptr.eq");
641 ItaniumCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
643 const MemberPointerType *MPT) {
644 CGBuilderTy &Builder = CGF.Builder;
646 /// For member data pointers, this is just a check against -1.
647 if (MPT->isMemberDataPointer()) {
648 assert(MemPtr->getType() == getPtrDiffTy());
649 llvm::Value *NegativeOne =
650 llvm::Constant::getAllOnesValue(MemPtr->getType());
651 return Builder.CreateICmpNE(MemPtr, NegativeOne, "memptr.tobool");
654 // In Itanium, a member function pointer is not null if 'ptr' is not null.
655 llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr");
657 llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0);
658 llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool");
660 // On ARM, a member function pointer is also non-null if the low bit of 'adj'
661 // (the virtual bit) is set.
663 llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1);
664 llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj");
665 llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit");
666 llvm::Value *IsVirtual = Builder.CreateICmpNE(VirtualBit, Zero,
668 Result = Builder.CreateOr(Result, IsVirtual);
674 /// The Itanium ABI requires non-zero initialization only for data
675 /// member pointers, for which '0' is a valid offset.
676 bool ItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
677 return MPT->getPointeeType()->isFunctionType();
680 /// The generic ABI passes 'this', plus a VTT if it's initializing a
682 void ItaniumCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor,
685 SmallVectorImpl<CanQualType> &ArgTys) {
686 ASTContext &Context = getContext();
688 // 'this' is already there.
690 // Check if we need to add a VTT parameter (which has type void **).
691 if (Type == Ctor_Base && Ctor->getParent()->getNumVBases() != 0)
692 ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
695 /// The ARM ABI does the same as the Itanium ABI, but returns 'this'.
696 void ARMCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor,
699 SmallVectorImpl<CanQualType> &ArgTys) {
700 ItaniumCXXABI::BuildConstructorSignature(Ctor, Type, ResTy, ArgTys);
704 /// The generic ABI passes 'this', plus a VTT if it's destroying a
706 void ItaniumCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor,
709 SmallVectorImpl<CanQualType> &ArgTys) {
710 ASTContext &Context = getContext();
712 // 'this' is already there.
714 // Check if we need to add a VTT parameter (which has type void **).
715 if (Type == Dtor_Base && Dtor->getParent()->getNumVBases() != 0)
716 ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
719 /// The ARM ABI does the same as the Itanium ABI, but returns 'this'
720 /// for non-deleting destructors.
721 void ARMCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor,
724 SmallVectorImpl<CanQualType> &ArgTys) {
725 ItaniumCXXABI::BuildDestructorSignature(Dtor, Type, ResTy, ArgTys);
727 if (Type != Dtor_Deleting)
731 void ItaniumCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF,
733 FunctionArgList &Params) {
734 /// Create the 'this' variable.
735 BuildThisParam(CGF, Params);
737 const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl());
738 assert(MD->isInstance());
740 // Check if we need a VTT parameter as well.
741 if (CodeGenVTables::needsVTTParameter(CGF.CurGD)) {
742 ASTContext &Context = getContext();
744 // FIXME: avoid the fake decl
745 QualType T = Context.getPointerType(Context.VoidPtrTy);
746 ImplicitParamDecl *VTTDecl
747 = ImplicitParamDecl::Create(Context, 0, MD->getLocation(),
748 &Context.Idents.get("vtt"), T);
749 Params.push_back(VTTDecl);
750 getVTTDecl(CGF) = VTTDecl;
754 void ARMCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF,
756 FunctionArgList &Params) {
757 ItaniumCXXABI::BuildInstanceFunctionParams(CGF, ResTy, Params);
759 // Return 'this' from certain constructors and destructors.
760 if (HasThisReturn(CGF.CurGD))
761 ResTy = Params[0]->getType();
764 void ItaniumCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
765 /// Initialize the 'this' slot.
768 /// Initialize the 'vtt' slot if needed.
769 if (getVTTDecl(CGF)) {
771 = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(getVTTDecl(CGF)),
776 void ARMCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
777 ItaniumCXXABI::EmitInstanceFunctionProlog(CGF);
779 /// Initialize the return slot to 'this' at the start of the
781 if (HasThisReturn(CGF.CurGD))
782 CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue);
785 void ARMCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF,
786 RValue RV, QualType ResultType) {
787 if (!isa<CXXDestructorDecl>(CGF.CurGD.getDecl()))
788 return ItaniumCXXABI::EmitReturnFromThunk(CGF, RV, ResultType);
790 // Destructor thunks in the ARM ABI have indeterminate results.
792 cast<llvm::PointerType>(CGF.ReturnValue->getType())->getElementType();
793 RValue Undef = RValue::get(llvm::UndefValue::get(T));
794 return ItaniumCXXABI::EmitReturnFromThunk(CGF, Undef, ResultType);
797 /************************** Array allocation cookies **************************/
799 bool ItaniumCXXABI::NeedsArrayCookie(const CXXNewExpr *expr) {
800 // If the class's usual deallocation function takes two arguments,
801 // it needs a cookie.
802 if (expr->doesUsualArrayDeleteWantSize())
805 // Automatic Reference Counting:
806 // We need an array cookie for pointers with strong or weak lifetime.
807 QualType AllocatedType = expr->getAllocatedType();
808 if (getContext().getLangOpts().ObjCAutoRefCount &&
809 AllocatedType->isObjCLifetimeType()) {
810 switch (AllocatedType.getObjCLifetime()) {
811 case Qualifiers::OCL_None:
812 case Qualifiers::OCL_ExplicitNone:
813 case Qualifiers::OCL_Autoreleasing:
816 case Qualifiers::OCL_Strong:
817 case Qualifiers::OCL_Weak:
822 // Otherwise, if the class has a non-trivial destructor, it always
824 const CXXRecordDecl *record =
825 AllocatedType->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
826 return (record && !record->hasTrivialDestructor());
829 bool ItaniumCXXABI::NeedsArrayCookie(const CXXDeleteExpr *expr,
830 QualType elementType) {
831 // If the class's usual deallocation function takes two arguments,
832 // it needs a cookie.
833 if (expr->doesUsualArrayDeleteWantSize())
836 return elementType.isDestructedType();
839 CharUnits ItaniumCXXABI::GetArrayCookieSize(const CXXNewExpr *expr) {
840 if (!NeedsArrayCookie(expr))
841 return CharUnits::Zero();
843 // Padding is the maximum of sizeof(size_t) and alignof(elementType)
844 ASTContext &Ctx = getContext();
845 return std::max(Ctx.getTypeSizeInChars(Ctx.getSizeType()),
846 Ctx.getTypeAlignInChars(expr->getAllocatedType()));
849 llvm::Value *ItaniumCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
851 llvm::Value *NumElements,
852 const CXXNewExpr *expr,
853 QualType ElementType) {
854 assert(NeedsArrayCookie(expr));
856 unsigned AS = cast<llvm::PointerType>(NewPtr->getType())->getAddressSpace();
858 ASTContext &Ctx = getContext();
859 QualType SizeTy = Ctx.getSizeType();
860 CharUnits SizeSize = Ctx.getTypeSizeInChars(SizeTy);
862 // The size of the cookie.
863 CharUnits CookieSize =
864 std::max(SizeSize, Ctx.getTypeAlignInChars(ElementType));
866 // Compute an offset to the cookie.
867 llvm::Value *CookiePtr = NewPtr;
868 CharUnits CookieOffset = CookieSize - SizeSize;
869 if (!CookieOffset.isZero())
870 CookiePtr = CGF.Builder.CreateConstInBoundsGEP1_64(CookiePtr,
871 CookieOffset.getQuantity());
873 // Write the number of elements into the appropriate slot.
874 llvm::Value *NumElementsPtr
875 = CGF.Builder.CreateBitCast(CookiePtr,
876 CGF.ConvertType(SizeTy)->getPointerTo(AS));
877 CGF.Builder.CreateStore(NumElements, NumElementsPtr);
879 // Finally, compute a pointer to the actual data buffer by skipping
880 // over the cookie completely.
881 return CGF.Builder.CreateConstInBoundsGEP1_64(NewPtr,
882 CookieSize.getQuantity());
885 void ItaniumCXXABI::ReadArrayCookie(CodeGenFunction &CGF,
887 const CXXDeleteExpr *expr,
888 QualType ElementType,
889 llvm::Value *&NumElements,
890 llvm::Value *&AllocPtr,
891 CharUnits &CookieSize) {
892 // Derive a char* in the same address space as the pointer.
893 unsigned AS = cast<llvm::PointerType>(Ptr->getType())->getAddressSpace();
894 llvm::Type *CharPtrTy = CGF.Builder.getInt8Ty()->getPointerTo(AS);
896 // If we don't need an array cookie, bail out early.
897 if (!NeedsArrayCookie(expr, ElementType)) {
898 AllocPtr = CGF.Builder.CreateBitCast(Ptr, CharPtrTy);
900 CookieSize = CharUnits::Zero();
904 QualType SizeTy = getContext().getSizeType();
905 CharUnits SizeSize = getContext().getTypeSizeInChars(SizeTy);
906 llvm::Type *SizeLTy = CGF.ConvertType(SizeTy);
909 = std::max(SizeSize, getContext().getTypeAlignInChars(ElementType));
911 CharUnits NumElementsOffset = CookieSize - SizeSize;
913 // Compute the allocated pointer.
914 AllocPtr = CGF.Builder.CreateBitCast(Ptr, CharPtrTy);
915 AllocPtr = CGF.Builder.CreateConstInBoundsGEP1_64(AllocPtr,
916 -CookieSize.getQuantity());
918 llvm::Value *NumElementsPtr = AllocPtr;
919 if (!NumElementsOffset.isZero())
921 CGF.Builder.CreateConstInBoundsGEP1_64(NumElementsPtr,
922 NumElementsOffset.getQuantity());
924 CGF.Builder.CreateBitCast(NumElementsPtr, SizeLTy->getPointerTo(AS));
925 NumElements = CGF.Builder.CreateLoad(NumElementsPtr);
928 CharUnits ARMCXXABI::GetArrayCookieSize(const CXXNewExpr *expr) {
929 if (!NeedsArrayCookie(expr))
930 return CharUnits::Zero();
932 // On ARM, the cookie is always:
933 // struct array_cookie {
934 // std::size_t element_size; // element_size != 0
935 // std::size_t element_count;
937 // TODO: what should we do if the allocated type actually wants
938 // greater alignment?
939 return getContext().getTypeSizeInChars(getContext().getSizeType()) * 2;
942 llvm::Value *ARMCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
944 llvm::Value *NumElements,
945 const CXXNewExpr *expr,
946 QualType ElementType) {
947 assert(NeedsArrayCookie(expr));
949 // NewPtr is a char*.
951 unsigned AS = cast<llvm::PointerType>(NewPtr->getType())->getAddressSpace();
953 ASTContext &Ctx = getContext();
954 CharUnits SizeSize = Ctx.getTypeSizeInChars(Ctx.getSizeType());
955 llvm::IntegerType *SizeTy =
956 cast<llvm::IntegerType>(CGF.ConvertType(Ctx.getSizeType()));
958 // The cookie is always at the start of the buffer.
959 llvm::Value *CookiePtr = NewPtr;
961 // The first element is the element size.
962 CookiePtr = CGF.Builder.CreateBitCast(CookiePtr, SizeTy->getPointerTo(AS));
963 llvm::Value *ElementSize = llvm::ConstantInt::get(SizeTy,
964 Ctx.getTypeSizeInChars(ElementType).getQuantity());
965 CGF.Builder.CreateStore(ElementSize, CookiePtr);
967 // The second element is the element count.
968 CookiePtr = CGF.Builder.CreateConstInBoundsGEP1_32(CookiePtr, 1);
969 CGF.Builder.CreateStore(NumElements, CookiePtr);
971 // Finally, compute a pointer to the actual data buffer by skipping
972 // over the cookie completely.
973 CharUnits CookieSize = 2 * SizeSize;
974 return CGF.Builder.CreateConstInBoundsGEP1_64(NewPtr,
975 CookieSize.getQuantity());
978 void ARMCXXABI::ReadArrayCookie(CodeGenFunction &CGF,
980 const CXXDeleteExpr *expr,
981 QualType ElementType,
982 llvm::Value *&NumElements,
983 llvm::Value *&AllocPtr,
984 CharUnits &CookieSize) {
985 // Derive a char* in the same address space as the pointer.
986 unsigned AS = cast<llvm::PointerType>(Ptr->getType())->getAddressSpace();
987 llvm::Type *CharPtrTy = CGF.Builder.getInt8Ty()->getPointerTo(AS);
989 // If we don't need an array cookie, bail out early.
990 if (!NeedsArrayCookie(expr, ElementType)) {
991 AllocPtr = CGF.Builder.CreateBitCast(Ptr, CharPtrTy);
993 CookieSize = CharUnits::Zero();
997 QualType SizeTy = getContext().getSizeType();
998 CharUnits SizeSize = getContext().getTypeSizeInChars(SizeTy);
999 llvm::Type *SizeLTy = CGF.ConvertType(SizeTy);
1001 // The cookie size is always 2 * sizeof(size_t).
1002 CookieSize = 2 * SizeSize;
1004 // The allocated pointer is the input ptr, minus that amount.
1005 AllocPtr = CGF.Builder.CreateBitCast(Ptr, CharPtrTy);
1006 AllocPtr = CGF.Builder.CreateConstInBoundsGEP1_64(AllocPtr,
1007 -CookieSize.getQuantity());
1009 // The number of elements is at offset sizeof(size_t) relative to that.
1010 llvm::Value *NumElementsPtr
1011 = CGF.Builder.CreateConstInBoundsGEP1_64(AllocPtr,
1012 SizeSize.getQuantity());
1014 CGF.Builder.CreateBitCast(NumElementsPtr, SizeLTy->getPointerTo(AS));
1015 NumElements = CGF.Builder.CreateLoad(NumElementsPtr);
1018 /*********************** Static local initialization **************************/
1020 static llvm::Constant *getGuardAcquireFn(CodeGenModule &CGM,
1021 llvm::PointerType *GuardPtrTy) {
1022 // int __cxa_guard_acquire(__guard *guard_object);
1023 llvm::FunctionType *FTy =
1024 llvm::FunctionType::get(CGM.getTypes().ConvertType(CGM.getContext().IntTy),
1025 GuardPtrTy, /*isVarArg=*/false);
1027 return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_acquire",
1028 llvm::Attribute::NoUnwind);
1031 static llvm::Constant *getGuardReleaseFn(CodeGenModule &CGM,
1032 llvm::PointerType *GuardPtrTy) {
1033 // void __cxa_guard_release(__guard *guard_object);
1034 llvm::FunctionType *FTy =
1035 llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false);
1037 return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_release",
1038 llvm::Attribute::NoUnwind);
1041 static llvm::Constant *getGuardAbortFn(CodeGenModule &CGM,
1042 llvm::PointerType *GuardPtrTy) {
1043 // void __cxa_guard_abort(__guard *guard_object);
1044 llvm::FunctionType *FTy =
1045 llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false);
1047 return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_abort",
1048 llvm::Attribute::NoUnwind);
1052 struct CallGuardAbort : EHScopeStack::Cleanup {
1053 llvm::GlobalVariable *Guard;
1054 CallGuardAbort(llvm::GlobalVariable *Guard) : Guard(Guard) {}
1056 void Emit(CodeGenFunction &CGF, Flags flags) {
1057 CGF.Builder.CreateCall(getGuardAbortFn(CGF.CGM, Guard->getType()), Guard)
1058 ->setDoesNotThrow();
1063 /// The ARM code here follows the Itanium code closely enough that we
1064 /// just special-case it at particular places.
1065 void ItaniumCXXABI::EmitGuardedInit(CodeGenFunction &CGF,
1067 llvm::GlobalVariable *var,
1068 bool shouldPerformInit) {
1069 CGBuilderTy &Builder = CGF.Builder;
1071 // We only need to use thread-safe statics for local variables;
1072 // global initialization is always single-threaded.
1074 (getContext().getLangOpts().ThreadsafeStatics && D.isLocalVarDecl());
1076 // If we have a global variable with internal linkage and thread-safe statics
1077 // are disabled, we can just let the guard variable be of type i8.
1078 bool useInt8GuardVariable = !threadsafe && var->hasInternalLinkage();
1080 llvm::IntegerType *guardTy;
1081 if (useInt8GuardVariable) {
1082 guardTy = CGF.Int8Ty;
1084 // Guard variables are 64 bits in the generic ABI and 32 bits on ARM.
1085 guardTy = (IsARM ? CGF.Int32Ty : CGF.Int64Ty);
1087 llvm::PointerType *guardPtrTy = guardTy->getPointerTo();
1089 // Create the guard variable if we don't already have it (as we
1090 // might if we're double-emitting this function body).
1091 llvm::GlobalVariable *guard = CGM.getStaticLocalDeclGuardAddress(&D);
1093 // Mangle the name for the guard.
1094 SmallString<256> guardName;
1096 llvm::raw_svector_ostream out(guardName);
1097 getMangleContext().mangleItaniumGuardVariable(&D, out);
1101 // Create the guard variable with a zero-initializer.
1102 // Just absorb linkage and visibility from the guarded variable.
1103 guard = new llvm::GlobalVariable(CGM.getModule(), guardTy,
1104 false, var->getLinkage(),
1105 llvm::ConstantInt::get(guardTy, 0),
1107 guard->setVisibility(var->getVisibility());
1109 CGM.setStaticLocalDeclGuardAddress(&D, guard);
1112 // Test whether the variable has completed initialization.
1113 llvm::Value *isInitialized;
1115 // ARM C++ ABI 3.2.3.1:
1116 // To support the potential use of initialization guard variables
1117 // as semaphores that are the target of ARM SWP and LDREX/STREX
1118 // synchronizing instructions we define a static initialization
1119 // guard variable to be a 4-byte aligned, 4- byte word with the
1120 // following inline access protocol.
1121 // #define INITIALIZED 1
1122 // if ((obj_guard & INITIALIZED) != INITIALIZED) {
1123 // if (__cxa_guard_acquire(&obj_guard))
1126 if (IsARM && !useInt8GuardVariable) {
1127 llvm::Value *V = Builder.CreateLoad(guard);
1128 V = Builder.CreateAnd(V, Builder.getInt32(1));
1129 isInitialized = Builder.CreateIsNull(V, "guard.uninitialized");
1131 // Itanium C++ ABI 3.3.2:
1132 // The following is pseudo-code showing how these functions can be used:
1133 // if (obj_guard.first_byte == 0) {
1134 // if ( __cxa_guard_acquire (&obj_guard) ) {
1136 // ... initialize the object ...;
1138 // __cxa_guard_abort (&obj_guard);
1141 // ... queue object destructor with __cxa_atexit() ...;
1142 // __cxa_guard_release (&obj_guard);
1146 // Load the first byte of the guard variable.
1147 llvm::LoadInst *LI =
1148 Builder.CreateLoad(Builder.CreateBitCast(guard, CGM.Int8PtrTy));
1149 LI->setAlignment(1);
1152 // An implementation supporting thread-safety on multiprocessor
1153 // systems must also guarantee that references to the initialized
1154 // object do not occur before the load of the initialization flag.
1156 // In LLVM, we do this by marking the load Acquire.
1158 LI->setAtomic(llvm::Acquire);
1160 isInitialized = Builder.CreateIsNull(LI, "guard.uninitialized");
1163 llvm::BasicBlock *InitCheckBlock = CGF.createBasicBlock("init.check");
1164 llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end");
1166 // Check if the first byte of the guard variable is zero.
1167 Builder.CreateCondBr(isInitialized, InitCheckBlock, EndBlock);
1169 CGF.EmitBlock(InitCheckBlock);
1171 // Variables used when coping with thread-safe statics and exceptions.
1173 // Call __cxa_guard_acquire.
1175 = Builder.CreateCall(getGuardAcquireFn(CGM, guardPtrTy), guard);
1177 llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init");
1179 Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"),
1180 InitBlock, EndBlock);
1182 // Call __cxa_guard_abort along the exceptional edge.
1183 CGF.EHStack.pushCleanup<CallGuardAbort>(EHCleanup, guard);
1185 CGF.EmitBlock(InitBlock);
1188 // Emit the initializer and add a global destructor if appropriate.
1189 CGF.EmitCXXGlobalVarDeclInit(D, var, shouldPerformInit);
1192 // Pop the guard-abort cleanup if we pushed one.
1193 CGF.PopCleanupBlock();
1195 // Call __cxa_guard_release. This cannot throw.
1196 Builder.CreateCall(getGuardReleaseFn(CGM, guardPtrTy), guard);
1198 Builder.CreateStore(llvm::ConstantInt::get(guardTy, 1), guard);
1201 CGF.EmitBlock(EndBlock);