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