blob: e8162fcfc0cde9e63168e7a050d8d3d8cc6ab8ff [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattnerf97fe382007-05-24 06:29:05 +00006//
7//===----------------------------------------------------------------------===//
8//
Mike Stump1676c5b2009-02-13 19:12:34 +00009// This is the internal per-translation-unit state used for llvm translation.
Chris Lattnerf97fe382007-05-24 06:29:05 +000010//
11//===----------------------------------------------------------------------===//
12
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000013#ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H
14#define LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H
Chris Lattnerf97fe382007-05-24 06:29:05 +000015
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "CGVTables.h"
John McCall7f416cc2015-09-08 08:05:57 +000017#include "CodeGenTypeCache.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "CodeGenTypes.h"
Alexey Samsonov4b8de112014-08-01 21:35:28 +000019#include "SanitizerMetadata.h"
Anders Carlssondae1abc2009-05-05 04:44:02 +000020#include "clang/AST/DeclCXX.h"
Anders Carlsson73fcc952009-09-11 00:07:24 +000021#include "clang/AST/DeclObjC.h"
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000022#include "clang/AST/DeclOpenMP.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"
Alexey Samsonov8d043e82014-10-15 19:57:45 +000028#include "clang/Basic/SanitizerBlacklist.h"
Dean Michael Berris835832d2017-03-30 00:29:36 +000029#include "clang/Basic/XRayLists.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;
Johannes Doerfertb3c06db2019-11-04 23:00:36 -060047class OpenMPIRBuilder;
Rafael Espindola42ae7452014-05-09 00:57:59 +000048class IndexedInstrProfReader;
Alexander Kornienkoab9db512015-06-22 23:07:51 +000049}
Chris Lattner23b7eb62007-06-15 23:05:46 +000050
Chris Lattnerf97fe382007-05-24 06:29:05 +000051namespace clang {
Rafael Espindola42ae7452014-05-09 00:57:59 +000052class 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;
Reid Kleckner98031782019-12-09 16:11:56 -080080class TargetAttr;
81class InitSegAttr;
82struct ParsedTargetAttr;
Mike Stump1676c5b2009-02-13 19:12:34 +000083
Chris Lattnerf97fe382007-05-24 06:29:05 +000084namespace CodeGen {
85
Rafael Espindola42ae7452014-05-09 00:57:59 +000086class CallArgList;
87class CodeGenFunction;
88class CodeGenTBAA;
89class CGCXXABI;
90class CGDebugInfo;
91class CGObjCRuntime;
92class CGOpenCLRuntime;
93class CGOpenMPRuntime;
94class CGCUDARuntime;
95class BlockFieldFlags;
96class FunctionArgList;
Alex Lorenzee024992014-08-04 18:41:51 +000097class CoverageMappingModuleGen;
John McCall12f23522016-04-04 18:33:08 +000098class TargetCodeGenInfo;
Justin Bogneref512b92014-01-06 22:27:43 +000099
John McCalld195d4c2016-11-30 23:25:13 +0000100enum ForDefinition_t : bool {
101 NotForDefinition = false,
102 ForDefinition = true
103};
104
Rafael Espindola42ae7452014-05-09 00:57:59 +0000105struct OrderGlobalInits {
106 unsigned int priority;
107 unsigned int lex_order;
108 OrderGlobalInits(unsigned int p, unsigned int l)
Chris Lattnere0009072010-06-27 06:32:58 +0000109 : priority(p), lex_order(l) {}
Rafael Espindola42ae7452014-05-09 00:57:59 +0000110
111 bool operator==(const OrderGlobalInits &RHS) const {
112 return priority == RHS.priority && lex_order == RHS.lex_order;
113 }
114
115 bool operator<(const OrderGlobalInits &RHS) const {
116 return std::tie(priority, lex_order) <
117 std::tie(RHS.priority, RHS.lex_order);
118 }
119};
120
John McCallb04ecb72015-10-21 18:06:43 +0000121struct ObjCEntrypoints {
122 ObjCEntrypoints() { memset(this, 0, sizeof(*this)); }
123
Pete Coopere3886802018-12-08 05:13:50 +0000124 /// void objc_alloc(id);
James Y Knight9871db02019-02-05 16:42:33 +0000125 llvm::FunctionCallee objc_alloc;
Pete Coopere3886802018-12-08 05:13:50 +0000126
127 /// void objc_allocWithZone(id);
James Y Knight9871db02019-02-05 16:42:33 +0000128 llvm::FunctionCallee objc_allocWithZone;
Pete Coopere3886802018-12-08 05:13:50 +0000129
Erik Pilkingtonec389b02019-02-14 19:58:37 +0000130 /// void objc_alloc_init(id);
131 llvm::FunctionCallee objc_alloc_init;
132
Pete Coopere3886802018-12-08 05:13:50 +0000133 /// void objc_autoreleasePoolPop(void*);
James Y Knight9871db02019-02-05 16:42:33 +0000134 llvm::FunctionCallee objc_autoreleasePoolPop;
John McCall31168b02011-06-15 23:02:42 +0000135
Pete Cooper2cd35962018-12-18 20:33:00 +0000136 /// void objc_autoreleasePoolPop(void*);
137 /// Note this method is used when we are using exception handling
James Y Knight9871db02019-02-05 16:42:33 +0000138 llvm::FunctionCallee objc_autoreleasePoolPopInvoke;
Pete Cooper2cd35962018-12-18 20:33:00 +0000139
John McCall31168b02011-06-15 23:02:42 +0000140 /// void *objc_autoreleasePoolPush(void);
James Y Knight9871db02019-02-05 16:42:33 +0000141 llvm::Function *objc_autoreleasePoolPush;
John McCall31168b02011-06-15 23:02:42 +0000142
143 /// id objc_autorelease(id);
James Y Knight9871db02019-02-05 16:42:33 +0000144 llvm::Function *objc_autorelease;
John McCall31168b02011-06-15 23:02:42 +0000145
Pete Coopere5b64ea2018-12-21 21:00:32 +0000146 /// id objc_autorelease(id);
147 /// Note this is the runtime method not the intrinsic.
James Y Knight9871db02019-02-05 16:42:33 +0000148 llvm::FunctionCallee objc_autoreleaseRuntimeFunction;
Pete Coopere5b64ea2018-12-21 21:00:32 +0000149
John McCall31168b02011-06-15 23:02:42 +0000150 /// id objc_autoreleaseReturnValue(id);
James Y Knight9871db02019-02-05 16:42:33 +0000151 llvm::Function *objc_autoreleaseReturnValue;
John McCall31168b02011-06-15 23:02:42 +0000152
153 /// void objc_copyWeak(id *dest, id *src);
James Y Knight9871db02019-02-05 16:42:33 +0000154 llvm::Function *objc_copyWeak;
John McCall31168b02011-06-15 23:02:42 +0000155
156 /// void objc_destroyWeak(id*);
James Y Knight9871db02019-02-05 16:42:33 +0000157 llvm::Function *objc_destroyWeak;
John McCall31168b02011-06-15 23:02:42 +0000158
159 /// id objc_initWeak(id*, id);
James Y Knight9871db02019-02-05 16:42:33 +0000160 llvm::Function *objc_initWeak;
John McCall31168b02011-06-15 23:02:42 +0000161
162 /// id objc_loadWeak(id*);
James Y Knight9871db02019-02-05 16:42:33 +0000163 llvm::Function *objc_loadWeak;
John McCall31168b02011-06-15 23:02:42 +0000164
165 /// id objc_loadWeakRetained(id*);
James Y Knight9871db02019-02-05 16:42:33 +0000166 llvm::Function *objc_loadWeakRetained;
John McCall31168b02011-06-15 23:02:42 +0000167
168 /// void objc_moveWeak(id *dest, id *src);
James Y Knight9871db02019-02-05 16:42:33 +0000169 llvm::Function *objc_moveWeak;
John McCall31168b02011-06-15 23:02:42 +0000170
171 /// id objc_retain(id);
James Y Knight9871db02019-02-05 16:42:33 +0000172 llvm::Function *objc_retain;
John McCall31168b02011-06-15 23:02:42 +0000173
Pete Coopere5b64ea2018-12-21 21:00:32 +0000174 /// id objc_retain(id);
175 /// Note this is the runtime method not the intrinsic.
James Y Knight9871db02019-02-05 16:42:33 +0000176 llvm::FunctionCallee objc_retainRuntimeFunction;
Pete Coopere5b64ea2018-12-21 21:00:32 +0000177
John McCall31168b02011-06-15 23:02:42 +0000178 /// id objc_retainAutorelease(id);
James Y Knight9871db02019-02-05 16:42:33 +0000179 llvm::Function *objc_retainAutorelease;
John McCall31168b02011-06-15 23:02:42 +0000180
181 /// id objc_retainAutoreleaseReturnValue(id);
James Y Knight9871db02019-02-05 16:42:33 +0000182 llvm::Function *objc_retainAutoreleaseReturnValue;
John McCall31168b02011-06-15 23:02:42 +0000183
184 /// id objc_retainAutoreleasedReturnValue(id);
James Y Knight9871db02019-02-05 16:42:33 +0000185 llvm::Function *objc_retainAutoreleasedReturnValue;
John McCall31168b02011-06-15 23:02:42 +0000186
187 /// id objc_retainBlock(id);
James Y Knight9871db02019-02-05 16:42:33 +0000188 llvm::Function *objc_retainBlock;
John McCall31168b02011-06-15 23:02:42 +0000189
190 /// void objc_release(id);
James Y Knight9871db02019-02-05 16:42:33 +0000191 llvm::Function *objc_release;
John McCall31168b02011-06-15 23:02:42 +0000192
Pete Coopere5b64ea2018-12-21 21:00:32 +0000193 /// void objc_release(id);
194 /// Note this is the runtime method not the intrinsic.
James Y Knight9871db02019-02-05 16:42:33 +0000195 llvm::FunctionCallee objc_releaseRuntimeFunction;
Pete Coopere5b64ea2018-12-21 21:00:32 +0000196
Saleem Abdulrasool5b1f0ed2017-02-11 17:24:09 +0000197 /// void objc_storeStrong(id*, id);
James Y Knight9871db02019-02-05 16:42:33 +0000198 llvm::Function *objc_storeStrong;
John McCall31168b02011-06-15 23:02:42 +0000199
200 /// id objc_storeWeak(id*, id);
James Y Knight9871db02019-02-05 16:42:33 +0000201 llvm::Function *objc_storeWeak;
John McCall31168b02011-06-15 23:02:42 +0000202
John McCalle399e5b2016-01-27 18:32:30 +0000203 /// id objc_unsafeClaimAutoreleasedReturnValue(id);
James Y Knight9871db02019-02-05 16:42:33 +0000204 llvm::Function *objc_unsafeClaimAutoreleasedReturnValue;
John McCalle399e5b2016-01-27 18:32:30 +0000205
John McCall31168b02011-06-15 23:02:42 +0000206 /// A void(void) inline asm to use to mark that the return value of
207 /// a call will be immediately retain.
208 llvm::InlineAsm *retainAutoreleasedReturnValueMarker;
John McCalleff18842013-03-23 02:35:54 +0000209
210 /// void clang.arc.use(...);
James Y Knight9871db02019-02-05 16:42:33 +0000211 llvm::Function *clang_arc_use;
John McCall31168b02011-06-15 23:02:42 +0000212};
Will Dietzf54319c2013-01-18 11:30:38 +0000213
Justin Bognere2ef2a02014-04-15 21:22:35 +0000214/// This class records statistics on instrumentation based profiling.
Justin Bogner40b8ba12014-06-26 01:45:07 +0000215class InstrProfStats {
216 uint32_t VisitedInMainFile;
217 uint32_t MissingInMainFile;
Justin Bognere2ef2a02014-04-15 21:22:35 +0000218 uint32_t Visited;
219 uint32_t Missing;
220 uint32_t Mismatched;
Justin Bogner40b8ba12014-06-26 01:45:07 +0000221
222public:
223 InstrProfStats()
224 : VisitedInMainFile(0), MissingInMainFile(0), Visited(0), Missing(0),
225 Mismatched(0) {}
226 /// Record that we've visited a function and whether or not that function was
227 /// in the main source file.
228 void addVisited(bool MainFile) {
229 if (MainFile)
230 ++VisitedInMainFile;
231 ++Visited;
232 }
233 /// Record that a function we've visited has no profile data.
234 void addMissing(bool MainFile) {
235 if (MainFile)
236 ++MissingInMainFile;
237 ++Missing;
238 }
239 /// Record that a function we've visited has mismatched profile data.
240 void addMismatched(bool MainFile) { ++Mismatched; }
241 /// Whether or not the stats we've gathered indicate any potential problems.
242 bool hasDiagnostics() { return Missing || Mismatched; }
243 /// Report potential problems we've found to \c Diags.
244 void reportDiagnostics(DiagnosticsEngine &Diags, StringRef MainFile);
Justin Bognere2ef2a02014-04-15 21:22:35 +0000245};
246
John McCallbd96e982015-09-08 08:21:11 +0000247/// A pair of helper functions for a __block variable.
248class BlockByrefHelpers : public llvm::FoldingSetNode {
249 // MSVC requires this type to be complete in order to process this
250 // header.
251public:
252 llvm::Constant *CopyHelper;
253 llvm::Constant *DisposeHelper;
254
255 /// The alignment of the field. This is important because
256 /// different offsets to the field within the byref struct need to
257 /// have different helper functions.
258 CharUnits Alignment;
259
Simon Pilgrim45330fe2019-05-21 11:37:54 +0000260 BlockByrefHelpers(CharUnits alignment)
261 : CopyHelper(nullptr), DisposeHelper(nullptr), Alignment(alignment) {}
John McCallbd96e982015-09-08 08:21:11 +0000262 BlockByrefHelpers(const BlockByrefHelpers &) = default;
263 virtual ~BlockByrefHelpers();
264
265 void Profile(llvm::FoldingSetNodeID &id) const {
266 id.AddInteger(Alignment.getQuantity());
267 profileImpl(id);
268 }
269 virtual void profileImpl(llvm::FoldingSetNodeID &id) const = 0;
270
271 virtual bool needsCopy() const { return true; }
272 virtual void emitCopy(CodeGenFunction &CGF, Address dest, Address src) = 0;
273
274 virtual bool needsDispose() const { return true; }
275 virtual void emitDispose(CodeGenFunction &CGF, Address field) = 0;
276};
277
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000278/// This class organizes the cross-function state that is used while generating
279/// LLVM code.
John McCalle3dc1702011-02-15 09:22:45 +0000280class CodeGenModule : public CodeGenTypeCache {
Aaron Ballmanabc18922015-02-15 22:54:08 +0000281 CodeGenModule(const CodeGenModule &) = delete;
282 void operator=(const CodeGenModule &) = delete;
Anders Carlsson729a8202009-02-24 04:21:31 +0000283
Keno Fischer76f4ab02014-12-30 08:12:39 +0000284public:
Reid Kleckner563f0e82014-05-23 21:13:45 +0000285 struct Structor {
286 Structor() : Priority(0), Initializer(nullptr), AssociatedData(nullptr) {}
287 Structor(int Priority, llvm::Constant *Initializer,
288 llvm::Constant *AssociatedData)
289 : Priority(Priority), Initializer(Initializer),
290 AssociatedData(AssociatedData) {}
291 int Priority;
292 llvm::Constant *Initializer;
293 llvm::Constant *AssociatedData;
294 };
295
296 typedef std::vector<Structor> CtorList;
Daniel Dunbar74aa7e12008-08-01 00:01:51 +0000297
Keno Fischer76f4ab02014-12-30 08:12:39 +0000298private:
Chris Lattnerf97fe382007-05-24 06:29:05 +0000299 ASTContext &Context;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000300 const LangOptions &LangOpts;
Adrian Prantle74f5252015-06-30 02:26:03 +0000301 const HeaderSearchOptions &HeaderSearchOpts; // Only used for debug info.
302 const PreprocessorOptions &PreprocessorOpts; // Only used for debug info.
Chandler Carruthbc55fe22009-11-12 17:24:48 +0000303 const CodeGenOptions &CodeGenOpts;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000304 llvm::Module &TheModule;
David Blaikie9c902b52011-09-25 23:23:43 +0000305 DiagnosticsEngine &Diags;
John McCallc8e01702013-04-16 22:48:15 +0000306 const TargetInfo &Target;
Ahmed Charlesb8984322014-03-07 20:03:18 +0000307 std::unique_ptr<CGCXXABI> ABI;
John McCallc8e01702013-04-16 22:48:15 +0000308 llvm::LLVMContext &VMContext;
Anders Carlssonff971e82009-10-07 01:06:45 +0000309
Reid Kleckner9305fd12016-04-13 23:37:17 +0000310 std::unique_ptr<CodeGenTBAA> TBAA;
Fangrui Song6907ce22018-07-30 19:24:48 +0000311
Reid Kleckner9305fd12016-04-13 23:37:17 +0000312 mutable std::unique_ptr<TargetCodeGenInfo> TheTargetCodeGenInfo;
Fangrui Song6907ce22018-07-30 19:24:48 +0000313
John McCallc8e01702013-04-16 22:48:15 +0000314 // This should not be moved earlier, since its initialization depends on some
315 // of the previous reference members being already initialized and also checks
316 // if TheTargetCodeGenInfo is NULL
317 CodeGenTypes Types;
Fangrui Song6907ce22018-07-30 19:24:48 +0000318
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000319 /// Holds information about C++ vtables.
Anders Carlssona864caf2010-03-23 04:11:45 +0000320 CodeGenVTables VTables;
Douglas Gregorbc6a4342010-04-08 16:07:47 +0000321
Reid Kleckner9305fd12016-04-13 23:37:17 +0000322 std::unique_ptr<CGObjCRuntime> ObjCRuntime;
323 std::unique_ptr<CGOpenCLRuntime> OpenCLRuntime;
324 std::unique_ptr<CGOpenMPRuntime> OpenMPRuntime;
Johannes Doerfertb3c06db2019-11-04 23:00:36 -0600325 std::unique_ptr<llvm::OpenMPIRBuilder> OMPBuilder;
Reid Kleckner9305fd12016-04-13 23:37:17 +0000326 std::unique_ptr<CGCUDARuntime> CUDARuntime;
327 std::unique_ptr<CGDebugInfo> DebugInfo;
328 std::unique_ptr<ObjCEntrypoints> ObjCData;
329 llvm::MDNode *NoObjCARCExceptionsMetadata = nullptr;
Justin Bogner837a6f62014-04-18 21:52:00 +0000330 std::unique_ptr<llvm::IndexedInstrProfReader> PGOReader;
Justin Bognere2ef2a02014-04-15 21:22:35 +0000331 InstrProfStats PGOStats;
Peter Collingbournedc134532016-01-16 00:31:22 +0000332 std::unique_ptr<llvm::SanitizerStatReport> SanStats;
Daniel Dunbare49df9b52008-07-30 16:32:24 +0000333
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000334 // A set of references that have only been seen via a weakref so far. This is
335 // used to remove the weak of the reference if we ever see a direct reference
336 // or a definition.
Rafael Espindola2e42fec2010-03-04 18:17:24 +0000337 llvm::SmallPtrSet<llvm::GlobalValue*, 10> WeakRefReferences;
338
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000339 /// This contains all the decls which have definitions but/ which are deferred
340 /// for emission and therefore should only be output if they are actually
341 /// used. If a decl is in this, then it is known to have not been referenced
342 /// yet.
Alp Tokerfb8d02b2014-06-05 22:10:59 +0000343 std::map<StringRef, GlobalDecl> DeferredDecls;
Daniel Dunbar08b26a02009-02-13 20:29:50 +0000344
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000345 /// This is a list of deferred decls which we have seen that *are* actually
346 /// referenced. These get code generated when the module is done.
Sanjoy Das70a60512017-05-01 06:12:13 +0000347 std::vector<GlobalDecl> DeferredDeclsToEmit;
348 void addDeferredDeclToEmit(GlobalDecl GD) {
349 DeferredDeclsToEmit.emplace_back(GD);
Rafael Espindolac0ff7442013-12-09 14:59:08 +0000350 }
Daniel Dunbar7dd749e2009-04-15 22:08:45 +0000351
Rafael Espindola208b5c02013-10-22 19:26:13 +0000352 /// List of alias we have emitted. Used to make sure that what they point to
353 /// is defined once we get to the end of the of the translation unit.
354 std::vector<GlobalDecl> Aliases;
355
Erich Keane281d20b2018-01-08 21:34:17 +0000356 /// List of multiversion functions that have to be emitted. Used to make sure
357 /// we properly emit the iFunc.
358 std::vector<GlobalDecl> MultiVersionFuncs;
359
Rafael Espindola2e2995b2013-11-05 21:37:29 +0000360 typedef llvm::StringMap<llvm::TrackingVH<llvm::Constant> > ReplacementsTy;
361 ReplacementsTy Replacements;
362
Andrey Bokhankocab58582015-08-31 13:20:44 +0000363 /// List of global values to be replaced with something else. Used when we
364 /// want to replace a GlobalValue but can't identify it by its mangled name
365 /// anymore (because the name is already taken).
366 llvm::SmallVector<std::pair<llvm::GlobalValue *, llvm::Constant *>, 8>
367 GlobalValReplacements;
368
Richard Smith24cdcad2019-06-14 17:46:37 +0000369 /// Variables for which we've emitted globals containing their constant
370 /// values along with the corresponding globals, for opportunistic reuse.
371 llvm::DenseMap<const VarDecl*, llvm::GlobalVariable*> InitializerConstants;
372
Andrey Bokhankocab58582015-08-31 13:20:44 +0000373 /// Set of global decls for which we already diagnosed mangled name conflict.
374 /// Required to not issue a warning (on a mangling conflict) multiple times
375 /// for the same decl.
376 llvm::DenseSet<GlobalDecl> DiagnosedConflictingDefinitions;
377
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000378 /// A queue of (optional) vtables to consider emitting.
John McCall6bd2a892013-01-25 22:31:03 +0000379 std::vector<const CXXRecordDecl*> DeferredVTables;
380
Piotr Padlewskid3b1cbd2017-06-01 08:04:05 +0000381 /// A queue of (optional) vtables that may be emitted opportunistically.
382 std::vector<const CXXRecordDecl *> OpportunisticVTables;
383
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000384 /// List of global values which are required to be present in the object file;
385 /// bitcast to i8*. This is used for forcing visibility of symbols which may
386 /// otherwise be optimized out.
Sanjoy Dase369bd92017-05-01 17:08:00 +0000387 std::vector<llvm::WeakTrackingVH> LLVMUsed;
388 std::vector<llvm::WeakTrackingVH> LLVMCompilerUsed;
Mike Stump1676c5b2009-02-13 19:12:34 +0000389
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000390 /// Store the list of global constructors and their respective priorities to
391 /// be emitted when the translation unit is complete.
Daniel Dunbar74aa7e12008-08-01 00:01:51 +0000392 CtorList GlobalCtors;
393
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000394 /// Store the list of global destructors and their respective priorities to be
395 /// emitted when the translation unit is complete.
Daniel Dunbar74aa7e12008-08-01 00:01:51 +0000396 CtorList GlobalDtors;
397
Robert Lytton57765d52014-07-03 09:30:33 +0000398 /// An ordered map of canonical GlobalDecls to their mangled names.
399 llvm::MapVector<GlobalDecl, StringRef> MangledDeclNames;
Alp Tokerfb8d02b2014-06-05 22:10:59 +0000400 llvm::StringMap<GlobalDecl, llvm::BumpPtrAllocator> Manglings;
401
Erich Keane3efe0022018-07-20 14:13:28 +0000402 // An ordered map of canonical GlobalDecls paired with the cpu-index for
403 // cpu-specific name manglings.
404 llvm::MapVector<std::pair<GlobalDecl, unsigned>, StringRef>
405 CPUSpecificMangledDeclNames;
406 llvm::StringMap<std::pair<GlobalDecl, unsigned>, llvm::BumpPtrAllocator>
407 CPUSpecificManglings;
408
Julien Lerouge5a6b6982011-09-09 22:41:49 +0000409 /// Global annotations.
Nate Begeman7fab5782008-04-18 23:43:57 +0000410 std::vector<llvm::Constant*> Annotations;
Mike Stump1676c5b2009-02-13 19:12:34 +0000411
Julien Lerouge5a6b6982011-09-09 22:41:49 +0000412 /// Map used to get unique annotation strings.
413 llvm::StringMap<llvm::Constant*> AnnotationStrings;
414
David Blaikie2e804282015-04-05 22:47:07 +0000415 llvm::StringMap<llvm::GlobalVariable *> CFConstantStringMap;
David Majnemer58e5bee2014-03-24 21:43:36 +0000416
Richard Smith00db2f12014-07-29 21:20:12 +0000417 llvm::DenseMap<llvm::Constant *, llvm::GlobalVariable *> ConstantStringMap;
Eli Friedman1c8d2ca2012-03-09 03:27:46 +0000418 llvm::DenseMap<const Decl*, llvm::Constant *> StaticLocalDeclMap;
John McCallb88a5662012-03-30 21:00:39 +0000419 llvm::DenseMap<const Decl*, llvm::GlobalVariable*> StaticLocalDeclGuardMap;
Richard Smithe6c01442013-06-05 00:46:14 +0000420 llvm::DenseMap<const Expr*, llvm::Constant *> MaterializedGlobalTemporaryMap;
421
Fariborz Jahanian1bed4132012-01-08 19:13:23 +0000422 llvm::DenseMap<QualType, llvm::Constant *> AtomicSetterHelperFnMap;
423 llvm::DenseMap<QualType, llvm::Constant *> AtomicGetterHelperFnMap;
Daniel Dunbard644ad62008-08-23 18:37:06 +0000424
Will Dietz949ec542013-11-08 01:09:22 +0000425 /// Map used to get unique type descriptor constants for sanitizers.
426 llvm::DenseMap<QualType, llvm::Constant *> TypeDescriptorMap;
427
Richard Smithdf931ce2013-04-06 05:00:46 +0000428 /// Map used to track internal linkage functions declared within
429 /// extern "C" regions.
Richard Smithf21c9612013-04-13 01:28:18 +0000430 typedef llvm::MapVector<IdentifierInfo *,
431 llvm::GlobalValue *> StaticExternCMap;
Richard Smithdf931ce2013-04-06 05:00:46 +0000432 StaticExternCMap StaticExternCValues;
433
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000434 /// thread_local variables defined or used in this TU.
Richard Smith5a99c492015-12-01 01:10:48 +0000435 std::vector<const VarDecl *> CXXThreadLocals;
Richard Smith2fd1d7a2013-04-19 16:42:07 +0000436
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000437 /// thread_local variables with initializers that need to run
Richard Smith2fd1d7a2013-04-19 16:42:07 +0000438 /// before any thread_local variable in this TU is odr-used.
David Majnemerb3341ea2014-10-05 05:05:40 +0000439 std::vector<llvm::Function *> CXXThreadLocalInits;
Richard Smith5a99c492015-12-01 01:10:48 +0000440 std::vector<const VarDecl *> CXXThreadLocalInitVars;
Richard Smith2fd1d7a2013-04-19 16:42:07 +0000441
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000442 /// Global variables with initializers that need to run before main.
David Majnemerb3341ea2014-10-05 05:05:40 +0000443 std::vector<llvm::Function *> CXXGlobalInits;
John McCall70013b62010-07-15 23:40:35 +0000444
445 /// When a C++ decl with an initializer is deferred, null is
446 /// appended to CXXGlobalInits, and the index of that null is placed
447 /// here so that the initializer will be performed in the correct
Reid Klecknere07140e2015-04-15 01:08:06 +0000448 /// order. Once the decl is emitted, the index is replaced with ~0U to ensure
449 /// that we don't re-emit the initializer.
John McCall70013b62010-07-15 23:40:35 +0000450 llvm::DenseMap<const Decl*, unsigned> DelayedCXXInitPosition;
Fangrui Song6907ce22018-07-30 19:24:48 +0000451
Anton Korobeynikovabed7492012-11-06 22:44:45 +0000452 typedef std::pair<OrderGlobalInits, llvm::Function*> GlobalInitData;
453
454 struct GlobalInitPriorityCmp {
455 bool operator()(const GlobalInitData &LHS,
456 const GlobalInitData &RHS) const {
457 return LHS.first.priority < RHS.first.priority;
458 }
459 };
460
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000461 /// Global variables with initializers whose order of initialization is set by
462 /// init_priority attribute.
Anton Korobeynikovabed7492012-11-06 22:44:45 +0000463 SmallVector<GlobalInitData, 8> PrioritizedCXXGlobalInits;
Mike Stump11289f42009-09-09 15:08:12 +0000464
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000465 /// Global destructor functions and arguments that need to run on termination.
James Y Knightf7321542019-02-07 01:14:17 +0000466 std::vector<
467 std::tuple<llvm::FunctionType *, llvm::WeakTrackingVH, llvm::Constant *>>
468 CXXGlobalDtors;
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000469
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000470 /// The complete set of modules that has been imported.
Douglas Gregor6ddfca92013-01-14 17:21:00 +0000471 llvm::SetVector<clang::Module *> ImportedModules;
472
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000473 /// The set of modules for which the module initializers
Manman Ren3b5dbf22016-10-14 18:55:44 +0000474 /// have been emitted.
475 llvm::SmallPtrSet<clang::Module *, 16> EmittedModuleInitializers;
476
Ben Dunbobbin1d165152019-05-17 03:44:15 +0000477 /// A vector of metadata strings for linker options.
Peter Collingbourne89061b22017-06-12 20:10:48 +0000478 SmallVector<llvm::MDNode *, 16> LinkerOptionsMetadata;
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000479
Ben Dunbobbin1d165152019-05-17 03:44:15 +0000480 /// A vector of metadata strings for dependent libraries for ELF.
481 SmallVector<llvm::MDNode *, 16> ELFDependentLibraries;
482
Douglas Gregorabf4e0d2011-08-09 15:54:21 +0000483 /// @name Cache for Objective-C runtime types
484 /// @{
485
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000486 /// Cached reference to the class for constant strings. This value has type
487 /// int * but is actually an Obj-C class pointer.
Sanjoy Dase369bd92017-05-01 17:08:00 +0000488 llvm::WeakTrackingVH CFConstantStringClassRef;
Mike Stump11289f42009-09-09 15:08:12 +0000489
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000490 /// The type used to describe the state of a fast enumeration in
Douglas Gregor636e2002011-08-09 17:23:49 +0000491 /// Objective-C's for..in loop.
492 QualType ObjCFastEnumerationStateType;
Fangrui Song6907ce22018-07-30 19:24:48 +0000493
Douglas Gregorabf4e0d2011-08-09 15:54:21 +0000494 /// @}
495
David Chisnall481e3a82010-01-23 02:40:42 +0000496 /// Lazily create the Objective-C runtime
497 void createObjCRuntime();
498
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000499 void createOpenCLRuntime();
Alexey Bataev9959db52014-05-06 10:08:46 +0000500 void createOpenMPRuntime();
Peter Collingbournefe883422011-10-06 18:29:37 +0000501 void createCUDARuntime();
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000502
Rafael Espindola4fdc1752011-12-19 14:41:01 +0000503 bool isTriviallyRecursive(const FunctionDecl *F);
Peter Collingbourne4d90dba2013-06-05 17:49:37 +0000504 bool shouldEmitFunction(GlobalDecl GD);
Piotr Padlewskid3b1cbd2017-06-01 08:04:05 +0000505 bool shouldOpportunisticallyEmitVTables();
George Burgess IV1a39b862016-12-28 07:27:40 +0000506 /// Map used to be sure we don't emit the same CompoundLiteral twice.
507 llvm::DenseMap<const CompoundLiteralExpr *, llvm::GlobalVariable *>
508 EmittedCompoundLiterals;
509
George Burgess IVe3763372016-12-22 02:50:20 +0000510 /// Map of the global blocks we've emitted, so that we don't have to re-emit
511 /// them if the constexpr evaluator gets aggressive.
512 llvm::DenseMap<const BlockExpr *, llvm::Constant *> EmittedGlobalBlocks;
513
Daniel Dunbar900546d2010-07-16 00:00:15 +0000514 /// @name Cache for Blocks Runtime Globals
515 /// @{
516
Reid Kleckner9305fd12016-04-13 23:37:17 +0000517 llvm::Constant *NSConcreteGlobalBlock = nullptr;
518 llvm::Constant *NSConcreteStackBlock = nullptr;
Daniel Dunbar3348e2d2010-07-16 00:00:19 +0000519
James Y Knight9871db02019-02-05 16:42:33 +0000520 llvm::FunctionCallee BlockObjectAssign = nullptr;
521 llvm::FunctionCallee BlockObjectDispose = nullptr;
Daniel Dunbar900546d2010-07-16 00:00:15 +0000522
Reid Kleckner9305fd12016-04-13 23:37:17 +0000523 llvm::Type *BlockDescriptorType = nullptr;
524 llvm::Type *GenericBlockLiteralType = nullptr;
John McCallad7c5c12011-02-08 08:22:06 +0000525
526 struct {
527 int GlobalUniqueCount;
528 } Block;
Will Dietzf54319c2013-01-18 11:30:38 +0000529
Vedant Kumarb95bb082019-11-07 12:46:26 -0800530 GlobalDecl initializedGlobalDecl;
531
532 /// @}
533
Nadav Rotem1da30942013-03-23 06:43:35 +0000534 /// void @llvm.lifetime.start(i64 %size, i8* nocapture <ptr>)
James Y Knight76f78742019-02-05 19:17:50 +0000535 llvm::Function *LifetimeStartFn = nullptr;
Nadav Rotem1da30942013-03-23 06:43:35 +0000536
537 /// void @llvm.lifetime.end(i64 %size, i8* nocapture <ptr>)
James Y Knight76f78742019-02-05 19:17:50 +0000538 llvm::Function *LifetimeEndFn = nullptr;
Nadav Rotem1da30942013-03-23 06:43:35 +0000539
Alexey Samsonov4b8de112014-08-01 21:35:28 +0000540 std::unique_ptr<SanitizerMetadata> SanitizerMD;
541
Eli Friedmandbcfade2017-12-13 00:14:17 +0000542 llvm::MapVector<const Decl *, bool> DeferredEmptyCoverageMappingDecls;
Alex Lorenzee024992014-08-04 18:41:51 +0000543
544 std::unique_ptr<CoverageMappingModuleGen> CoverageMapping;
Peter Collingbourne25a80bf2015-09-08 23:01:30 +0000545
546 /// Mapping from canonical types to their metadata identifiers. We need to
547 /// maintain this mapping because identifiers may be formed from distinct
548 /// MDNodes.
Vlad Tsyrklevich634c6012017-10-31 22:39:44 +0000549 typedef llvm::DenseMap<QualType, llvm::Metadata *> MetadataTypeMap;
550 MetadataTypeMap MetadataIdMap;
Peter Collingbournee44acad2018-06-26 02:15:47 +0000551 MetadataTypeMap VirtualMetadataIdMap;
Vlad Tsyrklevich634c6012017-10-31 22:39:44 +0000552 MetadataTypeMap GeneralizedMetadataIdMap;
Peter Collingbourne25a80bf2015-09-08 23:01:30 +0000553
Chris Lattnerf97fe382007-05-24 06:29:05 +0000554public:
Mehdi Aminica3cf9e2015-07-24 16:04:29 +0000555 CodeGenModule(ASTContext &C, const HeaderSearchOptions &headersearchopts,
Adrian Prantle74f5252015-06-30 02:26:03 +0000556 const PreprocessorOptions &ppopts,
Mehdi Aminica3cf9e2015-07-24 16:04:29 +0000557 const CodeGenOptions &CodeGenOpts, llvm::Module &M,
Alex Lorenzee024992014-08-04 18:41:51 +0000558 DiagnosticsEngine &Diags,
559 CoverageSourceInfo *CoverageInfo = nullptr);
Ted Kremenek2c674f62008-08-05 18:50:11 +0000560
Chris Lattnera087ff92008-03-01 08:45:05 +0000561 ~CodeGenModule();
Mike Stump1676c5b2009-02-13 19:12:34 +0000562
Rafael Espindolac0ff7442013-12-09 14:59:08 +0000563 void clear();
564
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000565 /// Finalize LLVM code generation.
Ted Kremenek2c674f62008-08-05 18:50:11 +0000566 void Release();
Daniel Dunbar8d480592008-08-11 18:12:00 +0000567
Bob Haarmanc6c9b8f2017-09-11 22:11:57 +0000568 /// Return true if we should emit location information for expressions.
569 bool getExpressionLocationsEnabled() const;
570
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000571 /// Return a reference to the configured Objective-C runtime.
Mike Stump1676c5b2009-02-13 19:12:34 +0000572 CGObjCRuntime &getObjCRuntime() {
Peter Collingbournee1d20992011-07-27 20:29:46 +0000573 if (!ObjCRuntime) createObjCRuntime();
574 return *ObjCRuntime;
Daniel Dunbar8d480592008-08-11 18:12:00 +0000575 }
Mike Stump1676c5b2009-02-13 19:12:34 +0000576
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000577 /// Return true iff an Objective-C runtime has been configured.
Peter Collingbournee1d20992011-07-27 20:29:46 +0000578 bool hasObjCRuntime() { return !!ObjCRuntime; }
Daniel Dunbar8d480592008-08-11 18:12:00 +0000579
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000580 /// Return a reference to the configured OpenCL runtime.
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000581 CGOpenCLRuntime &getOpenCLRuntime() {
Craig Topper8a13c412014-05-21 05:09:00 +0000582 assert(OpenCLRuntime != nullptr);
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000583 return *OpenCLRuntime;
584 }
585
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000586 /// Return a reference to the configured OpenMP runtime.
Alexey Bataev9959db52014-05-06 10:08:46 +0000587 CGOpenMPRuntime &getOpenMPRuntime() {
588 assert(OpenMPRuntime != nullptr);
589 return *OpenMPRuntime;
590 }
591
Johannes Doerfertb3c06db2019-11-04 23:00:36 -0600592 /// Return a pointer to the configured OpenMPIRBuilder, if any.
593 llvm::OpenMPIRBuilder *getOpenMPIRBuilder() { return OMPBuilder.get(); }
594
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000595 /// Return a reference to the configured CUDA runtime.
Peter Collingbournefe883422011-10-06 18:29:37 +0000596 CGCUDARuntime &getCUDARuntime() {
Craig Topper8a13c412014-05-21 05:09:00 +0000597 assert(CUDARuntime != nullptr);
Peter Collingbournefe883422011-10-06 18:29:37 +0000598 return *CUDARuntime;
599 }
600
John McCallb04ecb72015-10-21 18:06:43 +0000601 ObjCEntrypoints &getObjCEntrypoints() const {
602 assert(ObjCData != nullptr);
603 return *ObjCData;
John McCall31168b02011-06-15 23:02:42 +0000604 }
605
Erik Pilkington9c42a8d2017-02-23 21:08:08 +0000606 // Version checking function, used to implement ObjC's @available:
607 // i32 @__isOSVersionAtLeast(i32, i32, i32)
James Y Knight9871db02019-02-05 16:42:33 +0000608 llvm::FunctionCallee IsOSVersionAtLeastFn = nullptr;
Erik Pilkington9c42a8d2017-02-23 21:08:08 +0000609
Justin Bogner837a6f62014-04-18 21:52:00 +0000610 InstrProfStats &getPGOStats() { return PGOStats; }
611 llvm::IndexedInstrProfReader *getPGOReader() const { return PGOReader.get(); }
Justin Bogneref512b92014-01-06 22:27:43 +0000612
Alex Lorenzee024992014-08-04 18:41:51 +0000613 CoverageMappingModuleGen *getCoverageMapping() const {
614 return CoverageMapping.get();
615 }
616
Eli Friedman1c8d2ca2012-03-09 03:27:46 +0000617 llvm::Constant *getStaticLocalDeclAddress(const VarDecl *D) {
618 return StaticLocalDeclMap[D];
Fariborz Jahanian3fef72f2010-04-18 21:01:23 +0000619 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000620 void setStaticLocalDeclAddress(const VarDecl *D,
Eli Friedman1c8d2ca2012-03-09 03:27:46 +0000621 llvm::Constant *C) {
622 StaticLocalDeclMap[D] = C;
Fariborz Jahanian3fef72f2010-04-18 21:01:23 +0000623 }
624
Reid Kleckner453e0562014-10-08 01:07:54 +0000625 llvm::Constant *
626 getOrCreateStaticVarDecl(const VarDecl &D,
627 llvm::GlobalValue::LinkageTypes Linkage);
628
John McCallb88a5662012-03-30 21:00:39 +0000629 llvm::GlobalVariable *getStaticLocalDeclGuardAddress(const VarDecl *D) {
630 return StaticLocalDeclGuardMap[D];
631 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000632 void setStaticLocalDeclGuardAddress(const VarDecl *D,
John McCallb88a5662012-03-30 21:00:39 +0000633 llvm::GlobalVariable *C) {
634 StaticLocalDeclGuardMap[D] = C;
635 }
636
Richard Smith24cdcad2019-06-14 17:46:37 +0000637 Address createUnnamedGlobalFrom(const VarDecl &D, llvm::Constant *Constant,
638 CharUnits Align);
639
Alp Tokerfb8d02b2014-06-05 22:10:59 +0000640 bool lookupRepresentativeDecl(StringRef MangledName,
641 GlobalDecl &Result) const;
642
Fariborz Jahanian1bed4132012-01-08 19:13:23 +0000643 llvm::Constant *getAtomicSetterHelperFnMap(QualType Ty) {
644 return AtomicSetterHelperFnMap[Ty];
Fariborz Jahanian7ff610b2012-01-06 22:33:54 +0000645 }
Fariborz Jahanian1bed4132012-01-08 19:13:23 +0000646 void setAtomicSetterHelperFnMap(QualType Ty,
Fariborz Jahanian7ff610b2012-01-06 22:33:54 +0000647 llvm::Constant *Fn) {
Fariborz Jahanian1bed4132012-01-08 19:13:23 +0000648 AtomicSetterHelperFnMap[Ty] = Fn;
649 }
650
651 llvm::Constant *getAtomicGetterHelperFnMap(QualType Ty) {
652 return AtomicGetterHelperFnMap[Ty];
653 }
654 void setAtomicGetterHelperFnMap(QualType Ty,
655 llvm::Constant *Fn) {
656 AtomicGetterHelperFnMap[Ty] = Fn;
Fariborz Jahanian7ff610b2012-01-06 22:33:54 +0000657 }
658
Warren Hunt5c2b4ea2014-05-23 16:07:43 +0000659 llvm::Constant *getTypeDescriptorFromMap(QualType Ty) {
Will Dietz949ec542013-11-08 01:09:22 +0000660 return TypeDescriptorMap[Ty];
661 }
Warren Hunt5c2b4ea2014-05-23 16:07:43 +0000662 void setTypeDescriptorInMap(QualType Ty, llvm::Constant *C) {
Will Dietz949ec542013-11-08 01:09:22 +0000663 TypeDescriptorMap[Ty] = C;
664 }
665
Reid Kleckner9305fd12016-04-13 23:37:17 +0000666 CGDebugInfo *getModuleDebugInfo() { return DebugInfo.get(); }
Devang Patele65982c2011-03-07 18:29:53 +0000667
Dan Gohman515a60d2012-02-16 00:57:37 +0000668 llvm::MDNode *getNoObjCARCExceptionsMetadata() {
669 if (!NoObjCARCExceptionsMetadata)
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +0000670 NoObjCARCExceptionsMetadata = llvm::MDNode::get(getLLVMContext(), None);
Dan Gohman515a60d2012-02-16 00:57:37 +0000671 return NoObjCARCExceptionsMetadata;
672 }
673
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000674 ASTContext &getContext() const { return Context; }
David Blaikiebbafb8a2012-03-11 07:00:24 +0000675 const LangOptions &getLangOpts() const { return LangOpts; }
Adrian Prantle74f5252015-06-30 02:26:03 +0000676 const HeaderSearchOptions &getHeaderSearchOpts()
677 const { return HeaderSearchOpts; }
678 const PreprocessorOptions &getPreprocessorOpts()
679 const { return PreprocessorOpts; }
John McCallc8e01702013-04-16 22:48:15 +0000680 const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
Chris Lattner23b7eb62007-06-15 23:05:46 +0000681 llvm::Module &getModule() const { return TheModule; }
David Blaikie9c902b52011-09-25 23:23:43 +0000682 DiagnosticsEngine &getDiags() const { return Diags; }
Mehdi Aminica3cf9e2015-07-24 16:04:29 +0000683 const llvm::DataLayout &getDataLayout() const {
684 return TheModule.getDataLayout();
685 }
John McCallc8e01702013-04-16 22:48:15 +0000686 const TargetInfo &getTarget() const { return Target; }
Saleem Abdulrasool7246dcc2016-09-14 15:17:46 +0000687 const llvm::Triple &getTriple() const { return Target.getTriple(); }
Rafael Espindola9f834732014-09-19 01:54:22 +0000688 bool supportsCOMDAT() const;
Rafael Espindoladbee8a72015-01-15 21:36:08 +0000689 void maybeSetTrivialComdat(const Decl &D, llvm::GlobalObject &GO);
Rafael Espindola9f834732014-09-19 01:54:22 +0000690
Alp Toker82862252013-12-28 21:58:40 +0000691 CGCXXABI &getCXXABI() const { return *ABI; }
Owen Anderson170229f2009-07-14 23:10:40 +0000692 llvm::LLVMContext &getLLVMContext() { return VMContext; }
Craig Topper8a13c412014-05-21 05:09:00 +0000693
694 bool shouldUseTBAA() const { return TBAA != nullptr; }
John McCall53fcbd22011-02-26 08:07:02 +0000695
Fangrui Song6907ce22018-07-30 19:24:48 +0000696 const TargetCodeGenInfo &getTargetCodeGenInfo();
697
John McCallc8e01702013-04-16 22:48:15 +0000698 CodeGenTypes &getTypes() { return Types; }
Fangrui Song6907ce22018-07-30 19:24:48 +0000699
John McCallc8e01702013-04-16 22:48:15 +0000700 CodeGenVTables &getVTables() { return VTables; }
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000701
Timur Iskhodzhanov58776632013-11-05 15:54:58 +0000702 ItaniumVTableContext &getItaniumVTableContext() {
703 return VTables.getItaniumVTableContext();
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +0000704 }
John McCallc8e01702013-04-16 22:48:15 +0000705
Timur Iskhodzhanov58776632013-11-05 15:54:58 +0000706 MicrosoftVTableContext &getMicrosoftVTableContext() {
707 return VTables.getMicrosoftVTableContext();
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000708 }
709
Keno Fischer76f4ab02014-12-30 08:12:39 +0000710 CtorList &getGlobalCtors() { return GlobalCtors; }
711 CtorList &getGlobalDtors() { return GlobalDtors; }
712
Ivan A. Kosarev289574e2017-10-02 09:54:47 +0000713 /// getTBAATypeInfo - Get metadata used to describe accesses to objects of
714 /// the given type.
715 llvm::MDNode *getTBAATypeInfo(QualType QTy);
716
Ivan A. Kosarev3d68ce92017-10-05 11:08:17 +0000717 /// getTBAAAccessInfo - Get TBAA information that describes an access to
718 /// an object of the given type.
719 TBAAAccessInfo getTBAAAccessInfo(QualType AccessType);
720
721 /// getTBAAVTablePtrAccessInfo - Get the TBAA information that describes an
722 /// access to a virtual table pointer.
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000723 TBAAAccessInfo getTBAAVTablePtrAccessInfo(llvm::Type *VTablePtrType);
Ivan A. Kosarev3d68ce92017-10-05 11:08:17 +0000724
Dan Gohman22695fc2012-09-28 21:58:29 +0000725 llvm::MDNode *getTBAAStructInfo(QualType QTy);
Ivan A. Kosareva511ed72017-10-03 10:52:39 +0000726
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000727 /// getTBAABaseTypeInfo - Get metadata that describes the given base access
728 /// type. Return null if the type is not suitable for use in TBAA access tags.
729 llvm::MDNode *getTBAABaseTypeInfo(QualType QTy);
730
Ivan A. Kosarev3d68ce92017-10-05 11:08:17 +0000731 /// getTBAAAccessTagInfo - Get TBAA tag for a given memory access.
732 llvm::MDNode *getTBAAAccessTagInfo(TBAAAccessInfo Info);
Dan Gohman947c9af2010-10-14 23:06:10 +0000733
Ivan A. Kosareved141ba2017-10-17 09:12:13 +0000734 /// mergeTBAAInfoForCast - Get merged TBAA information for the purposes of
735 /// type casts.
736 TBAAAccessInfo mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo,
737 TBAAAccessInfo TargetInfo);
738
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +0000739 /// mergeTBAAInfoForConditionalOperator - Get merged TBAA information for the
740 /// purposes of conditional operator.
741 TBAAAccessInfo mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA,
742 TBAAAccessInfo InfoB);
743
Ivan A. Kosarev1860b522018-01-25 14:21:55 +0000744 /// mergeTBAAInfoForMemoryTransfer - Get merged TBAA information for the
745 /// purposes of memory transfer calls.
746 TBAAAccessInfo mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo,
747 TBAAAccessInfo SrcInfo);
748
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +0000749 /// getTBAAInfoForSubobject - Get TBAA information for an access with a given
750 /// base lvalue.
751 TBAAAccessInfo getTBAAInfoForSubobject(LValue Base, QualType AccessType) {
Hal Finkela5986b92017-12-03 03:10:13 +0000752 if (Base.getTBAAInfo().isMayAlias())
753 return TBAAAccessInfo::getMayAliasInfo();
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +0000754 return getTBAAAccessInfo(AccessType);
755 }
756
Richard Smithe070fd22012-02-17 06:48:11 +0000757 bool isTypeConstant(QualType QTy, bool ExcludeCtorDtor);
758
John McCalla8ec7eb2013-03-07 21:37:17 +0000759 bool isPaddedAtomicType(QualType type);
760 bool isPaddedAtomicType(const AtomicType *type);
761
Ivan A. Kosarev3d68ce92017-10-05 11:08:17 +0000762 /// DecorateInstructionWithTBAA - Decorate the instruction with a TBAA tag.
Piotr Padlewski4b1ac722015-09-15 21:46:55 +0000763 void DecorateInstructionWithTBAA(llvm::Instruction *Inst,
Ivan A. Kosarev3d68ce92017-10-05 11:08:17 +0000764 TBAAAccessInfo TBAAInfo);
Piotr Padlewski4b1ac722015-09-15 21:46:55 +0000765
766 /// Adds !invariant.barrier !tag to instruction
767 void DecorateInstructionWithInvariantGroup(llvm::Instruction *I,
768 const CXXRecordDecl *RD);
Dan Gohman947c9af2010-10-14 23:06:10 +0000769
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000770 /// Emit the given number of characters as a value of type size_t.
John McCall23c29fe2011-06-24 21:55:10 +0000771 llvm::ConstantInt *getSize(CharUnits numChars);
772
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000773 /// Set the visibility for the given LLVM GlobalValue.
Rafael Espindoladeb10be2018-02-07 19:04:41 +0000774 void setGlobalVisibility(llvm::GlobalValue *GV, const NamedDecl *D) const;
Daniel Dunbarf5f359f2009-04-14 06:00:08 +0000775
Rafael Espindola3dd49812018-02-23 00:22:15 +0000776 void setDSOLocal(llvm::GlobalValue *GV) const;
Rafael Espindola699f5d62018-02-07 22:15:33 +0000777
Rafael Espindolab7350042018-03-01 00:35:47 +0000778 void setDLLImportDLLExport(llvm::GlobalValue *GV, GlobalDecl D) const;
779 void setDLLImportDLLExport(llvm::GlobalValue *GV, const NamedDecl *D) const;
780 /// Set visibility, dllimport/dllexport and dso_local.
Rafael Espindola922f2aa2018-02-23 19:30:48 +0000781 /// This must be called after dllimport/dllexport is set.
Rafael Espindolab7350042018-03-01 00:35:47 +0000782 void setGVProperties(llvm::GlobalValue *GV, GlobalDecl GD) const;
Rafael Espindola699f5d62018-02-07 22:15:33 +0000783 void setGVProperties(llvm::GlobalValue *GV, const NamedDecl *D) const;
784
Peter Collingbournee08e68d2019-06-07 19:10:08 +0000785 void setGVPropertiesAux(llvm::GlobalValue *GV, const NamedDecl *D) const;
786
David Majnemerbb525f7c2014-10-15 22:38:23 +0000787 /// Set the TLS mode for the given LLVM GlobalValue for the thread-local
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000788 /// variable declaration D.
David Majnemerbb525f7c2014-10-15 22:38:23 +0000789 void setTLSMode(llvm::GlobalValue *GV, const VarDecl &D) const;
Hans Wennborgf60f6af2012-06-28 08:01:44 +0000790
Anders Carlsson537fdce2011-01-29 20:59:35 +0000791 static llvm::GlobalValue::VisibilityTypes GetLLVMVisibility(Visibility V) {
792 switch (V) {
793 case DefaultVisibility: return llvm::GlobalValue::DefaultVisibility;
794 case HiddenVisibility: return llvm::GlobalValue::HiddenVisibility;
795 case ProtectedVisibility: return llvm::GlobalValue::ProtectedVisibility;
796 }
797 llvm_unreachable("unknown visibility!");
Anders Carlsson537fdce2011-01-29 20:59:35 +0000798 }
799
John McCalld195d4c2016-11-30 23:25:13 +0000800 llvm::Constant *GetAddrOfGlobal(GlobalDecl GD,
801 ForDefinition_t IsForDefinition
802 = NotForDefinition);
John McCalld4324142010-02-19 01:32:20 +0000803
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000804 /// Will return a global variable of the given type. If a variable with a
805 /// different type already exists then a new variable with the right type
806 /// will be created and all uses of the old variable will be replaced with a
807 /// bitcast to the new variable.
Anders Carlssonda80af32011-01-29 18:20:20 +0000808 llvm::GlobalVariable *
Chris Lattner01cf8db2011-07-20 06:58:45 +0000809 CreateOrReplaceCXXRuntimeVariable(StringRef Name, llvm::Type *Ty,
David Greenbe0c5b62018-09-12 14:09:06 +0000810 llvm::GlobalValue::LinkageTypes Linkage,
811 unsigned Alignment);
Anders Carlssonda80af32011-01-29 18:20:20 +0000812
Alexey Samsonov1444bb92014-10-17 00:20:19 +0000813 llvm::Function *
814 CreateGlobalInitOrDestructFunction(llvm::FunctionType *ty, const Twine &name,
Akira Hatanaka7791f1a42015-10-31 01:28:07 +0000815 const CGFunctionInfo &FI,
Alexey Samsonov1444bb92014-10-17 00:20:19 +0000816 SourceLocation Loc = SourceLocation(),
817 bool TLS = false);
David Majnemerb3341ea2014-10-05 05:05:40 +0000818
Yaxun Liucbf647c2017-07-08 13:24:52 +0000819 /// Return the AST address space of the underlying global variable for D, as
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000820 /// determined by its declaration. Normally this is the same as the address
821 /// space of D's type, but in CUDA, address spaces are associated with
Yaxun Liucbf647c2017-07-08 13:24:52 +0000822 /// declarations, not types. If D is nullptr, return the default address
823 /// space for global variable.
824 ///
825 /// For languages without explicit address spaces, if D has default address
826 /// space, target-specific global or constant address space may be returned.
Alexander Richardson6d989432017-10-15 18:48:14 +0000827 LangAS GetGlobalVarAddressSpace(const VarDecl *D);
Peter Collingbournef44bdf92012-05-20 21:08:35 +0000828
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000829 /// Return the llvm::Constant for the address of the given global variable.
Andrey Bokhanko7aa88ce2016-01-14 10:41:16 +0000830 /// If Ty is non-null and if the global doesn't exist, then it will be created
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000831 /// with the specified type instead of whatever the normal requested type
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +0000832 /// would be. If IsForDefinition is true, it is guaranteed that an actual
Andrey Bokhanko7aa88ce2016-01-14 10:41:16 +0000833 /// global with type Ty will be returned, not conversion of a variable with
834 /// the same mangled name but some other type.
Chris Lattner149927c2009-03-21 09:16:30 +0000835 llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D,
Andrey Bokhanko7aa88ce2016-01-14 10:41:16 +0000836 llvm::Type *Ty = nullptr,
John McCalld195d4c2016-11-30 23:25:13 +0000837 ForDefinition_t IsForDefinition
838 = NotForDefinition);
Daniel Dunbar9c426522008-07-29 23:18:29 +0000839
Yaxun Liudaceb1e2018-05-14 19:20:12 +0000840 /// Return the AST address space of string literal, which is used to emit
841 /// the string literal as global variable in LLVM IR.
842 /// Note: This is not necessarily the address space of the string literal
843 /// in AST. For address space agnostic language, e.g. C++, string literal
844 /// in AST is always in default address space.
845 LangAS getStringLiteralAddressSpace() const;
846
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000847 /// Return the address of the given function. If Ty is non-null, then this
848 /// function will use the specified type if it has to create it.
Hans Wennborg59dbe862015-09-29 20:56:43 +0000849 llvm::Constant *GetAddrOfFunction(GlobalDecl GD, llvm::Type *Ty = nullptr,
Rafael Espindola94abb8f2013-12-09 04:29:47 +0000850 bool ForVTable = false,
Andrey Bokhankocab58582015-08-31 13:20:44 +0000851 bool DontDefer = false,
John McCalld195d4c2016-11-30 23:25:13 +0000852 ForDefinition_t IsForDefinition
853 = NotForDefinition);
Daniel Dunbarc4baa062008-08-13 23:20:05 +0000854
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000855 /// Get the address of the RTTI descriptor for the given type.
John McCall48bf3492010-04-30 01:15:21 +0000856 llvm::Constant *GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH = false);
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +0000857
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000858 /// Get the address of a uuid descriptor .
John McCall7f416cc2015-09-08 08:05:57 +0000859 ConstantAddress GetAddrOfUuidDescriptor(const CXXUuidofExpr* E);
Nico Webercf4ff5862012-10-11 10:13:44 +0000860
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000861 /// Get the address of the thunk for the given global decl.
Reid Kleckner399d96e2018-04-02 20:20:33 +0000862 llvm::Constant *GetAddrOfThunk(StringRef Name, llvm::Type *FnTy,
863 GlobalDecl GD);
Anders Carlssoncd836f02010-03-23 17:17:29 +0000864
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000865 /// Get a reference to the target of VD.
John McCall7f416cc2015-09-08 08:05:57 +0000866 ConstantAddress GetWeakRefReference(const ValueDecl *VD);
867
868 /// Returns the assumed alignment of an opaque pointer to the given class.
869 CharUnits getClassPointerAlignment(const CXXRecordDecl *CD);
870
Richard Smith0130b6c2020-01-31 19:06:21 -0800871 /// Returns the minimum object size for an object of the given class type
872 /// (or a class derived from it).
873 CharUnits getMinimumClassObjectSize(const CXXRecordDecl *CD);
874
875 /// Returns the minimum object size for an object of the given type.
876 CharUnits getMinimumObjectSize(QualType Ty) {
877 if (CXXRecordDecl *RD = Ty->getAsCXXRecordDecl())
878 return getMinimumClassObjectSize(RD);
879 return getContext().getTypeSizeInChars(Ty);
880 }
881
John McCall7f416cc2015-09-08 08:05:57 +0000882 /// Returns the assumed alignment of a virtual base of a class.
883 CharUnits getVBaseAlignment(CharUnits DerivedAlign,
884 const CXXRecordDecl *Derived,
885 const CXXRecordDecl *VBase);
886
887 /// Given a class pointer with an actual known alignment, and the
888 /// expected alignment of an object at a dynamic offset w.r.t that
889 /// pointer, return the alignment to assume at the offset.
890 CharUnits getDynamicOffsetAlignment(CharUnits ActualAlign,
891 const CXXRecordDecl *Class,
892 CharUnits ExpectedTargetAlign);
Rafael Espindola2e42fec2010-03-04 18:17:24 +0000893
David Majnemerc1709d32015-06-23 07:31:11 +0000894 CharUnits
895 computeNonVirtualBaseClassOffset(const CXXRecordDecl *DerivedClass,
896 CastExpr::path_const_iterator Start,
897 CastExpr::path_const_iterator End);
898
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000899 /// Returns the offset from a derived class to a class. Returns null if the
900 /// offset is 0.
Anders Carlsson8a64c1c2010-04-24 21:23:59 +0000901 llvm::Constant *
902 GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl,
John McCallcf142162010-08-07 06:22:56 +0000903 CastExpr::path_const_iterator PathBegin,
904 CastExpr::path_const_iterator PathEnd);
John McCallad7c5c12011-02-08 08:22:06 +0000905
John McCall7f416cc2015-09-08 08:05:57 +0000906 llvm::FoldingSet<BlockByrefHelpers> ByrefHelpersCache;
John McCallad7c5c12011-02-08 08:22:06 +0000907
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000908 /// Fetches the global unique block count.
John McCall147d0212011-02-22 22:38:33 +0000909 int getUniqueBlockCount() { return ++Block.GlobalUniqueCount; }
Fangrui Song6907ce22018-07-30 19:24:48 +0000910
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000911 /// Fetches the type of a generic block descriptor.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000912 llvm::Type *getBlockDescriptorType();
John McCallad7c5c12011-02-08 08:22:06 +0000913
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000914 /// The type of a generic block literal.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000915 llvm::Type *getGenericBlockLiteralType();
John McCallad7c5c12011-02-08 08:22:06 +0000916
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000917 /// Gets the address of a block which requires no captures.
George Burgess IV70d15b32016-11-03 02:21:43 +0000918 llvm::Constant *GetAddrOfGlobalBlock(const BlockExpr *BE, StringRef Name);
George Burgess IVe3763372016-12-22 02:50:20 +0000919
920 /// Returns the address of a block which requires no caputres, or null if
921 /// we've yet to emit the block for BE.
922 llvm::Constant *getAddrOfGlobalBlockIfEmitted(const BlockExpr *BE) {
923 return EmittedGlobalBlocks.lookup(BE);
924 }
925
926 /// Notes that BE's global block is available via Addr. Asserts that BE
927 /// isn't already emitted.
928 void setAddrOfGlobalBlock(const BlockExpr *BE, llvm::Constant *Addr);
Fangrui Song6907ce22018-07-30 19:24:48 +0000929
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000930 /// Return a pointer to a constant CFString object for the given string.
John McCall7f416cc2015-09-08 08:05:57 +0000931 ConstantAddress GetAddrOfConstantCFString(const StringLiteral *Literal);
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000932
933 /// Return a pointer to a constant NSString object for the given string. Or a
934 /// user defined String object as defined via
Fariborz Jahanian50c925f2010-10-19 17:19:29 +0000935 /// -fconstant-string-class=class_name option.
John McCall7f416cc2015-09-08 08:05:57 +0000936 ConstantAddress GetAddrOfConstantString(const StringLiteral *Literal);
Chris Lattner36fc8792008-02-11 00:02:17 +0000937
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000938 /// Return a constant array for the given string.
Eli Friedmanfcec6302011-11-01 02:23:42 +0000939 llvm::Constant *GetConstantArrayFromStringLiteral(const StringLiteral *E);
940
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000941 /// Return a pointer to a constant array for the given string literal.
John McCall7f416cc2015-09-08 08:05:57 +0000942 ConstantAddress
Alexey Bataevec474782014-10-09 08:45:04 +0000943 GetAddrOfConstantStringFromLiteral(const StringLiteral *S,
944 StringRef Name = ".str");
Daniel Dunbar6dfdf8c2008-08-10 20:25:57 +0000945
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000946 /// Return a pointer to a constant array for the given ObjCEncodeExpr node.
John McCall7f416cc2015-09-08 08:05:57 +0000947 ConstantAddress
Alexey Samsonovb2cc23d2014-07-10 22:18:36 +0000948 GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *);
Mike Stump11289f42009-09-09 15:08:12 +0000949
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000950 /// Returns a pointer to a character array containing the literal and a
951 /// terminating '\0' character. The result has pointer to array type.
Daniel Dunbardfcf5992008-10-17 21:56:50 +0000952 ///
Mike Stump1676c5b2009-02-13 19:12:34 +0000953 /// \param GlobalName If provided, the name to use for the global (if one is
954 /// created).
John McCall7f416cc2015-09-08 08:05:57 +0000955 ConstantAddress
Alexey Samsonovb2cc23d2014-07-10 22:18:36 +0000956 GetAddrOfConstantCString(const std::string &Str,
John McCall7f416cc2015-09-08 08:05:57 +0000957 const char *GlobalName = nullptr);
Richard Smith2d988f02011-11-22 22:48:32 +0000958
Rafael Espindolae98ed2f2014-05-09 01:34:38 +0000959 /// Returns a pointer to a constant global variable for the given file-scope
960 /// compound literal expression.
John McCall7f416cc2015-09-08 08:05:57 +0000961 ConstantAddress GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr*E);
Richard Smithe6c01442013-06-05 00:46:14 +0000962
George Burgess IV1a39b862016-12-28 07:27:40 +0000963 /// If it's been emitted already, returns the GlobalVariable corresponding to
964 /// a compound literal. Otherwise, returns null.
965 llvm::GlobalVariable *
966 getAddrOfConstantCompoundLiteralIfEmitted(const CompoundLiteralExpr *E);
967
968 /// Notes that CLE's GlobalVariable is GV. Asserts that CLE isn't already
969 /// emitted.
970 void setAddrOfConstantCompoundLiteral(const CompoundLiteralExpr *CLE,
971 llvm::GlobalVariable *GV);
972
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000973 /// Returns a pointer to a global variable representing a temporary
Richard Smithe6c01442013-06-05 00:46:14 +0000974 /// with static or thread storage duration.
John McCall7f416cc2015-09-08 08:05:57 +0000975 ConstantAddress GetAddrOfGlobalTemporary(const MaterializeTemporaryExpr *E,
Richard Smithe6c01442013-06-05 00:46:14 +0000976 const Expr *Inner);
977
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000978 /// Retrieve the record type that describes the state of an
Douglas Gregor636e2002011-08-09 17:23:49 +0000979 /// Objective-C fast enumeration loop (for..in).
980 QualType getObjCFastEnumerationStateType();
Rafael Espindola8d2a19b2014-09-08 16:01:27 +0000981
Rafael Espindola53686182014-09-15 19:34:18 +0000982 // Produce code for this constructor/destructor. This method doesn't try
983 // to apply any ABI rules about which other constructors/destructors
984 // are needed or if they are alias to each other.
Peter Collingbourned1c5b282019-03-22 23:05:10 +0000985 llvm::Function *codegenCXXStructor(GlobalDecl GD);
Rafael Espindola53686182014-09-15 19:34:18 +0000986
Rafael Espindola1ac0ec82014-09-11 15:42:06 +0000987 /// Return the address of the constructor/destructor of the given type.
Andrey Bokhankocab58582015-08-31 13:20:44 +0000988 llvm::Constant *
Peter Collingbourned1c5b282019-03-22 23:05:10 +0000989 getAddrOfCXXStructor(GlobalDecl GD, const CGFunctionInfo *FnInfo = nullptr,
Rafael Espindola8d2a19b2014-09-08 16:01:27 +0000990 llvm::FunctionType *FnType = nullptr,
John McCalld195d4c2016-11-30 23:25:13 +0000991 bool DontDefer = false,
James Y Knightf7321542019-02-07 01:14:17 +0000992 ForDefinition_t IsForDefinition = NotForDefinition) {
Peter Collingbourned1c5b282019-03-22 23:05:10 +0000993 return cast<llvm::Constant>(getAddrAndTypeOfCXXStructor(GD, FnInfo, FnType,
994 DontDefer,
James Y Knightf7321542019-02-07 01:14:17 +0000995 IsForDefinition)
996 .getCallee());
997 }
998
999 llvm::FunctionCallee getAddrAndTypeOfCXXStructor(
Peter Collingbourned1c5b282019-03-22 23:05:10 +00001000 GlobalDecl GD, const CGFunctionInfo *FnInfo = nullptr,
James Y Knightf7321542019-02-07 01:14:17 +00001001 llvm::FunctionType *FnType = nullptr, bool DontDefer = false,
1002 ForDefinition_t IsForDefinition = NotForDefinition);
Rafael Espindola8d2a19b2014-09-08 16:01:27 +00001003
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001004 /// Given a builtin id for a function like "__builtin_fabsf", return a
1005 /// Function* for "fabsf".
John McCallb92ab1a2016-10-26 23:46:34 +00001006 llvm::Constant *getBuiltinLibFunction(const FunctionDecl *FD,
1007 unsigned BuiltinID);
Daniel Dunbarc4baa062008-08-13 23:20:05 +00001008
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00001009 llvm::Function *getIntrinsic(unsigned IID, ArrayRef<llvm::Type*> Tys = None);
Daniel Dunbar9c426522008-07-29 23:18:29 +00001010
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001011 /// Emit code for a single top level declaration.
Daniel Dunbarfce4be82008-08-15 23:26:23 +00001012 void EmitTopLevelDecl(Decl *D);
Daniel Dunbar9c426522008-07-29 23:18:29 +00001013
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001014 /// Stored a deferred empty coverage mapping for an unused
Alex Lorenzee024992014-08-04 18:41:51 +00001015 /// and thus uninstrumented top level declaration.
1016 void AddDeferredUnusedCoverageMapping(Decl *D);
1017
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001018 /// Remove the deferred empty coverage mapping as this
Alex Lorenzee024992014-08-04 18:41:51 +00001019 /// declaration is actually instrumented.
1020 void ClearUnusedCoverageMapping(const Decl *D);
1021
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001022 /// Emit all the deferred coverage mappings
Alex Lorenzee024992014-08-04 18:41:51 +00001023 /// for the uninstrumented functions.
1024 void EmitDeferredUnusedCoverageMappings();
1025
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001026 /// Tell the consumer that this variable has been instantiated.
Rafael Espindoladf88f6f2012-03-08 15:51:03 +00001027 void HandleCXXStaticMemberVarInstantiation(VarDecl *VD);
Rafael Espindola189fa742012-03-05 10:54:55 +00001028
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001029 /// If the declaration has internal linkage but is inside an
Richard Smithdf931ce2013-04-06 05:00:46 +00001030 /// extern "C" linkage specification, prepare to emit an alias for it
1031 /// to the expected name.
1032 template<typename SomeDecl>
1033 void MaybeHandleStaticInExternC(const SomeDecl *D, llvm::GlobalValue *GV);
1034
Rafael Espindola060062a2014-03-06 22:15:10 +00001035 /// Add a global to a list to be added to the llvm.used metadata.
1036 void addUsedGlobal(llvm::GlobalValue *GV);
1037
1038 /// Add a global to a list to be added to the llvm.compiler.used metadata.
1039 void addCompilerUsedGlobal(llvm::GlobalValue *GV);
Daniel Dunbar08b26a02009-02-13 20:29:50 +00001040
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001041 /// Add a destructor and object to add to the C++ global destructor function.
James Y Knightf7321542019-02-07 01:14:17 +00001042 void AddCXXDtorEntry(llvm::FunctionCallee DtorFn, llvm::Constant *Object) {
1043 CXXGlobalDtors.emplace_back(DtorFn.getFunctionType(), DtorFn.getCallee(),
1044 Object);
Chris Lattner87233f72010-06-19 05:52:45 +00001045 }
Daniel Dunbarfe06df42010-03-20 04:15:41 +00001046
James Y Knight9871db02019-02-05 16:42:33 +00001047 /// Create or return a runtime function declaration with the specified type
Matt Arsenault40ab8ae2019-10-27 14:17:40 -07001048 /// and name. If \p AssumeConvergent is true, the call will have the
1049 /// convergent attribute added.
James Y Knight9871db02019-02-05 16:42:33 +00001050 llvm::FunctionCallee
Saleem Abdulrasool6cb07442016-12-15 06:59:05 +00001051 CreateRuntimeFunction(llvm::FunctionType *Ty, StringRef Name,
Reid Klecknerde864822017-03-21 16:57:30 +00001052 llvm::AttributeList ExtraAttrs = llvm::AttributeList(),
Matt Arsenault40ab8ae2019-10-27 14:17:40 -07001053 bool Local = false, bool AssumeConvergent = false);
1054
1055 /// Create or return a runtime function declaration with the specified type
1056 /// and name. This will automatically add the convergent attribute to the
1057 /// function declaration.
1058 llvm::FunctionCallee CreateConvergentRuntimeFunction(
1059 llvm::FunctionType *Ty, StringRef Name,
1060 llvm::AttributeList ExtraAttrs = llvm::AttributeList(),
1061 bool Local = false) {
1062 return CreateRuntimeFunction(Ty, Name, ExtraAttrs, Local, true);
1063 }
Saleem Abdulrasool6cb07442016-12-15 06:59:05 +00001064
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001065 /// Create a new runtime global variable with the specified type and name.
Chris Lattner2192fe52011-07-18 04:24:23 +00001066 llvm::Constant *CreateRuntimeVariable(llvm::Type *Ty,
Chris Lattner01cf8db2011-07-20 06:58:45 +00001067 StringRef Name);
Daniel Dunbar23fd4622008-10-01 00:49:24 +00001068
Daniel Dunbar900546d2010-07-16 00:00:15 +00001069 ///@name Custom Blocks Runtime Interfaces
1070 ///@{
1071
1072 llvm::Constant *getNSConcreteGlobalBlock();
1073 llvm::Constant *getNSConcreteStackBlock();
James Y Knight9871db02019-02-05 16:42:33 +00001074 llvm::FunctionCallee getBlockObjectAssign();
1075 llvm::FunctionCallee getBlockObjectDispose();
Daniel Dunbar900546d2010-07-16 00:00:15 +00001076
1077 ///@}
1078
James Y Knight76f78742019-02-05 19:17:50 +00001079 llvm::Function *getLLVMLifetimeStartFn();
1080 llvm::Function *getLLVMLifetimeEndFn();
Nadav Rotem1da30942013-03-23 06:43:35 +00001081
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001082 // Make sure that this type is translated.
Devang Patel945b8ae2011-03-23 16:29:39 +00001083 void UpdateCompletedType(const TagDecl *TD);
Daniel Dunbar38ad1e62009-02-17 18:43:32 +00001084
John McCallf3a88602011-02-03 08:15:49 +00001085 llvm::Constant *getMemberPointerConstant(const UnaryOperator *e);
1086
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001087 /// Emit type info if type of an expression is a variably modified
Alexey Bataev2bf9b4c2015-10-20 04:24:12 +00001088 /// type. Also emit proper debug info for cast types.
1089 void EmitExplicitCastExprType(const ExplicitCastExpr *E,
1090 CodeGenFunction *CGF = nullptr);
1091
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001092 /// Return the result of value-initializing the given type, i.e. a null
1093 /// expression of the given type. This is usually, but not always, an LLVM
1094 /// null constant.
Eli Friedman20bb5e02009-04-13 21:47:26 +00001095 llvm::Constant *EmitNullConstant(QualType T);
1096
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001097 /// Return a null constant appropriate for zero-initializing a base class with
1098 /// the given type. This is usually, but not always, an LLVM null constant.
Eli Friedmanfde961d2011-10-14 02:27:24 +00001099 llvm::Constant *EmitNullConstantForBase(const CXXRecordDecl *Record);
1100
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001101 /// Emit a general error that something can't be done.
Chandler Carruth84537952012-03-30 19:44:53 +00001102 void Error(SourceLocation loc, StringRef error);
John McCall7ef5cb32011-03-18 02:56:14 +00001103
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001104 /// Print out an error that codegen doesn't support the specified stmt yet.
David Blaikie4a9ec7b2013-08-19 21:02:26 +00001105 void ErrorUnsupported(const Stmt *S, const char *Type);
Mike Stump1676c5b2009-02-13 19:12:34 +00001106
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001107 /// Print out an error that codegen doesn't support the specified decl yet.
David Blaikie4a9ec7b2013-08-19 21:02:26 +00001108 void ErrorUnsupported(const Decl *D, const char *Type);
Dan Gohman75d69da2008-05-22 00:50:06 +00001109
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001110 /// Set the attributes on the LLVM function for the given decl and function
1111 /// info. This applies attributes necessary for handling the ABI as well as
1112 /// user specified attributes like section.
Rafael Espindola51ec5a92018-02-28 23:46:35 +00001113 void SetInternalFunctionAttributes(GlobalDecl GD, llvm::Function *F,
Daniel Dunbarc3e7cff2009-04-17 00:48:04 +00001114 const CGFunctionInfo &FI);
Daniel Dunbar449a3392008-09-04 23:41:35 +00001115
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001116 /// Set the LLVM function attributes (sext, zext, etc).
Erich Keanede6480a32018-11-13 15:48:08 +00001117 void SetLLVMFunctionAttributes(GlobalDecl GD, const CGFunctionInfo &Info,
Daniel Dunbaraeddffc2009-04-14 07:08:30 +00001118 llvm::Function *F);
Daniel Dunbarc68897d2008-09-10 00:41:16 +00001119
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001120 /// Set the LLVM function attributes which only apply to a function
James Dennett64fa1232014-08-15 20:04:40 +00001121 /// definition.
Daniel Dunbar38932572009-04-14 08:05:55 +00001122 void SetLLVMFunctionAttributesForDefinition(const Decl *D, llvm::Function *F);
1123
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001124 /// Return true iff the given type uses 'sret' when used as a return type.
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001125 bool ReturnTypeUsesSRet(const CGFunctionInfo &FI);
1126
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001127 /// Return true iff the given type uses an argument slot when 'sret' is used
1128 /// as a return type.
Tim Northovere77cc392014-03-29 13:28:05 +00001129 bool ReturnSlotInterferesWithArgs(const CGFunctionInfo &FI);
1130
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001131 /// Return true iff the given type uses 'fpret' when used as a return type.
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001132 bool ReturnTypeUsesFPRet(QualType ResultType);
Daniel Dunbarc68897d2008-09-10 00:41:16 +00001133
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001134 /// Return true iff the given type uses 'fp2ret' when used as a return type.
Anders Carlsson2f1a6c32011-10-31 16:27:11 +00001135 bool ReturnTypeUsesFP2Ret(QualType ResultType);
1136
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001137 /// Get the LLVM attributes and calling convention to use for a particular
1138 /// function type.
Daniel Dunbar0ef34792009-09-12 00:59:20 +00001139 ///
Chad Rosier7dbc9cf2016-01-06 14:35:46 +00001140 /// \param Name - The function name.
Daniel Dunbar0ef34792009-09-12 00:59:20 +00001141 /// \param Info - The function type information.
Samuel Antao798f11c2015-11-23 22:04:44 +00001142 /// \param CalleeInfo - The callee information these attributes are being
1143 /// constructed for. If valid, the attributes applied to this decl may
1144 /// contribute to the function attributes and calling convention.
Reid Klecknercdd26792017-04-18 23:50:03 +00001145 /// \param Attrs [out] - On return, the attribute list to use.
Daniel Dunbar0ef34792009-09-12 00:59:20 +00001146 /// \param CallingConv [out] - On return, the LLVM calling convention to use.
Chad Rosier7dbc9cf2016-01-06 14:35:46 +00001147 void ConstructAttributeList(StringRef Name, const CGFunctionInfo &Info,
Reid Klecknercdd26792017-04-18 23:50:03 +00001148 CGCalleeInfo CalleeInfo,
1149 llvm::AttributeList &Attrs, unsigned &CallingConv,
1150 bool AttrOnCallSite);
Daniel Dunbarc68897d2008-09-10 00:41:16 +00001151
Justin Lebarb080b632017-01-25 21:29:48 +00001152 /// Adds attributes to F according to our CodeGenOptions and LangOptions, as
1153 /// though we had emitted it ourselves. We remove any attributes on F that
1154 /// conflict with the attributes we add here.
1155 ///
1156 /// This is useful for adding attrs to bitcode modules that you want to link
1157 /// with but don't control, such as CUDA's libdevice. When linking with such
1158 /// a bitcode library, you might want to set e.g. its functions'
1159 /// "unsafe-fp-math" attribute to match the attr of the functions you're
1160 /// codegen'ing. Otherwise, LLVM will interpret the bitcode module's lack of
1161 /// unsafe-fp-math attrs as tantamount to unsafe-fp-math=false, and then LLVM
1162 /// will propagate unsafe-fp-math=false up to every transitive caller of a
1163 /// function in the bitcode library!
1164 ///
1165 /// With the exception of fast-math attrs, this will only make the attributes
1166 /// on the function more conservative. But it's unsafe to call this on a
1167 /// function which relies on particular fast-math attributes for correctness.
1168 /// It's up to you to ensure that this is safe.
1169 void AddDefaultFnAttrs(llvm::Function &F);
1170
Chris Lattner01cf8db2011-07-20 06:58:45 +00001171 StringRef getMangledName(GlobalDecl GD);
Alp Tokerfb8d02b2014-06-05 22:10:59 +00001172 StringRef getBlockMangledName(GlobalDecl GD, const BlockDecl *BD);
Douglas Gregorbeecd582009-04-21 17:11:58 +00001173
1174 void EmitTentativeDefinition(const VarDecl *D);
Mike Stumpbc78a722009-07-31 18:25:34 +00001175
Yonghong Songe3d8ee32019-11-22 08:45:37 -08001176 void EmitExternalDeclaration(const VarDecl *D);
1177
Nico Weberb6a5d052015-01-15 04:07:35 +00001178 void EmitVTable(CXXRecordDecl *Class);
Douglas Gregor88d292c2010-05-13 16:44:06 +00001179
David Majnemer929025d2016-01-26 19:30:26 +00001180 void RefreshTypeCacheForClass(const CXXRecordDecl *Class);
1181
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001182 /// Appends Opts to the "llvm.linker.options" metadata value.
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001183 void AppendLinkerOptions(StringRef Opts);
1184
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001185 /// Appends a detect mismatch command to the linker options.
Aaron Ballman5d041be2013-06-04 02:07:14 +00001186 void AddDetectMismatch(StringRef Name, StringRef Value);
1187
Ben Dunbobbin1d165152019-05-17 03:44:15 +00001188 /// Appends a dependent lib to the appropriate metadata value.
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001189 void AddDependentLib(StringRef Lib);
1190
Saleem Abdulrasoolfd4db532018-02-07 01:46:46 +00001191
Peter Collingbourne4d90dba2013-06-05 17:49:37 +00001192 llvm::GlobalVariable::LinkageTypes getFunctionLinkage(GlobalDecl GD);
John McCalld4324142010-02-19 01:32:20 +00001193
Rafael Espindola64926362014-05-08 14:46:46 +00001194 void setFunctionLinkage(GlobalDecl GD, llvm::Function *F) {
1195 F->setLinkage(getFunctionLinkage(GD));
John McCall7cb02202010-05-25 04:30:21 +00001196 }
1197
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001198 /// Return the appropriate linkage for the vtable, VTT, and type information
1199 /// of the given class.
Anders Carlsson68d34242011-01-24 02:04:33 +00001200 llvm::GlobalVariable::LinkageTypes getVTableLinkage(const CXXRecordDecl *RD);
Ken Dyck98ca7942010-01-26 13:48:07 +00001201
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001202 /// Return the store size, in character units, of the given LLVM type.
Chris Lattner2192fe52011-07-18 04:24:23 +00001203 CharUnits GetTargetTypeStoreSize(llvm::Type *Ty) const;
Fangrui Song6907ce22018-07-30 19:24:48 +00001204
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001205 /// Returns LLVM linkage for a declarator.
David Majnemer27d69db2014-04-28 22:17:59 +00001206 llvm::GlobalValue::LinkageTypes
Hans Wennborg1a3a0742014-05-14 19:54:53 +00001207 getLLVMLinkageForDeclarator(const DeclaratorDecl *D, GVALinkage Linkage,
Hans Wennborg275efb92014-05-28 01:52:23 +00001208 bool IsConstantVariable);
David Majnemer27d69db2014-04-28 22:17:59 +00001209
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001210 /// Returns LLVM linkage for a declarator.
David Majnemer27d69db2014-04-28 22:17:59 +00001211 llvm::GlobalValue::LinkageTypes
1212 getLLVMLinkageVarDefinition(const VarDecl *VD, bool IsConstant);
1213
Julien Lerouge5a6b6982011-09-09 22:41:49 +00001214 /// Emit all the global annotations.
1215 void EmitGlobalAnnotations();
1216
1217 /// Emit an annotation string.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001218 llvm::Constant *EmitAnnotationString(StringRef Str);
Julien Lerouge5a6b6982011-09-09 22:41:49 +00001219
1220 /// Emit the annotation's translation unit.
1221 llvm::Constant *EmitAnnotationUnit(SourceLocation Loc);
1222
1223 /// Emit the annotation line number.
1224 llvm::Constant *EmitAnnotationLineNo(SourceLocation L);
1225
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001226 /// Generate the llvm::ConstantStruct which contains the annotation
1227 /// information for a given GlobalValue. The annotation struct is
Julien Lerouge5a6b6982011-09-09 22:41:49 +00001228 /// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the
1229 /// GlobalValue being annotated. The second field is the constant string
1230 /// created from the AnnotateAttr's annotation. The third field is a constant
1231 /// string containing the name of the translation unit. The fourth field is
1232 /// the line number in the file of the annotated value declaration.
1233 llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV,
1234 const AnnotateAttr *AA,
1235 SourceLocation L);
1236
1237 /// Add global annotations that are set on D, for the global GV. Those
1238 /// annotations are emitted during finalization of the LLVM code.
1239 void AddGlobalAnnotations(const ValueDecl *D, llvm::GlobalValue *GV);
1240
Vlad Tsyrklevich2eccdab2017-09-25 22:11:12 +00001241 bool isInSanitizerBlacklist(SanitizerMask Kind, llvm::Function *Fn,
1242 SourceLocation Loc) const;
Will Dietzf54319c2013-01-18 11:30:38 +00001243
Alexey Samsonova0ac3c22014-10-17 22:37:33 +00001244 bool isInSanitizerBlacklist(llvm::GlobalVariable *GV, SourceLocation Loc,
1245 QualType Ty,
1246 StringRef Category = StringRef()) const;
1247
Dean Michael Berris835832d2017-03-30 00:29:36 +00001248 /// Imbue XRay attributes to a function, applying the always/never attribute
1249 /// lists in the process. Returns true if we did imbue attributes this way,
1250 /// false otherwise.
1251 bool imbueXRayAttrs(llvm::Function *Fn, SourceLocation Loc,
1252 StringRef Category = StringRef()) const;
1253
Alexey Samsonov4b8de112014-08-01 21:35:28 +00001254 SanitizerMetadata *getSanitizerMetadata() {
1255 return SanitizerMD.get();
1256 }
Alexey Samsonov4f319cc2014-07-02 16:54:41 +00001257
John McCall6bd2a892013-01-25 22:31:03 +00001258 void addDeferredVTable(const CXXRecordDecl *RD) {
1259 DeferredVTables.push_back(RD);
1260 }
1261
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00001262 /// Emit code for a single global function or var decl. Forward declarations
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001263 /// are emitted lazily.
Reid Klecknere7de47e2013-07-22 13:51:44 +00001264 void EmitGlobal(GlobalDecl D);
1265
Rafael Espindola02b77f42014-09-15 18:46:13 +00001266 bool TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D);
1267
Chris Lattner01cf8db2011-07-20 06:58:45 +00001268 llvm::GlobalValue *GetGlobalValue(StringRef Ref);
Mike Stump11289f42009-09-09 15:08:12 +00001269
Rafael Espindola1e4df922014-09-16 15:18:21 +00001270 /// Set attributes which are common to any form of a global definition (alias,
1271 /// Objective-C method, function, global variable).
1272 ///
1273 /// NOTE: This should only be called for definitions.
Rafael Espindola75c649c2018-03-01 00:06:55 +00001274 void SetCommonAttributes(GlobalDecl GD, llvm::GlobalValue *GV);
Rafael Espindola1e4df922014-09-16 15:18:21 +00001275
1276 void addReplacement(StringRef Name, llvm::Constant *C);
Alexey Bataev97720002014-11-11 04:05:39 +00001277
Andrey Bokhankocab58582015-08-31 13:20:44 +00001278 void addGlobalValReplacement(llvm::GlobalValue *GV, llvm::Constant *C);
1279
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001280 /// Emit a code for threadprivate directive.
Alexey Bataev97720002014-11-11 04:05:39 +00001281 /// \param D Threadprivate declaration.
1282 void EmitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D);
1283
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001284 /// Emit a code for declare reduction construct.
Alexey Bataevc5b1d322016-03-04 09:22:22 +00001285 void EmitOMPDeclareReduction(const OMPDeclareReductionDecl *D,
1286 CodeGenFunction *CGF = nullptr);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001287
Michael Kruse251e1482019-02-01 20:25:04 +00001288 /// Emit a code for declare mapper construct.
1289 void EmitOMPDeclareMapper(const OMPDeclareMapperDecl *D,
1290 CodeGenFunction *CGF = nullptr);
1291
Kelvin Li1408f912018-09-26 04:28:39 +00001292 /// Emit a code for requires directive.
1293 /// \param D Requires declaration
1294 void EmitOMPRequiresDecl(const OMPRequiresDecl *D);
1295
Alexey Bataevc2cd2d42019-10-10 17:28:10 +00001296 /// Emits the definition of \p OldGD function with body from \p NewGD.
1297 /// Required for proper handling of declare variant directive on the GPU.
1298 void emitOpenMPDeviceFunctionRedefinition(GlobalDecl OldGD, GlobalDecl NewGD,
1299 llvm::GlobalValue *GV);
1300
Peter Collingbourne3afb2662016-04-28 17:09:37 +00001301 /// Returns whether the given record has hidden LTO visibility and therefore
1302 /// may participate in (single-module) CFI and whole-program vtable
1303 /// optimization.
1304 bool HasHiddenLTOVisibility(const CXXRecordDecl *RD);
Peter Collingbournee5706442015-07-09 19:56:14 +00001305
Teresa Johnson2f63d542020-01-24 12:24:18 -08001306 /// Returns whether the given record has public std LTO visibility
1307 /// and therefore may not participate in (single-module) CFI and whole-program
1308 /// vtable optimization.
1309 bool HasLTOVisibilityPublicStd(const CXXRecordDecl *RD);
1310
Oliver Stannard3b598b92019-10-17 09:58:57 +00001311 /// Returns the vcall visibility of the given type. This is the scope in which
1312 /// a virtual function call could be made which ends up being dispatched to a
1313 /// member function of this class. This scope can be wider than the visibility
1314 /// of the class itself when the class has a more-visible dynamic base class.
1315 llvm::GlobalObject::VCallVisibility
1316 GetVCallVisibilityLevel(const CXXRecordDecl *RD);
1317
Peter Collingbourne8dd14da2016-06-24 21:21:46 +00001318 /// Emit type metadata for the given vtable using the given layout.
Oliver Stannard3b598b92019-10-17 09:58:57 +00001319 void EmitVTableTypeMetadata(const CXXRecordDecl *RD,
1320 llvm::GlobalVariable *VTable,
Peter Collingbourne8dd14da2016-06-24 21:21:46 +00001321 const VTableLayout &VTLayout);
Peter Collingbournea4ccff32015-02-20 20:30:56 +00001322
Peter Collingbourne8dd14da2016-06-24 21:21:46 +00001323 /// Generate a cross-DSO type identifier for MD.
1324 llvm::ConstantInt *CreateCrossDsoCfiTypeId(llvm::Metadata *MD);
Evgeniy Stepanovfd6f92d2015-12-15 23:00:20 +00001325
Peter Collingbourne25a80bf2015-09-08 23:01:30 +00001326 /// Create a metadata identifier for the given type. This may either be an
1327 /// MDString (for external identifiers) or a distinct unnamed MDNode (for
1328 /// internal identifiers).
1329 llvm::Metadata *CreateMetadataIdentifierForType(QualType T);
1330
Peter Collingbournee44acad2018-06-26 02:15:47 +00001331 /// Create a metadata identifier that is intended to be used to check virtual
1332 /// calls via a member function pointer.
1333 llvm::Metadata *CreateMetadataIdentifierForVirtualMemPtrType(QualType T);
1334
Vlad Tsyrklevich634c6012017-10-31 22:39:44 +00001335 /// Create a metadata identifier for the generalization of the given type.
1336 /// This may either be an MDString (for external identifiers) or a distinct
1337 /// unnamed MDNode (for internal identifiers).
1338 llvm::Metadata *CreateMetadataIdentifierGeneralized(QualType T);
1339
Peter Collingbourne8dd14da2016-06-24 21:21:46 +00001340 /// Create and attach type metadata to the given function.
Peter Collingbournee44acad2018-06-26 02:15:47 +00001341 void CreateFunctionTypeMetadataForIcall(const FunctionDecl *FD,
1342 llvm::Function *F);
Evgeniy Stepanovfd6f92d2015-12-15 23:00:20 +00001343
Peter Collingbourne8dd14da2016-06-24 21:21:46 +00001344 /// Returns whether this module needs the "all-vtables" type identifier.
1345 bool NeedAllVtablesTypeId() const;
Evgeniy Stepanovf31ea302016-02-03 22:18:55 +00001346
Peter Collingbourne8dd14da2016-06-24 21:21:46 +00001347 /// Create and attach type metadata for the given vtable.
1348 void AddVTableTypeMetadata(llvm::GlobalVariable *VTable, CharUnits Offset,
1349 const CXXRecordDecl *RD);
Peter Collingbourne86d34a72015-06-17 19:08:05 +00001350
Peter Collingbournee44acad2018-06-26 02:15:47 +00001351 /// Return a vector of most-base classes for RD. This is used to implement
1352 /// control flow integrity checks for member function pointers.
1353 ///
1354 /// A most-base class of a class C is defined as a recursive base class of C,
1355 /// including C itself, that does not have any bases.
1356 std::vector<const CXXRecordDecl *>
1357 getMostBaseClasses(const CXXRecordDecl *RD);
1358
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001359 /// Get the declaration of std::terminate for the platform.
James Y Knight9871db02019-02-05 16:42:33 +00001360 llvm::FunctionCallee getTerminateFn();
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00001361
Peter Collingbournedc134532016-01-16 00:31:22 +00001362 llvm::SanitizerStatReport &getSanStats();
1363
Yaxun Liu0bc4b2d2016-07-28 19:26:30 +00001364 llvm::Value *
1365 createOpenCLIntToSamplerConversion(const Expr *E, CodeGenFunction &CGF);
1366
Anastasia Stulovae6cf6c72019-05-09 13:55:44 +00001367 /// OpenCL v1.2 s5.6.4.6 allows the compiler to store kernel argument
1368 /// information in the program executable. The argument information stored
1369 /// includes the argument name, its type, the address and access qualifiers
1370 /// used. This helper can be used to generate metadata for source code kernel
1371 /// function as well as generated implicitly kernels. If a kernel is generated
1372 /// implicitly null value has to be passed to the last two parameters,
1373 /// otherwise all parameters must have valid non-null values.
1374 /// \param FN is a pointer to IR function being generated.
1375 /// \param FD is a pointer to function declaration if any.
1376 /// \param CGF is a pointer to CodeGenFunction that generates this function.
Simon Pilgrimba8f9122019-05-10 10:36:37 +00001377 void GenOpenCLArgMetadata(llvm::Function *FN,
1378 const FunctionDecl *FD = nullptr,
1379 CodeGenFunction *CGF = nullptr);
Anastasia Stulovae6cf6c72019-05-09 13:55:44 +00001380
Yaxun Liu402804b2016-12-15 08:09:08 +00001381 /// Get target specific null pointer.
1382 /// \param T is the LLVM type of the null pointer.
1383 /// \param QT is the clang QualType of the null pointer.
1384 llvm::Constant *getNullPointer(llvm::PointerType *T, QualType QT);
1385
Rafael Espindola1e4df922014-09-16 15:18:21 +00001386private:
Reid Klecknerde864822017-03-21 16:57:30 +00001387 llvm::Constant *GetOrCreateLLVMFunction(
1388 StringRef MangledName, llvm::Type *Ty, GlobalDecl D, bool ForVTable,
1389 bool DontDefer = false, bool IsThunk = false,
1390 llvm::AttributeList ExtraAttrs = llvm::AttributeList(),
1391 ForDefinition_t IsForDefinition = NotForDefinition);
Rafael Espindola94abb8f2013-12-09 04:29:47 +00001392
Erich Keane19a8adc2018-10-25 18:57:19 +00001393 llvm::Constant *GetOrCreateMultiVersionResolver(GlobalDecl GD,
1394 llvm::Type *DeclTy,
1395 const FunctionDecl *FD);
Erich Keane281d20b2018-01-08 21:34:17 +00001396 void UpdateMultiVersionNames(GlobalDecl GD, const FunctionDecl *FD);
1397
Chris Lattner01cf8db2011-07-20 06:58:45 +00001398 llvm::Constant *GetOrCreateLLVMGlobal(StringRef MangledName,
Chris Lattner2192fe52011-07-18 04:24:23 +00001399 llvm::PointerType *PTy,
Andrey Bokhanko7aa88ce2016-01-14 10:41:16 +00001400 const VarDecl *D,
John McCalld195d4c2016-11-30 23:25:13 +00001401 ForDefinition_t IsForDefinition
1402 = NotForDefinition);
Mike Stump11289f42009-09-09 15:08:12 +00001403
Erich Keanede6480a32018-11-13 15:48:08 +00001404 bool GetCPUAndFeaturesAttributes(GlobalDecl GD,
Erich Keane93e58662018-02-12 17:01:41 +00001405 llvm::AttrBuilder &AttrBuilder);
Rafael Espindola285271c2018-02-28 23:54:23 +00001406 void setNonAliasAttributes(GlobalDecl GD, llvm::GlobalObject *GO);
Rafael Espindola502f65a2014-05-05 20:21:03 +00001407
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001408 /// Set function attributes for a function declaration.
David Majnemerb9bd6fb2014-11-01 05:42:23 +00001409 void SetFunctionAttributes(GlobalDecl GD, llvm::Function *F,
Rafael Espindoladeb10be2018-02-07 19:04:41 +00001410 bool IsIncompleteFunction, bool IsThunk);
Nuno Lopesb6f79532008-06-08 15:45:52 +00001411
Craig Topper8a13c412014-05-21 05:09:00 +00001412 void EmitGlobalDefinition(GlobalDecl D, llvm::GlobalValue *GV = nullptr);
Daniel Dunbarf0acf7b2009-02-19 07:15:39 +00001413
Rafael Espindolacd7743b2013-12-09 16:01:03 +00001414 void EmitGlobalFunctionDefinition(GlobalDecl GD, llvm::GlobalValue *GV);
Erich Keanede6480a32018-11-13 15:48:08 +00001415 void EmitMultiVersionFunctionDefinition(GlobalDecl GD, llvm::GlobalValue *GV);
1416
Andrey Bokhanko7aa88ce2016-01-14 10:41:16 +00001417 void EmitGlobalVarDefinition(const VarDecl *D, bool IsTentative = false);
Yonghong Songe3d8ee32019-11-22 08:45:37 -08001418 void EmitExternalVarDeclaration(const VarDecl *D);
John McCall7ec50432010-03-19 23:29:14 +00001419 void EmitAliasDefinition(GlobalDecl GD);
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001420 void emitIFuncDefinition(GlobalDecl GD);
Erich Keane3efe0022018-07-20 14:13:28 +00001421 void emitCPUDispatchDefinition(GlobalDecl GD);
Daniel Dunbar89654ee2008-08-26 08:29:31 +00001422 void EmitObjCPropertyImplementations(const ObjCImplementationDecl *D);
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00001423 void EmitObjCIvarInitializations(ObjCImplementationDecl *D);
Fangrui Song6907ce22018-07-30 19:24:48 +00001424
Anders Carlssonf7475242009-04-15 15:55:24 +00001425 // C++ related functions.
Mike Stump11289f42009-09-09 15:08:12 +00001426
Richard Smith8df390f2016-09-08 23:14:54 +00001427 void EmitDeclContext(const DeclContext *DC);
Anders Carlssoncbaeb9e2009-04-02 05:55:18 +00001428 void EmitLinkageSpec(const LinkageSpecDecl *D);
Anders Carlssonf7475242009-04-15 15:55:24 +00001429
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001430 /// Emit the function that initializes C++ thread_local variables.
Richard Smith2fd1d7a2013-04-19 16:42:07 +00001431 void EmitCXXThreadLocalInitFunc();
1432
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001433 /// Emit the function that initializes C++ globals.
Anders Carlssonb8be93f2009-08-08 23:24:23 +00001434 void EmitCXXGlobalInitFunc();
Eli Friedman5866fe32010-01-08 00:50:11 +00001435
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001436 /// Emit the function that destroys C++ globals.
Daniel Dunbarfe06df42010-03-20 04:15:41 +00001437 void EmitCXXGlobalDtorFunc();
1438
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001439 /// Emit the function that initializes the specified global (if PerformInit is
1440 /// true) and registers its destructor.
John McCallcdf7ef52010-11-06 09:44:32 +00001441 void EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
Richard Smith6331c402012-02-13 22:16:19 +00001442 llvm::GlobalVariable *Addr,
1443 bool PerformInit);
Eli Friedman5866fe32010-01-08 00:50:11 +00001444
Reid Kleckner1a711b12014-07-22 00:53:05 +00001445 void EmitPointerToInitFunc(const VarDecl *VD, llvm::GlobalVariable *Addr,
1446 llvm::Function *InitFunc, InitSegAttr *ISA);
1447
Daniel Dunbar74aa7e12008-08-01 00:01:51 +00001448 // FIXME: Hardcoding priority here is gross.
Reid Kleckner563f0e82014-05-23 21:13:45 +00001449 void AddGlobalCtor(llvm::Function *Ctor, int Priority = 65535,
Hans Wennborg59dbe862015-09-29 20:56:43 +00001450 llvm::Constant *AssociatedData = nullptr);
Reid Kleckner563f0e82014-05-23 21:13:45 +00001451 void AddGlobalDtor(llvm::Function *Dtor, int Priority = 65535);
Daniel Dunbar9c426522008-07-29 23:18:29 +00001452
Vassil Vassilev188ad3a2016-10-27 09:12:20 +00001453 /// EmitCtorList - Generates a global array of functions and priorities using
1454 /// the given list and name. This array will have appending linkage and is
1455 /// suitable for use as a LLVM constructor or destructor array. Clears Fns.
1456 void EmitCtorList(CtorList &Fns, const char *GlobalName);
Daniel Dunbar74aa7e12008-08-01 00:01:51 +00001457
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001458 /// Emit any needed decls for which code generation was deferred.
Dmitri Gribenkob2aa9232012-11-15 14:28:07 +00001459 void EmitDeferred();
Daniel Dunbar08b26a02009-02-13 20:29:50 +00001460
Piotr Padlewskid3b1cbd2017-06-01 08:04:05 +00001461 /// Try to emit external vtables as available_externally if they have emitted
1462 /// all inlined virtual functions. It runs after EmitDeferred() and therefore
1463 /// is not allowed to create new references to things that need to be emitted
1464 /// lazily.
1465 void EmitVTablesOpportunistically();
1466
Rafael Espindola2e2995b2013-11-05 21:37:29 +00001467 /// Call replaceAllUsesWith on all pairs in Replacements.
1468 void applyReplacements();
1469
Andrey Bokhankocab58582015-08-31 13:20:44 +00001470 /// Call replaceAllUsesWith on all pairs in GlobalValReplacements.
1471 void applyGlobalValReplacements();
1472
Rafael Espindola208b5c02013-10-22 19:26:13 +00001473 void checkAliases();
1474
Akira Hatanaka617e2612018-04-17 18:41:52 +00001475 std::map<int, llvm::TinyPtrVector<llvm::Function *>> DtorsUsingAtExit;
1476
1477 /// Register functions annotated with __attribute__((destructor)) using
1478 /// __cxa_atexit, if it is available, or atexit otherwise.
1479 void registerGlobalDtorsWithAtExit();
1480
Erich Keane281d20b2018-01-08 21:34:17 +00001481 void emitMultiVersionFunctions();
1482
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001483 /// Emit any vtables which we deferred and still have a use for.
John McCall6bd2a892013-01-25 22:31:03 +00001484 void EmitDeferredVTables();
1485
Alex Lorenza8fbef42017-03-23 11:14:27 +00001486 /// Emit a dummy function that reference a CoreFoundation symbol when
1487 /// @available is used on Darwin.
1488 void emitAtAvailableLinkGuard();
1489
Rafael Espindola060062a2014-03-06 22:15:10 +00001490 /// Emit the llvm.used and llvm.compiler.used metadata.
1491 void emitLLVMUsed();
Daniel Dunbar23fd4622008-10-01 00:49:24 +00001492
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001493 /// Emit the link options introduced by imported modules.
Douglas Gregorbc25ff42013-01-14 20:53:57 +00001494 void EmitModuleLinkOptions();
1495
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001496 /// Emit aliases for internal-linkage declarations inside "C" language
Richard Smithdf931ce2013-04-06 05:00:46 +00001497 /// linkage specifications, giving them the "expected" name where possible.
1498 void EmitStaticExternCAliases();
1499
John McCall09ae0322010-07-06 23:57:41 +00001500 void EmitDeclMetadata();
1501
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001502 /// Emit the Clang version as llvm.ident metadata.
Rafael Espindola3bfa4682013-10-16 19:28:50 +00001503 void EmitVersionIdentMetadata();
1504
Scott Linderde6beb02018-12-14 15:38:15 +00001505 /// Emit the Clang commandline as llvm.commandline metadata.
1506 void EmitCommandLineMetadata();
1507
Robert Lytton57765d52014-07-03 09:30:33 +00001508 /// Emits target specific Metadata for global declarations.
1509 void EmitTargetMetadata();
1510
Alexey Bader364a1162017-06-20 14:30:18 +00001511 /// Emits OpenCL specific Metadata e.g. OpenCL version.
1512 void EmitOpenCLMetadata();
1513
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001514 /// Emit the llvm.gcov metadata used to tell LLVM where to emit the .gcno and
1515 /// .gcda files in a way that persists in .bc files.
Nick Lewycky85c011d2011-05-05 00:08:20 +00001516 void EmitCoverageFile();
Nick Lewycky480cb992011-05-04 20:46:58 +00001517
Nico Webercf4ff5862012-10-11 10:13:44 +00001518 /// Emits the initializer for a uuidof string.
Nico Weber17d3a2c2014-09-08 16:26:36 +00001519 llvm::Constant *EmitUuidofInitializer(StringRef uuidstr);
Nico Webercf4ff5862012-10-11 10:13:44 +00001520
Hans Wennborg43a0f992015-01-10 01:19:48 +00001521 /// Determine whether the definition must be emitted; if this returns \c
1522 /// false, the definition can be emitted lazily if it's used.
1523 bool MustBeEmitted(const ValueDecl *D);
1524
1525 /// Determine whether the definition can be emitted eagerly, or should be
1526 /// delayed until the end of the translation unit. This is relevant for
1527 /// definitions whose linkage can change, e.g. implicit function instantions
1528 /// which may later be explicitly instantiated.
1529 bool MayBeEmittedEagerly(const ValueDecl *D);
John McCall0bdb1fd2010-09-16 06:16:50 +00001530
Rafael Espindolae98ed2f2014-05-09 01:34:38 +00001531 /// Check whether we can use a "simpler", more core exceptions personality
1532 /// function.
John McCall0bdb1fd2010-09-16 06:16:50 +00001533 void SimplifyPersonality();
Justin Lebarb080b632017-01-25 21:29:48 +00001534
1535 /// Helper function for ConstructAttributeList and AddDefaultFnAttrs.
1536 /// Constructs an AttrList for a function with the given properties.
1537 void ConstructDefaultFnAttrList(StringRef Name, bool HasOptnone,
1538 bool AttrOnCallSite,
1539 llvm::AttrBuilder &FuncAttrs);
Peter Collingbournee44acad2018-06-26 02:15:47 +00001540
1541 llvm::Metadata *CreateMetadataIdentifierImpl(QualType T, MetadataTypeMap &Map,
1542 StringRef Suffix);
Chris Lattnerf97fe382007-05-24 06:29:05 +00001543};
John McCallde0fe072017-08-15 21:42:52 +00001544
Chris Lattnerf97fe382007-05-24 06:29:05 +00001545} // end namespace CodeGen
1546} // end namespace clang
Chris Lattnerf97fe382007-05-24 06:29:05 +00001547
Hans Wennborg59dbe862015-09-29 20:56:43 +00001548#endif // LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H