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