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