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 "CGCleanup.h"
23 #include "CGRecordLayout.h"
24 #include "CGVTables.h"
25 #include "CodeGenFunction.h"
26 #include "CodeGenModule.h"
27 #include "TargetInfo.h"
28 #include "clang/AST/Mangle.h"
29 #include "clang/AST/Type.h"
30 #include "clang/AST/StmtCXX.h"
31 #include "llvm/IR/CallSite.h"
32 #include "llvm/IR/DataLayout.h"
33 #include "llvm/IR/Instructions.h"
34 #include "llvm/IR/Intrinsics.h"
35 #include "llvm/IR/Value.h"
36 
37 using namespace clang;
38 using namespace CodeGen;
39 
40 namespace {
41 class ItaniumCXXABI : public CodeGen::CGCXXABI {
42   /// VTables - All the vtables which have been defined.
43   llvm::DenseMap<const CXXRecordDecl *, llvm::GlobalVariable *> VTables;
44 
45 protected:
46   bool UseARMMethodPtrABI;
47   bool UseARMGuardVarABI;
48 
49   ItaniumMangleContext &getMangleContext() {
50     return cast<ItaniumMangleContext>(CodeGen::CGCXXABI::getMangleContext());
51   }
52 
53 public:
54   ItaniumCXXABI(CodeGen::CodeGenModule &CGM,
55                 bool UseARMMethodPtrABI = false,
56                 bool UseARMGuardVarABI = false) :
57     CGCXXABI(CGM), UseARMMethodPtrABI(UseARMMethodPtrABI),
58     UseARMGuardVarABI(UseARMGuardVarABI) { }
59 
60   bool classifyReturnType(CGFunctionInfo &FI) const override;
61 
62   RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const override {
63     // Structures with either a non-trivial destructor or a non-trivial
64     // copy constructor are always indirect.
65     // FIXME: Use canCopyArgument() when it is fixed to handle lazily declared
66     // special members.
67     if (RD->hasNonTrivialDestructor() || RD->hasNonTrivialCopyConstructor())
68       return RAA_Indirect;
69     return RAA_Default;
70   }
71 
72   bool isZeroInitializable(const MemberPointerType *MPT) override;
73 
74   llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT) override;
75 
76   llvm::Value *
77     EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
78                                     const Expr *E,
79                                     llvm::Value *&This,
80                                     llvm::Value *MemFnPtr,
81                                     const MemberPointerType *MPT) override;
82 
83   llvm::Value *
84     EmitMemberDataPointerAddress(CodeGenFunction &CGF, const Expr *E,
85                                  llvm::Value *Base,
86                                  llvm::Value *MemPtr,
87                                  const MemberPointerType *MPT) override;
88 
89   llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
90                                            const CastExpr *E,
91                                            llvm::Value *Src) override;
92   llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
93                                               llvm::Constant *Src) override;
94 
95   llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT) override;
96 
97   llvm::Constant *EmitMemberFunctionPointer(const CXXMethodDecl *MD) override;
98   llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
99                                         CharUnits offset) override;
100   llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT) override;
101   llvm::Constant *BuildMemberPointer(const CXXMethodDecl *MD,
102                                      CharUnits ThisAdjustment);
103 
104   llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF,
105                                            llvm::Value *L, llvm::Value *R,
106                                            const MemberPointerType *MPT,
107                                            bool Inequality) override;
108 
109   llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
110                                          llvm::Value *Addr,
111                                          const MemberPointerType *MPT) override;
112 
113   void emitVirtualObjectDelete(CodeGenFunction &CGF, const CXXDeleteExpr *DE,
114                                llvm::Value *Ptr, QualType ElementType,
115                                const CXXDestructorDecl *Dtor) override;
116 
117   void emitRethrow(CodeGenFunction &CGF, bool isNoReturn) override;
118   void emitThrow(CodeGenFunction &CGF, const CXXThrowExpr *E) override;
119 
120   void emitBeginCatch(CodeGenFunction &CGF, const CXXCatchStmt *C) override;
121 
122   llvm::CallInst *
123   emitTerminateForUnexpectedException(CodeGenFunction &CGF,
124                                       llvm::Value *Exn) override;
125 
126   void EmitFundamentalRTTIDescriptor(QualType Type);
127   void EmitFundamentalRTTIDescriptors();
128   llvm::Constant *getAddrOfRTTIDescriptor(QualType Ty) override;
129   llvm::Constant *
130   getAddrOfCXXCatchHandlerType(QualType Ty,
131                                QualType CatchHandlerType) override {
132     return getAddrOfRTTIDescriptor(Ty);
133   }
134 
135   bool shouldTypeidBeNullChecked(bool IsDeref, QualType SrcRecordTy) override;
136   void EmitBadTypeidCall(CodeGenFunction &CGF) override;
137   llvm::Value *EmitTypeid(CodeGenFunction &CGF, QualType SrcRecordTy,
138                           llvm::Value *ThisPtr,
139                           llvm::Type *StdTypeInfoPtrTy) override;
140 
141   bool shouldDynamicCastCallBeNullChecked(bool SrcIsPtr,
142                                           QualType SrcRecordTy) override;
143 
144   llvm::Value *EmitDynamicCastCall(CodeGenFunction &CGF, llvm::Value *Value,
145                                    QualType SrcRecordTy, QualType DestTy,
146                                    QualType DestRecordTy,
147                                    llvm::BasicBlock *CastEnd) override;
148 
149   llvm::Value *EmitDynamicCastToVoid(CodeGenFunction &CGF, llvm::Value *Value,
150                                      QualType SrcRecordTy,
151                                      QualType DestTy) override;
152 
153   bool EmitBadCastCall(CodeGenFunction &CGF) override;
154 
155   llvm::Value *
156     GetVirtualBaseClassOffset(CodeGenFunction &CGF, llvm::Value *This,
157                               const CXXRecordDecl *ClassDecl,
158                               const CXXRecordDecl *BaseClassDecl) override;
159 
160   void EmitCXXConstructors(const CXXConstructorDecl *D) override;
161 
162   void buildStructorSignature(const CXXMethodDecl *MD, StructorType T,
163                               SmallVectorImpl<CanQualType> &ArgTys) override;
164 
165   bool useThunkForDtorVariant(const CXXDestructorDecl *Dtor,
166                               CXXDtorType DT) const override {
167     // Itanium does not emit any destructor variant as an inline thunk.
168     // Delegating may occur as an optimization, but all variants are either
169     // emitted with external linkage or as linkonce if they are inline and used.
170     return false;
171   }
172 
173   void EmitCXXDestructors(const CXXDestructorDecl *D) override;
174 
175   void addImplicitStructorParams(CodeGenFunction &CGF, QualType &ResTy,
176                                  FunctionArgList &Params) override;
177 
178   void EmitInstanceFunctionProlog(CodeGenFunction &CGF) override;
179 
180   unsigned addImplicitConstructorArgs(CodeGenFunction &CGF,
181                                       const CXXConstructorDecl *D,
182                                       CXXCtorType Type, bool ForVirtualBase,
183                                       bool Delegating,
184                                       CallArgList &Args) override;
185 
186   void EmitDestructorCall(CodeGenFunction &CGF, const CXXDestructorDecl *DD,
187                           CXXDtorType Type, bool ForVirtualBase,
188                           bool Delegating, llvm::Value *This) override;
189 
190   void emitVTableDefinitions(CodeGenVTables &CGVT,
191                              const CXXRecordDecl *RD) override;
192 
193   llvm::Value *getVTableAddressPointInStructor(
194       CodeGenFunction &CGF, const CXXRecordDecl *VTableClass,
195       BaseSubobject Base, const CXXRecordDecl *NearestVBase,
196       bool &NeedsVirtualOffset) override;
197 
198   llvm::Constant *
199   getVTableAddressPointForConstExpr(BaseSubobject Base,
200                                     const CXXRecordDecl *VTableClass) override;
201 
202   llvm::GlobalVariable *getAddrOfVTable(const CXXRecordDecl *RD,
203                                         CharUnits VPtrOffset) override;
204 
205   llvm::Value *getVirtualFunctionPointer(CodeGenFunction &CGF, GlobalDecl GD,
206                                          llvm::Value *This,
207                                          llvm::Type *Ty,
208                                          SourceLocation Loc) override;
209 
210   llvm::Value *EmitVirtualDestructorCall(CodeGenFunction &CGF,
211                                          const CXXDestructorDecl *Dtor,
212                                          CXXDtorType DtorType,
213                                          llvm::Value *This,
214                                          const CXXMemberCallExpr *CE) override;
215 
216   void emitVirtualInheritanceTables(const CXXRecordDecl *RD) override;
217 
218   void setThunkLinkage(llvm::Function *Thunk, bool ForVTable, GlobalDecl GD,
219                        bool ReturnAdjustment) override {
220     // Allow inlining of thunks by emitting them with available_externally
221     // linkage together with vtables when needed.
222     if (ForVTable && !Thunk->hasLocalLinkage())
223       Thunk->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage);
224   }
225 
226   llvm::Value *performThisAdjustment(CodeGenFunction &CGF, llvm::Value *This,
227                                      const ThisAdjustment &TA) override;
228 
229   llvm::Value *performReturnAdjustment(CodeGenFunction &CGF, llvm::Value *Ret,
230                                        const ReturnAdjustment &RA) override;
231 
232   size_t getSrcArgforCopyCtor(const CXXConstructorDecl *,
233                               FunctionArgList &Args) const override {
234     assert(!Args.empty() && "expected the arglist to not be empty!");
235     return Args.size() - 1;
236   }
237 
238   StringRef GetPureVirtualCallName() override { return "__cxa_pure_virtual"; }
239   StringRef GetDeletedVirtualCallName() override
240     { return "__cxa_deleted_virtual"; }
241 
242   CharUnits getArrayCookieSizeImpl(QualType elementType) override;
243   llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
244                                      llvm::Value *NewPtr,
245                                      llvm::Value *NumElements,
246                                      const CXXNewExpr *expr,
247                                      QualType ElementType) override;
248   llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF,
249                                    llvm::Value *allocPtr,
250                                    CharUnits cookieSize) override;
251 
252   void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
253                        llvm::GlobalVariable *DeclPtr,
254                        bool PerformInit) override;
255   void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
256                           llvm::Constant *dtor, llvm::Constant *addr) override;
257 
258   llvm::Function *getOrCreateThreadLocalWrapper(const VarDecl *VD,
259                                                 llvm::Value *Val);
260   void EmitThreadLocalInitFuncs(
261       CodeGenModule &CGM,
262       ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *>>
263           CXXThreadLocals,
264       ArrayRef<llvm::Function *> CXXThreadLocalInits,
265       ArrayRef<llvm::GlobalVariable *> CXXThreadLocalInitVars) override;
266 
267   bool usesThreadWrapperFunction() const override { return true; }
268   LValue EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF, const VarDecl *VD,
269                                       QualType LValType) override;
270 
271   bool NeedsVTTParameter(GlobalDecl GD) override;
272 
273   /**************************** RTTI Uniqueness ******************************/
274 
275 protected:
276   /// Returns true if the ABI requires RTTI type_info objects to be unique
277   /// across a program.
278   virtual bool shouldRTTIBeUnique() const { return true; }
279 
280 public:
281   /// What sort of unique-RTTI behavior should we use?
282   enum RTTIUniquenessKind {
283     /// We are guaranteeing, or need to guarantee, that the RTTI string
284     /// is unique.
285     RUK_Unique,
286 
287     /// We are not guaranteeing uniqueness for the RTTI string, so we
288     /// can demote to hidden visibility but must use string comparisons.
289     RUK_NonUniqueHidden,
290 
291     /// We are not guaranteeing uniqueness for the RTTI string, so we
292     /// have to use string comparisons, but we also have to emit it with
293     /// non-hidden visibility.
294     RUK_NonUniqueVisible
295   };
296 
297   /// Return the required visibility status for the given type and linkage in
298   /// the current ABI.
299   RTTIUniquenessKind
300   classifyRTTIUniqueness(QualType CanTy,
301                          llvm::GlobalValue::LinkageTypes Linkage) const;
302   friend class ItaniumRTTIBuilder;
303 
304   void emitCXXStructor(const CXXMethodDecl *MD, StructorType Type) override;
305 };
306 
307 class ARMCXXABI : public ItaniumCXXABI {
308 public:
309   ARMCXXABI(CodeGen::CodeGenModule &CGM) :
310     ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true,
311                   /* UseARMGuardVarABI = */ true) {}
312 
313   bool HasThisReturn(GlobalDecl GD) const override {
314     return (isa<CXXConstructorDecl>(GD.getDecl()) || (
315               isa<CXXDestructorDecl>(GD.getDecl()) &&
316               GD.getDtorType() != Dtor_Deleting));
317   }
318 
319   void EmitReturnFromThunk(CodeGenFunction &CGF, RValue RV,
320                            QualType ResTy) override;
321 
322   CharUnits getArrayCookieSizeImpl(QualType elementType) override;
323   llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
324                                      llvm::Value *NewPtr,
325                                      llvm::Value *NumElements,
326                                      const CXXNewExpr *expr,
327                                      QualType ElementType) override;
328   llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, llvm::Value *allocPtr,
329                                    CharUnits cookieSize) override;
330 };
331 
332 class iOS64CXXABI : public ARMCXXABI {
333 public:
334   iOS64CXXABI(CodeGen::CodeGenModule &CGM) : ARMCXXABI(CGM) {}
335 
336   // ARM64 libraries are prepared for non-unique RTTI.
337   bool shouldRTTIBeUnique() const override { return false; }
338 };
339 }
340 
341 CodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) {
342   switch (CGM.getTarget().getCXXABI().getKind()) {
343   // For IR-generation purposes, there's no significant difference
344   // between the ARM and iOS ABIs.
345   case TargetCXXABI::GenericARM:
346   case TargetCXXABI::iOS:
347     return new ARMCXXABI(CGM);
348 
349   case TargetCXXABI::iOS64:
350     return new iOS64CXXABI(CGM);
351 
352   // Note that AArch64 uses the generic ItaniumCXXABI class since it doesn't
353   // include the other 32-bit ARM oddities: constructor/destructor return values
354   // and array cookies.
355   case TargetCXXABI::GenericAArch64:
356     return new ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true,
357                              /* UseARMGuardVarABI = */ true);
358 
359   case TargetCXXABI::GenericMIPS:
360     return new ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true);
361 
362   case TargetCXXABI::GenericItanium:
363     if (CGM.getContext().getTargetInfo().getTriple().getArch()
364         == llvm::Triple::le32) {
365       // For PNaCl, use ARM-style method pointers so that PNaCl code
366       // does not assume anything about the alignment of function
367       // pointers.
368       return new ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true,
369                                /* UseARMGuardVarABI = */ false);
370     }
371     return new ItaniumCXXABI(CGM);
372 
373   case TargetCXXABI::Microsoft:
374     llvm_unreachable("Microsoft ABI is not Itanium-based");
375   }
376   llvm_unreachable("bad ABI kind");
377 }
378 
379 llvm::Type *
380 ItaniumCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) {
381   if (MPT->isMemberDataPointer())
382     return CGM.PtrDiffTy;
383   return llvm::StructType::get(CGM.PtrDiffTy, CGM.PtrDiffTy, nullptr);
384 }
385 
386 /// In the Itanium and ARM ABIs, method pointers have the form:
387 ///   struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr;
388 ///
389 /// In the Itanium ABI:
390 ///  - method pointers are virtual if (memptr.ptr & 1) is nonzero
391 ///  - the this-adjustment is (memptr.adj)
392 ///  - the virtual offset is (memptr.ptr - 1)
393 ///
394 /// In the ARM ABI:
395 ///  - method pointers are virtual if (memptr.adj & 1) is nonzero
396 ///  - the this-adjustment is (memptr.adj >> 1)
397 ///  - the virtual offset is (memptr.ptr)
398 /// ARM uses 'adj' for the virtual flag because Thumb functions
399 /// may be only single-byte aligned.
400 ///
401 /// If the member is virtual, the adjusted 'this' pointer points
402 /// to a vtable pointer from which the virtual offset is applied.
403 ///
404 /// If the member is non-virtual, memptr.ptr is the address of
405 /// the function to call.
406 llvm::Value *ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(
407     CodeGenFunction &CGF, const Expr *E, llvm::Value *&This,
408     llvm::Value *MemFnPtr, const MemberPointerType *MPT) {
409   CGBuilderTy &Builder = CGF.Builder;
410 
411   const FunctionProtoType *FPT =
412     MPT->getPointeeType()->getAs<FunctionProtoType>();
413   const CXXRecordDecl *RD =
414     cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
415 
416   llvm::FunctionType *FTy =
417     CGM.getTypes().GetFunctionType(
418       CGM.getTypes().arrangeCXXMethodType(RD, FPT));
419 
420   llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(CGM.PtrDiffTy, 1);
421 
422   llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual");
423   llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual");
424   llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end");
425 
426   // Extract memptr.adj, which is in the second field.
427   llvm::Value *RawAdj = Builder.CreateExtractValue(MemFnPtr, 1, "memptr.adj");
428 
429   // Compute the true adjustment.
430   llvm::Value *Adj = RawAdj;
431   if (UseARMMethodPtrABI)
432     Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted");
433 
434   // Apply the adjustment and cast back to the original struct type
435   // for consistency.
436   llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy());
437   Ptr = Builder.CreateInBoundsGEP(Ptr, Adj);
438   This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted");
439 
440   // Load the function pointer.
441   llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr");
442 
443   // If the LSB in the function pointer is 1, the function pointer points to
444   // a virtual function.
445   llvm::Value *IsVirtual;
446   if (UseARMMethodPtrABI)
447     IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1);
448   else
449     IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1);
450   IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual");
451   Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
452 
453   // In the virtual path, the adjustment left 'This' pointing to the
454   // vtable of the correct base subobject.  The "function pointer" is an
455   // offset within the vtable (+1 for the virtual flag on non-ARM).
456   CGF.EmitBlock(FnVirtual);
457 
458   // Cast the adjusted this to a pointer to vtable pointer and load.
459   llvm::Type *VTableTy = Builder.getInt8PtrTy();
460   llvm::Value *VTable = CGF.GetVTablePtr(This, VTableTy);
461 
462   // Apply the offset.
463   llvm::Value *VTableOffset = FnAsInt;
464   if (!UseARMMethodPtrABI)
465     VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1);
466   VTable = Builder.CreateGEP(VTable, VTableOffset);
467 
468   // Load the virtual function to call.
469   VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo());
470   llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "memptr.virtualfn");
471   CGF.EmitBranch(FnEnd);
472 
473   // In the non-virtual path, the function pointer is actually a
474   // function pointer.
475   CGF.EmitBlock(FnNonVirtual);
476   llvm::Value *NonVirtualFn =
477     Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn");
478 
479   // We're done.
480   CGF.EmitBlock(FnEnd);
481   llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo(), 2);
482   Callee->addIncoming(VirtualFn, FnVirtual);
483   Callee->addIncoming(NonVirtualFn, FnNonVirtual);
484   return Callee;
485 }
486 
487 /// Compute an l-value by applying the given pointer-to-member to a
488 /// base object.
489 llvm::Value *ItaniumCXXABI::EmitMemberDataPointerAddress(
490     CodeGenFunction &CGF, const Expr *E, llvm::Value *Base, llvm::Value *MemPtr,
491     const MemberPointerType *MPT) {
492   assert(MemPtr->getType() == CGM.PtrDiffTy);
493 
494   CGBuilderTy &Builder = CGF.Builder;
495 
496   unsigned AS = Base->getType()->getPointerAddressSpace();
497 
498   // Cast to char*.
499   Base = Builder.CreateBitCast(Base, Builder.getInt8Ty()->getPointerTo(AS));
500 
501   // Apply the offset, which we assume is non-null.
502   llvm::Value *Addr = Builder.CreateInBoundsGEP(Base, MemPtr, "memptr.offset");
503 
504   // Cast the address to the appropriate pointer type, adopting the
505   // address space of the base pointer.
506   llvm::Type *PType
507     = CGF.ConvertTypeForMem(MPT->getPointeeType())->getPointerTo(AS);
508   return Builder.CreateBitCast(Addr, PType);
509 }
510 
511 /// Perform a bitcast, derived-to-base, or base-to-derived member pointer
512 /// conversion.
513 ///
514 /// Bitcast conversions are always a no-op under Itanium.
515 ///
516 /// Obligatory offset/adjustment diagram:
517 ///         <-- offset -->          <-- adjustment -->
518 ///   |--------------------------|----------------------|--------------------|
519 ///   ^Derived address point     ^Base address point    ^Member address point
520 ///
521 /// So when converting a base member pointer to a derived member pointer,
522 /// we add the offset to the adjustment because the address point has
523 /// decreased;  and conversely, when converting a derived MP to a base MP
524 /// we subtract the offset from the adjustment because the address point
525 /// has increased.
526 ///
527 /// The standard forbids (at compile time) conversion to and from
528 /// virtual bases, which is why we don't have to consider them here.
529 ///
530 /// The standard forbids (at run time) casting a derived MP to a base
531 /// MP when the derived MP does not point to a member of the base.
532 /// This is why -1 is a reasonable choice for null data member
533 /// pointers.
534 llvm::Value *
535 ItaniumCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF,
536                                            const CastExpr *E,
537                                            llvm::Value *src) {
538   assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
539          E->getCastKind() == CK_BaseToDerivedMemberPointer ||
540          E->getCastKind() == CK_ReinterpretMemberPointer);
541 
542   // Under Itanium, reinterprets don't require any additional processing.
543   if (E->getCastKind() == CK_ReinterpretMemberPointer) return src;
544 
545   // Use constant emission if we can.
546   if (isa<llvm::Constant>(src))
547     return EmitMemberPointerConversion(E, cast<llvm::Constant>(src));
548 
549   llvm::Constant *adj = getMemberPointerAdjustment(E);
550   if (!adj) return src;
551 
552   CGBuilderTy &Builder = CGF.Builder;
553   bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
554 
555   const MemberPointerType *destTy =
556     E->getType()->castAs<MemberPointerType>();
557 
558   // For member data pointers, this is just a matter of adding the
559   // offset if the source is non-null.
560   if (destTy->isMemberDataPointer()) {
561     llvm::Value *dst;
562     if (isDerivedToBase)
563       dst = Builder.CreateNSWSub(src, adj, "adj");
564     else
565       dst = Builder.CreateNSWAdd(src, adj, "adj");
566 
567     // Null check.
568     llvm::Value *null = llvm::Constant::getAllOnesValue(src->getType());
569     llvm::Value *isNull = Builder.CreateICmpEQ(src, null, "memptr.isnull");
570     return Builder.CreateSelect(isNull, src, dst);
571   }
572 
573   // The this-adjustment is left-shifted by 1 on ARM.
574   if (UseARMMethodPtrABI) {
575     uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue();
576     offset <<= 1;
577     adj = llvm::ConstantInt::get(adj->getType(), offset);
578   }
579 
580   llvm::Value *srcAdj = Builder.CreateExtractValue(src, 1, "src.adj");
581   llvm::Value *dstAdj;
582   if (isDerivedToBase)
583     dstAdj = Builder.CreateNSWSub(srcAdj, adj, "adj");
584   else
585     dstAdj = Builder.CreateNSWAdd(srcAdj, adj, "adj");
586 
587   return Builder.CreateInsertValue(src, dstAdj, 1);
588 }
589 
590 llvm::Constant *
591 ItaniumCXXABI::EmitMemberPointerConversion(const CastExpr *E,
592                                            llvm::Constant *src) {
593   assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
594          E->getCastKind() == CK_BaseToDerivedMemberPointer ||
595          E->getCastKind() == CK_ReinterpretMemberPointer);
596 
597   // Under Itanium, reinterprets don't require any additional processing.
598   if (E->getCastKind() == CK_ReinterpretMemberPointer) return src;
599 
600   // If the adjustment is trivial, we don't need to do anything.
601   llvm::Constant *adj = getMemberPointerAdjustment(E);
602   if (!adj) return src;
603 
604   bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
605 
606   const MemberPointerType *destTy =
607     E->getType()->castAs<MemberPointerType>();
608 
609   // For member data pointers, this is just a matter of adding the
610   // offset if the source is non-null.
611   if (destTy->isMemberDataPointer()) {
612     // null maps to null.
613     if (src->isAllOnesValue()) return src;
614 
615     if (isDerivedToBase)
616       return llvm::ConstantExpr::getNSWSub(src, adj);
617     else
618       return llvm::ConstantExpr::getNSWAdd(src, adj);
619   }
620 
621   // The this-adjustment is left-shifted by 1 on ARM.
622   if (UseARMMethodPtrABI) {
623     uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue();
624     offset <<= 1;
625     adj = llvm::ConstantInt::get(adj->getType(), offset);
626   }
627 
628   llvm::Constant *srcAdj = llvm::ConstantExpr::getExtractValue(src, 1);
629   llvm::Constant *dstAdj;
630   if (isDerivedToBase)
631     dstAdj = llvm::ConstantExpr::getNSWSub(srcAdj, adj);
632   else
633     dstAdj = llvm::ConstantExpr::getNSWAdd(srcAdj, adj);
634 
635   return llvm::ConstantExpr::getInsertValue(src, dstAdj, 1);
636 }
637 
638 llvm::Constant *
639 ItaniumCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) {
640   // Itanium C++ ABI 2.3:
641   //   A NULL pointer is represented as -1.
642   if (MPT->isMemberDataPointer())
643     return llvm::ConstantInt::get(CGM.PtrDiffTy, -1ULL, /*isSigned=*/true);
644 
645   llvm::Constant *Zero = llvm::ConstantInt::get(CGM.PtrDiffTy, 0);
646   llvm::Constant *Values[2] = { Zero, Zero };
647   return llvm::ConstantStruct::getAnon(Values);
648 }
649 
650 llvm::Constant *
651 ItaniumCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT,
652                                      CharUnits offset) {
653   // Itanium C++ ABI 2.3:
654   //   A pointer to data member is an offset from the base address of
655   //   the class object containing it, represented as a ptrdiff_t
656   return llvm::ConstantInt::get(CGM.PtrDiffTy, offset.getQuantity());
657 }
658 
659 llvm::Constant *
660 ItaniumCXXABI::EmitMemberFunctionPointer(const CXXMethodDecl *MD) {
661   return BuildMemberPointer(MD, CharUnits::Zero());
662 }
663 
664 llvm::Constant *ItaniumCXXABI::BuildMemberPointer(const CXXMethodDecl *MD,
665                                                   CharUnits ThisAdjustment) {
666   assert(MD->isInstance() && "Member function must not be static!");
667   MD = MD->getCanonicalDecl();
668 
669   CodeGenTypes &Types = CGM.getTypes();
670 
671   // Get the function pointer (or index if this is a virtual function).
672   llvm::Constant *MemPtr[2];
673   if (MD->isVirtual()) {
674     uint64_t Index = CGM.getItaniumVTableContext().getMethodVTableIndex(MD);
675 
676     const ASTContext &Context = getContext();
677     CharUnits PointerWidth =
678       Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
679     uint64_t VTableOffset = (Index * PointerWidth.getQuantity());
680 
681     if (UseARMMethodPtrABI) {
682       // ARM C++ ABI 3.2.1:
683       //   This ABI specifies that adj contains twice the this
684       //   adjustment, plus 1 if the member function is virtual. The
685       //   least significant bit of adj then makes exactly the same
686       //   discrimination as the least significant bit of ptr does for
687       //   Itanium.
688       MemPtr[0] = llvm::ConstantInt::get(CGM.PtrDiffTy, VTableOffset);
689       MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy,
690                                          2 * ThisAdjustment.getQuantity() + 1);
691     } else {
692       // Itanium C++ ABI 2.3:
693       //   For a virtual function, [the pointer field] is 1 plus the
694       //   virtual table offset (in bytes) of the function,
695       //   represented as a ptrdiff_t.
696       MemPtr[0] = llvm::ConstantInt::get(CGM.PtrDiffTy, VTableOffset + 1);
697       MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy,
698                                          ThisAdjustment.getQuantity());
699     }
700   } else {
701     const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
702     llvm::Type *Ty;
703     // Check whether the function has a computable LLVM signature.
704     if (Types.isFuncTypeConvertible(FPT)) {
705       // The function has a computable LLVM signature; use the correct type.
706       Ty = Types.GetFunctionType(Types.arrangeCXXMethodDeclaration(MD));
707     } else {
708       // Use an arbitrary non-function type to tell GetAddrOfFunction that the
709       // function type is incomplete.
710       Ty = CGM.PtrDiffTy;
711     }
712     llvm::Constant *addr = CGM.GetAddrOfFunction(MD, Ty);
713 
714     MemPtr[0] = llvm::ConstantExpr::getPtrToInt(addr, CGM.PtrDiffTy);
715     MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy,
716                                        (UseARMMethodPtrABI ? 2 : 1) *
717                                        ThisAdjustment.getQuantity());
718   }
719 
720   return llvm::ConstantStruct::getAnon(MemPtr);
721 }
722 
723 llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const APValue &MP,
724                                                  QualType MPType) {
725   const MemberPointerType *MPT = MPType->castAs<MemberPointerType>();
726   const ValueDecl *MPD = MP.getMemberPointerDecl();
727   if (!MPD)
728     return EmitNullMemberPointer(MPT);
729 
730   CharUnits ThisAdjustment = getMemberPointerPathAdjustment(MP);
731 
732   if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MPD))
733     return BuildMemberPointer(MD, ThisAdjustment);
734 
735   CharUnits FieldOffset =
736     getContext().toCharUnitsFromBits(getContext().getFieldOffset(MPD));
737   return EmitMemberDataPointer(MPT, ThisAdjustment + FieldOffset);
738 }
739 
740 /// The comparison algorithm is pretty easy: the member pointers are
741 /// the same if they're either bitwise identical *or* both null.
742 ///
743 /// ARM is different here only because null-ness is more complicated.
744 llvm::Value *
745 ItaniumCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF,
746                                            llvm::Value *L,
747                                            llvm::Value *R,
748                                            const MemberPointerType *MPT,
749                                            bool Inequality) {
750   CGBuilderTy &Builder = CGF.Builder;
751 
752   llvm::ICmpInst::Predicate Eq;
753   llvm::Instruction::BinaryOps And, Or;
754   if (Inequality) {
755     Eq = llvm::ICmpInst::ICMP_NE;
756     And = llvm::Instruction::Or;
757     Or = llvm::Instruction::And;
758   } else {
759     Eq = llvm::ICmpInst::ICMP_EQ;
760     And = llvm::Instruction::And;
761     Or = llvm::Instruction::Or;
762   }
763 
764   // Member data pointers are easy because there's a unique null
765   // value, so it just comes down to bitwise equality.
766   if (MPT->isMemberDataPointer())
767     return Builder.CreateICmp(Eq, L, R);
768 
769   // For member function pointers, the tautologies are more complex.
770   // The Itanium tautology is:
771   //   (L == R) <==> (L.ptr == R.ptr && (L.ptr == 0 || L.adj == R.adj))
772   // The ARM tautology is:
773   //   (L == R) <==> (L.ptr == R.ptr &&
774   //                  (L.adj == R.adj ||
775   //                   (L.ptr == 0 && ((L.adj|R.adj) & 1) == 0)))
776   // The inequality tautologies have exactly the same structure, except
777   // applying De Morgan's laws.
778 
779   llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr");
780   llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr");
781 
782   // This condition tests whether L.ptr == R.ptr.  This must always be
783   // true for equality to hold.
784   llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr");
785 
786   // This condition, together with the assumption that L.ptr == R.ptr,
787   // tests whether the pointers are both null.  ARM imposes an extra
788   // condition.
789   llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType());
790   llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null");
791 
792   // This condition tests whether L.adj == R.adj.  If this isn't
793   // true, the pointers are unequal unless they're both null.
794   llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj");
795   llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj");
796   llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj");
797 
798   // Null member function pointers on ARM clear the low bit of Adj,
799   // so the zero condition has to check that neither low bit is set.
800   if (UseARMMethodPtrABI) {
801     llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1);
802 
803     // Compute (l.adj | r.adj) & 1 and test it against zero.
804     llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj");
805     llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One);
806     llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero,
807                                                       "cmp.or.adj");
808     EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero);
809   }
810 
811   // Tie together all our conditions.
812   llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq);
813   Result = Builder.CreateBinOp(And, PtrEq, Result,
814                                Inequality ? "memptr.ne" : "memptr.eq");
815   return Result;
816 }
817 
818 llvm::Value *
819 ItaniumCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
820                                           llvm::Value *MemPtr,
821                                           const MemberPointerType *MPT) {
822   CGBuilderTy &Builder = CGF.Builder;
823 
824   /// For member data pointers, this is just a check against -1.
825   if (MPT->isMemberDataPointer()) {
826     assert(MemPtr->getType() == CGM.PtrDiffTy);
827     llvm::Value *NegativeOne =
828       llvm::Constant::getAllOnesValue(MemPtr->getType());
829     return Builder.CreateICmpNE(MemPtr, NegativeOne, "memptr.tobool");
830   }
831 
832   // In Itanium, a member function pointer is not null if 'ptr' is not null.
833   llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr");
834 
835   llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0);
836   llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool");
837 
838   // On ARM, a member function pointer is also non-null if the low bit of 'adj'
839   // (the virtual bit) is set.
840   if (UseARMMethodPtrABI) {
841     llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1);
842     llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj");
843     llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit");
844     llvm::Value *IsVirtual = Builder.CreateICmpNE(VirtualBit, Zero,
845                                                   "memptr.isvirtual");
846     Result = Builder.CreateOr(Result, IsVirtual);
847   }
848 
849   return Result;
850 }
851 
852 bool ItaniumCXXABI::classifyReturnType(CGFunctionInfo &FI) const {
853   const CXXRecordDecl *RD = FI.getReturnType()->getAsCXXRecordDecl();
854   if (!RD)
855     return false;
856 
857   // Return indirectly if we have a non-trivial copy ctor or non-trivial dtor.
858   // FIXME: Use canCopyArgument() when it is fixed to handle lazily declared
859   // special members.
860   if (RD->hasNonTrivialDestructor() || RD->hasNonTrivialCopyConstructor()) {
861     FI.getReturnInfo() = ABIArgInfo::getIndirect(0, /*ByVal=*/false);
862     return true;
863   }
864   return false;
865 }
866 
867 /// The Itanium ABI requires non-zero initialization only for data
868 /// member pointers, for which '0' is a valid offset.
869 bool ItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
870   return MPT->isMemberFunctionPointer();
871 }
872 
873 /// The Itanium ABI always places an offset to the complete object
874 /// at entry -2 in the vtable.
875 void ItaniumCXXABI::emitVirtualObjectDelete(CodeGenFunction &CGF,
876                                             const CXXDeleteExpr *DE,
877                                             llvm::Value *Ptr,
878                                             QualType ElementType,
879                                             const CXXDestructorDecl *Dtor) {
880   bool UseGlobalDelete = DE->isGlobalDelete();
881   if (UseGlobalDelete) {
882     // Derive the complete-object pointer, which is what we need
883     // to pass to the deallocation function.
884 
885     // Grab the vtable pointer as an intptr_t*.
886     llvm::Value *VTable = CGF.GetVTablePtr(Ptr, CGF.IntPtrTy->getPointerTo());
887 
888     // Track back to entry -2 and pull out the offset there.
889     llvm::Value *OffsetPtr = CGF.Builder.CreateConstInBoundsGEP1_64(
890         VTable, -2, "complete-offset.ptr");
891     llvm::LoadInst *Offset = CGF.Builder.CreateLoad(OffsetPtr);
892     Offset->setAlignment(CGF.PointerAlignInBytes);
893 
894     // Apply the offset.
895     llvm::Value *CompletePtr = CGF.Builder.CreateBitCast(Ptr, CGF.Int8PtrTy);
896     CompletePtr = CGF.Builder.CreateInBoundsGEP(CompletePtr, Offset);
897 
898     // If we're supposed to call the global delete, make sure we do so
899     // even if the destructor throws.
900     CGF.pushCallObjectDeleteCleanup(DE->getOperatorDelete(), CompletePtr,
901                                     ElementType);
902   }
903 
904   // FIXME: Provide a source location here even though there's no
905   // CXXMemberCallExpr for dtor call.
906   CXXDtorType DtorType = UseGlobalDelete ? Dtor_Complete : Dtor_Deleting;
907   EmitVirtualDestructorCall(CGF, Dtor, DtorType, Ptr, /*CE=*/nullptr);
908 
909   if (UseGlobalDelete)
910     CGF.PopCleanupBlock();
911 }
912 
913 void ItaniumCXXABI::emitRethrow(CodeGenFunction &CGF, bool isNoReturn) {
914   // void __cxa_rethrow();
915 
916   llvm::FunctionType *FTy =
917     llvm::FunctionType::get(CGM.VoidTy, /*IsVarArgs=*/false);
918 
919   llvm::Constant *Fn = CGM.CreateRuntimeFunction(FTy, "__cxa_rethrow");
920 
921   if (isNoReturn)
922     CGF.EmitNoreturnRuntimeCallOrInvoke(Fn, None);
923   else
924     CGF.EmitRuntimeCallOrInvoke(Fn);
925 }
926 
927 static llvm::Constant *getAllocateExceptionFn(CodeGenModule &CGM) {
928   // void *__cxa_allocate_exception(size_t thrown_size);
929 
930   llvm::FunctionType *FTy =
931     llvm::FunctionType::get(CGM.Int8PtrTy, CGM.SizeTy, /*IsVarArgs=*/false);
932 
933   return CGM.CreateRuntimeFunction(FTy, "__cxa_allocate_exception");
934 }
935 
936 static llvm::Constant *getThrowFn(CodeGenModule &CGM) {
937   // void __cxa_throw(void *thrown_exception, std::type_info *tinfo,
938   //                  void (*dest) (void *));
939 
940   llvm::Type *Args[3] = { CGM.Int8PtrTy, CGM.Int8PtrTy, CGM.Int8PtrTy };
941   llvm::FunctionType *FTy =
942     llvm::FunctionType::get(CGM.VoidTy, Args, /*IsVarArgs=*/false);
943 
944   return CGM.CreateRuntimeFunction(FTy, "__cxa_throw");
945 }
946 
947 void ItaniumCXXABI::emitThrow(CodeGenFunction &CGF, const CXXThrowExpr *E) {
948   QualType ThrowType = E->getSubExpr()->getType();
949   // Now allocate the exception object.
950   llvm::Type *SizeTy = CGF.ConvertType(getContext().getSizeType());
951   uint64_t TypeSize = getContext().getTypeSizeInChars(ThrowType).getQuantity();
952 
953   llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(CGM);
954   llvm::CallInst *ExceptionPtr = CGF.EmitNounwindRuntimeCall(
955       AllocExceptionFn, llvm::ConstantInt::get(SizeTy, TypeSize), "exception");
956 
957   CGF.EmitAnyExprToExn(E->getSubExpr(), ExceptionPtr);
958 
959   // Now throw the exception.
960   llvm::Constant *TypeInfo = CGM.GetAddrOfRTTIDescriptor(ThrowType,
961                                                          /*ForEH=*/true);
962 
963   // The address of the destructor.  If the exception type has a
964   // trivial destructor (or isn't a record), we just pass null.
965   llvm::Constant *Dtor = nullptr;
966   if (const RecordType *RecordTy = ThrowType->getAs<RecordType>()) {
967     CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl());
968     if (!Record->hasTrivialDestructor()) {
969       CXXDestructorDecl *DtorD = Record->getDestructor();
970       Dtor = CGM.getAddrOfCXXStructor(DtorD, StructorType::Complete);
971       Dtor = llvm::ConstantExpr::getBitCast(Dtor, CGM.Int8PtrTy);
972     }
973   }
974   if (!Dtor) Dtor = llvm::Constant::getNullValue(CGM.Int8PtrTy);
975 
976   llvm::Value *args[] = { ExceptionPtr, TypeInfo, Dtor };
977   CGF.EmitNoreturnRuntimeCallOrInvoke(getThrowFn(CGM), args);
978 }
979 
980 static llvm::Constant *getItaniumDynamicCastFn(CodeGenFunction &CGF) {
981   // void *__dynamic_cast(const void *sub,
982   //                      const abi::__class_type_info *src,
983   //                      const abi::__class_type_info *dst,
984   //                      std::ptrdiff_t src2dst_offset);
985 
986   llvm::Type *Int8PtrTy = CGF.Int8PtrTy;
987   llvm::Type *PtrDiffTy =
988     CGF.ConvertType(CGF.getContext().getPointerDiffType());
989 
990   llvm::Type *Args[4] = { Int8PtrTy, Int8PtrTy, Int8PtrTy, PtrDiffTy };
991 
992   llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false);
993 
994   // Mark the function as nounwind readonly.
995   llvm::Attribute::AttrKind FuncAttrs[] = { llvm::Attribute::NoUnwind,
996                                             llvm::Attribute::ReadOnly };
997   llvm::AttributeSet Attrs = llvm::AttributeSet::get(
998       CGF.getLLVMContext(), llvm::AttributeSet::FunctionIndex, FuncAttrs);
999 
1000   return CGF.CGM.CreateRuntimeFunction(FTy, "__dynamic_cast", Attrs);
1001 }
1002 
1003 static llvm::Constant *getBadCastFn(CodeGenFunction &CGF) {
1004   // void __cxa_bad_cast();
1005   llvm::FunctionType *FTy = llvm::FunctionType::get(CGF.VoidTy, false);
1006   return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_cast");
1007 }
1008 
1009 /// \brief Compute the src2dst_offset hint as described in the
1010 /// Itanium C++ ABI [2.9.7]
1011 static CharUnits computeOffsetHint(ASTContext &Context,
1012                                    const CXXRecordDecl *Src,
1013                                    const CXXRecordDecl *Dst) {
1014   CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
1015                      /*DetectVirtual=*/false);
1016 
1017   // If Dst is not derived from Src we can skip the whole computation below and
1018   // return that Src is not a public base of Dst.  Record all inheritance paths.
1019   if (!Dst->isDerivedFrom(Src, Paths))
1020     return CharUnits::fromQuantity(-2ULL);
1021 
1022   unsigned NumPublicPaths = 0;
1023   CharUnits Offset;
1024 
1025   // Now walk all possible inheritance paths.
1026   for (CXXBasePaths::paths_iterator I = Paths.begin(), E = Paths.end(); I != E;
1027        ++I) {
1028     if (I->Access != AS_public) // Ignore non-public inheritance.
1029       continue;
1030 
1031     ++NumPublicPaths;
1032 
1033     for (CXXBasePath::iterator J = I->begin(), JE = I->end(); J != JE; ++J) {
1034       // If the path contains a virtual base class we can't give any hint.
1035       // -1: no hint.
1036       if (J->Base->isVirtual())
1037         return CharUnits::fromQuantity(-1ULL);
1038 
1039       if (NumPublicPaths > 1) // Won't use offsets, skip computation.
1040         continue;
1041 
1042       // Accumulate the base class offsets.
1043       const ASTRecordLayout &L = Context.getASTRecordLayout(J->Class);
1044       Offset += L.getBaseClassOffset(J->Base->getType()->getAsCXXRecordDecl());
1045     }
1046   }
1047 
1048   // -2: Src is not a public base of Dst.
1049   if (NumPublicPaths == 0)
1050     return CharUnits::fromQuantity(-2ULL);
1051 
1052   // -3: Src is a multiple public base type but never a virtual base type.
1053   if (NumPublicPaths > 1)
1054     return CharUnits::fromQuantity(-3ULL);
1055 
1056   // Otherwise, the Src type is a unique public nonvirtual base type of Dst.
1057   // Return the offset of Src from the origin of Dst.
1058   return Offset;
1059 }
1060 
1061 static llvm::Constant *getBadTypeidFn(CodeGenFunction &CGF) {
1062   // void __cxa_bad_typeid();
1063   llvm::FunctionType *FTy = llvm::FunctionType::get(CGF.VoidTy, false);
1064 
1065   return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_typeid");
1066 }
1067 
1068 bool ItaniumCXXABI::shouldTypeidBeNullChecked(bool IsDeref,
1069                                               QualType SrcRecordTy) {
1070   return IsDeref;
1071 }
1072 
1073 void ItaniumCXXABI::EmitBadTypeidCall(CodeGenFunction &CGF) {
1074   llvm::Value *Fn = getBadTypeidFn(CGF);
1075   CGF.EmitRuntimeCallOrInvoke(Fn).setDoesNotReturn();
1076   CGF.Builder.CreateUnreachable();
1077 }
1078 
1079 llvm::Value *ItaniumCXXABI::EmitTypeid(CodeGenFunction &CGF,
1080                                        QualType SrcRecordTy,
1081                                        llvm::Value *ThisPtr,
1082                                        llvm::Type *StdTypeInfoPtrTy) {
1083   llvm::Value *Value =
1084       CGF.GetVTablePtr(ThisPtr, StdTypeInfoPtrTy->getPointerTo());
1085 
1086   // Load the type info.
1087   Value = CGF.Builder.CreateConstInBoundsGEP1_64(Value, -1ULL);
1088   return CGF.Builder.CreateLoad(Value);
1089 }
1090 
1091 bool ItaniumCXXABI::shouldDynamicCastCallBeNullChecked(bool SrcIsPtr,
1092                                                        QualType SrcRecordTy) {
1093   return SrcIsPtr;
1094 }
1095 
1096 llvm::Value *ItaniumCXXABI::EmitDynamicCastCall(
1097     CodeGenFunction &CGF, llvm::Value *Value, QualType SrcRecordTy,
1098     QualType DestTy, QualType DestRecordTy, llvm::BasicBlock *CastEnd) {
1099   llvm::Type *PtrDiffLTy =
1100       CGF.ConvertType(CGF.getContext().getPointerDiffType());
1101   llvm::Type *DestLTy = CGF.ConvertType(DestTy);
1102 
1103   llvm::Value *SrcRTTI =
1104       CGF.CGM.GetAddrOfRTTIDescriptor(SrcRecordTy.getUnqualifiedType());
1105   llvm::Value *DestRTTI =
1106       CGF.CGM.GetAddrOfRTTIDescriptor(DestRecordTy.getUnqualifiedType());
1107 
1108   // Compute the offset hint.
1109   const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl();
1110   const CXXRecordDecl *DestDecl = DestRecordTy->getAsCXXRecordDecl();
1111   llvm::Value *OffsetHint = llvm::ConstantInt::get(
1112       PtrDiffLTy,
1113       computeOffsetHint(CGF.getContext(), SrcDecl, DestDecl).getQuantity());
1114 
1115   // Emit the call to __dynamic_cast.
1116   Value = CGF.EmitCastToVoidPtr(Value);
1117 
1118   llvm::Value *args[] = {Value, SrcRTTI, DestRTTI, OffsetHint};
1119   Value = CGF.EmitNounwindRuntimeCall(getItaniumDynamicCastFn(CGF), args);
1120   Value = CGF.Builder.CreateBitCast(Value, DestLTy);
1121 
1122   /// C++ [expr.dynamic.cast]p9:
1123   ///   A failed cast to reference type throws std::bad_cast
1124   if (DestTy->isReferenceType()) {
1125     llvm::BasicBlock *BadCastBlock =
1126         CGF.createBasicBlock("dynamic_cast.bad_cast");
1127 
1128     llvm::Value *IsNull = CGF.Builder.CreateIsNull(Value);
1129     CGF.Builder.CreateCondBr(IsNull, BadCastBlock, CastEnd);
1130 
1131     CGF.EmitBlock(BadCastBlock);
1132     EmitBadCastCall(CGF);
1133   }
1134 
1135   return Value;
1136 }
1137 
1138 llvm::Value *ItaniumCXXABI::EmitDynamicCastToVoid(CodeGenFunction &CGF,
1139                                                   llvm::Value *Value,
1140                                                   QualType SrcRecordTy,
1141                                                   QualType DestTy) {
1142   llvm::Type *PtrDiffLTy =
1143       CGF.ConvertType(CGF.getContext().getPointerDiffType());
1144   llvm::Type *DestLTy = CGF.ConvertType(DestTy);
1145 
1146   // Get the vtable pointer.
1147   llvm::Value *VTable = CGF.GetVTablePtr(Value, PtrDiffLTy->getPointerTo());
1148 
1149   // Get the offset-to-top from the vtable.
1150   llvm::Value *OffsetToTop =
1151       CGF.Builder.CreateConstInBoundsGEP1_64(VTable, -2ULL);
1152   OffsetToTop = CGF.Builder.CreateLoad(OffsetToTop, "offset.to.top");
1153 
1154   // Finally, add the offset to the pointer.
1155   Value = CGF.EmitCastToVoidPtr(Value);
1156   Value = CGF.Builder.CreateInBoundsGEP(Value, OffsetToTop);
1157 
1158   return CGF.Builder.CreateBitCast(Value, DestLTy);
1159 }
1160 
1161 bool ItaniumCXXABI::EmitBadCastCall(CodeGenFunction &CGF) {
1162   llvm::Value *Fn = getBadCastFn(CGF);
1163   CGF.EmitRuntimeCallOrInvoke(Fn).setDoesNotReturn();
1164   CGF.Builder.CreateUnreachable();
1165   return true;
1166 }
1167 
1168 llvm::Value *
1169 ItaniumCXXABI::GetVirtualBaseClassOffset(CodeGenFunction &CGF,
1170                                          llvm::Value *This,
1171                                          const CXXRecordDecl *ClassDecl,
1172                                          const CXXRecordDecl *BaseClassDecl) {
1173   llvm::Value *VTablePtr = CGF.GetVTablePtr(This, CGM.Int8PtrTy);
1174   CharUnits VBaseOffsetOffset =
1175       CGM.getItaniumVTableContext().getVirtualBaseOffsetOffset(ClassDecl,
1176                                                                BaseClassDecl);
1177 
1178   llvm::Value *VBaseOffsetPtr =
1179     CGF.Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetOffset.getQuantity(),
1180                                    "vbase.offset.ptr");
1181   VBaseOffsetPtr = CGF.Builder.CreateBitCast(VBaseOffsetPtr,
1182                                              CGM.PtrDiffTy->getPointerTo());
1183 
1184   llvm::Value *VBaseOffset =
1185     CGF.Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset");
1186 
1187   return VBaseOffset;
1188 }
1189 
1190 void ItaniumCXXABI::EmitCXXConstructors(const CXXConstructorDecl *D) {
1191   // Just make sure we're in sync with TargetCXXABI.
1192   assert(CGM.getTarget().getCXXABI().hasConstructorVariants());
1193 
1194   // The constructor used for constructing this as a base class;
1195   // ignores virtual bases.
1196   CGM.EmitGlobal(GlobalDecl(D, Ctor_Base));
1197 
1198   // The constructor used for constructing this as a complete class;
1199   // constructs the virtual bases, then calls the base constructor.
1200   if (!D->getParent()->isAbstract()) {
1201     // We don't need to emit the complete ctor if the class is abstract.
1202     CGM.EmitGlobal(GlobalDecl(D, Ctor_Complete));
1203   }
1204 }
1205 
1206 void
1207 ItaniumCXXABI::buildStructorSignature(const CXXMethodDecl *MD, StructorType T,
1208                                       SmallVectorImpl<CanQualType> &ArgTys) {
1209   ASTContext &Context = getContext();
1210 
1211   // All parameters are already in place except VTT, which goes after 'this'.
1212   // These are Clang types, so we don't need to worry about sret yet.
1213 
1214   // Check if we need to add a VTT parameter (which has type void **).
1215   if (T == StructorType::Base && MD->getParent()->getNumVBases() != 0)
1216     ArgTys.insert(ArgTys.begin() + 1,
1217                   Context.getPointerType(Context.VoidPtrTy));
1218 }
1219 
1220 void ItaniumCXXABI::EmitCXXDestructors(const CXXDestructorDecl *D) {
1221   // The destructor used for destructing this as a base class; ignores
1222   // virtual bases.
1223   CGM.EmitGlobal(GlobalDecl(D, Dtor_Base));
1224 
1225   // The destructor used for destructing this as a most-derived class;
1226   // call the base destructor and then destructs any virtual bases.
1227   CGM.EmitGlobal(GlobalDecl(D, Dtor_Complete));
1228 
1229   // The destructor in a virtual table is always a 'deleting'
1230   // destructor, which calls the complete destructor and then uses the
1231   // appropriate operator delete.
1232   if (D->isVirtual())
1233     CGM.EmitGlobal(GlobalDecl(D, Dtor_Deleting));
1234 }
1235 
1236 void ItaniumCXXABI::addImplicitStructorParams(CodeGenFunction &CGF,
1237                                               QualType &ResTy,
1238                                               FunctionArgList &Params) {
1239   const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl());
1240   assert(isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD));
1241 
1242   // Check if we need a VTT parameter as well.
1243   if (NeedsVTTParameter(CGF.CurGD)) {
1244     ASTContext &Context = getContext();
1245 
1246     // FIXME: avoid the fake decl
1247     QualType T = Context.getPointerType(Context.VoidPtrTy);
1248     ImplicitParamDecl *VTTDecl
1249       = ImplicitParamDecl::Create(Context, nullptr, MD->getLocation(),
1250                                   &Context.Idents.get("vtt"), T);
1251     Params.insert(Params.begin() + 1, VTTDecl);
1252     getStructorImplicitParamDecl(CGF) = VTTDecl;
1253   }
1254 }
1255 
1256 void ItaniumCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
1257   /// Initialize the 'this' slot.
1258   EmitThisParam(CGF);
1259 
1260   /// Initialize the 'vtt' slot if needed.
1261   if (getStructorImplicitParamDecl(CGF)) {
1262     getStructorImplicitParamValue(CGF) = CGF.Builder.CreateLoad(
1263         CGF.GetAddrOfLocalVar(getStructorImplicitParamDecl(CGF)), "vtt");
1264   }
1265 
1266   /// If this is a function that the ABI specifies returns 'this', initialize
1267   /// the return slot to 'this' at the start of the function.
1268   ///
1269   /// Unlike the setting of return types, this is done within the ABI
1270   /// implementation instead of by clients of CGCXXABI because:
1271   /// 1) getThisValue is currently protected
1272   /// 2) in theory, an ABI could implement 'this' returns some other way;
1273   ///    HasThisReturn only specifies a contract, not the implementation
1274   if (HasThisReturn(CGF.CurGD))
1275     CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue);
1276 }
1277 
1278 unsigned ItaniumCXXABI::addImplicitConstructorArgs(
1279     CodeGenFunction &CGF, const CXXConstructorDecl *D, CXXCtorType Type,
1280     bool ForVirtualBase, bool Delegating, CallArgList &Args) {
1281   if (!NeedsVTTParameter(GlobalDecl(D, Type)))
1282     return 0;
1283 
1284   // Insert the implicit 'vtt' argument as the second argument.
1285   llvm::Value *VTT =
1286       CGF.GetVTTParameter(GlobalDecl(D, Type), ForVirtualBase, Delegating);
1287   QualType VTTTy = getContext().getPointerType(getContext().VoidPtrTy);
1288   Args.insert(Args.begin() + 1,
1289               CallArg(RValue::get(VTT), VTTTy, /*needscopy=*/false));
1290   return 1;  // Added one arg.
1291 }
1292 
1293 void ItaniumCXXABI::EmitDestructorCall(CodeGenFunction &CGF,
1294                                        const CXXDestructorDecl *DD,
1295                                        CXXDtorType Type, bool ForVirtualBase,
1296                                        bool Delegating, llvm::Value *This) {
1297   GlobalDecl GD(DD, Type);
1298   llvm::Value *VTT = CGF.GetVTTParameter(GD, ForVirtualBase, Delegating);
1299   QualType VTTTy = getContext().getPointerType(getContext().VoidPtrTy);
1300 
1301   llvm::Value *Callee = nullptr;
1302   if (getContext().getLangOpts().AppleKext)
1303     Callee = CGF.BuildAppleKextVirtualDestructorCall(DD, Type, DD->getParent());
1304 
1305   if (!Callee)
1306     Callee = CGM.getAddrOfCXXStructor(DD, getFromDtorType(Type));
1307 
1308   CGF.EmitCXXMemberOrOperatorCall(DD, Callee, ReturnValueSlot(), This, VTT,
1309                                   VTTTy, nullptr);
1310 }
1311 
1312 void ItaniumCXXABI::emitVTableDefinitions(CodeGenVTables &CGVT,
1313                                           const CXXRecordDecl *RD) {
1314   llvm::GlobalVariable *VTable = getAddrOfVTable(RD, CharUnits());
1315   if (VTable->hasInitializer())
1316     return;
1317 
1318   ItaniumVTableContext &VTContext = CGM.getItaniumVTableContext();
1319   const VTableLayout &VTLayout = VTContext.getVTableLayout(RD);
1320   llvm::GlobalVariable::LinkageTypes Linkage = CGM.getVTableLinkage(RD);
1321   llvm::Constant *RTTI =
1322       CGM.GetAddrOfRTTIDescriptor(CGM.getContext().getTagDeclType(RD));
1323 
1324   // Create and set the initializer.
1325   llvm::Constant *Init = CGVT.CreateVTableInitializer(
1326       RD, VTLayout.vtable_component_begin(), VTLayout.getNumVTableComponents(),
1327       VTLayout.vtable_thunk_begin(), VTLayout.getNumVTableThunks(), RTTI);
1328   VTable->setInitializer(Init);
1329 
1330   // Set the correct linkage.
1331   VTable->setLinkage(Linkage);
1332 
1333   if (CGM.supportsCOMDAT() && VTable->isWeakForLinker())
1334     VTable->setComdat(CGM.getModule().getOrInsertComdat(VTable->getName()));
1335 
1336   // Set the right visibility.
1337   CGM.setGlobalVisibility(VTable, RD);
1338 
1339   // Use pointer alignment for the vtable. Otherwise we would align them based
1340   // on the size of the initializer which doesn't make sense as only single
1341   // values are read.
1342   unsigned PAlign = CGM.getTarget().getPointerAlign(0);
1343   VTable->setAlignment(getContext().toCharUnitsFromBits(PAlign).getQuantity());
1344 
1345   // If this is the magic class __cxxabiv1::__fundamental_type_info,
1346   // we will emit the typeinfo for the fundamental types. This is the
1347   // same behaviour as GCC.
1348   const DeclContext *DC = RD->getDeclContext();
1349   if (RD->getIdentifier() &&
1350       RD->getIdentifier()->isStr("__fundamental_type_info") &&
1351       isa<NamespaceDecl>(DC) && cast<NamespaceDecl>(DC)->getIdentifier() &&
1352       cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__cxxabiv1") &&
1353       DC->getParent()->isTranslationUnit())
1354     EmitFundamentalRTTIDescriptors();
1355 
1356   CGM.EmitVTableBitSetEntries(VTable, VTLayout);
1357 }
1358 
1359 llvm::Value *ItaniumCXXABI::getVTableAddressPointInStructor(
1360     CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, BaseSubobject Base,
1361     const CXXRecordDecl *NearestVBase, bool &NeedsVirtualOffset) {
1362   bool NeedsVTTParam = CGM.getCXXABI().NeedsVTTParameter(CGF.CurGD);
1363   NeedsVirtualOffset = (NeedsVTTParam && NearestVBase);
1364 
1365   llvm::Value *VTableAddressPoint;
1366   if (NeedsVTTParam && (Base.getBase()->getNumVBases() || NearestVBase)) {
1367     // Get the secondary vpointer index.
1368     uint64_t VirtualPointerIndex =
1369         CGM.getVTables().getSecondaryVirtualPointerIndex(VTableClass, Base);
1370 
1371     /// Load the VTT.
1372     llvm::Value *VTT = CGF.LoadCXXVTT();
1373     if (VirtualPointerIndex)
1374       VTT = CGF.Builder.CreateConstInBoundsGEP1_64(VTT, VirtualPointerIndex);
1375 
1376     // And load the address point from the VTT.
1377     VTableAddressPoint = CGF.Builder.CreateLoad(VTT);
1378   } else {
1379     llvm::Constant *VTable =
1380         CGM.getCXXABI().getAddrOfVTable(VTableClass, CharUnits());
1381     uint64_t AddressPoint = CGM.getItaniumVTableContext()
1382                                 .getVTableLayout(VTableClass)
1383                                 .getAddressPoint(Base);
1384     VTableAddressPoint =
1385         CGF.Builder.CreateConstInBoundsGEP2_64(VTable, 0, AddressPoint);
1386   }
1387 
1388   return VTableAddressPoint;
1389 }
1390 
1391 llvm::Constant *ItaniumCXXABI::getVTableAddressPointForConstExpr(
1392     BaseSubobject Base, const CXXRecordDecl *VTableClass) {
1393   auto *VTable = getAddrOfVTable(VTableClass, CharUnits());
1394 
1395   // Find the appropriate vtable within the vtable group.
1396   uint64_t AddressPoint = CGM.getItaniumVTableContext()
1397                               .getVTableLayout(VTableClass)
1398                               .getAddressPoint(Base);
1399   llvm::Value *Indices[] = {
1400     llvm::ConstantInt::get(CGM.Int64Ty, 0),
1401     llvm::ConstantInt::get(CGM.Int64Ty, AddressPoint)
1402   };
1403 
1404   return llvm::ConstantExpr::getInBoundsGetElementPtr(VTable->getValueType(),
1405                                                       VTable, Indices);
1406 }
1407 
1408 llvm::GlobalVariable *ItaniumCXXABI::getAddrOfVTable(const CXXRecordDecl *RD,
1409                                                      CharUnits VPtrOffset) {
1410   assert(VPtrOffset.isZero() && "Itanium ABI only supports zero vptr offsets");
1411 
1412   llvm::GlobalVariable *&VTable = VTables[RD];
1413   if (VTable)
1414     return VTable;
1415 
1416   // Queue up this v-table for possible deferred emission.
1417   CGM.addDeferredVTable(RD);
1418 
1419   SmallString<256> OutName;
1420   llvm::raw_svector_ostream Out(OutName);
1421   getMangleContext().mangleCXXVTable(RD, Out);
1422   Out.flush();
1423   StringRef Name = OutName.str();
1424 
1425   ItaniumVTableContext &VTContext = CGM.getItaniumVTableContext();
1426   llvm::ArrayType *ArrayType = llvm::ArrayType::get(
1427       CGM.Int8PtrTy, VTContext.getVTableLayout(RD).getNumVTableComponents());
1428 
1429   VTable = CGM.CreateOrReplaceCXXRuntimeVariable(
1430       Name, ArrayType, llvm::GlobalValue::ExternalLinkage);
1431   VTable->setUnnamedAddr(true);
1432 
1433   if (RD->hasAttr<DLLImportAttr>())
1434     VTable->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
1435   else if (RD->hasAttr<DLLExportAttr>())
1436     VTable->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
1437 
1438   return VTable;
1439 }
1440 
1441 llvm::Value *ItaniumCXXABI::getVirtualFunctionPointer(CodeGenFunction &CGF,
1442                                                       GlobalDecl GD,
1443                                                       llvm::Value *This,
1444                                                       llvm::Type *Ty,
1445                                                       SourceLocation Loc) {
1446   GD = GD.getCanonicalDecl();
1447   Ty = Ty->getPointerTo()->getPointerTo();
1448   llvm::Value *VTable = CGF.GetVTablePtr(This, Ty);
1449 
1450   if (CGF.SanOpts.has(SanitizerKind::CFIVCall))
1451     CGF.EmitVTablePtrCheckForCall(cast<CXXMethodDecl>(GD.getDecl()), VTable,
1452                                   CodeGenFunction::CFITCK_VCall, Loc);
1453 
1454   uint64_t VTableIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(GD);
1455   llvm::Value *VFuncPtr =
1456       CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfn");
1457   return CGF.Builder.CreateLoad(VFuncPtr);
1458 }
1459 
1460 llvm::Value *ItaniumCXXABI::EmitVirtualDestructorCall(
1461     CodeGenFunction &CGF, const CXXDestructorDecl *Dtor, CXXDtorType DtorType,
1462     llvm::Value *This, const CXXMemberCallExpr *CE) {
1463   assert(CE == nullptr || CE->arg_begin() == CE->arg_end());
1464   assert(DtorType == Dtor_Deleting || DtorType == Dtor_Complete);
1465 
1466   const CGFunctionInfo *FInfo = &CGM.getTypes().arrangeCXXStructorDeclaration(
1467       Dtor, getFromDtorType(DtorType));
1468   llvm::Type *Ty = CGF.CGM.getTypes().GetFunctionType(*FInfo);
1469   llvm::Value *Callee =
1470       getVirtualFunctionPointer(CGF, GlobalDecl(Dtor, DtorType), This, Ty,
1471                                 CE ? CE->getLocStart() : SourceLocation());
1472 
1473   CGF.EmitCXXMemberOrOperatorCall(Dtor, Callee, ReturnValueSlot(), This,
1474                                   /*ImplicitParam=*/nullptr, QualType(), CE);
1475   return nullptr;
1476 }
1477 
1478 void ItaniumCXXABI::emitVirtualInheritanceTables(const CXXRecordDecl *RD) {
1479   CodeGenVTables &VTables = CGM.getVTables();
1480   llvm::GlobalVariable *VTT = VTables.GetAddrOfVTT(RD);
1481   VTables.EmitVTTDefinition(VTT, CGM.getVTableLinkage(RD), RD);
1482 }
1483 
1484 static llvm::Value *performTypeAdjustment(CodeGenFunction &CGF,
1485                                           llvm::Value *Ptr,
1486                                           int64_t NonVirtualAdjustment,
1487                                           int64_t VirtualAdjustment,
1488                                           bool IsReturnAdjustment) {
1489   if (!NonVirtualAdjustment && !VirtualAdjustment)
1490     return Ptr;
1491 
1492   llvm::Type *Int8PtrTy = CGF.Int8PtrTy;
1493   llvm::Value *V = CGF.Builder.CreateBitCast(Ptr, Int8PtrTy);
1494 
1495   if (NonVirtualAdjustment && !IsReturnAdjustment) {
1496     // Perform the non-virtual adjustment for a base-to-derived cast.
1497     V = CGF.Builder.CreateConstInBoundsGEP1_64(V, NonVirtualAdjustment);
1498   }
1499 
1500   if (VirtualAdjustment) {
1501     llvm::Type *PtrDiffTy =
1502         CGF.ConvertType(CGF.getContext().getPointerDiffType());
1503 
1504     // Perform the virtual adjustment.
1505     llvm::Value *VTablePtrPtr =
1506         CGF.Builder.CreateBitCast(V, Int8PtrTy->getPointerTo());
1507 
1508     llvm::Value *VTablePtr = CGF.Builder.CreateLoad(VTablePtrPtr);
1509 
1510     llvm::Value *OffsetPtr =
1511         CGF.Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment);
1512 
1513     OffsetPtr = CGF.Builder.CreateBitCast(OffsetPtr, PtrDiffTy->getPointerTo());
1514 
1515     // Load the adjustment offset from the vtable.
1516     llvm::Value *Offset = CGF.Builder.CreateLoad(OffsetPtr);
1517 
1518     // Adjust our pointer.
1519     V = CGF.Builder.CreateInBoundsGEP(V, Offset);
1520   }
1521 
1522   if (NonVirtualAdjustment && IsReturnAdjustment) {
1523     // Perform the non-virtual adjustment for a derived-to-base cast.
1524     V = CGF.Builder.CreateConstInBoundsGEP1_64(V, NonVirtualAdjustment);
1525   }
1526 
1527   // Cast back to the original type.
1528   return CGF.Builder.CreateBitCast(V, Ptr->getType());
1529 }
1530 
1531 llvm::Value *ItaniumCXXABI::performThisAdjustment(CodeGenFunction &CGF,
1532                                                   llvm::Value *This,
1533                                                   const ThisAdjustment &TA) {
1534   return performTypeAdjustment(CGF, This, TA.NonVirtual,
1535                                TA.Virtual.Itanium.VCallOffsetOffset,
1536                                /*IsReturnAdjustment=*/false);
1537 }
1538 
1539 llvm::Value *
1540 ItaniumCXXABI::performReturnAdjustment(CodeGenFunction &CGF, llvm::Value *Ret,
1541                                        const ReturnAdjustment &RA) {
1542   return performTypeAdjustment(CGF, Ret, RA.NonVirtual,
1543                                RA.Virtual.Itanium.VBaseOffsetOffset,
1544                                /*IsReturnAdjustment=*/true);
1545 }
1546 
1547 void ARMCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF,
1548                                     RValue RV, QualType ResultType) {
1549   if (!isa<CXXDestructorDecl>(CGF.CurGD.getDecl()))
1550     return ItaniumCXXABI::EmitReturnFromThunk(CGF, RV, ResultType);
1551 
1552   // Destructor thunks in the ARM ABI have indeterminate results.
1553   llvm::Type *T =
1554     cast<llvm::PointerType>(CGF.ReturnValue->getType())->getElementType();
1555   RValue Undef = RValue::get(llvm::UndefValue::get(T));
1556   return ItaniumCXXABI::EmitReturnFromThunk(CGF, Undef, ResultType);
1557 }
1558 
1559 /************************** Array allocation cookies **************************/
1560 
1561 CharUnits ItaniumCXXABI::getArrayCookieSizeImpl(QualType elementType) {
1562   // The array cookie is a size_t; pad that up to the element alignment.
1563   // The cookie is actually right-justified in that space.
1564   return std::max(CharUnits::fromQuantity(CGM.SizeSizeInBytes),
1565                   CGM.getContext().getTypeAlignInChars(elementType));
1566 }
1567 
1568 llvm::Value *ItaniumCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
1569                                                   llvm::Value *NewPtr,
1570                                                   llvm::Value *NumElements,
1571                                                   const CXXNewExpr *expr,
1572                                                   QualType ElementType) {
1573   assert(requiresArrayCookie(expr));
1574 
1575   unsigned AS = NewPtr->getType()->getPointerAddressSpace();
1576 
1577   ASTContext &Ctx = getContext();
1578   QualType SizeTy = Ctx.getSizeType();
1579   CharUnits SizeSize = Ctx.getTypeSizeInChars(SizeTy);
1580 
1581   // The size of the cookie.
1582   CharUnits CookieSize =
1583     std::max(SizeSize, Ctx.getTypeAlignInChars(ElementType));
1584   assert(CookieSize == getArrayCookieSizeImpl(ElementType));
1585 
1586   // Compute an offset to the cookie.
1587   llvm::Value *CookiePtr = NewPtr;
1588   CharUnits CookieOffset = CookieSize - SizeSize;
1589   if (!CookieOffset.isZero())
1590     CookiePtr = CGF.Builder.CreateConstInBoundsGEP1_64(CookiePtr,
1591                                                  CookieOffset.getQuantity());
1592 
1593   // Write the number of elements into the appropriate slot.
1594   llvm::Type *NumElementsTy = CGF.ConvertType(SizeTy)->getPointerTo(AS);
1595   llvm::Value *NumElementsPtr =
1596       CGF.Builder.CreateBitCast(CookiePtr, NumElementsTy);
1597   llvm::Instruction *SI = CGF.Builder.CreateStore(NumElements, NumElementsPtr);
1598   if (CGM.getLangOpts().Sanitize.has(SanitizerKind::Address) && AS == 0 &&
1599       expr->getOperatorNew()->isReplaceableGlobalAllocationFunction()) {
1600     // The store to the CookiePtr does not need to be instrumented.
1601     CGM.getSanitizerMetadata()->disableSanitizerForInstruction(SI);
1602     llvm::FunctionType *FTy =
1603         llvm::FunctionType::get(CGM.VoidTy, NumElementsTy, false);
1604     llvm::Constant *F =
1605         CGM.CreateRuntimeFunction(FTy, "__asan_poison_cxx_array_cookie");
1606     CGF.Builder.CreateCall(F, NumElementsPtr);
1607   }
1608 
1609   // Finally, compute a pointer to the actual data buffer by skipping
1610   // over the cookie completely.
1611   return CGF.Builder.CreateConstInBoundsGEP1_64(NewPtr,
1612                                                 CookieSize.getQuantity());
1613 }
1614 
1615 llvm::Value *ItaniumCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
1616                                                 llvm::Value *allocPtr,
1617                                                 CharUnits cookieSize) {
1618   // The element size is right-justified in the cookie.
1619   llvm::Value *numElementsPtr = allocPtr;
1620   CharUnits numElementsOffset =
1621     cookieSize - CharUnits::fromQuantity(CGF.SizeSizeInBytes);
1622   if (!numElementsOffset.isZero())
1623     numElementsPtr =
1624       CGF.Builder.CreateConstInBoundsGEP1_64(numElementsPtr,
1625                                              numElementsOffset.getQuantity());
1626 
1627   unsigned AS = allocPtr->getType()->getPointerAddressSpace();
1628   numElementsPtr =
1629     CGF.Builder.CreateBitCast(numElementsPtr, CGF.SizeTy->getPointerTo(AS));
1630   if (!CGM.getLangOpts().Sanitize.has(SanitizerKind::Address) || AS != 0)
1631     return CGF.Builder.CreateLoad(numElementsPtr);
1632   // In asan mode emit a function call instead of a regular load and let the
1633   // run-time deal with it: if the shadow is properly poisoned return the
1634   // cookie, otherwise return 0 to avoid an infinite loop calling DTORs.
1635   // We can't simply ignore this load using nosanitize metadata because
1636   // the metadata may be lost.
1637   llvm::FunctionType *FTy =
1638       llvm::FunctionType::get(CGF.SizeTy, CGF.SizeTy->getPointerTo(0), false);
1639   llvm::Constant *F =
1640       CGM.CreateRuntimeFunction(FTy, "__asan_load_cxx_array_cookie");
1641   return CGF.Builder.CreateCall(F, numElementsPtr);
1642 }
1643 
1644 CharUnits ARMCXXABI::getArrayCookieSizeImpl(QualType elementType) {
1645   // ARM says that the cookie is always:
1646   //   struct array_cookie {
1647   //     std::size_t element_size; // element_size != 0
1648   //     std::size_t element_count;
1649   //   };
1650   // But the base ABI doesn't give anything an alignment greater than
1651   // 8, so we can dismiss this as typical ABI-author blindness to
1652   // actual language complexity and round up to the element alignment.
1653   return std::max(CharUnits::fromQuantity(2 * CGM.SizeSizeInBytes),
1654                   CGM.getContext().getTypeAlignInChars(elementType));
1655 }
1656 
1657 llvm::Value *ARMCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
1658                                               llvm::Value *newPtr,
1659                                               llvm::Value *numElements,
1660                                               const CXXNewExpr *expr,
1661                                               QualType elementType) {
1662   assert(requiresArrayCookie(expr));
1663 
1664   // NewPtr is a char*, but we generalize to arbitrary addrspaces.
1665   unsigned AS = newPtr->getType()->getPointerAddressSpace();
1666 
1667   // The cookie is always at the start of the buffer.
1668   llvm::Value *cookie = newPtr;
1669 
1670   // The first element is the element size.
1671   cookie = CGF.Builder.CreateBitCast(cookie, CGF.SizeTy->getPointerTo(AS));
1672   llvm::Value *elementSize = llvm::ConstantInt::get(CGF.SizeTy,
1673                  getContext().getTypeSizeInChars(elementType).getQuantity());
1674   CGF.Builder.CreateStore(elementSize, cookie);
1675 
1676   // The second element is the element count.
1677   cookie = CGF.Builder.CreateConstInBoundsGEP1_32(CGF.SizeTy, cookie, 1);
1678   CGF.Builder.CreateStore(numElements, cookie);
1679 
1680   // Finally, compute a pointer to the actual data buffer by skipping
1681   // over the cookie completely.
1682   CharUnits cookieSize = ARMCXXABI::getArrayCookieSizeImpl(elementType);
1683   return CGF.Builder.CreateConstInBoundsGEP1_64(newPtr,
1684                                                 cookieSize.getQuantity());
1685 }
1686 
1687 llvm::Value *ARMCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
1688                                             llvm::Value *allocPtr,
1689                                             CharUnits cookieSize) {
1690   // The number of elements is at offset sizeof(size_t) relative to
1691   // the allocated pointer.
1692   llvm::Value *numElementsPtr
1693     = CGF.Builder.CreateConstInBoundsGEP1_64(allocPtr, CGF.SizeSizeInBytes);
1694 
1695   unsigned AS = allocPtr->getType()->getPointerAddressSpace();
1696   numElementsPtr =
1697     CGF.Builder.CreateBitCast(numElementsPtr, CGF.SizeTy->getPointerTo(AS));
1698   return CGF.Builder.CreateLoad(numElementsPtr);
1699 }
1700 
1701 /*********************** Static local initialization **************************/
1702 
1703 static llvm::Constant *getGuardAcquireFn(CodeGenModule &CGM,
1704                                          llvm::PointerType *GuardPtrTy) {
1705   // int __cxa_guard_acquire(__guard *guard_object);
1706   llvm::FunctionType *FTy =
1707     llvm::FunctionType::get(CGM.getTypes().ConvertType(CGM.getContext().IntTy),
1708                             GuardPtrTy, /*isVarArg=*/false);
1709   return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_acquire",
1710                                    llvm::AttributeSet::get(CGM.getLLVMContext(),
1711                                               llvm::AttributeSet::FunctionIndex,
1712                                                  llvm::Attribute::NoUnwind));
1713 }
1714 
1715 static llvm::Constant *getGuardReleaseFn(CodeGenModule &CGM,
1716                                          llvm::PointerType *GuardPtrTy) {
1717   // void __cxa_guard_release(__guard *guard_object);
1718   llvm::FunctionType *FTy =
1719     llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false);
1720   return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_release",
1721                                    llvm::AttributeSet::get(CGM.getLLVMContext(),
1722                                               llvm::AttributeSet::FunctionIndex,
1723                                                  llvm::Attribute::NoUnwind));
1724 }
1725 
1726 static llvm::Constant *getGuardAbortFn(CodeGenModule &CGM,
1727                                        llvm::PointerType *GuardPtrTy) {
1728   // void __cxa_guard_abort(__guard *guard_object);
1729   llvm::FunctionType *FTy =
1730     llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false);
1731   return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_abort",
1732                                    llvm::AttributeSet::get(CGM.getLLVMContext(),
1733                                               llvm::AttributeSet::FunctionIndex,
1734                                                  llvm::Attribute::NoUnwind));
1735 }
1736 
1737 namespace {
1738   struct CallGuardAbort : EHScopeStack::Cleanup {
1739     llvm::GlobalVariable *Guard;
1740     CallGuardAbort(llvm::GlobalVariable *Guard) : Guard(Guard) {}
1741 
1742     void Emit(CodeGenFunction &CGF, Flags flags) override {
1743       CGF.EmitNounwindRuntimeCall(getGuardAbortFn(CGF.CGM, Guard->getType()),
1744                                   Guard);
1745     }
1746   };
1747 }
1748 
1749 /// The ARM code here follows the Itanium code closely enough that we
1750 /// just special-case it at particular places.
1751 void ItaniumCXXABI::EmitGuardedInit(CodeGenFunction &CGF,
1752                                     const VarDecl &D,
1753                                     llvm::GlobalVariable *var,
1754                                     bool shouldPerformInit) {
1755   CGBuilderTy &Builder = CGF.Builder;
1756 
1757   // We only need to use thread-safe statics for local non-TLS variables;
1758   // global initialization is always single-threaded.
1759   bool threadsafe = getContext().getLangOpts().ThreadsafeStatics &&
1760                     D.isLocalVarDecl() && !D.getTLSKind();
1761 
1762   // If we have a global variable with internal linkage and thread-safe statics
1763   // are disabled, we can just let the guard variable be of type i8.
1764   bool useInt8GuardVariable = !threadsafe && var->hasInternalLinkage();
1765 
1766   llvm::IntegerType *guardTy;
1767   if (useInt8GuardVariable) {
1768     guardTy = CGF.Int8Ty;
1769   } else {
1770     // Guard variables are 64 bits in the generic ABI and size width on ARM
1771     // (i.e. 32-bit on AArch32, 64-bit on AArch64).
1772     guardTy = (UseARMGuardVarABI ? CGF.SizeTy : CGF.Int64Ty);
1773   }
1774   llvm::PointerType *guardPtrTy = guardTy->getPointerTo();
1775 
1776   // Create the guard variable if we don't already have it (as we
1777   // might if we're double-emitting this function body).
1778   llvm::GlobalVariable *guard = CGM.getStaticLocalDeclGuardAddress(&D);
1779   if (!guard) {
1780     // Mangle the name for the guard.
1781     SmallString<256> guardName;
1782     {
1783       llvm::raw_svector_ostream out(guardName);
1784       getMangleContext().mangleStaticGuardVariable(&D, out);
1785       out.flush();
1786     }
1787 
1788     // Create the guard variable with a zero-initializer.
1789     // Just absorb linkage and visibility from the guarded variable.
1790     guard = new llvm::GlobalVariable(CGM.getModule(), guardTy,
1791                                      false, var->getLinkage(),
1792                                      llvm::ConstantInt::get(guardTy, 0),
1793                                      guardName.str());
1794     guard->setVisibility(var->getVisibility());
1795     // If the variable is thread-local, so is its guard variable.
1796     guard->setThreadLocalMode(var->getThreadLocalMode());
1797 
1798     // The ABI says: It is suggested that it be emitted in the same COMDAT group
1799     // as the associated data object
1800     llvm::Comdat *C = var->getComdat();
1801     if (!D.isLocalVarDecl() && C) {
1802       guard->setComdat(C);
1803       CGF.CurFn->setComdat(C);
1804     } else if (CGM.supportsCOMDAT() && guard->isWeakForLinker()) {
1805       guard->setComdat(CGM.getModule().getOrInsertComdat(guard->getName()));
1806     }
1807 
1808     CGM.setStaticLocalDeclGuardAddress(&D, guard);
1809   }
1810 
1811   // Test whether the variable has completed initialization.
1812   //
1813   // Itanium C++ ABI 3.3.2:
1814   //   The following is pseudo-code showing how these functions can be used:
1815   //     if (obj_guard.first_byte == 0) {
1816   //       if ( __cxa_guard_acquire (&obj_guard) ) {
1817   //         try {
1818   //           ... initialize the object ...;
1819   //         } catch (...) {
1820   //            __cxa_guard_abort (&obj_guard);
1821   //            throw;
1822   //         }
1823   //         ... queue object destructor with __cxa_atexit() ...;
1824   //         __cxa_guard_release (&obj_guard);
1825   //       }
1826   //     }
1827 
1828   // Load the first byte of the guard variable.
1829   llvm::LoadInst *LI =
1830       Builder.CreateLoad(Builder.CreateBitCast(guard, CGM.Int8PtrTy));
1831   LI->setAlignment(1);
1832 
1833   // Itanium ABI:
1834   //   An implementation supporting thread-safety on multiprocessor
1835   //   systems must also guarantee that references to the initialized
1836   //   object do not occur before the load of the initialization flag.
1837   //
1838   // In LLVM, we do this by marking the load Acquire.
1839   if (threadsafe)
1840     LI->setAtomic(llvm::Acquire);
1841 
1842   // For ARM, we should only check the first bit, rather than the entire byte:
1843   //
1844   // ARM C++ ABI 3.2.3.1:
1845   //   To support the potential use of initialization guard variables
1846   //   as semaphores that are the target of ARM SWP and LDREX/STREX
1847   //   synchronizing instructions we define a static initialization
1848   //   guard variable to be a 4-byte aligned, 4-byte word with the
1849   //   following inline access protocol.
1850   //     #define INITIALIZED 1
1851   //     if ((obj_guard & INITIALIZED) != INITIALIZED) {
1852   //       if (__cxa_guard_acquire(&obj_guard))
1853   //         ...
1854   //     }
1855   //
1856   // and similarly for ARM64:
1857   //
1858   // ARM64 C++ ABI 3.2.2:
1859   //   This ABI instead only specifies the value bit 0 of the static guard
1860   //   variable; all other bits are platform defined. Bit 0 shall be 0 when the
1861   //   variable is not initialized and 1 when it is.
1862   llvm::Value *V =
1863       (UseARMGuardVarABI && !useInt8GuardVariable)
1864           ? Builder.CreateAnd(LI, llvm::ConstantInt::get(CGM.Int8Ty, 1))
1865           : LI;
1866   llvm::Value *isInitialized = Builder.CreateIsNull(V, "guard.uninitialized");
1867 
1868   llvm::BasicBlock *InitCheckBlock = CGF.createBasicBlock("init.check");
1869   llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end");
1870 
1871   // Check if the first byte of the guard variable is zero.
1872   Builder.CreateCondBr(isInitialized, InitCheckBlock, EndBlock);
1873 
1874   CGF.EmitBlock(InitCheckBlock);
1875 
1876   // Variables used when coping with thread-safe statics and exceptions.
1877   if (threadsafe) {
1878     // Call __cxa_guard_acquire.
1879     llvm::Value *V
1880       = CGF.EmitNounwindRuntimeCall(getGuardAcquireFn(CGM, guardPtrTy), guard);
1881 
1882     llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init");
1883 
1884     Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"),
1885                          InitBlock, EndBlock);
1886 
1887     // Call __cxa_guard_abort along the exceptional edge.
1888     CGF.EHStack.pushCleanup<CallGuardAbort>(EHCleanup, guard);
1889 
1890     CGF.EmitBlock(InitBlock);
1891   }
1892 
1893   // Emit the initializer and add a global destructor if appropriate.
1894   CGF.EmitCXXGlobalVarDeclInit(D, var, shouldPerformInit);
1895 
1896   if (threadsafe) {
1897     // Pop the guard-abort cleanup if we pushed one.
1898     CGF.PopCleanupBlock();
1899 
1900     // Call __cxa_guard_release.  This cannot throw.
1901     CGF.EmitNounwindRuntimeCall(getGuardReleaseFn(CGM, guardPtrTy), guard);
1902   } else {
1903     Builder.CreateStore(llvm::ConstantInt::get(guardTy, 1), guard);
1904   }
1905 
1906   CGF.EmitBlock(EndBlock);
1907 }
1908 
1909 /// Register a global destructor using __cxa_atexit.
1910 static void emitGlobalDtorWithCXAAtExit(CodeGenFunction &CGF,
1911                                         llvm::Constant *dtor,
1912                                         llvm::Constant *addr,
1913                                         bool TLS) {
1914   const char *Name = "__cxa_atexit";
1915   if (TLS) {
1916     const llvm::Triple &T = CGF.getTarget().getTriple();
1917     Name = T.isMacOSX() ?  "_tlv_atexit" : "__cxa_thread_atexit";
1918   }
1919 
1920   // We're assuming that the destructor function is something we can
1921   // reasonably call with the default CC.  Go ahead and cast it to the
1922   // right prototype.
1923   llvm::Type *dtorTy =
1924     llvm::FunctionType::get(CGF.VoidTy, CGF.Int8PtrTy, false)->getPointerTo();
1925 
1926   // extern "C" int __cxa_atexit(void (*f)(void *), void *p, void *d);
1927   llvm::Type *paramTys[] = { dtorTy, CGF.Int8PtrTy, CGF.Int8PtrTy };
1928   llvm::FunctionType *atexitTy =
1929     llvm::FunctionType::get(CGF.IntTy, paramTys, false);
1930 
1931   // Fetch the actual function.
1932   llvm::Constant *atexit = CGF.CGM.CreateRuntimeFunction(atexitTy, Name);
1933   if (llvm::Function *fn = dyn_cast<llvm::Function>(atexit))
1934     fn->setDoesNotThrow();
1935 
1936   // Create a variable that binds the atexit to this shared object.
1937   llvm::Constant *handle =
1938     CGF.CGM.CreateRuntimeVariable(CGF.Int8Ty, "__dso_handle");
1939 
1940   llvm::Value *args[] = {
1941     llvm::ConstantExpr::getBitCast(dtor, dtorTy),
1942     llvm::ConstantExpr::getBitCast(addr, CGF.Int8PtrTy),
1943     handle
1944   };
1945   CGF.EmitNounwindRuntimeCall(atexit, args);
1946 }
1947 
1948 /// Register a global destructor as best as we know how.
1949 void ItaniumCXXABI::registerGlobalDtor(CodeGenFunction &CGF,
1950                                        const VarDecl &D,
1951                                        llvm::Constant *dtor,
1952                                        llvm::Constant *addr) {
1953   // Use __cxa_atexit if available.
1954   if (CGM.getCodeGenOpts().CXAAtExit)
1955     return emitGlobalDtorWithCXAAtExit(CGF, dtor, addr, D.getTLSKind());
1956 
1957   if (D.getTLSKind())
1958     CGM.ErrorUnsupported(&D, "non-trivial TLS destruction");
1959 
1960   // In Apple kexts, we want to add a global destructor entry.
1961   // FIXME: shouldn't this be guarded by some variable?
1962   if (CGM.getLangOpts().AppleKext) {
1963     // Generate a global destructor entry.
1964     return CGM.AddCXXDtorEntry(dtor, addr);
1965   }
1966 
1967   CGF.registerGlobalDtorWithAtExit(D, dtor, addr);
1968 }
1969 
1970 static bool isThreadWrapperReplaceable(const VarDecl *VD,
1971                                        CodeGen::CodeGenModule &CGM) {
1972   assert(!VD->isStaticLocal() && "static local VarDecls don't need wrappers!");
1973   // OS X prefers to have references to thread local variables to go through
1974   // the thread wrapper instead of directly referencing the backing variable.
1975   return VD->getTLSKind() == VarDecl::TLS_Dynamic &&
1976          CGM.getTarget().getTriple().isMacOSX();
1977 }
1978 
1979 /// Get the appropriate linkage for the wrapper function. This is essentially
1980 /// the weak form of the variable's linkage; every translation unit which needs
1981 /// the wrapper emits a copy, and we want the linker to merge them.
1982 static llvm::GlobalValue::LinkageTypes
1983 getThreadLocalWrapperLinkage(const VarDecl *VD, CodeGen::CodeGenModule &CGM) {
1984   llvm::GlobalValue::LinkageTypes VarLinkage =
1985       CGM.getLLVMLinkageVarDefinition(VD, /*isConstant=*/false);
1986 
1987   // For internal linkage variables, we don't need an external or weak wrapper.
1988   if (llvm::GlobalValue::isLocalLinkage(VarLinkage))
1989     return VarLinkage;
1990 
1991   // If the thread wrapper is replaceable, give it appropriate linkage.
1992   if (isThreadWrapperReplaceable(VD, CGM)) {
1993     if (llvm::GlobalVariable::isLinkOnceLinkage(VarLinkage) ||
1994         llvm::GlobalVariable::isWeakODRLinkage(VarLinkage))
1995       return llvm::GlobalVariable::WeakAnyLinkage;
1996     return VarLinkage;
1997   }
1998   return llvm::GlobalValue::WeakODRLinkage;
1999 }
2000 
2001 llvm::Function *
2002 ItaniumCXXABI::getOrCreateThreadLocalWrapper(const VarDecl *VD,
2003                                              llvm::Value *Val) {
2004   // Mangle the name for the thread_local wrapper function.
2005   SmallString<256> WrapperName;
2006   {
2007     llvm::raw_svector_ostream Out(WrapperName);
2008     getMangleContext().mangleItaniumThreadLocalWrapper(VD, Out);
2009     Out.flush();
2010   }
2011 
2012   if (llvm::Value *V = CGM.getModule().getNamedValue(WrapperName))
2013     return cast<llvm::Function>(V);
2014 
2015   llvm::Type *RetTy = Val->getType();
2016   if (VD->getType()->isReferenceType())
2017     RetTy = RetTy->getPointerElementType();
2018 
2019   llvm::FunctionType *FnTy = llvm::FunctionType::get(RetTy, false);
2020   llvm::Function *Wrapper =
2021       llvm::Function::Create(FnTy, getThreadLocalWrapperLinkage(VD, CGM),
2022                              WrapperName.str(), &CGM.getModule());
2023   // Always resolve references to the wrapper at link time.
2024   if (!Wrapper->hasLocalLinkage() && !isThreadWrapperReplaceable(VD, CGM))
2025     Wrapper->setVisibility(llvm::GlobalValue::HiddenVisibility);
2026   return Wrapper;
2027 }
2028 
2029 void ItaniumCXXABI::EmitThreadLocalInitFuncs(
2030     CodeGenModule &CGM,
2031     ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *>>
2032         CXXThreadLocals, ArrayRef<llvm::Function *> CXXThreadLocalInits,
2033     ArrayRef<llvm::GlobalVariable *> CXXThreadLocalInitVars) {
2034   llvm::Function *InitFunc = nullptr;
2035   if (!CXXThreadLocalInits.empty()) {
2036     // Generate a guarded initialization function.
2037     llvm::FunctionType *FTy =
2038         llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
2039     InitFunc = CGM.CreateGlobalInitOrDestructFunction(FTy, "__tls_init",
2040                                                       SourceLocation(),
2041                                                       /*TLS=*/true);
2042     llvm::GlobalVariable *Guard = new llvm::GlobalVariable(
2043         CGM.getModule(), CGM.Int8Ty, /*isConstant=*/false,
2044         llvm::GlobalVariable::InternalLinkage,
2045         llvm::ConstantInt::get(CGM.Int8Ty, 0), "__tls_guard");
2046     Guard->setThreadLocal(true);
2047     CodeGenFunction(CGM)
2048         .GenerateCXXGlobalInitFunc(InitFunc, CXXThreadLocalInits, Guard);
2049   }
2050   for (unsigned I = 0, N = CXXThreadLocals.size(); I != N; ++I) {
2051     const VarDecl *VD = CXXThreadLocals[I].first;
2052     llvm::GlobalVariable *Var = CXXThreadLocals[I].second;
2053 
2054     // Some targets require that all access to thread local variables go through
2055     // the thread wrapper.  This means that we cannot attempt to create a thread
2056     // wrapper or a thread helper.
2057     if (isThreadWrapperReplaceable(VD, CGM) && !VD->hasDefinition())
2058       continue;
2059 
2060     // Mangle the name for the thread_local initialization function.
2061     SmallString<256> InitFnName;
2062     {
2063       llvm::raw_svector_ostream Out(InitFnName);
2064       getMangleContext().mangleItaniumThreadLocalInit(VD, Out);
2065       Out.flush();
2066     }
2067 
2068     // If we have a definition for the variable, emit the initialization
2069     // function as an alias to the global Init function (if any). Otherwise,
2070     // produce a declaration of the initialization function.
2071     llvm::GlobalValue *Init = nullptr;
2072     bool InitIsInitFunc = false;
2073     if (VD->hasDefinition()) {
2074       InitIsInitFunc = true;
2075       if (InitFunc)
2076         Init = llvm::GlobalAlias::create(Var->getLinkage(), InitFnName.str(),
2077                                          InitFunc);
2078     } else {
2079       // Emit a weak global function referring to the initialization function.
2080       // This function will not exist if the TU defining the thread_local
2081       // variable in question does not need any dynamic initialization for
2082       // its thread_local variables.
2083       llvm::FunctionType *FnTy = llvm::FunctionType::get(CGM.VoidTy, false);
2084       Init = llvm::Function::Create(
2085           FnTy, llvm::GlobalVariable::ExternalWeakLinkage, InitFnName.str(),
2086           &CGM.getModule());
2087     }
2088 
2089     if (Init)
2090       Init->setVisibility(Var->getVisibility());
2091 
2092     llvm::Function *Wrapper = getOrCreateThreadLocalWrapper(VD, Var);
2093     llvm::LLVMContext &Context = CGM.getModule().getContext();
2094     llvm::BasicBlock *Entry = llvm::BasicBlock::Create(Context, "", Wrapper);
2095     CGBuilderTy Builder(Entry);
2096     if (InitIsInitFunc) {
2097       if (Init)
2098         Builder.CreateCall(Init);
2099     } else {
2100       // Don't know whether we have an init function. Call it if it exists.
2101       llvm::Value *Have = Builder.CreateIsNotNull(Init);
2102       llvm::BasicBlock *InitBB = llvm::BasicBlock::Create(Context, "", Wrapper);
2103       llvm::BasicBlock *ExitBB = llvm::BasicBlock::Create(Context, "", Wrapper);
2104       Builder.CreateCondBr(Have, InitBB, ExitBB);
2105 
2106       Builder.SetInsertPoint(InitBB);
2107       Builder.CreateCall(Init);
2108       Builder.CreateBr(ExitBB);
2109 
2110       Builder.SetInsertPoint(ExitBB);
2111     }
2112 
2113     // For a reference, the result of the wrapper function is a pointer to
2114     // the referenced object.
2115     llvm::Value *Val = Var;
2116     if (VD->getType()->isReferenceType()) {
2117       llvm::LoadInst *LI = Builder.CreateLoad(Val);
2118       LI->setAlignment(CGM.getContext().getDeclAlign(VD).getQuantity());
2119       Val = LI;
2120     }
2121     if (Val->getType() != Wrapper->getReturnType())
2122       Val = Builder.CreatePointerBitCastOrAddrSpaceCast(
2123           Val, Wrapper->getReturnType(), "");
2124     Builder.CreateRet(Val);
2125   }
2126 }
2127 
2128 LValue ItaniumCXXABI::EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF,
2129                                                    const VarDecl *VD,
2130                                                    QualType LValType) {
2131   QualType T = VD->getType();
2132   llvm::Type *Ty = CGF.getTypes().ConvertTypeForMem(T);
2133   llvm::Value *Val = CGF.CGM.GetAddrOfGlobalVar(VD, Ty);
2134   llvm::Function *Wrapper = getOrCreateThreadLocalWrapper(VD, Val);
2135 
2136   Val = CGF.Builder.CreateCall(Wrapper);
2137 
2138   LValue LV;
2139   if (VD->getType()->isReferenceType())
2140     LV = CGF.MakeNaturalAlignAddrLValue(Val, LValType);
2141   else
2142     LV = CGF.MakeAddrLValue(Val, LValType, CGF.getContext().getDeclAlign(VD));
2143   // FIXME: need setObjCGCLValueClass?
2144   return LV;
2145 }
2146 
2147 /// Return whether the given global decl needs a VTT parameter, which it does
2148 /// if it's a base constructor or destructor with virtual bases.
2149 bool ItaniumCXXABI::NeedsVTTParameter(GlobalDecl GD) {
2150   const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
2151 
2152   // We don't have any virtual bases, just return early.
2153   if (!MD->getParent()->getNumVBases())
2154     return false;
2155 
2156   // Check if we have a base constructor.
2157   if (isa<CXXConstructorDecl>(MD) && GD.getCtorType() == Ctor_Base)
2158     return true;
2159 
2160   // Check if we have a base destructor.
2161   if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
2162     return true;
2163 
2164   return false;
2165 }
2166 
2167 namespace {
2168 class ItaniumRTTIBuilder {
2169   CodeGenModule &CGM;  // Per-module state.
2170   llvm::LLVMContext &VMContext;
2171   const ItaniumCXXABI &CXXABI;  // Per-module state.
2172 
2173   /// Fields - The fields of the RTTI descriptor currently being built.
2174   SmallVector<llvm::Constant *, 16> Fields;
2175 
2176   /// GetAddrOfTypeName - Returns the mangled type name of the given type.
2177   llvm::GlobalVariable *
2178   GetAddrOfTypeName(QualType Ty, llvm::GlobalVariable::LinkageTypes Linkage);
2179 
2180   /// GetAddrOfExternalRTTIDescriptor - Returns the constant for the RTTI
2181   /// descriptor of the given type.
2182   llvm::Constant *GetAddrOfExternalRTTIDescriptor(QualType Ty);
2183 
2184   /// BuildVTablePointer - Build the vtable pointer for the given type.
2185   void BuildVTablePointer(const Type *Ty);
2186 
2187   /// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
2188   /// inheritance, according to the Itanium C++ ABI, 2.9.5p6b.
2189   void BuildSIClassTypeInfo(const CXXRecordDecl *RD);
2190 
2191   /// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
2192   /// classes with bases that do not satisfy the abi::__si_class_type_info
2193   /// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
2194   void BuildVMIClassTypeInfo(const CXXRecordDecl *RD);
2195 
2196   /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct, used
2197   /// for pointer types.
2198   void BuildPointerTypeInfo(QualType PointeeTy);
2199 
2200   /// BuildObjCObjectTypeInfo - Build the appropriate kind of
2201   /// type_info for an object type.
2202   void BuildObjCObjectTypeInfo(const ObjCObjectType *Ty);
2203 
2204   /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
2205   /// struct, used for member pointer types.
2206   void BuildPointerToMemberTypeInfo(const MemberPointerType *Ty);
2207 
2208 public:
2209   ItaniumRTTIBuilder(const ItaniumCXXABI &ABI)
2210       : CGM(ABI.CGM), VMContext(CGM.getModule().getContext()), CXXABI(ABI) {}
2211 
2212   // Pointer type info flags.
2213   enum {
2214     /// PTI_Const - Type has const qualifier.
2215     PTI_Const = 0x1,
2216 
2217     /// PTI_Volatile - Type has volatile qualifier.
2218     PTI_Volatile = 0x2,
2219 
2220     /// PTI_Restrict - Type has restrict qualifier.
2221     PTI_Restrict = 0x4,
2222 
2223     /// PTI_Incomplete - Type is incomplete.
2224     PTI_Incomplete = 0x8,
2225 
2226     /// PTI_ContainingClassIncomplete - Containing class is incomplete.
2227     /// (in pointer to member).
2228     PTI_ContainingClassIncomplete = 0x10
2229   };
2230 
2231   // VMI type info flags.
2232   enum {
2233     /// VMI_NonDiamondRepeat - Class has non-diamond repeated inheritance.
2234     VMI_NonDiamondRepeat = 0x1,
2235 
2236     /// VMI_DiamondShaped - Class is diamond shaped.
2237     VMI_DiamondShaped = 0x2
2238   };
2239 
2240   // Base class type info flags.
2241   enum {
2242     /// BCTI_Virtual - Base class is virtual.
2243     BCTI_Virtual = 0x1,
2244 
2245     /// BCTI_Public - Base class is public.
2246     BCTI_Public = 0x2
2247   };
2248 
2249   /// BuildTypeInfo - Build the RTTI type info struct for the given type.
2250   ///
2251   /// \param Force - true to force the creation of this RTTI value
2252   llvm::Constant *BuildTypeInfo(QualType Ty, bool Force = false);
2253 };
2254 }
2255 
2256 llvm::GlobalVariable *ItaniumRTTIBuilder::GetAddrOfTypeName(
2257     QualType Ty, llvm::GlobalVariable::LinkageTypes Linkage) {
2258   SmallString<256> OutName;
2259   llvm::raw_svector_ostream Out(OutName);
2260   CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(Ty, Out);
2261   Out.flush();
2262   StringRef Name = OutName.str();
2263 
2264   // We know that the mangled name of the type starts at index 4 of the
2265   // mangled name of the typename, so we can just index into it in order to
2266   // get the mangled name of the type.
2267   llvm::Constant *Init = llvm::ConstantDataArray::getString(VMContext,
2268                                                             Name.substr(4));
2269 
2270   llvm::GlobalVariable *GV =
2271     CGM.CreateOrReplaceCXXRuntimeVariable(Name, Init->getType(), Linkage);
2272 
2273   GV->setInitializer(Init);
2274 
2275   return GV;
2276 }
2277 
2278 llvm::Constant *
2279 ItaniumRTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) {
2280   // Mangle the RTTI name.
2281   SmallString<256> OutName;
2282   llvm::raw_svector_ostream Out(OutName);
2283   CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out);
2284   Out.flush();
2285   StringRef Name = OutName.str();
2286 
2287   // Look for an existing global.
2288   llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name);
2289 
2290   if (!GV) {
2291     // Create a new global variable.
2292     GV = new llvm::GlobalVariable(CGM.getModule(), CGM.Int8PtrTy,
2293                                   /*Constant=*/true,
2294                                   llvm::GlobalValue::ExternalLinkage, nullptr,
2295                                   Name);
2296     if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
2297       const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
2298       if (RD->hasAttr<DLLImportAttr>())
2299         GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass);
2300     }
2301   }
2302 
2303   return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy);
2304 }
2305 
2306 /// TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type
2307 /// info for that type is defined in the standard library.
2308 static bool TypeInfoIsInStandardLibrary(const BuiltinType *Ty) {
2309   // Itanium C++ ABI 2.9.2:
2310   //   Basic type information (e.g. for "int", "bool", etc.) will be kept in
2311   //   the run-time support library. Specifically, the run-time support
2312   //   library should contain type_info objects for the types X, X* and
2313   //   X const*, for every X in: void, std::nullptr_t, bool, wchar_t, char,
2314   //   unsigned char, signed char, short, unsigned short, int, unsigned int,
2315   //   long, unsigned long, long long, unsigned long long, float, double,
2316   //   long double, char16_t, char32_t, and the IEEE 754r decimal and
2317   //   half-precision floating point types.
2318   switch (Ty->getKind()) {
2319     case BuiltinType::Void:
2320     case BuiltinType::NullPtr:
2321     case BuiltinType::Bool:
2322     case BuiltinType::WChar_S:
2323     case BuiltinType::WChar_U:
2324     case BuiltinType::Char_U:
2325     case BuiltinType::Char_S:
2326     case BuiltinType::UChar:
2327     case BuiltinType::SChar:
2328     case BuiltinType::Short:
2329     case BuiltinType::UShort:
2330     case BuiltinType::Int:
2331     case BuiltinType::UInt:
2332     case BuiltinType::Long:
2333     case BuiltinType::ULong:
2334     case BuiltinType::LongLong:
2335     case BuiltinType::ULongLong:
2336     case BuiltinType::Half:
2337     case BuiltinType::Float:
2338     case BuiltinType::Double:
2339     case BuiltinType::LongDouble:
2340     case BuiltinType::Char16:
2341     case BuiltinType::Char32:
2342     case BuiltinType::Int128:
2343     case BuiltinType::UInt128:
2344     case BuiltinType::OCLImage1d:
2345     case BuiltinType::OCLImage1dArray:
2346     case BuiltinType::OCLImage1dBuffer:
2347     case BuiltinType::OCLImage2d:
2348     case BuiltinType::OCLImage2dArray:
2349     case BuiltinType::OCLImage3d:
2350     case BuiltinType::OCLSampler:
2351     case BuiltinType::OCLEvent:
2352       return true;
2353 
2354     case BuiltinType::Dependent:
2355 #define BUILTIN_TYPE(Id, SingletonId)
2356 #define PLACEHOLDER_TYPE(Id, SingletonId) \
2357     case BuiltinType::Id:
2358 #include "clang/AST/BuiltinTypes.def"
2359       llvm_unreachable("asking for RRTI for a placeholder type!");
2360 
2361     case BuiltinType::ObjCId:
2362     case BuiltinType::ObjCClass:
2363     case BuiltinType::ObjCSel:
2364       llvm_unreachable("FIXME: Objective-C types are unsupported!");
2365   }
2366 
2367   llvm_unreachable("Invalid BuiltinType Kind!");
2368 }
2369 
2370 static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) {
2371   QualType PointeeTy = PointerTy->getPointeeType();
2372   const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy);
2373   if (!BuiltinTy)
2374     return false;
2375 
2376   // Check the qualifiers.
2377   Qualifiers Quals = PointeeTy.getQualifiers();
2378   Quals.removeConst();
2379 
2380   if (!Quals.empty())
2381     return false;
2382 
2383   return TypeInfoIsInStandardLibrary(BuiltinTy);
2384 }
2385 
2386 /// IsStandardLibraryRTTIDescriptor - Returns whether the type
2387 /// information for the given type exists in the standard library.
2388 static bool IsStandardLibraryRTTIDescriptor(QualType Ty) {
2389   // Type info for builtin types is defined in the standard library.
2390   if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty))
2391     return TypeInfoIsInStandardLibrary(BuiltinTy);
2392 
2393   // Type info for some pointer types to builtin types is defined in the
2394   // standard library.
2395   if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
2396     return TypeInfoIsInStandardLibrary(PointerTy);
2397 
2398   return false;
2399 }
2400 
2401 /// ShouldUseExternalRTTIDescriptor - Returns whether the type information for
2402 /// the given type exists somewhere else, and that we should not emit the type
2403 /// information in this translation unit.  Assumes that it is not a
2404 /// standard-library type.
2405 static bool ShouldUseExternalRTTIDescriptor(CodeGenModule &CGM,
2406                                             QualType Ty) {
2407   ASTContext &Context = CGM.getContext();
2408 
2409   // If RTTI is disabled, assume it might be disabled in the
2410   // translation unit that defines any potential key function, too.
2411   if (!Context.getLangOpts().RTTI) return false;
2412 
2413   if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
2414     const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
2415     if (!RD->hasDefinition())
2416       return false;
2417 
2418     if (!RD->isDynamicClass())
2419       return false;
2420 
2421     // FIXME: this may need to be reconsidered if the key function
2422     // changes.
2423     if (CGM.getVTables().isVTableExternal(RD))
2424       return true;
2425 
2426     if (RD->hasAttr<DLLImportAttr>())
2427       return true;
2428   }
2429 
2430   return false;
2431 }
2432 
2433 /// IsIncompleteClassType - Returns whether the given record type is incomplete.
2434 static bool IsIncompleteClassType(const RecordType *RecordTy) {
2435   return !RecordTy->getDecl()->isCompleteDefinition();
2436 }
2437 
2438 /// ContainsIncompleteClassType - Returns whether the given type contains an
2439 /// incomplete class type. This is true if
2440 ///
2441 ///   * The given type is an incomplete class type.
2442 ///   * The given type is a pointer type whose pointee type contains an
2443 ///     incomplete class type.
2444 ///   * The given type is a member pointer type whose class is an incomplete
2445 ///     class type.
2446 ///   * The given type is a member pointer type whoise pointee type contains an
2447 ///     incomplete class type.
2448 /// is an indirect or direct pointer to an incomplete class type.
2449 static bool ContainsIncompleteClassType(QualType Ty) {
2450   if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
2451     if (IsIncompleteClassType(RecordTy))
2452       return true;
2453   }
2454 
2455   if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
2456     return ContainsIncompleteClassType(PointerTy->getPointeeType());
2457 
2458   if (const MemberPointerType *MemberPointerTy =
2459       dyn_cast<MemberPointerType>(Ty)) {
2460     // Check if the class type is incomplete.
2461     const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass());
2462     if (IsIncompleteClassType(ClassType))
2463       return true;
2464 
2465     return ContainsIncompleteClassType(MemberPointerTy->getPointeeType());
2466   }
2467 
2468   return false;
2469 }
2470 
2471 // CanUseSingleInheritance - Return whether the given record decl has a "single,
2472 // public, non-virtual base at offset zero (i.e. the derived class is dynamic
2473 // iff the base is)", according to Itanium C++ ABI, 2.95p6b.
2474 static bool CanUseSingleInheritance(const CXXRecordDecl *RD) {
2475   // Check the number of bases.
2476   if (RD->getNumBases() != 1)
2477     return false;
2478 
2479   // Get the base.
2480   CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin();
2481 
2482   // Check that the base is not virtual.
2483   if (Base->isVirtual())
2484     return false;
2485 
2486   // Check that the base is public.
2487   if (Base->getAccessSpecifier() != AS_public)
2488     return false;
2489 
2490   // Check that the class is dynamic iff the base is.
2491   const CXXRecordDecl *BaseDecl =
2492     cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
2493   if (!BaseDecl->isEmpty() &&
2494       BaseDecl->isDynamicClass() != RD->isDynamicClass())
2495     return false;
2496 
2497   return true;
2498 }
2499 
2500 void ItaniumRTTIBuilder::BuildVTablePointer(const Type *Ty) {
2501   // abi::__class_type_info.
2502   static const char * const ClassTypeInfo =
2503     "_ZTVN10__cxxabiv117__class_type_infoE";
2504   // abi::__si_class_type_info.
2505   static const char * const SIClassTypeInfo =
2506     "_ZTVN10__cxxabiv120__si_class_type_infoE";
2507   // abi::__vmi_class_type_info.
2508   static const char * const VMIClassTypeInfo =
2509     "_ZTVN10__cxxabiv121__vmi_class_type_infoE";
2510 
2511   const char *VTableName = nullptr;
2512 
2513   switch (Ty->getTypeClass()) {
2514 #define TYPE(Class, Base)
2515 #define ABSTRACT_TYPE(Class, Base)
2516 #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
2517 #define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2518 #define DEPENDENT_TYPE(Class, Base) case Type::Class:
2519 #include "clang/AST/TypeNodes.def"
2520     llvm_unreachable("Non-canonical and dependent types shouldn't get here");
2521 
2522   case Type::LValueReference:
2523   case Type::RValueReference:
2524     llvm_unreachable("References shouldn't get here");
2525 
2526   case Type::Auto:
2527     llvm_unreachable("Undeduced auto type shouldn't get here");
2528 
2529   case Type::Builtin:
2530   // GCC treats vector and complex types as fundamental types.
2531   case Type::Vector:
2532   case Type::ExtVector:
2533   case Type::Complex:
2534   case Type::Atomic:
2535   // FIXME: GCC treats block pointers as fundamental types?!
2536   case Type::BlockPointer:
2537     // abi::__fundamental_type_info.
2538     VTableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE";
2539     break;
2540 
2541   case Type::ConstantArray:
2542   case Type::IncompleteArray:
2543   case Type::VariableArray:
2544     // abi::__array_type_info.
2545     VTableName = "_ZTVN10__cxxabiv117__array_type_infoE";
2546     break;
2547 
2548   case Type::FunctionNoProto:
2549   case Type::FunctionProto:
2550     // abi::__function_type_info.
2551     VTableName = "_ZTVN10__cxxabiv120__function_type_infoE";
2552     break;
2553 
2554   case Type::Enum:
2555     // abi::__enum_type_info.
2556     VTableName = "_ZTVN10__cxxabiv116__enum_type_infoE";
2557     break;
2558 
2559   case Type::Record: {
2560     const CXXRecordDecl *RD =
2561       cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
2562 
2563     if (!RD->hasDefinition() || !RD->getNumBases()) {
2564       VTableName = ClassTypeInfo;
2565     } else if (CanUseSingleInheritance(RD)) {
2566       VTableName = SIClassTypeInfo;
2567     } else {
2568       VTableName = VMIClassTypeInfo;
2569     }
2570 
2571     break;
2572   }
2573 
2574   case Type::ObjCObject:
2575     // Ignore protocol qualifiers.
2576     Ty = cast<ObjCObjectType>(Ty)->getBaseType().getTypePtr();
2577 
2578     // Handle id and Class.
2579     if (isa<BuiltinType>(Ty)) {
2580       VTableName = ClassTypeInfo;
2581       break;
2582     }
2583 
2584     assert(isa<ObjCInterfaceType>(Ty));
2585     // Fall through.
2586 
2587   case Type::ObjCInterface:
2588     if (cast<ObjCInterfaceType>(Ty)->getDecl()->getSuperClass()) {
2589       VTableName = SIClassTypeInfo;
2590     } else {
2591       VTableName = ClassTypeInfo;
2592     }
2593     break;
2594 
2595   case Type::ObjCObjectPointer:
2596   case Type::Pointer:
2597     // abi::__pointer_type_info.
2598     VTableName = "_ZTVN10__cxxabiv119__pointer_type_infoE";
2599     break;
2600 
2601   case Type::MemberPointer:
2602     // abi::__pointer_to_member_type_info.
2603     VTableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE";
2604     break;
2605   }
2606 
2607   llvm::Constant *VTable =
2608     CGM.getModule().getOrInsertGlobal(VTableName, CGM.Int8PtrTy);
2609 
2610   llvm::Type *PtrDiffTy =
2611     CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
2612 
2613   // The vtable address point is 2.
2614   llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
2615   VTable =
2616       llvm::ConstantExpr::getInBoundsGetElementPtr(CGM.Int8PtrTy, VTable, Two);
2617   VTable = llvm::ConstantExpr::getBitCast(VTable, CGM.Int8PtrTy);
2618 
2619   Fields.push_back(VTable);
2620 }
2621 
2622 /// \brief Return the linkage that the type info and type info name constants
2623 /// should have for the given type.
2624 static llvm::GlobalVariable::LinkageTypes getTypeInfoLinkage(CodeGenModule &CGM,
2625                                                              QualType Ty) {
2626   // Itanium C++ ABI 2.9.5p7:
2627   //   In addition, it and all of the intermediate abi::__pointer_type_info
2628   //   structs in the chain down to the abi::__class_type_info for the
2629   //   incomplete class type must be prevented from resolving to the
2630   //   corresponding type_info structs for the complete class type, possibly
2631   //   by making them local static objects. Finally, a dummy class RTTI is
2632   //   generated for the incomplete type that will not resolve to the final
2633   //   complete class RTTI (because the latter need not exist), possibly by
2634   //   making it a local static object.
2635   if (ContainsIncompleteClassType(Ty))
2636     return llvm::GlobalValue::InternalLinkage;
2637 
2638   switch (Ty->getLinkage()) {
2639   case NoLinkage:
2640   case InternalLinkage:
2641   case UniqueExternalLinkage:
2642     return llvm::GlobalValue::InternalLinkage;
2643 
2644   case VisibleNoLinkage:
2645   case ExternalLinkage:
2646     if (!CGM.getLangOpts().RTTI) {
2647       // RTTI is not enabled, which means that this type info struct is going
2648       // to be used for exception handling. Give it linkonce_odr linkage.
2649       return llvm::GlobalValue::LinkOnceODRLinkage;
2650     }
2651 
2652     if (const RecordType *Record = dyn_cast<RecordType>(Ty)) {
2653       const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl());
2654       if (RD->hasAttr<WeakAttr>())
2655         return llvm::GlobalValue::WeakODRLinkage;
2656       if (RD->isDynamicClass())
2657         return CGM.getVTableLinkage(RD);
2658     }
2659 
2660     return llvm::GlobalValue::LinkOnceODRLinkage;
2661   }
2662 
2663   llvm_unreachable("Invalid linkage!");
2664 }
2665 
2666 llvm::Constant *ItaniumRTTIBuilder::BuildTypeInfo(QualType Ty, bool Force) {
2667   // We want to operate on the canonical type.
2668   Ty = CGM.getContext().getCanonicalType(Ty);
2669 
2670   // Check if we've already emitted an RTTI descriptor for this type.
2671   SmallString<256> OutName;
2672   llvm::raw_svector_ostream Out(OutName);
2673   CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out);
2674   Out.flush();
2675   StringRef Name = OutName.str();
2676 
2677   llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
2678   if (OldGV && !OldGV->isDeclaration()) {
2679     assert(!OldGV->hasAvailableExternallyLinkage() &&
2680            "available_externally typeinfos not yet implemented");
2681 
2682     return llvm::ConstantExpr::getBitCast(OldGV, CGM.Int8PtrTy);
2683   }
2684 
2685   // Check if there is already an external RTTI descriptor for this type.
2686   bool IsStdLib = IsStandardLibraryRTTIDescriptor(Ty);
2687   if (!Force && (IsStdLib || ShouldUseExternalRTTIDescriptor(CGM, Ty)))
2688     return GetAddrOfExternalRTTIDescriptor(Ty);
2689 
2690   // Emit the standard library with external linkage.
2691   llvm::GlobalVariable::LinkageTypes Linkage;
2692   if (IsStdLib)
2693     Linkage = llvm::GlobalValue::ExternalLinkage;
2694   else
2695     Linkage = getTypeInfoLinkage(CGM, Ty);
2696 
2697   // Add the vtable pointer.
2698   BuildVTablePointer(cast<Type>(Ty));
2699 
2700   // And the name.
2701   llvm::GlobalVariable *TypeName = GetAddrOfTypeName(Ty, Linkage);
2702   llvm::Constant *TypeNameField;
2703 
2704   // If we're supposed to demote the visibility, be sure to set a flag
2705   // to use a string comparison for type_info comparisons.
2706   ItaniumCXXABI::RTTIUniquenessKind RTTIUniqueness =
2707       CXXABI.classifyRTTIUniqueness(Ty, Linkage);
2708   if (RTTIUniqueness != ItaniumCXXABI::RUK_Unique) {
2709     // The flag is the sign bit, which on ARM64 is defined to be clear
2710     // for global pointers.  This is very ARM64-specific.
2711     TypeNameField = llvm::ConstantExpr::getPtrToInt(TypeName, CGM.Int64Ty);
2712     llvm::Constant *flag =
2713         llvm::ConstantInt::get(CGM.Int64Ty, ((uint64_t)1) << 63);
2714     TypeNameField = llvm::ConstantExpr::getAdd(TypeNameField, flag);
2715     TypeNameField =
2716         llvm::ConstantExpr::getIntToPtr(TypeNameField, CGM.Int8PtrTy);
2717   } else {
2718     TypeNameField = llvm::ConstantExpr::getBitCast(TypeName, CGM.Int8PtrTy);
2719   }
2720   Fields.push_back(TypeNameField);
2721 
2722   switch (Ty->getTypeClass()) {
2723 #define TYPE(Class, Base)
2724 #define ABSTRACT_TYPE(Class, Base)
2725 #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
2726 #define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2727 #define DEPENDENT_TYPE(Class, Base) case Type::Class:
2728 #include "clang/AST/TypeNodes.def"
2729     llvm_unreachable("Non-canonical and dependent types shouldn't get here");
2730 
2731   // GCC treats vector types as fundamental types.
2732   case Type::Builtin:
2733   case Type::Vector:
2734   case Type::ExtVector:
2735   case Type::Complex:
2736   case Type::BlockPointer:
2737     // Itanium C++ ABI 2.9.5p4:
2738     // abi::__fundamental_type_info adds no data members to std::type_info.
2739     break;
2740 
2741   case Type::LValueReference:
2742   case Type::RValueReference:
2743     llvm_unreachable("References shouldn't get here");
2744 
2745   case Type::Auto:
2746     llvm_unreachable("Undeduced auto type shouldn't get here");
2747 
2748   case Type::ConstantArray:
2749   case Type::IncompleteArray:
2750   case Type::VariableArray:
2751     // Itanium C++ ABI 2.9.5p5:
2752     // abi::__array_type_info adds no data members to std::type_info.
2753     break;
2754 
2755   case Type::FunctionNoProto:
2756   case Type::FunctionProto:
2757     // Itanium C++ ABI 2.9.5p5:
2758     // abi::__function_type_info adds no data members to std::type_info.
2759     break;
2760 
2761   case Type::Enum:
2762     // Itanium C++ ABI 2.9.5p5:
2763     // abi::__enum_type_info adds no data members to std::type_info.
2764     break;
2765 
2766   case Type::Record: {
2767     const CXXRecordDecl *RD =
2768       cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
2769     if (!RD->hasDefinition() || !RD->getNumBases()) {
2770       // We don't need to emit any fields.
2771       break;
2772     }
2773 
2774     if (CanUseSingleInheritance(RD))
2775       BuildSIClassTypeInfo(RD);
2776     else
2777       BuildVMIClassTypeInfo(RD);
2778 
2779     break;
2780   }
2781 
2782   case Type::ObjCObject:
2783   case Type::ObjCInterface:
2784     BuildObjCObjectTypeInfo(cast<ObjCObjectType>(Ty));
2785     break;
2786 
2787   case Type::ObjCObjectPointer:
2788     BuildPointerTypeInfo(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
2789     break;
2790 
2791   case Type::Pointer:
2792     BuildPointerTypeInfo(cast<PointerType>(Ty)->getPointeeType());
2793     break;
2794 
2795   case Type::MemberPointer:
2796     BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
2797     break;
2798 
2799   case Type::Atomic:
2800     // No fields, at least for the moment.
2801     break;
2802   }
2803 
2804   llvm::Constant *Init = llvm::ConstantStruct::getAnon(Fields);
2805 
2806   llvm::Module &M = CGM.getModule();
2807   llvm::GlobalVariable *GV =
2808       new llvm::GlobalVariable(M, Init->getType(),
2809                                /*Constant=*/true, Linkage, Init, Name);
2810 
2811   if (CGM.supportsCOMDAT() && GV->isWeakForLinker())
2812     GV->setComdat(M.getOrInsertComdat(GV->getName()));
2813 
2814   // If there's already an old global variable, replace it with the new one.
2815   if (OldGV) {
2816     GV->takeName(OldGV);
2817     llvm::Constant *NewPtr =
2818       llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
2819     OldGV->replaceAllUsesWith(NewPtr);
2820     OldGV->eraseFromParent();
2821   }
2822 
2823   // The Itanium ABI specifies that type_info objects must be globally
2824   // unique, with one exception: if the type is an incomplete class
2825   // type or a (possibly indirect) pointer to one.  That exception
2826   // affects the general case of comparing type_info objects produced
2827   // by the typeid operator, which is why the comparison operators on
2828   // std::type_info generally use the type_info name pointers instead
2829   // of the object addresses.  However, the language's built-in uses
2830   // of RTTI generally require class types to be complete, even when
2831   // manipulating pointers to those class types.  This allows the
2832   // implementation of dynamic_cast to rely on address equality tests,
2833   // which is much faster.
2834 
2835   // All of this is to say that it's important that both the type_info
2836   // object and the type_info name be uniqued when weakly emitted.
2837 
2838   // Give the type_info object and name the formal visibility of the
2839   // type itself.
2840   llvm::GlobalValue::VisibilityTypes llvmVisibility;
2841   if (llvm::GlobalValue::isLocalLinkage(Linkage))
2842     // If the linkage is local, only default visibility makes sense.
2843     llvmVisibility = llvm::GlobalValue::DefaultVisibility;
2844   else if (RTTIUniqueness == ItaniumCXXABI::RUK_NonUniqueHidden)
2845     llvmVisibility = llvm::GlobalValue::HiddenVisibility;
2846   else
2847     llvmVisibility = CodeGenModule::GetLLVMVisibility(Ty->getVisibility());
2848   TypeName->setVisibility(llvmVisibility);
2849   GV->setVisibility(llvmVisibility);
2850 
2851   return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy);
2852 }
2853 
2854 /// ComputeQualifierFlags - Compute the pointer type info flags from the
2855 /// given qualifier.
2856 static unsigned ComputeQualifierFlags(Qualifiers Quals) {
2857   unsigned Flags = 0;
2858 
2859   if (Quals.hasConst())
2860     Flags |= ItaniumRTTIBuilder::PTI_Const;
2861   if (Quals.hasVolatile())
2862     Flags |= ItaniumRTTIBuilder::PTI_Volatile;
2863   if (Quals.hasRestrict())
2864     Flags |= ItaniumRTTIBuilder::PTI_Restrict;
2865 
2866   return Flags;
2867 }
2868 
2869 /// BuildObjCObjectTypeInfo - Build the appropriate kind of type_info
2870 /// for the given Objective-C object type.
2871 void ItaniumRTTIBuilder::BuildObjCObjectTypeInfo(const ObjCObjectType *OT) {
2872   // Drop qualifiers.
2873   const Type *T = OT->getBaseType().getTypePtr();
2874   assert(isa<BuiltinType>(T) || isa<ObjCInterfaceType>(T));
2875 
2876   // The builtin types are abi::__class_type_infos and don't require
2877   // extra fields.
2878   if (isa<BuiltinType>(T)) return;
2879 
2880   ObjCInterfaceDecl *Class = cast<ObjCInterfaceType>(T)->getDecl();
2881   ObjCInterfaceDecl *Super = Class->getSuperClass();
2882 
2883   // Root classes are also __class_type_info.
2884   if (!Super) return;
2885 
2886   QualType SuperTy = CGM.getContext().getObjCInterfaceType(Super);
2887 
2888   // Everything else is single inheritance.
2889   llvm::Constant *BaseTypeInfo =
2890       ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(SuperTy);
2891   Fields.push_back(BaseTypeInfo);
2892 }
2893 
2894 /// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
2895 /// inheritance, according to the Itanium C++ ABI, 2.95p6b.
2896 void ItaniumRTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) {
2897   // Itanium C++ ABI 2.9.5p6b:
2898   // It adds to abi::__class_type_info a single member pointing to the
2899   // type_info structure for the base type,
2900   llvm::Constant *BaseTypeInfo =
2901     ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(RD->bases_begin()->getType());
2902   Fields.push_back(BaseTypeInfo);
2903 }
2904 
2905 namespace {
2906   /// SeenBases - Contains virtual and non-virtual bases seen when traversing
2907   /// a class hierarchy.
2908   struct SeenBases {
2909     llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases;
2910     llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases;
2911   };
2912 }
2913 
2914 /// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in
2915 /// abi::__vmi_class_type_info.
2916 ///
2917 static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base,
2918                                              SeenBases &Bases) {
2919 
2920   unsigned Flags = 0;
2921 
2922   const CXXRecordDecl *BaseDecl =
2923     cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
2924 
2925   if (Base->isVirtual()) {
2926     // Mark the virtual base as seen.
2927     if (!Bases.VirtualBases.insert(BaseDecl).second) {
2928       // If this virtual base has been seen before, then the class is diamond
2929       // shaped.
2930       Flags |= ItaniumRTTIBuilder::VMI_DiamondShaped;
2931     } else {
2932       if (Bases.NonVirtualBases.count(BaseDecl))
2933         Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat;
2934     }
2935   } else {
2936     // Mark the non-virtual base as seen.
2937     if (!Bases.NonVirtualBases.insert(BaseDecl).second) {
2938       // If this non-virtual base has been seen before, then the class has non-
2939       // diamond shaped repeated inheritance.
2940       Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat;
2941     } else {
2942       if (Bases.VirtualBases.count(BaseDecl))
2943         Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat;
2944     }
2945   }
2946 
2947   // Walk all bases.
2948   for (const auto &I : BaseDecl->bases())
2949     Flags |= ComputeVMIClassTypeInfoFlags(&I, Bases);
2950 
2951   return Flags;
2952 }
2953 
2954 static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) {
2955   unsigned Flags = 0;
2956   SeenBases Bases;
2957 
2958   // Walk all bases.
2959   for (const auto &I : RD->bases())
2960     Flags |= ComputeVMIClassTypeInfoFlags(&I, Bases);
2961 
2962   return Flags;
2963 }
2964 
2965 /// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
2966 /// classes with bases that do not satisfy the abi::__si_class_type_info
2967 /// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
2968 void ItaniumRTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) {
2969   llvm::Type *UnsignedIntLTy =
2970     CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
2971 
2972   // Itanium C++ ABI 2.9.5p6c:
2973   //   __flags is a word with flags describing details about the class
2974   //   structure, which may be referenced by using the __flags_masks
2975   //   enumeration. These flags refer to both direct and indirect bases.
2976   unsigned Flags = ComputeVMIClassTypeInfoFlags(RD);
2977   Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
2978 
2979   // Itanium C++ ABI 2.9.5p6c:
2980   //   __base_count is a word with the number of direct proper base class
2981   //   descriptions that follow.
2982   Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases()));
2983 
2984   if (!RD->getNumBases())
2985     return;
2986 
2987   llvm::Type *LongLTy =
2988     CGM.getTypes().ConvertType(CGM.getContext().LongTy);
2989 
2990   // Now add the base class descriptions.
2991 
2992   // Itanium C++ ABI 2.9.5p6c:
2993   //   __base_info[] is an array of base class descriptions -- one for every
2994   //   direct proper base. Each description is of the type:
2995   //
2996   //   struct abi::__base_class_type_info {
2997   //   public:
2998   //     const __class_type_info *__base_type;
2999   //     long __offset_flags;
3000   //
3001   //     enum __offset_flags_masks {
3002   //       __virtual_mask = 0x1,
3003   //       __public_mask = 0x2,
3004   //       __offset_shift = 8
3005   //     };
3006   //   };
3007   for (const auto &Base : RD->bases()) {
3008     // The __base_type member points to the RTTI for the base type.
3009     Fields.push_back(ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(Base.getType()));
3010 
3011     const CXXRecordDecl *BaseDecl =
3012       cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl());
3013 
3014     int64_t OffsetFlags = 0;
3015 
3016     // All but the lower 8 bits of __offset_flags are a signed offset.
3017     // For a non-virtual base, this is the offset in the object of the base
3018     // subobject. For a virtual base, this is the offset in the virtual table of
3019     // the virtual base offset for the virtual base referenced (negative).
3020     CharUnits Offset;
3021     if (Base.isVirtual())
3022       Offset =
3023         CGM.getItaniumVTableContext().getVirtualBaseOffsetOffset(RD, BaseDecl);
3024     else {
3025       const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
3026       Offset = Layout.getBaseClassOffset(BaseDecl);
3027     };
3028 
3029     OffsetFlags = uint64_t(Offset.getQuantity()) << 8;
3030 
3031     // The low-order byte of __offset_flags contains flags, as given by the
3032     // masks from the enumeration __offset_flags_masks.
3033     if (Base.isVirtual())
3034       OffsetFlags |= BCTI_Virtual;
3035     if (Base.getAccessSpecifier() == AS_public)
3036       OffsetFlags |= BCTI_Public;
3037 
3038     Fields.push_back(llvm::ConstantInt::get(LongLTy, OffsetFlags));
3039   }
3040 }
3041 
3042 /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct,
3043 /// used for pointer types.
3044 void ItaniumRTTIBuilder::BuildPointerTypeInfo(QualType PointeeTy) {
3045   Qualifiers Quals;
3046   QualType UnqualifiedPointeeTy =
3047     CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
3048 
3049   // Itanium C++ ABI 2.9.5p7:
3050   //   __flags is a flag word describing the cv-qualification and other
3051   //   attributes of the type pointed to
3052   unsigned Flags = ComputeQualifierFlags(Quals);
3053 
3054   // Itanium C++ ABI 2.9.5p7:
3055   //   When the abi::__pbase_type_info is for a direct or indirect pointer to an
3056   //   incomplete class type, the incomplete target type flag is set.
3057   if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
3058     Flags |= PTI_Incomplete;
3059 
3060   llvm::Type *UnsignedIntLTy =
3061     CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
3062   Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
3063 
3064   // Itanium C++ ABI 2.9.5p7:
3065   //  __pointee is a pointer to the std::type_info derivation for the
3066   //  unqualified type being pointed to.
3067   llvm::Constant *PointeeTypeInfo =
3068     ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(UnqualifiedPointeeTy);
3069   Fields.push_back(PointeeTypeInfo);
3070 }
3071 
3072 /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
3073 /// struct, used for member pointer types.
3074 void
3075 ItaniumRTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) {
3076   QualType PointeeTy = Ty->getPointeeType();
3077 
3078   Qualifiers Quals;
3079   QualType UnqualifiedPointeeTy =
3080     CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
3081 
3082   // Itanium C++ ABI 2.9.5p7:
3083   //   __flags is a flag word describing the cv-qualification and other
3084   //   attributes of the type pointed to.
3085   unsigned Flags = ComputeQualifierFlags(Quals);
3086 
3087   const RecordType *ClassType = cast<RecordType>(Ty->getClass());
3088 
3089   // Itanium C++ ABI 2.9.5p7:
3090   //   When the abi::__pbase_type_info is for a direct or indirect pointer to an
3091   //   incomplete class type, the incomplete target type flag is set.
3092   if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
3093     Flags |= PTI_Incomplete;
3094 
3095   if (IsIncompleteClassType(ClassType))
3096     Flags |= PTI_ContainingClassIncomplete;
3097 
3098   llvm::Type *UnsignedIntLTy =
3099     CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
3100   Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
3101 
3102   // Itanium C++ ABI 2.9.5p7:
3103   //   __pointee is a pointer to the std::type_info derivation for the
3104   //   unqualified type being pointed to.
3105   llvm::Constant *PointeeTypeInfo =
3106     ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(UnqualifiedPointeeTy);
3107   Fields.push_back(PointeeTypeInfo);
3108 
3109   // Itanium C++ ABI 2.9.5p9:
3110   //   __context is a pointer to an abi::__class_type_info corresponding to the
3111   //   class type containing the member pointed to
3112   //   (e.g., the "A" in "int A::*").
3113   Fields.push_back(
3114       ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(QualType(ClassType, 0)));
3115 }
3116 
3117 llvm::Constant *ItaniumCXXABI::getAddrOfRTTIDescriptor(QualType Ty) {
3118   return ItaniumRTTIBuilder(*this).BuildTypeInfo(Ty);
3119 }
3120 
3121 void ItaniumCXXABI::EmitFundamentalRTTIDescriptor(QualType Type) {
3122   QualType PointerType = getContext().getPointerType(Type);
3123   QualType PointerTypeConst = getContext().getPointerType(Type.withConst());
3124   ItaniumRTTIBuilder(*this).BuildTypeInfo(Type, true);
3125   ItaniumRTTIBuilder(*this).BuildTypeInfo(PointerType, true);
3126   ItaniumRTTIBuilder(*this).BuildTypeInfo(PointerTypeConst, true);
3127 }
3128 
3129 void ItaniumCXXABI::EmitFundamentalRTTIDescriptors() {
3130   QualType FundamentalTypes[] = {
3131       getContext().VoidTy,             getContext().NullPtrTy,
3132       getContext().BoolTy,             getContext().WCharTy,
3133       getContext().CharTy,             getContext().UnsignedCharTy,
3134       getContext().SignedCharTy,       getContext().ShortTy,
3135       getContext().UnsignedShortTy,    getContext().IntTy,
3136       getContext().UnsignedIntTy,      getContext().LongTy,
3137       getContext().UnsignedLongTy,     getContext().LongLongTy,
3138       getContext().UnsignedLongLongTy, getContext().HalfTy,
3139       getContext().FloatTy,            getContext().DoubleTy,
3140       getContext().LongDoubleTy,       getContext().Char16Ty,
3141       getContext().Char32Ty,
3142   };
3143   for (const QualType &FundamentalType : FundamentalTypes)
3144     EmitFundamentalRTTIDescriptor(FundamentalType);
3145 }
3146 
3147 /// What sort of uniqueness rules should we use for the RTTI for the
3148 /// given type?
3149 ItaniumCXXABI::RTTIUniquenessKind ItaniumCXXABI::classifyRTTIUniqueness(
3150     QualType CanTy, llvm::GlobalValue::LinkageTypes Linkage) const {
3151   if (shouldRTTIBeUnique())
3152     return RUK_Unique;
3153 
3154   // It's only necessary for linkonce_odr or weak_odr linkage.
3155   if (Linkage != llvm::GlobalValue::LinkOnceODRLinkage &&
3156       Linkage != llvm::GlobalValue::WeakODRLinkage)
3157     return RUK_Unique;
3158 
3159   // It's only necessary with default visibility.
3160   if (CanTy->getVisibility() != DefaultVisibility)
3161     return RUK_Unique;
3162 
3163   // If we're not required to publish this symbol, hide it.
3164   if (Linkage == llvm::GlobalValue::LinkOnceODRLinkage)
3165     return RUK_NonUniqueHidden;
3166 
3167   // If we're required to publish this symbol, as we might be under an
3168   // explicit instantiation, leave it with default visibility but
3169   // enable string-comparisons.
3170   assert(Linkage == llvm::GlobalValue::WeakODRLinkage);
3171   return RUK_NonUniqueVisible;
3172 }
3173 
3174 // Find out how to codegen the complete destructor and constructor
3175 namespace {
3176 enum class StructorCodegen { Emit, RAUW, Alias, COMDAT };
3177 }
3178 static StructorCodegen getCodegenToUse(CodeGenModule &CGM,
3179                                        const CXXMethodDecl *MD) {
3180   if (!CGM.getCodeGenOpts().CXXCtorDtorAliases)
3181     return StructorCodegen::Emit;
3182 
3183   // The complete and base structors are not equivalent if there are any virtual
3184   // bases, so emit separate functions.
3185   if (MD->getParent()->getNumVBases())
3186     return StructorCodegen::Emit;
3187 
3188   GlobalDecl AliasDecl;
3189   if (const auto *DD = dyn_cast<CXXDestructorDecl>(MD)) {
3190     AliasDecl = GlobalDecl(DD, Dtor_Complete);
3191   } else {
3192     const auto *CD = cast<CXXConstructorDecl>(MD);
3193     AliasDecl = GlobalDecl(CD, Ctor_Complete);
3194   }
3195   llvm::GlobalValue::LinkageTypes Linkage = CGM.getFunctionLinkage(AliasDecl);
3196 
3197   if (llvm::GlobalValue::isDiscardableIfUnused(Linkage))
3198     return StructorCodegen::RAUW;
3199 
3200   // FIXME: Should we allow available_externally aliases?
3201   if (!llvm::GlobalAlias::isValidLinkage(Linkage))
3202     return StructorCodegen::RAUW;
3203 
3204   if (llvm::GlobalValue::isWeakForLinker(Linkage)) {
3205     // Only ELF supports COMDATs with arbitrary names (C5/D5).
3206     if (CGM.getTarget().getTriple().isOSBinFormatELF())
3207       return StructorCodegen::COMDAT;
3208     return StructorCodegen::Emit;
3209   }
3210 
3211   return StructorCodegen::Alias;
3212 }
3213 
3214 static void emitConstructorDestructorAlias(CodeGenModule &CGM,
3215                                            GlobalDecl AliasDecl,
3216                                            GlobalDecl TargetDecl) {
3217   llvm::GlobalValue::LinkageTypes Linkage = CGM.getFunctionLinkage(AliasDecl);
3218 
3219   StringRef MangledName = CGM.getMangledName(AliasDecl);
3220   llvm::GlobalValue *Entry = CGM.GetGlobalValue(MangledName);
3221   if (Entry && !Entry->isDeclaration())
3222     return;
3223 
3224   auto *Aliasee = cast<llvm::GlobalValue>(CGM.GetAddrOfGlobal(TargetDecl));
3225   llvm::PointerType *AliasType = Aliasee->getType();
3226 
3227   // Create the alias with no name.
3228   auto *Alias = llvm::GlobalAlias::create(AliasType, Linkage, "", Aliasee,
3229                                           &CGM.getModule());
3230 
3231   // Switch any previous uses to the alias.
3232   if (Entry) {
3233     assert(Entry->getType() == AliasType &&
3234            "declaration exists with different type");
3235     Alias->takeName(Entry);
3236     Entry->replaceAllUsesWith(Alias);
3237     Entry->eraseFromParent();
3238   } else {
3239     Alias->setName(MangledName);
3240   }
3241 
3242   // Finally, set up the alias with its proper name and attributes.
3243   CGM.setAliasAttributes(cast<NamedDecl>(AliasDecl.getDecl()), Alias);
3244 }
3245 
3246 void ItaniumCXXABI::emitCXXStructor(const CXXMethodDecl *MD,
3247                                     StructorType Type) {
3248   auto *CD = dyn_cast<CXXConstructorDecl>(MD);
3249   const CXXDestructorDecl *DD = CD ? nullptr : cast<CXXDestructorDecl>(MD);
3250 
3251   StructorCodegen CGType = getCodegenToUse(CGM, MD);
3252 
3253   if (Type == StructorType::Complete) {
3254     GlobalDecl CompleteDecl;
3255     GlobalDecl BaseDecl;
3256     if (CD) {
3257       CompleteDecl = GlobalDecl(CD, Ctor_Complete);
3258       BaseDecl = GlobalDecl(CD, Ctor_Base);
3259     } else {
3260       CompleteDecl = GlobalDecl(DD, Dtor_Complete);
3261       BaseDecl = GlobalDecl(DD, Dtor_Base);
3262     }
3263 
3264     if (CGType == StructorCodegen::Alias || CGType == StructorCodegen::COMDAT) {
3265       emitConstructorDestructorAlias(CGM, CompleteDecl, BaseDecl);
3266       return;
3267     }
3268 
3269     if (CGType == StructorCodegen::RAUW) {
3270       StringRef MangledName = CGM.getMangledName(CompleteDecl);
3271       auto *Aliasee = cast<llvm::GlobalValue>(CGM.GetAddrOfGlobal(BaseDecl));
3272       CGM.addReplacement(MangledName, Aliasee);
3273       return;
3274     }
3275   }
3276 
3277   // The base destructor is equivalent to the base destructor of its
3278   // base class if there is exactly one non-virtual base class with a
3279   // non-trivial destructor, there are no fields with a non-trivial
3280   // destructor, and the body of the destructor is trivial.
3281   if (DD && Type == StructorType::Base && CGType != StructorCodegen::COMDAT &&
3282       !CGM.TryEmitBaseDestructorAsAlias(DD))
3283     return;
3284 
3285   llvm::Function *Fn = CGM.codegenCXXStructor(MD, Type);
3286 
3287   if (CGType == StructorCodegen::COMDAT) {
3288     SmallString<256> Buffer;
3289     llvm::raw_svector_ostream Out(Buffer);
3290     if (DD)
3291       getMangleContext().mangleCXXDtorComdat(DD, Out);
3292     else
3293       getMangleContext().mangleCXXCtorComdat(CD, Out);
3294     llvm::Comdat *C = CGM.getModule().getOrInsertComdat(Out.str());
3295     Fn->setComdat(C);
3296   } else {
3297     CGM.maybeSetTrivialComdat(*MD, *Fn);
3298   }
3299 }
3300 
3301 static llvm::Constant *getBeginCatchFn(CodeGenModule &CGM) {
3302   // void *__cxa_begin_catch(void*);
3303   llvm::FunctionType *FTy = llvm::FunctionType::get(
3304       CGM.Int8PtrTy, CGM.Int8PtrTy, /*IsVarArgs=*/false);
3305 
3306   return CGM.CreateRuntimeFunction(FTy, "__cxa_begin_catch");
3307 }
3308 
3309 static llvm::Constant *getEndCatchFn(CodeGenModule &CGM) {
3310   // void __cxa_end_catch();
3311   llvm::FunctionType *FTy =
3312       llvm::FunctionType::get(CGM.VoidTy, /*IsVarArgs=*/false);
3313 
3314   return CGM.CreateRuntimeFunction(FTy, "__cxa_end_catch");
3315 }
3316 
3317 static llvm::Constant *getGetExceptionPtrFn(CodeGenModule &CGM) {
3318   // void *__cxa_get_exception_ptr(void*);
3319   llvm::FunctionType *FTy = llvm::FunctionType::get(
3320       CGM.Int8PtrTy, CGM.Int8PtrTy, /*IsVarArgs=*/false);
3321 
3322   return CGM.CreateRuntimeFunction(FTy, "__cxa_get_exception_ptr");
3323 }
3324 
3325 namespace {
3326   /// A cleanup to call __cxa_end_catch.  In many cases, the caught
3327   /// exception type lets us state definitively that the thrown exception
3328   /// type does not have a destructor.  In particular:
3329   ///   - Catch-alls tell us nothing, so we have to conservatively
3330   ///     assume that the thrown exception might have a destructor.
3331   ///   - Catches by reference behave according to their base types.
3332   ///   - Catches of non-record types will only trigger for exceptions
3333   ///     of non-record types, which never have destructors.
3334   ///   - Catches of record types can trigger for arbitrary subclasses
3335   ///     of the caught type, so we have to assume the actual thrown
3336   ///     exception type might have a throwing destructor, even if the
3337   ///     caught type's destructor is trivial or nothrow.
3338   struct CallEndCatch : EHScopeStack::Cleanup {
3339     CallEndCatch(bool MightThrow) : MightThrow(MightThrow) {}
3340     bool MightThrow;
3341 
3342     void Emit(CodeGenFunction &CGF, Flags flags) override {
3343       if (!MightThrow) {
3344         CGF.EmitNounwindRuntimeCall(getEndCatchFn(CGF.CGM));
3345         return;
3346       }
3347 
3348       CGF.EmitRuntimeCallOrInvoke(getEndCatchFn(CGF.CGM));
3349     }
3350   };
3351 }
3352 
3353 /// Emits a call to __cxa_begin_catch and enters a cleanup to call
3354 /// __cxa_end_catch.
3355 ///
3356 /// \param EndMightThrow - true if __cxa_end_catch might throw
3357 static llvm::Value *CallBeginCatch(CodeGenFunction &CGF,
3358                                    llvm::Value *Exn,
3359                                    bool EndMightThrow) {
3360   llvm::CallInst *call =
3361     CGF.EmitNounwindRuntimeCall(getBeginCatchFn(CGF.CGM), Exn);
3362 
3363   CGF.EHStack.pushCleanup<CallEndCatch>(NormalAndEHCleanup, EndMightThrow);
3364 
3365   return call;
3366 }
3367 
3368 /// A "special initializer" callback for initializing a catch
3369 /// parameter during catch initialization.
3370 static void InitCatchParam(CodeGenFunction &CGF,
3371                            const VarDecl &CatchParam,
3372                            llvm::Value *ParamAddr,
3373                            SourceLocation Loc) {
3374   // Load the exception from where the landing pad saved it.
3375   llvm::Value *Exn = CGF.getExceptionFromSlot();
3376 
3377   CanQualType CatchType =
3378     CGF.CGM.getContext().getCanonicalType(CatchParam.getType());
3379   llvm::Type *LLVMCatchTy = CGF.ConvertTypeForMem(CatchType);
3380 
3381   // If we're catching by reference, we can just cast the object
3382   // pointer to the appropriate pointer.
3383   if (isa<ReferenceType>(CatchType)) {
3384     QualType CaughtType = cast<ReferenceType>(CatchType)->getPointeeType();
3385     bool EndCatchMightThrow = CaughtType->isRecordType();
3386 
3387     // __cxa_begin_catch returns the adjusted object pointer.
3388     llvm::Value *AdjustedExn = CallBeginCatch(CGF, Exn, EndCatchMightThrow);
3389 
3390     // We have no way to tell the personality function that we're
3391     // catching by reference, so if we're catching a pointer,
3392     // __cxa_begin_catch will actually return that pointer by value.
3393     if (const PointerType *PT = dyn_cast<PointerType>(CaughtType)) {
3394       QualType PointeeType = PT->getPointeeType();
3395 
3396       // When catching by reference, generally we should just ignore
3397       // this by-value pointer and use the exception object instead.
3398       if (!PointeeType->isRecordType()) {
3399 
3400         // Exn points to the struct _Unwind_Exception header, which
3401         // we have to skip past in order to reach the exception data.
3402         unsigned HeaderSize =
3403           CGF.CGM.getTargetCodeGenInfo().getSizeOfUnwindException();
3404         AdjustedExn = CGF.Builder.CreateConstGEP1_32(Exn, HeaderSize);
3405 
3406       // However, if we're catching a pointer-to-record type that won't
3407       // work, because the personality function might have adjusted
3408       // the pointer.  There's actually no way for us to fully satisfy
3409       // the language/ABI contract here:  we can't use Exn because it
3410       // might have the wrong adjustment, but we can't use the by-value
3411       // pointer because it's off by a level of abstraction.
3412       //
3413       // The current solution is to dump the adjusted pointer into an
3414       // alloca, which breaks language semantics (because changing the
3415       // pointer doesn't change the exception) but at least works.
3416       // The better solution would be to filter out non-exact matches
3417       // and rethrow them, but this is tricky because the rethrow
3418       // really needs to be catchable by other sites at this landing
3419       // pad.  The best solution is to fix the personality function.
3420       } else {
3421         // Pull the pointer for the reference type off.
3422         llvm::Type *PtrTy =
3423           cast<llvm::PointerType>(LLVMCatchTy)->getElementType();
3424 
3425         // Create the temporary and write the adjusted pointer into it.
3426         llvm::Value *ExnPtrTmp = CGF.CreateTempAlloca(PtrTy, "exn.byref.tmp");
3427         llvm::Value *Casted = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy);
3428         CGF.Builder.CreateStore(Casted, ExnPtrTmp);
3429 
3430         // Bind the reference to the temporary.
3431         AdjustedExn = ExnPtrTmp;
3432       }
3433     }
3434 
3435     llvm::Value *ExnCast =
3436       CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.byref");
3437     CGF.Builder.CreateStore(ExnCast, ParamAddr);
3438     return;
3439   }
3440 
3441   // Scalars and complexes.
3442   TypeEvaluationKind TEK = CGF.getEvaluationKind(CatchType);
3443   if (TEK != TEK_Aggregate) {
3444     llvm::Value *AdjustedExn = CallBeginCatch(CGF, Exn, false);
3445 
3446     // If the catch type is a pointer type, __cxa_begin_catch returns
3447     // the pointer by value.
3448     if (CatchType->hasPointerRepresentation()) {
3449       llvm::Value *CastExn =
3450         CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.casted");
3451 
3452       switch (CatchType.getQualifiers().getObjCLifetime()) {
3453       case Qualifiers::OCL_Strong:
3454         CastExn = CGF.EmitARCRetainNonBlock(CastExn);
3455         // fallthrough
3456 
3457       case Qualifiers::OCL_None:
3458       case Qualifiers::OCL_ExplicitNone:
3459       case Qualifiers::OCL_Autoreleasing:
3460         CGF.Builder.CreateStore(CastExn, ParamAddr);
3461         return;
3462 
3463       case Qualifiers::OCL_Weak:
3464         CGF.EmitARCInitWeak(ParamAddr, CastExn);
3465         return;
3466       }
3467       llvm_unreachable("bad ownership qualifier!");
3468     }
3469 
3470     // Otherwise, it returns a pointer into the exception object.
3471 
3472     llvm::Type *PtrTy = LLVMCatchTy->getPointerTo(0); // addrspace 0 ok
3473     llvm::Value *Cast = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy);
3474 
3475     LValue srcLV = CGF.MakeNaturalAlignAddrLValue(Cast, CatchType);
3476     LValue destLV = CGF.MakeAddrLValue(ParamAddr, CatchType,
3477                                   CGF.getContext().getDeclAlign(&CatchParam));
3478     switch (TEK) {
3479     case TEK_Complex:
3480       CGF.EmitStoreOfComplex(CGF.EmitLoadOfComplex(srcLV, Loc), destLV,
3481                              /*init*/ true);
3482       return;
3483     case TEK_Scalar: {
3484       llvm::Value *ExnLoad = CGF.EmitLoadOfScalar(srcLV, Loc);
3485       CGF.EmitStoreOfScalar(ExnLoad, destLV, /*init*/ true);
3486       return;
3487     }
3488     case TEK_Aggregate:
3489       llvm_unreachable("evaluation kind filtered out!");
3490     }
3491     llvm_unreachable("bad evaluation kind");
3492   }
3493 
3494   assert(isa<RecordType>(CatchType) && "unexpected catch type!");
3495 
3496   llvm::Type *PtrTy = LLVMCatchTy->getPointerTo(0); // addrspace 0 ok
3497 
3498   // Check for a copy expression.  If we don't have a copy expression,
3499   // that means a trivial copy is okay.
3500   const Expr *copyExpr = CatchParam.getInit();
3501   if (!copyExpr) {
3502     llvm::Value *rawAdjustedExn = CallBeginCatch(CGF, Exn, true);
3503     llvm::Value *adjustedExn = CGF.Builder.CreateBitCast(rawAdjustedExn, PtrTy);
3504     CGF.EmitAggregateCopy(ParamAddr, adjustedExn, CatchType);
3505     return;
3506   }
3507 
3508   // We have to call __cxa_get_exception_ptr to get the adjusted
3509   // pointer before copying.
3510   llvm::CallInst *rawAdjustedExn =
3511     CGF.EmitNounwindRuntimeCall(getGetExceptionPtrFn(CGF.CGM), Exn);
3512 
3513   // Cast that to the appropriate type.
3514   llvm::Value *adjustedExn = CGF.Builder.CreateBitCast(rawAdjustedExn, PtrTy);
3515 
3516   // The copy expression is defined in terms of an OpaqueValueExpr.
3517   // Find it and map it to the adjusted expression.
3518   CodeGenFunction::OpaqueValueMapping
3519     opaque(CGF, OpaqueValueExpr::findInCopyConstruct(copyExpr),
3520            CGF.MakeAddrLValue(adjustedExn, CatchParam.getType()));
3521 
3522   // Call the copy ctor in a terminate scope.
3523   CGF.EHStack.pushTerminate();
3524 
3525   // Perform the copy construction.
3526   CharUnits Alignment = CGF.getContext().getDeclAlign(&CatchParam);
3527   CGF.EmitAggExpr(copyExpr,
3528                   AggValueSlot::forAddr(ParamAddr, Alignment, Qualifiers(),
3529                                         AggValueSlot::IsNotDestructed,
3530                                         AggValueSlot::DoesNotNeedGCBarriers,
3531                                         AggValueSlot::IsNotAliased));
3532 
3533   // Leave the terminate scope.
3534   CGF.EHStack.popTerminate();
3535 
3536   // Undo the opaque value mapping.
3537   opaque.pop();
3538 
3539   // Finally we can call __cxa_begin_catch.
3540   CallBeginCatch(CGF, Exn, true);
3541 }
3542 
3543 /// Begins a catch statement by initializing the catch variable and
3544 /// calling __cxa_begin_catch.
3545 void ItaniumCXXABI::emitBeginCatch(CodeGenFunction &CGF,
3546                                    const CXXCatchStmt *S) {
3547   // We have to be very careful with the ordering of cleanups here:
3548   //   C++ [except.throw]p4:
3549   //     The destruction [of the exception temporary] occurs
3550   //     immediately after the destruction of the object declared in
3551   //     the exception-declaration in the handler.
3552   //
3553   // So the precise ordering is:
3554   //   1.  Construct catch variable.
3555   //   2.  __cxa_begin_catch
3556   //   3.  Enter __cxa_end_catch cleanup
3557   //   4.  Enter dtor cleanup
3558   //
3559   // We do this by using a slightly abnormal initialization process.
3560   // Delegation sequence:
3561   //   - ExitCXXTryStmt opens a RunCleanupsScope
3562   //     - EmitAutoVarAlloca creates the variable and debug info
3563   //       - InitCatchParam initializes the variable from the exception
3564   //       - CallBeginCatch calls __cxa_begin_catch
3565   //       - CallBeginCatch enters the __cxa_end_catch cleanup
3566   //     - EmitAutoVarCleanups enters the variable destructor cleanup
3567   //   - EmitCXXTryStmt emits the code for the catch body
3568   //   - EmitCXXTryStmt close the RunCleanupsScope
3569 
3570   VarDecl *CatchParam = S->getExceptionDecl();
3571   if (!CatchParam) {
3572     llvm::Value *Exn = CGF.getExceptionFromSlot();
3573     CallBeginCatch(CGF, Exn, true);
3574     return;
3575   }
3576 
3577   // Emit the local.
3578   CodeGenFunction::AutoVarEmission var = CGF.EmitAutoVarAlloca(*CatchParam);
3579   InitCatchParam(CGF, *CatchParam, var.getObjectAddress(CGF), S->getLocStart());
3580   CGF.EmitAutoVarCleanups(var);
3581 }
3582 
3583 /// Get or define the following function:
3584 ///   void @__clang_call_terminate(i8* %exn) nounwind noreturn
3585 /// This code is used only in C++.
3586 static llvm::Constant *getClangCallTerminateFn(CodeGenModule &CGM) {
3587   llvm::FunctionType *fnTy =
3588     llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*IsVarArgs=*/false);
3589   llvm::Constant *fnRef =
3590     CGM.CreateRuntimeFunction(fnTy, "__clang_call_terminate");
3591 
3592   llvm::Function *fn = dyn_cast<llvm::Function>(fnRef);
3593   if (fn && fn->empty()) {
3594     fn->setDoesNotThrow();
3595     fn->setDoesNotReturn();
3596 
3597     // What we really want is to massively penalize inlining without
3598     // forbidding it completely.  The difference between that and
3599     // 'noinline' is negligible.
3600     fn->addFnAttr(llvm::Attribute::NoInline);
3601 
3602     // Allow this function to be shared across translation units, but
3603     // we don't want it to turn into an exported symbol.
3604     fn->setLinkage(llvm::Function::LinkOnceODRLinkage);
3605     fn->setVisibility(llvm::Function::HiddenVisibility);
3606     if (CGM.supportsCOMDAT())
3607       fn->setComdat(CGM.getModule().getOrInsertComdat(fn->getName()));
3608 
3609     // Set up the function.
3610     llvm::BasicBlock *entry =
3611       llvm::BasicBlock::Create(CGM.getLLVMContext(), "", fn);
3612     CGBuilderTy builder(entry);
3613 
3614     // Pull the exception pointer out of the parameter list.
3615     llvm::Value *exn = &*fn->arg_begin();
3616 
3617     // Call __cxa_begin_catch(exn).
3618     llvm::CallInst *catchCall = builder.CreateCall(getBeginCatchFn(CGM), exn);
3619     catchCall->setDoesNotThrow();
3620     catchCall->setCallingConv(CGM.getRuntimeCC());
3621 
3622     // Call std::terminate().
3623     llvm::CallInst *termCall = builder.CreateCall(CGM.getTerminateFn());
3624     termCall->setDoesNotThrow();
3625     termCall->setDoesNotReturn();
3626     termCall->setCallingConv(CGM.getRuntimeCC());
3627 
3628     // std::terminate cannot return.
3629     builder.CreateUnreachable();
3630   }
3631 
3632   return fnRef;
3633 }
3634 
3635 llvm::CallInst *
3636 ItaniumCXXABI::emitTerminateForUnexpectedException(CodeGenFunction &CGF,
3637                                                    llvm::Value *Exn) {
3638   // In C++, we want to call __cxa_begin_catch() before terminating.
3639   if (Exn) {
3640     assert(CGF.CGM.getLangOpts().CPlusPlus);
3641     return CGF.EmitNounwindRuntimeCall(getClangCallTerminateFn(CGF.CGM), Exn);
3642   }
3643   return CGF.EmitNounwindRuntimeCall(CGF.CGM.getTerminateFn());
3644 }
3645