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