blob: c54f6dea521fb600dce1c332b9a4081ea12a7647 [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"
Dan Gohman4f8d1232008-05-22 00:50:06 +000019#include "clang/AST/Attr.h"
Anders Carlsson2a131fb2009-05-05 04:44:02 +000020#include "clang/AST/DeclCXX.h"
Anders Carlsson0ff8baf2009-09-11 00:07:24 +000021#include "clang/AST/DeclObjC.h"
Peter Collingbournefd05ca02011-06-14 04:02:39 +000022#include "clang/AST/GlobalDecl.h"
Peter Collingbourne14110472011-01-13 18:57:25 +000023#include "clang/AST/Mangle.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000024#include "clang/Basic/ABI.h"
25#include "clang/Basic/LangOptions.h"
Douglas Gregorb6cbe512013-01-14 17:21:00 +000026#include "clang/Basic/Module.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000027#include "llvm/ADT/DenseMap.h"
Douglas Gregorb6cbe512013-01-14 17:21:00 +000028#include "llvm/ADT/SetVector.h"
Rafael Espindola6a836702010-03-04 18:17:24 +000029#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000030#include "llvm/ADT/StringMap.h"
John McCallbd7370a2013-02-28 19:01:20 +000031#include "llvm/IR/CallingConv.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000032#include "llvm/IR/Module.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070033#include "llvm/IR/ValueHandle.h"
Peter Collingbournea9679352013-07-09 22:03:30 +000034#include "llvm/Transforms/Utils/SpecialCaseList.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;
74class MangleBuffer;
75class Module;
Mike Stumpecc90e92009-02-13 19:12:34 +000076
Reid Spencer5f016e22007-07-11 17:01:13 +000077namespace CodeGen {
78
Stephen Hines6bcf27b2014-05-29 04:14:42 -070079class CallArgList;
80class CodeGenFunction;
81class CodeGenTBAA;
82class CGCXXABI;
83class CGDebugInfo;
84class CGObjCRuntime;
85class CGOpenCLRuntime;
86class CGOpenMPRuntime;
87class CGCUDARuntime;
88class BlockFieldFlags;
89class FunctionArgList;
Stephen Hines651f13c2014-04-23 16:59:28 -070090
Stephen Hines6bcf27b2014-05-29 04:14:42 -070091struct OrderGlobalInits {
92 unsigned int priority;
93 unsigned int lex_order;
94 OrderGlobalInits(unsigned int p, unsigned int l)
Chris Lattnerec2830d2010-06-27 06:32:58 +000095 : priority(p), lex_order(l) {}
Stephen Hines6bcf27b2014-05-29 04:14:42 -070096
97 bool operator==(const OrderGlobalInits &RHS) const {
98 return priority == RHS.priority && lex_order == RHS.lex_order;
99 }
100
101 bool operator<(const OrderGlobalInits &RHS) const {
102 return std::tie(priority, lex_order) <
103 std::tie(RHS.priority, RHS.lex_order);
104 }
105};
106
107struct CodeGenTypeCache {
108 /// void
109 llvm::Type *VoidTy;
110
111 /// i8, i16, i32, and i64
112 llvm::IntegerType *Int8Ty, *Int16Ty, *Int32Ty, *Int64Ty;
113 /// float, double
114 llvm::Type *FloatTy, *DoubleTy;
115
116 /// int
117 llvm::IntegerType *IntTy;
118
119 /// intptr_t, size_t, and ptrdiff_t, which we assume are the same size.
120 union {
121 llvm::IntegerType *IntPtrTy;
122 llvm::IntegerType *SizeTy;
123 llvm::IntegerType *PtrDiffTy;
Chris Lattnerec2830d2010-06-27 06:32:58 +0000124 };
John McCall5936e332011-02-15 09:22:45 +0000125
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700126 /// void* in address space 0
127 union {
128 llvm::PointerType *VoidPtrTy;
129 llvm::PointerType *Int8PtrTy;
John McCall5936e332011-02-15 09:22:45 +0000130 };
John McCallf85e1932011-06-15 23:02:42 +0000131
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700132 /// void** in address space 0
133 union {
134 llvm::PointerType *VoidPtrPtrTy;
135 llvm::PointerType *Int8PtrPtrTy;
136 };
137
138 /// The width of a pointer into the generic address space.
139 unsigned char PointerWidthInBits;
140
141 /// The size and alignment of a pointer into the generic address
142 /// space.
143 union {
144 unsigned char PointerAlignInBytes;
145 unsigned char PointerSizeInBytes;
146 unsigned char SizeSizeInBytes; // sizeof(size_t)
147 };
148
149 llvm::CallingConv::ID RuntimeCC;
150 llvm::CallingConv::ID getRuntimeCC() const { return RuntimeCC; }
151};
152
John McCallf85e1932011-06-15 23:02:42 +0000153struct RREntrypoints {
154 RREntrypoints() { memset(this, 0, sizeof(*this)); }
155 /// void objc_autoreleasePoolPop(void*);
156 llvm::Constant *objc_autoreleasePoolPop;
157
158 /// void *objc_autoreleasePoolPush(void);
159 llvm::Constant *objc_autoreleasePoolPush;
160};
161
162struct ARCEntrypoints {
163 ARCEntrypoints() { memset(this, 0, sizeof(*this)); }
164
165 /// id objc_autorelease(id);
166 llvm::Constant *objc_autorelease;
167
168 /// id objc_autoreleaseReturnValue(id);
169 llvm::Constant *objc_autoreleaseReturnValue;
170
171 /// void objc_copyWeak(id *dest, id *src);
172 llvm::Constant *objc_copyWeak;
173
174 /// void objc_destroyWeak(id*);
175 llvm::Constant *objc_destroyWeak;
176
177 /// id objc_initWeak(id*, id);
178 llvm::Constant *objc_initWeak;
179
180 /// id objc_loadWeak(id*);
181 llvm::Constant *objc_loadWeak;
182
183 /// id objc_loadWeakRetained(id*);
184 llvm::Constant *objc_loadWeakRetained;
185
186 /// void objc_moveWeak(id *dest, id *src);
187 llvm::Constant *objc_moveWeak;
188
189 /// id objc_retain(id);
190 llvm::Constant *objc_retain;
191
192 /// id objc_retainAutorelease(id);
193 llvm::Constant *objc_retainAutorelease;
194
195 /// id objc_retainAutoreleaseReturnValue(id);
196 llvm::Constant *objc_retainAutoreleaseReturnValue;
197
198 /// id objc_retainAutoreleasedReturnValue(id);
199 llvm::Constant *objc_retainAutoreleasedReturnValue;
200
201 /// id objc_retainBlock(id);
202 llvm::Constant *objc_retainBlock;
203
204 /// void objc_release(id);
205 llvm::Constant *objc_release;
206
207 /// id objc_storeStrong(id*, id);
208 llvm::Constant *objc_storeStrong;
209
210 /// id objc_storeWeak(id*, id);
211 llvm::Constant *objc_storeWeak;
212
213 /// A void(void) inline asm to use to mark that the return value of
214 /// a call will be immediately retain.
215 llvm::InlineAsm *retainAutoreleasedReturnValueMarker;
John McCallb6a60792013-03-23 02:35:54 +0000216
217 /// void clang.arc.use(...);
218 llvm::Constant *clang_arc_use;
John McCallf85e1932011-06-15 23:02:42 +0000219};
Will Dietz4f45bc02013-01-18 11:30:38 +0000220
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700221/// This class records statistics on instrumentation based profiling.
222struct InstrProfStats {
223 InstrProfStats() : Visited(0), Missing(0), Mismatched(0) {}
224 bool isOutOfDate() { return Missing || Mismatched; }
225 uint32_t Visited;
226 uint32_t Missing;
227 uint32_t Mismatched;
228};
229
230/// This class organizes the cross-function state that is used while generating
231/// LLVM code.
John McCall5936e332011-02-15 09:22:45 +0000232class CodeGenModule : public CodeGenTypeCache {
Dmitri Gribenkof56faa02012-09-15 20:20:27 +0000233 CodeGenModule(const CodeGenModule &) LLVM_DELETED_FUNCTION;
234 void operator=(const CodeGenModule &) LLVM_DELETED_FUNCTION;
Anders Carlsson8a219ce2009-02-24 04:21:31 +0000235
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700236 struct Structor {
237 Structor() : Priority(0), Initializer(nullptr), AssociatedData(nullptr) {}
238 Structor(int Priority, llvm::Constant *Initializer,
239 llvm::Constant *AssociatedData)
240 : Priority(Priority), Initializer(Initializer),
241 AssociatedData(AssociatedData) {}
242 int Priority;
243 llvm::Constant *Initializer;
244 llvm::Constant *AssociatedData;
245 };
246
247 typedef std::vector<Structor> CtorList;
Daniel Dunbar6bfed7e2008-08-01 00:01:51 +0000248
Reid Spencer5f016e22007-07-11 17:01:13 +0000249 ASTContext &Context;
David Blaikie4e4d0842012-03-11 07:00:24 +0000250 const LangOptions &LangOpts;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000251 const CodeGenOptions &CodeGenOpts;
Reid Spencer5f016e22007-07-11 17:01:13 +0000252 llvm::Module &TheModule;
David Blaikied6471f72011-09-25 23:23:43 +0000253 DiagnosticsEngine &Diags;
John McCall64aa4b32013-04-16 22:48:15 +0000254 const llvm::DataLayout &TheDataLayout;
255 const TargetInfo &Target;
Stephen Hines651f13c2014-04-23 16:59:28 -0700256 std::unique_ptr<CGCXXABI> ABI;
John McCall64aa4b32013-04-16 22:48:15 +0000257 llvm::LLVMContext &VMContext;
Anders Carlssonb5404912009-10-07 01:06:45 +0000258
John McCall64aa4b32013-04-16 22:48:15 +0000259 CodeGenTBAA *TBAA;
260
261 mutable const TargetCodeGenInfo *TheTargetCodeGenInfo;
262
263 // This should not be moved earlier, since its initialization depends on some
264 // of the previous reference members being already initialized and also checks
265 // if TheTargetCodeGenInfo is NULL
266 CodeGenTypes Types;
267
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700268 /// Holds information about C++ vtables.
Anders Carlssonaf440352010-03-23 04:11:45 +0000269 CodeGenVTables VTables;
Douglas Gregore17ad2f2010-04-08 16:07:47 +0000270
Peter Collingbournee9265232011-07-27 20:29:46 +0000271 CGObjCRuntime* ObjCRuntime;
Peter Collingbourne8c25fc52011-09-19 21:14:35 +0000272 CGOpenCLRuntime* OpenCLRuntime;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700273 CGOpenMPRuntime* OpenMPRuntime;
Peter Collingbourne6c0aa5f2011-10-06 18:29:37 +0000274 CGCUDARuntime* CUDARuntime;
Ted Kremenek815c78f2008-08-05 18:50:11 +0000275 CGDebugInfo* DebugInfo;
John McCallf85e1932011-06-15 23:02:42 +0000276 ARCEntrypoints *ARCData;
Dan Gohmanb49bd272012-02-16 00:57:37 +0000277 llvm::MDNode *NoObjCARCExceptionsMetadata;
John McCallf85e1932011-06-15 23:02:42 +0000278 RREntrypoints *RRData;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700279 std::unique_ptr<llvm::IndexedInstrProfReader> PGOReader;
280 InstrProfStats PGOStats;
Daniel Dunbar9986eab2008-07-30 16:32:24 +0000281
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700282 // A set of references that have only been seen via a weakref so far. This is
283 // used to remove the weak of the reference if we ever see a direct reference
284 // or a definition.
Rafael Espindola6a836702010-03-04 18:17:24 +0000285 llvm::SmallPtrSet<llvm::GlobalValue*, 10> WeakRefReferences;
286
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700287 /// This contains all the decls which have definitions but/ which are deferred
288 /// for emission and therefore should only be output if they are actually
289 /// used. If a decl is in this, then it is known to have not been referenced
290 /// yet.
John McCallf746aa62010-03-19 23:29:14 +0000291 llvm::StringMap<GlobalDecl> DeferredDecls;
Daniel Dunbar02698712009-02-13 20:29:50 +0000292
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700293 /// This is a list of deferred decls which we have seen that *are* actually
294 /// referenced. These get code generated when the module is done.
Stephen Hines651f13c2014-04-23 16:59:28 -0700295 struct DeferredGlobal {
296 DeferredGlobal(llvm::GlobalValue *GV, GlobalDecl GD) : GV(GV), GD(GD) {}
297 llvm::AssertingVH<llvm::GlobalValue> GV;
298 GlobalDecl GD;
299 };
300 std::vector<DeferredGlobal> DeferredDeclsToEmit;
301 void addDeferredDeclToEmit(llvm::GlobalValue *GV, GlobalDecl GD) {
302 DeferredDeclsToEmit.push_back(DeferredGlobal(GV, GD));
303 }
Daniel Dunbar03f5ad92009-04-15 22:08:45 +0000304
Rafael Espindolad2054982013-10-22 19:26:13 +0000305 /// List of alias we have emitted. Used to make sure that what they point to
306 /// is defined once we get to the end of the of the translation unit.
307 std::vector<GlobalDecl> Aliases;
308
Rafael Espindola61a0a752013-11-05 21:37:29 +0000309 typedef llvm::StringMap<llvm::TrackingVH<llvm::Constant> > ReplacementsTy;
310 ReplacementsTy Replacements;
311
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700312 /// A queue of (optional) vtables to consider emitting.
John McCalld5617ee2013-01-25 22:31:03 +0000313 std::vector<const CXXRecordDecl*> DeferredVTables;
314
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700315 /// List of global values which are required to be present in the object file;
316 /// bitcast to i8*. This is used for forcing visibility of symbols which may
317 /// otherwise be optimized out.
Chris Lattner35f38a22009-03-31 22:37:52 +0000318 std::vector<llvm::WeakVH> LLVMUsed;
Stephen Hines651f13c2014-04-23 16:59:28 -0700319 std::vector<llvm::WeakVH> LLVMCompilerUsed;
Mike Stumpecc90e92009-02-13 19:12:34 +0000320
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700321 /// Store the list of global constructors and their respective priorities to
322 /// be emitted when the translation unit is complete.
Daniel Dunbar6bfed7e2008-08-01 00:01:51 +0000323 CtorList GlobalCtors;
324
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700325 /// Store the list of global destructors and their respective priorities to be
326 /// emitted when the translation unit is complete.
Daniel Dunbar6bfed7e2008-08-01 00:01:51 +0000327 CtorList GlobalDtors;
328
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700329 /// A map of canonical GlobalDecls to their mangled names.
Chris Lattner686775d2011-07-20 06:58:45 +0000330 llvm::DenseMap<GlobalDecl, StringRef> MangledDeclNames;
Anders Carlsson793a9902010-06-22 16:05:32 +0000331 llvm::BumpPtrAllocator MangledNamesAllocator;
332
Julien Lerouge77f68bb2011-09-09 22:41:49 +0000333 /// Global annotations.
Nate Begeman532485c2008-04-18 23:43:57 +0000334 std::vector<llvm::Constant*> Annotations;
Mike Stumpecc90e92009-02-13 19:12:34 +0000335
Julien Lerouge77f68bb2011-09-09 22:41:49 +0000336 /// Map used to get unique annotation strings.
337 llvm::StringMap<llvm::Constant*> AnnotationStrings;
338
Anders Carlssonc9e20912007-08-21 00:21:21 +0000339 llvm::StringMap<llvm::Constant*> CFConstantStringMap;
Stephen Hines651f13c2014-04-23 16:59:28 -0700340
341 llvm::StringMap<llvm::GlobalVariable *> Constant1ByteStringMap;
342 llvm::StringMap<llvm::GlobalVariable *> Constant2ByteStringMap;
343 llvm::StringMap<llvm::GlobalVariable *> Constant4ByteStringMap;
Eli Friedman71cba342012-03-09 03:27:46 +0000344 llvm::DenseMap<const Decl*, llvm::Constant *> StaticLocalDeclMap;
John McCall355bba72012-03-30 21:00:39 +0000345 llvm::DenseMap<const Decl*, llvm::GlobalVariable*> StaticLocalDeclGuardMap;
Richard Smith211c8dd2013-06-05 00:46:14 +0000346 llvm::DenseMap<const Expr*, llvm::Constant *> MaterializedGlobalTemporaryMap;
347
Fariborz Jahanianb08cfb32012-01-08 19:13:23 +0000348 llvm::DenseMap<QualType, llvm::Constant *> AtomicSetterHelperFnMap;
349 llvm::DenseMap<QualType, llvm::Constant *> AtomicGetterHelperFnMap;
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000350
Will Dietz562d45c2013-11-08 01:09:22 +0000351 /// Map used to get unique type descriptor constants for sanitizers.
352 llvm::DenseMap<QualType, llvm::Constant *> TypeDescriptorMap;
353
Richard Smith00249372013-04-06 05:00:46 +0000354 /// Map used to track internal linkage functions declared within
355 /// extern "C" regions.
Richard Smith84083b72013-04-13 01:28:18 +0000356 typedef llvm::MapVector<IdentifierInfo *,
357 llvm::GlobalValue *> StaticExternCMap;
Richard Smith00249372013-04-06 05:00:46 +0000358 StaticExternCMap StaticExternCValues;
359
Richard Smithb80a16e2013-04-19 16:42:07 +0000360 /// \brief thread_local variables defined or used in this TU.
361 std::vector<std::pair<const VarDecl *, llvm::GlobalVariable *> >
362 CXXThreadLocals;
363
364 /// \brief thread_local variables with initializers that need to run
365 /// before any thread_local variable in this TU is odr-used.
366 std::vector<llvm::Constant*> CXXThreadLocalInits;
367
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700368 /// Global variables with initializers that need to run before main.
Eli Friedman6c6bda32010-01-08 00:50:11 +0000369 std::vector<llvm::Constant*> CXXGlobalInits;
John McCallbf40cb52010-07-15 23:40:35 +0000370
371 /// When a C++ decl with an initializer is deferred, null is
372 /// appended to CXXGlobalInits, and the index of that null is placed
373 /// here so that the initializer will be performed in the correct
374 /// order.
375 llvm::DenseMap<const Decl*, unsigned> DelayedCXXInitPosition;
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000376
Anton Korobeynikov4179ddd2012-11-06 22:44:45 +0000377 typedef std::pair<OrderGlobalInits, llvm::Function*> GlobalInitData;
378
379 struct GlobalInitPriorityCmp {
380 bool operator()(const GlobalInitData &LHS,
381 const GlobalInitData &RHS) const {
382 return LHS.first.priority < RHS.first.priority;
383 }
384 };
385
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700386 /// Global variables with initializers whose order of initialization is set by
387 /// init_priority attribute.
Anton Korobeynikov4179ddd2012-11-06 22:44:45 +0000388 SmallVector<GlobalInitData, 8> PrioritizedCXXGlobalInits;
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700390 /// Global destructor functions and arguments that need to run on termination.
Chris Lattner810112e2010-06-19 05:52:45 +0000391 std::vector<std::pair<llvm::WeakVH,llvm::Constant*> > CXXGlobalDtors;
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000392
Douglas Gregorb6cbe512013-01-14 17:21:00 +0000393 /// \brief The complete set of modules that has been imported.
394 llvm::SetVector<clang::Module *> ImportedModules;
395
Reid Kleckner3190ca92013-05-08 13:44:39 +0000396 /// \brief A vector of metadata strings.
397 SmallVector<llvm::Value *, 16> LinkerOptionsMetadata;
398
Douglas Gregor45c4ea72011-08-09 15:54:21 +0000399 /// @name Cache for Objective-C runtime types
400 /// @{
401
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700402 /// Cached reference to the class for constant strings. This value has type
403 /// int * but is actually an Obj-C class pointer.
Fariborz Jahanian428edb72013-04-16 15:25:39 +0000404 llvm::WeakVH CFConstantStringClassRef;
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700406 /// Cached reference to the class for constant strings. This value has type
407 /// int * but is actually an Obj-C class pointer.
Fariborz Jahanian428edb72013-04-16 15:25:39 +0000408 llvm::WeakVH ConstantStringClassRef;
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +0000409
Douglas Gregor45c4ea72011-08-09 15:54:21 +0000410 /// \brief The LLVM type corresponding to NSConstantString.
411 llvm::StructType *NSConstantStringType;
412
Douglas Gregor0815b572011-08-09 17:23:49 +0000413 /// \brief The type used to describe the state of a fast enumeration in
414 /// Objective-C's for..in loop.
415 QualType ObjCFastEnumerationStateType;
416
Douglas Gregor45c4ea72011-08-09 15:54:21 +0000417 /// @}
418
David Chisnall0d13f6f2010-01-23 02:40:42 +0000419 /// Lazily create the Objective-C runtime
420 void createObjCRuntime();
421
Peter Collingbourne8c25fc52011-09-19 21:14:35 +0000422 void createOpenCLRuntime();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700423 void createOpenMPRuntime();
Peter Collingbourne6c0aa5f2011-10-06 18:29:37 +0000424 void createCUDARuntime();
Peter Collingbourne8c25fc52011-09-19 21:14:35 +0000425
Rafael Espindolabcf6b982011-12-19 14:41:01 +0000426 bool isTriviallyRecursive(const FunctionDecl *F);
Peter Collingbourne144a31f2013-06-05 17:49:37 +0000427 bool shouldEmitFunction(GlobalDecl GD);
Daniel Dunbar673431a2010-07-16 00:00:15 +0000428
429 /// @name Cache for Blocks Runtime Globals
430 /// @{
431
432 llvm::Constant *NSConcreteGlobalBlock;
433 llvm::Constant *NSConcreteStackBlock;
Daniel Dunbar754b9fb2010-07-16 00:00:19 +0000434
Daniel Dunbar673431a2010-07-16 00:00:15 +0000435 llvm::Constant *BlockObjectAssign;
436 llvm::Constant *BlockObjectDispose;
437
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000438 llvm::Type *BlockDescriptorType;
439 llvm::Type *GenericBlockLiteralType;
John McCalld16c2cf2011-02-08 08:22:06 +0000440
441 struct {
442 int GlobalUniqueCount;
443 } Block;
Will Dietz4f45bc02013-01-18 11:30:38 +0000444
Nadav Rotem495cfa42013-03-23 06:43:35 +0000445 /// void @llvm.lifetime.start(i64 %size, i8* nocapture <ptr>)
446 llvm::Constant *LifetimeStartFn;
447
448 /// void @llvm.lifetime.end(i64 %size, i8* nocapture <ptr>)
449 llvm::Constant *LifetimeEndFn;
450
Fariborz Jahanian4904bf42012-06-26 16:06:38 +0000451 GlobalDecl initializedGlobalDecl;
John McCalld16c2cf2011-02-08 08:22:06 +0000452
Stephen Hines651f13c2014-04-23 16:59:28 -0700453 std::unique_ptr<llvm::SpecialCaseList> SanitizerBlacklist;
Will Dietz4f45bc02013-01-18 11:30:38 +0000454
455 const SanitizerOptions &SanOpts;
456
Daniel Dunbar673431a2010-07-16 00:00:15 +0000457 /// @}
Reid Spencer5f016e22007-07-11 17:01:13 +0000458public:
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000459 CodeGenModule(ASTContext &C, const CodeGenOptions &CodeGenOpts,
John McCall3abae092013-04-16 22:48:20 +0000460 llvm::Module &M, const llvm::DataLayout &TD,
461 DiagnosticsEngine &Diags);
Ted Kremenek815c78f2008-08-05 18:50:11 +0000462
Chris Lattner2b94fe32008-03-01 08:45:05 +0000463 ~CodeGenModule();
Mike Stumpecc90e92009-02-13 19:12:34 +0000464
Stephen Hines651f13c2014-04-23 16:59:28 -0700465 void clear();
466
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700467 /// Finalize LLVM code generation.
Ted Kremenek815c78f2008-08-05 18:50:11 +0000468 void Release();
Daniel Dunbar208ff5e2008-08-11 18:12:00 +0000469
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700470 /// Return a reference to the configured Objective-C runtime.
Mike Stumpecc90e92009-02-13 19:12:34 +0000471 CGObjCRuntime &getObjCRuntime() {
Peter Collingbournee9265232011-07-27 20:29:46 +0000472 if (!ObjCRuntime) createObjCRuntime();
473 return *ObjCRuntime;
Daniel Dunbar208ff5e2008-08-11 18:12:00 +0000474 }
Mike Stumpecc90e92009-02-13 19:12:34 +0000475
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700476 /// Return true iff an Objective-C runtime has been configured.
Peter Collingbournee9265232011-07-27 20:29:46 +0000477 bool hasObjCRuntime() { return !!ObjCRuntime; }
Daniel Dunbar208ff5e2008-08-11 18:12:00 +0000478
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700479 /// Return a reference to the configured OpenCL runtime.
Peter Collingbourne8c25fc52011-09-19 21:14:35 +0000480 CGOpenCLRuntime &getOpenCLRuntime() {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700481 assert(OpenCLRuntime != nullptr);
Peter Collingbourne8c25fc52011-09-19 21:14:35 +0000482 return *OpenCLRuntime;
483 }
484
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700485 /// Return a reference to the configured OpenMP runtime.
486 CGOpenMPRuntime &getOpenMPRuntime() {
487 assert(OpenMPRuntime != nullptr);
488 return *OpenMPRuntime;
489 }
490
491 /// Return a reference to the configured CUDA runtime.
Peter Collingbourne6c0aa5f2011-10-06 18:29:37 +0000492 CGCUDARuntime &getCUDARuntime() {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700493 assert(CUDARuntime != nullptr);
Peter Collingbourne6c0aa5f2011-10-06 18:29:37 +0000494 return *CUDARuntime;
495 }
496
John McCallf85e1932011-06-15 23:02:42 +0000497 ARCEntrypoints &getARCEntrypoints() const {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700498 assert(getLangOpts().ObjCAutoRefCount && ARCData != nullptr);
John McCallf85e1932011-06-15 23:02:42 +0000499 return *ARCData;
500 }
501
502 RREntrypoints &getRREntrypoints() const {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700503 assert(RRData != nullptr);
John McCallf85e1932011-06-15 23:02:42 +0000504 return *RRData;
505 }
506
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700507 InstrProfStats &getPGOStats() { return PGOStats; }
508 llvm::IndexedInstrProfReader *getPGOReader() const { return PGOReader.get(); }
Stephen Hines651f13c2014-04-23 16:59:28 -0700509
Eli Friedman71cba342012-03-09 03:27:46 +0000510 llvm::Constant *getStaticLocalDeclAddress(const VarDecl *D) {
511 return StaticLocalDeclMap[D];
Fariborz Jahanian65ad5a42010-04-18 21:01:23 +0000512 }
Fariborz Jahanian63326a52010-04-19 18:15:02 +0000513 void setStaticLocalDeclAddress(const VarDecl *D,
Eli Friedman71cba342012-03-09 03:27:46 +0000514 llvm::Constant *C) {
515 StaticLocalDeclMap[D] = C;
Fariborz Jahanian65ad5a42010-04-18 21:01:23 +0000516 }
517
John McCall355bba72012-03-30 21:00:39 +0000518 llvm::GlobalVariable *getStaticLocalDeclGuardAddress(const VarDecl *D) {
519 return StaticLocalDeclGuardMap[D];
520 }
521 void setStaticLocalDeclGuardAddress(const VarDecl *D,
522 llvm::GlobalVariable *C) {
523 StaticLocalDeclGuardMap[D] = C;
524 }
525
Fariborz Jahanianb08cfb32012-01-08 19:13:23 +0000526 llvm::Constant *getAtomicSetterHelperFnMap(QualType Ty) {
527 return AtomicSetterHelperFnMap[Ty];
Fariborz Jahaniancd93b962012-01-06 22:33:54 +0000528 }
Fariborz Jahanianb08cfb32012-01-08 19:13:23 +0000529 void setAtomicSetterHelperFnMap(QualType Ty,
Fariborz Jahaniancd93b962012-01-06 22:33:54 +0000530 llvm::Constant *Fn) {
Fariborz Jahanianb08cfb32012-01-08 19:13:23 +0000531 AtomicSetterHelperFnMap[Ty] = Fn;
532 }
533
534 llvm::Constant *getAtomicGetterHelperFnMap(QualType Ty) {
535 return AtomicGetterHelperFnMap[Ty];
536 }
537 void setAtomicGetterHelperFnMap(QualType Ty,
538 llvm::Constant *Fn) {
539 AtomicGetterHelperFnMap[Ty] = Fn;
Fariborz Jahaniancd93b962012-01-06 22:33:54 +0000540 }
541
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700542 llvm::Constant *getTypeDescriptorFromMap(QualType Ty) {
Will Dietz562d45c2013-11-08 01:09:22 +0000543 return TypeDescriptorMap[Ty];
544 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700545 void setTypeDescriptorInMap(QualType Ty, llvm::Constant *C) {
Will Dietz562d45c2013-11-08 01:09:22 +0000546 TypeDescriptorMap[Ty] = C;
547 }
548
Devang Patelaa112892011-03-07 18:45:56 +0000549 CGDebugInfo *getModuleDebugInfo() { return DebugInfo; }
Devang Patel5de7a0e2011-03-07 18:29:53 +0000550
Dan Gohmanb49bd272012-02-16 00:57:37 +0000551 llvm::MDNode *getNoObjCARCExceptionsMetadata() {
552 if (!NoObjCARCExceptionsMetadata)
553 NoObjCARCExceptionsMetadata =
554 llvm::MDNode::get(getLLVMContext(),
555 SmallVector<llvm::Value*,1>());
556 return NoObjCARCExceptionsMetadata;
557 }
558
Reid Spencer5f016e22007-07-11 17:01:13 +0000559 ASTContext &getContext() const { return Context; }
David Blaikie4e4d0842012-03-11 07:00:24 +0000560 const LangOptions &getLangOpts() const { return LangOpts; }
John McCall64aa4b32013-04-16 22:48:15 +0000561 const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
Reid Spencer5f016e22007-07-11 17:01:13 +0000562 llvm::Module &getModule() const { return TheModule; }
David Blaikied6471f72011-09-25 23:23:43 +0000563 DiagnosticsEngine &getDiags() const { return Diags; }
Micah Villmow25a6a842012-10-08 16:25:52 +0000564 const llvm::DataLayout &getDataLayout() const { return TheDataLayout; }
John McCall64aa4b32013-04-16 22:48:15 +0000565 const TargetInfo &getTarget() const { return Target; }
Stephen Hines651f13c2014-04-23 16:59:28 -0700566 CGCXXABI &getCXXABI() const { return *ABI; }
Owen Andersona1cf15f2009-07-14 23:10:40 +0000567 llvm::LLVMContext &getLLVMContext() { return VMContext; }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700568
569 bool shouldUseTBAA() const { return TBAA != nullptr; }
John McCallbc7fbf02011-02-26 08:07:02 +0000570
John McCall64aa4b32013-04-16 22:48:15 +0000571 const TargetCodeGenInfo &getTargetCodeGenInfo();
572
573 CodeGenTypes &getTypes() { return Types; }
574
575 CodeGenVTables &getVTables() { return VTables; }
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +0000576
Timur Iskhodzhanov5f0db582013-11-05 15:54:58 +0000577 ItaniumVTableContext &getItaniumVTableContext() {
578 return VTables.getItaniumVTableContext();
Timur Iskhodzhanovf0746582013-10-09 11:33:51 +0000579 }
John McCall64aa4b32013-04-16 22:48:15 +0000580
Timur Iskhodzhanov5f0db582013-11-05 15:54:58 +0000581 MicrosoftVTableContext &getMicrosoftVTableContext() {
582 return VTables.getMicrosoftVTableContext();
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +0000583 }
584
Dan Gohman3d5aff52010-10-14 23:06:10 +0000585 llvm::MDNode *getTBAAInfo(QualType QTy);
Kostya Serebryany8cb4a072012-03-26 17:03:51 +0000586 llvm::MDNode *getTBAAInfoForVTablePtr();
Dan Gohmanb22c7dc2012-09-28 21:58:29 +0000587 llvm::MDNode *getTBAAStructInfo(QualType QTy);
Manman Renb37a73d2013-04-04 21:53:22 +0000588 /// Return the MDNode in the type DAG for the given struct type.
589 llvm::MDNode *getTBAAStructTypeInfo(QualType QTy);
590 /// Return the path-aware tag for given base type, access node and offset.
591 llvm::MDNode *getTBAAStructTagInfo(QualType BaseTy, llvm::MDNode *AccessN,
592 uint64_t O);
Dan Gohman3d5aff52010-10-14 23:06:10 +0000593
Richard Smitha9b21d22012-02-17 06:48:11 +0000594 bool isTypeConstant(QualType QTy, bool ExcludeCtorDtor);
595
John McCall9eda3ab2013-03-07 21:37:17 +0000596 bool isPaddedAtomicType(QualType type);
597 bool isPaddedAtomicType(const AtomicType *type);
598
Manman Renca835182013-04-11 23:02:56 +0000599 /// Decorate the instruction with a TBAA tag. For scalar TBAA, the tag
600 /// is the same as the type. For struct-path aware TBAA, the tag
601 /// is different from the type: base type, access type and offset.
602 /// When ConvertTypeToTag is true, we create a tag based on the scalar type.
603 void DecorateInstruction(llvm::Instruction *Inst,
604 llvm::MDNode *TBAAInfo,
605 bool ConvertTypeToTag = true);
Dan Gohman3d5aff52010-10-14 23:06:10 +0000606
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700607 /// Emit the given number of characters as a value of type size_t.
John McCallbc8d40d2011-06-24 21:55:10 +0000608 llvm::ConstantInt *getSize(CharUnits numChars);
609
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700610 /// Set the visibility for the given LLVM GlobalValue.
Anders Carlsson0ffeaad2011-01-29 19:39:23 +0000611 void setGlobalVisibility(llvm::GlobalValue *GV, const NamedDecl *D) const;
Daniel Dunbar04d40782009-04-14 06:00:08 +0000612
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700613 /// Set the TLS mode for the given LLVM GlobalVariable for the thread-local
614 /// variable declaration D.
Hans Wennborgde981f32012-06-28 08:01:44 +0000615 void setTLSMode(llvm::GlobalVariable *GV, const VarDecl &D) const;
616
Anders Carlssonc7e98fa2011-01-29 20:59:35 +0000617 static llvm::GlobalValue::VisibilityTypes GetLLVMVisibility(Visibility V) {
618 switch (V) {
619 case DefaultVisibility: return llvm::GlobalValue::DefaultVisibility;
620 case HiddenVisibility: return llvm::GlobalValue::HiddenVisibility;
621 case ProtectedVisibility: return llvm::GlobalValue::ProtectedVisibility;
622 }
623 llvm_unreachable("unknown visibility!");
Anders Carlssonc7e98fa2011-01-29 20:59:35 +0000624 }
625
John McCalld46f9852010-02-19 01:32:20 +0000626 llvm::Constant *GetAddrOfGlobal(GlobalDecl GD) {
627 if (isa<CXXConstructorDecl>(GD.getDecl()))
628 return GetAddrOfCXXConstructor(cast<CXXConstructorDecl>(GD.getDecl()),
629 GD.getCtorType());
630 else if (isa<CXXDestructorDecl>(GD.getDecl()))
631 return GetAddrOfCXXDestructor(cast<CXXDestructorDecl>(GD.getDecl()),
632 GD.getDtorType());
633 else if (isa<FunctionDecl>(GD.getDecl()))
634 return GetAddrOfFunction(GD);
635 else
636 return GetAddrOfGlobalVar(cast<VarDecl>(GD.getDecl()));
637 }
638
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700639 /// Will return a global variable of the given type. If a variable with a
640 /// different type already exists then a new variable with the right type
641 /// will be created and all uses of the old variable will be replaced with a
642 /// bitcast to the new variable.
Anders Carlsson3bd62022011-01-29 18:20:20 +0000643 llvm::GlobalVariable *
Chris Lattner686775d2011-07-20 06:58:45 +0000644 CreateOrReplaceCXXRuntimeVariable(StringRef Name, llvm::Type *Ty,
Anders Carlsson3bd62022011-01-29 18:20:20 +0000645 llvm::GlobalValue::LinkageTypes Linkage);
646
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700647 /// Return the address space of the underlying global variable for D, as
648 /// determined by its declaration. Normally this is the same as the address
649 /// space of D's type, but in CUDA, address spaces are associated with
650 /// declarations, not types.
Peter Collingbourne4dc34eb2012-05-20 21:08:35 +0000651 unsigned GetGlobalVarAddressSpace(const VarDecl *D, unsigned AddrSpace);
652
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700653 /// Return the llvm::Constant for the address of the given global variable.
654 /// If Ty is non-null and if the global doesn't exist, then it will be greated
655 /// with the specified type instead of whatever the normal requested type
656 /// would be.
Chris Lattner570585c2009-03-21 09:16:30 +0000657 llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D,
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700658 llvm::Type *Ty = nullptr);
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000659
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700660 /// Return the address of the given function. If Ty is non-null, then this
661 /// function will use the specified type if it has to create it.
Stephen Hines651f13c2014-04-23 16:59:28 -0700662 llvm::Constant *GetAddrOfFunction(GlobalDecl GD, llvm::Type *Ty = 0,
663 bool ForVTable = false,
664 bool DontDefer = false);
Daniel Dunbar61432932008-08-13 23:20:05 +0000665
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700666 /// Get the address of the RTTI descriptor for the given type.
John McCall9dffe6f2010-04-30 01:15:21 +0000667 llvm::Constant *GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH = false);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000668
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700669 /// Get the address of a uuid descriptor .
Nico Weberc5f80462012-10-11 10:13:44 +0000670 llvm::Constant *GetAddrOfUuidDescriptor(const CXXUuidofExpr* E);
671
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700672 /// Get the address of the thunk for the given global decl.
Anders Carlsson84c49e42011-02-06 17:15:43 +0000673 llvm::Constant *GetAddrOfThunk(GlobalDecl GD, const ThunkInfo &Thunk);
Anders Carlsson19879c92010-03-23 17:17:29 +0000674
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700675 /// Get a reference to the target of VD.
Rafael Espindola6a836702010-03-04 18:17:24 +0000676 llvm::Constant *GetWeakRefReference(const ValueDecl *VD);
677
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700678 /// Returns the offset from a derived class to a class. Returns null if the
679 /// offset is 0.
Anders Carlssona04efdf2010-04-24 21:23:59 +0000680 llvm::Constant *
681 GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl,
John McCallf871d0c2010-08-07 06:22:56 +0000682 CastExpr::path_const_iterator PathBegin,
683 CastExpr::path_const_iterator PathEnd);
John McCalld16c2cf2011-02-08 08:22:06 +0000684
John McCallf0c11f72011-03-31 08:03:29 +0000685 /// A pair of helper functions for a __block variable.
686 class ByrefHelpers : public llvm::FoldingSetNode {
687 public:
688 llvm::Constant *CopyHelper;
689 llvm::Constant *DisposeHelper;
690
691 /// The alignment of the field. This is important because
692 /// different offsets to the field within the byref struct need to
693 /// have different helper functions.
694 CharUnits Alignment;
695
696 ByrefHelpers(CharUnits alignment) : Alignment(alignment) {}
697 virtual ~ByrefHelpers();
698
699 void Profile(llvm::FoldingSetNodeID &id) const {
700 id.AddInteger(Alignment.getQuantity());
701 profileImpl(id);
702 }
703 virtual void profileImpl(llvm::FoldingSetNodeID &id) const = 0;
704
705 virtual bool needsCopy() const { return true; }
706 virtual void emitCopy(CodeGenFunction &CGF,
707 llvm::Value *dest, llvm::Value *src) = 0;
708
709 virtual bool needsDispose() const { return true; }
710 virtual void emitDispose(CodeGenFunction &CGF, llvm::Value *field) = 0;
711 };
712
713 llvm::FoldingSet<ByrefHelpers> ByrefHelpersCache;
John McCalld16c2cf2011-02-08 08:22:06 +0000714
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700715 /// Fetches the global unique block count.
John McCall8178df32011-02-22 22:38:33 +0000716 int getUniqueBlockCount() { return ++Block.GlobalUniqueCount; }
Fariborz Jahanian4904bf42012-06-26 16:06:38 +0000717
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700718 /// Fetches the type of a generic block descriptor.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000719 llvm::Type *getBlockDescriptorType();
John McCalld16c2cf2011-02-08 08:22:06 +0000720
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700721 /// The type of a generic block literal.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000722 llvm::Type *getGenericBlockLiteralType();
John McCalld16c2cf2011-02-08 08:22:06 +0000723
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700724 /// \brief Gets or a creats a Microsoft TypeDescriptor.
725 llvm::Constant *getMSTypeDescriptor(QualType Ty);
726 /// \brief Gets or a creats a Microsoft CompleteObjectLocator.
727 llvm::GlobalVariable *getMSCompleteObjectLocator(const CXXRecordDecl *RD,
728 const VPtrInfo *Info);
729
730 /// Gets the address of a block which requires no captures.
John McCalld16c2cf2011-02-08 08:22:06 +0000731 llvm::Constant *GetAddrOfGlobalBlock(const BlockExpr *BE, const char *);
Anders Carlssona04efdf2010-04-24 21:23:59 +0000732
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700733 /// Return a pointer to a constant CFString object for the given string.
Steve Naroff8d4141f2009-04-01 13:55:36 +0000734 llvm::Constant *GetAddrOfConstantCFString(const StringLiteral *Literal);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700735
736 /// Return a pointer to a constant NSString object for the given string. Or a
737 /// user defined String object as defined via
Fariborz Jahanian4c733072010-10-19 17:19:29 +0000738 /// -fconstant-string-class=class_name option.
739 llvm::Constant *GetAddrOfConstantString(const StringLiteral *Literal);
Chris Lattnera7ad98f2008-02-11 00:02:17 +0000740
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700741 /// Return a constant array for the given string.
Eli Friedman64f45a22011-11-01 02:23:42 +0000742 llvm::Constant *GetConstantArrayFromStringLiteral(const StringLiteral *E);
743
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700744 /// Return a pointer to a constant array for the given string literal.
Daniel Dunbar61432932008-08-13 23:20:05 +0000745 llvm::Constant *GetAddrOfConstantStringFromLiteral(const StringLiteral *S);
Daniel Dunbar1e049762008-08-10 20:25:57 +0000746
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700747 /// Return a pointer to a constant array for the given ObjCEncodeExpr node.
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000748 llvm::Constant *GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *);
Mike Stump1eb44332009-09-09 15:08:12 +0000749
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700750 /// Returns a pointer to a character array containing the literal. This
751 /// contents are exactly that of the given string, i.e. it will not be null
752 /// terminated automatically; see GetAddrOfConstantCString. Note that whether
753 /// the result is actually a pointer to an LLVM constant depends on
754 /// Feature.WriteableStrings.
Daniel Dunbar61432932008-08-13 23:20:05 +0000755 ///
756 /// The result has pointer to array type.
Daniel Dunbar5fabf9d2008-10-17 21:56:50 +0000757 ///
758 /// \param GlobalName If provided, the name to use for the global
759 /// (if one is created).
Chris Lattner686775d2011-07-20 06:58:45 +0000760 llvm::Constant *GetAddrOfConstantString(StringRef Str,
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700761 const char *GlobalName=nullptr,
Ulrich Weigand6b203512013-05-06 16:23:57 +0000762 unsigned Alignment=0);
Daniel Dunbar61432932008-08-13 23:20:05 +0000763
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700764 /// Returns a pointer to a character array containing the literal and a
765 /// terminating '\0' character. The result has pointer to array type.
Daniel Dunbar5fabf9d2008-10-17 21:56:50 +0000766 ///
Mike Stumpecc90e92009-02-13 19:12:34 +0000767 /// \param GlobalName If provided, the name to use for the global (if one is
768 /// created).
Daniel Dunbar5fabf9d2008-10-17 21:56:50 +0000769 llvm::Constant *GetAddrOfConstantCString(const std::string &str,
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700770 const char *GlobalName=nullptr,
Ulrich Weigand6b203512013-05-06 16:23:57 +0000771 unsigned Alignment=0);
Richard Smith7401cf52011-11-22 22:48:32 +0000772
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700773 /// Returns a pointer to a constant global variable for the given file-scope
774 /// compound literal expression.
Richard Smith7401cf52011-11-22 22:48:32 +0000775 llvm::Constant *GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr*E);
Richard Smith211c8dd2013-06-05 00:46:14 +0000776
777 /// \brief Returns a pointer to a global variable representing a temporary
778 /// with static or thread storage duration.
779 llvm::Constant *GetAddrOfGlobalTemporary(const MaterializeTemporaryExpr *E,
780 const Expr *Inner);
781
Douglas Gregor0815b572011-08-09 17:23:49 +0000782 /// \brief Retrieve the record type that describes the state of an
783 /// Objective-C fast enumeration loop (for..in).
784 QualType getObjCFastEnumerationStateType();
785
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700786 /// Return the address of the constructor of the given type.
787 llvm::GlobalValue *
788 GetAddrOfCXXConstructor(const CXXConstructorDecl *ctor, CXXCtorType ctorType,
789 const CGFunctionInfo *fnInfo = nullptr,
790 bool DontDefer = false);
Anders Carlsson27ae5362009-04-17 01:58:57 +0000791
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700792 /// Return the address of the constructor of the given type.
793 llvm::GlobalValue *
794 GetAddrOfCXXDestructor(const CXXDestructorDecl *dtor,
795 CXXDtorType dtorType,
796 const CGFunctionInfo *fnInfo = nullptr,
797 llvm::FunctionType *fnType = nullptr,
798 bool DontDefer = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000799
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700800 /// Given a builtin id for a function like "__builtin_fabsf", return a
801 /// Function* for "fabsf".
Daniel Dunbar34771b52009-09-14 04:33:21 +0000802 llvm::Value *getBuiltinLibFunction(const FunctionDecl *FD,
803 unsigned BuiltinID);
Daniel Dunbar61432932008-08-13 23:20:05 +0000804
Dmitri Gribenko55431692013-05-05 00:41:58 +0000805 llvm::Function *getIntrinsic(unsigned IID, ArrayRef<llvm::Type*> Tys = None);
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000806
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700807 /// Emit code for a single top level declaration.
Daniel Dunbar41071de2008-08-15 23:26:23 +0000808 void EmitTopLevelDecl(Decl *D);
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000809
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700810 /// Tell the consumer that this variable has been instantiated.
Rafael Espindola02503932012-03-08 15:51:03 +0000811 void HandleCXXStaticMemberVarInstantiation(VarDecl *VD);
Rafael Espindola234fe652012-03-05 10:54:55 +0000812
Richard Smith00249372013-04-06 05:00:46 +0000813 /// \brief If the declaration has internal linkage but is inside an
814 /// extern "C" linkage specification, prepare to emit an alias for it
815 /// to the expected name.
816 template<typename SomeDecl>
817 void MaybeHandleStaticInExternC(const SomeDecl *D, llvm::GlobalValue *GV);
818
Stephen Hines651f13c2014-04-23 16:59:28 -0700819 /// Add a global to a list to be added to the llvm.used metadata.
820 void addUsedGlobal(llvm::GlobalValue *GV);
821
822 /// Add a global to a list to be added to the llvm.compiler.used metadata.
823 void addCompilerUsedGlobal(llvm::GlobalValue *GV);
Daniel Dunbar02698712009-02-13 20:29:50 +0000824
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700825 /// Add a destructor and object to add to the C++ global destructor function.
Chris Lattner810112e2010-06-19 05:52:45 +0000826 void AddCXXDtorEntry(llvm::Constant *DtorFn, llvm::Constant *Object) {
827 CXXGlobalDtors.push_back(std::make_pair(DtorFn, Object));
828 }
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000829
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700830 /// Create a new runtime function with the specified type and name.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000831 llvm::Constant *CreateRuntimeFunction(llvm::FunctionType *Ty,
Chris Lattner686775d2011-07-20 06:58:45 +0000832 StringRef Name,
Bill Wendlingc4c62fd2013-01-31 00:30:05 +0000833 llvm::AttributeSet ExtraAttrs =
834 llvm::AttributeSet());
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700835 /// Create a new runtime global variable with the specified type and name.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000836 llvm::Constant *CreateRuntimeVariable(llvm::Type *Ty,
Chris Lattner686775d2011-07-20 06:58:45 +0000837 StringRef Name);
Daniel Dunbarf1968f22008-10-01 00:49:24 +0000838
Daniel Dunbar673431a2010-07-16 00:00:15 +0000839 ///@name Custom Blocks Runtime Interfaces
840 ///@{
841
842 llvm::Constant *getNSConcreteGlobalBlock();
843 llvm::Constant *getNSConcreteStackBlock();
844 llvm::Constant *getBlockObjectAssign();
845 llvm::Constant *getBlockObjectDispose();
846
847 ///@}
848
Nadav Rotem495cfa42013-03-23 06:43:35 +0000849 llvm::Constant *getLLVMLifetimeStartFn();
850 llvm::Constant *getLLVMLifetimeEndFn();
851
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700852 // Make sure that this type is translated.
Devang Patele80d5672011-03-23 16:29:39 +0000853 void UpdateCompletedType(const TagDecl *TD);
Daniel Dunbard60f2fb2009-02-17 18:43:32 +0000854
John McCall5808ce42011-02-03 08:15:49 +0000855 llvm::Constant *getMemberPointerConstant(const UnaryOperator *e);
856
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700857 /// Try to emit the initializer for the given declaration as a constant;
858 /// returns 0 if the expression cannot be emitted as a constant.
859 llvm::Constant *EmitConstantInit(const VarDecl &D,
860 CodeGenFunction *CGF = nullptr);
Richard Smith2d6a5672012-01-14 04:30:29 +0000861
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700862 /// Try to emit the given expression as a constant; returns 0 if the
863 /// expression cannot be emitted as a constant.
Anders Carlssone9352cc2009-04-08 04:48:15 +0000864 llvm::Constant *EmitConstantExpr(const Expr *E, QualType DestType,
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700865 CodeGenFunction *CGF = nullptr);
Daniel Dunbard60f2fb2009-02-17 18:43:32 +0000866
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700867 /// Emit the given constant value as a constant, in the type's scalar
868 /// representation.
Richard Smith2d6a5672012-01-14 04:30:29 +0000869 llvm::Constant *EmitConstantValue(const APValue &Value, QualType DestType,
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700870 CodeGenFunction *CGF = nullptr);
Richard Smith2d6a5672012-01-14 04:30:29 +0000871
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700872 /// Emit the given constant value as a constant, in the type's memory
873 /// representation.
Richard Smitha3ca41f2012-03-02 23:27:11 +0000874 llvm::Constant *EmitConstantValueForMemory(const APValue &Value,
875 QualType DestType,
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700876 CodeGenFunction *CGF = nullptr);
Richard Smitha3ca41f2012-03-02 23:27:11 +0000877
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700878 /// Return the result of value-initializing the given type, i.e. a null
879 /// expression of the given type. This is usually, but not always, an LLVM
880 /// null constant.
Eli Friedman0f593122009-04-13 21:47:26 +0000881 llvm::Constant *EmitNullConstant(QualType T);
882
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700883 /// Return a null constant appropriate for zero-initializing a base class with
884 /// the given type. This is usually, but not always, an LLVM null constant.
Eli Friedman2ed7cb62011-10-14 02:27:24 +0000885 llvm::Constant *EmitNullConstantForBase(const CXXRecordDecl *Record);
886
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700887 /// Emit a general error that something can't be done.
Chandler Carruth0f30a122012-03-30 19:44:53 +0000888 void Error(SourceLocation loc, StringRef error);
John McCall32096692011-03-18 02:56:14 +0000889
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700890 /// Print out an error that codegen doesn't support the specified stmt yet.
David Blaikie0a1c8622013-08-19 21:02:26 +0000891 void ErrorUnsupported(const Stmt *S, const char *Type);
Mike Stumpecc90e92009-02-13 19:12:34 +0000892
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700893 /// Print out an error that codegen doesn't support the specified decl yet.
David Blaikie0a1c8622013-08-19 21:02:26 +0000894 void ErrorUnsupported(const Decl *D, const char *Type);
Dan Gohman4f8d1232008-05-22 00:50:06 +0000895
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700896 /// Set the attributes on the LLVM function for the given decl and function
897 /// info. This applies attributes necessary for handling the ABI as well as
898 /// user specified attributes like section.
Daniel Dunbar0e4f40e2009-04-17 00:48:04 +0000899 void SetInternalFunctionAttributes(const Decl *D, llvm::Function *F,
900 const CGFunctionInfo &FI);
Daniel Dunbarf80519b2008-09-04 23:41:35 +0000901
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700902 /// Set the LLVM function attributes (sext, zext, etc).
Daniel Dunbar7dbd8192009-04-14 07:08:30 +0000903 void SetLLVMFunctionAttributes(const Decl *D,
904 const CGFunctionInfo &Info,
905 llvm::Function *F);
Daniel Dunbarb7688072008-09-10 00:41:16 +0000906
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700907 /// Set the LLVM function attributes which only apply to a function
908 /// definintion.
Daniel Dunbar7c65e992009-04-14 08:05:55 +0000909 void SetLLVMFunctionAttributesForDefinition(const Decl *D, llvm::Function *F);
910
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700911 /// Return true iff the given type uses 'sret' when used as a return type.
Daniel Dunbardacf9dd2010-07-14 23:39:36 +0000912 bool ReturnTypeUsesSRet(const CGFunctionInfo &FI);
913
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700914 /// Return true iff the given type uses an argument slot when 'sret' is used
915 /// as a return type.
Stephen Hines651f13c2014-04-23 16:59:28 -0700916 bool ReturnSlotInterferesWithArgs(const CGFunctionInfo &FI);
917
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700918 /// Return true iff the given type uses 'fpret' when used as a return type.
Daniel Dunbardacf9dd2010-07-14 23:39:36 +0000919 bool ReturnTypeUsesFPRet(QualType ResultType);
Daniel Dunbarb7688072008-09-10 00:41:16 +0000920
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700921 /// Return true iff the given type uses 'fp2ret' when used as a return type.
Anders Carlssoneea64802011-10-31 16:27:11 +0000922 bool ReturnTypeUsesFP2Ret(QualType ResultType);
923
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700924 /// Get the LLVM attributes and calling convention to use for a particular
925 /// function type.
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000926 ///
927 /// \param Info - The function type information.
928 /// \param TargetDecl - The decl these attributes are being constructed
929 /// for. If supplied the attributes applied to this decl may contribute to the
930 /// function attributes and calling convention.
931 /// \param PAL [out] - On return, the attribute list to use.
932 /// \param CallingConv [out] - On return, the LLVM calling convention to use.
Daniel Dunbar88b53962009-02-02 22:03:45 +0000933 void ConstructAttributeList(const CGFunctionInfo &Info,
934 const Decl *TargetDecl,
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000935 AttributeListType &PAL,
Bill Wendling94236e72013-02-22 00:13:35 +0000936 unsigned &CallingConv,
937 bool AttrOnCallSite);
Daniel Dunbarb7688072008-09-10 00:41:16 +0000938
Chris Lattner686775d2011-07-20 06:58:45 +0000939 StringRef getMangledName(GlobalDecl GD);
Peter Collingbourne14110472011-01-13 18:57:25 +0000940 void getBlockMangledName(GlobalDecl GD, MangleBuffer &Buffer,
941 const BlockDecl *BD);
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000942
943 void EmitTentativeDefinition(const VarDecl *D);
Mike Stumpf1216772009-07-31 18:25:34 +0000944
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000945 void EmitVTable(CXXRecordDecl *Class, bool DefinitionRequired);
946
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700947 /// Emit the RTTI descriptors for the builtin types.
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +0000948 void EmitFundamentalRTTIDescriptors();
949
Reid Kleckner3190ca92013-05-08 13:44:39 +0000950 /// \brief Appends Opts to the "Linker Options" metadata value.
951 void AppendLinkerOptions(StringRef Opts);
952
Benjamin Kramer785f1e72013-06-04 09:13:21 +0000953 /// \brief Appends a detect mismatch command to the linker options.
Aaron Ballmana7ff62f2013-06-04 02:07:14 +0000954 void AddDetectMismatch(StringRef Name, StringRef Value);
955
Reid Kleckner3190ca92013-05-08 13:44:39 +0000956 /// \brief Appends a dependent lib to the "Linker Options" metadata value.
957 void AddDependentLib(StringRef Lib);
958
Peter Collingbourne144a31f2013-06-05 17:49:37 +0000959 llvm::GlobalVariable::LinkageTypes getFunctionLinkage(GlobalDecl GD);
John McCalld46f9852010-02-19 01:32:20 +0000960
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700961 void setFunctionLinkage(GlobalDecl GD, llvm::Function *F) {
962 F->setLinkage(getFunctionLinkage(GD));
John McCall8b242332010-05-25 04:30:21 +0000963 }
964
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700965 /// \brief Returns the appropriate linkage for the TypeInfo struct for a type.
966 llvm::GlobalVariable::LinkageTypes getTypeInfoLinkage(QualType Ty);
967
968 /// 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
Peter Collingbournea9679352013-07-09 22:03:30 +00001011 const llvm::SpecialCaseList &getSanitizerBlacklist() const {
Alexey Samsonov6a4c5dc2013-08-12 11:48:05 +00001012 return *SanitizerBlacklist;
Will Dietz4f45bc02013-01-18 11:30:38 +00001013 }
1014
1015 const SanitizerOptions &getSanOpts() const { return SanOpts; }
1016
John McCalld5617ee2013-01-25 22:31:03 +00001017 void addDeferredVTable(const CXXRecordDecl *RD) {
1018 DeferredVTables.push_back(RD);
1019 }
1020
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001021 /// Emit code for a singal global function or var decl. Forward declarations
1022 /// are emitted lazily.
Reid Klecknera4130ba2013-07-22 13:51:44 +00001023 void EmitGlobal(GlobalDecl D);
1024
Chris Lattner9cd4fe42007-12-02 07:09:19 +00001025private:
Chris Lattner686775d2011-07-20 06:58:45 +00001026 llvm::GlobalValue *GetGlobalValue(StringRef Ref);
Mike Stump1eb44332009-09-09 15:08:12 +00001027
Stephen Hines651f13c2014-04-23 16:59:28 -07001028 llvm::Constant *
1029 GetOrCreateLLVMFunction(StringRef MangledName, llvm::Type *Ty, GlobalDecl D,
1030 bool ForVTable, bool DontDefer = false,
1031 llvm::AttributeSet ExtraAttrs = llvm::AttributeSet());
1032
Chris Lattner686775d2011-07-20 06:58:45 +00001033 llvm::Constant *GetOrCreateLLVMGlobal(StringRef MangledName,
Chris Lattner2acc6e32011-07-18 04:24:23 +00001034 llvm::PointerType *PTy,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001035 const VarDecl *D);
Mike Stump1eb44332009-09-09 15:08:12 +00001036
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001037 /// Set attributes which are common to any form of a global definition (alias,
1038 /// Objective-C method, function, global variable).
Daniel Dunbar7dbd8192009-04-14 07:08:30 +00001039 ///
Daniel Dunbar7c65e992009-04-14 08:05:55 +00001040 /// NOTE: This should only be called for definitions.
1041 void SetCommonAttributes(const Decl *D, llvm::GlobalValue *GV);
Daniel Dunbarf80519b2008-09-04 23:41:35 +00001042
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001043 void setNonAliasAttributes(const Decl *D, llvm::GlobalObject *GO);
Mike Stump1eb44332009-09-09 15:08:12 +00001044
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001045 /// Set attributes for a global definition.
1046 void setFunctionDefinitionAttributes(const FunctionDecl *D,
1047 llvm::Function *F);
1048
1049 /// Set function attributes for a function declaration.
Anders Carlssonb2bcf1c2010-02-06 02:44:09 +00001050 void SetFunctionAttributes(GlobalDecl GD,
Eli Friedmanc6c14d12009-05-26 01:22:57 +00001051 llvm::Function *F,
1052 bool IsIncompleteFunction);
Nuno Lopesd4cbda62008-06-08 15:45:52 +00001053
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001054 void EmitGlobalDefinition(GlobalDecl D, llvm::GlobalValue *GV = nullptr);
Daniel Dunbard5d31802009-02-19 07:15:39 +00001055
Stephen Hines651f13c2014-04-23 16:59:28 -07001056 void EmitGlobalFunctionDefinition(GlobalDecl GD, llvm::GlobalValue *GV);
Daniel Dunbarbd012ff2008-07-29 23:18:29 +00001057 void EmitGlobalVarDefinition(const VarDecl *D);
John McCallf746aa62010-03-19 23:29:14 +00001058 void EmitAliasDefinition(GlobalDecl GD);
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001059 void EmitObjCPropertyImplementations(const ObjCImplementationDecl *D);
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00001060 void EmitObjCIvarInitializations(ObjCImplementationDecl *D);
Fariborz Jahanian354e7122010-10-27 16:21:54 +00001061
Anders Carlsson95d4e5d2009-04-15 15:55:24 +00001062 // C++ related functions.
Mike Stump1eb44332009-09-09 15:08:12 +00001063
Rafael Espindola71fdc122013-11-04 18:38:59 +00001064 bool TryEmitDefinitionAsAlias(GlobalDecl Alias, GlobalDecl Target,
1065 bool InEveryTU);
John McCallc0bf4622010-02-23 00:48:20 +00001066 bool TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D);
John McCalld46f9852010-02-19 01:32:20 +00001067
Anders Carlsson984e0682009-04-01 00:58:25 +00001068 void EmitNamespace(const NamespaceDecl *D);
Anders Carlsson91e20dd2009-04-02 05:55:18 +00001069 void EmitLinkageSpec(const LinkageSpecDecl *D);
Adrian Prantl0a050f72013-05-09 23:16:27 +00001070 void CompleteDIClassType(const CXXMethodDecl* D);
Anders Carlsson95d4e5d2009-04-15 15:55:24 +00001071
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001072 /// Emit a single constructor with the given type from a C++ constructor Decl.
Anders Carlsson95d4e5d2009-04-15 15:55:24 +00001073 void EmitCXXConstructor(const CXXConstructorDecl *D, CXXCtorType Type);
Mike Stump1eb44332009-09-09 15:08:12 +00001074
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001075 /// Emit a single destructor with the given type from a C++ destructor Decl.
Anders Carlsson27ae5362009-04-17 01:58:57 +00001076 void EmitCXXDestructor(const CXXDestructorDecl *D, CXXDtorType Type);
Mike Stump1eb44332009-09-09 15:08:12 +00001077
Richard Smithb80a16e2013-04-19 16:42:07 +00001078 /// \brief Emit the function that initializes C++ thread_local variables.
1079 void EmitCXXThreadLocalInitFunc();
1080
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001081 /// Emit the function that initializes C++ globals.
Anders Carlsson89ed31d2009-08-08 23:24:23 +00001082 void EmitCXXGlobalInitFunc();
Eli Friedman6c6bda32010-01-08 00:50:11 +00001083
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001084 /// Emit the function that destroys C++ globals.
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00001085 void EmitCXXGlobalDtorFunc();
1086
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001087 /// Emit the function that initializes the specified global (if PerformInit is
1088 /// true) and registers its destructor.
John McCall3030eb82010-11-06 09:44:32 +00001089 void EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
Richard Smith7ca48502012-02-13 22:16:19 +00001090 llvm::GlobalVariable *Addr,
1091 bool PerformInit);
Eli Friedman6c6bda32010-01-08 00:50:11 +00001092
Daniel Dunbar6bfed7e2008-08-01 00:01:51 +00001093 // FIXME: Hardcoding priority here is gross.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001094 void AddGlobalCtor(llvm::Function *Ctor, int Priority = 65535,
1095 llvm::Constant *AssociatedData = 0);
1096 void AddGlobalDtor(llvm::Function *Dtor, int Priority = 65535);
Daniel Dunbarbd012ff2008-07-29 23:18:29 +00001097
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001098 /// Generates a global array of functions and priorities using the given list
1099 /// and name. This array will have appending linkage and is suitable for use
1100 /// as a LLVM constructor or destructor array.
Daniel Dunbar6bfed7e2008-08-01 00:01:51 +00001101 void EmitCtorList(const CtorList &Fns, const char *GlobalName);
1102
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001103 /// Emit the RTTI descriptors for the given type.
Rafael Espindolad1a5c312010-03-27 02:52:14 +00001104 void EmitFundamentalRTTIDescriptor(QualType Type);
1105
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001106 /// Emit any needed decls for which code generation was deferred.
Dmitri Gribenkoc4a77902012-11-15 14:28:07 +00001107 void EmitDeferred();
Daniel Dunbar02698712009-02-13 20:29:50 +00001108
Rafael Espindola61a0a752013-11-05 21:37:29 +00001109 /// Call replaceAllUsesWith on all pairs in Replacements.
1110 void applyReplacements();
1111
Rafael Espindolad2054982013-10-22 19:26:13 +00001112 void checkAliases();
1113
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001114 /// Emit any vtables which we deferred and still have a use for.
John McCalld5617ee2013-01-25 22:31:03 +00001115 void EmitDeferredVTables();
1116
Stephen Hines651f13c2014-04-23 16:59:28 -07001117 /// Emit the llvm.used and llvm.compiler.used metadata.
1118 void emitLLVMUsed();
Daniel Dunbarf1968f22008-10-01 00:49:24 +00001119
Douglas Gregor858afb32013-01-14 20:53:57 +00001120 /// \brief Emit the link options introduced by imported modules.
1121 void EmitModuleLinkOptions();
1122
Richard Smith00249372013-04-06 05:00:46 +00001123 /// \brief Emit aliases for internal-linkage declarations inside "C" language
1124 /// linkage specifications, giving them the "expected" name where possible.
1125 void EmitStaticExternCAliases();
1126
John McCall744016d2010-07-06 23:57:41 +00001127 void EmitDeclMetadata();
1128
Rafael Espindola9686f122013-10-16 19:28:50 +00001129 /// \brief Emit the Clang version as llvm.ident metadata.
1130 void EmitVersionIdentMetadata();
1131
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001132 /// Emit the llvm.gcov metadata used to tell LLVM where to emit the .gcno and
1133 /// .gcda files in a way that persists in .bc files.
Nick Lewycky3dc05412011-05-05 00:08:20 +00001134 void EmitCoverageFile();
Nick Lewycky5ea4f442011-05-04 20:46:58 +00001135
Nico Weberc5f80462012-10-11 10:13:44 +00001136 /// Emits the initializer for a uuidof string.
1137 llvm::Constant *EmitUuidofInitializer(StringRef uuidstr, QualType IIDType);
1138
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001139 /// Determine if the given decl can be emitted lazily; this is only relevant
1140 /// for definitions. The given decl must be either a function or var decl.
Daniel Dunbar73241df2009-02-13 21:18:01 +00001141 bool MayDeferGeneration(const ValueDecl *D);
John McCallb2593832010-09-16 06:16:50 +00001142
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001143 /// Check whether we can use a "simpler", more core exceptions personality
1144 /// function.
John McCallb2593832010-09-16 06:16:50 +00001145 void SimplifyPersonality();
Reid Spencer5f016e22007-07-11 17:01:13 +00001146};
1147} // end namespace CodeGen
1148} // end namespace clang
1149
1150#endif