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