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