blob: 649b0e546039287417177f54e0e9595af2e6e292 [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
Chris Lattneref52a2f2008-02-29 17:10:38 +000014#ifndef CLANG_CODEGEN_CODEGENMODULE_H
15#define CLANG_CODEGEN_CODEGENMODULE_H
Reid Spencer5f016e22007-07-11 17:01:13 +000016
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "CGVTables.h"
18#include "CodeGenTypes.h"
Stephen Hinesef822542014-07-21 00:47:37 -070019#include "SanitizerBlacklist.h"
Dan Gohman4f8d1232008-05-22 00:50:06 +000020#include "clang/AST/Attr.h"
Anders Carlsson2a131fb2009-05-05 04:44:02 +000021#include "clang/AST/DeclCXX.h"
Anders Carlsson0ff8baf2009-09-11 00:07:24 +000022#include "clang/AST/DeclObjC.h"
Peter Collingbournefd05ca02011-06-14 04:02:39 +000023#include "clang/AST/GlobalDecl.h"
Peter Collingbourne14110472011-01-13 18:57:25 +000024#include "clang/AST/Mangle.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000025#include "clang/Basic/ABI.h"
26#include "clang/Basic/LangOptions.h"
Douglas Gregorb6cbe512013-01-14 17:21:00 +000027#include "clang/Basic/Module.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000028#include "llvm/ADT/DenseMap.h"
Douglas Gregorb6cbe512013-01-14 17:21:00 +000029#include "llvm/ADT/SetVector.h"
Rafael Espindola6a836702010-03-04 18:17:24 +000030#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000031#include "llvm/ADT/StringMap.h"
John McCallbd7370a2013-02-28 19:01:20 +000032#include "llvm/IR/CallingConv.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000033#include "llvm/IR/Module.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070034#include "llvm/IR/ValueHandle.h"
Anders Carlssonb723f752009-01-04 02:08:04 +000035
Reid Spencer5f016e22007-07-11 17:01:13 +000036namespace llvm {
Stephen Hines6bcf27b2014-05-29 04:14:42 -070037class Module;
38class Constant;
39class ConstantInt;
40class Function;
41class GlobalValue;
42class DataLayout;
43class FunctionType;
44class LLVMContext;
45class IndexedInstrProfReader;
Reid Spencer5f016e22007-07-11 17:01:13 +000046}
47
48namespace clang {
Stephen Hines6bcf27b2014-05-29 04:14:42 -070049class TargetCodeGenInfo;
50class ASTContext;
51class AtomicType;
52class FunctionDecl;
53class IdentifierInfo;
54class ObjCMethodDecl;
55class ObjCImplementationDecl;
56class ObjCCategoryImplDecl;
57class ObjCProtocolDecl;
58class ObjCEncodeExpr;
59class BlockExpr;
60class CharUnits;
61class Decl;
62class Expr;
63class Stmt;
64class InitListExpr;
65class StringLiteral;
66class NamedDecl;
67class ValueDecl;
68class VarDecl;
69class LangOptions;
70class CodeGenOptions;
71class DiagnosticsEngine;
72class AnnotateAttr;
73class CXXDestructorDecl;
Stephen Hines6bcf27b2014-05-29 04:14:42 -070074class Module;
Mike Stumpecc90e92009-02-13 19:12:34 +000075
Reid Spencer5f016e22007-07-11 17:01:13 +000076namespace CodeGen {
77
Stephen Hines6bcf27b2014-05-29 04:14:42 -070078class CallArgList;
79class CodeGenFunction;
80class CodeGenTBAA;
81class CGCXXABI;
82class CGDebugInfo;
83class CGObjCRuntime;
84class CGOpenCLRuntime;
85class CGOpenMPRuntime;
86class CGCUDARuntime;
87class BlockFieldFlags;
88class FunctionArgList;
Stephen Hines651f13c2014-04-23 16:59:28 -070089
Stephen Hines6bcf27b2014-05-29 04:14:42 -070090struct OrderGlobalInits {
91 unsigned int priority;
92 unsigned int lex_order;
93 OrderGlobalInits(unsigned int p, unsigned int l)
Chris Lattnerec2830d2010-06-27 06:32:58 +000094 : priority(p), lex_order(l) {}
Stephen Hines6bcf27b2014-05-29 04:14:42 -070095
96 bool operator==(const OrderGlobalInits &RHS) const {
97 return priority == RHS.priority && lex_order == RHS.lex_order;
98 }
99
100 bool operator<(const OrderGlobalInits &RHS) const {
101 return std::tie(priority, lex_order) <
102 std::tie(RHS.priority, RHS.lex_order);
103 }
104};
105
106struct CodeGenTypeCache {
107 /// void
108 llvm::Type *VoidTy;
109
110 /// i8, i16, i32, and i64
111 llvm::IntegerType *Int8Ty, *Int16Ty, *Int32Ty, *Int64Ty;
112 /// float, double
113 llvm::Type *FloatTy, *DoubleTy;
114
115 /// int
116 llvm::IntegerType *IntTy;
117
118 /// intptr_t, size_t, and ptrdiff_t, which we assume are the same size.
119 union {
120 llvm::IntegerType *IntPtrTy;
121 llvm::IntegerType *SizeTy;
122 llvm::IntegerType *PtrDiffTy;
Chris Lattnerec2830d2010-06-27 06:32:58 +0000123 };
John McCall5936e332011-02-15 09:22:45 +0000124
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700125 /// void* in address space 0
126 union {
127 llvm::PointerType *VoidPtrTy;
128 llvm::PointerType *Int8PtrTy;
John McCall5936e332011-02-15 09:22:45 +0000129 };
John McCallf85e1932011-06-15 23:02:42 +0000130
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700131 /// void** in address space 0
132 union {
133 llvm::PointerType *VoidPtrPtrTy;
134 llvm::PointerType *Int8PtrPtrTy;
135 };
136
137 /// The width of a pointer into the generic address space.
138 unsigned char PointerWidthInBits;
139
140 /// The size and alignment of a pointer into the generic address
141 /// space.
142 union {
143 unsigned char PointerAlignInBytes;
144 unsigned char PointerSizeInBytes;
145 unsigned char SizeSizeInBytes; // sizeof(size_t)
146 };
147
148 llvm::CallingConv::ID RuntimeCC;
149 llvm::CallingConv::ID getRuntimeCC() const { return RuntimeCC; }
150};
151
John McCallf85e1932011-06-15 23:02:42 +0000152struct RREntrypoints {
153 RREntrypoints() { memset(this, 0, sizeof(*this)); }
154 /// void objc_autoreleasePoolPop(void*);
155 llvm::Constant *objc_autoreleasePoolPop;
156
157 /// void *objc_autoreleasePoolPush(void);
158 llvm::Constant *objc_autoreleasePoolPush;
159};
160
161struct ARCEntrypoints {
162 ARCEntrypoints() { memset(this, 0, sizeof(*this)); }
163
164 /// id objc_autorelease(id);
165 llvm::Constant *objc_autorelease;
166
167 /// id objc_autoreleaseReturnValue(id);
168 llvm::Constant *objc_autoreleaseReturnValue;
169
170 /// void objc_copyWeak(id *dest, id *src);
171 llvm::Constant *objc_copyWeak;
172
173 /// void objc_destroyWeak(id*);
174 llvm::Constant *objc_destroyWeak;
175
176 /// id objc_initWeak(id*, id);
177 llvm::Constant *objc_initWeak;
178
179 /// id objc_loadWeak(id*);
180 llvm::Constant *objc_loadWeak;
181
182 /// id objc_loadWeakRetained(id*);
183 llvm::Constant *objc_loadWeakRetained;
184
185 /// void objc_moveWeak(id *dest, id *src);
186 llvm::Constant *objc_moveWeak;
187
188 /// id objc_retain(id);
189 llvm::Constant *objc_retain;
190
191 /// id objc_retainAutorelease(id);
192 llvm::Constant *objc_retainAutorelease;
193
194 /// id objc_retainAutoreleaseReturnValue(id);
195 llvm::Constant *objc_retainAutoreleaseReturnValue;
196
197 /// id objc_retainAutoreleasedReturnValue(id);
198 llvm::Constant *objc_retainAutoreleasedReturnValue;
199
200 /// id objc_retainBlock(id);
201 llvm::Constant *objc_retainBlock;
202
203 /// void objc_release(id);
204 llvm::Constant *objc_release;
205
206 /// id objc_storeStrong(id*, id);
207 llvm::Constant *objc_storeStrong;
208
209 /// id objc_storeWeak(id*, id);
210 llvm::Constant *objc_storeWeak;
211
212 /// A void(void) inline asm to use to mark that the return value of
213 /// a call will be immediately retain.
214 llvm::InlineAsm *retainAutoreleasedReturnValueMarker;
John McCallb6a60792013-03-23 02:35:54 +0000215
216 /// void clang.arc.use(...);
217 llvm::Constant *clang_arc_use;
John McCallf85e1932011-06-15 23:02:42 +0000218};
Will Dietz4f45bc02013-01-18 11:30:38 +0000219
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700220/// This class records statistics on instrumentation based profiling.
Stephen Hinesef822542014-07-21 00:47:37 -0700221class InstrProfStats {
222 uint32_t VisitedInMainFile;
223 uint32_t MissingInMainFile;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700224 uint32_t Visited;
225 uint32_t Missing;
226 uint32_t Mismatched;
Stephen Hinesef822542014-07-21 00:47:37 -0700227
228public:
229 InstrProfStats()
230 : VisitedInMainFile(0), MissingInMainFile(0), Visited(0), Missing(0),
231 Mismatched(0) {}
232 /// Record that we've visited a function and whether or not that function was
233 /// in the main source file.
234 void addVisited(bool MainFile) {
235 if (MainFile)
236 ++VisitedInMainFile;
237 ++Visited;
238 }
239 /// Record that a function we've visited has no profile data.
240 void addMissing(bool MainFile) {
241 if (MainFile)
242 ++MissingInMainFile;
243 ++Missing;
244 }
245 /// Record that a function we've visited has mismatched profile data.
246 void addMismatched(bool MainFile) { ++Mismatched; }
247 /// Whether or not the stats we've gathered indicate any potential problems.
248 bool hasDiagnostics() { return Missing || Mismatched; }
249 /// Report potential problems we've found to \c Diags.
250 void reportDiagnostics(DiagnosticsEngine &Diags, StringRef MainFile);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700251};
252
253/// This class organizes the cross-function state that is used while generating
254/// LLVM code.
John McCall5936e332011-02-15 09:22:45 +0000255class CodeGenModule : public CodeGenTypeCache {
Dmitri Gribenkof56faa02012-09-15 20:20:27 +0000256 CodeGenModule(const CodeGenModule &) LLVM_DELETED_FUNCTION;
257 void operator=(const CodeGenModule &) LLVM_DELETED_FUNCTION;
Anders Carlsson8a219ce2009-02-24 04:21:31 +0000258
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700259 struct Structor {
260 Structor() : Priority(0), Initializer(nullptr), AssociatedData(nullptr) {}
261 Structor(int Priority, llvm::Constant *Initializer,
262 llvm::Constant *AssociatedData)
263 : Priority(Priority), Initializer(Initializer),
264 AssociatedData(AssociatedData) {}
265 int Priority;
266 llvm::Constant *Initializer;
267 llvm::Constant *AssociatedData;
268 };
269
270 typedef std::vector<Structor> CtorList;
Daniel Dunbar6bfed7e2008-08-01 00:01:51 +0000271
Reid Spencer5f016e22007-07-11 17:01:13 +0000272 ASTContext &Context;
David Blaikie4e4d0842012-03-11 07:00:24 +0000273 const LangOptions &LangOpts;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000274 const CodeGenOptions &CodeGenOpts;
Reid Spencer5f016e22007-07-11 17:01:13 +0000275 llvm::Module &TheModule;
David Blaikied6471f72011-09-25 23:23:43 +0000276 DiagnosticsEngine &Diags;
John McCall64aa4b32013-04-16 22:48:15 +0000277 const llvm::DataLayout &TheDataLayout;
278 const TargetInfo &Target;
Stephen Hines651f13c2014-04-23 16:59:28 -0700279 std::unique_ptr<CGCXXABI> ABI;
John McCall64aa4b32013-04-16 22:48:15 +0000280 llvm::LLVMContext &VMContext;
Anders Carlssonb5404912009-10-07 01:06:45 +0000281
John McCall64aa4b32013-04-16 22:48:15 +0000282 CodeGenTBAA *TBAA;
283
284 mutable const TargetCodeGenInfo *TheTargetCodeGenInfo;
285
286 // This should not be moved earlier, since its initialization depends on some
287 // of the previous reference members being already initialized and also checks
288 // if TheTargetCodeGenInfo is NULL
289 CodeGenTypes Types;
290
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700291 /// Holds information about C++ vtables.
Anders Carlssonaf440352010-03-23 04:11:45 +0000292 CodeGenVTables VTables;
Douglas Gregore17ad2f2010-04-08 16:07:47 +0000293
Peter Collingbournee9265232011-07-27 20:29:46 +0000294 CGObjCRuntime* ObjCRuntime;
Peter Collingbourne8c25fc52011-09-19 21:14:35 +0000295 CGOpenCLRuntime* OpenCLRuntime;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700296 CGOpenMPRuntime* OpenMPRuntime;
Peter Collingbourne6c0aa5f2011-10-06 18:29:37 +0000297 CGCUDARuntime* CUDARuntime;
Ted Kremenek815c78f2008-08-05 18:50:11 +0000298 CGDebugInfo* DebugInfo;
John McCallf85e1932011-06-15 23:02:42 +0000299 ARCEntrypoints *ARCData;
Dan Gohmanb49bd272012-02-16 00:57:37 +0000300 llvm::MDNode *NoObjCARCExceptionsMetadata;
John McCallf85e1932011-06-15 23:02:42 +0000301 RREntrypoints *RRData;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700302 std::unique_ptr<llvm::IndexedInstrProfReader> PGOReader;
303 InstrProfStats PGOStats;
Daniel Dunbar9986eab2008-07-30 16:32:24 +0000304
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700305 // A set of references that have only been seen via a weakref so far. This is
306 // used to remove the weak of the reference if we ever see a direct reference
307 // or a definition.
Rafael Espindola6a836702010-03-04 18:17:24 +0000308 llvm::SmallPtrSet<llvm::GlobalValue*, 10> WeakRefReferences;
309
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700310 /// This contains all the decls which have definitions but/ which are deferred
311 /// for emission and therefore should only be output if they are actually
312 /// used. If a decl is in this, then it is known to have not been referenced
313 /// yet.
Stephen Hinesef822542014-07-21 00:47:37 -0700314 std::map<StringRef, GlobalDecl> DeferredDecls;
Daniel Dunbar02698712009-02-13 20:29:50 +0000315
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700316 /// This is a list of deferred decls which we have seen that *are* actually
317 /// referenced. These get code generated when the module is done.
Stephen Hines651f13c2014-04-23 16:59:28 -0700318 struct DeferredGlobal {
319 DeferredGlobal(llvm::GlobalValue *GV, GlobalDecl GD) : GV(GV), GD(GD) {}
320 llvm::AssertingVH<llvm::GlobalValue> GV;
321 GlobalDecl GD;
322 };
323 std::vector<DeferredGlobal> DeferredDeclsToEmit;
324 void addDeferredDeclToEmit(llvm::GlobalValue *GV, GlobalDecl GD) {
325 DeferredDeclsToEmit.push_back(DeferredGlobal(GV, GD));
326 }
Daniel Dunbar03f5ad92009-04-15 22:08:45 +0000327
Rafael Espindolad2054982013-10-22 19:26:13 +0000328 /// List of alias we have emitted. Used to make sure that what they point to
329 /// is defined once we get to the end of the of the translation unit.
330 std::vector<GlobalDecl> Aliases;
331
Rafael Espindola61a0a752013-11-05 21:37:29 +0000332 typedef llvm::StringMap<llvm::TrackingVH<llvm::Constant> > ReplacementsTy;
333 ReplacementsTy Replacements;
334
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700335 /// A queue of (optional) vtables to consider emitting.
John McCalld5617ee2013-01-25 22:31:03 +0000336 std::vector<const CXXRecordDecl*> DeferredVTables;
337
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700338 /// List of global values which are required to be present in the object file;
339 /// bitcast to i8*. This is used for forcing visibility of symbols which may
340 /// otherwise be optimized out.
Chris Lattner35f38a22009-03-31 22:37:52 +0000341 std::vector<llvm::WeakVH> LLVMUsed;
Stephen Hines651f13c2014-04-23 16:59:28 -0700342 std::vector<llvm::WeakVH> LLVMCompilerUsed;
Mike Stumpecc90e92009-02-13 19:12:34 +0000343
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700344 /// Store the list of global constructors and their respective priorities to
345 /// be emitted when the translation unit is complete.
Daniel Dunbar6bfed7e2008-08-01 00:01:51 +0000346 CtorList GlobalCtors;
347
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700348 /// Store the list of global destructors and their respective priorities to be
349 /// emitted when the translation unit is complete.
Daniel Dunbar6bfed7e2008-08-01 00:01:51 +0000350 CtorList GlobalDtors;
351
Stephen Hinesef822542014-07-21 00:47:37 -0700352 /// An ordered map of canonical GlobalDecls to their mangled names.
353 llvm::MapVector<GlobalDecl, StringRef> MangledDeclNames;
354 llvm::StringMap<GlobalDecl, llvm::BumpPtrAllocator> Manglings;
355
Julien Lerouge77f68bb2011-09-09 22:41:49 +0000356 /// Global annotations.
Nate Begeman532485c2008-04-18 23:43:57 +0000357 std::vector<llvm::Constant*> Annotations;
Mike Stumpecc90e92009-02-13 19:12:34 +0000358
Julien Lerouge77f68bb2011-09-09 22:41:49 +0000359 /// Map used to get unique annotation strings.
360 llvm::StringMap<llvm::Constant*> AnnotationStrings;
361
Anders Carlssonc9e20912007-08-21 00:21:21 +0000362 llvm::StringMap<llvm::Constant*> CFConstantStringMap;
Stephen Hines651f13c2014-04-23 16:59:28 -0700363
364 llvm::StringMap<llvm::GlobalVariable *> Constant1ByteStringMap;
365 llvm::StringMap<llvm::GlobalVariable *> Constant2ByteStringMap;
366 llvm::StringMap<llvm::GlobalVariable *> Constant4ByteStringMap;
Eli Friedman71cba342012-03-09 03:27:46 +0000367 llvm::DenseMap<const Decl*, llvm::Constant *> StaticLocalDeclMap;
John McCall355bba72012-03-30 21:00:39 +0000368 llvm::DenseMap<const Decl*, llvm::GlobalVariable*> StaticLocalDeclGuardMap;
Richard Smith211c8dd2013-06-05 00:46:14 +0000369 llvm::DenseMap<const Expr*, llvm::Constant *> MaterializedGlobalTemporaryMap;
370
Fariborz Jahanianb08cfb32012-01-08 19:13:23 +0000371 llvm::DenseMap<QualType, llvm::Constant *> AtomicSetterHelperFnMap;
372 llvm::DenseMap<QualType, llvm::Constant *> AtomicGetterHelperFnMap;
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000373
Will Dietz562d45c2013-11-08 01:09:22 +0000374 /// Map used to get unique type descriptor constants for sanitizers.
375 llvm::DenseMap<QualType, llvm::Constant *> TypeDescriptorMap;
376
Richard Smith00249372013-04-06 05:00:46 +0000377 /// Map used to track internal linkage functions declared within
378 /// extern "C" regions.
Richard Smith84083b72013-04-13 01:28:18 +0000379 typedef llvm::MapVector<IdentifierInfo *,
380 llvm::GlobalValue *> StaticExternCMap;
Richard Smith00249372013-04-06 05:00:46 +0000381 StaticExternCMap StaticExternCValues;
382
Richard Smithb80a16e2013-04-19 16:42:07 +0000383 /// \brief thread_local variables defined or used in this TU.
384 std::vector<std::pair<const VarDecl *, llvm::GlobalVariable *> >
385 CXXThreadLocals;
386
387 /// \brief thread_local variables with initializers that need to run
388 /// before any thread_local variable in this TU is odr-used.
389 std::vector<llvm::Constant*> CXXThreadLocalInits;
390
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700391 /// Global variables with initializers that need to run before main.
Eli Friedman6c6bda32010-01-08 00:50:11 +0000392 std::vector<llvm::Constant*> CXXGlobalInits;
John McCallbf40cb52010-07-15 23:40:35 +0000393
394 /// When a C++ decl with an initializer is deferred, null is
395 /// appended to CXXGlobalInits, and the index of that null is placed
396 /// here so that the initializer will be performed in the correct
397 /// order.
398 llvm::DenseMap<const Decl*, unsigned> DelayedCXXInitPosition;
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000399
Anton Korobeynikov4179ddd2012-11-06 22:44:45 +0000400 typedef std::pair<OrderGlobalInits, llvm::Function*> GlobalInitData;
401
402 struct GlobalInitPriorityCmp {
403 bool operator()(const GlobalInitData &LHS,
404 const GlobalInitData &RHS) const {
405 return LHS.first.priority < RHS.first.priority;
406 }
407 };
408
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700409 /// Global variables with initializers whose order of initialization is set by
410 /// init_priority attribute.
Anton Korobeynikov4179ddd2012-11-06 22:44:45 +0000411 SmallVector<GlobalInitData, 8> PrioritizedCXXGlobalInits;
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700413 /// Global destructor functions and arguments that need to run on termination.
Chris Lattner810112e2010-06-19 05:52:45 +0000414 std::vector<std::pair<llvm::WeakVH,llvm::Constant*> > CXXGlobalDtors;
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000415
Douglas Gregorb6cbe512013-01-14 17:21:00 +0000416 /// \brief The complete set of modules that has been imported.
417 llvm::SetVector<clang::Module *> ImportedModules;
418
Reid Kleckner3190ca92013-05-08 13:44:39 +0000419 /// \brief A vector of metadata strings.
420 SmallVector<llvm::Value *, 16> LinkerOptionsMetadata;
421
Douglas Gregor45c4ea72011-08-09 15:54:21 +0000422 /// @name Cache for Objective-C runtime types
423 /// @{
424
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700425 /// Cached reference to the class for constant strings. This value has type
426 /// int * but is actually an Obj-C class pointer.
Fariborz Jahanian428edb72013-04-16 15:25:39 +0000427 llvm::WeakVH CFConstantStringClassRef;
Mike Stump1eb44332009-09-09 15:08:12 +0000428
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 ConstantStringClassRef;
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +0000432
Douglas Gregor45c4ea72011-08-09 15:54:21 +0000433 /// \brief The LLVM type corresponding to NSConstantString.
434 llvm::StructType *NSConstantStringType;
435
Douglas Gregor0815b572011-08-09 17:23:49 +0000436 /// \brief The type used to describe the state of a fast enumeration in
437 /// Objective-C's for..in loop.
438 QualType ObjCFastEnumerationStateType;
439
Douglas Gregor45c4ea72011-08-09 15:54:21 +0000440 /// @}
441
David Chisnall0d13f6f2010-01-23 02:40:42 +0000442 /// Lazily create the Objective-C runtime
443 void createObjCRuntime();
444
Peter Collingbourne8c25fc52011-09-19 21:14:35 +0000445 void createOpenCLRuntime();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700446 void createOpenMPRuntime();
Peter Collingbourne6c0aa5f2011-10-06 18:29:37 +0000447 void createCUDARuntime();
Peter Collingbourne8c25fc52011-09-19 21:14:35 +0000448
Rafael Espindolabcf6b982011-12-19 14:41:01 +0000449 bool isTriviallyRecursive(const FunctionDecl *F);
Peter Collingbourne144a31f2013-06-05 17:49:37 +0000450 bool shouldEmitFunction(GlobalDecl GD);
Daniel Dunbar673431a2010-07-16 00:00:15 +0000451
452 /// @name Cache for Blocks Runtime Globals
453 /// @{
454
455 llvm::Constant *NSConcreteGlobalBlock;
456 llvm::Constant *NSConcreteStackBlock;
Daniel Dunbar754b9fb2010-07-16 00:00:19 +0000457
Daniel Dunbar673431a2010-07-16 00:00:15 +0000458 llvm::Constant *BlockObjectAssign;
459 llvm::Constant *BlockObjectDispose;
460
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000461 llvm::Type *BlockDescriptorType;
462 llvm::Type *GenericBlockLiteralType;
John McCalld16c2cf2011-02-08 08:22:06 +0000463
464 struct {
465 int GlobalUniqueCount;
466 } Block;
Will Dietz4f45bc02013-01-18 11:30:38 +0000467
Nadav Rotem495cfa42013-03-23 06:43:35 +0000468 /// void @llvm.lifetime.start(i64 %size, i8* nocapture <ptr>)
469 llvm::Constant *LifetimeStartFn;
470
471 /// void @llvm.lifetime.end(i64 %size, i8* nocapture <ptr>)
472 llvm::Constant *LifetimeEndFn;
473
Fariborz Jahanian4904bf42012-06-26 16:06:38 +0000474 GlobalDecl initializedGlobalDecl;
John McCalld16c2cf2011-02-08 08:22:06 +0000475
Stephen Hinesef822542014-07-21 00:47:37 -0700476 SanitizerBlacklist SanitizerBL;
Will Dietz4f45bc02013-01-18 11:30:38 +0000477
Daniel Dunbar673431a2010-07-16 00:00:15 +0000478 /// @}
Reid Spencer5f016e22007-07-11 17:01:13 +0000479public:
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000480 CodeGenModule(ASTContext &C, const CodeGenOptions &CodeGenOpts,
John McCall3abae092013-04-16 22:48:20 +0000481 llvm::Module &M, const llvm::DataLayout &TD,
482 DiagnosticsEngine &Diags);
Ted Kremenek815c78f2008-08-05 18:50:11 +0000483
Chris Lattner2b94fe32008-03-01 08:45:05 +0000484 ~CodeGenModule();
Mike Stumpecc90e92009-02-13 19:12:34 +0000485
Stephen Hines651f13c2014-04-23 16:59:28 -0700486 void clear();
487
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700488 /// Finalize LLVM code generation.
Ted Kremenek815c78f2008-08-05 18:50:11 +0000489 void Release();
Daniel Dunbar208ff5e2008-08-11 18:12:00 +0000490
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700491 /// Return a reference to the configured Objective-C runtime.
Mike Stumpecc90e92009-02-13 19:12:34 +0000492 CGObjCRuntime &getObjCRuntime() {
Peter Collingbournee9265232011-07-27 20:29:46 +0000493 if (!ObjCRuntime) createObjCRuntime();
494 return *ObjCRuntime;
Daniel Dunbar208ff5e2008-08-11 18:12:00 +0000495 }
Mike Stumpecc90e92009-02-13 19:12:34 +0000496
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700497 /// Return true iff an Objective-C runtime has been configured.
Peter Collingbournee9265232011-07-27 20:29:46 +0000498 bool hasObjCRuntime() { return !!ObjCRuntime; }
Daniel Dunbar208ff5e2008-08-11 18:12:00 +0000499
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700500 /// Return a reference to the configured OpenCL runtime.
Peter Collingbourne8c25fc52011-09-19 21:14:35 +0000501 CGOpenCLRuntime &getOpenCLRuntime() {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700502 assert(OpenCLRuntime != nullptr);
Peter Collingbourne8c25fc52011-09-19 21:14:35 +0000503 return *OpenCLRuntime;
504 }
505
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700506 /// Return a reference to the configured OpenMP runtime.
507 CGOpenMPRuntime &getOpenMPRuntime() {
508 assert(OpenMPRuntime != nullptr);
509 return *OpenMPRuntime;
510 }
511
512 /// Return a reference to the configured CUDA runtime.
Peter Collingbourne6c0aa5f2011-10-06 18:29:37 +0000513 CGCUDARuntime &getCUDARuntime() {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700514 assert(CUDARuntime != nullptr);
Peter Collingbourne6c0aa5f2011-10-06 18:29:37 +0000515 return *CUDARuntime;
516 }
517
John McCallf85e1932011-06-15 23:02:42 +0000518 ARCEntrypoints &getARCEntrypoints() const {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700519 assert(getLangOpts().ObjCAutoRefCount && ARCData != nullptr);
John McCallf85e1932011-06-15 23:02:42 +0000520 return *ARCData;
521 }
522
523 RREntrypoints &getRREntrypoints() const {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700524 assert(RRData != nullptr);
John McCallf85e1932011-06-15 23:02:42 +0000525 return *RRData;
526 }
527
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700528 InstrProfStats &getPGOStats() { return PGOStats; }
529 llvm::IndexedInstrProfReader *getPGOReader() const { return PGOReader.get(); }
Stephen Hines651f13c2014-04-23 16:59:28 -0700530
Eli Friedman71cba342012-03-09 03:27:46 +0000531 llvm::Constant *getStaticLocalDeclAddress(const VarDecl *D) {
532 return StaticLocalDeclMap[D];
Fariborz Jahanian65ad5a42010-04-18 21:01:23 +0000533 }
Fariborz Jahanian63326a52010-04-19 18:15:02 +0000534 void setStaticLocalDeclAddress(const VarDecl *D,
Eli Friedman71cba342012-03-09 03:27:46 +0000535 llvm::Constant *C) {
536 StaticLocalDeclMap[D] = C;
Fariborz Jahanian65ad5a42010-04-18 21:01:23 +0000537 }
538
John McCall355bba72012-03-30 21:00:39 +0000539 llvm::GlobalVariable *getStaticLocalDeclGuardAddress(const VarDecl *D) {
540 return StaticLocalDeclGuardMap[D];
541 }
542 void setStaticLocalDeclGuardAddress(const VarDecl *D,
543 llvm::GlobalVariable *C) {
544 StaticLocalDeclGuardMap[D] = C;
545 }
546
Stephen Hinesef822542014-07-21 00:47:37 -0700547 bool lookupRepresentativeDecl(StringRef MangledName,
548 GlobalDecl &Result) const;
549
Fariborz Jahanianb08cfb32012-01-08 19:13:23 +0000550 llvm::Constant *getAtomicSetterHelperFnMap(QualType Ty) {
551 return AtomicSetterHelperFnMap[Ty];
Fariborz Jahaniancd93b962012-01-06 22:33:54 +0000552 }
Fariborz Jahanianb08cfb32012-01-08 19:13:23 +0000553 void setAtomicSetterHelperFnMap(QualType Ty,
Fariborz Jahaniancd93b962012-01-06 22:33:54 +0000554 llvm::Constant *Fn) {
Fariborz Jahanianb08cfb32012-01-08 19:13:23 +0000555 AtomicSetterHelperFnMap[Ty] = Fn;
556 }
557
558 llvm::Constant *getAtomicGetterHelperFnMap(QualType Ty) {
559 return AtomicGetterHelperFnMap[Ty];
560 }
561 void setAtomicGetterHelperFnMap(QualType Ty,
562 llvm::Constant *Fn) {
563 AtomicGetterHelperFnMap[Ty] = Fn;
Fariborz Jahaniancd93b962012-01-06 22:33:54 +0000564 }
565
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700566 llvm::Constant *getTypeDescriptorFromMap(QualType Ty) {
Will Dietz562d45c2013-11-08 01:09:22 +0000567 return TypeDescriptorMap[Ty];
568 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700569 void setTypeDescriptorInMap(QualType Ty, llvm::Constant *C) {
Will Dietz562d45c2013-11-08 01:09:22 +0000570 TypeDescriptorMap[Ty] = C;
571 }
572
Devang Patelaa112892011-03-07 18:45:56 +0000573 CGDebugInfo *getModuleDebugInfo() { return DebugInfo; }
Devang Patel5de7a0e2011-03-07 18:29:53 +0000574
Dan Gohmanb49bd272012-02-16 00:57:37 +0000575 llvm::MDNode *getNoObjCARCExceptionsMetadata() {
576 if (!NoObjCARCExceptionsMetadata)
577 NoObjCARCExceptionsMetadata =
578 llvm::MDNode::get(getLLVMContext(),
579 SmallVector<llvm::Value*,1>());
580 return NoObjCARCExceptionsMetadata;
581 }
582
Reid Spencer5f016e22007-07-11 17:01:13 +0000583 ASTContext &getContext() const { return Context; }
David Blaikie4e4d0842012-03-11 07:00:24 +0000584 const LangOptions &getLangOpts() const { return LangOpts; }
John McCall64aa4b32013-04-16 22:48:15 +0000585 const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
Reid Spencer5f016e22007-07-11 17:01:13 +0000586 llvm::Module &getModule() const { return TheModule; }
David Blaikied6471f72011-09-25 23:23:43 +0000587 DiagnosticsEngine &getDiags() const { return Diags; }
Micah Villmow25a6a842012-10-08 16:25:52 +0000588 const llvm::DataLayout &getDataLayout() const { return TheDataLayout; }
John McCall64aa4b32013-04-16 22:48:15 +0000589 const TargetInfo &getTarget() const { return Target; }
Stephen Hines651f13c2014-04-23 16:59:28 -0700590 CGCXXABI &getCXXABI() const { return *ABI; }
Owen Andersona1cf15f2009-07-14 23:10:40 +0000591 llvm::LLVMContext &getLLVMContext() { return VMContext; }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700592
593 bool shouldUseTBAA() const { return TBAA != nullptr; }
John McCallbc7fbf02011-02-26 08:07:02 +0000594
John McCall64aa4b32013-04-16 22:48:15 +0000595 const TargetCodeGenInfo &getTargetCodeGenInfo();
596
597 CodeGenTypes &getTypes() { return Types; }
598
599 CodeGenVTables &getVTables() { return VTables; }
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +0000600
Timur Iskhodzhanov5f0db582013-11-05 15:54:58 +0000601 ItaniumVTableContext &getItaniumVTableContext() {
602 return VTables.getItaniumVTableContext();
Timur Iskhodzhanovf0746582013-10-09 11:33:51 +0000603 }
John McCall64aa4b32013-04-16 22:48:15 +0000604
Timur Iskhodzhanov5f0db582013-11-05 15:54:58 +0000605 MicrosoftVTableContext &getMicrosoftVTableContext() {
606 return VTables.getMicrosoftVTableContext();
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +0000607 }
608
Dan Gohman3d5aff52010-10-14 23:06:10 +0000609 llvm::MDNode *getTBAAInfo(QualType QTy);
Kostya Serebryany8cb4a072012-03-26 17:03:51 +0000610 llvm::MDNode *getTBAAInfoForVTablePtr();
Dan Gohmanb22c7dc2012-09-28 21:58:29 +0000611 llvm::MDNode *getTBAAStructInfo(QualType QTy);
Manman Renb37a73d2013-04-04 21:53:22 +0000612 /// Return the MDNode in the type DAG for the given struct type.
613 llvm::MDNode *getTBAAStructTypeInfo(QualType QTy);
614 /// Return the path-aware tag for given base type, access node and offset.
615 llvm::MDNode *getTBAAStructTagInfo(QualType BaseTy, llvm::MDNode *AccessN,
616 uint64_t O);
Dan Gohman3d5aff52010-10-14 23:06:10 +0000617
Richard Smitha9b21d22012-02-17 06:48:11 +0000618 bool isTypeConstant(QualType QTy, bool ExcludeCtorDtor);
619
John McCall9eda3ab2013-03-07 21:37:17 +0000620 bool isPaddedAtomicType(QualType type);
621 bool isPaddedAtomicType(const AtomicType *type);
622
Manman Renca835182013-04-11 23:02:56 +0000623 /// Decorate the instruction with a TBAA tag. For scalar TBAA, the tag
624 /// is the same as the type. For struct-path aware TBAA, the tag
625 /// is different from the type: base type, access type and offset.
626 /// When ConvertTypeToTag is true, we create a tag based on the scalar type.
627 void DecorateInstruction(llvm::Instruction *Inst,
628 llvm::MDNode *TBAAInfo,
629 bool ConvertTypeToTag = true);
Dan Gohman3d5aff52010-10-14 23:06:10 +0000630
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700631 /// Emit the given number of characters as a value of type size_t.
John McCallbc8d40d2011-06-24 21:55:10 +0000632 llvm::ConstantInt *getSize(CharUnits numChars);
633
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700634 /// Set the visibility for the given LLVM GlobalValue.
Anders Carlsson0ffeaad2011-01-29 19:39:23 +0000635 void setGlobalVisibility(llvm::GlobalValue *GV, const NamedDecl *D) const;
Daniel Dunbar04d40782009-04-14 06:00:08 +0000636
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700637 /// Set the TLS mode for the given LLVM GlobalVariable for the thread-local
638 /// variable declaration D.
Hans Wennborgde981f32012-06-28 08:01:44 +0000639 void setTLSMode(llvm::GlobalVariable *GV, const VarDecl &D) const;
640
Anders Carlssonc7e98fa2011-01-29 20:59:35 +0000641 static llvm::GlobalValue::VisibilityTypes GetLLVMVisibility(Visibility V) {
642 switch (V) {
643 case DefaultVisibility: return llvm::GlobalValue::DefaultVisibility;
644 case HiddenVisibility: return llvm::GlobalValue::HiddenVisibility;
645 case ProtectedVisibility: return llvm::GlobalValue::ProtectedVisibility;
646 }
647 llvm_unreachable("unknown visibility!");
Anders Carlssonc7e98fa2011-01-29 20:59:35 +0000648 }
649
John McCalld46f9852010-02-19 01:32:20 +0000650 llvm::Constant *GetAddrOfGlobal(GlobalDecl GD) {
651 if (isa<CXXConstructorDecl>(GD.getDecl()))
652 return GetAddrOfCXXConstructor(cast<CXXConstructorDecl>(GD.getDecl()),
653 GD.getCtorType());
654 else if (isa<CXXDestructorDecl>(GD.getDecl()))
655 return GetAddrOfCXXDestructor(cast<CXXDestructorDecl>(GD.getDecl()),
656 GD.getDtorType());
657 else if (isa<FunctionDecl>(GD.getDecl()))
658 return GetAddrOfFunction(GD);
659 else
660 return GetAddrOfGlobalVar(cast<VarDecl>(GD.getDecl()));
661 }
662
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700663 /// Will return a global variable of the given type. If a variable with a
664 /// different type already exists then a new variable with the right type
665 /// will be created and all uses of the old variable will be replaced with a
666 /// bitcast to the new variable.
Anders Carlsson3bd62022011-01-29 18:20:20 +0000667 llvm::GlobalVariable *
Chris Lattner686775d2011-07-20 06:58:45 +0000668 CreateOrReplaceCXXRuntimeVariable(StringRef Name, llvm::Type *Ty,
Anders Carlsson3bd62022011-01-29 18:20:20 +0000669 llvm::GlobalValue::LinkageTypes Linkage);
670
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700671 /// Return the address space of the underlying global variable for D, as
672 /// determined by its declaration. Normally this is the same as the address
673 /// space of D's type, but in CUDA, address spaces are associated with
674 /// declarations, not types.
Peter Collingbourne4dc34eb2012-05-20 21:08:35 +0000675 unsigned GetGlobalVarAddressSpace(const VarDecl *D, unsigned AddrSpace);
676
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700677 /// Return the llvm::Constant for the address of the given global variable.
678 /// If Ty is non-null and if the global doesn't exist, then it will be greated
679 /// with the specified type instead of whatever the normal requested type
680 /// would be.
Chris Lattner570585c2009-03-21 09:16:30 +0000681 llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D,
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700682 llvm::Type *Ty = nullptr);
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000683
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700684 /// Return the address of the given function. If Ty is non-null, then this
685 /// function will use the specified type if it has to create it.
Stephen Hines651f13c2014-04-23 16:59:28 -0700686 llvm::Constant *GetAddrOfFunction(GlobalDecl GD, llvm::Type *Ty = 0,
687 bool ForVTable = false,
688 bool DontDefer = false);
Daniel Dunbar61432932008-08-13 23:20:05 +0000689
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700690 /// Get the address of the RTTI descriptor for the given type.
John McCall9dffe6f2010-04-30 01:15:21 +0000691 llvm::Constant *GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH = false);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000692
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700693 /// Get the address of a uuid descriptor .
Nico Weberc5f80462012-10-11 10:13:44 +0000694 llvm::Constant *GetAddrOfUuidDescriptor(const CXXUuidofExpr* E);
695
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700696 /// Get the address of the thunk for the given global decl.
Anders Carlsson84c49e42011-02-06 17:15:43 +0000697 llvm::Constant *GetAddrOfThunk(GlobalDecl GD, const ThunkInfo &Thunk);
Anders Carlsson19879c92010-03-23 17:17:29 +0000698
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700699 /// Get a reference to the target of VD.
Rafael Espindola6a836702010-03-04 18:17:24 +0000700 llvm::Constant *GetWeakRefReference(const ValueDecl *VD);
701
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700702 /// Returns the offset from a derived class to a class. Returns null if the
703 /// offset is 0.
Anders Carlssona04efdf2010-04-24 21:23:59 +0000704 llvm::Constant *
705 GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl,
John McCallf871d0c2010-08-07 06:22:56 +0000706 CastExpr::path_const_iterator PathBegin,
707 CastExpr::path_const_iterator PathEnd);
John McCalld16c2cf2011-02-08 08:22:06 +0000708
John McCallf0c11f72011-03-31 08:03:29 +0000709 /// A pair of helper functions for a __block variable.
710 class ByrefHelpers : public llvm::FoldingSetNode {
711 public:
712 llvm::Constant *CopyHelper;
713 llvm::Constant *DisposeHelper;
714
715 /// The alignment of the field. This is important because
716 /// different offsets to the field within the byref struct need to
717 /// have different helper functions.
718 CharUnits Alignment;
719
720 ByrefHelpers(CharUnits alignment) : Alignment(alignment) {}
721 virtual ~ByrefHelpers();
722
723 void Profile(llvm::FoldingSetNodeID &id) const {
724 id.AddInteger(Alignment.getQuantity());
725 profileImpl(id);
726 }
727 virtual void profileImpl(llvm::FoldingSetNodeID &id) const = 0;
728
729 virtual bool needsCopy() const { return true; }
730 virtual void emitCopy(CodeGenFunction &CGF,
731 llvm::Value *dest, llvm::Value *src) = 0;
732
733 virtual bool needsDispose() const { return true; }
734 virtual void emitDispose(CodeGenFunction &CGF, llvm::Value *field) = 0;
735 };
736
737 llvm::FoldingSet<ByrefHelpers> ByrefHelpersCache;
John McCalld16c2cf2011-02-08 08:22:06 +0000738
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700739 /// Fetches the global unique block count.
John McCall8178df32011-02-22 22:38:33 +0000740 int getUniqueBlockCount() { return ++Block.GlobalUniqueCount; }
Fariborz Jahanian4904bf42012-06-26 16:06:38 +0000741
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700742 /// Fetches the type of a generic block descriptor.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000743 llvm::Type *getBlockDescriptorType();
John McCalld16c2cf2011-02-08 08:22:06 +0000744
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700745 /// The type of a generic block literal.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000746 llvm::Type *getGenericBlockLiteralType();
John McCalld16c2cf2011-02-08 08:22:06 +0000747
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700748 /// Gets the address of a block which requires no captures.
John McCalld16c2cf2011-02-08 08:22:06 +0000749 llvm::Constant *GetAddrOfGlobalBlock(const BlockExpr *BE, const char *);
Anders Carlssona04efdf2010-04-24 21:23:59 +0000750
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700751 /// Return a pointer to a constant CFString object for the given string.
Steve Naroff8d4141f2009-04-01 13:55:36 +0000752 llvm::Constant *GetAddrOfConstantCFString(const StringLiteral *Literal);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700753
754 /// Return a pointer to a constant NSString object for the given string. Or a
755 /// user defined String object as defined via
Fariborz Jahanian4c733072010-10-19 17:19:29 +0000756 /// -fconstant-string-class=class_name option.
757 llvm::Constant *GetAddrOfConstantString(const StringLiteral *Literal);
Chris Lattnera7ad98f2008-02-11 00:02:17 +0000758
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700759 /// Return a constant array for the given string.
Eli Friedman64f45a22011-11-01 02:23:42 +0000760 llvm::Constant *GetConstantArrayFromStringLiteral(const StringLiteral *E);
761
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700762 /// Return a pointer to a constant array for the given string literal.
Daniel Dunbar61432932008-08-13 23:20:05 +0000763 llvm::Constant *GetAddrOfConstantStringFromLiteral(const StringLiteral *S);
Daniel Dunbar1e049762008-08-10 20:25:57 +0000764
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700765 /// Return a pointer to a constant array for the given ObjCEncodeExpr node.
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000766 llvm::Constant *GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *);
Mike Stump1eb44332009-09-09 15:08:12 +0000767
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700768 /// Returns a pointer to a character array containing the literal and a
769 /// terminating '\0' character. The result has pointer to array type.
Daniel Dunbar5fabf9d2008-10-17 21:56:50 +0000770 ///
Mike Stumpecc90e92009-02-13 19:12:34 +0000771 /// \param GlobalName If provided, the name to use for the global (if one is
772 /// created).
Daniel Dunbar5fabf9d2008-10-17 21:56:50 +0000773 llvm::Constant *GetAddrOfConstantCString(const std::string &str,
Stephen Hinesef822542014-07-21 00:47:37 -0700774 const char *GlobalName = nullptr,
775 unsigned Alignment = 0);
Richard Smith7401cf52011-11-22 22:48:32 +0000776
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700777 /// Returns a pointer to a constant global variable for the given file-scope
778 /// compound literal expression.
Richard Smith7401cf52011-11-22 22:48:32 +0000779 llvm::Constant *GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr*E);
Richard Smith211c8dd2013-06-05 00:46:14 +0000780
781 /// \brief Returns a pointer to a global variable representing a temporary
782 /// with static or thread storage duration.
783 llvm::Constant *GetAddrOfGlobalTemporary(const MaterializeTemporaryExpr *E,
784 const Expr *Inner);
785
Douglas Gregor0815b572011-08-09 17:23:49 +0000786 /// \brief Retrieve the record type that describes the state of an
787 /// Objective-C fast enumeration loop (for..in).
788 QualType getObjCFastEnumerationStateType();
789
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700790 /// Return the address of the constructor of the given type.
791 llvm::GlobalValue *
792 GetAddrOfCXXConstructor(const CXXConstructorDecl *ctor, CXXCtorType ctorType,
793 const CGFunctionInfo *fnInfo = nullptr,
794 bool DontDefer = false);
Anders Carlsson27ae5362009-04-17 01:58:57 +0000795
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700796 /// Return the address of the constructor of the given type.
797 llvm::GlobalValue *
798 GetAddrOfCXXDestructor(const CXXDestructorDecl *dtor,
799 CXXDtorType dtorType,
800 const CGFunctionInfo *fnInfo = nullptr,
801 llvm::FunctionType *fnType = nullptr,
802 bool DontDefer = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000803
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700804 /// Given a builtin id for a function like "__builtin_fabsf", return a
805 /// Function* for "fabsf".
Daniel Dunbar34771b52009-09-14 04:33:21 +0000806 llvm::Value *getBuiltinLibFunction(const FunctionDecl *FD,
807 unsigned BuiltinID);
Daniel Dunbar61432932008-08-13 23:20:05 +0000808
Dmitri Gribenko55431692013-05-05 00:41:58 +0000809 llvm::Function *getIntrinsic(unsigned IID, ArrayRef<llvm::Type*> Tys = None);
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000810
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700811 /// Emit code for a single top level declaration.
Daniel Dunbar41071de2008-08-15 23:26:23 +0000812 void EmitTopLevelDecl(Decl *D);
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000813
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700814 /// Tell the consumer that this variable has been instantiated.
Rafael Espindola02503932012-03-08 15:51:03 +0000815 void HandleCXXStaticMemberVarInstantiation(VarDecl *VD);
Rafael Espindola234fe652012-03-05 10:54:55 +0000816
Richard Smith00249372013-04-06 05:00:46 +0000817 /// \brief If the declaration has internal linkage but is inside an
818 /// extern "C" linkage specification, prepare to emit an alias for it
819 /// to the expected name.
820 template<typename SomeDecl>
821 void MaybeHandleStaticInExternC(const SomeDecl *D, llvm::GlobalValue *GV);
822
Stephen Hines651f13c2014-04-23 16:59:28 -0700823 /// Add a global to a list to be added to the llvm.used metadata.
824 void addUsedGlobal(llvm::GlobalValue *GV);
825
826 /// Add a global to a list to be added to the llvm.compiler.used metadata.
827 void addCompilerUsedGlobal(llvm::GlobalValue *GV);
Daniel Dunbar02698712009-02-13 20:29:50 +0000828
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700829 /// Add a destructor and object to add to the C++ global destructor function.
Chris Lattner810112e2010-06-19 05:52:45 +0000830 void AddCXXDtorEntry(llvm::Constant *DtorFn, llvm::Constant *Object) {
831 CXXGlobalDtors.push_back(std::make_pair(DtorFn, Object));
832 }
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000833
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700834 /// Create a new runtime function with the specified type and name.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000835 llvm::Constant *CreateRuntimeFunction(llvm::FunctionType *Ty,
Chris Lattner686775d2011-07-20 06:58:45 +0000836 StringRef Name,
Bill Wendlingc4c62fd2013-01-31 00:30:05 +0000837 llvm::AttributeSet ExtraAttrs =
838 llvm::AttributeSet());
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700839 /// Create a new runtime global variable with the specified type and name.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000840 llvm::Constant *CreateRuntimeVariable(llvm::Type *Ty,
Chris Lattner686775d2011-07-20 06:58:45 +0000841 StringRef Name);
Daniel Dunbarf1968f22008-10-01 00:49:24 +0000842
Daniel Dunbar673431a2010-07-16 00:00:15 +0000843 ///@name Custom Blocks Runtime Interfaces
844 ///@{
845
846 llvm::Constant *getNSConcreteGlobalBlock();
847 llvm::Constant *getNSConcreteStackBlock();
848 llvm::Constant *getBlockObjectAssign();
849 llvm::Constant *getBlockObjectDispose();
850
851 ///@}
852
Nadav Rotem495cfa42013-03-23 06:43:35 +0000853 llvm::Constant *getLLVMLifetimeStartFn();
854 llvm::Constant *getLLVMLifetimeEndFn();
855
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700856 // Make sure that this type is translated.
Devang Patele80d5672011-03-23 16:29:39 +0000857 void UpdateCompletedType(const TagDecl *TD);
Daniel Dunbard60f2fb2009-02-17 18:43:32 +0000858
John McCall5808ce42011-02-03 08:15:49 +0000859 llvm::Constant *getMemberPointerConstant(const UnaryOperator *e);
860
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700861 /// Try to emit the initializer for the given declaration as a constant;
862 /// returns 0 if the expression cannot be emitted as a constant.
863 llvm::Constant *EmitConstantInit(const VarDecl &D,
864 CodeGenFunction *CGF = nullptr);
Richard Smith2d6a5672012-01-14 04:30:29 +0000865
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700866 /// Try to emit the given expression as a constant; returns 0 if the
867 /// expression cannot be emitted as a constant.
Anders Carlssone9352cc2009-04-08 04:48:15 +0000868 llvm::Constant *EmitConstantExpr(const Expr *E, QualType DestType,
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700869 CodeGenFunction *CGF = nullptr);
Daniel Dunbard60f2fb2009-02-17 18:43:32 +0000870
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700871 /// Emit the given constant value as a constant, in the type's scalar
872 /// representation.
Richard Smith2d6a5672012-01-14 04:30:29 +0000873 llvm::Constant *EmitConstantValue(const APValue &Value, QualType DestType,
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700874 CodeGenFunction *CGF = nullptr);
Richard Smith2d6a5672012-01-14 04:30:29 +0000875
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700876 /// Emit the given constant value as a constant, in the type's memory
877 /// representation.
Richard Smitha3ca41f2012-03-02 23:27:11 +0000878 llvm::Constant *EmitConstantValueForMemory(const APValue &Value,
879 QualType DestType,
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700880 CodeGenFunction *CGF = nullptr);
Richard Smitha3ca41f2012-03-02 23:27:11 +0000881
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700882 /// Return the result of value-initializing the given type, i.e. a null
883 /// expression of the given type. This is usually, but not always, an LLVM
884 /// null constant.
Eli Friedman0f593122009-04-13 21:47:26 +0000885 llvm::Constant *EmitNullConstant(QualType T);
886
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700887 /// Return a null constant appropriate for zero-initializing a base class with
888 /// the given type. This is usually, but not always, an LLVM null constant.
Eli Friedman2ed7cb62011-10-14 02:27:24 +0000889 llvm::Constant *EmitNullConstantForBase(const CXXRecordDecl *Record);
890
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700891 /// Emit a general error that something can't be done.
Chandler Carruth0f30a122012-03-30 19:44:53 +0000892 void Error(SourceLocation loc, StringRef error);
John McCall32096692011-03-18 02:56:14 +0000893
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700894 /// Print out an error that codegen doesn't support the specified stmt yet.
David Blaikie0a1c8622013-08-19 21:02:26 +0000895 void ErrorUnsupported(const Stmt *S, const char *Type);
Mike Stumpecc90e92009-02-13 19:12:34 +0000896
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700897 /// Print out an error that codegen doesn't support the specified decl yet.
David Blaikie0a1c8622013-08-19 21:02:26 +0000898 void ErrorUnsupported(const Decl *D, const char *Type);
Dan Gohman4f8d1232008-05-22 00:50:06 +0000899
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700900 /// Set the attributes on the LLVM function for the given decl and function
901 /// info. This applies attributes necessary for handling the ABI as well as
902 /// user specified attributes like section.
Daniel Dunbar0e4f40e2009-04-17 00:48:04 +0000903 void SetInternalFunctionAttributes(const Decl *D, llvm::Function *F,
904 const CGFunctionInfo &FI);
Daniel Dunbarf80519b2008-09-04 23:41:35 +0000905
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700906 /// Set the LLVM function attributes (sext, zext, etc).
Daniel Dunbar7dbd8192009-04-14 07:08:30 +0000907 void SetLLVMFunctionAttributes(const Decl *D,
908 const CGFunctionInfo &Info,
909 llvm::Function *F);
Daniel Dunbarb7688072008-09-10 00:41:16 +0000910
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700911 /// Set the LLVM function attributes which only apply to a function
912 /// definintion.
Daniel Dunbar7c65e992009-04-14 08:05:55 +0000913 void SetLLVMFunctionAttributesForDefinition(const Decl *D, llvm::Function *F);
914
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700915 /// Return true iff the given type uses 'sret' when used as a return type.
Daniel Dunbardacf9dd2010-07-14 23:39:36 +0000916 bool ReturnTypeUsesSRet(const CGFunctionInfo &FI);
917
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700918 /// Return true iff the given type uses an argument slot when 'sret' is used
919 /// as a return type.
Stephen Hines651f13c2014-04-23 16:59:28 -0700920 bool ReturnSlotInterferesWithArgs(const CGFunctionInfo &FI);
921
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700922 /// Return true iff the given type uses 'fpret' when used as a return type.
Daniel Dunbardacf9dd2010-07-14 23:39:36 +0000923 bool ReturnTypeUsesFPRet(QualType ResultType);
Daniel Dunbarb7688072008-09-10 00:41:16 +0000924
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700925 /// Return true iff the given type uses 'fp2ret' when used as a return type.
Anders Carlssoneea64802011-10-31 16:27:11 +0000926 bool ReturnTypeUsesFP2Ret(QualType ResultType);
927
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700928 /// Get the LLVM attributes and calling convention to use for a particular
929 /// function type.
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000930 ///
931 /// \param Info - The function type information.
932 /// \param TargetDecl - The decl these attributes are being constructed
933 /// for. If supplied the attributes applied to this decl may contribute to the
934 /// function attributes and calling convention.
935 /// \param PAL [out] - On return, the attribute list to use.
936 /// \param CallingConv [out] - On return, the LLVM calling convention to use.
Daniel Dunbar88b53962009-02-02 22:03:45 +0000937 void ConstructAttributeList(const CGFunctionInfo &Info,
938 const Decl *TargetDecl,
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000939 AttributeListType &PAL,
Bill Wendling94236e72013-02-22 00:13:35 +0000940 unsigned &CallingConv,
941 bool AttrOnCallSite);
Daniel Dunbarb7688072008-09-10 00:41:16 +0000942
Chris Lattner686775d2011-07-20 06:58:45 +0000943 StringRef getMangledName(GlobalDecl GD);
Stephen Hinesef822542014-07-21 00:47:37 -0700944 StringRef getBlockMangledName(GlobalDecl GD, const BlockDecl *BD);
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000945
946 void EmitTentativeDefinition(const VarDecl *D);
Mike Stumpf1216772009-07-31 18:25:34 +0000947
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000948 void EmitVTable(CXXRecordDecl *Class, bool DefinitionRequired);
949
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700950 /// Emit the RTTI descriptors for the builtin types.
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +0000951 void EmitFundamentalRTTIDescriptors();
952
Reid Kleckner3190ca92013-05-08 13:44:39 +0000953 /// \brief Appends Opts to the "Linker Options" metadata value.
954 void AppendLinkerOptions(StringRef Opts);
955
Benjamin Kramer785f1e72013-06-04 09:13:21 +0000956 /// \brief Appends a detect mismatch command to the linker options.
Aaron Ballmana7ff62f2013-06-04 02:07:14 +0000957 void AddDetectMismatch(StringRef Name, StringRef Value);
958
Reid Kleckner3190ca92013-05-08 13:44:39 +0000959 /// \brief Appends a dependent lib to the "Linker Options" metadata value.
960 void AddDependentLib(StringRef Lib);
961
Peter Collingbourne144a31f2013-06-05 17:49:37 +0000962 llvm::GlobalVariable::LinkageTypes getFunctionLinkage(GlobalDecl GD);
John McCalld46f9852010-02-19 01:32:20 +0000963
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700964 void setFunctionLinkage(GlobalDecl GD, llvm::Function *F) {
965 F->setLinkage(getFunctionLinkage(GD));
John McCall8b242332010-05-25 04:30:21 +0000966 }
967
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700968 /// Return the appropriate linkage for the vtable, VTT, and type information
969 /// of the given class.
Anders Carlsson3a717f72011-01-24 02:04:33 +0000970 llvm::GlobalVariable::LinkageTypes getVTableLinkage(const CXXRecordDecl *RD);
Ken Dyck687cc4a2010-01-26 13:48:07 +0000971
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700972 /// Return the store size, in character units, of the given LLVM type.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000973 CharUnits GetTargetTypeStoreSize(llvm::Type *Ty) const;
Fariborz Jahanian354e7122010-10-27 16:21:54 +0000974
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700975 /// Returns LLVM linkage for a declarator.
976 llvm::GlobalValue::LinkageTypes
977 getLLVMLinkageForDeclarator(const DeclaratorDecl *D, GVALinkage Linkage,
978 bool IsConstantVariable);
979
980 /// Returns LLVM linkage for a declarator.
981 llvm::GlobalValue::LinkageTypes
982 getLLVMLinkageVarDefinition(const VarDecl *VD, bool IsConstant);
983
Julien Lerouge77f68bb2011-09-09 22:41:49 +0000984 /// Emit all the global annotations.
985 void EmitGlobalAnnotations();
986
987 /// Emit an annotation string.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000988 llvm::Constant *EmitAnnotationString(StringRef Str);
Julien Lerouge77f68bb2011-09-09 22:41:49 +0000989
990 /// Emit the annotation's translation unit.
991 llvm::Constant *EmitAnnotationUnit(SourceLocation Loc);
992
993 /// Emit the annotation line number.
994 llvm::Constant *EmitAnnotationLineNo(SourceLocation L);
995
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700996 /// Generate the llvm::ConstantStruct which contains the annotation
997 /// information for a given GlobalValue. The annotation struct is
Julien Lerouge77f68bb2011-09-09 22:41:49 +0000998 /// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the
999 /// GlobalValue being annotated. The second field is the constant string
1000 /// created from the AnnotateAttr's annotation. The third field is a constant
1001 /// string containing the name of the translation unit. The fourth field is
1002 /// the line number in the file of the annotated value declaration.
1003 llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV,
1004 const AnnotateAttr *AA,
1005 SourceLocation L);
1006
1007 /// Add global annotations that are set on D, for the global GV. Those
1008 /// annotations are emitted during finalization of the LLVM code.
1009 void AddGlobalAnnotations(const ValueDecl *D, llvm::GlobalValue *GV);
1010
Stephen Hinesef822542014-07-21 00:47:37 -07001011 const SanitizerBlacklist &getSanitizerBlacklist() const {
1012 return SanitizerBL;
Will Dietz4f45bc02013-01-18 11:30:38 +00001013 }
1014
Stephen Hinesef822542014-07-21 00:47:37 -07001015 void reportGlobalToASan(llvm::GlobalVariable *GV, SourceLocation Loc,
1016 bool IsDynInit = false);
Will Dietz4f45bc02013-01-18 11:30:38 +00001017
John McCalld5617ee2013-01-25 22:31:03 +00001018 void addDeferredVTable(const CXXRecordDecl *RD) {
1019 DeferredVTables.push_back(RD);
1020 }
1021
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001022 /// Emit code for a singal global function or var decl. Forward declarations
1023 /// are emitted lazily.
Reid Klecknera4130ba2013-07-22 13:51:44 +00001024 void EmitGlobal(GlobalDecl D);
1025
Chris Lattner9cd4fe42007-12-02 07:09:19 +00001026private:
Chris Lattner686775d2011-07-20 06:58:45 +00001027 llvm::GlobalValue *GetGlobalValue(StringRef Ref);
Mike Stump1eb44332009-09-09 15:08:12 +00001028
Stephen Hines651f13c2014-04-23 16:59:28 -07001029 llvm::Constant *
1030 GetOrCreateLLVMFunction(StringRef MangledName, llvm::Type *Ty, GlobalDecl D,
1031 bool ForVTable, bool DontDefer = false,
1032 llvm::AttributeSet ExtraAttrs = llvm::AttributeSet());
1033
Chris Lattner686775d2011-07-20 06:58:45 +00001034 llvm::Constant *GetOrCreateLLVMGlobal(StringRef MangledName,
Chris Lattner2acc6e32011-07-18 04:24:23 +00001035 llvm::PointerType *PTy,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001036 const VarDecl *D);
Mike Stump1eb44332009-09-09 15:08:12 +00001037
Stephen Hinesef822542014-07-21 00:47:37 -07001038 llvm::StringMapEntry<llvm::GlobalVariable *> *
1039 getConstantStringMapEntry(StringRef Str, int CharByteWidth);
1040
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001041 /// Set attributes which are common to any form of a global definition (alias,
1042 /// Objective-C method, function, global variable).
Daniel Dunbar7dbd8192009-04-14 07:08:30 +00001043 ///
Daniel Dunbar7c65e992009-04-14 08:05:55 +00001044 /// NOTE: This should only be called for definitions.
1045 void SetCommonAttributes(const Decl *D, llvm::GlobalValue *GV);
Daniel Dunbarf80519b2008-09-04 23:41:35 +00001046
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001047 void setNonAliasAttributes(const Decl *D, llvm::GlobalObject *GO);
Mike Stump1eb44332009-09-09 15:08:12 +00001048
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001049 /// Set attributes for a global definition.
1050 void setFunctionDefinitionAttributes(const FunctionDecl *D,
1051 llvm::Function *F);
1052
1053 /// Set function attributes for a function declaration.
Anders Carlssonb2bcf1c2010-02-06 02:44:09 +00001054 void SetFunctionAttributes(GlobalDecl GD,
Eli Friedmanc6c14d12009-05-26 01:22:57 +00001055 llvm::Function *F,
1056 bool IsIncompleteFunction);
Nuno Lopesd4cbda62008-06-08 15:45:52 +00001057
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001058 void EmitGlobalDefinition(GlobalDecl D, llvm::GlobalValue *GV = nullptr);
Daniel Dunbard5d31802009-02-19 07:15:39 +00001059
Stephen Hines651f13c2014-04-23 16:59:28 -07001060 void EmitGlobalFunctionDefinition(GlobalDecl GD, llvm::GlobalValue *GV);
Daniel Dunbarbd012ff2008-07-29 23:18:29 +00001061 void EmitGlobalVarDefinition(const VarDecl *D);
John McCallf746aa62010-03-19 23:29:14 +00001062 void EmitAliasDefinition(GlobalDecl GD);
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001063 void EmitObjCPropertyImplementations(const ObjCImplementationDecl *D);
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00001064 void EmitObjCIvarInitializations(ObjCImplementationDecl *D);
Fariborz Jahanian354e7122010-10-27 16:21:54 +00001065
Anders Carlsson95d4e5d2009-04-15 15:55:24 +00001066 // C++ related functions.
Mike Stump1eb44332009-09-09 15:08:12 +00001067
Rafael Espindola71fdc122013-11-04 18:38:59 +00001068 bool TryEmitDefinitionAsAlias(GlobalDecl Alias, GlobalDecl Target,
1069 bool InEveryTU);
John McCallc0bf4622010-02-23 00:48:20 +00001070 bool TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D);
John McCalld46f9852010-02-19 01:32:20 +00001071
Anders Carlsson984e0682009-04-01 00:58:25 +00001072 void EmitNamespace(const NamespaceDecl *D);
Anders Carlsson91e20dd2009-04-02 05:55:18 +00001073 void EmitLinkageSpec(const LinkageSpecDecl *D);
Adrian Prantl0a050f72013-05-09 23:16:27 +00001074 void CompleteDIClassType(const CXXMethodDecl* D);
Anders Carlsson95d4e5d2009-04-15 15:55:24 +00001075
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001076 /// Emit a single constructor with the given type from a C++ constructor Decl.
Anders Carlsson95d4e5d2009-04-15 15:55:24 +00001077 void EmitCXXConstructor(const CXXConstructorDecl *D, CXXCtorType Type);
Mike Stump1eb44332009-09-09 15:08:12 +00001078
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001079 /// Emit a single destructor with the given type from a C++ destructor Decl.
Anders Carlsson27ae5362009-04-17 01:58:57 +00001080 void EmitCXXDestructor(const CXXDestructorDecl *D, CXXDtorType Type);
Mike Stump1eb44332009-09-09 15:08:12 +00001081
Richard Smithb80a16e2013-04-19 16:42:07 +00001082 /// \brief Emit the function that initializes C++ thread_local variables.
1083 void EmitCXXThreadLocalInitFunc();
1084
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001085 /// Emit the function that initializes C++ globals.
Anders Carlsson89ed31d2009-08-08 23:24:23 +00001086 void EmitCXXGlobalInitFunc();
Eli Friedman6c6bda32010-01-08 00:50:11 +00001087
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001088 /// Emit the function that destroys C++ globals.
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00001089 void EmitCXXGlobalDtorFunc();
1090
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001091 /// Emit the function that initializes the specified global (if PerformInit is
1092 /// true) and registers its destructor.
John McCall3030eb82010-11-06 09:44:32 +00001093 void EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
Richard Smith7ca48502012-02-13 22:16:19 +00001094 llvm::GlobalVariable *Addr,
1095 bool PerformInit);
Eli Friedman6c6bda32010-01-08 00:50:11 +00001096
Daniel Dunbar6bfed7e2008-08-01 00:01:51 +00001097 // FIXME: Hardcoding priority here is gross.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001098 void AddGlobalCtor(llvm::Function *Ctor, int Priority = 65535,
1099 llvm::Constant *AssociatedData = 0);
1100 void AddGlobalDtor(llvm::Function *Dtor, int Priority = 65535);
Daniel Dunbarbd012ff2008-07-29 23:18:29 +00001101
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001102 /// Generates a global array of functions and priorities using the given list
1103 /// and name. This array will have appending linkage and is suitable for use
1104 /// as a LLVM constructor or destructor array.
Daniel Dunbar6bfed7e2008-08-01 00:01:51 +00001105 void EmitCtorList(const CtorList &Fns, const char *GlobalName);
1106
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001107 /// Emit the RTTI descriptors for the given type.
Rafael Espindolad1a5c312010-03-27 02:52:14 +00001108 void EmitFundamentalRTTIDescriptor(QualType Type);
1109
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001110 /// Emit any needed decls for which code generation was deferred.
Dmitri Gribenkoc4a77902012-11-15 14:28:07 +00001111 void EmitDeferred();
Daniel Dunbar02698712009-02-13 20:29:50 +00001112
Rafael Espindola61a0a752013-11-05 21:37:29 +00001113 /// Call replaceAllUsesWith on all pairs in Replacements.
1114 void applyReplacements();
1115
Rafael Espindolad2054982013-10-22 19:26:13 +00001116 void checkAliases();
1117
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001118 /// Emit any vtables which we deferred and still have a use for.
John McCalld5617ee2013-01-25 22:31:03 +00001119 void EmitDeferredVTables();
1120
Stephen Hines651f13c2014-04-23 16:59:28 -07001121 /// Emit the llvm.used and llvm.compiler.used metadata.
1122 void emitLLVMUsed();
Daniel Dunbarf1968f22008-10-01 00:49:24 +00001123
Douglas Gregor858afb32013-01-14 20:53:57 +00001124 /// \brief Emit the link options introduced by imported modules.
1125 void EmitModuleLinkOptions();
1126
Richard Smith00249372013-04-06 05:00:46 +00001127 /// \brief Emit aliases for internal-linkage declarations inside "C" language
1128 /// linkage specifications, giving them the "expected" name where possible.
1129 void EmitStaticExternCAliases();
1130
John McCall744016d2010-07-06 23:57:41 +00001131 void EmitDeclMetadata();
1132
Rafael Espindola9686f122013-10-16 19:28:50 +00001133 /// \brief Emit the Clang version as llvm.ident metadata.
1134 void EmitVersionIdentMetadata();
1135
Stephen Hinesef822542014-07-21 00:47:37 -07001136 /// Emits target specific Metadata for global declarations.
1137 void EmitTargetMetadata();
1138
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001139 /// Emit the llvm.gcov metadata used to tell LLVM where to emit the .gcno and
1140 /// .gcda files in a way that persists in .bc files.
Nick Lewycky3dc05412011-05-05 00:08:20 +00001141 void EmitCoverageFile();
Nick Lewycky5ea4f442011-05-04 20:46:58 +00001142
Nico Weberc5f80462012-10-11 10:13:44 +00001143 /// Emits the initializer for a uuidof string.
1144 llvm::Constant *EmitUuidofInitializer(StringRef uuidstr, QualType IIDType);
1145
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001146 /// Determine if the given decl can be emitted lazily; this is only relevant
1147 /// for definitions. The given decl must be either a function or var decl.
Daniel Dunbar73241df2009-02-13 21:18:01 +00001148 bool MayDeferGeneration(const ValueDecl *D);
John McCallb2593832010-09-16 06:16:50 +00001149
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001150 /// Check whether we can use a "simpler", more core exceptions personality
1151 /// function.
John McCallb2593832010-09-16 06:16:50 +00001152 void SimplifyPersonality();
Reid Spencer5f016e22007-07-11 17:01:13 +00001153};
1154} // end namespace CodeGen
1155} // end namespace clang
1156
1157#endif