blob: f8084ef43d9a6bd0f0cc82f65252ebd04bb98bfb [file] [log] [blame]
Daniel Dunbar9c426522008-07-29 23:18:29 +00001//===--- CodeGenModule.h - Per-Module state for LLVM CodeGen ----*- C++ -*-===//
Chris Lattnerf97fe382007-05-24 06:29:05 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerf97fe382007-05-24 06:29:05 +00007//
8//===----------------------------------------------------------------------===//
9//
Mike Stump1676c5b2009-02-13 19:12:34 +000010// This is the internal per-translation-unit state used for llvm translation.
Chris Lattnerf97fe382007-05-24 06:29:05 +000011//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000014#ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H
15#define LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H
Chris Lattnerf97fe382007-05-24 06:29:05 +000016
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "CGVTables.h"
John McCall7f416cc2015-09-08 08:05:57 +000018#include "CodeGenTypeCache.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "CodeGenTypes.h"
Alexey Samsonov4b8de112014-08-01 21:35:28 +000020#include "SanitizerMetadata.h"
Dan Gohman75d69da2008-05-22 00:50:06 +000021#include "clang/AST/Attr.h"
Anders Carlssondae1abc2009-05-05 04:44:02 +000022#include "clang/AST/DeclCXX.h"
Anders Carlsson73fcc952009-09-11 00:07:24 +000023#include "clang/AST/DeclObjC.h"
Peter Collingbourneee781d52011-06-14 04:02:39 +000024#include "clang/AST/GlobalDecl.h"
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000025#include "clang/AST/Mangle.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000026#include "clang/Basic/ABI.h"
27#include "clang/Basic/LangOptions.h"
Douglas Gregor6ddfca92013-01-14 17:21:00 +000028#include "clang/Basic/Module.h"
Alexey Samsonov8d043e82014-10-15 19:57:45 +000029#include "clang/Basic/SanitizerBlacklist.h"
Chris Lattnerb6984c42007-06-20 04:44:43 +000030#include "llvm/ADT/DenseMap.h"
Douglas Gregor6ddfca92013-01-14 17:21:00 +000031#include "llvm/ADT/SetVector.h"
Rafael Espindola2e42fec2010-03-04 18:17:24 +000032#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000033#include "llvm/ADT/StringMap.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000034#include "llvm/IR/Module.h"
Chandler Carruth61743af2014-03-04 11:18:19 +000035#include "llvm/IR/ValueHandle.h"
Peter Collingbournedc134532016-01-16 00:31:22 +000036#include "llvm/Transforms/Utils/SanitizerStats.h"
Anders Carlsson762e1622009-01-04 02:08:04 +000037
Chris Lattnerf97fe382007-05-24 06:29:05 +000038namespace llvm {
Rafael Espindola42ae7452014-05-09 00:57:59 +000039class Module;
40class Constant;
41class ConstantInt;
42class Function;
43class GlobalValue;
44class DataLayout;
45class FunctionType;
46class LLVMContext;
47class IndexedInstrProfReader;
Alexander Kornienkoab9db512015-06-22 23:07:51 +000048}
Chris Lattner23b7eb62007-06-15 23:05:46 +000049
Chris Lattnerf97fe382007-05-24 06:29:05 +000050namespace clang {
Rafael Espindola42ae7452014-05-09 00:57:59 +000051class TargetCodeGenInfo;
52class ASTContext;
53class AtomicType;
54class FunctionDecl;
55class IdentifierInfo;
56class ObjCMethodDecl;
57class ObjCImplementationDecl;
58class ObjCCategoryImplDecl;
59class ObjCProtocolDecl;
60class ObjCEncodeExpr;
61class BlockExpr;
62class CharUnits;
63class Decl;
64class Expr;
65class Stmt;
66class InitListExpr;
67class StringLiteral;
68class NamedDecl;
69class ValueDecl;
70class VarDecl;
71class LangOptions;
72class CodeGenOptions;
Adrian Prantle74f5252015-06-30 02:26:03 +000073class HeaderSearchOptions;
74class PreprocessorOptions;
Rafael Espindola42ae7452014-05-09 00:57:59 +000075class DiagnosticsEngine;
76class AnnotateAttr;
77class CXXDestructorDecl;
Rafael Espindola42ae7452014-05-09 00:57:59 +000078class Module;
Alex Lorenzee024992014-08-04 18:41:51 +000079class CoverageSourceInfo;
Mike Stump1676c5b2009-02-13 19:12:34 +000080
Chris Lattnerf97fe382007-05-24 06:29:05 +000081namespace CodeGen {
82
Rafael Espindola42ae7452014-05-09 00:57:59 +000083class CallArgList;
84class CodeGenFunction;
85class CodeGenTBAA;
86class CGCXXABI;
87class CGDebugInfo;
88class CGObjCRuntime;
89class CGOpenCLRuntime;
90class CGOpenMPRuntime;
91class CGCUDARuntime;
92class BlockFieldFlags;
93class FunctionArgList;
Alex Lorenzee024992014-08-04 18:41:51 +000094class CoverageMappingModuleGen;
Justin Bogneref512b92014-01-06 22:27:43 +000095
Rafael Espindola42ae7452014-05-09 00:57:59 +000096struct OrderGlobalInits {
97 unsigned int priority;
98 unsigned int lex_order;
99 OrderGlobalInits(unsigned int p, unsigned int l)
Chris Lattnere0009072010-06-27 06:32:58 +0000100 : priority(p), lex_order(l) {}
Rafael Espindola42ae7452014-05-09 00:57:59 +0000101
102 bool operator==(const OrderGlobalInits &RHS) const {
103 return priority == RHS.priority && lex_order == RHS.lex_order;
104 }
105
106 bool operator<(const OrderGlobalInits &RHS) const {
107 return std::tie(priority, lex_order) <
108 std::tie(RHS.priority, RHS.lex_order);
109 }
110};
111
John McCallb04ecb72015-10-21 18:06:43 +0000112struct ObjCEntrypoints {
113 ObjCEntrypoints() { memset(this, 0, sizeof(*this)); }
114
115 /// void objc_autoreleasePoolPop(void*);
John McCall31168b02011-06-15 23:02:42 +0000116 llvm::Constant *objc_autoreleasePoolPop;
117
118 /// void *objc_autoreleasePoolPush(void);
119 llvm::Constant *objc_autoreleasePoolPush;
John McCall31168b02011-06-15 23:02:42 +0000120
121 /// id objc_autorelease(id);
122 llvm::Constant *objc_autorelease;
123
124 /// id objc_autoreleaseReturnValue(id);
125 llvm::Constant *objc_autoreleaseReturnValue;
126
127 /// void objc_copyWeak(id *dest, id *src);
128 llvm::Constant *objc_copyWeak;
129
130 /// void objc_destroyWeak(id*);
131 llvm::Constant *objc_destroyWeak;
132
133 /// id objc_initWeak(id*, id);
134 llvm::Constant *objc_initWeak;
135
136 /// id objc_loadWeak(id*);
137 llvm::Constant *objc_loadWeak;
138
139 /// id objc_loadWeakRetained(id*);
140 llvm::Constant *objc_loadWeakRetained;
141
142 /// void objc_moveWeak(id *dest, id *src);
143 llvm::Constant *objc_moveWeak;
144
145 /// id objc_retain(id);
146 llvm::Constant *objc_retain;
147
148 /// id objc_retainAutorelease(id);
149 llvm::Constant *objc_retainAutorelease;
150
151 /// id objc_retainAutoreleaseReturnValue(id);
152 llvm::Constant *objc_retainAutoreleaseReturnValue;
153
154 /// id objc_retainAutoreleasedReturnValue(id);
155 llvm::Constant *objc_retainAutoreleasedReturnValue;
156
157 /// id objc_retainBlock(id);
158 llvm::Constant *objc_retainBlock;
159
160 /// void objc_release(id);
161 llvm::Constant *objc_release;
162
163 /// id objc_storeStrong(id*, id);
164 llvm::Constant *objc_storeStrong;
165
166 /// id objc_storeWeak(id*, id);
167 llvm::Constant *objc_storeWeak;
168
John McCalle399e5b2016-01-27 18:32:30 +0000169 /// id objc_unsafeClaimAutoreleasedReturnValue(id);
170 llvm::Constant *objc_unsafeClaimAutoreleasedReturnValue;
171
John McCall31168b02011-06-15 23:02:42 +0000172 /// A void(void) inline asm to use to mark that the return value of
173 /// a call will be immediately retain.
174 llvm::InlineAsm *retainAutoreleasedReturnValueMarker;
John McCalleff18842013-03-23 02:35:54 +0000175
176 /// void clang.arc.use(...);
177 llvm::Constant *clang_arc_use;
John McCall31168b02011-06-15 23:02:42 +0000178};
Will Dietzf54319c2013-01-18 11:30:38 +0000179
Justin Bognere2ef2a02014-04-15 21:22:35 +0000180/// This class records statistics on instrumentation based profiling.
Justin Bogner40b8ba12014-06-26 01:45:07 +0000181class InstrProfStats {
182 uint32_t VisitedInMainFile;
183 uint32_t MissingInMainFile;
Justin Bognere2ef2a02014-04-15 21:22:35 +0000184 uint32_t Visited;
185 uint32_t Missing;
186 uint32_t Mismatched;
Justin Bogner40b8ba12014-06-26 01:45:07 +0000187
188public:
189 InstrProfStats()
190 : VisitedInMainFile(0), MissingInMainFile(0), Visited(0), Missing(0),
191 Mismatched(0) {}
192 /// Record that we've visited a function and whether or not that function was
193 /// in the main source file.
194 void addVisited(bool MainFile) {
195 if (MainFile)
196 ++VisitedInMainFile;
197 ++Visited;
198 }
199 /// Record that a function we've visited has no profile data.
200 void addMissing(bool MainFile) {
201 if (MainFile)
202 ++MissingInMainFile;
203 ++Missing;
204 }
205 /// Record that a function we've visited has mismatched profile data.
206 void addMismatched(bool MainFile) { ++Mismatched; }
207 /// Whether or not the stats we've gathered indicate any potential problems.
208 bool hasDiagnostics() { return Missing || Mismatched; }
209 /// Report potential problems we've found to \c Diags.
210 void reportDiagnostics(DiagnosticsEngine &Diags, StringRef MainFile);
Justin Bognere2ef2a02014-04-15 21:22:35 +0000211};
212
John McCallbd96e982015-09-08 08:21:11 +0000213/// A pair of helper functions for a __block variable.
214class BlockByrefHelpers : public llvm::FoldingSetNode {
215 // MSVC requires this type to be complete in order to process this
216 // header.
217public:
218 llvm::Constant *CopyHelper;
219 llvm::Constant *DisposeHelper;
220
221 /// The alignment of the field. This is important because
222 /// different offsets to the field within the byref struct need to
223 /// have different helper functions.
224 CharUnits Alignment;
225
226 BlockByrefHelpers(CharUnits alignment) : Alignment(alignment) {}
227 BlockByrefHelpers(const BlockByrefHelpers &) = default;
228 virtual ~BlockByrefHelpers();
229
230 void Profile(llvm::FoldingSetNodeID &id) const {
231 id.AddInteger(Alignment.getQuantity());
232 profileImpl(id);
233 }
234 virtual void profileImpl(llvm::FoldingSetNodeID &id) const = 0;
235
236 virtual bool needsCopy() const { return true; }
237 virtual void emitCopy(CodeGenFunction &CGF, Address dest, Address src) = 0;
238
239 virtual bool needsDispose() const { return true; }
240 virtual void emitDispose(CodeGenFunction &CGF, Address field) = 0;
241};
242
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000243/// This class organizes the cross-function state that is used while generating
244/// LLVM code.
John McCalle3dc1702011-02-15 09:22:45 +0000245class CodeGenModule : public CodeGenTypeCache {
Aaron Ballmanabc18922015-02-15 22:54:08 +0000246 CodeGenModule(const CodeGenModule &) = delete;
247 void operator=(const CodeGenModule &) = delete;
Anders Carlsson729a8202009-02-24 04:21:31 +0000248
Keno Fischer76f4ab02014-12-30 08:12:39 +0000249public:
Reid Kleckner563f0e82014-05-23 21:13:45 +0000250 struct Structor {
251 Structor() : Priority(0), Initializer(nullptr), AssociatedData(nullptr) {}
252 Structor(int Priority, llvm::Constant *Initializer,
253 llvm::Constant *AssociatedData)
254 : Priority(Priority), Initializer(Initializer),
255 AssociatedData(AssociatedData) {}
256 int Priority;
257 llvm::Constant *Initializer;
258 llvm::Constant *AssociatedData;
259 };
260
261 typedef std::vector<Structor> CtorList;
Daniel Dunbar74aa7e12008-08-01 00:01:51 +0000262
Keno Fischer76f4ab02014-12-30 08:12:39 +0000263private:
Chris Lattnerf97fe382007-05-24 06:29:05 +0000264 ASTContext &Context;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000265 const LangOptions &LangOpts;
Adrian Prantle74f5252015-06-30 02:26:03 +0000266 const HeaderSearchOptions &HeaderSearchOpts; // Only used for debug info.
267 const PreprocessorOptions &PreprocessorOpts; // Only used for debug info.
Chandler Carruthbc55fe22009-11-12 17:24:48 +0000268 const CodeGenOptions &CodeGenOpts;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000269 llvm::Module &TheModule;
David Blaikie9c902b52011-09-25 23:23:43 +0000270 DiagnosticsEngine &Diags;
John McCallc8e01702013-04-16 22:48:15 +0000271 const TargetInfo &Target;
Ahmed Charlesb8984322014-03-07 20:03:18 +0000272 std::unique_ptr<CGCXXABI> ABI;
John McCallc8e01702013-04-16 22:48:15 +0000273 llvm::LLVMContext &VMContext;
Anders Carlssonff971e82009-10-07 01:06:45 +0000274
John McCallc8e01702013-04-16 22:48:15 +0000275 CodeGenTBAA *TBAA;
276
277 mutable const TargetCodeGenInfo *TheTargetCodeGenInfo;
278
279 // This should not be moved earlier, since its initialization depends on some
280 // of the previous reference members being already initialized and also checks
281 // if TheTargetCodeGenInfo is NULL
282 CodeGenTypes Types;
283
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000284 /// Holds information about C++ vtables.
Anders Carlssona864caf2010-03-23 04:11:45 +0000285 CodeGenVTables VTables;
Douglas Gregorbc6a4342010-04-08 16:07:47 +0000286
Peter Collingbournee1d20992011-07-27 20:29:46 +0000287 CGObjCRuntime* ObjCRuntime;
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000288 CGOpenCLRuntime* OpenCLRuntime;
Alexey Bataev9959db52014-05-06 10:08:46 +0000289 CGOpenMPRuntime* OpenMPRuntime;
Peter Collingbournefe883422011-10-06 18:29:37 +0000290 CGCUDARuntime* CUDARuntime;
Ted Kremenek2c674f62008-08-05 18:50:11 +0000291 CGDebugInfo* DebugInfo;
John McCallb04ecb72015-10-21 18:06:43 +0000292 ObjCEntrypoints *ObjCData;
Dan Gohman515a60d2012-02-16 00:57:37 +0000293 llvm::MDNode *NoObjCARCExceptionsMetadata;
Justin Bogner837a6f62014-04-18 21:52:00 +0000294 std::unique_ptr<llvm::IndexedInstrProfReader> PGOReader;
Justin Bognere2ef2a02014-04-15 21:22:35 +0000295 InstrProfStats PGOStats;
Peter Collingbournedc134532016-01-16 00:31:22 +0000296 std::unique_ptr<llvm::SanitizerStatReport> SanStats;
Daniel Dunbare49df9b52008-07-30 16:32:24 +0000297
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000298 // A set of references that have only been seen via a weakref so far. This is
299 // used to remove the weak of the reference if we ever see a direct reference
300 // or a definition.
Rafael Espindola2e42fec2010-03-04 18:17:24 +0000301 llvm::SmallPtrSet<llvm::GlobalValue*, 10> WeakRefReferences;
302
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000303 /// This contains all the decls which have definitions but/ which are deferred
304 /// for emission and therefore should only be output if they are actually
305 /// used. If a decl is in this, then it is known to have not been referenced
306 /// yet.
Alp Tokerfb8d02b2014-06-05 22:10:59 +0000307 std::map<StringRef, GlobalDecl> DeferredDecls;
Daniel Dunbar08b26a02009-02-13 20:29:50 +0000308
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000309 /// This is a list of deferred decls which we have seen that *are* actually
310 /// referenced. These get code generated when the module is done.
Rafael Espindolac0ff7442013-12-09 14:59:08 +0000311 struct DeferredGlobal {
312 DeferredGlobal(llvm::GlobalValue *GV, GlobalDecl GD) : GV(GV), GD(GD) {}
Rafael Espindola6565e922015-01-23 05:26:38 +0000313 llvm::TrackingVH<llvm::GlobalValue> GV;
Rafael Espindolac0ff7442013-12-09 14:59:08 +0000314 GlobalDecl GD;
315 };
316 std::vector<DeferredGlobal> DeferredDeclsToEmit;
317 void addDeferredDeclToEmit(llvm::GlobalValue *GV, GlobalDecl GD) {
Benjamin Kramer3204b152015-05-29 19:42:19 +0000318 DeferredDeclsToEmit.emplace_back(GV, GD);
Rafael Espindolac0ff7442013-12-09 14:59:08 +0000319 }
Daniel Dunbar7dd749e2009-04-15 22:08:45 +0000320
Rafael Espindola208b5c02013-10-22 19:26:13 +0000321 /// List of alias we have emitted. Used to make sure that what they point to
322 /// is defined once we get to the end of the of the translation unit.
323 std::vector<GlobalDecl> Aliases;
324
Rafael Espindola2e2995b2013-11-05 21:37:29 +0000325 typedef llvm::StringMap<llvm::TrackingVH<llvm::Constant> > ReplacementsTy;
326 ReplacementsTy Replacements;
327
Andrey Bokhankocab58582015-08-31 13:20:44 +0000328 /// List of global values to be replaced with something else. Used when we
329 /// want to replace a GlobalValue but can't identify it by its mangled name
330 /// anymore (because the name is already taken).
331 llvm::SmallVector<std::pair<llvm::GlobalValue *, llvm::Constant *>, 8>
332 GlobalValReplacements;
333
334 /// Set of global decls for which we already diagnosed mangled name conflict.
335 /// Required to not issue a warning (on a mangling conflict) multiple times
336 /// for the same decl.
337 llvm::DenseSet<GlobalDecl> DiagnosedConflictingDefinitions;
338
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000339 /// A queue of (optional) vtables to consider emitting.
John McCall6bd2a892013-01-25 22:31:03 +0000340 std::vector<const CXXRecordDecl*> DeferredVTables;
341
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000342 /// List of global values which are required to be present in the object file;
343 /// bitcast to i8*. This is used for forcing visibility of symbols which may
344 /// otherwise be optimized out.
Chris Lattnerf41e87f2009-03-31 22:37:52 +0000345 std::vector<llvm::WeakVH> LLVMUsed;
Rafael Espindola060062a2014-03-06 22:15:10 +0000346 std::vector<llvm::WeakVH> LLVMCompilerUsed;
Mike Stump1676c5b2009-02-13 19:12:34 +0000347
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000348 /// Store the list of global constructors and their respective priorities to
349 /// be emitted when the translation unit is complete.
Daniel Dunbar74aa7e12008-08-01 00:01:51 +0000350 CtorList GlobalCtors;
351
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000352 /// Store the list of global destructors and their respective priorities to be
353 /// emitted when the translation unit is complete.
Daniel Dunbar74aa7e12008-08-01 00:01:51 +0000354 CtorList GlobalDtors;
355
Robert Lytton57765d52014-07-03 09:30:33 +0000356 /// An ordered map of canonical GlobalDecls to their mangled names.
357 llvm::MapVector<GlobalDecl, StringRef> MangledDeclNames;
Alp Tokerfb8d02b2014-06-05 22:10:59 +0000358 llvm::StringMap<GlobalDecl, llvm::BumpPtrAllocator> Manglings;
359
Julien Lerouge5a6b6982011-09-09 22:41:49 +0000360 /// Global annotations.
Nate Begeman7fab5782008-04-18 23:43:57 +0000361 std::vector<llvm::Constant*> Annotations;
Mike Stump1676c5b2009-02-13 19:12:34 +0000362
Julien Lerouge5a6b6982011-09-09 22:41:49 +0000363 /// Map used to get unique annotation strings.
364 llvm::StringMap<llvm::Constant*> AnnotationStrings;
365
David Blaikie2e804282015-04-05 22:47:07 +0000366 llvm::StringMap<llvm::GlobalVariable *> CFConstantStringMap;
David Majnemer58e5bee2014-03-24 21:43:36 +0000367
Richard Smith00db2f12014-07-29 21:20:12 +0000368 llvm::DenseMap<llvm::Constant *, llvm::GlobalVariable *> ConstantStringMap;
Eli Friedman1c8d2ca2012-03-09 03:27:46 +0000369 llvm::DenseMap<const Decl*, llvm::Constant *> StaticLocalDeclMap;
John McCallb88a5662012-03-30 21:00:39 +0000370 llvm::DenseMap<const Decl*, llvm::GlobalVariable*> StaticLocalDeclGuardMap;
Richard Smithe6c01442013-06-05 00:46:14 +0000371 llvm::DenseMap<const Expr*, llvm::Constant *> MaterializedGlobalTemporaryMap;
372
Fariborz Jahanian1bed4132012-01-08 19:13:23 +0000373 llvm::DenseMap<QualType, llvm::Constant *> AtomicSetterHelperFnMap;
374 llvm::DenseMap<QualType, llvm::Constant *> AtomicGetterHelperFnMap;
Daniel Dunbard644ad62008-08-23 18:37:06 +0000375
Will Dietz949ec542013-11-08 01:09:22 +0000376 /// Map used to get unique type descriptor constants for sanitizers.
377 llvm::DenseMap<QualType, llvm::Constant *> TypeDescriptorMap;
378
Richard Smithdf931ce2013-04-06 05:00:46 +0000379 /// Map used to track internal linkage functions declared within
380 /// extern "C" regions.
Richard Smithf21c9612013-04-13 01:28:18 +0000381 typedef llvm::MapVector<IdentifierInfo *,
382 llvm::GlobalValue *> StaticExternCMap;
Richard Smithdf931ce2013-04-06 05:00:46 +0000383 StaticExternCMap StaticExternCValues;
384
Richard Smith2fd1d7a2013-04-19 16:42:07 +0000385 /// \brief thread_local variables defined or used in this TU.
Richard Smith5a99c492015-12-01 01:10:48 +0000386 std::vector<const VarDecl *> CXXThreadLocals;
Richard Smith2fd1d7a2013-04-19 16:42:07 +0000387
388 /// \brief thread_local variables with initializers that need to run
389 /// before any thread_local variable in this TU is odr-used.
David Majnemerb3341ea2014-10-05 05:05:40 +0000390 std::vector<llvm::Function *> CXXThreadLocalInits;
Richard Smith5a99c492015-12-01 01:10:48 +0000391 std::vector<const VarDecl *> CXXThreadLocalInitVars;
Richard Smith2fd1d7a2013-04-19 16:42:07 +0000392
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000393 /// Global variables with initializers that need to run before main.
David Majnemerb3341ea2014-10-05 05:05:40 +0000394 std::vector<llvm::Function *> CXXGlobalInits;
John McCall70013b62010-07-15 23:40:35 +0000395
396 /// When a C++ decl with an initializer is deferred, null is
397 /// appended to CXXGlobalInits, and the index of that null is placed
398 /// here so that the initializer will be performed in the correct
Reid Klecknere07140e2015-04-15 01:08:06 +0000399 /// order. Once the decl is emitted, the index is replaced with ~0U to ensure
400 /// that we don't re-emit the initializer.
John McCall70013b62010-07-15 23:40:35 +0000401 llvm::DenseMap<const Decl*, unsigned> DelayedCXXInitPosition;
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +0000402
Anton Korobeynikovabed7492012-11-06 22:44:45 +0000403 typedef std::pair<OrderGlobalInits, llvm::Function*> GlobalInitData;
404
405 struct GlobalInitPriorityCmp {
406 bool operator()(const GlobalInitData &LHS,
407 const GlobalInitData &RHS) const {
408 return LHS.first.priority < RHS.first.priority;
409 }
410 };
411
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000412 /// Global variables with initializers whose order of initialization is set by
413 /// init_priority attribute.
Anton Korobeynikovabed7492012-11-06 22:44:45 +0000414 SmallVector<GlobalInitData, 8> PrioritizedCXXGlobalInits;
Mike Stump11289f42009-09-09 15:08:12 +0000415
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000416 /// Global destructor functions and arguments that need to run on termination.
Chris Lattner87233f72010-06-19 05:52:45 +0000417 std::vector<std::pair<llvm::WeakVH,llvm::Constant*> > CXXGlobalDtors;
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000418
Douglas Gregor6ddfca92013-01-14 17:21:00 +0000419 /// \brief The complete set of modules that has been imported.
420 llvm::SetVector<clang::Module *> ImportedModules;
421
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000422 /// \brief A vector of metadata strings.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000423 SmallVector<llvm::Metadata *, 16> LinkerOptionsMetadata;
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000424
Douglas Gregorabf4e0d2011-08-09 15:54:21 +0000425 /// @name Cache for Objective-C runtime types
426 /// @{
427
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000428 /// Cached reference to the class for constant strings. This value has type
429 /// int * but is actually an Obj-C class pointer.
Fariborz Jahaniand1b67782013-04-16 15:25:39 +0000430 llvm::WeakVH CFConstantStringClassRef;
Mike Stump11289f42009-09-09 15:08:12 +0000431
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000432 /// Cached reference to the class for constant strings. This value has type
433 /// int * but is actually an Obj-C class pointer.
Fariborz Jahaniand1b67782013-04-16 15:25:39 +0000434 llvm::WeakVH ConstantStringClassRef;
Fariborz Jahaniane804c282010-04-23 17:41:07 +0000435
Douglas Gregorabf4e0d2011-08-09 15:54:21 +0000436 /// \brief The LLVM type corresponding to NSConstantString.
437 llvm::StructType *NSConstantStringType;
438
Douglas Gregor636e2002011-08-09 17:23:49 +0000439 /// \brief The type used to describe the state of a fast enumeration in
440 /// Objective-C's for..in loop.
441 QualType ObjCFastEnumerationStateType;
442
Douglas Gregorabf4e0d2011-08-09 15:54:21 +0000443 /// @}
444
David Chisnall481e3a82010-01-23 02:40:42 +0000445 /// Lazily create the Objective-C runtime
446 void createObjCRuntime();
447
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000448 void createOpenCLRuntime();
Alexey Bataev9959db52014-05-06 10:08:46 +0000449 void createOpenMPRuntime();
Peter Collingbournefe883422011-10-06 18:29:37 +0000450 void createCUDARuntime();
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000451
Rafael Espindola4fdc1752011-12-19 14:41:01 +0000452 bool isTriviallyRecursive(const FunctionDecl *F);
Peter Collingbourne4d90dba2013-06-05 17:49:37 +0000453 bool shouldEmitFunction(GlobalDecl GD);
Daniel Dunbar900546d2010-07-16 00:00:15 +0000454
455 /// @name Cache for Blocks Runtime Globals
456 /// @{
457
458 llvm::Constant *NSConcreteGlobalBlock;
459 llvm::Constant *NSConcreteStackBlock;
Daniel Dunbar3348e2d2010-07-16 00:00:19 +0000460
Daniel Dunbar900546d2010-07-16 00:00:15 +0000461 llvm::Constant *BlockObjectAssign;
462 llvm::Constant *BlockObjectDispose;
463
Chris Lattnera5f58b02011-07-09 17:41:47 +0000464 llvm::Type *BlockDescriptorType;
465 llvm::Type *GenericBlockLiteralType;
John McCallad7c5c12011-02-08 08:22:06 +0000466
467 struct {
468 int GlobalUniqueCount;
469 } Block;
Will Dietzf54319c2013-01-18 11:30:38 +0000470
Nadav Rotem1da30942013-03-23 06:43:35 +0000471 /// void @llvm.lifetime.start(i64 %size, i8* nocapture <ptr>)
472 llvm::Constant *LifetimeStartFn;
473
474 /// void @llvm.lifetime.end(i64 %size, i8* nocapture <ptr>)
475 llvm::Constant *LifetimeEndFn;
476
Fariborz Jahanian63628032012-06-26 16:06:38 +0000477 GlobalDecl initializedGlobalDecl;
John McCallad7c5c12011-02-08 08:22:06 +0000478
Alexey Samsonov4b8de112014-08-01 21:35:28 +0000479 std::unique_ptr<SanitizerMetadata> SanitizerMD;
480
Daniel Dunbar900546d2010-07-16 00:00:15 +0000481 /// @}
Alex Lorenzee024992014-08-04 18:41:51 +0000482
483 llvm::DenseMap<const Decl *, bool> DeferredEmptyCoverageMappingDecls;
484
485 std::unique_ptr<CoverageMappingModuleGen> CoverageMapping;
Peter Collingbourne25a80bf2015-09-08 23:01:30 +0000486
487 /// Mapping from canonical types to their metadata identifiers. We need to
488 /// maintain this mapping because identifiers may be formed from distinct
489 /// MDNodes.
490 llvm::DenseMap<QualType, llvm::Metadata *> MetadataIdMap;
491
Chris Lattnerf97fe382007-05-24 06:29:05 +0000492public:
Mehdi Aminica3cf9e2015-07-24 16:04:29 +0000493 CodeGenModule(ASTContext &C, const HeaderSearchOptions &headersearchopts,
Adrian Prantle74f5252015-06-30 02:26:03 +0000494 const PreprocessorOptions &ppopts,
Mehdi Aminica3cf9e2015-07-24 16:04:29 +0000495 const CodeGenOptions &CodeGenOpts, llvm::Module &M,
Alex Lorenzee024992014-08-04 18:41:51 +0000496 DiagnosticsEngine &Diags,
497 CoverageSourceInfo *CoverageInfo = nullptr);
Ted Kremenek2c674f62008-08-05 18:50:11 +0000498
Chris Lattnera087ff92008-03-01 08:45:05 +0000499 ~CodeGenModule();
Mike Stump1676c5b2009-02-13 19:12:34 +0000500
Rafael Espindolac0ff7442013-12-09 14:59:08 +0000501 void clear();
502
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000503 /// Finalize LLVM code generation.
Ted Kremenek2c674f62008-08-05 18:50:11 +0000504 void Release();
Daniel Dunbar8d480592008-08-11 18:12:00 +0000505
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000506 /// Return a reference to the configured Objective-C runtime.
Mike Stump1676c5b2009-02-13 19:12:34 +0000507 CGObjCRuntime &getObjCRuntime() {
Peter Collingbournee1d20992011-07-27 20:29:46 +0000508 if (!ObjCRuntime) createObjCRuntime();
509 return *ObjCRuntime;
Daniel Dunbar8d480592008-08-11 18:12:00 +0000510 }
Mike Stump1676c5b2009-02-13 19:12:34 +0000511
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000512 /// Return true iff an Objective-C runtime has been configured.
Peter Collingbournee1d20992011-07-27 20:29:46 +0000513 bool hasObjCRuntime() { return !!ObjCRuntime; }
Daniel Dunbar8d480592008-08-11 18:12:00 +0000514
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000515 /// Return a reference to the configured OpenCL runtime.
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000516 CGOpenCLRuntime &getOpenCLRuntime() {
Craig Topper8a13c412014-05-21 05:09:00 +0000517 assert(OpenCLRuntime != nullptr);
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000518 return *OpenCLRuntime;
519 }
520
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000521 /// Return a reference to the configured OpenMP runtime.
Alexey Bataev9959db52014-05-06 10:08:46 +0000522 CGOpenMPRuntime &getOpenMPRuntime() {
523 assert(OpenMPRuntime != nullptr);
524 return *OpenMPRuntime;
525 }
526
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000527 /// Return a reference to the configured CUDA runtime.
Peter Collingbournefe883422011-10-06 18:29:37 +0000528 CGCUDARuntime &getCUDARuntime() {
Craig Topper8a13c412014-05-21 05:09:00 +0000529 assert(CUDARuntime != nullptr);
Peter Collingbournefe883422011-10-06 18:29:37 +0000530 return *CUDARuntime;
531 }
532
John McCallb04ecb72015-10-21 18:06:43 +0000533 ObjCEntrypoints &getObjCEntrypoints() const {
534 assert(ObjCData != nullptr);
535 return *ObjCData;
John McCall31168b02011-06-15 23:02:42 +0000536 }
537
Justin Bogner837a6f62014-04-18 21:52:00 +0000538 InstrProfStats &getPGOStats() { return PGOStats; }
539 llvm::IndexedInstrProfReader *getPGOReader() const { return PGOReader.get(); }
Justin Bogneref512b92014-01-06 22:27:43 +0000540
Alex Lorenzee024992014-08-04 18:41:51 +0000541 CoverageMappingModuleGen *getCoverageMapping() const {
542 return CoverageMapping.get();
543 }
544
Eli Friedman1c8d2ca2012-03-09 03:27:46 +0000545 llvm::Constant *getStaticLocalDeclAddress(const VarDecl *D) {
546 return StaticLocalDeclMap[D];
Fariborz Jahanian3fef72f2010-04-18 21:01:23 +0000547 }
Fariborz Jahanian4d55b2d2010-04-19 18:15:02 +0000548 void setStaticLocalDeclAddress(const VarDecl *D,
Eli Friedman1c8d2ca2012-03-09 03:27:46 +0000549 llvm::Constant *C) {
550 StaticLocalDeclMap[D] = C;
Fariborz Jahanian3fef72f2010-04-18 21:01:23 +0000551 }
552
Reid Kleckner453e0562014-10-08 01:07:54 +0000553 llvm::Constant *
554 getOrCreateStaticVarDecl(const VarDecl &D,
555 llvm::GlobalValue::LinkageTypes Linkage);
556
John McCallb88a5662012-03-30 21:00:39 +0000557 llvm::GlobalVariable *getStaticLocalDeclGuardAddress(const VarDecl *D) {
558 return StaticLocalDeclGuardMap[D];
559 }
560 void setStaticLocalDeclGuardAddress(const VarDecl *D,
561 llvm::GlobalVariable *C) {
562 StaticLocalDeclGuardMap[D] = C;
563 }
564
Alp Tokerfb8d02b2014-06-05 22:10:59 +0000565 bool lookupRepresentativeDecl(StringRef MangledName,
566 GlobalDecl &Result) const;
567
Fariborz Jahanian1bed4132012-01-08 19:13:23 +0000568 llvm::Constant *getAtomicSetterHelperFnMap(QualType Ty) {
569 return AtomicSetterHelperFnMap[Ty];
Fariborz Jahanian7ff610b2012-01-06 22:33:54 +0000570 }
Fariborz Jahanian1bed4132012-01-08 19:13:23 +0000571 void setAtomicSetterHelperFnMap(QualType Ty,
Fariborz Jahanian7ff610b2012-01-06 22:33:54 +0000572 llvm::Constant *Fn) {
Fariborz Jahanian1bed4132012-01-08 19:13:23 +0000573 AtomicSetterHelperFnMap[Ty] = Fn;
574 }
575
576 llvm::Constant *getAtomicGetterHelperFnMap(QualType Ty) {
577 return AtomicGetterHelperFnMap[Ty];
578 }
579 void setAtomicGetterHelperFnMap(QualType Ty,
580 llvm::Constant *Fn) {
581 AtomicGetterHelperFnMap[Ty] = Fn;
Fariborz Jahanian7ff610b2012-01-06 22:33:54 +0000582 }
583
Warren Hunt5c2b4ea2014-05-23 16:07:43 +0000584 llvm::Constant *getTypeDescriptorFromMap(QualType Ty) {
Will Dietz949ec542013-11-08 01:09:22 +0000585 return TypeDescriptorMap[Ty];
586 }
Warren Hunt5c2b4ea2014-05-23 16:07:43 +0000587 void setTypeDescriptorInMap(QualType Ty, llvm::Constant *C) {
Will Dietz949ec542013-11-08 01:09:22 +0000588 TypeDescriptorMap[Ty] = C;
589 }
590
Devang Pateld6ffebb2011-03-07 18:45:56 +0000591 CGDebugInfo *getModuleDebugInfo() { return DebugInfo; }
Devang Patele65982c2011-03-07 18:29:53 +0000592
Dan Gohman515a60d2012-02-16 00:57:37 +0000593 llvm::MDNode *getNoObjCARCExceptionsMetadata() {
594 if (!NoObjCARCExceptionsMetadata)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000595 NoObjCARCExceptionsMetadata = llvm::MDNode::get(getLLVMContext(), None);
Dan Gohman515a60d2012-02-16 00:57:37 +0000596 return NoObjCARCExceptionsMetadata;
597 }
598
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000599 ASTContext &getContext() const { return Context; }
David Blaikiebbafb8a2012-03-11 07:00:24 +0000600 const LangOptions &getLangOpts() const { return LangOpts; }
Adrian Prantle74f5252015-06-30 02:26:03 +0000601 const HeaderSearchOptions &getHeaderSearchOpts()
602 const { return HeaderSearchOpts; }
603 const PreprocessorOptions &getPreprocessorOpts()
604 const { return PreprocessorOpts; }
John McCallc8e01702013-04-16 22:48:15 +0000605 const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
Chris Lattner23b7eb62007-06-15 23:05:46 +0000606 llvm::Module &getModule() const { return TheModule; }
David Blaikie9c902b52011-09-25 23:23:43 +0000607 DiagnosticsEngine &getDiags() const { return Diags; }
Mehdi Aminica3cf9e2015-07-24 16:04:29 +0000608 const llvm::DataLayout &getDataLayout() const {
609 return TheModule.getDataLayout();
610 }
John McCallc8e01702013-04-16 22:48:15 +0000611 const TargetInfo &getTarget() const { return Target; }
Rafael Espindola9f834732014-09-19 01:54:22 +0000612 const llvm::Triple &getTriple() const;
613 bool supportsCOMDAT() const;
Rafael Espindoladbee8a72015-01-15 21:36:08 +0000614 void maybeSetTrivialComdat(const Decl &D, llvm::GlobalObject &GO);
Rafael Espindola9f834732014-09-19 01:54:22 +0000615
Alp Toker82862252013-12-28 21:58:40 +0000616 CGCXXABI &getCXXABI() const { return *ABI; }
Owen Anderson170229f2009-07-14 23:10:40 +0000617 llvm::LLVMContext &getLLVMContext() { return VMContext; }
Craig Topper8a13c412014-05-21 05:09:00 +0000618
619 bool shouldUseTBAA() const { return TBAA != nullptr; }
John McCall53fcbd22011-02-26 08:07:02 +0000620
John McCallc8e01702013-04-16 22:48:15 +0000621 const TargetCodeGenInfo &getTargetCodeGenInfo();
622
623 CodeGenTypes &getTypes() { return Types; }
624
625 CodeGenVTables &getVTables() { return VTables; }
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000626
Timur Iskhodzhanov58776632013-11-05 15:54:58 +0000627 ItaniumVTableContext &getItaniumVTableContext() {
628 return VTables.getItaniumVTableContext();
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +0000629 }
John McCallc8e01702013-04-16 22:48:15 +0000630
Timur Iskhodzhanov58776632013-11-05 15:54:58 +0000631 MicrosoftVTableContext &getMicrosoftVTableContext() {
632 return VTables.getMicrosoftVTableContext();
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000633 }
634
Keno Fischer76f4ab02014-12-30 08:12:39 +0000635 CtorList &getGlobalCtors() { return GlobalCtors; }
636 CtorList &getGlobalDtors() { return GlobalDtors; }
637
Dan Gohman947c9af2010-10-14 23:06:10 +0000638 llvm::MDNode *getTBAAInfo(QualType QTy);
Kostya Serebryany141e46f2012-03-26 17:03:51 +0000639 llvm::MDNode *getTBAAInfoForVTablePtr();
Dan Gohman22695fc2012-09-28 21:58:29 +0000640 llvm::MDNode *getTBAAStructInfo(QualType QTy);
Manman Renc451e572013-04-04 21:53:22 +0000641 /// Return the path-aware tag for given base type, access node and offset.
642 llvm::MDNode *getTBAAStructTagInfo(QualType BaseTy, llvm::MDNode *AccessN,
643 uint64_t O);
Dan Gohman947c9af2010-10-14 23:06:10 +0000644
Richard Smithe070fd22012-02-17 06:48:11 +0000645 bool isTypeConstant(QualType QTy, bool ExcludeCtorDtor);
646
John McCalla8ec7eb2013-03-07 21:37:17 +0000647 bool isPaddedAtomicType(QualType type);
648 bool isPaddedAtomicType(const AtomicType *type);
649
Manman Rene1ad74e2013-04-11 23:02:56 +0000650 /// Decorate the instruction with a TBAA tag. For scalar TBAA, the tag
651 /// is the same as the type. For struct-path aware TBAA, the tag
652 /// is different from the type: base type, access type and offset.
653 /// When ConvertTypeToTag is true, we create a tag based on the scalar type.
Piotr Padlewski4b1ac722015-09-15 21:46:55 +0000654 void DecorateInstructionWithTBAA(llvm::Instruction *Inst,
655 llvm::MDNode *TBAAInfo,
656 bool ConvertTypeToTag = true);
657
658 /// Adds !invariant.barrier !tag to instruction
659 void DecorateInstructionWithInvariantGroup(llvm::Instruction *I,
660 const CXXRecordDecl *RD);
Dan Gohman947c9af2010-10-14 23:06:10 +0000661
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000662 /// Emit the given number of characters as a value of type size_t.
John McCall23c29fe2011-06-24 21:55:10 +0000663 llvm::ConstantInt *getSize(CharUnits numChars);
664
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000665 /// Set the visibility for the given LLVM GlobalValue.
Anders Carlssonc6a47892011-01-29 19:39:23 +0000666 void setGlobalVisibility(llvm::GlobalValue *GV, const NamedDecl *D) const;
Daniel Dunbarf5f359f2009-04-14 06:00:08 +0000667
David Majnemerbb525f7c2014-10-15 22:38:23 +0000668 /// Set the TLS mode for the given LLVM GlobalValue for the thread-local
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000669 /// variable declaration D.
David Majnemerbb525f7c2014-10-15 22:38:23 +0000670 void setTLSMode(llvm::GlobalValue *GV, const VarDecl &D) const;
Hans Wennborgf60f6af2012-06-28 08:01:44 +0000671
Anders Carlsson537fdce2011-01-29 20:59:35 +0000672 static llvm::GlobalValue::VisibilityTypes GetLLVMVisibility(Visibility V) {
673 switch (V) {
674 case DefaultVisibility: return llvm::GlobalValue::DefaultVisibility;
675 case HiddenVisibility: return llvm::GlobalValue::HiddenVisibility;
676 case ProtectedVisibility: return llvm::GlobalValue::ProtectedVisibility;
677 }
678 llvm_unreachable("unknown visibility!");
Anders Carlsson537fdce2011-01-29 20:59:35 +0000679 }
680
Andrey Bokhankocab58582015-08-31 13:20:44 +0000681 llvm::Constant *GetAddrOfGlobal(GlobalDecl GD, bool IsForDefinition = false);
John McCalld4324142010-02-19 01:32:20 +0000682
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000683 /// Will return a global variable of the given type. If a variable with a
684 /// different type already exists then a new variable with the right type
685 /// will be created and all uses of the old variable will be replaced with a
686 /// bitcast to the new variable.
Anders Carlssonda80af32011-01-29 18:20:20 +0000687 llvm::GlobalVariable *
Chris Lattner01cf8db2011-07-20 06:58:45 +0000688 CreateOrReplaceCXXRuntimeVariable(StringRef Name, llvm::Type *Ty,
Anders Carlssonda80af32011-01-29 18:20:20 +0000689 llvm::GlobalValue::LinkageTypes Linkage);
690
Alexey Samsonov1444bb92014-10-17 00:20:19 +0000691 llvm::Function *
692 CreateGlobalInitOrDestructFunction(llvm::FunctionType *ty, const Twine &name,
Akira Hatanaka7791f1a42015-10-31 01:28:07 +0000693 const CGFunctionInfo &FI,
Alexey Samsonov1444bb92014-10-17 00:20:19 +0000694 SourceLocation Loc = SourceLocation(),
695 bool TLS = false);
David Majnemerb3341ea2014-10-05 05:05:40 +0000696
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000697 /// Return the address space of the underlying global variable for D, as
698 /// determined by its declaration. Normally this is the same as the address
699 /// space of D's type, but in CUDA, address spaces are associated with
700 /// declarations, not types.
Peter Collingbournef44bdf92012-05-20 21:08:35 +0000701 unsigned GetGlobalVarAddressSpace(const VarDecl *D, unsigned AddrSpace);
702
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000703 /// Return the llvm::Constant for the address of the given global variable.
Andrey Bokhanko7aa88ce2016-01-14 10:41:16 +0000704 /// If Ty is non-null and if the global doesn't exist, then it will be created
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000705 /// with the specified type instead of whatever the normal requested type
Andrey Bokhanko7aa88ce2016-01-14 10:41:16 +0000706 /// would be. If IsForDefinition is true, it is guranteed that an actual
707 /// global with type Ty will be returned, not conversion of a variable with
708 /// the same mangled name but some other type.
Chris Lattner149927c2009-03-21 09:16:30 +0000709 llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D,
Andrey Bokhanko7aa88ce2016-01-14 10:41:16 +0000710 llvm::Type *Ty = nullptr,
711 bool IsForDefinition = false);
Daniel Dunbar9c426522008-07-29 23:18:29 +0000712
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000713 /// Return the address of the given function. If Ty is non-null, then this
714 /// function will use the specified type if it has to create it.
Hans Wennborg59dbe862015-09-29 20:56:43 +0000715 llvm::Constant *GetAddrOfFunction(GlobalDecl GD, llvm::Type *Ty = nullptr,
Rafael Espindola94abb8f2013-12-09 04:29:47 +0000716 bool ForVTable = false,
Andrey Bokhankocab58582015-08-31 13:20:44 +0000717 bool DontDefer = false,
718 bool IsForDefinition = false);
Daniel Dunbarc4baa062008-08-13 23:20:05 +0000719
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000720 /// Get the address of the RTTI descriptor for the given type.
John McCall48bf3492010-04-30 01:15:21 +0000721 llvm::Constant *GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH = false);
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +0000722
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000723 /// Get the address of a uuid descriptor .
John McCall7f416cc2015-09-08 08:05:57 +0000724 ConstantAddress GetAddrOfUuidDescriptor(const CXXUuidofExpr* E);
Nico Webercf4ff5862012-10-11 10:13:44 +0000725
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000726 /// Get the address of the thunk for the given global decl.
Anders Carlssonfe8a9932011-02-06 17:15:43 +0000727 llvm::Constant *GetAddrOfThunk(GlobalDecl GD, const ThunkInfo &Thunk);
Anders Carlssoncd836f02010-03-23 17:17:29 +0000728
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000729 /// Get a reference to the target of VD.
John McCall7f416cc2015-09-08 08:05:57 +0000730 ConstantAddress GetWeakRefReference(const ValueDecl *VD);
731
732 /// Returns the assumed alignment of an opaque pointer to the given class.
733 CharUnits getClassPointerAlignment(const CXXRecordDecl *CD);
734
735 /// Returns the assumed alignment of a virtual base of a class.
736 CharUnits getVBaseAlignment(CharUnits DerivedAlign,
737 const CXXRecordDecl *Derived,
738 const CXXRecordDecl *VBase);
739
740 /// Given a class pointer with an actual known alignment, and the
741 /// expected alignment of an object at a dynamic offset w.r.t that
742 /// pointer, return the alignment to assume at the offset.
743 CharUnits getDynamicOffsetAlignment(CharUnits ActualAlign,
744 const CXXRecordDecl *Class,
745 CharUnits ExpectedTargetAlign);
Rafael Espindola2e42fec2010-03-04 18:17:24 +0000746
David Majnemerc1709d32015-06-23 07:31:11 +0000747 CharUnits
748 computeNonVirtualBaseClassOffset(const CXXRecordDecl *DerivedClass,
749 CastExpr::path_const_iterator Start,
750 CastExpr::path_const_iterator End);
751
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000752 /// Returns the offset from a derived class to a class. Returns null if the
753 /// offset is 0.
Anders Carlsson8a64c1c2010-04-24 21:23:59 +0000754 llvm::Constant *
755 GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl,
John McCallcf142162010-08-07 06:22:56 +0000756 CastExpr::path_const_iterator PathBegin,
757 CastExpr::path_const_iterator PathEnd);
John McCallad7c5c12011-02-08 08:22:06 +0000758
John McCall7f416cc2015-09-08 08:05:57 +0000759 llvm::FoldingSet<BlockByrefHelpers> ByrefHelpersCache;
John McCallad7c5c12011-02-08 08:22:06 +0000760
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000761 /// Fetches the global unique block count.
John McCall147d0212011-02-22 22:38:33 +0000762 int getUniqueBlockCount() { return ++Block.GlobalUniqueCount; }
Fariborz Jahanian63628032012-06-26 16:06:38 +0000763
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000764 /// Fetches the type of a generic block descriptor.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000765 llvm::Type *getBlockDescriptorType();
John McCallad7c5c12011-02-08 08:22:06 +0000766
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000767 /// The type of a generic block literal.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000768 llvm::Type *getGenericBlockLiteralType();
John McCallad7c5c12011-02-08 08:22:06 +0000769
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000770 /// Gets the address of a block which requires no captures.
John McCallad7c5c12011-02-08 08:22:06 +0000771 llvm::Constant *GetAddrOfGlobalBlock(const BlockExpr *BE, const char *);
Anders Carlsson8a64c1c2010-04-24 21:23:59 +0000772
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000773 /// Return a pointer to a constant CFString object for the given string.
John McCall7f416cc2015-09-08 08:05:57 +0000774 ConstantAddress GetAddrOfConstantCFString(const StringLiteral *Literal);
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000775
776 /// Return a pointer to a constant NSString object for the given string. Or a
777 /// user defined String object as defined via
Fariborz Jahanian50c925f2010-10-19 17:19:29 +0000778 /// -fconstant-string-class=class_name option.
John McCall7f416cc2015-09-08 08:05:57 +0000779 ConstantAddress GetAddrOfConstantString(const StringLiteral *Literal);
Chris Lattner36fc8792008-02-11 00:02:17 +0000780
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000781 /// Return a constant array for the given string.
Eli Friedmanfcec6302011-11-01 02:23:42 +0000782 llvm::Constant *GetConstantArrayFromStringLiteral(const StringLiteral *E);
783
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000784 /// Return a pointer to a constant array for the given string literal.
John McCall7f416cc2015-09-08 08:05:57 +0000785 ConstantAddress
Alexey Bataevec474782014-10-09 08:45:04 +0000786 GetAddrOfConstantStringFromLiteral(const StringLiteral *S,
787 StringRef Name = ".str");
Daniel Dunbar6dfdf8c2008-08-10 20:25:57 +0000788
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000789 /// Return a pointer to a constant array for the given ObjCEncodeExpr node.
John McCall7f416cc2015-09-08 08:05:57 +0000790 ConstantAddress
Alexey Samsonovb2cc23d2014-07-10 22:18:36 +0000791 GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *);
Mike Stump11289f42009-09-09 15:08:12 +0000792
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000793 /// Returns a pointer to a character array containing the literal and a
794 /// terminating '\0' character. The result has pointer to array type.
Daniel Dunbardfcf5992008-10-17 21:56:50 +0000795 ///
Mike Stump1676c5b2009-02-13 19:12:34 +0000796 /// \param GlobalName If provided, the name to use for the global (if one is
797 /// created).
John McCall7f416cc2015-09-08 08:05:57 +0000798 ConstantAddress
Alexey Samsonovb2cc23d2014-07-10 22:18:36 +0000799 GetAddrOfConstantCString(const std::string &Str,
John McCall7f416cc2015-09-08 08:05:57 +0000800 const char *GlobalName = nullptr);
Richard Smith2d988f02011-11-22 22:48:32 +0000801
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000802 /// Returns a pointer to a constant global variable for the given file-scope
803 /// compound literal expression.
John McCall7f416cc2015-09-08 08:05:57 +0000804 ConstantAddress GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr*E);
Richard Smithe6c01442013-06-05 00:46:14 +0000805
806 /// \brief Returns a pointer to a global variable representing a temporary
807 /// with static or thread storage duration.
John McCall7f416cc2015-09-08 08:05:57 +0000808 ConstantAddress GetAddrOfGlobalTemporary(const MaterializeTemporaryExpr *E,
Richard Smithe6c01442013-06-05 00:46:14 +0000809 const Expr *Inner);
810
Douglas Gregor636e2002011-08-09 17:23:49 +0000811 /// \brief Retrieve the record type that describes the state of an
812 /// Objective-C fast enumeration loop (for..in).
813 QualType getObjCFastEnumerationStateType();
Rafael Espindola8d2a19b2014-09-08 16:01:27 +0000814
Rafael Espindola53686182014-09-15 19:34:18 +0000815 // Produce code for this constructor/destructor. This method doesn't try
816 // to apply any ABI rules about which other constructors/destructors
817 // are needed or if they are alias to each other.
818 llvm::Function *codegenCXXStructor(const CXXMethodDecl *MD,
819 StructorType Type);
820
Rafael Espindola1ac0ec82014-09-11 15:42:06 +0000821 /// Return the address of the constructor/destructor of the given type.
Andrey Bokhankocab58582015-08-31 13:20:44 +0000822 llvm::Constant *
Rafael Espindola8d2a19b2014-09-08 16:01:27 +0000823 getAddrOfCXXStructor(const CXXMethodDecl *MD, StructorType Type,
824 const CGFunctionInfo *FnInfo = nullptr,
825 llvm::FunctionType *FnType = nullptr,
Andrey Bokhankocab58582015-08-31 13:20:44 +0000826 bool DontDefer = false, bool IsForDefinition = false);
Rafael Espindola8d2a19b2014-09-08 16:01:27 +0000827
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000828 /// Given a builtin id for a function like "__builtin_fabsf", return a
829 /// Function* for "fabsf".
Daniel Dunbarff0553e2009-09-14 04:33:21 +0000830 llvm::Value *getBuiltinLibFunction(const FunctionDecl *FD,
831 unsigned BuiltinID);
Daniel Dunbarc4baa062008-08-13 23:20:05 +0000832
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000833 llvm::Function *getIntrinsic(unsigned IID, ArrayRef<llvm::Type*> Tys = None);
Daniel Dunbar9c426522008-07-29 23:18:29 +0000834
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000835 /// Emit code for a single top level declaration.
Daniel Dunbarfce4be82008-08-15 23:26:23 +0000836 void EmitTopLevelDecl(Decl *D);
Daniel Dunbar9c426522008-07-29 23:18:29 +0000837
Alex Lorenzee024992014-08-04 18:41:51 +0000838 /// \brief Stored a deferred empty coverage mapping for an unused
839 /// and thus uninstrumented top level declaration.
840 void AddDeferredUnusedCoverageMapping(Decl *D);
841
842 /// \brief Remove the deferred empty coverage mapping as this
843 /// declaration is actually instrumented.
844 void ClearUnusedCoverageMapping(const Decl *D);
845
846 /// \brief Emit all the deferred coverage mappings
847 /// for the uninstrumented functions.
848 void EmitDeferredUnusedCoverageMappings();
849
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000850 /// Tell the consumer that this variable has been instantiated.
Rafael Espindoladf88f6f2012-03-08 15:51:03 +0000851 void HandleCXXStaticMemberVarInstantiation(VarDecl *VD);
Rafael Espindola189fa742012-03-05 10:54:55 +0000852
Richard Smithdf931ce2013-04-06 05:00:46 +0000853 /// \brief If the declaration has internal linkage but is inside an
854 /// extern "C" linkage specification, prepare to emit an alias for it
855 /// to the expected name.
856 template<typename SomeDecl>
857 void MaybeHandleStaticInExternC(const SomeDecl *D, llvm::GlobalValue *GV);
858
Rafael Espindola060062a2014-03-06 22:15:10 +0000859 /// Add a global to a list to be added to the llvm.used metadata.
860 void addUsedGlobal(llvm::GlobalValue *GV);
861
862 /// Add a global to a list to be added to the llvm.compiler.used metadata.
863 void addCompilerUsedGlobal(llvm::GlobalValue *GV);
Daniel Dunbar08b26a02009-02-13 20:29:50 +0000864
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000865 /// Add a destructor and object to add to the C++ global destructor function.
Chris Lattner87233f72010-06-19 05:52:45 +0000866 void AddCXXDtorEntry(llvm::Constant *DtorFn, llvm::Constant *Object) {
Benjamin Kramer3204b152015-05-29 19:42:19 +0000867 CXXGlobalDtors.emplace_back(DtorFn, Object);
Chris Lattner87233f72010-06-19 05:52:45 +0000868 }
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000869
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000870 /// Create a new runtime function with the specified type and name.
Chris Lattner2192fe52011-07-18 04:24:23 +0000871 llvm::Constant *CreateRuntimeFunction(llvm::FunctionType *Ty,
Chris Lattner01cf8db2011-07-20 06:58:45 +0000872 StringRef Name,
Bill Wendling8594fcb2013-01-31 00:30:05 +0000873 llvm::AttributeSet ExtraAttrs =
874 llvm::AttributeSet());
Anton Korobeynikovd90dd792014-12-02 16:04:58 +0000875 /// Create a new compiler builtin function with the specified type and name.
876 llvm::Constant *CreateBuiltinFunction(llvm::FunctionType *Ty,
877 StringRef Name,
878 llvm::AttributeSet ExtraAttrs =
879 llvm::AttributeSet());
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000880 /// Create a new runtime global variable with the specified type and name.
Chris Lattner2192fe52011-07-18 04:24:23 +0000881 llvm::Constant *CreateRuntimeVariable(llvm::Type *Ty,
Chris Lattner01cf8db2011-07-20 06:58:45 +0000882 StringRef Name);
Daniel Dunbar23fd4622008-10-01 00:49:24 +0000883
Daniel Dunbar900546d2010-07-16 00:00:15 +0000884 ///@name Custom Blocks Runtime Interfaces
885 ///@{
886
887 llvm::Constant *getNSConcreteGlobalBlock();
888 llvm::Constant *getNSConcreteStackBlock();
889 llvm::Constant *getBlockObjectAssign();
890 llvm::Constant *getBlockObjectDispose();
891
892 ///@}
893
Nadav Rotem1da30942013-03-23 06:43:35 +0000894 llvm::Constant *getLLVMLifetimeStartFn();
895 llvm::Constant *getLLVMLifetimeEndFn();
896
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000897 // Make sure that this type is translated.
Devang Patel945b8ae2011-03-23 16:29:39 +0000898 void UpdateCompletedType(const TagDecl *TD);
Daniel Dunbar38ad1e62009-02-17 18:43:32 +0000899
John McCallf3a88602011-02-03 08:15:49 +0000900 llvm::Constant *getMemberPointerConstant(const UnaryOperator *e);
901
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000902 /// Try to emit the initializer for the given declaration as a constant;
903 /// returns 0 if the expression cannot be emitted as a constant.
Craig Topper8a13c412014-05-21 05:09:00 +0000904 llvm::Constant *EmitConstantInit(const VarDecl &D,
905 CodeGenFunction *CGF = nullptr);
Richard Smithdafff942012-01-14 04:30:29 +0000906
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000907 /// Try to emit the given expression as a constant; returns 0 if the
908 /// expression cannot be emitted as a constant.
Anders Carlsson80f97ab2009-04-08 04:48:15 +0000909 llvm::Constant *EmitConstantExpr(const Expr *E, QualType DestType,
Craig Topper8a13c412014-05-21 05:09:00 +0000910 CodeGenFunction *CGF = nullptr);
Daniel Dunbar38ad1e62009-02-17 18:43:32 +0000911
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000912 /// Emit the given constant value as a constant, in the type's scalar
913 /// representation.
Richard Smithdafff942012-01-14 04:30:29 +0000914 llvm::Constant *EmitConstantValue(const APValue &Value, QualType DestType,
Craig Topper8a13c412014-05-21 05:09:00 +0000915 CodeGenFunction *CGF = nullptr);
Richard Smithdafff942012-01-14 04:30:29 +0000916
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000917 /// Emit the given constant value as a constant, in the type's memory
918 /// representation.
Richard Smithbc6387672012-03-02 23:27:11 +0000919 llvm::Constant *EmitConstantValueForMemory(const APValue &Value,
920 QualType DestType,
Craig Topper8a13c412014-05-21 05:09:00 +0000921 CodeGenFunction *CGF = nullptr);
Richard Smithbc6387672012-03-02 23:27:11 +0000922
Alexey Bataev2bf9b4c2015-10-20 04:24:12 +0000923 /// \brief Emit type info if type of an expression is a variably modified
924 /// type. Also emit proper debug info for cast types.
925 void EmitExplicitCastExprType(const ExplicitCastExpr *E,
926 CodeGenFunction *CGF = nullptr);
927
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000928 /// Return the result of value-initializing the given type, i.e. a null
929 /// expression of the given type. This is usually, but not always, an LLVM
930 /// null constant.
Eli Friedman20bb5e02009-04-13 21:47:26 +0000931 llvm::Constant *EmitNullConstant(QualType T);
932
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000933 /// Return a null constant appropriate for zero-initializing a base class with
934 /// the given type. This is usually, but not always, an LLVM null constant.
Eli Friedmanfde961d2011-10-14 02:27:24 +0000935 llvm::Constant *EmitNullConstantForBase(const CXXRecordDecl *Record);
936
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000937 /// Emit a general error that something can't be done.
Chandler Carruth84537952012-03-30 19:44:53 +0000938 void Error(SourceLocation loc, StringRef error);
John McCall7ef5cb32011-03-18 02:56:14 +0000939
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000940 /// Print out an error that codegen doesn't support the specified stmt yet.
David Blaikie4a9ec7b2013-08-19 21:02:26 +0000941 void ErrorUnsupported(const Stmt *S, const char *Type);
Mike Stump1676c5b2009-02-13 19:12:34 +0000942
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000943 /// Print out an error that codegen doesn't support the specified decl yet.
David Blaikie4a9ec7b2013-08-19 21:02:26 +0000944 void ErrorUnsupported(const Decl *D, const char *Type);
Dan Gohman75d69da2008-05-22 00:50:06 +0000945
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000946 /// Set the attributes on the LLVM function for the given decl and function
947 /// info. This applies attributes necessary for handling the ABI as well as
948 /// user specified attributes like section.
Daniel Dunbarc3e7cff2009-04-17 00:48:04 +0000949 void SetInternalFunctionAttributes(const Decl *D, llvm::Function *F,
950 const CGFunctionInfo &FI);
Daniel Dunbar449a3392008-09-04 23:41:35 +0000951
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000952 /// Set the LLVM function attributes (sext, zext, etc).
Daniel Dunbaraeddffc2009-04-14 07:08:30 +0000953 void SetLLVMFunctionAttributes(const Decl *D,
954 const CGFunctionInfo &Info,
955 llvm::Function *F);
Daniel Dunbarc68897d2008-09-10 00:41:16 +0000956
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000957 /// Set the LLVM function attributes which only apply to a function
James Dennett64fa1232014-08-15 20:04:40 +0000958 /// definition.
Daniel Dunbar38932572009-04-14 08:05:55 +0000959 void SetLLVMFunctionAttributesForDefinition(const Decl *D, llvm::Function *F);
960
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000961 /// Return true iff the given type uses 'sret' when used as a return type.
Daniel Dunbar6f2e8392010-07-14 23:39:36 +0000962 bool ReturnTypeUsesSRet(const CGFunctionInfo &FI);
963
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000964 /// Return true iff the given type uses an argument slot when 'sret' is used
965 /// as a return type.
Tim Northovere77cc392014-03-29 13:28:05 +0000966 bool ReturnSlotInterferesWithArgs(const CGFunctionInfo &FI);
967
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000968 /// Return true iff the given type uses 'fpret' when used as a return type.
Daniel Dunbar6f2e8392010-07-14 23:39:36 +0000969 bool ReturnTypeUsesFPRet(QualType ResultType);
Daniel Dunbarc68897d2008-09-10 00:41:16 +0000970
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000971 /// Return true iff the given type uses 'fp2ret' when used as a return type.
Anders Carlsson2f1a6c32011-10-31 16:27:11 +0000972 bool ReturnTypeUsesFP2Ret(QualType ResultType);
973
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000974 /// Get the LLVM attributes and calling convention to use for a particular
975 /// function type.
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000976 ///
Chad Rosier7dbc9cf2016-01-06 14:35:46 +0000977 /// \param Name - The function name.
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000978 /// \param Info - The function type information.
Samuel Antao798f11c2015-11-23 22:04:44 +0000979 /// \param CalleeInfo - The callee information these attributes are being
980 /// constructed for. If valid, the attributes applied to this decl may
981 /// contribute to the function attributes and calling convention.
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000982 /// \param PAL [out] - On return, the attribute list to use.
983 /// \param CallingConv [out] - On return, the LLVM calling convention to use.
Chad Rosier7dbc9cf2016-01-06 14:35:46 +0000984 void ConstructAttributeList(StringRef Name, const CGFunctionInfo &Info,
Samuel Antao798f11c2015-11-23 22:04:44 +0000985 CGCalleeInfo CalleeInfo, AttributeListType &PAL,
986 unsigned &CallingConv, bool AttrOnCallSite);
Daniel Dunbarc68897d2008-09-10 00:41:16 +0000987
Eric Christopher2b90a642015-11-11 23:05:08 +0000988 // Fills in the supplied string map with the set of target features for the
989 // passed in function.
990 void getFunctionFeatureMap(llvm::StringMap<bool> &FeatureMap,
991 const FunctionDecl *FD);
992
Chris Lattner01cf8db2011-07-20 06:58:45 +0000993 StringRef getMangledName(GlobalDecl GD);
Alp Tokerfb8d02b2014-06-05 22:10:59 +0000994 StringRef getBlockMangledName(GlobalDecl GD, const BlockDecl *BD);
Douglas Gregorbeecd582009-04-21 17:11:58 +0000995
996 void EmitTentativeDefinition(const VarDecl *D);
Mike Stumpbc78a722009-07-31 18:25:34 +0000997
Nico Weberb6a5d052015-01-15 04:07:35 +0000998 void EmitVTable(CXXRecordDecl *Class);
Douglas Gregor88d292c2010-05-13 16:44:06 +0000999
David Majnemer929025d2016-01-26 19:30:26 +00001000 void RefreshTypeCacheForClass(const CXXRecordDecl *Class);
1001
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001002 /// \brief Appends Opts to the "Linker Options" metadata value.
1003 void AppendLinkerOptions(StringRef Opts);
1004
Benjamin Kramer82dfc972013-06-04 09:13:21 +00001005 /// \brief Appends a detect mismatch command to the linker options.
Aaron Ballman5d041be2013-06-04 02:07:14 +00001006 void AddDetectMismatch(StringRef Name, StringRef Value);
1007
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001008 /// \brief Appends a dependent lib to the "Linker Options" metadata value.
1009 void AddDependentLib(StringRef Lib);
1010
Peter Collingbourne4d90dba2013-06-05 17:49:37 +00001011 llvm::GlobalVariable::LinkageTypes getFunctionLinkage(GlobalDecl GD);
John McCalld4324142010-02-19 01:32:20 +00001012
Rafael Espindola64926362014-05-08 14:46:46 +00001013 void setFunctionLinkage(GlobalDecl GD, llvm::Function *F) {
1014 F->setLinkage(getFunctionLinkage(GD));
John McCall7cb02202010-05-25 04:30:21 +00001015 }
1016
Hans Wennborgbb4f9622015-05-28 17:44:56 +00001017 /// Set the DLL storage class on F.
1018 void setFunctionDLLStorageClass(GlobalDecl GD, llvm::Function *F);
1019
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001020 /// Return the appropriate linkage for the vtable, VTT, and type information
1021 /// of the given class.
Anders Carlsson68d34242011-01-24 02:04:33 +00001022 llvm::GlobalVariable::LinkageTypes getVTableLinkage(const CXXRecordDecl *RD);
Ken Dyck98ca7942010-01-26 13:48:07 +00001023
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001024 /// Return the store size, in character units, of the given LLVM type.
Chris Lattner2192fe52011-07-18 04:24:23 +00001025 CharUnits GetTargetTypeStoreSize(llvm::Type *Ty) const;
Fariborz Jahanian1518a5ec2010-10-27 16:21:54 +00001026
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001027 /// Returns LLVM linkage for a declarator.
David Majnemer27d69db2014-04-28 22:17:59 +00001028 llvm::GlobalValue::LinkageTypes
Hans Wennborg1a3a0742014-05-14 19:54:53 +00001029 getLLVMLinkageForDeclarator(const DeclaratorDecl *D, GVALinkage Linkage,
Hans Wennborg275efb92014-05-28 01:52:23 +00001030 bool IsConstantVariable);
David Majnemer27d69db2014-04-28 22:17:59 +00001031
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001032 /// Returns LLVM linkage for a declarator.
David Majnemer27d69db2014-04-28 22:17:59 +00001033 llvm::GlobalValue::LinkageTypes
1034 getLLVMLinkageVarDefinition(const VarDecl *VD, bool IsConstant);
1035
Julien Lerouge5a6b6982011-09-09 22:41:49 +00001036 /// Emit all the global annotations.
1037 void EmitGlobalAnnotations();
1038
1039 /// Emit an annotation string.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001040 llvm::Constant *EmitAnnotationString(StringRef Str);
Julien Lerouge5a6b6982011-09-09 22:41:49 +00001041
1042 /// Emit the annotation's translation unit.
1043 llvm::Constant *EmitAnnotationUnit(SourceLocation Loc);
1044
1045 /// Emit the annotation line number.
1046 llvm::Constant *EmitAnnotationLineNo(SourceLocation L);
1047
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001048 /// Generate the llvm::ConstantStruct which contains the annotation
1049 /// information for a given GlobalValue. The annotation struct is
Julien Lerouge5a6b6982011-09-09 22:41:49 +00001050 /// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the
1051 /// GlobalValue being annotated. The second field is the constant string
1052 /// created from the AnnotateAttr's annotation. The third field is a constant
1053 /// string containing the name of the translation unit. The fourth field is
1054 /// the line number in the file of the annotated value declaration.
1055 llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV,
1056 const AnnotateAttr *AA,
1057 SourceLocation L);
1058
1059 /// Add global annotations that are set on D, for the global GV. Those
1060 /// annotations are emitted during finalization of the LLVM code.
1061 void AddGlobalAnnotations(const ValueDecl *D, llvm::GlobalValue *GV);
1062
Alexey Samsonov1444bb92014-10-17 00:20:19 +00001063 bool isInSanitizerBlacklist(llvm::Function *Fn, SourceLocation Loc) const;
Will Dietzf54319c2013-01-18 11:30:38 +00001064
Alexey Samsonova0ac3c22014-10-17 22:37:33 +00001065 bool isInSanitizerBlacklist(llvm::GlobalVariable *GV, SourceLocation Loc,
1066 QualType Ty,
1067 StringRef Category = StringRef()) const;
1068
Alexey Samsonov4b8de112014-08-01 21:35:28 +00001069 SanitizerMetadata *getSanitizerMetadata() {
1070 return SanitizerMD.get();
1071 }
Alexey Samsonov4f319cc2014-07-02 16:54:41 +00001072
John McCall6bd2a892013-01-25 22:31:03 +00001073 void addDeferredVTable(const CXXRecordDecl *RD) {
1074 DeferredVTables.push_back(RD);
1075 }
1076
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001077 /// Emit code for a singal global function or var decl. Forward declarations
1078 /// are emitted lazily.
Reid Klecknere7de47e2013-07-22 13:51:44 +00001079 void EmitGlobal(GlobalDecl D);
1080
Rafael Espindola02b77f42014-09-15 18:46:13 +00001081 bool TryEmitDefinitionAsAlias(GlobalDecl Alias, GlobalDecl Target,
1082 bool InEveryTU);
1083 bool TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D);
1084
1085 /// Set attributes for a global definition.
1086 void setFunctionDefinitionAttributes(const FunctionDecl *D,
1087 llvm::Function *F);
1088
Chris Lattner01cf8db2011-07-20 06:58:45 +00001089 llvm::GlobalValue *GetGlobalValue(StringRef Ref);
Mike Stump11289f42009-09-09 15:08:12 +00001090
Rafael Espindola1e4df922014-09-16 15:18:21 +00001091 /// Set attributes which are common to any form of a global definition (alias,
1092 /// Objective-C method, function, global variable).
1093 ///
1094 /// NOTE: This should only be called for definitions.
1095 void SetCommonAttributes(const Decl *D, llvm::GlobalValue *GV);
1096
Dario Domiziolic4fb8ca72014-09-19 22:06:24 +00001097 /// Set attributes which must be preserved by an alias. This includes common
1098 /// attributes (i.e. it includes a call to SetCommonAttributes).
1099 ///
1100 /// NOTE: This should only be called for definitions.
1101 void setAliasAttributes(const Decl *D, llvm::GlobalValue *GV);
1102
Rafael Espindola1e4df922014-09-16 15:18:21 +00001103 void addReplacement(StringRef Name, llvm::Constant *C);
Alexey Bataev97720002014-11-11 04:05:39 +00001104
Andrey Bokhankocab58582015-08-31 13:20:44 +00001105 void addGlobalValReplacement(llvm::GlobalValue *GV, llvm::Constant *C);
1106
Alexey Bataev97720002014-11-11 04:05:39 +00001107 /// \brief Emit a code for threadprivate directive.
1108 /// \param D Threadprivate declaration.
1109 void EmitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D);
1110
Peter Collingbournee5706442015-07-09 19:56:14 +00001111 /// Returns whether the given record is blacklisted from control flow
1112 /// integrity checks.
1113 bool IsCFIBlacklistedRecord(const CXXRecordDecl *RD);
1114
Peter Collingbournea4ccff32015-02-20 20:30:56 +00001115 /// Emit bit set entries for the given vtable using the given layout if
1116 /// vptr CFI is enabled.
1117 void EmitVTableBitSetEntries(llvm::GlobalVariable *VTable,
1118 const VTableLayout &VTLayout);
1119
Evgeniy Stepanovfd6f92d2015-12-15 23:00:20 +00001120 /// Generate a cross-DSO type identifier for type.
1121 llvm::ConstantInt *CreateCfiIdForTypeMetadata(llvm::Metadata *MD);
1122
Peter Collingbourne25a80bf2015-09-08 23:01:30 +00001123 /// Create a metadata identifier for the given type. This may either be an
1124 /// MDString (for external identifiers) or a distinct unnamed MDNode (for
1125 /// internal identifiers).
1126 llvm::Metadata *CreateMetadataIdentifierForType(QualType T);
1127
Evgeniy Stepanovfd6f92d2015-12-15 23:00:20 +00001128 /// Create a bitset entry for the given function and add it to BitsetsMD.
1129 void CreateFunctionBitSetEntry(const FunctionDecl *FD, llvm::Function *F);
1130
Evgeniy Stepanovf31ea302016-02-03 22:18:55 +00001131 /// Returns whether this module needs the "all-vtables" bitset.
1132 bool NeedAllVtablesBitSet() const;
1133
Evgeniy Stepanovfd6f92d2015-12-15 23:00:20 +00001134 /// Create a bitset entry for the given vtable and add it to BitsetsMD.
1135 void CreateVTableBitSetEntry(llvm::NamedMDNode *BitsetsMD,
1136 llvm::GlobalVariable *VTable, CharUnits Offset,
1137 const CXXRecordDecl *RD);
Peter Collingbourne86d34a72015-06-17 19:08:05 +00001138
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00001139 /// \breif Get the declaration of std::terminate for the platform.
1140 llvm::Constant *getTerminateFn();
1141
Peter Collingbournedc134532016-01-16 00:31:22 +00001142 llvm::SanitizerStatReport &getSanStats();
1143
Rafael Espindola1e4df922014-09-16 15:18:21 +00001144private:
Rafael Espindola94abb8f2013-12-09 04:29:47 +00001145 llvm::Constant *
1146 GetOrCreateLLVMFunction(StringRef MangledName, llvm::Type *Ty, GlobalDecl D,
1147 bool ForVTable, bool DontDefer = false,
David Majnemerb9bd6fb2014-11-01 05:42:23 +00001148 bool IsThunk = false,
Andrey Bokhankocab58582015-08-31 13:20:44 +00001149 llvm::AttributeSet ExtraAttrs = llvm::AttributeSet(),
1150 bool IsForDefinition = false);
Rafael Espindola94abb8f2013-12-09 04:29:47 +00001151
Chris Lattner01cf8db2011-07-20 06:58:45 +00001152 llvm::Constant *GetOrCreateLLVMGlobal(StringRef MangledName,
Chris Lattner2192fe52011-07-18 04:24:23 +00001153 llvm::PointerType *PTy,
Andrey Bokhanko7aa88ce2016-01-14 10:41:16 +00001154 const VarDecl *D,
1155 bool IsForDefinition = false);
Mike Stump11289f42009-09-09 15:08:12 +00001156
Rafael Espindolaee076a22014-05-13 18:45:53 +00001157 void setNonAliasAttributes(const Decl *D, llvm::GlobalObject *GO);
Rafael Espindola502f65a2014-05-05 20:21:03 +00001158
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001159 /// Set function attributes for a function declaration.
David Majnemerb9bd6fb2014-11-01 05:42:23 +00001160 void SetFunctionAttributes(GlobalDecl GD, llvm::Function *F,
1161 bool IsIncompleteFunction, bool IsThunk);
Nuno Lopesb6f79532008-06-08 15:45:52 +00001162
Craig Topper8a13c412014-05-21 05:09:00 +00001163 void EmitGlobalDefinition(GlobalDecl D, llvm::GlobalValue *GV = nullptr);
Daniel Dunbarf0acf7b2009-02-19 07:15:39 +00001164
Rafael Espindolacd7743b2013-12-09 16:01:03 +00001165 void EmitGlobalFunctionDefinition(GlobalDecl GD, llvm::GlobalValue *GV);
Andrey Bokhanko7aa88ce2016-01-14 10:41:16 +00001166 void EmitGlobalVarDefinition(const VarDecl *D, bool IsTentative = false);
John McCall7ec50432010-03-19 23:29:14 +00001167 void EmitAliasDefinition(GlobalDecl GD);
Daniel Dunbar89654ee2008-08-26 08:29:31 +00001168 void EmitObjCPropertyImplementations(const ObjCImplementationDecl *D);
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00001169 void EmitObjCIvarInitializations(ObjCImplementationDecl *D);
Fariborz Jahanian1518a5ec2010-10-27 16:21:54 +00001170
Anders Carlssonf7475242009-04-15 15:55:24 +00001171 // C++ related functions.
Mike Stump11289f42009-09-09 15:08:12 +00001172
Anders Carlsson237f3492009-04-01 00:58:25 +00001173 void EmitNamespace(const NamespaceDecl *D);
Anders Carlssoncbaeb9e2009-04-02 05:55:18 +00001174 void EmitLinkageSpec(const LinkageSpecDecl *D);
Adrian Prantlffcf4ba2013-05-09 23:16:27 +00001175 void CompleteDIClassType(const CXXMethodDecl* D);
Anders Carlssonf7475242009-04-15 15:55:24 +00001176
Richard Smith2fd1d7a2013-04-19 16:42:07 +00001177 /// \brief Emit the function that initializes C++ thread_local variables.
1178 void EmitCXXThreadLocalInitFunc();
1179
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001180 /// Emit the function that initializes C++ globals.
Anders Carlssonb8be93f2009-08-08 23:24:23 +00001181 void EmitCXXGlobalInitFunc();
Eli Friedman5866fe32010-01-08 00:50:11 +00001182
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001183 /// Emit the function that destroys C++ globals.
Daniel Dunbarfe06df42010-03-20 04:15:41 +00001184 void EmitCXXGlobalDtorFunc();
1185
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001186 /// Emit the function that initializes the specified global (if PerformInit is
1187 /// true) and registers its destructor.
John McCallcdf7ef52010-11-06 09:44:32 +00001188 void EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
Richard Smith6331c402012-02-13 22:16:19 +00001189 llvm::GlobalVariable *Addr,
1190 bool PerformInit);
Eli Friedman5866fe32010-01-08 00:50:11 +00001191
Reid Kleckner1a711b12014-07-22 00:53:05 +00001192 void EmitPointerToInitFunc(const VarDecl *VD, llvm::GlobalVariable *Addr,
1193 llvm::Function *InitFunc, InitSegAttr *ISA);
1194
Daniel Dunbar74aa7e12008-08-01 00:01:51 +00001195 // FIXME: Hardcoding priority here is gross.
Reid Kleckner563f0e82014-05-23 21:13:45 +00001196 void AddGlobalCtor(llvm::Function *Ctor, int Priority = 65535,
Hans Wennborg59dbe862015-09-29 20:56:43 +00001197 llvm::Constant *AssociatedData = nullptr);
Reid Kleckner563f0e82014-05-23 21:13:45 +00001198 void AddGlobalDtor(llvm::Function *Dtor, int Priority = 65535);
Daniel Dunbar9c426522008-07-29 23:18:29 +00001199
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001200 /// Generates a global array of functions and priorities using the given list
1201 /// and name. This array will have appending linkage and is suitable for use
1202 /// as a LLVM constructor or destructor array.
Daniel Dunbar74aa7e12008-08-01 00:01:51 +00001203 void EmitCtorList(const CtorList &Fns, const char *GlobalName);
1204
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001205 /// Emit any needed decls for which code generation was deferred.
Dmitri Gribenkob2aa9232012-11-15 14:28:07 +00001206 void EmitDeferred();
Daniel Dunbar08b26a02009-02-13 20:29:50 +00001207
Rafael Espindola2e2995b2013-11-05 21:37:29 +00001208 /// Call replaceAllUsesWith on all pairs in Replacements.
1209 void applyReplacements();
1210
Andrey Bokhankocab58582015-08-31 13:20:44 +00001211 /// Call replaceAllUsesWith on all pairs in GlobalValReplacements.
1212 void applyGlobalValReplacements();
1213
Rafael Espindola208b5c02013-10-22 19:26:13 +00001214 void checkAliases();
1215
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001216 /// Emit any vtables which we deferred and still have a use for.
John McCall6bd2a892013-01-25 22:31:03 +00001217 void EmitDeferredVTables();
1218
Rafael Espindola060062a2014-03-06 22:15:10 +00001219 /// Emit the llvm.used and llvm.compiler.used metadata.
1220 void emitLLVMUsed();
Daniel Dunbar23fd4622008-10-01 00:49:24 +00001221
Douglas Gregorbc25ff42013-01-14 20:53:57 +00001222 /// \brief Emit the link options introduced by imported modules.
1223 void EmitModuleLinkOptions();
1224
Richard Smithdf931ce2013-04-06 05:00:46 +00001225 /// \brief Emit aliases for internal-linkage declarations inside "C" language
1226 /// linkage specifications, giving them the "expected" name where possible.
1227 void EmitStaticExternCAliases();
1228
John McCall09ae0322010-07-06 23:57:41 +00001229 void EmitDeclMetadata();
1230
Rafael Espindola3bfa4682013-10-16 19:28:50 +00001231 /// \brief Emit the Clang version as llvm.ident metadata.
1232 void EmitVersionIdentMetadata();
1233
Robert Lytton57765d52014-07-03 09:30:33 +00001234 /// Emits target specific Metadata for global declarations.
1235 void EmitTargetMetadata();
1236
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001237 /// Emit the llvm.gcov metadata used to tell LLVM where to emit the .gcno and
1238 /// .gcda files in a way that persists in .bc files.
Nick Lewycky85c011d2011-05-05 00:08:20 +00001239 void EmitCoverageFile();
Nick Lewycky480cb992011-05-04 20:46:58 +00001240
Nico Webercf4ff5862012-10-11 10:13:44 +00001241 /// Emits the initializer for a uuidof string.
Nico Weber17d3a2c2014-09-08 16:26:36 +00001242 llvm::Constant *EmitUuidofInitializer(StringRef uuidstr);
Nico Webercf4ff5862012-10-11 10:13:44 +00001243
Hans Wennborg43a0f992015-01-10 01:19:48 +00001244 /// Determine whether the definition must be emitted; if this returns \c
1245 /// false, the definition can be emitted lazily if it's used.
1246 bool MustBeEmitted(const ValueDecl *D);
1247
1248 /// Determine whether the definition can be emitted eagerly, or should be
1249 /// delayed until the end of the translation unit. This is relevant for
1250 /// definitions whose linkage can change, e.g. implicit function instantions
1251 /// which may later be explicitly instantiated.
1252 bool MayBeEmittedEagerly(const ValueDecl *D);
John McCall0bdb1fd2010-09-16 06:16:50 +00001253
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001254 /// Check whether we can use a "simpler", more core exceptions personality
1255 /// function.
John McCall0bdb1fd2010-09-16 06:16:50 +00001256 void SimplifyPersonality();
Chris Lattnerf97fe382007-05-24 06:29:05 +00001257};
1258} // end namespace CodeGen
1259} // end namespace clang
Chris Lattnerf97fe382007-05-24 06:29:05 +00001260
Hans Wennborg59dbe862015-09-29 20:56:43 +00001261#endif // LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H