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