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