1 //===------- 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 //
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
12 // documented at:
13 //  http://www.codesourcery.com/public/cxx-abi/abi.html
14 //  http://www.codesourcery.com/public/cxx-abi/abi-eh.html
15 //
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 //
19 //===----------------------------------------------------------------------===//
20 
21 #include "CGCXXABI.h"
22 #include "CGRecordLayout.h"
23 #include "CGVTables.h"
24 #include "CodeGenFunction.h"
25 #include "CodeGenModule.h"
26 #include "clang/AST/Mangle.h"
27 #include "clang/AST/Type.h"
28 #include "llvm/IR/DataLayout.h"
29 #include "llvm/IR/Intrinsics.h"
30 #include "llvm/IR/Value.h"
31 
32 using namespace clang;
33 using namespace CodeGen;
34 
35 namespace {
36 class ItaniumCXXABI : public CodeGen::CGCXXABI {
37 private:
38   llvm::IntegerType *PtrDiffTy;
39 protected:
40   bool IsARM;
41 
42   // It's a little silly for us to cache this.
43   llvm::IntegerType *getPtrDiffTy() {
44     if (!PtrDiffTy) {
45       QualType T = getContext().getPointerDiffType();
46       llvm::Type *Ty = CGM.getTypes().ConvertType(T);
47       PtrDiffTy = cast<llvm::IntegerType>(Ty);
48     }
49     return PtrDiffTy;
50   }
51 
52 public:
53   ItaniumCXXABI(CodeGen::CodeGenModule &CGM, bool IsARM = false) :
54     CGCXXABI(CGM), PtrDiffTy(0), IsARM(IsARM) { }
55 
56   bool isZeroInitializable(const MemberPointerType *MPT);
57 
58   llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT);
59 
60   llvm::Value *EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
61                                                llvm::Value *&This,
62                                                llvm::Value *MemFnPtr,
63                                                const MemberPointerType *MPT);
64 
65   llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF,
66                                             llvm::Value *Base,
67                                             llvm::Value *MemPtr,
68                                             const MemberPointerType *MPT);
69 
70   llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
71                                            const CastExpr *E,
72                                            llvm::Value *Src);
73   llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
74                                               llvm::Constant *Src);
75 
76   llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
77 
78   llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
79   llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
80                                         CharUnits offset);
81   llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT);
82   llvm::Constant *BuildMemberPointer(const CXXMethodDecl *MD,
83                                      CharUnits ThisAdjustment);
84 
85   llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF,
86                                            llvm::Value *L,
87                                            llvm::Value *R,
88                                            const MemberPointerType *MPT,
89                                            bool Inequality);
90 
91   llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
92                                           llvm::Value *Addr,
93                                           const MemberPointerType *MPT);
94 
95   llvm::Value *adjustToCompleteObject(CodeGenFunction &CGF,
96                                       llvm::Value *ptr,
97                                       QualType type);
98 
99   void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
100                                  CXXCtorType T,
101                                  CanQualType &ResTy,
102                                  SmallVectorImpl<CanQualType> &ArgTys);
103 
104   void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
105                                 CXXDtorType T,
106                                 CanQualType &ResTy,
107                                 SmallVectorImpl<CanQualType> &ArgTys);
108 
109   void BuildInstanceFunctionParams(CodeGenFunction &CGF,
110                                    QualType &ResTy,
111                                    FunctionArgList &Params);
112 
113   void EmitInstanceFunctionProlog(CodeGenFunction &CGF);
114 
115   llvm::Value *EmitConstructorCall(CodeGenFunction &CGF,
116                            const CXXConstructorDecl *D,
117                            CXXCtorType Type, bool ForVirtualBase,
118                            bool Delegating,
119                            llvm::Value *This,
120                            CallExpr::const_arg_iterator ArgBeg,
121                            CallExpr::const_arg_iterator ArgEnd);
122 
123   RValue EmitVirtualDestructorCall(CodeGenFunction &CGF,
124                                    const CXXDestructorDecl *Dtor,
125                                    CXXDtorType DtorType,
126                                    SourceLocation CallLoc,
127                                    ReturnValueSlot ReturnValue,
128                                    llvm::Value *This);
129 
130   StringRef GetPureVirtualCallName() { return "__cxa_pure_virtual"; }
131   StringRef GetDeletedVirtualCallName() { return "__cxa_deleted_virtual"; }
132 
133   CharUnits getArrayCookieSizeImpl(QualType elementType);
134   llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
135                                      llvm::Value *NewPtr,
136                                      llvm::Value *NumElements,
137                                      const CXXNewExpr *expr,
138                                      QualType ElementType);
139   llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF,
140                                    llvm::Value *allocPtr,
141                                    CharUnits cookieSize);
142 
143   void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
144                        llvm::GlobalVariable *DeclPtr, bool PerformInit);
145   void registerGlobalDtor(CodeGenFunction &CGF, llvm::Constant *dtor,
146                           llvm::Constant *addr);
147 };
148 
149 class ARMCXXABI : public ItaniumCXXABI {
150 public:
151   ARMCXXABI(CodeGen::CodeGenModule &CGM) : ItaniumCXXABI(CGM, /*ARM*/ true) {}
152 
153   void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
154                                  CXXCtorType T,
155                                  CanQualType &ResTy,
156                                  SmallVectorImpl<CanQualType> &ArgTys);
157 
158   void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
159                                 CXXDtorType T,
160                                 CanQualType &ResTy,
161                                 SmallVectorImpl<CanQualType> &ArgTys);
162 
163   void BuildInstanceFunctionParams(CodeGenFunction &CGF,
164                                    QualType &ResTy,
165                                    FunctionArgList &Params);
166 
167   void EmitInstanceFunctionProlog(CodeGenFunction &CGF);
168 
169   void EmitReturnFromThunk(CodeGenFunction &CGF, RValue RV, QualType ResTy);
170 
171   CharUnits getArrayCookieSizeImpl(QualType elementType);
172   llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
173                                      llvm::Value *NewPtr,
174                                      llvm::Value *NumElements,
175                                      const CXXNewExpr *expr,
176                                      QualType ElementType);
177   llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, llvm::Value *allocPtr,
178                                    CharUnits cookieSize);
179 
180   /// \brief Returns true if the given instance method is one of the
181   /// kinds that the ARM ABI says returns 'this'.
182   bool HasThisReturn(GlobalDecl GD) const {
183     const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(GD.getDecl());
184     if (!MD) return false;
185     return ((isa<CXXDestructorDecl>(MD) && GD.getDtorType() != Dtor_Deleting) ||
186             (isa<CXXConstructorDecl>(MD)));
187   }
188 };
189 }
190 
191 CodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) {
192   switch (CGM.getContext().getTargetInfo().getCXXABI().getKind()) {
193   // For IR-generation purposes, there's no significant difference
194   // between the ARM and iOS ABIs.
195   case TargetCXXABI::GenericARM:
196   case TargetCXXABI::iOS:
197     return new ARMCXXABI(CGM);
198 
199   // Note that AArch64 uses the generic ItaniumCXXABI class since it doesn't
200   // include the other 32-bit ARM oddities: constructor/destructor return values
201   // and array cookies.
202   case TargetCXXABI::GenericAArch64:
203     return  new ItaniumCXXABI(CGM, /*IsARM = */ true);
204 
205   case TargetCXXABI::GenericItanium:
206     return new ItaniumCXXABI(CGM);
207 
208   case TargetCXXABI::Microsoft:
209     llvm_unreachable("Microsoft ABI is not Itanium-based");
210   }
211   llvm_unreachable("bad ABI kind");
212 }
213 
214 llvm::Type *
215 ItaniumCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) {
216   if (MPT->isMemberDataPointer())
217     return getPtrDiffTy();
218   return llvm::StructType::get(getPtrDiffTy(), getPtrDiffTy(), NULL);
219 }
220 
221 /// In the Itanium and ARM ABIs, method pointers have the form:
222 ///   struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr;
223 ///
224 /// In the Itanium ABI:
225 ///  - method pointers are virtual if (memptr.ptr & 1) is nonzero
226 ///  - the this-adjustment is (memptr.adj)
227 ///  - the virtual offset is (memptr.ptr - 1)
228 ///
229 /// In the ARM ABI:
230 ///  - method pointers are virtual if (memptr.adj & 1) is nonzero
231 ///  - the this-adjustment is (memptr.adj >> 1)
232 ///  - the virtual offset is (memptr.ptr)
233 /// ARM uses 'adj' for the virtual flag because Thumb functions
234 /// may be only single-byte aligned.
235 ///
236 /// If the member is virtual, the adjusted 'this' pointer points
237 /// to a vtable pointer from which the virtual offset is applied.
238 ///
239 /// If the member is non-virtual, memptr.ptr is the address of
240 /// the function to call.
241 llvm::Value *
242 ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
243                                                llvm::Value *&This,
244                                                llvm::Value *MemFnPtr,
245                                                const MemberPointerType *MPT) {
246   CGBuilderTy &Builder = CGF.Builder;
247 
248   const FunctionProtoType *FPT =
249     MPT->getPointeeType()->getAs<FunctionProtoType>();
250   const CXXRecordDecl *RD =
251     cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
252 
253   llvm::FunctionType *FTy =
254     CGM.getTypes().GetFunctionType(
255       CGM.getTypes().arrangeCXXMethodType(RD, FPT));
256 
257   llvm::IntegerType *ptrdiff = getPtrDiffTy();
258   llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(ptrdiff, 1);
259 
260   llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual");
261   llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual");
262   llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end");
263 
264   // Extract memptr.adj, which is in the second field.
265   llvm::Value *RawAdj = Builder.CreateExtractValue(MemFnPtr, 1, "memptr.adj");
266 
267   // Compute the true adjustment.
268   llvm::Value *Adj = RawAdj;
269   if (IsARM)
270     Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted");
271 
272   // Apply the adjustment and cast back to the original struct type
273   // for consistency.
274   llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy());
275   Ptr = Builder.CreateInBoundsGEP(Ptr, Adj);
276   This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted");
277 
278   // Load the function pointer.
279   llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr");
280 
281   // If the LSB in the function pointer is 1, the function pointer points to
282   // a virtual function.
283   llvm::Value *IsVirtual;
284   if (IsARM)
285     IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1);
286   else
287     IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1);
288   IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual");
289   Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
290 
291   // In the virtual path, the adjustment left 'This' pointing to the
292   // vtable of the correct base subobject.  The "function pointer" is an
293   // offset within the vtable (+1 for the virtual flag on non-ARM).
294   CGF.EmitBlock(FnVirtual);
295 
296   // Cast the adjusted this to a pointer to vtable pointer and load.
297   llvm::Type *VTableTy = Builder.getInt8PtrTy();
298   llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy->getPointerTo());
299   VTable = Builder.CreateLoad(VTable, "memptr.vtable");
300 
301   // Apply the offset.
302   llvm::Value *VTableOffset = FnAsInt;
303   if (!IsARM) VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1);
304   VTable = Builder.CreateGEP(VTable, VTableOffset);
305 
306   // Load the virtual function to call.
307   VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo());
308   llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "memptr.virtualfn");
309   CGF.EmitBranch(FnEnd);
310 
311   // In the non-virtual path, the function pointer is actually a
312   // function pointer.
313   CGF.EmitBlock(FnNonVirtual);
314   llvm::Value *NonVirtualFn =
315     Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn");
316 
317   // We're done.
318   CGF.EmitBlock(FnEnd);
319   llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo(), 2);
320   Callee->addIncoming(VirtualFn, FnVirtual);
321   Callee->addIncoming(NonVirtualFn, FnNonVirtual);
322   return Callee;
323 }
324 
325 /// Compute an l-value by applying the given pointer-to-member to a
326 /// base object.
327 llvm::Value *ItaniumCXXABI::EmitMemberDataPointerAddress(CodeGenFunction &CGF,
328                                                          llvm::Value *Base,
329                                                          llvm::Value *MemPtr,
330                                            const MemberPointerType *MPT) {
331   assert(MemPtr->getType() == getPtrDiffTy());
332 
333   CGBuilderTy &Builder = CGF.Builder;
334 
335   unsigned AS = Base->getType()->getPointerAddressSpace();
336 
337   // Cast to char*.
338   Base = Builder.CreateBitCast(Base, Builder.getInt8Ty()->getPointerTo(AS));
339 
340   // Apply the offset, which we assume is non-null.
341   llvm::Value *Addr = Builder.CreateInBoundsGEP(Base, MemPtr, "memptr.offset");
342 
343   // Cast the address to the appropriate pointer type, adopting the
344   // address space of the base pointer.
345   llvm::Type *PType
346     = CGF.ConvertTypeForMem(MPT->getPointeeType())->getPointerTo(AS);
347   return Builder.CreateBitCast(Addr, PType);
348 }
349 
350 /// Perform a bitcast, derived-to-base, or base-to-derived member pointer
351 /// conversion.
352 ///
353 /// Bitcast conversions are always a no-op under Itanium.
354 ///
355 /// Obligatory offset/adjustment diagram:
356 ///         <-- offset -->          <-- adjustment -->
357 ///   |--------------------------|----------------------|--------------------|
358 ///   ^Derived address point     ^Base address point    ^Member address point
359 ///
360 /// So when converting a base member pointer to a derived member pointer,
361 /// we add the offset to the adjustment because the address point has
362 /// decreased;  and conversely, when converting a derived MP to a base MP
363 /// we subtract the offset from the adjustment because the address point
364 /// has increased.
365 ///
366 /// The standard forbids (at compile time) conversion to and from
367 /// virtual bases, which is why we don't have to consider them here.
368 ///
369 /// The standard forbids (at run time) casting a derived MP to a base
370 /// MP when the derived MP does not point to a member of the base.
371 /// This is why -1 is a reasonable choice for null data member
372 /// pointers.
373 llvm::Value *
374 ItaniumCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF,
375                                            const CastExpr *E,
376                                            llvm::Value *src) {
377   assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
378          E->getCastKind() == CK_BaseToDerivedMemberPointer ||
379          E->getCastKind() == CK_ReinterpretMemberPointer);
380 
381   // Under Itanium, reinterprets don't require any additional processing.
382   if (E->getCastKind() == CK_ReinterpretMemberPointer) return src;
383 
384   // Use constant emission if we can.
385   if (isa<llvm::Constant>(src))
386     return EmitMemberPointerConversion(E, cast<llvm::Constant>(src));
387 
388   llvm::Constant *adj = getMemberPointerAdjustment(E);
389   if (!adj) return src;
390 
391   CGBuilderTy &Builder = CGF.Builder;
392   bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
393 
394   const MemberPointerType *destTy =
395     E->getType()->castAs<MemberPointerType>();
396 
397   // For member data pointers, this is just a matter of adding the
398   // offset if the source is non-null.
399   if (destTy->isMemberDataPointer()) {
400     llvm::Value *dst;
401     if (isDerivedToBase)
402       dst = Builder.CreateNSWSub(src, adj, "adj");
403     else
404       dst = Builder.CreateNSWAdd(src, adj, "adj");
405 
406     // Null check.
407     llvm::Value *null = llvm::Constant::getAllOnesValue(src->getType());
408     llvm::Value *isNull = Builder.CreateICmpEQ(src, null, "memptr.isnull");
409     return Builder.CreateSelect(isNull, src, dst);
410   }
411 
412   // The this-adjustment is left-shifted by 1 on ARM.
413   if (IsARM) {
414     uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue();
415     offset <<= 1;
416     adj = llvm::ConstantInt::get(adj->getType(), offset);
417   }
418 
419   llvm::Value *srcAdj = Builder.CreateExtractValue(src, 1, "src.adj");
420   llvm::Value *dstAdj;
421   if (isDerivedToBase)
422     dstAdj = Builder.CreateNSWSub(srcAdj, adj, "adj");
423   else
424     dstAdj = Builder.CreateNSWAdd(srcAdj, adj, "adj");
425 
426   return Builder.CreateInsertValue(src, dstAdj, 1);
427 }
428 
429 llvm::Constant *
430 ItaniumCXXABI::EmitMemberPointerConversion(const CastExpr *E,
431                                            llvm::Constant *src) {
432   assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
433          E->getCastKind() == CK_BaseToDerivedMemberPointer ||
434          E->getCastKind() == CK_ReinterpretMemberPointer);
435 
436   // Under Itanium, reinterprets don't require any additional processing.
437   if (E->getCastKind() == CK_ReinterpretMemberPointer) return src;
438 
439   // If the adjustment is trivial, we don't need to do anything.
440   llvm::Constant *adj = getMemberPointerAdjustment(E);
441   if (!adj) return src;
442 
443   bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
444 
445   const MemberPointerType *destTy =
446     E->getType()->castAs<MemberPointerType>();
447 
448   // For member data pointers, this is just a matter of adding the
449   // offset if the source is non-null.
450   if (destTy->isMemberDataPointer()) {
451     // null maps to null.
452     if (src->isAllOnesValue()) return src;
453 
454     if (isDerivedToBase)
455       return llvm::ConstantExpr::getNSWSub(src, adj);
456     else
457       return llvm::ConstantExpr::getNSWAdd(src, adj);
458   }
459 
460   // The this-adjustment is left-shifted by 1 on ARM.
461   if (IsARM) {
462     uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue();
463     offset <<= 1;
464     adj = llvm::ConstantInt::get(adj->getType(), offset);
465   }
466 
467   llvm::Constant *srcAdj = llvm::ConstantExpr::getExtractValue(src, 1);
468   llvm::Constant *dstAdj;
469   if (isDerivedToBase)
470     dstAdj = llvm::ConstantExpr::getNSWSub(srcAdj, adj);
471   else
472     dstAdj = llvm::ConstantExpr::getNSWAdd(srcAdj, adj);
473 
474   return llvm::ConstantExpr::getInsertValue(src, dstAdj, 1);
475 }
476 
477 llvm::Constant *
478 ItaniumCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) {
479   llvm::Type *ptrdiff_t = getPtrDiffTy();
480 
481   // Itanium C++ ABI 2.3:
482   //   A NULL pointer is represented as -1.
483   if (MPT->isMemberDataPointer())
484     return llvm::ConstantInt::get(ptrdiff_t, -1ULL, /*isSigned=*/true);
485 
486   llvm::Constant *Zero = llvm::ConstantInt::get(ptrdiff_t, 0);
487   llvm::Constant *Values[2] = { Zero, Zero };
488   return llvm::ConstantStruct::getAnon(Values);
489 }
490 
491 llvm::Constant *
492 ItaniumCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT,
493                                      CharUnits offset) {
494   // Itanium C++ ABI 2.3:
495   //   A pointer to data member is an offset from the base address of
496   //   the class object containing it, represented as a ptrdiff_t
497   return llvm::ConstantInt::get(getPtrDiffTy(), offset.getQuantity());
498 }
499 
500 llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) {
501   return BuildMemberPointer(MD, CharUnits::Zero());
502 }
503 
504 llvm::Constant *ItaniumCXXABI::BuildMemberPointer(const CXXMethodDecl *MD,
505                                                   CharUnits ThisAdjustment) {
506   assert(MD->isInstance() && "Member function must not be static!");
507   MD = MD->getCanonicalDecl();
508 
509   CodeGenTypes &Types = CGM.getTypes();
510   llvm::Type *ptrdiff_t = getPtrDiffTy();
511 
512   // Get the function pointer (or index if this is a virtual function).
513   llvm::Constant *MemPtr[2];
514   if (MD->isVirtual()) {
515     uint64_t Index = CGM.getVTableContext().getMethodVTableIndex(MD);
516 
517     const ASTContext &Context = getContext();
518     CharUnits PointerWidth =
519       Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
520     uint64_t VTableOffset = (Index * PointerWidth.getQuantity());
521 
522     if (IsARM) {
523       // ARM C++ ABI 3.2.1:
524       //   This ABI specifies that adj contains twice the this
525       //   adjustment, plus 1 if the member function is virtual. The
526       //   least significant bit of adj then makes exactly the same
527       //   discrimination as the least significant bit of ptr does for
528       //   Itanium.
529       MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset);
530       MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t,
531                                          2 * ThisAdjustment.getQuantity() + 1);
532     } else {
533       // Itanium C++ ABI 2.3:
534       //   For a virtual function, [the pointer field] is 1 plus the
535       //   virtual table offset (in bytes) of the function,
536       //   represented as a ptrdiff_t.
537       MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset + 1);
538       MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t,
539                                          ThisAdjustment.getQuantity());
540     }
541   } else {
542     const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
543     llvm::Type *Ty;
544     // Check whether the function has a computable LLVM signature.
545     if (Types.isFuncTypeConvertible(FPT)) {
546       // The function has a computable LLVM signature; use the correct type.
547       Ty = Types.GetFunctionType(Types.arrangeCXXMethodDeclaration(MD));
548     } else {
549       // Use an arbitrary non-function type to tell GetAddrOfFunction that the
550       // function type is incomplete.
551       Ty = ptrdiff_t;
552     }
553     llvm::Constant *addr = CGM.GetAddrOfFunction(MD, Ty);
554 
555     MemPtr[0] = llvm::ConstantExpr::getPtrToInt(addr, ptrdiff_t);
556     MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, (IsARM ? 2 : 1) *
557                                        ThisAdjustment.getQuantity());
558   }
559 
560   return llvm::ConstantStruct::getAnon(MemPtr);
561 }
562 
563 llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const APValue &MP,
564                                                  QualType MPType) {
565   const MemberPointerType *MPT = MPType->castAs<MemberPointerType>();
566   const ValueDecl *MPD = MP.getMemberPointerDecl();
567   if (!MPD)
568     return EmitNullMemberPointer(MPT);
569 
570   // Compute the this-adjustment.
571   CharUnits ThisAdjustment = CharUnits::Zero();
572   ArrayRef<const CXXRecordDecl*> Path = MP.getMemberPointerPath();
573   bool DerivedMember = MP.isMemberPointerToDerivedMember();
574   const CXXRecordDecl *RD = cast<CXXRecordDecl>(MPD->getDeclContext());
575   for (unsigned I = 0, N = Path.size(); I != N; ++I) {
576     const CXXRecordDecl *Base = RD;
577     const CXXRecordDecl *Derived = Path[I];
578     if (DerivedMember)
579       std::swap(Base, Derived);
580     ThisAdjustment +=
581       getContext().getASTRecordLayout(Derived).getBaseClassOffset(Base);
582     RD = Path[I];
583   }
584   if (DerivedMember)
585     ThisAdjustment = -ThisAdjustment;
586 
587   if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MPD))
588     return BuildMemberPointer(MD, ThisAdjustment);
589 
590   CharUnits FieldOffset =
591     getContext().toCharUnitsFromBits(getContext().getFieldOffset(MPD));
592   return EmitMemberDataPointer(MPT, ThisAdjustment + FieldOffset);
593 }
594 
595 /// The comparison algorithm is pretty easy: the member pointers are
596 /// the same if they're either bitwise identical *or* both null.
597 ///
598 /// ARM is different here only because null-ness is more complicated.
599 llvm::Value *
600 ItaniumCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF,
601                                            llvm::Value *L,
602                                            llvm::Value *R,
603                                            const MemberPointerType *MPT,
604                                            bool Inequality) {
605   CGBuilderTy &Builder = CGF.Builder;
606 
607   llvm::ICmpInst::Predicate Eq;
608   llvm::Instruction::BinaryOps And, Or;
609   if (Inequality) {
610     Eq = llvm::ICmpInst::ICMP_NE;
611     And = llvm::Instruction::Or;
612     Or = llvm::Instruction::And;
613   } else {
614     Eq = llvm::ICmpInst::ICMP_EQ;
615     And = llvm::Instruction::And;
616     Or = llvm::Instruction::Or;
617   }
618 
619   // Member data pointers are easy because there's a unique null
620   // value, so it just comes down to bitwise equality.
621   if (MPT->isMemberDataPointer())
622     return Builder.CreateICmp(Eq, L, R);
623 
624   // For member function pointers, the tautologies are more complex.
625   // The Itanium tautology is:
626   //   (L == R) <==> (L.ptr == R.ptr && (L.ptr == 0 || L.adj == R.adj))
627   // The ARM tautology is:
628   //   (L == R) <==> (L.ptr == R.ptr &&
629   //                  (L.adj == R.adj ||
630   //                   (L.ptr == 0 && ((L.adj|R.adj) & 1) == 0)))
631   // The inequality tautologies have exactly the same structure, except
632   // applying De Morgan's laws.
633 
634   llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr");
635   llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr");
636 
637   // This condition tests whether L.ptr == R.ptr.  This must always be
638   // true for equality to hold.
639   llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr");
640 
641   // This condition, together with the assumption that L.ptr == R.ptr,
642   // tests whether the pointers are both null.  ARM imposes an extra
643   // condition.
644   llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType());
645   llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null");
646 
647   // This condition tests whether L.adj == R.adj.  If this isn't
648   // true, the pointers are unequal unless they're both null.
649   llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj");
650   llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj");
651   llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj");
652 
653   // Null member function pointers on ARM clear the low bit of Adj,
654   // so the zero condition has to check that neither low bit is set.
655   if (IsARM) {
656     llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1);
657 
658     // Compute (l.adj | r.adj) & 1 and test it against zero.
659     llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj");
660     llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One);
661     llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero,
662                                                       "cmp.or.adj");
663     EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero);
664   }
665 
666   // Tie together all our conditions.
667   llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq);
668   Result = Builder.CreateBinOp(And, PtrEq, Result,
669                                Inequality ? "memptr.ne" : "memptr.eq");
670   return Result;
671 }
672 
673 llvm::Value *
674 ItaniumCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
675                                           llvm::Value *MemPtr,
676                                           const MemberPointerType *MPT) {
677   CGBuilderTy &Builder = CGF.Builder;
678 
679   /// For member data pointers, this is just a check against -1.
680   if (MPT->isMemberDataPointer()) {
681     assert(MemPtr->getType() == getPtrDiffTy());
682     llvm::Value *NegativeOne =
683       llvm::Constant::getAllOnesValue(MemPtr->getType());
684     return Builder.CreateICmpNE(MemPtr, NegativeOne, "memptr.tobool");
685   }
686 
687   // In Itanium, a member function pointer is not null if 'ptr' is not null.
688   llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr");
689 
690   llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0);
691   llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool");
692 
693   // On ARM, a member function pointer is also non-null if the low bit of 'adj'
694   // (the virtual bit) is set.
695   if (IsARM) {
696     llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1);
697     llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj");
698     llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit");
699     llvm::Value *IsVirtual = Builder.CreateICmpNE(VirtualBit, Zero,
700                                                   "memptr.isvirtual");
701     Result = Builder.CreateOr(Result, IsVirtual);
702   }
703 
704   return Result;
705 }
706 
707 /// The Itanium ABI requires non-zero initialization only for data
708 /// member pointers, for which '0' is a valid offset.
709 bool ItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
710   return MPT->getPointeeType()->isFunctionType();
711 }
712 
713 /// The Itanium ABI always places an offset to the complete object
714 /// at entry -2 in the vtable.
715 llvm::Value *ItaniumCXXABI::adjustToCompleteObject(CodeGenFunction &CGF,
716                                                    llvm::Value *ptr,
717                                                    QualType type) {
718   // Grab the vtable pointer as an intptr_t*.
719   llvm::Value *vtable = CGF.GetVTablePtr(ptr, CGF.IntPtrTy->getPointerTo());
720 
721   // Track back to entry -2 and pull out the offset there.
722   llvm::Value *offsetPtr =
723     CGF.Builder.CreateConstInBoundsGEP1_64(vtable, -2, "complete-offset.ptr");
724   llvm::LoadInst *offset = CGF.Builder.CreateLoad(offsetPtr);
725   offset->setAlignment(CGF.PointerAlignInBytes);
726 
727   // Apply the offset.
728   ptr = CGF.Builder.CreateBitCast(ptr, CGF.Int8PtrTy);
729   return CGF.Builder.CreateInBoundsGEP(ptr, offset);
730 }
731 
732 /// The generic ABI passes 'this', plus a VTT if it's initializing a
733 /// base subobject.
734 void ItaniumCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor,
735                                               CXXCtorType Type,
736                                               CanQualType &ResTy,
737                                 SmallVectorImpl<CanQualType> &ArgTys) {
738   ASTContext &Context = getContext();
739 
740   // 'this' is already there.
741 
742   // Check if we need to add a VTT parameter (which has type void **).
743   if (Type == Ctor_Base && Ctor->getParent()->getNumVBases() != 0)
744     ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
745 }
746 
747 /// The ARM ABI does the same as the Itanium ABI, but returns 'this'.
748 void ARMCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor,
749                                           CXXCtorType Type,
750                                           CanQualType &ResTy,
751                                 SmallVectorImpl<CanQualType> &ArgTys) {
752   ItaniumCXXABI::BuildConstructorSignature(Ctor, Type, ResTy, ArgTys);
753   ResTy = ArgTys[0];
754 }
755 
756 /// The generic ABI passes 'this', plus a VTT if it's destroying a
757 /// base subobject.
758 void ItaniumCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor,
759                                              CXXDtorType Type,
760                                              CanQualType &ResTy,
761                                 SmallVectorImpl<CanQualType> &ArgTys) {
762   ASTContext &Context = getContext();
763 
764   // 'this' is already there.
765 
766   // Check if we need to add a VTT parameter (which has type void **).
767   if (Type == Dtor_Base && Dtor->getParent()->getNumVBases() != 0)
768     ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
769 }
770 
771 /// The ARM ABI does the same as the Itanium ABI, but returns 'this'
772 /// for non-deleting destructors.
773 void ARMCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor,
774                                          CXXDtorType Type,
775                                          CanQualType &ResTy,
776                                 SmallVectorImpl<CanQualType> &ArgTys) {
777   ItaniumCXXABI::BuildDestructorSignature(Dtor, Type, ResTy, ArgTys);
778 
779   if (Type != Dtor_Deleting)
780     ResTy = ArgTys[0];
781 }
782 
783 void ItaniumCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF,
784                                                 QualType &ResTy,
785                                                 FunctionArgList &Params) {
786   /// Create the 'this' variable.
787   BuildThisParam(CGF, Params);
788 
789   const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl());
790   assert(MD->isInstance());
791 
792   // Check if we need a VTT parameter as well.
793   if (CodeGenVTables::needsVTTParameter(CGF.CurGD)) {
794     ASTContext &Context = getContext();
795 
796     // FIXME: avoid the fake decl
797     QualType T = Context.getPointerType(Context.VoidPtrTy);
798     ImplicitParamDecl *VTTDecl
799       = ImplicitParamDecl::Create(Context, 0, MD->getLocation(),
800                                   &Context.Idents.get("vtt"), T);
801     Params.push_back(VTTDecl);
802     getVTTDecl(CGF) = VTTDecl;
803   }
804 }
805 
806 void ARMCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF,
807                                             QualType &ResTy,
808                                             FunctionArgList &Params) {
809   ItaniumCXXABI::BuildInstanceFunctionParams(CGF, ResTy, Params);
810 
811   // Return 'this' from certain constructors and destructors.
812   if (HasThisReturn(CGF.CurGD))
813     ResTy = Params[0]->getType();
814 }
815 
816 void ItaniumCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
817   /// Initialize the 'this' slot.
818   EmitThisParam(CGF);
819 
820   /// Initialize the 'vtt' slot if needed.
821   if (getVTTDecl(CGF)) {
822     getVTTValue(CGF)
823       = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(getVTTDecl(CGF)),
824                                "vtt");
825   }
826 }
827 
828 void ARMCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
829   ItaniumCXXABI::EmitInstanceFunctionProlog(CGF);
830 
831   /// Initialize the return slot to 'this' at the start of the
832   /// function.
833   if (HasThisReturn(CGF.CurGD))
834     CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue);
835 }
836 
837 llvm::Value *ItaniumCXXABI::EmitConstructorCall(CodeGenFunction &CGF,
838                                         const CXXConstructorDecl *D,
839                                         CXXCtorType Type, bool ForVirtualBase,
840                                         bool Delegating,
841                                         llvm::Value *This,
842                                         CallExpr::const_arg_iterator ArgBeg,
843                                         CallExpr::const_arg_iterator ArgEnd) {
844   llvm::Value *VTT = CGF.GetVTTParameter(GlobalDecl(D, Type), ForVirtualBase,
845                                          Delegating);
846   QualType VTTTy = getContext().getPointerType(getContext().VoidPtrTy);
847   llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type);
848 
849   // FIXME: Provide a source location here.
850   CGF.EmitCXXMemberCall(D, SourceLocation(), Callee, ReturnValueSlot(), This,
851                         VTT, VTTTy, ArgBeg, ArgEnd);
852   return Callee;
853 }
854 
855 RValue ItaniumCXXABI::EmitVirtualDestructorCall(CodeGenFunction &CGF,
856                                                 const CXXDestructorDecl *Dtor,
857                                                 CXXDtorType DtorType,
858                                                 SourceLocation CallLoc,
859                                                 ReturnValueSlot ReturnValue,
860                                                 llvm::Value *This) {
861   assert(DtorType == Dtor_Deleting || DtorType == Dtor_Complete);
862 
863   const CGFunctionInfo *FInfo
864     = &CGM.getTypes().arrangeCXXDestructor(Dtor, DtorType);
865   llvm::Type *Ty = CGF.CGM.getTypes().GetFunctionType(*FInfo);
866   llvm::Value *Callee = CGF.BuildVirtualCall(Dtor, DtorType, This, Ty);
867 
868   return CGF.EmitCXXMemberCall(Dtor, CallLoc, Callee, ReturnValue, This,
869                                /*ImplicitParam=*/0, QualType(), 0, 0);
870 }
871 
872 void ARMCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF,
873                                     RValue RV, QualType ResultType) {
874   if (!isa<CXXDestructorDecl>(CGF.CurGD.getDecl()))
875     return ItaniumCXXABI::EmitReturnFromThunk(CGF, RV, ResultType);
876 
877   // Destructor thunks in the ARM ABI have indeterminate results.
878   llvm::Type *T =
879     cast<llvm::PointerType>(CGF.ReturnValue->getType())->getElementType();
880   RValue Undef = RValue::get(llvm::UndefValue::get(T));
881   return ItaniumCXXABI::EmitReturnFromThunk(CGF, Undef, ResultType);
882 }
883 
884 /************************** Array allocation cookies **************************/
885 
886 CharUnits ItaniumCXXABI::getArrayCookieSizeImpl(QualType elementType) {
887   // The array cookie is a size_t; pad that up to the element alignment.
888   // The cookie is actually right-justified in that space.
889   return std::max(CharUnits::fromQuantity(CGM.SizeSizeInBytes),
890                   CGM.getContext().getTypeAlignInChars(elementType));
891 }
892 
893 llvm::Value *ItaniumCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
894                                                   llvm::Value *NewPtr,
895                                                   llvm::Value *NumElements,
896                                                   const CXXNewExpr *expr,
897                                                   QualType ElementType) {
898   assert(requiresArrayCookie(expr));
899 
900   unsigned AS = NewPtr->getType()->getPointerAddressSpace();
901 
902   ASTContext &Ctx = getContext();
903   QualType SizeTy = Ctx.getSizeType();
904   CharUnits SizeSize = Ctx.getTypeSizeInChars(SizeTy);
905 
906   // The size of the cookie.
907   CharUnits CookieSize =
908     std::max(SizeSize, Ctx.getTypeAlignInChars(ElementType));
909   assert(CookieSize == getArrayCookieSizeImpl(ElementType));
910 
911   // Compute an offset to the cookie.
912   llvm::Value *CookiePtr = NewPtr;
913   CharUnits CookieOffset = CookieSize - SizeSize;
914   if (!CookieOffset.isZero())
915     CookiePtr = CGF.Builder.CreateConstInBoundsGEP1_64(CookiePtr,
916                                                  CookieOffset.getQuantity());
917 
918   // Write the number of elements into the appropriate slot.
919   llvm::Value *NumElementsPtr
920     = CGF.Builder.CreateBitCast(CookiePtr,
921                                 CGF.ConvertType(SizeTy)->getPointerTo(AS));
922   CGF.Builder.CreateStore(NumElements, NumElementsPtr);
923 
924   // Finally, compute a pointer to the actual data buffer by skipping
925   // over the cookie completely.
926   return CGF.Builder.CreateConstInBoundsGEP1_64(NewPtr,
927                                                 CookieSize.getQuantity());
928 }
929 
930 llvm::Value *ItaniumCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
931                                                 llvm::Value *allocPtr,
932                                                 CharUnits cookieSize) {
933   // The element size is right-justified in the cookie.
934   llvm::Value *numElementsPtr = allocPtr;
935   CharUnits numElementsOffset =
936     cookieSize - CharUnits::fromQuantity(CGF.SizeSizeInBytes);
937   if (!numElementsOffset.isZero())
938     numElementsPtr =
939       CGF.Builder.CreateConstInBoundsGEP1_64(numElementsPtr,
940                                              numElementsOffset.getQuantity());
941 
942   unsigned AS = allocPtr->getType()->getPointerAddressSpace();
943   numElementsPtr =
944     CGF.Builder.CreateBitCast(numElementsPtr, CGF.SizeTy->getPointerTo(AS));
945   return CGF.Builder.CreateLoad(numElementsPtr);
946 }
947 
948 CharUnits ARMCXXABI::getArrayCookieSizeImpl(QualType elementType) {
949   // ARM says that the cookie is always:
950   //   struct array_cookie {
951   //     std::size_t element_size; // element_size != 0
952   //     std::size_t element_count;
953   //   };
954   // But the base ABI doesn't give anything an alignment greater than
955   // 8, so we can dismiss this as typical ABI-author blindness to
956   // actual language complexity and round up to the element alignment.
957   return std::max(CharUnits::fromQuantity(2 * CGM.SizeSizeInBytes),
958                   CGM.getContext().getTypeAlignInChars(elementType));
959 }
960 
961 llvm::Value *ARMCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
962                                               llvm::Value *newPtr,
963                                               llvm::Value *numElements,
964                                               const CXXNewExpr *expr,
965                                               QualType elementType) {
966   assert(requiresArrayCookie(expr));
967 
968   // NewPtr is a char*, but we generalize to arbitrary addrspaces.
969   unsigned AS = newPtr->getType()->getPointerAddressSpace();
970 
971   // The cookie is always at the start of the buffer.
972   llvm::Value *cookie = newPtr;
973 
974   // The first element is the element size.
975   cookie = CGF.Builder.CreateBitCast(cookie, CGF.SizeTy->getPointerTo(AS));
976   llvm::Value *elementSize = llvm::ConstantInt::get(CGF.SizeTy,
977                  getContext().getTypeSizeInChars(elementType).getQuantity());
978   CGF.Builder.CreateStore(elementSize, cookie);
979 
980   // The second element is the element count.
981   cookie = CGF.Builder.CreateConstInBoundsGEP1_32(cookie, 1);
982   CGF.Builder.CreateStore(numElements, cookie);
983 
984   // Finally, compute a pointer to the actual data buffer by skipping
985   // over the cookie completely.
986   CharUnits cookieSize = ARMCXXABI::getArrayCookieSizeImpl(elementType);
987   return CGF.Builder.CreateConstInBoundsGEP1_64(newPtr,
988                                                 cookieSize.getQuantity());
989 }
990 
991 llvm::Value *ARMCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
992                                             llvm::Value *allocPtr,
993                                             CharUnits cookieSize) {
994   // The number of elements is at offset sizeof(size_t) relative to
995   // the allocated pointer.
996   llvm::Value *numElementsPtr
997     = CGF.Builder.CreateConstInBoundsGEP1_64(allocPtr, CGF.SizeSizeInBytes);
998 
999   unsigned AS = allocPtr->getType()->getPointerAddressSpace();
1000   numElementsPtr =
1001     CGF.Builder.CreateBitCast(numElementsPtr, CGF.SizeTy->getPointerTo(AS));
1002   return CGF.Builder.CreateLoad(numElementsPtr);
1003 }
1004 
1005 /*********************** Static local initialization **************************/
1006 
1007 static llvm::Constant *getGuardAcquireFn(CodeGenModule &CGM,
1008                                          llvm::PointerType *GuardPtrTy) {
1009   // int __cxa_guard_acquire(__guard *guard_object);
1010   llvm::FunctionType *FTy =
1011     llvm::FunctionType::get(CGM.getTypes().ConvertType(CGM.getContext().IntTy),
1012                             GuardPtrTy, /*isVarArg=*/false);
1013   return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_acquire",
1014                                    llvm::AttributeSet::get(CGM.getLLVMContext(),
1015                                               llvm::AttributeSet::FunctionIndex,
1016                                                  llvm::Attribute::NoUnwind));
1017 }
1018 
1019 static llvm::Constant *getGuardReleaseFn(CodeGenModule &CGM,
1020                                          llvm::PointerType *GuardPtrTy) {
1021   // void __cxa_guard_release(__guard *guard_object);
1022   llvm::FunctionType *FTy =
1023     llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false);
1024   return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_release",
1025                                    llvm::AttributeSet::get(CGM.getLLVMContext(),
1026                                               llvm::AttributeSet::FunctionIndex,
1027                                                  llvm::Attribute::NoUnwind));
1028 }
1029 
1030 static llvm::Constant *getGuardAbortFn(CodeGenModule &CGM,
1031                                        llvm::PointerType *GuardPtrTy) {
1032   // void __cxa_guard_abort(__guard *guard_object);
1033   llvm::FunctionType *FTy =
1034     llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false);
1035   return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_abort",
1036                                    llvm::AttributeSet::get(CGM.getLLVMContext(),
1037                                               llvm::AttributeSet::FunctionIndex,
1038                                                  llvm::Attribute::NoUnwind));
1039 }
1040 
1041 namespace {
1042   struct CallGuardAbort : EHScopeStack::Cleanup {
1043     llvm::GlobalVariable *Guard;
1044     CallGuardAbort(llvm::GlobalVariable *Guard) : Guard(Guard) {}
1045 
1046     void Emit(CodeGenFunction &CGF, Flags flags) {
1047       CGF.EmitNounwindRuntimeCall(getGuardAbortFn(CGF.CGM, Guard->getType()),
1048                                   Guard);
1049     }
1050   };
1051 }
1052 
1053 /// The ARM code here follows the Itanium code closely enough that we
1054 /// just special-case it at particular places.
1055 void ItaniumCXXABI::EmitGuardedInit(CodeGenFunction &CGF,
1056                                     const VarDecl &D,
1057                                     llvm::GlobalVariable *var,
1058                                     bool shouldPerformInit) {
1059   CGBuilderTy &Builder = CGF.Builder;
1060 
1061   // We only need to use thread-safe statics for local variables;
1062   // global initialization is always single-threaded.
1063   bool threadsafe =
1064     (getContext().getLangOpts().ThreadsafeStatics && D.isLocalVarDecl());
1065 
1066   // If we have a global variable with internal linkage and thread-safe statics
1067   // are disabled, we can just let the guard variable be of type i8.
1068   bool useInt8GuardVariable = !threadsafe && var->hasInternalLinkage();
1069 
1070   llvm::IntegerType *guardTy;
1071   if (useInt8GuardVariable) {
1072     guardTy = CGF.Int8Ty;
1073   } else {
1074     // Guard variables are 64 bits in the generic ABI and size width on ARM
1075     // (i.e. 32-bit on AArch32, 64-bit on AArch64).
1076     guardTy = (IsARM ? CGF.SizeTy : CGF.Int64Ty);
1077   }
1078   llvm::PointerType *guardPtrTy = guardTy->getPointerTo();
1079 
1080   // Create the guard variable if we don't already have it (as we
1081   // might if we're double-emitting this function body).
1082   llvm::GlobalVariable *guard = CGM.getStaticLocalDeclGuardAddress(&D);
1083   if (!guard) {
1084     // Mangle the name for the guard.
1085     SmallString<256> guardName;
1086     {
1087       llvm::raw_svector_ostream out(guardName);
1088       getMangleContext().mangleItaniumGuardVariable(&D, out);
1089       out.flush();
1090     }
1091 
1092     // Create the guard variable with a zero-initializer.
1093     // Just absorb linkage and visibility from the guarded variable.
1094     guard = new llvm::GlobalVariable(CGM.getModule(), guardTy,
1095                                      false, var->getLinkage(),
1096                                      llvm::ConstantInt::get(guardTy, 0),
1097                                      guardName.str());
1098     guard->setVisibility(var->getVisibility());
1099 
1100     CGM.setStaticLocalDeclGuardAddress(&D, guard);
1101   }
1102 
1103   // Test whether the variable has completed initialization.
1104   llvm::Value *isInitialized;
1105 
1106   // ARM C++ ABI 3.2.3.1:
1107   //   To support the potential use of initialization guard variables
1108   //   as semaphores that are the target of ARM SWP and LDREX/STREX
1109   //   synchronizing instructions we define a static initialization
1110   //   guard variable to be a 4-byte aligned, 4- byte word with the
1111   //   following inline access protocol.
1112   //     #define INITIALIZED 1
1113   //     if ((obj_guard & INITIALIZED) != INITIALIZED) {
1114   //       if (__cxa_guard_acquire(&obj_guard))
1115   //         ...
1116   //     }
1117   if (IsARM && !useInt8GuardVariable) {
1118     llvm::Value *V = Builder.CreateLoad(guard);
1119     llvm::Value *Test1 = llvm::ConstantInt::get(guardTy, 1);
1120     V = Builder.CreateAnd(V, Test1);
1121     isInitialized = Builder.CreateIsNull(V, "guard.uninitialized");
1122 
1123   // Itanium C++ ABI 3.3.2:
1124   //   The following is pseudo-code showing how these functions can be used:
1125   //     if (obj_guard.first_byte == 0) {
1126   //       if ( __cxa_guard_acquire (&obj_guard) ) {
1127   //         try {
1128   //           ... initialize the object ...;
1129   //         } catch (...) {
1130   //            __cxa_guard_abort (&obj_guard);
1131   //            throw;
1132   //         }
1133   //         ... queue object destructor with __cxa_atexit() ...;
1134   //         __cxa_guard_release (&obj_guard);
1135   //       }
1136   //     }
1137   } else {
1138     // Load the first byte of the guard variable.
1139     llvm::LoadInst *LI =
1140       Builder.CreateLoad(Builder.CreateBitCast(guard, CGM.Int8PtrTy));
1141     LI->setAlignment(1);
1142 
1143     // Itanium ABI:
1144     //   An implementation supporting thread-safety on multiprocessor
1145     //   systems must also guarantee that references to the initialized
1146     //   object do not occur before the load of the initialization flag.
1147     //
1148     // In LLVM, we do this by marking the load Acquire.
1149     if (threadsafe)
1150       LI->setAtomic(llvm::Acquire);
1151 
1152     isInitialized = Builder.CreateIsNull(LI, "guard.uninitialized");
1153   }
1154 
1155   llvm::BasicBlock *InitCheckBlock = CGF.createBasicBlock("init.check");
1156   llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end");
1157 
1158   // Check if the first byte of the guard variable is zero.
1159   Builder.CreateCondBr(isInitialized, InitCheckBlock, EndBlock);
1160 
1161   CGF.EmitBlock(InitCheckBlock);
1162 
1163   // Variables used when coping with thread-safe statics and exceptions.
1164   if (threadsafe) {
1165     // Call __cxa_guard_acquire.
1166     llvm::Value *V
1167       = CGF.EmitNounwindRuntimeCall(getGuardAcquireFn(CGM, guardPtrTy), guard);
1168 
1169     llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init");
1170 
1171     Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"),
1172                          InitBlock, EndBlock);
1173 
1174     // Call __cxa_guard_abort along the exceptional edge.
1175     CGF.EHStack.pushCleanup<CallGuardAbort>(EHCleanup, guard);
1176 
1177     CGF.EmitBlock(InitBlock);
1178   }
1179 
1180   // Emit the initializer and add a global destructor if appropriate.
1181   CGF.EmitCXXGlobalVarDeclInit(D, var, shouldPerformInit);
1182 
1183   if (threadsafe) {
1184     // Pop the guard-abort cleanup if we pushed one.
1185     CGF.PopCleanupBlock();
1186 
1187     // Call __cxa_guard_release.  This cannot throw.
1188     CGF.EmitNounwindRuntimeCall(getGuardReleaseFn(CGM, guardPtrTy), guard);
1189   } else {
1190     Builder.CreateStore(llvm::ConstantInt::get(guardTy, 1), guard);
1191   }
1192 
1193   CGF.EmitBlock(EndBlock);
1194 }
1195 
1196 /// Register a global destructor using __cxa_atexit.
1197 static void emitGlobalDtorWithCXAAtExit(CodeGenFunction &CGF,
1198                                         llvm::Constant *dtor,
1199                                         llvm::Constant *addr) {
1200   // We're assuming that the destructor function is something we can
1201   // reasonably call with the default CC.  Go ahead and cast it to the
1202   // right prototype.
1203   llvm::Type *dtorTy =
1204     llvm::FunctionType::get(CGF.VoidTy, CGF.Int8PtrTy, false)->getPointerTo();
1205 
1206   // extern "C" int __cxa_atexit(void (*f)(void *), void *p, void *d);
1207   llvm::Type *paramTys[] = { dtorTy, CGF.Int8PtrTy, CGF.Int8PtrTy };
1208   llvm::FunctionType *atexitTy =
1209     llvm::FunctionType::get(CGF.IntTy, paramTys, false);
1210 
1211   // Fetch the actual function.
1212   llvm::Constant *atexit =
1213     CGF.CGM.CreateRuntimeFunction(atexitTy, "__cxa_atexit");
1214   if (llvm::Function *fn = dyn_cast<llvm::Function>(atexit))
1215     fn->setDoesNotThrow();
1216 
1217   // Create a variable that binds the atexit to this shared object.
1218   llvm::Constant *handle =
1219     CGF.CGM.CreateRuntimeVariable(CGF.Int8Ty, "__dso_handle");
1220 
1221   llvm::Value *args[] = {
1222     llvm::ConstantExpr::getBitCast(dtor, dtorTy),
1223     llvm::ConstantExpr::getBitCast(addr, CGF.Int8PtrTy),
1224     handle
1225   };
1226   CGF.EmitNounwindRuntimeCall(atexit, args);
1227 }
1228 
1229 /// Register a global destructor as best as we know how.
1230 void ItaniumCXXABI::registerGlobalDtor(CodeGenFunction &CGF,
1231                                        llvm::Constant *dtor,
1232                                        llvm::Constant *addr) {
1233   // Use __cxa_atexit if available.
1234   if (CGM.getCodeGenOpts().CXAAtExit) {
1235     return emitGlobalDtorWithCXAAtExit(CGF, dtor, addr);
1236   }
1237 
1238   // In Apple kexts, we want to add a global destructor entry.
1239   // FIXME: shouldn't this be guarded by some variable?
1240   if (CGM.getLangOpts().AppleKext) {
1241     // Generate a global destructor entry.
1242     return CGM.AddCXXDtorEntry(dtor, addr);
1243   }
1244 
1245   CGF.registerGlobalDtorWithAtExit(dtor, addr);
1246 }
1247