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