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       // Don't mark an extern_weak function DSO local on windows.
2688       if (!CGM.getTriple().isOSWindows() || !Init->hasExternalWeakLinkage())
2689         Init->setDSOLocal(Var->isDSOLocal());
2690     }
2691 
2692     llvm::LLVMContext &Context = CGM.getModule().getContext();
2693     llvm::BasicBlock *Entry = llvm::BasicBlock::Create(Context, "", Wrapper);
2694     CGBuilderTy Builder(CGM, Entry);
2695     if (HasConstantInitialization) {
2696       // No dynamic initialization to invoke.
2697     } else if (InitIsInitFunc) {
2698       if (Init) {
2699         llvm::CallInst *CallVal = Builder.CreateCall(InitFnTy, Init);
2700         if (isThreadWrapperReplaceable(VD, CGM)) {
2701           CallVal->setCallingConv(llvm::CallingConv::CXX_FAST_TLS);
2702           llvm::Function *Fn =
2703               cast<llvm::Function>(cast<llvm::GlobalAlias>(Init)->getAliasee());
2704           Fn->setCallingConv(llvm::CallingConv::CXX_FAST_TLS);
2705         }
2706       }
2707     } else {
2708       // Don't know whether we have an init function. Call it if it exists.
2709       llvm::Value *Have = Builder.CreateIsNotNull(Init);
2710       llvm::BasicBlock *InitBB = llvm::BasicBlock::Create(Context, "", Wrapper);
2711       llvm::BasicBlock *ExitBB = llvm::BasicBlock::Create(Context, "", Wrapper);
2712       Builder.CreateCondBr(Have, InitBB, ExitBB);
2713 
2714       Builder.SetInsertPoint(InitBB);
2715       Builder.CreateCall(InitFnTy, Init);
2716       Builder.CreateBr(ExitBB);
2717 
2718       Builder.SetInsertPoint(ExitBB);
2719     }
2720 
2721     // For a reference, the result of the wrapper function is a pointer to
2722     // the referenced object.
2723     llvm::Value *Val = Var;
2724     if (VD->getType()->isReferenceType()) {
2725       CharUnits Align = CGM.getContext().getDeclAlign(VD);
2726       Val = Builder.CreateAlignedLoad(Val, Align);
2727     }
2728     if (Val->getType() != Wrapper->getReturnType())
2729       Val = Builder.CreatePointerBitCastOrAddrSpaceCast(
2730           Val, Wrapper->getReturnType(), "");
2731     Builder.CreateRet(Val);
2732   }
2733 }
2734 
2735 LValue ItaniumCXXABI::EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF,
2736                                                    const VarDecl *VD,
2737                                                    QualType LValType) {
2738   llvm::Value *Val = CGF.CGM.GetAddrOfGlobalVar(VD);
2739   llvm::Function *Wrapper = getOrCreateThreadLocalWrapper(VD, Val);
2740 
2741   llvm::CallInst *CallVal = CGF.Builder.CreateCall(Wrapper);
2742   CallVal->setCallingConv(Wrapper->getCallingConv());
2743 
2744   LValue LV;
2745   if (VD->getType()->isReferenceType())
2746     LV = CGF.MakeNaturalAlignAddrLValue(CallVal, LValType);
2747   else
2748     LV = CGF.MakeAddrLValue(CallVal, LValType,
2749                             CGF.getContext().getDeclAlign(VD));
2750   // FIXME: need setObjCGCLValueClass?
2751   return LV;
2752 }
2753 
2754 /// Return whether the given global decl needs a VTT parameter, which it does
2755 /// if it's a base constructor or destructor with virtual bases.
2756 bool ItaniumCXXABI::NeedsVTTParameter(GlobalDecl GD) {
2757   const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
2758 
2759   // We don't have any virtual bases, just return early.
2760   if (!MD->getParent()->getNumVBases())
2761     return false;
2762 
2763   // Check if we have a base constructor.
2764   if (isa<CXXConstructorDecl>(MD) && GD.getCtorType() == Ctor_Base)
2765     return true;
2766 
2767   // Check if we have a base destructor.
2768   if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
2769     return true;
2770 
2771   return false;
2772 }
2773 
2774 namespace {
2775 class ItaniumRTTIBuilder {
2776   CodeGenModule &CGM;  // Per-module state.
2777   llvm::LLVMContext &VMContext;
2778   const ItaniumCXXABI &CXXABI;  // Per-module state.
2779 
2780   /// Fields - The fields of the RTTI descriptor currently being built.
2781   SmallVector<llvm::Constant *, 16> Fields;
2782 
2783   /// GetAddrOfTypeName - Returns the mangled type name of the given type.
2784   llvm::GlobalVariable *
2785   GetAddrOfTypeName(QualType Ty, llvm::GlobalVariable::LinkageTypes Linkage);
2786 
2787   /// GetAddrOfExternalRTTIDescriptor - Returns the constant for the RTTI
2788   /// descriptor of the given type.
2789   llvm::Constant *GetAddrOfExternalRTTIDescriptor(QualType Ty);
2790 
2791   /// BuildVTablePointer - Build the vtable pointer for the given type.
2792   void BuildVTablePointer(const Type *Ty);
2793 
2794   /// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
2795   /// inheritance, according to the Itanium C++ ABI, 2.9.5p6b.
2796   void BuildSIClassTypeInfo(const CXXRecordDecl *RD);
2797 
2798   /// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
2799   /// classes with bases that do not satisfy the abi::__si_class_type_info
2800   /// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
2801   void BuildVMIClassTypeInfo(const CXXRecordDecl *RD);
2802 
2803   /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct, used
2804   /// for pointer types.
2805   void BuildPointerTypeInfo(QualType PointeeTy);
2806 
2807   /// BuildObjCObjectTypeInfo - Build the appropriate kind of
2808   /// type_info for an object type.
2809   void BuildObjCObjectTypeInfo(const ObjCObjectType *Ty);
2810 
2811   /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
2812   /// struct, used for member pointer types.
2813   void BuildPointerToMemberTypeInfo(const MemberPointerType *Ty);
2814 
2815 public:
2816   ItaniumRTTIBuilder(const ItaniumCXXABI &ABI)
2817       : CGM(ABI.CGM), VMContext(CGM.getModule().getContext()), CXXABI(ABI) {}
2818 
2819   // Pointer type info flags.
2820   enum {
2821     /// PTI_Const - Type has const qualifier.
2822     PTI_Const = 0x1,
2823 
2824     /// PTI_Volatile - Type has volatile qualifier.
2825     PTI_Volatile = 0x2,
2826 
2827     /// PTI_Restrict - Type has restrict qualifier.
2828     PTI_Restrict = 0x4,
2829 
2830     /// PTI_Incomplete - Type is incomplete.
2831     PTI_Incomplete = 0x8,
2832 
2833     /// PTI_ContainingClassIncomplete - Containing class is incomplete.
2834     /// (in pointer to member).
2835     PTI_ContainingClassIncomplete = 0x10,
2836 
2837     /// PTI_TransactionSafe - Pointee is transaction_safe function (C++ TM TS).
2838     //PTI_TransactionSafe = 0x20,
2839 
2840     /// PTI_Noexcept - Pointee is noexcept function (C++1z).
2841     PTI_Noexcept = 0x40,
2842   };
2843 
2844   // VMI type info flags.
2845   enum {
2846     /// VMI_NonDiamondRepeat - Class has non-diamond repeated inheritance.
2847     VMI_NonDiamondRepeat = 0x1,
2848 
2849     /// VMI_DiamondShaped - Class is diamond shaped.
2850     VMI_DiamondShaped = 0x2
2851   };
2852 
2853   // Base class type info flags.
2854   enum {
2855     /// BCTI_Virtual - Base class is virtual.
2856     BCTI_Virtual = 0x1,
2857 
2858     /// BCTI_Public - Base class is public.
2859     BCTI_Public = 0x2
2860   };
2861 
2862   /// BuildTypeInfo - Build the RTTI type info struct for the given type, or
2863   /// link to an existing RTTI descriptor if one already exists.
2864   llvm::Constant *BuildTypeInfo(QualType Ty);
2865 
2866   /// BuildTypeInfo - Build the RTTI type info struct for the given type.
2867   llvm::Constant *BuildTypeInfo(
2868       QualType Ty,
2869       llvm::GlobalVariable::LinkageTypes Linkage,
2870       llvm::GlobalValue::VisibilityTypes Visibility,
2871       llvm::GlobalValue::DLLStorageClassTypes DLLStorageClass);
2872 };
2873 }
2874 
2875 llvm::GlobalVariable *ItaniumRTTIBuilder::GetAddrOfTypeName(
2876     QualType Ty, llvm::GlobalVariable::LinkageTypes Linkage) {
2877   SmallString<256> Name;
2878   llvm::raw_svector_ostream Out(Name);
2879   CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(Ty, Out);
2880 
2881   // We know that the mangled name of the type starts at index 4 of the
2882   // mangled name of the typename, so we can just index into it in order to
2883   // get the mangled name of the type.
2884   llvm::Constant *Init = llvm::ConstantDataArray::getString(VMContext,
2885                                                             Name.substr(4));
2886   auto Align = CGM.getContext().getTypeAlignInChars(CGM.getContext().CharTy);
2887 
2888   llvm::GlobalVariable *GV = CGM.CreateOrReplaceCXXRuntimeVariable(
2889       Name, Init->getType(), Linkage, Align.getQuantity());
2890 
2891   GV->setInitializer(Init);
2892 
2893   return GV;
2894 }
2895 
2896 llvm::Constant *
2897 ItaniumRTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) {
2898   // Mangle the RTTI name.
2899   SmallString<256> Name;
2900   llvm::raw_svector_ostream Out(Name);
2901   CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out);
2902 
2903   // Look for an existing global.
2904   llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name);
2905 
2906   if (!GV) {
2907     // Create a new global variable.
2908     // Note for the future: If we would ever like to do deferred emission of
2909     // RTTI, check if emitting vtables opportunistically need any adjustment.
2910 
2911     GV = new llvm::GlobalVariable(CGM.getModule(), CGM.Int8PtrTy,
2912                                   /*isConstant=*/true,
2913                                   llvm::GlobalValue::ExternalLinkage, nullptr,
2914                                   Name);
2915     const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
2916     CGM.setGVProperties(GV, RD);
2917   }
2918 
2919   return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy);
2920 }
2921 
2922 /// TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type
2923 /// info for that type is defined in the standard library.
2924 static bool TypeInfoIsInStandardLibrary(const BuiltinType *Ty) {
2925   // Itanium C++ ABI 2.9.2:
2926   //   Basic type information (e.g. for "int", "bool", etc.) will be kept in
2927   //   the run-time support library. Specifically, the run-time support
2928   //   library should contain type_info objects for the types X, X* and
2929   //   X const*, for every X in: void, std::nullptr_t, bool, wchar_t, char,
2930   //   unsigned char, signed char, short, unsigned short, int, unsigned int,
2931   //   long, unsigned long, long long, unsigned long long, float, double,
2932   //   long double, char16_t, char32_t, and the IEEE 754r decimal and
2933   //   half-precision floating point types.
2934   //
2935   // GCC also emits RTTI for __int128.
2936   // FIXME: We do not emit RTTI information for decimal types here.
2937 
2938   // Types added here must also be added to EmitFundamentalRTTIDescriptors.
2939   switch (Ty->getKind()) {
2940     case BuiltinType::Void:
2941     case BuiltinType::NullPtr:
2942     case BuiltinType::Bool:
2943     case BuiltinType::WChar_S:
2944     case BuiltinType::WChar_U:
2945     case BuiltinType::Char_U:
2946     case BuiltinType::Char_S:
2947     case BuiltinType::UChar:
2948     case BuiltinType::SChar:
2949     case BuiltinType::Short:
2950     case BuiltinType::UShort:
2951     case BuiltinType::Int:
2952     case BuiltinType::UInt:
2953     case BuiltinType::Long:
2954     case BuiltinType::ULong:
2955     case BuiltinType::LongLong:
2956     case BuiltinType::ULongLong:
2957     case BuiltinType::Half:
2958     case BuiltinType::Float:
2959     case BuiltinType::Double:
2960     case BuiltinType::LongDouble:
2961     case BuiltinType::Float16:
2962     case BuiltinType::Float128:
2963     case BuiltinType::Char8:
2964     case BuiltinType::Char16:
2965     case BuiltinType::Char32:
2966     case BuiltinType::Int128:
2967     case BuiltinType::UInt128:
2968       return true;
2969 
2970 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
2971     case BuiltinType::Id:
2972 #include "clang/Basic/OpenCLImageTypes.def"
2973 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
2974     case BuiltinType::Id:
2975 #include "clang/Basic/OpenCLExtensionTypes.def"
2976     case BuiltinType::OCLSampler:
2977     case BuiltinType::OCLEvent:
2978     case BuiltinType::OCLClkEvent:
2979     case BuiltinType::OCLQueue:
2980     case BuiltinType::OCLReserveID:
2981 #define SVE_TYPE(Name, Id, SingletonId) \
2982     case BuiltinType::Id:
2983 #include "clang/Basic/AArch64SVEACLETypes.def"
2984     case BuiltinType::ShortAccum:
2985     case BuiltinType::Accum:
2986     case BuiltinType::LongAccum:
2987     case BuiltinType::UShortAccum:
2988     case BuiltinType::UAccum:
2989     case BuiltinType::ULongAccum:
2990     case BuiltinType::ShortFract:
2991     case BuiltinType::Fract:
2992     case BuiltinType::LongFract:
2993     case BuiltinType::UShortFract:
2994     case BuiltinType::UFract:
2995     case BuiltinType::ULongFract:
2996     case BuiltinType::SatShortAccum:
2997     case BuiltinType::SatAccum:
2998     case BuiltinType::SatLongAccum:
2999     case BuiltinType::SatUShortAccum:
3000     case BuiltinType::SatUAccum:
3001     case BuiltinType::SatULongAccum:
3002     case BuiltinType::SatShortFract:
3003     case BuiltinType::SatFract:
3004     case BuiltinType::SatLongFract:
3005     case BuiltinType::SatUShortFract:
3006     case BuiltinType::SatUFract:
3007     case BuiltinType::SatULongFract:
3008       return false;
3009 
3010     case BuiltinType::Dependent:
3011 #define BUILTIN_TYPE(Id, SingletonId)
3012 #define PLACEHOLDER_TYPE(Id, SingletonId) \
3013     case BuiltinType::Id:
3014 #include "clang/AST/BuiltinTypes.def"
3015       llvm_unreachable("asking for RRTI for a placeholder type!");
3016 
3017     case BuiltinType::ObjCId:
3018     case BuiltinType::ObjCClass:
3019     case BuiltinType::ObjCSel:
3020       llvm_unreachable("FIXME: Objective-C types are unsupported!");
3021   }
3022 
3023   llvm_unreachable("Invalid BuiltinType Kind!");
3024 }
3025 
3026 static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) {
3027   QualType PointeeTy = PointerTy->getPointeeType();
3028   const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy);
3029   if (!BuiltinTy)
3030     return false;
3031 
3032   // Check the qualifiers.
3033   Qualifiers Quals = PointeeTy.getQualifiers();
3034   Quals.removeConst();
3035 
3036   if (!Quals.empty())
3037     return false;
3038 
3039   return TypeInfoIsInStandardLibrary(BuiltinTy);
3040 }
3041 
3042 /// IsStandardLibraryRTTIDescriptor - Returns whether the type
3043 /// information for the given type exists in the standard library.
3044 static bool IsStandardLibraryRTTIDescriptor(QualType Ty) {
3045   // Type info for builtin types is defined in the standard library.
3046   if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty))
3047     return TypeInfoIsInStandardLibrary(BuiltinTy);
3048 
3049   // Type info for some pointer types to builtin types is defined in the
3050   // standard library.
3051   if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
3052     return TypeInfoIsInStandardLibrary(PointerTy);
3053 
3054   return false;
3055 }
3056 
3057 /// ShouldUseExternalRTTIDescriptor - Returns whether the type information for
3058 /// the given type exists somewhere else, and that we should not emit the type
3059 /// information in this translation unit.  Assumes that it is not a
3060 /// standard-library type.
3061 static bool ShouldUseExternalRTTIDescriptor(CodeGenModule &CGM,
3062                                             QualType Ty) {
3063   ASTContext &Context = CGM.getContext();
3064 
3065   // If RTTI is disabled, assume it might be disabled in the
3066   // translation unit that defines any potential key function, too.
3067   if (!Context.getLangOpts().RTTI) return false;
3068 
3069   if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
3070     const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
3071     if (!RD->hasDefinition())
3072       return false;
3073 
3074     if (!RD->isDynamicClass())
3075       return false;
3076 
3077     // FIXME: this may need to be reconsidered if the key function
3078     // changes.
3079     // N.B. We must always emit the RTTI data ourselves if there exists a key
3080     // function.
3081     bool IsDLLImport = RD->hasAttr<DLLImportAttr>();
3082 
3083     // Don't import the RTTI but emit it locally.
3084     if (CGM.getTriple().isWindowsGNUEnvironment())
3085       return false;
3086 
3087     if (CGM.getVTables().isVTableExternal(RD))
3088       return IsDLLImport && !CGM.getTriple().isWindowsItaniumEnvironment()
3089                  ? false
3090                  : true;
3091 
3092     if (IsDLLImport)
3093       return true;
3094   }
3095 
3096   return false;
3097 }
3098 
3099 /// IsIncompleteClassType - Returns whether the given record type is incomplete.
3100 static bool IsIncompleteClassType(const RecordType *RecordTy) {
3101   return !RecordTy->getDecl()->isCompleteDefinition();
3102 }
3103 
3104 /// ContainsIncompleteClassType - Returns whether the given type contains an
3105 /// incomplete class type. This is true if
3106 ///
3107 ///   * The given type is an incomplete class type.
3108 ///   * The given type is a pointer type whose pointee type contains an
3109 ///     incomplete class type.
3110 ///   * The given type is a member pointer type whose class is an incomplete
3111 ///     class type.
3112 ///   * The given type is a member pointer type whoise pointee type contains an
3113 ///     incomplete class type.
3114 /// is an indirect or direct pointer to an incomplete class type.
3115 static bool ContainsIncompleteClassType(QualType Ty) {
3116   if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
3117     if (IsIncompleteClassType(RecordTy))
3118       return true;
3119   }
3120 
3121   if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
3122     return ContainsIncompleteClassType(PointerTy->getPointeeType());
3123 
3124   if (const MemberPointerType *MemberPointerTy =
3125       dyn_cast<MemberPointerType>(Ty)) {
3126     // Check if the class type is incomplete.
3127     const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass());
3128     if (IsIncompleteClassType(ClassType))
3129       return true;
3130 
3131     return ContainsIncompleteClassType(MemberPointerTy->getPointeeType());
3132   }
3133 
3134   return false;
3135 }
3136 
3137 // CanUseSingleInheritance - Return whether the given record decl has a "single,
3138 // public, non-virtual base at offset zero (i.e. the derived class is dynamic
3139 // iff the base is)", according to Itanium C++ ABI, 2.95p6b.
3140 static bool CanUseSingleInheritance(const CXXRecordDecl *RD) {
3141   // Check the number of bases.
3142   if (RD->getNumBases() != 1)
3143     return false;
3144 
3145   // Get the base.
3146   CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin();
3147 
3148   // Check that the base is not virtual.
3149   if (Base->isVirtual())
3150     return false;
3151 
3152   // Check that the base is public.
3153   if (Base->getAccessSpecifier() != AS_public)
3154     return false;
3155 
3156   // Check that the class is dynamic iff the base is.
3157   auto *BaseDecl =
3158       cast<CXXRecordDecl>(Base->getType()->castAs<RecordType>()->getDecl());
3159   if (!BaseDecl->isEmpty() &&
3160       BaseDecl->isDynamicClass() != RD->isDynamicClass())
3161     return false;
3162 
3163   return true;
3164 }
3165 
3166 void ItaniumRTTIBuilder::BuildVTablePointer(const Type *Ty) {
3167   // abi::__class_type_info.
3168   static const char * const ClassTypeInfo =
3169     "_ZTVN10__cxxabiv117__class_type_infoE";
3170   // abi::__si_class_type_info.
3171   static const char * const SIClassTypeInfo =
3172     "_ZTVN10__cxxabiv120__si_class_type_infoE";
3173   // abi::__vmi_class_type_info.
3174   static const char * const VMIClassTypeInfo =
3175     "_ZTVN10__cxxabiv121__vmi_class_type_infoE";
3176 
3177   const char *VTableName = nullptr;
3178 
3179   switch (Ty->getTypeClass()) {
3180 #define TYPE(Class, Base)
3181 #define ABSTRACT_TYPE(Class, Base)
3182 #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
3183 #define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3184 #define DEPENDENT_TYPE(Class, Base) case Type::Class:
3185 #include "clang/AST/TypeNodes.inc"
3186     llvm_unreachable("Non-canonical and dependent types shouldn't get here");
3187 
3188   case Type::LValueReference:
3189   case Type::RValueReference:
3190     llvm_unreachable("References shouldn't get here");
3191 
3192   case Type::Auto:
3193   case Type::DeducedTemplateSpecialization:
3194     llvm_unreachable("Undeduced type shouldn't get here");
3195 
3196   case Type::Pipe:
3197     llvm_unreachable("Pipe types shouldn't get here");
3198 
3199   case Type::Builtin:
3200   // GCC treats vector and complex types as fundamental types.
3201   case Type::Vector:
3202   case Type::ExtVector:
3203   case Type::Complex:
3204   case Type::Atomic:
3205   // FIXME: GCC treats block pointers as fundamental types?!
3206   case Type::BlockPointer:
3207     // abi::__fundamental_type_info.
3208     VTableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE";
3209     break;
3210 
3211   case Type::ConstantArray:
3212   case Type::IncompleteArray:
3213   case Type::VariableArray:
3214     // abi::__array_type_info.
3215     VTableName = "_ZTVN10__cxxabiv117__array_type_infoE";
3216     break;
3217 
3218   case Type::FunctionNoProto:
3219   case Type::FunctionProto:
3220     // abi::__function_type_info.
3221     VTableName = "_ZTVN10__cxxabiv120__function_type_infoE";
3222     break;
3223 
3224   case Type::Enum:
3225     // abi::__enum_type_info.
3226     VTableName = "_ZTVN10__cxxabiv116__enum_type_infoE";
3227     break;
3228 
3229   case Type::Record: {
3230     const CXXRecordDecl *RD =
3231       cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
3232 
3233     if (!RD->hasDefinition() || !RD->getNumBases()) {
3234       VTableName = ClassTypeInfo;
3235     } else if (CanUseSingleInheritance(RD)) {
3236       VTableName = SIClassTypeInfo;
3237     } else {
3238       VTableName = VMIClassTypeInfo;
3239     }
3240 
3241     break;
3242   }
3243 
3244   case Type::ObjCObject:
3245     // Ignore protocol qualifiers.
3246     Ty = cast<ObjCObjectType>(Ty)->getBaseType().getTypePtr();
3247 
3248     // Handle id and Class.
3249     if (isa<BuiltinType>(Ty)) {
3250       VTableName = ClassTypeInfo;
3251       break;
3252     }
3253 
3254     assert(isa<ObjCInterfaceType>(Ty));
3255     LLVM_FALLTHROUGH;
3256 
3257   case Type::ObjCInterface:
3258     if (cast<ObjCInterfaceType>(Ty)->getDecl()->getSuperClass()) {
3259       VTableName = SIClassTypeInfo;
3260     } else {
3261       VTableName = ClassTypeInfo;
3262     }
3263     break;
3264 
3265   case Type::ObjCObjectPointer:
3266   case Type::Pointer:
3267     // abi::__pointer_type_info.
3268     VTableName = "_ZTVN10__cxxabiv119__pointer_type_infoE";
3269     break;
3270 
3271   case Type::MemberPointer:
3272     // abi::__pointer_to_member_type_info.
3273     VTableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE";
3274     break;
3275   }
3276 
3277   llvm::Constant *VTable =
3278     CGM.getModule().getOrInsertGlobal(VTableName, CGM.Int8PtrTy);
3279   CGM.setDSOLocal(cast<llvm::GlobalValue>(VTable->stripPointerCasts()));
3280 
3281   llvm::Type *PtrDiffTy =
3282     CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
3283 
3284   // The vtable address point is 2.
3285   llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
3286   VTable =
3287       llvm::ConstantExpr::getInBoundsGetElementPtr(CGM.Int8PtrTy, VTable, Two);
3288   VTable = llvm::ConstantExpr::getBitCast(VTable, CGM.Int8PtrTy);
3289 
3290   Fields.push_back(VTable);
3291 }
3292 
3293 /// Return the linkage that the type info and type info name constants
3294 /// should have for the given type.
3295 static llvm::GlobalVariable::LinkageTypes getTypeInfoLinkage(CodeGenModule &CGM,
3296                                                              QualType Ty) {
3297   // Itanium C++ ABI 2.9.5p7:
3298   //   In addition, it and all of the intermediate abi::__pointer_type_info
3299   //   structs in the chain down to the abi::__class_type_info for the
3300   //   incomplete class type must be prevented from resolving to the
3301   //   corresponding type_info structs for the complete class type, possibly
3302   //   by making them local static objects. Finally, a dummy class RTTI is
3303   //   generated for the incomplete type that will not resolve to the final
3304   //   complete class RTTI (because the latter need not exist), possibly by
3305   //   making it a local static object.
3306   if (ContainsIncompleteClassType(Ty))
3307     return llvm::GlobalValue::InternalLinkage;
3308 
3309   switch (Ty->getLinkage()) {
3310   case NoLinkage:
3311   case InternalLinkage:
3312   case UniqueExternalLinkage:
3313     return llvm::GlobalValue::InternalLinkage;
3314 
3315   case VisibleNoLinkage:
3316   case ModuleInternalLinkage:
3317   case ModuleLinkage:
3318   case ExternalLinkage:
3319     // RTTI is not enabled, which means that this type info struct is going
3320     // to be used for exception handling. Give it linkonce_odr linkage.
3321     if (!CGM.getLangOpts().RTTI)
3322       return llvm::GlobalValue::LinkOnceODRLinkage;
3323 
3324     if (const RecordType *Record = dyn_cast<RecordType>(Ty)) {
3325       const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl());
3326       if (RD->hasAttr<WeakAttr>())
3327         return llvm::GlobalValue::WeakODRLinkage;
3328       if (CGM.getTriple().isWindowsItaniumEnvironment())
3329         if (RD->hasAttr<DLLImportAttr>() &&
3330             ShouldUseExternalRTTIDescriptor(CGM, Ty))
3331           return llvm::GlobalValue::ExternalLinkage;
3332       // MinGW always uses LinkOnceODRLinkage for type info.
3333       if (RD->isDynamicClass() &&
3334           !CGM.getContext()
3335                .getTargetInfo()
3336                .getTriple()
3337                .isWindowsGNUEnvironment())
3338         return CGM.getVTableLinkage(RD);
3339     }
3340 
3341     return llvm::GlobalValue::LinkOnceODRLinkage;
3342   }
3343 
3344   llvm_unreachable("Invalid linkage!");
3345 }
3346 
3347 llvm::Constant *ItaniumRTTIBuilder::BuildTypeInfo(QualType Ty) {
3348   // We want to operate on the canonical type.
3349   Ty = Ty.getCanonicalType();
3350 
3351   // Check if we've already emitted an RTTI descriptor for this type.
3352   SmallString<256> Name;
3353   llvm::raw_svector_ostream Out(Name);
3354   CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out);
3355 
3356   llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
3357   if (OldGV && !OldGV->isDeclaration()) {
3358     assert(!OldGV->hasAvailableExternallyLinkage() &&
3359            "available_externally typeinfos not yet implemented");
3360 
3361     return llvm::ConstantExpr::getBitCast(OldGV, CGM.Int8PtrTy);
3362   }
3363 
3364   // Check if there is already an external RTTI descriptor for this type.
3365   if (IsStandardLibraryRTTIDescriptor(Ty) ||
3366       ShouldUseExternalRTTIDescriptor(CGM, Ty))
3367     return GetAddrOfExternalRTTIDescriptor(Ty);
3368 
3369   // Emit the standard library with external linkage.
3370   llvm::GlobalVariable::LinkageTypes Linkage = getTypeInfoLinkage(CGM, Ty);
3371 
3372   // Give the type_info object and name the formal visibility of the
3373   // type itself.
3374   llvm::GlobalValue::VisibilityTypes llvmVisibility;
3375   if (llvm::GlobalValue::isLocalLinkage(Linkage))
3376     // If the linkage is local, only default visibility makes sense.
3377     llvmVisibility = llvm::GlobalValue::DefaultVisibility;
3378   else if (CXXABI.classifyRTTIUniqueness(Ty, Linkage) ==
3379            ItaniumCXXABI::RUK_NonUniqueHidden)
3380     llvmVisibility = llvm::GlobalValue::HiddenVisibility;
3381   else
3382     llvmVisibility = CodeGenModule::GetLLVMVisibility(Ty->getVisibility());
3383 
3384   llvm::GlobalValue::DLLStorageClassTypes DLLStorageClass =
3385       llvm::GlobalValue::DefaultStorageClass;
3386   if (CGM.getTriple().isWindowsItaniumEnvironment()) {
3387     auto RD = Ty->getAsCXXRecordDecl();
3388     if (RD && RD->hasAttr<DLLExportAttr>())
3389       DLLStorageClass = llvm::GlobalValue::DLLExportStorageClass;
3390   }
3391 
3392   return BuildTypeInfo(Ty, Linkage, llvmVisibility, DLLStorageClass);
3393 }
3394 
3395 llvm::Constant *ItaniumRTTIBuilder::BuildTypeInfo(
3396       QualType Ty,
3397       llvm::GlobalVariable::LinkageTypes Linkage,
3398       llvm::GlobalValue::VisibilityTypes Visibility,
3399       llvm::GlobalValue::DLLStorageClassTypes DLLStorageClass) {
3400   // Add the vtable pointer.
3401   BuildVTablePointer(cast<Type>(Ty));
3402 
3403   // And the name.
3404   llvm::GlobalVariable *TypeName = GetAddrOfTypeName(Ty, Linkage);
3405   llvm::Constant *TypeNameField;
3406 
3407   // If we're supposed to demote the visibility, be sure to set a flag
3408   // to use a string comparison for type_info comparisons.
3409   ItaniumCXXABI::RTTIUniquenessKind RTTIUniqueness =
3410       CXXABI.classifyRTTIUniqueness(Ty, Linkage);
3411   if (RTTIUniqueness != ItaniumCXXABI::RUK_Unique) {
3412     // The flag is the sign bit, which on ARM64 is defined to be clear
3413     // for global pointers.  This is very ARM64-specific.
3414     TypeNameField = llvm::ConstantExpr::getPtrToInt(TypeName, CGM.Int64Ty);
3415     llvm::Constant *flag =
3416         llvm::ConstantInt::get(CGM.Int64Ty, ((uint64_t)1) << 63);
3417     TypeNameField = llvm::ConstantExpr::getAdd(TypeNameField, flag);
3418     TypeNameField =
3419         llvm::ConstantExpr::getIntToPtr(TypeNameField, CGM.Int8PtrTy);
3420   } else {
3421     TypeNameField = llvm::ConstantExpr::getBitCast(TypeName, CGM.Int8PtrTy);
3422   }
3423   Fields.push_back(TypeNameField);
3424 
3425   switch (Ty->getTypeClass()) {
3426 #define TYPE(Class, Base)
3427 #define ABSTRACT_TYPE(Class, Base)
3428 #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
3429 #define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3430 #define DEPENDENT_TYPE(Class, Base) case Type::Class:
3431 #include "clang/AST/TypeNodes.inc"
3432     llvm_unreachable("Non-canonical and dependent types shouldn't get here");
3433 
3434   // GCC treats vector types as fundamental types.
3435   case Type::Builtin:
3436   case Type::Vector:
3437   case Type::ExtVector:
3438   case Type::Complex:
3439   case Type::BlockPointer:
3440     // Itanium C++ ABI 2.9.5p4:
3441     // abi::__fundamental_type_info adds no data members to std::type_info.
3442     break;
3443 
3444   case Type::LValueReference:
3445   case Type::RValueReference:
3446     llvm_unreachable("References shouldn't get here");
3447 
3448   case Type::Auto:
3449   case Type::DeducedTemplateSpecialization:
3450     llvm_unreachable("Undeduced type shouldn't get here");
3451 
3452   case Type::Pipe:
3453     llvm_unreachable("Pipe type shouldn't get here");
3454 
3455   case Type::ConstantArray:
3456   case Type::IncompleteArray:
3457   case Type::VariableArray:
3458     // Itanium C++ ABI 2.9.5p5:
3459     // abi::__array_type_info adds no data members to std::type_info.
3460     break;
3461 
3462   case Type::FunctionNoProto:
3463   case Type::FunctionProto:
3464     // Itanium C++ ABI 2.9.5p5:
3465     // abi::__function_type_info adds no data members to std::type_info.
3466     break;
3467 
3468   case Type::Enum:
3469     // Itanium C++ ABI 2.9.5p5:
3470     // abi::__enum_type_info adds no data members to std::type_info.
3471     break;
3472 
3473   case Type::Record: {
3474     const CXXRecordDecl *RD =
3475       cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
3476     if (!RD->hasDefinition() || !RD->getNumBases()) {
3477       // We don't need to emit any fields.
3478       break;
3479     }
3480 
3481     if (CanUseSingleInheritance(RD))
3482       BuildSIClassTypeInfo(RD);
3483     else
3484       BuildVMIClassTypeInfo(RD);
3485 
3486     break;
3487   }
3488 
3489   case Type::ObjCObject:
3490   case Type::ObjCInterface:
3491     BuildObjCObjectTypeInfo(cast<ObjCObjectType>(Ty));
3492     break;
3493 
3494   case Type::ObjCObjectPointer:
3495     BuildPointerTypeInfo(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
3496     break;
3497 
3498   case Type::Pointer:
3499     BuildPointerTypeInfo(cast<PointerType>(Ty)->getPointeeType());
3500     break;
3501 
3502   case Type::MemberPointer:
3503     BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
3504     break;
3505 
3506   case Type::Atomic:
3507     // No fields, at least for the moment.
3508     break;
3509   }
3510 
3511   llvm::Constant *Init = llvm::ConstantStruct::getAnon(Fields);
3512 
3513   SmallString<256> Name;
3514   llvm::raw_svector_ostream Out(Name);
3515   CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out);
3516   llvm::Module &M = CGM.getModule();
3517   llvm::GlobalVariable *OldGV = M.getNamedGlobal(Name);
3518   llvm::GlobalVariable *GV =
3519       new llvm::GlobalVariable(M, Init->getType(),
3520                                /*isConstant=*/true, Linkage, Init, Name);
3521 
3522   // If there's already an old global variable, replace it with the new one.
3523   if (OldGV) {
3524     GV->takeName(OldGV);
3525     llvm::Constant *NewPtr =
3526       llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
3527     OldGV->replaceAllUsesWith(NewPtr);
3528     OldGV->eraseFromParent();
3529   }
3530 
3531   if (CGM.supportsCOMDAT() && GV->isWeakForLinker())
3532     GV->setComdat(M.getOrInsertComdat(GV->getName()));
3533 
3534   CharUnits Align =
3535       CGM.getContext().toCharUnitsFromBits(CGM.getTarget().getPointerAlign(0));
3536   GV->setAlignment(Align.getAsAlign());
3537 
3538   // The Itanium ABI specifies that type_info objects must be globally
3539   // unique, with one exception: if the type is an incomplete class
3540   // type or a (possibly indirect) pointer to one.  That exception
3541   // affects the general case of comparing type_info objects produced
3542   // by the typeid operator, which is why the comparison operators on
3543   // std::type_info generally use the type_info name pointers instead
3544   // of the object addresses.  However, the language's built-in uses
3545   // of RTTI generally require class types to be complete, even when
3546   // manipulating pointers to those class types.  This allows the
3547   // implementation of dynamic_cast to rely on address equality tests,
3548   // which is much faster.
3549 
3550   // All of this is to say that it's important that both the type_info
3551   // object and the type_info name be uniqued when weakly emitted.
3552 
3553   TypeName->setVisibility(Visibility);
3554   CGM.setDSOLocal(TypeName);
3555 
3556   GV->setVisibility(Visibility);
3557   CGM.setDSOLocal(GV);
3558 
3559   TypeName->setDLLStorageClass(DLLStorageClass);
3560   GV->setDLLStorageClass(DLLStorageClass);
3561 
3562   TypeName->setPartition(CGM.getCodeGenOpts().SymbolPartition);
3563   GV->setPartition(CGM.getCodeGenOpts().SymbolPartition);
3564 
3565   return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy);
3566 }
3567 
3568 /// BuildObjCObjectTypeInfo - Build the appropriate kind of type_info
3569 /// for the given Objective-C object type.
3570 void ItaniumRTTIBuilder::BuildObjCObjectTypeInfo(const ObjCObjectType *OT) {
3571   // Drop qualifiers.
3572   const Type *T = OT->getBaseType().getTypePtr();
3573   assert(isa<BuiltinType>(T) || isa<ObjCInterfaceType>(T));
3574 
3575   // The builtin types are abi::__class_type_infos and don't require
3576   // extra fields.
3577   if (isa<BuiltinType>(T)) return;
3578 
3579   ObjCInterfaceDecl *Class = cast<ObjCInterfaceType>(T)->getDecl();
3580   ObjCInterfaceDecl *Super = Class->getSuperClass();
3581 
3582   // Root classes are also __class_type_info.
3583   if (!Super) return;
3584 
3585   QualType SuperTy = CGM.getContext().getObjCInterfaceType(Super);
3586 
3587   // Everything else is single inheritance.
3588   llvm::Constant *BaseTypeInfo =
3589       ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(SuperTy);
3590   Fields.push_back(BaseTypeInfo);
3591 }
3592 
3593 /// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
3594 /// inheritance, according to the Itanium C++ ABI, 2.95p6b.
3595 void ItaniumRTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) {
3596   // Itanium C++ ABI 2.9.5p6b:
3597   // It adds to abi::__class_type_info a single member pointing to the
3598   // type_info structure for the base type,
3599   llvm::Constant *BaseTypeInfo =
3600     ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(RD->bases_begin()->getType());
3601   Fields.push_back(BaseTypeInfo);
3602 }
3603 
3604 namespace {
3605   /// SeenBases - Contains virtual and non-virtual bases seen when traversing
3606   /// a class hierarchy.
3607   struct SeenBases {
3608     llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases;
3609     llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases;
3610   };
3611 }
3612 
3613 /// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in
3614 /// abi::__vmi_class_type_info.
3615 ///
3616 static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base,
3617                                              SeenBases &Bases) {
3618 
3619   unsigned Flags = 0;
3620 
3621   auto *BaseDecl =
3622       cast<CXXRecordDecl>(Base->getType()->castAs<RecordType>()->getDecl());
3623 
3624   if (Base->isVirtual()) {
3625     // Mark the virtual base as seen.
3626     if (!Bases.VirtualBases.insert(BaseDecl).second) {
3627       // If this virtual base has been seen before, then the class is diamond
3628       // shaped.
3629       Flags |= ItaniumRTTIBuilder::VMI_DiamondShaped;
3630     } else {
3631       if (Bases.NonVirtualBases.count(BaseDecl))
3632         Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat;
3633     }
3634   } else {
3635     // Mark the non-virtual base as seen.
3636     if (!Bases.NonVirtualBases.insert(BaseDecl).second) {
3637       // If this non-virtual base has been seen before, then the class has non-
3638       // diamond shaped repeated inheritance.
3639       Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat;
3640     } else {
3641       if (Bases.VirtualBases.count(BaseDecl))
3642         Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat;
3643     }
3644   }
3645 
3646   // Walk all bases.
3647   for (const auto &I : BaseDecl->bases())
3648     Flags |= ComputeVMIClassTypeInfoFlags(&I, Bases);
3649 
3650   return Flags;
3651 }
3652 
3653 static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) {
3654   unsigned Flags = 0;
3655   SeenBases Bases;
3656 
3657   // Walk all bases.
3658   for (const auto &I : RD->bases())
3659     Flags |= ComputeVMIClassTypeInfoFlags(&I, Bases);
3660 
3661   return Flags;
3662 }
3663 
3664 /// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
3665 /// classes with bases that do not satisfy the abi::__si_class_type_info
3666 /// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
3667 void ItaniumRTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) {
3668   llvm::Type *UnsignedIntLTy =
3669     CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
3670 
3671   // Itanium C++ ABI 2.9.5p6c:
3672   //   __flags is a word with flags describing details about the class
3673   //   structure, which may be referenced by using the __flags_masks
3674   //   enumeration. These flags refer to both direct and indirect bases.
3675   unsigned Flags = ComputeVMIClassTypeInfoFlags(RD);
3676   Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
3677 
3678   // Itanium C++ ABI 2.9.5p6c:
3679   //   __base_count is a word with the number of direct proper base class
3680   //   descriptions that follow.
3681   Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases()));
3682 
3683   if (!RD->getNumBases())
3684     return;
3685 
3686   // Now add the base class descriptions.
3687 
3688   // Itanium C++ ABI 2.9.5p6c:
3689   //   __base_info[] is an array of base class descriptions -- one for every
3690   //   direct proper base. Each description is of the type:
3691   //
3692   //   struct abi::__base_class_type_info {
3693   //   public:
3694   //     const __class_type_info *__base_type;
3695   //     long __offset_flags;
3696   //
3697   //     enum __offset_flags_masks {
3698   //       __virtual_mask = 0x1,
3699   //       __public_mask = 0x2,
3700   //       __offset_shift = 8
3701   //     };
3702   //   };
3703 
3704   // If we're in mingw and 'long' isn't wide enough for a pointer, use 'long
3705   // long' instead of 'long' for __offset_flags. libstdc++abi uses long long on
3706   // LLP64 platforms.
3707   // FIXME: Consider updating libc++abi to match, and extend this logic to all
3708   // LLP64 platforms.
3709   QualType OffsetFlagsTy = CGM.getContext().LongTy;
3710   const TargetInfo &TI = CGM.getContext().getTargetInfo();
3711   if (TI.getTriple().isOSCygMing() && TI.getPointerWidth(0) > TI.getLongWidth())
3712     OffsetFlagsTy = CGM.getContext().LongLongTy;
3713   llvm::Type *OffsetFlagsLTy =
3714       CGM.getTypes().ConvertType(OffsetFlagsTy);
3715 
3716   for (const auto &Base : RD->bases()) {
3717     // The __base_type member points to the RTTI for the base type.
3718     Fields.push_back(ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(Base.getType()));
3719 
3720     auto *BaseDecl =
3721         cast<CXXRecordDecl>(Base.getType()->castAs<RecordType>()->getDecl());
3722 
3723     int64_t OffsetFlags = 0;
3724 
3725     // All but the lower 8 bits of __offset_flags are a signed offset.
3726     // For a non-virtual base, this is the offset in the object of the base
3727     // subobject. For a virtual base, this is the offset in the virtual table of
3728     // the virtual base offset for the virtual base referenced (negative).
3729     CharUnits Offset;
3730     if (Base.isVirtual())
3731       Offset =
3732         CGM.getItaniumVTableContext().getVirtualBaseOffsetOffset(RD, BaseDecl);
3733     else {
3734       const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
3735       Offset = Layout.getBaseClassOffset(BaseDecl);
3736     };
3737 
3738     OffsetFlags = uint64_t(Offset.getQuantity()) << 8;
3739 
3740     // The low-order byte of __offset_flags contains flags, as given by the
3741     // masks from the enumeration __offset_flags_masks.
3742     if (Base.isVirtual())
3743       OffsetFlags |= BCTI_Virtual;
3744     if (Base.getAccessSpecifier() == AS_public)
3745       OffsetFlags |= BCTI_Public;
3746 
3747     Fields.push_back(llvm::ConstantInt::get(OffsetFlagsLTy, OffsetFlags));
3748   }
3749 }
3750 
3751 /// Compute the flags for a __pbase_type_info, and remove the corresponding
3752 /// pieces from \p Type.
3753 static unsigned extractPBaseFlags(ASTContext &Ctx, QualType &Type) {
3754   unsigned Flags = 0;
3755 
3756   if (Type.isConstQualified())
3757     Flags |= ItaniumRTTIBuilder::PTI_Const;
3758   if (Type.isVolatileQualified())
3759     Flags |= ItaniumRTTIBuilder::PTI_Volatile;
3760   if (Type.isRestrictQualified())
3761     Flags |= ItaniumRTTIBuilder::PTI_Restrict;
3762   Type = Type.getUnqualifiedType();
3763 
3764   // Itanium C++ ABI 2.9.5p7:
3765   //   When the abi::__pbase_type_info is for a direct or indirect pointer to an
3766   //   incomplete class type, the incomplete target type flag is set.
3767   if (ContainsIncompleteClassType(Type))
3768     Flags |= ItaniumRTTIBuilder::PTI_Incomplete;
3769 
3770   if (auto *Proto = Type->getAs<FunctionProtoType>()) {
3771     if (Proto->isNothrow()) {
3772       Flags |= ItaniumRTTIBuilder::PTI_Noexcept;
3773       Type = Ctx.getFunctionTypeWithExceptionSpec(Type, EST_None);
3774     }
3775   }
3776 
3777   return Flags;
3778 }
3779 
3780 /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct,
3781 /// used for pointer types.
3782 void ItaniumRTTIBuilder::BuildPointerTypeInfo(QualType PointeeTy) {
3783   // Itanium C++ ABI 2.9.5p7:
3784   //   __flags is a flag word describing the cv-qualification and other
3785   //   attributes of the type pointed to
3786   unsigned Flags = extractPBaseFlags(CGM.getContext(), PointeeTy);
3787 
3788   llvm::Type *UnsignedIntLTy =
3789     CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
3790   Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
3791 
3792   // Itanium C++ ABI 2.9.5p7:
3793   //  __pointee is a pointer to the std::type_info derivation for the
3794   //  unqualified type being pointed to.
3795   llvm::Constant *PointeeTypeInfo =
3796       ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(PointeeTy);
3797   Fields.push_back(PointeeTypeInfo);
3798 }
3799 
3800 /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
3801 /// struct, used for member pointer types.
3802 void
3803 ItaniumRTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) {
3804   QualType PointeeTy = Ty->getPointeeType();
3805 
3806   // Itanium C++ ABI 2.9.5p7:
3807   //   __flags is a flag word describing the cv-qualification and other
3808   //   attributes of the type pointed to.
3809   unsigned Flags = extractPBaseFlags(CGM.getContext(), PointeeTy);
3810 
3811   const RecordType *ClassType = cast<RecordType>(Ty->getClass());
3812   if (IsIncompleteClassType(ClassType))
3813     Flags |= PTI_ContainingClassIncomplete;
3814 
3815   llvm::Type *UnsignedIntLTy =
3816     CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
3817   Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
3818 
3819   // Itanium C++ ABI 2.9.5p7:
3820   //   __pointee is a pointer to the std::type_info derivation for the
3821   //   unqualified type being pointed to.
3822   llvm::Constant *PointeeTypeInfo =
3823       ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(PointeeTy);
3824   Fields.push_back(PointeeTypeInfo);
3825 
3826   // Itanium C++ ABI 2.9.5p9:
3827   //   __context is a pointer to an abi::__class_type_info corresponding to the
3828   //   class type containing the member pointed to
3829   //   (e.g., the "A" in "int A::*").
3830   Fields.push_back(
3831       ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(QualType(ClassType, 0)));
3832 }
3833 
3834 llvm::Constant *ItaniumCXXABI::getAddrOfRTTIDescriptor(QualType Ty) {
3835   return ItaniumRTTIBuilder(*this).BuildTypeInfo(Ty);
3836 }
3837 
3838 void ItaniumCXXABI::EmitFundamentalRTTIDescriptors(const CXXRecordDecl *RD) {
3839   // Types added here must also be added to TypeInfoIsInStandardLibrary.
3840   QualType FundamentalTypes[] = {
3841       getContext().VoidTy,             getContext().NullPtrTy,
3842       getContext().BoolTy,             getContext().WCharTy,
3843       getContext().CharTy,             getContext().UnsignedCharTy,
3844       getContext().SignedCharTy,       getContext().ShortTy,
3845       getContext().UnsignedShortTy,    getContext().IntTy,
3846       getContext().UnsignedIntTy,      getContext().LongTy,
3847       getContext().UnsignedLongTy,     getContext().LongLongTy,
3848       getContext().UnsignedLongLongTy, getContext().Int128Ty,
3849       getContext().UnsignedInt128Ty,   getContext().HalfTy,
3850       getContext().FloatTy,            getContext().DoubleTy,
3851       getContext().LongDoubleTy,       getContext().Float128Ty,
3852       getContext().Char8Ty,            getContext().Char16Ty,
3853       getContext().Char32Ty
3854   };
3855   llvm::GlobalValue::DLLStorageClassTypes DLLStorageClass =
3856       RD->hasAttr<DLLExportAttr>()
3857       ? llvm::GlobalValue::DLLExportStorageClass
3858       : llvm::GlobalValue::DefaultStorageClass;
3859   llvm::GlobalValue::VisibilityTypes Visibility =
3860       CodeGenModule::GetLLVMVisibility(RD->getVisibility());
3861   for (const QualType &FundamentalType : FundamentalTypes) {
3862     QualType PointerType = getContext().getPointerType(FundamentalType);
3863     QualType PointerTypeConst = getContext().getPointerType(
3864         FundamentalType.withConst());
3865     for (QualType Type : {FundamentalType, PointerType, PointerTypeConst})
3866       ItaniumRTTIBuilder(*this).BuildTypeInfo(
3867           Type, llvm::GlobalValue::ExternalLinkage,
3868           Visibility, DLLStorageClass);
3869   }
3870 }
3871 
3872 /// What sort of uniqueness rules should we use for the RTTI for the
3873 /// given type?
3874 ItaniumCXXABI::RTTIUniquenessKind ItaniumCXXABI::classifyRTTIUniqueness(
3875     QualType CanTy, llvm::GlobalValue::LinkageTypes Linkage) const {
3876   if (shouldRTTIBeUnique())
3877     return RUK_Unique;
3878 
3879   // It's only necessary for linkonce_odr or weak_odr linkage.
3880   if (Linkage != llvm::GlobalValue::LinkOnceODRLinkage &&
3881       Linkage != llvm::GlobalValue::WeakODRLinkage)
3882     return RUK_Unique;
3883 
3884   // It's only necessary with default visibility.
3885   if (CanTy->getVisibility() != DefaultVisibility)
3886     return RUK_Unique;
3887 
3888   // If we're not required to publish this symbol, hide it.
3889   if (Linkage == llvm::GlobalValue::LinkOnceODRLinkage)
3890     return RUK_NonUniqueHidden;
3891 
3892   // If we're required to publish this symbol, as we might be under an
3893   // explicit instantiation, leave it with default visibility but
3894   // enable string-comparisons.
3895   assert(Linkage == llvm::GlobalValue::WeakODRLinkage);
3896   return RUK_NonUniqueVisible;
3897 }
3898 
3899 // Find out how to codegen the complete destructor and constructor
3900 namespace {
3901 enum class StructorCodegen { Emit, RAUW, Alias, COMDAT };
3902 }
3903 static StructorCodegen getCodegenToUse(CodeGenModule &CGM,
3904                                        const CXXMethodDecl *MD) {
3905   if (!CGM.getCodeGenOpts().CXXCtorDtorAliases)
3906     return StructorCodegen::Emit;
3907 
3908   // The complete and base structors are not equivalent if there are any virtual
3909   // bases, so emit separate functions.
3910   if (MD->getParent()->getNumVBases())
3911     return StructorCodegen::Emit;
3912 
3913   GlobalDecl AliasDecl;
3914   if (const auto *DD = dyn_cast<CXXDestructorDecl>(MD)) {
3915     AliasDecl = GlobalDecl(DD, Dtor_Complete);
3916   } else {
3917     const auto *CD = cast<CXXConstructorDecl>(MD);
3918     AliasDecl = GlobalDecl(CD, Ctor_Complete);
3919   }
3920   llvm::GlobalValue::LinkageTypes Linkage = CGM.getFunctionLinkage(AliasDecl);
3921 
3922   if (llvm::GlobalValue::isDiscardableIfUnused(Linkage))
3923     return StructorCodegen::RAUW;
3924 
3925   // FIXME: Should we allow available_externally aliases?
3926   if (!llvm::GlobalAlias::isValidLinkage(Linkage))
3927     return StructorCodegen::RAUW;
3928 
3929   if (llvm::GlobalValue::isWeakForLinker(Linkage)) {
3930     // Only ELF and wasm support COMDATs with arbitrary names (C5/D5).
3931     if (CGM.getTarget().getTriple().isOSBinFormatELF() ||
3932         CGM.getTarget().getTriple().isOSBinFormatWasm())
3933       return StructorCodegen::COMDAT;
3934     return StructorCodegen::Emit;
3935   }
3936 
3937   return StructorCodegen::Alias;
3938 }
3939 
3940 static void emitConstructorDestructorAlias(CodeGenModule &CGM,
3941                                            GlobalDecl AliasDecl,
3942                                            GlobalDecl TargetDecl) {
3943   llvm::GlobalValue::LinkageTypes Linkage = CGM.getFunctionLinkage(AliasDecl);
3944 
3945   StringRef MangledName = CGM.getMangledName(AliasDecl);
3946   llvm::GlobalValue *Entry = CGM.GetGlobalValue(MangledName);
3947   if (Entry && !Entry->isDeclaration())
3948     return;
3949 
3950   auto *Aliasee = cast<llvm::GlobalValue>(CGM.GetAddrOfGlobal(TargetDecl));
3951 
3952   // Create the alias with no name.
3953   auto *Alias = llvm::GlobalAlias::create(Linkage, "", Aliasee);
3954 
3955   // Constructors and destructors are always unnamed_addr.
3956   Alias->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3957 
3958   // Switch any previous uses to the alias.
3959   if (Entry) {
3960     assert(Entry->getType() == Aliasee->getType() &&
3961            "declaration exists with different type");
3962     Alias->takeName(Entry);
3963     Entry->replaceAllUsesWith(Alias);
3964     Entry->eraseFromParent();
3965   } else {
3966     Alias->setName(MangledName);
3967   }
3968 
3969   // Finally, set up the alias with its proper name and attributes.
3970   CGM.SetCommonAttributes(AliasDecl, Alias);
3971 }
3972 
3973 void ItaniumCXXABI::emitCXXStructor(GlobalDecl GD) {
3974   auto *MD = cast<CXXMethodDecl>(GD.getDecl());
3975   auto *CD = dyn_cast<CXXConstructorDecl>(MD);
3976   const CXXDestructorDecl *DD = CD ? nullptr : cast<CXXDestructorDecl>(MD);
3977 
3978   StructorCodegen CGType = getCodegenToUse(CGM, MD);
3979 
3980   if (CD ? GD.getCtorType() == Ctor_Complete
3981          : GD.getDtorType() == Dtor_Complete) {
3982     GlobalDecl BaseDecl;
3983     if (CD)
3984       BaseDecl = GD.getWithCtorType(Ctor_Base);
3985     else
3986       BaseDecl = GD.getWithDtorType(Dtor_Base);
3987 
3988     if (CGType == StructorCodegen::Alias || CGType == StructorCodegen::COMDAT) {
3989       emitConstructorDestructorAlias(CGM, GD, BaseDecl);
3990       return;
3991     }
3992 
3993     if (CGType == StructorCodegen::RAUW) {
3994       StringRef MangledName = CGM.getMangledName(GD);
3995       auto *Aliasee = CGM.GetAddrOfGlobal(BaseDecl);
3996       CGM.addReplacement(MangledName, Aliasee);
3997       return;
3998     }
3999   }
4000 
4001   // The base destructor is equivalent to the base destructor of its
4002   // base class if there is exactly one non-virtual base class with a
4003   // non-trivial destructor, there are no fields with a non-trivial
4004   // destructor, and the body of the destructor is trivial.
4005   if (DD && GD.getDtorType() == Dtor_Base &&
4006       CGType != StructorCodegen::COMDAT &&
4007       !CGM.TryEmitBaseDestructorAsAlias(DD))
4008     return;
4009 
4010   // FIXME: The deleting destructor is equivalent to the selected operator
4011   // delete if:
4012   //  * either the delete is a destroying operator delete or the destructor
4013   //    would be trivial if it weren't virtual,
4014   //  * the conversion from the 'this' parameter to the first parameter of the
4015   //    destructor is equivalent to a bitcast,
4016   //  * the destructor does not have an implicit "this" return, and
4017   //  * the operator delete has the same calling convention and IR function type
4018   //    as the destructor.
4019   // In such cases we should try to emit the deleting dtor as an alias to the
4020   // selected 'operator delete'.
4021 
4022   llvm::Function *Fn = CGM.codegenCXXStructor(GD);
4023 
4024   if (CGType == StructorCodegen::COMDAT) {
4025     SmallString<256> Buffer;
4026     llvm::raw_svector_ostream Out(Buffer);
4027     if (DD)
4028       getMangleContext().mangleCXXDtorComdat(DD, Out);
4029     else
4030       getMangleContext().mangleCXXCtorComdat(CD, Out);
4031     llvm::Comdat *C = CGM.getModule().getOrInsertComdat(Out.str());
4032     Fn->setComdat(C);
4033   } else {
4034     CGM.maybeSetTrivialComdat(*MD, *Fn);
4035   }
4036 }
4037 
4038 static llvm::FunctionCallee getBeginCatchFn(CodeGenModule &CGM) {
4039   // void *__cxa_begin_catch(void*);
4040   llvm::FunctionType *FTy = llvm::FunctionType::get(
4041       CGM.Int8PtrTy, CGM.Int8PtrTy, /*isVarArg=*/false);
4042 
4043   return CGM.CreateRuntimeFunction(FTy, "__cxa_begin_catch");
4044 }
4045 
4046 static llvm::FunctionCallee getEndCatchFn(CodeGenModule &CGM) {
4047   // void __cxa_end_catch();
4048   llvm::FunctionType *FTy =
4049       llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
4050 
4051   return CGM.CreateRuntimeFunction(FTy, "__cxa_end_catch");
4052 }
4053 
4054 static llvm::FunctionCallee getGetExceptionPtrFn(CodeGenModule &CGM) {
4055   // void *__cxa_get_exception_ptr(void*);
4056   llvm::FunctionType *FTy = llvm::FunctionType::get(
4057       CGM.Int8PtrTy, CGM.Int8PtrTy, /*isVarArg=*/false);
4058 
4059   return CGM.CreateRuntimeFunction(FTy, "__cxa_get_exception_ptr");
4060 }
4061 
4062 namespace {
4063   /// A cleanup to call __cxa_end_catch.  In many cases, the caught
4064   /// exception type lets us state definitively that the thrown exception
4065   /// type does not have a destructor.  In particular:
4066   ///   - Catch-alls tell us nothing, so we have to conservatively
4067   ///     assume that the thrown exception might have a destructor.
4068   ///   - Catches by reference behave according to their base types.
4069   ///   - Catches of non-record types will only trigger for exceptions
4070   ///     of non-record types, which never have destructors.
4071   ///   - Catches of record types can trigger for arbitrary subclasses
4072   ///     of the caught type, so we have to assume the actual thrown
4073   ///     exception type might have a throwing destructor, even if the
4074   ///     caught type's destructor is trivial or nothrow.
4075   struct CallEndCatch final : EHScopeStack::Cleanup {
4076     CallEndCatch(bool MightThrow) : MightThrow(MightThrow) {}
4077     bool MightThrow;
4078 
4079     void Emit(CodeGenFunction &CGF, Flags flags) override {
4080       if (!MightThrow) {
4081         CGF.EmitNounwindRuntimeCall(getEndCatchFn(CGF.CGM));
4082         return;
4083       }
4084 
4085       CGF.EmitRuntimeCallOrInvoke(getEndCatchFn(CGF.CGM));
4086     }
4087   };
4088 }
4089 
4090 /// Emits a call to __cxa_begin_catch and enters a cleanup to call
4091 /// __cxa_end_catch.
4092 ///
4093 /// \param EndMightThrow - true if __cxa_end_catch might throw
4094 static llvm::Value *CallBeginCatch(CodeGenFunction &CGF,
4095                                    llvm::Value *Exn,
4096                                    bool EndMightThrow) {
4097   llvm::CallInst *call =
4098     CGF.EmitNounwindRuntimeCall(getBeginCatchFn(CGF.CGM), Exn);
4099 
4100   CGF.EHStack.pushCleanup<CallEndCatch>(NormalAndEHCleanup, EndMightThrow);
4101 
4102   return call;
4103 }
4104 
4105 /// A "special initializer" callback for initializing a catch
4106 /// parameter during catch initialization.
4107 static void InitCatchParam(CodeGenFunction &CGF,
4108                            const VarDecl &CatchParam,
4109                            Address ParamAddr,
4110                            SourceLocation Loc) {
4111   // Load the exception from where the landing pad saved it.
4112   llvm::Value *Exn = CGF.getExceptionFromSlot();
4113 
4114   CanQualType CatchType =
4115     CGF.CGM.getContext().getCanonicalType(CatchParam.getType());
4116   llvm::Type *LLVMCatchTy = CGF.ConvertTypeForMem(CatchType);
4117 
4118   // If we're catching by reference, we can just cast the object
4119   // pointer to the appropriate pointer.
4120   if (isa<ReferenceType>(CatchType)) {
4121     QualType CaughtType = cast<ReferenceType>(CatchType)->getPointeeType();
4122     bool EndCatchMightThrow = CaughtType->isRecordType();
4123 
4124     // __cxa_begin_catch returns the adjusted object pointer.
4125     llvm::Value *AdjustedExn = CallBeginCatch(CGF, Exn, EndCatchMightThrow);
4126 
4127     // We have no way to tell the personality function that we're
4128     // catching by reference, so if we're catching a pointer,
4129     // __cxa_begin_catch will actually return that pointer by value.
4130     if (const PointerType *PT = dyn_cast<PointerType>(CaughtType)) {
4131       QualType PointeeType = PT->getPointeeType();
4132 
4133       // When catching by reference, generally we should just ignore
4134       // this by-value pointer and use the exception object instead.
4135       if (!PointeeType->isRecordType()) {
4136 
4137         // Exn points to the struct _Unwind_Exception header, which
4138         // we have to skip past in order to reach the exception data.
4139         unsigned HeaderSize =
4140           CGF.CGM.getTargetCodeGenInfo().getSizeOfUnwindException();
4141         AdjustedExn = CGF.Builder.CreateConstGEP1_32(Exn, HeaderSize);
4142 
4143       // However, if we're catching a pointer-to-record type that won't
4144       // work, because the personality function might have adjusted
4145       // the pointer.  There's actually no way for us to fully satisfy
4146       // the language/ABI contract here:  we can't use Exn because it
4147       // might have the wrong adjustment, but we can't use the by-value
4148       // pointer because it's off by a level of abstraction.
4149       //
4150       // The current solution is to dump the adjusted pointer into an
4151       // alloca, which breaks language semantics (because changing the
4152       // pointer doesn't change the exception) but at least works.
4153       // The better solution would be to filter out non-exact matches
4154       // and rethrow them, but this is tricky because the rethrow
4155       // really needs to be catchable by other sites at this landing
4156       // pad.  The best solution is to fix the personality function.
4157       } else {
4158         // Pull the pointer for the reference type off.
4159         llvm::Type *PtrTy =
4160           cast<llvm::PointerType>(LLVMCatchTy)->getElementType();
4161 
4162         // Create the temporary and write the adjusted pointer into it.
4163         Address ExnPtrTmp =
4164           CGF.CreateTempAlloca(PtrTy, CGF.getPointerAlign(), "exn.byref.tmp");
4165         llvm::Value *Casted = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy);
4166         CGF.Builder.CreateStore(Casted, ExnPtrTmp);
4167 
4168         // Bind the reference to the temporary.
4169         AdjustedExn = ExnPtrTmp.getPointer();
4170       }
4171     }
4172 
4173     llvm::Value *ExnCast =
4174       CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.byref");
4175     CGF.Builder.CreateStore(ExnCast, ParamAddr);
4176     return;
4177   }
4178 
4179   // Scalars and complexes.
4180   TypeEvaluationKind TEK = CGF.getEvaluationKind(CatchType);
4181   if (TEK != TEK_Aggregate) {
4182     llvm::Value *AdjustedExn = CallBeginCatch(CGF, Exn, false);
4183 
4184     // If the catch type is a pointer type, __cxa_begin_catch returns
4185     // the pointer by value.
4186     if (CatchType->hasPointerRepresentation()) {
4187       llvm::Value *CastExn =
4188         CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.casted");
4189 
4190       switch (CatchType.getQualifiers().getObjCLifetime()) {
4191       case Qualifiers::OCL_Strong:
4192         CastExn = CGF.EmitARCRetainNonBlock(CastExn);
4193         LLVM_FALLTHROUGH;
4194 
4195       case Qualifiers::OCL_None:
4196       case Qualifiers::OCL_ExplicitNone:
4197       case Qualifiers::OCL_Autoreleasing:
4198         CGF.Builder.CreateStore(CastExn, ParamAddr);
4199         return;
4200 
4201       case Qualifiers::OCL_Weak:
4202         CGF.EmitARCInitWeak(ParamAddr, CastExn);
4203         return;
4204       }
4205       llvm_unreachable("bad ownership qualifier!");
4206     }
4207 
4208     // Otherwise, it returns a pointer into the exception object.
4209 
4210     llvm::Type *PtrTy = LLVMCatchTy->getPointerTo(0); // addrspace 0 ok
4211     llvm::Value *Cast = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy);
4212 
4213     LValue srcLV = CGF.MakeNaturalAlignAddrLValue(Cast, CatchType);
4214     LValue destLV = CGF.MakeAddrLValue(ParamAddr, CatchType);
4215     switch (TEK) {
4216     case TEK_Complex:
4217       CGF.EmitStoreOfComplex(CGF.EmitLoadOfComplex(srcLV, Loc), destLV,
4218                              /*init*/ true);
4219       return;
4220     case TEK_Scalar: {
4221       llvm::Value *ExnLoad = CGF.EmitLoadOfScalar(srcLV, Loc);
4222       CGF.EmitStoreOfScalar(ExnLoad, destLV, /*init*/ true);
4223       return;
4224     }
4225     case TEK_Aggregate:
4226       llvm_unreachable("evaluation kind filtered out!");
4227     }
4228     llvm_unreachable("bad evaluation kind");
4229   }
4230 
4231   assert(isa<RecordType>(CatchType) && "unexpected catch type!");
4232   auto catchRD = CatchType->getAsCXXRecordDecl();
4233   CharUnits caughtExnAlignment = CGF.CGM.getClassPointerAlignment(catchRD);
4234 
4235   llvm::Type *PtrTy = LLVMCatchTy->getPointerTo(0); // addrspace 0 ok
4236 
4237   // Check for a copy expression.  If we don't have a copy expression,
4238   // that means a trivial copy is okay.
4239   const Expr *copyExpr = CatchParam.getInit();
4240   if (!copyExpr) {
4241     llvm::Value *rawAdjustedExn = CallBeginCatch(CGF, Exn, true);
4242     Address adjustedExn(CGF.Builder.CreateBitCast(rawAdjustedExn, PtrTy),
4243                         caughtExnAlignment);
4244     LValue Dest = CGF.MakeAddrLValue(ParamAddr, CatchType);
4245     LValue Src = CGF.MakeAddrLValue(adjustedExn, CatchType);
4246     CGF.EmitAggregateCopy(Dest, Src, CatchType, AggValueSlot::DoesNotOverlap);
4247     return;
4248   }
4249 
4250   // We have to call __cxa_get_exception_ptr to get the adjusted
4251   // pointer before copying.
4252   llvm::CallInst *rawAdjustedExn =
4253     CGF.EmitNounwindRuntimeCall(getGetExceptionPtrFn(CGF.CGM), Exn);
4254 
4255   // Cast that to the appropriate type.
4256   Address adjustedExn(CGF.Builder.CreateBitCast(rawAdjustedExn, PtrTy),
4257                       caughtExnAlignment);
4258 
4259   // The copy expression is defined in terms of an OpaqueValueExpr.
4260   // Find it and map it to the adjusted expression.
4261   CodeGenFunction::OpaqueValueMapping
4262     opaque(CGF, OpaqueValueExpr::findInCopyConstruct(copyExpr),
4263            CGF.MakeAddrLValue(adjustedExn, CatchParam.getType()));
4264 
4265   // Call the copy ctor in a terminate scope.
4266   CGF.EHStack.pushTerminate();
4267 
4268   // Perform the copy construction.
4269   CGF.EmitAggExpr(copyExpr,
4270                   AggValueSlot::forAddr(ParamAddr, Qualifiers(),
4271                                         AggValueSlot::IsNotDestructed,
4272                                         AggValueSlot::DoesNotNeedGCBarriers,
4273                                         AggValueSlot::IsNotAliased,
4274                                         AggValueSlot::DoesNotOverlap));
4275 
4276   // Leave the terminate scope.
4277   CGF.EHStack.popTerminate();
4278 
4279   // Undo the opaque value mapping.
4280   opaque.pop();
4281 
4282   // Finally we can call __cxa_begin_catch.
4283   CallBeginCatch(CGF, Exn, true);
4284 }
4285 
4286 /// Begins a catch statement by initializing the catch variable and
4287 /// calling __cxa_begin_catch.
4288 void ItaniumCXXABI::emitBeginCatch(CodeGenFunction &CGF,
4289                                    const CXXCatchStmt *S) {
4290   // We have to be very careful with the ordering of cleanups here:
4291   //   C++ [except.throw]p4:
4292   //     The destruction [of the exception temporary] occurs
4293   //     immediately after the destruction of the object declared in
4294   //     the exception-declaration in the handler.
4295   //
4296   // So the precise ordering is:
4297   //   1.  Construct catch variable.
4298   //   2.  __cxa_begin_catch
4299   //   3.  Enter __cxa_end_catch cleanup
4300   //   4.  Enter dtor cleanup
4301   //
4302   // We do this by using a slightly abnormal initialization process.
4303   // Delegation sequence:
4304   //   - ExitCXXTryStmt opens a RunCleanupsScope
4305   //     - EmitAutoVarAlloca creates the variable and debug info
4306   //       - InitCatchParam initializes the variable from the exception
4307   //       - CallBeginCatch calls __cxa_begin_catch
4308   //       - CallBeginCatch enters the __cxa_end_catch cleanup
4309   //     - EmitAutoVarCleanups enters the variable destructor cleanup
4310   //   - EmitCXXTryStmt emits the code for the catch body
4311   //   - EmitCXXTryStmt close the RunCleanupsScope
4312 
4313   VarDecl *CatchParam = S->getExceptionDecl();
4314   if (!CatchParam) {
4315     llvm::Value *Exn = CGF.getExceptionFromSlot();
4316     CallBeginCatch(CGF, Exn, true);
4317     return;
4318   }
4319 
4320   // Emit the local.
4321   CodeGenFunction::AutoVarEmission var = CGF.EmitAutoVarAlloca(*CatchParam);
4322   InitCatchParam(CGF, *CatchParam, var.getObjectAddress(CGF), S->getBeginLoc());
4323   CGF.EmitAutoVarCleanups(var);
4324 }
4325 
4326 /// Get or define the following function:
4327 ///   void @__clang_call_terminate(i8* %exn) nounwind noreturn
4328 /// This code is used only in C++.
4329 static llvm::FunctionCallee getClangCallTerminateFn(CodeGenModule &CGM) {
4330   llvm::FunctionType *fnTy =
4331     llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*isVarArg=*/false);
4332   llvm::FunctionCallee fnRef = CGM.CreateRuntimeFunction(
4333       fnTy, "__clang_call_terminate", llvm::AttributeList(), /*Local=*/true);
4334   llvm::Function *fn =
4335       cast<llvm::Function>(fnRef.getCallee()->stripPointerCasts());
4336   if (fn->empty()) {
4337     fn->setDoesNotThrow();
4338     fn->setDoesNotReturn();
4339 
4340     // What we really want is to massively penalize inlining without
4341     // forbidding it completely.  The difference between that and
4342     // 'noinline' is negligible.
4343     fn->addFnAttr(llvm::Attribute::NoInline);
4344 
4345     // Allow this function to be shared across translation units, but
4346     // we don't want it to turn into an exported symbol.
4347     fn->setLinkage(llvm::Function::LinkOnceODRLinkage);
4348     fn->setVisibility(llvm::Function::HiddenVisibility);
4349     if (CGM.supportsCOMDAT())
4350       fn->setComdat(CGM.getModule().getOrInsertComdat(fn->getName()));
4351 
4352     // Set up the function.
4353     llvm::BasicBlock *entry =
4354         llvm::BasicBlock::Create(CGM.getLLVMContext(), "", fn);
4355     CGBuilderTy builder(CGM, entry);
4356 
4357     // Pull the exception pointer out of the parameter list.
4358     llvm::Value *exn = &*fn->arg_begin();
4359 
4360     // Call __cxa_begin_catch(exn).
4361     llvm::CallInst *catchCall = builder.CreateCall(getBeginCatchFn(CGM), exn);
4362     catchCall->setDoesNotThrow();
4363     catchCall->setCallingConv(CGM.getRuntimeCC());
4364 
4365     // Call std::terminate().
4366     llvm::CallInst *termCall = builder.CreateCall(CGM.getTerminateFn());
4367     termCall->setDoesNotThrow();
4368     termCall->setDoesNotReturn();
4369     termCall->setCallingConv(CGM.getRuntimeCC());
4370 
4371     // std::terminate cannot return.
4372     builder.CreateUnreachable();
4373   }
4374   return fnRef;
4375 }
4376 
4377 llvm::CallInst *
4378 ItaniumCXXABI::emitTerminateForUnexpectedException(CodeGenFunction &CGF,
4379                                                    llvm::Value *Exn) {
4380   // In C++, we want to call __cxa_begin_catch() before terminating.
4381   if (Exn) {
4382     assert(CGF.CGM.getLangOpts().CPlusPlus);
4383     return CGF.EmitNounwindRuntimeCall(getClangCallTerminateFn(CGF.CGM), Exn);
4384   }
4385   return CGF.EmitNounwindRuntimeCall(CGF.CGM.getTerminateFn());
4386 }
4387 
4388 std::pair<llvm::Value *, const CXXRecordDecl *>
4389 ItaniumCXXABI::LoadVTablePtr(CodeGenFunction &CGF, Address This,
4390                              const CXXRecordDecl *RD) {
4391   return {CGF.GetVTablePtr(This, CGM.Int8PtrTy, RD), RD};
4392 }
4393 
4394 void WebAssemblyCXXABI::emitBeginCatch(CodeGenFunction &CGF,
4395                                        const CXXCatchStmt *C) {
4396   if (CGF.getTarget().hasFeature("exception-handling"))
4397     CGF.EHStack.pushCleanup<CatchRetScope>(
4398         NormalCleanup, cast<llvm::CatchPadInst>(CGF.CurrentFuncletPad));
4399   ItaniumCXXABI::emitBeginCatch(CGF, C);
4400 }
4401