17330f729Sjoerg //===--- CodeGenModule.h - Per-Module state for LLVM CodeGen ----*- C++ -*-===// 27330f729Sjoerg // 37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information. 57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 67330f729Sjoerg // 77330f729Sjoerg //===----------------------------------------------------------------------===// 87330f729Sjoerg // 97330f729Sjoerg // This is the internal per-translation-unit state used for llvm translation. 107330f729Sjoerg // 117330f729Sjoerg //===----------------------------------------------------------------------===// 127330f729Sjoerg 137330f729Sjoerg #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H 147330f729Sjoerg #define LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H 157330f729Sjoerg 167330f729Sjoerg #include "CGVTables.h" 177330f729Sjoerg #include "CodeGenTypeCache.h" 187330f729Sjoerg #include "CodeGenTypes.h" 197330f729Sjoerg #include "SanitizerMetadata.h" 207330f729Sjoerg #include "clang/AST/DeclCXX.h" 217330f729Sjoerg #include "clang/AST/DeclObjC.h" 227330f729Sjoerg #include "clang/AST/DeclOpenMP.h" 237330f729Sjoerg #include "clang/AST/GlobalDecl.h" 247330f729Sjoerg #include "clang/AST/Mangle.h" 257330f729Sjoerg #include "clang/Basic/ABI.h" 267330f729Sjoerg #include "clang/Basic/LangOptions.h" 277330f729Sjoerg #include "clang/Basic/Module.h" 28*e038c9c4Sjoerg #include "clang/Basic/NoSanitizeList.h" 29*e038c9c4Sjoerg #include "clang/Basic/TargetInfo.h" 307330f729Sjoerg #include "clang/Basic/XRayLists.h" 31*e038c9c4Sjoerg #include "clang/Lex/PreprocessorOptions.h" 327330f729Sjoerg #include "llvm/ADT/DenseMap.h" 337330f729Sjoerg #include "llvm/ADT/SetVector.h" 347330f729Sjoerg #include "llvm/ADT/SmallPtrSet.h" 357330f729Sjoerg #include "llvm/ADT/StringMap.h" 367330f729Sjoerg #include "llvm/IR/Module.h" 377330f729Sjoerg #include "llvm/IR/ValueHandle.h" 387330f729Sjoerg #include "llvm/Transforms/Utils/SanitizerStats.h" 397330f729Sjoerg 407330f729Sjoerg namespace llvm { 417330f729Sjoerg class Module; 427330f729Sjoerg class Constant; 437330f729Sjoerg class ConstantInt; 447330f729Sjoerg class Function; 457330f729Sjoerg class GlobalValue; 467330f729Sjoerg class DataLayout; 477330f729Sjoerg class FunctionType; 487330f729Sjoerg class LLVMContext; 49*e038c9c4Sjoerg class OpenMPIRBuilder; 507330f729Sjoerg class IndexedInstrProfReader; 517330f729Sjoerg } 527330f729Sjoerg 537330f729Sjoerg namespace clang { 547330f729Sjoerg class ASTContext; 557330f729Sjoerg class AtomicType; 567330f729Sjoerg class FunctionDecl; 577330f729Sjoerg class IdentifierInfo; 587330f729Sjoerg class ObjCMethodDecl; 597330f729Sjoerg class ObjCImplementationDecl; 607330f729Sjoerg class ObjCCategoryImplDecl; 617330f729Sjoerg class ObjCProtocolDecl; 627330f729Sjoerg class ObjCEncodeExpr; 637330f729Sjoerg class BlockExpr; 647330f729Sjoerg class CharUnits; 657330f729Sjoerg class Decl; 667330f729Sjoerg class Expr; 677330f729Sjoerg class Stmt; 687330f729Sjoerg class InitListExpr; 697330f729Sjoerg class StringLiteral; 707330f729Sjoerg class NamedDecl; 717330f729Sjoerg class ValueDecl; 727330f729Sjoerg class VarDecl; 737330f729Sjoerg class LangOptions; 747330f729Sjoerg class CodeGenOptions; 757330f729Sjoerg class HeaderSearchOptions; 767330f729Sjoerg class DiagnosticsEngine; 777330f729Sjoerg class AnnotateAttr; 787330f729Sjoerg class CXXDestructorDecl; 797330f729Sjoerg class Module; 807330f729Sjoerg class CoverageSourceInfo; 81*e038c9c4Sjoerg class TargetAttr; 82*e038c9c4Sjoerg class InitSegAttr; 83*e038c9c4Sjoerg struct ParsedTargetAttr; 847330f729Sjoerg 857330f729Sjoerg namespace CodeGen { 867330f729Sjoerg 877330f729Sjoerg class CallArgList; 887330f729Sjoerg class CodeGenFunction; 897330f729Sjoerg class CodeGenTBAA; 907330f729Sjoerg class CGCXXABI; 917330f729Sjoerg class CGDebugInfo; 927330f729Sjoerg class CGObjCRuntime; 937330f729Sjoerg class CGOpenCLRuntime; 947330f729Sjoerg class CGOpenMPRuntime; 957330f729Sjoerg class CGCUDARuntime; 967330f729Sjoerg class BlockFieldFlags; 977330f729Sjoerg class FunctionArgList; 987330f729Sjoerg class CoverageMappingModuleGen; 997330f729Sjoerg class TargetCodeGenInfo; 1007330f729Sjoerg 1017330f729Sjoerg enum ForDefinition_t : bool { 1027330f729Sjoerg NotForDefinition = false, 1037330f729Sjoerg ForDefinition = true 1047330f729Sjoerg }; 1057330f729Sjoerg 106*e038c9c4Sjoerg struct OrderGlobalInitsOrStermFinalizers { 1077330f729Sjoerg unsigned int priority; 1087330f729Sjoerg unsigned int lex_order; OrderGlobalInitsOrStermFinalizersOrderGlobalInitsOrStermFinalizers109*e038c9c4Sjoerg OrderGlobalInitsOrStermFinalizers(unsigned int p, unsigned int l) 1107330f729Sjoerg : priority(p), lex_order(l) {} 1117330f729Sjoerg 112*e038c9c4Sjoerg bool operator==(const OrderGlobalInitsOrStermFinalizers &RHS) const { 1137330f729Sjoerg return priority == RHS.priority && lex_order == RHS.lex_order; 1147330f729Sjoerg } 1157330f729Sjoerg 116*e038c9c4Sjoerg bool operator<(const OrderGlobalInitsOrStermFinalizers &RHS) const { 1177330f729Sjoerg return std::tie(priority, lex_order) < 1187330f729Sjoerg std::tie(RHS.priority, RHS.lex_order); 1197330f729Sjoerg } 1207330f729Sjoerg }; 1217330f729Sjoerg 1227330f729Sjoerg struct ObjCEntrypoints { ObjCEntrypointsObjCEntrypoints1237330f729Sjoerg ObjCEntrypoints() { memset(this, 0, sizeof(*this)); } 1247330f729Sjoerg 1257330f729Sjoerg /// void objc_alloc(id); 1267330f729Sjoerg llvm::FunctionCallee objc_alloc; 1277330f729Sjoerg 1287330f729Sjoerg /// void objc_allocWithZone(id); 1297330f729Sjoerg llvm::FunctionCallee objc_allocWithZone; 1307330f729Sjoerg 1317330f729Sjoerg /// void objc_alloc_init(id); 1327330f729Sjoerg llvm::FunctionCallee objc_alloc_init; 1337330f729Sjoerg 1347330f729Sjoerg /// void objc_autoreleasePoolPop(void*); 1357330f729Sjoerg llvm::FunctionCallee objc_autoreleasePoolPop; 1367330f729Sjoerg 1377330f729Sjoerg /// void objc_autoreleasePoolPop(void*); 1387330f729Sjoerg /// Note this method is used when we are using exception handling 1397330f729Sjoerg llvm::FunctionCallee objc_autoreleasePoolPopInvoke; 1407330f729Sjoerg 1417330f729Sjoerg /// void *objc_autoreleasePoolPush(void); 1427330f729Sjoerg llvm::Function *objc_autoreleasePoolPush; 1437330f729Sjoerg 1447330f729Sjoerg /// id objc_autorelease(id); 1457330f729Sjoerg llvm::Function *objc_autorelease; 1467330f729Sjoerg 1477330f729Sjoerg /// id objc_autorelease(id); 1487330f729Sjoerg /// Note this is the runtime method not the intrinsic. 1497330f729Sjoerg llvm::FunctionCallee objc_autoreleaseRuntimeFunction; 1507330f729Sjoerg 1517330f729Sjoerg /// id objc_autoreleaseReturnValue(id); 1527330f729Sjoerg llvm::Function *objc_autoreleaseReturnValue; 1537330f729Sjoerg 1547330f729Sjoerg /// void objc_copyWeak(id *dest, id *src); 1557330f729Sjoerg llvm::Function *objc_copyWeak; 1567330f729Sjoerg 1577330f729Sjoerg /// void objc_destroyWeak(id*); 1587330f729Sjoerg llvm::Function *objc_destroyWeak; 1597330f729Sjoerg 1607330f729Sjoerg /// id objc_initWeak(id*, id); 1617330f729Sjoerg llvm::Function *objc_initWeak; 1627330f729Sjoerg 1637330f729Sjoerg /// id objc_loadWeak(id*); 1647330f729Sjoerg llvm::Function *objc_loadWeak; 1657330f729Sjoerg 1667330f729Sjoerg /// id objc_loadWeakRetained(id*); 1677330f729Sjoerg llvm::Function *objc_loadWeakRetained; 1687330f729Sjoerg 1697330f729Sjoerg /// void objc_moveWeak(id *dest, id *src); 1707330f729Sjoerg llvm::Function *objc_moveWeak; 1717330f729Sjoerg 1727330f729Sjoerg /// id objc_retain(id); 1737330f729Sjoerg llvm::Function *objc_retain; 1747330f729Sjoerg 1757330f729Sjoerg /// id objc_retain(id); 1767330f729Sjoerg /// Note this is the runtime method not the intrinsic. 1777330f729Sjoerg llvm::FunctionCallee objc_retainRuntimeFunction; 1787330f729Sjoerg 1797330f729Sjoerg /// id objc_retainAutorelease(id); 1807330f729Sjoerg llvm::Function *objc_retainAutorelease; 1817330f729Sjoerg 1827330f729Sjoerg /// id objc_retainAutoreleaseReturnValue(id); 1837330f729Sjoerg llvm::Function *objc_retainAutoreleaseReturnValue; 1847330f729Sjoerg 1857330f729Sjoerg /// id objc_retainAutoreleasedReturnValue(id); 1867330f729Sjoerg llvm::Function *objc_retainAutoreleasedReturnValue; 1877330f729Sjoerg 1887330f729Sjoerg /// id objc_retainBlock(id); 1897330f729Sjoerg llvm::Function *objc_retainBlock; 1907330f729Sjoerg 1917330f729Sjoerg /// void objc_release(id); 1927330f729Sjoerg llvm::Function *objc_release; 1937330f729Sjoerg 1947330f729Sjoerg /// void objc_release(id); 1957330f729Sjoerg /// Note this is the runtime method not the intrinsic. 1967330f729Sjoerg llvm::FunctionCallee objc_releaseRuntimeFunction; 1977330f729Sjoerg 1987330f729Sjoerg /// void objc_storeStrong(id*, id); 1997330f729Sjoerg llvm::Function *objc_storeStrong; 2007330f729Sjoerg 2017330f729Sjoerg /// id objc_storeWeak(id*, id); 2027330f729Sjoerg llvm::Function *objc_storeWeak; 2037330f729Sjoerg 2047330f729Sjoerg /// id objc_unsafeClaimAutoreleasedReturnValue(id); 2057330f729Sjoerg llvm::Function *objc_unsafeClaimAutoreleasedReturnValue; 2067330f729Sjoerg 2077330f729Sjoerg /// A void(void) inline asm to use to mark that the return value of 2087330f729Sjoerg /// a call will be immediately retain. 2097330f729Sjoerg llvm::InlineAsm *retainAutoreleasedReturnValueMarker; 2107330f729Sjoerg 2117330f729Sjoerg /// void clang.arc.use(...); 2127330f729Sjoerg llvm::Function *clang_arc_use; 213*e038c9c4Sjoerg 214*e038c9c4Sjoerg /// void clang.arc.noop.use(...); 215*e038c9c4Sjoerg llvm::Function *clang_arc_noop_use; 2167330f729Sjoerg }; 2177330f729Sjoerg 2187330f729Sjoerg /// This class records statistics on instrumentation based profiling. 2197330f729Sjoerg class InstrProfStats { 2207330f729Sjoerg uint32_t VisitedInMainFile; 2217330f729Sjoerg uint32_t MissingInMainFile; 2227330f729Sjoerg uint32_t Visited; 2237330f729Sjoerg uint32_t Missing; 2247330f729Sjoerg uint32_t Mismatched; 2257330f729Sjoerg 2267330f729Sjoerg public: InstrProfStats()2277330f729Sjoerg InstrProfStats() 2287330f729Sjoerg : VisitedInMainFile(0), MissingInMainFile(0), Visited(0), Missing(0), 2297330f729Sjoerg Mismatched(0) {} 2307330f729Sjoerg /// Record that we've visited a function and whether or not that function was 2317330f729Sjoerg /// in the main source file. addVisited(bool MainFile)2327330f729Sjoerg void addVisited(bool MainFile) { 2337330f729Sjoerg if (MainFile) 2347330f729Sjoerg ++VisitedInMainFile; 2357330f729Sjoerg ++Visited; 2367330f729Sjoerg } 2377330f729Sjoerg /// Record that a function we've visited has no profile data. addMissing(bool MainFile)2387330f729Sjoerg void addMissing(bool MainFile) { 2397330f729Sjoerg if (MainFile) 2407330f729Sjoerg ++MissingInMainFile; 2417330f729Sjoerg ++Missing; 2427330f729Sjoerg } 2437330f729Sjoerg /// Record that a function we've visited has mismatched profile data. addMismatched(bool MainFile)2447330f729Sjoerg void addMismatched(bool MainFile) { ++Mismatched; } 2457330f729Sjoerg /// Whether or not the stats we've gathered indicate any potential problems. hasDiagnostics()2467330f729Sjoerg bool hasDiagnostics() { return Missing || Mismatched; } 2477330f729Sjoerg /// Report potential problems we've found to \c Diags. 2487330f729Sjoerg void reportDiagnostics(DiagnosticsEngine &Diags, StringRef MainFile); 2497330f729Sjoerg }; 2507330f729Sjoerg 2517330f729Sjoerg /// A pair of helper functions for a __block variable. 2527330f729Sjoerg class BlockByrefHelpers : public llvm::FoldingSetNode { 2537330f729Sjoerg // MSVC requires this type to be complete in order to process this 2547330f729Sjoerg // header. 2557330f729Sjoerg public: 2567330f729Sjoerg llvm::Constant *CopyHelper; 2577330f729Sjoerg llvm::Constant *DisposeHelper; 2587330f729Sjoerg 2597330f729Sjoerg /// The alignment of the field. This is important because 2607330f729Sjoerg /// different offsets to the field within the byref struct need to 2617330f729Sjoerg /// have different helper functions. 2627330f729Sjoerg CharUnits Alignment; 2637330f729Sjoerg BlockByrefHelpers(CharUnits alignment)2647330f729Sjoerg BlockByrefHelpers(CharUnits alignment) 2657330f729Sjoerg : CopyHelper(nullptr), DisposeHelper(nullptr), Alignment(alignment) {} 2667330f729Sjoerg BlockByrefHelpers(const BlockByrefHelpers &) = default; 2677330f729Sjoerg virtual ~BlockByrefHelpers(); 2687330f729Sjoerg Profile(llvm::FoldingSetNodeID & id)2697330f729Sjoerg void Profile(llvm::FoldingSetNodeID &id) const { 2707330f729Sjoerg id.AddInteger(Alignment.getQuantity()); 2717330f729Sjoerg profileImpl(id); 2727330f729Sjoerg } 2737330f729Sjoerg virtual void profileImpl(llvm::FoldingSetNodeID &id) const = 0; 2747330f729Sjoerg needsCopy()2757330f729Sjoerg virtual bool needsCopy() const { return true; } 2767330f729Sjoerg virtual void emitCopy(CodeGenFunction &CGF, Address dest, Address src) = 0; 2777330f729Sjoerg needsDispose()2787330f729Sjoerg virtual bool needsDispose() const { return true; } 2797330f729Sjoerg virtual void emitDispose(CodeGenFunction &CGF, Address field) = 0; 2807330f729Sjoerg }; 2817330f729Sjoerg 2827330f729Sjoerg /// This class organizes the cross-function state that is used while generating 2837330f729Sjoerg /// LLVM code. 2847330f729Sjoerg class CodeGenModule : public CodeGenTypeCache { 2857330f729Sjoerg CodeGenModule(const CodeGenModule &) = delete; 2867330f729Sjoerg void operator=(const CodeGenModule &) = delete; 2877330f729Sjoerg 2887330f729Sjoerg public: 2897330f729Sjoerg struct Structor { StructorStructor2907330f729Sjoerg Structor() : Priority(0), Initializer(nullptr), AssociatedData(nullptr) {} StructorStructor2917330f729Sjoerg Structor(int Priority, llvm::Constant *Initializer, 2927330f729Sjoerg llvm::Constant *AssociatedData) 2937330f729Sjoerg : Priority(Priority), Initializer(Initializer), 2947330f729Sjoerg AssociatedData(AssociatedData) {} 2957330f729Sjoerg int Priority; 2967330f729Sjoerg llvm::Constant *Initializer; 2977330f729Sjoerg llvm::Constant *AssociatedData; 2987330f729Sjoerg }; 2997330f729Sjoerg 3007330f729Sjoerg typedef std::vector<Structor> CtorList; 3017330f729Sjoerg 3027330f729Sjoerg private: 3037330f729Sjoerg ASTContext &Context; 3047330f729Sjoerg const LangOptions &LangOpts; 3057330f729Sjoerg const HeaderSearchOptions &HeaderSearchOpts; // Only used for debug info. 3067330f729Sjoerg const PreprocessorOptions &PreprocessorOpts; // Only used for debug info. 3077330f729Sjoerg const CodeGenOptions &CodeGenOpts; 308*e038c9c4Sjoerg unsigned NumAutoVarInit = 0; 3097330f729Sjoerg llvm::Module &TheModule; 3107330f729Sjoerg DiagnosticsEngine &Diags; 3117330f729Sjoerg const TargetInfo &Target; 3127330f729Sjoerg std::unique_ptr<CGCXXABI> ABI; 3137330f729Sjoerg llvm::LLVMContext &VMContext; 314*e038c9c4Sjoerg std::string ModuleNameHash = ""; 3157330f729Sjoerg 3167330f729Sjoerg std::unique_ptr<CodeGenTBAA> TBAA; 3177330f729Sjoerg 3187330f729Sjoerg mutable std::unique_ptr<TargetCodeGenInfo> TheTargetCodeGenInfo; 3197330f729Sjoerg 3207330f729Sjoerg // This should not be moved earlier, since its initialization depends on some 3217330f729Sjoerg // of the previous reference members being already initialized and also checks 3227330f729Sjoerg // if TheTargetCodeGenInfo is NULL 3237330f729Sjoerg CodeGenTypes Types; 3247330f729Sjoerg 3257330f729Sjoerg /// Holds information about C++ vtables. 3267330f729Sjoerg CodeGenVTables VTables; 3277330f729Sjoerg 3287330f729Sjoerg std::unique_ptr<CGObjCRuntime> ObjCRuntime; 3297330f729Sjoerg std::unique_ptr<CGOpenCLRuntime> OpenCLRuntime; 3307330f729Sjoerg std::unique_ptr<CGOpenMPRuntime> OpenMPRuntime; 3317330f729Sjoerg std::unique_ptr<CGCUDARuntime> CUDARuntime; 3327330f729Sjoerg std::unique_ptr<CGDebugInfo> DebugInfo; 3337330f729Sjoerg std::unique_ptr<ObjCEntrypoints> ObjCData; 3347330f729Sjoerg llvm::MDNode *NoObjCARCExceptionsMetadata = nullptr; 3357330f729Sjoerg std::unique_ptr<llvm::IndexedInstrProfReader> PGOReader; 3367330f729Sjoerg InstrProfStats PGOStats; 3377330f729Sjoerg std::unique_ptr<llvm::SanitizerStatReport> SanStats; 3387330f729Sjoerg 3397330f729Sjoerg // A set of references that have only been seen via a weakref so far. This is 3407330f729Sjoerg // used to remove the weak of the reference if we ever see a direct reference 3417330f729Sjoerg // or a definition. 3427330f729Sjoerg llvm::SmallPtrSet<llvm::GlobalValue*, 10> WeakRefReferences; 3437330f729Sjoerg 3447330f729Sjoerg /// This contains all the decls which have definitions but/ which are deferred 3457330f729Sjoerg /// for emission and therefore should only be output if they are actually 3467330f729Sjoerg /// used. If a decl is in this, then it is known to have not been referenced 3477330f729Sjoerg /// yet. 3487330f729Sjoerg std::map<StringRef, GlobalDecl> DeferredDecls; 3497330f729Sjoerg 3507330f729Sjoerg /// This is a list of deferred decls which we have seen that *are* actually 3517330f729Sjoerg /// referenced. These get code generated when the module is done. 3527330f729Sjoerg std::vector<GlobalDecl> DeferredDeclsToEmit; addDeferredDeclToEmit(GlobalDecl GD)3537330f729Sjoerg void addDeferredDeclToEmit(GlobalDecl GD) { 3547330f729Sjoerg DeferredDeclsToEmit.emplace_back(GD); 3557330f729Sjoerg } 3567330f729Sjoerg 3577330f729Sjoerg /// List of alias we have emitted. Used to make sure that what they point to 3587330f729Sjoerg /// is defined once we get to the end of the of the translation unit. 3597330f729Sjoerg std::vector<GlobalDecl> Aliases; 3607330f729Sjoerg 3617330f729Sjoerg /// List of multiversion functions that have to be emitted. Used to make sure 3627330f729Sjoerg /// we properly emit the iFunc. 3637330f729Sjoerg std::vector<GlobalDecl> MultiVersionFuncs; 3647330f729Sjoerg 3657330f729Sjoerg typedef llvm::StringMap<llvm::TrackingVH<llvm::Constant> > ReplacementsTy; 3667330f729Sjoerg ReplacementsTy Replacements; 3677330f729Sjoerg 3687330f729Sjoerg /// List of global values to be replaced with something else. Used when we 3697330f729Sjoerg /// want to replace a GlobalValue but can't identify it by its mangled name 3707330f729Sjoerg /// anymore (because the name is already taken). 3717330f729Sjoerg llvm::SmallVector<std::pair<llvm::GlobalValue *, llvm::Constant *>, 8> 3727330f729Sjoerg GlobalValReplacements; 3737330f729Sjoerg 3747330f729Sjoerg /// Variables for which we've emitted globals containing their constant 3757330f729Sjoerg /// values along with the corresponding globals, for opportunistic reuse. 3767330f729Sjoerg llvm::DenseMap<const VarDecl*, llvm::GlobalVariable*> InitializerConstants; 3777330f729Sjoerg 3787330f729Sjoerg /// Set of global decls for which we already diagnosed mangled name conflict. 3797330f729Sjoerg /// Required to not issue a warning (on a mangling conflict) multiple times 3807330f729Sjoerg /// for the same decl. 3817330f729Sjoerg llvm::DenseSet<GlobalDecl> DiagnosedConflictingDefinitions; 3827330f729Sjoerg 3837330f729Sjoerg /// A queue of (optional) vtables to consider emitting. 3847330f729Sjoerg std::vector<const CXXRecordDecl*> DeferredVTables; 3857330f729Sjoerg 3867330f729Sjoerg /// A queue of (optional) vtables that may be emitted opportunistically. 3877330f729Sjoerg std::vector<const CXXRecordDecl *> OpportunisticVTables; 3887330f729Sjoerg 3897330f729Sjoerg /// List of global values which are required to be present in the object file; 3907330f729Sjoerg /// bitcast to i8*. This is used for forcing visibility of symbols which may 3917330f729Sjoerg /// otherwise be optimized out. 3927330f729Sjoerg std::vector<llvm::WeakTrackingVH> LLVMUsed; 3937330f729Sjoerg std::vector<llvm::WeakTrackingVH> LLVMCompilerUsed; 3947330f729Sjoerg 3957330f729Sjoerg /// Store the list of global constructors and their respective priorities to 3967330f729Sjoerg /// be emitted when the translation unit is complete. 3977330f729Sjoerg CtorList GlobalCtors; 3987330f729Sjoerg 3997330f729Sjoerg /// Store the list of global destructors and their respective priorities to be 4007330f729Sjoerg /// emitted when the translation unit is complete. 4017330f729Sjoerg CtorList GlobalDtors; 4027330f729Sjoerg 4037330f729Sjoerg /// An ordered map of canonical GlobalDecls to their mangled names. 4047330f729Sjoerg llvm::MapVector<GlobalDecl, StringRef> MangledDeclNames; 4057330f729Sjoerg llvm::StringMap<GlobalDecl, llvm::BumpPtrAllocator> Manglings; 4067330f729Sjoerg 4077330f729Sjoerg // An ordered map of canonical GlobalDecls paired with the cpu-index for 4087330f729Sjoerg // cpu-specific name manglings. 4097330f729Sjoerg llvm::MapVector<std::pair<GlobalDecl, unsigned>, StringRef> 4107330f729Sjoerg CPUSpecificMangledDeclNames; 4117330f729Sjoerg llvm::StringMap<std::pair<GlobalDecl, unsigned>, llvm::BumpPtrAllocator> 4127330f729Sjoerg CPUSpecificManglings; 4137330f729Sjoerg 4147330f729Sjoerg /// Global annotations. 4157330f729Sjoerg std::vector<llvm::Constant*> Annotations; 4167330f729Sjoerg 4177330f729Sjoerg /// Map used to get unique annotation strings. 4187330f729Sjoerg llvm::StringMap<llvm::Constant*> AnnotationStrings; 4197330f729Sjoerg 420*e038c9c4Sjoerg /// Used for uniquing of annotation arguments. 421*e038c9c4Sjoerg llvm::DenseMap<unsigned, llvm::Constant *> AnnotationArgs; 422*e038c9c4Sjoerg 4237330f729Sjoerg llvm::StringMap<llvm::GlobalVariable *> CFConstantStringMap; 4247330f729Sjoerg 4257330f729Sjoerg llvm::DenseMap<llvm::Constant *, llvm::GlobalVariable *> ConstantStringMap; 4267330f729Sjoerg llvm::DenseMap<const Decl*, llvm::Constant *> StaticLocalDeclMap; 4277330f729Sjoerg llvm::DenseMap<const Decl*, llvm::GlobalVariable*> StaticLocalDeclGuardMap; 4287330f729Sjoerg llvm::DenseMap<const Expr*, llvm::Constant *> MaterializedGlobalTemporaryMap; 4297330f729Sjoerg 4307330f729Sjoerg llvm::DenseMap<QualType, llvm::Constant *> AtomicSetterHelperFnMap; 4317330f729Sjoerg llvm::DenseMap<QualType, llvm::Constant *> AtomicGetterHelperFnMap; 4327330f729Sjoerg 4337330f729Sjoerg /// Map used to get unique type descriptor constants for sanitizers. 4347330f729Sjoerg llvm::DenseMap<QualType, llvm::Constant *> TypeDescriptorMap; 4357330f729Sjoerg 4367330f729Sjoerg /// Map used to track internal linkage functions declared within 4377330f729Sjoerg /// extern "C" regions. 4387330f729Sjoerg typedef llvm::MapVector<IdentifierInfo *, 4397330f729Sjoerg llvm::GlobalValue *> StaticExternCMap; 4407330f729Sjoerg StaticExternCMap StaticExternCValues; 4417330f729Sjoerg 4427330f729Sjoerg /// thread_local variables defined or used in this TU. 4437330f729Sjoerg std::vector<const VarDecl *> CXXThreadLocals; 4447330f729Sjoerg 4457330f729Sjoerg /// thread_local variables with initializers that need to run 4467330f729Sjoerg /// before any thread_local variable in this TU is odr-used. 4477330f729Sjoerg std::vector<llvm::Function *> CXXThreadLocalInits; 4487330f729Sjoerg std::vector<const VarDecl *> CXXThreadLocalInitVars; 4497330f729Sjoerg 4507330f729Sjoerg /// Global variables with initializers that need to run before main. 4517330f729Sjoerg std::vector<llvm::Function *> CXXGlobalInits; 4527330f729Sjoerg 4537330f729Sjoerg /// When a C++ decl with an initializer is deferred, null is 4547330f729Sjoerg /// appended to CXXGlobalInits, and the index of that null is placed 4557330f729Sjoerg /// here so that the initializer will be performed in the correct 4567330f729Sjoerg /// order. Once the decl is emitted, the index is replaced with ~0U to ensure 4577330f729Sjoerg /// that we don't re-emit the initializer. 4587330f729Sjoerg llvm::DenseMap<const Decl*, unsigned> DelayedCXXInitPosition; 4597330f729Sjoerg 460*e038c9c4Sjoerg typedef std::pair<OrderGlobalInitsOrStermFinalizers, llvm::Function *> 461*e038c9c4Sjoerg GlobalInitData; 4627330f729Sjoerg 4637330f729Sjoerg struct GlobalInitPriorityCmp { operatorGlobalInitPriorityCmp4647330f729Sjoerg bool operator()(const GlobalInitData &LHS, 4657330f729Sjoerg const GlobalInitData &RHS) const { 4667330f729Sjoerg return LHS.first.priority < RHS.first.priority; 4677330f729Sjoerg } 4687330f729Sjoerg }; 4697330f729Sjoerg 4707330f729Sjoerg /// Global variables with initializers whose order of initialization is set by 4717330f729Sjoerg /// init_priority attribute. 4727330f729Sjoerg SmallVector<GlobalInitData, 8> PrioritizedCXXGlobalInits; 4737330f729Sjoerg 4747330f729Sjoerg /// Global destructor functions and arguments that need to run on termination. 475*e038c9c4Sjoerg /// When UseSinitAndSterm is set, it instead contains sterm finalizer 476*e038c9c4Sjoerg /// functions, which also run on unloading a shared library. 477*e038c9c4Sjoerg typedef std::tuple<llvm::FunctionType *, llvm::WeakTrackingVH, 478*e038c9c4Sjoerg llvm::Constant *> 479*e038c9c4Sjoerg CXXGlobalDtorsOrStermFinalizer_t; 480*e038c9c4Sjoerg SmallVector<CXXGlobalDtorsOrStermFinalizer_t, 8> 481*e038c9c4Sjoerg CXXGlobalDtorsOrStermFinalizers; 482*e038c9c4Sjoerg 483*e038c9c4Sjoerg typedef std::pair<OrderGlobalInitsOrStermFinalizers, llvm::Function *> 484*e038c9c4Sjoerg StermFinalizerData; 485*e038c9c4Sjoerg 486*e038c9c4Sjoerg struct StermFinalizerPriorityCmp { operatorStermFinalizerPriorityCmp487*e038c9c4Sjoerg bool operator()(const StermFinalizerData &LHS, 488*e038c9c4Sjoerg const StermFinalizerData &RHS) const { 489*e038c9c4Sjoerg return LHS.first.priority < RHS.first.priority; 490*e038c9c4Sjoerg } 491*e038c9c4Sjoerg }; 492*e038c9c4Sjoerg 493*e038c9c4Sjoerg /// Global variables with sterm finalizers whose order of initialization is 494*e038c9c4Sjoerg /// set by init_priority attribute. 495*e038c9c4Sjoerg SmallVector<StermFinalizerData, 8> PrioritizedCXXStermFinalizers; 4967330f729Sjoerg 4977330f729Sjoerg /// The complete set of modules that has been imported. 4987330f729Sjoerg llvm::SetVector<clang::Module *> ImportedModules; 4997330f729Sjoerg 5007330f729Sjoerg /// The set of modules for which the module initializers 5017330f729Sjoerg /// have been emitted. 5027330f729Sjoerg llvm::SmallPtrSet<clang::Module *, 16> EmittedModuleInitializers; 5037330f729Sjoerg 5047330f729Sjoerg /// A vector of metadata strings for linker options. 5057330f729Sjoerg SmallVector<llvm::MDNode *, 16> LinkerOptionsMetadata; 5067330f729Sjoerg 5077330f729Sjoerg /// A vector of metadata strings for dependent libraries for ELF. 5087330f729Sjoerg SmallVector<llvm::MDNode *, 16> ELFDependentLibraries; 5097330f729Sjoerg 5107330f729Sjoerg /// @name Cache for Objective-C runtime types 5117330f729Sjoerg /// @{ 5127330f729Sjoerg 5137330f729Sjoerg /// Cached reference to the class for constant strings. This value has type 5147330f729Sjoerg /// int * but is actually an Obj-C class pointer. 5157330f729Sjoerg llvm::WeakTrackingVH CFConstantStringClassRef; 5167330f729Sjoerg 5177330f729Sjoerg /// The type used to describe the state of a fast enumeration in 5187330f729Sjoerg /// Objective-C's for..in loop. 5197330f729Sjoerg QualType ObjCFastEnumerationStateType; 5207330f729Sjoerg 5217330f729Sjoerg /// @} 5227330f729Sjoerg 5237330f729Sjoerg /// Lazily create the Objective-C runtime 5247330f729Sjoerg void createObjCRuntime(); 5257330f729Sjoerg 5267330f729Sjoerg void createOpenCLRuntime(); 5277330f729Sjoerg void createOpenMPRuntime(); 5287330f729Sjoerg void createCUDARuntime(); 5297330f729Sjoerg 5307330f729Sjoerg bool isTriviallyRecursive(const FunctionDecl *F); 5317330f729Sjoerg bool shouldEmitFunction(GlobalDecl GD); 5327330f729Sjoerg bool shouldOpportunisticallyEmitVTables(); 5337330f729Sjoerg /// Map used to be sure we don't emit the same CompoundLiteral twice. 5347330f729Sjoerg llvm::DenseMap<const CompoundLiteralExpr *, llvm::GlobalVariable *> 5357330f729Sjoerg EmittedCompoundLiterals; 5367330f729Sjoerg 5377330f729Sjoerg /// Map of the global blocks we've emitted, so that we don't have to re-emit 5387330f729Sjoerg /// them if the constexpr evaluator gets aggressive. 5397330f729Sjoerg llvm::DenseMap<const BlockExpr *, llvm::Constant *> EmittedGlobalBlocks; 5407330f729Sjoerg 5417330f729Sjoerg /// @name Cache for Blocks Runtime Globals 5427330f729Sjoerg /// @{ 5437330f729Sjoerg 5447330f729Sjoerg llvm::Constant *NSConcreteGlobalBlock = nullptr; 5457330f729Sjoerg llvm::Constant *NSConcreteStackBlock = nullptr; 5467330f729Sjoerg 5477330f729Sjoerg llvm::FunctionCallee BlockObjectAssign = nullptr; 5487330f729Sjoerg llvm::FunctionCallee BlockObjectDispose = nullptr; 5497330f729Sjoerg 5507330f729Sjoerg llvm::Type *BlockDescriptorType = nullptr; 5517330f729Sjoerg llvm::Type *GenericBlockLiteralType = nullptr; 5527330f729Sjoerg 5537330f729Sjoerg struct { 5547330f729Sjoerg int GlobalUniqueCount; 5557330f729Sjoerg } Block; 5567330f729Sjoerg 557*e038c9c4Sjoerg GlobalDecl initializedGlobalDecl; 558*e038c9c4Sjoerg 559*e038c9c4Sjoerg /// @} 560*e038c9c4Sjoerg 5617330f729Sjoerg /// void @llvm.lifetime.start(i64 %size, i8* nocapture <ptr>) 5627330f729Sjoerg llvm::Function *LifetimeStartFn = nullptr; 5637330f729Sjoerg 5647330f729Sjoerg /// void @llvm.lifetime.end(i64 %size, i8* nocapture <ptr>) 5657330f729Sjoerg llvm::Function *LifetimeEndFn = nullptr; 5667330f729Sjoerg 5677330f729Sjoerg std::unique_ptr<SanitizerMetadata> SanitizerMD; 5687330f729Sjoerg 5697330f729Sjoerg llvm::MapVector<const Decl *, bool> DeferredEmptyCoverageMappingDecls; 5707330f729Sjoerg 5717330f729Sjoerg std::unique_ptr<CoverageMappingModuleGen> CoverageMapping; 5727330f729Sjoerg 5737330f729Sjoerg /// Mapping from canonical types to their metadata identifiers. We need to 5747330f729Sjoerg /// maintain this mapping because identifiers may be formed from distinct 5757330f729Sjoerg /// MDNodes. 5767330f729Sjoerg typedef llvm::DenseMap<QualType, llvm::Metadata *> MetadataTypeMap; 5777330f729Sjoerg MetadataTypeMap MetadataIdMap; 5787330f729Sjoerg MetadataTypeMap VirtualMetadataIdMap; 5797330f729Sjoerg MetadataTypeMap GeneralizedMetadataIdMap; 5807330f729Sjoerg 5817330f729Sjoerg public: 5827330f729Sjoerg CodeGenModule(ASTContext &C, const HeaderSearchOptions &headersearchopts, 5837330f729Sjoerg const PreprocessorOptions &ppopts, 5847330f729Sjoerg const CodeGenOptions &CodeGenOpts, llvm::Module &M, 5857330f729Sjoerg DiagnosticsEngine &Diags, 5867330f729Sjoerg CoverageSourceInfo *CoverageInfo = nullptr); 5877330f729Sjoerg 5887330f729Sjoerg ~CodeGenModule(); 5897330f729Sjoerg 5907330f729Sjoerg void clear(); 5917330f729Sjoerg 5927330f729Sjoerg /// Finalize LLVM code generation. 5937330f729Sjoerg void Release(); 5947330f729Sjoerg 5957330f729Sjoerg /// Return true if we should emit location information for expressions. 5967330f729Sjoerg bool getExpressionLocationsEnabled() const; 5977330f729Sjoerg 5987330f729Sjoerg /// Return a reference to the configured Objective-C runtime. getObjCRuntime()5997330f729Sjoerg CGObjCRuntime &getObjCRuntime() { 6007330f729Sjoerg if (!ObjCRuntime) createObjCRuntime(); 6017330f729Sjoerg return *ObjCRuntime; 6027330f729Sjoerg } 6037330f729Sjoerg 6047330f729Sjoerg /// Return true iff an Objective-C runtime has been configured. hasObjCRuntime()6057330f729Sjoerg bool hasObjCRuntime() { return !!ObjCRuntime; } 6067330f729Sjoerg getModuleNameHash()607*e038c9c4Sjoerg const std::string &getModuleNameHash() const { return ModuleNameHash; } 608*e038c9c4Sjoerg 6097330f729Sjoerg /// Return a reference to the configured OpenCL runtime. getOpenCLRuntime()6107330f729Sjoerg CGOpenCLRuntime &getOpenCLRuntime() { 6117330f729Sjoerg assert(OpenCLRuntime != nullptr); 6127330f729Sjoerg return *OpenCLRuntime; 6137330f729Sjoerg } 6147330f729Sjoerg 6157330f729Sjoerg /// Return a reference to the configured OpenMP runtime. getOpenMPRuntime()6167330f729Sjoerg CGOpenMPRuntime &getOpenMPRuntime() { 6177330f729Sjoerg assert(OpenMPRuntime != nullptr); 6187330f729Sjoerg return *OpenMPRuntime; 6197330f729Sjoerg } 6207330f729Sjoerg 6217330f729Sjoerg /// Return a reference to the configured CUDA runtime. getCUDARuntime()6227330f729Sjoerg CGCUDARuntime &getCUDARuntime() { 6237330f729Sjoerg assert(CUDARuntime != nullptr); 6247330f729Sjoerg return *CUDARuntime; 6257330f729Sjoerg } 6267330f729Sjoerg getObjCEntrypoints()6277330f729Sjoerg ObjCEntrypoints &getObjCEntrypoints() const { 6287330f729Sjoerg assert(ObjCData != nullptr); 6297330f729Sjoerg return *ObjCData; 6307330f729Sjoerg } 6317330f729Sjoerg 632*e038c9c4Sjoerg // Version checking functions, used to implement ObjC's @available: 6337330f729Sjoerg // i32 @__isOSVersionAtLeast(i32, i32, i32) 6347330f729Sjoerg llvm::FunctionCallee IsOSVersionAtLeastFn = nullptr; 635*e038c9c4Sjoerg // i32 @__isPlatformVersionAtLeast(i32, i32, i32, i32) 636*e038c9c4Sjoerg llvm::FunctionCallee IsPlatformVersionAtLeastFn = nullptr; 6377330f729Sjoerg getPGOStats()6387330f729Sjoerg InstrProfStats &getPGOStats() { return PGOStats; } getPGOReader()6397330f729Sjoerg llvm::IndexedInstrProfReader *getPGOReader() const { return PGOReader.get(); } 6407330f729Sjoerg getCoverageMapping()6417330f729Sjoerg CoverageMappingModuleGen *getCoverageMapping() const { 6427330f729Sjoerg return CoverageMapping.get(); 6437330f729Sjoerg } 6447330f729Sjoerg getStaticLocalDeclAddress(const VarDecl * D)6457330f729Sjoerg llvm::Constant *getStaticLocalDeclAddress(const VarDecl *D) { 6467330f729Sjoerg return StaticLocalDeclMap[D]; 6477330f729Sjoerg } setStaticLocalDeclAddress(const VarDecl * D,llvm::Constant * C)6487330f729Sjoerg void setStaticLocalDeclAddress(const VarDecl *D, 6497330f729Sjoerg llvm::Constant *C) { 6507330f729Sjoerg StaticLocalDeclMap[D] = C; 6517330f729Sjoerg } 6527330f729Sjoerg 6537330f729Sjoerg llvm::Constant * 6547330f729Sjoerg getOrCreateStaticVarDecl(const VarDecl &D, 6557330f729Sjoerg llvm::GlobalValue::LinkageTypes Linkage); 6567330f729Sjoerg getStaticLocalDeclGuardAddress(const VarDecl * D)6577330f729Sjoerg llvm::GlobalVariable *getStaticLocalDeclGuardAddress(const VarDecl *D) { 6587330f729Sjoerg return StaticLocalDeclGuardMap[D]; 6597330f729Sjoerg } setStaticLocalDeclGuardAddress(const VarDecl * D,llvm::GlobalVariable * C)6607330f729Sjoerg void setStaticLocalDeclGuardAddress(const VarDecl *D, 6617330f729Sjoerg llvm::GlobalVariable *C) { 6627330f729Sjoerg StaticLocalDeclGuardMap[D] = C; 6637330f729Sjoerg } 6647330f729Sjoerg 6657330f729Sjoerg Address createUnnamedGlobalFrom(const VarDecl &D, llvm::Constant *Constant, 6667330f729Sjoerg CharUnits Align); 6677330f729Sjoerg 6687330f729Sjoerg bool lookupRepresentativeDecl(StringRef MangledName, 6697330f729Sjoerg GlobalDecl &Result) const; 6707330f729Sjoerg getAtomicSetterHelperFnMap(QualType Ty)6717330f729Sjoerg llvm::Constant *getAtomicSetterHelperFnMap(QualType Ty) { 6727330f729Sjoerg return AtomicSetterHelperFnMap[Ty]; 6737330f729Sjoerg } setAtomicSetterHelperFnMap(QualType Ty,llvm::Constant * Fn)6747330f729Sjoerg void setAtomicSetterHelperFnMap(QualType Ty, 6757330f729Sjoerg llvm::Constant *Fn) { 6767330f729Sjoerg AtomicSetterHelperFnMap[Ty] = Fn; 6777330f729Sjoerg } 6787330f729Sjoerg getAtomicGetterHelperFnMap(QualType Ty)6797330f729Sjoerg llvm::Constant *getAtomicGetterHelperFnMap(QualType Ty) { 6807330f729Sjoerg return AtomicGetterHelperFnMap[Ty]; 6817330f729Sjoerg } setAtomicGetterHelperFnMap(QualType Ty,llvm::Constant * Fn)6827330f729Sjoerg void setAtomicGetterHelperFnMap(QualType Ty, 6837330f729Sjoerg llvm::Constant *Fn) { 6847330f729Sjoerg AtomicGetterHelperFnMap[Ty] = Fn; 6857330f729Sjoerg } 6867330f729Sjoerg getTypeDescriptorFromMap(QualType Ty)6877330f729Sjoerg llvm::Constant *getTypeDescriptorFromMap(QualType Ty) { 6887330f729Sjoerg return TypeDescriptorMap[Ty]; 6897330f729Sjoerg } setTypeDescriptorInMap(QualType Ty,llvm::Constant * C)6907330f729Sjoerg void setTypeDescriptorInMap(QualType Ty, llvm::Constant *C) { 6917330f729Sjoerg TypeDescriptorMap[Ty] = C; 6927330f729Sjoerg } 6937330f729Sjoerg getModuleDebugInfo()6947330f729Sjoerg CGDebugInfo *getModuleDebugInfo() { return DebugInfo.get(); } 6957330f729Sjoerg getNoObjCARCExceptionsMetadata()6967330f729Sjoerg llvm::MDNode *getNoObjCARCExceptionsMetadata() { 6977330f729Sjoerg if (!NoObjCARCExceptionsMetadata) 6987330f729Sjoerg NoObjCARCExceptionsMetadata = llvm::MDNode::get(getLLVMContext(), None); 6997330f729Sjoerg return NoObjCARCExceptionsMetadata; 7007330f729Sjoerg } 7017330f729Sjoerg getContext()7027330f729Sjoerg ASTContext &getContext() const { return Context; } getLangOpts()7037330f729Sjoerg const LangOptions &getLangOpts() const { return LangOpts; } getHeaderSearchOpts()7047330f729Sjoerg const HeaderSearchOptions &getHeaderSearchOpts() 7057330f729Sjoerg const { return HeaderSearchOpts; } getPreprocessorOpts()7067330f729Sjoerg const PreprocessorOptions &getPreprocessorOpts() 7077330f729Sjoerg const { return PreprocessorOpts; } getCodeGenOpts()7087330f729Sjoerg const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; } getModule()7097330f729Sjoerg llvm::Module &getModule() const { return TheModule; } getDiags()7107330f729Sjoerg DiagnosticsEngine &getDiags() const { return Diags; } getDataLayout()7117330f729Sjoerg const llvm::DataLayout &getDataLayout() const { 7127330f729Sjoerg return TheModule.getDataLayout(); 7137330f729Sjoerg } getTarget()7147330f729Sjoerg const TargetInfo &getTarget() const { return Target; } getTriple()7157330f729Sjoerg const llvm::Triple &getTriple() const { return Target.getTriple(); } 7167330f729Sjoerg bool supportsCOMDAT() const; 7177330f729Sjoerg void maybeSetTrivialComdat(const Decl &D, llvm::GlobalObject &GO); 7187330f729Sjoerg getCXXABI()7197330f729Sjoerg CGCXXABI &getCXXABI() const { return *ABI; } getLLVMContext()7207330f729Sjoerg llvm::LLVMContext &getLLVMContext() { return VMContext; } 7217330f729Sjoerg shouldUseTBAA()7227330f729Sjoerg bool shouldUseTBAA() const { return TBAA != nullptr; } 7237330f729Sjoerg 7247330f729Sjoerg const TargetCodeGenInfo &getTargetCodeGenInfo(); 7257330f729Sjoerg getTypes()7267330f729Sjoerg CodeGenTypes &getTypes() { return Types; } 7277330f729Sjoerg getVTables()7287330f729Sjoerg CodeGenVTables &getVTables() { return VTables; } 7297330f729Sjoerg getItaniumVTableContext()7307330f729Sjoerg ItaniumVTableContext &getItaniumVTableContext() { 7317330f729Sjoerg return VTables.getItaniumVTableContext(); 7327330f729Sjoerg } 7337330f729Sjoerg getMicrosoftVTableContext()7347330f729Sjoerg MicrosoftVTableContext &getMicrosoftVTableContext() { 7357330f729Sjoerg return VTables.getMicrosoftVTableContext(); 7367330f729Sjoerg } 7377330f729Sjoerg getGlobalCtors()7387330f729Sjoerg CtorList &getGlobalCtors() { return GlobalCtors; } getGlobalDtors()7397330f729Sjoerg CtorList &getGlobalDtors() { return GlobalDtors; } 7407330f729Sjoerg 7417330f729Sjoerg /// getTBAATypeInfo - Get metadata used to describe accesses to objects of 7427330f729Sjoerg /// the given type. 7437330f729Sjoerg llvm::MDNode *getTBAATypeInfo(QualType QTy); 7447330f729Sjoerg 7457330f729Sjoerg /// getTBAAAccessInfo - Get TBAA information that describes an access to 7467330f729Sjoerg /// an object of the given type. 7477330f729Sjoerg TBAAAccessInfo getTBAAAccessInfo(QualType AccessType); 7487330f729Sjoerg 7497330f729Sjoerg /// getTBAAVTablePtrAccessInfo - Get the TBAA information that describes an 7507330f729Sjoerg /// access to a virtual table pointer. 7517330f729Sjoerg TBAAAccessInfo getTBAAVTablePtrAccessInfo(llvm::Type *VTablePtrType); 7527330f729Sjoerg 7537330f729Sjoerg llvm::MDNode *getTBAAStructInfo(QualType QTy); 7547330f729Sjoerg 7557330f729Sjoerg /// getTBAABaseTypeInfo - Get metadata that describes the given base access 7567330f729Sjoerg /// type. Return null if the type is not suitable for use in TBAA access tags. 7577330f729Sjoerg llvm::MDNode *getTBAABaseTypeInfo(QualType QTy); 7587330f729Sjoerg 7597330f729Sjoerg /// getTBAAAccessTagInfo - Get TBAA tag for a given memory access. 7607330f729Sjoerg llvm::MDNode *getTBAAAccessTagInfo(TBAAAccessInfo Info); 7617330f729Sjoerg 7627330f729Sjoerg /// mergeTBAAInfoForCast - Get merged TBAA information for the purposes of 7637330f729Sjoerg /// type casts. 7647330f729Sjoerg TBAAAccessInfo mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo, 7657330f729Sjoerg TBAAAccessInfo TargetInfo); 7667330f729Sjoerg 7677330f729Sjoerg /// mergeTBAAInfoForConditionalOperator - Get merged TBAA information for the 7687330f729Sjoerg /// purposes of conditional operator. 7697330f729Sjoerg TBAAAccessInfo mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA, 7707330f729Sjoerg TBAAAccessInfo InfoB); 7717330f729Sjoerg 7727330f729Sjoerg /// mergeTBAAInfoForMemoryTransfer - Get merged TBAA information for the 7737330f729Sjoerg /// purposes of memory transfer calls. 7747330f729Sjoerg TBAAAccessInfo mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo, 7757330f729Sjoerg TBAAAccessInfo SrcInfo); 7767330f729Sjoerg 7777330f729Sjoerg /// getTBAAInfoForSubobject - Get TBAA information for an access with a given 7787330f729Sjoerg /// base lvalue. getTBAAInfoForSubobject(LValue Base,QualType AccessType)7797330f729Sjoerg TBAAAccessInfo getTBAAInfoForSubobject(LValue Base, QualType AccessType) { 7807330f729Sjoerg if (Base.getTBAAInfo().isMayAlias()) 7817330f729Sjoerg return TBAAAccessInfo::getMayAliasInfo(); 7827330f729Sjoerg return getTBAAAccessInfo(AccessType); 7837330f729Sjoerg } 7847330f729Sjoerg 7857330f729Sjoerg bool isTypeConstant(QualType QTy, bool ExcludeCtorDtor); 7867330f729Sjoerg 7877330f729Sjoerg bool isPaddedAtomicType(QualType type); 7887330f729Sjoerg bool isPaddedAtomicType(const AtomicType *type); 7897330f729Sjoerg 7907330f729Sjoerg /// DecorateInstructionWithTBAA - Decorate the instruction with a TBAA tag. 7917330f729Sjoerg void DecorateInstructionWithTBAA(llvm::Instruction *Inst, 7927330f729Sjoerg TBAAAccessInfo TBAAInfo); 7937330f729Sjoerg 7947330f729Sjoerg /// Adds !invariant.barrier !tag to instruction 7957330f729Sjoerg void DecorateInstructionWithInvariantGroup(llvm::Instruction *I, 7967330f729Sjoerg const CXXRecordDecl *RD); 7977330f729Sjoerg 7987330f729Sjoerg /// Emit the given number of characters as a value of type size_t. 7997330f729Sjoerg llvm::ConstantInt *getSize(CharUnits numChars); 8007330f729Sjoerg 8017330f729Sjoerg /// Set the visibility for the given LLVM GlobalValue. 8027330f729Sjoerg void setGlobalVisibility(llvm::GlobalValue *GV, const NamedDecl *D) const; 8037330f729Sjoerg 8047330f729Sjoerg void setDSOLocal(llvm::GlobalValue *GV) const; 8057330f729Sjoerg 8067330f729Sjoerg void setDLLImportDLLExport(llvm::GlobalValue *GV, GlobalDecl D) const; 8077330f729Sjoerg void setDLLImportDLLExport(llvm::GlobalValue *GV, const NamedDecl *D) const; 8087330f729Sjoerg /// Set visibility, dllimport/dllexport and dso_local. 8097330f729Sjoerg /// This must be called after dllimport/dllexport is set. 8107330f729Sjoerg void setGVProperties(llvm::GlobalValue *GV, GlobalDecl GD) const; 8117330f729Sjoerg void setGVProperties(llvm::GlobalValue *GV, const NamedDecl *D) const; 8127330f729Sjoerg 8137330f729Sjoerg void setGVPropertiesAux(llvm::GlobalValue *GV, const NamedDecl *D) const; 8147330f729Sjoerg 8157330f729Sjoerg /// Set the TLS mode for the given LLVM GlobalValue for the thread-local 8167330f729Sjoerg /// variable declaration D. 8177330f729Sjoerg void setTLSMode(llvm::GlobalValue *GV, const VarDecl &D) const; 8187330f729Sjoerg 819*e038c9c4Sjoerg /// Get LLVM TLS mode from CodeGenOptions. 820*e038c9c4Sjoerg llvm::GlobalVariable::ThreadLocalMode GetDefaultLLVMTLSModel() const; 821*e038c9c4Sjoerg GetLLVMVisibility(Visibility V)8227330f729Sjoerg static llvm::GlobalValue::VisibilityTypes GetLLVMVisibility(Visibility V) { 8237330f729Sjoerg switch (V) { 8247330f729Sjoerg case DefaultVisibility: return llvm::GlobalValue::DefaultVisibility; 8257330f729Sjoerg case HiddenVisibility: return llvm::GlobalValue::HiddenVisibility; 8267330f729Sjoerg case ProtectedVisibility: return llvm::GlobalValue::ProtectedVisibility; 8277330f729Sjoerg } 8287330f729Sjoerg llvm_unreachable("unknown visibility!"); 8297330f729Sjoerg } 8307330f729Sjoerg 8317330f729Sjoerg llvm::Constant *GetAddrOfGlobal(GlobalDecl GD, 8327330f729Sjoerg ForDefinition_t IsForDefinition 8337330f729Sjoerg = NotForDefinition); 8347330f729Sjoerg 8357330f729Sjoerg /// Will return a global variable of the given type. If a variable with a 8367330f729Sjoerg /// different type already exists then a new variable with the right type 8377330f729Sjoerg /// will be created and all uses of the old variable will be replaced with a 8387330f729Sjoerg /// bitcast to the new variable. 8397330f729Sjoerg llvm::GlobalVariable * 8407330f729Sjoerg CreateOrReplaceCXXRuntimeVariable(StringRef Name, llvm::Type *Ty, 8417330f729Sjoerg llvm::GlobalValue::LinkageTypes Linkage, 8427330f729Sjoerg unsigned Alignment); 8437330f729Sjoerg 844*e038c9c4Sjoerg llvm::Function *CreateGlobalInitOrCleanUpFunction( 845*e038c9c4Sjoerg llvm::FunctionType *ty, const Twine &name, const CGFunctionInfo &FI, 846*e038c9c4Sjoerg SourceLocation Loc = SourceLocation(), bool TLS = false); 8477330f729Sjoerg 8487330f729Sjoerg /// Return the AST address space of the underlying global variable for D, as 8497330f729Sjoerg /// determined by its declaration. Normally this is the same as the address 8507330f729Sjoerg /// space of D's type, but in CUDA, address spaces are associated with 8517330f729Sjoerg /// declarations, not types. If D is nullptr, return the default address 8527330f729Sjoerg /// space for global variable. 8537330f729Sjoerg /// 8547330f729Sjoerg /// For languages without explicit address spaces, if D has default address 8557330f729Sjoerg /// space, target-specific global or constant address space may be returned. 8567330f729Sjoerg LangAS GetGlobalVarAddressSpace(const VarDecl *D); 8577330f729Sjoerg 858*e038c9c4Sjoerg /// Return the AST address space of constant literal, which is used to emit 859*e038c9c4Sjoerg /// the constant literal as global variable in LLVM IR. 860*e038c9c4Sjoerg /// Note: This is not necessarily the address space of the constant literal 861*e038c9c4Sjoerg /// in AST. For address space agnostic language, e.g. C++, constant literal 862*e038c9c4Sjoerg /// in AST is always in default address space. 863*e038c9c4Sjoerg LangAS GetGlobalConstantAddressSpace() const; 864*e038c9c4Sjoerg 8657330f729Sjoerg /// Return the llvm::Constant for the address of the given global variable. 8667330f729Sjoerg /// If Ty is non-null and if the global doesn't exist, then it will be created 8677330f729Sjoerg /// with the specified type instead of whatever the normal requested type 8687330f729Sjoerg /// would be. If IsForDefinition is true, it is guaranteed that an actual 8697330f729Sjoerg /// global with type Ty will be returned, not conversion of a variable with 8707330f729Sjoerg /// the same mangled name but some other type. 8717330f729Sjoerg llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D, 8727330f729Sjoerg llvm::Type *Ty = nullptr, 8737330f729Sjoerg ForDefinition_t IsForDefinition 8747330f729Sjoerg = NotForDefinition); 8757330f729Sjoerg 8767330f729Sjoerg /// Return the address of the given function. If Ty is non-null, then this 8777330f729Sjoerg /// function will use the specified type if it has to create it. 8787330f729Sjoerg llvm::Constant *GetAddrOfFunction(GlobalDecl GD, llvm::Type *Ty = nullptr, 8797330f729Sjoerg bool ForVTable = false, 8807330f729Sjoerg bool DontDefer = false, 8817330f729Sjoerg ForDefinition_t IsForDefinition 8827330f729Sjoerg = NotForDefinition); 8837330f729Sjoerg 8847330f729Sjoerg /// Get the address of the RTTI descriptor for the given type. 8857330f729Sjoerg llvm::Constant *GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH = false); 8867330f729Sjoerg 887*e038c9c4Sjoerg /// Get the address of a GUID. 888*e038c9c4Sjoerg ConstantAddress GetAddrOfMSGuidDecl(const MSGuidDecl *GD); 889*e038c9c4Sjoerg 890*e038c9c4Sjoerg /// Get the address of a template parameter object. 891*e038c9c4Sjoerg ConstantAddress 892*e038c9c4Sjoerg GetAddrOfTemplateParamObject(const TemplateParamObjectDecl *TPO); 8937330f729Sjoerg 8947330f729Sjoerg /// Get the address of the thunk for the given global decl. 8957330f729Sjoerg llvm::Constant *GetAddrOfThunk(StringRef Name, llvm::Type *FnTy, 8967330f729Sjoerg GlobalDecl GD); 8977330f729Sjoerg 8987330f729Sjoerg /// Get a reference to the target of VD. 8997330f729Sjoerg ConstantAddress GetWeakRefReference(const ValueDecl *VD); 9007330f729Sjoerg 9017330f729Sjoerg /// Returns the assumed alignment of an opaque pointer to the given class. 9027330f729Sjoerg CharUnits getClassPointerAlignment(const CXXRecordDecl *CD); 9037330f729Sjoerg 904*e038c9c4Sjoerg /// Returns the minimum object size for an object of the given class type 905*e038c9c4Sjoerg /// (or a class derived from it). 906*e038c9c4Sjoerg CharUnits getMinimumClassObjectSize(const CXXRecordDecl *CD); 907*e038c9c4Sjoerg 908*e038c9c4Sjoerg /// Returns the minimum object size for an object of the given type. getMinimumObjectSize(QualType Ty)909*e038c9c4Sjoerg CharUnits getMinimumObjectSize(QualType Ty) { 910*e038c9c4Sjoerg if (CXXRecordDecl *RD = Ty->getAsCXXRecordDecl()) 911*e038c9c4Sjoerg return getMinimumClassObjectSize(RD); 912*e038c9c4Sjoerg return getContext().getTypeSizeInChars(Ty); 913*e038c9c4Sjoerg } 914*e038c9c4Sjoerg 9157330f729Sjoerg /// Returns the assumed alignment of a virtual base of a class. 9167330f729Sjoerg CharUnits getVBaseAlignment(CharUnits DerivedAlign, 9177330f729Sjoerg const CXXRecordDecl *Derived, 9187330f729Sjoerg const CXXRecordDecl *VBase); 9197330f729Sjoerg 9207330f729Sjoerg /// Given a class pointer with an actual known alignment, and the 9217330f729Sjoerg /// expected alignment of an object at a dynamic offset w.r.t that 9227330f729Sjoerg /// pointer, return the alignment to assume at the offset. 9237330f729Sjoerg CharUnits getDynamicOffsetAlignment(CharUnits ActualAlign, 9247330f729Sjoerg const CXXRecordDecl *Class, 9257330f729Sjoerg CharUnits ExpectedTargetAlign); 9267330f729Sjoerg 9277330f729Sjoerg CharUnits 9287330f729Sjoerg computeNonVirtualBaseClassOffset(const CXXRecordDecl *DerivedClass, 9297330f729Sjoerg CastExpr::path_const_iterator Start, 9307330f729Sjoerg CastExpr::path_const_iterator End); 9317330f729Sjoerg 9327330f729Sjoerg /// Returns the offset from a derived class to a class. Returns null if the 9337330f729Sjoerg /// offset is 0. 9347330f729Sjoerg llvm::Constant * 9357330f729Sjoerg GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl, 9367330f729Sjoerg CastExpr::path_const_iterator PathBegin, 9377330f729Sjoerg CastExpr::path_const_iterator PathEnd); 9387330f729Sjoerg 9397330f729Sjoerg llvm::FoldingSet<BlockByrefHelpers> ByrefHelpersCache; 9407330f729Sjoerg 9417330f729Sjoerg /// Fetches the global unique block count. getUniqueBlockCount()9427330f729Sjoerg int getUniqueBlockCount() { return ++Block.GlobalUniqueCount; } 9437330f729Sjoerg 9447330f729Sjoerg /// Fetches the type of a generic block descriptor. 9457330f729Sjoerg llvm::Type *getBlockDescriptorType(); 9467330f729Sjoerg 9477330f729Sjoerg /// The type of a generic block literal. 9487330f729Sjoerg llvm::Type *getGenericBlockLiteralType(); 9497330f729Sjoerg 9507330f729Sjoerg /// Gets the address of a block which requires no captures. 9517330f729Sjoerg llvm::Constant *GetAddrOfGlobalBlock(const BlockExpr *BE, StringRef Name); 9527330f729Sjoerg 9537330f729Sjoerg /// Returns the address of a block which requires no caputres, or null if 9547330f729Sjoerg /// we've yet to emit the block for BE. getAddrOfGlobalBlockIfEmitted(const BlockExpr * BE)9557330f729Sjoerg llvm::Constant *getAddrOfGlobalBlockIfEmitted(const BlockExpr *BE) { 9567330f729Sjoerg return EmittedGlobalBlocks.lookup(BE); 9577330f729Sjoerg } 9587330f729Sjoerg 9597330f729Sjoerg /// Notes that BE's global block is available via Addr. Asserts that BE 9607330f729Sjoerg /// isn't already emitted. 9617330f729Sjoerg void setAddrOfGlobalBlock(const BlockExpr *BE, llvm::Constant *Addr); 9627330f729Sjoerg 9637330f729Sjoerg /// Return a pointer to a constant CFString object for the given string. 9647330f729Sjoerg ConstantAddress GetAddrOfConstantCFString(const StringLiteral *Literal); 9657330f729Sjoerg 9667330f729Sjoerg /// Return a pointer to a constant NSString object for the given string. Or a 9677330f729Sjoerg /// user defined String object as defined via 9687330f729Sjoerg /// -fconstant-string-class=class_name option. 9697330f729Sjoerg ConstantAddress GetAddrOfConstantString(const StringLiteral *Literal); 9707330f729Sjoerg 9717330f729Sjoerg /// Return a constant array for the given string. 9727330f729Sjoerg llvm::Constant *GetConstantArrayFromStringLiteral(const StringLiteral *E); 9737330f729Sjoerg 9747330f729Sjoerg /// Return a pointer to a constant array for the given string literal. 9757330f729Sjoerg ConstantAddress 9767330f729Sjoerg GetAddrOfConstantStringFromLiteral(const StringLiteral *S, 9777330f729Sjoerg StringRef Name = ".str"); 9787330f729Sjoerg 9797330f729Sjoerg /// Return a pointer to a constant array for the given ObjCEncodeExpr node. 9807330f729Sjoerg ConstantAddress 9817330f729Sjoerg GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *); 9827330f729Sjoerg 9837330f729Sjoerg /// Returns a pointer to a character array containing the literal and a 9847330f729Sjoerg /// terminating '\0' character. The result has pointer to array type. 9857330f729Sjoerg /// 9867330f729Sjoerg /// \param GlobalName If provided, the name to use for the global (if one is 9877330f729Sjoerg /// created). 9887330f729Sjoerg ConstantAddress 9897330f729Sjoerg GetAddrOfConstantCString(const std::string &Str, 9907330f729Sjoerg const char *GlobalName = nullptr); 9917330f729Sjoerg 9927330f729Sjoerg /// Returns a pointer to a constant global variable for the given file-scope 9937330f729Sjoerg /// compound literal expression. 9947330f729Sjoerg ConstantAddress GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr*E); 9957330f729Sjoerg 9967330f729Sjoerg /// If it's been emitted already, returns the GlobalVariable corresponding to 9977330f729Sjoerg /// a compound literal. Otherwise, returns null. 9987330f729Sjoerg llvm::GlobalVariable * 9997330f729Sjoerg getAddrOfConstantCompoundLiteralIfEmitted(const CompoundLiteralExpr *E); 10007330f729Sjoerg 10017330f729Sjoerg /// Notes that CLE's GlobalVariable is GV. Asserts that CLE isn't already 10027330f729Sjoerg /// emitted. 10037330f729Sjoerg void setAddrOfConstantCompoundLiteral(const CompoundLiteralExpr *CLE, 10047330f729Sjoerg llvm::GlobalVariable *GV); 10057330f729Sjoerg 10067330f729Sjoerg /// Returns a pointer to a global variable representing a temporary 10077330f729Sjoerg /// with static or thread storage duration. 10087330f729Sjoerg ConstantAddress GetAddrOfGlobalTemporary(const MaterializeTemporaryExpr *E, 10097330f729Sjoerg const Expr *Inner); 10107330f729Sjoerg 10117330f729Sjoerg /// Retrieve the record type that describes the state of an 10127330f729Sjoerg /// Objective-C fast enumeration loop (for..in). 10137330f729Sjoerg QualType getObjCFastEnumerationStateType(); 10147330f729Sjoerg 10157330f729Sjoerg // Produce code for this constructor/destructor. This method doesn't try 10167330f729Sjoerg // to apply any ABI rules about which other constructors/destructors 10177330f729Sjoerg // are needed or if they are alias to each other. 10187330f729Sjoerg llvm::Function *codegenCXXStructor(GlobalDecl GD); 10197330f729Sjoerg 10207330f729Sjoerg /// Return the address of the constructor/destructor of the given type. 10217330f729Sjoerg llvm::Constant * 10227330f729Sjoerg getAddrOfCXXStructor(GlobalDecl GD, const CGFunctionInfo *FnInfo = nullptr, 10237330f729Sjoerg llvm::FunctionType *FnType = nullptr, 10247330f729Sjoerg bool DontDefer = false, 10257330f729Sjoerg ForDefinition_t IsForDefinition = NotForDefinition) { 10267330f729Sjoerg return cast<llvm::Constant>(getAddrAndTypeOfCXXStructor(GD, FnInfo, FnType, 10277330f729Sjoerg DontDefer, 10287330f729Sjoerg IsForDefinition) 10297330f729Sjoerg .getCallee()); 10307330f729Sjoerg } 10317330f729Sjoerg 10327330f729Sjoerg llvm::FunctionCallee getAddrAndTypeOfCXXStructor( 10337330f729Sjoerg GlobalDecl GD, const CGFunctionInfo *FnInfo = nullptr, 10347330f729Sjoerg llvm::FunctionType *FnType = nullptr, bool DontDefer = false, 10357330f729Sjoerg ForDefinition_t IsForDefinition = NotForDefinition); 10367330f729Sjoerg 10377330f729Sjoerg /// Given a builtin id for a function like "__builtin_fabsf", return a 10387330f729Sjoerg /// Function* for "fabsf". 10397330f729Sjoerg llvm::Constant *getBuiltinLibFunction(const FunctionDecl *FD, 10407330f729Sjoerg unsigned BuiltinID); 10417330f729Sjoerg 10427330f729Sjoerg llvm::Function *getIntrinsic(unsigned IID, ArrayRef<llvm::Type*> Tys = None); 10437330f729Sjoerg 10447330f729Sjoerg /// Emit code for a single top level declaration. 10457330f729Sjoerg void EmitTopLevelDecl(Decl *D); 10467330f729Sjoerg 10477330f729Sjoerg /// Stored a deferred empty coverage mapping for an unused 10487330f729Sjoerg /// and thus uninstrumented top level declaration. 10497330f729Sjoerg void AddDeferredUnusedCoverageMapping(Decl *D); 10507330f729Sjoerg 10517330f729Sjoerg /// Remove the deferred empty coverage mapping as this 10527330f729Sjoerg /// declaration is actually instrumented. 10537330f729Sjoerg void ClearUnusedCoverageMapping(const Decl *D); 10547330f729Sjoerg 10557330f729Sjoerg /// Emit all the deferred coverage mappings 10567330f729Sjoerg /// for the uninstrumented functions. 10577330f729Sjoerg void EmitDeferredUnusedCoverageMappings(); 10587330f729Sjoerg 1059*e038c9c4Sjoerg /// Emit an alias for "main" if it has no arguments (needed for wasm). 1060*e038c9c4Sjoerg void EmitMainVoidAlias(); 1061*e038c9c4Sjoerg 10627330f729Sjoerg /// Tell the consumer that this variable has been instantiated. 10637330f729Sjoerg void HandleCXXStaticMemberVarInstantiation(VarDecl *VD); 10647330f729Sjoerg 10657330f729Sjoerg /// If the declaration has internal linkage but is inside an 10667330f729Sjoerg /// extern "C" linkage specification, prepare to emit an alias for it 10677330f729Sjoerg /// to the expected name. 10687330f729Sjoerg template<typename SomeDecl> 10697330f729Sjoerg void MaybeHandleStaticInExternC(const SomeDecl *D, llvm::GlobalValue *GV); 10707330f729Sjoerg 10717330f729Sjoerg /// Add a global to a list to be added to the llvm.used metadata. 10727330f729Sjoerg void addUsedGlobal(llvm::GlobalValue *GV); 10737330f729Sjoerg 10747330f729Sjoerg /// Add a global to a list to be added to the llvm.compiler.used metadata. 10757330f729Sjoerg void addCompilerUsedGlobal(llvm::GlobalValue *GV); 10767330f729Sjoerg 1077*e038c9c4Sjoerg /// Add a global to a list to be added to the llvm.compiler.used metadata. 1078*e038c9c4Sjoerg void addUsedOrCompilerUsedGlobal(llvm::GlobalValue *GV); 1079*e038c9c4Sjoerg 10807330f729Sjoerg /// Add a destructor and object to add to the C++ global destructor function. AddCXXDtorEntry(llvm::FunctionCallee DtorFn,llvm::Constant * Object)10817330f729Sjoerg void AddCXXDtorEntry(llvm::FunctionCallee DtorFn, llvm::Constant *Object) { 1082*e038c9c4Sjoerg CXXGlobalDtorsOrStermFinalizers.emplace_back(DtorFn.getFunctionType(), 1083*e038c9c4Sjoerg DtorFn.getCallee(), Object); 1084*e038c9c4Sjoerg } 1085*e038c9c4Sjoerg 1086*e038c9c4Sjoerg /// Add an sterm finalizer to the C++ global cleanup function. AddCXXStermFinalizerEntry(llvm::FunctionCallee DtorFn)1087*e038c9c4Sjoerg void AddCXXStermFinalizerEntry(llvm::FunctionCallee DtorFn) { 1088*e038c9c4Sjoerg CXXGlobalDtorsOrStermFinalizers.emplace_back(DtorFn.getFunctionType(), 1089*e038c9c4Sjoerg DtorFn.getCallee(), nullptr); 1090*e038c9c4Sjoerg } 1091*e038c9c4Sjoerg 1092*e038c9c4Sjoerg /// Add an sterm finalizer to its own llvm.global_dtors entry. AddCXXStermFinalizerToGlobalDtor(llvm::Function * StermFinalizer,int Priority)1093*e038c9c4Sjoerg void AddCXXStermFinalizerToGlobalDtor(llvm::Function *StermFinalizer, 1094*e038c9c4Sjoerg int Priority) { 1095*e038c9c4Sjoerg AddGlobalDtor(StermFinalizer, Priority); 1096*e038c9c4Sjoerg } 1097*e038c9c4Sjoerg AddCXXPrioritizedStermFinalizerEntry(llvm::Function * StermFinalizer,int Priority)1098*e038c9c4Sjoerg void AddCXXPrioritizedStermFinalizerEntry(llvm::Function *StermFinalizer, 1099*e038c9c4Sjoerg int Priority) { 1100*e038c9c4Sjoerg OrderGlobalInitsOrStermFinalizers Key(Priority, 1101*e038c9c4Sjoerg PrioritizedCXXStermFinalizers.size()); 1102*e038c9c4Sjoerg PrioritizedCXXStermFinalizers.push_back( 1103*e038c9c4Sjoerg std::make_pair(Key, StermFinalizer)); 11047330f729Sjoerg } 11057330f729Sjoerg 11067330f729Sjoerg /// Create or return a runtime function declaration with the specified type 11077330f729Sjoerg /// and name. If \p AssumeConvergent is true, the call will have the 11087330f729Sjoerg /// convergent attribute added. 11097330f729Sjoerg llvm::FunctionCallee 11107330f729Sjoerg CreateRuntimeFunction(llvm::FunctionType *Ty, StringRef Name, 11117330f729Sjoerg llvm::AttributeList ExtraAttrs = llvm::AttributeList(), 11127330f729Sjoerg bool Local = false, bool AssumeConvergent = false); 11137330f729Sjoerg 11147330f729Sjoerg /// Create a new runtime global variable with the specified type and name. 11157330f729Sjoerg llvm::Constant *CreateRuntimeVariable(llvm::Type *Ty, 11167330f729Sjoerg StringRef Name); 11177330f729Sjoerg 11187330f729Sjoerg ///@name Custom Blocks Runtime Interfaces 11197330f729Sjoerg ///@{ 11207330f729Sjoerg 11217330f729Sjoerg llvm::Constant *getNSConcreteGlobalBlock(); 11227330f729Sjoerg llvm::Constant *getNSConcreteStackBlock(); 11237330f729Sjoerg llvm::FunctionCallee getBlockObjectAssign(); 11247330f729Sjoerg llvm::FunctionCallee getBlockObjectDispose(); 11257330f729Sjoerg 11267330f729Sjoerg ///@} 11277330f729Sjoerg 11287330f729Sjoerg llvm::Function *getLLVMLifetimeStartFn(); 11297330f729Sjoerg llvm::Function *getLLVMLifetimeEndFn(); 11307330f729Sjoerg 11317330f729Sjoerg // Make sure that this type is translated. 11327330f729Sjoerg void UpdateCompletedType(const TagDecl *TD); 11337330f729Sjoerg 11347330f729Sjoerg llvm::Constant *getMemberPointerConstant(const UnaryOperator *e); 11357330f729Sjoerg 11367330f729Sjoerg /// Emit type info if type of an expression is a variably modified 11377330f729Sjoerg /// type. Also emit proper debug info for cast types. 11387330f729Sjoerg void EmitExplicitCastExprType(const ExplicitCastExpr *E, 11397330f729Sjoerg CodeGenFunction *CGF = nullptr); 11407330f729Sjoerg 11417330f729Sjoerg /// Return the result of value-initializing the given type, i.e. a null 11427330f729Sjoerg /// expression of the given type. This is usually, but not always, an LLVM 11437330f729Sjoerg /// null constant. 11447330f729Sjoerg llvm::Constant *EmitNullConstant(QualType T); 11457330f729Sjoerg 11467330f729Sjoerg /// Return a null constant appropriate for zero-initializing a base class with 11477330f729Sjoerg /// the given type. This is usually, but not always, an LLVM null constant. 11487330f729Sjoerg llvm::Constant *EmitNullConstantForBase(const CXXRecordDecl *Record); 11497330f729Sjoerg 11507330f729Sjoerg /// Emit a general error that something can't be done. 11517330f729Sjoerg void Error(SourceLocation loc, StringRef error); 11527330f729Sjoerg 11537330f729Sjoerg /// Print out an error that codegen doesn't support the specified stmt yet. 11547330f729Sjoerg void ErrorUnsupported(const Stmt *S, const char *Type); 11557330f729Sjoerg 11567330f729Sjoerg /// Print out an error that codegen doesn't support the specified decl yet. 11577330f729Sjoerg void ErrorUnsupported(const Decl *D, const char *Type); 11587330f729Sjoerg 11597330f729Sjoerg /// Set the attributes on the LLVM function for the given decl and function 11607330f729Sjoerg /// info. This applies attributes necessary for handling the ABI as well as 11617330f729Sjoerg /// user specified attributes like section. 11627330f729Sjoerg void SetInternalFunctionAttributes(GlobalDecl GD, llvm::Function *F, 11637330f729Sjoerg const CGFunctionInfo &FI); 11647330f729Sjoerg 11657330f729Sjoerg /// Set the LLVM function attributes (sext, zext, etc). 11667330f729Sjoerg void SetLLVMFunctionAttributes(GlobalDecl GD, const CGFunctionInfo &Info, 1167*e038c9c4Sjoerg llvm::Function *F, bool IsThunk); 11687330f729Sjoerg 11697330f729Sjoerg /// Set the LLVM function attributes which only apply to a function 11707330f729Sjoerg /// definition. 11717330f729Sjoerg void SetLLVMFunctionAttributesForDefinition(const Decl *D, llvm::Function *F); 11727330f729Sjoerg 1173*e038c9c4Sjoerg /// Set the LLVM function attributes that represent floating point 1174*e038c9c4Sjoerg /// environment. 1175*e038c9c4Sjoerg void setLLVMFunctionFEnvAttributes(const FunctionDecl *D, llvm::Function *F); 1176*e038c9c4Sjoerg 11777330f729Sjoerg /// Return true iff the given type uses 'sret' when used as a return type. 11787330f729Sjoerg bool ReturnTypeUsesSRet(const CGFunctionInfo &FI); 11797330f729Sjoerg 11807330f729Sjoerg /// Return true iff the given type uses an argument slot when 'sret' is used 11817330f729Sjoerg /// as a return type. 11827330f729Sjoerg bool ReturnSlotInterferesWithArgs(const CGFunctionInfo &FI); 11837330f729Sjoerg 11847330f729Sjoerg /// Return true iff the given type uses 'fpret' when used as a return type. 11857330f729Sjoerg bool ReturnTypeUsesFPRet(QualType ResultType); 11867330f729Sjoerg 11877330f729Sjoerg /// Return true iff the given type uses 'fp2ret' when used as a return type. 11887330f729Sjoerg bool ReturnTypeUsesFP2Ret(QualType ResultType); 11897330f729Sjoerg 11907330f729Sjoerg /// Get the LLVM attributes and calling convention to use for a particular 11917330f729Sjoerg /// function type. 11927330f729Sjoerg /// 11937330f729Sjoerg /// \param Name - The function name. 11947330f729Sjoerg /// \param Info - The function type information. 11957330f729Sjoerg /// \param CalleeInfo - The callee information these attributes are being 11967330f729Sjoerg /// constructed for. If valid, the attributes applied to this decl may 11977330f729Sjoerg /// contribute to the function attributes and calling convention. 11987330f729Sjoerg /// \param Attrs [out] - On return, the attribute list to use. 11997330f729Sjoerg /// \param CallingConv [out] - On return, the LLVM calling convention to use. 12007330f729Sjoerg void ConstructAttributeList(StringRef Name, const CGFunctionInfo &Info, 12017330f729Sjoerg CGCalleeInfo CalleeInfo, 12027330f729Sjoerg llvm::AttributeList &Attrs, unsigned &CallingConv, 1203*e038c9c4Sjoerg bool AttrOnCallSite, bool IsThunk); 12047330f729Sjoerg 12057330f729Sjoerg /// Adds attributes to F according to our CodeGenOptions and LangOptions, as 12067330f729Sjoerg /// though we had emitted it ourselves. We remove any attributes on F that 12077330f729Sjoerg /// conflict with the attributes we add here. 12087330f729Sjoerg /// 12097330f729Sjoerg /// This is useful for adding attrs to bitcode modules that you want to link 12107330f729Sjoerg /// with but don't control, such as CUDA's libdevice. When linking with such 12117330f729Sjoerg /// a bitcode library, you might want to set e.g. its functions' 12127330f729Sjoerg /// "unsafe-fp-math" attribute to match the attr of the functions you're 12137330f729Sjoerg /// codegen'ing. Otherwise, LLVM will interpret the bitcode module's lack of 12147330f729Sjoerg /// unsafe-fp-math attrs as tantamount to unsafe-fp-math=false, and then LLVM 12157330f729Sjoerg /// will propagate unsafe-fp-math=false up to every transitive caller of a 12167330f729Sjoerg /// function in the bitcode library! 12177330f729Sjoerg /// 12187330f729Sjoerg /// With the exception of fast-math attrs, this will only make the attributes 12197330f729Sjoerg /// on the function more conservative. But it's unsafe to call this on a 12207330f729Sjoerg /// function which relies on particular fast-math attributes for correctness. 12217330f729Sjoerg /// It's up to you to ensure that this is safe. 1222*e038c9c4Sjoerg void addDefaultFunctionDefinitionAttributes(llvm::Function &F); 12237330f729Sjoerg 1224*e038c9c4Sjoerg /// Like the overload taking a `Function &`, but intended specifically 1225*e038c9c4Sjoerg /// for frontends that want to build on Clang's target-configuration logic. 1226*e038c9c4Sjoerg void addDefaultFunctionDefinitionAttributes(llvm::AttrBuilder &attrs); 12277330f729Sjoerg 12287330f729Sjoerg StringRef getMangledName(GlobalDecl GD); 12297330f729Sjoerg StringRef getBlockMangledName(GlobalDecl GD, const BlockDecl *BD); 12307330f729Sjoerg 12317330f729Sjoerg void EmitTentativeDefinition(const VarDecl *D); 12327330f729Sjoerg 1233*e038c9c4Sjoerg void EmitExternalDeclaration(const VarDecl *D); 1234*e038c9c4Sjoerg 12357330f729Sjoerg void EmitVTable(CXXRecordDecl *Class); 12367330f729Sjoerg 12377330f729Sjoerg void RefreshTypeCacheForClass(const CXXRecordDecl *Class); 12387330f729Sjoerg 12397330f729Sjoerg /// Appends Opts to the "llvm.linker.options" metadata value. 12407330f729Sjoerg void AppendLinkerOptions(StringRef Opts); 12417330f729Sjoerg 12427330f729Sjoerg /// Appends a detect mismatch command to the linker options. 12437330f729Sjoerg void AddDetectMismatch(StringRef Name, StringRef Value); 12447330f729Sjoerg 12457330f729Sjoerg /// Appends a dependent lib to the appropriate metadata value. 12467330f729Sjoerg void AddDependentLib(StringRef Lib); 12477330f729Sjoerg 12487330f729Sjoerg 12497330f729Sjoerg llvm::GlobalVariable::LinkageTypes getFunctionLinkage(GlobalDecl GD); 12507330f729Sjoerg setFunctionLinkage(GlobalDecl GD,llvm::Function * F)12517330f729Sjoerg void setFunctionLinkage(GlobalDecl GD, llvm::Function *F) { 12527330f729Sjoerg F->setLinkage(getFunctionLinkage(GD)); 12537330f729Sjoerg } 12547330f729Sjoerg 12557330f729Sjoerg /// Return the appropriate linkage for the vtable, VTT, and type information 12567330f729Sjoerg /// of the given class. 12577330f729Sjoerg llvm::GlobalVariable::LinkageTypes getVTableLinkage(const CXXRecordDecl *RD); 12587330f729Sjoerg 12597330f729Sjoerg /// Return the store size, in character units, of the given LLVM type. 12607330f729Sjoerg CharUnits GetTargetTypeStoreSize(llvm::Type *Ty) const; 12617330f729Sjoerg 12627330f729Sjoerg /// Returns LLVM linkage for a declarator. 12637330f729Sjoerg llvm::GlobalValue::LinkageTypes 12647330f729Sjoerg getLLVMLinkageForDeclarator(const DeclaratorDecl *D, GVALinkage Linkage, 12657330f729Sjoerg bool IsConstantVariable); 12667330f729Sjoerg 12677330f729Sjoerg /// Returns LLVM linkage for a declarator. 12687330f729Sjoerg llvm::GlobalValue::LinkageTypes 12697330f729Sjoerg getLLVMLinkageVarDefinition(const VarDecl *VD, bool IsConstant); 12707330f729Sjoerg 12717330f729Sjoerg /// Emit all the global annotations. 12727330f729Sjoerg void EmitGlobalAnnotations(); 12737330f729Sjoerg 12747330f729Sjoerg /// Emit an annotation string. 12757330f729Sjoerg llvm::Constant *EmitAnnotationString(StringRef Str); 12767330f729Sjoerg 12777330f729Sjoerg /// Emit the annotation's translation unit. 12787330f729Sjoerg llvm::Constant *EmitAnnotationUnit(SourceLocation Loc); 12797330f729Sjoerg 12807330f729Sjoerg /// Emit the annotation line number. 12817330f729Sjoerg llvm::Constant *EmitAnnotationLineNo(SourceLocation L); 12827330f729Sjoerg 1283*e038c9c4Sjoerg /// Emit additional args of the annotation. 1284*e038c9c4Sjoerg llvm::Constant *EmitAnnotationArgs(const AnnotateAttr *Attr); 1285*e038c9c4Sjoerg 12867330f729Sjoerg /// Generate the llvm::ConstantStruct which contains the annotation 12877330f729Sjoerg /// information for a given GlobalValue. The annotation struct is 12887330f729Sjoerg /// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the 12897330f729Sjoerg /// GlobalValue being annotated. The second field is the constant string 12907330f729Sjoerg /// created from the AnnotateAttr's annotation. The third field is a constant 12917330f729Sjoerg /// string containing the name of the translation unit. The fourth field is 12927330f729Sjoerg /// the line number in the file of the annotated value declaration. 12937330f729Sjoerg llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV, 12947330f729Sjoerg const AnnotateAttr *AA, 12957330f729Sjoerg SourceLocation L); 12967330f729Sjoerg 12977330f729Sjoerg /// Add global annotations that are set on D, for the global GV. Those 12987330f729Sjoerg /// annotations are emitted during finalization of the LLVM code. 12997330f729Sjoerg void AddGlobalAnnotations(const ValueDecl *D, llvm::GlobalValue *GV); 13007330f729Sjoerg 1301*e038c9c4Sjoerg bool isInNoSanitizeList(SanitizerMask Kind, llvm::Function *Fn, 13027330f729Sjoerg SourceLocation Loc) const; 13037330f729Sjoerg 1304*e038c9c4Sjoerg bool isInNoSanitizeList(llvm::GlobalVariable *GV, SourceLocation Loc, 1305*e038c9c4Sjoerg QualType Ty, StringRef Category = StringRef()) const; 13067330f729Sjoerg 13077330f729Sjoerg /// Imbue XRay attributes to a function, applying the always/never attribute 13087330f729Sjoerg /// lists in the process. Returns true if we did imbue attributes this way, 13097330f729Sjoerg /// false otherwise. 13107330f729Sjoerg bool imbueXRayAttrs(llvm::Function *Fn, SourceLocation Loc, 13117330f729Sjoerg StringRef Category = StringRef()) const; 13127330f729Sjoerg 1313*e038c9c4Sjoerg /// Returns true if function at the given location should be excluded from 1314*e038c9c4Sjoerg /// profile instrumentation. 1315*e038c9c4Sjoerg bool isProfileInstrExcluded(llvm::Function *Fn, SourceLocation Loc) const; 1316*e038c9c4Sjoerg getSanitizerMetadata()13177330f729Sjoerg SanitizerMetadata *getSanitizerMetadata() { 13187330f729Sjoerg return SanitizerMD.get(); 13197330f729Sjoerg } 13207330f729Sjoerg addDeferredVTable(const CXXRecordDecl * RD)13217330f729Sjoerg void addDeferredVTable(const CXXRecordDecl *RD) { 13227330f729Sjoerg DeferredVTables.push_back(RD); 13237330f729Sjoerg } 13247330f729Sjoerg 13257330f729Sjoerg /// Emit code for a single global function or var decl. Forward declarations 13267330f729Sjoerg /// are emitted lazily. 13277330f729Sjoerg void EmitGlobal(GlobalDecl D); 13287330f729Sjoerg 13297330f729Sjoerg bool TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D); 13307330f729Sjoerg 13317330f729Sjoerg llvm::GlobalValue *GetGlobalValue(StringRef Ref); 13327330f729Sjoerg 13337330f729Sjoerg /// Set attributes which are common to any form of a global definition (alias, 13347330f729Sjoerg /// Objective-C method, function, global variable). 13357330f729Sjoerg /// 13367330f729Sjoerg /// NOTE: This should only be called for definitions. 13377330f729Sjoerg void SetCommonAttributes(GlobalDecl GD, llvm::GlobalValue *GV); 13387330f729Sjoerg 13397330f729Sjoerg void addReplacement(StringRef Name, llvm::Constant *C); 13407330f729Sjoerg 13417330f729Sjoerg void addGlobalValReplacement(llvm::GlobalValue *GV, llvm::Constant *C); 13427330f729Sjoerg 13437330f729Sjoerg /// Emit a code for threadprivate directive. 13447330f729Sjoerg /// \param D Threadprivate declaration. 13457330f729Sjoerg void EmitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D); 13467330f729Sjoerg 13477330f729Sjoerg /// Emit a code for declare reduction construct. 13487330f729Sjoerg void EmitOMPDeclareReduction(const OMPDeclareReductionDecl *D, 13497330f729Sjoerg CodeGenFunction *CGF = nullptr); 13507330f729Sjoerg 13517330f729Sjoerg /// Emit a code for declare mapper construct. 13527330f729Sjoerg void EmitOMPDeclareMapper(const OMPDeclareMapperDecl *D, 13537330f729Sjoerg CodeGenFunction *CGF = nullptr); 13547330f729Sjoerg 13557330f729Sjoerg /// Emit a code for requires directive. 13567330f729Sjoerg /// \param D Requires declaration 13577330f729Sjoerg void EmitOMPRequiresDecl(const OMPRequiresDecl *D); 13587330f729Sjoerg 1359*e038c9c4Sjoerg /// Emit a code for the allocate directive. 1360*e038c9c4Sjoerg /// \param D The allocate declaration 1361*e038c9c4Sjoerg void EmitOMPAllocateDecl(const OMPAllocateDecl *D); 13627330f729Sjoerg 13637330f729Sjoerg /// Returns whether the given record has hidden LTO visibility and therefore 13647330f729Sjoerg /// may participate in (single-module) CFI and whole-program vtable 13657330f729Sjoerg /// optimization. 13667330f729Sjoerg bool HasHiddenLTOVisibility(const CXXRecordDecl *RD); 13677330f729Sjoerg 1368*e038c9c4Sjoerg /// Returns whether the given record has public std LTO visibility 1369*e038c9c4Sjoerg /// and therefore may not participate in (single-module) CFI and whole-program 1370*e038c9c4Sjoerg /// vtable optimization. 1371*e038c9c4Sjoerg bool HasLTOVisibilityPublicStd(const CXXRecordDecl *RD); 1372*e038c9c4Sjoerg 13737330f729Sjoerg /// Returns the vcall visibility of the given type. This is the scope in which 13747330f729Sjoerg /// a virtual function call could be made which ends up being dispatched to a 13757330f729Sjoerg /// member function of this class. This scope can be wider than the visibility 13767330f729Sjoerg /// of the class itself when the class has a more-visible dynamic base class. 1377*e038c9c4Sjoerg /// The client should pass in an empty Visited set, which is used to prevent 1378*e038c9c4Sjoerg /// redundant recursive processing. 13797330f729Sjoerg llvm::GlobalObject::VCallVisibility 1380*e038c9c4Sjoerg GetVCallVisibilityLevel(const CXXRecordDecl *RD, 1381*e038c9c4Sjoerg llvm::DenseSet<const CXXRecordDecl *> &Visited); 13827330f729Sjoerg 13837330f729Sjoerg /// Emit type metadata for the given vtable using the given layout. 13847330f729Sjoerg void EmitVTableTypeMetadata(const CXXRecordDecl *RD, 13857330f729Sjoerg llvm::GlobalVariable *VTable, 13867330f729Sjoerg const VTableLayout &VTLayout); 13877330f729Sjoerg 13887330f729Sjoerg /// Generate a cross-DSO type identifier for MD. 13897330f729Sjoerg llvm::ConstantInt *CreateCrossDsoCfiTypeId(llvm::Metadata *MD); 13907330f729Sjoerg 13917330f729Sjoerg /// Create a metadata identifier for the given type. This may either be an 13927330f729Sjoerg /// MDString (for external identifiers) or a distinct unnamed MDNode (for 13937330f729Sjoerg /// internal identifiers). 13947330f729Sjoerg llvm::Metadata *CreateMetadataIdentifierForType(QualType T); 13957330f729Sjoerg 13967330f729Sjoerg /// Create a metadata identifier that is intended to be used to check virtual 13977330f729Sjoerg /// calls via a member function pointer. 13987330f729Sjoerg llvm::Metadata *CreateMetadataIdentifierForVirtualMemPtrType(QualType T); 13997330f729Sjoerg 14007330f729Sjoerg /// Create a metadata identifier for the generalization of the given type. 14017330f729Sjoerg /// This may either be an MDString (for external identifiers) or a distinct 14027330f729Sjoerg /// unnamed MDNode (for internal identifiers). 14037330f729Sjoerg llvm::Metadata *CreateMetadataIdentifierGeneralized(QualType T); 14047330f729Sjoerg 14057330f729Sjoerg /// Create and attach type metadata to the given function. 14067330f729Sjoerg void CreateFunctionTypeMetadataForIcall(const FunctionDecl *FD, 14077330f729Sjoerg llvm::Function *F); 14087330f729Sjoerg 1409*e038c9c4Sjoerg /// Whether this function's return type has no side effects, and thus may 1410*e038c9c4Sjoerg /// be trivially discarded if it is unused. 1411*e038c9c4Sjoerg bool MayDropFunctionReturn(const ASTContext &Context, QualType ReturnType); 1412*e038c9c4Sjoerg 14137330f729Sjoerg /// Returns whether this module needs the "all-vtables" type identifier. 14147330f729Sjoerg bool NeedAllVtablesTypeId() const; 14157330f729Sjoerg 14167330f729Sjoerg /// Create and attach type metadata for the given vtable. 14177330f729Sjoerg void AddVTableTypeMetadata(llvm::GlobalVariable *VTable, CharUnits Offset, 14187330f729Sjoerg const CXXRecordDecl *RD); 14197330f729Sjoerg 14207330f729Sjoerg /// Return a vector of most-base classes for RD. This is used to implement 14217330f729Sjoerg /// control flow integrity checks for member function pointers. 14227330f729Sjoerg /// 14237330f729Sjoerg /// A most-base class of a class C is defined as a recursive base class of C, 14247330f729Sjoerg /// including C itself, that does not have any bases. 14257330f729Sjoerg std::vector<const CXXRecordDecl *> 14267330f729Sjoerg getMostBaseClasses(const CXXRecordDecl *RD); 14277330f729Sjoerg 14287330f729Sjoerg /// Get the declaration of std::terminate for the platform. 14297330f729Sjoerg llvm::FunctionCallee getTerminateFn(); 14307330f729Sjoerg 14317330f729Sjoerg llvm::SanitizerStatReport &getSanStats(); 14327330f729Sjoerg 14337330f729Sjoerg llvm::Value * 14347330f729Sjoerg createOpenCLIntToSamplerConversion(const Expr *E, CodeGenFunction &CGF); 14357330f729Sjoerg 14367330f729Sjoerg /// OpenCL v1.2 s5.6.4.6 allows the compiler to store kernel argument 14377330f729Sjoerg /// information in the program executable. The argument information stored 14387330f729Sjoerg /// includes the argument name, its type, the address and access qualifiers 14397330f729Sjoerg /// used. This helper can be used to generate metadata for source code kernel 14407330f729Sjoerg /// function as well as generated implicitly kernels. If a kernel is generated 14417330f729Sjoerg /// implicitly null value has to be passed to the last two parameters, 14427330f729Sjoerg /// otherwise all parameters must have valid non-null values. 14437330f729Sjoerg /// \param FN is a pointer to IR function being generated. 14447330f729Sjoerg /// \param FD is a pointer to function declaration if any. 14457330f729Sjoerg /// \param CGF is a pointer to CodeGenFunction that generates this function. 14467330f729Sjoerg void GenOpenCLArgMetadata(llvm::Function *FN, 14477330f729Sjoerg const FunctionDecl *FD = nullptr, 14487330f729Sjoerg CodeGenFunction *CGF = nullptr); 14497330f729Sjoerg 14507330f729Sjoerg /// Get target specific null pointer. 14517330f729Sjoerg /// \param T is the LLVM type of the null pointer. 14527330f729Sjoerg /// \param QT is the clang QualType of the null pointer. 14537330f729Sjoerg llvm::Constant *getNullPointer(llvm::PointerType *T, QualType QT); 14547330f729Sjoerg 1455*e038c9c4Sjoerg CharUnits getNaturalTypeAlignment(QualType T, 1456*e038c9c4Sjoerg LValueBaseInfo *BaseInfo = nullptr, 1457*e038c9c4Sjoerg TBAAAccessInfo *TBAAInfo = nullptr, 1458*e038c9c4Sjoerg bool forPointeeType = false); 1459*e038c9c4Sjoerg CharUnits getNaturalPointeeTypeAlignment(QualType T, 1460*e038c9c4Sjoerg LValueBaseInfo *BaseInfo = nullptr, 1461*e038c9c4Sjoerg TBAAAccessInfo *TBAAInfo = nullptr); 1462*e038c9c4Sjoerg bool stopAutoInit(); 1463*e038c9c4Sjoerg 1464*e038c9c4Sjoerg /// Print the postfix for externalized static variable for single source 1465*e038c9c4Sjoerg /// offloading languages CUDA and HIP. 1466*e038c9c4Sjoerg void printPostfixForExternalizedStaticVar(llvm::raw_ostream &OS) const; 1467*e038c9c4Sjoerg 14687330f729Sjoerg private: 14697330f729Sjoerg llvm::Constant *GetOrCreateLLVMFunction( 14707330f729Sjoerg StringRef MangledName, llvm::Type *Ty, GlobalDecl D, bool ForVTable, 14717330f729Sjoerg bool DontDefer = false, bool IsThunk = false, 14727330f729Sjoerg llvm::AttributeList ExtraAttrs = llvm::AttributeList(), 14737330f729Sjoerg ForDefinition_t IsForDefinition = NotForDefinition); 14747330f729Sjoerg 14757330f729Sjoerg llvm::Constant *GetOrCreateMultiVersionResolver(GlobalDecl GD, 14767330f729Sjoerg llvm::Type *DeclTy, 14777330f729Sjoerg const FunctionDecl *FD); 14787330f729Sjoerg void UpdateMultiVersionNames(GlobalDecl GD, const FunctionDecl *FD); 14797330f729Sjoerg 1480*e038c9c4Sjoerg llvm::Constant * 1481*e038c9c4Sjoerg GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty, 1482*e038c9c4Sjoerg unsigned AddrSpace, const VarDecl *D, 1483*e038c9c4Sjoerg ForDefinition_t IsForDefinition = NotForDefinition); 14847330f729Sjoerg 14857330f729Sjoerg bool GetCPUAndFeaturesAttributes(GlobalDecl GD, 14867330f729Sjoerg llvm::AttrBuilder &AttrBuilder); 14877330f729Sjoerg void setNonAliasAttributes(GlobalDecl GD, llvm::GlobalObject *GO); 14887330f729Sjoerg 14897330f729Sjoerg /// Set function attributes for a function declaration. 14907330f729Sjoerg void SetFunctionAttributes(GlobalDecl GD, llvm::Function *F, 14917330f729Sjoerg bool IsIncompleteFunction, bool IsThunk); 14927330f729Sjoerg 14937330f729Sjoerg void EmitGlobalDefinition(GlobalDecl D, llvm::GlobalValue *GV = nullptr); 14947330f729Sjoerg 14957330f729Sjoerg void EmitGlobalFunctionDefinition(GlobalDecl GD, llvm::GlobalValue *GV); 14967330f729Sjoerg void EmitMultiVersionFunctionDefinition(GlobalDecl GD, llvm::GlobalValue *GV); 14977330f729Sjoerg 14987330f729Sjoerg void EmitGlobalVarDefinition(const VarDecl *D, bool IsTentative = false); 1499*e038c9c4Sjoerg void EmitExternalVarDeclaration(const VarDecl *D); 15007330f729Sjoerg void EmitAliasDefinition(GlobalDecl GD); 15017330f729Sjoerg void emitIFuncDefinition(GlobalDecl GD); 15027330f729Sjoerg void emitCPUDispatchDefinition(GlobalDecl GD); 15037330f729Sjoerg void EmitObjCPropertyImplementations(const ObjCImplementationDecl *D); 15047330f729Sjoerg void EmitObjCIvarInitializations(ObjCImplementationDecl *D); 15057330f729Sjoerg 15067330f729Sjoerg // C++ related functions. 15077330f729Sjoerg 15087330f729Sjoerg void EmitDeclContext(const DeclContext *DC); 15097330f729Sjoerg void EmitLinkageSpec(const LinkageSpecDecl *D); 15107330f729Sjoerg 15117330f729Sjoerg /// Emit the function that initializes C++ thread_local variables. 15127330f729Sjoerg void EmitCXXThreadLocalInitFunc(); 15137330f729Sjoerg 15147330f729Sjoerg /// Emit the function that initializes C++ globals. 15157330f729Sjoerg void EmitCXXGlobalInitFunc(); 15167330f729Sjoerg 1517*e038c9c4Sjoerg /// Emit the function that performs cleanup associated with C++ globals. 1518*e038c9c4Sjoerg void EmitCXXGlobalCleanUpFunc(); 15197330f729Sjoerg 15207330f729Sjoerg /// Emit the function that initializes the specified global (if PerformInit is 15217330f729Sjoerg /// true) and registers its destructor. 15227330f729Sjoerg void EmitCXXGlobalVarDeclInitFunc(const VarDecl *D, 15237330f729Sjoerg llvm::GlobalVariable *Addr, 15247330f729Sjoerg bool PerformInit); 15257330f729Sjoerg 15267330f729Sjoerg void EmitPointerToInitFunc(const VarDecl *VD, llvm::GlobalVariable *Addr, 15277330f729Sjoerg llvm::Function *InitFunc, InitSegAttr *ISA); 15287330f729Sjoerg 15297330f729Sjoerg // FIXME: Hardcoding priority here is gross. 15307330f729Sjoerg void AddGlobalCtor(llvm::Function *Ctor, int Priority = 65535, 15317330f729Sjoerg llvm::Constant *AssociatedData = nullptr); 1532*e038c9c4Sjoerg void AddGlobalDtor(llvm::Function *Dtor, int Priority = 65535, 1533*e038c9c4Sjoerg bool IsDtorAttrFunc = false); 15347330f729Sjoerg 15357330f729Sjoerg /// EmitCtorList - Generates a global array of functions and priorities using 15367330f729Sjoerg /// the given list and name. This array will have appending linkage and is 15377330f729Sjoerg /// suitable for use as a LLVM constructor or destructor array. Clears Fns. 15387330f729Sjoerg void EmitCtorList(CtorList &Fns, const char *GlobalName); 15397330f729Sjoerg 15407330f729Sjoerg /// Emit any needed decls for which code generation was deferred. 15417330f729Sjoerg void EmitDeferred(); 15427330f729Sjoerg 15437330f729Sjoerg /// Try to emit external vtables as available_externally if they have emitted 15447330f729Sjoerg /// all inlined virtual functions. It runs after EmitDeferred() and therefore 15457330f729Sjoerg /// is not allowed to create new references to things that need to be emitted 15467330f729Sjoerg /// lazily. 15477330f729Sjoerg void EmitVTablesOpportunistically(); 15487330f729Sjoerg 15497330f729Sjoerg /// Call replaceAllUsesWith on all pairs in Replacements. 15507330f729Sjoerg void applyReplacements(); 15517330f729Sjoerg 15527330f729Sjoerg /// Call replaceAllUsesWith on all pairs in GlobalValReplacements. 15537330f729Sjoerg void applyGlobalValReplacements(); 15547330f729Sjoerg 15557330f729Sjoerg void checkAliases(); 15567330f729Sjoerg 15577330f729Sjoerg std::map<int, llvm::TinyPtrVector<llvm::Function *>> DtorsUsingAtExit; 15587330f729Sjoerg 15597330f729Sjoerg /// Register functions annotated with __attribute__((destructor)) using 15607330f729Sjoerg /// __cxa_atexit, if it is available, or atexit otherwise. 15617330f729Sjoerg void registerGlobalDtorsWithAtExit(); 15627330f729Sjoerg 1563*e038c9c4Sjoerg // When using sinit and sterm functions, unregister 1564*e038c9c4Sjoerg // __attribute__((destructor)) annotated functions which were previously 1565*e038c9c4Sjoerg // registered by the atexit subroutine using unatexit. 1566*e038c9c4Sjoerg void unregisterGlobalDtorsWithUnAtExit(); 1567*e038c9c4Sjoerg 15687330f729Sjoerg void emitMultiVersionFunctions(); 15697330f729Sjoerg 15707330f729Sjoerg /// Emit any vtables which we deferred and still have a use for. 15717330f729Sjoerg void EmitDeferredVTables(); 15727330f729Sjoerg 15737330f729Sjoerg /// Emit a dummy function that reference a CoreFoundation symbol when 15747330f729Sjoerg /// @available is used on Darwin. 15757330f729Sjoerg void emitAtAvailableLinkGuard(); 15767330f729Sjoerg 15777330f729Sjoerg /// Emit the llvm.used and llvm.compiler.used metadata. 15787330f729Sjoerg void emitLLVMUsed(); 15797330f729Sjoerg 15807330f729Sjoerg /// Emit the link options introduced by imported modules. 15817330f729Sjoerg void EmitModuleLinkOptions(); 15827330f729Sjoerg 15837330f729Sjoerg /// Emit aliases for internal-linkage declarations inside "C" language 15847330f729Sjoerg /// linkage specifications, giving them the "expected" name where possible. 15857330f729Sjoerg void EmitStaticExternCAliases(); 15867330f729Sjoerg 15877330f729Sjoerg void EmitDeclMetadata(); 15887330f729Sjoerg 15897330f729Sjoerg /// Emit the Clang version as llvm.ident metadata. 15907330f729Sjoerg void EmitVersionIdentMetadata(); 15917330f729Sjoerg 15927330f729Sjoerg /// Emit the Clang commandline as llvm.commandline metadata. 15937330f729Sjoerg void EmitCommandLineMetadata(); 15947330f729Sjoerg 1595*e038c9c4Sjoerg /// Emit the module flag metadata used to pass options controlling the 1596*e038c9c4Sjoerg /// the backend to LLVM. 1597*e038c9c4Sjoerg void EmitBackendOptionsMetadata(const CodeGenOptions CodeGenOpts); 15987330f729Sjoerg 15997330f729Sjoerg /// Emits OpenCL specific Metadata e.g. OpenCL version. 16007330f729Sjoerg void EmitOpenCLMetadata(); 16017330f729Sjoerg 16027330f729Sjoerg /// Emit the llvm.gcov metadata used to tell LLVM where to emit the .gcno and 16037330f729Sjoerg /// .gcda files in a way that persists in .bc files. 16047330f729Sjoerg void EmitCoverageFile(); 16057330f729Sjoerg 16067330f729Sjoerg /// Determine whether the definition must be emitted; if this returns \c 16077330f729Sjoerg /// false, the definition can be emitted lazily if it's used. 16087330f729Sjoerg bool MustBeEmitted(const ValueDecl *D); 16097330f729Sjoerg 16107330f729Sjoerg /// Determine whether the definition can be emitted eagerly, or should be 16117330f729Sjoerg /// delayed until the end of the translation unit. This is relevant for 16127330f729Sjoerg /// definitions whose linkage can change, e.g. implicit function instantions 16137330f729Sjoerg /// which may later be explicitly instantiated. 16147330f729Sjoerg bool MayBeEmittedEagerly(const ValueDecl *D); 16157330f729Sjoerg 16167330f729Sjoerg /// Check whether we can use a "simpler", more core exceptions personality 16177330f729Sjoerg /// function. 16187330f729Sjoerg void SimplifyPersonality(); 16197330f729Sjoerg 1620*e038c9c4Sjoerg /// Helper function for ConstructAttributeList and 1621*e038c9c4Sjoerg /// addDefaultFunctionDefinitionAttributes. Builds a set of function 1622*e038c9c4Sjoerg /// attributes to add to a function with the given properties. 1623*e038c9c4Sjoerg void getDefaultFunctionAttributes(StringRef Name, bool HasOptnone, 16247330f729Sjoerg bool AttrOnCallSite, 16257330f729Sjoerg llvm::AttrBuilder &FuncAttrs); 16267330f729Sjoerg 16277330f729Sjoerg llvm::Metadata *CreateMetadataIdentifierImpl(QualType T, MetadataTypeMap &Map, 16287330f729Sjoerg StringRef Suffix); 16297330f729Sjoerg }; 16307330f729Sjoerg 16317330f729Sjoerg } // end namespace CodeGen 16327330f729Sjoerg } // end namespace clang 16337330f729Sjoerg 16347330f729Sjoerg #endif // LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H 1635