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