blob: 8334f7ce8bcca12609981f10c35d33e86d2a4650 [file] [log] [blame]
Daniel Dunbar9c426522008-07-29 23:18:29 +00001//===--- CodeGenModule.h - Per-Module state for LLVM CodeGen ----*- C++ -*-===//
Chris Lattnerf97fe382007-05-24 06:29:05 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerf97fe382007-05-24 06:29:05 +00007//
8//===----------------------------------------------------------------------===//
9//
Mike Stump1676c5b2009-02-13 19:12:34 +000010// This is the internal per-translation-unit state used for llvm translation.
Chris Lattnerf97fe382007-05-24 06:29:05 +000011//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000014#ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H
15#define LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H
Chris Lattnerf97fe382007-05-24 06:29:05 +000016
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "CGVTables.h"
18#include "CodeGenTypes.h"
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000019#include "SanitizerBlacklist.h"
Alexey Samsonov4b8de112014-08-01 21:35:28 +000020#include "SanitizerMetadata.h"
Dan Gohman75d69da2008-05-22 00:50:06 +000021#include "clang/AST/Attr.h"
Anders Carlssondae1abc2009-05-05 04:44:02 +000022#include "clang/AST/DeclCXX.h"
Anders Carlsson73fcc952009-09-11 00:07:24 +000023#include "clang/AST/DeclObjC.h"
Peter Collingbourneee781d52011-06-14 04:02:39 +000024#include "clang/AST/GlobalDecl.h"
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000025#include "clang/AST/Mangle.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000026#include "clang/Basic/ABI.h"
27#include "clang/Basic/LangOptions.h"
Douglas Gregor6ddfca92013-01-14 17:21:00 +000028#include "clang/Basic/Module.h"
Chris Lattnerb6984c42007-06-20 04:44:43 +000029#include "llvm/ADT/DenseMap.h"
Douglas Gregor6ddfca92013-01-14 17:21:00 +000030#include "llvm/ADT/SetVector.h"
Rafael Espindola2e42fec2010-03-04 18:17:24 +000031#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000032#include "llvm/ADT/StringMap.h"
John McCall882987f2013-02-28 19:01:20 +000033#include "llvm/IR/CallingConv.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000034#include "llvm/IR/Module.h"
Chandler Carruth61743af2014-03-04 11:18:19 +000035#include "llvm/IR/ValueHandle.h"
Anders Carlsson762e1622009-01-04 02:08:04 +000036
Chris Lattnerf97fe382007-05-24 06:29:05 +000037namespace llvm {
Rafael Espindola42ae7452014-05-09 00:57:59 +000038class Module;
39class Constant;
40class ConstantInt;
41class Function;
42class GlobalValue;
43class DataLayout;
44class FunctionType;
45class LLVMContext;
46class IndexedInstrProfReader;
Chris Lattner23b7eb62007-06-15 23:05:46 +000047}
48
Chris Lattnerf97fe382007-05-24 06:29:05 +000049namespace clang {
Rafael Espindola42ae7452014-05-09 00:57:59 +000050class TargetCodeGenInfo;
51class ASTContext;
52class AtomicType;
53class FunctionDecl;
54class IdentifierInfo;
55class ObjCMethodDecl;
56class ObjCImplementationDecl;
57class ObjCCategoryImplDecl;
58class ObjCProtocolDecl;
59class ObjCEncodeExpr;
60class BlockExpr;
61class CharUnits;
62class Decl;
63class Expr;
64class Stmt;
65class InitListExpr;
66class StringLiteral;
67class NamedDecl;
68class ValueDecl;
69class VarDecl;
70class LangOptions;
71class CodeGenOptions;
72class DiagnosticsEngine;
73class AnnotateAttr;
74class CXXDestructorDecl;
Rafael Espindola42ae7452014-05-09 00:57:59 +000075class Module;
Alex Lorenzee024992014-08-04 18:41:51 +000076class CoverageSourceInfo;
Mike Stump1676c5b2009-02-13 19:12:34 +000077
Chris Lattnerf97fe382007-05-24 06:29:05 +000078namespace CodeGen {
79
Rafael Espindola42ae7452014-05-09 00:57:59 +000080class CallArgList;
81class CodeGenFunction;
82class CodeGenTBAA;
83class CGCXXABI;
84class CGDebugInfo;
85class CGObjCRuntime;
86class CGOpenCLRuntime;
87class CGOpenMPRuntime;
88class CGCUDARuntime;
89class BlockFieldFlags;
90class FunctionArgList;
Alex Lorenzee024992014-08-04 18:41:51 +000091class CoverageMappingModuleGen;
Justin Bogneref512b92014-01-06 22:27:43 +000092
Rafael Espindola42ae7452014-05-09 00:57:59 +000093struct OrderGlobalInits {
94 unsigned int priority;
95 unsigned int lex_order;
96 OrderGlobalInits(unsigned int p, unsigned int l)
Chris Lattnere0009072010-06-27 06:32:58 +000097 : priority(p), lex_order(l) {}
Rafael Espindola42ae7452014-05-09 00:57:59 +000098
99 bool operator==(const OrderGlobalInits &RHS) const {
100 return priority == RHS.priority && lex_order == RHS.lex_order;
101 }
102
103 bool operator<(const OrderGlobalInits &RHS) const {
104 return std::tie(priority, lex_order) <
105 std::tie(RHS.priority, RHS.lex_order);
106 }
107};
108
109struct CodeGenTypeCache {
110 /// void
111 llvm::Type *VoidTy;
112
113 /// i8, i16, i32, and i64
114 llvm::IntegerType *Int8Ty, *Int16Ty, *Int32Ty, *Int64Ty;
115 /// float, double
116 llvm::Type *FloatTy, *DoubleTy;
117
118 /// int
119 llvm::IntegerType *IntTy;
120
121 /// intptr_t, size_t, and ptrdiff_t, which we assume are the same size.
122 union {
123 llvm::IntegerType *IntPtrTy;
124 llvm::IntegerType *SizeTy;
125 llvm::IntegerType *PtrDiffTy;
Chris Lattnere0009072010-06-27 06:32:58 +0000126 };
John McCalle3dc1702011-02-15 09:22:45 +0000127
Rafael Espindola42ae7452014-05-09 00:57:59 +0000128 /// void* in address space 0
129 union {
130 llvm::PointerType *VoidPtrTy;
131 llvm::PointerType *Int8PtrTy;
John McCalle3dc1702011-02-15 09:22:45 +0000132 };
John McCall31168b02011-06-15 23:02:42 +0000133
Rafael Espindola42ae7452014-05-09 00:57:59 +0000134 /// void** in address space 0
135 union {
136 llvm::PointerType *VoidPtrPtrTy;
137 llvm::PointerType *Int8PtrPtrTy;
138 };
139
140 /// The width of a pointer into the generic address space.
141 unsigned char PointerWidthInBits;
142
143 /// The size and alignment of a pointer into the generic address
144 /// space.
145 union {
146 unsigned char PointerAlignInBytes;
147 unsigned char PointerSizeInBytes;
148 unsigned char SizeSizeInBytes; // sizeof(size_t)
149 };
150
151 llvm::CallingConv::ID RuntimeCC;
152 llvm::CallingConv::ID getRuntimeCC() const { return RuntimeCC; }
153};
154
John McCall31168b02011-06-15 23:02:42 +0000155struct RREntrypoints {
156 RREntrypoints() { memset(this, 0, sizeof(*this)); }
157 /// void objc_autoreleasePoolPop(void*);
158 llvm::Constant *objc_autoreleasePoolPop;
159
160 /// void *objc_autoreleasePoolPush(void);
161 llvm::Constant *objc_autoreleasePoolPush;
162};
163
164struct ARCEntrypoints {
165 ARCEntrypoints() { memset(this, 0, sizeof(*this)); }
166
167 /// id objc_autorelease(id);
168 llvm::Constant *objc_autorelease;
169
170 /// id objc_autoreleaseReturnValue(id);
171 llvm::Constant *objc_autoreleaseReturnValue;
172
173 /// void objc_copyWeak(id *dest, id *src);
174 llvm::Constant *objc_copyWeak;
175
176 /// void objc_destroyWeak(id*);
177 llvm::Constant *objc_destroyWeak;
178
179 /// id objc_initWeak(id*, id);
180 llvm::Constant *objc_initWeak;
181
182 /// id objc_loadWeak(id*);
183 llvm::Constant *objc_loadWeak;
184
185 /// id objc_loadWeakRetained(id*);
186 llvm::Constant *objc_loadWeakRetained;
187
188 /// void objc_moveWeak(id *dest, id *src);
189 llvm::Constant *objc_moveWeak;
190
191 /// id objc_retain(id);
192 llvm::Constant *objc_retain;
193
194 /// id objc_retainAutorelease(id);
195 llvm::Constant *objc_retainAutorelease;
196
197 /// id objc_retainAutoreleaseReturnValue(id);
198 llvm::Constant *objc_retainAutoreleaseReturnValue;
199
200 /// id objc_retainAutoreleasedReturnValue(id);
201 llvm::Constant *objc_retainAutoreleasedReturnValue;
202
203 /// id objc_retainBlock(id);
204 llvm::Constant *objc_retainBlock;
205
206 /// void objc_release(id);
207 llvm::Constant *objc_release;
208
209 /// id objc_storeStrong(id*, id);
210 llvm::Constant *objc_storeStrong;
211
212 /// id objc_storeWeak(id*, id);
213 llvm::Constant *objc_storeWeak;
214
215 /// A void(void) inline asm to use to mark that the return value of
216 /// a call will be immediately retain.
217 llvm::InlineAsm *retainAutoreleasedReturnValueMarker;
John McCalleff18842013-03-23 02:35:54 +0000218
219 /// void clang.arc.use(...);
220 llvm::Constant *clang_arc_use;
John McCall31168b02011-06-15 23:02:42 +0000221};
Will Dietzf54319c2013-01-18 11:30:38 +0000222
Justin Bognere2ef2a02014-04-15 21:22:35 +0000223/// This class records statistics on instrumentation based profiling.
Justin Bogner40b8ba12014-06-26 01:45:07 +0000224class InstrProfStats {
225 uint32_t VisitedInMainFile;
226 uint32_t MissingInMainFile;
Justin Bognere2ef2a02014-04-15 21:22:35 +0000227 uint32_t Visited;
228 uint32_t Missing;
229 uint32_t Mismatched;
Justin Bogner40b8ba12014-06-26 01:45:07 +0000230
231public:
232 InstrProfStats()
233 : VisitedInMainFile(0), MissingInMainFile(0), Visited(0), Missing(0),
234 Mismatched(0) {}
235 /// Record that we've visited a function and whether or not that function was
236 /// in the main source file.
237 void addVisited(bool MainFile) {
238 if (MainFile)
239 ++VisitedInMainFile;
240 ++Visited;
241 }
242 /// Record that a function we've visited has no profile data.
243 void addMissing(bool MainFile) {
244 if (MainFile)
245 ++MissingInMainFile;
246 ++Missing;
247 }
248 /// Record that a function we've visited has mismatched profile data.
249 void addMismatched(bool MainFile) { ++Mismatched; }
250 /// Whether or not the stats we've gathered indicate any potential problems.
251 bool hasDiagnostics() { return Missing || Mismatched; }
252 /// Report potential problems we've found to \c Diags.
253 void reportDiagnostics(DiagnosticsEngine &Diags, StringRef MainFile);
Justin Bognere2ef2a02014-04-15 21:22:35 +0000254};
255
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000256/// This class organizes the cross-function state that is used while generating
257/// LLVM code.
John McCalle3dc1702011-02-15 09:22:45 +0000258class CodeGenModule : public CodeGenTypeCache {
Dmitri Gribenkoa664e5b2012-09-15 20:20:27 +0000259 CodeGenModule(const CodeGenModule &) LLVM_DELETED_FUNCTION;
260 void operator=(const CodeGenModule &) LLVM_DELETED_FUNCTION;
Anders Carlsson729a8202009-02-24 04:21:31 +0000261
Reid Kleckner563f0e82014-05-23 21:13:45 +0000262 struct Structor {
263 Structor() : Priority(0), Initializer(nullptr), AssociatedData(nullptr) {}
264 Structor(int Priority, llvm::Constant *Initializer,
265 llvm::Constant *AssociatedData)
266 : Priority(Priority), Initializer(Initializer),
267 AssociatedData(AssociatedData) {}
268 int Priority;
269 llvm::Constant *Initializer;
270 llvm::Constant *AssociatedData;
271 };
272
273 typedef std::vector<Structor> CtorList;
Daniel Dunbar74aa7e12008-08-01 00:01:51 +0000274
Chris Lattnerf97fe382007-05-24 06:29:05 +0000275 ASTContext &Context;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000276 const LangOptions &LangOpts;
Chandler Carruthbc55fe22009-11-12 17:24:48 +0000277 const CodeGenOptions &CodeGenOpts;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000278 llvm::Module &TheModule;
David Blaikie9c902b52011-09-25 23:23:43 +0000279 DiagnosticsEngine &Diags;
John McCallc8e01702013-04-16 22:48:15 +0000280 const llvm::DataLayout &TheDataLayout;
281 const TargetInfo &Target;
Ahmed Charlesb8984322014-03-07 20:03:18 +0000282 std::unique_ptr<CGCXXABI> ABI;
John McCallc8e01702013-04-16 22:48:15 +0000283 llvm::LLVMContext &VMContext;
Anders Carlssonff971e82009-10-07 01:06:45 +0000284
John McCallc8e01702013-04-16 22:48:15 +0000285 CodeGenTBAA *TBAA;
286
287 mutable const TargetCodeGenInfo *TheTargetCodeGenInfo;
288
289 // This should not be moved earlier, since its initialization depends on some
290 // of the previous reference members being already initialized and also checks
291 // if TheTargetCodeGenInfo is NULL
292 CodeGenTypes Types;
293
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000294 /// Holds information about C++ vtables.
Anders Carlssona864caf2010-03-23 04:11:45 +0000295 CodeGenVTables VTables;
Douglas Gregorbc6a4342010-04-08 16:07:47 +0000296
Peter Collingbournee1d20992011-07-27 20:29:46 +0000297 CGObjCRuntime* ObjCRuntime;
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000298 CGOpenCLRuntime* OpenCLRuntime;
Alexey Bataev9959db52014-05-06 10:08:46 +0000299 CGOpenMPRuntime* OpenMPRuntime;
Peter Collingbournefe883422011-10-06 18:29:37 +0000300 CGCUDARuntime* CUDARuntime;
Ted Kremenek2c674f62008-08-05 18:50:11 +0000301 CGDebugInfo* DebugInfo;
John McCall31168b02011-06-15 23:02:42 +0000302 ARCEntrypoints *ARCData;
Dan Gohman515a60d2012-02-16 00:57:37 +0000303 llvm::MDNode *NoObjCARCExceptionsMetadata;
John McCall31168b02011-06-15 23:02:42 +0000304 RREntrypoints *RRData;
Justin Bogner837a6f62014-04-18 21:52:00 +0000305 std::unique_ptr<llvm::IndexedInstrProfReader> PGOReader;
Justin Bognere2ef2a02014-04-15 21:22:35 +0000306 InstrProfStats PGOStats;
Daniel Dunbare49df9b52008-07-30 16:32:24 +0000307
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000308 // A set of references that have only been seen via a weakref so far. This is
309 // used to remove the weak of the reference if we ever see a direct reference
310 // or a definition.
Rafael Espindola2e42fec2010-03-04 18:17:24 +0000311 llvm::SmallPtrSet<llvm::GlobalValue*, 10> WeakRefReferences;
312
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000313 /// This contains all the decls which have definitions but/ which are deferred
314 /// for emission and therefore should only be output if they are actually
315 /// used. If a decl is in this, then it is known to have not been referenced
316 /// yet.
Alp Tokerfb8d02b2014-06-05 22:10:59 +0000317 std::map<StringRef, GlobalDecl> DeferredDecls;
Daniel Dunbar08b26a02009-02-13 20:29:50 +0000318
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000319 /// This is a list of deferred decls which we have seen that *are* actually
320 /// referenced. These get code generated when the module is done.
Rafael Espindolac0ff7442013-12-09 14:59:08 +0000321 struct DeferredGlobal {
322 DeferredGlobal(llvm::GlobalValue *GV, GlobalDecl GD) : GV(GV), GD(GD) {}
323 llvm::AssertingVH<llvm::GlobalValue> GV;
324 GlobalDecl GD;
325 };
326 std::vector<DeferredGlobal> DeferredDeclsToEmit;
327 void addDeferredDeclToEmit(llvm::GlobalValue *GV, GlobalDecl GD) {
328 DeferredDeclsToEmit.push_back(DeferredGlobal(GV, GD));
329 }
Daniel Dunbar7dd749e2009-04-15 22:08:45 +0000330
Rafael Espindola208b5c02013-10-22 19:26:13 +0000331 /// List of alias we have emitted. Used to make sure that what they point to
332 /// is defined once we get to the end of the of the translation unit.
333 std::vector<GlobalDecl> Aliases;
334
Rafael Espindola2e2995b2013-11-05 21:37:29 +0000335 typedef llvm::StringMap<llvm::TrackingVH<llvm::Constant> > ReplacementsTy;
336 ReplacementsTy Replacements;
337
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000338 /// A queue of (optional) vtables to consider emitting.
John McCall6bd2a892013-01-25 22:31:03 +0000339 std::vector<const CXXRecordDecl*> DeferredVTables;
340
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000341 /// List of global values which are required to be present in the object file;
342 /// bitcast to i8*. This is used for forcing visibility of symbols which may
343 /// otherwise be optimized out.
Chris Lattnerf41e87f2009-03-31 22:37:52 +0000344 std::vector<llvm::WeakVH> LLVMUsed;
Rafael Espindola060062a2014-03-06 22:15:10 +0000345 std::vector<llvm::WeakVH> LLVMCompilerUsed;
Mike Stump1676c5b2009-02-13 19:12:34 +0000346
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000347 /// Store the list of global constructors and their respective priorities to
348 /// be emitted when the translation unit is complete.
Daniel Dunbar74aa7e12008-08-01 00:01:51 +0000349 CtorList GlobalCtors;
350
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000351 /// Store the list of global destructors and their respective priorities to be
352 /// emitted when the translation unit is complete.
Daniel Dunbar74aa7e12008-08-01 00:01:51 +0000353 CtorList GlobalDtors;
354
Robert Lytton57765d52014-07-03 09:30:33 +0000355 /// An ordered map of canonical GlobalDecls to their mangled names.
356 llvm::MapVector<GlobalDecl, StringRef> MangledDeclNames;
Alp Tokerfb8d02b2014-06-05 22:10:59 +0000357 llvm::StringMap<GlobalDecl, llvm::BumpPtrAllocator> Manglings;
358
Julien Lerouge5a6b6982011-09-09 22:41:49 +0000359 /// Global annotations.
Nate Begeman7fab5782008-04-18 23:43:57 +0000360 std::vector<llvm::Constant*> Annotations;
Mike Stump1676c5b2009-02-13 19:12:34 +0000361
Julien Lerouge5a6b6982011-09-09 22:41:49 +0000362 /// Map used to get unique annotation strings.
363 llvm::StringMap<llvm::Constant*> AnnotationStrings;
364
Anders Carlssonb04ea612007-08-21 00:21:21 +0000365 llvm::StringMap<llvm::Constant*> CFConstantStringMap;
David Majnemer58e5bee2014-03-24 21:43:36 +0000366
Richard Smith00db2f12014-07-29 21:20:12 +0000367 llvm::DenseMap<llvm::Constant *, llvm::GlobalVariable *> ConstantStringMap;
Eli Friedman1c8d2ca2012-03-09 03:27:46 +0000368 llvm::DenseMap<const Decl*, llvm::Constant *> StaticLocalDeclMap;
John McCallb88a5662012-03-30 21:00:39 +0000369 llvm::DenseMap<const Decl*, llvm::GlobalVariable*> StaticLocalDeclGuardMap;
Richard Smithe6c01442013-06-05 00:46:14 +0000370 llvm::DenseMap<const Expr*, llvm::Constant *> MaterializedGlobalTemporaryMap;
371
Fariborz Jahanian1bed4132012-01-08 19:13:23 +0000372 llvm::DenseMap<QualType, llvm::Constant *> AtomicSetterHelperFnMap;
373 llvm::DenseMap<QualType, llvm::Constant *> AtomicGetterHelperFnMap;
Daniel Dunbard644ad62008-08-23 18:37:06 +0000374
Will Dietz949ec542013-11-08 01:09:22 +0000375 /// Map used to get unique type descriptor constants for sanitizers.
376 llvm::DenseMap<QualType, llvm::Constant *> TypeDescriptorMap;
377
Richard Smithdf931ce2013-04-06 05:00:46 +0000378 /// Map used to track internal linkage functions declared within
379 /// extern "C" regions.
Richard Smithf21c9612013-04-13 01:28:18 +0000380 typedef llvm::MapVector<IdentifierInfo *,
381 llvm::GlobalValue *> StaticExternCMap;
Richard Smithdf931ce2013-04-06 05:00:46 +0000382 StaticExternCMap StaticExternCValues;
383
Richard Smith2fd1d7a2013-04-19 16:42:07 +0000384 /// \brief thread_local variables defined or used in this TU.
385 std::vector<std::pair<const VarDecl *, llvm::GlobalVariable *> >
386 CXXThreadLocals;
387
388 /// \brief thread_local variables with initializers that need to run
389 /// before any thread_local variable in this TU is odr-used.
David Majnemerb3341ea2014-10-05 05:05:40 +0000390 std::vector<llvm::Function *> CXXThreadLocalInits;
391 std::vector<llvm::GlobalVariable *> CXXThreadLocalInitVars;
Richard Smith2fd1d7a2013-04-19 16:42:07 +0000392
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000393 /// Global variables with initializers that need to run before main.
David Majnemerb3341ea2014-10-05 05:05:40 +0000394 std::vector<llvm::Function *> CXXGlobalInits;
John McCall70013b62010-07-15 23:40:35 +0000395
396 /// When a C++ decl with an initializer is deferred, null is
397 /// appended to CXXGlobalInits, and the index of that null is placed
398 /// here so that the initializer will be performed in the correct
399 /// order.
400 llvm::DenseMap<const Decl*, unsigned> DelayedCXXInitPosition;
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +0000401
Anton Korobeynikovabed7492012-11-06 22:44:45 +0000402 typedef std::pair<OrderGlobalInits, llvm::Function*> GlobalInitData;
403
404 struct GlobalInitPriorityCmp {
405 bool operator()(const GlobalInitData &LHS,
406 const GlobalInitData &RHS) const {
407 return LHS.first.priority < RHS.first.priority;
408 }
409 };
410
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000411 /// Global variables with initializers whose order of initialization is set by
412 /// init_priority attribute.
Anton Korobeynikovabed7492012-11-06 22:44:45 +0000413 SmallVector<GlobalInitData, 8> PrioritizedCXXGlobalInits;
Mike Stump11289f42009-09-09 15:08:12 +0000414
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000415 /// Global destructor functions and arguments that need to run on termination.
Chris Lattner87233f72010-06-19 05:52:45 +0000416 std::vector<std::pair<llvm::WeakVH,llvm::Constant*> > CXXGlobalDtors;
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000417
Douglas Gregor6ddfca92013-01-14 17:21:00 +0000418 /// \brief The complete set of modules that has been imported.
419 llvm::SetVector<clang::Module *> ImportedModules;
420
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000421 /// \brief A vector of metadata strings.
422 SmallVector<llvm::Value *, 16> LinkerOptionsMetadata;
423
Douglas Gregorabf4e0d2011-08-09 15:54:21 +0000424 /// @name Cache for Objective-C runtime types
425 /// @{
426
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000427 /// Cached reference to the class for constant strings. This value has type
428 /// int * but is actually an Obj-C class pointer.
Fariborz Jahaniand1b67782013-04-16 15:25:39 +0000429 llvm::WeakVH CFConstantStringClassRef;
Mike Stump11289f42009-09-09 15:08:12 +0000430
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000431 /// Cached reference to the class for constant strings. This value has type
432 /// int * but is actually an Obj-C class pointer.
Fariborz Jahaniand1b67782013-04-16 15:25:39 +0000433 llvm::WeakVH ConstantStringClassRef;
Fariborz Jahaniane804c282010-04-23 17:41:07 +0000434
Douglas Gregorabf4e0d2011-08-09 15:54:21 +0000435 /// \brief The LLVM type corresponding to NSConstantString.
436 llvm::StructType *NSConstantStringType;
437
Douglas Gregor636e2002011-08-09 17:23:49 +0000438 /// \brief The type used to describe the state of a fast enumeration in
439 /// Objective-C's for..in loop.
440 QualType ObjCFastEnumerationStateType;
441
Douglas Gregorabf4e0d2011-08-09 15:54:21 +0000442 /// @}
443
David Chisnall481e3a82010-01-23 02:40:42 +0000444 /// Lazily create the Objective-C runtime
445 void createObjCRuntime();
446
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000447 void createOpenCLRuntime();
Alexey Bataev9959db52014-05-06 10:08:46 +0000448 void createOpenMPRuntime();
Peter Collingbournefe883422011-10-06 18:29:37 +0000449 void createCUDARuntime();
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000450
Rafael Espindola4fdc1752011-12-19 14:41:01 +0000451 bool isTriviallyRecursive(const FunctionDecl *F);
Peter Collingbourne4d90dba2013-06-05 17:49:37 +0000452 bool shouldEmitFunction(GlobalDecl GD);
Daniel Dunbar900546d2010-07-16 00:00:15 +0000453
454 /// @name Cache for Blocks Runtime Globals
455 /// @{
456
457 llvm::Constant *NSConcreteGlobalBlock;
458 llvm::Constant *NSConcreteStackBlock;
Daniel Dunbar3348e2d2010-07-16 00:00:19 +0000459
Daniel Dunbar900546d2010-07-16 00:00:15 +0000460 llvm::Constant *BlockObjectAssign;
461 llvm::Constant *BlockObjectDispose;
462
Chris Lattnera5f58b02011-07-09 17:41:47 +0000463 llvm::Type *BlockDescriptorType;
464 llvm::Type *GenericBlockLiteralType;
John McCallad7c5c12011-02-08 08:22:06 +0000465
466 struct {
467 int GlobalUniqueCount;
468 } Block;
Will Dietzf54319c2013-01-18 11:30:38 +0000469
Nadav Rotem1da30942013-03-23 06:43:35 +0000470 /// void @llvm.lifetime.start(i64 %size, i8* nocapture <ptr>)
471 llvm::Constant *LifetimeStartFn;
472
473 /// void @llvm.lifetime.end(i64 %size, i8* nocapture <ptr>)
474 llvm::Constant *LifetimeEndFn;
475
Fariborz Jahanian63628032012-06-26 16:06:38 +0000476 GlobalDecl initializedGlobalDecl;
John McCallad7c5c12011-02-08 08:22:06 +0000477
Alexey Samsonovb7dd3292014-07-09 19:40:08 +0000478 SanitizerBlacklist SanitizerBL;
Will Dietzf54319c2013-01-18 11:30:38 +0000479
Alexey Samsonov4b8de112014-08-01 21:35:28 +0000480 std::unique_ptr<SanitizerMetadata> SanitizerMD;
481
Daniel Dunbar900546d2010-07-16 00:00:15 +0000482 /// @}
Alex Lorenzee024992014-08-04 18:41:51 +0000483
484 llvm::DenseMap<const Decl *, bool> DeferredEmptyCoverageMappingDecls;
485
486 std::unique_ptr<CoverageMappingModuleGen> CoverageMapping;
Chris Lattnerf97fe382007-05-24 06:29:05 +0000487public:
Chandler Carruthbc55fe22009-11-12 17:24:48 +0000488 CodeGenModule(ASTContext &C, const CodeGenOptions &CodeGenOpts,
John McCall568d4102013-04-16 22:48:20 +0000489 llvm::Module &M, const llvm::DataLayout &TD,
Alex Lorenzee024992014-08-04 18:41:51 +0000490 DiagnosticsEngine &Diags,
491 CoverageSourceInfo *CoverageInfo = nullptr);
Ted Kremenek2c674f62008-08-05 18:50:11 +0000492
Chris Lattnera087ff92008-03-01 08:45:05 +0000493 ~CodeGenModule();
Mike Stump1676c5b2009-02-13 19:12:34 +0000494
Rafael Espindolac0ff7442013-12-09 14:59:08 +0000495 void clear();
496
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000497 /// Finalize LLVM code generation.
Ted Kremenek2c674f62008-08-05 18:50:11 +0000498 void Release();
Daniel Dunbar8d480592008-08-11 18:12:00 +0000499
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000500 /// Return a reference to the configured Objective-C runtime.
Mike Stump1676c5b2009-02-13 19:12:34 +0000501 CGObjCRuntime &getObjCRuntime() {
Peter Collingbournee1d20992011-07-27 20:29:46 +0000502 if (!ObjCRuntime) createObjCRuntime();
503 return *ObjCRuntime;
Daniel Dunbar8d480592008-08-11 18:12:00 +0000504 }
Mike Stump1676c5b2009-02-13 19:12:34 +0000505
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000506 /// Return true iff an Objective-C runtime has been configured.
Peter Collingbournee1d20992011-07-27 20:29:46 +0000507 bool hasObjCRuntime() { return !!ObjCRuntime; }
Daniel Dunbar8d480592008-08-11 18:12:00 +0000508
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000509 /// Return a reference to the configured OpenCL runtime.
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000510 CGOpenCLRuntime &getOpenCLRuntime() {
Craig Topper8a13c412014-05-21 05:09:00 +0000511 assert(OpenCLRuntime != nullptr);
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000512 return *OpenCLRuntime;
513 }
514
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000515 /// Return a reference to the configured OpenMP runtime.
Alexey Bataev9959db52014-05-06 10:08:46 +0000516 CGOpenMPRuntime &getOpenMPRuntime() {
517 assert(OpenMPRuntime != nullptr);
518 return *OpenMPRuntime;
519 }
520
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000521 /// Return a reference to the configured CUDA runtime.
Peter Collingbournefe883422011-10-06 18:29:37 +0000522 CGCUDARuntime &getCUDARuntime() {
Craig Topper8a13c412014-05-21 05:09:00 +0000523 assert(CUDARuntime != nullptr);
Peter Collingbournefe883422011-10-06 18:29:37 +0000524 return *CUDARuntime;
525 }
526
John McCall31168b02011-06-15 23:02:42 +0000527 ARCEntrypoints &getARCEntrypoints() const {
Craig Topper8a13c412014-05-21 05:09:00 +0000528 assert(getLangOpts().ObjCAutoRefCount && ARCData != nullptr);
John McCall31168b02011-06-15 23:02:42 +0000529 return *ARCData;
530 }
531
532 RREntrypoints &getRREntrypoints() const {
Craig Topper8a13c412014-05-21 05:09:00 +0000533 assert(RRData != nullptr);
John McCall31168b02011-06-15 23:02:42 +0000534 return *RRData;
535 }
536
Justin Bogner837a6f62014-04-18 21:52:00 +0000537 InstrProfStats &getPGOStats() { return PGOStats; }
538 llvm::IndexedInstrProfReader *getPGOReader() const { return PGOReader.get(); }
Justin Bogneref512b92014-01-06 22:27:43 +0000539
Alex Lorenzee024992014-08-04 18:41:51 +0000540 CoverageMappingModuleGen *getCoverageMapping() const {
541 return CoverageMapping.get();
542 }
543
Eli Friedman1c8d2ca2012-03-09 03:27:46 +0000544 llvm::Constant *getStaticLocalDeclAddress(const VarDecl *D) {
545 return StaticLocalDeclMap[D];
Fariborz Jahanian3fef72f2010-04-18 21:01:23 +0000546 }
Fariborz Jahanian4d55b2d2010-04-19 18:15:02 +0000547 void setStaticLocalDeclAddress(const VarDecl *D,
Eli Friedman1c8d2ca2012-03-09 03:27:46 +0000548 llvm::Constant *C) {
549 StaticLocalDeclMap[D] = C;
Fariborz Jahanian3fef72f2010-04-18 21:01:23 +0000550 }
551
John McCallb88a5662012-03-30 21:00:39 +0000552 llvm::GlobalVariable *getStaticLocalDeclGuardAddress(const VarDecl *D) {
553 return StaticLocalDeclGuardMap[D];
554 }
555 void setStaticLocalDeclGuardAddress(const VarDecl *D,
556 llvm::GlobalVariable *C) {
557 StaticLocalDeclGuardMap[D] = C;
558 }
559
Alp Tokerfb8d02b2014-06-05 22:10:59 +0000560 bool lookupRepresentativeDecl(StringRef MangledName,
561 GlobalDecl &Result) const;
562
Fariborz Jahanian1bed4132012-01-08 19:13:23 +0000563 llvm::Constant *getAtomicSetterHelperFnMap(QualType Ty) {
564 return AtomicSetterHelperFnMap[Ty];
Fariborz Jahanian7ff610b2012-01-06 22:33:54 +0000565 }
Fariborz Jahanian1bed4132012-01-08 19:13:23 +0000566 void setAtomicSetterHelperFnMap(QualType Ty,
Fariborz Jahanian7ff610b2012-01-06 22:33:54 +0000567 llvm::Constant *Fn) {
Fariborz Jahanian1bed4132012-01-08 19:13:23 +0000568 AtomicSetterHelperFnMap[Ty] = Fn;
569 }
570
571 llvm::Constant *getAtomicGetterHelperFnMap(QualType Ty) {
572 return AtomicGetterHelperFnMap[Ty];
573 }
574 void setAtomicGetterHelperFnMap(QualType Ty,
575 llvm::Constant *Fn) {
576 AtomicGetterHelperFnMap[Ty] = Fn;
Fariborz Jahanian7ff610b2012-01-06 22:33:54 +0000577 }
578
Warren Hunt5c2b4ea2014-05-23 16:07:43 +0000579 llvm::Constant *getTypeDescriptorFromMap(QualType Ty) {
Will Dietz949ec542013-11-08 01:09:22 +0000580 return TypeDescriptorMap[Ty];
581 }
Warren Hunt5c2b4ea2014-05-23 16:07:43 +0000582 void setTypeDescriptorInMap(QualType Ty, llvm::Constant *C) {
Will Dietz949ec542013-11-08 01:09:22 +0000583 TypeDescriptorMap[Ty] = C;
584 }
585
Devang Pateld6ffebb2011-03-07 18:45:56 +0000586 CGDebugInfo *getModuleDebugInfo() { return DebugInfo; }
Devang Patele65982c2011-03-07 18:29:53 +0000587
Dan Gohman515a60d2012-02-16 00:57:37 +0000588 llvm::MDNode *getNoObjCARCExceptionsMetadata() {
589 if (!NoObjCARCExceptionsMetadata)
590 NoObjCARCExceptionsMetadata =
591 llvm::MDNode::get(getLLVMContext(),
592 SmallVector<llvm::Value*,1>());
593 return NoObjCARCExceptionsMetadata;
594 }
595
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000596 ASTContext &getContext() const { return Context; }
David Blaikiebbafb8a2012-03-11 07:00:24 +0000597 const LangOptions &getLangOpts() const { return LangOpts; }
John McCallc8e01702013-04-16 22:48:15 +0000598 const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
Chris Lattner23b7eb62007-06-15 23:05:46 +0000599 llvm::Module &getModule() const { return TheModule; }
David Blaikie9c902b52011-09-25 23:23:43 +0000600 DiagnosticsEngine &getDiags() const { return Diags; }
Micah Villmowdd31ca12012-10-08 16:25:52 +0000601 const llvm::DataLayout &getDataLayout() const { return TheDataLayout; }
John McCallc8e01702013-04-16 22:48:15 +0000602 const TargetInfo &getTarget() const { return Target; }
Rafael Espindola9f834732014-09-19 01:54:22 +0000603 const llvm::Triple &getTriple() const;
604 bool supportsCOMDAT() const;
605
Alp Toker82862252013-12-28 21:58:40 +0000606 CGCXXABI &getCXXABI() const { return *ABI; }
Owen Anderson170229f2009-07-14 23:10:40 +0000607 llvm::LLVMContext &getLLVMContext() { return VMContext; }
Craig Topper8a13c412014-05-21 05:09:00 +0000608
609 bool shouldUseTBAA() const { return TBAA != nullptr; }
John McCall53fcbd22011-02-26 08:07:02 +0000610
John McCallc8e01702013-04-16 22:48:15 +0000611 const TargetCodeGenInfo &getTargetCodeGenInfo();
612
613 CodeGenTypes &getTypes() { return Types; }
614
615 CodeGenVTables &getVTables() { return VTables; }
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000616
Timur Iskhodzhanov58776632013-11-05 15:54:58 +0000617 ItaniumVTableContext &getItaniumVTableContext() {
618 return VTables.getItaniumVTableContext();
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +0000619 }
John McCallc8e01702013-04-16 22:48:15 +0000620
Timur Iskhodzhanov58776632013-11-05 15:54:58 +0000621 MicrosoftVTableContext &getMicrosoftVTableContext() {
622 return VTables.getMicrosoftVTableContext();
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000623 }
624
Dan Gohman947c9af2010-10-14 23:06:10 +0000625 llvm::MDNode *getTBAAInfo(QualType QTy);
Kostya Serebryany141e46f2012-03-26 17:03:51 +0000626 llvm::MDNode *getTBAAInfoForVTablePtr();
Dan Gohman22695fc2012-09-28 21:58:29 +0000627 llvm::MDNode *getTBAAStructInfo(QualType QTy);
Manman Renc451e572013-04-04 21:53:22 +0000628 /// Return the MDNode in the type DAG for the given struct type.
629 llvm::MDNode *getTBAAStructTypeInfo(QualType QTy);
630 /// Return the path-aware tag for given base type, access node and offset.
631 llvm::MDNode *getTBAAStructTagInfo(QualType BaseTy, llvm::MDNode *AccessN,
632 uint64_t O);
Dan Gohman947c9af2010-10-14 23:06:10 +0000633
Richard Smithe070fd22012-02-17 06:48:11 +0000634 bool isTypeConstant(QualType QTy, bool ExcludeCtorDtor);
635
John McCalla8ec7eb2013-03-07 21:37:17 +0000636 bool isPaddedAtomicType(QualType type);
637 bool isPaddedAtomicType(const AtomicType *type);
638
Manman Rene1ad74e2013-04-11 23:02:56 +0000639 /// Decorate the instruction with a TBAA tag. For scalar TBAA, the tag
640 /// is the same as the type. For struct-path aware TBAA, the tag
641 /// is different from the type: base type, access type and offset.
642 /// When ConvertTypeToTag is true, we create a tag based on the scalar type.
643 void DecorateInstruction(llvm::Instruction *Inst,
644 llvm::MDNode *TBAAInfo,
645 bool ConvertTypeToTag = true);
Dan Gohman947c9af2010-10-14 23:06:10 +0000646
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000647 /// Emit the given number of characters as a value of type size_t.
John McCall23c29fe2011-06-24 21:55:10 +0000648 llvm::ConstantInt *getSize(CharUnits numChars);
649
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000650 /// Set the visibility for the given LLVM GlobalValue.
Anders Carlssonc6a47892011-01-29 19:39:23 +0000651 void setGlobalVisibility(llvm::GlobalValue *GV, const NamedDecl *D) const;
Daniel Dunbarf5f359f2009-04-14 06:00:08 +0000652
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000653 /// Set the TLS mode for the given LLVM GlobalVariable for the thread-local
654 /// variable declaration D.
Hans Wennborgf60f6af2012-06-28 08:01:44 +0000655 void setTLSMode(llvm::GlobalVariable *GV, const VarDecl &D) const;
656
Anders Carlsson537fdce2011-01-29 20:59:35 +0000657 static llvm::GlobalValue::VisibilityTypes GetLLVMVisibility(Visibility V) {
658 switch (V) {
659 case DefaultVisibility: return llvm::GlobalValue::DefaultVisibility;
660 case HiddenVisibility: return llvm::GlobalValue::HiddenVisibility;
661 case ProtectedVisibility: return llvm::GlobalValue::ProtectedVisibility;
662 }
663 llvm_unreachable("unknown visibility!");
Anders Carlsson537fdce2011-01-29 20:59:35 +0000664 }
665
John McCalld4324142010-02-19 01:32:20 +0000666 llvm::Constant *GetAddrOfGlobal(GlobalDecl GD) {
667 if (isa<CXXConstructorDecl>(GD.getDecl()))
Rafael Espindola1ac0ec82014-09-11 15:42:06 +0000668 return getAddrOfCXXStructor(cast<CXXConstructorDecl>(GD.getDecl()),
669 getFromCtorType(GD.getCtorType()));
John McCalld4324142010-02-19 01:32:20 +0000670 else if (isa<CXXDestructorDecl>(GD.getDecl()))
Rafael Espindola1ac0ec82014-09-11 15:42:06 +0000671 return getAddrOfCXXStructor(cast<CXXDestructorDecl>(GD.getDecl()),
672 getFromDtorType(GD.getDtorType()));
John McCalld4324142010-02-19 01:32:20 +0000673 else if (isa<FunctionDecl>(GD.getDecl()))
674 return GetAddrOfFunction(GD);
675 else
676 return GetAddrOfGlobalVar(cast<VarDecl>(GD.getDecl()));
677 }
678
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000679 /// Will return a global variable of the given type. If a variable with a
680 /// different type already exists then a new variable with the right type
681 /// will be created and all uses of the old variable will be replaced with a
682 /// bitcast to the new variable.
Anders Carlssonda80af32011-01-29 18:20:20 +0000683 llvm::GlobalVariable *
Chris Lattner01cf8db2011-07-20 06:58:45 +0000684 CreateOrReplaceCXXRuntimeVariable(StringRef Name, llvm::Type *Ty,
Anders Carlssonda80af32011-01-29 18:20:20 +0000685 llvm::GlobalValue::LinkageTypes Linkage);
686
David Majnemerb3341ea2014-10-05 05:05:40 +0000687 llvm::Function *CreateGlobalInitOrDestructFunction(llvm::FunctionType *ty,
688 const Twine &name,
689 bool TLS = false);
690
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000691 /// Return the address space of the underlying global variable for D, as
692 /// determined by its declaration. Normally this is the same as the address
693 /// space of D's type, but in CUDA, address spaces are associated with
694 /// declarations, not types.
Peter Collingbournef44bdf92012-05-20 21:08:35 +0000695 unsigned GetGlobalVarAddressSpace(const VarDecl *D, unsigned AddrSpace);
696
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000697 /// Return the llvm::Constant for the address of the given global variable.
698 /// If Ty is non-null and if the global doesn't exist, then it will be greated
699 /// with the specified type instead of whatever the normal requested type
700 /// would be.
Chris Lattner149927c2009-03-21 09:16:30 +0000701 llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D,
Craig Topper8a13c412014-05-21 05:09:00 +0000702 llvm::Type *Ty = nullptr);
Daniel Dunbar9c426522008-07-29 23:18:29 +0000703
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000704 /// Return the address of the given function. If Ty is non-null, then this
705 /// function will use the specified type if it has to create it.
Rafael Espindola94abb8f2013-12-09 04:29:47 +0000706 llvm::Constant *GetAddrOfFunction(GlobalDecl GD, llvm::Type *Ty = 0,
707 bool ForVTable = false,
708 bool DontDefer = false);
Daniel Dunbarc4baa062008-08-13 23:20:05 +0000709
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000710 /// Get the address of the RTTI descriptor for the given type.
John McCall48bf3492010-04-30 01:15:21 +0000711 llvm::Constant *GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH = false);
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +0000712
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000713 /// Get the address of a uuid descriptor .
Nico Webercf4ff5862012-10-11 10:13:44 +0000714 llvm::Constant *GetAddrOfUuidDescriptor(const CXXUuidofExpr* E);
715
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000716 /// Get the address of the thunk for the given global decl.
Anders Carlssonfe8a9932011-02-06 17:15:43 +0000717 llvm::Constant *GetAddrOfThunk(GlobalDecl GD, const ThunkInfo &Thunk);
Anders Carlssoncd836f02010-03-23 17:17:29 +0000718
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000719 /// Get a reference to the target of VD.
Rafael Espindola2e42fec2010-03-04 18:17:24 +0000720 llvm::Constant *GetWeakRefReference(const ValueDecl *VD);
721
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000722 /// Returns the offset from a derived class to a class. Returns null if the
723 /// offset is 0.
Anders Carlsson8a64c1c2010-04-24 21:23:59 +0000724 llvm::Constant *
725 GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl,
John McCallcf142162010-08-07 06:22:56 +0000726 CastExpr::path_const_iterator PathBegin,
727 CastExpr::path_const_iterator PathEnd);
John McCallad7c5c12011-02-08 08:22:06 +0000728
John McCallf9b056b2011-03-31 08:03:29 +0000729 /// A pair of helper functions for a __block variable.
730 class ByrefHelpers : public llvm::FoldingSetNode {
731 public:
732 llvm::Constant *CopyHelper;
733 llvm::Constant *DisposeHelper;
734
735 /// The alignment of the field. This is important because
736 /// different offsets to the field within the byref struct need to
737 /// have different helper functions.
738 CharUnits Alignment;
739
740 ByrefHelpers(CharUnits alignment) : Alignment(alignment) {}
741 virtual ~ByrefHelpers();
742
743 void Profile(llvm::FoldingSetNodeID &id) const {
744 id.AddInteger(Alignment.getQuantity());
745 profileImpl(id);
746 }
747 virtual void profileImpl(llvm::FoldingSetNodeID &id) const = 0;
748
749 virtual bool needsCopy() const { return true; }
750 virtual void emitCopy(CodeGenFunction &CGF,
751 llvm::Value *dest, llvm::Value *src) = 0;
752
753 virtual bool needsDispose() const { return true; }
754 virtual void emitDispose(CodeGenFunction &CGF, llvm::Value *field) = 0;
755 };
756
757 llvm::FoldingSet<ByrefHelpers> ByrefHelpersCache;
John McCallad7c5c12011-02-08 08:22:06 +0000758
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000759 /// Fetches the global unique block count.
John McCall147d0212011-02-22 22:38:33 +0000760 int getUniqueBlockCount() { return ++Block.GlobalUniqueCount; }
Fariborz Jahanian63628032012-06-26 16:06:38 +0000761
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000762 /// Fetches the type of a generic block descriptor.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000763 llvm::Type *getBlockDescriptorType();
John McCallad7c5c12011-02-08 08:22:06 +0000764
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000765 /// The type of a generic block literal.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000766 llvm::Type *getGenericBlockLiteralType();
John McCallad7c5c12011-02-08 08:22:06 +0000767
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000768 /// Gets the address of a block which requires no captures.
John McCallad7c5c12011-02-08 08:22:06 +0000769 llvm::Constant *GetAddrOfGlobalBlock(const BlockExpr *BE, const char *);
Anders Carlsson8a64c1c2010-04-24 21:23:59 +0000770
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000771 /// Return a pointer to a constant CFString object for the given string.
Steve Naroffe14b3682009-04-01 13:55:36 +0000772 llvm::Constant *GetAddrOfConstantCFString(const StringLiteral *Literal);
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000773
774 /// Return a pointer to a constant NSString object for the given string. Or a
775 /// user defined String object as defined via
Fariborz Jahanian50c925f2010-10-19 17:19:29 +0000776 /// -fconstant-string-class=class_name option.
777 llvm::Constant *GetAddrOfConstantString(const StringLiteral *Literal);
Chris Lattner36fc8792008-02-11 00:02:17 +0000778
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000779 /// Return a constant array for the given string.
Eli Friedmanfcec6302011-11-01 02:23:42 +0000780 llvm::Constant *GetConstantArrayFromStringLiteral(const StringLiteral *E);
781
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000782 /// Return a pointer to a constant array for the given string literal.
Alexey Samsonovb2cc23d2014-07-10 22:18:36 +0000783 llvm::GlobalVariable *
784 GetAddrOfConstantStringFromLiteral(const StringLiteral *S);
Daniel Dunbar6dfdf8c2008-08-10 20:25:57 +0000785
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000786 /// Return a pointer to a constant array for the given ObjCEncodeExpr node.
Alexey Samsonovb2cc23d2014-07-10 22:18:36 +0000787 llvm::GlobalVariable *
788 GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *);
Mike Stump11289f42009-09-09 15:08:12 +0000789
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000790 /// Returns a pointer to a character array containing the literal and a
791 /// terminating '\0' character. The result has pointer to array type.
Daniel Dunbardfcf5992008-10-17 21:56:50 +0000792 ///
Mike Stump1676c5b2009-02-13 19:12:34 +0000793 /// \param GlobalName If provided, the name to use for the global (if one is
794 /// created).
Alexey Samsonovb2cc23d2014-07-10 22:18:36 +0000795 llvm::GlobalVariable *
796 GetAddrOfConstantCString(const std::string &Str,
797 const char *GlobalName = nullptr,
798 unsigned Alignment = 0);
Richard Smith2d988f02011-11-22 22:48:32 +0000799
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000800 /// Returns a pointer to a constant global variable for the given file-scope
801 /// compound literal expression.
Richard Smith2d988f02011-11-22 22:48:32 +0000802 llvm::Constant *GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr*E);
Richard Smithe6c01442013-06-05 00:46:14 +0000803
804 /// \brief Returns a pointer to a global variable representing a temporary
805 /// with static or thread storage duration.
806 llvm::Constant *GetAddrOfGlobalTemporary(const MaterializeTemporaryExpr *E,
807 const Expr *Inner);
808
Douglas Gregor636e2002011-08-09 17:23:49 +0000809 /// \brief Retrieve the record type that describes the state of an
810 /// Objective-C fast enumeration loop (for..in).
811 QualType getObjCFastEnumerationStateType();
Rafael Espindola8d2a19b2014-09-08 16:01:27 +0000812
Rafael Espindola53686182014-09-15 19:34:18 +0000813 // Produce code for this constructor/destructor. This method doesn't try
814 // to apply any ABI rules about which other constructors/destructors
815 // are needed or if they are alias to each other.
816 llvm::Function *codegenCXXStructor(const CXXMethodDecl *MD,
817 StructorType Type);
818
Rafael Espindola1ac0ec82014-09-11 15:42:06 +0000819 /// Return the address of the constructor/destructor of the given type.
Rafael Espindola8d2a19b2014-09-08 16:01:27 +0000820 llvm::GlobalValue *
821 getAddrOfCXXStructor(const CXXMethodDecl *MD, StructorType Type,
822 const CGFunctionInfo *FnInfo = nullptr,
823 llvm::FunctionType *FnType = nullptr,
824 bool DontDefer = false);
825
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000826 /// Given a builtin id for a function like "__builtin_fabsf", return a
827 /// Function* for "fabsf".
Daniel Dunbarff0553e2009-09-14 04:33:21 +0000828 llvm::Value *getBuiltinLibFunction(const FunctionDecl *FD,
829 unsigned BuiltinID);
Daniel Dunbarc4baa062008-08-13 23:20:05 +0000830
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000831 llvm::Function *getIntrinsic(unsigned IID, ArrayRef<llvm::Type*> Tys = None);
Daniel Dunbar9c426522008-07-29 23:18:29 +0000832
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000833 /// Emit code for a single top level declaration.
Daniel Dunbarfce4be82008-08-15 23:26:23 +0000834 void EmitTopLevelDecl(Decl *D);
Daniel Dunbar9c426522008-07-29 23:18:29 +0000835
Alex Lorenzee024992014-08-04 18:41:51 +0000836 /// \brief Stored a deferred empty coverage mapping for an unused
837 /// and thus uninstrumented top level declaration.
838 void AddDeferredUnusedCoverageMapping(Decl *D);
839
840 /// \brief Remove the deferred empty coverage mapping as this
841 /// declaration is actually instrumented.
842 void ClearUnusedCoverageMapping(const Decl *D);
843
844 /// \brief Emit all the deferred coverage mappings
845 /// for the uninstrumented functions.
846 void EmitDeferredUnusedCoverageMappings();
847
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000848 /// Tell the consumer that this variable has been instantiated.
Rafael Espindoladf88f6f2012-03-08 15:51:03 +0000849 void HandleCXXStaticMemberVarInstantiation(VarDecl *VD);
Rafael Espindola189fa742012-03-05 10:54:55 +0000850
Richard Smithdf931ce2013-04-06 05:00:46 +0000851 /// \brief If the declaration has internal linkage but is inside an
852 /// extern "C" linkage specification, prepare to emit an alias for it
853 /// to the expected name.
854 template<typename SomeDecl>
855 void MaybeHandleStaticInExternC(const SomeDecl *D, llvm::GlobalValue *GV);
856
Rafael Espindola060062a2014-03-06 22:15:10 +0000857 /// Add a global to a list to be added to the llvm.used metadata.
858 void addUsedGlobal(llvm::GlobalValue *GV);
859
860 /// Add a global to a list to be added to the llvm.compiler.used metadata.
861 void addCompilerUsedGlobal(llvm::GlobalValue *GV);
Daniel Dunbar08b26a02009-02-13 20:29:50 +0000862
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000863 /// Add a destructor and object to add to the C++ global destructor function.
Chris Lattner87233f72010-06-19 05:52:45 +0000864 void AddCXXDtorEntry(llvm::Constant *DtorFn, llvm::Constant *Object) {
865 CXXGlobalDtors.push_back(std::make_pair(DtorFn, Object));
866 }
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000867
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000868 /// Create a new runtime function with the specified type and name.
Chris Lattner2192fe52011-07-18 04:24:23 +0000869 llvm::Constant *CreateRuntimeFunction(llvm::FunctionType *Ty,
Chris Lattner01cf8db2011-07-20 06:58:45 +0000870 StringRef Name,
Bill Wendling8594fcb2013-01-31 00:30:05 +0000871 llvm::AttributeSet ExtraAttrs =
872 llvm::AttributeSet());
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000873 /// Create a new runtime global variable with the specified type and name.
Chris Lattner2192fe52011-07-18 04:24:23 +0000874 llvm::Constant *CreateRuntimeVariable(llvm::Type *Ty,
Chris Lattner01cf8db2011-07-20 06:58:45 +0000875 StringRef Name);
Daniel Dunbar23fd4622008-10-01 00:49:24 +0000876
Daniel Dunbar900546d2010-07-16 00:00:15 +0000877 ///@name Custom Blocks Runtime Interfaces
878 ///@{
879
880 llvm::Constant *getNSConcreteGlobalBlock();
881 llvm::Constant *getNSConcreteStackBlock();
882 llvm::Constant *getBlockObjectAssign();
883 llvm::Constant *getBlockObjectDispose();
884
885 ///@}
886
Nadav Rotem1da30942013-03-23 06:43:35 +0000887 llvm::Constant *getLLVMLifetimeStartFn();
888 llvm::Constant *getLLVMLifetimeEndFn();
889
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000890 // Make sure that this type is translated.
Devang Patel945b8ae2011-03-23 16:29:39 +0000891 void UpdateCompletedType(const TagDecl *TD);
Daniel Dunbar38ad1e62009-02-17 18:43:32 +0000892
John McCallf3a88602011-02-03 08:15:49 +0000893 llvm::Constant *getMemberPointerConstant(const UnaryOperator *e);
894
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000895 /// Try to emit the initializer for the given declaration as a constant;
896 /// returns 0 if the expression cannot be emitted as a constant.
Craig Topper8a13c412014-05-21 05:09:00 +0000897 llvm::Constant *EmitConstantInit(const VarDecl &D,
898 CodeGenFunction *CGF = nullptr);
Richard Smithdafff942012-01-14 04:30:29 +0000899
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000900 /// Try to emit the given expression as a constant; returns 0 if the
901 /// expression cannot be emitted as a constant.
Anders Carlsson80f97ab2009-04-08 04:48:15 +0000902 llvm::Constant *EmitConstantExpr(const Expr *E, QualType DestType,
Craig Topper8a13c412014-05-21 05:09:00 +0000903 CodeGenFunction *CGF = nullptr);
Daniel Dunbar38ad1e62009-02-17 18:43:32 +0000904
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000905 /// Emit the given constant value as a constant, in the type's scalar
906 /// representation.
Richard Smithdafff942012-01-14 04:30:29 +0000907 llvm::Constant *EmitConstantValue(const APValue &Value, QualType DestType,
Craig Topper8a13c412014-05-21 05:09:00 +0000908 CodeGenFunction *CGF = nullptr);
Richard Smithdafff942012-01-14 04:30:29 +0000909
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000910 /// Emit the given constant value as a constant, in the type's memory
911 /// representation.
Richard Smithbc6387672012-03-02 23:27:11 +0000912 llvm::Constant *EmitConstantValueForMemory(const APValue &Value,
913 QualType DestType,
Craig Topper8a13c412014-05-21 05:09:00 +0000914 CodeGenFunction *CGF = nullptr);
Richard Smithbc6387672012-03-02 23:27:11 +0000915
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000916 /// Return the result of value-initializing the given type, i.e. a null
917 /// expression of the given type. This is usually, but not always, an LLVM
918 /// null constant.
Eli Friedman20bb5e02009-04-13 21:47:26 +0000919 llvm::Constant *EmitNullConstant(QualType T);
920
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000921 /// Return a null constant appropriate for zero-initializing a base class with
922 /// the given type. This is usually, but not always, an LLVM null constant.
Eli Friedmanfde961d2011-10-14 02:27:24 +0000923 llvm::Constant *EmitNullConstantForBase(const CXXRecordDecl *Record);
924
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000925 /// Emit a general error that something can't be done.
Chandler Carruth84537952012-03-30 19:44:53 +0000926 void Error(SourceLocation loc, StringRef error);
John McCall7ef5cb32011-03-18 02:56:14 +0000927
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000928 /// Print out an error that codegen doesn't support the specified stmt yet.
David Blaikie4a9ec7b2013-08-19 21:02:26 +0000929 void ErrorUnsupported(const Stmt *S, const char *Type);
Mike Stump1676c5b2009-02-13 19:12:34 +0000930
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000931 /// Print out an error that codegen doesn't support the specified decl yet.
David Blaikie4a9ec7b2013-08-19 21:02:26 +0000932 void ErrorUnsupported(const Decl *D, const char *Type);
Dan Gohman75d69da2008-05-22 00:50:06 +0000933
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000934 /// Set the attributes on the LLVM function for the given decl and function
935 /// info. This applies attributes necessary for handling the ABI as well as
936 /// user specified attributes like section.
Daniel Dunbarc3e7cff2009-04-17 00:48:04 +0000937 void SetInternalFunctionAttributes(const Decl *D, llvm::Function *F,
938 const CGFunctionInfo &FI);
Daniel Dunbar449a3392008-09-04 23:41:35 +0000939
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000940 /// Set the LLVM function attributes (sext, zext, etc).
Daniel Dunbaraeddffc2009-04-14 07:08:30 +0000941 void SetLLVMFunctionAttributes(const Decl *D,
942 const CGFunctionInfo &Info,
943 llvm::Function *F);
Daniel Dunbarc68897d2008-09-10 00:41:16 +0000944
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000945 /// Set the LLVM function attributes which only apply to a function
James Dennett64fa1232014-08-15 20:04:40 +0000946 /// definition.
Daniel Dunbar38932572009-04-14 08:05:55 +0000947 void SetLLVMFunctionAttributesForDefinition(const Decl *D, llvm::Function *F);
948
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000949 /// Return true iff the given type uses 'sret' when used as a return type.
Daniel Dunbar6f2e8392010-07-14 23:39:36 +0000950 bool ReturnTypeUsesSRet(const CGFunctionInfo &FI);
951
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000952 /// Return true iff the given type uses an argument slot when 'sret' is used
953 /// as a return type.
Tim Northovere77cc392014-03-29 13:28:05 +0000954 bool ReturnSlotInterferesWithArgs(const CGFunctionInfo &FI);
955
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000956 /// Return true iff the given type uses 'fpret' when used as a return type.
Daniel Dunbar6f2e8392010-07-14 23:39:36 +0000957 bool ReturnTypeUsesFPRet(QualType ResultType);
Daniel Dunbarc68897d2008-09-10 00:41:16 +0000958
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000959 /// Return true iff the given type uses 'fp2ret' when used as a return type.
Anders Carlsson2f1a6c32011-10-31 16:27:11 +0000960 bool ReturnTypeUsesFP2Ret(QualType ResultType);
961
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000962 /// Get the LLVM attributes and calling convention to use for a particular
963 /// function type.
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000964 ///
965 /// \param Info - The function type information.
966 /// \param TargetDecl - The decl these attributes are being constructed
967 /// for. If supplied the attributes applied to this decl may contribute to the
968 /// function attributes and calling convention.
969 /// \param PAL [out] - On return, the attribute list to use.
970 /// \param CallingConv [out] - On return, the LLVM calling convention to use.
Daniel Dunbard931a872009-02-02 22:03:45 +0000971 void ConstructAttributeList(const CGFunctionInfo &Info,
972 const Decl *TargetDecl,
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000973 AttributeListType &PAL,
Bill Wendlingf4d64cb2013-02-22 00:13:35 +0000974 unsigned &CallingConv,
975 bool AttrOnCallSite);
Daniel Dunbarc68897d2008-09-10 00:41:16 +0000976
Chris Lattner01cf8db2011-07-20 06:58:45 +0000977 StringRef getMangledName(GlobalDecl GD);
Alp Tokerfb8d02b2014-06-05 22:10:59 +0000978 StringRef getBlockMangledName(GlobalDecl GD, const BlockDecl *BD);
Douglas Gregorbeecd582009-04-21 17:11:58 +0000979
980 void EmitTentativeDefinition(const VarDecl *D);
Mike Stumpbc78a722009-07-31 18:25:34 +0000981
Douglas Gregor88d292c2010-05-13 16:44:06 +0000982 void EmitVTable(CXXRecordDecl *Class, bool DefinitionRequired);
983
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000984 /// Emit the RTTI descriptors for the builtin types.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +0000985 void EmitFundamentalRTTIDescriptors();
986
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000987 /// \brief Appends Opts to the "Linker Options" metadata value.
988 void AppendLinkerOptions(StringRef Opts);
989
Benjamin Kramer82dfc972013-06-04 09:13:21 +0000990 /// \brief Appends a detect mismatch command to the linker options.
Aaron Ballman5d041be2013-06-04 02:07:14 +0000991 void AddDetectMismatch(StringRef Name, StringRef Value);
992
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000993 /// \brief Appends a dependent lib to the "Linker Options" metadata value.
994 void AddDependentLib(StringRef Lib);
995
Peter Collingbourne4d90dba2013-06-05 17:49:37 +0000996 llvm::GlobalVariable::LinkageTypes getFunctionLinkage(GlobalDecl GD);
John McCalld4324142010-02-19 01:32:20 +0000997
Rafael Espindola64926362014-05-08 14:46:46 +0000998 void setFunctionLinkage(GlobalDecl GD, llvm::Function *F) {
999 F->setLinkage(getFunctionLinkage(GD));
John McCall7cb02202010-05-25 04:30:21 +00001000 }
1001
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001002 /// Return the appropriate linkage for the vtable, VTT, and type information
1003 /// of the given class.
Anders Carlsson68d34242011-01-24 02:04:33 +00001004 llvm::GlobalVariable::LinkageTypes getVTableLinkage(const CXXRecordDecl *RD);
Ken Dyck98ca7942010-01-26 13:48:07 +00001005
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001006 /// Return the store size, in character units, of the given LLVM type.
Chris Lattner2192fe52011-07-18 04:24:23 +00001007 CharUnits GetTargetTypeStoreSize(llvm::Type *Ty) const;
Fariborz Jahanian1518a5ec2010-10-27 16:21:54 +00001008
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001009 /// Returns LLVM linkage for a declarator.
David Majnemer27d69db2014-04-28 22:17:59 +00001010 llvm::GlobalValue::LinkageTypes
Hans Wennborg1a3a0742014-05-14 19:54:53 +00001011 getLLVMLinkageForDeclarator(const DeclaratorDecl *D, GVALinkage Linkage,
Hans Wennborg275efb92014-05-28 01:52:23 +00001012 bool IsConstantVariable);
David Majnemer27d69db2014-04-28 22:17:59 +00001013
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001014 /// Returns LLVM linkage for a declarator.
David Majnemer27d69db2014-04-28 22:17:59 +00001015 llvm::GlobalValue::LinkageTypes
1016 getLLVMLinkageVarDefinition(const VarDecl *VD, bool IsConstant);
1017
Julien Lerouge5a6b6982011-09-09 22:41:49 +00001018 /// Emit all the global annotations.
1019 void EmitGlobalAnnotations();
1020
1021 /// Emit an annotation string.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001022 llvm::Constant *EmitAnnotationString(StringRef Str);
Julien Lerouge5a6b6982011-09-09 22:41:49 +00001023
1024 /// Emit the annotation's translation unit.
1025 llvm::Constant *EmitAnnotationUnit(SourceLocation Loc);
1026
1027 /// Emit the annotation line number.
1028 llvm::Constant *EmitAnnotationLineNo(SourceLocation L);
1029
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001030 /// Generate the llvm::ConstantStruct which contains the annotation
1031 /// information for a given GlobalValue. The annotation struct is
Julien Lerouge5a6b6982011-09-09 22:41:49 +00001032 /// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the
1033 /// GlobalValue being annotated. The second field is the constant string
1034 /// created from the AnnotateAttr's annotation. The third field is a constant
1035 /// string containing the name of the translation unit. The fourth field is
1036 /// the line number in the file of the annotated value declaration.
1037 llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV,
1038 const AnnotateAttr *AA,
1039 SourceLocation L);
1040
1041 /// Add global annotations that are set on D, for the global GV. Those
1042 /// annotations are emitted during finalization of the LLVM code.
1043 void AddGlobalAnnotations(const ValueDecl *D, llvm::GlobalValue *GV);
1044
Alexey Samsonovb7dd3292014-07-09 19:40:08 +00001045 const SanitizerBlacklist &getSanitizerBlacklist() const {
1046 return SanitizerBL;
Will Dietzf54319c2013-01-18 11:30:38 +00001047 }
1048
Alexey Samsonov4b8de112014-08-01 21:35:28 +00001049 SanitizerMetadata *getSanitizerMetadata() {
1050 return SanitizerMD.get();
1051 }
Alexey Samsonov4f319cc2014-07-02 16:54:41 +00001052
John McCall6bd2a892013-01-25 22:31:03 +00001053 void addDeferredVTable(const CXXRecordDecl *RD) {
1054 DeferredVTables.push_back(RD);
1055 }
1056
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001057 /// Emit code for a singal global function or var decl. Forward declarations
1058 /// are emitted lazily.
Reid Klecknere7de47e2013-07-22 13:51:44 +00001059 void EmitGlobal(GlobalDecl D);
1060
Rafael Espindola02b77f42014-09-15 18:46:13 +00001061 bool TryEmitDefinitionAsAlias(GlobalDecl Alias, GlobalDecl Target,
1062 bool InEveryTU);
1063 bool TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D);
1064
1065 /// Set attributes for a global definition.
1066 void setFunctionDefinitionAttributes(const FunctionDecl *D,
1067 llvm::Function *F);
1068
Chris Lattner01cf8db2011-07-20 06:58:45 +00001069 llvm::GlobalValue *GetGlobalValue(StringRef Ref);
Mike Stump11289f42009-09-09 15:08:12 +00001070
Rafael Espindola1e4df922014-09-16 15:18:21 +00001071 /// Set attributes which are common to any form of a global definition (alias,
1072 /// Objective-C method, function, global variable).
1073 ///
1074 /// NOTE: This should only be called for definitions.
1075 void SetCommonAttributes(const Decl *D, llvm::GlobalValue *GV);
1076
Dario Domiziolic4fb8ca72014-09-19 22:06:24 +00001077 /// Set attributes which must be preserved by an alias. This includes common
1078 /// attributes (i.e. it includes a call to SetCommonAttributes).
1079 ///
1080 /// NOTE: This should only be called for definitions.
1081 void setAliasAttributes(const Decl *D, llvm::GlobalValue *GV);
1082
Rafael Espindola1e4df922014-09-16 15:18:21 +00001083 void addReplacement(StringRef Name, llvm::Constant *C);
1084private:
1085
Rafael Espindola94abb8f2013-12-09 04:29:47 +00001086 llvm::Constant *
1087 GetOrCreateLLVMFunction(StringRef MangledName, llvm::Type *Ty, GlobalDecl D,
1088 bool ForVTable, bool DontDefer = false,
1089 llvm::AttributeSet ExtraAttrs = llvm::AttributeSet());
1090
Chris Lattner01cf8db2011-07-20 06:58:45 +00001091 llvm::Constant *GetOrCreateLLVMGlobal(StringRef MangledName,
Chris Lattner2192fe52011-07-18 04:24:23 +00001092 llvm::PointerType *PTy,
Rafael Espindolab73c9732014-05-22 23:33:27 +00001093 const VarDecl *D);
Mike Stump11289f42009-09-09 15:08:12 +00001094
Rafael Espindolaee076a22014-05-13 18:45:53 +00001095 void setNonAliasAttributes(const Decl *D, llvm::GlobalObject *GO);
Rafael Espindola502f65a2014-05-05 20:21:03 +00001096
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001097 /// Set function attributes for a function declaration.
Anders Carlsson6710c532010-02-06 02:44:09 +00001098 void SetFunctionAttributes(GlobalDecl GD,
Eli Friedman895771a2009-05-26 01:22:57 +00001099 llvm::Function *F,
1100 bool IsIncompleteFunction);
Nuno Lopesb6f79532008-06-08 15:45:52 +00001101
Craig Topper8a13c412014-05-21 05:09:00 +00001102 void EmitGlobalDefinition(GlobalDecl D, llvm::GlobalValue *GV = nullptr);
Daniel Dunbarf0acf7b2009-02-19 07:15:39 +00001103
Rafael Espindolacd7743b2013-12-09 16:01:03 +00001104 void EmitGlobalFunctionDefinition(GlobalDecl GD, llvm::GlobalValue *GV);
Daniel Dunbar9c426522008-07-29 23:18:29 +00001105 void EmitGlobalVarDefinition(const VarDecl *D);
John McCall7ec50432010-03-19 23:29:14 +00001106 void EmitAliasDefinition(GlobalDecl GD);
Daniel Dunbar89654ee2008-08-26 08:29:31 +00001107 void EmitObjCPropertyImplementations(const ObjCImplementationDecl *D);
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00001108 void EmitObjCIvarInitializations(ObjCImplementationDecl *D);
Fariborz Jahanian1518a5ec2010-10-27 16:21:54 +00001109
Anders Carlssonf7475242009-04-15 15:55:24 +00001110 // C++ related functions.
Mike Stump11289f42009-09-09 15:08:12 +00001111
Anders Carlsson237f3492009-04-01 00:58:25 +00001112 void EmitNamespace(const NamespaceDecl *D);
Anders Carlssoncbaeb9e2009-04-02 05:55:18 +00001113 void EmitLinkageSpec(const LinkageSpecDecl *D);
Adrian Prantlffcf4ba2013-05-09 23:16:27 +00001114 void CompleteDIClassType(const CXXMethodDecl* D);
Anders Carlssonf7475242009-04-15 15:55:24 +00001115
Richard Smith2fd1d7a2013-04-19 16:42:07 +00001116 /// \brief Emit the function that initializes C++ thread_local variables.
1117 void EmitCXXThreadLocalInitFunc();
1118
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001119 /// Emit the function that initializes C++ globals.
Anders Carlssonb8be93f2009-08-08 23:24:23 +00001120 void EmitCXXGlobalInitFunc();
Eli Friedman5866fe32010-01-08 00:50:11 +00001121
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001122 /// Emit the function that destroys C++ globals.
Daniel Dunbarfe06df42010-03-20 04:15:41 +00001123 void EmitCXXGlobalDtorFunc();
1124
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001125 /// Emit the function that initializes the specified global (if PerformInit is
1126 /// true) and registers its destructor.
John McCallcdf7ef52010-11-06 09:44:32 +00001127 void EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
Richard Smith6331c402012-02-13 22:16:19 +00001128 llvm::GlobalVariable *Addr,
1129 bool PerformInit);
Eli Friedman5866fe32010-01-08 00:50:11 +00001130
Reid Kleckner1a711b12014-07-22 00:53:05 +00001131 void EmitPointerToInitFunc(const VarDecl *VD, llvm::GlobalVariable *Addr,
1132 llvm::Function *InitFunc, InitSegAttr *ISA);
1133
Daniel Dunbar74aa7e12008-08-01 00:01:51 +00001134 // FIXME: Hardcoding priority here is gross.
Reid Kleckner563f0e82014-05-23 21:13:45 +00001135 void AddGlobalCtor(llvm::Function *Ctor, int Priority = 65535,
1136 llvm::Constant *AssociatedData = 0);
1137 void AddGlobalDtor(llvm::Function *Dtor, int Priority = 65535);
Daniel Dunbar9c426522008-07-29 23:18:29 +00001138
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001139 /// Generates a global array of functions and priorities using the given list
1140 /// and name. This array will have appending linkage and is suitable for use
1141 /// as a LLVM constructor or destructor array.
Daniel Dunbar74aa7e12008-08-01 00:01:51 +00001142 void EmitCtorList(const CtorList &Fns, const char *GlobalName);
1143
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001144 /// Emit the RTTI descriptors for the given type.
Rafael Espindolaadcc1d12010-03-27 02:52:14 +00001145 void EmitFundamentalRTTIDescriptor(QualType Type);
1146
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001147 /// Emit any needed decls for which code generation was deferred.
Dmitri Gribenkob2aa9232012-11-15 14:28:07 +00001148 void EmitDeferred();
Daniel Dunbar08b26a02009-02-13 20:29:50 +00001149
Rafael Espindola2e2995b2013-11-05 21:37:29 +00001150 /// Call replaceAllUsesWith on all pairs in Replacements.
1151 void applyReplacements();
1152
Rafael Espindola208b5c02013-10-22 19:26:13 +00001153 void checkAliases();
1154
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001155 /// Emit any vtables which we deferred and still have a use for.
John McCall6bd2a892013-01-25 22:31:03 +00001156 void EmitDeferredVTables();
1157
Rafael Espindola060062a2014-03-06 22:15:10 +00001158 /// Emit the llvm.used and llvm.compiler.used metadata.
1159 void emitLLVMUsed();
Daniel Dunbar23fd4622008-10-01 00:49:24 +00001160
Douglas Gregorbc25ff42013-01-14 20:53:57 +00001161 /// \brief Emit the link options introduced by imported modules.
1162 void EmitModuleLinkOptions();
1163
Richard Smithdf931ce2013-04-06 05:00:46 +00001164 /// \brief Emit aliases for internal-linkage declarations inside "C" language
1165 /// linkage specifications, giving them the "expected" name where possible.
1166 void EmitStaticExternCAliases();
1167
John McCall09ae0322010-07-06 23:57:41 +00001168 void EmitDeclMetadata();
1169
Rafael Espindola3bfa4682013-10-16 19:28:50 +00001170 /// \brief Emit the Clang version as llvm.ident metadata.
1171 void EmitVersionIdentMetadata();
1172
Robert Lytton57765d52014-07-03 09:30:33 +00001173 /// Emits target specific Metadata for global declarations.
1174 void EmitTargetMetadata();
1175
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001176 /// Emit the llvm.gcov metadata used to tell LLVM where to emit the .gcno and
1177 /// .gcda files in a way that persists in .bc files.
Nick Lewycky85c011d2011-05-05 00:08:20 +00001178 void EmitCoverageFile();
Nick Lewycky480cb992011-05-04 20:46:58 +00001179
Nico Webercf4ff5862012-10-11 10:13:44 +00001180 /// Emits the initializer for a uuidof string.
Nico Weber17d3a2c2014-09-08 16:26:36 +00001181 llvm::Constant *EmitUuidofInitializer(StringRef uuidstr);
Nico Webercf4ff5862012-10-11 10:13:44 +00001182
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001183 /// Determine if the given decl can be emitted lazily; this is only relevant
1184 /// for definitions. The given decl must be either a function or var decl.
Daniel Dunbar6b8720e2009-02-13 21:18:01 +00001185 bool MayDeferGeneration(const ValueDecl *D);
John McCall0bdb1fd2010-09-16 06:16:50 +00001186
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001187 /// Check whether we can use a "simpler", more core exceptions personality
1188 /// function.
John McCall0bdb1fd2010-09-16 06:16:50 +00001189 void SimplifyPersonality();
Chris Lattnerf97fe382007-05-24 06:29:05 +00001190};
1191} // end namespace CodeGen
1192} // end namespace clang
Chris Lattnerf97fe382007-05-24 06:29:05 +00001193
1194#endif