106f32e7eSjoerg //===--- CodeGenModule.h - Per-Module state for LLVM CodeGen ----*- C++ -*-===//
206f32e7eSjoerg //
306f32e7eSjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
406f32e7eSjoerg // See https://llvm.org/LICENSE.txt for license information.
506f32e7eSjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
606f32e7eSjoerg //
706f32e7eSjoerg //===----------------------------------------------------------------------===//
806f32e7eSjoerg //
906f32e7eSjoerg // This is the internal per-translation-unit state used for llvm translation.
1006f32e7eSjoerg //
1106f32e7eSjoerg //===----------------------------------------------------------------------===//
1206f32e7eSjoerg 
1306f32e7eSjoerg #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H
1406f32e7eSjoerg #define LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H
1506f32e7eSjoerg 
1606f32e7eSjoerg #include "CGVTables.h"
1706f32e7eSjoerg #include "CodeGenTypeCache.h"
1806f32e7eSjoerg #include "CodeGenTypes.h"
1906f32e7eSjoerg #include "SanitizerMetadata.h"
2006f32e7eSjoerg #include "clang/AST/DeclCXX.h"
2106f32e7eSjoerg #include "clang/AST/DeclObjC.h"
2206f32e7eSjoerg #include "clang/AST/DeclOpenMP.h"
2306f32e7eSjoerg #include "clang/AST/GlobalDecl.h"
2406f32e7eSjoerg #include "clang/AST/Mangle.h"
2506f32e7eSjoerg #include "clang/Basic/ABI.h"
2606f32e7eSjoerg #include "clang/Basic/LangOptions.h"
2706f32e7eSjoerg #include "clang/Basic/Module.h"
28*13fbcb42Sjoerg #include "clang/Basic/NoSanitizeList.h"
29*13fbcb42Sjoerg #include "clang/Basic/TargetInfo.h"
3006f32e7eSjoerg #include "clang/Basic/XRayLists.h"
31*13fbcb42Sjoerg #include "clang/Lex/PreprocessorOptions.h"
3206f32e7eSjoerg #include "llvm/ADT/DenseMap.h"
3306f32e7eSjoerg #include "llvm/ADT/SetVector.h"
3406f32e7eSjoerg #include "llvm/ADT/SmallPtrSet.h"
3506f32e7eSjoerg #include "llvm/ADT/StringMap.h"
3606f32e7eSjoerg #include "llvm/IR/Module.h"
3706f32e7eSjoerg #include "llvm/IR/ValueHandle.h"
3806f32e7eSjoerg #include "llvm/Transforms/Utils/SanitizerStats.h"
3906f32e7eSjoerg 
4006f32e7eSjoerg namespace llvm {
4106f32e7eSjoerg class Module;
4206f32e7eSjoerg class Constant;
4306f32e7eSjoerg class ConstantInt;
4406f32e7eSjoerg class Function;
4506f32e7eSjoerg class GlobalValue;
4606f32e7eSjoerg class DataLayout;
4706f32e7eSjoerg class FunctionType;
4806f32e7eSjoerg class LLVMContext;
49*13fbcb42Sjoerg class OpenMPIRBuilder;
5006f32e7eSjoerg class IndexedInstrProfReader;
5106f32e7eSjoerg }
5206f32e7eSjoerg 
5306f32e7eSjoerg namespace clang {
5406f32e7eSjoerg class ASTContext;
5506f32e7eSjoerg class AtomicType;
5606f32e7eSjoerg class FunctionDecl;
5706f32e7eSjoerg class IdentifierInfo;
5806f32e7eSjoerg class ObjCMethodDecl;
5906f32e7eSjoerg class ObjCImplementationDecl;
6006f32e7eSjoerg class ObjCCategoryImplDecl;
6106f32e7eSjoerg class ObjCProtocolDecl;
6206f32e7eSjoerg class ObjCEncodeExpr;
6306f32e7eSjoerg class BlockExpr;
6406f32e7eSjoerg class CharUnits;
6506f32e7eSjoerg class Decl;
6606f32e7eSjoerg class Expr;
6706f32e7eSjoerg class Stmt;
6806f32e7eSjoerg class InitListExpr;
6906f32e7eSjoerg class StringLiteral;
7006f32e7eSjoerg class NamedDecl;
7106f32e7eSjoerg class ValueDecl;
7206f32e7eSjoerg class VarDecl;
7306f32e7eSjoerg class LangOptions;
7406f32e7eSjoerg class CodeGenOptions;
7506f32e7eSjoerg class HeaderSearchOptions;
7606f32e7eSjoerg class DiagnosticsEngine;
7706f32e7eSjoerg class AnnotateAttr;
7806f32e7eSjoerg class CXXDestructorDecl;
7906f32e7eSjoerg class Module;
8006f32e7eSjoerg class CoverageSourceInfo;
81*13fbcb42Sjoerg class TargetAttr;
82*13fbcb42Sjoerg class InitSegAttr;
83*13fbcb42Sjoerg struct ParsedTargetAttr;
8406f32e7eSjoerg 
8506f32e7eSjoerg namespace CodeGen {
8606f32e7eSjoerg 
8706f32e7eSjoerg class CallArgList;
8806f32e7eSjoerg class CodeGenFunction;
8906f32e7eSjoerg class CodeGenTBAA;
9006f32e7eSjoerg class CGCXXABI;
9106f32e7eSjoerg class CGDebugInfo;
9206f32e7eSjoerg class CGObjCRuntime;
9306f32e7eSjoerg class CGOpenCLRuntime;
9406f32e7eSjoerg class CGOpenMPRuntime;
9506f32e7eSjoerg class CGCUDARuntime;
9606f32e7eSjoerg class BlockFieldFlags;
9706f32e7eSjoerg class FunctionArgList;
9806f32e7eSjoerg class CoverageMappingModuleGen;
9906f32e7eSjoerg class TargetCodeGenInfo;
10006f32e7eSjoerg 
10106f32e7eSjoerg enum ForDefinition_t : bool {
10206f32e7eSjoerg   NotForDefinition = false,
10306f32e7eSjoerg   ForDefinition = true
10406f32e7eSjoerg };
10506f32e7eSjoerg 
106*13fbcb42Sjoerg struct OrderGlobalInitsOrStermFinalizers {
10706f32e7eSjoerg   unsigned int priority;
10806f32e7eSjoerg   unsigned int lex_order;
OrderGlobalInitsOrStermFinalizersOrderGlobalInitsOrStermFinalizers109*13fbcb42Sjoerg   OrderGlobalInitsOrStermFinalizers(unsigned int p, unsigned int l)
11006f32e7eSjoerg       : priority(p), lex_order(l) {}
11106f32e7eSjoerg 
112*13fbcb42Sjoerg   bool operator==(const OrderGlobalInitsOrStermFinalizers &RHS) const {
11306f32e7eSjoerg     return priority == RHS.priority && lex_order == RHS.lex_order;
11406f32e7eSjoerg   }
11506f32e7eSjoerg 
116*13fbcb42Sjoerg   bool operator<(const OrderGlobalInitsOrStermFinalizers &RHS) const {
11706f32e7eSjoerg     return std::tie(priority, lex_order) <
11806f32e7eSjoerg            std::tie(RHS.priority, RHS.lex_order);
11906f32e7eSjoerg   }
12006f32e7eSjoerg };
12106f32e7eSjoerg 
12206f32e7eSjoerg struct ObjCEntrypoints {
ObjCEntrypointsObjCEntrypoints12306f32e7eSjoerg   ObjCEntrypoints() { memset(this, 0, sizeof(*this)); }
12406f32e7eSjoerg 
12506f32e7eSjoerg   /// void objc_alloc(id);
12606f32e7eSjoerg   llvm::FunctionCallee objc_alloc;
12706f32e7eSjoerg 
12806f32e7eSjoerg   /// void objc_allocWithZone(id);
12906f32e7eSjoerg   llvm::FunctionCallee objc_allocWithZone;
13006f32e7eSjoerg 
13106f32e7eSjoerg   /// void objc_alloc_init(id);
13206f32e7eSjoerg   llvm::FunctionCallee objc_alloc_init;
13306f32e7eSjoerg 
13406f32e7eSjoerg   /// void objc_autoreleasePoolPop(void*);
13506f32e7eSjoerg   llvm::FunctionCallee objc_autoreleasePoolPop;
13606f32e7eSjoerg 
13706f32e7eSjoerg   /// void objc_autoreleasePoolPop(void*);
13806f32e7eSjoerg   /// Note this method is used when we are using exception handling
13906f32e7eSjoerg   llvm::FunctionCallee objc_autoreleasePoolPopInvoke;
14006f32e7eSjoerg 
14106f32e7eSjoerg   /// void *objc_autoreleasePoolPush(void);
14206f32e7eSjoerg   llvm::Function *objc_autoreleasePoolPush;
14306f32e7eSjoerg 
14406f32e7eSjoerg   /// id objc_autorelease(id);
14506f32e7eSjoerg   llvm::Function *objc_autorelease;
14606f32e7eSjoerg 
14706f32e7eSjoerg   /// id objc_autorelease(id);
14806f32e7eSjoerg   /// Note this is the runtime method not the intrinsic.
14906f32e7eSjoerg   llvm::FunctionCallee objc_autoreleaseRuntimeFunction;
15006f32e7eSjoerg 
15106f32e7eSjoerg   /// id objc_autoreleaseReturnValue(id);
15206f32e7eSjoerg   llvm::Function *objc_autoreleaseReturnValue;
15306f32e7eSjoerg 
15406f32e7eSjoerg   /// void objc_copyWeak(id *dest, id *src);
15506f32e7eSjoerg   llvm::Function *objc_copyWeak;
15606f32e7eSjoerg 
15706f32e7eSjoerg   /// void objc_destroyWeak(id*);
15806f32e7eSjoerg   llvm::Function *objc_destroyWeak;
15906f32e7eSjoerg 
16006f32e7eSjoerg   /// id objc_initWeak(id*, id);
16106f32e7eSjoerg   llvm::Function *objc_initWeak;
16206f32e7eSjoerg 
16306f32e7eSjoerg   /// id objc_loadWeak(id*);
16406f32e7eSjoerg   llvm::Function *objc_loadWeak;
16506f32e7eSjoerg 
16606f32e7eSjoerg   /// id objc_loadWeakRetained(id*);
16706f32e7eSjoerg   llvm::Function *objc_loadWeakRetained;
16806f32e7eSjoerg 
16906f32e7eSjoerg   /// void objc_moveWeak(id *dest, id *src);
17006f32e7eSjoerg   llvm::Function *objc_moveWeak;
17106f32e7eSjoerg 
17206f32e7eSjoerg   /// id objc_retain(id);
17306f32e7eSjoerg   llvm::Function *objc_retain;
17406f32e7eSjoerg 
17506f32e7eSjoerg   /// id objc_retain(id);
17606f32e7eSjoerg   /// Note this is the runtime method not the intrinsic.
17706f32e7eSjoerg   llvm::FunctionCallee objc_retainRuntimeFunction;
17806f32e7eSjoerg 
17906f32e7eSjoerg   /// id objc_retainAutorelease(id);
18006f32e7eSjoerg   llvm::Function *objc_retainAutorelease;
18106f32e7eSjoerg 
18206f32e7eSjoerg   /// id objc_retainAutoreleaseReturnValue(id);
18306f32e7eSjoerg   llvm::Function *objc_retainAutoreleaseReturnValue;
18406f32e7eSjoerg 
18506f32e7eSjoerg   /// id objc_retainAutoreleasedReturnValue(id);
18606f32e7eSjoerg   llvm::Function *objc_retainAutoreleasedReturnValue;
18706f32e7eSjoerg 
18806f32e7eSjoerg   /// id objc_retainBlock(id);
18906f32e7eSjoerg   llvm::Function *objc_retainBlock;
19006f32e7eSjoerg 
19106f32e7eSjoerg   /// void objc_release(id);
19206f32e7eSjoerg   llvm::Function *objc_release;
19306f32e7eSjoerg 
19406f32e7eSjoerg   /// void objc_release(id);
19506f32e7eSjoerg   /// Note this is the runtime method not the intrinsic.
19606f32e7eSjoerg   llvm::FunctionCallee objc_releaseRuntimeFunction;
19706f32e7eSjoerg 
19806f32e7eSjoerg   /// void objc_storeStrong(id*, id);
19906f32e7eSjoerg   llvm::Function *objc_storeStrong;
20006f32e7eSjoerg 
20106f32e7eSjoerg   /// id objc_storeWeak(id*, id);
20206f32e7eSjoerg   llvm::Function *objc_storeWeak;
20306f32e7eSjoerg 
20406f32e7eSjoerg   /// id objc_unsafeClaimAutoreleasedReturnValue(id);
20506f32e7eSjoerg   llvm::Function *objc_unsafeClaimAutoreleasedReturnValue;
20606f32e7eSjoerg 
20706f32e7eSjoerg   /// A void(void) inline asm to use to mark that the return value of
20806f32e7eSjoerg   /// a call will be immediately retain.
20906f32e7eSjoerg   llvm::InlineAsm *retainAutoreleasedReturnValueMarker;
21006f32e7eSjoerg 
21106f32e7eSjoerg   /// void clang.arc.use(...);
21206f32e7eSjoerg   llvm::Function *clang_arc_use;
213*13fbcb42Sjoerg 
214*13fbcb42Sjoerg   /// void clang.arc.noop.use(...);
215*13fbcb42Sjoerg   llvm::Function *clang_arc_noop_use;
21606f32e7eSjoerg };
21706f32e7eSjoerg 
21806f32e7eSjoerg /// This class records statistics on instrumentation based profiling.
21906f32e7eSjoerg class InstrProfStats {
22006f32e7eSjoerg   uint32_t VisitedInMainFile;
22106f32e7eSjoerg   uint32_t MissingInMainFile;
22206f32e7eSjoerg   uint32_t Visited;
22306f32e7eSjoerg   uint32_t Missing;
22406f32e7eSjoerg   uint32_t Mismatched;
22506f32e7eSjoerg 
22606f32e7eSjoerg public:
InstrProfStats()22706f32e7eSjoerg   InstrProfStats()
22806f32e7eSjoerg       : VisitedInMainFile(0), MissingInMainFile(0), Visited(0), Missing(0),
22906f32e7eSjoerg         Mismatched(0) {}
23006f32e7eSjoerg   /// Record that we've visited a function and whether or not that function was
23106f32e7eSjoerg   /// in the main source file.
addVisited(bool MainFile)23206f32e7eSjoerg   void addVisited(bool MainFile) {
23306f32e7eSjoerg     if (MainFile)
23406f32e7eSjoerg       ++VisitedInMainFile;
23506f32e7eSjoerg     ++Visited;
23606f32e7eSjoerg   }
23706f32e7eSjoerg   /// Record that a function we've visited has no profile data.
addMissing(bool MainFile)23806f32e7eSjoerg   void addMissing(bool MainFile) {
23906f32e7eSjoerg     if (MainFile)
24006f32e7eSjoerg       ++MissingInMainFile;
24106f32e7eSjoerg     ++Missing;
24206f32e7eSjoerg   }
24306f32e7eSjoerg   /// Record that a function we've visited has mismatched profile data.
addMismatched(bool MainFile)24406f32e7eSjoerg   void addMismatched(bool MainFile) { ++Mismatched; }
24506f32e7eSjoerg   /// Whether or not the stats we've gathered indicate any potential problems.
hasDiagnostics()24606f32e7eSjoerg   bool hasDiagnostics() { return Missing || Mismatched; }
24706f32e7eSjoerg   /// Report potential problems we've found to \c Diags.
24806f32e7eSjoerg   void reportDiagnostics(DiagnosticsEngine &Diags, StringRef MainFile);
24906f32e7eSjoerg };
25006f32e7eSjoerg 
25106f32e7eSjoerg /// A pair of helper functions for a __block variable.
25206f32e7eSjoerg class BlockByrefHelpers : public llvm::FoldingSetNode {
25306f32e7eSjoerg   // MSVC requires this type to be complete in order to process this
25406f32e7eSjoerg   // header.
25506f32e7eSjoerg public:
25606f32e7eSjoerg   llvm::Constant *CopyHelper;
25706f32e7eSjoerg   llvm::Constant *DisposeHelper;
25806f32e7eSjoerg 
25906f32e7eSjoerg   /// The alignment of the field.  This is important because
26006f32e7eSjoerg   /// different offsets to the field within the byref struct need to
26106f32e7eSjoerg   /// have different helper functions.
26206f32e7eSjoerg   CharUnits Alignment;
26306f32e7eSjoerg 
BlockByrefHelpers(CharUnits alignment)26406f32e7eSjoerg   BlockByrefHelpers(CharUnits alignment)
26506f32e7eSjoerg       : CopyHelper(nullptr), DisposeHelper(nullptr), Alignment(alignment) {}
26606f32e7eSjoerg   BlockByrefHelpers(const BlockByrefHelpers &) = default;
26706f32e7eSjoerg   virtual ~BlockByrefHelpers();
26806f32e7eSjoerg 
Profile(llvm::FoldingSetNodeID & id)26906f32e7eSjoerg   void Profile(llvm::FoldingSetNodeID &id) const {
27006f32e7eSjoerg     id.AddInteger(Alignment.getQuantity());
27106f32e7eSjoerg     profileImpl(id);
27206f32e7eSjoerg   }
27306f32e7eSjoerg   virtual void profileImpl(llvm::FoldingSetNodeID &id) const = 0;
27406f32e7eSjoerg 
needsCopy()27506f32e7eSjoerg   virtual bool needsCopy() const { return true; }
27606f32e7eSjoerg   virtual void emitCopy(CodeGenFunction &CGF, Address dest, Address src) = 0;
27706f32e7eSjoerg 
needsDispose()27806f32e7eSjoerg   virtual bool needsDispose() const { return true; }
27906f32e7eSjoerg   virtual void emitDispose(CodeGenFunction &CGF, Address field) = 0;
28006f32e7eSjoerg };
28106f32e7eSjoerg 
28206f32e7eSjoerg /// This class organizes the cross-function state that is used while generating
28306f32e7eSjoerg /// LLVM code.
28406f32e7eSjoerg class CodeGenModule : public CodeGenTypeCache {
28506f32e7eSjoerg   CodeGenModule(const CodeGenModule &) = delete;
28606f32e7eSjoerg   void operator=(const CodeGenModule &) = delete;
28706f32e7eSjoerg 
28806f32e7eSjoerg public:
28906f32e7eSjoerg   struct Structor {
StructorStructor29006f32e7eSjoerg     Structor() : Priority(0), Initializer(nullptr), AssociatedData(nullptr) {}
StructorStructor29106f32e7eSjoerg     Structor(int Priority, llvm::Constant *Initializer,
29206f32e7eSjoerg              llvm::Constant *AssociatedData)
29306f32e7eSjoerg         : Priority(Priority), Initializer(Initializer),
29406f32e7eSjoerg           AssociatedData(AssociatedData) {}
29506f32e7eSjoerg     int Priority;
29606f32e7eSjoerg     llvm::Constant *Initializer;
29706f32e7eSjoerg     llvm::Constant *AssociatedData;
29806f32e7eSjoerg   };
29906f32e7eSjoerg 
30006f32e7eSjoerg   typedef std::vector<Structor> CtorList;
30106f32e7eSjoerg 
30206f32e7eSjoerg private:
30306f32e7eSjoerg   ASTContext &Context;
30406f32e7eSjoerg   const LangOptions &LangOpts;
30506f32e7eSjoerg   const HeaderSearchOptions &HeaderSearchOpts; // Only used for debug info.
30606f32e7eSjoerg   const PreprocessorOptions &PreprocessorOpts; // Only used for debug info.
30706f32e7eSjoerg   const CodeGenOptions &CodeGenOpts;
308*13fbcb42Sjoerg   unsigned NumAutoVarInit = 0;
30906f32e7eSjoerg   llvm::Module &TheModule;
31006f32e7eSjoerg   DiagnosticsEngine &Diags;
31106f32e7eSjoerg   const TargetInfo &Target;
31206f32e7eSjoerg   std::unique_ptr<CGCXXABI> ABI;
31306f32e7eSjoerg   llvm::LLVMContext &VMContext;
314*13fbcb42Sjoerg   std::string ModuleNameHash = "";
31506f32e7eSjoerg 
31606f32e7eSjoerg   std::unique_ptr<CodeGenTBAA> TBAA;
31706f32e7eSjoerg 
31806f32e7eSjoerg   mutable std::unique_ptr<TargetCodeGenInfo> TheTargetCodeGenInfo;
31906f32e7eSjoerg 
32006f32e7eSjoerg   // This should not be moved earlier, since its initialization depends on some
32106f32e7eSjoerg   // of the previous reference members being already initialized and also checks
32206f32e7eSjoerg   // if TheTargetCodeGenInfo is NULL
32306f32e7eSjoerg   CodeGenTypes Types;
32406f32e7eSjoerg 
32506f32e7eSjoerg   /// Holds information about C++ vtables.
32606f32e7eSjoerg   CodeGenVTables VTables;
32706f32e7eSjoerg 
32806f32e7eSjoerg   std::unique_ptr<CGObjCRuntime> ObjCRuntime;
32906f32e7eSjoerg   std::unique_ptr<CGOpenCLRuntime> OpenCLRuntime;
33006f32e7eSjoerg   std::unique_ptr<CGOpenMPRuntime> OpenMPRuntime;
33106f32e7eSjoerg   std::unique_ptr<CGCUDARuntime> CUDARuntime;
33206f32e7eSjoerg   std::unique_ptr<CGDebugInfo> DebugInfo;
33306f32e7eSjoerg   std::unique_ptr<ObjCEntrypoints> ObjCData;
33406f32e7eSjoerg   llvm::MDNode *NoObjCARCExceptionsMetadata = nullptr;
33506f32e7eSjoerg   std::unique_ptr<llvm::IndexedInstrProfReader> PGOReader;
33606f32e7eSjoerg   InstrProfStats PGOStats;
33706f32e7eSjoerg   std::unique_ptr<llvm::SanitizerStatReport> SanStats;
33806f32e7eSjoerg 
33906f32e7eSjoerg   // A set of references that have only been seen via a weakref so far. This is
34006f32e7eSjoerg   // used to remove the weak of the reference if we ever see a direct reference
34106f32e7eSjoerg   // or a definition.
34206f32e7eSjoerg   llvm::SmallPtrSet<llvm::GlobalValue*, 10> WeakRefReferences;
34306f32e7eSjoerg 
34406f32e7eSjoerg   /// This contains all the decls which have definitions but/ which are deferred
34506f32e7eSjoerg   /// for emission and therefore should only be output if they are actually
34606f32e7eSjoerg   /// used. If a decl is in this, then it is known to have not been referenced
34706f32e7eSjoerg   /// yet.
34806f32e7eSjoerg   std::map<StringRef, GlobalDecl> DeferredDecls;
34906f32e7eSjoerg 
35006f32e7eSjoerg   /// This is a list of deferred decls which we have seen that *are* actually
35106f32e7eSjoerg   /// referenced. These get code generated when the module is done.
35206f32e7eSjoerg   std::vector<GlobalDecl> DeferredDeclsToEmit;
addDeferredDeclToEmit(GlobalDecl GD)35306f32e7eSjoerg   void addDeferredDeclToEmit(GlobalDecl GD) {
35406f32e7eSjoerg     DeferredDeclsToEmit.emplace_back(GD);
35506f32e7eSjoerg   }
35606f32e7eSjoerg 
35706f32e7eSjoerg   /// List of alias we have emitted. Used to make sure that what they point to
35806f32e7eSjoerg   /// is defined once we get to the end of the of the translation unit.
35906f32e7eSjoerg   std::vector<GlobalDecl> Aliases;
36006f32e7eSjoerg 
36106f32e7eSjoerg   /// List of multiversion functions that have to be emitted.  Used to make sure
36206f32e7eSjoerg   /// we properly emit the iFunc.
36306f32e7eSjoerg   std::vector<GlobalDecl> MultiVersionFuncs;
36406f32e7eSjoerg 
36506f32e7eSjoerg   typedef llvm::StringMap<llvm::TrackingVH<llvm::Constant> > ReplacementsTy;
36606f32e7eSjoerg   ReplacementsTy Replacements;
36706f32e7eSjoerg 
36806f32e7eSjoerg   /// List of global values to be replaced with something else. Used when we
36906f32e7eSjoerg   /// want to replace a GlobalValue but can't identify it by its mangled name
37006f32e7eSjoerg   /// anymore (because the name is already taken).
37106f32e7eSjoerg   llvm::SmallVector<std::pair<llvm::GlobalValue *, llvm::Constant *>, 8>
37206f32e7eSjoerg     GlobalValReplacements;
37306f32e7eSjoerg 
37406f32e7eSjoerg   /// Variables for which we've emitted globals containing their constant
37506f32e7eSjoerg   /// values along with the corresponding globals, for opportunistic reuse.
37606f32e7eSjoerg   llvm::DenseMap<const VarDecl*, llvm::GlobalVariable*> InitializerConstants;
37706f32e7eSjoerg 
37806f32e7eSjoerg   /// Set of global decls for which we already diagnosed mangled name conflict.
37906f32e7eSjoerg   /// Required to not issue a warning (on a mangling conflict) multiple times
38006f32e7eSjoerg   /// for the same decl.
38106f32e7eSjoerg   llvm::DenseSet<GlobalDecl> DiagnosedConflictingDefinitions;
38206f32e7eSjoerg 
38306f32e7eSjoerg   /// A queue of (optional) vtables to consider emitting.
38406f32e7eSjoerg   std::vector<const CXXRecordDecl*> DeferredVTables;
38506f32e7eSjoerg 
38606f32e7eSjoerg   /// A queue of (optional) vtables that may be emitted opportunistically.
38706f32e7eSjoerg   std::vector<const CXXRecordDecl *> OpportunisticVTables;
38806f32e7eSjoerg 
38906f32e7eSjoerg   /// List of global values which are required to be present in the object file;
39006f32e7eSjoerg   /// bitcast to i8*. This is used for forcing visibility of symbols which may
39106f32e7eSjoerg   /// otherwise be optimized out.
39206f32e7eSjoerg   std::vector<llvm::WeakTrackingVH> LLVMUsed;
39306f32e7eSjoerg   std::vector<llvm::WeakTrackingVH> LLVMCompilerUsed;
39406f32e7eSjoerg 
39506f32e7eSjoerg   /// Store the list of global constructors and their respective priorities to
39606f32e7eSjoerg   /// be emitted when the translation unit is complete.
39706f32e7eSjoerg   CtorList GlobalCtors;
39806f32e7eSjoerg 
39906f32e7eSjoerg   /// Store the list of global destructors and their respective priorities to be
40006f32e7eSjoerg   /// emitted when the translation unit is complete.
40106f32e7eSjoerg   CtorList GlobalDtors;
40206f32e7eSjoerg 
40306f32e7eSjoerg   /// An ordered map of canonical GlobalDecls to their mangled names.
40406f32e7eSjoerg   llvm::MapVector<GlobalDecl, StringRef> MangledDeclNames;
40506f32e7eSjoerg   llvm::StringMap<GlobalDecl, llvm::BumpPtrAllocator> Manglings;
40606f32e7eSjoerg 
40706f32e7eSjoerg   // An ordered map of canonical GlobalDecls paired with the cpu-index for
40806f32e7eSjoerg   // cpu-specific name manglings.
40906f32e7eSjoerg   llvm::MapVector<std::pair<GlobalDecl, unsigned>, StringRef>
41006f32e7eSjoerg       CPUSpecificMangledDeclNames;
41106f32e7eSjoerg   llvm::StringMap<std::pair<GlobalDecl, unsigned>, llvm::BumpPtrAllocator>
41206f32e7eSjoerg       CPUSpecificManglings;
41306f32e7eSjoerg 
41406f32e7eSjoerg   /// Global annotations.
41506f32e7eSjoerg   std::vector<llvm::Constant*> Annotations;
41606f32e7eSjoerg 
41706f32e7eSjoerg   /// Map used to get unique annotation strings.
41806f32e7eSjoerg   llvm::StringMap<llvm::Constant*> AnnotationStrings;
41906f32e7eSjoerg 
420*13fbcb42Sjoerg   /// Used for uniquing of annotation arguments.
421*13fbcb42Sjoerg   llvm::DenseMap<unsigned, llvm::Constant *> AnnotationArgs;
422*13fbcb42Sjoerg 
42306f32e7eSjoerg   llvm::StringMap<llvm::GlobalVariable *> CFConstantStringMap;
42406f32e7eSjoerg 
42506f32e7eSjoerg   llvm::DenseMap<llvm::Constant *, llvm::GlobalVariable *> ConstantStringMap;
42606f32e7eSjoerg   llvm::DenseMap<const Decl*, llvm::Constant *> StaticLocalDeclMap;
42706f32e7eSjoerg   llvm::DenseMap<const Decl*, llvm::GlobalVariable*> StaticLocalDeclGuardMap;
42806f32e7eSjoerg   llvm::DenseMap<const Expr*, llvm::Constant *> MaterializedGlobalTemporaryMap;
42906f32e7eSjoerg 
43006f32e7eSjoerg   llvm::DenseMap<QualType, llvm::Constant *> AtomicSetterHelperFnMap;
43106f32e7eSjoerg   llvm::DenseMap<QualType, llvm::Constant *> AtomicGetterHelperFnMap;
43206f32e7eSjoerg 
43306f32e7eSjoerg   /// Map used to get unique type descriptor constants for sanitizers.
43406f32e7eSjoerg   llvm::DenseMap<QualType, llvm::Constant *> TypeDescriptorMap;
43506f32e7eSjoerg 
43606f32e7eSjoerg   /// Map used to track internal linkage functions declared within
43706f32e7eSjoerg   /// extern "C" regions.
43806f32e7eSjoerg   typedef llvm::MapVector<IdentifierInfo *,
43906f32e7eSjoerg                           llvm::GlobalValue *> StaticExternCMap;
44006f32e7eSjoerg   StaticExternCMap StaticExternCValues;
44106f32e7eSjoerg 
44206f32e7eSjoerg   /// thread_local variables defined or used in this TU.
44306f32e7eSjoerg   std::vector<const VarDecl *> CXXThreadLocals;
44406f32e7eSjoerg 
44506f32e7eSjoerg   /// thread_local variables with initializers that need to run
44606f32e7eSjoerg   /// before any thread_local variable in this TU is odr-used.
44706f32e7eSjoerg   std::vector<llvm::Function *> CXXThreadLocalInits;
44806f32e7eSjoerg   std::vector<const VarDecl *> CXXThreadLocalInitVars;
44906f32e7eSjoerg 
45006f32e7eSjoerg   /// Global variables with initializers that need to run before main.
45106f32e7eSjoerg   std::vector<llvm::Function *> CXXGlobalInits;
45206f32e7eSjoerg 
45306f32e7eSjoerg   /// When a C++ decl with an initializer is deferred, null is
45406f32e7eSjoerg   /// appended to CXXGlobalInits, and the index of that null is placed
45506f32e7eSjoerg   /// here so that the initializer will be performed in the correct
45606f32e7eSjoerg   /// order. Once the decl is emitted, the index is replaced with ~0U to ensure
45706f32e7eSjoerg   /// that we don't re-emit the initializer.
45806f32e7eSjoerg   llvm::DenseMap<const Decl*, unsigned> DelayedCXXInitPosition;
45906f32e7eSjoerg 
460*13fbcb42Sjoerg   typedef std::pair<OrderGlobalInitsOrStermFinalizers, llvm::Function *>
461*13fbcb42Sjoerg       GlobalInitData;
46206f32e7eSjoerg 
46306f32e7eSjoerg   struct GlobalInitPriorityCmp {
operatorGlobalInitPriorityCmp46406f32e7eSjoerg     bool operator()(const GlobalInitData &LHS,
46506f32e7eSjoerg                     const GlobalInitData &RHS) const {
46606f32e7eSjoerg       return LHS.first.priority < RHS.first.priority;
46706f32e7eSjoerg     }
46806f32e7eSjoerg   };
46906f32e7eSjoerg 
47006f32e7eSjoerg   /// Global variables with initializers whose order of initialization is set by
47106f32e7eSjoerg   /// init_priority attribute.
47206f32e7eSjoerg   SmallVector<GlobalInitData, 8> PrioritizedCXXGlobalInits;
47306f32e7eSjoerg 
47406f32e7eSjoerg   /// Global destructor functions and arguments that need to run on termination.
475*13fbcb42Sjoerg   /// When UseSinitAndSterm is set, it instead contains sterm finalizer
476*13fbcb42Sjoerg   /// functions, which also run on unloading a shared library.
477*13fbcb42Sjoerg   typedef std::tuple<llvm::FunctionType *, llvm::WeakTrackingVH,
478*13fbcb42Sjoerg                      llvm::Constant *>
479*13fbcb42Sjoerg       CXXGlobalDtorsOrStermFinalizer_t;
480*13fbcb42Sjoerg   SmallVector<CXXGlobalDtorsOrStermFinalizer_t, 8>
481*13fbcb42Sjoerg       CXXGlobalDtorsOrStermFinalizers;
482*13fbcb42Sjoerg 
483*13fbcb42Sjoerg   typedef std::pair<OrderGlobalInitsOrStermFinalizers, llvm::Function *>
484*13fbcb42Sjoerg       StermFinalizerData;
485*13fbcb42Sjoerg 
486*13fbcb42Sjoerg   struct StermFinalizerPriorityCmp {
operatorStermFinalizerPriorityCmp487*13fbcb42Sjoerg     bool operator()(const StermFinalizerData &LHS,
488*13fbcb42Sjoerg                     const StermFinalizerData &RHS) const {
489*13fbcb42Sjoerg       return LHS.first.priority < RHS.first.priority;
490*13fbcb42Sjoerg     }
491*13fbcb42Sjoerg   };
492*13fbcb42Sjoerg 
493*13fbcb42Sjoerg   /// Global variables with sterm finalizers whose order of initialization is
494*13fbcb42Sjoerg   /// set by init_priority attribute.
495*13fbcb42Sjoerg   SmallVector<StermFinalizerData, 8> PrioritizedCXXStermFinalizers;
49606f32e7eSjoerg 
49706f32e7eSjoerg   /// The complete set of modules that has been imported.
49806f32e7eSjoerg   llvm::SetVector<clang::Module *> ImportedModules;
49906f32e7eSjoerg 
50006f32e7eSjoerg   /// The set of modules for which the module initializers
50106f32e7eSjoerg   /// have been emitted.
50206f32e7eSjoerg   llvm::SmallPtrSet<clang::Module *, 16> EmittedModuleInitializers;
50306f32e7eSjoerg 
50406f32e7eSjoerg   /// A vector of metadata strings for linker options.
50506f32e7eSjoerg   SmallVector<llvm::MDNode *, 16> LinkerOptionsMetadata;
50606f32e7eSjoerg 
50706f32e7eSjoerg   /// A vector of metadata strings for dependent libraries for ELF.
50806f32e7eSjoerg   SmallVector<llvm::MDNode *, 16> ELFDependentLibraries;
50906f32e7eSjoerg 
51006f32e7eSjoerg   /// @name Cache for Objective-C runtime types
51106f32e7eSjoerg   /// @{
51206f32e7eSjoerg 
51306f32e7eSjoerg   /// Cached reference to the class for constant strings. This value has type
51406f32e7eSjoerg   /// int * but is actually an Obj-C class pointer.
51506f32e7eSjoerg   llvm::WeakTrackingVH CFConstantStringClassRef;
51606f32e7eSjoerg 
51706f32e7eSjoerg   /// The type used to describe the state of a fast enumeration in
51806f32e7eSjoerg   /// Objective-C's for..in loop.
51906f32e7eSjoerg   QualType ObjCFastEnumerationStateType;
52006f32e7eSjoerg 
52106f32e7eSjoerg   /// @}
52206f32e7eSjoerg 
52306f32e7eSjoerg   /// Lazily create the Objective-C runtime
52406f32e7eSjoerg   void createObjCRuntime();
52506f32e7eSjoerg 
52606f32e7eSjoerg   void createOpenCLRuntime();
52706f32e7eSjoerg   void createOpenMPRuntime();
52806f32e7eSjoerg   void createCUDARuntime();
52906f32e7eSjoerg 
53006f32e7eSjoerg   bool isTriviallyRecursive(const FunctionDecl *F);
53106f32e7eSjoerg   bool shouldEmitFunction(GlobalDecl GD);
53206f32e7eSjoerg   bool shouldOpportunisticallyEmitVTables();
53306f32e7eSjoerg   /// Map used to be sure we don't emit the same CompoundLiteral twice.
53406f32e7eSjoerg   llvm::DenseMap<const CompoundLiteralExpr *, llvm::GlobalVariable *>
53506f32e7eSjoerg       EmittedCompoundLiterals;
53606f32e7eSjoerg 
53706f32e7eSjoerg   /// Map of the global blocks we've emitted, so that we don't have to re-emit
53806f32e7eSjoerg   /// them if the constexpr evaluator gets aggressive.
53906f32e7eSjoerg   llvm::DenseMap<const BlockExpr *, llvm::Constant *> EmittedGlobalBlocks;
54006f32e7eSjoerg 
54106f32e7eSjoerg   /// @name Cache for Blocks Runtime Globals
54206f32e7eSjoerg   /// @{
54306f32e7eSjoerg 
54406f32e7eSjoerg   llvm::Constant *NSConcreteGlobalBlock = nullptr;
54506f32e7eSjoerg   llvm::Constant *NSConcreteStackBlock = nullptr;
54606f32e7eSjoerg 
54706f32e7eSjoerg   llvm::FunctionCallee BlockObjectAssign = nullptr;
54806f32e7eSjoerg   llvm::FunctionCallee BlockObjectDispose = nullptr;
54906f32e7eSjoerg 
55006f32e7eSjoerg   llvm::Type *BlockDescriptorType = nullptr;
55106f32e7eSjoerg   llvm::Type *GenericBlockLiteralType = nullptr;
55206f32e7eSjoerg 
55306f32e7eSjoerg   struct {
55406f32e7eSjoerg     int GlobalUniqueCount;
55506f32e7eSjoerg   } Block;
55606f32e7eSjoerg 
557*13fbcb42Sjoerg   GlobalDecl initializedGlobalDecl;
558*13fbcb42Sjoerg 
559*13fbcb42Sjoerg   /// @}
560*13fbcb42Sjoerg 
56106f32e7eSjoerg   /// void @llvm.lifetime.start(i64 %size, i8* nocapture <ptr>)
56206f32e7eSjoerg   llvm::Function *LifetimeStartFn = nullptr;
56306f32e7eSjoerg 
56406f32e7eSjoerg   /// void @llvm.lifetime.end(i64 %size, i8* nocapture <ptr>)
56506f32e7eSjoerg   llvm::Function *LifetimeEndFn = nullptr;
56606f32e7eSjoerg 
56706f32e7eSjoerg   std::unique_ptr<SanitizerMetadata> SanitizerMD;
56806f32e7eSjoerg 
56906f32e7eSjoerg   llvm::MapVector<const Decl *, bool> DeferredEmptyCoverageMappingDecls;
57006f32e7eSjoerg 
57106f32e7eSjoerg   std::unique_ptr<CoverageMappingModuleGen> CoverageMapping;
57206f32e7eSjoerg 
57306f32e7eSjoerg   /// Mapping from canonical types to their metadata identifiers. We need to
57406f32e7eSjoerg   /// maintain this mapping because identifiers may be formed from distinct
57506f32e7eSjoerg   /// MDNodes.
57606f32e7eSjoerg   typedef llvm::DenseMap<QualType, llvm::Metadata *> MetadataTypeMap;
57706f32e7eSjoerg   MetadataTypeMap MetadataIdMap;
57806f32e7eSjoerg   MetadataTypeMap VirtualMetadataIdMap;
57906f32e7eSjoerg   MetadataTypeMap GeneralizedMetadataIdMap;
58006f32e7eSjoerg 
58106f32e7eSjoerg public:
58206f32e7eSjoerg   CodeGenModule(ASTContext &C, const HeaderSearchOptions &headersearchopts,
58306f32e7eSjoerg                 const PreprocessorOptions &ppopts,
58406f32e7eSjoerg                 const CodeGenOptions &CodeGenOpts, llvm::Module &M,
58506f32e7eSjoerg                 DiagnosticsEngine &Diags,
58606f32e7eSjoerg                 CoverageSourceInfo *CoverageInfo = nullptr);
58706f32e7eSjoerg 
58806f32e7eSjoerg   ~CodeGenModule();
58906f32e7eSjoerg 
59006f32e7eSjoerg   void clear();
59106f32e7eSjoerg 
59206f32e7eSjoerg   /// Finalize LLVM code generation.
59306f32e7eSjoerg   void Release();
59406f32e7eSjoerg 
59506f32e7eSjoerg   /// Return true if we should emit location information for expressions.
59606f32e7eSjoerg   bool getExpressionLocationsEnabled() const;
59706f32e7eSjoerg 
59806f32e7eSjoerg   /// Return a reference to the configured Objective-C runtime.
getObjCRuntime()59906f32e7eSjoerg   CGObjCRuntime &getObjCRuntime() {
60006f32e7eSjoerg     if (!ObjCRuntime) createObjCRuntime();
60106f32e7eSjoerg     return *ObjCRuntime;
60206f32e7eSjoerg   }
60306f32e7eSjoerg 
60406f32e7eSjoerg   /// Return true iff an Objective-C runtime has been configured.
hasObjCRuntime()60506f32e7eSjoerg   bool hasObjCRuntime() { return !!ObjCRuntime; }
60606f32e7eSjoerg 
getModuleNameHash()607*13fbcb42Sjoerg   const std::string &getModuleNameHash() const { return ModuleNameHash; }
608*13fbcb42Sjoerg 
60906f32e7eSjoerg   /// Return a reference to the configured OpenCL runtime.
getOpenCLRuntime()61006f32e7eSjoerg   CGOpenCLRuntime &getOpenCLRuntime() {
61106f32e7eSjoerg     assert(OpenCLRuntime != nullptr);
61206f32e7eSjoerg     return *OpenCLRuntime;
61306f32e7eSjoerg   }
61406f32e7eSjoerg 
61506f32e7eSjoerg   /// Return a reference to the configured OpenMP runtime.
getOpenMPRuntime()61606f32e7eSjoerg   CGOpenMPRuntime &getOpenMPRuntime() {
61706f32e7eSjoerg     assert(OpenMPRuntime != nullptr);
61806f32e7eSjoerg     return *OpenMPRuntime;
61906f32e7eSjoerg   }
62006f32e7eSjoerg 
62106f32e7eSjoerg   /// Return a reference to the configured CUDA runtime.
getCUDARuntime()62206f32e7eSjoerg   CGCUDARuntime &getCUDARuntime() {
62306f32e7eSjoerg     assert(CUDARuntime != nullptr);
62406f32e7eSjoerg     return *CUDARuntime;
62506f32e7eSjoerg   }
62606f32e7eSjoerg 
getObjCEntrypoints()62706f32e7eSjoerg   ObjCEntrypoints &getObjCEntrypoints() const {
62806f32e7eSjoerg     assert(ObjCData != nullptr);
62906f32e7eSjoerg     return *ObjCData;
63006f32e7eSjoerg   }
63106f32e7eSjoerg 
632*13fbcb42Sjoerg   // Version checking functions, used to implement ObjC's @available:
63306f32e7eSjoerg   // i32 @__isOSVersionAtLeast(i32, i32, i32)
63406f32e7eSjoerg   llvm::FunctionCallee IsOSVersionAtLeastFn = nullptr;
635*13fbcb42Sjoerg   // i32 @__isPlatformVersionAtLeast(i32, i32, i32, i32)
636*13fbcb42Sjoerg   llvm::FunctionCallee IsPlatformVersionAtLeastFn = nullptr;
63706f32e7eSjoerg 
getPGOStats()63806f32e7eSjoerg   InstrProfStats &getPGOStats() { return PGOStats; }
getPGOReader()63906f32e7eSjoerg   llvm::IndexedInstrProfReader *getPGOReader() const { return PGOReader.get(); }
64006f32e7eSjoerg 
getCoverageMapping()64106f32e7eSjoerg   CoverageMappingModuleGen *getCoverageMapping() const {
64206f32e7eSjoerg     return CoverageMapping.get();
64306f32e7eSjoerg   }
64406f32e7eSjoerg 
getStaticLocalDeclAddress(const VarDecl * D)64506f32e7eSjoerg   llvm::Constant *getStaticLocalDeclAddress(const VarDecl *D) {
64606f32e7eSjoerg     return StaticLocalDeclMap[D];
64706f32e7eSjoerg   }
setStaticLocalDeclAddress(const VarDecl * D,llvm::Constant * C)64806f32e7eSjoerg   void setStaticLocalDeclAddress(const VarDecl *D,
64906f32e7eSjoerg                                  llvm::Constant *C) {
65006f32e7eSjoerg     StaticLocalDeclMap[D] = C;
65106f32e7eSjoerg   }
65206f32e7eSjoerg 
65306f32e7eSjoerg   llvm::Constant *
65406f32e7eSjoerg   getOrCreateStaticVarDecl(const VarDecl &D,
65506f32e7eSjoerg                            llvm::GlobalValue::LinkageTypes Linkage);
65606f32e7eSjoerg 
getStaticLocalDeclGuardAddress(const VarDecl * D)65706f32e7eSjoerg   llvm::GlobalVariable *getStaticLocalDeclGuardAddress(const VarDecl *D) {
65806f32e7eSjoerg     return StaticLocalDeclGuardMap[D];
65906f32e7eSjoerg   }
setStaticLocalDeclGuardAddress(const VarDecl * D,llvm::GlobalVariable * C)66006f32e7eSjoerg   void setStaticLocalDeclGuardAddress(const VarDecl *D,
66106f32e7eSjoerg                                       llvm::GlobalVariable *C) {
66206f32e7eSjoerg     StaticLocalDeclGuardMap[D] = C;
66306f32e7eSjoerg   }
66406f32e7eSjoerg 
66506f32e7eSjoerg   Address createUnnamedGlobalFrom(const VarDecl &D, llvm::Constant *Constant,
66606f32e7eSjoerg                                   CharUnits Align);
66706f32e7eSjoerg 
66806f32e7eSjoerg   bool lookupRepresentativeDecl(StringRef MangledName,
66906f32e7eSjoerg                                 GlobalDecl &Result) const;
67006f32e7eSjoerg 
getAtomicSetterHelperFnMap(QualType Ty)67106f32e7eSjoerg   llvm::Constant *getAtomicSetterHelperFnMap(QualType Ty) {
67206f32e7eSjoerg     return AtomicSetterHelperFnMap[Ty];
67306f32e7eSjoerg   }
setAtomicSetterHelperFnMap(QualType Ty,llvm::Constant * Fn)67406f32e7eSjoerg   void setAtomicSetterHelperFnMap(QualType Ty,
67506f32e7eSjoerg                             llvm::Constant *Fn) {
67606f32e7eSjoerg     AtomicSetterHelperFnMap[Ty] = Fn;
67706f32e7eSjoerg   }
67806f32e7eSjoerg 
getAtomicGetterHelperFnMap(QualType Ty)67906f32e7eSjoerg   llvm::Constant *getAtomicGetterHelperFnMap(QualType Ty) {
68006f32e7eSjoerg     return AtomicGetterHelperFnMap[Ty];
68106f32e7eSjoerg   }
setAtomicGetterHelperFnMap(QualType Ty,llvm::Constant * Fn)68206f32e7eSjoerg   void setAtomicGetterHelperFnMap(QualType Ty,
68306f32e7eSjoerg                             llvm::Constant *Fn) {
68406f32e7eSjoerg     AtomicGetterHelperFnMap[Ty] = Fn;
68506f32e7eSjoerg   }
68606f32e7eSjoerg 
getTypeDescriptorFromMap(QualType Ty)68706f32e7eSjoerg   llvm::Constant *getTypeDescriptorFromMap(QualType Ty) {
68806f32e7eSjoerg     return TypeDescriptorMap[Ty];
68906f32e7eSjoerg   }
setTypeDescriptorInMap(QualType Ty,llvm::Constant * C)69006f32e7eSjoerg   void setTypeDescriptorInMap(QualType Ty, llvm::Constant *C) {
69106f32e7eSjoerg     TypeDescriptorMap[Ty] = C;
69206f32e7eSjoerg   }
69306f32e7eSjoerg 
getModuleDebugInfo()69406f32e7eSjoerg   CGDebugInfo *getModuleDebugInfo() { return DebugInfo.get(); }
69506f32e7eSjoerg 
getNoObjCARCExceptionsMetadata()69606f32e7eSjoerg   llvm::MDNode *getNoObjCARCExceptionsMetadata() {
69706f32e7eSjoerg     if (!NoObjCARCExceptionsMetadata)
69806f32e7eSjoerg       NoObjCARCExceptionsMetadata = llvm::MDNode::get(getLLVMContext(), None);
69906f32e7eSjoerg     return NoObjCARCExceptionsMetadata;
70006f32e7eSjoerg   }
70106f32e7eSjoerg 
getContext()70206f32e7eSjoerg   ASTContext &getContext() const { return Context; }
getLangOpts()70306f32e7eSjoerg   const LangOptions &getLangOpts() const { return LangOpts; }
getHeaderSearchOpts()70406f32e7eSjoerg   const HeaderSearchOptions &getHeaderSearchOpts()
70506f32e7eSjoerg     const { return HeaderSearchOpts; }
getPreprocessorOpts()70606f32e7eSjoerg   const PreprocessorOptions &getPreprocessorOpts()
70706f32e7eSjoerg     const { return PreprocessorOpts; }
getCodeGenOpts()70806f32e7eSjoerg   const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
getModule()70906f32e7eSjoerg   llvm::Module &getModule() const { return TheModule; }
getDiags()71006f32e7eSjoerg   DiagnosticsEngine &getDiags() const { return Diags; }
getDataLayout()71106f32e7eSjoerg   const llvm::DataLayout &getDataLayout() const {
71206f32e7eSjoerg     return TheModule.getDataLayout();
71306f32e7eSjoerg   }
getTarget()71406f32e7eSjoerg   const TargetInfo &getTarget() const { return Target; }
getTriple()71506f32e7eSjoerg   const llvm::Triple &getTriple() const { return Target.getTriple(); }
71606f32e7eSjoerg   bool supportsCOMDAT() const;
71706f32e7eSjoerg   void maybeSetTrivialComdat(const Decl &D, llvm::GlobalObject &GO);
71806f32e7eSjoerg 
getCXXABI()71906f32e7eSjoerg   CGCXXABI &getCXXABI() const { return *ABI; }
getLLVMContext()72006f32e7eSjoerg   llvm::LLVMContext &getLLVMContext() { return VMContext; }
72106f32e7eSjoerg 
shouldUseTBAA()72206f32e7eSjoerg   bool shouldUseTBAA() const { return TBAA != nullptr; }
72306f32e7eSjoerg 
72406f32e7eSjoerg   const TargetCodeGenInfo &getTargetCodeGenInfo();
72506f32e7eSjoerg 
getTypes()72606f32e7eSjoerg   CodeGenTypes &getTypes() { return Types; }
72706f32e7eSjoerg 
getVTables()72806f32e7eSjoerg   CodeGenVTables &getVTables() { return VTables; }
72906f32e7eSjoerg 
getItaniumVTableContext()73006f32e7eSjoerg   ItaniumVTableContext &getItaniumVTableContext() {
73106f32e7eSjoerg     return VTables.getItaniumVTableContext();
73206f32e7eSjoerg   }
73306f32e7eSjoerg 
getMicrosoftVTableContext()73406f32e7eSjoerg   MicrosoftVTableContext &getMicrosoftVTableContext() {
73506f32e7eSjoerg     return VTables.getMicrosoftVTableContext();
73606f32e7eSjoerg   }
73706f32e7eSjoerg 
getGlobalCtors()73806f32e7eSjoerg   CtorList &getGlobalCtors() { return GlobalCtors; }
getGlobalDtors()73906f32e7eSjoerg   CtorList &getGlobalDtors() { return GlobalDtors; }
74006f32e7eSjoerg 
74106f32e7eSjoerg   /// getTBAATypeInfo - Get metadata used to describe accesses to objects of
74206f32e7eSjoerg   /// the given type.
74306f32e7eSjoerg   llvm::MDNode *getTBAATypeInfo(QualType QTy);
74406f32e7eSjoerg 
74506f32e7eSjoerg   /// getTBAAAccessInfo - Get TBAA information that describes an access to
74606f32e7eSjoerg   /// an object of the given type.
74706f32e7eSjoerg   TBAAAccessInfo getTBAAAccessInfo(QualType AccessType);
74806f32e7eSjoerg 
74906f32e7eSjoerg   /// getTBAAVTablePtrAccessInfo - Get the TBAA information that describes an
75006f32e7eSjoerg   /// access to a virtual table pointer.
75106f32e7eSjoerg   TBAAAccessInfo getTBAAVTablePtrAccessInfo(llvm::Type *VTablePtrType);
75206f32e7eSjoerg 
75306f32e7eSjoerg   llvm::MDNode *getTBAAStructInfo(QualType QTy);
75406f32e7eSjoerg 
75506f32e7eSjoerg   /// getTBAABaseTypeInfo - Get metadata that describes the given base access
75606f32e7eSjoerg   /// type. Return null if the type is not suitable for use in TBAA access tags.
75706f32e7eSjoerg   llvm::MDNode *getTBAABaseTypeInfo(QualType QTy);
75806f32e7eSjoerg 
75906f32e7eSjoerg   /// getTBAAAccessTagInfo - Get TBAA tag for a given memory access.
76006f32e7eSjoerg   llvm::MDNode *getTBAAAccessTagInfo(TBAAAccessInfo Info);
76106f32e7eSjoerg 
76206f32e7eSjoerg   /// mergeTBAAInfoForCast - Get merged TBAA information for the purposes of
76306f32e7eSjoerg   /// type casts.
76406f32e7eSjoerg   TBAAAccessInfo mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo,
76506f32e7eSjoerg                                       TBAAAccessInfo TargetInfo);
76606f32e7eSjoerg 
76706f32e7eSjoerg   /// mergeTBAAInfoForConditionalOperator - Get merged TBAA information for the
76806f32e7eSjoerg   /// purposes of conditional operator.
76906f32e7eSjoerg   TBAAAccessInfo mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA,
77006f32e7eSjoerg                                                      TBAAAccessInfo InfoB);
77106f32e7eSjoerg 
77206f32e7eSjoerg   /// mergeTBAAInfoForMemoryTransfer - Get merged TBAA information for the
77306f32e7eSjoerg   /// purposes of memory transfer calls.
77406f32e7eSjoerg   TBAAAccessInfo mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo,
77506f32e7eSjoerg                                                 TBAAAccessInfo SrcInfo);
77606f32e7eSjoerg 
77706f32e7eSjoerg   /// getTBAAInfoForSubobject - Get TBAA information for an access with a given
77806f32e7eSjoerg   /// base lvalue.
getTBAAInfoForSubobject(LValue Base,QualType AccessType)77906f32e7eSjoerg   TBAAAccessInfo getTBAAInfoForSubobject(LValue Base, QualType AccessType) {
78006f32e7eSjoerg     if (Base.getTBAAInfo().isMayAlias())
78106f32e7eSjoerg       return TBAAAccessInfo::getMayAliasInfo();
78206f32e7eSjoerg     return getTBAAAccessInfo(AccessType);
78306f32e7eSjoerg   }
78406f32e7eSjoerg 
78506f32e7eSjoerg   bool isTypeConstant(QualType QTy, bool ExcludeCtorDtor);
78606f32e7eSjoerg 
78706f32e7eSjoerg   bool isPaddedAtomicType(QualType type);
78806f32e7eSjoerg   bool isPaddedAtomicType(const AtomicType *type);
78906f32e7eSjoerg 
79006f32e7eSjoerg   /// DecorateInstructionWithTBAA - Decorate the instruction with a TBAA tag.
79106f32e7eSjoerg   void DecorateInstructionWithTBAA(llvm::Instruction *Inst,
79206f32e7eSjoerg                                    TBAAAccessInfo TBAAInfo);
79306f32e7eSjoerg 
79406f32e7eSjoerg   /// Adds !invariant.barrier !tag to instruction
79506f32e7eSjoerg   void DecorateInstructionWithInvariantGroup(llvm::Instruction *I,
79606f32e7eSjoerg                                              const CXXRecordDecl *RD);
79706f32e7eSjoerg 
79806f32e7eSjoerg   /// Emit the given number of characters as a value of type size_t.
79906f32e7eSjoerg   llvm::ConstantInt *getSize(CharUnits numChars);
80006f32e7eSjoerg 
80106f32e7eSjoerg   /// Set the visibility for the given LLVM GlobalValue.
80206f32e7eSjoerg   void setGlobalVisibility(llvm::GlobalValue *GV, const NamedDecl *D) const;
80306f32e7eSjoerg 
80406f32e7eSjoerg   void setDSOLocal(llvm::GlobalValue *GV) const;
80506f32e7eSjoerg 
80606f32e7eSjoerg   void setDLLImportDLLExport(llvm::GlobalValue *GV, GlobalDecl D) const;
80706f32e7eSjoerg   void setDLLImportDLLExport(llvm::GlobalValue *GV, const NamedDecl *D) const;
80806f32e7eSjoerg   /// Set visibility, dllimport/dllexport and dso_local.
80906f32e7eSjoerg   /// This must be called after dllimport/dllexport is set.
81006f32e7eSjoerg   void setGVProperties(llvm::GlobalValue *GV, GlobalDecl GD) const;
81106f32e7eSjoerg   void setGVProperties(llvm::GlobalValue *GV, const NamedDecl *D) const;
81206f32e7eSjoerg 
81306f32e7eSjoerg   void setGVPropertiesAux(llvm::GlobalValue *GV, const NamedDecl *D) const;
81406f32e7eSjoerg 
81506f32e7eSjoerg   /// Set the TLS mode for the given LLVM GlobalValue for the thread-local
81606f32e7eSjoerg   /// variable declaration D.
81706f32e7eSjoerg   void setTLSMode(llvm::GlobalValue *GV, const VarDecl &D) const;
81806f32e7eSjoerg 
819*13fbcb42Sjoerg   /// Get LLVM TLS mode from CodeGenOptions.
820*13fbcb42Sjoerg   llvm::GlobalVariable::ThreadLocalMode GetDefaultLLVMTLSModel() const;
821*13fbcb42Sjoerg 
GetLLVMVisibility(Visibility V)82206f32e7eSjoerg   static llvm::GlobalValue::VisibilityTypes GetLLVMVisibility(Visibility V) {
82306f32e7eSjoerg     switch (V) {
82406f32e7eSjoerg     case DefaultVisibility:   return llvm::GlobalValue::DefaultVisibility;
82506f32e7eSjoerg     case HiddenVisibility:    return llvm::GlobalValue::HiddenVisibility;
82606f32e7eSjoerg     case ProtectedVisibility: return llvm::GlobalValue::ProtectedVisibility;
82706f32e7eSjoerg     }
82806f32e7eSjoerg     llvm_unreachable("unknown visibility!");
82906f32e7eSjoerg   }
83006f32e7eSjoerg 
83106f32e7eSjoerg   llvm::Constant *GetAddrOfGlobal(GlobalDecl GD,
83206f32e7eSjoerg                                   ForDefinition_t IsForDefinition
83306f32e7eSjoerg                                     = NotForDefinition);
83406f32e7eSjoerg 
83506f32e7eSjoerg   /// Will return a global variable of the given type. If a variable with a
83606f32e7eSjoerg   /// different type already exists then a new  variable with the right type
83706f32e7eSjoerg   /// will be created and all uses of the old variable will be replaced with a
83806f32e7eSjoerg   /// bitcast to the new variable.
83906f32e7eSjoerg   llvm::GlobalVariable *
84006f32e7eSjoerg   CreateOrReplaceCXXRuntimeVariable(StringRef Name, llvm::Type *Ty,
84106f32e7eSjoerg                                     llvm::GlobalValue::LinkageTypes Linkage,
84206f32e7eSjoerg                                     unsigned Alignment);
84306f32e7eSjoerg 
844*13fbcb42Sjoerg   llvm::Function *CreateGlobalInitOrCleanUpFunction(
845*13fbcb42Sjoerg       llvm::FunctionType *ty, const Twine &name, const CGFunctionInfo &FI,
846*13fbcb42Sjoerg       SourceLocation Loc = SourceLocation(), bool TLS = false);
84706f32e7eSjoerg 
84806f32e7eSjoerg   /// Return the AST address space of the underlying global variable for D, as
84906f32e7eSjoerg   /// determined by its declaration. Normally this is the same as the address
85006f32e7eSjoerg   /// space of D's type, but in CUDA, address spaces are associated with
85106f32e7eSjoerg   /// declarations, not types. If D is nullptr, return the default address
85206f32e7eSjoerg   /// space for global variable.
85306f32e7eSjoerg   ///
85406f32e7eSjoerg   /// For languages without explicit address spaces, if D has default address
85506f32e7eSjoerg   /// space, target-specific global or constant address space may be returned.
85606f32e7eSjoerg   LangAS GetGlobalVarAddressSpace(const VarDecl *D);
85706f32e7eSjoerg 
858*13fbcb42Sjoerg   /// Return the AST address space of constant literal, which is used to emit
859*13fbcb42Sjoerg   /// the constant literal as global variable in LLVM IR.
860*13fbcb42Sjoerg   /// Note: This is not necessarily the address space of the constant literal
861*13fbcb42Sjoerg   /// in AST. For address space agnostic language, e.g. C++, constant literal
862*13fbcb42Sjoerg   /// in AST is always in default address space.
863*13fbcb42Sjoerg   LangAS GetGlobalConstantAddressSpace() const;
864*13fbcb42Sjoerg 
86506f32e7eSjoerg   /// Return the llvm::Constant for the address of the given global variable.
86606f32e7eSjoerg   /// If Ty is non-null and if the global doesn't exist, then it will be created
86706f32e7eSjoerg   /// with the specified type instead of whatever the normal requested type
86806f32e7eSjoerg   /// would be. If IsForDefinition is true, it is guaranteed that an actual
86906f32e7eSjoerg   /// global with type Ty will be returned, not conversion of a variable with
87006f32e7eSjoerg   /// the same mangled name but some other type.
87106f32e7eSjoerg   llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D,
87206f32e7eSjoerg                                      llvm::Type *Ty = nullptr,
87306f32e7eSjoerg                                      ForDefinition_t IsForDefinition
87406f32e7eSjoerg                                        = NotForDefinition);
87506f32e7eSjoerg 
87606f32e7eSjoerg   /// Return the address of the given function. If Ty is non-null, then this
87706f32e7eSjoerg   /// function will use the specified type if it has to create it.
87806f32e7eSjoerg   llvm::Constant *GetAddrOfFunction(GlobalDecl GD, llvm::Type *Ty = nullptr,
87906f32e7eSjoerg                                     bool ForVTable = false,
88006f32e7eSjoerg                                     bool DontDefer = false,
88106f32e7eSjoerg                                     ForDefinition_t IsForDefinition
88206f32e7eSjoerg                                       = NotForDefinition);
88306f32e7eSjoerg 
88406f32e7eSjoerg   /// Get the address of the RTTI descriptor for the given type.
88506f32e7eSjoerg   llvm::Constant *GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH = false);
88606f32e7eSjoerg 
887*13fbcb42Sjoerg   /// Get the address of a GUID.
888*13fbcb42Sjoerg   ConstantAddress GetAddrOfMSGuidDecl(const MSGuidDecl *GD);
889*13fbcb42Sjoerg 
890*13fbcb42Sjoerg   /// Get the address of a template parameter object.
891*13fbcb42Sjoerg   ConstantAddress
892*13fbcb42Sjoerg   GetAddrOfTemplateParamObject(const TemplateParamObjectDecl *TPO);
89306f32e7eSjoerg 
89406f32e7eSjoerg   /// Get the address of the thunk for the given global decl.
89506f32e7eSjoerg   llvm::Constant *GetAddrOfThunk(StringRef Name, llvm::Type *FnTy,
89606f32e7eSjoerg                                  GlobalDecl GD);
89706f32e7eSjoerg 
89806f32e7eSjoerg   /// Get a reference to the target of VD.
89906f32e7eSjoerg   ConstantAddress GetWeakRefReference(const ValueDecl *VD);
90006f32e7eSjoerg 
90106f32e7eSjoerg   /// Returns the assumed alignment of an opaque pointer to the given class.
90206f32e7eSjoerg   CharUnits getClassPointerAlignment(const CXXRecordDecl *CD);
90306f32e7eSjoerg 
904*13fbcb42Sjoerg   /// Returns the minimum object size for an object of the given class type
905*13fbcb42Sjoerg   /// (or a class derived from it).
906*13fbcb42Sjoerg   CharUnits getMinimumClassObjectSize(const CXXRecordDecl *CD);
907*13fbcb42Sjoerg 
908*13fbcb42Sjoerg   /// Returns the minimum object size for an object of the given type.
getMinimumObjectSize(QualType Ty)909*13fbcb42Sjoerg   CharUnits getMinimumObjectSize(QualType Ty) {
910*13fbcb42Sjoerg     if (CXXRecordDecl *RD = Ty->getAsCXXRecordDecl())
911*13fbcb42Sjoerg       return getMinimumClassObjectSize(RD);
912*13fbcb42Sjoerg     return getContext().getTypeSizeInChars(Ty);
913*13fbcb42Sjoerg   }
914*13fbcb42Sjoerg 
91506f32e7eSjoerg   /// Returns the assumed alignment of a virtual base of a class.
91606f32e7eSjoerg   CharUnits getVBaseAlignment(CharUnits DerivedAlign,
91706f32e7eSjoerg                               const CXXRecordDecl *Derived,
91806f32e7eSjoerg                               const CXXRecordDecl *VBase);
91906f32e7eSjoerg 
92006f32e7eSjoerg   /// Given a class pointer with an actual known alignment, and the
92106f32e7eSjoerg   /// expected alignment of an object at a dynamic offset w.r.t that
92206f32e7eSjoerg   /// pointer, return the alignment to assume at the offset.
92306f32e7eSjoerg   CharUnits getDynamicOffsetAlignment(CharUnits ActualAlign,
92406f32e7eSjoerg                                       const CXXRecordDecl *Class,
92506f32e7eSjoerg                                       CharUnits ExpectedTargetAlign);
92606f32e7eSjoerg 
92706f32e7eSjoerg   CharUnits
92806f32e7eSjoerg   computeNonVirtualBaseClassOffset(const CXXRecordDecl *DerivedClass,
92906f32e7eSjoerg                                    CastExpr::path_const_iterator Start,
93006f32e7eSjoerg                                    CastExpr::path_const_iterator End);
93106f32e7eSjoerg 
93206f32e7eSjoerg   /// Returns the offset from a derived class to  a class. Returns null if the
93306f32e7eSjoerg   /// offset is 0.
93406f32e7eSjoerg   llvm::Constant *
93506f32e7eSjoerg   GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl,
93606f32e7eSjoerg                                CastExpr::path_const_iterator PathBegin,
93706f32e7eSjoerg                                CastExpr::path_const_iterator PathEnd);
93806f32e7eSjoerg 
93906f32e7eSjoerg   llvm::FoldingSet<BlockByrefHelpers> ByrefHelpersCache;
94006f32e7eSjoerg 
94106f32e7eSjoerg   /// Fetches the global unique block count.
getUniqueBlockCount()94206f32e7eSjoerg   int getUniqueBlockCount() { return ++Block.GlobalUniqueCount; }
94306f32e7eSjoerg 
94406f32e7eSjoerg   /// Fetches the type of a generic block descriptor.
94506f32e7eSjoerg   llvm::Type *getBlockDescriptorType();
94606f32e7eSjoerg 
94706f32e7eSjoerg   /// The type of a generic block literal.
94806f32e7eSjoerg   llvm::Type *getGenericBlockLiteralType();
94906f32e7eSjoerg 
95006f32e7eSjoerg   /// Gets the address of a block which requires no captures.
95106f32e7eSjoerg   llvm::Constant *GetAddrOfGlobalBlock(const BlockExpr *BE, StringRef Name);
95206f32e7eSjoerg 
95306f32e7eSjoerg   /// Returns the address of a block which requires no caputres, or null if
95406f32e7eSjoerg   /// we've yet to emit the block for BE.
getAddrOfGlobalBlockIfEmitted(const BlockExpr * BE)95506f32e7eSjoerg   llvm::Constant *getAddrOfGlobalBlockIfEmitted(const BlockExpr *BE) {
95606f32e7eSjoerg     return EmittedGlobalBlocks.lookup(BE);
95706f32e7eSjoerg   }
95806f32e7eSjoerg 
95906f32e7eSjoerg   /// Notes that BE's global block is available via Addr. Asserts that BE
96006f32e7eSjoerg   /// isn't already emitted.
96106f32e7eSjoerg   void setAddrOfGlobalBlock(const BlockExpr *BE, llvm::Constant *Addr);
96206f32e7eSjoerg 
96306f32e7eSjoerg   /// Return a pointer to a constant CFString object for the given string.
96406f32e7eSjoerg   ConstantAddress GetAddrOfConstantCFString(const StringLiteral *Literal);
96506f32e7eSjoerg 
96606f32e7eSjoerg   /// Return a pointer to a constant NSString object for the given string. Or a
96706f32e7eSjoerg   /// user defined String object as defined via
96806f32e7eSjoerg   /// -fconstant-string-class=class_name option.
96906f32e7eSjoerg   ConstantAddress GetAddrOfConstantString(const StringLiteral *Literal);
97006f32e7eSjoerg 
97106f32e7eSjoerg   /// Return a constant array for the given string.
97206f32e7eSjoerg   llvm::Constant *GetConstantArrayFromStringLiteral(const StringLiteral *E);
97306f32e7eSjoerg 
97406f32e7eSjoerg   /// Return a pointer to a constant array for the given string literal.
97506f32e7eSjoerg   ConstantAddress
97606f32e7eSjoerg   GetAddrOfConstantStringFromLiteral(const StringLiteral *S,
97706f32e7eSjoerg                                      StringRef Name = ".str");
97806f32e7eSjoerg 
97906f32e7eSjoerg   /// Return a pointer to a constant array for the given ObjCEncodeExpr node.
98006f32e7eSjoerg   ConstantAddress
98106f32e7eSjoerg   GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *);
98206f32e7eSjoerg 
98306f32e7eSjoerg   /// Returns a pointer to a character array containing the literal and a
98406f32e7eSjoerg   /// terminating '\0' character. The result has pointer to array type.
98506f32e7eSjoerg   ///
98606f32e7eSjoerg   /// \param GlobalName If provided, the name to use for the global (if one is
98706f32e7eSjoerg   /// created).
98806f32e7eSjoerg   ConstantAddress
98906f32e7eSjoerg   GetAddrOfConstantCString(const std::string &Str,
99006f32e7eSjoerg                            const char *GlobalName = nullptr);
99106f32e7eSjoerg 
99206f32e7eSjoerg   /// Returns a pointer to a constant global variable for the given file-scope
99306f32e7eSjoerg   /// compound literal expression.
99406f32e7eSjoerg   ConstantAddress GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr*E);
99506f32e7eSjoerg 
99606f32e7eSjoerg   /// If it's been emitted already, returns the GlobalVariable corresponding to
99706f32e7eSjoerg   /// a compound literal. Otherwise, returns null.
99806f32e7eSjoerg   llvm::GlobalVariable *
99906f32e7eSjoerg   getAddrOfConstantCompoundLiteralIfEmitted(const CompoundLiteralExpr *E);
100006f32e7eSjoerg 
100106f32e7eSjoerg   /// Notes that CLE's GlobalVariable is GV. Asserts that CLE isn't already
100206f32e7eSjoerg   /// emitted.
100306f32e7eSjoerg   void setAddrOfConstantCompoundLiteral(const CompoundLiteralExpr *CLE,
100406f32e7eSjoerg                                         llvm::GlobalVariable *GV);
100506f32e7eSjoerg 
100606f32e7eSjoerg   /// Returns a pointer to a global variable representing a temporary
100706f32e7eSjoerg   /// with static or thread storage duration.
100806f32e7eSjoerg   ConstantAddress GetAddrOfGlobalTemporary(const MaterializeTemporaryExpr *E,
100906f32e7eSjoerg                                            const Expr *Inner);
101006f32e7eSjoerg 
101106f32e7eSjoerg   /// Retrieve the record type that describes the state of an
101206f32e7eSjoerg   /// Objective-C fast enumeration loop (for..in).
101306f32e7eSjoerg   QualType getObjCFastEnumerationStateType();
101406f32e7eSjoerg 
101506f32e7eSjoerg   // Produce code for this constructor/destructor. This method doesn't try
101606f32e7eSjoerg   // to apply any ABI rules about which other constructors/destructors
101706f32e7eSjoerg   // are needed or if they are alias to each other.
101806f32e7eSjoerg   llvm::Function *codegenCXXStructor(GlobalDecl GD);
101906f32e7eSjoerg 
102006f32e7eSjoerg   /// Return the address of the constructor/destructor of the given type.
102106f32e7eSjoerg   llvm::Constant *
102206f32e7eSjoerg   getAddrOfCXXStructor(GlobalDecl GD, const CGFunctionInfo *FnInfo = nullptr,
102306f32e7eSjoerg                        llvm::FunctionType *FnType = nullptr,
102406f32e7eSjoerg                        bool DontDefer = false,
102506f32e7eSjoerg                        ForDefinition_t IsForDefinition = NotForDefinition) {
102606f32e7eSjoerg     return cast<llvm::Constant>(getAddrAndTypeOfCXXStructor(GD, FnInfo, FnType,
102706f32e7eSjoerg                                                             DontDefer,
102806f32e7eSjoerg                                                             IsForDefinition)
102906f32e7eSjoerg                                     .getCallee());
103006f32e7eSjoerg   }
103106f32e7eSjoerg 
103206f32e7eSjoerg   llvm::FunctionCallee getAddrAndTypeOfCXXStructor(
103306f32e7eSjoerg       GlobalDecl GD, const CGFunctionInfo *FnInfo = nullptr,
103406f32e7eSjoerg       llvm::FunctionType *FnType = nullptr, bool DontDefer = false,
103506f32e7eSjoerg       ForDefinition_t IsForDefinition = NotForDefinition);
103606f32e7eSjoerg 
103706f32e7eSjoerg   /// Given a builtin id for a function like "__builtin_fabsf", return a
103806f32e7eSjoerg   /// Function* for "fabsf".
103906f32e7eSjoerg   llvm::Constant *getBuiltinLibFunction(const FunctionDecl *FD,
104006f32e7eSjoerg                                         unsigned BuiltinID);
104106f32e7eSjoerg 
104206f32e7eSjoerg   llvm::Function *getIntrinsic(unsigned IID, ArrayRef<llvm::Type*> Tys = None);
104306f32e7eSjoerg 
104406f32e7eSjoerg   /// Emit code for a single top level declaration.
104506f32e7eSjoerg   void EmitTopLevelDecl(Decl *D);
104606f32e7eSjoerg 
104706f32e7eSjoerg   /// Stored a deferred empty coverage mapping for an unused
104806f32e7eSjoerg   /// and thus uninstrumented top level declaration.
104906f32e7eSjoerg   void AddDeferredUnusedCoverageMapping(Decl *D);
105006f32e7eSjoerg 
105106f32e7eSjoerg   /// Remove the deferred empty coverage mapping as this
105206f32e7eSjoerg   /// declaration is actually instrumented.
105306f32e7eSjoerg   void ClearUnusedCoverageMapping(const Decl *D);
105406f32e7eSjoerg 
105506f32e7eSjoerg   /// Emit all the deferred coverage mappings
105606f32e7eSjoerg   /// for the uninstrumented functions.
105706f32e7eSjoerg   void EmitDeferredUnusedCoverageMappings();
105806f32e7eSjoerg 
1059*13fbcb42Sjoerg   /// Emit an alias for "main" if it has no arguments (needed for wasm).
1060*13fbcb42Sjoerg   void EmitMainVoidAlias();
1061*13fbcb42Sjoerg 
106206f32e7eSjoerg   /// Tell the consumer that this variable has been instantiated.
106306f32e7eSjoerg   void HandleCXXStaticMemberVarInstantiation(VarDecl *VD);
106406f32e7eSjoerg 
106506f32e7eSjoerg   /// If the declaration has internal linkage but is inside an
106606f32e7eSjoerg   /// extern "C" linkage specification, prepare to emit an alias for it
106706f32e7eSjoerg   /// to the expected name.
106806f32e7eSjoerg   template<typename SomeDecl>
106906f32e7eSjoerg   void MaybeHandleStaticInExternC(const SomeDecl *D, llvm::GlobalValue *GV);
107006f32e7eSjoerg 
107106f32e7eSjoerg   /// Add a global to a list to be added to the llvm.used metadata.
107206f32e7eSjoerg   void addUsedGlobal(llvm::GlobalValue *GV);
107306f32e7eSjoerg 
107406f32e7eSjoerg   /// Add a global to a list to be added to the llvm.compiler.used metadata.
107506f32e7eSjoerg   void addCompilerUsedGlobal(llvm::GlobalValue *GV);
107606f32e7eSjoerg 
1077*13fbcb42Sjoerg   /// Add a global to a list to be added to the llvm.compiler.used metadata.
1078*13fbcb42Sjoerg   void addUsedOrCompilerUsedGlobal(llvm::GlobalValue *GV);
1079*13fbcb42Sjoerg 
108006f32e7eSjoerg   /// Add a destructor and object to add to the C++ global destructor function.
AddCXXDtorEntry(llvm::FunctionCallee DtorFn,llvm::Constant * Object)108106f32e7eSjoerg   void AddCXXDtorEntry(llvm::FunctionCallee DtorFn, llvm::Constant *Object) {
1082*13fbcb42Sjoerg     CXXGlobalDtorsOrStermFinalizers.emplace_back(DtorFn.getFunctionType(),
1083*13fbcb42Sjoerg                                                  DtorFn.getCallee(), Object);
1084*13fbcb42Sjoerg   }
1085*13fbcb42Sjoerg 
1086*13fbcb42Sjoerg   /// Add an sterm finalizer to the C++ global cleanup function.
AddCXXStermFinalizerEntry(llvm::FunctionCallee DtorFn)1087*13fbcb42Sjoerg   void AddCXXStermFinalizerEntry(llvm::FunctionCallee DtorFn) {
1088*13fbcb42Sjoerg     CXXGlobalDtorsOrStermFinalizers.emplace_back(DtorFn.getFunctionType(),
1089*13fbcb42Sjoerg                                                  DtorFn.getCallee(), nullptr);
1090*13fbcb42Sjoerg   }
1091*13fbcb42Sjoerg 
1092*13fbcb42Sjoerg   /// Add an sterm finalizer to its own llvm.global_dtors entry.
AddCXXStermFinalizerToGlobalDtor(llvm::Function * StermFinalizer,int Priority)1093*13fbcb42Sjoerg   void AddCXXStermFinalizerToGlobalDtor(llvm::Function *StermFinalizer,
1094*13fbcb42Sjoerg                                         int Priority) {
1095*13fbcb42Sjoerg     AddGlobalDtor(StermFinalizer, Priority);
1096*13fbcb42Sjoerg   }
1097*13fbcb42Sjoerg 
AddCXXPrioritizedStermFinalizerEntry(llvm::Function * StermFinalizer,int Priority)1098*13fbcb42Sjoerg   void AddCXXPrioritizedStermFinalizerEntry(llvm::Function *StermFinalizer,
1099*13fbcb42Sjoerg                                             int Priority) {
1100*13fbcb42Sjoerg     OrderGlobalInitsOrStermFinalizers Key(Priority,
1101*13fbcb42Sjoerg                                           PrioritizedCXXStermFinalizers.size());
1102*13fbcb42Sjoerg     PrioritizedCXXStermFinalizers.push_back(
1103*13fbcb42Sjoerg         std::make_pair(Key, StermFinalizer));
110406f32e7eSjoerg   }
110506f32e7eSjoerg 
110606f32e7eSjoerg   /// Create or return a runtime function declaration with the specified type
110706f32e7eSjoerg   /// and name. If \p AssumeConvergent is true, the call will have the
110806f32e7eSjoerg   /// convergent attribute added.
110906f32e7eSjoerg   llvm::FunctionCallee
111006f32e7eSjoerg   CreateRuntimeFunction(llvm::FunctionType *Ty, StringRef Name,
111106f32e7eSjoerg                         llvm::AttributeList ExtraAttrs = llvm::AttributeList(),
111206f32e7eSjoerg                         bool Local = false, bool AssumeConvergent = false);
111306f32e7eSjoerg 
111406f32e7eSjoerg   /// Create a new runtime global variable with the specified type and name.
111506f32e7eSjoerg   llvm::Constant *CreateRuntimeVariable(llvm::Type *Ty,
111606f32e7eSjoerg                                         StringRef Name);
111706f32e7eSjoerg 
111806f32e7eSjoerg   ///@name Custom Blocks Runtime Interfaces
111906f32e7eSjoerg   ///@{
112006f32e7eSjoerg 
112106f32e7eSjoerg   llvm::Constant *getNSConcreteGlobalBlock();
112206f32e7eSjoerg   llvm::Constant *getNSConcreteStackBlock();
112306f32e7eSjoerg   llvm::FunctionCallee getBlockObjectAssign();
112406f32e7eSjoerg   llvm::FunctionCallee getBlockObjectDispose();
112506f32e7eSjoerg 
112606f32e7eSjoerg   ///@}
112706f32e7eSjoerg 
112806f32e7eSjoerg   llvm::Function *getLLVMLifetimeStartFn();
112906f32e7eSjoerg   llvm::Function *getLLVMLifetimeEndFn();
113006f32e7eSjoerg 
113106f32e7eSjoerg   // Make sure that this type is translated.
113206f32e7eSjoerg   void UpdateCompletedType(const TagDecl *TD);
113306f32e7eSjoerg 
113406f32e7eSjoerg   llvm::Constant *getMemberPointerConstant(const UnaryOperator *e);
113506f32e7eSjoerg 
113606f32e7eSjoerg   /// Emit type info if type of an expression is a variably modified
113706f32e7eSjoerg   /// type. Also emit proper debug info for cast types.
113806f32e7eSjoerg   void EmitExplicitCastExprType(const ExplicitCastExpr *E,
113906f32e7eSjoerg                                 CodeGenFunction *CGF = nullptr);
114006f32e7eSjoerg 
114106f32e7eSjoerg   /// Return the result of value-initializing the given type, i.e. a null
114206f32e7eSjoerg   /// expression of the given type.  This is usually, but not always, an LLVM
114306f32e7eSjoerg   /// null constant.
114406f32e7eSjoerg   llvm::Constant *EmitNullConstant(QualType T);
114506f32e7eSjoerg 
114606f32e7eSjoerg   /// Return a null constant appropriate for zero-initializing a base class with
114706f32e7eSjoerg   /// the given type. This is usually, but not always, an LLVM null constant.
114806f32e7eSjoerg   llvm::Constant *EmitNullConstantForBase(const CXXRecordDecl *Record);
114906f32e7eSjoerg 
115006f32e7eSjoerg   /// Emit a general error that something can't be done.
115106f32e7eSjoerg   void Error(SourceLocation loc, StringRef error);
115206f32e7eSjoerg 
115306f32e7eSjoerg   /// Print out an error that codegen doesn't support the specified stmt yet.
115406f32e7eSjoerg   void ErrorUnsupported(const Stmt *S, const char *Type);
115506f32e7eSjoerg 
115606f32e7eSjoerg   /// Print out an error that codegen doesn't support the specified decl yet.
115706f32e7eSjoerg   void ErrorUnsupported(const Decl *D, const char *Type);
115806f32e7eSjoerg 
115906f32e7eSjoerg   /// Set the attributes on the LLVM function for the given decl and function
116006f32e7eSjoerg   /// info. This applies attributes necessary for handling the ABI as well as
116106f32e7eSjoerg   /// user specified attributes like section.
116206f32e7eSjoerg   void SetInternalFunctionAttributes(GlobalDecl GD, llvm::Function *F,
116306f32e7eSjoerg                                      const CGFunctionInfo &FI);
116406f32e7eSjoerg 
116506f32e7eSjoerg   /// Set the LLVM function attributes (sext, zext, etc).
116606f32e7eSjoerg   void SetLLVMFunctionAttributes(GlobalDecl GD, const CGFunctionInfo &Info,
1167*13fbcb42Sjoerg                                  llvm::Function *F, bool IsThunk);
116806f32e7eSjoerg 
116906f32e7eSjoerg   /// Set the LLVM function attributes which only apply to a function
117006f32e7eSjoerg   /// definition.
117106f32e7eSjoerg   void SetLLVMFunctionAttributesForDefinition(const Decl *D, llvm::Function *F);
117206f32e7eSjoerg 
1173*13fbcb42Sjoerg   /// Set the LLVM function attributes that represent floating point
1174*13fbcb42Sjoerg   /// environment.
1175*13fbcb42Sjoerg   void setLLVMFunctionFEnvAttributes(const FunctionDecl *D, llvm::Function *F);
1176*13fbcb42Sjoerg 
117706f32e7eSjoerg   /// Return true iff the given type uses 'sret' when used as a return type.
117806f32e7eSjoerg   bool ReturnTypeUsesSRet(const CGFunctionInfo &FI);
117906f32e7eSjoerg 
118006f32e7eSjoerg   /// Return true iff the given type uses an argument slot when 'sret' is used
118106f32e7eSjoerg   /// as a return type.
118206f32e7eSjoerg   bool ReturnSlotInterferesWithArgs(const CGFunctionInfo &FI);
118306f32e7eSjoerg 
118406f32e7eSjoerg   /// Return true iff the given type uses 'fpret' when used as a return type.
118506f32e7eSjoerg   bool ReturnTypeUsesFPRet(QualType ResultType);
118606f32e7eSjoerg 
118706f32e7eSjoerg   /// Return true iff the given type uses 'fp2ret' when used as a return type.
118806f32e7eSjoerg   bool ReturnTypeUsesFP2Ret(QualType ResultType);
118906f32e7eSjoerg 
119006f32e7eSjoerg   /// Get the LLVM attributes and calling convention to use for a particular
119106f32e7eSjoerg   /// function type.
119206f32e7eSjoerg   ///
119306f32e7eSjoerg   /// \param Name - The function name.
119406f32e7eSjoerg   /// \param Info - The function type information.
119506f32e7eSjoerg   /// \param CalleeInfo - The callee information these attributes are being
119606f32e7eSjoerg   /// constructed for. If valid, the attributes applied to this decl may
119706f32e7eSjoerg   /// contribute to the function attributes and calling convention.
119806f32e7eSjoerg   /// \param Attrs [out] - On return, the attribute list to use.
119906f32e7eSjoerg   /// \param CallingConv [out] - On return, the LLVM calling convention to use.
120006f32e7eSjoerg   void ConstructAttributeList(StringRef Name, const CGFunctionInfo &Info,
120106f32e7eSjoerg                               CGCalleeInfo CalleeInfo,
120206f32e7eSjoerg                               llvm::AttributeList &Attrs, unsigned &CallingConv,
1203*13fbcb42Sjoerg                               bool AttrOnCallSite, bool IsThunk);
120406f32e7eSjoerg 
120506f32e7eSjoerg   /// Adds attributes to F according to our CodeGenOptions and LangOptions, as
120606f32e7eSjoerg   /// though we had emitted it ourselves.  We remove any attributes on F that
120706f32e7eSjoerg   /// conflict with the attributes we add here.
120806f32e7eSjoerg   ///
120906f32e7eSjoerg   /// This is useful for adding attrs to bitcode modules that you want to link
121006f32e7eSjoerg   /// with but don't control, such as CUDA's libdevice.  When linking with such
121106f32e7eSjoerg   /// a bitcode library, you might want to set e.g. its functions'
121206f32e7eSjoerg   /// "unsafe-fp-math" attribute to match the attr of the functions you're
121306f32e7eSjoerg   /// codegen'ing.  Otherwise, LLVM will interpret the bitcode module's lack of
121406f32e7eSjoerg   /// unsafe-fp-math attrs as tantamount to unsafe-fp-math=false, and then LLVM
121506f32e7eSjoerg   /// will propagate unsafe-fp-math=false up to every transitive caller of a
121606f32e7eSjoerg   /// function in the bitcode library!
121706f32e7eSjoerg   ///
121806f32e7eSjoerg   /// With the exception of fast-math attrs, this will only make the attributes
121906f32e7eSjoerg   /// on the function more conservative.  But it's unsafe to call this on a
122006f32e7eSjoerg   /// function which relies on particular fast-math attributes for correctness.
122106f32e7eSjoerg   /// It's up to you to ensure that this is safe.
1222*13fbcb42Sjoerg   void addDefaultFunctionDefinitionAttributes(llvm::Function &F);
122306f32e7eSjoerg 
1224*13fbcb42Sjoerg   /// Like the overload taking a `Function &`, but intended specifically
1225*13fbcb42Sjoerg   /// for frontends that want to build on Clang's target-configuration logic.
1226*13fbcb42Sjoerg   void addDefaultFunctionDefinitionAttributes(llvm::AttrBuilder &attrs);
122706f32e7eSjoerg 
122806f32e7eSjoerg   StringRef getMangledName(GlobalDecl GD);
122906f32e7eSjoerg   StringRef getBlockMangledName(GlobalDecl GD, const BlockDecl *BD);
123006f32e7eSjoerg 
123106f32e7eSjoerg   void EmitTentativeDefinition(const VarDecl *D);
123206f32e7eSjoerg 
1233*13fbcb42Sjoerg   void EmitExternalDeclaration(const VarDecl *D);
1234*13fbcb42Sjoerg 
123506f32e7eSjoerg   void EmitVTable(CXXRecordDecl *Class);
123606f32e7eSjoerg 
123706f32e7eSjoerg   void RefreshTypeCacheForClass(const CXXRecordDecl *Class);
123806f32e7eSjoerg 
123906f32e7eSjoerg   /// Appends Opts to the "llvm.linker.options" metadata value.
124006f32e7eSjoerg   void AppendLinkerOptions(StringRef Opts);
124106f32e7eSjoerg 
124206f32e7eSjoerg   /// Appends a detect mismatch command to the linker options.
124306f32e7eSjoerg   void AddDetectMismatch(StringRef Name, StringRef Value);
124406f32e7eSjoerg 
124506f32e7eSjoerg   /// Appends a dependent lib to the appropriate metadata value.
124606f32e7eSjoerg   void AddDependentLib(StringRef Lib);
124706f32e7eSjoerg 
124806f32e7eSjoerg 
124906f32e7eSjoerg   llvm::GlobalVariable::LinkageTypes getFunctionLinkage(GlobalDecl GD);
125006f32e7eSjoerg 
setFunctionLinkage(GlobalDecl GD,llvm::Function * F)125106f32e7eSjoerg   void setFunctionLinkage(GlobalDecl GD, llvm::Function *F) {
125206f32e7eSjoerg     F->setLinkage(getFunctionLinkage(GD));
125306f32e7eSjoerg   }
125406f32e7eSjoerg 
125506f32e7eSjoerg   /// Return the appropriate linkage for the vtable, VTT, and type information
125606f32e7eSjoerg   /// of the given class.
125706f32e7eSjoerg   llvm::GlobalVariable::LinkageTypes getVTableLinkage(const CXXRecordDecl *RD);
125806f32e7eSjoerg 
125906f32e7eSjoerg   /// Return the store size, in character units, of the given LLVM type.
126006f32e7eSjoerg   CharUnits GetTargetTypeStoreSize(llvm::Type *Ty) const;
126106f32e7eSjoerg 
126206f32e7eSjoerg   /// Returns LLVM linkage for a declarator.
126306f32e7eSjoerg   llvm::GlobalValue::LinkageTypes
126406f32e7eSjoerg   getLLVMLinkageForDeclarator(const DeclaratorDecl *D, GVALinkage Linkage,
126506f32e7eSjoerg                               bool IsConstantVariable);
126606f32e7eSjoerg 
126706f32e7eSjoerg   /// Returns LLVM linkage for a declarator.
126806f32e7eSjoerg   llvm::GlobalValue::LinkageTypes
126906f32e7eSjoerg   getLLVMLinkageVarDefinition(const VarDecl *VD, bool IsConstant);
127006f32e7eSjoerg 
127106f32e7eSjoerg   /// Emit all the global annotations.
127206f32e7eSjoerg   void EmitGlobalAnnotations();
127306f32e7eSjoerg 
127406f32e7eSjoerg   /// Emit an annotation string.
127506f32e7eSjoerg   llvm::Constant *EmitAnnotationString(StringRef Str);
127606f32e7eSjoerg 
127706f32e7eSjoerg   /// Emit the annotation's translation unit.
127806f32e7eSjoerg   llvm::Constant *EmitAnnotationUnit(SourceLocation Loc);
127906f32e7eSjoerg 
128006f32e7eSjoerg   /// Emit the annotation line number.
128106f32e7eSjoerg   llvm::Constant *EmitAnnotationLineNo(SourceLocation L);
128206f32e7eSjoerg 
1283*13fbcb42Sjoerg   /// Emit additional args of the annotation.
1284*13fbcb42Sjoerg   llvm::Constant *EmitAnnotationArgs(const AnnotateAttr *Attr);
1285*13fbcb42Sjoerg 
128606f32e7eSjoerg   /// Generate the llvm::ConstantStruct which contains the annotation
128706f32e7eSjoerg   /// information for a given GlobalValue. The annotation struct is
128806f32e7eSjoerg   /// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the
128906f32e7eSjoerg   /// GlobalValue being annotated. The second field is the constant string
129006f32e7eSjoerg   /// created from the AnnotateAttr's annotation. The third field is a constant
129106f32e7eSjoerg   /// string containing the name of the translation unit. The fourth field is
129206f32e7eSjoerg   /// the line number in the file of the annotated value declaration.
129306f32e7eSjoerg   llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV,
129406f32e7eSjoerg                                    const AnnotateAttr *AA,
129506f32e7eSjoerg                                    SourceLocation L);
129606f32e7eSjoerg 
129706f32e7eSjoerg   /// Add global annotations that are set on D, for the global GV. Those
129806f32e7eSjoerg   /// annotations are emitted during finalization of the LLVM code.
129906f32e7eSjoerg   void AddGlobalAnnotations(const ValueDecl *D, llvm::GlobalValue *GV);
130006f32e7eSjoerg 
1301*13fbcb42Sjoerg   bool isInNoSanitizeList(SanitizerMask Kind, llvm::Function *Fn,
130206f32e7eSjoerg                           SourceLocation Loc) const;
130306f32e7eSjoerg 
1304*13fbcb42Sjoerg   bool isInNoSanitizeList(llvm::GlobalVariable *GV, SourceLocation Loc,
1305*13fbcb42Sjoerg                           QualType Ty, StringRef Category = StringRef()) const;
130606f32e7eSjoerg 
130706f32e7eSjoerg   /// Imbue XRay attributes to a function, applying the always/never attribute
130806f32e7eSjoerg   /// lists in the process. Returns true if we did imbue attributes this way,
130906f32e7eSjoerg   /// false otherwise.
131006f32e7eSjoerg   bool imbueXRayAttrs(llvm::Function *Fn, SourceLocation Loc,
131106f32e7eSjoerg                       StringRef Category = StringRef()) const;
131206f32e7eSjoerg 
1313*13fbcb42Sjoerg   /// Returns true if function at the given location should be excluded from
1314*13fbcb42Sjoerg   /// profile instrumentation.
1315*13fbcb42Sjoerg   bool isProfileInstrExcluded(llvm::Function *Fn, SourceLocation Loc) const;
1316*13fbcb42Sjoerg 
getSanitizerMetadata()131706f32e7eSjoerg   SanitizerMetadata *getSanitizerMetadata() {
131806f32e7eSjoerg     return SanitizerMD.get();
131906f32e7eSjoerg   }
132006f32e7eSjoerg 
addDeferredVTable(const CXXRecordDecl * RD)132106f32e7eSjoerg   void addDeferredVTable(const CXXRecordDecl *RD) {
132206f32e7eSjoerg     DeferredVTables.push_back(RD);
132306f32e7eSjoerg   }
132406f32e7eSjoerg 
132506f32e7eSjoerg   /// Emit code for a single global function or var decl. Forward declarations
132606f32e7eSjoerg   /// are emitted lazily.
132706f32e7eSjoerg   void EmitGlobal(GlobalDecl D);
132806f32e7eSjoerg 
132906f32e7eSjoerg   bool TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D);
133006f32e7eSjoerg 
133106f32e7eSjoerg   llvm::GlobalValue *GetGlobalValue(StringRef Ref);
133206f32e7eSjoerg 
133306f32e7eSjoerg   /// Set attributes which are common to any form of a global definition (alias,
133406f32e7eSjoerg   /// Objective-C method, function, global variable).
133506f32e7eSjoerg   ///
133606f32e7eSjoerg   /// NOTE: This should only be called for definitions.
133706f32e7eSjoerg   void SetCommonAttributes(GlobalDecl GD, llvm::GlobalValue *GV);
133806f32e7eSjoerg 
133906f32e7eSjoerg   void addReplacement(StringRef Name, llvm::Constant *C);
134006f32e7eSjoerg 
134106f32e7eSjoerg   void addGlobalValReplacement(llvm::GlobalValue *GV, llvm::Constant *C);
134206f32e7eSjoerg 
134306f32e7eSjoerg   /// Emit a code for threadprivate directive.
134406f32e7eSjoerg   /// \param D Threadprivate declaration.
134506f32e7eSjoerg   void EmitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D);
134606f32e7eSjoerg 
134706f32e7eSjoerg   /// Emit a code for declare reduction construct.
134806f32e7eSjoerg   void EmitOMPDeclareReduction(const OMPDeclareReductionDecl *D,
134906f32e7eSjoerg                                CodeGenFunction *CGF = nullptr);
135006f32e7eSjoerg 
135106f32e7eSjoerg   /// Emit a code for declare mapper construct.
135206f32e7eSjoerg   void EmitOMPDeclareMapper(const OMPDeclareMapperDecl *D,
135306f32e7eSjoerg                             CodeGenFunction *CGF = nullptr);
135406f32e7eSjoerg 
135506f32e7eSjoerg   /// Emit a code for requires directive.
135606f32e7eSjoerg   /// \param D Requires declaration
135706f32e7eSjoerg   void EmitOMPRequiresDecl(const OMPRequiresDecl *D);
135806f32e7eSjoerg 
1359*13fbcb42Sjoerg   /// Emit a code for the allocate directive.
1360*13fbcb42Sjoerg   /// \param D The allocate declaration
1361*13fbcb42Sjoerg   void EmitOMPAllocateDecl(const OMPAllocateDecl *D);
136206f32e7eSjoerg 
136306f32e7eSjoerg   /// Returns whether the given record has hidden LTO visibility and therefore
136406f32e7eSjoerg   /// may participate in (single-module) CFI and whole-program vtable
136506f32e7eSjoerg   /// optimization.
136606f32e7eSjoerg   bool HasHiddenLTOVisibility(const CXXRecordDecl *RD);
136706f32e7eSjoerg 
1368*13fbcb42Sjoerg   /// Returns whether the given record has public std LTO visibility
1369*13fbcb42Sjoerg   /// and therefore may not participate in (single-module) CFI and whole-program
1370*13fbcb42Sjoerg   /// vtable optimization.
1371*13fbcb42Sjoerg   bool HasLTOVisibilityPublicStd(const CXXRecordDecl *RD);
1372*13fbcb42Sjoerg 
137306f32e7eSjoerg   /// Returns the vcall visibility of the given type. This is the scope in which
137406f32e7eSjoerg   /// a virtual function call could be made which ends up being dispatched to a
137506f32e7eSjoerg   /// member function of this class. This scope can be wider than the visibility
137606f32e7eSjoerg   /// of the class itself when the class has a more-visible dynamic base class.
1377*13fbcb42Sjoerg   /// The client should pass in an empty Visited set, which is used to prevent
1378*13fbcb42Sjoerg   /// redundant recursive processing.
137906f32e7eSjoerg   llvm::GlobalObject::VCallVisibility
1380*13fbcb42Sjoerg   GetVCallVisibilityLevel(const CXXRecordDecl *RD,
1381*13fbcb42Sjoerg                           llvm::DenseSet<const CXXRecordDecl *> &Visited);
138206f32e7eSjoerg 
138306f32e7eSjoerg   /// Emit type metadata for the given vtable using the given layout.
138406f32e7eSjoerg   void EmitVTableTypeMetadata(const CXXRecordDecl *RD,
138506f32e7eSjoerg                               llvm::GlobalVariable *VTable,
138606f32e7eSjoerg                               const VTableLayout &VTLayout);
138706f32e7eSjoerg 
138806f32e7eSjoerg   /// Generate a cross-DSO type identifier for MD.
138906f32e7eSjoerg   llvm::ConstantInt *CreateCrossDsoCfiTypeId(llvm::Metadata *MD);
139006f32e7eSjoerg 
139106f32e7eSjoerg   /// Create a metadata identifier for the given type. This may either be an
139206f32e7eSjoerg   /// MDString (for external identifiers) or a distinct unnamed MDNode (for
139306f32e7eSjoerg   /// internal identifiers).
139406f32e7eSjoerg   llvm::Metadata *CreateMetadataIdentifierForType(QualType T);
139506f32e7eSjoerg 
139606f32e7eSjoerg   /// Create a metadata identifier that is intended to be used to check virtual
139706f32e7eSjoerg   /// calls via a member function pointer.
139806f32e7eSjoerg   llvm::Metadata *CreateMetadataIdentifierForVirtualMemPtrType(QualType T);
139906f32e7eSjoerg 
140006f32e7eSjoerg   /// Create a metadata identifier for the generalization of the given type.
140106f32e7eSjoerg   /// This may either be an MDString (for external identifiers) or a distinct
140206f32e7eSjoerg   /// unnamed MDNode (for internal identifiers).
140306f32e7eSjoerg   llvm::Metadata *CreateMetadataIdentifierGeneralized(QualType T);
140406f32e7eSjoerg 
140506f32e7eSjoerg   /// Create and attach type metadata to the given function.
140606f32e7eSjoerg   void CreateFunctionTypeMetadataForIcall(const FunctionDecl *FD,
140706f32e7eSjoerg                                           llvm::Function *F);
140806f32e7eSjoerg 
1409*13fbcb42Sjoerg   /// Whether this function's return type has no side effects, and thus may
1410*13fbcb42Sjoerg   /// be trivially discarded if it is unused.
1411*13fbcb42Sjoerg   bool MayDropFunctionReturn(const ASTContext &Context, QualType ReturnType);
1412*13fbcb42Sjoerg 
141306f32e7eSjoerg   /// Returns whether this module needs the "all-vtables" type identifier.
141406f32e7eSjoerg   bool NeedAllVtablesTypeId() const;
141506f32e7eSjoerg 
141606f32e7eSjoerg   /// Create and attach type metadata for the given vtable.
141706f32e7eSjoerg   void AddVTableTypeMetadata(llvm::GlobalVariable *VTable, CharUnits Offset,
141806f32e7eSjoerg                              const CXXRecordDecl *RD);
141906f32e7eSjoerg 
142006f32e7eSjoerg   /// Return a vector of most-base classes for RD. This is used to implement
142106f32e7eSjoerg   /// control flow integrity checks for member function pointers.
142206f32e7eSjoerg   ///
142306f32e7eSjoerg   /// A most-base class of a class C is defined as a recursive base class of C,
142406f32e7eSjoerg   /// including C itself, that does not have any bases.
142506f32e7eSjoerg   std::vector<const CXXRecordDecl *>
142606f32e7eSjoerg   getMostBaseClasses(const CXXRecordDecl *RD);
142706f32e7eSjoerg 
142806f32e7eSjoerg   /// Get the declaration of std::terminate for the platform.
142906f32e7eSjoerg   llvm::FunctionCallee getTerminateFn();
143006f32e7eSjoerg 
143106f32e7eSjoerg   llvm::SanitizerStatReport &getSanStats();
143206f32e7eSjoerg 
143306f32e7eSjoerg   llvm::Value *
143406f32e7eSjoerg   createOpenCLIntToSamplerConversion(const Expr *E, CodeGenFunction &CGF);
143506f32e7eSjoerg 
143606f32e7eSjoerg   /// OpenCL v1.2 s5.6.4.6 allows the compiler to store kernel argument
143706f32e7eSjoerg   /// information in the program executable. The argument information stored
143806f32e7eSjoerg   /// includes the argument name, its type, the address and access qualifiers
143906f32e7eSjoerg   /// used. This helper can be used to generate metadata for source code kernel
144006f32e7eSjoerg   /// function as well as generated implicitly kernels. If a kernel is generated
144106f32e7eSjoerg   /// implicitly null value has to be passed to the last two parameters,
144206f32e7eSjoerg   /// otherwise all parameters must have valid non-null values.
144306f32e7eSjoerg   /// \param FN is a pointer to IR function being generated.
144406f32e7eSjoerg   /// \param FD is a pointer to function declaration if any.
144506f32e7eSjoerg   /// \param CGF is a pointer to CodeGenFunction that generates this function.
144606f32e7eSjoerg   void GenOpenCLArgMetadata(llvm::Function *FN,
144706f32e7eSjoerg                             const FunctionDecl *FD = nullptr,
144806f32e7eSjoerg                             CodeGenFunction *CGF = nullptr);
144906f32e7eSjoerg 
145006f32e7eSjoerg   /// Get target specific null pointer.
145106f32e7eSjoerg   /// \param T is the LLVM type of the null pointer.
145206f32e7eSjoerg   /// \param QT is the clang QualType of the null pointer.
145306f32e7eSjoerg   llvm::Constant *getNullPointer(llvm::PointerType *T, QualType QT);
145406f32e7eSjoerg 
1455*13fbcb42Sjoerg   CharUnits getNaturalTypeAlignment(QualType T,
1456*13fbcb42Sjoerg                                     LValueBaseInfo *BaseInfo = nullptr,
1457*13fbcb42Sjoerg                                     TBAAAccessInfo *TBAAInfo = nullptr,
1458*13fbcb42Sjoerg                                     bool forPointeeType = false);
1459*13fbcb42Sjoerg   CharUnits getNaturalPointeeTypeAlignment(QualType T,
1460*13fbcb42Sjoerg                                            LValueBaseInfo *BaseInfo = nullptr,
1461*13fbcb42Sjoerg                                            TBAAAccessInfo *TBAAInfo = nullptr);
1462*13fbcb42Sjoerg   bool stopAutoInit();
1463*13fbcb42Sjoerg 
1464*13fbcb42Sjoerg   /// Print the postfix for externalized static variable for single source
1465*13fbcb42Sjoerg   /// offloading languages CUDA and HIP.
1466*13fbcb42Sjoerg   void printPostfixForExternalizedStaticVar(llvm::raw_ostream &OS) const;
1467*13fbcb42Sjoerg 
146806f32e7eSjoerg private:
146906f32e7eSjoerg   llvm::Constant *GetOrCreateLLVMFunction(
147006f32e7eSjoerg       StringRef MangledName, llvm::Type *Ty, GlobalDecl D, bool ForVTable,
147106f32e7eSjoerg       bool DontDefer = false, bool IsThunk = false,
147206f32e7eSjoerg       llvm::AttributeList ExtraAttrs = llvm::AttributeList(),
147306f32e7eSjoerg       ForDefinition_t IsForDefinition = NotForDefinition);
147406f32e7eSjoerg 
147506f32e7eSjoerg   llvm::Constant *GetOrCreateMultiVersionResolver(GlobalDecl GD,
147606f32e7eSjoerg                                                   llvm::Type *DeclTy,
147706f32e7eSjoerg                                                   const FunctionDecl *FD);
147806f32e7eSjoerg   void UpdateMultiVersionNames(GlobalDecl GD, const FunctionDecl *FD);
147906f32e7eSjoerg 
1480*13fbcb42Sjoerg   llvm::Constant *
1481*13fbcb42Sjoerg   GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty,
1482*13fbcb42Sjoerg                         unsigned AddrSpace, const VarDecl *D,
1483*13fbcb42Sjoerg                         ForDefinition_t IsForDefinition = NotForDefinition);
148406f32e7eSjoerg 
148506f32e7eSjoerg   bool GetCPUAndFeaturesAttributes(GlobalDecl GD,
148606f32e7eSjoerg                                    llvm::AttrBuilder &AttrBuilder);
148706f32e7eSjoerg   void setNonAliasAttributes(GlobalDecl GD, llvm::GlobalObject *GO);
148806f32e7eSjoerg 
148906f32e7eSjoerg   /// Set function attributes for a function declaration.
149006f32e7eSjoerg   void SetFunctionAttributes(GlobalDecl GD, llvm::Function *F,
149106f32e7eSjoerg                              bool IsIncompleteFunction, bool IsThunk);
149206f32e7eSjoerg 
149306f32e7eSjoerg   void EmitGlobalDefinition(GlobalDecl D, llvm::GlobalValue *GV = nullptr);
149406f32e7eSjoerg 
149506f32e7eSjoerg   void EmitGlobalFunctionDefinition(GlobalDecl GD, llvm::GlobalValue *GV);
149606f32e7eSjoerg   void EmitMultiVersionFunctionDefinition(GlobalDecl GD, llvm::GlobalValue *GV);
149706f32e7eSjoerg 
149806f32e7eSjoerg   void EmitGlobalVarDefinition(const VarDecl *D, bool IsTentative = false);
1499*13fbcb42Sjoerg   void EmitExternalVarDeclaration(const VarDecl *D);
150006f32e7eSjoerg   void EmitAliasDefinition(GlobalDecl GD);
150106f32e7eSjoerg   void emitIFuncDefinition(GlobalDecl GD);
150206f32e7eSjoerg   void emitCPUDispatchDefinition(GlobalDecl GD);
150306f32e7eSjoerg   void EmitObjCPropertyImplementations(const ObjCImplementationDecl *D);
150406f32e7eSjoerg   void EmitObjCIvarInitializations(ObjCImplementationDecl *D);
150506f32e7eSjoerg 
150606f32e7eSjoerg   // C++ related functions.
150706f32e7eSjoerg 
150806f32e7eSjoerg   void EmitDeclContext(const DeclContext *DC);
150906f32e7eSjoerg   void EmitLinkageSpec(const LinkageSpecDecl *D);
151006f32e7eSjoerg 
151106f32e7eSjoerg   /// Emit the function that initializes C++ thread_local variables.
151206f32e7eSjoerg   void EmitCXXThreadLocalInitFunc();
151306f32e7eSjoerg 
151406f32e7eSjoerg   /// Emit the function that initializes C++ globals.
151506f32e7eSjoerg   void EmitCXXGlobalInitFunc();
151606f32e7eSjoerg 
1517*13fbcb42Sjoerg   /// Emit the function that performs cleanup associated with C++ globals.
1518*13fbcb42Sjoerg   void EmitCXXGlobalCleanUpFunc();
151906f32e7eSjoerg 
152006f32e7eSjoerg   /// Emit the function that initializes the specified global (if PerformInit is
152106f32e7eSjoerg   /// true) and registers its destructor.
152206f32e7eSjoerg   void EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
152306f32e7eSjoerg                                     llvm::GlobalVariable *Addr,
152406f32e7eSjoerg                                     bool PerformInit);
152506f32e7eSjoerg 
152606f32e7eSjoerg   void EmitPointerToInitFunc(const VarDecl *VD, llvm::GlobalVariable *Addr,
152706f32e7eSjoerg                              llvm::Function *InitFunc, InitSegAttr *ISA);
152806f32e7eSjoerg 
152906f32e7eSjoerg   // FIXME: Hardcoding priority here is gross.
153006f32e7eSjoerg   void AddGlobalCtor(llvm::Function *Ctor, int Priority = 65535,
153106f32e7eSjoerg                      llvm::Constant *AssociatedData = nullptr);
1532*13fbcb42Sjoerg   void AddGlobalDtor(llvm::Function *Dtor, int Priority = 65535,
1533*13fbcb42Sjoerg                      bool IsDtorAttrFunc = false);
153406f32e7eSjoerg 
153506f32e7eSjoerg   /// EmitCtorList - Generates a global array of functions and priorities using
153606f32e7eSjoerg   /// the given list and name. This array will have appending linkage and is
153706f32e7eSjoerg   /// suitable for use as a LLVM constructor or destructor array. Clears Fns.
153806f32e7eSjoerg   void EmitCtorList(CtorList &Fns, const char *GlobalName);
153906f32e7eSjoerg 
154006f32e7eSjoerg   /// Emit any needed decls for which code generation was deferred.
154106f32e7eSjoerg   void EmitDeferred();
154206f32e7eSjoerg 
154306f32e7eSjoerg   /// Try to emit external vtables as available_externally if they have emitted
154406f32e7eSjoerg   /// all inlined virtual functions.  It runs after EmitDeferred() and therefore
154506f32e7eSjoerg   /// is not allowed to create new references to things that need to be emitted
154606f32e7eSjoerg   /// lazily.
154706f32e7eSjoerg   void EmitVTablesOpportunistically();
154806f32e7eSjoerg 
154906f32e7eSjoerg   /// Call replaceAllUsesWith on all pairs in Replacements.
155006f32e7eSjoerg   void applyReplacements();
155106f32e7eSjoerg 
155206f32e7eSjoerg   /// Call replaceAllUsesWith on all pairs in GlobalValReplacements.
155306f32e7eSjoerg   void applyGlobalValReplacements();
155406f32e7eSjoerg 
155506f32e7eSjoerg   void checkAliases();
155606f32e7eSjoerg 
155706f32e7eSjoerg   std::map<int, llvm::TinyPtrVector<llvm::Function *>> DtorsUsingAtExit;
155806f32e7eSjoerg 
155906f32e7eSjoerg   /// Register functions annotated with __attribute__((destructor)) using
156006f32e7eSjoerg   /// __cxa_atexit, if it is available, or atexit otherwise.
156106f32e7eSjoerg   void registerGlobalDtorsWithAtExit();
156206f32e7eSjoerg 
1563*13fbcb42Sjoerg   // When using sinit and sterm functions, unregister
1564*13fbcb42Sjoerg   // __attribute__((destructor)) annotated functions which were previously
1565*13fbcb42Sjoerg   // registered by the atexit subroutine using unatexit.
1566*13fbcb42Sjoerg   void unregisterGlobalDtorsWithUnAtExit();
1567*13fbcb42Sjoerg 
156806f32e7eSjoerg   void emitMultiVersionFunctions();
156906f32e7eSjoerg 
157006f32e7eSjoerg   /// Emit any vtables which we deferred and still have a use for.
157106f32e7eSjoerg   void EmitDeferredVTables();
157206f32e7eSjoerg 
157306f32e7eSjoerg   /// Emit a dummy function that reference a CoreFoundation symbol when
157406f32e7eSjoerg   /// @available is used on Darwin.
157506f32e7eSjoerg   void emitAtAvailableLinkGuard();
157606f32e7eSjoerg 
157706f32e7eSjoerg   /// Emit the llvm.used and llvm.compiler.used metadata.
157806f32e7eSjoerg   void emitLLVMUsed();
157906f32e7eSjoerg 
158006f32e7eSjoerg   /// Emit the link options introduced by imported modules.
158106f32e7eSjoerg   void EmitModuleLinkOptions();
158206f32e7eSjoerg 
158306f32e7eSjoerg   /// Emit aliases for internal-linkage declarations inside "C" language
158406f32e7eSjoerg   /// linkage specifications, giving them the "expected" name where possible.
158506f32e7eSjoerg   void EmitStaticExternCAliases();
158606f32e7eSjoerg 
158706f32e7eSjoerg   void EmitDeclMetadata();
158806f32e7eSjoerg 
158906f32e7eSjoerg   /// Emit the Clang version as llvm.ident metadata.
159006f32e7eSjoerg   void EmitVersionIdentMetadata();
159106f32e7eSjoerg 
159206f32e7eSjoerg   /// Emit the Clang commandline as llvm.commandline metadata.
159306f32e7eSjoerg   void EmitCommandLineMetadata();
159406f32e7eSjoerg 
1595*13fbcb42Sjoerg   /// Emit the module flag metadata used to pass options controlling the
1596*13fbcb42Sjoerg   /// the backend to LLVM.
1597*13fbcb42Sjoerg   void EmitBackendOptionsMetadata(const CodeGenOptions CodeGenOpts);
159806f32e7eSjoerg 
159906f32e7eSjoerg   /// Emits OpenCL specific Metadata e.g. OpenCL version.
160006f32e7eSjoerg   void EmitOpenCLMetadata();
160106f32e7eSjoerg 
160206f32e7eSjoerg   /// Emit the llvm.gcov metadata used to tell LLVM where to emit the .gcno and
160306f32e7eSjoerg   /// .gcda files in a way that persists in .bc files.
160406f32e7eSjoerg   void EmitCoverageFile();
160506f32e7eSjoerg 
160606f32e7eSjoerg   /// Determine whether the definition must be emitted; if this returns \c
160706f32e7eSjoerg   /// false, the definition can be emitted lazily if it's used.
160806f32e7eSjoerg   bool MustBeEmitted(const ValueDecl *D);
160906f32e7eSjoerg 
161006f32e7eSjoerg   /// Determine whether the definition can be emitted eagerly, or should be
161106f32e7eSjoerg   /// delayed until the end of the translation unit. This is relevant for
161206f32e7eSjoerg   /// definitions whose linkage can change, e.g. implicit function instantions
161306f32e7eSjoerg   /// which may later be explicitly instantiated.
161406f32e7eSjoerg   bool MayBeEmittedEagerly(const ValueDecl *D);
161506f32e7eSjoerg 
161606f32e7eSjoerg   /// Check whether we can use a "simpler", more core exceptions personality
161706f32e7eSjoerg   /// function.
161806f32e7eSjoerg   void SimplifyPersonality();
161906f32e7eSjoerg 
1620*13fbcb42Sjoerg   /// Helper function for ConstructAttributeList and
1621*13fbcb42Sjoerg   /// addDefaultFunctionDefinitionAttributes.  Builds a set of function
1622*13fbcb42Sjoerg   /// attributes to add to a function with the given properties.
1623*13fbcb42Sjoerg   void getDefaultFunctionAttributes(StringRef Name, bool HasOptnone,
162406f32e7eSjoerg                                     bool AttrOnCallSite,
162506f32e7eSjoerg                                     llvm::AttrBuilder &FuncAttrs);
162606f32e7eSjoerg 
162706f32e7eSjoerg   llvm::Metadata *CreateMetadataIdentifierImpl(QualType T, MetadataTypeMap &Map,
162806f32e7eSjoerg                                                StringRef Suffix);
162906f32e7eSjoerg };
163006f32e7eSjoerg 
163106f32e7eSjoerg }  // end namespace CodeGen
163206f32e7eSjoerg }  // end namespace clang
163306f32e7eSjoerg 
163406f32e7eSjoerg #endif // LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H
1635