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