1f4a2713aSLionel Sambuc //===--- CodeGenModule.h - Per-Module state for LLVM CodeGen ----*- C++ -*-===// 2f4a2713aSLionel Sambuc // 3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure 4f4a2713aSLionel Sambuc // 5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source 6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details. 7f4a2713aSLionel Sambuc // 8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 9f4a2713aSLionel Sambuc // 10f4a2713aSLionel Sambuc // This is the internal per-translation-unit state used for llvm translation. 11f4a2713aSLionel Sambuc // 12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 13f4a2713aSLionel Sambuc 14*0a6a1f1dSLionel Sambuc #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H 15*0a6a1f1dSLionel Sambuc #define LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H 16f4a2713aSLionel Sambuc 17f4a2713aSLionel Sambuc #include "CGVTables.h" 18f4a2713aSLionel Sambuc #include "CodeGenTypes.h" 19*0a6a1f1dSLionel Sambuc #include "SanitizerMetadata.h" 20f4a2713aSLionel Sambuc #include "clang/AST/Attr.h" 21f4a2713aSLionel Sambuc #include "clang/AST/DeclCXX.h" 22f4a2713aSLionel Sambuc #include "clang/AST/DeclObjC.h" 23f4a2713aSLionel Sambuc #include "clang/AST/GlobalDecl.h" 24f4a2713aSLionel Sambuc #include "clang/AST/Mangle.h" 25f4a2713aSLionel Sambuc #include "clang/Basic/ABI.h" 26f4a2713aSLionel Sambuc #include "clang/Basic/LangOptions.h" 27f4a2713aSLionel Sambuc #include "clang/Basic/Module.h" 28*0a6a1f1dSLionel Sambuc #include "clang/Basic/SanitizerBlacklist.h" 29f4a2713aSLionel Sambuc #include "llvm/ADT/DenseMap.h" 30f4a2713aSLionel Sambuc #include "llvm/ADT/SetVector.h" 31f4a2713aSLionel Sambuc #include "llvm/ADT/SmallPtrSet.h" 32f4a2713aSLionel Sambuc #include "llvm/ADT/StringMap.h" 33f4a2713aSLionel Sambuc #include "llvm/IR/CallingConv.h" 34f4a2713aSLionel Sambuc #include "llvm/IR/Module.h" 35*0a6a1f1dSLionel Sambuc #include "llvm/IR/ValueHandle.h" 36f4a2713aSLionel Sambuc 37f4a2713aSLionel Sambuc namespace llvm { 38f4a2713aSLionel Sambuc class Module; 39f4a2713aSLionel Sambuc class Constant; 40f4a2713aSLionel Sambuc class ConstantInt; 41f4a2713aSLionel Sambuc class Function; 42f4a2713aSLionel Sambuc class GlobalValue; 43f4a2713aSLionel Sambuc class DataLayout; 44f4a2713aSLionel Sambuc class FunctionType; 45f4a2713aSLionel Sambuc class LLVMContext; 46*0a6a1f1dSLionel Sambuc class IndexedInstrProfReader; 47f4a2713aSLionel Sambuc } 48f4a2713aSLionel Sambuc 49f4a2713aSLionel Sambuc namespace clang { 50f4a2713aSLionel Sambuc class TargetCodeGenInfo; 51f4a2713aSLionel Sambuc class ASTContext; 52f4a2713aSLionel Sambuc class AtomicType; 53f4a2713aSLionel Sambuc class FunctionDecl; 54f4a2713aSLionel Sambuc class IdentifierInfo; 55f4a2713aSLionel Sambuc class ObjCMethodDecl; 56f4a2713aSLionel Sambuc class ObjCImplementationDecl; 57f4a2713aSLionel Sambuc class ObjCCategoryImplDecl; 58f4a2713aSLionel Sambuc class ObjCProtocolDecl; 59f4a2713aSLionel Sambuc class ObjCEncodeExpr; 60f4a2713aSLionel Sambuc class BlockExpr; 61f4a2713aSLionel Sambuc class CharUnits; 62f4a2713aSLionel Sambuc class Decl; 63f4a2713aSLionel Sambuc class Expr; 64f4a2713aSLionel Sambuc class Stmt; 65f4a2713aSLionel Sambuc class InitListExpr; 66f4a2713aSLionel Sambuc class StringLiteral; 67f4a2713aSLionel Sambuc class NamedDecl; 68f4a2713aSLionel Sambuc class ValueDecl; 69f4a2713aSLionel Sambuc class VarDecl; 70f4a2713aSLionel Sambuc class LangOptions; 71f4a2713aSLionel Sambuc class CodeGenOptions; 72f4a2713aSLionel Sambuc class DiagnosticsEngine; 73f4a2713aSLionel Sambuc class AnnotateAttr; 74f4a2713aSLionel Sambuc class CXXDestructorDecl; 75f4a2713aSLionel Sambuc class Module; 76*0a6a1f1dSLionel Sambuc class CoverageSourceInfo; 77f4a2713aSLionel Sambuc 78f4a2713aSLionel Sambuc namespace CodeGen { 79f4a2713aSLionel Sambuc 80f4a2713aSLionel Sambuc class CallArgList; 81f4a2713aSLionel Sambuc class CodeGenFunction; 82f4a2713aSLionel Sambuc class CodeGenTBAA; 83f4a2713aSLionel Sambuc class CGCXXABI; 84f4a2713aSLionel Sambuc class CGDebugInfo; 85f4a2713aSLionel Sambuc class CGObjCRuntime; 86f4a2713aSLionel Sambuc class CGOpenCLRuntime; 87*0a6a1f1dSLionel Sambuc class CGOpenMPRuntime; 88f4a2713aSLionel Sambuc class CGCUDARuntime; 89f4a2713aSLionel Sambuc class BlockFieldFlags; 90f4a2713aSLionel Sambuc class FunctionArgList; 91*0a6a1f1dSLionel Sambuc class CoverageMappingModuleGen; 92f4a2713aSLionel Sambuc 93f4a2713aSLionel Sambuc struct OrderGlobalInits { 94f4a2713aSLionel Sambuc unsigned int priority; 95f4a2713aSLionel Sambuc unsigned int lex_order; OrderGlobalInitsOrderGlobalInits96f4a2713aSLionel Sambuc OrderGlobalInits(unsigned int p, unsigned int l) 97f4a2713aSLionel Sambuc : priority(p), lex_order(l) {} 98f4a2713aSLionel Sambuc 99f4a2713aSLionel Sambuc bool operator==(const OrderGlobalInits &RHS) const { 100*0a6a1f1dSLionel Sambuc return priority == RHS.priority && lex_order == RHS.lex_order; 101f4a2713aSLionel Sambuc } 102f4a2713aSLionel Sambuc 103f4a2713aSLionel Sambuc bool operator<(const OrderGlobalInits &RHS) const { 104*0a6a1f1dSLionel Sambuc return std::tie(priority, lex_order) < 105*0a6a1f1dSLionel Sambuc std::tie(RHS.priority, RHS.lex_order); 106f4a2713aSLionel Sambuc } 107f4a2713aSLionel Sambuc }; 108f4a2713aSLionel Sambuc 109f4a2713aSLionel Sambuc struct CodeGenTypeCache { 110f4a2713aSLionel Sambuc /// void 111f4a2713aSLionel Sambuc llvm::Type *VoidTy; 112f4a2713aSLionel Sambuc 113f4a2713aSLionel Sambuc /// i8, i16, i32, and i64 114f4a2713aSLionel Sambuc llvm::IntegerType *Int8Ty, *Int16Ty, *Int32Ty, *Int64Ty; 115f4a2713aSLionel Sambuc /// float, double 116f4a2713aSLionel Sambuc llvm::Type *FloatTy, *DoubleTy; 117f4a2713aSLionel Sambuc 118f4a2713aSLionel Sambuc /// int 119f4a2713aSLionel Sambuc llvm::IntegerType *IntTy; 120f4a2713aSLionel Sambuc 121f4a2713aSLionel Sambuc /// intptr_t, size_t, and ptrdiff_t, which we assume are the same size. 122f4a2713aSLionel Sambuc union { 123f4a2713aSLionel Sambuc llvm::IntegerType *IntPtrTy; 124f4a2713aSLionel Sambuc llvm::IntegerType *SizeTy; 125f4a2713aSLionel Sambuc llvm::IntegerType *PtrDiffTy; 126f4a2713aSLionel Sambuc }; 127f4a2713aSLionel Sambuc 128f4a2713aSLionel Sambuc /// void* in address space 0 129f4a2713aSLionel Sambuc union { 130f4a2713aSLionel Sambuc llvm::PointerType *VoidPtrTy; 131f4a2713aSLionel Sambuc llvm::PointerType *Int8PtrTy; 132f4a2713aSLionel Sambuc }; 133f4a2713aSLionel Sambuc 134f4a2713aSLionel Sambuc /// void** in address space 0 135f4a2713aSLionel Sambuc union { 136f4a2713aSLionel Sambuc llvm::PointerType *VoidPtrPtrTy; 137f4a2713aSLionel Sambuc llvm::PointerType *Int8PtrPtrTy; 138f4a2713aSLionel Sambuc }; 139f4a2713aSLionel Sambuc 140f4a2713aSLionel Sambuc /// The width of a pointer into the generic address space. 141f4a2713aSLionel Sambuc unsigned char PointerWidthInBits; 142f4a2713aSLionel Sambuc 143f4a2713aSLionel Sambuc /// The size and alignment of a pointer into the generic address 144f4a2713aSLionel Sambuc /// space. 145f4a2713aSLionel Sambuc union { 146f4a2713aSLionel Sambuc unsigned char PointerAlignInBytes; 147f4a2713aSLionel Sambuc unsigned char PointerSizeInBytes; 148f4a2713aSLionel Sambuc unsigned char SizeSizeInBytes; // sizeof(size_t) 149f4a2713aSLionel Sambuc }; 150f4a2713aSLionel Sambuc 151f4a2713aSLionel Sambuc llvm::CallingConv::ID RuntimeCC; getRuntimeCCCodeGenTypeCache152*0a6a1f1dSLionel Sambuc llvm::CallingConv::ID getRuntimeCC() const { return RuntimeCC; } 153*0a6a1f1dSLionel Sambuc llvm::CallingConv::ID BuiltinCC; getBuiltinCCCodeGenTypeCache154*0a6a1f1dSLionel Sambuc llvm::CallingConv::ID getBuiltinCC() const { return BuiltinCC; } 155f4a2713aSLionel Sambuc }; 156f4a2713aSLionel Sambuc 157f4a2713aSLionel Sambuc struct RREntrypoints { RREntrypointsRREntrypoints158f4a2713aSLionel Sambuc RREntrypoints() { memset(this, 0, sizeof(*this)); } 159f4a2713aSLionel Sambuc /// void objc_autoreleasePoolPop(void*); 160f4a2713aSLionel Sambuc llvm::Constant *objc_autoreleasePoolPop; 161f4a2713aSLionel Sambuc 162f4a2713aSLionel Sambuc /// void *objc_autoreleasePoolPush(void); 163f4a2713aSLionel Sambuc llvm::Constant *objc_autoreleasePoolPush; 164f4a2713aSLionel Sambuc }; 165f4a2713aSLionel Sambuc 166f4a2713aSLionel Sambuc struct ARCEntrypoints { ARCEntrypointsARCEntrypoints167f4a2713aSLionel Sambuc ARCEntrypoints() { memset(this, 0, sizeof(*this)); } 168f4a2713aSLionel Sambuc 169f4a2713aSLionel Sambuc /// id objc_autorelease(id); 170f4a2713aSLionel Sambuc llvm::Constant *objc_autorelease; 171f4a2713aSLionel Sambuc 172f4a2713aSLionel Sambuc /// id objc_autoreleaseReturnValue(id); 173f4a2713aSLionel Sambuc llvm::Constant *objc_autoreleaseReturnValue; 174f4a2713aSLionel Sambuc 175f4a2713aSLionel Sambuc /// void objc_copyWeak(id *dest, id *src); 176f4a2713aSLionel Sambuc llvm::Constant *objc_copyWeak; 177f4a2713aSLionel Sambuc 178f4a2713aSLionel Sambuc /// void objc_destroyWeak(id*); 179f4a2713aSLionel Sambuc llvm::Constant *objc_destroyWeak; 180f4a2713aSLionel Sambuc 181f4a2713aSLionel Sambuc /// id objc_initWeak(id*, id); 182f4a2713aSLionel Sambuc llvm::Constant *objc_initWeak; 183f4a2713aSLionel Sambuc 184f4a2713aSLionel Sambuc /// id objc_loadWeak(id*); 185f4a2713aSLionel Sambuc llvm::Constant *objc_loadWeak; 186f4a2713aSLionel Sambuc 187f4a2713aSLionel Sambuc /// id objc_loadWeakRetained(id*); 188f4a2713aSLionel Sambuc llvm::Constant *objc_loadWeakRetained; 189f4a2713aSLionel Sambuc 190f4a2713aSLionel Sambuc /// void objc_moveWeak(id *dest, id *src); 191f4a2713aSLionel Sambuc llvm::Constant *objc_moveWeak; 192f4a2713aSLionel Sambuc 193f4a2713aSLionel Sambuc /// id objc_retain(id); 194f4a2713aSLionel Sambuc llvm::Constant *objc_retain; 195f4a2713aSLionel Sambuc 196f4a2713aSLionel Sambuc /// id objc_retainAutorelease(id); 197f4a2713aSLionel Sambuc llvm::Constant *objc_retainAutorelease; 198f4a2713aSLionel Sambuc 199f4a2713aSLionel Sambuc /// id objc_retainAutoreleaseReturnValue(id); 200f4a2713aSLionel Sambuc llvm::Constant *objc_retainAutoreleaseReturnValue; 201f4a2713aSLionel Sambuc 202f4a2713aSLionel Sambuc /// id objc_retainAutoreleasedReturnValue(id); 203f4a2713aSLionel Sambuc llvm::Constant *objc_retainAutoreleasedReturnValue; 204f4a2713aSLionel Sambuc 205f4a2713aSLionel Sambuc /// id objc_retainBlock(id); 206f4a2713aSLionel Sambuc llvm::Constant *objc_retainBlock; 207f4a2713aSLionel Sambuc 208f4a2713aSLionel Sambuc /// void objc_release(id); 209f4a2713aSLionel Sambuc llvm::Constant *objc_release; 210f4a2713aSLionel Sambuc 211f4a2713aSLionel Sambuc /// id objc_storeStrong(id*, id); 212f4a2713aSLionel Sambuc llvm::Constant *objc_storeStrong; 213f4a2713aSLionel Sambuc 214f4a2713aSLionel Sambuc /// id objc_storeWeak(id*, id); 215f4a2713aSLionel Sambuc llvm::Constant *objc_storeWeak; 216f4a2713aSLionel Sambuc 217f4a2713aSLionel Sambuc /// A void(void) inline asm to use to mark that the return value of 218f4a2713aSLionel Sambuc /// a call will be immediately retain. 219f4a2713aSLionel Sambuc llvm::InlineAsm *retainAutoreleasedReturnValueMarker; 220f4a2713aSLionel Sambuc 221f4a2713aSLionel Sambuc /// void clang.arc.use(...); 222f4a2713aSLionel Sambuc llvm::Constant *clang_arc_use; 223f4a2713aSLionel Sambuc }; 224f4a2713aSLionel Sambuc 225*0a6a1f1dSLionel Sambuc /// This class records statistics on instrumentation based profiling. 226*0a6a1f1dSLionel Sambuc class InstrProfStats { 227*0a6a1f1dSLionel Sambuc uint32_t VisitedInMainFile; 228*0a6a1f1dSLionel Sambuc uint32_t MissingInMainFile; 229*0a6a1f1dSLionel Sambuc uint32_t Visited; 230*0a6a1f1dSLionel Sambuc uint32_t Missing; 231*0a6a1f1dSLionel Sambuc uint32_t Mismatched; 232*0a6a1f1dSLionel Sambuc 233*0a6a1f1dSLionel Sambuc public: InstrProfStats()234*0a6a1f1dSLionel Sambuc InstrProfStats() 235*0a6a1f1dSLionel Sambuc : VisitedInMainFile(0), MissingInMainFile(0), Visited(0), Missing(0), 236*0a6a1f1dSLionel Sambuc Mismatched(0) {} 237*0a6a1f1dSLionel Sambuc /// Record that we've visited a function and whether or not that function was 238*0a6a1f1dSLionel Sambuc /// in the main source file. addVisited(bool MainFile)239*0a6a1f1dSLionel Sambuc void addVisited(bool MainFile) { 240*0a6a1f1dSLionel Sambuc if (MainFile) 241*0a6a1f1dSLionel Sambuc ++VisitedInMainFile; 242*0a6a1f1dSLionel Sambuc ++Visited; 243*0a6a1f1dSLionel Sambuc } 244*0a6a1f1dSLionel Sambuc /// Record that a function we've visited has no profile data. addMissing(bool MainFile)245*0a6a1f1dSLionel Sambuc void addMissing(bool MainFile) { 246*0a6a1f1dSLionel Sambuc if (MainFile) 247*0a6a1f1dSLionel Sambuc ++MissingInMainFile; 248*0a6a1f1dSLionel Sambuc ++Missing; 249*0a6a1f1dSLionel Sambuc } 250*0a6a1f1dSLionel Sambuc /// Record that a function we've visited has mismatched profile data. addMismatched(bool MainFile)251*0a6a1f1dSLionel Sambuc void addMismatched(bool MainFile) { ++Mismatched; } 252*0a6a1f1dSLionel Sambuc /// Whether or not the stats we've gathered indicate any potential problems. hasDiagnostics()253*0a6a1f1dSLionel Sambuc bool hasDiagnostics() { return Missing || Mismatched; } 254*0a6a1f1dSLionel Sambuc /// Report potential problems we've found to \c Diags. 255*0a6a1f1dSLionel Sambuc void reportDiagnostics(DiagnosticsEngine &Diags, StringRef MainFile); 256*0a6a1f1dSLionel Sambuc }; 257*0a6a1f1dSLionel Sambuc 258*0a6a1f1dSLionel Sambuc /// This class organizes the cross-function state that is used while generating 259*0a6a1f1dSLionel Sambuc /// LLVM code. 260f4a2713aSLionel Sambuc class CodeGenModule : public CodeGenTypeCache { 261f4a2713aSLionel Sambuc CodeGenModule(const CodeGenModule &) LLVM_DELETED_FUNCTION; 262f4a2713aSLionel Sambuc void operator=(const CodeGenModule &) LLVM_DELETED_FUNCTION; 263f4a2713aSLionel Sambuc 264*0a6a1f1dSLionel Sambuc public: 265*0a6a1f1dSLionel Sambuc struct Structor { StructorStructor266*0a6a1f1dSLionel Sambuc Structor() : Priority(0), Initializer(nullptr), AssociatedData(nullptr) {} StructorStructor267*0a6a1f1dSLionel Sambuc Structor(int Priority, llvm::Constant *Initializer, 268*0a6a1f1dSLionel Sambuc llvm::Constant *AssociatedData) 269*0a6a1f1dSLionel Sambuc : Priority(Priority), Initializer(Initializer), 270*0a6a1f1dSLionel Sambuc AssociatedData(AssociatedData) {} 271*0a6a1f1dSLionel Sambuc int Priority; 272*0a6a1f1dSLionel Sambuc llvm::Constant *Initializer; 273*0a6a1f1dSLionel Sambuc llvm::Constant *AssociatedData; 274*0a6a1f1dSLionel Sambuc }; 275f4a2713aSLionel Sambuc 276*0a6a1f1dSLionel Sambuc typedef std::vector<Structor> CtorList; 277*0a6a1f1dSLionel Sambuc 278*0a6a1f1dSLionel Sambuc private: 279f4a2713aSLionel Sambuc ASTContext &Context; 280f4a2713aSLionel Sambuc const LangOptions &LangOpts; 281f4a2713aSLionel Sambuc const CodeGenOptions &CodeGenOpts; 282f4a2713aSLionel Sambuc llvm::Module &TheModule; 283f4a2713aSLionel Sambuc DiagnosticsEngine &Diags; 284f4a2713aSLionel Sambuc const llvm::DataLayout &TheDataLayout; 285f4a2713aSLionel Sambuc const TargetInfo &Target; 286*0a6a1f1dSLionel Sambuc std::unique_ptr<CGCXXABI> ABI; 287f4a2713aSLionel Sambuc llvm::LLVMContext &VMContext; 288f4a2713aSLionel Sambuc 289f4a2713aSLionel Sambuc CodeGenTBAA *TBAA; 290f4a2713aSLionel Sambuc 291f4a2713aSLionel Sambuc mutable const TargetCodeGenInfo *TheTargetCodeGenInfo; 292f4a2713aSLionel Sambuc 293f4a2713aSLionel Sambuc // This should not be moved earlier, since its initialization depends on some 294f4a2713aSLionel Sambuc // of the previous reference members being already initialized and also checks 295f4a2713aSLionel Sambuc // if TheTargetCodeGenInfo is NULL 296f4a2713aSLionel Sambuc CodeGenTypes Types; 297f4a2713aSLionel Sambuc 298*0a6a1f1dSLionel Sambuc /// Holds information about C++ vtables. 299f4a2713aSLionel Sambuc CodeGenVTables VTables; 300f4a2713aSLionel Sambuc 301f4a2713aSLionel Sambuc CGObjCRuntime* ObjCRuntime; 302f4a2713aSLionel Sambuc CGOpenCLRuntime* OpenCLRuntime; 303*0a6a1f1dSLionel Sambuc CGOpenMPRuntime* OpenMPRuntime; 304f4a2713aSLionel Sambuc CGCUDARuntime* CUDARuntime; 305f4a2713aSLionel Sambuc CGDebugInfo* DebugInfo; 306f4a2713aSLionel Sambuc ARCEntrypoints *ARCData; 307f4a2713aSLionel Sambuc llvm::MDNode *NoObjCARCExceptionsMetadata; 308f4a2713aSLionel Sambuc RREntrypoints *RRData; 309*0a6a1f1dSLionel Sambuc std::unique_ptr<llvm::IndexedInstrProfReader> PGOReader; 310*0a6a1f1dSLionel Sambuc InstrProfStats PGOStats; 311f4a2713aSLionel Sambuc 312*0a6a1f1dSLionel Sambuc // A set of references that have only been seen via a weakref so far. This is 313*0a6a1f1dSLionel Sambuc // used to remove the weak of the reference if we ever see a direct reference 314*0a6a1f1dSLionel Sambuc // or a definition. 315f4a2713aSLionel Sambuc llvm::SmallPtrSet<llvm::GlobalValue*, 10> WeakRefReferences; 316f4a2713aSLionel Sambuc 317*0a6a1f1dSLionel Sambuc /// This contains all the decls which have definitions but/ which are deferred 318*0a6a1f1dSLionel Sambuc /// for emission and therefore should only be output if they are actually 319*0a6a1f1dSLionel Sambuc /// used. If a decl is in this, then it is known to have not been referenced 320*0a6a1f1dSLionel Sambuc /// yet. 321*0a6a1f1dSLionel Sambuc std::map<StringRef, GlobalDecl> DeferredDecls; 322f4a2713aSLionel Sambuc 323*0a6a1f1dSLionel Sambuc /// This is a list of deferred decls which we have seen that *are* actually 324*0a6a1f1dSLionel Sambuc /// referenced. These get code generated when the module is done. 325*0a6a1f1dSLionel Sambuc struct DeferredGlobal { DeferredGlobalDeferredGlobal326*0a6a1f1dSLionel Sambuc DeferredGlobal(llvm::GlobalValue *GV, GlobalDecl GD) : GV(GV), GD(GD) {} 327*0a6a1f1dSLionel Sambuc llvm::TrackingVH<llvm::GlobalValue> GV; 328*0a6a1f1dSLionel Sambuc GlobalDecl GD; 329*0a6a1f1dSLionel Sambuc }; 330*0a6a1f1dSLionel Sambuc std::vector<DeferredGlobal> DeferredDeclsToEmit; addDeferredDeclToEmit(llvm::GlobalValue * GV,GlobalDecl GD)331*0a6a1f1dSLionel Sambuc void addDeferredDeclToEmit(llvm::GlobalValue *GV, GlobalDecl GD) { 332*0a6a1f1dSLionel Sambuc DeferredDeclsToEmit.push_back(DeferredGlobal(GV, GD)); 333*0a6a1f1dSLionel Sambuc } 334f4a2713aSLionel Sambuc 335f4a2713aSLionel Sambuc /// List of alias we have emitted. Used to make sure that what they point to 336f4a2713aSLionel Sambuc /// is defined once we get to the end of the of the translation unit. 337f4a2713aSLionel Sambuc std::vector<GlobalDecl> Aliases; 338f4a2713aSLionel Sambuc 339f4a2713aSLionel Sambuc typedef llvm::StringMap<llvm::TrackingVH<llvm::Constant> > ReplacementsTy; 340f4a2713aSLionel Sambuc ReplacementsTy Replacements; 341f4a2713aSLionel Sambuc 342*0a6a1f1dSLionel Sambuc /// A queue of (optional) vtables to consider emitting. 343f4a2713aSLionel Sambuc std::vector<const CXXRecordDecl*> DeferredVTables; 344f4a2713aSLionel Sambuc 345*0a6a1f1dSLionel Sambuc /// List of global values which are required to be present in the object file; 346*0a6a1f1dSLionel Sambuc /// bitcast to i8*. This is used for forcing visibility of symbols which may 347*0a6a1f1dSLionel Sambuc /// otherwise be optimized out. 348f4a2713aSLionel Sambuc std::vector<llvm::WeakVH> LLVMUsed; 349*0a6a1f1dSLionel Sambuc std::vector<llvm::WeakVH> LLVMCompilerUsed; 350f4a2713aSLionel Sambuc 351*0a6a1f1dSLionel Sambuc /// Store the list of global constructors and their respective priorities to 352*0a6a1f1dSLionel Sambuc /// be emitted when the translation unit is complete. 353f4a2713aSLionel Sambuc CtorList GlobalCtors; 354f4a2713aSLionel Sambuc 355*0a6a1f1dSLionel Sambuc /// Store the list of global destructors and their respective priorities to be 356*0a6a1f1dSLionel Sambuc /// emitted when the translation unit is complete. 357f4a2713aSLionel Sambuc CtorList GlobalDtors; 358f4a2713aSLionel Sambuc 359*0a6a1f1dSLionel Sambuc /// An ordered map of canonical GlobalDecls to their mangled names. 360*0a6a1f1dSLionel Sambuc llvm::MapVector<GlobalDecl, StringRef> MangledDeclNames; 361*0a6a1f1dSLionel Sambuc llvm::StringMap<GlobalDecl, llvm::BumpPtrAllocator> Manglings; 362f4a2713aSLionel Sambuc 363f4a2713aSLionel Sambuc /// Global annotations. 364f4a2713aSLionel Sambuc std::vector<llvm::Constant*> Annotations; 365f4a2713aSLionel Sambuc 366f4a2713aSLionel Sambuc /// Map used to get unique annotation strings. 367f4a2713aSLionel Sambuc llvm::StringMap<llvm::Constant*> AnnotationStrings; 368f4a2713aSLionel Sambuc 369f4a2713aSLionel Sambuc llvm::StringMap<llvm::Constant*> CFConstantStringMap; 370*0a6a1f1dSLionel Sambuc 371*0a6a1f1dSLionel Sambuc llvm::DenseMap<llvm::Constant *, llvm::GlobalVariable *> ConstantStringMap; 372f4a2713aSLionel Sambuc llvm::DenseMap<const Decl*, llvm::Constant *> StaticLocalDeclMap; 373f4a2713aSLionel Sambuc llvm::DenseMap<const Decl*, llvm::GlobalVariable*> StaticLocalDeclGuardMap; 374f4a2713aSLionel Sambuc llvm::DenseMap<const Expr*, llvm::Constant *> MaterializedGlobalTemporaryMap; 375f4a2713aSLionel Sambuc 376f4a2713aSLionel Sambuc llvm::DenseMap<QualType, llvm::Constant *> AtomicSetterHelperFnMap; 377f4a2713aSLionel Sambuc llvm::DenseMap<QualType, llvm::Constant *> AtomicGetterHelperFnMap; 378f4a2713aSLionel Sambuc 379f4a2713aSLionel Sambuc /// Map used to get unique type descriptor constants for sanitizers. 380f4a2713aSLionel Sambuc llvm::DenseMap<QualType, llvm::Constant *> TypeDescriptorMap; 381f4a2713aSLionel Sambuc 382f4a2713aSLionel Sambuc /// Map used to track internal linkage functions declared within 383f4a2713aSLionel Sambuc /// extern "C" regions. 384f4a2713aSLionel Sambuc typedef llvm::MapVector<IdentifierInfo *, 385f4a2713aSLionel Sambuc llvm::GlobalValue *> StaticExternCMap; 386f4a2713aSLionel Sambuc StaticExternCMap StaticExternCValues; 387f4a2713aSLionel Sambuc 388f4a2713aSLionel Sambuc /// \brief thread_local variables defined or used in this TU. 389f4a2713aSLionel Sambuc std::vector<std::pair<const VarDecl *, llvm::GlobalVariable *> > 390f4a2713aSLionel Sambuc CXXThreadLocals; 391f4a2713aSLionel Sambuc 392f4a2713aSLionel Sambuc /// \brief thread_local variables with initializers that need to run 393f4a2713aSLionel Sambuc /// before any thread_local variable in this TU is odr-used. 394*0a6a1f1dSLionel Sambuc std::vector<llvm::Function *> CXXThreadLocalInits; 395*0a6a1f1dSLionel Sambuc std::vector<llvm::GlobalVariable *> CXXThreadLocalInitVars; 396f4a2713aSLionel Sambuc 397*0a6a1f1dSLionel Sambuc /// Global variables with initializers that need to run before main. 398*0a6a1f1dSLionel Sambuc std::vector<llvm::Function *> CXXGlobalInits; 399f4a2713aSLionel Sambuc 400f4a2713aSLionel Sambuc /// When a C++ decl with an initializer is deferred, null is 401f4a2713aSLionel Sambuc /// appended to CXXGlobalInits, and the index of that null is placed 402f4a2713aSLionel Sambuc /// here so that the initializer will be performed in the correct 403f4a2713aSLionel Sambuc /// order. 404f4a2713aSLionel Sambuc llvm::DenseMap<const Decl*, unsigned> DelayedCXXInitPosition; 405f4a2713aSLionel Sambuc 406f4a2713aSLionel Sambuc typedef std::pair<OrderGlobalInits, llvm::Function*> GlobalInitData; 407f4a2713aSLionel Sambuc 408f4a2713aSLionel Sambuc struct GlobalInitPriorityCmp { operatorGlobalInitPriorityCmp409f4a2713aSLionel Sambuc bool operator()(const GlobalInitData &LHS, 410f4a2713aSLionel Sambuc const GlobalInitData &RHS) const { 411f4a2713aSLionel Sambuc return LHS.first.priority < RHS.first.priority; 412f4a2713aSLionel Sambuc } 413f4a2713aSLionel Sambuc }; 414f4a2713aSLionel Sambuc 415*0a6a1f1dSLionel Sambuc /// Global variables with initializers whose order of initialization is set by 416*0a6a1f1dSLionel Sambuc /// init_priority attribute. 417f4a2713aSLionel Sambuc SmallVector<GlobalInitData, 8> PrioritizedCXXGlobalInits; 418f4a2713aSLionel Sambuc 419*0a6a1f1dSLionel Sambuc /// Global destructor functions and arguments that need to run on termination. 420f4a2713aSLionel Sambuc std::vector<std::pair<llvm::WeakVH,llvm::Constant*> > CXXGlobalDtors; 421f4a2713aSLionel Sambuc 422f4a2713aSLionel Sambuc /// \brief The complete set of modules that has been imported. 423f4a2713aSLionel Sambuc llvm::SetVector<clang::Module *> ImportedModules; 424f4a2713aSLionel Sambuc 425f4a2713aSLionel Sambuc /// \brief A vector of metadata strings. 426*0a6a1f1dSLionel Sambuc SmallVector<llvm::Metadata *, 16> LinkerOptionsMetadata; 427f4a2713aSLionel Sambuc 428f4a2713aSLionel Sambuc /// @name Cache for Objective-C runtime types 429f4a2713aSLionel Sambuc /// @{ 430f4a2713aSLionel Sambuc 431*0a6a1f1dSLionel Sambuc /// Cached reference to the class for constant strings. This value has type 432*0a6a1f1dSLionel Sambuc /// int * but is actually an Obj-C class pointer. 433f4a2713aSLionel Sambuc llvm::WeakVH CFConstantStringClassRef; 434f4a2713aSLionel Sambuc 435*0a6a1f1dSLionel Sambuc /// Cached reference to the class for constant strings. This value has type 436*0a6a1f1dSLionel Sambuc /// int * but is actually an Obj-C class pointer. 437f4a2713aSLionel Sambuc llvm::WeakVH ConstantStringClassRef; 438f4a2713aSLionel Sambuc 439f4a2713aSLionel Sambuc /// \brief The LLVM type corresponding to NSConstantString. 440f4a2713aSLionel Sambuc llvm::StructType *NSConstantStringType; 441f4a2713aSLionel Sambuc 442f4a2713aSLionel Sambuc /// \brief The type used to describe the state of a fast enumeration in 443f4a2713aSLionel Sambuc /// Objective-C's for..in loop. 444f4a2713aSLionel Sambuc QualType ObjCFastEnumerationStateType; 445f4a2713aSLionel Sambuc 446f4a2713aSLionel Sambuc /// @} 447f4a2713aSLionel Sambuc 448f4a2713aSLionel Sambuc /// Lazily create the Objective-C runtime 449f4a2713aSLionel Sambuc void createObjCRuntime(); 450f4a2713aSLionel Sambuc 451f4a2713aSLionel Sambuc void createOpenCLRuntime(); 452*0a6a1f1dSLionel Sambuc void createOpenMPRuntime(); 453f4a2713aSLionel Sambuc void createCUDARuntime(); 454f4a2713aSLionel Sambuc 455f4a2713aSLionel Sambuc bool isTriviallyRecursive(const FunctionDecl *F); 456f4a2713aSLionel Sambuc bool shouldEmitFunction(GlobalDecl GD); 457f4a2713aSLionel Sambuc 458f4a2713aSLionel Sambuc /// @name Cache for Blocks Runtime Globals 459f4a2713aSLionel Sambuc /// @{ 460f4a2713aSLionel Sambuc 461f4a2713aSLionel Sambuc llvm::Constant *NSConcreteGlobalBlock; 462f4a2713aSLionel Sambuc llvm::Constant *NSConcreteStackBlock; 463f4a2713aSLionel Sambuc 464f4a2713aSLionel Sambuc llvm::Constant *BlockObjectAssign; 465f4a2713aSLionel Sambuc llvm::Constant *BlockObjectDispose; 466f4a2713aSLionel Sambuc 467f4a2713aSLionel Sambuc llvm::Type *BlockDescriptorType; 468f4a2713aSLionel Sambuc llvm::Type *GenericBlockLiteralType; 469f4a2713aSLionel Sambuc 470f4a2713aSLionel Sambuc struct { 471f4a2713aSLionel Sambuc int GlobalUniqueCount; 472f4a2713aSLionel Sambuc } Block; 473f4a2713aSLionel Sambuc 474f4a2713aSLionel Sambuc /// void @llvm.lifetime.start(i64 %size, i8* nocapture <ptr>) 475f4a2713aSLionel Sambuc llvm::Constant *LifetimeStartFn; 476f4a2713aSLionel Sambuc 477f4a2713aSLionel Sambuc /// void @llvm.lifetime.end(i64 %size, i8* nocapture <ptr>) 478f4a2713aSLionel Sambuc llvm::Constant *LifetimeEndFn; 479f4a2713aSLionel Sambuc 480f4a2713aSLionel Sambuc GlobalDecl initializedGlobalDecl; 481f4a2713aSLionel Sambuc 482*0a6a1f1dSLionel Sambuc std::unique_ptr<SanitizerMetadata> SanitizerMD; 483f4a2713aSLionel Sambuc 484f4a2713aSLionel Sambuc /// @} 485*0a6a1f1dSLionel Sambuc 486*0a6a1f1dSLionel Sambuc llvm::DenseMap<const Decl *, bool> DeferredEmptyCoverageMappingDecls; 487*0a6a1f1dSLionel Sambuc 488*0a6a1f1dSLionel Sambuc std::unique_ptr<CoverageMappingModuleGen> CoverageMapping; 489f4a2713aSLionel Sambuc public: 490f4a2713aSLionel Sambuc CodeGenModule(ASTContext &C, const CodeGenOptions &CodeGenOpts, 491f4a2713aSLionel Sambuc llvm::Module &M, const llvm::DataLayout &TD, 492*0a6a1f1dSLionel Sambuc DiagnosticsEngine &Diags, 493*0a6a1f1dSLionel Sambuc CoverageSourceInfo *CoverageInfo = nullptr); 494f4a2713aSLionel Sambuc 495f4a2713aSLionel Sambuc ~CodeGenModule(); 496f4a2713aSLionel Sambuc 497*0a6a1f1dSLionel Sambuc void clear(); 498*0a6a1f1dSLionel Sambuc 499*0a6a1f1dSLionel Sambuc /// Finalize LLVM code generation. 500f4a2713aSLionel Sambuc void Release(); 501f4a2713aSLionel Sambuc 502*0a6a1f1dSLionel Sambuc /// Return a reference to the configured Objective-C runtime. getObjCRuntime()503f4a2713aSLionel Sambuc CGObjCRuntime &getObjCRuntime() { 504f4a2713aSLionel Sambuc if (!ObjCRuntime) createObjCRuntime(); 505f4a2713aSLionel Sambuc return *ObjCRuntime; 506f4a2713aSLionel Sambuc } 507f4a2713aSLionel Sambuc 508*0a6a1f1dSLionel Sambuc /// Return true iff an Objective-C runtime has been configured. hasObjCRuntime()509f4a2713aSLionel Sambuc bool hasObjCRuntime() { return !!ObjCRuntime; } 510f4a2713aSLionel Sambuc 511*0a6a1f1dSLionel Sambuc /// Return a reference to the configured OpenCL runtime. getOpenCLRuntime()512f4a2713aSLionel Sambuc CGOpenCLRuntime &getOpenCLRuntime() { 513*0a6a1f1dSLionel Sambuc assert(OpenCLRuntime != nullptr); 514f4a2713aSLionel Sambuc return *OpenCLRuntime; 515f4a2713aSLionel Sambuc } 516f4a2713aSLionel Sambuc 517*0a6a1f1dSLionel Sambuc /// Return a reference to the configured OpenMP runtime. getOpenMPRuntime()518*0a6a1f1dSLionel Sambuc CGOpenMPRuntime &getOpenMPRuntime() { 519*0a6a1f1dSLionel Sambuc assert(OpenMPRuntime != nullptr); 520*0a6a1f1dSLionel Sambuc return *OpenMPRuntime; 521*0a6a1f1dSLionel Sambuc } 522*0a6a1f1dSLionel Sambuc 523*0a6a1f1dSLionel Sambuc /// Return a reference to the configured CUDA runtime. getCUDARuntime()524f4a2713aSLionel Sambuc CGCUDARuntime &getCUDARuntime() { 525*0a6a1f1dSLionel Sambuc assert(CUDARuntime != nullptr); 526f4a2713aSLionel Sambuc return *CUDARuntime; 527f4a2713aSLionel Sambuc } 528f4a2713aSLionel Sambuc getARCEntrypoints()529f4a2713aSLionel Sambuc ARCEntrypoints &getARCEntrypoints() const { 530*0a6a1f1dSLionel Sambuc assert(getLangOpts().ObjCAutoRefCount && ARCData != nullptr); 531f4a2713aSLionel Sambuc return *ARCData; 532f4a2713aSLionel Sambuc } 533f4a2713aSLionel Sambuc getRREntrypoints()534f4a2713aSLionel Sambuc RREntrypoints &getRREntrypoints() const { 535*0a6a1f1dSLionel Sambuc assert(RRData != nullptr); 536f4a2713aSLionel Sambuc return *RRData; 537f4a2713aSLionel Sambuc } 538f4a2713aSLionel Sambuc getPGOStats()539*0a6a1f1dSLionel Sambuc InstrProfStats &getPGOStats() { return PGOStats; } getPGOReader()540*0a6a1f1dSLionel Sambuc llvm::IndexedInstrProfReader *getPGOReader() const { return PGOReader.get(); } 541*0a6a1f1dSLionel Sambuc getCoverageMapping()542*0a6a1f1dSLionel Sambuc CoverageMappingModuleGen *getCoverageMapping() const { 543*0a6a1f1dSLionel Sambuc return CoverageMapping.get(); 544*0a6a1f1dSLionel Sambuc } 545*0a6a1f1dSLionel Sambuc getStaticLocalDeclAddress(const VarDecl * D)546f4a2713aSLionel Sambuc llvm::Constant *getStaticLocalDeclAddress(const VarDecl *D) { 547f4a2713aSLionel Sambuc return StaticLocalDeclMap[D]; 548f4a2713aSLionel Sambuc } setStaticLocalDeclAddress(const VarDecl * D,llvm::Constant * C)549f4a2713aSLionel Sambuc void setStaticLocalDeclAddress(const VarDecl *D, 550f4a2713aSLionel Sambuc llvm::Constant *C) { 551f4a2713aSLionel Sambuc StaticLocalDeclMap[D] = C; 552f4a2713aSLionel Sambuc } 553f4a2713aSLionel Sambuc 554*0a6a1f1dSLionel Sambuc llvm::Constant * 555*0a6a1f1dSLionel Sambuc getOrCreateStaticVarDecl(const VarDecl &D, 556*0a6a1f1dSLionel Sambuc llvm::GlobalValue::LinkageTypes Linkage); 557*0a6a1f1dSLionel Sambuc getStaticLocalDeclGuardAddress(const VarDecl * D)558f4a2713aSLionel Sambuc llvm::GlobalVariable *getStaticLocalDeclGuardAddress(const VarDecl *D) { 559f4a2713aSLionel Sambuc return StaticLocalDeclGuardMap[D]; 560f4a2713aSLionel Sambuc } setStaticLocalDeclGuardAddress(const VarDecl * D,llvm::GlobalVariable * C)561f4a2713aSLionel Sambuc void setStaticLocalDeclGuardAddress(const VarDecl *D, 562f4a2713aSLionel Sambuc llvm::GlobalVariable *C) { 563f4a2713aSLionel Sambuc StaticLocalDeclGuardMap[D] = C; 564f4a2713aSLionel Sambuc } 565f4a2713aSLionel Sambuc 566*0a6a1f1dSLionel Sambuc bool lookupRepresentativeDecl(StringRef MangledName, 567*0a6a1f1dSLionel Sambuc GlobalDecl &Result) const; 568*0a6a1f1dSLionel Sambuc getAtomicSetterHelperFnMap(QualType Ty)569f4a2713aSLionel Sambuc llvm::Constant *getAtomicSetterHelperFnMap(QualType Ty) { 570f4a2713aSLionel Sambuc return AtomicSetterHelperFnMap[Ty]; 571f4a2713aSLionel Sambuc } setAtomicSetterHelperFnMap(QualType Ty,llvm::Constant * Fn)572f4a2713aSLionel Sambuc void setAtomicSetterHelperFnMap(QualType Ty, 573f4a2713aSLionel Sambuc llvm::Constant *Fn) { 574f4a2713aSLionel Sambuc AtomicSetterHelperFnMap[Ty] = Fn; 575f4a2713aSLionel Sambuc } 576f4a2713aSLionel Sambuc getAtomicGetterHelperFnMap(QualType Ty)577f4a2713aSLionel Sambuc llvm::Constant *getAtomicGetterHelperFnMap(QualType Ty) { 578f4a2713aSLionel Sambuc return AtomicGetterHelperFnMap[Ty]; 579f4a2713aSLionel Sambuc } setAtomicGetterHelperFnMap(QualType Ty,llvm::Constant * Fn)580f4a2713aSLionel Sambuc void setAtomicGetterHelperFnMap(QualType Ty, 581f4a2713aSLionel Sambuc llvm::Constant *Fn) { 582f4a2713aSLionel Sambuc AtomicGetterHelperFnMap[Ty] = Fn; 583f4a2713aSLionel Sambuc } 584f4a2713aSLionel Sambuc getTypeDescriptorFromMap(QualType Ty)585*0a6a1f1dSLionel Sambuc llvm::Constant *getTypeDescriptorFromMap(QualType Ty) { 586f4a2713aSLionel Sambuc return TypeDescriptorMap[Ty]; 587f4a2713aSLionel Sambuc } setTypeDescriptorInMap(QualType Ty,llvm::Constant * C)588*0a6a1f1dSLionel Sambuc void setTypeDescriptorInMap(QualType Ty, llvm::Constant *C) { 589f4a2713aSLionel Sambuc TypeDescriptorMap[Ty] = C; 590f4a2713aSLionel Sambuc } 591f4a2713aSLionel Sambuc getModuleDebugInfo()592f4a2713aSLionel Sambuc CGDebugInfo *getModuleDebugInfo() { return DebugInfo; } 593f4a2713aSLionel Sambuc getNoObjCARCExceptionsMetadata()594f4a2713aSLionel Sambuc llvm::MDNode *getNoObjCARCExceptionsMetadata() { 595f4a2713aSLionel Sambuc if (!NoObjCARCExceptionsMetadata) 596*0a6a1f1dSLionel Sambuc NoObjCARCExceptionsMetadata = llvm::MDNode::get(getLLVMContext(), None); 597f4a2713aSLionel Sambuc return NoObjCARCExceptionsMetadata; 598f4a2713aSLionel Sambuc } 599f4a2713aSLionel Sambuc getContext()600f4a2713aSLionel Sambuc ASTContext &getContext() const { return Context; } getLangOpts()601f4a2713aSLionel Sambuc const LangOptions &getLangOpts() const { return LangOpts; } getCodeGenOpts()602f4a2713aSLionel Sambuc const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; } getModule()603f4a2713aSLionel Sambuc llvm::Module &getModule() const { return TheModule; } getDiags()604f4a2713aSLionel Sambuc DiagnosticsEngine &getDiags() const { return Diags; } getDataLayout()605f4a2713aSLionel Sambuc const llvm::DataLayout &getDataLayout() const { return TheDataLayout; } getTarget()606f4a2713aSLionel Sambuc const TargetInfo &getTarget() const { return Target; } 607*0a6a1f1dSLionel Sambuc const llvm::Triple &getTriple() const; 608*0a6a1f1dSLionel Sambuc bool supportsCOMDAT() const; 609*0a6a1f1dSLionel Sambuc getCXXABI()610*0a6a1f1dSLionel Sambuc CGCXXABI &getCXXABI() const { return *ABI; } getLLVMContext()611f4a2713aSLionel Sambuc llvm::LLVMContext &getLLVMContext() { return VMContext; } 612f4a2713aSLionel Sambuc shouldUseTBAA()613*0a6a1f1dSLionel Sambuc bool shouldUseTBAA() const { return TBAA != nullptr; } 614f4a2713aSLionel Sambuc 615f4a2713aSLionel Sambuc const TargetCodeGenInfo &getTargetCodeGenInfo(); 616f4a2713aSLionel Sambuc getTypes()617f4a2713aSLionel Sambuc CodeGenTypes &getTypes() { return Types; } 618f4a2713aSLionel Sambuc getVTables()619f4a2713aSLionel Sambuc CodeGenVTables &getVTables() { return VTables; } 620f4a2713aSLionel Sambuc getItaniumVTableContext()621f4a2713aSLionel Sambuc ItaniumVTableContext &getItaniumVTableContext() { 622f4a2713aSLionel Sambuc return VTables.getItaniumVTableContext(); 623f4a2713aSLionel Sambuc } 624f4a2713aSLionel Sambuc getMicrosoftVTableContext()625f4a2713aSLionel Sambuc MicrosoftVTableContext &getMicrosoftVTableContext() { 626f4a2713aSLionel Sambuc return VTables.getMicrosoftVTableContext(); 627f4a2713aSLionel Sambuc } 628f4a2713aSLionel Sambuc getGlobalCtors()629*0a6a1f1dSLionel Sambuc CtorList &getGlobalCtors() { return GlobalCtors; } getGlobalDtors()630*0a6a1f1dSLionel Sambuc CtorList &getGlobalDtors() { return GlobalDtors; } 631*0a6a1f1dSLionel Sambuc 632f4a2713aSLionel Sambuc llvm::MDNode *getTBAAInfo(QualType QTy); 633f4a2713aSLionel Sambuc llvm::MDNode *getTBAAInfoForVTablePtr(); 634f4a2713aSLionel Sambuc llvm::MDNode *getTBAAStructInfo(QualType QTy); 635f4a2713aSLionel Sambuc /// Return the MDNode in the type DAG for the given struct type. 636f4a2713aSLionel Sambuc llvm::MDNode *getTBAAStructTypeInfo(QualType QTy); 637f4a2713aSLionel Sambuc /// Return the path-aware tag for given base type, access node and offset. 638f4a2713aSLionel Sambuc llvm::MDNode *getTBAAStructTagInfo(QualType BaseTy, llvm::MDNode *AccessN, 639f4a2713aSLionel Sambuc uint64_t O); 640f4a2713aSLionel Sambuc 641f4a2713aSLionel Sambuc bool isTypeConstant(QualType QTy, bool ExcludeCtorDtor); 642f4a2713aSLionel Sambuc 643f4a2713aSLionel Sambuc bool isPaddedAtomicType(QualType type); 644f4a2713aSLionel Sambuc bool isPaddedAtomicType(const AtomicType *type); 645f4a2713aSLionel Sambuc 646f4a2713aSLionel Sambuc /// Decorate the instruction with a TBAA tag. For scalar TBAA, the tag 647f4a2713aSLionel Sambuc /// is the same as the type. For struct-path aware TBAA, the tag 648f4a2713aSLionel Sambuc /// is different from the type: base type, access type and offset. 649f4a2713aSLionel Sambuc /// When ConvertTypeToTag is true, we create a tag based on the scalar type. 650f4a2713aSLionel Sambuc void DecorateInstruction(llvm::Instruction *Inst, 651f4a2713aSLionel Sambuc llvm::MDNode *TBAAInfo, 652f4a2713aSLionel Sambuc bool ConvertTypeToTag = true); 653f4a2713aSLionel Sambuc 654*0a6a1f1dSLionel Sambuc /// Emit the given number of characters as a value of type size_t. 655f4a2713aSLionel Sambuc llvm::ConstantInt *getSize(CharUnits numChars); 656f4a2713aSLionel Sambuc 657*0a6a1f1dSLionel Sambuc /// Set the visibility for the given LLVM GlobalValue. 658f4a2713aSLionel Sambuc void setGlobalVisibility(llvm::GlobalValue *GV, const NamedDecl *D) const; 659f4a2713aSLionel Sambuc 660*0a6a1f1dSLionel Sambuc /// Set the TLS mode for the given LLVM GlobalValue for the thread-local 661*0a6a1f1dSLionel Sambuc /// variable declaration D. 662*0a6a1f1dSLionel Sambuc void setTLSMode(llvm::GlobalValue *GV, const VarDecl &D) const; 663f4a2713aSLionel Sambuc GetLLVMVisibility(Visibility V)664f4a2713aSLionel Sambuc static llvm::GlobalValue::VisibilityTypes GetLLVMVisibility(Visibility V) { 665f4a2713aSLionel Sambuc switch (V) { 666f4a2713aSLionel Sambuc case DefaultVisibility: return llvm::GlobalValue::DefaultVisibility; 667f4a2713aSLionel Sambuc case HiddenVisibility: return llvm::GlobalValue::HiddenVisibility; 668f4a2713aSLionel Sambuc case ProtectedVisibility: return llvm::GlobalValue::ProtectedVisibility; 669f4a2713aSLionel Sambuc } 670f4a2713aSLionel Sambuc llvm_unreachable("unknown visibility!"); 671f4a2713aSLionel Sambuc } 672f4a2713aSLionel Sambuc GetAddrOfGlobal(GlobalDecl GD)673f4a2713aSLionel Sambuc llvm::Constant *GetAddrOfGlobal(GlobalDecl GD) { 674f4a2713aSLionel Sambuc if (isa<CXXConstructorDecl>(GD.getDecl())) 675*0a6a1f1dSLionel Sambuc return getAddrOfCXXStructor(cast<CXXConstructorDecl>(GD.getDecl()), 676*0a6a1f1dSLionel Sambuc getFromCtorType(GD.getCtorType())); 677f4a2713aSLionel Sambuc else if (isa<CXXDestructorDecl>(GD.getDecl())) 678*0a6a1f1dSLionel Sambuc return getAddrOfCXXStructor(cast<CXXDestructorDecl>(GD.getDecl()), 679*0a6a1f1dSLionel Sambuc getFromDtorType(GD.getDtorType())); 680f4a2713aSLionel Sambuc else if (isa<FunctionDecl>(GD.getDecl())) 681f4a2713aSLionel Sambuc return GetAddrOfFunction(GD); 682f4a2713aSLionel Sambuc else 683f4a2713aSLionel Sambuc return GetAddrOfGlobalVar(cast<VarDecl>(GD.getDecl())); 684f4a2713aSLionel Sambuc } 685f4a2713aSLionel Sambuc 686*0a6a1f1dSLionel Sambuc /// Will return a global variable of the given type. If a variable with a 687*0a6a1f1dSLionel Sambuc /// different type already exists then a new variable with the right type 688*0a6a1f1dSLionel Sambuc /// will be created and all uses of the old variable will be replaced with a 689*0a6a1f1dSLionel Sambuc /// bitcast to the new variable. 690f4a2713aSLionel Sambuc llvm::GlobalVariable * 691f4a2713aSLionel Sambuc CreateOrReplaceCXXRuntimeVariable(StringRef Name, llvm::Type *Ty, 692f4a2713aSLionel Sambuc llvm::GlobalValue::LinkageTypes Linkage); 693f4a2713aSLionel Sambuc 694*0a6a1f1dSLionel Sambuc llvm::Function * 695*0a6a1f1dSLionel Sambuc CreateGlobalInitOrDestructFunction(llvm::FunctionType *ty, const Twine &name, 696*0a6a1f1dSLionel Sambuc SourceLocation Loc = SourceLocation(), 697*0a6a1f1dSLionel Sambuc bool TLS = false); 698*0a6a1f1dSLionel Sambuc 699*0a6a1f1dSLionel Sambuc /// Return the address space of the underlying global variable for D, as 700*0a6a1f1dSLionel Sambuc /// determined by its declaration. Normally this is the same as the address 701*0a6a1f1dSLionel Sambuc /// space of D's type, but in CUDA, address spaces are associated with 702*0a6a1f1dSLionel Sambuc /// declarations, not types. 703f4a2713aSLionel Sambuc unsigned GetGlobalVarAddressSpace(const VarDecl *D, unsigned AddrSpace); 704f4a2713aSLionel Sambuc 705*0a6a1f1dSLionel Sambuc /// Return the llvm::Constant for the address of the given global variable. 706*0a6a1f1dSLionel Sambuc /// If Ty is non-null and if the global doesn't exist, then it will be greated 707*0a6a1f1dSLionel Sambuc /// with the specified type instead of whatever the normal requested type 708*0a6a1f1dSLionel Sambuc /// would be. 709f4a2713aSLionel Sambuc llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D, 710*0a6a1f1dSLionel Sambuc llvm::Type *Ty = nullptr); 711f4a2713aSLionel Sambuc 712*0a6a1f1dSLionel Sambuc /// Return the address of the given function. If Ty is non-null, then this 713*0a6a1f1dSLionel Sambuc /// function will use the specified type if it has to create it. 714*0a6a1f1dSLionel Sambuc llvm::Constant *GetAddrOfFunction(GlobalDecl GD, llvm::Type *Ty = 0, 715*0a6a1f1dSLionel Sambuc bool ForVTable = false, 716*0a6a1f1dSLionel Sambuc bool DontDefer = false); 717f4a2713aSLionel Sambuc 718*0a6a1f1dSLionel Sambuc /// Get the address of the RTTI descriptor for the given type. 719f4a2713aSLionel Sambuc llvm::Constant *GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH = false); 720f4a2713aSLionel Sambuc 721*0a6a1f1dSLionel Sambuc /// Get the address of a uuid descriptor . 722f4a2713aSLionel Sambuc llvm::Constant *GetAddrOfUuidDescriptor(const CXXUuidofExpr* E); 723f4a2713aSLionel Sambuc 724*0a6a1f1dSLionel Sambuc /// Get the address of the thunk for the given global decl. 725f4a2713aSLionel Sambuc llvm::Constant *GetAddrOfThunk(GlobalDecl GD, const ThunkInfo &Thunk); 726f4a2713aSLionel Sambuc 727*0a6a1f1dSLionel Sambuc /// Get a reference to the target of VD. 728f4a2713aSLionel Sambuc llvm::Constant *GetWeakRefReference(const ValueDecl *VD); 729f4a2713aSLionel Sambuc 730*0a6a1f1dSLionel Sambuc /// Returns the offset from a derived class to a class. Returns null if the 731*0a6a1f1dSLionel Sambuc /// offset is 0. 732f4a2713aSLionel Sambuc llvm::Constant * 733f4a2713aSLionel Sambuc GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl, 734f4a2713aSLionel Sambuc CastExpr::path_const_iterator PathBegin, 735f4a2713aSLionel Sambuc CastExpr::path_const_iterator PathEnd); 736f4a2713aSLionel Sambuc 737f4a2713aSLionel Sambuc /// A pair of helper functions for a __block variable. 738f4a2713aSLionel Sambuc class ByrefHelpers : public llvm::FoldingSetNode { 739f4a2713aSLionel Sambuc public: 740f4a2713aSLionel Sambuc llvm::Constant *CopyHelper; 741f4a2713aSLionel Sambuc llvm::Constant *DisposeHelper; 742f4a2713aSLionel Sambuc 743f4a2713aSLionel Sambuc /// The alignment of the field. This is important because 744f4a2713aSLionel Sambuc /// different offsets to the field within the byref struct need to 745f4a2713aSLionel Sambuc /// have different helper functions. 746f4a2713aSLionel Sambuc CharUnits Alignment; 747f4a2713aSLionel Sambuc ByrefHelpers(CharUnits alignment)748f4a2713aSLionel Sambuc ByrefHelpers(CharUnits alignment) : Alignment(alignment) {} 749f4a2713aSLionel Sambuc virtual ~ByrefHelpers(); 750f4a2713aSLionel Sambuc Profile(llvm::FoldingSetNodeID & id)751f4a2713aSLionel Sambuc void Profile(llvm::FoldingSetNodeID &id) const { 752f4a2713aSLionel Sambuc id.AddInteger(Alignment.getQuantity()); 753f4a2713aSLionel Sambuc profileImpl(id); 754f4a2713aSLionel Sambuc } 755f4a2713aSLionel Sambuc virtual void profileImpl(llvm::FoldingSetNodeID &id) const = 0; 756f4a2713aSLionel Sambuc needsCopy()757f4a2713aSLionel Sambuc virtual bool needsCopy() const { return true; } 758f4a2713aSLionel Sambuc virtual void emitCopy(CodeGenFunction &CGF, 759f4a2713aSLionel Sambuc llvm::Value *dest, llvm::Value *src) = 0; 760f4a2713aSLionel Sambuc needsDispose()761f4a2713aSLionel Sambuc virtual bool needsDispose() const { return true; } 762f4a2713aSLionel Sambuc virtual void emitDispose(CodeGenFunction &CGF, llvm::Value *field) = 0; 763f4a2713aSLionel Sambuc }; 764f4a2713aSLionel Sambuc 765f4a2713aSLionel Sambuc llvm::FoldingSet<ByrefHelpers> ByrefHelpersCache; 766f4a2713aSLionel Sambuc 767*0a6a1f1dSLionel Sambuc /// Fetches the global unique block count. getUniqueBlockCount()768f4a2713aSLionel Sambuc int getUniqueBlockCount() { return ++Block.GlobalUniqueCount; } 769f4a2713aSLionel Sambuc 770*0a6a1f1dSLionel Sambuc /// Fetches the type of a generic block descriptor. 771f4a2713aSLionel Sambuc llvm::Type *getBlockDescriptorType(); 772f4a2713aSLionel Sambuc 773*0a6a1f1dSLionel Sambuc /// The type of a generic block literal. 774f4a2713aSLionel Sambuc llvm::Type *getGenericBlockLiteralType(); 775f4a2713aSLionel Sambuc 776*0a6a1f1dSLionel Sambuc /// Gets the address of a block which requires no captures. 777f4a2713aSLionel Sambuc llvm::Constant *GetAddrOfGlobalBlock(const BlockExpr *BE, const char *); 778f4a2713aSLionel Sambuc 779*0a6a1f1dSLionel Sambuc /// Return a pointer to a constant CFString object for the given string. 780f4a2713aSLionel Sambuc llvm::Constant *GetAddrOfConstantCFString(const StringLiteral *Literal); 781f4a2713aSLionel Sambuc 782*0a6a1f1dSLionel Sambuc /// Return a pointer to a constant NSString object for the given string. Or a 783*0a6a1f1dSLionel Sambuc /// user defined String object as defined via 784f4a2713aSLionel Sambuc /// -fconstant-string-class=class_name option. 785f4a2713aSLionel Sambuc llvm::Constant *GetAddrOfConstantString(const StringLiteral *Literal); 786f4a2713aSLionel Sambuc 787*0a6a1f1dSLionel Sambuc /// Return a constant array for the given string. 788f4a2713aSLionel Sambuc llvm::Constant *GetConstantArrayFromStringLiteral(const StringLiteral *E); 789f4a2713aSLionel Sambuc 790*0a6a1f1dSLionel Sambuc /// Return a pointer to a constant array for the given string literal. 791*0a6a1f1dSLionel Sambuc llvm::GlobalVariable * 792*0a6a1f1dSLionel Sambuc GetAddrOfConstantStringFromLiteral(const StringLiteral *S, 793*0a6a1f1dSLionel Sambuc StringRef Name = ".str"); 794f4a2713aSLionel Sambuc 795*0a6a1f1dSLionel Sambuc /// Return a pointer to a constant array for the given ObjCEncodeExpr node. 796*0a6a1f1dSLionel Sambuc llvm::GlobalVariable * 797*0a6a1f1dSLionel Sambuc GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *); 798f4a2713aSLionel Sambuc 799*0a6a1f1dSLionel Sambuc /// Returns a pointer to a character array containing the literal and a 800*0a6a1f1dSLionel Sambuc /// terminating '\0' character. The result has pointer to array type. 801f4a2713aSLionel Sambuc /// 802f4a2713aSLionel Sambuc /// \param GlobalName If provided, the name to use for the global (if one is 803f4a2713aSLionel Sambuc /// created). 804*0a6a1f1dSLionel Sambuc llvm::GlobalVariable * 805*0a6a1f1dSLionel Sambuc GetAddrOfConstantCString(const std::string &Str, 806*0a6a1f1dSLionel Sambuc const char *GlobalName = nullptr, 807f4a2713aSLionel Sambuc unsigned Alignment = 0); 808f4a2713aSLionel Sambuc 809*0a6a1f1dSLionel Sambuc /// Returns a pointer to a constant global variable for the given file-scope 810*0a6a1f1dSLionel Sambuc /// compound literal expression. 811f4a2713aSLionel Sambuc llvm::Constant *GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr*E); 812f4a2713aSLionel Sambuc 813f4a2713aSLionel Sambuc /// \brief Returns a pointer to a global variable representing a temporary 814f4a2713aSLionel Sambuc /// with static or thread storage duration. 815f4a2713aSLionel Sambuc llvm::Constant *GetAddrOfGlobalTemporary(const MaterializeTemporaryExpr *E, 816f4a2713aSLionel Sambuc const Expr *Inner); 817f4a2713aSLionel Sambuc 818f4a2713aSLionel Sambuc /// \brief Retrieve the record type that describes the state of an 819f4a2713aSLionel Sambuc /// Objective-C fast enumeration loop (for..in). 820f4a2713aSLionel Sambuc QualType getObjCFastEnumerationStateType(); 821f4a2713aSLionel Sambuc 822*0a6a1f1dSLionel Sambuc // Produce code for this constructor/destructor. This method doesn't try 823*0a6a1f1dSLionel Sambuc // to apply any ABI rules about which other constructors/destructors 824*0a6a1f1dSLionel Sambuc // are needed or if they are alias to each other. 825*0a6a1f1dSLionel Sambuc llvm::Function *codegenCXXStructor(const CXXMethodDecl *MD, 826*0a6a1f1dSLionel Sambuc StructorType Type); 827f4a2713aSLionel Sambuc 828*0a6a1f1dSLionel Sambuc /// Return the address of the constructor/destructor of the given type. 829*0a6a1f1dSLionel Sambuc llvm::GlobalValue * 830*0a6a1f1dSLionel Sambuc getAddrOfCXXStructor(const CXXMethodDecl *MD, StructorType Type, 831*0a6a1f1dSLionel Sambuc const CGFunctionInfo *FnInfo = nullptr, 832*0a6a1f1dSLionel Sambuc llvm::FunctionType *FnType = nullptr, 833*0a6a1f1dSLionel Sambuc bool DontDefer = false); 834f4a2713aSLionel Sambuc 835*0a6a1f1dSLionel Sambuc /// Given a builtin id for a function like "__builtin_fabsf", return a 836*0a6a1f1dSLionel Sambuc /// Function* for "fabsf". 837f4a2713aSLionel Sambuc llvm::Value *getBuiltinLibFunction(const FunctionDecl *FD, 838f4a2713aSLionel Sambuc unsigned BuiltinID); 839f4a2713aSLionel Sambuc 840f4a2713aSLionel Sambuc llvm::Function *getIntrinsic(unsigned IID, ArrayRef<llvm::Type*> Tys = None); 841f4a2713aSLionel Sambuc 842*0a6a1f1dSLionel Sambuc /// Emit code for a single top level declaration. 843f4a2713aSLionel Sambuc void EmitTopLevelDecl(Decl *D); 844f4a2713aSLionel Sambuc 845*0a6a1f1dSLionel Sambuc /// \brief Stored a deferred empty coverage mapping for an unused 846*0a6a1f1dSLionel Sambuc /// and thus uninstrumented top level declaration. 847*0a6a1f1dSLionel Sambuc void AddDeferredUnusedCoverageMapping(Decl *D); 848*0a6a1f1dSLionel Sambuc 849*0a6a1f1dSLionel Sambuc /// \brief Remove the deferred empty coverage mapping as this 850*0a6a1f1dSLionel Sambuc /// declaration is actually instrumented. 851*0a6a1f1dSLionel Sambuc void ClearUnusedCoverageMapping(const Decl *D); 852*0a6a1f1dSLionel Sambuc 853*0a6a1f1dSLionel Sambuc /// \brief Emit all the deferred coverage mappings 854*0a6a1f1dSLionel Sambuc /// for the uninstrumented functions. 855*0a6a1f1dSLionel Sambuc void EmitDeferredUnusedCoverageMappings(); 856*0a6a1f1dSLionel Sambuc 857*0a6a1f1dSLionel Sambuc /// Tell the consumer that this variable has been instantiated. 858f4a2713aSLionel Sambuc void HandleCXXStaticMemberVarInstantiation(VarDecl *VD); 859f4a2713aSLionel Sambuc 860f4a2713aSLionel Sambuc /// \brief If the declaration has internal linkage but is inside an 861f4a2713aSLionel Sambuc /// extern "C" linkage specification, prepare to emit an alias for it 862f4a2713aSLionel Sambuc /// to the expected name. 863f4a2713aSLionel Sambuc template<typename SomeDecl> 864f4a2713aSLionel Sambuc void MaybeHandleStaticInExternC(const SomeDecl *D, llvm::GlobalValue *GV); 865f4a2713aSLionel Sambuc 866*0a6a1f1dSLionel Sambuc /// Add a global to a list to be added to the llvm.used metadata. 867*0a6a1f1dSLionel Sambuc void addUsedGlobal(llvm::GlobalValue *GV); 868f4a2713aSLionel Sambuc 869*0a6a1f1dSLionel Sambuc /// Add a global to a list to be added to the llvm.compiler.used metadata. 870*0a6a1f1dSLionel Sambuc void addCompilerUsedGlobal(llvm::GlobalValue *GV); 871*0a6a1f1dSLionel Sambuc 872*0a6a1f1dSLionel Sambuc /// Add a destructor and object to add to the C++ global destructor function. AddCXXDtorEntry(llvm::Constant * DtorFn,llvm::Constant * Object)873f4a2713aSLionel Sambuc void AddCXXDtorEntry(llvm::Constant *DtorFn, llvm::Constant *Object) { 874f4a2713aSLionel Sambuc CXXGlobalDtors.push_back(std::make_pair(DtorFn, Object)); 875f4a2713aSLionel Sambuc } 876f4a2713aSLionel Sambuc 877*0a6a1f1dSLionel Sambuc /// Create a new runtime function with the specified type and name. 878f4a2713aSLionel Sambuc llvm::Constant *CreateRuntimeFunction(llvm::FunctionType *Ty, 879f4a2713aSLionel Sambuc StringRef Name, 880f4a2713aSLionel Sambuc llvm::AttributeSet ExtraAttrs = 881f4a2713aSLionel Sambuc llvm::AttributeSet()); 882*0a6a1f1dSLionel Sambuc /// Create a new compiler builtin function with the specified type and name. 883*0a6a1f1dSLionel Sambuc llvm::Constant *CreateBuiltinFunction(llvm::FunctionType *Ty, 884*0a6a1f1dSLionel Sambuc StringRef Name, 885*0a6a1f1dSLionel Sambuc llvm::AttributeSet ExtraAttrs = 886*0a6a1f1dSLionel Sambuc llvm::AttributeSet()); 887*0a6a1f1dSLionel Sambuc /// Create a new runtime global variable with the specified type and name. 888f4a2713aSLionel Sambuc llvm::Constant *CreateRuntimeVariable(llvm::Type *Ty, 889f4a2713aSLionel Sambuc StringRef Name); 890f4a2713aSLionel Sambuc 891f4a2713aSLionel Sambuc ///@name Custom Blocks Runtime Interfaces 892f4a2713aSLionel Sambuc ///@{ 893f4a2713aSLionel Sambuc 894f4a2713aSLionel Sambuc llvm::Constant *getNSConcreteGlobalBlock(); 895f4a2713aSLionel Sambuc llvm::Constant *getNSConcreteStackBlock(); 896f4a2713aSLionel Sambuc llvm::Constant *getBlockObjectAssign(); 897f4a2713aSLionel Sambuc llvm::Constant *getBlockObjectDispose(); 898f4a2713aSLionel Sambuc 899f4a2713aSLionel Sambuc ///@} 900f4a2713aSLionel Sambuc 901f4a2713aSLionel Sambuc llvm::Constant *getLLVMLifetimeStartFn(); 902f4a2713aSLionel Sambuc llvm::Constant *getLLVMLifetimeEndFn(); 903f4a2713aSLionel Sambuc 904*0a6a1f1dSLionel Sambuc // Make sure that this type is translated. 905f4a2713aSLionel Sambuc void UpdateCompletedType(const TagDecl *TD); 906f4a2713aSLionel Sambuc 907f4a2713aSLionel Sambuc llvm::Constant *getMemberPointerConstant(const UnaryOperator *e); 908f4a2713aSLionel Sambuc 909*0a6a1f1dSLionel Sambuc /// Try to emit the initializer for the given declaration as a constant; 910*0a6a1f1dSLionel Sambuc /// returns 0 if the expression cannot be emitted as a constant. 911*0a6a1f1dSLionel Sambuc llvm::Constant *EmitConstantInit(const VarDecl &D, 912*0a6a1f1dSLionel Sambuc CodeGenFunction *CGF = nullptr); 913f4a2713aSLionel Sambuc 914*0a6a1f1dSLionel Sambuc /// Try to emit the given expression as a constant; returns 0 if the 915*0a6a1f1dSLionel Sambuc /// expression cannot be emitted as a constant. 916f4a2713aSLionel Sambuc llvm::Constant *EmitConstantExpr(const Expr *E, QualType DestType, 917*0a6a1f1dSLionel Sambuc CodeGenFunction *CGF = nullptr); 918f4a2713aSLionel Sambuc 919*0a6a1f1dSLionel Sambuc /// Emit the given constant value as a constant, in the type's scalar 920*0a6a1f1dSLionel Sambuc /// representation. 921f4a2713aSLionel Sambuc llvm::Constant *EmitConstantValue(const APValue &Value, QualType DestType, 922*0a6a1f1dSLionel Sambuc CodeGenFunction *CGF = nullptr); 923f4a2713aSLionel Sambuc 924*0a6a1f1dSLionel Sambuc /// Emit the given constant value as a constant, in the type's memory 925*0a6a1f1dSLionel Sambuc /// representation. 926f4a2713aSLionel Sambuc llvm::Constant *EmitConstantValueForMemory(const APValue &Value, 927f4a2713aSLionel Sambuc QualType DestType, 928*0a6a1f1dSLionel Sambuc CodeGenFunction *CGF = nullptr); 929f4a2713aSLionel Sambuc 930*0a6a1f1dSLionel Sambuc /// Return the result of value-initializing the given type, i.e. a null 931*0a6a1f1dSLionel Sambuc /// expression of the given type. This is usually, but not always, an LLVM 932*0a6a1f1dSLionel Sambuc /// null constant. 933f4a2713aSLionel Sambuc llvm::Constant *EmitNullConstant(QualType T); 934f4a2713aSLionel Sambuc 935*0a6a1f1dSLionel Sambuc /// Return a null constant appropriate for zero-initializing a base class with 936*0a6a1f1dSLionel Sambuc /// the given type. This is usually, but not always, an LLVM null constant. 937f4a2713aSLionel Sambuc llvm::Constant *EmitNullConstantForBase(const CXXRecordDecl *Record); 938f4a2713aSLionel Sambuc 939*0a6a1f1dSLionel Sambuc /// Emit a general error that something can't be done. 940f4a2713aSLionel Sambuc void Error(SourceLocation loc, StringRef error); 941f4a2713aSLionel Sambuc 942*0a6a1f1dSLionel Sambuc /// Print out an error that codegen doesn't support the specified stmt yet. 943f4a2713aSLionel Sambuc void ErrorUnsupported(const Stmt *S, const char *Type); 944f4a2713aSLionel Sambuc 945*0a6a1f1dSLionel Sambuc /// Print out an error that codegen doesn't support the specified decl yet. 946f4a2713aSLionel Sambuc void ErrorUnsupported(const Decl *D, const char *Type); 947f4a2713aSLionel Sambuc 948*0a6a1f1dSLionel Sambuc /// Set the attributes on the LLVM function for the given decl and function 949*0a6a1f1dSLionel Sambuc /// info. This applies attributes necessary for handling the ABI as well as 950*0a6a1f1dSLionel Sambuc /// user specified attributes like section. 951f4a2713aSLionel Sambuc void SetInternalFunctionAttributes(const Decl *D, llvm::Function *F, 952f4a2713aSLionel Sambuc const CGFunctionInfo &FI); 953f4a2713aSLionel Sambuc 954*0a6a1f1dSLionel Sambuc /// Set the LLVM function attributes (sext, zext, etc). 955f4a2713aSLionel Sambuc void SetLLVMFunctionAttributes(const Decl *D, 956f4a2713aSLionel Sambuc const CGFunctionInfo &Info, 957f4a2713aSLionel Sambuc llvm::Function *F); 958f4a2713aSLionel Sambuc 959*0a6a1f1dSLionel Sambuc /// Set the LLVM function attributes which only apply to a function 960*0a6a1f1dSLionel Sambuc /// definition. 961f4a2713aSLionel Sambuc void SetLLVMFunctionAttributesForDefinition(const Decl *D, llvm::Function *F); 962f4a2713aSLionel Sambuc 963*0a6a1f1dSLionel Sambuc /// Return true iff the given type uses 'sret' when used as a return type. 964f4a2713aSLionel Sambuc bool ReturnTypeUsesSRet(const CGFunctionInfo &FI); 965f4a2713aSLionel Sambuc 966*0a6a1f1dSLionel Sambuc /// Return true iff the given type uses an argument slot when 'sret' is used 967*0a6a1f1dSLionel Sambuc /// as a return type. 968*0a6a1f1dSLionel Sambuc bool ReturnSlotInterferesWithArgs(const CGFunctionInfo &FI); 969*0a6a1f1dSLionel Sambuc 970*0a6a1f1dSLionel Sambuc /// Return true iff the given type uses 'fpret' when used as a return type. 971f4a2713aSLionel Sambuc bool ReturnTypeUsesFPRet(QualType ResultType); 972f4a2713aSLionel Sambuc 973*0a6a1f1dSLionel Sambuc /// Return true iff the given type uses 'fp2ret' when used as a return type. 974f4a2713aSLionel Sambuc bool ReturnTypeUsesFP2Ret(QualType ResultType); 975f4a2713aSLionel Sambuc 976*0a6a1f1dSLionel Sambuc /// Get the LLVM attributes and calling convention to use for a particular 977*0a6a1f1dSLionel Sambuc /// function type. 978f4a2713aSLionel Sambuc /// 979f4a2713aSLionel Sambuc /// \param Info - The function type information. 980f4a2713aSLionel Sambuc /// \param TargetDecl - The decl these attributes are being constructed 981f4a2713aSLionel Sambuc /// for. If supplied the attributes applied to this decl may contribute to the 982f4a2713aSLionel Sambuc /// function attributes and calling convention. 983f4a2713aSLionel Sambuc /// \param PAL [out] - On return, the attribute list to use. 984f4a2713aSLionel Sambuc /// \param CallingConv [out] - On return, the LLVM calling convention to use. 985f4a2713aSLionel Sambuc void ConstructAttributeList(const CGFunctionInfo &Info, 986f4a2713aSLionel Sambuc const Decl *TargetDecl, 987f4a2713aSLionel Sambuc AttributeListType &PAL, 988f4a2713aSLionel Sambuc unsigned &CallingConv, 989f4a2713aSLionel Sambuc bool AttrOnCallSite); 990f4a2713aSLionel Sambuc 991f4a2713aSLionel Sambuc StringRef getMangledName(GlobalDecl GD); 992*0a6a1f1dSLionel Sambuc StringRef getBlockMangledName(GlobalDecl GD, const BlockDecl *BD); 993f4a2713aSLionel Sambuc 994f4a2713aSLionel Sambuc void EmitTentativeDefinition(const VarDecl *D); 995f4a2713aSLionel Sambuc 996f4a2713aSLionel Sambuc void EmitVTable(CXXRecordDecl *Class, bool DefinitionRequired); 997f4a2713aSLionel Sambuc 998*0a6a1f1dSLionel Sambuc /// Emit the RTTI descriptors for the builtin types. 999f4a2713aSLionel Sambuc void EmitFundamentalRTTIDescriptors(); 1000f4a2713aSLionel Sambuc 1001f4a2713aSLionel Sambuc /// \brief Appends Opts to the "Linker Options" metadata value. 1002f4a2713aSLionel Sambuc void AppendLinkerOptions(StringRef Opts); 1003f4a2713aSLionel Sambuc 1004f4a2713aSLionel Sambuc /// \brief Appends a detect mismatch command to the linker options. 1005f4a2713aSLionel Sambuc void AddDetectMismatch(StringRef Name, StringRef Value); 1006f4a2713aSLionel Sambuc 1007f4a2713aSLionel Sambuc /// \brief Appends a dependent lib to the "Linker Options" metadata value. 1008f4a2713aSLionel Sambuc void AddDependentLib(StringRef Lib); 1009f4a2713aSLionel Sambuc 1010f4a2713aSLionel Sambuc llvm::GlobalVariable::LinkageTypes getFunctionLinkage(GlobalDecl GD); 1011f4a2713aSLionel Sambuc setFunctionLinkage(GlobalDecl GD,llvm::Function * F)1012*0a6a1f1dSLionel Sambuc void setFunctionLinkage(GlobalDecl GD, llvm::Function *F) { 1013*0a6a1f1dSLionel Sambuc F->setLinkage(getFunctionLinkage(GD)); 1014f4a2713aSLionel Sambuc } 1015f4a2713aSLionel Sambuc 1016*0a6a1f1dSLionel Sambuc /// Return the appropriate linkage for the vtable, VTT, and type information 1017*0a6a1f1dSLionel Sambuc /// of the given class. 1018f4a2713aSLionel Sambuc llvm::GlobalVariable::LinkageTypes getVTableLinkage(const CXXRecordDecl *RD); 1019f4a2713aSLionel Sambuc 1020*0a6a1f1dSLionel Sambuc /// Return the store size, in character units, of the given LLVM type. 1021f4a2713aSLionel Sambuc CharUnits GetTargetTypeStoreSize(llvm::Type *Ty) const; 1022f4a2713aSLionel Sambuc 1023*0a6a1f1dSLionel Sambuc /// Returns LLVM linkage for a declarator. 1024f4a2713aSLionel Sambuc llvm::GlobalValue::LinkageTypes 1025*0a6a1f1dSLionel Sambuc getLLVMLinkageForDeclarator(const DeclaratorDecl *D, GVALinkage Linkage, 1026*0a6a1f1dSLionel Sambuc bool IsConstantVariable); 1027*0a6a1f1dSLionel Sambuc 1028*0a6a1f1dSLionel Sambuc /// Returns LLVM linkage for a declarator. 1029*0a6a1f1dSLionel Sambuc llvm::GlobalValue::LinkageTypes 1030*0a6a1f1dSLionel Sambuc getLLVMLinkageVarDefinition(const VarDecl *VD, bool IsConstant); 1031f4a2713aSLionel Sambuc 1032f4a2713aSLionel Sambuc /// Emit all the global annotations. 1033f4a2713aSLionel Sambuc void EmitGlobalAnnotations(); 1034f4a2713aSLionel Sambuc 1035f4a2713aSLionel Sambuc /// Emit an annotation string. 1036f4a2713aSLionel Sambuc llvm::Constant *EmitAnnotationString(StringRef Str); 1037f4a2713aSLionel Sambuc 1038f4a2713aSLionel Sambuc /// Emit the annotation's translation unit. 1039f4a2713aSLionel Sambuc llvm::Constant *EmitAnnotationUnit(SourceLocation Loc); 1040f4a2713aSLionel Sambuc 1041f4a2713aSLionel Sambuc /// Emit the annotation line number. 1042f4a2713aSLionel Sambuc llvm::Constant *EmitAnnotationLineNo(SourceLocation L); 1043f4a2713aSLionel Sambuc 1044*0a6a1f1dSLionel Sambuc /// Generate the llvm::ConstantStruct which contains the annotation 1045*0a6a1f1dSLionel Sambuc /// information for a given GlobalValue. The annotation struct is 1046f4a2713aSLionel Sambuc /// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the 1047f4a2713aSLionel Sambuc /// GlobalValue being annotated. The second field is the constant string 1048f4a2713aSLionel Sambuc /// created from the AnnotateAttr's annotation. The third field is a constant 1049f4a2713aSLionel Sambuc /// string containing the name of the translation unit. The fourth field is 1050f4a2713aSLionel Sambuc /// the line number in the file of the annotated value declaration. 1051f4a2713aSLionel Sambuc llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV, 1052f4a2713aSLionel Sambuc const AnnotateAttr *AA, 1053f4a2713aSLionel Sambuc SourceLocation L); 1054f4a2713aSLionel Sambuc 1055f4a2713aSLionel Sambuc /// Add global annotations that are set on D, for the global GV. Those 1056f4a2713aSLionel Sambuc /// annotations are emitted during finalization of the LLVM code. 1057f4a2713aSLionel Sambuc void AddGlobalAnnotations(const ValueDecl *D, llvm::GlobalValue *GV); 1058f4a2713aSLionel Sambuc 1059*0a6a1f1dSLionel Sambuc bool isInSanitizerBlacklist(llvm::Function *Fn, SourceLocation Loc) const; 1060f4a2713aSLionel Sambuc 1061*0a6a1f1dSLionel Sambuc bool isInSanitizerBlacklist(llvm::GlobalVariable *GV, SourceLocation Loc, 1062*0a6a1f1dSLionel Sambuc QualType Ty, 1063*0a6a1f1dSLionel Sambuc StringRef Category = StringRef()) const; 1064*0a6a1f1dSLionel Sambuc getSanitizerMetadata()1065*0a6a1f1dSLionel Sambuc SanitizerMetadata *getSanitizerMetadata() { 1066*0a6a1f1dSLionel Sambuc return SanitizerMD.get(); 1067*0a6a1f1dSLionel Sambuc } 1068f4a2713aSLionel Sambuc addDeferredVTable(const CXXRecordDecl * RD)1069f4a2713aSLionel Sambuc void addDeferredVTable(const CXXRecordDecl *RD) { 1070f4a2713aSLionel Sambuc DeferredVTables.push_back(RD); 1071f4a2713aSLionel Sambuc } 1072f4a2713aSLionel Sambuc 1073*0a6a1f1dSLionel Sambuc /// Emit code for a singal global function or var decl. Forward declarations 1074*0a6a1f1dSLionel Sambuc /// are emitted lazily. 1075f4a2713aSLionel Sambuc void EmitGlobal(GlobalDecl D); 1076f4a2713aSLionel Sambuc 1077*0a6a1f1dSLionel Sambuc bool TryEmitDefinitionAsAlias(GlobalDecl Alias, GlobalDecl Target, 1078*0a6a1f1dSLionel Sambuc bool InEveryTU); 1079*0a6a1f1dSLionel Sambuc bool TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D); 1080*0a6a1f1dSLionel Sambuc 1081*0a6a1f1dSLionel Sambuc /// Set attributes for a global definition. 1082*0a6a1f1dSLionel Sambuc void setFunctionDefinitionAttributes(const FunctionDecl *D, 1083*0a6a1f1dSLionel Sambuc llvm::Function *F); 1084*0a6a1f1dSLionel Sambuc 1085f4a2713aSLionel Sambuc llvm::GlobalValue *GetGlobalValue(StringRef Ref); 1086f4a2713aSLionel Sambuc 1087*0a6a1f1dSLionel Sambuc /// Set attributes which are common to any form of a global definition (alias, 1088*0a6a1f1dSLionel Sambuc /// Objective-C method, function, global variable). 1089f4a2713aSLionel Sambuc /// 1090f4a2713aSLionel Sambuc /// NOTE: This should only be called for definitions. 1091f4a2713aSLionel Sambuc void SetCommonAttributes(const Decl *D, llvm::GlobalValue *GV); 1092f4a2713aSLionel Sambuc 1093*0a6a1f1dSLionel Sambuc /// Set attributes which must be preserved by an alias. This includes common 1094*0a6a1f1dSLionel Sambuc /// attributes (i.e. it includes a call to SetCommonAttributes). 1095*0a6a1f1dSLionel Sambuc /// 1096*0a6a1f1dSLionel Sambuc /// NOTE: This should only be called for definitions. 1097*0a6a1f1dSLionel Sambuc void setAliasAttributes(const Decl *D, llvm::GlobalValue *GV); 1098f4a2713aSLionel Sambuc 1099*0a6a1f1dSLionel Sambuc void addReplacement(StringRef Name, llvm::Constant *C); 1100f4a2713aSLionel Sambuc 1101*0a6a1f1dSLionel Sambuc /// \brief Emit a code for threadprivate directive. 1102*0a6a1f1dSLionel Sambuc /// \param D Threadprivate declaration. 1103*0a6a1f1dSLionel Sambuc void EmitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D); 1104f4a2713aSLionel Sambuc 1105*0a6a1f1dSLionel Sambuc private: 1106*0a6a1f1dSLionel Sambuc llvm::Constant * 1107*0a6a1f1dSLionel Sambuc GetOrCreateLLVMFunction(StringRef MangledName, llvm::Type *Ty, GlobalDecl D, 1108*0a6a1f1dSLionel Sambuc bool ForVTable, bool DontDefer = false, 1109*0a6a1f1dSLionel Sambuc bool IsThunk = false, 1110*0a6a1f1dSLionel Sambuc llvm::AttributeSet ExtraAttrs = llvm::AttributeSet()); 1111*0a6a1f1dSLionel Sambuc 1112*0a6a1f1dSLionel Sambuc llvm::Constant *GetOrCreateLLVMGlobal(StringRef MangledName, 1113*0a6a1f1dSLionel Sambuc llvm::PointerType *PTy, 1114*0a6a1f1dSLionel Sambuc const VarDecl *D); 1115*0a6a1f1dSLionel Sambuc 1116*0a6a1f1dSLionel Sambuc void setNonAliasAttributes(const Decl *D, llvm::GlobalObject *GO); 1117*0a6a1f1dSLionel Sambuc 1118*0a6a1f1dSLionel Sambuc /// Set function attributes for a function declaration. 1119*0a6a1f1dSLionel Sambuc void SetFunctionAttributes(GlobalDecl GD, llvm::Function *F, 1120*0a6a1f1dSLionel Sambuc bool IsIncompleteFunction, bool IsThunk); 1121*0a6a1f1dSLionel Sambuc 1122*0a6a1f1dSLionel Sambuc void EmitGlobalDefinition(GlobalDecl D, llvm::GlobalValue *GV = nullptr); 1123*0a6a1f1dSLionel Sambuc 1124*0a6a1f1dSLionel Sambuc void EmitGlobalFunctionDefinition(GlobalDecl GD, llvm::GlobalValue *GV); 1125f4a2713aSLionel Sambuc void EmitGlobalVarDefinition(const VarDecl *D); 1126f4a2713aSLionel Sambuc void EmitAliasDefinition(GlobalDecl GD); 1127f4a2713aSLionel Sambuc void EmitObjCPropertyImplementations(const ObjCImplementationDecl *D); 1128f4a2713aSLionel Sambuc void EmitObjCIvarInitializations(ObjCImplementationDecl *D); 1129f4a2713aSLionel Sambuc 1130f4a2713aSLionel Sambuc // C++ related functions. 1131f4a2713aSLionel Sambuc 1132f4a2713aSLionel Sambuc void EmitNamespace(const NamespaceDecl *D); 1133f4a2713aSLionel Sambuc void EmitLinkageSpec(const LinkageSpecDecl *D); 1134f4a2713aSLionel Sambuc void CompleteDIClassType(const CXXMethodDecl* D); 1135f4a2713aSLionel Sambuc 1136f4a2713aSLionel Sambuc /// \brief Emit the function that initializes C++ thread_local variables. 1137f4a2713aSLionel Sambuc void EmitCXXThreadLocalInitFunc(); 1138f4a2713aSLionel Sambuc 1139*0a6a1f1dSLionel Sambuc /// Emit the function that initializes C++ globals. 1140f4a2713aSLionel Sambuc void EmitCXXGlobalInitFunc(); 1141f4a2713aSLionel Sambuc 1142*0a6a1f1dSLionel Sambuc /// Emit the function that destroys C++ globals. 1143f4a2713aSLionel Sambuc void EmitCXXGlobalDtorFunc(); 1144f4a2713aSLionel Sambuc 1145*0a6a1f1dSLionel Sambuc /// Emit the function that initializes the specified global (if PerformInit is 1146*0a6a1f1dSLionel Sambuc /// true) and registers its destructor. 1147f4a2713aSLionel Sambuc void EmitCXXGlobalVarDeclInitFunc(const VarDecl *D, 1148f4a2713aSLionel Sambuc llvm::GlobalVariable *Addr, 1149f4a2713aSLionel Sambuc bool PerformInit); 1150f4a2713aSLionel Sambuc 1151*0a6a1f1dSLionel Sambuc void EmitPointerToInitFunc(const VarDecl *VD, llvm::GlobalVariable *Addr, 1152*0a6a1f1dSLionel Sambuc llvm::Function *InitFunc, InitSegAttr *ISA); 1153*0a6a1f1dSLionel Sambuc 1154f4a2713aSLionel Sambuc // FIXME: Hardcoding priority here is gross. 1155*0a6a1f1dSLionel Sambuc void AddGlobalCtor(llvm::Function *Ctor, int Priority = 65535, 1156*0a6a1f1dSLionel Sambuc llvm::Constant *AssociatedData = 0); 1157f4a2713aSLionel Sambuc void AddGlobalDtor(llvm::Function *Dtor, int Priority = 65535); 1158f4a2713aSLionel Sambuc 1159*0a6a1f1dSLionel Sambuc /// Generates a global array of functions and priorities using the given list 1160*0a6a1f1dSLionel Sambuc /// and name. This array will have appending linkage and is suitable for use 1161*0a6a1f1dSLionel Sambuc /// as a LLVM constructor or destructor array. 1162f4a2713aSLionel Sambuc void EmitCtorList(const CtorList &Fns, const char *GlobalName); 1163f4a2713aSLionel Sambuc 1164*0a6a1f1dSLionel Sambuc /// Emit the RTTI descriptors for the given type. 1165f4a2713aSLionel Sambuc void EmitFundamentalRTTIDescriptor(QualType Type); 1166f4a2713aSLionel Sambuc 1167*0a6a1f1dSLionel Sambuc /// Emit any needed decls for which code generation was deferred. 1168f4a2713aSLionel Sambuc void EmitDeferred(); 1169f4a2713aSLionel Sambuc 1170f4a2713aSLionel Sambuc /// Call replaceAllUsesWith on all pairs in Replacements. 1171f4a2713aSLionel Sambuc void applyReplacements(); 1172f4a2713aSLionel Sambuc 1173f4a2713aSLionel Sambuc void checkAliases(); 1174f4a2713aSLionel Sambuc 1175*0a6a1f1dSLionel Sambuc /// Emit any vtables which we deferred and still have a use for. 1176f4a2713aSLionel Sambuc void EmitDeferredVTables(); 1177f4a2713aSLionel Sambuc 1178*0a6a1f1dSLionel Sambuc /// Emit the llvm.used and llvm.compiler.used metadata. 1179*0a6a1f1dSLionel Sambuc void emitLLVMUsed(); 1180f4a2713aSLionel Sambuc 1181f4a2713aSLionel Sambuc /// \brief Emit the link options introduced by imported modules. 1182f4a2713aSLionel Sambuc void EmitModuleLinkOptions(); 1183f4a2713aSLionel Sambuc 1184f4a2713aSLionel Sambuc /// \brief Emit aliases for internal-linkage declarations inside "C" language 1185f4a2713aSLionel Sambuc /// linkage specifications, giving them the "expected" name where possible. 1186f4a2713aSLionel Sambuc void EmitStaticExternCAliases(); 1187f4a2713aSLionel Sambuc 1188f4a2713aSLionel Sambuc void EmitDeclMetadata(); 1189f4a2713aSLionel Sambuc 1190f4a2713aSLionel Sambuc /// \brief Emit the Clang version as llvm.ident metadata. 1191f4a2713aSLionel Sambuc void EmitVersionIdentMetadata(); 1192f4a2713aSLionel Sambuc 1193*0a6a1f1dSLionel Sambuc /// Emits target specific Metadata for global declarations. 1194*0a6a1f1dSLionel Sambuc void EmitTargetMetadata(); 1195*0a6a1f1dSLionel Sambuc 1196*0a6a1f1dSLionel Sambuc /// Emit the llvm.gcov metadata used to tell LLVM where to emit the .gcno and 1197*0a6a1f1dSLionel Sambuc /// .gcda files in a way that persists in .bc files. 1198f4a2713aSLionel Sambuc void EmitCoverageFile(); 1199f4a2713aSLionel Sambuc 1200f4a2713aSLionel Sambuc /// Emits the initializer for a uuidof string. 1201*0a6a1f1dSLionel Sambuc llvm::Constant *EmitUuidofInitializer(StringRef uuidstr); 1202f4a2713aSLionel Sambuc 1203*0a6a1f1dSLionel Sambuc /// Determine whether the definition must be emitted; if this returns \c 1204*0a6a1f1dSLionel Sambuc /// false, the definition can be emitted lazily if it's used. 1205*0a6a1f1dSLionel Sambuc bool MustBeEmitted(const ValueDecl *D); 1206f4a2713aSLionel Sambuc 1207*0a6a1f1dSLionel Sambuc /// Determine whether the definition can be emitted eagerly, or should be 1208*0a6a1f1dSLionel Sambuc /// delayed until the end of the translation unit. This is relevant for 1209*0a6a1f1dSLionel Sambuc /// definitions whose linkage can change, e.g. implicit function instantions 1210*0a6a1f1dSLionel Sambuc /// which may later be explicitly instantiated. 1211*0a6a1f1dSLionel Sambuc bool MayBeEmittedEagerly(const ValueDecl *D); 1212*0a6a1f1dSLionel Sambuc 1213*0a6a1f1dSLionel Sambuc /// Check whether we can use a "simpler", more core exceptions personality 1214*0a6a1f1dSLionel Sambuc /// function. 1215f4a2713aSLionel Sambuc void SimplifyPersonality(); 1216f4a2713aSLionel Sambuc }; 1217f4a2713aSLionel Sambuc } // end namespace CodeGen 1218f4a2713aSLionel Sambuc } // end namespace clang 1219f4a2713aSLionel Sambuc 1220f4a2713aSLionel Sambuc #endif 1221