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