1//===--- CodeGenModule.h - Per-Module state for LLVM CodeGen ----*- C++ -*-===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8//
9// This is the internal per-translation-unit state used for llvm translation.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H
14#define LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H
15
16#include "CGVTables.h"
17#include "CodeGenTypeCache.h"
18#include "CodeGenTypes.h"
19#include "SanitizerMetadata.h"
20#include "TrapReasonBuilder.h"
21#include "clang/AST/DeclCXX.h"
22#include "clang/AST/DeclObjC.h"
23#include "clang/AST/DeclOpenMP.h"
24#include "clang/AST/GlobalDecl.h"
25#include "clang/AST/Mangle.h"
26#include "clang/Basic/ABI.h"
27#include "clang/Basic/LangOptions.h"
28#include "clang/Basic/NoSanitizeList.h"
29#include "clang/Basic/ProfileList.h"
30#include "clang/Basic/StackExhaustionHandler.h"
31#include "clang/Basic/TargetInfo.h"
32#include "clang/Basic/XRayLists.h"
33#include "clang/Lex/PreprocessorOptions.h"
34#include "llvm/ADT/DenseMap.h"
35#include "llvm/ADT/MapVector.h"
36#include "llvm/ADT/SetVector.h"
37#include "llvm/ADT/SmallPtrSet.h"
38#include "llvm/ADT/StringMap.h"
39#include "llvm/IR/Module.h"
40#include "llvm/IR/ValueHandle.h"
41#include "llvm/Support/Allocator.h"
42#include "llvm/Transforms/Utils/SanitizerStats.h"
43#include <optional>
44
45namespace llvm {
46class Module;
47class Constant;
48class ConstantInt;
49class Function;
50class GlobalValue;
51class DataLayout;
52class FunctionType;
53class LLVMContext;
54class IndexedInstrProfReader;
55
56namespace vfs {
57class FileSystem;
58}
59
60namespace abi {
61class ArgInfo;
62class IRTypeMapper;
63class TargetInfo;
64class TypeBuilder;
65} // namespace abi
66}
67
68namespace clang {
69class ASTContext;
70class AtomicType;
71class FunctionDecl;
72class IdentifierInfo;
73class ObjCImplementationDecl;
74class ObjCEncodeExpr;
75class BlockExpr;
76class CharUnits;
77class Decl;
78class Expr;
79class Stmt;
80class StringLiteral;
81class NamedDecl;
82class PointerAuthSchema;
83class ValueDecl;
84class VarDecl;
85class LangOptions;
86class CodeGenOptions;
87class HeaderSearchOptions;
88class DiagnosticsEngine;
89class AnnotateAttr;
90class CXXDestructorDecl;
91class Module;
92class CoverageSourceInfo;
93class InitSegAttr;
94
95namespace CodeGen {
96
97class CodeGenFunction;
98class CodeGenTBAA;
99class CGCXXABI;
100class CGDebugInfo;
101class CGObjCRuntime;
102class CGOpenCLRuntime;
103class CGOpenMPRuntime;
104class CGCUDARuntime;
105class CGHLSLRuntime;
106class CGFunctionInfo;
107class CoverageMappingModuleGen;
108class QualTypeMapper;
109class TargetCodeGenInfo;
110
111enum ForDefinition_t : bool {
112 NotForDefinition = false,
113 ForDefinition = true
114};
115
116/// The Counter with an optional additional Counter for
117/// branches. `Skipped` counter can be calculated with `Executed` and
118/// a common Counter (like `Parent`) as `(Parent-Executed)`.
119///
120/// In SingleByte mode, Counters are binary. Subtraction is not
121/// applicable (but addition is capable). In this case, both
122/// `Executed` and `Skipped` counters are required. `Skipped` is
123/// `None` by default. It is allocated in the coverage mapping.
124///
125/// There might be cases that `Parent` could be induced with
126/// `(Executed+Skipped)`. This is not always applicable.
127class CounterPair {
128public:
129 /// Optional value.
130 class ValueOpt {
131 private:
132 static constexpr uint32_t None = (1u << 31); /// None is allocated.
133 static constexpr uint32_t Mask = None - 1;
134
135 uint32_t Val;
136
137 public:
138 ValueOpt() : Val(None) {}
139
140 ValueOpt(unsigned InitVal) {
141 assert(!(InitVal & ~Mask));
142 Val = InitVal;
143 }
144
145 bool hasValue() const { return !(Val & None); }
146
147 operator uint32_t() const { return Val; }
148 };
149
150 ValueOpt Executed;
151 ValueOpt Skipped; /// May be None.
152
153 /// Initialized with Skipped=None.
154 CounterPair(unsigned Val) : Executed(Val) {}
155
156 // FIXME: Should work with {None, None}
157 CounterPair() : Executed(0) {}
158};
159
160struct OrderGlobalInitsOrStermFinalizers {
161 unsigned int priority;
162 unsigned int lex_order;
163 OrderGlobalInitsOrStermFinalizers(unsigned int p, unsigned int l)
164 : priority(p), lex_order(l) {}
165
166 bool operator==(const OrderGlobalInitsOrStermFinalizers &RHS) const {
167 return priority == RHS.priority && lex_order == RHS.lex_order;
168 }
169
170 bool operator<(const OrderGlobalInitsOrStermFinalizers &RHS) const {
171 return std::tie(args: priority, args: lex_order) <
172 std::tie(args: RHS.priority, args: RHS.lex_order);
173 }
174};
175
176struct ObjCEntrypoints {
177 ObjCEntrypoints() { memset(s: this, c: 0, n: sizeof(*this)); }
178
179 /// void objc_alloc(id);
180 llvm::FunctionCallee objc_alloc;
181
182 /// void objc_allocWithZone(id);
183 llvm::FunctionCallee objc_allocWithZone;
184
185 /// void objc_alloc_init(id);
186 llvm::FunctionCallee objc_alloc_init;
187
188 /// void objc_autoreleasePoolPop(void*);
189 llvm::FunctionCallee objc_autoreleasePoolPop;
190
191 /// void objc_autoreleasePoolPop(void*);
192 /// Note this method is used when we are using exception handling
193 llvm::FunctionCallee objc_autoreleasePoolPopInvoke;
194
195 /// void *objc_autoreleasePoolPush(void);
196 llvm::Function *objc_autoreleasePoolPush;
197
198 /// id objc_autorelease(id);
199 llvm::Function *objc_autorelease;
200
201 /// id objc_autorelease(id);
202 /// Note this is the runtime method not the intrinsic.
203 llvm::FunctionCallee objc_autoreleaseRuntimeFunction;
204
205 /// id objc_autoreleaseReturnValue(id);
206 llvm::Function *objc_autoreleaseReturnValue;
207
208 /// void objc_copyWeak(id *dest, id *src);
209 llvm::Function *objc_copyWeak;
210
211 /// void objc_destroyWeak(id*);
212 llvm::Function *objc_destroyWeak;
213
214 /// id objc_initWeak(id*, id);
215 llvm::Function *objc_initWeak;
216
217 /// id objc_loadWeak(id*);
218 llvm::Function *objc_loadWeak;
219
220 /// id objc_loadWeakRetained(id*);
221 llvm::Function *objc_loadWeakRetained;
222
223 /// void objc_moveWeak(id *dest, id *src);
224 llvm::Function *objc_moveWeak;
225
226 /// id objc_retain(id);
227 llvm::Function *objc_retain;
228
229 /// id objc_retain(id);
230 /// Note this is the runtime method not the intrinsic.
231 llvm::FunctionCallee objc_retainRuntimeFunction;
232
233 /// id objc_retainAutorelease(id);
234 llvm::Function *objc_retainAutorelease;
235
236 /// id objc_retainAutoreleaseReturnValue(id);
237 llvm::Function *objc_retainAutoreleaseReturnValue;
238
239 /// id objc_retainAutoreleasedReturnValue(id);
240 llvm::Function *objc_retainAutoreleasedReturnValue;
241
242 /// id objc_retainBlock(id);
243 llvm::Function *objc_retainBlock;
244
245 /// void objc_release(id);
246 llvm::Function *objc_release;
247
248 /// void objc_release(id);
249 /// Note this is the runtime method not the intrinsic.
250 llvm::FunctionCallee objc_releaseRuntimeFunction;
251
252 /// void objc_storeStrong(id*, id);
253 llvm::Function *objc_storeStrong;
254
255 /// id objc_storeWeak(id*, id);
256 llvm::Function *objc_storeWeak;
257
258 /// id objc_unsafeClaimAutoreleasedReturnValue(id);
259 llvm::Function *objc_unsafeClaimAutoreleasedReturnValue;
260
261 /// A void(void) inline asm to use to mark that the return value of
262 /// a call will be immediately retain.
263 llvm::InlineAsm *retainAutoreleasedReturnValueMarker;
264
265 /// void clang.arc.use(...);
266 llvm::Function *clang_arc_use;
267
268 /// void clang.arc.noop.use(...);
269 llvm::Function *clang_arc_noop_use;
270};
271
272/// This class records statistics on instrumentation based profiling.
273class InstrProfStats {
274 uint32_t VisitedInMainFile = 0;
275 uint32_t MissingInMainFile = 0;
276 uint32_t Visited = 0;
277 uint32_t Missing = 0;
278 uint32_t Mismatched = 0;
279
280public:
281 InstrProfStats() = default;
282 /// Record that we've visited a function and whether or not that function was
283 /// in the main source file.
284 void addVisited(bool MainFile) {
285 if (MainFile)
286 ++VisitedInMainFile;
287 ++Visited;
288 }
289 /// Record that a function we've visited has no profile data.
290 void addMissing(bool MainFile) {
291 if (MainFile)
292 ++MissingInMainFile;
293 ++Missing;
294 }
295 /// Record that a function we've visited has mismatched profile data.
296 void addMismatched(bool MainFile) { ++Mismatched; }
297 /// Whether or not the stats we've gathered indicate any potential problems.
298 bool hasDiagnostics() { return Missing || Mismatched; }
299 /// Report potential problems we've found to \c Diags.
300 void reportDiagnostics(DiagnosticsEngine &Diags, StringRef MainFile);
301};
302
303/// A pair of helper functions for a __block variable.
304class BlockByrefHelpers : public llvm::FoldingSetNode {
305 // MSVC requires this type to be complete in order to process this
306 // header.
307public:
308 llvm::Constant *CopyHelper;
309 llvm::Constant *DisposeHelper;
310
311 /// The alignment of the field. This is important because
312 /// different offsets to the field within the byref struct need to
313 /// have different helper functions.
314 CharUnits Alignment;
315
316 BlockByrefHelpers(CharUnits alignment)
317 : CopyHelper(nullptr), DisposeHelper(nullptr), Alignment(alignment) {}
318 BlockByrefHelpers(const BlockByrefHelpers &) = default;
319 virtual ~BlockByrefHelpers();
320
321 void Profile(llvm::FoldingSetNodeID &id) const {
322 id.AddInteger(I: Alignment.getQuantity());
323 profileImpl(id);
324 }
325 virtual void profileImpl(llvm::FoldingSetNodeID &id) const = 0;
326
327 virtual bool needsCopy() const { return true; }
328 virtual void emitCopy(CodeGenFunction &CGF, Address dest, Address src) = 0;
329
330 virtual bool needsDispose() const { return true; }
331 virtual void emitDispose(CodeGenFunction &CGF, Address field) = 0;
332};
333
334/// This class organizes the cross-function state that is used while generating
335/// LLVM code.
336class CodeGenModule : public CodeGenTypeCache {
337 CodeGenModule(const CodeGenModule &) = delete;
338 void operator=(const CodeGenModule &) = delete;
339
340public:
341 struct Structor {
342 Structor()
343 : Priority(0), LexOrder(~0u), Initializer(nullptr),
344 AssociatedData(nullptr) {}
345 Structor(int Priority, unsigned LexOrder, llvm::Constant *Initializer,
346 llvm::Constant *AssociatedData)
347 : Priority(Priority), LexOrder(LexOrder), Initializer(Initializer),
348 AssociatedData(AssociatedData) {}
349 int Priority;
350 unsigned LexOrder;
351 llvm::Constant *Initializer;
352 llvm::Constant *AssociatedData;
353 };
354
355 typedef std::vector<Structor> CtorList;
356
357private:
358 ASTContext &Context;
359 const LangOptions &LangOpts;
360 IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS; // Only used for debug info.
361 const HeaderSearchOptions &HeaderSearchOpts; // Only used for debug info.
362 const PreprocessorOptions &PreprocessorOpts; // Only used for debug info.
363 const CodeGenOptions &CodeGenOpts;
364 unsigned NumAutoVarInit = 0;
365 llvm::Module &TheModule;
366 DiagnosticsEngine &Diags;
367 const TargetInfo &Target;
368 std::unique_ptr<CGCXXABI> ABI;
369 llvm::LLVMContext &VMContext;
370 std::string ModuleNameHash;
371 bool CXX20ModuleInits = false;
372 std::unique_ptr<CodeGenTBAA> TBAA;
373
374 mutable std::unique_ptr<TargetCodeGenInfo> TheTargetCodeGenInfo;
375
376 /// Cached LLVMABI target lowering info, lazily constructed when the
377 /// experimental ABI lowering path is taken.
378 mutable std::unique_ptr<llvm::abi::TargetInfo> TheLLVMABITargetInfo;
379
380 /// Allocator and mappers used by the experimental LLVMABI-based lowering
381 /// path (gated on -fexperimental-abi-lowering). Constructed unconditionally
382 /// so the path can be entered without re-checking initialization, but the
383 /// caches stay empty when the flag is off.
384 llvm::BumpPtrAllocator AbiAlloc;
385 std::unique_ptr<QualTypeMapper> AbiMapper;
386 std::unique_ptr<llvm::abi::IRTypeMapper> AbiReverseMapper;
387
388 // This should not be moved earlier, since its initialization depends on some
389 // of the previous reference members being already initialized and also checks
390 // if TheTargetCodeGenInfo is NULL
391 std::unique_ptr<CodeGenTypes> Types;
392
393 /// Holds information about C++ vtables.
394 CodeGenVTables VTables;
395
396 std::unique_ptr<CGObjCRuntime> ObjCRuntime;
397 std::unique_ptr<CGOpenCLRuntime> OpenCLRuntime;
398 std::unique_ptr<CGOpenMPRuntime> OpenMPRuntime;
399 std::unique_ptr<CGCUDARuntime> CUDARuntime;
400 std::unique_ptr<CGHLSLRuntime> HLSLRuntime;
401 std::unique_ptr<CGDebugInfo> DebugInfo;
402 std::unique_ptr<ObjCEntrypoints> ObjCData;
403 llvm::MDNode *NoObjCARCExceptionsMetadata = nullptr;
404 std::unique_ptr<llvm::IndexedInstrProfReader> PGOReader;
405 InstrProfStats PGOStats;
406 std::unique_ptr<llvm::SanitizerStatReport> SanStats;
407 StackExhaustionHandler StackHandler;
408
409 // A set of references that have only been seen via a weakref so far. This is
410 // used to remove the weak of the reference if we ever see a direct reference
411 // or a definition.
412 llvm::SmallPtrSet<llvm::GlobalValue*, 10> WeakRefReferences;
413
414 /// This contains all the decls which have definitions but/ which are deferred
415 /// for emission and therefore should only be output if they are actually
416 /// used. If a decl is in this, then it is known to have not been referenced
417 /// yet.
418 llvm::DenseMap<StringRef, GlobalDecl> DeferredDecls;
419
420 llvm::StringSet<llvm::BumpPtrAllocator> DeferredResolversToEmit;
421
422 /// This is a list of deferred decls which we have seen that *are* actually
423 /// referenced. These get code generated when the module is done.
424 std::vector<GlobalDecl> DeferredDeclsToEmit;
425 void addDeferredDeclToEmit(GlobalDecl GD) {
426 DeferredDeclsToEmit.emplace_back(args&: GD);
427 addEmittedDeferredDecl(GD);
428 }
429
430 /// Decls that were DeferredDecls and have now been emitted.
431 llvm::DenseMap<llvm::StringRef, GlobalDecl> EmittedDeferredDecls;
432
433 void addEmittedDeferredDecl(GlobalDecl GD) {
434 // Reemission is only needed in incremental mode.
435 if (!Context.getLangOpts().IncrementalExtensions)
436 return;
437
438 // Assume a linkage by default that does not need reemission.
439 auto L = llvm::GlobalValue::ExternalLinkage;
440 if (llvm::isa<FunctionDecl>(Val: GD.getDecl()))
441 L = getFunctionLinkage(GD);
442 else if (auto *VD = llvm::dyn_cast<VarDecl>(Val: GD.getDecl()))
443 L = getLLVMLinkageVarDefinition(VD);
444
445 if (llvm::GlobalValue::isInternalLinkage(Linkage: L) ||
446 llvm::GlobalValue::isLinkOnceLinkage(Linkage: L) ||
447 llvm::GlobalValue::isWeakLinkage(Linkage: L)) {
448 EmittedDeferredDecls[getMangledName(GD)] = GD;
449 }
450 }
451
452 /// List of alias we have emitted. Used to make sure that what they point to
453 /// is defined once we get to the end of the of the translation unit.
454 std::vector<GlobalDecl> Aliases;
455
456 /// List of multiversion functions to be emitted. This list is processed in
457 /// conjunction with other deferred symbols and is used to ensure that
458 /// multiversion function resolvers and ifuncs are defined and emitted.
459 std::vector<GlobalDecl> MultiVersionFuncs;
460
461 llvm::MapVector<StringRef, llvm::TrackingVH<llvm::Constant>> Replacements;
462
463 /// List of global values to be replaced with something else. Used when we
464 /// want to replace a GlobalValue but can't identify it by its mangled name
465 /// anymore (because the name is already taken).
466 llvm::SmallVector<std::pair<llvm::GlobalValue *, llvm::Constant *>, 8>
467 GlobalValReplacements;
468
469 /// Variables for which we've emitted globals containing their constant
470 /// values along with the corresponding globals, for opportunistic reuse.
471 llvm::DenseMap<const VarDecl*, llvm::GlobalVariable*> InitializerConstants;
472
473 /// Set of global decls for which we already diagnosed mangled name conflict.
474 /// Required to not issue a warning (on a mangling conflict) multiple times
475 /// for the same decl.
476 llvm::DenseSet<GlobalDecl> DiagnosedConflictingDefinitions;
477
478 /// A queue of (optional) vtables to consider emitting.
479 std::vector<const CXXRecordDecl*> DeferredVTables;
480
481 /// In incremental compilation, the set of vtable classes whose vtable
482 /// definitions were emitted into a previous PTU's module. Carried forward
483 /// by moveLazyEmissionStates() so later PTUs skip re-defining them.
484 llvm::SmallPtrSet<const CXXRecordDecl *, 8> EmittedVTables;
485
486 /// A queue of (optional) vtables that may be emitted opportunistically.
487 std::vector<const CXXRecordDecl *> OpportunisticVTables;
488
489 /// List of global values which are required to be present in the object file;
490 /// bitcast to i8*. This is used for forcing visibility of symbols which may
491 /// otherwise be optimized out.
492 std::vector<llvm::WeakTrackingVH> LLVMUsed;
493 std::vector<llvm::WeakTrackingVH> LLVMCompilerUsed;
494
495 /// Store the list of global constructors and their respective priorities to
496 /// be emitted when the translation unit is complete.
497 CtorList GlobalCtors;
498
499 /// Store the list of global destructors and their respective priorities to be
500 /// emitted when the translation unit is complete.
501 CtorList GlobalDtors;
502
503 /// An ordered map of canonical GlobalDecls to their mangled names.
504 llvm::MapVector<GlobalDecl, StringRef> MangledDeclNames;
505 llvm::StringMap<GlobalDecl, llvm::BumpPtrAllocator> Manglings;
506
507 /// Global annotations.
508 std::vector<llvm::Constant*> Annotations;
509
510 // Store deferred function annotations so they can be emitted at the end with
511 // most up to date ValueDecl that will have all the inherited annotations.
512 llvm::MapVector<StringRef, const ValueDecl *> DeferredAnnotations;
513
514 /// Map used to get unique annotation strings.
515 llvm::StringMap<llvm::Constant*> AnnotationStrings;
516
517 /// Used for uniquing of annotation arguments.
518 llvm::DenseMap<unsigned, llvm::Constant *> AnnotationArgs;
519
520 llvm::StringMap<llvm::GlobalVariable *> CFConstantStringMap;
521
522 llvm::DenseMap<llvm::Constant *, llvm::GlobalVariable *> ConstantStringMap;
523 llvm::DenseMap<const UnnamedGlobalConstantDecl *, llvm::GlobalVariable *>
524 UnnamedGlobalConstantDeclMap;
525 llvm::DenseMap<const Decl*, llvm::Constant *> StaticLocalDeclMap;
526 llvm::DenseMap<const Decl*, llvm::GlobalVariable*> StaticLocalDeclGuardMap;
527 llvm::DenseMap<const Expr*, llvm::Constant *> MaterializedGlobalTemporaryMap;
528
529 llvm::DenseMap<QualType, llvm::Constant *> AtomicSetterHelperFnMap;
530 llvm::DenseMap<QualType, llvm::Constant *> AtomicGetterHelperFnMap;
531
532 /// Map used to get unique type descriptor constants for sanitizers.
533 llvm::DenseMap<QualType, llvm::Constant *> TypeDescriptorMap;
534
535 /// Map used to track internal linkage functions declared within
536 /// extern "C" regions.
537 typedef llvm::MapVector<IdentifierInfo *,
538 llvm::GlobalValue *> StaticExternCMap;
539 StaticExternCMap StaticExternCValues;
540
541 /// thread_local variables defined or used in this TU.
542 std::vector<const VarDecl *> CXXThreadLocals;
543
544 /// thread_local variables with initializers that need to run
545 /// before any thread_local variable in this TU is odr-used.
546 std::vector<llvm::Function *> CXXThreadLocalInits;
547 std::vector<const VarDecl *> CXXThreadLocalInitVars;
548
549 /// Global variables with initializers that need to run before main.
550 std::vector<llvm::Function *> CXXGlobalInits;
551
552 /// When a C++ decl with an initializer is deferred, null is
553 /// appended to CXXGlobalInits, and the index of that null is placed
554 /// here so that the initializer will be performed in the correct
555 /// order. Once the decl is emitted, the index is replaced with ~0U to ensure
556 /// that we don't re-emit the initializer.
557 llvm::DenseMap<const Decl*, unsigned> DelayedCXXInitPosition;
558
559 /// To remember which types did require a vector deleting destructor body.
560 /// This set basically contains classes that have virtual destructor and new[]
561 /// was emitted for the class.
562 llvm::SmallPtrSet<const CXXRecordDecl *, 16> RequireVectorDeletingDtor;
563
564 typedef std::pair<OrderGlobalInitsOrStermFinalizers, llvm::Function *>
565 GlobalInitData;
566
567 // When a tail call is performed on an "undefined" symbol, on PPC without pc
568 // relative feature, the tail call is not allowed. In "EmitCall" for such
569 // tail calls, the "undefined" symbols may be forward declarations, their
570 // definitions are provided in the module after the callsites. For such tail
571 // calls, diagnose message should not be emitted.
572 llvm::SmallSetVector<std::pair<const FunctionDecl *, SourceLocation>, 4>
573 MustTailCallUndefinedGlobals;
574
575 struct GlobalInitPriorityCmp {
576 bool operator()(const GlobalInitData &LHS,
577 const GlobalInitData &RHS) const {
578 return LHS.first.priority < RHS.first.priority;
579 }
580 };
581
582 /// Global variables with initializers whose order of initialization is set by
583 /// init_priority attribute.
584 SmallVector<GlobalInitData, 8> PrioritizedCXXGlobalInits;
585
586 /// Global destructor functions and arguments that need to run on termination.
587 /// When UseSinitAndSterm is set, it instead contains sterm finalizer
588 /// functions, which also run on unloading a shared library.
589 typedef std::tuple<llvm::FunctionType *, llvm::WeakTrackingVH,
590 llvm::Constant *>
591 CXXGlobalDtorsOrStermFinalizer_t;
592 SmallVector<CXXGlobalDtorsOrStermFinalizer_t, 8>
593 CXXGlobalDtorsOrStermFinalizers;
594
595 typedef std::pair<OrderGlobalInitsOrStermFinalizers, llvm::Function *>
596 StermFinalizerData;
597
598 struct StermFinalizerPriorityCmp {
599 bool operator()(const StermFinalizerData &LHS,
600 const StermFinalizerData &RHS) const {
601 return LHS.first.priority < RHS.first.priority;
602 }
603 };
604
605 /// Global variables with sterm finalizers whose order of initialization is
606 /// set by init_priority attribute.
607 SmallVector<StermFinalizerData, 8> PrioritizedCXXStermFinalizers;
608
609 /// The complete set of modules that has been imported.
610 llvm::SetVector<clang::Module *> ImportedModules;
611
612 /// The set of modules for which the module initializers
613 /// have been emitted.
614 llvm::SmallPtrSet<clang::Module *, 16> EmittedModuleInitializers;
615
616 /// A vector of metadata strings for linker options.
617 SmallVector<llvm::MDNode *, 16> LinkerOptionsMetadata;
618
619 /// A vector of metadata strings for dependent libraries for ELF.
620 SmallVector<llvm::MDNode *, 16> ELFDependentLibraries;
621
622 /// Global variable for copyright pragma comment (if present).
623 llvm::GlobalVariable *LoadTimeCommentGlobal = nullptr;
624
625 /// @name Cache for Objective-C runtime types
626 /// @{
627
628 /// Cached reference to the class for constant strings. This value has type
629 /// int * but is actually an Obj-C class pointer.
630 llvm::WeakTrackingVH CFConstantStringClassRef;
631
632 /// The type used to describe the state of a fast enumeration in
633 /// Objective-C's for..in loop.
634 QualType ObjCFastEnumerationStateType;
635
636 /// @}
637
638 /// Lazily create the Objective-C runtime
639 void createObjCRuntime();
640
641 void createOpenCLRuntime();
642 void createOpenMPRuntime();
643 void createCUDARuntime();
644 void createHLSLRuntime();
645
646 bool shouldEmitFunction(GlobalDecl GD);
647 // Whether a global variable should be emitted by CUDA/HIP host/device
648 // related attributes.
649 bool shouldEmitCUDAGlobalVar(const VarDecl *VD) const;
650 bool shouldOpportunisticallyEmitVTables();
651 /// Map used to be sure we don't emit the same CompoundLiteral twice.
652 llvm::DenseMap<const CompoundLiteralExpr *, llvm::GlobalVariable *>
653 EmittedCompoundLiterals;
654
655 /// Map of the global blocks we've emitted, so that we don't have to re-emit
656 /// them if the constexpr evaluator gets aggressive.
657 llvm::DenseMap<const BlockExpr *, llvm::Constant *> EmittedGlobalBlocks;
658
659 /// @name Cache for Blocks Runtime Globals
660 /// @{
661
662 llvm::Constant *NSConcreteGlobalBlock = nullptr;
663 llvm::Constant *NSConcreteStackBlock = nullptr;
664
665 llvm::FunctionCallee BlockObjectAssign = nullptr;
666 llvm::FunctionCallee BlockObjectDispose = nullptr;
667
668 llvm::Type *BlockDescriptorType = nullptr;
669 llvm::Type *GenericBlockLiteralType = nullptr;
670
671 struct {
672 int GlobalUniqueCount;
673 } Block;
674
675 GlobalDecl initializedGlobalDecl;
676
677 /// @}
678
679 /// void @llvm.lifetime.start(i64 %size, i8* nocapture <ptr>)
680 llvm::Function *LifetimeStartFn = nullptr;
681
682 /// void @llvm.lifetime.end(i64 %size, i8* nocapture <ptr>)
683 llvm::Function *LifetimeEndFn = nullptr;
684
685 /// void @llvm.fake.use(...)
686 llvm::Function *FakeUseFn = nullptr;
687
688 std::unique_ptr<SanitizerMetadata> SanitizerMD;
689
690 llvm::MapVector<const Decl *, bool> DeferredEmptyCoverageMappingDecls;
691
692 std::unique_ptr<CoverageMappingModuleGen> CoverageMapping;
693
694 /// Mapping from canonical types to their metadata identifiers. We need to
695 /// maintain this mapping because identifiers may be formed from distinct
696 /// MDNodes.
697 typedef llvm::DenseMap<QualType, llvm::Metadata *> MetadataTypeMap;
698 MetadataTypeMap MetadataIdMap;
699 MetadataTypeMap VirtualMetadataIdMap;
700 MetadataTypeMap GeneralizedMetadataIdMap;
701
702 // Helps squashing blocks of TopLevelStmtDecl into a single llvm::Function
703 // when used with -fincremental-extensions.
704 std::pair<std::unique_ptr<CodeGenFunction>, const TopLevelStmtDecl *>
705 GlobalTopLevelStmtBlockInFlight;
706
707 llvm::DenseMap<GlobalDecl, uint16_t> PtrAuthDiscriminatorHashes;
708
709 llvm::DenseMap<const CXXRecordDecl *, std::optional<PointerAuthQualifier>>
710 VTablePtrAuthInfos;
711 std::optional<PointerAuthQualifier>
712 computeVTPointerAuthentication(const CXXRecordDecl *ThisClass);
713
714 AtomicOptions AtomicOpts;
715
716 // A set of functions which should be hot-patched; see
717 // -fms-hotpatch-functions-file (and -list). This will nearly always be empty.
718 // The list is sorted for binary-searching.
719 std::vector<std::string> MSHotPatchFunctions;
720
721public:
722 CodeGenModule(ASTContext &C, IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
723 const HeaderSearchOptions &headersearchopts,
724 const PreprocessorOptions &ppopts,
725 const CodeGenOptions &CodeGenOpts, llvm::Module &M,
726 DiagnosticsEngine &Diags,
727 CoverageSourceInfo *CoverageInfo = nullptr);
728
729 ~CodeGenModule();
730
731 void clear();
732
733 /// Finalize LLVM code generation.
734 void Release();
735
736 /// Get the current Atomic options.
737 AtomicOptions getAtomicOpts() { return AtomicOpts; }
738
739 /// Set the current Atomic options.
740 void setAtomicOpts(AtomicOptions AO) { AtomicOpts = AO; }
741
742 /// Return true if we should emit location information for expressions.
743 bool getExpressionLocationsEnabled() const;
744
745 /// Return a reference to the configured Objective-C runtime.
746 CGObjCRuntime &getObjCRuntime() {
747 if (!ObjCRuntime) createObjCRuntime();
748 return *ObjCRuntime;
749 }
750
751 /// Return true iff an Objective-C runtime has been configured.
752 bool hasObjCRuntime() { return !!ObjCRuntime; }
753
754 /// Check if the precondition thunk optimization is enabled.
755 /// This checks runtime support and codegen options, but does NOT check
756 /// whether a specific method is eligible for thunks or inline preconditions.
757 ///
758 /// TODO: Add support for GNUStep as well, currently only supports NeXT
759 /// family.
760 bool isObjCDirectPreconditionThunkEnabled() const {
761 return getLangOpts().ObjCRuntime.allowsDirectDispatch() &&
762 getLangOpts().ObjCRuntime.isNeXTFamily() &&
763 getCodeGenOpts().ObjCDirectPreconditionThunk;
764 }
765
766 /// Check if a direct method should use precondition thunks at call sites.
767 /// Returns false if OMD is null, not a direct method, or variadic.
768 ///
769 /// Variadic methods use inline preconditions instead of thunks to avoid
770 /// musttail complexity across different architectures.
771 bool shouldHavePreconditionThunk(const ObjCMethodDecl *OMD) const {
772 return OMD && OMD->isDirectMethod() && !OMD->isVariadic() &&
773 isObjCDirectPreconditionThunkEnabled();
774 }
775
776 /// Check if a direct method should have inline precondition checks at call
777 /// sites.
778 /// Returns false if OMD is null, not a direct method, or not variadic.
779 ///
780 /// Variadic direct methods use inline preconditions rather than thunks
781 /// to avoid musttail complexity across different architectures.
782 bool shouldHavePreconditionInline(const ObjCMethodDecl *OMD) const {
783 return OMD && OMD->isDirectMethod() && OMD->isVariadic() &&
784 isObjCDirectPreconditionThunkEnabled();
785 }
786
787 const std::string &getModuleNameHash() const { return ModuleNameHash; }
788
789 /// Return a reference to the configured OpenCL runtime.
790 CGOpenCLRuntime &getOpenCLRuntime() {
791 assert(OpenCLRuntime != nullptr);
792 return *OpenCLRuntime;
793 }
794
795 /// Return a reference to the configured OpenMP runtime.
796 CGOpenMPRuntime &getOpenMPRuntime() {
797 assert(OpenMPRuntime != nullptr);
798 return *OpenMPRuntime;
799 }
800
801 /// Return a reference to the configured CUDA runtime.
802 CGCUDARuntime &getCUDARuntime() {
803 assert(CUDARuntime != nullptr);
804 return *CUDARuntime;
805 }
806
807 /// Return a reference to the configured HLSL runtime.
808 CGHLSLRuntime &getHLSLRuntime() {
809 assert(HLSLRuntime != nullptr);
810 return *HLSLRuntime;
811 }
812
813 ObjCEntrypoints &getObjCEntrypoints() const {
814 assert(ObjCData != nullptr);
815 return *ObjCData;
816 }
817
818 // Version checking functions, used to implement ObjC's @available:
819 // i32 @__isOSVersionAtLeast(i32, i32, i32)
820 llvm::FunctionCallee IsOSVersionAtLeastFn = nullptr;
821 // i32 @__isPlatformVersionAtLeast(i32, i32, i32, i32)
822 llvm::FunctionCallee IsPlatformVersionAtLeastFn = nullptr;
823
824 InstrProfStats &getPGOStats() { return PGOStats; }
825 llvm::IndexedInstrProfReader *getPGOReader() const { return PGOReader.get(); }
826
827 CoverageMappingModuleGen *getCoverageMapping() const {
828 return CoverageMapping.get();
829 }
830
831 llvm::Constant *getStaticLocalDeclAddress(const VarDecl *D) {
832 return StaticLocalDeclMap[D];
833 }
834 void setStaticLocalDeclAddress(const VarDecl *D,
835 llvm::Constant *C) {
836 StaticLocalDeclMap[D] = C;
837 }
838
839 llvm::Constant *
840 getOrCreateStaticVarDecl(const VarDecl &D,
841 llvm::GlobalValue::LinkageTypes Linkage);
842
843 llvm::GlobalVariable *getStaticLocalDeclGuardAddress(const VarDecl *D) {
844 return StaticLocalDeclGuardMap[D];
845 }
846 void setStaticLocalDeclGuardAddress(const VarDecl *D,
847 llvm::GlobalVariable *C) {
848 StaticLocalDeclGuardMap[D] = C;
849 }
850
851 Address createUnnamedGlobalFrom(const VarDecl &D, llvm::Constant *Constant,
852 CharUnits Align);
853
854 bool lookupRepresentativeDecl(StringRef MangledName,
855 GlobalDecl &Result) const;
856
857 llvm::Constant *getAtomicSetterHelperFnMap(QualType Ty) {
858 return AtomicSetterHelperFnMap[Ty];
859 }
860 void setAtomicSetterHelperFnMap(QualType Ty,
861 llvm::Constant *Fn) {
862 AtomicSetterHelperFnMap[Ty] = Fn;
863 }
864
865 llvm::Constant *getAtomicGetterHelperFnMap(QualType Ty) {
866 return AtomicGetterHelperFnMap[Ty];
867 }
868 void setAtomicGetterHelperFnMap(QualType Ty,
869 llvm::Constant *Fn) {
870 AtomicGetterHelperFnMap[Ty] = Fn;
871 }
872
873 llvm::Constant *getTypeDescriptorFromMap(QualType Ty) {
874 return TypeDescriptorMap[Ty];
875 }
876 void setTypeDescriptorInMap(QualType Ty, llvm::Constant *C) {
877 TypeDescriptorMap[Ty] = C;
878 }
879
880 CGDebugInfo *getModuleDebugInfo() { return DebugInfo.get(); }
881
882 llvm::MDNode *getNoObjCARCExceptionsMetadata() {
883 if (!NoObjCARCExceptionsMetadata)
884 NoObjCARCExceptionsMetadata = llvm::MDNode::get(Context&: getLLVMContext(), MDs: {});
885 return NoObjCARCExceptionsMetadata;
886 }
887
888 ASTContext &getContext() const { return Context; }
889 const LangOptions &getLangOpts() const { return LangOpts; }
890 const IntrusiveRefCntPtr<llvm::vfs::FileSystem> &getFileSystem() const {
891 return FS;
892 }
893 const HeaderSearchOptions &getHeaderSearchOpts()
894 const { return HeaderSearchOpts; }
895 const PreprocessorOptions &getPreprocessorOpts()
896 const { return PreprocessorOpts; }
897 const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
898 llvm::Module &getModule() const { return TheModule; }
899 DiagnosticsEngine &getDiags() const { return Diags; }
900 const llvm::DataLayout &getDataLayout() const {
901 return TheModule.getDataLayout();
902 }
903 const TargetInfo &getTarget() const { return Target; }
904 const llvm::Triple &getTriple() const { return Target.getTriple(); }
905 bool supportsCOMDAT() const;
906 void maybeSetTrivialComdat(const Decl &D, llvm::GlobalObject &GO);
907
908 const ABIInfo &getABIInfo();
909
910 /// Lazily build and return the LLVMABI library's TargetInfo for the current
911 /// target. Used by the experimental ABI lowering path
912 /// (-fexperimental-abi-lowering).
913 const llvm::abi::TargetInfo &getLLVMABITargetInfo(llvm::abi::TypeBuilder &TB);
914
915 /// True when -fexperimental-abi-lowering is in effect AND the active target
916 /// has an LLVMABI implementation we can route to.
917 bool shouldUseLLVMABILowering() const;
918
919 /// Drive the experimental LLVMABI-based lowering path: map argument and
920 /// return types into the LLVMABI library, ask its target lowering to fill
921 /// in classification, and write the results back into FI.
922 void computeABIInfoUsingLib(CGFunctionInfo &FI);
923
924 CGCXXABI &getCXXABI() const { return *ABI; }
925 llvm::LLVMContext &getLLVMContext() { return VMContext; }
926
927 bool shouldUseTBAA() const { return TBAA != nullptr; }
928
929 const TargetCodeGenInfo &getTargetCodeGenInfo();
930
931 CodeGenTypes &getTypes() { return *Types; }
932
933 CodeGenVTables &getVTables() { return VTables; }
934
935 ItaniumVTableContext &getItaniumVTableContext() {
936 return VTables.getItaniumVTableContext();
937 }
938
939 const ItaniumVTableContext &getItaniumVTableContext() const {
940 return VTables.getItaniumVTableContext();
941 }
942
943 MicrosoftVTableContext &getMicrosoftVTableContext() {
944 return VTables.getMicrosoftVTableContext();
945 }
946
947 CtorList &getGlobalCtors() { return GlobalCtors; }
948 CtorList &getGlobalDtors() { return GlobalDtors; }
949
950 /// getTBAATypeInfo - Get metadata used to describe accesses to objects of
951 /// the given type.
952 llvm::MDNode *getTBAATypeInfo(QualType QTy);
953
954 /// getTBAAAccessInfo - Get TBAA information that describes an access to
955 /// an object of the given type.
956 TBAAAccessInfo getTBAAAccessInfo(QualType AccessType);
957
958 /// getTBAAVTablePtrAccessInfo - Get the TBAA information that describes an
959 /// access to a virtual table pointer.
960 TBAAAccessInfo getTBAAVTablePtrAccessInfo(llvm::Type *VTablePtrType);
961
962 llvm::MDNode *getTBAAStructInfo(QualType QTy);
963
964 /// getTBAABaseTypeInfo - Get metadata that describes the given base access
965 /// type. Return null if the type is not suitable for use in TBAA access tags.
966 llvm::MDNode *getTBAABaseTypeInfo(QualType QTy);
967
968 /// getTBAAAccessTagInfo - Get TBAA tag for a given memory access.
969 llvm::MDNode *getTBAAAccessTagInfo(TBAAAccessInfo Info);
970
971 /// mergeTBAAInfoForCast - Get merged TBAA information for the purposes of
972 /// type casts.
973 TBAAAccessInfo mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo,
974 TBAAAccessInfo TargetInfo);
975
976 /// mergeTBAAInfoForConditionalOperator - Get merged TBAA information for the
977 /// purposes of conditional operator.
978 TBAAAccessInfo mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA,
979 TBAAAccessInfo InfoB);
980
981 /// mergeTBAAInfoForMemoryTransfer - Get merged TBAA information for the
982 /// purposes of memory transfer calls.
983 TBAAAccessInfo mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo,
984 TBAAAccessInfo SrcInfo);
985
986 /// getTBAAInfoForSubobject - Get TBAA information for an access with a given
987 /// base lvalue.
988 TBAAAccessInfo getTBAAInfoForSubobject(LValue Base, QualType AccessType) {
989 if (Base.getTBAAInfo().isMayAlias())
990 return TBAAAccessInfo::getMayAliasInfo();
991 return getTBAAAccessInfo(AccessType);
992 }
993
994 bool isPaddedAtomicType(QualType type);
995 bool isPaddedAtomicType(const AtomicType *type);
996
997 /// DecorateInstructionWithTBAA - Decorate the instruction with a TBAA tag.
998 void DecorateInstructionWithTBAA(llvm::Instruction *Inst,
999 TBAAAccessInfo TBAAInfo);
1000
1001 /// Adds !invariant.barrier !tag to instruction
1002 void DecorateInstructionWithInvariantGroup(llvm::Instruction *I,
1003 const CXXRecordDecl *RD);
1004
1005 /// Emit the given number of characters as a value of type size_t.
1006 llvm::ConstantInt *getSize(CharUnits numChars);
1007
1008 /// Set the visibility for the given LLVM GlobalValue.
1009 void setGlobalVisibility(llvm::GlobalValue *GV, const NamedDecl *D) const;
1010
1011 void setDSOLocal(llvm::GlobalValue *GV) const;
1012
1013 bool shouldMapVisibilityToDLLExport(const NamedDecl *D) const {
1014 return getLangOpts().hasDefaultVisibilityExportMapping() && D &&
1015 (D->getLinkageAndVisibility().getVisibility() ==
1016 DefaultVisibility) &&
1017 (getLangOpts().isAllDefaultVisibilityExportMapping() ||
1018 (getLangOpts().isExplicitDefaultVisibilityExportMapping() &&
1019 D->getLinkageAndVisibility().isVisibilityExplicit()));
1020 }
1021 void setDLLImportDLLExport(llvm::GlobalValue *GV, GlobalDecl D) const;
1022 void setDLLImportDLLExport(llvm::GlobalValue *GV, const NamedDecl *D) const;
1023 /// Set visibility, dllimport/dllexport and dso_local.
1024 /// This must be called after dllimport/dllexport is set.
1025 void setGVProperties(llvm::GlobalValue *GV, GlobalDecl GD) const;
1026 void setGVProperties(llvm::GlobalValue *GV, const NamedDecl *D) const;
1027
1028 void setGVPropertiesAux(llvm::GlobalValue *GV, const NamedDecl *D) const;
1029
1030 /// Set the TLS mode for the given LLVM GlobalValue for the thread-local
1031 /// variable declaration D.
1032 void setTLSMode(llvm::GlobalValue *GV, const VarDecl &D) const;
1033
1034 /// Get LLVM TLS mode from CodeGenOptions.
1035 llvm::GlobalVariable::ThreadLocalMode GetDefaultLLVMTLSModel() const;
1036
1037 static llvm::GlobalValue::VisibilityTypes GetLLVMVisibility(Visibility V) {
1038 switch (V) {
1039 case DefaultVisibility: return llvm::GlobalValue::DefaultVisibility;
1040 case HiddenVisibility: return llvm::GlobalValue::HiddenVisibility;
1041 case ProtectedVisibility: return llvm::GlobalValue::ProtectedVisibility;
1042 }
1043 llvm_unreachable("unknown visibility!");
1044 }
1045
1046 llvm::Constant *GetAddrOfGlobal(GlobalDecl GD,
1047 ForDefinition_t IsForDefinition
1048 = NotForDefinition);
1049
1050 /// Will return a global variable of the given type. If a variable with a
1051 /// different type already exists then a new variable with the right type
1052 /// will be created and all uses of the old variable will be replaced with a
1053 /// bitcast to the new variable.
1054 llvm::GlobalVariable *
1055 CreateOrReplaceCXXRuntimeVariable(StringRef Name, llvm::Type *Ty,
1056 llvm::GlobalValue::LinkageTypes Linkage,
1057 llvm::Align Alignment);
1058
1059 llvm::Function *CreateGlobalInitOrCleanUpFunction(
1060 llvm::FunctionType *ty, const Twine &name, const CGFunctionInfo &FI,
1061 SourceLocation Loc = SourceLocation(), bool TLS = false,
1062 llvm::GlobalVariable::LinkageTypes Linkage =
1063 llvm::GlobalVariable::InternalLinkage);
1064
1065 /// Return the AST address space of the underlying global variable for D, as
1066 /// determined by its declaration. Normally this is the same as the address
1067 /// space of D's type, but in CUDA, address spaces are associated with
1068 /// declarations, not types. If D is nullptr, return the default address
1069 /// space for global variable.
1070 ///
1071 /// For languages without explicit address spaces, if D has default address
1072 /// space, target-specific global or constant address space may be returned.
1073 LangAS GetGlobalVarAddressSpace(const VarDecl *D);
1074
1075 /// Return the AST address space of constant literal, which is used to emit
1076 /// the constant literal as global variable in LLVM IR.
1077 /// Note: This is not necessarily the address space of the constant literal
1078 /// in AST. For address space agnostic language, e.g. C++, constant literal
1079 /// in AST is always in default address space.
1080 LangAS GetGlobalConstantAddressSpace() const;
1081
1082 /// Return the llvm::Constant for the address of the given global variable.
1083 /// If Ty is non-null and if the global doesn't exist, then it will be created
1084 /// with the specified type instead of whatever the normal requested type
1085 /// would be. If IsForDefinition is true, it is guaranteed that an actual
1086 /// global with type Ty will be returned, not conversion of a variable with
1087 /// the same mangled name but some other type.
1088 llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D,
1089 llvm::Type *Ty = nullptr,
1090 ForDefinition_t IsForDefinition
1091 = NotForDefinition);
1092
1093 /// Return the address of the given function. If Ty is non-null, then this
1094 /// function will use the specified type if it has to create it.
1095 llvm::Constant *GetAddrOfFunction(GlobalDecl GD, llvm::Type *Ty = nullptr,
1096 bool ForVTable = false,
1097 bool DontDefer = false,
1098 ForDefinition_t IsForDefinition
1099 = NotForDefinition);
1100
1101 // Return the function body address of the given function.
1102 llvm::Constant *GetFunctionStart(const ValueDecl *Decl);
1103
1104 /// Return a function pointer for a reference to the given function.
1105 /// This correctly handles weak references, but does not apply a
1106 /// pointer signature.
1107 llvm::Constant *getRawFunctionPointer(GlobalDecl GD,
1108 llvm::Type *Ty = nullptr);
1109
1110 /// Return the ABI-correct function pointer value for a reference
1111 /// to the given function. This will apply a pointer signature if
1112 /// necessary, caching the result for the given function.
1113 llvm::Constant *getFunctionPointer(GlobalDecl GD, llvm::Type *Ty = nullptr);
1114
1115 /// Return the ABI-correct function pointer value for a reference
1116 /// to the given function. This will apply a pointer signature if
1117 /// necessary.
1118 llvm::Constant *getFunctionPointer(llvm::Constant *Pointer,
1119 QualType FunctionType);
1120
1121 llvm::Constant *getMemberFunctionPointer(const FunctionDecl *FD,
1122 llvm::Type *Ty = nullptr);
1123
1124 llvm::Constant *getMemberFunctionPointer(llvm::Constant *Pointer,
1125 QualType FT);
1126
1127 CGPointerAuthInfo getFunctionPointerAuthInfo(QualType T);
1128
1129 CGPointerAuthInfo getMemberFunctionPointerAuthInfo(QualType FT);
1130
1131 CGPointerAuthInfo getPointerAuthInfoForPointeeType(QualType type);
1132
1133 CGPointerAuthInfo getPointerAuthInfoForType(QualType type);
1134
1135 bool shouldSignPointer(const PointerAuthSchema &Schema);
1136 llvm::Constant *getConstantSignedPointer(llvm::Constant *Pointer,
1137 const PointerAuthSchema &Schema,
1138 llvm::Constant *StorageAddress,
1139 GlobalDecl SchemaDecl,
1140 QualType SchemaType);
1141
1142 llvm::Constant *
1143 getConstantSignedPointer(llvm::Constant *Pointer, unsigned Key,
1144 llvm::Constant *StorageAddress,
1145 llvm::ConstantInt *OtherDiscriminator);
1146
1147 llvm::ConstantInt *
1148 getPointerAuthOtherDiscriminator(const PointerAuthSchema &Schema,
1149 GlobalDecl SchemaDecl, QualType SchemaType);
1150
1151 uint16_t getPointerAuthDeclDiscriminator(GlobalDecl GD);
1152 std::optional<CGPointerAuthInfo>
1153 getVTablePointerAuthInfo(CodeGenFunction *Context,
1154 const CXXRecordDecl *Record,
1155 llvm::Value *StorageAddress);
1156
1157 std::optional<PointerAuthQualifier>
1158 getVTablePointerAuthentication(const CXXRecordDecl *thisClass);
1159
1160 CGPointerAuthInfo EmitPointerAuthInfo(const RecordDecl *RD);
1161
1162 // Return whether RTTI information should be emitted for this target.
1163 bool shouldEmitRTTI(bool ForEH = false) {
1164 return (ForEH || getLangOpts().RTTI) &&
1165 (!getLangOpts().isTargetDevice() || !getTriple().isGPU());
1166 }
1167
1168 /// Get the address of the RTTI descriptor for the given type.
1169 llvm::Constant *GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH = false);
1170
1171 /// Get the address of a GUID.
1172 ConstantAddress GetAddrOfMSGuidDecl(const MSGuidDecl *GD);
1173
1174 /// Get the address of a UnnamedGlobalConstant
1175 ConstantAddress
1176 GetAddrOfUnnamedGlobalConstantDecl(const UnnamedGlobalConstantDecl *GCD);
1177
1178 /// Get the address of a template parameter object.
1179 ConstantAddress
1180 GetAddrOfTemplateParamObject(const TemplateParamObjectDecl *TPO);
1181
1182 /// Get the address of the thunk for the given global decl.
1183 llvm::Constant *GetAddrOfThunk(StringRef Name, llvm::Type *FnTy,
1184 GlobalDecl GD);
1185
1186 /// Get a reference to the target of VD.
1187 ConstantAddress GetWeakRefReference(const ValueDecl *VD);
1188
1189 /// Returns the assumed alignment of an opaque pointer to the given class.
1190 CharUnits getClassPointerAlignment(const CXXRecordDecl *CD);
1191
1192 /// Returns the minimum object size for an object of the given class type
1193 /// (or a class derived from it).
1194 CharUnits getMinimumClassObjectSize(const CXXRecordDecl *CD);
1195
1196 /// Returns the minimum object size for an object of the given type.
1197 CharUnits getMinimumObjectSize(QualType Ty) {
1198 if (CXXRecordDecl *RD = Ty->getAsCXXRecordDecl())
1199 return getMinimumClassObjectSize(CD: RD);
1200 return getContext().getTypeSizeInChars(T: Ty);
1201 }
1202
1203 /// Returns the assumed alignment of a virtual base of a class.
1204 CharUnits getVBaseAlignment(CharUnits DerivedAlign,
1205 const CXXRecordDecl *Derived,
1206 const CXXRecordDecl *VBase);
1207
1208 /// Given a class pointer with an actual known alignment, and the
1209 /// expected alignment of an object at a dynamic offset w.r.t that
1210 /// pointer, return the alignment to assume at the offset.
1211 CharUnits getDynamicOffsetAlignment(CharUnits ActualAlign,
1212 const CXXRecordDecl *Class,
1213 CharUnits ExpectedTargetAlign);
1214
1215 CharUnits
1216 computeNonVirtualBaseClassOffset(const CXXRecordDecl *DerivedClass,
1217 CastExpr::path_const_iterator Start,
1218 CastExpr::path_const_iterator End);
1219
1220 /// Returns the offset from a derived class to a class. Returns null if the
1221 /// offset is 0.
1222 llvm::Constant *
1223 GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl,
1224 CastExpr::path_const_iterator PathBegin,
1225 CastExpr::path_const_iterator PathEnd);
1226
1227 llvm::FoldingSet<BlockByrefHelpers> ByrefHelpersCache;
1228
1229 /// Fetches the global unique block count.
1230 int getUniqueBlockCount() { return ++Block.GlobalUniqueCount; }
1231
1232 /// Fetches the type of a generic block descriptor.
1233 llvm::Type *getBlockDescriptorType();
1234
1235 /// The type of a generic block literal.
1236 llvm::Type *getGenericBlockLiteralType();
1237
1238 /// Gets the address of a block which requires no captures.
1239 llvm::Constant *GetAddrOfGlobalBlock(const BlockExpr *BE, StringRef Name);
1240
1241 /// Returns the address of a block which requires no caputres, or null if
1242 /// we've yet to emit the block for BE.
1243 llvm::Constant *getAddrOfGlobalBlockIfEmitted(const BlockExpr *BE) {
1244 return EmittedGlobalBlocks.lookup(Val: BE);
1245 }
1246
1247 /// Notes that BE's global block is available via Addr. Asserts that BE
1248 /// isn't already emitted.
1249 void setAddrOfGlobalBlock(const BlockExpr *BE, llvm::Constant *Addr);
1250
1251 /// Return a pointer to a constant CFString object for the given string.
1252 ConstantAddress GetAddrOfConstantCFString(const StringLiteral *Literal);
1253
1254 /// Return a constant array for the given string.
1255 llvm::Constant *GetConstantArrayFromStringLiteral(const StringLiteral *E);
1256
1257 /// Return a pointer to a constant array for the given string literal.
1258 ConstantAddress
1259 GetAddrOfConstantStringFromLiteral(const StringLiteral *S,
1260 StringRef Name = ".str");
1261
1262 /// Return a pointer to a constant array for the given ObjCEncodeExpr node.
1263 ConstantAddress
1264 GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *);
1265
1266 /// Returns a pointer to a character array containing the literal and a
1267 /// terminating '\0' character. The result has pointer to array type.
1268 ///
1269 /// \param GlobalName If provided, the name to use for the global (if one is
1270 /// created).
1271 ConstantAddress GetAddrOfConstantCString(const std::string &Str,
1272 StringRef GlobalName = ".str");
1273
1274 /// Returns a pointer to a constant global variable for the given file-scope
1275 /// compound literal expression.
1276 ConstantAddress GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr*E);
1277
1278 /// If it's been emitted already, returns the GlobalVariable corresponding to
1279 /// a compound literal. Otherwise, returns null.
1280 llvm::GlobalVariable *
1281 getAddrOfConstantCompoundLiteralIfEmitted(const CompoundLiteralExpr *E);
1282
1283 /// Notes that CLE's GlobalVariable is GV. Asserts that CLE isn't already
1284 /// emitted.
1285 void setAddrOfConstantCompoundLiteral(const CompoundLiteralExpr *CLE,
1286 llvm::GlobalVariable *GV);
1287
1288 /// Returns a pointer to a global variable representing a temporary
1289 /// with static or thread storage duration.
1290 ConstantAddress GetAddrOfGlobalTemporary(const MaterializeTemporaryExpr *E,
1291 const Expr *Inner);
1292
1293 /// Retrieve the record type that describes the state of an
1294 /// Objective-C fast enumeration loop (for..in).
1295 QualType getObjCFastEnumerationStateType();
1296
1297 // Produce code for this constructor/destructor. This method doesn't try
1298 // to apply any ABI rules about which other constructors/destructors
1299 // are needed or if they are alias to each other.
1300 llvm::Function *codegenCXXStructor(GlobalDecl GD);
1301
1302 /// Emit a trap stub body for functions in ASTContext::CUDADeviceInvalidFuncs.
1303 bool tryEmitCUDADeviceInvalidFunctionBody(GlobalDecl GD, llvm::Function *Fn);
1304
1305 /// Return the address of the constructor/destructor of the given type.
1306 llvm::Constant *
1307 getAddrOfCXXStructor(GlobalDecl GD, const CGFunctionInfo *FnInfo = nullptr,
1308 llvm::FunctionType *FnType = nullptr,
1309 bool DontDefer = false,
1310 ForDefinition_t IsForDefinition = NotForDefinition) {
1311 return cast<llvm::Constant>(Val: getAddrAndTypeOfCXXStructor(GD, FnInfo, FnType,
1312 DontDefer,
1313 IsForDefinition)
1314 .getCallee());
1315 }
1316
1317 llvm::FunctionCallee getAddrAndTypeOfCXXStructor(
1318 GlobalDecl GD, const CGFunctionInfo *FnInfo = nullptr,
1319 llvm::FunctionType *FnType = nullptr, bool DontDefer = false,
1320 ForDefinition_t IsForDefinition = NotForDefinition);
1321
1322 /// Given a builtin id for a function like "__builtin_fabsf", return a
1323 /// Function* for "fabsf".
1324 llvm::Constant *getBuiltinLibFunction(const FunctionDecl *FD,
1325 unsigned BuiltinID);
1326
1327 llvm::Function *getIntrinsic(unsigned IID, ArrayRef<llvm::Type *> Tys = {});
1328
1329 void AddCXXGlobalInit(llvm::Function *F) { CXXGlobalInits.push_back(x: F); }
1330
1331 /// Emit code for a single top level declaration.
1332 void EmitTopLevelDecl(Decl *D);
1333
1334 /// Stored a deferred empty coverage mapping for an unused
1335 /// and thus uninstrumented top level declaration.
1336 void AddDeferredUnusedCoverageMapping(Decl *D);
1337
1338 /// Remove the deferred empty coverage mapping as this
1339 /// declaration is actually instrumented.
1340 void ClearUnusedCoverageMapping(const Decl *D);
1341
1342 /// Emit all the deferred coverage mappings
1343 /// for the uninstrumented functions.
1344 void EmitDeferredUnusedCoverageMappings();
1345
1346 /// Emit an alias for "main" if it has no arguments (needed for wasm).
1347 void EmitMainVoidAlias();
1348
1349 /// Tell the consumer that this variable has been instantiated.
1350 void HandleCXXStaticMemberVarInstantiation(VarDecl *VD);
1351
1352 /// If the declaration has internal linkage but is inside an
1353 /// extern "C" linkage specification, prepare to emit an alias for it
1354 /// to the expected name.
1355 template<typename SomeDecl>
1356 void MaybeHandleStaticInExternC(const SomeDecl *D, llvm::GlobalValue *GV);
1357
1358 /// Add a global to a list to be added to the llvm.used metadata.
1359 void addUsedGlobal(llvm::GlobalValue *GV);
1360
1361 /// Add a global to a list to be added to the llvm.compiler.used metadata.
1362 void addCompilerUsedGlobal(llvm::GlobalValue *GV);
1363
1364 /// Add a global to a list to be added to the llvm.compiler.used metadata.
1365 void addUsedOrCompilerUsedGlobal(llvm::GlobalValue *GV);
1366
1367 /// Add a destructor and object to add to the C++ global destructor function.
1368 void AddCXXDtorEntry(llvm::FunctionCallee DtorFn, llvm::Constant *Object) {
1369 CXXGlobalDtorsOrStermFinalizers.emplace_back(Args: DtorFn.getFunctionType(),
1370 Args: DtorFn.getCallee(), Args&: Object);
1371 }
1372
1373 /// Add an sterm finalizer to the C++ global cleanup function.
1374 void AddCXXStermFinalizerEntry(llvm::FunctionCallee DtorFn) {
1375 CXXGlobalDtorsOrStermFinalizers.emplace_back(Args: DtorFn.getFunctionType(),
1376 Args: DtorFn.getCallee(), Args: nullptr);
1377 }
1378
1379 /// Add an sterm finalizer to its own llvm.global_dtors entry.
1380 void AddCXXStermFinalizerToGlobalDtor(llvm::Function *StermFinalizer,
1381 int Priority) {
1382 AddGlobalDtor(Dtor: StermFinalizer, Priority);
1383 }
1384
1385 void AddCXXPrioritizedStermFinalizerEntry(llvm::Function *StermFinalizer,
1386 int Priority) {
1387 OrderGlobalInitsOrStermFinalizers Key(Priority,
1388 PrioritizedCXXStermFinalizers.size());
1389 PrioritizedCXXStermFinalizers.push_back(
1390 Elt: std::make_pair(x&: Key, y&: StermFinalizer));
1391 }
1392
1393 /// Create or return a runtime function declaration with the specified type
1394 /// and name. If \p AssumeConvergent is true, the call will have the
1395 /// convergent attribute added.
1396 ///
1397 /// For new code, please use the overload that takes a QualType; it sets
1398 /// function attributes more accurately.
1399 llvm::FunctionCallee
1400 CreateRuntimeFunction(llvm::FunctionType *Ty, StringRef Name,
1401 llvm::AttributeList ExtraAttrs = llvm::AttributeList(),
1402 bool Local = false, bool AssumeConvergent = false);
1403
1404 /// Create or return a runtime function declaration with the specified type
1405 /// and name. If \p AssumeConvergent is true, the call will have the
1406 /// convergent attribute added.
1407 llvm::FunctionCallee
1408 CreateRuntimeFunction(QualType ReturnTy, ArrayRef<QualType> ArgTys,
1409 StringRef Name,
1410 llvm::AttributeList ExtraAttrs = llvm::AttributeList(),
1411 bool Local = false, bool AssumeConvergent = false);
1412
1413 /// Create a new runtime global variable with the specified type and name.
1414 llvm::Constant *CreateRuntimeVariable(llvm::Type *Ty,
1415 StringRef Name);
1416
1417 ///@name Custom Blocks Runtime Interfaces
1418 ///@{
1419
1420 llvm::Constant *getNSConcreteGlobalBlock();
1421 llvm::Constant *getNSConcreteStackBlock();
1422 llvm::FunctionCallee getBlockObjectAssign();
1423 llvm::FunctionCallee getBlockObjectDispose();
1424
1425 ///@}
1426
1427 llvm::Function *getLLVMLifetimeStartFn();
1428 llvm::Function *getLLVMLifetimeEndFn();
1429 llvm::Function *getLLVMFakeUseFn();
1430
1431 // Make sure that this type is translated.
1432 void UpdateCompletedType(const TagDecl *TD);
1433
1434 llvm::Constant *getMemberPointerConstant(const UnaryOperator *e);
1435
1436 /// Emit type info if type of an expression is a variably modified
1437 /// type. Also emit proper debug info for cast types.
1438 void EmitExplicitCastExprType(const ExplicitCastExpr *E,
1439 CodeGenFunction *CGF = nullptr);
1440
1441 /// Return the result of value-initializing the given type, i.e. a null
1442 /// expression of the given type. This is usually, but not always, an LLVM
1443 /// null constant.
1444 llvm::Constant *EmitNullConstant(QualType T);
1445
1446 /// Return a null constant appropriate for zero-initializing a base class with
1447 /// the given type. This is usually, but not always, an LLVM null constant.
1448 llvm::Constant *EmitNullConstantForBase(const CXXRecordDecl *Record);
1449
1450 /// Emit a general error that something can't be done.
1451 void Error(SourceLocation loc, StringRef error);
1452
1453 /// Print out an error that codegen doesn't support the specified stmt yet.
1454 void ErrorUnsupported(const Stmt *S, const char *Type);
1455
1456 /// Print out an error that codegen doesn't support the specified stmt yet.
1457 void ErrorUnsupported(const Stmt *S, llvm::StringRef Type);
1458
1459 /// Print out an error that codegen doesn't support the specified decl yet.
1460 void ErrorUnsupported(const Decl *D, const char *Type);
1461
1462 /// Run some code with "sufficient" stack space. (Currently, at least 256K is
1463 /// guaranteed). Produces a warning if we're low on stack space and allocates
1464 /// more in that case. Use this in code that may recurse deeply to avoid stack
1465 /// overflow.
1466 void runWithSufficientStackSpace(SourceLocation Loc,
1467 llvm::function_ref<void()> Fn);
1468
1469 /// Set the attributes on the LLVM function for the given decl and function
1470 /// info. This applies attributes necessary for handling the ABI as well as
1471 /// user specified attributes like section.
1472 void SetInternalFunctionAttributes(GlobalDecl GD, llvm::Function *F,
1473 const CGFunctionInfo &FI);
1474
1475 /// Set the LLVM function attributes (sext, zext, etc).
1476 void SetLLVMFunctionAttributes(GlobalDecl GD, const CGFunctionInfo &Info,
1477 llvm::Function *F, bool IsThunk);
1478
1479 /// Set the LLVM function attributes which only apply to a function
1480 /// definition.
1481 void SetLLVMFunctionAttributesForDefinition(const Decl *D, llvm::Function *F);
1482
1483 /// Set the LLVM function attributes that represent floating point
1484 /// environment.
1485 void setLLVMFunctionFEnvAttributes(const FunctionDecl *D, llvm::Function *F);
1486
1487 /// Return true iff the given type uses 'sret' when used as a return type.
1488 bool ReturnTypeUsesSRet(const CGFunctionInfo &FI);
1489
1490 /// Return true iff the given type has `inreg` set.
1491 bool ReturnTypeHasInReg(const CGFunctionInfo &FI);
1492
1493 /// Return true iff the given type uses an argument slot when 'sret' is used
1494 /// as a return type.
1495 bool ReturnSlotInterferesWithArgs(const CGFunctionInfo &FI);
1496
1497 /// Return true iff the given type uses 'fpret' when used as a return type.
1498 bool ReturnTypeUsesFPRet(QualType ResultType);
1499
1500 /// Return true iff the given type uses 'fp2ret' when used as a return type.
1501 bool ReturnTypeUsesFP2Ret(QualType ResultType);
1502
1503 /// Get the LLVM attributes and calling convention to use for a particular
1504 /// function type.
1505 ///
1506 /// \param Name - The function name.
1507 /// \param Info - The function type information.
1508 /// \param CalleeInfo - The callee information these attributes are being
1509 /// constructed for. If valid, the attributes applied to this decl may
1510 /// contribute to the function attributes and calling convention.
1511 /// \param Attrs [out] - On return, the attribute list to use.
1512 /// \param CallingConv [out] - On return, the LLVM calling convention to use.
1513 void ConstructAttributeList(StringRef Name, const CGFunctionInfo &Info,
1514 CGCalleeInfo CalleeInfo,
1515 llvm::AttributeList &Attrs, unsigned &CallingConv,
1516 bool AttrOnCallSite, bool IsThunk);
1517
1518 /// Adjust Memory attribute to ensure that the BE gets the right attribute
1519 // in order to generate the library call or the intrinsic for the function
1520 // name 'Name'.
1521 void AdjustMemoryAttribute(StringRef Name, CGCalleeInfo CalleeInfo,
1522 llvm::AttributeList &Attrs);
1523
1524 /// Like the overload taking a `Function &`, but intended specifically
1525 /// for frontends that want to build on Clang's target-configuration logic.
1526 void addDefaultFunctionDefinitionAttributes(llvm::AttrBuilder &attrs);
1527
1528 StringRef getMangledName(GlobalDecl GD);
1529 StringRef getBlockMangledName(GlobalDecl GD, const BlockDecl *BD);
1530 const GlobalDecl getMangledNameDecl(StringRef);
1531
1532 void EmitTentativeDefinition(const VarDecl *D);
1533
1534 void EmitExternalDeclaration(const DeclaratorDecl *D);
1535
1536 void EmitVTable(CXXRecordDecl *Class);
1537
1538 void RefreshTypeCacheForClass(const CXXRecordDecl *Class);
1539
1540 /// Appends Opts to the "llvm.linker.options" metadata value.
1541 void AppendLinkerOptions(StringRef Opts);
1542
1543 /// Appends a detect mismatch command to the linker options.
1544 void AddDetectMismatch(StringRef Name, StringRef Value);
1545
1546 /// Appends a dependent lib to the appropriate metadata value.
1547 void AddDependentLib(StringRef Lib);
1548
1549 llvm::GlobalVariable::LinkageTypes getFunctionLinkage(GlobalDecl GD);
1550
1551 void setFunctionLinkage(GlobalDecl GD, llvm::Function *F) {
1552 F->setLinkage(getFunctionLinkage(GD));
1553 }
1554
1555 /// Return the appropriate linkage for the vtable, VTT, and type information
1556 /// of the given class.
1557 llvm::GlobalVariable::LinkageTypes getVTableLinkage(const CXXRecordDecl *RD);
1558
1559 /// Return the store size, in character units, of the given LLVM type.
1560 CharUnits GetTargetTypeStoreSize(llvm::Type *Ty) const;
1561
1562 /// Returns LLVM linkage for a declarator.
1563 llvm::GlobalValue::LinkageTypes
1564 getLLVMLinkageForDeclarator(const DeclaratorDecl *D, GVALinkage Linkage);
1565
1566 /// Returns LLVM linkage for a declarator.
1567 llvm::GlobalValue::LinkageTypes
1568 getLLVMLinkageVarDefinition(const VarDecl *VD);
1569
1570 /// Emit all the global annotations.
1571 void EmitGlobalAnnotations();
1572
1573 /// Emit an annotation string.
1574 llvm::Constant *EmitAnnotationString(StringRef Str);
1575
1576 /// Emit the annotation's translation unit.
1577 llvm::Constant *EmitAnnotationUnit(SourceLocation Loc);
1578
1579 /// Emit the annotation line number.
1580 llvm::Constant *EmitAnnotationLineNo(SourceLocation L);
1581
1582 /// Emit additional args of the annotation.
1583 llvm::Constant *EmitAnnotationArgs(const AnnotateAttr *Attr);
1584
1585 /// Generate the llvm::ConstantStruct which contains the annotation
1586 /// information for a given GlobalValue. The annotation struct is
1587 /// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the
1588 /// GlobalValue being annotated. The second field is the constant string
1589 /// created from the AnnotateAttr's annotation. The third field is a constant
1590 /// string containing the name of the translation unit. The fourth field is
1591 /// the line number in the file of the annotated value declaration.
1592 llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV,
1593 const AnnotateAttr *AA,
1594 SourceLocation L);
1595
1596 /// Add global annotations that are set on D, for the global GV. Those
1597 /// annotations are emitted during finalization of the LLVM code.
1598 void AddGlobalAnnotations(const ValueDecl *D, llvm::GlobalValue *GV);
1599
1600 bool isInNoSanitizeList(SanitizerMask Kind, llvm::Function *Fn,
1601 SourceLocation Loc) const;
1602
1603 bool isInNoSanitizeList(SanitizerMask Kind, llvm::GlobalVariable *GV,
1604 SourceLocation Loc, QualType Ty,
1605 StringRef Category = StringRef()) const;
1606
1607 /// Imbue XRay attributes to a function, applying the always/never attribute
1608 /// lists in the process. Returns true if we did imbue attributes this way,
1609 /// false otherwise.
1610 bool imbueXRayAttrs(llvm::Function *Fn, SourceLocation Loc,
1611 StringRef Category = StringRef()) const;
1612
1613 /// \returns true if \p Fn at \p Loc should be excluded from profile
1614 /// instrumentation by the SCL passed by \p -fprofile-list.
1615 ProfileList::ExclusionType
1616 isFunctionBlockedByProfileList(llvm::Function *Fn, SourceLocation Loc) const;
1617
1618 /// \returns true if \p Fn at \p Loc should be excluded from profile
1619 /// instrumentation.
1620 ProfileList::ExclusionType
1621 isFunctionBlockedFromProfileInstr(llvm::Function *Fn,
1622 SourceLocation Loc) const;
1623
1624 SanitizerMetadata *getSanitizerMetadata() {
1625 return SanitizerMD.get();
1626 }
1627
1628 void addDeferredVTable(const CXXRecordDecl *RD) {
1629 DeferredVTables.push_back(x: RD);
1630 }
1631
1632 /// Emit code for a single global function or var decl. Forward declarations
1633 /// are emitted lazily.
1634 void EmitGlobal(GlobalDecl D);
1635
1636 /// Record that new[] was called for the class, transform vector deleting
1637 /// destructor definition in a form of alias to the actual definition.
1638 void requireVectorDestructorDefinition(const CXXRecordDecl *RD);
1639
1640 /// Check that class need vector deleting destructor body.
1641 bool classNeedsVectorDestructor(const CXXRecordDecl *RD);
1642
1643 bool TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D);
1644 void EmitDefinitionAsAlias(GlobalDecl Alias, GlobalDecl Target);
1645
1646 llvm::GlobalValue *GetGlobalValue(StringRef Ref);
1647
1648 /// Set attributes which are common to any form of a global definition (alias,
1649 /// Objective-C method, function, global variable).
1650 ///
1651 /// NOTE: This should only be called for definitions.
1652 void SetCommonAttributes(GlobalDecl GD, llvm::GlobalValue *GV);
1653
1654 void addReplacement(StringRef Name, llvm::Constant *C);
1655
1656 void addGlobalValReplacement(llvm::GlobalValue *GV, llvm::Constant *C);
1657
1658 /// Emit a code for threadprivate directive.
1659 /// \param D Threadprivate declaration.
1660 void EmitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D);
1661
1662 /// Emit a code for declare reduction construct.
1663 void EmitOMPDeclareReduction(const OMPDeclareReductionDecl *D,
1664 CodeGenFunction *CGF = nullptr);
1665
1666 /// Emit a code for declare mapper construct.
1667 void EmitOMPDeclareMapper(const OMPDeclareMapperDecl *D,
1668 CodeGenFunction *CGF = nullptr);
1669
1670 // Emit code for the OpenACC Declare declaration.
1671 void EmitOpenACCDeclare(const OpenACCDeclareDecl *D,
1672 CodeGenFunction *CGF = nullptr);
1673 // Emit code for the OpenACC Routine declaration.
1674 void EmitOpenACCRoutine(const OpenACCRoutineDecl *D,
1675 CodeGenFunction *CGF = nullptr);
1676
1677 /// Emit a code for requires directive.
1678 /// \param D Requires declaration
1679 void EmitOMPRequiresDecl(const OMPRequiresDecl *D);
1680
1681 /// Emit a code for the allocate directive.
1682 /// \param D The allocate declaration
1683 void EmitOMPAllocateDecl(const OMPAllocateDecl *D);
1684
1685 /// Return the alignment specified in an allocate directive, if present.
1686 std::optional<CharUnits> getOMPAllocateAlignment(const VarDecl *VD);
1687
1688 /// Returns whether the given record has hidden LTO visibility and therefore
1689 /// may participate in (single-module) CFI and whole-program vtable
1690 /// optimization.
1691 bool HasHiddenLTOVisibility(const CXXRecordDecl *RD);
1692
1693 /// Returns whether the given record has public LTO visibility (regardless of
1694 /// -lto-whole-program-visibility) and therefore may not participate in
1695 /// (single-module) CFI and whole-program vtable optimization.
1696 bool AlwaysHasLTOVisibilityPublic(const CXXRecordDecl *RD);
1697
1698 /// Returns the vcall visibility of the given type. This is the scope in which
1699 /// a virtual function call could be made which ends up being dispatched to a
1700 /// member function of this class. This scope can be wider than the visibility
1701 /// of the class itself when the class has a more-visible dynamic base class.
1702 /// The client should pass in an empty Visited set, which is used to prevent
1703 /// redundant recursive processing.
1704 llvm::GlobalObject::VCallVisibility
1705 GetVCallVisibilityLevel(const CXXRecordDecl *RD,
1706 llvm::DenseSet<const CXXRecordDecl *> &Visited);
1707
1708 /// Emit type metadata for the given vtable using the given layout.
1709 void EmitVTableTypeMetadata(const CXXRecordDecl *RD,
1710 llvm::GlobalVariable *VTable,
1711 const VTableLayout &VTLayout);
1712
1713 llvm::Type *getVTableComponentType() const;
1714
1715 /// Generate a cross-DSO type identifier for MD.
1716 llvm::ConstantInt *CreateCrossDsoCfiTypeId(llvm::Metadata *MD);
1717
1718 /// Generate a KCFI type identifier for T.
1719 llvm::ConstantInt *CreateKCFITypeId(QualType T, StringRef Salt);
1720
1721 /// Create a metadata identifier for the given function type.
1722 llvm::Metadata *CreateMetadataIdentifierForFnType(QualType T);
1723
1724 /// Create a metadata identifier for the given type. This may either be an
1725 /// MDString (for external identifiers) or a distinct unnamed MDNode (for
1726 /// internal identifiers).
1727 llvm::Metadata *CreateMetadataIdentifierForType(QualType T);
1728
1729 /// Create a metadata identifier that is intended to be used to check virtual
1730 /// calls via a member function pointer.
1731 llvm::Metadata *CreateMetadataIdentifierForVirtualMemPtrType(QualType T);
1732
1733 /// Create a metadata identifier for the generalization of the given type.
1734 /// This may either be an MDString (for external identifiers) or a distinct
1735 /// unnamed MDNode (for internal identifiers).
1736 llvm::Metadata *CreateMetadataIdentifierGeneralized(QualType T);
1737
1738 /// Create and attach type metadata to the given function.
1739 void createFunctionTypeMetadataForIcall(const FunctionDecl *FD,
1740 llvm::Function *F);
1741
1742 /// Create and attach type metadata if the function is a potential indirect
1743 /// call target to support call graph section.
1744 void createIndirectFunctionTypeMD(const FunctionDecl *FD, llvm::Function *F);
1745
1746 /// Create and attach type metadata to the given call.
1747 void createCalleeTypeMetadataForIcall(const QualType &QT, llvm::CallBase *CB);
1748
1749 /// Set type metadata to the given function.
1750 void setKCFIType(const FunctionDecl *FD, llvm::Function *F);
1751
1752 /// Emit KCFI type identifier constants and remove unused identifiers.
1753 void finalizeKCFITypes();
1754
1755 /// Whether this function's return type has no side effects, and thus may
1756 /// be trivially discarded if it is unused.
1757 bool MayDropFunctionReturn(const ASTContext &Context,
1758 QualType ReturnType) const;
1759
1760 /// Returns whether this module needs the "all-vtables" type identifier.
1761 bool NeedAllVtablesTypeId() const;
1762
1763 /// Create and attach type metadata for the given vtable.
1764 void AddVTableTypeMetadata(llvm::GlobalVariable *VTable, CharUnits Offset,
1765 const CXXRecordDecl *RD);
1766
1767 /// Return a vector of most-base classes for RD. This is used to implement
1768 /// control flow integrity checks for member function pointers.
1769 ///
1770 /// A most-base class of a class C is defined as a recursive base class of C,
1771 /// including C itself, that does not have any bases.
1772 SmallVector<const CXXRecordDecl *, 0>
1773 getMostBaseClasses(const CXXRecordDecl *RD);
1774
1775 /// Get the declaration of std::terminate for the platform.
1776 llvm::FunctionCallee getTerminateFn();
1777
1778 llvm::SanitizerStatReport &getSanStats();
1779
1780 llvm::Value *
1781 createOpenCLIntToSamplerConversion(const Expr *E, CodeGenFunction &CGF);
1782
1783 /// OpenCL v1.2 s5.6.4.6 allows the compiler to store kernel argument
1784 /// information in the program executable. The argument information stored
1785 /// includes the argument name, its type, the address and access qualifiers
1786 /// used. This helper can be used to generate metadata for source code kernel
1787 /// function as well as generated implicitly kernels. If a kernel is generated
1788 /// implicitly null value has to be passed to the last two parameters,
1789 /// otherwise all parameters must have valid non-null values.
1790 /// \param FN is a pointer to IR function being generated.
1791 /// \param FD is a pointer to function declaration if any.
1792 /// \param CGF is a pointer to CodeGenFunction that generates this function.
1793 void GenKernelArgMetadata(llvm::Function *FN,
1794 const FunctionDecl *FD = nullptr,
1795 CodeGenFunction *CGF = nullptr);
1796
1797 /// Get target specific null pointer.
1798 /// \param T is the LLVM type of the null pointer.
1799 /// \param QT is the clang QualType of the null pointer.
1800 llvm::Constant *getNullPointer(llvm::PointerType *T, QualType QT);
1801
1802 CharUnits getNaturalTypeAlignment(QualType T,
1803 LValueBaseInfo *BaseInfo = nullptr,
1804 TBAAAccessInfo *TBAAInfo = nullptr,
1805 bool forPointeeType = false);
1806 CharUnits getNaturalPointeeTypeAlignment(QualType T,
1807 LValueBaseInfo *BaseInfo = nullptr,
1808 TBAAAccessInfo *TBAAInfo = nullptr);
1809 bool stopAutoInit();
1810
1811 /// Print the postfix for externalized static variable or kernels for single
1812 /// source offloading languages CUDA and HIP. The unique postfix is created
1813 /// using either the CUID argument, or the file's UniqueID and active macros.
1814 /// The fallback method without a CUID requires that the offloading toolchain
1815 /// does not define separate macros via the -cc1 options.
1816 void printPostfixForExternalizedDecl(llvm::raw_ostream &OS,
1817 const Decl *D) const;
1818
1819 /// Move some lazily-emitted states to the NewBuilder. This is especially
1820 /// essential for the incremental parsing environment like Clang Interpreter,
1821 /// because we'll lose all important information after each repl.
1822 void moveLazyEmissionStates(CodeGenModule *NewBuilder);
1823
1824 /// Emit the IR encoding to attach the CUDA launch bounds attribute to \p F.
1825 /// If \p MaxThreadsVal is not nullptr, the max threads value is stored in it,
1826 /// if a valid one was found.
1827 void handleCUDALaunchBoundsAttr(llvm::Function *F,
1828 const CUDALaunchBoundsAttr *A,
1829 int32_t *MaxThreadsVal = nullptr,
1830 int32_t *MinBlocksVal = nullptr,
1831 int32_t *MaxClusterRankVal = nullptr);
1832
1833 /// Emit the IR encoding to attach the AMD GPU flat-work-group-size attribute
1834 /// to \p F. Alternatively, the work group size can be taken from a \p
1835 /// ReqdWGS. If \p MinThreadsVal is not nullptr, the min threads value is
1836 /// stored in it, if a valid one was found. If \p MaxThreadsVal is not
1837 /// nullptr, the max threads value is stored in it, if a valid one was found.
1838 void handleAMDGPUFlatWorkGroupSizeAttr(
1839 llvm::Function *F, const AMDGPUFlatWorkGroupSizeAttr *A,
1840 const ReqdWorkGroupSizeAttr *ReqdWGS = nullptr,
1841 int32_t *MinThreadsVal = nullptr, int32_t *MaxThreadsVal = nullptr);
1842
1843 /// Emit the IR encoding to attach the AMD GPU waves-per-eu attribute to \p F.
1844 void handleAMDGPUWavesPerEUAttr(llvm::Function *F,
1845 const AMDGPUWavesPerEUAttr *A);
1846
1847 llvm::Constant *
1848 GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty, LangAS AddrSpace,
1849 const VarDecl *D,
1850 ForDefinition_t IsForDefinition = NotForDefinition);
1851
1852 // FIXME: Hardcoding priority here is gross.
1853 void AddGlobalCtor(llvm::Function *Ctor, int Priority = 65535,
1854 unsigned LexOrder = ~0U,
1855 llvm::Constant *AssociatedData = nullptr);
1856 void AddGlobalDtor(llvm::Function *Dtor, int Priority = 65535,
1857 bool IsDtorAttrFunc = false);
1858
1859 // Return whether structured convergence intrinsics should be generated for
1860 // this target.
1861 bool shouldEmitConvergenceTokens() const {
1862 // TODO: this should probably become unconditional once the controlled
1863 // convergence becomes the norm.
1864 return getTriple().isSPIRVLogical() || getTriple().isDXIL();
1865 }
1866
1867 void addUndefinedGlobalForTailCall(
1868 std::pair<const FunctionDecl *, SourceLocation> Global) {
1869 MustTailCallUndefinedGlobals.insert(X: Global);
1870 }
1871
1872 bool shouldZeroInitPadding() const {
1873 // In C23 (N3096) $6.7.10:
1874 // """
1875 // If any object is initialized with an empty iniitializer, then it is
1876 // subject to default initialization:
1877 // - if it is an aggregate, every member is initialized (recursively)
1878 // according to these rules, and any padding is initialized to zero bits;
1879 // - if it is a union, the first named member is initialized (recursively)
1880 // according to these rules, and any padding is initialized to zero bits.
1881 //
1882 // If the aggregate or union contains elements or members that are
1883 // aggregates or unions, these rules apply recursively to the subaggregates
1884 // or contained unions.
1885 //
1886 // If there are fewer initializers in a brace-enclosed list than there are
1887 // elements or members of an aggregate, or fewer characters in a string
1888 // literal used to initialize an array of known size than there are elements
1889 // in the array, the remainder of the aggregate is subject to default
1890 // initialization.
1891 // """
1892 //
1893 // From my understanding, the standard is ambiguous in the following two
1894 // areas:
1895 // 1. For a union type with empty initializer, if the first named member is
1896 // not the largest member, then the bytes comes after the first named member
1897 // but before padding are left unspecified. An example is:
1898 // union U { int a; long long b;};
1899 // union U u = {}; // The first 4 bytes are 0, but 4-8 bytes are left
1900 // unspecified.
1901 //
1902 // 2. It only mentions padding for empty initializer, but doesn't mention
1903 // padding for a non empty initialization list. And if the aggregation or
1904 // union contains elements or members that are aggregates or unions, and
1905 // some are non empty initializers, while others are empty initiailizers,
1906 // the padding initialization is unclear. An example is:
1907 // struct S1 { int a; long long b; };
1908 // struct S2 { char c; struct S1 s1; };
1909 // // The values for paddings between s2.c and s2.s1.a, between s2.s1.a
1910 // and s2.s1.b are unclear.
1911 // struct S2 s2 = { 'c' };
1912 //
1913 // Here we choose to zero initiailize left bytes of a union type. Because
1914 // projects like the Linux kernel are relying on this behavior. If we don't
1915 // explicitly zero initialize them, the undef values can be optimized to
1916 // return gabage data. We also choose to zero initialize paddings for
1917 // aggregates and unions, no matter they are initialized by empty
1918 // initializers or non empty initializers. This can provide a consistent
1919 // behavior. So projects like the Linux kernel can rely on it.
1920 return !getLangOpts().CPlusPlus;
1921 }
1922
1923 // Helper to get the alignment for a variable.
1924 unsigned getVtableGlobalVarAlignment(const VarDecl *D = nullptr) {
1925 LangAS AS = GetGlobalVarAddressSpace(D);
1926 unsigned PAlign = Context.getLangOpts().RelativeCXXABIVTables
1927 ? 32
1928 : getTarget().getPointerAlign(AddrSpace: AS);
1929 return PAlign;
1930 }
1931
1932 /// Helper function to construct a TrapReasonBuilder
1933 TrapReasonBuilder BuildTrapReason(unsigned DiagID, TrapReason &TR) {
1934 return TrapReasonBuilder(&getDiags(), DiagID, TR);
1935 }
1936
1937 llvm::Constant *performAddrSpaceCast(llvm::Constant *Src,
1938 llvm::Type *DestTy) {
1939 // Since target may map different address spaces in AST to the same address
1940 // space, an address space conversion may end up as a bitcast.
1941 return llvm::ConstantExpr::getPointerCast(C: Src, Ty: DestTy);
1942 }
1943
1944 std::optional<llvm::Attribute::AttrKind>
1945 StackProtectorAttribute(const Decl *D) const;
1946
1947 std::string getPFPFieldName(const FieldDecl *FD);
1948 llvm::GlobalValue *getPFPDeactivationSymbol(const FieldDecl *FD);
1949
1950private:
1951 /// Translate an llvm::abi::ArgInfo (computed by the LLVMABI library) into
1952 /// the clang ABIArgInfo consumed by the rest of CodeGen. Used by the
1953 /// experimental ABI lowering path.
1954 ABIArgInfo convertABIArgInfo(const llvm::abi::ArgInfo &AbiInfo,
1955 QualType Type);
1956
1957 /// Process #pragma comment(copyright, ...).
1958 void ProcessPragmaCommentCopyright(StringRef Comment, bool isFromASTFile);
1959
1960 bool shouldDropDLLAttribute(const Decl *D, const llvm::GlobalValue *GV) const;
1961
1962 llvm::Constant *GetOrCreateLLVMFunction(
1963 StringRef MangledName, llvm::Type *Ty, GlobalDecl D, bool ForVTable,
1964 bool DontDefer = false, bool IsThunk = false,
1965 llvm::AttributeList ExtraAttrs = llvm::AttributeList(),
1966 ForDefinition_t IsForDefinition = NotForDefinition);
1967
1968 // Adds a declaration to the list of multi version functions if not present.
1969 void AddDeferredMultiVersionResolverToEmit(GlobalDecl GD);
1970
1971 // References to multiversion functions are resolved through an implicitly
1972 // defined resolver function. This function is responsible for creating
1973 // the resolver symbol for the provided declaration. The value returned
1974 // will be for an ifunc (llvm::GlobalIFunc) if the current target supports
1975 // that feature and for a regular function (llvm::GlobalValue) otherwise.
1976 llvm::Constant *GetOrCreateMultiVersionResolver(GlobalDecl GD);
1977
1978 // Set attributes to a resolver function generated by Clang.
1979 // GD is either the cpu_dispatch declaration or an arbitrarily chosen
1980 // function declaration that triggered the implicit generation of this
1981 // resolver function.
1982 //
1983 /// NOTE: This should only be called for definitions.
1984 void setMultiVersionResolverAttributes(llvm::Function *Resolver,
1985 GlobalDecl GD);
1986
1987 // In scenarios where a function is not known to be a multiversion function
1988 // until a later declaration, it is sometimes necessary to change the
1989 // previously created mangled name to align with requirements of whatever
1990 // multiversion function kind the function is now known to be. This function
1991 // is responsible for performing such mangled name updates.
1992 void UpdateMultiVersionNames(GlobalDecl GD, const FunctionDecl *FD,
1993 StringRef &CurName);
1994
1995 bool GetCPUAndFeaturesAttributes(GlobalDecl GD,
1996 llvm::AttrBuilder &AttrBuilder,
1997 bool SetTargetFeatures = true);
1998 void setNonAliasAttributes(GlobalDecl GD, llvm::GlobalObject *GO);
1999
2000 /// Set function attributes for a function declaration.
2001 void SetFunctionAttributes(GlobalDecl GD, llvm::Function *F,
2002 bool IsIncompleteFunction, bool IsThunk);
2003
2004 void EmitGlobalDefinition(GlobalDecl D, llvm::GlobalValue *GV = nullptr);
2005
2006 void EmitGlobalFunctionDefinition(GlobalDecl GD, llvm::GlobalValue *GV);
2007 void EmitMultiVersionFunctionDefinition(GlobalDecl GD, llvm::GlobalValue *GV);
2008
2009 void EmitGlobalVarDefinition(const VarDecl *D, bool IsTentative = false);
2010 void EmitAliasDefinition(GlobalDecl GD);
2011 void emitIFuncDefinition(GlobalDecl GD);
2012 void emitCPUDispatchDefinition(GlobalDecl GD);
2013 void EmitObjCPropertyImplementations(const ObjCImplementationDecl *D);
2014 void EmitObjCIvarInitializations(ObjCImplementationDecl *D);
2015
2016 // C++ related functions.
2017
2018 void EmitDeclContext(const DeclContext *DC);
2019 void EmitLinkageSpec(const LinkageSpecDecl *D);
2020 void EmitTopLevelStmt(const TopLevelStmtDecl *D);
2021
2022 /// Emit the function that initializes C++ thread_local variables.
2023 void EmitCXXThreadLocalInitFunc();
2024
2025 /// Emit the function that initializes global variables for a C++ Module.
2026 void EmitCXXModuleInitFunc(clang::Module *Primary);
2027
2028 /// Emit the function that initializes C++ globals.
2029 void EmitCXXGlobalInitFunc();
2030
2031 /// Emit the function that performs cleanup associated with C++ globals.
2032 void EmitCXXGlobalCleanUpFunc();
2033
2034 /// Emit the function that initializes the specified global (if PerformInit is
2035 /// true) and registers its destructor.
2036 void EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
2037 llvm::GlobalVariable *Addr,
2038 bool PerformInit);
2039
2040 void EmitPointerToInitFunc(const VarDecl *VD, llvm::GlobalVariable *Addr,
2041 llvm::Function *InitFunc, InitSegAttr *ISA);
2042
2043 /// EmitCtorList - Generates a global array of functions and priorities using
2044 /// the given list and name. This array will have appending linkage and is
2045 /// suitable for use as a LLVM constructor or destructor array. Clears Fns.
2046 void EmitCtorList(CtorList &Fns, const char *GlobalName);
2047
2048 /// Emit any needed decls for which code generation was deferred.
2049 void EmitDeferred();
2050
2051 /// Try to emit external vtables as available_externally if they have emitted
2052 /// all inlined virtual functions. It runs after EmitDeferred() and therefore
2053 /// is not allowed to create new references to things that need to be emitted
2054 /// lazily.
2055 void EmitVTablesOpportunistically();
2056
2057 /// Call replaceAllUsesWith on all pairs in Replacements.
2058 void applyReplacements();
2059
2060 /// Call replaceAllUsesWith on all pairs in GlobalValReplacements.
2061 void applyGlobalValReplacements();
2062
2063 void checkAliases();
2064
2065 std::map<int, llvm::TinyPtrVector<llvm::Function *>> DtorsUsingAtExit;
2066
2067 /// Register functions annotated with __attribute__((destructor)) using
2068 /// __cxa_atexit, if it is available, or atexit otherwise.
2069 void registerGlobalDtorsWithAtExit();
2070
2071 // When using sinit and sterm functions, unregister
2072 // __attribute__((destructor)) annotated functions which were previously
2073 // registered by the atexit subroutine using unatexit.
2074 void unregisterGlobalDtorsWithUnAtExit();
2075
2076 /// Emit deferred multiversion function resolvers and associated variants.
2077 void emitMultiVersionFunctions();
2078
2079 /// Emit any vtables which we deferred and still have a use for.
2080 void EmitDeferredVTables();
2081
2082 /// Emit a dummy function that reference a CoreFoundation symbol when
2083 /// @available is used on Darwin.
2084 void emitAtAvailableLinkGuard();
2085
2086 /// Emit the llvm.used and llvm.compiler.used metadata.
2087 void emitLLVMUsed();
2088
2089 /// For C++20 Itanium ABI, emit the initializers for the module.
2090 void EmitModuleInitializers(clang::Module *Primary);
2091
2092 /// Emit the link options introduced by imported modules.
2093 void EmitModuleLinkOptions();
2094
2095 /// Helper function for EmitStaticExternCAliases() to redirect ifuncs that
2096 /// have a resolver name that matches 'Elem' to instead resolve to the name of
2097 /// 'CppFunc'. This redirection is necessary in cases where 'Elem' has a name
2098 /// that will be emitted as an alias of the name bound to 'CppFunc'; ifuncs
2099 /// may not reference aliases. Redirection is only performed if 'Elem' is only
2100 /// used by ifuncs in which case, 'Elem' is destroyed. 'true' is returned if
2101 /// redirection is successful, and 'false' is returned otherwise.
2102 bool CheckAndReplaceExternCIFuncs(llvm::GlobalValue *Elem,
2103 llvm::GlobalValue *CppFunc);
2104
2105 /// Emit aliases for internal-linkage declarations inside "C" language
2106 /// linkage specifications, giving them the "expected" name where possible.
2107 void EmitStaticExternCAliases();
2108
2109 void EmitDeclMetadata();
2110
2111 /// Emit the Clang version as llvm.ident metadata.
2112 void EmitVersionIdentMetadata();
2113
2114 /// Emit the Clang commandline as llvm.commandline metadata.
2115 void EmitCommandLineMetadata();
2116
2117 /// Emit the module flag metadata used to pass options controlling the
2118 /// the backend to LLVM.
2119 void EmitBackendOptionsMetadata(const CodeGenOptions &CodeGenOpts);
2120
2121 /// Emits OpenCL specific Metadata e.g. OpenCL version.
2122 void EmitOpenCLMetadata();
2123
2124 /// Emit the llvm.gcov metadata used to tell LLVM where to emit the .gcno and
2125 /// .gcda files in a way that persists in .bc files.
2126 void EmitCoverageFile();
2127
2128 /// Given a sycl_kernel_entry_point attributed function, emit the
2129 /// corresponding SYCL kernel caller offload entry point function.
2130 void EmitSYCLKernelCaller(const FunctionDecl *KernelEntryPointFn,
2131 ASTContext &Ctx);
2132
2133 /// Attach the "sycl-module-id" function attribute to \p Fn, to record the
2134 /// module ID for the translation unit. This attribute is applied to SYCL
2135 /// kernel entry point functions and functions declared with the
2136 /// sycl_external attribute to enable them to be identified as entry points
2137 /// by clang-sycl-linker during device-code splitting.
2138 void addSYCLModuleIdAttr(llvm::Function *Fn);
2139
2140 /// Determine whether the definition must be emitted; if this returns \c
2141 /// false, the definition can be emitted lazily if it's used.
2142 bool MustBeEmitted(const ValueDecl *D);
2143
2144 /// Determine whether the definition can be emitted eagerly, or should be
2145 /// delayed until the end of the translation unit. This is relevant for
2146 /// definitions whose linkage can change, e.g. implicit function instantions
2147 /// which may later be explicitly instantiated.
2148 bool MayBeEmittedEagerly(const ValueDecl *D);
2149
2150 /// Check whether we can use a "simpler", more core exceptions personality
2151 /// function.
2152 void SimplifyPersonality();
2153
2154 /// Helper function for getDefaultFunctionAttributes. Builds a set of function
2155 /// attributes which can be simply added to a function.
2156 void getTrivialDefaultFunctionAttributes(StringRef Name, bool HasOptnone,
2157 bool AttrOnCallSite,
2158 llvm::AttrBuilder &FuncAttrs);
2159
2160 /// Helper function for ConstructAttributeList and
2161 /// addDefaultFunctionDefinitionAttributes. Builds a set of function
2162 /// attributes to add to a function with the given properties.
2163 void getDefaultFunctionAttributes(StringRef Name, bool HasOptnone,
2164 bool AttrOnCallSite,
2165 llvm::AttrBuilder &FuncAttrs);
2166
2167 llvm::Metadata *CreateMetadataIdentifierImpl(QualType T, MetadataTypeMap &Map,
2168 StringRef Suffix);
2169
2170 /// Emit deactivation symbols for any PFP fields whose offset is taken with
2171 /// offsetof.
2172 void emitPFPFieldsWithEvaluatedOffset();
2173};
2174
2175} // end namespace CodeGen
2176} // end namespace clang
2177
2178#endif // LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H
2179