xref: /llvm-project/clang/lib/CodeGen/CodeGenModule.h (revision 7c814c13d0df6dbd0ef6a8b2be214d3f6edbb566)
1 //===--- CodeGenModule.h - Per-Module state for LLVM CodeGen ----*- C++ -*-===//
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 is the internal per-translation-unit state used for llvm translation.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H
14 #define LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H
15 
16 #include "CGVTables.h"
17 #include "CodeGenTypeCache.h"
18 #include "CodeGenTypes.h"
19 #include "SanitizerMetadata.h"
20 #include "clang/AST/DeclCXX.h"
21 #include "clang/AST/DeclObjC.h"
22 #include "clang/AST/DeclOpenMP.h"
23 #include "clang/AST/GlobalDecl.h"
24 #include "clang/AST/Mangle.h"
25 #include "clang/Basic/ABI.h"
26 #include "clang/Basic/LangOptions.h"
27 #include "clang/Basic/NoSanitizeList.h"
28 #include "clang/Basic/ProfileList.h"
29 #include "clang/Basic/TargetInfo.h"
30 #include "clang/Basic/XRayLists.h"
31 #include "clang/Lex/PreprocessorOptions.h"
32 #include "llvm/ADT/DenseMap.h"
33 #include "llvm/ADT/MapVector.h"
34 #include "llvm/ADT/SetVector.h"
35 #include "llvm/ADT/SmallPtrSet.h"
36 #include "llvm/ADT/StringMap.h"
37 #include "llvm/IR/Module.h"
38 #include "llvm/IR/ValueHandle.h"
39 #include "llvm/Transforms/Utils/SanitizerStats.h"
40 #include <optional>
41 
42 namespace llvm {
43 class Module;
44 class Constant;
45 class ConstantInt;
46 class Function;
47 class GlobalValue;
48 class DataLayout;
49 class FunctionType;
50 class LLVMContext;
51 class IndexedInstrProfReader;
52 
53 namespace vfs {
54 class FileSystem;
55 }
56 }
57 
58 namespace clang {
59 class ASTContext;
60 class AtomicType;
61 class FunctionDecl;
62 class IdentifierInfo;
63 class ObjCImplementationDecl;
64 class ObjCEncodeExpr;
65 class BlockExpr;
66 class CharUnits;
67 class Decl;
68 class Expr;
69 class Stmt;
70 class StringLiteral;
71 class NamedDecl;
72 class ValueDecl;
73 class VarDecl;
74 class LangOptions;
75 class CodeGenOptions;
76 class HeaderSearchOptions;
77 class DiagnosticsEngine;
78 class AnnotateAttr;
79 class CXXDestructorDecl;
80 class Module;
81 class CoverageSourceInfo;
82 class InitSegAttr;
83 
84 namespace CodeGen {
85 
86 class CodeGenFunction;
87 class CodeGenTBAA;
88 class CGCXXABI;
89 class CGDebugInfo;
90 class CGObjCRuntime;
91 class CGOpenCLRuntime;
92 class CGOpenMPRuntime;
93 class CGCUDARuntime;
94 class CGHLSLRuntime;
95 class CoverageMappingModuleGen;
96 class TargetCodeGenInfo;
97 
98 enum ForDefinition_t : bool {
99   NotForDefinition = false,
100   ForDefinition = true
101 };
102 
103 struct OrderGlobalInitsOrStermFinalizers {
104   unsigned int priority;
105   unsigned int lex_order;
106   OrderGlobalInitsOrStermFinalizers(unsigned int p, unsigned int l)
107       : priority(p), lex_order(l) {}
108 
109   bool operator==(const OrderGlobalInitsOrStermFinalizers &RHS) const {
110     return priority == RHS.priority && lex_order == RHS.lex_order;
111   }
112 
113   bool operator<(const OrderGlobalInitsOrStermFinalizers &RHS) const {
114     return std::tie(priority, lex_order) <
115            std::tie(RHS.priority, RHS.lex_order);
116   }
117 };
118 
119 struct ObjCEntrypoints {
120   ObjCEntrypoints() { memset(this, 0, sizeof(*this)); }
121 
122   /// void objc_alloc(id);
123   llvm::FunctionCallee objc_alloc;
124 
125   /// void objc_allocWithZone(id);
126   llvm::FunctionCallee objc_allocWithZone;
127 
128   /// void objc_alloc_init(id);
129   llvm::FunctionCallee objc_alloc_init;
130 
131   /// void objc_autoreleasePoolPop(void*);
132   llvm::FunctionCallee objc_autoreleasePoolPop;
133 
134   /// void objc_autoreleasePoolPop(void*);
135   /// Note this method is used when we are using exception handling
136   llvm::FunctionCallee objc_autoreleasePoolPopInvoke;
137 
138   /// void *objc_autoreleasePoolPush(void);
139   llvm::Function *objc_autoreleasePoolPush;
140 
141   /// id objc_autorelease(id);
142   llvm::Function *objc_autorelease;
143 
144   /// id objc_autorelease(id);
145   /// Note this is the runtime method not the intrinsic.
146   llvm::FunctionCallee objc_autoreleaseRuntimeFunction;
147 
148   /// id objc_autoreleaseReturnValue(id);
149   llvm::Function *objc_autoreleaseReturnValue;
150 
151   /// void objc_copyWeak(id *dest, id *src);
152   llvm::Function *objc_copyWeak;
153 
154   /// void objc_destroyWeak(id*);
155   llvm::Function *objc_destroyWeak;
156 
157   /// id objc_initWeak(id*, id);
158   llvm::Function *objc_initWeak;
159 
160   /// id objc_loadWeak(id*);
161   llvm::Function *objc_loadWeak;
162 
163   /// id objc_loadWeakRetained(id*);
164   llvm::Function *objc_loadWeakRetained;
165 
166   /// void objc_moveWeak(id *dest, id *src);
167   llvm::Function *objc_moveWeak;
168 
169   /// id objc_retain(id);
170   llvm::Function *objc_retain;
171 
172   /// id objc_retain(id);
173   /// Note this is the runtime method not the intrinsic.
174   llvm::FunctionCallee objc_retainRuntimeFunction;
175 
176   /// id objc_retainAutorelease(id);
177   llvm::Function *objc_retainAutorelease;
178 
179   /// id objc_retainAutoreleaseReturnValue(id);
180   llvm::Function *objc_retainAutoreleaseReturnValue;
181 
182   /// id objc_retainAutoreleasedReturnValue(id);
183   llvm::Function *objc_retainAutoreleasedReturnValue;
184 
185   /// id objc_retainBlock(id);
186   llvm::Function *objc_retainBlock;
187 
188   /// void objc_release(id);
189   llvm::Function *objc_release;
190 
191   /// void objc_release(id);
192   /// Note this is the runtime method not the intrinsic.
193   llvm::FunctionCallee objc_releaseRuntimeFunction;
194 
195   /// void objc_storeStrong(id*, id);
196   llvm::Function *objc_storeStrong;
197 
198   /// id objc_storeWeak(id*, id);
199   llvm::Function *objc_storeWeak;
200 
201   /// id objc_unsafeClaimAutoreleasedReturnValue(id);
202   llvm::Function *objc_unsafeClaimAutoreleasedReturnValue;
203 
204   /// A void(void) inline asm to use to mark that the return value of
205   /// a call will be immediately retain.
206   llvm::InlineAsm *retainAutoreleasedReturnValueMarker;
207 
208   /// void clang.arc.use(...);
209   llvm::Function *clang_arc_use;
210 
211   /// void clang.arc.noop.use(...);
212   llvm::Function *clang_arc_noop_use;
213 };
214 
215 /// This class records statistics on instrumentation based profiling.
216 class InstrProfStats {
217   uint32_t VisitedInMainFile = 0;
218   uint32_t MissingInMainFile = 0;
219   uint32_t Visited = 0;
220   uint32_t Missing = 0;
221   uint32_t Mismatched = 0;
222 
223 public:
224   InstrProfStats() = default;
225   /// Record that we've visited a function and whether or not that function was
226   /// in the main source file.
227   void addVisited(bool MainFile) {
228     if (MainFile)
229       ++VisitedInMainFile;
230     ++Visited;
231   }
232   /// Record that a function we've visited has no profile data.
233   void addMissing(bool MainFile) {
234     if (MainFile)
235       ++MissingInMainFile;
236     ++Missing;
237   }
238   /// Record that a function we've visited has mismatched profile data.
239   void addMismatched(bool MainFile) { ++Mismatched; }
240   /// Whether or not the stats we've gathered indicate any potential problems.
241   bool hasDiagnostics() { return Missing || Mismatched; }
242   /// Report potential problems we've found to \c Diags.
243   void reportDiagnostics(DiagnosticsEngine &Diags, StringRef MainFile);
244 };
245 
246 /// A pair of helper functions for a __block variable.
247 class BlockByrefHelpers : public llvm::FoldingSetNode {
248   // MSVC requires this type to be complete in order to process this
249   // header.
250 public:
251   llvm::Constant *CopyHelper;
252   llvm::Constant *DisposeHelper;
253 
254   /// The alignment of the field.  This is important because
255   /// different offsets to the field within the byref struct need to
256   /// have different helper functions.
257   CharUnits Alignment;
258 
259   BlockByrefHelpers(CharUnits alignment)
260       : CopyHelper(nullptr), DisposeHelper(nullptr), Alignment(alignment) {}
261   BlockByrefHelpers(const BlockByrefHelpers &) = default;
262   virtual ~BlockByrefHelpers();
263 
264   void Profile(llvm::FoldingSetNodeID &id) const {
265     id.AddInteger(Alignment.getQuantity());
266     profileImpl(id);
267   }
268   virtual void profileImpl(llvm::FoldingSetNodeID &id) const = 0;
269 
270   virtual bool needsCopy() const { return true; }
271   virtual void emitCopy(CodeGenFunction &CGF, Address dest, Address src) = 0;
272 
273   virtual bool needsDispose() const { return true; }
274   virtual void emitDispose(CodeGenFunction &CGF, Address field) = 0;
275 };
276 
277 /// This class organizes the cross-function state that is used while generating
278 /// LLVM code.
279 class CodeGenModule : public CodeGenTypeCache {
280   CodeGenModule(const CodeGenModule &) = delete;
281   void operator=(const CodeGenModule &) = delete;
282 
283 public:
284   struct Structor {
285     Structor()
286         : Priority(0), LexOrder(~0u), Initializer(nullptr),
287           AssociatedData(nullptr) {}
288     Structor(int Priority, unsigned LexOrder, llvm::Constant *Initializer,
289              llvm::Constant *AssociatedData)
290         : Priority(Priority), LexOrder(LexOrder), Initializer(Initializer),
291           AssociatedData(AssociatedData) {}
292     int Priority;
293     unsigned LexOrder;
294     llvm::Constant *Initializer;
295     llvm::Constant *AssociatedData;
296   };
297 
298   typedef std::vector<Structor> CtorList;
299 
300 private:
301   ASTContext &Context;
302   const LangOptions &LangOpts;
303   IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS; // Only used for debug info.
304   const HeaderSearchOptions &HeaderSearchOpts; // Only used for debug info.
305   const PreprocessorOptions &PreprocessorOpts; // Only used for debug info.
306   const CodeGenOptions &CodeGenOpts;
307   unsigned NumAutoVarInit = 0;
308   llvm::Module &TheModule;
309   DiagnosticsEngine &Diags;
310   const TargetInfo &Target;
311   std::unique_ptr<CGCXXABI> ABI;
312   llvm::LLVMContext &VMContext;
313   std::string ModuleNameHash;
314   bool CXX20ModuleInits = false;
315   std::unique_ptr<CodeGenTBAA> TBAA;
316 
317   mutable std::unique_ptr<TargetCodeGenInfo> TheTargetCodeGenInfo;
318 
319   // This should not be moved earlier, since its initialization depends on some
320   // of the previous reference members being already initialized and also checks
321   // if TheTargetCodeGenInfo is NULL
322   CodeGenTypes Types;
323 
324   /// Holds information about C++ vtables.
325   CodeGenVTables VTables;
326 
327   std::unique_ptr<CGObjCRuntime> ObjCRuntime;
328   std::unique_ptr<CGOpenCLRuntime> OpenCLRuntime;
329   std::unique_ptr<CGOpenMPRuntime> OpenMPRuntime;
330   std::unique_ptr<CGCUDARuntime> CUDARuntime;
331   std::unique_ptr<CGHLSLRuntime> HLSLRuntime;
332   std::unique_ptr<CGDebugInfo> DebugInfo;
333   std::unique_ptr<ObjCEntrypoints> ObjCData;
334   llvm::MDNode *NoObjCARCExceptionsMetadata = nullptr;
335   std::unique_ptr<llvm::IndexedInstrProfReader> PGOReader;
336   InstrProfStats PGOStats;
337   std::unique_ptr<llvm::SanitizerStatReport> SanStats;
338 
339   // A set of references that have only been seen via a weakref so far. This is
340   // used to remove the weak of the reference if we ever see a direct reference
341   // or a definition.
342   llvm::SmallPtrSet<llvm::GlobalValue*, 10> WeakRefReferences;
343 
344   /// This contains all the decls which have definitions but/ which are deferred
345   /// for emission and therefore should only be output if they are actually
346   /// used. If a decl is in this, then it is known to have not been referenced
347   /// yet.
348   llvm::DenseMap<StringRef, GlobalDecl> DeferredDecls;
349 
350   llvm::StringSet<llvm::BumpPtrAllocator> DeferredResolversToEmit;
351 
352   /// This is a list of deferred decls which we have seen that *are* actually
353   /// referenced. These get code generated when the module is done.
354   std::vector<GlobalDecl> DeferredDeclsToEmit;
355   void addDeferredDeclToEmit(GlobalDecl GD) {
356     DeferredDeclsToEmit.emplace_back(GD);
357     addEmittedDeferredDecl(GD);
358   }
359 
360   /// Decls that were DeferredDecls and have now been emitted.
361   llvm::DenseMap<llvm::StringRef, GlobalDecl> EmittedDeferredDecls;
362 
363   void addEmittedDeferredDecl(GlobalDecl GD) {
364     // Reemission is only needed in incremental mode.
365     if (!Context.getLangOpts().IncrementalExtensions)
366       return;
367 
368     // Assume a linkage by default that does not need reemission.
369     auto L = llvm::GlobalValue::ExternalLinkage;
370     if (llvm::isa<FunctionDecl>(GD.getDecl()))
371       L = getFunctionLinkage(GD);
372     else if (auto *VD = llvm::dyn_cast<VarDecl>(GD.getDecl()))
373       L = getLLVMLinkageVarDefinition(VD);
374 
375     if (llvm::GlobalValue::isInternalLinkage(L) ||
376         llvm::GlobalValue::isLinkOnceLinkage(L) ||
377         llvm::GlobalValue::isWeakLinkage(L)) {
378       EmittedDeferredDecls[getMangledName(GD)] = GD;
379     }
380   }
381 
382   /// List of alias we have emitted. Used to make sure that what they point to
383   /// is defined once we get to the end of the of the translation unit.
384   std::vector<GlobalDecl> Aliases;
385 
386   /// List of multiversion functions to be emitted. This list is processed in
387   /// conjunction with other deferred symbols and is used to ensure that
388   /// multiversion function resolvers and ifuncs are defined and emitted.
389   std::vector<GlobalDecl> MultiVersionFuncs;
390 
391   llvm::MapVector<StringRef, llvm::TrackingVH<llvm::Constant>> Replacements;
392 
393   /// List of global values to be replaced with something else. Used when we
394   /// want to replace a GlobalValue but can't identify it by its mangled name
395   /// anymore (because the name is already taken).
396   llvm::SmallVector<std::pair<llvm::GlobalValue *, llvm::Constant *>, 8>
397     GlobalValReplacements;
398 
399   /// Variables for which we've emitted globals containing their constant
400   /// values along with the corresponding globals, for opportunistic reuse.
401   llvm::DenseMap<const VarDecl*, llvm::GlobalVariable*> InitializerConstants;
402 
403   /// Set of global decls for which we already diagnosed mangled name conflict.
404   /// Required to not issue a warning (on a mangling conflict) multiple times
405   /// for the same decl.
406   llvm::DenseSet<GlobalDecl> DiagnosedConflictingDefinitions;
407 
408   /// A queue of (optional) vtables to consider emitting.
409   std::vector<const CXXRecordDecl*> DeferredVTables;
410 
411   /// A queue of (optional) vtables that may be emitted opportunistically.
412   std::vector<const CXXRecordDecl *> OpportunisticVTables;
413 
414   /// List of global values which are required to be present in the object file;
415   /// bitcast to i8*. This is used for forcing visibility of symbols which may
416   /// otherwise be optimized out.
417   std::vector<llvm::WeakTrackingVH> LLVMUsed;
418   std::vector<llvm::WeakTrackingVH> LLVMCompilerUsed;
419 
420   /// Store the list of global constructors and their respective priorities to
421   /// be emitted when the translation unit is complete.
422   CtorList GlobalCtors;
423 
424   /// Store the list of global destructors and their respective priorities to be
425   /// emitted when the translation unit is complete.
426   CtorList GlobalDtors;
427 
428   /// An ordered map of canonical GlobalDecls to their mangled names.
429   llvm::MapVector<GlobalDecl, StringRef> MangledDeclNames;
430   llvm::StringMap<GlobalDecl, llvm::BumpPtrAllocator> Manglings;
431 
432   /// Global annotations.
433   std::vector<llvm::Constant*> Annotations;
434 
435   // Store deferred function annotations so they can be emitted at the end with
436   // most up to date ValueDecl that will have all the inherited annotations.
437   llvm::DenseMap<StringRef, const ValueDecl *> DeferredAnnotations;
438 
439   /// Map used to get unique annotation strings.
440   llvm::StringMap<llvm::Constant*> AnnotationStrings;
441 
442   /// Used for uniquing of annotation arguments.
443   llvm::DenseMap<unsigned, llvm::Constant *> AnnotationArgs;
444 
445   llvm::StringMap<llvm::GlobalVariable *> CFConstantStringMap;
446 
447   llvm::DenseMap<llvm::Constant *, llvm::GlobalVariable *> ConstantStringMap;
448   llvm::DenseMap<const UnnamedGlobalConstantDecl *, llvm::GlobalVariable *>
449       UnnamedGlobalConstantDeclMap;
450   llvm::DenseMap<const Decl*, llvm::Constant *> StaticLocalDeclMap;
451   llvm::DenseMap<const Decl*, llvm::GlobalVariable*> StaticLocalDeclGuardMap;
452   llvm::DenseMap<const Expr*, llvm::Constant *> MaterializedGlobalTemporaryMap;
453 
454   llvm::DenseMap<QualType, llvm::Constant *> AtomicSetterHelperFnMap;
455   llvm::DenseMap<QualType, llvm::Constant *> AtomicGetterHelperFnMap;
456 
457   /// Map used to get unique type descriptor constants for sanitizers.
458   llvm::DenseMap<QualType, llvm::Constant *> TypeDescriptorMap;
459 
460   /// Map used to track internal linkage functions declared within
461   /// extern "C" regions.
462   typedef llvm::MapVector<IdentifierInfo *,
463                           llvm::GlobalValue *> StaticExternCMap;
464   StaticExternCMap StaticExternCValues;
465 
466   /// thread_local variables defined or used in this TU.
467   std::vector<const VarDecl *> CXXThreadLocals;
468 
469   /// thread_local variables with initializers that need to run
470   /// before any thread_local variable in this TU is odr-used.
471   std::vector<llvm::Function *> CXXThreadLocalInits;
472   std::vector<const VarDecl *> CXXThreadLocalInitVars;
473 
474   /// Global variables with initializers that need to run before main.
475   std::vector<llvm::Function *> CXXGlobalInits;
476 
477   /// When a C++ decl with an initializer is deferred, null is
478   /// appended to CXXGlobalInits, and the index of that null is placed
479   /// here so that the initializer will be performed in the correct
480   /// order. Once the decl is emitted, the index is replaced with ~0U to ensure
481   /// that we don't re-emit the initializer.
482   llvm::DenseMap<const Decl*, unsigned> DelayedCXXInitPosition;
483 
484   typedef std::pair<OrderGlobalInitsOrStermFinalizers, llvm::Function *>
485       GlobalInitData;
486 
487   struct GlobalInitPriorityCmp {
488     bool operator()(const GlobalInitData &LHS,
489                     const GlobalInitData &RHS) const {
490       return LHS.first.priority < RHS.first.priority;
491     }
492   };
493 
494   /// Global variables with initializers whose order of initialization is set by
495   /// init_priority attribute.
496   SmallVector<GlobalInitData, 8> PrioritizedCXXGlobalInits;
497 
498   /// Global destructor functions and arguments that need to run on termination.
499   /// When UseSinitAndSterm is set, it instead contains sterm finalizer
500   /// functions, which also run on unloading a shared library.
501   typedef std::tuple<llvm::FunctionType *, llvm::WeakTrackingVH,
502                      llvm::Constant *>
503       CXXGlobalDtorsOrStermFinalizer_t;
504   SmallVector<CXXGlobalDtorsOrStermFinalizer_t, 8>
505       CXXGlobalDtorsOrStermFinalizers;
506 
507   typedef std::pair<OrderGlobalInitsOrStermFinalizers, llvm::Function *>
508       StermFinalizerData;
509 
510   struct StermFinalizerPriorityCmp {
511     bool operator()(const StermFinalizerData &LHS,
512                     const StermFinalizerData &RHS) const {
513       return LHS.first.priority < RHS.first.priority;
514     }
515   };
516 
517   /// Global variables with sterm finalizers whose order of initialization is
518   /// set by init_priority attribute.
519   SmallVector<StermFinalizerData, 8> PrioritizedCXXStermFinalizers;
520 
521   /// The complete set of modules that has been imported.
522   llvm::SetVector<clang::Module *> ImportedModules;
523 
524   /// The set of modules for which the module initializers
525   /// have been emitted.
526   llvm::SmallPtrSet<clang::Module *, 16> EmittedModuleInitializers;
527 
528   /// A vector of metadata strings for linker options.
529   SmallVector<llvm::MDNode *, 16> LinkerOptionsMetadata;
530 
531   /// A vector of metadata strings for dependent libraries for ELF.
532   SmallVector<llvm::MDNode *, 16> ELFDependentLibraries;
533 
534   /// @name Cache for Objective-C runtime types
535   /// @{
536 
537   /// Cached reference to the class for constant strings. This value has type
538   /// int * but is actually an Obj-C class pointer.
539   llvm::WeakTrackingVH CFConstantStringClassRef;
540 
541   /// The type used to describe the state of a fast enumeration in
542   /// Objective-C's for..in loop.
543   QualType ObjCFastEnumerationStateType;
544 
545   /// @}
546 
547   /// Lazily create the Objective-C runtime
548   void createObjCRuntime();
549 
550   void createOpenCLRuntime();
551   void createOpenMPRuntime();
552   void createCUDARuntime();
553   void createHLSLRuntime();
554 
555   bool isTriviallyRecursive(const FunctionDecl *F);
556   bool shouldEmitFunction(GlobalDecl GD);
557   bool shouldOpportunisticallyEmitVTables();
558   /// Map used to be sure we don't emit the same CompoundLiteral twice.
559   llvm::DenseMap<const CompoundLiteralExpr *, llvm::GlobalVariable *>
560       EmittedCompoundLiterals;
561 
562   /// Map of the global blocks we've emitted, so that we don't have to re-emit
563   /// them if the constexpr evaluator gets aggressive.
564   llvm::DenseMap<const BlockExpr *, llvm::Constant *> EmittedGlobalBlocks;
565 
566   /// @name Cache for Blocks Runtime Globals
567   /// @{
568 
569   llvm::Constant *NSConcreteGlobalBlock = nullptr;
570   llvm::Constant *NSConcreteStackBlock = nullptr;
571 
572   llvm::FunctionCallee BlockObjectAssign = nullptr;
573   llvm::FunctionCallee BlockObjectDispose = nullptr;
574 
575   llvm::Type *BlockDescriptorType = nullptr;
576   llvm::Type *GenericBlockLiteralType = nullptr;
577 
578   struct {
579     int GlobalUniqueCount;
580   } Block;
581 
582   GlobalDecl initializedGlobalDecl;
583 
584   /// @}
585 
586   /// void @llvm.lifetime.start(i64 %size, i8* nocapture <ptr>)
587   llvm::Function *LifetimeStartFn = nullptr;
588 
589   /// void @llvm.lifetime.end(i64 %size, i8* nocapture <ptr>)
590   llvm::Function *LifetimeEndFn = nullptr;
591 
592   std::unique_ptr<SanitizerMetadata> SanitizerMD;
593 
594   llvm::MapVector<const Decl *, bool> DeferredEmptyCoverageMappingDecls;
595 
596   std::unique_ptr<CoverageMappingModuleGen> CoverageMapping;
597 
598   /// Mapping from canonical types to their metadata identifiers. We need to
599   /// maintain this mapping because identifiers may be formed from distinct
600   /// MDNodes.
601   typedef llvm::DenseMap<QualType, llvm::Metadata *> MetadataTypeMap;
602   MetadataTypeMap MetadataIdMap;
603   MetadataTypeMap VirtualMetadataIdMap;
604   MetadataTypeMap GeneralizedMetadataIdMap;
605 
606   // Helps squashing blocks of TopLevelStmtDecl into a single llvm::Function
607   // when used with -fincremental-extensions.
608   std::pair<std::unique_ptr<CodeGenFunction>, const TopLevelStmtDecl *>
609       GlobalTopLevelStmtBlockInFlight;
610 
611 public:
612   CodeGenModule(ASTContext &C, IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
613                 const HeaderSearchOptions &headersearchopts,
614                 const PreprocessorOptions &ppopts,
615                 const CodeGenOptions &CodeGenOpts, llvm::Module &M,
616                 DiagnosticsEngine &Diags,
617                 CoverageSourceInfo *CoverageInfo = nullptr);
618 
619   ~CodeGenModule();
620 
621   void clear();
622 
623   /// Finalize LLVM code generation.
624   void Release();
625 
626   /// Return true if we should emit location information for expressions.
627   bool getExpressionLocationsEnabled() const;
628 
629   /// Return a reference to the configured Objective-C runtime.
630   CGObjCRuntime &getObjCRuntime() {
631     if (!ObjCRuntime) createObjCRuntime();
632     return *ObjCRuntime;
633   }
634 
635   /// Return true iff an Objective-C runtime has been configured.
636   bool hasObjCRuntime() { return !!ObjCRuntime; }
637 
638   const std::string &getModuleNameHash() const { return ModuleNameHash; }
639 
640   /// Return a reference to the configured OpenCL runtime.
641   CGOpenCLRuntime &getOpenCLRuntime() {
642     assert(OpenCLRuntime != nullptr);
643     return *OpenCLRuntime;
644   }
645 
646   /// Return a reference to the configured OpenMP runtime.
647   CGOpenMPRuntime &getOpenMPRuntime() {
648     assert(OpenMPRuntime != nullptr);
649     return *OpenMPRuntime;
650   }
651 
652   /// Return a reference to the configured CUDA runtime.
653   CGCUDARuntime &getCUDARuntime() {
654     assert(CUDARuntime != nullptr);
655     return *CUDARuntime;
656   }
657 
658   /// Return a reference to the configured HLSL runtime.
659   CGHLSLRuntime &getHLSLRuntime() {
660     assert(HLSLRuntime != nullptr);
661     return *HLSLRuntime;
662   }
663 
664   ObjCEntrypoints &getObjCEntrypoints() const {
665     assert(ObjCData != nullptr);
666     return *ObjCData;
667   }
668 
669   // Version checking functions, used to implement ObjC's @available:
670   // i32 @__isOSVersionAtLeast(i32, i32, i32)
671   llvm::FunctionCallee IsOSVersionAtLeastFn = nullptr;
672   // i32 @__isPlatformVersionAtLeast(i32, i32, i32, i32)
673   llvm::FunctionCallee IsPlatformVersionAtLeastFn = nullptr;
674 
675   InstrProfStats &getPGOStats() { return PGOStats; }
676   llvm::IndexedInstrProfReader *getPGOReader() const { return PGOReader.get(); }
677 
678   CoverageMappingModuleGen *getCoverageMapping() const {
679     return CoverageMapping.get();
680   }
681 
682   llvm::Constant *getStaticLocalDeclAddress(const VarDecl *D) {
683     return StaticLocalDeclMap[D];
684   }
685   void setStaticLocalDeclAddress(const VarDecl *D,
686                                  llvm::Constant *C) {
687     StaticLocalDeclMap[D] = C;
688   }
689 
690   llvm::Constant *
691   getOrCreateStaticVarDecl(const VarDecl &D,
692                            llvm::GlobalValue::LinkageTypes Linkage);
693 
694   llvm::GlobalVariable *getStaticLocalDeclGuardAddress(const VarDecl *D) {
695     return StaticLocalDeclGuardMap[D];
696   }
697   void setStaticLocalDeclGuardAddress(const VarDecl *D,
698                                       llvm::GlobalVariable *C) {
699     StaticLocalDeclGuardMap[D] = C;
700   }
701 
702   Address createUnnamedGlobalFrom(const VarDecl &D, llvm::Constant *Constant,
703                                   CharUnits Align);
704 
705   bool lookupRepresentativeDecl(StringRef MangledName,
706                                 GlobalDecl &Result) const;
707 
708   llvm::Constant *getAtomicSetterHelperFnMap(QualType Ty) {
709     return AtomicSetterHelperFnMap[Ty];
710   }
711   void setAtomicSetterHelperFnMap(QualType Ty,
712                             llvm::Constant *Fn) {
713     AtomicSetterHelperFnMap[Ty] = Fn;
714   }
715 
716   llvm::Constant *getAtomicGetterHelperFnMap(QualType Ty) {
717     return AtomicGetterHelperFnMap[Ty];
718   }
719   void setAtomicGetterHelperFnMap(QualType Ty,
720                             llvm::Constant *Fn) {
721     AtomicGetterHelperFnMap[Ty] = Fn;
722   }
723 
724   llvm::Constant *getTypeDescriptorFromMap(QualType Ty) {
725     return TypeDescriptorMap[Ty];
726   }
727   void setTypeDescriptorInMap(QualType Ty, llvm::Constant *C) {
728     TypeDescriptorMap[Ty] = C;
729   }
730 
731   CGDebugInfo *getModuleDebugInfo() { return DebugInfo.get(); }
732 
733   llvm::MDNode *getNoObjCARCExceptionsMetadata() {
734     if (!NoObjCARCExceptionsMetadata)
735       NoObjCARCExceptionsMetadata =
736           llvm::MDNode::get(getLLVMContext(), std::nullopt);
737     return NoObjCARCExceptionsMetadata;
738   }
739 
740   ASTContext &getContext() const { return Context; }
741   const LangOptions &getLangOpts() const { return LangOpts; }
742   const IntrusiveRefCntPtr<llvm::vfs::FileSystem> &getFileSystem() const {
743     return FS;
744   }
745   const HeaderSearchOptions &getHeaderSearchOpts()
746     const { return HeaderSearchOpts; }
747   const PreprocessorOptions &getPreprocessorOpts()
748     const { return PreprocessorOpts; }
749   const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
750   llvm::Module &getModule() const { return TheModule; }
751   DiagnosticsEngine &getDiags() const { return Diags; }
752   const llvm::DataLayout &getDataLayout() const {
753     return TheModule.getDataLayout();
754   }
755   const TargetInfo &getTarget() const { return Target; }
756   const llvm::Triple &getTriple() const { return Target.getTriple(); }
757   bool supportsCOMDAT() const;
758   void maybeSetTrivialComdat(const Decl &D, llvm::GlobalObject &GO);
759 
760   CGCXXABI &getCXXABI() const { return *ABI; }
761   llvm::LLVMContext &getLLVMContext() { return VMContext; }
762 
763   bool shouldUseTBAA() const { return TBAA != nullptr; }
764 
765   const TargetCodeGenInfo &getTargetCodeGenInfo();
766 
767   CodeGenTypes &getTypes() { return Types; }
768 
769   CodeGenVTables &getVTables() { return VTables; }
770 
771   ItaniumVTableContext &getItaniumVTableContext() {
772     return VTables.getItaniumVTableContext();
773   }
774 
775   const ItaniumVTableContext &getItaniumVTableContext() const {
776     return VTables.getItaniumVTableContext();
777   }
778 
779   MicrosoftVTableContext &getMicrosoftVTableContext() {
780     return VTables.getMicrosoftVTableContext();
781   }
782 
783   CtorList &getGlobalCtors() { return GlobalCtors; }
784   CtorList &getGlobalDtors() { return GlobalDtors; }
785 
786   /// getTBAATypeInfo - Get metadata used to describe accesses to objects of
787   /// the given type.
788   llvm::MDNode *getTBAATypeInfo(QualType QTy);
789 
790   /// getTBAAAccessInfo - Get TBAA information that describes an access to
791   /// an object of the given type.
792   TBAAAccessInfo getTBAAAccessInfo(QualType AccessType);
793 
794   /// getTBAAVTablePtrAccessInfo - Get the TBAA information that describes an
795   /// access to a virtual table pointer.
796   TBAAAccessInfo getTBAAVTablePtrAccessInfo(llvm::Type *VTablePtrType);
797 
798   llvm::MDNode *getTBAAStructInfo(QualType QTy);
799 
800   /// getTBAABaseTypeInfo - Get metadata that describes the given base access
801   /// type. Return null if the type is not suitable for use in TBAA access tags.
802   llvm::MDNode *getTBAABaseTypeInfo(QualType QTy);
803 
804   /// getTBAAAccessTagInfo - Get TBAA tag for a given memory access.
805   llvm::MDNode *getTBAAAccessTagInfo(TBAAAccessInfo Info);
806 
807   /// mergeTBAAInfoForCast - Get merged TBAA information for the purposes of
808   /// type casts.
809   TBAAAccessInfo mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo,
810                                       TBAAAccessInfo TargetInfo);
811 
812   /// mergeTBAAInfoForConditionalOperator - Get merged TBAA information for the
813   /// purposes of conditional operator.
814   TBAAAccessInfo mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA,
815                                                      TBAAAccessInfo InfoB);
816 
817   /// mergeTBAAInfoForMemoryTransfer - Get merged TBAA information for the
818   /// purposes of memory transfer calls.
819   TBAAAccessInfo mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo,
820                                                 TBAAAccessInfo SrcInfo);
821 
822   /// getTBAAInfoForSubobject - Get TBAA information for an access with a given
823   /// base lvalue.
824   TBAAAccessInfo getTBAAInfoForSubobject(LValue Base, QualType AccessType) {
825     if (Base.getTBAAInfo().isMayAlias())
826       return TBAAAccessInfo::getMayAliasInfo();
827     return getTBAAAccessInfo(AccessType);
828   }
829 
830   bool isPaddedAtomicType(QualType type);
831   bool isPaddedAtomicType(const AtomicType *type);
832 
833   /// DecorateInstructionWithTBAA - Decorate the instruction with a TBAA tag.
834   void DecorateInstructionWithTBAA(llvm::Instruction *Inst,
835                                    TBAAAccessInfo TBAAInfo);
836 
837   /// Adds !invariant.barrier !tag to instruction
838   void DecorateInstructionWithInvariantGroup(llvm::Instruction *I,
839                                              const CXXRecordDecl *RD);
840 
841   /// Emit the given number of characters as a value of type size_t.
842   llvm::ConstantInt *getSize(CharUnits numChars);
843 
844   /// Set the visibility for the given LLVM GlobalValue.
845   void setGlobalVisibility(llvm::GlobalValue *GV, const NamedDecl *D) const;
846 
847   void setDSOLocal(llvm::GlobalValue *GV) const;
848 
849   bool shouldMapVisibilityToDLLExport(const NamedDecl *D) const {
850     return getLangOpts().hasDefaultVisibilityExportMapping() && D &&
851            (D->getLinkageAndVisibility().getVisibility() ==
852             DefaultVisibility) &&
853            (getLangOpts().isAllDefaultVisibilityExportMapping() ||
854             (getLangOpts().isExplicitDefaultVisibilityExportMapping() &&
855              D->getLinkageAndVisibility().isVisibilityExplicit()));
856   }
857   void setDLLImportDLLExport(llvm::GlobalValue *GV, GlobalDecl D) const;
858   void setDLLImportDLLExport(llvm::GlobalValue *GV, const NamedDecl *D) const;
859   /// Set visibility, dllimport/dllexport and dso_local.
860   /// This must be called after dllimport/dllexport is set.
861   void setGVProperties(llvm::GlobalValue *GV, GlobalDecl GD) const;
862   void setGVProperties(llvm::GlobalValue *GV, const NamedDecl *D) const;
863 
864   void setGVPropertiesAux(llvm::GlobalValue *GV, const NamedDecl *D) const;
865 
866   /// Set the TLS mode for the given LLVM GlobalValue for the thread-local
867   /// variable declaration D.
868   void setTLSMode(llvm::GlobalValue *GV, const VarDecl &D) const;
869 
870   /// Get LLVM TLS mode from CodeGenOptions.
871   llvm::GlobalVariable::ThreadLocalMode GetDefaultLLVMTLSModel() const;
872 
873   static llvm::GlobalValue::VisibilityTypes GetLLVMVisibility(Visibility V) {
874     switch (V) {
875     case DefaultVisibility:   return llvm::GlobalValue::DefaultVisibility;
876     case HiddenVisibility:    return llvm::GlobalValue::HiddenVisibility;
877     case ProtectedVisibility: return llvm::GlobalValue::ProtectedVisibility;
878     }
879     llvm_unreachable("unknown visibility!");
880   }
881 
882   llvm::Constant *GetAddrOfGlobal(GlobalDecl GD,
883                                   ForDefinition_t IsForDefinition
884                                     = NotForDefinition);
885 
886   /// Will return a global variable of the given type. If a variable with a
887   /// different type already exists then a new  variable with the right type
888   /// will be created and all uses of the old variable will be replaced with a
889   /// bitcast to the new variable.
890   llvm::GlobalVariable *
891   CreateOrReplaceCXXRuntimeVariable(StringRef Name, llvm::Type *Ty,
892                                     llvm::GlobalValue::LinkageTypes Linkage,
893                                     llvm::Align Alignment);
894 
895   llvm::Function *CreateGlobalInitOrCleanUpFunction(
896       llvm::FunctionType *ty, const Twine &name, const CGFunctionInfo &FI,
897       SourceLocation Loc = SourceLocation(), bool TLS = false,
898       llvm::GlobalVariable::LinkageTypes Linkage =
899           llvm::GlobalVariable::InternalLinkage);
900 
901   /// Return the AST address space of the underlying global variable for D, as
902   /// determined by its declaration. Normally this is the same as the address
903   /// space of D's type, but in CUDA, address spaces are associated with
904   /// declarations, not types. If D is nullptr, return the default address
905   /// space for global variable.
906   ///
907   /// For languages without explicit address spaces, if D has default address
908   /// space, target-specific global or constant address space may be returned.
909   LangAS GetGlobalVarAddressSpace(const VarDecl *D);
910 
911   /// Return the AST address space of constant literal, which is used to emit
912   /// the constant literal as global variable in LLVM IR.
913   /// Note: This is not necessarily the address space of the constant literal
914   /// in AST. For address space agnostic language, e.g. C++, constant literal
915   /// in AST is always in default address space.
916   LangAS GetGlobalConstantAddressSpace() const;
917 
918   /// Return the llvm::Constant for the address of the given global variable.
919   /// If Ty is non-null and if the global doesn't exist, then it will be created
920   /// with the specified type instead of whatever the normal requested type
921   /// would be. If IsForDefinition is true, it is guaranteed that an actual
922   /// global with type Ty will be returned, not conversion of a variable with
923   /// the same mangled name but some other type.
924   llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D,
925                                      llvm::Type *Ty = nullptr,
926                                      ForDefinition_t IsForDefinition
927                                        = NotForDefinition);
928 
929   /// Return the address of the given function. If Ty is non-null, then this
930   /// function will use the specified type if it has to create it.
931   llvm::Constant *GetAddrOfFunction(GlobalDecl GD, llvm::Type *Ty = nullptr,
932                                     bool ForVTable = false,
933                                     bool DontDefer = false,
934                                     ForDefinition_t IsForDefinition
935                                       = NotForDefinition);
936 
937   // Return the function body address of the given function.
938   llvm::Constant *GetFunctionStart(const ValueDecl *Decl);
939 
940   llvm::Constant *
941   getConstantSignedPointer(llvm::Constant *Pointer, unsigned Key,
942                            llvm::Constant *StorageAddress,
943                            llvm::ConstantInt *OtherDiscriminator);
944 
945   // Return whether RTTI information should be emitted for this target.
946   bool shouldEmitRTTI(bool ForEH = false) {
947     return (ForEH || getLangOpts().RTTI) && !getLangOpts().CUDAIsDevice &&
948            !(getLangOpts().OpenMP && getLangOpts().OpenMPIsTargetDevice &&
949              getTriple().isNVPTX());
950   }
951 
952   /// Get the address of the RTTI descriptor for the given type.
953   llvm::Constant *GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH = false);
954 
955   /// Get the address of a GUID.
956   ConstantAddress GetAddrOfMSGuidDecl(const MSGuidDecl *GD);
957 
958   /// Get the address of a UnnamedGlobalConstant
959   ConstantAddress
960   GetAddrOfUnnamedGlobalConstantDecl(const UnnamedGlobalConstantDecl *GCD);
961 
962   /// Get the address of a template parameter object.
963   ConstantAddress
964   GetAddrOfTemplateParamObject(const TemplateParamObjectDecl *TPO);
965 
966   /// Get the address of the thunk for the given global decl.
967   llvm::Constant *GetAddrOfThunk(StringRef Name, llvm::Type *FnTy,
968                                  GlobalDecl GD);
969 
970   /// Get a reference to the target of VD.
971   ConstantAddress GetWeakRefReference(const ValueDecl *VD);
972 
973   /// Returns the assumed alignment of an opaque pointer to the given class.
974   CharUnits getClassPointerAlignment(const CXXRecordDecl *CD);
975 
976   /// Returns the minimum object size for an object of the given class type
977   /// (or a class derived from it).
978   CharUnits getMinimumClassObjectSize(const CXXRecordDecl *CD);
979 
980   /// Returns the minimum object size for an object of the given type.
981   CharUnits getMinimumObjectSize(QualType Ty) {
982     if (CXXRecordDecl *RD = Ty->getAsCXXRecordDecl())
983       return getMinimumClassObjectSize(RD);
984     return getContext().getTypeSizeInChars(Ty);
985   }
986 
987   /// Returns the assumed alignment of a virtual base of a class.
988   CharUnits getVBaseAlignment(CharUnits DerivedAlign,
989                               const CXXRecordDecl *Derived,
990                               const CXXRecordDecl *VBase);
991 
992   /// Given a class pointer with an actual known alignment, and the
993   /// expected alignment of an object at a dynamic offset w.r.t that
994   /// pointer, return the alignment to assume at the offset.
995   CharUnits getDynamicOffsetAlignment(CharUnits ActualAlign,
996                                       const CXXRecordDecl *Class,
997                                       CharUnits ExpectedTargetAlign);
998 
999   CharUnits
1000   computeNonVirtualBaseClassOffset(const CXXRecordDecl *DerivedClass,
1001                                    CastExpr::path_const_iterator Start,
1002                                    CastExpr::path_const_iterator End);
1003 
1004   /// Returns the offset from a derived class to  a class. Returns null if the
1005   /// offset is 0.
1006   llvm::Constant *
1007   GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl,
1008                                CastExpr::path_const_iterator PathBegin,
1009                                CastExpr::path_const_iterator PathEnd);
1010 
1011   llvm::FoldingSet<BlockByrefHelpers> ByrefHelpersCache;
1012 
1013   /// Fetches the global unique block count.
1014   int getUniqueBlockCount() { return ++Block.GlobalUniqueCount; }
1015 
1016   /// Fetches the type of a generic block descriptor.
1017   llvm::Type *getBlockDescriptorType();
1018 
1019   /// The type of a generic block literal.
1020   llvm::Type *getGenericBlockLiteralType();
1021 
1022   /// Gets the address of a block which requires no captures.
1023   llvm::Constant *GetAddrOfGlobalBlock(const BlockExpr *BE, StringRef Name);
1024 
1025   /// Returns the address of a block which requires no caputres, or null if
1026   /// we've yet to emit the block for BE.
1027   llvm::Constant *getAddrOfGlobalBlockIfEmitted(const BlockExpr *BE) {
1028     return EmittedGlobalBlocks.lookup(BE);
1029   }
1030 
1031   /// Notes that BE's global block is available via Addr. Asserts that BE
1032   /// isn't already emitted.
1033   void setAddrOfGlobalBlock(const BlockExpr *BE, llvm::Constant *Addr);
1034 
1035   /// Return a pointer to a constant CFString object for the given string.
1036   ConstantAddress GetAddrOfConstantCFString(const StringLiteral *Literal);
1037 
1038   /// Return a constant array for the given string.
1039   llvm::Constant *GetConstantArrayFromStringLiteral(const StringLiteral *E);
1040 
1041   /// Return a pointer to a constant array for the given string literal.
1042   ConstantAddress
1043   GetAddrOfConstantStringFromLiteral(const StringLiteral *S,
1044                                      StringRef Name = ".str");
1045 
1046   /// Return a pointer to a constant array for the given ObjCEncodeExpr node.
1047   ConstantAddress
1048   GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *);
1049 
1050   /// Returns a pointer to a character array containing the literal and a
1051   /// terminating '\0' character. The result has pointer to array type.
1052   ///
1053   /// \param GlobalName If provided, the name to use for the global (if one is
1054   /// created).
1055   ConstantAddress
1056   GetAddrOfConstantCString(const std::string &Str,
1057                            const char *GlobalName = nullptr);
1058 
1059   /// Returns a pointer to a constant global variable for the given file-scope
1060   /// compound literal expression.
1061   ConstantAddress GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr*E);
1062 
1063   /// If it's been emitted already, returns the GlobalVariable corresponding to
1064   /// a compound literal. Otherwise, returns null.
1065   llvm::GlobalVariable *
1066   getAddrOfConstantCompoundLiteralIfEmitted(const CompoundLiteralExpr *E);
1067 
1068   /// Notes that CLE's GlobalVariable is GV. Asserts that CLE isn't already
1069   /// emitted.
1070   void setAddrOfConstantCompoundLiteral(const CompoundLiteralExpr *CLE,
1071                                         llvm::GlobalVariable *GV);
1072 
1073   /// Returns a pointer to a global variable representing a temporary
1074   /// with static or thread storage duration.
1075   ConstantAddress GetAddrOfGlobalTemporary(const MaterializeTemporaryExpr *E,
1076                                            const Expr *Inner);
1077 
1078   /// Retrieve the record type that describes the state of an
1079   /// Objective-C fast enumeration loop (for..in).
1080   QualType getObjCFastEnumerationStateType();
1081 
1082   // Produce code for this constructor/destructor. This method doesn't try
1083   // to apply any ABI rules about which other constructors/destructors
1084   // are needed or if they are alias to each other.
1085   llvm::Function *codegenCXXStructor(GlobalDecl GD);
1086 
1087   /// Return the address of the constructor/destructor of the given type.
1088   llvm::Constant *
1089   getAddrOfCXXStructor(GlobalDecl GD, const CGFunctionInfo *FnInfo = nullptr,
1090                        llvm::FunctionType *FnType = nullptr,
1091                        bool DontDefer = false,
1092                        ForDefinition_t IsForDefinition = NotForDefinition) {
1093     return cast<llvm::Constant>(getAddrAndTypeOfCXXStructor(GD, FnInfo, FnType,
1094                                                             DontDefer,
1095                                                             IsForDefinition)
1096                                     .getCallee());
1097   }
1098 
1099   llvm::FunctionCallee getAddrAndTypeOfCXXStructor(
1100       GlobalDecl GD, const CGFunctionInfo *FnInfo = nullptr,
1101       llvm::FunctionType *FnType = nullptr, bool DontDefer = false,
1102       ForDefinition_t IsForDefinition = NotForDefinition);
1103 
1104   /// Given a builtin id for a function like "__builtin_fabsf", return a
1105   /// Function* for "fabsf".
1106   llvm::Constant *getBuiltinLibFunction(const FunctionDecl *FD,
1107                                         unsigned BuiltinID);
1108 
1109   llvm::Function *getIntrinsic(unsigned IID,
1110                                ArrayRef<llvm::Type *> Tys = std::nullopt);
1111 
1112   /// Emit code for a single top level declaration.
1113   void EmitTopLevelDecl(Decl *D);
1114 
1115   /// Stored a deferred empty coverage mapping for an unused
1116   /// and thus uninstrumented top level declaration.
1117   void AddDeferredUnusedCoverageMapping(Decl *D);
1118 
1119   /// Remove the deferred empty coverage mapping as this
1120   /// declaration is actually instrumented.
1121   void ClearUnusedCoverageMapping(const Decl *D);
1122 
1123   /// Emit all the deferred coverage mappings
1124   /// for the uninstrumented functions.
1125   void EmitDeferredUnusedCoverageMappings();
1126 
1127   /// Emit an alias for "main" if it has no arguments (needed for wasm).
1128   void EmitMainVoidAlias();
1129 
1130   /// Tell the consumer that this variable has been instantiated.
1131   void HandleCXXStaticMemberVarInstantiation(VarDecl *VD);
1132 
1133   /// If the declaration has internal linkage but is inside an
1134   /// extern "C" linkage specification, prepare to emit an alias for it
1135   /// to the expected name.
1136   template<typename SomeDecl>
1137   void MaybeHandleStaticInExternC(const SomeDecl *D, llvm::GlobalValue *GV);
1138 
1139   /// Add a global to a list to be added to the llvm.used metadata.
1140   void addUsedGlobal(llvm::GlobalValue *GV);
1141 
1142   /// Add a global to a list to be added to the llvm.compiler.used metadata.
1143   void addCompilerUsedGlobal(llvm::GlobalValue *GV);
1144 
1145   /// Add a global to a list to be added to the llvm.compiler.used metadata.
1146   void addUsedOrCompilerUsedGlobal(llvm::GlobalValue *GV);
1147 
1148   /// Add a destructor and object to add to the C++ global destructor function.
1149   void AddCXXDtorEntry(llvm::FunctionCallee DtorFn, llvm::Constant *Object) {
1150     CXXGlobalDtorsOrStermFinalizers.emplace_back(DtorFn.getFunctionType(),
1151                                                  DtorFn.getCallee(), Object);
1152   }
1153 
1154   /// Add an sterm finalizer to the C++ global cleanup function.
1155   void AddCXXStermFinalizerEntry(llvm::FunctionCallee DtorFn) {
1156     CXXGlobalDtorsOrStermFinalizers.emplace_back(DtorFn.getFunctionType(),
1157                                                  DtorFn.getCallee(), nullptr);
1158   }
1159 
1160   /// Add an sterm finalizer to its own llvm.global_dtors entry.
1161   void AddCXXStermFinalizerToGlobalDtor(llvm::Function *StermFinalizer,
1162                                         int Priority) {
1163     AddGlobalDtor(StermFinalizer, Priority);
1164   }
1165 
1166   void AddCXXPrioritizedStermFinalizerEntry(llvm::Function *StermFinalizer,
1167                                             int Priority) {
1168     OrderGlobalInitsOrStermFinalizers Key(Priority,
1169                                           PrioritizedCXXStermFinalizers.size());
1170     PrioritizedCXXStermFinalizers.push_back(
1171         std::make_pair(Key, StermFinalizer));
1172   }
1173 
1174   /// Create or return a runtime function declaration with the specified type
1175   /// and name. If \p AssumeConvergent is true, the call will have the
1176   /// convergent attribute added.
1177   llvm::FunctionCallee
1178   CreateRuntimeFunction(llvm::FunctionType *Ty, StringRef Name,
1179                         llvm::AttributeList ExtraAttrs = llvm::AttributeList(),
1180                         bool Local = false, bool AssumeConvergent = false);
1181 
1182   /// Create a new runtime global variable with the specified type and name.
1183   llvm::Constant *CreateRuntimeVariable(llvm::Type *Ty,
1184                                         StringRef Name);
1185 
1186   ///@name Custom Blocks Runtime Interfaces
1187   ///@{
1188 
1189   llvm::Constant *getNSConcreteGlobalBlock();
1190   llvm::Constant *getNSConcreteStackBlock();
1191   llvm::FunctionCallee getBlockObjectAssign();
1192   llvm::FunctionCallee getBlockObjectDispose();
1193 
1194   ///@}
1195 
1196   llvm::Function *getLLVMLifetimeStartFn();
1197   llvm::Function *getLLVMLifetimeEndFn();
1198 
1199   // Make sure that this type is translated.
1200   void UpdateCompletedType(const TagDecl *TD);
1201 
1202   llvm::Constant *getMemberPointerConstant(const UnaryOperator *e);
1203 
1204   /// Emit type info if type of an expression is a variably modified
1205   /// type. Also emit proper debug info for cast types.
1206   void EmitExplicitCastExprType(const ExplicitCastExpr *E,
1207                                 CodeGenFunction *CGF = nullptr);
1208 
1209   /// Return the result of value-initializing the given type, i.e. a null
1210   /// expression of the given type.  This is usually, but not always, an LLVM
1211   /// null constant.
1212   llvm::Constant *EmitNullConstant(QualType T);
1213 
1214   /// Return a null constant appropriate for zero-initializing a base class with
1215   /// the given type. This is usually, but not always, an LLVM null constant.
1216   llvm::Constant *EmitNullConstantForBase(const CXXRecordDecl *Record);
1217 
1218   /// Emit a general error that something can't be done.
1219   void Error(SourceLocation loc, StringRef error);
1220 
1221   /// Print out an error that codegen doesn't support the specified stmt yet.
1222   void ErrorUnsupported(const Stmt *S, const char *Type);
1223 
1224   /// Print out an error that codegen doesn't support the specified decl yet.
1225   void ErrorUnsupported(const Decl *D, const char *Type);
1226 
1227   /// Set the attributes on the LLVM function for the given decl and function
1228   /// info. This applies attributes necessary for handling the ABI as well as
1229   /// user specified attributes like section.
1230   void SetInternalFunctionAttributes(GlobalDecl GD, llvm::Function *F,
1231                                      const CGFunctionInfo &FI);
1232 
1233   /// Set the LLVM function attributes (sext, zext, etc).
1234   void SetLLVMFunctionAttributes(GlobalDecl GD, const CGFunctionInfo &Info,
1235                                  llvm::Function *F, bool IsThunk);
1236 
1237   /// Set the LLVM function attributes which only apply to a function
1238   /// definition.
1239   void SetLLVMFunctionAttributesForDefinition(const Decl *D, llvm::Function *F);
1240 
1241   /// Set the LLVM function attributes that represent floating point
1242   /// environment.
1243   void setLLVMFunctionFEnvAttributes(const FunctionDecl *D, llvm::Function *F);
1244 
1245   /// Return true iff the given type uses 'sret' when used as a return type.
1246   bool ReturnTypeUsesSRet(const CGFunctionInfo &FI);
1247 
1248   /// Return true iff the given type has `inreg` set.
1249   bool ReturnTypeHasInReg(const CGFunctionInfo &FI);
1250 
1251   /// Return true iff the given type uses an argument slot when 'sret' is used
1252   /// as a return type.
1253   bool ReturnSlotInterferesWithArgs(const CGFunctionInfo &FI);
1254 
1255   /// Return true iff the given type uses 'fpret' when used as a return type.
1256   bool ReturnTypeUsesFPRet(QualType ResultType);
1257 
1258   /// Return true iff the given type uses 'fp2ret' when used as a return type.
1259   bool ReturnTypeUsesFP2Ret(QualType ResultType);
1260 
1261   /// Get the LLVM attributes and calling convention to use for a particular
1262   /// function type.
1263   ///
1264   /// \param Name - The function name.
1265   /// \param Info - The function type information.
1266   /// \param CalleeInfo - The callee information these attributes are being
1267   /// constructed for. If valid, the attributes applied to this decl may
1268   /// contribute to the function attributes and calling convention.
1269   /// \param Attrs [out] - On return, the attribute list to use.
1270   /// \param CallingConv [out] - On return, the LLVM calling convention to use.
1271   void ConstructAttributeList(StringRef Name, const CGFunctionInfo &Info,
1272                               CGCalleeInfo CalleeInfo,
1273                               llvm::AttributeList &Attrs, unsigned &CallingConv,
1274                               bool AttrOnCallSite, bool IsThunk);
1275 
1276   /// Adjust Memory attribute to ensure that the BE gets the right attribute
1277   // in order to generate the library call or the intrinsic for the function
1278   // name 'Name'.
1279   void AdjustMemoryAttribute(StringRef Name, CGCalleeInfo CalleeInfo,
1280                              llvm::AttributeList &Attrs);
1281 
1282   /// Like the overload taking a `Function &`, but intended specifically
1283   /// for frontends that want to build on Clang's target-configuration logic.
1284   void addDefaultFunctionDefinitionAttributes(llvm::AttrBuilder &attrs);
1285 
1286   StringRef getMangledName(GlobalDecl GD);
1287   StringRef getBlockMangledName(GlobalDecl GD, const BlockDecl *BD);
1288   const GlobalDecl getMangledNameDecl(StringRef);
1289 
1290   void EmitTentativeDefinition(const VarDecl *D);
1291 
1292   void EmitExternalDeclaration(const VarDecl *D);
1293 
1294   void EmitVTable(CXXRecordDecl *Class);
1295 
1296   void RefreshTypeCacheForClass(const CXXRecordDecl *Class);
1297 
1298   /// Appends Opts to the "llvm.linker.options" metadata value.
1299   void AppendLinkerOptions(StringRef Opts);
1300 
1301   /// Appends a detect mismatch command to the linker options.
1302   void AddDetectMismatch(StringRef Name, StringRef Value);
1303 
1304   /// Appends a dependent lib to the appropriate metadata value.
1305   void AddDependentLib(StringRef Lib);
1306 
1307 
1308   llvm::GlobalVariable::LinkageTypes getFunctionLinkage(GlobalDecl GD);
1309 
1310   void setFunctionLinkage(GlobalDecl GD, llvm::Function *F) {
1311     F->setLinkage(getFunctionLinkage(GD));
1312   }
1313 
1314   /// Return the appropriate linkage for the vtable, VTT, and type information
1315   /// of the given class.
1316   llvm::GlobalVariable::LinkageTypes getVTableLinkage(const CXXRecordDecl *RD);
1317 
1318   /// Return the store size, in character units, of the given LLVM type.
1319   CharUnits GetTargetTypeStoreSize(llvm::Type *Ty) const;
1320 
1321   /// Returns LLVM linkage for a declarator.
1322   llvm::GlobalValue::LinkageTypes
1323   getLLVMLinkageForDeclarator(const DeclaratorDecl *D, GVALinkage Linkage);
1324 
1325   /// Returns LLVM linkage for a declarator.
1326   llvm::GlobalValue::LinkageTypes
1327   getLLVMLinkageVarDefinition(const VarDecl *VD);
1328 
1329   /// Emit all the global annotations.
1330   void EmitGlobalAnnotations();
1331 
1332   /// Emit an annotation string.
1333   llvm::Constant *EmitAnnotationString(StringRef Str);
1334 
1335   /// Emit the annotation's translation unit.
1336   llvm::Constant *EmitAnnotationUnit(SourceLocation Loc);
1337 
1338   /// Emit the annotation line number.
1339   llvm::Constant *EmitAnnotationLineNo(SourceLocation L);
1340 
1341   /// Emit additional args of the annotation.
1342   llvm::Constant *EmitAnnotationArgs(const AnnotateAttr *Attr);
1343 
1344   /// Generate the llvm::ConstantStruct which contains the annotation
1345   /// information for a given GlobalValue. The annotation struct is
1346   /// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the
1347   /// GlobalValue being annotated. The second field is the constant string
1348   /// created from the AnnotateAttr's annotation. The third field is a constant
1349   /// string containing the name of the translation unit. The fourth field is
1350   /// the line number in the file of the annotated value declaration.
1351   llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV,
1352                                    const AnnotateAttr *AA,
1353                                    SourceLocation L);
1354 
1355   /// Add global annotations that are set on D, for the global GV. Those
1356   /// annotations are emitted during finalization of the LLVM code.
1357   void AddGlobalAnnotations(const ValueDecl *D, llvm::GlobalValue *GV);
1358 
1359   bool isInNoSanitizeList(SanitizerMask Kind, llvm::Function *Fn,
1360                           SourceLocation Loc) const;
1361 
1362   bool isInNoSanitizeList(SanitizerMask Kind, llvm::GlobalVariable *GV,
1363                           SourceLocation Loc, QualType Ty,
1364                           StringRef Category = StringRef()) const;
1365 
1366   /// Imbue XRay attributes to a function, applying the always/never attribute
1367   /// lists in the process. Returns true if we did imbue attributes this way,
1368   /// false otherwise.
1369   bool imbueXRayAttrs(llvm::Function *Fn, SourceLocation Loc,
1370                       StringRef Category = StringRef()) const;
1371 
1372   /// \returns true if \p Fn at \p Loc should be excluded from profile
1373   /// instrumentation by the SCL passed by \p -fprofile-list.
1374   ProfileList::ExclusionType
1375   isFunctionBlockedByProfileList(llvm::Function *Fn, SourceLocation Loc) const;
1376 
1377   /// \returns true if \p Fn at \p Loc should be excluded from profile
1378   /// instrumentation.
1379   ProfileList::ExclusionType
1380   isFunctionBlockedFromProfileInstr(llvm::Function *Fn,
1381                                     SourceLocation Loc) const;
1382 
1383   SanitizerMetadata *getSanitizerMetadata() {
1384     return SanitizerMD.get();
1385   }
1386 
1387   void addDeferredVTable(const CXXRecordDecl *RD) {
1388     DeferredVTables.push_back(RD);
1389   }
1390 
1391   /// Emit code for a single global function or var decl. Forward declarations
1392   /// are emitted lazily.
1393   void EmitGlobal(GlobalDecl D);
1394 
1395   bool TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D);
1396 
1397   llvm::GlobalValue *GetGlobalValue(StringRef Ref);
1398 
1399   /// Set attributes which are common to any form of a global definition (alias,
1400   /// Objective-C method, function, global variable).
1401   ///
1402   /// NOTE: This should only be called for definitions.
1403   void SetCommonAttributes(GlobalDecl GD, llvm::GlobalValue *GV);
1404 
1405   void addReplacement(StringRef Name, llvm::Constant *C);
1406 
1407   void addGlobalValReplacement(llvm::GlobalValue *GV, llvm::Constant *C);
1408 
1409   /// Emit a code for threadprivate directive.
1410   /// \param D Threadprivate declaration.
1411   void EmitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D);
1412 
1413   /// Emit a code for declare reduction construct.
1414   void EmitOMPDeclareReduction(const OMPDeclareReductionDecl *D,
1415                                CodeGenFunction *CGF = nullptr);
1416 
1417   /// Emit a code for declare mapper construct.
1418   void EmitOMPDeclareMapper(const OMPDeclareMapperDecl *D,
1419                             CodeGenFunction *CGF = nullptr);
1420 
1421   /// Emit a code for requires directive.
1422   /// \param D Requires declaration
1423   void EmitOMPRequiresDecl(const OMPRequiresDecl *D);
1424 
1425   /// Emit a code for the allocate directive.
1426   /// \param D The allocate declaration
1427   void EmitOMPAllocateDecl(const OMPAllocateDecl *D);
1428 
1429   /// Return the alignment specified in an allocate directive, if present.
1430   std::optional<CharUnits> getOMPAllocateAlignment(const VarDecl *VD);
1431 
1432   /// Returns whether the given record has hidden LTO visibility and therefore
1433   /// may participate in (single-module) CFI and whole-program vtable
1434   /// optimization.
1435   bool HasHiddenLTOVisibility(const CXXRecordDecl *RD);
1436 
1437   /// Returns whether the given record has public LTO visibility (regardless of
1438   /// -lto-whole-program-visibility) and therefore may not participate in
1439   /// (single-module) CFI and whole-program vtable optimization.
1440   bool AlwaysHasLTOVisibilityPublic(const CXXRecordDecl *RD);
1441 
1442   /// Returns the vcall visibility of the given type. This is the scope in which
1443   /// a virtual function call could be made which ends up being dispatched to a
1444   /// member function of this class. This scope can be wider than the visibility
1445   /// of the class itself when the class has a more-visible dynamic base class.
1446   /// The client should pass in an empty Visited set, which is used to prevent
1447   /// redundant recursive processing.
1448   llvm::GlobalObject::VCallVisibility
1449   GetVCallVisibilityLevel(const CXXRecordDecl *RD,
1450                           llvm::DenseSet<const CXXRecordDecl *> &Visited);
1451 
1452   /// Emit type metadata for the given vtable using the given layout.
1453   void EmitVTableTypeMetadata(const CXXRecordDecl *RD,
1454                               llvm::GlobalVariable *VTable,
1455                               const VTableLayout &VTLayout);
1456 
1457   llvm::Type *getVTableComponentType() const;
1458 
1459   /// Generate a cross-DSO type identifier for MD.
1460   llvm::ConstantInt *CreateCrossDsoCfiTypeId(llvm::Metadata *MD);
1461 
1462   /// Generate a KCFI type identifier for T.
1463   llvm::ConstantInt *CreateKCFITypeId(QualType T);
1464 
1465   /// Create a metadata identifier for the given type. This may either be an
1466   /// MDString (for external identifiers) or a distinct unnamed MDNode (for
1467   /// internal identifiers).
1468   llvm::Metadata *CreateMetadataIdentifierForType(QualType T);
1469 
1470   /// Create a metadata identifier that is intended to be used to check virtual
1471   /// calls via a member function pointer.
1472   llvm::Metadata *CreateMetadataIdentifierForVirtualMemPtrType(QualType T);
1473 
1474   /// Create a metadata identifier for the generalization of the given type.
1475   /// This may either be an MDString (for external identifiers) or a distinct
1476   /// unnamed MDNode (for internal identifiers).
1477   llvm::Metadata *CreateMetadataIdentifierGeneralized(QualType T);
1478 
1479   /// Create and attach type metadata to the given function.
1480   void CreateFunctionTypeMetadataForIcall(const FunctionDecl *FD,
1481                                           llvm::Function *F);
1482 
1483   /// Set type metadata to the given function.
1484   void setKCFIType(const FunctionDecl *FD, llvm::Function *F);
1485 
1486   /// Emit KCFI type identifier constants and remove unused identifiers.
1487   void finalizeKCFITypes();
1488 
1489   /// Whether this function's return type has no side effects, and thus may
1490   /// be trivially discarded if it is unused.
1491   bool MayDropFunctionReturn(const ASTContext &Context,
1492                              QualType ReturnType) const;
1493 
1494   /// Returns whether this module needs the "all-vtables" type identifier.
1495   bool NeedAllVtablesTypeId() const;
1496 
1497   /// Create and attach type metadata for the given vtable.
1498   void AddVTableTypeMetadata(llvm::GlobalVariable *VTable, CharUnits Offset,
1499                              const CXXRecordDecl *RD);
1500 
1501   /// Return a vector of most-base classes for RD. This is used to implement
1502   /// control flow integrity checks for member function pointers.
1503   ///
1504   /// A most-base class of a class C is defined as a recursive base class of C,
1505   /// including C itself, that does not have any bases.
1506   SmallVector<const CXXRecordDecl *, 0>
1507   getMostBaseClasses(const CXXRecordDecl *RD);
1508 
1509   /// Get the declaration of std::terminate for the platform.
1510   llvm::FunctionCallee getTerminateFn();
1511 
1512   llvm::SanitizerStatReport &getSanStats();
1513 
1514   llvm::Value *
1515   createOpenCLIntToSamplerConversion(const Expr *E, CodeGenFunction &CGF);
1516 
1517   /// OpenCL v1.2 s5.6.4.6 allows the compiler to store kernel argument
1518   /// information in the program executable. The argument information stored
1519   /// includes the argument name, its type, the address and access qualifiers
1520   /// used. This helper can be used to generate metadata for source code kernel
1521   /// function as well as generated implicitly kernels. If a kernel is generated
1522   /// implicitly null value has to be passed to the last two parameters,
1523   /// otherwise all parameters must have valid non-null values.
1524   /// \param FN is a pointer to IR function being generated.
1525   /// \param FD is a pointer to function declaration if any.
1526   /// \param CGF is a pointer to CodeGenFunction that generates this function.
1527   void GenKernelArgMetadata(llvm::Function *FN,
1528                             const FunctionDecl *FD = nullptr,
1529                             CodeGenFunction *CGF = nullptr);
1530 
1531   /// Get target specific null pointer.
1532   /// \param T is the LLVM type of the null pointer.
1533   /// \param QT is the clang QualType of the null pointer.
1534   llvm::Constant *getNullPointer(llvm::PointerType *T, QualType QT);
1535 
1536   CharUnits getNaturalTypeAlignment(QualType T,
1537                                     LValueBaseInfo *BaseInfo = nullptr,
1538                                     TBAAAccessInfo *TBAAInfo = nullptr,
1539                                     bool forPointeeType = false);
1540   CharUnits getNaturalPointeeTypeAlignment(QualType T,
1541                                            LValueBaseInfo *BaseInfo = nullptr,
1542                                            TBAAAccessInfo *TBAAInfo = nullptr);
1543   bool stopAutoInit();
1544 
1545   /// Print the postfix for externalized static variable or kernels for single
1546   /// source offloading languages CUDA and HIP. The unique postfix is created
1547   /// using either the CUID argument, or the file's UniqueID and active macros.
1548   /// The fallback method without a CUID requires that the offloading toolchain
1549   /// does not define separate macros via the -cc1 options.
1550   void printPostfixForExternalizedDecl(llvm::raw_ostream &OS,
1551                                        const Decl *D) const;
1552 
1553   /// Move some lazily-emitted states to the NewBuilder. This is especially
1554   /// essential for the incremental parsing environment like Clang Interpreter,
1555   /// because we'll lose all important information after each repl.
1556   void moveLazyEmissionStates(CodeGenModule *NewBuilder);
1557 
1558   /// Emit the IR encoding to attach the CUDA launch bounds attribute to \p F.
1559   /// If \p MaxThreadsVal is not nullptr, the max threads value is stored in it,
1560   /// if a valid one was found.
1561   void handleCUDALaunchBoundsAttr(llvm::Function *F,
1562                                   const CUDALaunchBoundsAttr *A,
1563                                   int32_t *MaxThreadsVal = nullptr,
1564                                   int32_t *MinBlocksVal = nullptr,
1565                                   int32_t *MaxClusterRankVal = nullptr);
1566 
1567   /// Emit the IR encoding to attach the AMD GPU flat-work-group-size attribute
1568   /// to \p F. Alternatively, the work group size can be taken from a \p
1569   /// ReqdWGS. If \p MinThreadsVal is not nullptr, the min threads value is
1570   /// stored in it, if a valid one was found. If \p MaxThreadsVal is not
1571   /// nullptr, the max threads value is stored in it, if a valid one was found.
1572   void handleAMDGPUFlatWorkGroupSizeAttr(
1573       llvm::Function *F, const AMDGPUFlatWorkGroupSizeAttr *A,
1574       const ReqdWorkGroupSizeAttr *ReqdWGS = nullptr,
1575       int32_t *MinThreadsVal = nullptr, int32_t *MaxThreadsVal = nullptr);
1576 
1577   /// Emit the IR encoding to attach the AMD GPU waves-per-eu attribute to \p F.
1578   void handleAMDGPUWavesPerEUAttr(llvm::Function *F,
1579                                   const AMDGPUWavesPerEUAttr *A);
1580 
1581   llvm::Constant *
1582   GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty, LangAS AddrSpace,
1583                         const VarDecl *D,
1584                         ForDefinition_t IsForDefinition = NotForDefinition);
1585 
1586   // FIXME: Hardcoding priority here is gross.
1587   void AddGlobalCtor(llvm::Function *Ctor, int Priority = 65535,
1588                      unsigned LexOrder = ~0U,
1589                      llvm::Constant *AssociatedData = nullptr);
1590   void AddGlobalDtor(llvm::Function *Dtor, int Priority = 65535,
1591                      bool IsDtorAttrFunc = false);
1592 
1593   // Return whether structured convergence intrinsics should be generated for
1594   // this target.
1595   bool shouldEmitConvergenceTokens() const {
1596     // TODO: this should probably become unconditional once the controlled
1597     // convergence becomes the norm.
1598     return getTriple().isSPIRVLogical();
1599   }
1600 
1601 private:
1602   bool shouldDropDLLAttribute(const Decl *D, const llvm::GlobalValue *GV) const;
1603 
1604   llvm::Constant *GetOrCreateLLVMFunction(
1605       StringRef MangledName, llvm::Type *Ty, GlobalDecl D, bool ForVTable,
1606       bool DontDefer = false, bool IsThunk = false,
1607       llvm::AttributeList ExtraAttrs = llvm::AttributeList(),
1608       ForDefinition_t IsForDefinition = NotForDefinition);
1609 
1610   // Adds a declaration to the list of multi version functions if not present.
1611   void AddDeferredMultiVersionResolverToEmit(GlobalDecl GD);
1612 
1613   // References to multiversion functions are resolved through an implicitly
1614   // defined resolver function. This function is responsible for creating
1615   // the resolver symbol for the provided declaration. The value returned
1616   // will be for an ifunc (llvm::GlobalIFunc) if the current target supports
1617   // that feature and for a regular function (llvm::GlobalValue) otherwise.
1618   llvm::Constant *GetOrCreateMultiVersionResolver(GlobalDecl GD);
1619 
1620   // In scenarios where a function is not known to be a multiversion function
1621   // until a later declaration, it is sometimes necessary to change the
1622   // previously created mangled name to align with requirements of whatever
1623   // multiversion function kind the function is now known to be. This function
1624   // is responsible for performing such mangled name updates.
1625   void UpdateMultiVersionNames(GlobalDecl GD, const FunctionDecl *FD,
1626                                StringRef &CurName);
1627 
1628   bool GetCPUAndFeaturesAttributes(GlobalDecl GD,
1629                                    llvm::AttrBuilder &AttrBuilder,
1630                                    bool SetTargetFeatures = true);
1631   void setNonAliasAttributes(GlobalDecl GD, llvm::GlobalObject *GO);
1632 
1633   /// Set function attributes for a function declaration.
1634   void SetFunctionAttributes(GlobalDecl GD, llvm::Function *F,
1635                              bool IsIncompleteFunction, bool IsThunk);
1636 
1637   void EmitGlobalDefinition(GlobalDecl D, llvm::GlobalValue *GV = nullptr);
1638 
1639   void EmitGlobalFunctionDefinition(GlobalDecl GD, llvm::GlobalValue *GV);
1640   void EmitMultiVersionFunctionDefinition(GlobalDecl GD, llvm::GlobalValue *GV);
1641 
1642   void EmitGlobalVarDefinition(const VarDecl *D, bool IsTentative = false);
1643   void EmitExternalVarDeclaration(const VarDecl *D);
1644   void EmitAliasDefinition(GlobalDecl GD);
1645   void emitIFuncDefinition(GlobalDecl GD);
1646   void emitCPUDispatchDefinition(GlobalDecl GD);
1647   void EmitObjCPropertyImplementations(const ObjCImplementationDecl *D);
1648   void EmitObjCIvarInitializations(ObjCImplementationDecl *D);
1649 
1650   // C++ related functions.
1651 
1652   void EmitDeclContext(const DeclContext *DC);
1653   void EmitLinkageSpec(const LinkageSpecDecl *D);
1654   void EmitTopLevelStmt(const TopLevelStmtDecl *D);
1655 
1656   /// Emit the function that initializes C++ thread_local variables.
1657   void EmitCXXThreadLocalInitFunc();
1658 
1659   /// Emit the function that initializes global variables for a C++ Module.
1660   void EmitCXXModuleInitFunc(clang::Module *Primary);
1661 
1662   /// Emit the function that initializes C++ globals.
1663   void EmitCXXGlobalInitFunc();
1664 
1665   /// Emit the function that performs cleanup associated with C++ globals.
1666   void EmitCXXGlobalCleanUpFunc();
1667 
1668   /// Emit the function that initializes the specified global (if PerformInit is
1669   /// true) and registers its destructor.
1670   void EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
1671                                     llvm::GlobalVariable *Addr,
1672                                     bool PerformInit);
1673 
1674   void EmitPointerToInitFunc(const VarDecl *VD, llvm::GlobalVariable *Addr,
1675                              llvm::Function *InitFunc, InitSegAttr *ISA);
1676 
1677   /// EmitCtorList - Generates a global array of functions and priorities using
1678   /// the given list and name. This array will have appending linkage and is
1679   /// suitable for use as a LLVM constructor or destructor array. Clears Fns.
1680   void EmitCtorList(CtorList &Fns, const char *GlobalName);
1681 
1682   /// Emit any needed decls for which code generation was deferred.
1683   void EmitDeferred();
1684 
1685   /// Try to emit external vtables as available_externally if they have emitted
1686   /// all inlined virtual functions.  It runs after EmitDeferred() and therefore
1687   /// is not allowed to create new references to things that need to be emitted
1688   /// lazily.
1689   void EmitVTablesOpportunistically();
1690 
1691   /// Call replaceAllUsesWith on all pairs in Replacements.
1692   void applyReplacements();
1693 
1694   /// Call replaceAllUsesWith on all pairs in GlobalValReplacements.
1695   void applyGlobalValReplacements();
1696 
1697   void checkAliases();
1698 
1699   std::map<int, llvm::TinyPtrVector<llvm::Function *>> DtorsUsingAtExit;
1700 
1701   /// Register functions annotated with __attribute__((destructor)) using
1702   /// __cxa_atexit, if it is available, or atexit otherwise.
1703   void registerGlobalDtorsWithAtExit();
1704 
1705   // When using sinit and sterm functions, unregister
1706   // __attribute__((destructor)) annotated functions which were previously
1707   // registered by the atexit subroutine using unatexit.
1708   void unregisterGlobalDtorsWithUnAtExit();
1709 
1710   /// Emit deferred multiversion function resolvers and associated variants.
1711   void emitMultiVersionFunctions();
1712 
1713   /// Emit any vtables which we deferred and still have a use for.
1714   void EmitDeferredVTables();
1715 
1716   /// Emit a dummy function that reference a CoreFoundation symbol when
1717   /// @available is used on Darwin.
1718   void emitAtAvailableLinkGuard();
1719 
1720   /// Emit the llvm.used and llvm.compiler.used metadata.
1721   void emitLLVMUsed();
1722 
1723   /// For C++20 Itanium ABI, emit the initializers for the module.
1724   void EmitModuleInitializers(clang::Module *Primary);
1725 
1726   /// Emit the link options introduced by imported modules.
1727   void EmitModuleLinkOptions();
1728 
1729   /// Helper function for EmitStaticExternCAliases() to redirect ifuncs that
1730   /// have a resolver name that matches 'Elem' to instead resolve to the name of
1731   /// 'CppFunc'. This redirection is necessary in cases where 'Elem' has a name
1732   /// that will be emitted as an alias of the name bound to 'CppFunc'; ifuncs
1733   /// may not reference aliases. Redirection is only performed if 'Elem' is only
1734   /// used by ifuncs in which case, 'Elem' is destroyed. 'true' is returned if
1735   /// redirection is successful, and 'false' is returned otherwise.
1736   bool CheckAndReplaceExternCIFuncs(llvm::GlobalValue *Elem,
1737                                     llvm::GlobalValue *CppFunc);
1738 
1739   /// Emit aliases for internal-linkage declarations inside "C" language
1740   /// linkage specifications, giving them the "expected" name where possible.
1741   void EmitStaticExternCAliases();
1742 
1743   void EmitDeclMetadata();
1744 
1745   /// Emit the Clang version as llvm.ident metadata.
1746   void EmitVersionIdentMetadata();
1747 
1748   /// Emit the Clang commandline as llvm.commandline metadata.
1749   void EmitCommandLineMetadata();
1750 
1751   /// Emit the module flag metadata used to pass options controlling the
1752   /// the backend to LLVM.
1753   void EmitBackendOptionsMetadata(const CodeGenOptions &CodeGenOpts);
1754 
1755   /// Emits OpenCL specific Metadata e.g. OpenCL version.
1756   void EmitOpenCLMetadata();
1757 
1758   /// Emit the llvm.gcov metadata used to tell LLVM where to emit the .gcno and
1759   /// .gcda files in a way that persists in .bc files.
1760   void EmitCoverageFile();
1761 
1762   /// Determine whether the definition must be emitted; if this returns \c
1763   /// false, the definition can be emitted lazily if it's used.
1764   bool MustBeEmitted(const ValueDecl *D);
1765 
1766   /// Determine whether the definition can be emitted eagerly, or should be
1767   /// delayed until the end of the translation unit. This is relevant for
1768   /// definitions whose linkage can change, e.g. implicit function instantions
1769   /// which may later be explicitly instantiated.
1770   bool MayBeEmittedEagerly(const ValueDecl *D);
1771 
1772   /// Check whether we can use a "simpler", more core exceptions personality
1773   /// function.
1774   void SimplifyPersonality();
1775 
1776   /// Helper function for getDefaultFunctionAttributes. Builds a set of function
1777   /// attributes which can be simply added to a function.
1778   void getTrivialDefaultFunctionAttributes(StringRef Name, bool HasOptnone,
1779                                            bool AttrOnCallSite,
1780                                            llvm::AttrBuilder &FuncAttrs);
1781 
1782   /// Helper function for ConstructAttributeList and
1783   /// addDefaultFunctionDefinitionAttributes.  Builds a set of function
1784   /// attributes to add to a function with the given properties.
1785   void getDefaultFunctionAttributes(StringRef Name, bool HasOptnone,
1786                                     bool AttrOnCallSite,
1787                                     llvm::AttrBuilder &FuncAttrs);
1788 
1789   llvm::Metadata *CreateMetadataIdentifierImpl(QualType T, MetadataTypeMap &Map,
1790                                                StringRef Suffix);
1791 };
1792 
1793 }  // end namespace CodeGen
1794 }  // end namespace clang
1795 
1796 #endif // LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H
1797