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