blob: 44597bfaa9170c94a59b83d6393d3590377d628f [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
Chris Lattnerc18bfbb2008-02-29 17:10:38 +000014#ifndef CLANG_CODEGEN_CODEGENMODULE_H
15#define CLANG_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"
Dan Gohman75d69da2008-05-22 00:50:06 +000020#include "clang/AST/Attr.h"
Anders Carlssondae1abc2009-05-05 04:44:02 +000021#include "clang/AST/DeclCXX.h"
Anders Carlsson73fcc952009-09-11 00:07:24 +000022#include "clang/AST/DeclObjC.h"
Peter Collingbourneee781d52011-06-14 04:02:39 +000023#include "clang/AST/GlobalDecl.h"
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000024#include "clang/AST/Mangle.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000025#include "clang/Basic/ABI.h"
26#include "clang/Basic/LangOptions.h"
Douglas Gregor6ddfca92013-01-14 17:21:00 +000027#include "clang/Basic/Module.h"
Chris Lattnerb6984c42007-06-20 04:44:43 +000028#include "llvm/ADT/DenseMap.h"
Douglas Gregor6ddfca92013-01-14 17:21:00 +000029#include "llvm/ADT/SetVector.h"
Rafael Espindola2e42fec2010-03-04 18:17:24 +000030#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000031#include "llvm/ADT/StringMap.h"
John McCall882987f2013-02-28 19:01:20 +000032#include "llvm/IR/CallingConv.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000033#include "llvm/IR/Module.h"
Chandler Carruth61743af2014-03-04 11:18:19 +000034#include "llvm/IR/ValueHandle.h"
Anders Carlsson762e1622009-01-04 02:08:04 +000035
Chris Lattnerf97fe382007-05-24 06:29:05 +000036namespace llvm {
Rafael Espindola42ae7452014-05-09 00:57:59 +000037class Module;
38class Constant;
39class ConstantInt;
40class Function;
41class GlobalValue;
42class DataLayout;
43class FunctionType;
44class LLVMContext;
45class IndexedInstrProfReader;
Chris Lattner23b7eb62007-06-15 23:05:46 +000046}
47
Chris Lattnerf97fe382007-05-24 06:29:05 +000048namespace clang {
Rafael Espindola42ae7452014-05-09 00:57:59 +000049class 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;
Rafael Espindola42ae7452014-05-09 00:57:59 +000074class Module;
Mike Stump1676c5b2009-02-13 19:12:34 +000075
Chris Lattnerf97fe382007-05-24 06:29:05 +000076namespace CodeGen {
77
Rafael Espindola42ae7452014-05-09 00:57:59 +000078class CallArgList;
79class CodeGenFunction;
80class CodeGenTBAA;
81class CGCXXABI;
82class CGDebugInfo;
83class CGObjCRuntime;
84class CGOpenCLRuntime;
85class CGOpenMPRuntime;
86class CGCUDARuntime;
87class BlockFieldFlags;
88class FunctionArgList;
Justin Bogneref512b92014-01-06 22:27:43 +000089
Rafael Espindola42ae7452014-05-09 00:57:59 +000090struct OrderGlobalInits {
91 unsigned int priority;
92 unsigned int lex_order;
93 OrderGlobalInits(unsigned int p, unsigned int l)
Chris Lattnere0009072010-06-27 06:32:58 +000094 : priority(p), lex_order(l) {}
Rafael Espindola42ae7452014-05-09 00:57:59 +000095
96 bool operator==(const OrderGlobalInits &RHS) const {
97 return priority == RHS.priority && lex_order == RHS.lex_order;
98 }
99
100 bool operator<(const OrderGlobalInits &RHS) const {
101 return std::tie(priority, lex_order) <
102 std::tie(RHS.priority, RHS.lex_order);
103 }
104};
105
106struct CodeGenTypeCache {
107 /// void
108 llvm::Type *VoidTy;
109
110 /// i8, i16, i32, and i64
111 llvm::IntegerType *Int8Ty, *Int16Ty, *Int32Ty, *Int64Ty;
112 /// float, double
113 llvm::Type *FloatTy, *DoubleTy;
114
115 /// int
116 llvm::IntegerType *IntTy;
117
118 /// intptr_t, size_t, and ptrdiff_t, which we assume are the same size.
119 union {
120 llvm::IntegerType *IntPtrTy;
121 llvm::IntegerType *SizeTy;
122 llvm::IntegerType *PtrDiffTy;
Chris Lattnere0009072010-06-27 06:32:58 +0000123 };
John McCalle3dc1702011-02-15 09:22:45 +0000124
Rafael Espindola42ae7452014-05-09 00:57:59 +0000125 /// void* in address space 0
126 union {
127 llvm::PointerType *VoidPtrTy;
128 llvm::PointerType *Int8PtrTy;
John McCalle3dc1702011-02-15 09:22:45 +0000129 };
John McCall31168b02011-06-15 23:02:42 +0000130
Rafael Espindola42ae7452014-05-09 00:57:59 +0000131 /// void** in address space 0
132 union {
133 llvm::PointerType *VoidPtrPtrTy;
134 llvm::PointerType *Int8PtrPtrTy;
135 };
136
137 /// The width of a pointer into the generic address space.
138 unsigned char PointerWidthInBits;
139
140 /// The size and alignment of a pointer into the generic address
141 /// space.
142 union {
143 unsigned char PointerAlignInBytes;
144 unsigned char PointerSizeInBytes;
145 unsigned char SizeSizeInBytes; // sizeof(size_t)
146 };
147
148 llvm::CallingConv::ID RuntimeCC;
149 llvm::CallingConv::ID getRuntimeCC() const { return RuntimeCC; }
150};
151
John McCall31168b02011-06-15 23:02:42 +0000152struct RREntrypoints {
153 RREntrypoints() { memset(this, 0, sizeof(*this)); }
154 /// void objc_autoreleasePoolPop(void*);
155 llvm::Constant *objc_autoreleasePoolPop;
156
157 /// void *objc_autoreleasePoolPush(void);
158 llvm::Constant *objc_autoreleasePoolPush;
159};
160
161struct ARCEntrypoints {
162 ARCEntrypoints() { memset(this, 0, sizeof(*this)); }
163
164 /// id objc_autorelease(id);
165 llvm::Constant *objc_autorelease;
166
167 /// id objc_autoreleaseReturnValue(id);
168 llvm::Constant *objc_autoreleaseReturnValue;
169
170 /// void objc_copyWeak(id *dest, id *src);
171 llvm::Constant *objc_copyWeak;
172
173 /// void objc_destroyWeak(id*);
174 llvm::Constant *objc_destroyWeak;
175
176 /// id objc_initWeak(id*, id);
177 llvm::Constant *objc_initWeak;
178
179 /// id objc_loadWeak(id*);
180 llvm::Constant *objc_loadWeak;
181
182 /// id objc_loadWeakRetained(id*);
183 llvm::Constant *objc_loadWeakRetained;
184
185 /// void objc_moveWeak(id *dest, id *src);
186 llvm::Constant *objc_moveWeak;
187
188 /// id objc_retain(id);
189 llvm::Constant *objc_retain;
190
191 /// id objc_retainAutorelease(id);
192 llvm::Constant *objc_retainAutorelease;
193
194 /// id objc_retainAutoreleaseReturnValue(id);
195 llvm::Constant *objc_retainAutoreleaseReturnValue;
196
197 /// id objc_retainAutoreleasedReturnValue(id);
198 llvm::Constant *objc_retainAutoreleasedReturnValue;
199
200 /// id objc_retainBlock(id);
201 llvm::Constant *objc_retainBlock;
202
203 /// void objc_release(id);
204 llvm::Constant *objc_release;
205
206 /// id objc_storeStrong(id*, id);
207 llvm::Constant *objc_storeStrong;
208
209 /// id objc_storeWeak(id*, id);
210 llvm::Constant *objc_storeWeak;
211
212 /// A void(void) inline asm to use to mark that the return value of
213 /// a call will be immediately retain.
214 llvm::InlineAsm *retainAutoreleasedReturnValueMarker;
John McCalleff18842013-03-23 02:35:54 +0000215
216 /// void clang.arc.use(...);
217 llvm::Constant *clang_arc_use;
John McCall31168b02011-06-15 23:02:42 +0000218};
Will Dietzf54319c2013-01-18 11:30:38 +0000219
Justin Bognere2ef2a02014-04-15 21:22:35 +0000220/// This class records statistics on instrumentation based profiling.
Justin Bogner40b8ba12014-06-26 01:45:07 +0000221class InstrProfStats {
222 uint32_t VisitedInMainFile;
223 uint32_t MissingInMainFile;
Justin Bognere2ef2a02014-04-15 21:22:35 +0000224 uint32_t Visited;
225 uint32_t Missing;
226 uint32_t Mismatched;
Justin Bogner40b8ba12014-06-26 01:45:07 +0000227
228public:
229 InstrProfStats()
230 : VisitedInMainFile(0), MissingInMainFile(0), Visited(0), Missing(0),
231 Mismatched(0) {}
232 /// Record that we've visited a function and whether or not that function was
233 /// in the main source file.
234 void addVisited(bool MainFile) {
235 if (MainFile)
236 ++VisitedInMainFile;
237 ++Visited;
238 }
239 /// Record that a function we've visited has no profile data.
240 void addMissing(bool MainFile) {
241 if (MainFile)
242 ++MissingInMainFile;
243 ++Missing;
244 }
245 /// Record that a function we've visited has mismatched profile data.
246 void addMismatched(bool MainFile) { ++Mismatched; }
247 /// Whether or not the stats we've gathered indicate any potential problems.
248 bool hasDiagnostics() { return Missing || Mismatched; }
249 /// Report potential problems we've found to \c Diags.
250 void reportDiagnostics(DiagnosticsEngine &Diags, StringRef MainFile);
Justin Bognere2ef2a02014-04-15 21:22:35 +0000251};
252
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000253/// This class organizes the cross-function state that is used while generating
254/// LLVM code.
John McCalle3dc1702011-02-15 09:22:45 +0000255class CodeGenModule : public CodeGenTypeCache {
Dmitri Gribenkoa664e5b2012-09-15 20:20:27 +0000256 CodeGenModule(const CodeGenModule &) LLVM_DELETED_FUNCTION;
257 void operator=(const CodeGenModule &) LLVM_DELETED_FUNCTION;
Anders Carlsson729a8202009-02-24 04:21:31 +0000258
Reid Kleckner563f0e82014-05-23 21:13:45 +0000259 struct Structor {
260 Structor() : Priority(0), Initializer(nullptr), AssociatedData(nullptr) {}
261 Structor(int Priority, llvm::Constant *Initializer,
262 llvm::Constant *AssociatedData)
263 : Priority(Priority), Initializer(Initializer),
264 AssociatedData(AssociatedData) {}
265 int Priority;
266 llvm::Constant *Initializer;
267 llvm::Constant *AssociatedData;
268 };
269
270 typedef std::vector<Structor> CtorList;
Daniel Dunbar74aa7e12008-08-01 00:01:51 +0000271
Chris Lattnerf97fe382007-05-24 06:29:05 +0000272 ASTContext &Context;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000273 const LangOptions &LangOpts;
Chandler Carruthbc55fe22009-11-12 17:24:48 +0000274 const CodeGenOptions &CodeGenOpts;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000275 llvm::Module &TheModule;
David Blaikie9c902b52011-09-25 23:23:43 +0000276 DiagnosticsEngine &Diags;
John McCallc8e01702013-04-16 22:48:15 +0000277 const llvm::DataLayout &TheDataLayout;
278 const TargetInfo &Target;
Ahmed Charlesb8984322014-03-07 20:03:18 +0000279 std::unique_ptr<CGCXXABI> ABI;
John McCallc8e01702013-04-16 22:48:15 +0000280 llvm::LLVMContext &VMContext;
Anders Carlssonff971e82009-10-07 01:06:45 +0000281
John McCallc8e01702013-04-16 22:48:15 +0000282 CodeGenTBAA *TBAA;
283
284 mutable const TargetCodeGenInfo *TheTargetCodeGenInfo;
285
286 // This should not be moved earlier, since its initialization depends on some
287 // of the previous reference members being already initialized and also checks
288 // if TheTargetCodeGenInfo is NULL
289 CodeGenTypes Types;
290
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000291 /// Holds information about C++ vtables.
Anders Carlssona864caf2010-03-23 04:11:45 +0000292 CodeGenVTables VTables;
Douglas Gregorbc6a4342010-04-08 16:07:47 +0000293
Peter Collingbournee1d20992011-07-27 20:29:46 +0000294 CGObjCRuntime* ObjCRuntime;
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000295 CGOpenCLRuntime* OpenCLRuntime;
Alexey Bataev9959db52014-05-06 10:08:46 +0000296 CGOpenMPRuntime* OpenMPRuntime;
Peter Collingbournefe883422011-10-06 18:29:37 +0000297 CGCUDARuntime* CUDARuntime;
Ted Kremenek2c674f62008-08-05 18:50:11 +0000298 CGDebugInfo* DebugInfo;
John McCall31168b02011-06-15 23:02:42 +0000299 ARCEntrypoints *ARCData;
Dan Gohman515a60d2012-02-16 00:57:37 +0000300 llvm::MDNode *NoObjCARCExceptionsMetadata;
John McCall31168b02011-06-15 23:02:42 +0000301 RREntrypoints *RRData;
Justin Bogner837a6f62014-04-18 21:52:00 +0000302 std::unique_ptr<llvm::IndexedInstrProfReader> PGOReader;
Justin Bognere2ef2a02014-04-15 21:22:35 +0000303 InstrProfStats PGOStats;
Daniel Dunbare49df9b52008-07-30 16:32:24 +0000304
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000305 // A set of references that have only been seen via a weakref so far. This is
306 // used to remove the weak of the reference if we ever see a direct reference
307 // or a definition.
Rafael Espindola2e42fec2010-03-04 18:17:24 +0000308 llvm::SmallPtrSet<llvm::GlobalValue*, 10> WeakRefReferences;
309
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000310 /// This contains all the decls which have definitions but/ which are deferred
311 /// for emission and therefore should only be output if they are actually
312 /// used. If a decl is in this, then it is known to have not been referenced
313 /// yet.
Alp Tokerfb8d02b2014-06-05 22:10:59 +0000314 std::map<StringRef, GlobalDecl> DeferredDecls;
Daniel Dunbar08b26a02009-02-13 20:29:50 +0000315
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000316 /// This is a list of deferred decls which we have seen that *are* actually
317 /// referenced. These get code generated when the module is done.
Rafael Espindolac0ff7442013-12-09 14:59:08 +0000318 struct DeferredGlobal {
319 DeferredGlobal(llvm::GlobalValue *GV, GlobalDecl GD) : GV(GV), GD(GD) {}
320 llvm::AssertingVH<llvm::GlobalValue> GV;
321 GlobalDecl GD;
322 };
323 std::vector<DeferredGlobal> DeferredDeclsToEmit;
324 void addDeferredDeclToEmit(llvm::GlobalValue *GV, GlobalDecl GD) {
325 DeferredDeclsToEmit.push_back(DeferredGlobal(GV, GD));
326 }
Daniel Dunbar7dd749e2009-04-15 22:08:45 +0000327
Rafael Espindola208b5c02013-10-22 19:26:13 +0000328 /// List of alias we have emitted. Used to make sure that what they point to
329 /// is defined once we get to the end of the of the translation unit.
330 std::vector<GlobalDecl> Aliases;
331
Rafael Espindola2e2995b2013-11-05 21:37:29 +0000332 typedef llvm::StringMap<llvm::TrackingVH<llvm::Constant> > ReplacementsTy;
333 ReplacementsTy Replacements;
334
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000335 /// A queue of (optional) vtables to consider emitting.
John McCall6bd2a892013-01-25 22:31:03 +0000336 std::vector<const CXXRecordDecl*> DeferredVTables;
337
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000338 /// List of global values which are required to be present in the object file;
339 /// bitcast to i8*. This is used for forcing visibility of symbols which may
340 /// otherwise be optimized out.
Chris Lattnerf41e87f2009-03-31 22:37:52 +0000341 std::vector<llvm::WeakVH> LLVMUsed;
Rafael Espindola060062a2014-03-06 22:15:10 +0000342 std::vector<llvm::WeakVH> LLVMCompilerUsed;
Mike Stump1676c5b2009-02-13 19:12:34 +0000343
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000344 /// Store the list of global constructors and their respective priorities to
345 /// be emitted when the translation unit is complete.
Daniel Dunbar74aa7e12008-08-01 00:01:51 +0000346 CtorList GlobalCtors;
347
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000348 /// Store the list of global destructors and their respective priorities to be
349 /// emitted when the translation unit is complete.
Daniel Dunbar74aa7e12008-08-01 00:01:51 +0000350 CtorList GlobalDtors;
351
Robert Lytton57765d52014-07-03 09:30:33 +0000352 /// An ordered map of canonical GlobalDecls to their mangled names.
353 llvm::MapVector<GlobalDecl, StringRef> MangledDeclNames;
Alp Tokerfb8d02b2014-06-05 22:10:59 +0000354 llvm::StringMap<GlobalDecl, llvm::BumpPtrAllocator> Manglings;
355
Julien Lerouge5a6b6982011-09-09 22:41:49 +0000356 /// Global annotations.
Nate Begeman7fab5782008-04-18 23:43:57 +0000357 std::vector<llvm::Constant*> Annotations;
Mike Stump1676c5b2009-02-13 19:12:34 +0000358
Julien Lerouge5a6b6982011-09-09 22:41:49 +0000359 /// Map used to get unique annotation strings.
360 llvm::StringMap<llvm::Constant*> AnnotationStrings;
361
Anders Carlssonb04ea612007-08-21 00:21:21 +0000362 llvm::StringMap<llvm::Constant*> CFConstantStringMap;
David Majnemer58e5bee2014-03-24 21:43:36 +0000363
364 llvm::StringMap<llvm::GlobalVariable *> Constant1ByteStringMap;
365 llvm::StringMap<llvm::GlobalVariable *> Constant2ByteStringMap;
366 llvm::StringMap<llvm::GlobalVariable *> Constant4ByteStringMap;
Eli Friedman1c8d2ca2012-03-09 03:27:46 +0000367 llvm::DenseMap<const Decl*, llvm::Constant *> StaticLocalDeclMap;
John McCallb88a5662012-03-30 21:00:39 +0000368 llvm::DenseMap<const Decl*, llvm::GlobalVariable*> StaticLocalDeclGuardMap;
Richard Smithe6c01442013-06-05 00:46:14 +0000369 llvm::DenseMap<const Expr*, llvm::Constant *> MaterializedGlobalTemporaryMap;
370
Fariborz Jahanian1bed4132012-01-08 19:13:23 +0000371 llvm::DenseMap<QualType, llvm::Constant *> AtomicSetterHelperFnMap;
372 llvm::DenseMap<QualType, llvm::Constant *> AtomicGetterHelperFnMap;
Daniel Dunbard644ad62008-08-23 18:37:06 +0000373
Will Dietz949ec542013-11-08 01:09:22 +0000374 /// Map used to get unique type descriptor constants for sanitizers.
375 llvm::DenseMap<QualType, llvm::Constant *> TypeDescriptorMap;
376
Richard Smithdf931ce2013-04-06 05:00:46 +0000377 /// Map used to track internal linkage functions declared within
378 /// extern "C" regions.
Richard Smithf21c9612013-04-13 01:28:18 +0000379 typedef llvm::MapVector<IdentifierInfo *,
380 llvm::GlobalValue *> StaticExternCMap;
Richard Smithdf931ce2013-04-06 05:00:46 +0000381 StaticExternCMap StaticExternCValues;
382
Richard Smith2fd1d7a2013-04-19 16:42:07 +0000383 /// \brief thread_local variables defined or used in this TU.
384 std::vector<std::pair<const VarDecl *, llvm::GlobalVariable *> >
385 CXXThreadLocals;
386
387 /// \brief thread_local variables with initializers that need to run
388 /// before any thread_local variable in this TU is odr-used.
389 std::vector<llvm::Constant*> CXXThreadLocalInits;
390
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000391 /// Global variables with initializers that need to run before main.
Eli Friedman5866fe32010-01-08 00:50:11 +0000392 std::vector<llvm::Constant*> CXXGlobalInits;
John McCall70013b62010-07-15 23:40:35 +0000393
394 /// When a C++ decl with an initializer is deferred, null is
395 /// appended to CXXGlobalInits, and the index of that null is placed
396 /// here so that the initializer will be performed in the correct
397 /// order.
398 llvm::DenseMap<const Decl*, unsigned> DelayedCXXInitPosition;
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +0000399
Anton Korobeynikovabed7492012-11-06 22:44:45 +0000400 typedef std::pair<OrderGlobalInits, llvm::Function*> GlobalInitData;
401
402 struct GlobalInitPriorityCmp {
403 bool operator()(const GlobalInitData &LHS,
404 const GlobalInitData &RHS) const {
405 return LHS.first.priority < RHS.first.priority;
406 }
407 };
408
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000409 /// Global variables with initializers whose order of initialization is set by
410 /// init_priority attribute.
Anton Korobeynikovabed7492012-11-06 22:44:45 +0000411 SmallVector<GlobalInitData, 8> PrioritizedCXXGlobalInits;
Mike Stump11289f42009-09-09 15:08:12 +0000412
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000413 /// Global destructor functions and arguments that need to run on termination.
Chris Lattner87233f72010-06-19 05:52:45 +0000414 std::vector<std::pair<llvm::WeakVH,llvm::Constant*> > CXXGlobalDtors;
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000415
Douglas Gregor6ddfca92013-01-14 17:21:00 +0000416 /// \brief The complete set of modules that has been imported.
417 llvm::SetVector<clang::Module *> ImportedModules;
418
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000419 /// \brief A vector of metadata strings.
420 SmallVector<llvm::Value *, 16> LinkerOptionsMetadata;
421
Douglas Gregorabf4e0d2011-08-09 15:54:21 +0000422 /// @name Cache for Objective-C runtime types
423 /// @{
424
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000425 /// Cached reference to the class for constant strings. This value has type
426 /// int * but is actually an Obj-C class pointer.
Fariborz Jahaniand1b67782013-04-16 15:25:39 +0000427 llvm::WeakVH CFConstantStringClassRef;
Mike Stump11289f42009-09-09 15:08:12 +0000428
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000429 /// Cached reference to the class for constant strings. This value has type
430 /// int * but is actually an Obj-C class pointer.
Fariborz Jahaniand1b67782013-04-16 15:25:39 +0000431 llvm::WeakVH ConstantStringClassRef;
Fariborz Jahaniane804c282010-04-23 17:41:07 +0000432
Douglas Gregorabf4e0d2011-08-09 15:54:21 +0000433 /// \brief The LLVM type corresponding to NSConstantString.
434 llvm::StructType *NSConstantStringType;
435
Douglas Gregor636e2002011-08-09 17:23:49 +0000436 /// \brief The type used to describe the state of a fast enumeration in
437 /// Objective-C's for..in loop.
438 QualType ObjCFastEnumerationStateType;
439
Douglas Gregorabf4e0d2011-08-09 15:54:21 +0000440 /// @}
441
David Chisnall481e3a82010-01-23 02:40:42 +0000442 /// Lazily create the Objective-C runtime
443 void createObjCRuntime();
444
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000445 void createOpenCLRuntime();
Alexey Bataev9959db52014-05-06 10:08:46 +0000446 void createOpenMPRuntime();
Peter Collingbournefe883422011-10-06 18:29:37 +0000447 void createCUDARuntime();
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000448
Rafael Espindola4fdc1752011-12-19 14:41:01 +0000449 bool isTriviallyRecursive(const FunctionDecl *F);
Peter Collingbourne4d90dba2013-06-05 17:49:37 +0000450 bool shouldEmitFunction(GlobalDecl GD);
Daniel Dunbar900546d2010-07-16 00:00:15 +0000451
452 /// @name Cache for Blocks Runtime Globals
453 /// @{
454
455 llvm::Constant *NSConcreteGlobalBlock;
456 llvm::Constant *NSConcreteStackBlock;
Daniel Dunbar3348e2d2010-07-16 00:00:19 +0000457
Daniel Dunbar900546d2010-07-16 00:00:15 +0000458 llvm::Constant *BlockObjectAssign;
459 llvm::Constant *BlockObjectDispose;
460
Chris Lattnera5f58b02011-07-09 17:41:47 +0000461 llvm::Type *BlockDescriptorType;
462 llvm::Type *GenericBlockLiteralType;
John McCallad7c5c12011-02-08 08:22:06 +0000463
464 struct {
465 int GlobalUniqueCount;
466 } Block;
Will Dietzf54319c2013-01-18 11:30:38 +0000467
Nadav Rotem1da30942013-03-23 06:43:35 +0000468 /// void @llvm.lifetime.start(i64 %size, i8* nocapture <ptr>)
469 llvm::Constant *LifetimeStartFn;
470
471 /// void @llvm.lifetime.end(i64 %size, i8* nocapture <ptr>)
472 llvm::Constant *LifetimeEndFn;
473
Fariborz Jahanian63628032012-06-26 16:06:38 +0000474 GlobalDecl initializedGlobalDecl;
John McCallad7c5c12011-02-08 08:22:06 +0000475
Alexey Samsonovb7dd3292014-07-09 19:40:08 +0000476 SanitizerBlacklist SanitizerBL;
Will Dietzf54319c2013-01-18 11:30:38 +0000477
Daniel Dunbar900546d2010-07-16 00:00:15 +0000478 /// @}
Chris Lattnerf97fe382007-05-24 06:29:05 +0000479public:
Chandler Carruthbc55fe22009-11-12 17:24:48 +0000480 CodeGenModule(ASTContext &C, const CodeGenOptions &CodeGenOpts,
John McCall568d4102013-04-16 22:48:20 +0000481 llvm::Module &M, const llvm::DataLayout &TD,
482 DiagnosticsEngine &Diags);
Ted Kremenek2c674f62008-08-05 18:50:11 +0000483
Chris Lattnera087ff92008-03-01 08:45:05 +0000484 ~CodeGenModule();
Mike Stump1676c5b2009-02-13 19:12:34 +0000485
Rafael Espindolac0ff7442013-12-09 14:59:08 +0000486 void clear();
487
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000488 /// Finalize LLVM code generation.
Ted Kremenek2c674f62008-08-05 18:50:11 +0000489 void Release();
Daniel Dunbar8d480592008-08-11 18:12:00 +0000490
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000491 /// Return a reference to the configured Objective-C runtime.
Mike Stump1676c5b2009-02-13 19:12:34 +0000492 CGObjCRuntime &getObjCRuntime() {
Peter Collingbournee1d20992011-07-27 20:29:46 +0000493 if (!ObjCRuntime) createObjCRuntime();
494 return *ObjCRuntime;
Daniel Dunbar8d480592008-08-11 18:12:00 +0000495 }
Mike Stump1676c5b2009-02-13 19:12:34 +0000496
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000497 /// Return true iff an Objective-C runtime has been configured.
Peter Collingbournee1d20992011-07-27 20:29:46 +0000498 bool hasObjCRuntime() { return !!ObjCRuntime; }
Daniel Dunbar8d480592008-08-11 18:12:00 +0000499
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000500 /// Return a reference to the configured OpenCL runtime.
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000501 CGOpenCLRuntime &getOpenCLRuntime() {
Craig Topper8a13c412014-05-21 05:09:00 +0000502 assert(OpenCLRuntime != nullptr);
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000503 return *OpenCLRuntime;
504 }
505
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000506 /// Return a reference to the configured OpenMP runtime.
Alexey Bataev9959db52014-05-06 10:08:46 +0000507 CGOpenMPRuntime &getOpenMPRuntime() {
508 assert(OpenMPRuntime != nullptr);
509 return *OpenMPRuntime;
510 }
511
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000512 /// Return a reference to the configured CUDA runtime.
Peter Collingbournefe883422011-10-06 18:29:37 +0000513 CGCUDARuntime &getCUDARuntime() {
Craig Topper8a13c412014-05-21 05:09:00 +0000514 assert(CUDARuntime != nullptr);
Peter Collingbournefe883422011-10-06 18:29:37 +0000515 return *CUDARuntime;
516 }
517
John McCall31168b02011-06-15 23:02:42 +0000518 ARCEntrypoints &getARCEntrypoints() const {
Craig Topper8a13c412014-05-21 05:09:00 +0000519 assert(getLangOpts().ObjCAutoRefCount && ARCData != nullptr);
John McCall31168b02011-06-15 23:02:42 +0000520 return *ARCData;
521 }
522
523 RREntrypoints &getRREntrypoints() const {
Craig Topper8a13c412014-05-21 05:09:00 +0000524 assert(RRData != nullptr);
John McCall31168b02011-06-15 23:02:42 +0000525 return *RRData;
526 }
527
Justin Bogner837a6f62014-04-18 21:52:00 +0000528 InstrProfStats &getPGOStats() { return PGOStats; }
529 llvm::IndexedInstrProfReader *getPGOReader() const { return PGOReader.get(); }
Justin Bogneref512b92014-01-06 22:27:43 +0000530
Eli Friedman1c8d2ca2012-03-09 03:27:46 +0000531 llvm::Constant *getStaticLocalDeclAddress(const VarDecl *D) {
532 return StaticLocalDeclMap[D];
Fariborz Jahanian3fef72f2010-04-18 21:01:23 +0000533 }
Fariborz Jahanian4d55b2d2010-04-19 18:15:02 +0000534 void setStaticLocalDeclAddress(const VarDecl *D,
Eli Friedman1c8d2ca2012-03-09 03:27:46 +0000535 llvm::Constant *C) {
536 StaticLocalDeclMap[D] = C;
Fariborz Jahanian3fef72f2010-04-18 21:01:23 +0000537 }
538
John McCallb88a5662012-03-30 21:00:39 +0000539 llvm::GlobalVariable *getStaticLocalDeclGuardAddress(const VarDecl *D) {
540 return StaticLocalDeclGuardMap[D];
541 }
542 void setStaticLocalDeclGuardAddress(const VarDecl *D,
543 llvm::GlobalVariable *C) {
544 StaticLocalDeclGuardMap[D] = C;
545 }
546
Alp Tokerfb8d02b2014-06-05 22:10:59 +0000547 bool lookupRepresentativeDecl(StringRef MangledName,
548 GlobalDecl &Result) const;
549
Fariborz Jahanian1bed4132012-01-08 19:13:23 +0000550 llvm::Constant *getAtomicSetterHelperFnMap(QualType Ty) {
551 return AtomicSetterHelperFnMap[Ty];
Fariborz Jahanian7ff610b2012-01-06 22:33:54 +0000552 }
Fariborz Jahanian1bed4132012-01-08 19:13:23 +0000553 void setAtomicSetterHelperFnMap(QualType Ty,
Fariborz Jahanian7ff610b2012-01-06 22:33:54 +0000554 llvm::Constant *Fn) {
Fariborz Jahanian1bed4132012-01-08 19:13:23 +0000555 AtomicSetterHelperFnMap[Ty] = Fn;
556 }
557
558 llvm::Constant *getAtomicGetterHelperFnMap(QualType Ty) {
559 return AtomicGetterHelperFnMap[Ty];
560 }
561 void setAtomicGetterHelperFnMap(QualType Ty,
562 llvm::Constant *Fn) {
563 AtomicGetterHelperFnMap[Ty] = Fn;
Fariborz Jahanian7ff610b2012-01-06 22:33:54 +0000564 }
565
Warren Hunt5c2b4ea2014-05-23 16:07:43 +0000566 llvm::Constant *getTypeDescriptorFromMap(QualType Ty) {
Will Dietz949ec542013-11-08 01:09:22 +0000567 return TypeDescriptorMap[Ty];
568 }
Warren Hunt5c2b4ea2014-05-23 16:07:43 +0000569 void setTypeDescriptorInMap(QualType Ty, llvm::Constant *C) {
Will Dietz949ec542013-11-08 01:09:22 +0000570 TypeDescriptorMap[Ty] = C;
571 }
572
Devang Pateld6ffebb2011-03-07 18:45:56 +0000573 CGDebugInfo *getModuleDebugInfo() { return DebugInfo; }
Devang Patele65982c2011-03-07 18:29:53 +0000574
Dan Gohman515a60d2012-02-16 00:57:37 +0000575 llvm::MDNode *getNoObjCARCExceptionsMetadata() {
576 if (!NoObjCARCExceptionsMetadata)
577 NoObjCARCExceptionsMetadata =
578 llvm::MDNode::get(getLLVMContext(),
579 SmallVector<llvm::Value*,1>());
580 return NoObjCARCExceptionsMetadata;
581 }
582
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000583 ASTContext &getContext() const { return Context; }
David Blaikiebbafb8a2012-03-11 07:00:24 +0000584 const LangOptions &getLangOpts() const { return LangOpts; }
John McCallc8e01702013-04-16 22:48:15 +0000585 const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
Chris Lattner23b7eb62007-06-15 23:05:46 +0000586 llvm::Module &getModule() const { return TheModule; }
David Blaikie9c902b52011-09-25 23:23:43 +0000587 DiagnosticsEngine &getDiags() const { return Diags; }
Micah Villmowdd31ca12012-10-08 16:25:52 +0000588 const llvm::DataLayout &getDataLayout() const { return TheDataLayout; }
John McCallc8e01702013-04-16 22:48:15 +0000589 const TargetInfo &getTarget() const { return Target; }
Alp Toker82862252013-12-28 21:58:40 +0000590 CGCXXABI &getCXXABI() const { return *ABI; }
Owen Anderson170229f2009-07-14 23:10:40 +0000591 llvm::LLVMContext &getLLVMContext() { return VMContext; }
Craig Topper8a13c412014-05-21 05:09:00 +0000592
593 bool shouldUseTBAA() const { return TBAA != nullptr; }
John McCall53fcbd22011-02-26 08:07:02 +0000594
John McCallc8e01702013-04-16 22:48:15 +0000595 const TargetCodeGenInfo &getTargetCodeGenInfo();
596
597 CodeGenTypes &getTypes() { return Types; }
598
599 CodeGenVTables &getVTables() { return VTables; }
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000600
Timur Iskhodzhanov58776632013-11-05 15:54:58 +0000601 ItaniumVTableContext &getItaniumVTableContext() {
602 return VTables.getItaniumVTableContext();
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +0000603 }
John McCallc8e01702013-04-16 22:48:15 +0000604
Timur Iskhodzhanov58776632013-11-05 15:54:58 +0000605 MicrosoftVTableContext &getMicrosoftVTableContext() {
606 return VTables.getMicrosoftVTableContext();
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000607 }
608
Dan Gohman947c9af2010-10-14 23:06:10 +0000609 llvm::MDNode *getTBAAInfo(QualType QTy);
Kostya Serebryany141e46f2012-03-26 17:03:51 +0000610 llvm::MDNode *getTBAAInfoForVTablePtr();
Dan Gohman22695fc2012-09-28 21:58:29 +0000611 llvm::MDNode *getTBAAStructInfo(QualType QTy);
Manman Renc451e572013-04-04 21:53:22 +0000612 /// Return the MDNode in the type DAG for the given struct type.
613 llvm::MDNode *getTBAAStructTypeInfo(QualType QTy);
614 /// Return the path-aware tag for given base type, access node and offset.
615 llvm::MDNode *getTBAAStructTagInfo(QualType BaseTy, llvm::MDNode *AccessN,
616 uint64_t O);
Dan Gohman947c9af2010-10-14 23:06:10 +0000617
Richard Smithe070fd22012-02-17 06:48:11 +0000618 bool isTypeConstant(QualType QTy, bool ExcludeCtorDtor);
619
John McCalla8ec7eb2013-03-07 21:37:17 +0000620 bool isPaddedAtomicType(QualType type);
621 bool isPaddedAtomicType(const AtomicType *type);
622
Manman Rene1ad74e2013-04-11 23:02:56 +0000623 /// Decorate the instruction with a TBAA tag. For scalar TBAA, the tag
624 /// is the same as the type. For struct-path aware TBAA, the tag
625 /// is different from the type: base type, access type and offset.
626 /// When ConvertTypeToTag is true, we create a tag based on the scalar type.
627 void DecorateInstruction(llvm::Instruction *Inst,
628 llvm::MDNode *TBAAInfo,
629 bool ConvertTypeToTag = true);
Dan Gohman947c9af2010-10-14 23:06:10 +0000630
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000631 /// Emit the given number of characters as a value of type size_t.
John McCall23c29fe2011-06-24 21:55:10 +0000632 llvm::ConstantInt *getSize(CharUnits numChars);
633
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000634 /// Set the visibility for the given LLVM GlobalValue.
Anders Carlssonc6a47892011-01-29 19:39:23 +0000635 void setGlobalVisibility(llvm::GlobalValue *GV, const NamedDecl *D) const;
Daniel Dunbarf5f359f2009-04-14 06:00:08 +0000636
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000637 /// Set the TLS mode for the given LLVM GlobalVariable for the thread-local
638 /// variable declaration D.
Hans Wennborgf60f6af2012-06-28 08:01:44 +0000639 void setTLSMode(llvm::GlobalVariable *GV, const VarDecl &D) const;
640
Anders Carlsson537fdce2011-01-29 20:59:35 +0000641 static llvm::GlobalValue::VisibilityTypes GetLLVMVisibility(Visibility V) {
642 switch (V) {
643 case DefaultVisibility: return llvm::GlobalValue::DefaultVisibility;
644 case HiddenVisibility: return llvm::GlobalValue::HiddenVisibility;
645 case ProtectedVisibility: return llvm::GlobalValue::ProtectedVisibility;
646 }
647 llvm_unreachable("unknown visibility!");
Anders Carlsson537fdce2011-01-29 20:59:35 +0000648 }
649
John McCalld4324142010-02-19 01:32:20 +0000650 llvm::Constant *GetAddrOfGlobal(GlobalDecl GD) {
651 if (isa<CXXConstructorDecl>(GD.getDecl()))
652 return GetAddrOfCXXConstructor(cast<CXXConstructorDecl>(GD.getDecl()),
653 GD.getCtorType());
654 else if (isa<CXXDestructorDecl>(GD.getDecl()))
655 return GetAddrOfCXXDestructor(cast<CXXDestructorDecl>(GD.getDecl()),
656 GD.getDtorType());
657 else if (isa<FunctionDecl>(GD.getDecl()))
658 return GetAddrOfFunction(GD);
659 else
660 return GetAddrOfGlobalVar(cast<VarDecl>(GD.getDecl()));
661 }
662
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000663 /// Will return a global variable of the given type. If a variable with a
664 /// different type already exists then a new variable with the right type
665 /// will be created and all uses of the old variable will be replaced with a
666 /// bitcast to the new variable.
Anders Carlssonda80af32011-01-29 18:20:20 +0000667 llvm::GlobalVariable *
Chris Lattner01cf8db2011-07-20 06:58:45 +0000668 CreateOrReplaceCXXRuntimeVariable(StringRef Name, llvm::Type *Ty,
Anders Carlssonda80af32011-01-29 18:20:20 +0000669 llvm::GlobalValue::LinkageTypes Linkage);
670
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000671 /// Return the address space of the underlying global variable for D, as
672 /// determined by its declaration. Normally this is the same as the address
673 /// space of D's type, but in CUDA, address spaces are associated with
674 /// declarations, not types.
Peter Collingbournef44bdf92012-05-20 21:08:35 +0000675 unsigned GetGlobalVarAddressSpace(const VarDecl *D, unsigned AddrSpace);
676
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000677 /// Return the llvm::Constant for the address of the given global variable.
678 /// If Ty is non-null and if the global doesn't exist, then it will be greated
679 /// with the specified type instead of whatever the normal requested type
680 /// would be.
Chris Lattner149927c2009-03-21 09:16:30 +0000681 llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D,
Craig Topper8a13c412014-05-21 05:09:00 +0000682 llvm::Type *Ty = nullptr);
Daniel Dunbar9c426522008-07-29 23:18:29 +0000683
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000684 /// Return the address of the given function. If Ty is non-null, then this
685 /// function will use the specified type if it has to create it.
Rafael Espindola94abb8f2013-12-09 04:29:47 +0000686 llvm::Constant *GetAddrOfFunction(GlobalDecl GD, llvm::Type *Ty = 0,
687 bool ForVTable = false,
688 bool DontDefer = false);
Daniel Dunbarc4baa062008-08-13 23:20:05 +0000689
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000690 /// Get the address of the RTTI descriptor for the given type.
John McCall48bf3492010-04-30 01:15:21 +0000691 llvm::Constant *GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH = false);
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +0000692
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000693 /// Get the address of a uuid descriptor .
Nico Webercf4ff5862012-10-11 10:13:44 +0000694 llvm::Constant *GetAddrOfUuidDescriptor(const CXXUuidofExpr* E);
695
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000696 /// Get the address of the thunk for the given global decl.
Anders Carlssonfe8a9932011-02-06 17:15:43 +0000697 llvm::Constant *GetAddrOfThunk(GlobalDecl GD, const ThunkInfo &Thunk);
Anders Carlssoncd836f02010-03-23 17:17:29 +0000698
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000699 /// Get a reference to the target of VD.
Rafael Espindola2e42fec2010-03-04 18:17:24 +0000700 llvm::Constant *GetWeakRefReference(const ValueDecl *VD);
701
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000702 /// Returns the offset from a derived class to a class. Returns null if the
703 /// offset is 0.
Anders Carlsson8a64c1c2010-04-24 21:23:59 +0000704 llvm::Constant *
705 GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl,
John McCallcf142162010-08-07 06:22:56 +0000706 CastExpr::path_const_iterator PathBegin,
707 CastExpr::path_const_iterator PathEnd);
John McCallad7c5c12011-02-08 08:22:06 +0000708
John McCallf9b056b2011-03-31 08:03:29 +0000709 /// A pair of helper functions for a __block variable.
710 class ByrefHelpers : public llvm::FoldingSetNode {
711 public:
712 llvm::Constant *CopyHelper;
713 llvm::Constant *DisposeHelper;
714
715 /// The alignment of the field. This is important because
716 /// different offsets to the field within the byref struct need to
717 /// have different helper functions.
718 CharUnits Alignment;
719
720 ByrefHelpers(CharUnits alignment) : Alignment(alignment) {}
721 virtual ~ByrefHelpers();
722
723 void Profile(llvm::FoldingSetNodeID &id) const {
724 id.AddInteger(Alignment.getQuantity());
725 profileImpl(id);
726 }
727 virtual void profileImpl(llvm::FoldingSetNodeID &id) const = 0;
728
729 virtual bool needsCopy() const { return true; }
730 virtual void emitCopy(CodeGenFunction &CGF,
731 llvm::Value *dest, llvm::Value *src) = 0;
732
733 virtual bool needsDispose() const { return true; }
734 virtual void emitDispose(CodeGenFunction &CGF, llvm::Value *field) = 0;
735 };
736
737 llvm::FoldingSet<ByrefHelpers> ByrefHelpersCache;
John McCallad7c5c12011-02-08 08:22:06 +0000738
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000739 /// Fetches the global unique block count.
John McCall147d0212011-02-22 22:38:33 +0000740 int getUniqueBlockCount() { return ++Block.GlobalUniqueCount; }
Fariborz Jahanian63628032012-06-26 16:06:38 +0000741
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000742 /// Fetches the type of a generic block descriptor.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000743 llvm::Type *getBlockDescriptorType();
John McCallad7c5c12011-02-08 08:22:06 +0000744
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000745 /// The type of a generic block literal.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000746 llvm::Type *getGenericBlockLiteralType();
John McCallad7c5c12011-02-08 08:22:06 +0000747
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000748 /// Gets the address of a block which requires no captures.
John McCallad7c5c12011-02-08 08:22:06 +0000749 llvm::Constant *GetAddrOfGlobalBlock(const BlockExpr *BE, const char *);
Anders Carlsson8a64c1c2010-04-24 21:23:59 +0000750
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000751 /// Return a pointer to a constant CFString object for the given string.
Steve Naroffe14b3682009-04-01 13:55:36 +0000752 llvm::Constant *GetAddrOfConstantCFString(const StringLiteral *Literal);
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000753
754 /// Return a pointer to a constant NSString object for the given string. Or a
755 /// user defined String object as defined via
Fariborz Jahanian50c925f2010-10-19 17:19:29 +0000756 /// -fconstant-string-class=class_name option.
757 llvm::Constant *GetAddrOfConstantString(const StringLiteral *Literal);
Chris Lattner36fc8792008-02-11 00:02:17 +0000758
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000759 /// Return a constant array for the given string.
Eli Friedmanfcec6302011-11-01 02:23:42 +0000760 llvm::Constant *GetConstantArrayFromStringLiteral(const StringLiteral *E);
761
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000762 /// Return a pointer to a constant array for the given string literal.
Alexey Samsonovb2cc23d2014-07-10 22:18:36 +0000763 llvm::GlobalVariable *
764 GetAddrOfConstantStringFromLiteral(const StringLiteral *S);
Daniel Dunbar6dfdf8c2008-08-10 20:25:57 +0000765
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000766 /// Return a pointer to a constant array for the given ObjCEncodeExpr node.
Alexey Samsonovb2cc23d2014-07-10 22:18:36 +0000767 llvm::GlobalVariable *
768 GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *);
Mike Stump11289f42009-09-09 15:08:12 +0000769
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000770 /// Returns a pointer to a character array containing the literal and a
771 /// terminating '\0' character. The result has pointer to array type.
Daniel Dunbardfcf5992008-10-17 21:56:50 +0000772 ///
Mike Stump1676c5b2009-02-13 19:12:34 +0000773 /// \param GlobalName If provided, the name to use for the global (if one is
774 /// created).
Alexey Samsonovb2cc23d2014-07-10 22:18:36 +0000775 llvm::GlobalVariable *
776 GetAddrOfConstantCString(const std::string &Str,
777 const char *GlobalName = nullptr,
778 unsigned Alignment = 0);
Richard Smith2d988f02011-11-22 22:48:32 +0000779
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000780 /// Returns a pointer to a constant global variable for the given file-scope
781 /// compound literal expression.
Richard Smith2d988f02011-11-22 22:48:32 +0000782 llvm::Constant *GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr*E);
Richard Smithe6c01442013-06-05 00:46:14 +0000783
784 /// \brief Returns a pointer to a global variable representing a temporary
785 /// with static or thread storage duration.
786 llvm::Constant *GetAddrOfGlobalTemporary(const MaterializeTemporaryExpr *E,
787 const Expr *Inner);
788
Douglas Gregor636e2002011-08-09 17:23:49 +0000789 /// \brief Retrieve the record type that describes the state of an
790 /// Objective-C fast enumeration loop (for..in).
791 QualType getObjCFastEnumerationStateType();
792
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000793 /// Return the address of the constructor of the given type.
Craig Topper8a13c412014-05-21 05:09:00 +0000794 llvm::GlobalValue *
795 GetAddrOfCXXConstructor(const CXXConstructorDecl *ctor, CXXCtorType ctorType,
796 const CGFunctionInfo *fnInfo = nullptr,
797 bool DontDefer = false);
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000798
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000799 /// Return the address of the constructor of the given type.
Craig Topper8a13c412014-05-21 05:09:00 +0000800 llvm::GlobalValue *
801 GetAddrOfCXXDestructor(const CXXDestructorDecl *dtor,
802 CXXDtorType dtorType,
803 const CGFunctionInfo *fnInfo = nullptr,
804 llvm::FunctionType *fnType = nullptr,
805 bool DontDefer = false);
Mike Stump11289f42009-09-09 15:08:12 +0000806
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000807 /// Given a builtin id for a function like "__builtin_fabsf", return a
808 /// Function* for "fabsf".
Daniel Dunbarff0553e2009-09-14 04:33:21 +0000809 llvm::Value *getBuiltinLibFunction(const FunctionDecl *FD,
810 unsigned BuiltinID);
Daniel Dunbarc4baa062008-08-13 23:20:05 +0000811
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000812 llvm::Function *getIntrinsic(unsigned IID, ArrayRef<llvm::Type*> Tys = None);
Daniel Dunbar9c426522008-07-29 23:18:29 +0000813
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000814 /// Emit code for a single top level declaration.
Daniel Dunbarfce4be82008-08-15 23:26:23 +0000815 void EmitTopLevelDecl(Decl *D);
Daniel Dunbar9c426522008-07-29 23:18:29 +0000816
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000817 /// Tell the consumer that this variable has been instantiated.
Rafael Espindoladf88f6f2012-03-08 15:51:03 +0000818 void HandleCXXStaticMemberVarInstantiation(VarDecl *VD);
Rafael Espindola189fa742012-03-05 10:54:55 +0000819
Richard Smithdf931ce2013-04-06 05:00:46 +0000820 /// \brief If the declaration has internal linkage but is inside an
821 /// extern "C" linkage specification, prepare to emit an alias for it
822 /// to the expected name.
823 template<typename SomeDecl>
824 void MaybeHandleStaticInExternC(const SomeDecl *D, llvm::GlobalValue *GV);
825
Rafael Espindola060062a2014-03-06 22:15:10 +0000826 /// Add a global to a list to be added to the llvm.used metadata.
827 void addUsedGlobal(llvm::GlobalValue *GV);
828
829 /// Add a global to a list to be added to the llvm.compiler.used metadata.
830 void addCompilerUsedGlobal(llvm::GlobalValue *GV);
Daniel Dunbar08b26a02009-02-13 20:29:50 +0000831
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000832 /// Add a destructor and object to add to the C++ global destructor function.
Chris Lattner87233f72010-06-19 05:52:45 +0000833 void AddCXXDtorEntry(llvm::Constant *DtorFn, llvm::Constant *Object) {
834 CXXGlobalDtors.push_back(std::make_pair(DtorFn, Object));
835 }
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000836
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000837 /// Create a new runtime function with the specified type and name.
Chris Lattner2192fe52011-07-18 04:24:23 +0000838 llvm::Constant *CreateRuntimeFunction(llvm::FunctionType *Ty,
Chris Lattner01cf8db2011-07-20 06:58:45 +0000839 StringRef Name,
Bill Wendling8594fcb2013-01-31 00:30:05 +0000840 llvm::AttributeSet ExtraAttrs =
841 llvm::AttributeSet());
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000842 /// Create a new runtime global variable with the specified type and name.
Chris Lattner2192fe52011-07-18 04:24:23 +0000843 llvm::Constant *CreateRuntimeVariable(llvm::Type *Ty,
Chris Lattner01cf8db2011-07-20 06:58:45 +0000844 StringRef Name);
Daniel Dunbar23fd4622008-10-01 00:49:24 +0000845
Daniel Dunbar900546d2010-07-16 00:00:15 +0000846 ///@name Custom Blocks Runtime Interfaces
847 ///@{
848
849 llvm::Constant *getNSConcreteGlobalBlock();
850 llvm::Constant *getNSConcreteStackBlock();
851 llvm::Constant *getBlockObjectAssign();
852 llvm::Constant *getBlockObjectDispose();
853
854 ///@}
855
Nadav Rotem1da30942013-03-23 06:43:35 +0000856 llvm::Constant *getLLVMLifetimeStartFn();
857 llvm::Constant *getLLVMLifetimeEndFn();
858
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000859 // Make sure that this type is translated.
Devang Patel945b8ae2011-03-23 16:29:39 +0000860 void UpdateCompletedType(const TagDecl *TD);
Daniel Dunbar38ad1e62009-02-17 18:43:32 +0000861
John McCallf3a88602011-02-03 08:15:49 +0000862 llvm::Constant *getMemberPointerConstant(const UnaryOperator *e);
863
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000864 /// Try to emit the initializer for the given declaration as a constant;
865 /// returns 0 if the expression cannot be emitted as a constant.
Craig Topper8a13c412014-05-21 05:09:00 +0000866 llvm::Constant *EmitConstantInit(const VarDecl &D,
867 CodeGenFunction *CGF = nullptr);
Richard Smithdafff942012-01-14 04:30:29 +0000868
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000869 /// Try to emit the given expression as a constant; returns 0 if the
870 /// expression cannot be emitted as a constant.
Anders Carlsson80f97ab2009-04-08 04:48:15 +0000871 llvm::Constant *EmitConstantExpr(const Expr *E, QualType DestType,
Craig Topper8a13c412014-05-21 05:09:00 +0000872 CodeGenFunction *CGF = nullptr);
Daniel Dunbar38ad1e62009-02-17 18:43:32 +0000873
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000874 /// Emit the given constant value as a constant, in the type's scalar
875 /// representation.
Richard Smithdafff942012-01-14 04:30:29 +0000876 llvm::Constant *EmitConstantValue(const APValue &Value, QualType DestType,
Craig Topper8a13c412014-05-21 05:09:00 +0000877 CodeGenFunction *CGF = nullptr);
Richard Smithdafff942012-01-14 04:30:29 +0000878
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000879 /// Emit the given constant value as a constant, in the type's memory
880 /// representation.
Richard Smithbc6387672012-03-02 23:27:11 +0000881 llvm::Constant *EmitConstantValueForMemory(const APValue &Value,
882 QualType DestType,
Craig Topper8a13c412014-05-21 05:09:00 +0000883 CodeGenFunction *CGF = nullptr);
Richard Smithbc6387672012-03-02 23:27:11 +0000884
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000885 /// Return the result of value-initializing the given type, i.e. a null
886 /// expression of the given type. This is usually, but not always, an LLVM
887 /// null constant.
Eli Friedman20bb5e02009-04-13 21:47:26 +0000888 llvm::Constant *EmitNullConstant(QualType T);
889
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000890 /// Return a null constant appropriate for zero-initializing a base class with
891 /// the given type. This is usually, but not always, an LLVM null constant.
Eli Friedmanfde961d2011-10-14 02:27:24 +0000892 llvm::Constant *EmitNullConstantForBase(const CXXRecordDecl *Record);
893
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000894 /// Emit a general error that something can't be done.
Chandler Carruth84537952012-03-30 19:44:53 +0000895 void Error(SourceLocation loc, StringRef error);
John McCall7ef5cb32011-03-18 02:56:14 +0000896
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000897 /// Print out an error that codegen doesn't support the specified stmt yet.
David Blaikie4a9ec7b2013-08-19 21:02:26 +0000898 void ErrorUnsupported(const Stmt *S, const char *Type);
Mike Stump1676c5b2009-02-13 19:12:34 +0000899
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000900 /// Print out an error that codegen doesn't support the specified decl yet.
David Blaikie4a9ec7b2013-08-19 21:02:26 +0000901 void ErrorUnsupported(const Decl *D, const char *Type);
Dan Gohman75d69da2008-05-22 00:50:06 +0000902
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000903 /// Set the attributes on the LLVM function for the given decl and function
904 /// info. This applies attributes necessary for handling the ABI as well as
905 /// user specified attributes like section.
Daniel Dunbarc3e7cff2009-04-17 00:48:04 +0000906 void SetInternalFunctionAttributes(const Decl *D, llvm::Function *F,
907 const CGFunctionInfo &FI);
Daniel Dunbar449a3392008-09-04 23:41:35 +0000908
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000909 /// Set the LLVM function attributes (sext, zext, etc).
Daniel Dunbaraeddffc2009-04-14 07:08:30 +0000910 void SetLLVMFunctionAttributes(const Decl *D,
911 const CGFunctionInfo &Info,
912 llvm::Function *F);
Daniel Dunbarc68897d2008-09-10 00:41:16 +0000913
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000914 /// Set the LLVM function attributes which only apply to a function
915 /// definintion.
Daniel Dunbar38932572009-04-14 08:05:55 +0000916 void SetLLVMFunctionAttributesForDefinition(const Decl *D, llvm::Function *F);
917
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000918 /// Return true iff the given type uses 'sret' when used as a return type.
Daniel Dunbar6f2e8392010-07-14 23:39:36 +0000919 bool ReturnTypeUsesSRet(const CGFunctionInfo &FI);
920
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000921 /// Return true iff the given type uses an argument slot when 'sret' is used
922 /// as a return type.
Tim Northovere77cc392014-03-29 13:28:05 +0000923 bool ReturnSlotInterferesWithArgs(const CGFunctionInfo &FI);
924
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000925 /// Return true iff the given type uses 'fpret' when used as a return type.
Daniel Dunbar6f2e8392010-07-14 23:39:36 +0000926 bool ReturnTypeUsesFPRet(QualType ResultType);
Daniel Dunbarc68897d2008-09-10 00:41:16 +0000927
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000928 /// Return true iff the given type uses 'fp2ret' when used as a return type.
Anders Carlsson2f1a6c32011-10-31 16:27:11 +0000929 bool ReturnTypeUsesFP2Ret(QualType ResultType);
930
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000931 /// Get the LLVM attributes and calling convention to use for a particular
932 /// function type.
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000933 ///
934 /// \param Info - The function type information.
935 /// \param TargetDecl - The decl these attributes are being constructed
936 /// for. If supplied the attributes applied to this decl may contribute to the
937 /// function attributes and calling convention.
938 /// \param PAL [out] - On return, the attribute list to use.
939 /// \param CallingConv [out] - On return, the LLVM calling convention to use.
Daniel Dunbard931a872009-02-02 22:03:45 +0000940 void ConstructAttributeList(const CGFunctionInfo &Info,
941 const Decl *TargetDecl,
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000942 AttributeListType &PAL,
Bill Wendlingf4d64cb2013-02-22 00:13:35 +0000943 unsigned &CallingConv,
944 bool AttrOnCallSite);
Daniel Dunbarc68897d2008-09-10 00:41:16 +0000945
Chris Lattner01cf8db2011-07-20 06:58:45 +0000946 StringRef getMangledName(GlobalDecl GD);
Alp Tokerfb8d02b2014-06-05 22:10:59 +0000947 StringRef getBlockMangledName(GlobalDecl GD, const BlockDecl *BD);
Douglas Gregorbeecd582009-04-21 17:11:58 +0000948
949 void EmitTentativeDefinition(const VarDecl *D);
Mike Stumpbc78a722009-07-31 18:25:34 +0000950
Douglas Gregor88d292c2010-05-13 16:44:06 +0000951 void EmitVTable(CXXRecordDecl *Class, bool DefinitionRequired);
952
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000953 /// Emit the RTTI descriptors for the builtin types.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +0000954 void EmitFundamentalRTTIDescriptors();
955
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000956 /// \brief Appends Opts to the "Linker Options" metadata value.
957 void AppendLinkerOptions(StringRef Opts);
958
Benjamin Kramer82dfc972013-06-04 09:13:21 +0000959 /// \brief Appends a detect mismatch command to the linker options.
Aaron Ballman5d041be2013-06-04 02:07:14 +0000960 void AddDetectMismatch(StringRef Name, StringRef Value);
961
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000962 /// \brief Appends a dependent lib to the "Linker Options" metadata value.
963 void AddDependentLib(StringRef Lib);
964
Peter Collingbourne4d90dba2013-06-05 17:49:37 +0000965 llvm::GlobalVariable::LinkageTypes getFunctionLinkage(GlobalDecl GD);
John McCalld4324142010-02-19 01:32:20 +0000966
Rafael Espindola64926362014-05-08 14:46:46 +0000967 void setFunctionLinkage(GlobalDecl GD, llvm::Function *F) {
968 F->setLinkage(getFunctionLinkage(GD));
John McCall7cb02202010-05-25 04:30:21 +0000969 }
970
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000971 /// Return the appropriate linkage for the vtable, VTT, and type information
972 /// of the given class.
Anders Carlsson68d34242011-01-24 02:04:33 +0000973 llvm::GlobalVariable::LinkageTypes getVTableLinkage(const CXXRecordDecl *RD);
Ken Dyck98ca7942010-01-26 13:48:07 +0000974
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000975 /// Return the store size, in character units, of the given LLVM type.
Chris Lattner2192fe52011-07-18 04:24:23 +0000976 CharUnits GetTargetTypeStoreSize(llvm::Type *Ty) const;
Fariborz Jahanian1518a5ec2010-10-27 16:21:54 +0000977
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000978 /// Returns LLVM linkage for a declarator.
David Majnemer27d69db2014-04-28 22:17:59 +0000979 llvm::GlobalValue::LinkageTypes
Hans Wennborg1a3a0742014-05-14 19:54:53 +0000980 getLLVMLinkageForDeclarator(const DeclaratorDecl *D, GVALinkage Linkage,
Hans Wennborg275efb92014-05-28 01:52:23 +0000981 bool IsConstantVariable);
David Majnemer27d69db2014-04-28 22:17:59 +0000982
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000983 /// Returns LLVM linkage for a declarator.
David Majnemer27d69db2014-04-28 22:17:59 +0000984 llvm::GlobalValue::LinkageTypes
985 getLLVMLinkageVarDefinition(const VarDecl *VD, bool IsConstant);
986
Julien Lerouge5a6b6982011-09-09 22:41:49 +0000987 /// Emit all the global annotations.
988 void EmitGlobalAnnotations();
989
990 /// Emit an annotation string.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000991 llvm::Constant *EmitAnnotationString(StringRef Str);
Julien Lerouge5a6b6982011-09-09 22:41:49 +0000992
993 /// Emit the annotation's translation unit.
994 llvm::Constant *EmitAnnotationUnit(SourceLocation Loc);
995
996 /// Emit the annotation line number.
997 llvm::Constant *EmitAnnotationLineNo(SourceLocation L);
998
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000999 /// Generate the llvm::ConstantStruct which contains the annotation
1000 /// information for a given GlobalValue. The annotation struct is
Julien Lerouge5a6b6982011-09-09 22:41:49 +00001001 /// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the
1002 /// GlobalValue being annotated. The second field is the constant string
1003 /// created from the AnnotateAttr's annotation. The third field is a constant
1004 /// string containing the name of the translation unit. The fourth field is
1005 /// the line number in the file of the annotated value declaration.
1006 llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV,
1007 const AnnotateAttr *AA,
1008 SourceLocation L);
1009
1010 /// Add global annotations that are set on D, for the global GV. Those
1011 /// annotations are emitted during finalization of the LLVM code.
1012 void AddGlobalAnnotations(const ValueDecl *D, llvm::GlobalValue *GV);
1013
Alexey Samsonovb7dd3292014-07-09 19:40:08 +00001014 const SanitizerBlacklist &getSanitizerBlacklist() const {
1015 return SanitizerBL;
Will Dietzf54319c2013-01-18 11:30:38 +00001016 }
1017
Alexey Samsonov15c96692014-07-12 00:42:52 +00001018 void reportGlobalToASan(llvm::GlobalVariable *GV, const VarDecl &D,
Alexey Samsonov4f319cc2014-07-02 16:54:41 +00001019 bool IsDynInit = false);
Alexey Samsonov15c96692014-07-12 00:42:52 +00001020 void reportGlobalToASan(llvm::GlobalVariable *GV, SourceLocation Loc,
Alexey Samsonov6c124142014-07-18 17:50:06 +00001021 StringRef Name, bool IsDynInit = false,
1022 bool IsBlacklisted = false);
1023
1024 /// Disable sanitizer instrumentation for this global.
1025 void disableSanitizerForGlobal(llvm::GlobalVariable *GV);
Alexey Samsonov4f319cc2014-07-02 16:54:41 +00001026
John McCall6bd2a892013-01-25 22:31:03 +00001027 void addDeferredVTable(const CXXRecordDecl *RD) {
1028 DeferredVTables.push_back(RD);
1029 }
1030
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001031 /// Emit code for a singal global function or var decl. Forward declarations
1032 /// are emitted lazily.
Reid Klecknere7de47e2013-07-22 13:51:44 +00001033 void EmitGlobal(GlobalDecl D);
1034
Chris Lattner5bcdf242007-12-02 07:09:19 +00001035private:
Chris Lattner01cf8db2011-07-20 06:58:45 +00001036 llvm::GlobalValue *GetGlobalValue(StringRef Ref);
Mike Stump11289f42009-09-09 15:08:12 +00001037
Rafael Espindola94abb8f2013-12-09 04:29:47 +00001038 llvm::Constant *
1039 GetOrCreateLLVMFunction(StringRef MangledName, llvm::Type *Ty, GlobalDecl D,
1040 bool ForVTable, bool DontDefer = false,
1041 llvm::AttributeSet ExtraAttrs = llvm::AttributeSet());
1042
Chris Lattner01cf8db2011-07-20 06:58:45 +00001043 llvm::Constant *GetOrCreateLLVMGlobal(StringRef MangledName,
Chris Lattner2192fe52011-07-18 04:24:23 +00001044 llvm::PointerType *PTy,
Rafael Espindolab73c9732014-05-22 23:33:27 +00001045 const VarDecl *D);
Mike Stump11289f42009-09-09 15:08:12 +00001046
Alexey Samsonov175e52f2014-06-04 19:56:57 +00001047 llvm::StringMapEntry<llvm::GlobalVariable *> *
1048 getConstantStringMapEntry(StringRef Str, int CharByteWidth);
1049
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001050 /// Set attributes which are common to any form of a global definition (alias,
1051 /// Objective-C method, function, global variable).
Daniel Dunbaraeddffc2009-04-14 07:08:30 +00001052 ///
Daniel Dunbar38932572009-04-14 08:05:55 +00001053 /// NOTE: This should only be called for definitions.
1054 void SetCommonAttributes(const Decl *D, llvm::GlobalValue *GV);
Daniel Dunbar449a3392008-09-04 23:41:35 +00001055
Rafael Espindolaee076a22014-05-13 18:45:53 +00001056 void setNonAliasAttributes(const Decl *D, llvm::GlobalObject *GO);
Rafael Espindola502f65a2014-05-05 20:21:03 +00001057
Rafael Espindolae033c8c2014-05-08 15:26:12 +00001058 /// Set attributes for a global definition.
1059 void setFunctionDefinitionAttributes(const FunctionDecl *D,
1060 llvm::Function *F);
Mike Stump11289f42009-09-09 15:08:12 +00001061
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001062 /// Set function attributes for a function declaration.
Anders Carlsson6710c532010-02-06 02:44:09 +00001063 void SetFunctionAttributes(GlobalDecl GD,
Eli Friedman895771a2009-05-26 01:22:57 +00001064 llvm::Function *F,
1065 bool IsIncompleteFunction);
Nuno Lopesb6f79532008-06-08 15:45:52 +00001066
Craig Topper8a13c412014-05-21 05:09:00 +00001067 void EmitGlobalDefinition(GlobalDecl D, llvm::GlobalValue *GV = nullptr);
Daniel Dunbarf0acf7b2009-02-19 07:15:39 +00001068
Rafael Espindolacd7743b2013-12-09 16:01:03 +00001069 void EmitGlobalFunctionDefinition(GlobalDecl GD, llvm::GlobalValue *GV);
Daniel Dunbar9c426522008-07-29 23:18:29 +00001070 void EmitGlobalVarDefinition(const VarDecl *D);
John McCall7ec50432010-03-19 23:29:14 +00001071 void EmitAliasDefinition(GlobalDecl GD);
Daniel Dunbar89654ee2008-08-26 08:29:31 +00001072 void EmitObjCPropertyImplementations(const ObjCImplementationDecl *D);
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00001073 void EmitObjCIvarInitializations(ObjCImplementationDecl *D);
Fariborz Jahanian1518a5ec2010-10-27 16:21:54 +00001074
Anders Carlssonf7475242009-04-15 15:55:24 +00001075 // C++ related functions.
Mike Stump11289f42009-09-09 15:08:12 +00001076
Rafael Espindola3f643bd2013-11-04 18:38:59 +00001077 bool TryEmitDefinitionAsAlias(GlobalDecl Alias, GlobalDecl Target,
1078 bool InEveryTU);
John McCallf8ff7b92010-02-23 00:48:20 +00001079 bool TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D);
John McCalld4324142010-02-19 01:32:20 +00001080
Anders Carlsson237f3492009-04-01 00:58:25 +00001081 void EmitNamespace(const NamespaceDecl *D);
Anders Carlssoncbaeb9e2009-04-02 05:55:18 +00001082 void EmitLinkageSpec(const LinkageSpecDecl *D);
Adrian Prantlffcf4ba2013-05-09 23:16:27 +00001083 void CompleteDIClassType(const CXXMethodDecl* D);
Anders Carlssonf7475242009-04-15 15:55:24 +00001084
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001085 /// Emit a single constructor with the given type from a C++ constructor Decl.
Anders Carlssonf7475242009-04-15 15:55:24 +00001086 void EmitCXXConstructor(const CXXConstructorDecl *D, CXXCtorType Type);
Mike Stump11289f42009-09-09 15:08:12 +00001087
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001088 /// Emit a single destructor with the given type from a C++ destructor Decl.
Anders Carlssoneaa28f72009-04-17 01:58:57 +00001089 void EmitCXXDestructor(const CXXDestructorDecl *D, CXXDtorType Type);
Mike Stump11289f42009-09-09 15:08:12 +00001090
Richard Smith2fd1d7a2013-04-19 16:42:07 +00001091 /// \brief Emit the function that initializes C++ thread_local variables.
1092 void EmitCXXThreadLocalInitFunc();
1093
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001094 /// Emit the function that initializes C++ globals.
Anders Carlssonb8be93f2009-08-08 23:24:23 +00001095 void EmitCXXGlobalInitFunc();
Eli Friedman5866fe32010-01-08 00:50:11 +00001096
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001097 /// Emit the function that destroys C++ globals.
Daniel Dunbarfe06df42010-03-20 04:15:41 +00001098 void EmitCXXGlobalDtorFunc();
1099
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001100 /// Emit the function that initializes the specified global (if PerformInit is
1101 /// true) and registers its destructor.
John McCallcdf7ef52010-11-06 09:44:32 +00001102 void EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
Richard Smith6331c402012-02-13 22:16:19 +00001103 llvm::GlobalVariable *Addr,
1104 bool PerformInit);
Eli Friedman5866fe32010-01-08 00:50:11 +00001105
Reid Kleckner1a711b12014-07-22 00:53:05 +00001106 void EmitPointerToInitFunc(const VarDecl *VD, llvm::GlobalVariable *Addr,
1107 llvm::Function *InitFunc, InitSegAttr *ISA);
1108
Daniel Dunbar74aa7e12008-08-01 00:01:51 +00001109 // FIXME: Hardcoding priority here is gross.
Reid Kleckner563f0e82014-05-23 21:13:45 +00001110 void AddGlobalCtor(llvm::Function *Ctor, int Priority = 65535,
1111 llvm::Constant *AssociatedData = 0);
1112 void AddGlobalDtor(llvm::Function *Dtor, int Priority = 65535);
Daniel Dunbar9c426522008-07-29 23:18:29 +00001113
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001114 /// Generates a global array of functions and priorities using the given list
1115 /// and name. This array will have appending linkage and is suitable for use
1116 /// as a LLVM constructor or destructor array.
Daniel Dunbar74aa7e12008-08-01 00:01:51 +00001117 void EmitCtorList(const CtorList &Fns, const char *GlobalName);
1118
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001119 /// Emit the RTTI descriptors for the given type.
Rafael Espindolaadcc1d12010-03-27 02:52:14 +00001120 void EmitFundamentalRTTIDescriptor(QualType Type);
1121
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001122 /// Emit any needed decls for which code generation was deferred.
Dmitri Gribenkob2aa9232012-11-15 14:28:07 +00001123 void EmitDeferred();
Daniel Dunbar08b26a02009-02-13 20:29:50 +00001124
Rafael Espindola2e2995b2013-11-05 21:37:29 +00001125 /// Call replaceAllUsesWith on all pairs in Replacements.
1126 void applyReplacements();
1127
Rafael Espindola208b5c02013-10-22 19:26:13 +00001128 void checkAliases();
1129
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001130 /// Emit any vtables which we deferred and still have a use for.
John McCall6bd2a892013-01-25 22:31:03 +00001131 void EmitDeferredVTables();
1132
Rafael Espindola060062a2014-03-06 22:15:10 +00001133 /// Emit the llvm.used and llvm.compiler.used metadata.
1134 void emitLLVMUsed();
Daniel Dunbar23fd4622008-10-01 00:49:24 +00001135
Douglas Gregorbc25ff42013-01-14 20:53:57 +00001136 /// \brief Emit the link options introduced by imported modules.
1137 void EmitModuleLinkOptions();
1138
Richard Smithdf931ce2013-04-06 05:00:46 +00001139 /// \brief Emit aliases for internal-linkage declarations inside "C" language
1140 /// linkage specifications, giving them the "expected" name where possible.
1141 void EmitStaticExternCAliases();
1142
John McCall09ae0322010-07-06 23:57:41 +00001143 void EmitDeclMetadata();
1144
Rafael Espindola3bfa4682013-10-16 19:28:50 +00001145 /// \brief Emit the Clang version as llvm.ident metadata.
1146 void EmitVersionIdentMetadata();
1147
Robert Lytton57765d52014-07-03 09:30:33 +00001148 /// Emits target specific Metadata for global declarations.
1149 void EmitTargetMetadata();
1150
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001151 /// Emit the llvm.gcov metadata used to tell LLVM where to emit the .gcno and
1152 /// .gcda files in a way that persists in .bc files.
Nick Lewycky85c011d2011-05-05 00:08:20 +00001153 void EmitCoverageFile();
Nick Lewycky480cb992011-05-04 20:46:58 +00001154
Nico Webercf4ff5862012-10-11 10:13:44 +00001155 /// Emits the initializer for a uuidof string.
1156 llvm::Constant *EmitUuidofInitializer(StringRef uuidstr, QualType IIDType);
1157
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001158 /// Determine if the given decl can be emitted lazily; this is only relevant
1159 /// for definitions. The given decl must be either a function or var decl.
Daniel Dunbar6b8720e2009-02-13 21:18:01 +00001160 bool MayDeferGeneration(const ValueDecl *D);
John McCall0bdb1fd2010-09-16 06:16:50 +00001161
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001162 /// Check whether we can use a "simpler", more core exceptions personality
1163 /// function.
John McCall0bdb1fd2010-09-16 06:16:50 +00001164 void SimplifyPersonality();
Chris Lattnerf97fe382007-05-24 06:29:05 +00001165};
1166} // end namespace CodeGen
1167} // end namespace clang
Chris Lattnerf97fe382007-05-24 06:29:05 +00001168
1169#endif