blob: 94904997d629172b0c231067e37e7da6d9bcd039 [file] [log] [blame]
Daniel Dunbarbd012ff2008-07-29 23:18:29 +00001//===--- CodeGenModule.h - Per-Module state for LLVM CodeGen ----*- C++ -*-===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Mike Stumpecc90e92009-02-13 19:12:34 +000010// This is the internal per-translation-unit state used for llvm translation.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13
Stephen Hines176edba2014-12-01 14:53:08 -080014#ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H
15#define LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H
Reid Spencer5f016e22007-07-11 17:01:13 +000016
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "CGVTables.h"
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -080018#include "CodeGenTypeCache.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000019#include "CodeGenTypes.h"
Stephen Hines176edba2014-12-01 14:53:08 -080020#include "SanitizerMetadata.h"
Dan Gohman4f8d1232008-05-22 00:50:06 +000021#include "clang/AST/Attr.h"
Anders Carlsson2a131fb2009-05-05 04:44:02 +000022#include "clang/AST/DeclCXX.h"
Anders Carlsson0ff8baf2009-09-11 00:07:24 +000023#include "clang/AST/DeclObjC.h"
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -070024#include "clang/AST/DeclOpenMP.h"
Peter Collingbournefd05ca02011-06-14 04:02:39 +000025#include "clang/AST/GlobalDecl.h"
Peter Collingbourne14110472011-01-13 18:57:25 +000026#include "clang/AST/Mangle.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000027#include "clang/Basic/ABI.h"
28#include "clang/Basic/LangOptions.h"
Douglas Gregorb6cbe512013-01-14 17:21:00 +000029#include "clang/Basic/Module.h"
Stephen Hines176edba2014-12-01 14:53:08 -080030#include "clang/Basic/SanitizerBlacklist.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000031#include "llvm/ADT/DenseMap.h"
Douglas Gregorb6cbe512013-01-14 17:21:00 +000032#include "llvm/ADT/SetVector.h"
Rafael Espindola6a836702010-03-04 18:17:24 +000033#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000034#include "llvm/ADT/StringMap.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000035#include "llvm/IR/Module.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070036#include "llvm/IR/ValueHandle.h"
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -070037#include "llvm/Transforms/Utils/SanitizerStats.h"
Anders Carlssonb723f752009-01-04 02:08:04 +000038
Reid Spencer5f016e22007-07-11 17:01:13 +000039namespace llvm {
Stephen Hines6bcf27b2014-05-29 04:14:42 -070040class Module;
41class Constant;
42class ConstantInt;
43class Function;
44class GlobalValue;
45class DataLayout;
46class FunctionType;
47class LLVMContext;
48class IndexedInstrProfReader;
Reid Spencer5f016e22007-07-11 17:01:13 +000049}
50
51namespace clang {
Stephen Hines6bcf27b2014-05-29 04:14:42 -070052class ASTContext;
53class AtomicType;
54class FunctionDecl;
55class IdentifierInfo;
56class ObjCMethodDecl;
57class ObjCImplementationDecl;
58class ObjCCategoryImplDecl;
59class ObjCProtocolDecl;
60class ObjCEncodeExpr;
61class BlockExpr;
62class CharUnits;
63class Decl;
64class Expr;
65class Stmt;
66class InitListExpr;
67class StringLiteral;
68class NamedDecl;
69class ValueDecl;
70class VarDecl;
71class LangOptions;
72class CodeGenOptions;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -080073class HeaderSearchOptions;
74class PreprocessorOptions;
Stephen Hines6bcf27b2014-05-29 04:14:42 -070075class DiagnosticsEngine;
76class AnnotateAttr;
77class CXXDestructorDecl;
Stephen Hines6bcf27b2014-05-29 04:14:42 -070078class Module;
Stephen Hines176edba2014-12-01 14:53:08 -080079class CoverageSourceInfo;
Mike Stumpecc90e92009-02-13 19:12:34 +000080
Reid Spencer5f016e22007-07-11 17:01:13 +000081namespace CodeGen {
82
Stephen Hines6bcf27b2014-05-29 04:14:42 -070083class CallArgList;
84class CodeGenFunction;
85class CodeGenTBAA;
86class CGCXXABI;
87class CGDebugInfo;
88class CGObjCRuntime;
89class CGOpenCLRuntime;
90class CGOpenMPRuntime;
91class CGCUDARuntime;
92class BlockFieldFlags;
93class FunctionArgList;
Stephen Hines176edba2014-12-01 14:53:08 -080094class CoverageMappingModuleGen;
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -070095class TargetCodeGenInfo;
Stephen Hines651f13c2014-04-23 16:59:28 -070096
Stephen Hines6bcf27b2014-05-29 04:14:42 -070097struct OrderGlobalInits {
98 unsigned int priority;
99 unsigned int lex_order;
100 OrderGlobalInits(unsigned int p, unsigned int l)
Chris Lattnerec2830d2010-06-27 06:32:58 +0000101 : priority(p), lex_order(l) {}
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700102
103 bool operator==(const OrderGlobalInits &RHS) const {
104 return priority == RHS.priority && lex_order == RHS.lex_order;
105 }
106
107 bool operator<(const OrderGlobalInits &RHS) const {
108 return std::tie(priority, lex_order) <
109 std::tie(RHS.priority, RHS.lex_order);
110 }
111};
112
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800113struct ObjCEntrypoints {
114 ObjCEntrypoints() { memset(this, 0, sizeof(*this)); }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700115
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800116 /// void objc_autoreleasePoolPop(void*);
John McCallf85e1932011-06-15 23:02:42 +0000117 llvm::Constant *objc_autoreleasePoolPop;
118
119 /// void *objc_autoreleasePoolPush(void);
120 llvm::Constant *objc_autoreleasePoolPush;
John McCallf85e1932011-06-15 23:02:42 +0000121
122 /// id objc_autorelease(id);
123 llvm::Constant *objc_autorelease;
124
125 /// id objc_autoreleaseReturnValue(id);
126 llvm::Constant *objc_autoreleaseReturnValue;
127
128 /// void objc_copyWeak(id *dest, id *src);
129 llvm::Constant *objc_copyWeak;
130
131 /// void objc_destroyWeak(id*);
132 llvm::Constant *objc_destroyWeak;
133
134 /// id objc_initWeak(id*, id);
135 llvm::Constant *objc_initWeak;
136
137 /// id objc_loadWeak(id*);
138 llvm::Constant *objc_loadWeak;
139
140 /// id objc_loadWeakRetained(id*);
141 llvm::Constant *objc_loadWeakRetained;
142
143 /// void objc_moveWeak(id *dest, id *src);
144 llvm::Constant *objc_moveWeak;
145
146 /// id objc_retain(id);
147 llvm::Constant *objc_retain;
148
149 /// id objc_retainAutorelease(id);
150 llvm::Constant *objc_retainAutorelease;
151
152 /// id objc_retainAutoreleaseReturnValue(id);
153 llvm::Constant *objc_retainAutoreleaseReturnValue;
154
155 /// id objc_retainAutoreleasedReturnValue(id);
156 llvm::Constant *objc_retainAutoreleasedReturnValue;
157
158 /// id objc_retainBlock(id);
159 llvm::Constant *objc_retainBlock;
160
161 /// void objc_release(id);
162 llvm::Constant *objc_release;
163
164 /// id objc_storeStrong(id*, id);
165 llvm::Constant *objc_storeStrong;
166
167 /// id objc_storeWeak(id*, id);
168 llvm::Constant *objc_storeWeak;
169
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700170 /// id objc_unsafeClaimAutoreleasedReturnValue(id);
171 llvm::Constant *objc_unsafeClaimAutoreleasedReturnValue;
172
John McCallf85e1932011-06-15 23:02:42 +0000173 /// A void(void) inline asm to use to mark that the return value of
174 /// a call will be immediately retain.
175 llvm::InlineAsm *retainAutoreleasedReturnValueMarker;
John McCallb6a60792013-03-23 02:35:54 +0000176
177 /// void clang.arc.use(...);
178 llvm::Constant *clang_arc_use;
John McCallf85e1932011-06-15 23:02:42 +0000179};
Will Dietz4f45bc02013-01-18 11:30:38 +0000180
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700181/// This class records statistics on instrumentation based profiling.
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700182class InstrProfStats {
183 uint32_t VisitedInMainFile;
184 uint32_t MissingInMainFile;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700185 uint32_t Visited;
186 uint32_t Missing;
187 uint32_t Mismatched;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700188
189public:
190 InstrProfStats()
191 : VisitedInMainFile(0), MissingInMainFile(0), Visited(0), Missing(0),
192 Mismatched(0) {}
193 /// Record that we've visited a function and whether or not that function was
194 /// in the main source file.
195 void addVisited(bool MainFile) {
196 if (MainFile)
197 ++VisitedInMainFile;
198 ++Visited;
199 }
200 /// Record that a function we've visited has no profile data.
201 void addMissing(bool MainFile) {
202 if (MainFile)
203 ++MissingInMainFile;
204 ++Missing;
205 }
206 /// Record that a function we've visited has mismatched profile data.
207 void addMismatched(bool MainFile) { ++Mismatched; }
208 /// Whether or not the stats we've gathered indicate any potential problems.
209 bool hasDiagnostics() { return Missing || Mismatched; }
210 /// Report potential problems we've found to \c Diags.
211 void reportDiagnostics(DiagnosticsEngine &Diags, StringRef MainFile);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700212};
213
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800214/// A pair of helper functions for a __block variable.
215class BlockByrefHelpers : public llvm::FoldingSetNode {
216 // MSVC requires this type to be complete in order to process this
217 // header.
218public:
219 llvm::Constant *CopyHelper;
220 llvm::Constant *DisposeHelper;
221
222 /// The alignment of the field. This is important because
223 /// different offsets to the field within the byref struct need to
224 /// have different helper functions.
225 CharUnits Alignment;
226
227 BlockByrefHelpers(CharUnits alignment) : Alignment(alignment) {}
228 BlockByrefHelpers(const BlockByrefHelpers &) = default;
229 virtual ~BlockByrefHelpers();
230
231 void Profile(llvm::FoldingSetNodeID &id) const {
232 id.AddInteger(Alignment.getQuantity());
233 profileImpl(id);
234 }
235 virtual void profileImpl(llvm::FoldingSetNodeID &id) const = 0;
236
237 virtual bool needsCopy() const { return true; }
238 virtual void emitCopy(CodeGenFunction &CGF, Address dest, Address src) = 0;
239
240 virtual bool needsDispose() const { return true; }
241 virtual void emitDispose(CodeGenFunction &CGF, Address field) = 0;
242};
243
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700244/// This class organizes the cross-function state that is used while generating
245/// LLVM code.
John McCall5936e332011-02-15 09:22:45 +0000246class CodeGenModule : public CodeGenTypeCache {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700247 CodeGenModule(const CodeGenModule &) = delete;
248 void operator=(const CodeGenModule &) = delete;
Anders Carlsson8a219ce2009-02-24 04:21:31 +0000249
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700250public:
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700251 struct Structor {
252 Structor() : Priority(0), Initializer(nullptr), AssociatedData(nullptr) {}
253 Structor(int Priority, llvm::Constant *Initializer,
254 llvm::Constant *AssociatedData)
255 : Priority(Priority), Initializer(Initializer),
256 AssociatedData(AssociatedData) {}
257 int Priority;
258 llvm::Constant *Initializer;
259 llvm::Constant *AssociatedData;
260 };
261
262 typedef std::vector<Structor> CtorList;
Daniel Dunbar6bfed7e2008-08-01 00:01:51 +0000263
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700264private:
Reid Spencer5f016e22007-07-11 17:01:13 +0000265 ASTContext &Context;
David Blaikie4e4d0842012-03-11 07:00:24 +0000266 const LangOptions &LangOpts;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800267 const HeaderSearchOptions &HeaderSearchOpts; // Only used for debug info.
268 const PreprocessorOptions &PreprocessorOpts; // Only used for debug info.
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000269 const CodeGenOptions &CodeGenOpts;
Reid Spencer5f016e22007-07-11 17:01:13 +0000270 llvm::Module &TheModule;
David Blaikied6471f72011-09-25 23:23:43 +0000271 DiagnosticsEngine &Diags;
John McCall64aa4b32013-04-16 22:48:15 +0000272 const TargetInfo &Target;
Stephen Hines651f13c2014-04-23 16:59:28 -0700273 std::unique_ptr<CGCXXABI> ABI;
John McCall64aa4b32013-04-16 22:48:15 +0000274 llvm::LLVMContext &VMContext;
Anders Carlssonb5404912009-10-07 01:06:45 +0000275
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700276 std::unique_ptr<CodeGenTBAA> TBAA;
John McCall64aa4b32013-04-16 22:48:15 +0000277
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700278 mutable std::unique_ptr<TargetCodeGenInfo> TheTargetCodeGenInfo;
John McCall64aa4b32013-04-16 22:48:15 +0000279
280 // This should not be moved earlier, since its initialization depends on some
281 // of the previous reference members being already initialized and also checks
282 // if TheTargetCodeGenInfo is NULL
283 CodeGenTypes Types;
284
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700285 /// Holds information about C++ vtables.
Anders Carlssonaf440352010-03-23 04:11:45 +0000286 CodeGenVTables VTables;
Douglas Gregore17ad2f2010-04-08 16:07:47 +0000287
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700288 std::unique_ptr<CGObjCRuntime> ObjCRuntime;
289 std::unique_ptr<CGOpenCLRuntime> OpenCLRuntime;
290 std::unique_ptr<CGOpenMPRuntime> OpenMPRuntime;
291 std::unique_ptr<CGCUDARuntime> CUDARuntime;
292 std::unique_ptr<CGDebugInfo> DebugInfo;
293 std::unique_ptr<ObjCEntrypoints> ObjCData;
294 llvm::MDNode *NoObjCARCExceptionsMetadata = nullptr;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700295 std::unique_ptr<llvm::IndexedInstrProfReader> PGOReader;
296 InstrProfStats PGOStats;
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700297 std::unique_ptr<llvm::SanitizerStatReport> SanStats;
Daniel Dunbar9986eab2008-07-30 16:32:24 +0000298
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700299 // A set of references that have only been seen via a weakref so far. This is
300 // used to remove the weak of the reference if we ever see a direct reference
301 // or a definition.
Rafael Espindola6a836702010-03-04 18:17:24 +0000302 llvm::SmallPtrSet<llvm::GlobalValue*, 10> WeakRefReferences;
303
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700304 /// This contains all the decls which have definitions but/ which are deferred
305 /// for emission and therefore should only be output if they are actually
306 /// used. If a decl is in this, then it is known to have not been referenced
307 /// yet.
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700308 std::map<StringRef, GlobalDecl> DeferredDecls;
Daniel Dunbar02698712009-02-13 20:29:50 +0000309
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700310 /// This is a list of deferred decls which we have seen that *are* actually
311 /// referenced. These get code generated when the module is done.
Stephen Hines651f13c2014-04-23 16:59:28 -0700312 struct DeferredGlobal {
313 DeferredGlobal(llvm::GlobalValue *GV, GlobalDecl GD) : GV(GV), GD(GD) {}
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700314 llvm::TrackingVH<llvm::GlobalValue> GV;
Stephen Hines651f13c2014-04-23 16:59:28 -0700315 GlobalDecl GD;
316 };
317 std::vector<DeferredGlobal> DeferredDeclsToEmit;
318 void addDeferredDeclToEmit(llvm::GlobalValue *GV, GlobalDecl GD) {
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700319 DeferredDeclsToEmit.emplace_back(GV, GD);
Stephen Hines651f13c2014-04-23 16:59:28 -0700320 }
Daniel Dunbar03f5ad92009-04-15 22:08:45 +0000321
Rafael Espindolad2054982013-10-22 19:26:13 +0000322 /// List of alias we have emitted. Used to make sure that what they point to
323 /// is defined once we get to the end of the of the translation unit.
324 std::vector<GlobalDecl> Aliases;
325
Rafael Espindola61a0a752013-11-05 21:37:29 +0000326 typedef llvm::StringMap<llvm::TrackingVH<llvm::Constant> > ReplacementsTy;
327 ReplacementsTy Replacements;
328
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800329 /// List of global values to be replaced with something else. Used when we
330 /// want to replace a GlobalValue but can't identify it by its mangled name
331 /// anymore (because the name is already taken).
332 llvm::SmallVector<std::pair<llvm::GlobalValue *, llvm::Constant *>, 8>
333 GlobalValReplacements;
334
335 /// Set of global decls for which we already diagnosed mangled name conflict.
336 /// Required to not issue a warning (on a mangling conflict) multiple times
337 /// for the same decl.
338 llvm::DenseSet<GlobalDecl> DiagnosedConflictingDefinitions;
339
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700340 /// A queue of (optional) vtables to consider emitting.
John McCalld5617ee2013-01-25 22:31:03 +0000341 std::vector<const CXXRecordDecl*> DeferredVTables;
342
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700343 /// List of global values which are required to be present in the object file;
344 /// bitcast to i8*. This is used for forcing visibility of symbols which may
345 /// otherwise be optimized out.
Chris Lattner35f38a22009-03-31 22:37:52 +0000346 std::vector<llvm::WeakVH> LLVMUsed;
Stephen Hines651f13c2014-04-23 16:59:28 -0700347 std::vector<llvm::WeakVH> LLVMCompilerUsed;
Mike Stumpecc90e92009-02-13 19:12:34 +0000348
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700349 /// Store the list of global constructors and their respective priorities to
350 /// be emitted when the translation unit is complete.
Daniel Dunbar6bfed7e2008-08-01 00:01:51 +0000351 CtorList GlobalCtors;
352
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700353 /// Store the list of global destructors and their respective priorities to be
354 /// emitted when the translation unit is complete.
Daniel Dunbar6bfed7e2008-08-01 00:01:51 +0000355 CtorList GlobalDtors;
356
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700357 /// An ordered map of canonical GlobalDecls to their mangled names.
358 llvm::MapVector<GlobalDecl, StringRef> MangledDeclNames;
359 llvm::StringMap<GlobalDecl, llvm::BumpPtrAllocator> Manglings;
360
Julien Lerouge77f68bb2011-09-09 22:41:49 +0000361 /// Global annotations.
Nate Begeman532485c2008-04-18 23:43:57 +0000362 std::vector<llvm::Constant*> Annotations;
Mike Stumpecc90e92009-02-13 19:12:34 +0000363
Julien Lerouge77f68bb2011-09-09 22:41:49 +0000364 /// Map used to get unique annotation strings.
365 llvm::StringMap<llvm::Constant*> AnnotationStrings;
366
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -0700367 llvm::StringMap<llvm::GlobalVariable *> CFConstantStringMap;
Stephen Hines651f13c2014-04-23 16:59:28 -0700368
Stephen Hines176edba2014-12-01 14:53:08 -0800369 llvm::DenseMap<llvm::Constant *, llvm::GlobalVariable *> ConstantStringMap;
Eli Friedman71cba342012-03-09 03:27:46 +0000370 llvm::DenseMap<const Decl*, llvm::Constant *> StaticLocalDeclMap;
John McCall355bba72012-03-30 21:00:39 +0000371 llvm::DenseMap<const Decl*, llvm::GlobalVariable*> StaticLocalDeclGuardMap;
Richard Smith211c8dd2013-06-05 00:46:14 +0000372 llvm::DenseMap<const Expr*, llvm::Constant *> MaterializedGlobalTemporaryMap;
373
Fariborz Jahanianb08cfb32012-01-08 19:13:23 +0000374 llvm::DenseMap<QualType, llvm::Constant *> AtomicSetterHelperFnMap;
375 llvm::DenseMap<QualType, llvm::Constant *> AtomicGetterHelperFnMap;
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000376
Will Dietz562d45c2013-11-08 01:09:22 +0000377 /// Map used to get unique type descriptor constants for sanitizers.
378 llvm::DenseMap<QualType, llvm::Constant *> TypeDescriptorMap;
379
Richard Smith00249372013-04-06 05:00:46 +0000380 /// Map used to track internal linkage functions declared within
381 /// extern "C" regions.
Richard Smith84083b72013-04-13 01:28:18 +0000382 typedef llvm::MapVector<IdentifierInfo *,
383 llvm::GlobalValue *> StaticExternCMap;
Richard Smith00249372013-04-06 05:00:46 +0000384 StaticExternCMap StaticExternCValues;
385
Richard Smithb80a16e2013-04-19 16:42:07 +0000386 /// \brief thread_local variables defined or used in this TU.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800387 std::vector<const VarDecl *> CXXThreadLocals;
Richard Smithb80a16e2013-04-19 16:42:07 +0000388
389 /// \brief thread_local variables with initializers that need to run
390 /// before any thread_local variable in this TU is odr-used.
Stephen Hines176edba2014-12-01 14:53:08 -0800391 std::vector<llvm::Function *> CXXThreadLocalInits;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800392 std::vector<const VarDecl *> CXXThreadLocalInitVars;
Richard Smithb80a16e2013-04-19 16:42:07 +0000393
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700394 /// Global variables with initializers that need to run before main.
Stephen Hines176edba2014-12-01 14:53:08 -0800395 std::vector<llvm::Function *> CXXGlobalInits;
John McCallbf40cb52010-07-15 23:40:35 +0000396
397 /// When a C++ decl with an initializer is deferred, null is
398 /// appended to CXXGlobalInits, and the index of that null is placed
399 /// here so that the initializer will be performed in the correct
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -0700400 /// order. Once the decl is emitted, the index is replaced with ~0U to ensure
401 /// that we don't re-emit the initializer.
John McCallbf40cb52010-07-15 23:40:35 +0000402 llvm::DenseMap<const Decl*, unsigned> DelayedCXXInitPosition;
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000403
Anton Korobeynikov4179ddd2012-11-06 22:44:45 +0000404 typedef std::pair<OrderGlobalInits, llvm::Function*> GlobalInitData;
405
406 struct GlobalInitPriorityCmp {
407 bool operator()(const GlobalInitData &LHS,
408 const GlobalInitData &RHS) const {
409 return LHS.first.priority < RHS.first.priority;
410 }
411 };
412
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700413 /// Global variables with initializers whose order of initialization is set by
414 /// init_priority attribute.
Anton Korobeynikov4179ddd2012-11-06 22:44:45 +0000415 SmallVector<GlobalInitData, 8> PrioritizedCXXGlobalInits;
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700417 /// Global destructor functions and arguments that need to run on termination.
Chris Lattner810112e2010-06-19 05:52:45 +0000418 std::vector<std::pair<llvm::WeakVH,llvm::Constant*> > CXXGlobalDtors;
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000419
Douglas Gregorb6cbe512013-01-14 17:21:00 +0000420 /// \brief The complete set of modules that has been imported.
421 llvm::SetVector<clang::Module *> ImportedModules;
422
Reid Kleckner3190ca92013-05-08 13:44:39 +0000423 /// \brief A vector of metadata strings.
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700424 SmallVector<llvm::Metadata *, 16> LinkerOptionsMetadata;
Reid Kleckner3190ca92013-05-08 13:44:39 +0000425
Douglas Gregor45c4ea72011-08-09 15:54:21 +0000426 /// @name Cache for Objective-C runtime types
427 /// @{
428
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700429 /// Cached reference to the class for constant strings. This value has type
430 /// int * but is actually an Obj-C class pointer.
Fariborz Jahanian428edb72013-04-16 15:25:39 +0000431 llvm::WeakVH CFConstantStringClassRef;
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700433 /// Cached reference to the class for constant strings. This value has type
434 /// int * but is actually an Obj-C class pointer.
Fariborz Jahanian428edb72013-04-16 15:25:39 +0000435 llvm::WeakVH ConstantStringClassRef;
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +0000436
Douglas Gregor45c4ea72011-08-09 15:54:21 +0000437 /// \brief The LLVM type corresponding to NSConstantString.
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700438 llvm::StructType *NSConstantStringType = nullptr;
439
Douglas Gregor0815b572011-08-09 17:23:49 +0000440 /// \brief The type used to describe the state of a fast enumeration in
441 /// Objective-C's for..in loop.
442 QualType ObjCFastEnumerationStateType;
443
Douglas Gregor45c4ea72011-08-09 15:54:21 +0000444 /// @}
445
David Chisnall0d13f6f2010-01-23 02:40:42 +0000446 /// Lazily create the Objective-C runtime
447 void createObjCRuntime();
448
Peter Collingbourne8c25fc52011-09-19 21:14:35 +0000449 void createOpenCLRuntime();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700450 void createOpenMPRuntime();
Peter Collingbourne6c0aa5f2011-10-06 18:29:37 +0000451 void createCUDARuntime();
Peter Collingbourne8c25fc52011-09-19 21:14:35 +0000452
Rafael Espindolabcf6b982011-12-19 14:41:01 +0000453 bool isTriviallyRecursive(const FunctionDecl *F);
Peter Collingbourne144a31f2013-06-05 17:49:37 +0000454 bool shouldEmitFunction(GlobalDecl GD);
Daniel Dunbar673431a2010-07-16 00:00:15 +0000455
456 /// @name Cache for Blocks Runtime Globals
457 /// @{
458
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700459 llvm::Constant *NSConcreteGlobalBlock = nullptr;
460 llvm::Constant *NSConcreteStackBlock = nullptr;
Daniel Dunbar754b9fb2010-07-16 00:00:19 +0000461
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700462 llvm::Constant *BlockObjectAssign = nullptr;
463 llvm::Constant *BlockObjectDispose = nullptr;
Daniel Dunbar673431a2010-07-16 00:00:15 +0000464
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700465 llvm::Type *BlockDescriptorType = nullptr;
466 llvm::Type *GenericBlockLiteralType = nullptr;
John McCalld16c2cf2011-02-08 08:22:06 +0000467
468 struct {
469 int GlobalUniqueCount;
470 } Block;
Will Dietz4f45bc02013-01-18 11:30:38 +0000471
Nadav Rotem495cfa42013-03-23 06:43:35 +0000472 /// void @llvm.lifetime.start(i64 %size, i8* nocapture <ptr>)
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700473 llvm::Constant *LifetimeStartFn = nullptr;
Nadav Rotem495cfa42013-03-23 06:43:35 +0000474
475 /// void @llvm.lifetime.end(i64 %size, i8* nocapture <ptr>)
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700476 llvm::Constant *LifetimeEndFn = nullptr;
Nadav Rotem495cfa42013-03-23 06:43:35 +0000477
Fariborz Jahanian4904bf42012-06-26 16:06:38 +0000478 GlobalDecl initializedGlobalDecl;
John McCalld16c2cf2011-02-08 08:22:06 +0000479
Stephen Hines176edba2014-12-01 14:53:08 -0800480 std::unique_ptr<SanitizerMetadata> SanitizerMD;
Will Dietz4f45bc02013-01-18 11:30:38 +0000481
Daniel Dunbar673431a2010-07-16 00:00:15 +0000482 /// @}
Stephen Hines176edba2014-12-01 14:53:08 -0800483
484 llvm::DenseMap<const Decl *, bool> DeferredEmptyCoverageMappingDecls;
485
486 std::unique_ptr<CoverageMappingModuleGen> CoverageMapping;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800487
488 /// Mapping from canonical types to their metadata identifiers. We need to
489 /// maintain this mapping because identifiers may be formed from distinct
490 /// MDNodes.
491 llvm::DenseMap<QualType, llvm::Metadata *> MetadataIdMap;
492
Reid Spencer5f016e22007-07-11 17:01:13 +0000493public:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800494 CodeGenModule(ASTContext &C, const HeaderSearchOptions &headersearchopts,
495 const PreprocessorOptions &ppopts,
496 const CodeGenOptions &CodeGenOpts, llvm::Module &M,
Stephen Hines176edba2014-12-01 14:53:08 -0800497 DiagnosticsEngine &Diags,
498 CoverageSourceInfo *CoverageInfo = nullptr);
Ted Kremenek815c78f2008-08-05 18:50:11 +0000499
Chris Lattner2b94fe32008-03-01 08:45:05 +0000500 ~CodeGenModule();
Mike Stumpecc90e92009-02-13 19:12:34 +0000501
Stephen Hines651f13c2014-04-23 16:59:28 -0700502 void clear();
503
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700504 /// Finalize LLVM code generation.
Ted Kremenek815c78f2008-08-05 18:50:11 +0000505 void Release();
Daniel Dunbar208ff5e2008-08-11 18:12:00 +0000506
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700507 /// Return a reference to the configured Objective-C runtime.
Mike Stumpecc90e92009-02-13 19:12:34 +0000508 CGObjCRuntime &getObjCRuntime() {
Peter Collingbournee9265232011-07-27 20:29:46 +0000509 if (!ObjCRuntime) createObjCRuntime();
510 return *ObjCRuntime;
Daniel Dunbar208ff5e2008-08-11 18:12:00 +0000511 }
Mike Stumpecc90e92009-02-13 19:12:34 +0000512
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700513 /// Return true iff an Objective-C runtime has been configured.
Peter Collingbournee9265232011-07-27 20:29:46 +0000514 bool hasObjCRuntime() { return !!ObjCRuntime; }
Daniel Dunbar208ff5e2008-08-11 18:12:00 +0000515
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700516 /// Return a reference to the configured OpenCL runtime.
Peter Collingbourne8c25fc52011-09-19 21:14:35 +0000517 CGOpenCLRuntime &getOpenCLRuntime() {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700518 assert(OpenCLRuntime != nullptr);
Peter Collingbourne8c25fc52011-09-19 21:14:35 +0000519 return *OpenCLRuntime;
520 }
521
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700522 /// Return a reference to the configured OpenMP runtime.
523 CGOpenMPRuntime &getOpenMPRuntime() {
524 assert(OpenMPRuntime != nullptr);
525 return *OpenMPRuntime;
526 }
527
528 /// Return a reference to the configured CUDA runtime.
Peter Collingbourne6c0aa5f2011-10-06 18:29:37 +0000529 CGCUDARuntime &getCUDARuntime() {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700530 assert(CUDARuntime != nullptr);
Peter Collingbourne6c0aa5f2011-10-06 18:29:37 +0000531 return *CUDARuntime;
532 }
533
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800534 ObjCEntrypoints &getObjCEntrypoints() const {
535 assert(ObjCData != nullptr);
536 return *ObjCData;
John McCallf85e1932011-06-15 23:02:42 +0000537 }
538
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700539 InstrProfStats &getPGOStats() { return PGOStats; }
540 llvm::IndexedInstrProfReader *getPGOReader() const { return PGOReader.get(); }
Stephen Hines651f13c2014-04-23 16:59:28 -0700541
Stephen Hines176edba2014-12-01 14:53:08 -0800542 CoverageMappingModuleGen *getCoverageMapping() const {
543 return CoverageMapping.get();
544 }
545
Eli Friedman71cba342012-03-09 03:27:46 +0000546 llvm::Constant *getStaticLocalDeclAddress(const VarDecl *D) {
547 return StaticLocalDeclMap[D];
Fariborz Jahanian65ad5a42010-04-18 21:01:23 +0000548 }
Fariborz Jahanian63326a52010-04-19 18:15:02 +0000549 void setStaticLocalDeclAddress(const VarDecl *D,
Eli Friedman71cba342012-03-09 03:27:46 +0000550 llvm::Constant *C) {
551 StaticLocalDeclMap[D] = C;
Fariborz Jahanian65ad5a42010-04-18 21:01:23 +0000552 }
553
Stephen Hines176edba2014-12-01 14:53:08 -0800554 llvm::Constant *
555 getOrCreateStaticVarDecl(const VarDecl &D,
556 llvm::GlobalValue::LinkageTypes Linkage);
557
John McCall355bba72012-03-30 21:00:39 +0000558 llvm::GlobalVariable *getStaticLocalDeclGuardAddress(const VarDecl *D) {
559 return StaticLocalDeclGuardMap[D];
560 }
561 void setStaticLocalDeclGuardAddress(const VarDecl *D,
562 llvm::GlobalVariable *C) {
563 StaticLocalDeclGuardMap[D] = C;
564 }
565
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700566 bool lookupRepresentativeDecl(StringRef MangledName,
567 GlobalDecl &Result) const;
568
Fariborz Jahanianb08cfb32012-01-08 19:13:23 +0000569 llvm::Constant *getAtomicSetterHelperFnMap(QualType Ty) {
570 return AtomicSetterHelperFnMap[Ty];
Fariborz Jahaniancd93b962012-01-06 22:33:54 +0000571 }
Fariborz Jahanianb08cfb32012-01-08 19:13:23 +0000572 void setAtomicSetterHelperFnMap(QualType Ty,
Fariborz Jahaniancd93b962012-01-06 22:33:54 +0000573 llvm::Constant *Fn) {
Fariborz Jahanianb08cfb32012-01-08 19:13:23 +0000574 AtomicSetterHelperFnMap[Ty] = Fn;
575 }
576
577 llvm::Constant *getAtomicGetterHelperFnMap(QualType Ty) {
578 return AtomicGetterHelperFnMap[Ty];
579 }
580 void setAtomicGetterHelperFnMap(QualType Ty,
581 llvm::Constant *Fn) {
582 AtomicGetterHelperFnMap[Ty] = Fn;
Fariborz Jahaniancd93b962012-01-06 22:33:54 +0000583 }
584
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700585 llvm::Constant *getTypeDescriptorFromMap(QualType Ty) {
Will Dietz562d45c2013-11-08 01:09:22 +0000586 return TypeDescriptorMap[Ty];
587 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700588 void setTypeDescriptorInMap(QualType Ty, llvm::Constant *C) {
Will Dietz562d45c2013-11-08 01:09:22 +0000589 TypeDescriptorMap[Ty] = C;
590 }
591
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700592 CGDebugInfo *getModuleDebugInfo() { return DebugInfo.get(); }
Devang Patel5de7a0e2011-03-07 18:29:53 +0000593
Dan Gohmanb49bd272012-02-16 00:57:37 +0000594 llvm::MDNode *getNoObjCARCExceptionsMetadata() {
595 if (!NoObjCARCExceptionsMetadata)
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700596 NoObjCARCExceptionsMetadata = llvm::MDNode::get(getLLVMContext(), None);
Dan Gohmanb49bd272012-02-16 00:57:37 +0000597 return NoObjCARCExceptionsMetadata;
598 }
599
Reid Spencer5f016e22007-07-11 17:01:13 +0000600 ASTContext &getContext() const { return Context; }
David Blaikie4e4d0842012-03-11 07:00:24 +0000601 const LangOptions &getLangOpts() const { return LangOpts; }
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800602 const HeaderSearchOptions &getHeaderSearchOpts()
603 const { return HeaderSearchOpts; }
604 const PreprocessorOptions &getPreprocessorOpts()
605 const { return PreprocessorOpts; }
John McCall64aa4b32013-04-16 22:48:15 +0000606 const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
Reid Spencer5f016e22007-07-11 17:01:13 +0000607 llvm::Module &getModule() const { return TheModule; }
David Blaikied6471f72011-09-25 23:23:43 +0000608 DiagnosticsEngine &getDiags() const { return Diags; }
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800609 const llvm::DataLayout &getDataLayout() const {
610 return TheModule.getDataLayout();
611 }
John McCall64aa4b32013-04-16 22:48:15 +0000612 const TargetInfo &getTarget() const { return Target; }
Stephen Hines176edba2014-12-01 14:53:08 -0800613 const llvm::Triple &getTriple() const;
614 bool supportsCOMDAT() const;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700615 void maybeSetTrivialComdat(const Decl &D, llvm::GlobalObject &GO);
Stephen Hines176edba2014-12-01 14:53:08 -0800616
Stephen Hines651f13c2014-04-23 16:59:28 -0700617 CGCXXABI &getCXXABI() const { return *ABI; }
Owen Andersona1cf15f2009-07-14 23:10:40 +0000618 llvm::LLVMContext &getLLVMContext() { return VMContext; }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700619
620 bool shouldUseTBAA() const { return TBAA != nullptr; }
John McCallbc7fbf02011-02-26 08:07:02 +0000621
John McCall64aa4b32013-04-16 22:48:15 +0000622 const TargetCodeGenInfo &getTargetCodeGenInfo();
623
624 CodeGenTypes &getTypes() { return Types; }
625
626 CodeGenVTables &getVTables() { return VTables; }
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +0000627
Timur Iskhodzhanov5f0db582013-11-05 15:54:58 +0000628 ItaniumVTableContext &getItaniumVTableContext() {
629 return VTables.getItaniumVTableContext();
Timur Iskhodzhanovf0746582013-10-09 11:33:51 +0000630 }
John McCall64aa4b32013-04-16 22:48:15 +0000631
Timur Iskhodzhanov5f0db582013-11-05 15:54:58 +0000632 MicrosoftVTableContext &getMicrosoftVTableContext() {
633 return VTables.getMicrosoftVTableContext();
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +0000634 }
635
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700636 CtorList &getGlobalCtors() { return GlobalCtors; }
637 CtorList &getGlobalDtors() { return GlobalDtors; }
638
Dan Gohman3d5aff52010-10-14 23:06:10 +0000639 llvm::MDNode *getTBAAInfo(QualType QTy);
Kostya Serebryany8cb4a072012-03-26 17:03:51 +0000640 llvm::MDNode *getTBAAInfoForVTablePtr();
Dan Gohmanb22c7dc2012-09-28 21:58:29 +0000641 llvm::MDNode *getTBAAStructInfo(QualType QTy);
Manman Renb37a73d2013-04-04 21:53:22 +0000642 /// Return the path-aware tag for given base type, access node and offset.
643 llvm::MDNode *getTBAAStructTagInfo(QualType BaseTy, llvm::MDNode *AccessN,
644 uint64_t O);
Dan Gohman3d5aff52010-10-14 23:06:10 +0000645
Richard Smitha9b21d22012-02-17 06:48:11 +0000646 bool isTypeConstant(QualType QTy, bool ExcludeCtorDtor);
647
John McCall9eda3ab2013-03-07 21:37:17 +0000648 bool isPaddedAtomicType(QualType type);
649 bool isPaddedAtomicType(const AtomicType *type);
650
Manman Renca835182013-04-11 23:02:56 +0000651 /// Decorate the instruction with a TBAA tag. For scalar TBAA, the tag
652 /// is the same as the type. For struct-path aware TBAA, the tag
653 /// is different from the type: base type, access type and offset.
654 /// When ConvertTypeToTag is true, we create a tag based on the scalar type.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800655 void DecorateInstructionWithTBAA(llvm::Instruction *Inst,
656 llvm::MDNode *TBAAInfo,
657 bool ConvertTypeToTag = true);
658
659 /// Adds !invariant.barrier !tag to instruction
660 void DecorateInstructionWithInvariantGroup(llvm::Instruction *I,
661 const CXXRecordDecl *RD);
Dan Gohman3d5aff52010-10-14 23:06:10 +0000662
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700663 /// Emit the given number of characters as a value of type size_t.
John McCallbc8d40d2011-06-24 21:55:10 +0000664 llvm::ConstantInt *getSize(CharUnits numChars);
665
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700666 /// Set the visibility for the given LLVM GlobalValue.
Anders Carlsson0ffeaad2011-01-29 19:39:23 +0000667 void setGlobalVisibility(llvm::GlobalValue *GV, const NamedDecl *D) const;
Daniel Dunbar04d40782009-04-14 06:00:08 +0000668
Stephen Hines176edba2014-12-01 14:53:08 -0800669 /// Set the TLS mode for the given LLVM GlobalValue for the thread-local
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700670 /// variable declaration D.
Stephen Hines176edba2014-12-01 14:53:08 -0800671 void setTLSMode(llvm::GlobalValue *GV, const VarDecl &D) const;
Hans Wennborgde981f32012-06-28 08:01:44 +0000672
Anders Carlssonc7e98fa2011-01-29 20:59:35 +0000673 static llvm::GlobalValue::VisibilityTypes GetLLVMVisibility(Visibility V) {
674 switch (V) {
675 case DefaultVisibility: return llvm::GlobalValue::DefaultVisibility;
676 case HiddenVisibility: return llvm::GlobalValue::HiddenVisibility;
677 case ProtectedVisibility: return llvm::GlobalValue::ProtectedVisibility;
678 }
679 llvm_unreachable("unknown visibility!");
Anders Carlssonc7e98fa2011-01-29 20:59:35 +0000680 }
681
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800682 llvm::Constant *GetAddrOfGlobal(GlobalDecl GD, bool IsForDefinition = false);
John McCalld46f9852010-02-19 01:32:20 +0000683
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700684 /// Will return a global variable of the given type. If a variable with a
685 /// different type already exists then a new variable with the right type
686 /// will be created and all uses of the old variable will be replaced with a
687 /// bitcast to the new variable.
Anders Carlsson3bd62022011-01-29 18:20:20 +0000688 llvm::GlobalVariable *
Chris Lattner686775d2011-07-20 06:58:45 +0000689 CreateOrReplaceCXXRuntimeVariable(StringRef Name, llvm::Type *Ty,
Anders Carlsson3bd62022011-01-29 18:20:20 +0000690 llvm::GlobalValue::LinkageTypes Linkage);
691
Stephen Hines176edba2014-12-01 14:53:08 -0800692 llvm::Function *
693 CreateGlobalInitOrDestructFunction(llvm::FunctionType *ty, const Twine &name,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800694 const CGFunctionInfo &FI,
Stephen Hines176edba2014-12-01 14:53:08 -0800695 SourceLocation Loc = SourceLocation(),
696 bool TLS = false);
697
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700698 /// Return the address space of the underlying global variable for D, as
699 /// determined by its declaration. Normally this is the same as the address
700 /// space of D's type, but in CUDA, address spaces are associated with
701 /// declarations, not types.
Peter Collingbourne4dc34eb2012-05-20 21:08:35 +0000702 unsigned GetGlobalVarAddressSpace(const VarDecl *D, unsigned AddrSpace);
703
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700704 /// Return the llvm::Constant for the address of the given global variable.
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700705 /// If Ty is non-null and if the global doesn't exist, then it will be created
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700706 /// with the specified type instead of whatever the normal requested type
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700707 /// would be. If IsForDefinition is true, it is guranteed that an actual
708 /// global with type Ty will be returned, not conversion of a variable with
709 /// the same mangled name but some other type.
Chris Lattner570585c2009-03-21 09:16:30 +0000710 llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D,
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700711 llvm::Type *Ty = nullptr,
712 bool IsForDefinition = false);
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000713
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700714 /// Return the address of the given function. If Ty is non-null, then this
715 /// function will use the specified type if it has to create it.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800716 llvm::Constant *GetAddrOfFunction(GlobalDecl GD, llvm::Type *Ty = nullptr,
Stephen Hines651f13c2014-04-23 16:59:28 -0700717 bool ForVTable = false,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800718 bool DontDefer = false,
719 bool IsForDefinition = false);
Daniel Dunbar61432932008-08-13 23:20:05 +0000720
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700721 /// Get the address of the RTTI descriptor for the given type.
John McCall9dffe6f2010-04-30 01:15:21 +0000722 llvm::Constant *GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH = false);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000723
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700724 /// Get the address of a uuid descriptor .
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800725 ConstantAddress GetAddrOfUuidDescriptor(const CXXUuidofExpr* E);
Nico Weberc5f80462012-10-11 10:13:44 +0000726
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700727 /// Get the address of the thunk for the given global decl.
Anders Carlsson84c49e42011-02-06 17:15:43 +0000728 llvm::Constant *GetAddrOfThunk(GlobalDecl GD, const ThunkInfo &Thunk);
Anders Carlsson19879c92010-03-23 17:17:29 +0000729
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700730 /// Get a reference to the target of VD.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800731 ConstantAddress GetWeakRefReference(const ValueDecl *VD);
732
733 /// Returns the assumed alignment of an opaque pointer to the given class.
734 CharUnits getClassPointerAlignment(const CXXRecordDecl *CD);
735
736 /// Returns the assumed alignment of a virtual base of a class.
737 CharUnits getVBaseAlignment(CharUnits DerivedAlign,
738 const CXXRecordDecl *Derived,
739 const CXXRecordDecl *VBase);
740
741 /// Given a class pointer with an actual known alignment, and the
742 /// expected alignment of an object at a dynamic offset w.r.t that
743 /// pointer, return the alignment to assume at the offset.
744 CharUnits getDynamicOffsetAlignment(CharUnits ActualAlign,
745 const CXXRecordDecl *Class,
746 CharUnits ExpectedTargetAlign);
747
748 CharUnits
749 computeNonVirtualBaseClassOffset(const CXXRecordDecl *DerivedClass,
750 CastExpr::path_const_iterator Start,
751 CastExpr::path_const_iterator End);
Rafael Espindola6a836702010-03-04 18:17:24 +0000752
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700753 /// Returns the offset from a derived class to a class. Returns null if the
754 /// offset is 0.
Anders Carlssona04efdf2010-04-24 21:23:59 +0000755 llvm::Constant *
756 GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl,
John McCallf871d0c2010-08-07 06:22:56 +0000757 CastExpr::path_const_iterator PathBegin,
758 CastExpr::path_const_iterator PathEnd);
John McCalld16c2cf2011-02-08 08:22:06 +0000759
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800760 llvm::FoldingSet<BlockByrefHelpers> ByrefHelpersCache;
John McCalld16c2cf2011-02-08 08:22:06 +0000761
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700762 /// Fetches the global unique block count.
John McCall8178df32011-02-22 22:38:33 +0000763 int getUniqueBlockCount() { return ++Block.GlobalUniqueCount; }
Fariborz Jahanian4904bf42012-06-26 16:06:38 +0000764
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700765 /// Fetches the type of a generic block descriptor.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000766 llvm::Type *getBlockDescriptorType();
John McCalld16c2cf2011-02-08 08:22:06 +0000767
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700768 /// The type of a generic block literal.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000769 llvm::Type *getGenericBlockLiteralType();
John McCalld16c2cf2011-02-08 08:22:06 +0000770
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700771 /// Gets the address of a block which requires no captures.
John McCalld16c2cf2011-02-08 08:22:06 +0000772 llvm::Constant *GetAddrOfGlobalBlock(const BlockExpr *BE, const char *);
Anders Carlssona04efdf2010-04-24 21:23:59 +0000773
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700774 /// Return a pointer to a constant CFString object for the given string.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800775 ConstantAddress GetAddrOfConstantCFString(const StringLiteral *Literal);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700776
777 /// Return a pointer to a constant NSString object for the given string. Or a
778 /// user defined String object as defined via
Fariborz Jahanian4c733072010-10-19 17:19:29 +0000779 /// -fconstant-string-class=class_name option.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800780 ConstantAddress GetAddrOfConstantString(const StringLiteral *Literal);
Chris Lattnera7ad98f2008-02-11 00:02:17 +0000781
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700782 /// Return a constant array for the given string.
Eli Friedman64f45a22011-11-01 02:23:42 +0000783 llvm::Constant *GetConstantArrayFromStringLiteral(const StringLiteral *E);
784
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700785 /// Return a pointer to a constant array for the given string literal.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800786 ConstantAddress
Stephen Hines176edba2014-12-01 14:53:08 -0800787 GetAddrOfConstantStringFromLiteral(const StringLiteral *S,
788 StringRef Name = ".str");
Daniel Dunbar1e049762008-08-10 20:25:57 +0000789
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700790 /// Return a pointer to a constant array for the given ObjCEncodeExpr node.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800791 ConstantAddress
Stephen Hines176edba2014-12-01 14:53:08 -0800792 GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *);
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700794 /// Returns a pointer to a character array containing the literal and a
795 /// terminating '\0' character. The result has pointer to array type.
Daniel Dunbar5fabf9d2008-10-17 21:56:50 +0000796 ///
Mike Stumpecc90e92009-02-13 19:12:34 +0000797 /// \param GlobalName If provided, the name to use for the global (if one is
798 /// created).
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800799 ConstantAddress
Stephen Hines176edba2014-12-01 14:53:08 -0800800 GetAddrOfConstantCString(const std::string &Str,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800801 const char *GlobalName = nullptr);
Richard Smith7401cf52011-11-22 22:48:32 +0000802
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700803 /// Returns a pointer to a constant global variable for the given file-scope
804 /// compound literal expression.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800805 ConstantAddress GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr*E);
Richard Smith211c8dd2013-06-05 00:46:14 +0000806
807 /// \brief Returns a pointer to a global variable representing a temporary
808 /// with static or thread storage duration.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800809 ConstantAddress GetAddrOfGlobalTemporary(const MaterializeTemporaryExpr *E,
Richard Smith211c8dd2013-06-05 00:46:14 +0000810 const Expr *Inner);
811
Douglas Gregor0815b572011-08-09 17:23:49 +0000812 /// \brief Retrieve the record type that describes the state of an
813 /// Objective-C fast enumeration loop (for..in).
814 QualType getObjCFastEnumerationStateType();
Anders Carlsson27ae5362009-04-17 01:58:57 +0000815
Stephen Hines176edba2014-12-01 14:53:08 -0800816 // Produce code for this constructor/destructor. This method doesn't try
817 // to apply any ABI rules about which other constructors/destructors
818 // are needed or if they are alias to each other.
819 llvm::Function *codegenCXXStructor(const CXXMethodDecl *MD,
820 StructorType Type);
821
822 /// Return the address of the constructor/destructor of the given type.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800823 llvm::Constant *
Stephen Hines176edba2014-12-01 14:53:08 -0800824 getAddrOfCXXStructor(const CXXMethodDecl *MD, StructorType Type,
825 const CGFunctionInfo *FnInfo = nullptr,
826 llvm::FunctionType *FnType = nullptr,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800827 bool DontDefer = false, bool IsForDefinition = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000828
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700829 /// Given a builtin id for a function like "__builtin_fabsf", return a
830 /// Function* for "fabsf".
Daniel Dunbar34771b52009-09-14 04:33:21 +0000831 llvm::Value *getBuiltinLibFunction(const FunctionDecl *FD,
832 unsigned BuiltinID);
Daniel Dunbar61432932008-08-13 23:20:05 +0000833
Dmitri Gribenko55431692013-05-05 00:41:58 +0000834 llvm::Function *getIntrinsic(unsigned IID, ArrayRef<llvm::Type*> Tys = None);
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000835
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700836 /// Emit code for a single top level declaration.
Daniel Dunbar41071de2008-08-15 23:26:23 +0000837 void EmitTopLevelDecl(Decl *D);
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000838
Stephen Hines176edba2014-12-01 14:53:08 -0800839 /// \brief Stored a deferred empty coverage mapping for an unused
840 /// and thus uninstrumented top level declaration.
841 void AddDeferredUnusedCoverageMapping(Decl *D);
842
843 /// \brief Remove the deferred empty coverage mapping as this
844 /// declaration is actually instrumented.
845 void ClearUnusedCoverageMapping(const Decl *D);
846
847 /// \brief Emit all the deferred coverage mappings
848 /// for the uninstrumented functions.
849 void EmitDeferredUnusedCoverageMappings();
850
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700851 /// Tell the consumer that this variable has been instantiated.
Rafael Espindola02503932012-03-08 15:51:03 +0000852 void HandleCXXStaticMemberVarInstantiation(VarDecl *VD);
Rafael Espindola234fe652012-03-05 10:54:55 +0000853
Richard Smith00249372013-04-06 05:00:46 +0000854 /// \brief If the declaration has internal linkage but is inside an
855 /// extern "C" linkage specification, prepare to emit an alias for it
856 /// to the expected name.
857 template<typename SomeDecl>
858 void MaybeHandleStaticInExternC(const SomeDecl *D, llvm::GlobalValue *GV);
859
Stephen Hines651f13c2014-04-23 16:59:28 -0700860 /// Add a global to a list to be added to the llvm.used metadata.
861 void addUsedGlobal(llvm::GlobalValue *GV);
862
863 /// Add a global to a list to be added to the llvm.compiler.used metadata.
864 void addCompilerUsedGlobal(llvm::GlobalValue *GV);
Daniel Dunbar02698712009-02-13 20:29:50 +0000865
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700866 /// Add a destructor and object to add to the C++ global destructor function.
Chris Lattner810112e2010-06-19 05:52:45 +0000867 void AddCXXDtorEntry(llvm::Constant *DtorFn, llvm::Constant *Object) {
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700868 CXXGlobalDtors.emplace_back(DtorFn, Object);
Chris Lattner810112e2010-06-19 05:52:45 +0000869 }
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000870
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700871 /// Create a new runtime function with the specified type and name.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000872 llvm::Constant *CreateRuntimeFunction(llvm::FunctionType *Ty,
Chris Lattner686775d2011-07-20 06:58:45 +0000873 StringRef Name,
Bill Wendlingc4c62fd2013-01-31 00:30:05 +0000874 llvm::AttributeSet ExtraAttrs =
875 llvm::AttributeSet());
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700876 /// Create a new compiler builtin function with the specified type and name.
877 llvm::Constant *CreateBuiltinFunction(llvm::FunctionType *Ty,
878 StringRef Name,
879 llvm::AttributeSet ExtraAttrs =
880 llvm::AttributeSet());
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700881 /// Create a new runtime global variable with the specified type and name.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000882 llvm::Constant *CreateRuntimeVariable(llvm::Type *Ty,
Chris Lattner686775d2011-07-20 06:58:45 +0000883 StringRef Name);
Daniel Dunbarf1968f22008-10-01 00:49:24 +0000884
Daniel Dunbar673431a2010-07-16 00:00:15 +0000885 ///@name Custom Blocks Runtime Interfaces
886 ///@{
887
888 llvm::Constant *getNSConcreteGlobalBlock();
889 llvm::Constant *getNSConcreteStackBlock();
890 llvm::Constant *getBlockObjectAssign();
891 llvm::Constant *getBlockObjectDispose();
892
893 ///@}
894
Nadav Rotem495cfa42013-03-23 06:43:35 +0000895 llvm::Constant *getLLVMLifetimeStartFn();
896 llvm::Constant *getLLVMLifetimeEndFn();
897
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700898 // Make sure that this type is translated.
Devang Patele80d5672011-03-23 16:29:39 +0000899 void UpdateCompletedType(const TagDecl *TD);
Daniel Dunbard60f2fb2009-02-17 18:43:32 +0000900
John McCall5808ce42011-02-03 08:15:49 +0000901 llvm::Constant *getMemberPointerConstant(const UnaryOperator *e);
902
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700903 /// Try to emit the initializer for the given declaration as a constant;
904 /// returns 0 if the expression cannot be emitted as a constant.
905 llvm::Constant *EmitConstantInit(const VarDecl &D,
906 CodeGenFunction *CGF = nullptr);
Richard Smith2d6a5672012-01-14 04:30:29 +0000907
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700908 /// Try to emit the given expression as a constant; returns 0 if the
909 /// expression cannot be emitted as a constant.
Anders Carlssone9352cc2009-04-08 04:48:15 +0000910 llvm::Constant *EmitConstantExpr(const Expr *E, QualType DestType,
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700911 CodeGenFunction *CGF = nullptr);
Daniel Dunbard60f2fb2009-02-17 18:43:32 +0000912
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700913 /// Emit the given constant value as a constant, in the type's scalar
914 /// representation.
Richard Smith2d6a5672012-01-14 04:30:29 +0000915 llvm::Constant *EmitConstantValue(const APValue &Value, QualType DestType,
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700916 CodeGenFunction *CGF = nullptr);
Richard Smith2d6a5672012-01-14 04:30:29 +0000917
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700918 /// Emit the given constant value as a constant, in the type's memory
919 /// representation.
Richard Smitha3ca41f2012-03-02 23:27:11 +0000920 llvm::Constant *EmitConstantValueForMemory(const APValue &Value,
921 QualType DestType,
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700922 CodeGenFunction *CGF = nullptr);
Richard Smitha3ca41f2012-03-02 23:27:11 +0000923
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800924 /// \brief Emit type info if type of an expression is a variably modified
925 /// type. Also emit proper debug info for cast types.
926 void EmitExplicitCastExprType(const ExplicitCastExpr *E,
927 CodeGenFunction *CGF = nullptr);
928
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700929 /// Return the result of value-initializing the given type, i.e. a null
930 /// expression of the given type. This is usually, but not always, an LLVM
931 /// null constant.
Eli Friedman0f593122009-04-13 21:47:26 +0000932 llvm::Constant *EmitNullConstant(QualType T);
933
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700934 /// Return a null constant appropriate for zero-initializing a base class with
935 /// the given type. This is usually, but not always, an LLVM null constant.
Eli Friedman2ed7cb62011-10-14 02:27:24 +0000936 llvm::Constant *EmitNullConstantForBase(const CXXRecordDecl *Record);
937
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700938 /// Emit a general error that something can't be done.
Chandler Carruth0f30a122012-03-30 19:44:53 +0000939 void Error(SourceLocation loc, StringRef error);
John McCall32096692011-03-18 02:56:14 +0000940
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700941 /// Print out an error that codegen doesn't support the specified stmt yet.
David Blaikie0a1c8622013-08-19 21:02:26 +0000942 void ErrorUnsupported(const Stmt *S, const char *Type);
Mike Stumpecc90e92009-02-13 19:12:34 +0000943
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700944 /// Print out an error that codegen doesn't support the specified decl yet.
David Blaikie0a1c8622013-08-19 21:02:26 +0000945 void ErrorUnsupported(const Decl *D, const char *Type);
Dan Gohman4f8d1232008-05-22 00:50:06 +0000946
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700947 /// Set the attributes on the LLVM function for the given decl and function
948 /// info. This applies attributes necessary for handling the ABI as well as
949 /// user specified attributes like section.
Daniel Dunbar0e4f40e2009-04-17 00:48:04 +0000950 void SetInternalFunctionAttributes(const Decl *D, llvm::Function *F,
951 const CGFunctionInfo &FI);
Daniel Dunbarf80519b2008-09-04 23:41:35 +0000952
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700953 /// Set the LLVM function attributes (sext, zext, etc).
Daniel Dunbar7dbd8192009-04-14 07:08:30 +0000954 void SetLLVMFunctionAttributes(const Decl *D,
955 const CGFunctionInfo &Info,
956 llvm::Function *F);
Daniel Dunbarb7688072008-09-10 00:41:16 +0000957
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700958 /// Set the LLVM function attributes which only apply to a function
Stephen Hines176edba2014-12-01 14:53:08 -0800959 /// definition.
Daniel Dunbar7c65e992009-04-14 08:05:55 +0000960 void SetLLVMFunctionAttributesForDefinition(const Decl *D, llvm::Function *F);
961
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700962 /// Return true iff the given type uses 'sret' when used as a return type.
Daniel Dunbardacf9dd2010-07-14 23:39:36 +0000963 bool ReturnTypeUsesSRet(const CGFunctionInfo &FI);
964
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700965 /// Return true iff the given type uses an argument slot when 'sret' is used
966 /// as a return type.
Stephen Hines651f13c2014-04-23 16:59:28 -0700967 bool ReturnSlotInterferesWithArgs(const CGFunctionInfo &FI);
968
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700969 /// Return true iff the given type uses 'fpret' when used as a return type.
Daniel Dunbardacf9dd2010-07-14 23:39:36 +0000970 bool ReturnTypeUsesFPRet(QualType ResultType);
Daniel Dunbarb7688072008-09-10 00:41:16 +0000971
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700972 /// Return true iff the given type uses 'fp2ret' when used as a return type.
Anders Carlssoneea64802011-10-31 16:27:11 +0000973 bool ReturnTypeUsesFP2Ret(QualType ResultType);
974
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700975 /// Get the LLVM attributes and calling convention to use for a particular
976 /// function type.
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000977 ///
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700978 /// \param Name - The function name.
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000979 /// \param Info - The function type information.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800980 /// \param CalleeInfo - The callee information these attributes are being
981 /// constructed for. If valid, the attributes applied to this decl may
982 /// contribute to the function attributes and calling convention.
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000983 /// \param PAL [out] - On return, the attribute list to use.
984 /// \param CallingConv [out] - On return, the LLVM calling convention to use.
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700985 void ConstructAttributeList(StringRef Name, const CGFunctionInfo &Info,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800986 CGCalleeInfo CalleeInfo, AttributeListType &PAL,
987 unsigned &CallingConv, bool AttrOnCallSite);
988
989 // Fills in the supplied string map with the set of target features for the
990 // passed in function.
991 void getFunctionFeatureMap(llvm::StringMap<bool> &FeatureMap,
992 const FunctionDecl *FD);
Daniel Dunbarb7688072008-09-10 00:41:16 +0000993
Chris Lattner686775d2011-07-20 06:58:45 +0000994 StringRef getMangledName(GlobalDecl GD);
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700995 StringRef getBlockMangledName(GlobalDecl GD, const BlockDecl *BD);
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000996
997 void EmitTentativeDefinition(const VarDecl *D);
Mike Stumpf1216772009-07-31 18:25:34 +0000998
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700999 void EmitVTable(CXXRecordDecl *Class);
Douglas Gregor6fb745b2010-05-13 16:44:06 +00001000
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001001 void RefreshTypeCacheForClass(const CXXRecordDecl *Class);
1002
Reid Kleckner3190ca92013-05-08 13:44:39 +00001003 /// \brief Appends Opts to the "Linker Options" metadata value.
1004 void AppendLinkerOptions(StringRef Opts);
1005
Benjamin Kramer785f1e72013-06-04 09:13:21 +00001006 /// \brief Appends a detect mismatch command to the linker options.
Aaron Ballmana7ff62f2013-06-04 02:07:14 +00001007 void AddDetectMismatch(StringRef Name, StringRef Value);
1008
Reid Kleckner3190ca92013-05-08 13:44:39 +00001009 /// \brief Appends a dependent lib to the "Linker Options" metadata value.
1010 void AddDependentLib(StringRef Lib);
1011
Peter Collingbourne144a31f2013-06-05 17:49:37 +00001012 llvm::GlobalVariable::LinkageTypes getFunctionLinkage(GlobalDecl GD);
John McCalld46f9852010-02-19 01:32:20 +00001013
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001014 void setFunctionLinkage(GlobalDecl GD, llvm::Function *F) {
1015 F->setLinkage(getFunctionLinkage(GD));
John McCall8b242332010-05-25 04:30:21 +00001016 }
1017
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001018 /// Set the DLL storage class on F.
1019 void setFunctionDLLStorageClass(GlobalDecl GD, llvm::Function *F);
1020
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001021 /// Return the appropriate linkage for the vtable, VTT, and type information
1022 /// of the given class.
Anders Carlsson3a717f72011-01-24 02:04:33 +00001023 llvm::GlobalVariable::LinkageTypes getVTableLinkage(const CXXRecordDecl *RD);
Ken Dyck687cc4a2010-01-26 13:48:07 +00001024
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001025 /// Return the store size, in character units, of the given LLVM type.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001026 CharUnits GetTargetTypeStoreSize(llvm::Type *Ty) const;
Fariborz Jahanian354e7122010-10-27 16:21:54 +00001027
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001028 /// Returns LLVM linkage for a declarator.
1029 llvm::GlobalValue::LinkageTypes
1030 getLLVMLinkageForDeclarator(const DeclaratorDecl *D, GVALinkage Linkage,
1031 bool IsConstantVariable);
1032
1033 /// Returns LLVM linkage for a declarator.
1034 llvm::GlobalValue::LinkageTypes
1035 getLLVMLinkageVarDefinition(const VarDecl *VD, bool IsConstant);
1036
Julien Lerouge77f68bb2011-09-09 22:41:49 +00001037 /// Emit all the global annotations.
1038 void EmitGlobalAnnotations();
1039
1040 /// Emit an annotation string.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001041 llvm::Constant *EmitAnnotationString(StringRef Str);
Julien Lerouge77f68bb2011-09-09 22:41:49 +00001042
1043 /// Emit the annotation's translation unit.
1044 llvm::Constant *EmitAnnotationUnit(SourceLocation Loc);
1045
1046 /// Emit the annotation line number.
1047 llvm::Constant *EmitAnnotationLineNo(SourceLocation L);
1048
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001049 /// Generate the llvm::ConstantStruct which contains the annotation
1050 /// information for a given GlobalValue. The annotation struct is
Julien Lerouge77f68bb2011-09-09 22:41:49 +00001051 /// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the
1052 /// GlobalValue being annotated. The second field is the constant string
1053 /// created from the AnnotateAttr's annotation. The third field is a constant
1054 /// string containing the name of the translation unit. The fourth field is
1055 /// the line number in the file of the annotated value declaration.
1056 llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV,
1057 const AnnotateAttr *AA,
1058 SourceLocation L);
1059
1060 /// Add global annotations that are set on D, for the global GV. Those
1061 /// annotations are emitted during finalization of the LLVM code.
1062 void AddGlobalAnnotations(const ValueDecl *D, llvm::GlobalValue *GV);
1063
Stephen Hines176edba2014-12-01 14:53:08 -08001064 bool isInSanitizerBlacklist(llvm::Function *Fn, SourceLocation Loc) const;
Will Dietz4f45bc02013-01-18 11:30:38 +00001065
Stephen Hines176edba2014-12-01 14:53:08 -08001066 bool isInSanitizerBlacklist(llvm::GlobalVariable *GV, SourceLocation Loc,
1067 QualType Ty,
1068 StringRef Category = StringRef()) const;
1069
1070 SanitizerMetadata *getSanitizerMetadata() {
1071 return SanitizerMD.get();
1072 }
Will Dietz4f45bc02013-01-18 11:30:38 +00001073
John McCalld5617ee2013-01-25 22:31:03 +00001074 void addDeferredVTable(const CXXRecordDecl *RD) {
1075 DeferredVTables.push_back(RD);
1076 }
1077
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001078 /// Emit code for a singal global function or var decl. Forward declarations
1079 /// are emitted lazily.
Reid Klecknera4130ba2013-07-22 13:51:44 +00001080 void EmitGlobal(GlobalDecl D);
1081
Stephen Hines176edba2014-12-01 14:53:08 -08001082 bool TryEmitDefinitionAsAlias(GlobalDecl Alias, GlobalDecl Target,
1083 bool InEveryTU);
1084 bool TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D);
1085
1086 /// Set attributes for a global definition.
1087 void setFunctionDefinitionAttributes(const FunctionDecl *D,
1088 llvm::Function *F);
1089
Chris Lattner686775d2011-07-20 06:58:45 +00001090 llvm::GlobalValue *GetGlobalValue(StringRef Ref);
Mike Stump1eb44332009-09-09 15:08:12 +00001091
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001092 /// Set attributes which are common to any form of a global definition (alias,
1093 /// Objective-C method, function, global variable).
Daniel Dunbar7dbd8192009-04-14 07:08:30 +00001094 ///
Daniel Dunbar7c65e992009-04-14 08:05:55 +00001095 /// NOTE: This should only be called for definitions.
1096 void SetCommonAttributes(const Decl *D, llvm::GlobalValue *GV);
Daniel Dunbarf80519b2008-09-04 23:41:35 +00001097
Stephen Hines176edba2014-12-01 14:53:08 -08001098 /// Set attributes which must be preserved by an alias. This includes common
1099 /// attributes (i.e. it includes a call to SetCommonAttributes).
1100 ///
1101 /// NOTE: This should only be called for definitions.
1102 void setAliasAttributes(const Decl *D, llvm::GlobalValue *GV);
1103
1104 void addReplacement(StringRef Name, llvm::Constant *C);
1105
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001106 void addGlobalValReplacement(llvm::GlobalValue *GV, llvm::Constant *C);
1107
Stephen Hines176edba2014-12-01 14:53:08 -08001108 /// \brief Emit a code for threadprivate directive.
1109 /// \param D Threadprivate declaration.
1110 void EmitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D);
1111
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001112 /// \brief Emit a code for declare reduction construct.
1113 void EmitOMPDeclareReduction(const OMPDeclareReductionDecl *D,
1114 CodeGenFunction *CGF = nullptr);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001115
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001116 /// Returns whether the given record has hidden LTO visibility and therefore
1117 /// may participate in (single-module) CFI and whole-program vtable
1118 /// optimization.
1119 bool HasHiddenLTOVisibility(const CXXRecordDecl *RD);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001120
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001121 /// Emit type metadata for the given vtable using the given layout.
1122 void EmitVTableTypeMetadata(llvm::GlobalVariable *VTable,
1123 const VTableLayout &VTLayout);
1124
1125 /// Generate a cross-DSO type identifier for MD.
1126 llvm::ConstantInt *CreateCrossDsoCfiTypeId(llvm::Metadata *MD);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001127
1128 /// Create a metadata identifier for the given type. This may either be an
1129 /// MDString (for external identifiers) or a distinct unnamed MDNode (for
1130 /// internal identifiers).
1131 llvm::Metadata *CreateMetadataIdentifierForType(QualType T);
1132
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001133 /// Create and attach type metadata to the given function.
1134 void CreateFunctionTypeMetadata(const FunctionDecl *FD, llvm::Function *F);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001135
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001136 /// Returns whether this module needs the "all-vtables" type identifier.
1137 bool NeedAllVtablesTypeId() const;
1138
1139 /// Create and attach type metadata for the given vtable.
1140 void AddVTableTypeMetadata(llvm::GlobalVariable *VTable, CharUnits Offset,
1141 const CXXRecordDecl *RD);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001142
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001143 /// \breif Get the declaration of std::terminate for the platform.
1144 llvm::Constant *getTerminateFn();
1145
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001146 llvm::SanitizerStatReport &getSanStats();
1147
Stephen Hines176edba2014-12-01 14:53:08 -08001148private:
1149 llvm::Constant *
1150 GetOrCreateLLVMFunction(StringRef MangledName, llvm::Type *Ty, GlobalDecl D,
1151 bool ForVTable, bool DontDefer = false,
1152 bool IsThunk = false,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001153 llvm::AttributeSet ExtraAttrs = llvm::AttributeSet(),
1154 bool IsForDefinition = false);
Stephen Hines176edba2014-12-01 14:53:08 -08001155
1156 llvm::Constant *GetOrCreateLLVMGlobal(StringRef MangledName,
1157 llvm::PointerType *PTy,
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001158 const VarDecl *D,
1159 bool IsForDefinition = false);
Stephen Hines176edba2014-12-01 14:53:08 -08001160
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001161 void setNonAliasAttributes(const Decl *D, llvm::GlobalObject *GO);
Mike Stump1eb44332009-09-09 15:08:12 +00001162
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001163 /// Set function attributes for a function declaration.
Stephen Hines176edba2014-12-01 14:53:08 -08001164 void SetFunctionAttributes(GlobalDecl GD, llvm::Function *F,
1165 bool IsIncompleteFunction, bool IsThunk);
Nuno Lopesd4cbda62008-06-08 15:45:52 +00001166
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001167 void EmitGlobalDefinition(GlobalDecl D, llvm::GlobalValue *GV = nullptr);
Daniel Dunbard5d31802009-02-19 07:15:39 +00001168
Stephen Hines651f13c2014-04-23 16:59:28 -07001169 void EmitGlobalFunctionDefinition(GlobalDecl GD, llvm::GlobalValue *GV);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001170 void EmitGlobalVarDefinition(const VarDecl *D, bool IsTentative = false);
John McCallf746aa62010-03-19 23:29:14 +00001171 void EmitAliasDefinition(GlobalDecl GD);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001172 void emitIFuncDefinition(GlobalDecl GD);
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001173 void EmitObjCPropertyImplementations(const ObjCImplementationDecl *D);
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00001174 void EmitObjCIvarInitializations(ObjCImplementationDecl *D);
Fariborz Jahanian354e7122010-10-27 16:21:54 +00001175
Anders Carlsson95d4e5d2009-04-15 15:55:24 +00001176 // C++ related functions.
Mike Stump1eb44332009-09-09 15:08:12 +00001177
Anders Carlsson984e0682009-04-01 00:58:25 +00001178 void EmitNamespace(const NamespaceDecl *D);
Anders Carlsson91e20dd2009-04-02 05:55:18 +00001179 void EmitLinkageSpec(const LinkageSpecDecl *D);
Adrian Prantl0a050f72013-05-09 23:16:27 +00001180 void CompleteDIClassType(const CXXMethodDecl* D);
Anders Carlsson95d4e5d2009-04-15 15:55:24 +00001181
Richard Smithb80a16e2013-04-19 16:42:07 +00001182 /// \brief Emit the function that initializes C++ thread_local variables.
1183 void EmitCXXThreadLocalInitFunc();
1184
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001185 /// Emit the function that initializes C++ globals.
Anders Carlsson89ed31d2009-08-08 23:24:23 +00001186 void EmitCXXGlobalInitFunc();
Eli Friedman6c6bda32010-01-08 00:50:11 +00001187
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001188 /// Emit the function that destroys C++ globals.
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00001189 void EmitCXXGlobalDtorFunc();
1190
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001191 /// Emit the function that initializes the specified global (if PerformInit is
1192 /// true) and registers its destructor.
John McCall3030eb82010-11-06 09:44:32 +00001193 void EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
Richard Smith7ca48502012-02-13 22:16:19 +00001194 llvm::GlobalVariable *Addr,
1195 bool PerformInit);
Eli Friedman6c6bda32010-01-08 00:50:11 +00001196
Stephen Hines176edba2014-12-01 14:53:08 -08001197 void EmitPointerToInitFunc(const VarDecl *VD, llvm::GlobalVariable *Addr,
1198 llvm::Function *InitFunc, InitSegAttr *ISA);
1199
Daniel Dunbar6bfed7e2008-08-01 00:01:51 +00001200 // FIXME: Hardcoding priority here is gross.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001201 void AddGlobalCtor(llvm::Function *Ctor, int Priority = 65535,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001202 llvm::Constant *AssociatedData = nullptr);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001203 void AddGlobalDtor(llvm::Function *Dtor, int Priority = 65535);
Daniel Dunbarbd012ff2008-07-29 23:18:29 +00001204
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001205 /// Generates a global array of functions and priorities using the given list
1206 /// and name. This array will have appending linkage and is suitable for use
1207 /// as a LLVM constructor or destructor array.
Daniel Dunbar6bfed7e2008-08-01 00:01:51 +00001208 void EmitCtorList(const CtorList &Fns, const char *GlobalName);
1209
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001210 /// Emit any needed decls for which code generation was deferred.
Dmitri Gribenkoc4a77902012-11-15 14:28:07 +00001211 void EmitDeferred();
Daniel Dunbar02698712009-02-13 20:29:50 +00001212
Rafael Espindola61a0a752013-11-05 21:37:29 +00001213 /// Call replaceAllUsesWith on all pairs in Replacements.
1214 void applyReplacements();
1215
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001216 /// Call replaceAllUsesWith on all pairs in GlobalValReplacements.
1217 void applyGlobalValReplacements();
1218
Rafael Espindolad2054982013-10-22 19:26:13 +00001219 void checkAliases();
1220
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001221 /// Emit any vtables which we deferred and still have a use for.
John McCalld5617ee2013-01-25 22:31:03 +00001222 void EmitDeferredVTables();
1223
Stephen Hines651f13c2014-04-23 16:59:28 -07001224 /// Emit the llvm.used and llvm.compiler.used metadata.
1225 void emitLLVMUsed();
Daniel Dunbarf1968f22008-10-01 00:49:24 +00001226
Douglas Gregor858afb32013-01-14 20:53:57 +00001227 /// \brief Emit the link options introduced by imported modules.
1228 void EmitModuleLinkOptions();
1229
Richard Smith00249372013-04-06 05:00:46 +00001230 /// \brief Emit aliases for internal-linkage declarations inside "C" language
1231 /// linkage specifications, giving them the "expected" name where possible.
1232 void EmitStaticExternCAliases();
1233
John McCall744016d2010-07-06 23:57:41 +00001234 void EmitDeclMetadata();
1235
Rafael Espindola9686f122013-10-16 19:28:50 +00001236 /// \brief Emit the Clang version as llvm.ident metadata.
1237 void EmitVersionIdentMetadata();
1238
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001239 /// Emits target specific Metadata for global declarations.
1240 void EmitTargetMetadata();
1241
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001242 /// Emit the llvm.gcov metadata used to tell LLVM where to emit the .gcno and
1243 /// .gcda files in a way that persists in .bc files.
Nick Lewycky3dc05412011-05-05 00:08:20 +00001244 void EmitCoverageFile();
Nick Lewycky5ea4f442011-05-04 20:46:58 +00001245
Nico Weberc5f80462012-10-11 10:13:44 +00001246 /// Emits the initializer for a uuidof string.
Stephen Hines176edba2014-12-01 14:53:08 -08001247 llvm::Constant *EmitUuidofInitializer(StringRef uuidstr);
Nico Weberc5f80462012-10-11 10:13:44 +00001248
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001249 /// Determine whether the definition must be emitted; if this returns \c
1250 /// false, the definition can be emitted lazily if it's used.
1251 bool MustBeEmitted(const ValueDecl *D);
1252
1253 /// Determine whether the definition can be emitted eagerly, or should be
1254 /// delayed until the end of the translation unit. This is relevant for
1255 /// definitions whose linkage can change, e.g. implicit function instantions
1256 /// which may later be explicitly instantiated.
1257 bool MayBeEmittedEagerly(const ValueDecl *D);
John McCallb2593832010-09-16 06:16:50 +00001258
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001259 /// Check whether we can use a "simpler", more core exceptions personality
1260 /// function.
John McCallb2593832010-09-16 06:16:50 +00001261 void SimplifyPersonality();
Reid Spencer5f016e22007-07-11 17:01:13 +00001262};
1263} // end namespace CodeGen
1264} // end namespace clang
1265
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001266#endif // LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H