1 //===--- CodeGenModule.h - Per-Module state for LLVM CodeGen ----*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This is the internal per-translation-unit state used for llvm translation.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H
14 #define LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H
16 #include "CGVTables.h"
17 #include "CodeGenTypeCache.h"
18 #include "CodeGenTypes.h"
19 #include "SanitizerMetadata.h"
20 #include "clang/AST/DeclCXX.h"
21 #include "clang/AST/DeclObjC.h"
22 #include "clang/AST/DeclOpenMP.h"
23 #include "clang/AST/GlobalDecl.h"
24 #include "clang/AST/Mangle.h"
25 #include "clang/Basic/ABI.h"
26 #include "clang/Basic/LangOptions.h"
27 #include "clang/Basic/NoSanitizeList.h"
28 #include "clang/Basic/ProfileList.h"
29 #include "clang/Basic/TargetInfo.h"
30 #include "clang/Basic/XRayLists.h"
31 #include "clang/Lex/PreprocessorOptions.h"
32 #include "llvm/ADT/DenseMap.h"
33 #include "llvm/ADT/MapVector.h"
34 #include "llvm/ADT/SetVector.h"
35 #include "llvm/ADT/SmallPtrSet.h"
36 #include "llvm/ADT/StringMap.h"
37 #include "llvm/IR/Module.h"
38 #include "llvm/IR/ValueHandle.h"
39 #include "llvm/Transforms/Utils/SanitizerStats.h"
51 class IndexedInstrProfReader
;
63 class ObjCImplementationDecl
;
72 class PointerAuthSchema
;
77 class HeaderSearchOptions
;
78 class DiagnosticsEngine
;
80 class CXXDestructorDecl
;
82 class CoverageSourceInfo
;
87 class CodeGenFunction
;
92 class CGOpenCLRuntime
;
93 class CGOpenMPRuntime
;
96 class CoverageMappingModuleGen
;
97 class TargetCodeGenInfo
;
99 enum ForDefinition_t
: bool {
100 NotForDefinition
= false,
104 struct OrderGlobalInitsOrStermFinalizers
{
105 unsigned int priority
;
106 unsigned int lex_order
;
107 OrderGlobalInitsOrStermFinalizers(unsigned int p
, unsigned int l
)
108 : priority(p
), lex_order(l
) {}
110 bool operator==(const OrderGlobalInitsOrStermFinalizers
&RHS
) const {
111 return priority
== RHS
.priority
&& lex_order
== RHS
.lex_order
;
114 bool operator<(const OrderGlobalInitsOrStermFinalizers
&RHS
) const {
115 return std::tie(priority
, lex_order
) <
116 std::tie(RHS
.priority
, RHS
.lex_order
);
120 struct ObjCEntrypoints
{
121 ObjCEntrypoints() { memset(this, 0, sizeof(*this)); }
123 /// void objc_alloc(id);
124 llvm::FunctionCallee objc_alloc
;
126 /// void objc_allocWithZone(id);
127 llvm::FunctionCallee objc_allocWithZone
;
129 /// void objc_alloc_init(id);
130 llvm::FunctionCallee objc_alloc_init
;
132 /// void objc_autoreleasePoolPop(void*);
133 llvm::FunctionCallee objc_autoreleasePoolPop
;
135 /// void objc_autoreleasePoolPop(void*);
136 /// Note this method is used when we are using exception handling
137 llvm::FunctionCallee objc_autoreleasePoolPopInvoke
;
139 /// void *objc_autoreleasePoolPush(void);
140 llvm::Function
*objc_autoreleasePoolPush
;
142 /// id objc_autorelease(id);
143 llvm::Function
*objc_autorelease
;
145 /// id objc_autorelease(id);
146 /// Note this is the runtime method not the intrinsic.
147 llvm::FunctionCallee objc_autoreleaseRuntimeFunction
;
149 /// id objc_autoreleaseReturnValue(id);
150 llvm::Function
*objc_autoreleaseReturnValue
;
152 /// void objc_copyWeak(id *dest, id *src);
153 llvm::Function
*objc_copyWeak
;
155 /// void objc_destroyWeak(id*);
156 llvm::Function
*objc_destroyWeak
;
158 /// id objc_initWeak(id*, id);
159 llvm::Function
*objc_initWeak
;
161 /// id objc_loadWeak(id*);
162 llvm::Function
*objc_loadWeak
;
164 /// id objc_loadWeakRetained(id*);
165 llvm::Function
*objc_loadWeakRetained
;
167 /// void objc_moveWeak(id *dest, id *src);
168 llvm::Function
*objc_moveWeak
;
170 /// id objc_retain(id);
171 llvm::Function
*objc_retain
;
173 /// id objc_retain(id);
174 /// Note this is the runtime method not the intrinsic.
175 llvm::FunctionCallee objc_retainRuntimeFunction
;
177 /// id objc_retainAutorelease(id);
178 llvm::Function
*objc_retainAutorelease
;
180 /// id objc_retainAutoreleaseReturnValue(id);
181 llvm::Function
*objc_retainAutoreleaseReturnValue
;
183 /// id objc_retainAutoreleasedReturnValue(id);
184 llvm::Function
*objc_retainAutoreleasedReturnValue
;
186 /// id objc_retainBlock(id);
187 llvm::Function
*objc_retainBlock
;
189 /// void objc_release(id);
190 llvm::Function
*objc_release
;
192 /// void objc_release(id);
193 /// Note this is the runtime method not the intrinsic.
194 llvm::FunctionCallee objc_releaseRuntimeFunction
;
196 /// void objc_storeStrong(id*, id);
197 llvm::Function
*objc_storeStrong
;
199 /// id objc_storeWeak(id*, id);
200 llvm::Function
*objc_storeWeak
;
202 /// id objc_unsafeClaimAutoreleasedReturnValue(id);
203 llvm::Function
*objc_unsafeClaimAutoreleasedReturnValue
;
205 /// A void(void) inline asm to use to mark that the return value of
206 /// a call will be immediately retain.
207 llvm::InlineAsm
*retainAutoreleasedReturnValueMarker
;
209 /// void clang.arc.use(...);
210 llvm::Function
*clang_arc_use
;
212 /// void clang.arc.noop.use(...);
213 llvm::Function
*clang_arc_noop_use
;
216 /// This class records statistics on instrumentation based profiling.
217 class InstrProfStats
{
218 uint32_t VisitedInMainFile
= 0;
219 uint32_t MissingInMainFile
= 0;
220 uint32_t Visited
= 0;
221 uint32_t Missing
= 0;
222 uint32_t Mismatched
= 0;
225 InstrProfStats() = default;
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
) {
233 /// Record that a function we've visited has no profile data.
234 void addMissing(bool MainFile
) {
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
);
247 /// A pair of helper functions for a __block variable.
248 class BlockByrefHelpers
: public llvm::FoldingSetNode
{
249 // MSVC requires this type to be complete in order to process this
252 llvm::Constant
*CopyHelper
;
253 llvm::Constant
*DisposeHelper
;
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.
260 BlockByrefHelpers(CharUnits alignment
)
261 : CopyHelper(nullptr), DisposeHelper(nullptr), Alignment(alignment
) {}
262 BlockByrefHelpers(const BlockByrefHelpers
&) = default;
263 virtual ~BlockByrefHelpers();
265 void Profile(llvm::FoldingSetNodeID
&id
) const {
266 id
.AddInteger(Alignment
.getQuantity());
269 virtual void profileImpl(llvm::FoldingSetNodeID
&id
) const = 0;
271 virtual bool needsCopy() const { return true; }
272 virtual void emitCopy(CodeGenFunction
&CGF
, Address dest
, Address src
) = 0;
274 virtual bool needsDispose() const { return true; }
275 virtual void emitDispose(CodeGenFunction
&CGF
, Address field
) = 0;
278 /// This class organizes the cross-function state that is used while generating
280 class CodeGenModule
: public CodeGenTypeCache
{
281 CodeGenModule(const CodeGenModule
&) = delete;
282 void operator=(const CodeGenModule
&) = delete;
287 : Priority(0), LexOrder(~0u), Initializer(nullptr),
288 AssociatedData(nullptr) {}
289 Structor(int Priority
, unsigned LexOrder
, llvm::Constant
*Initializer
,
290 llvm::Constant
*AssociatedData
)
291 : Priority(Priority
), LexOrder(LexOrder
), Initializer(Initializer
),
292 AssociatedData(AssociatedData
) {}
295 llvm::Constant
*Initializer
;
296 llvm::Constant
*AssociatedData
;
299 typedef std::vector
<Structor
> CtorList
;
303 const LangOptions
&LangOpts
;
304 IntrusiveRefCntPtr
<llvm::vfs::FileSystem
> FS
; // Only used for debug info.
305 const HeaderSearchOptions
&HeaderSearchOpts
; // Only used for debug info.
306 const PreprocessorOptions
&PreprocessorOpts
; // Only used for debug info.
307 const CodeGenOptions
&CodeGenOpts
;
308 unsigned NumAutoVarInit
= 0;
309 llvm::Module
&TheModule
;
310 DiagnosticsEngine
&Diags
;
311 const TargetInfo
&Target
;
312 std::unique_ptr
<CGCXXABI
> ABI
;
313 llvm::LLVMContext
&VMContext
;
314 std::string ModuleNameHash
;
315 bool CXX20ModuleInits
= false;
316 std::unique_ptr
<CodeGenTBAA
> TBAA
;
318 mutable std::unique_ptr
<TargetCodeGenInfo
> TheTargetCodeGenInfo
;
320 // This should not be moved earlier, since its initialization depends on some
321 // of the previous reference members being already initialized and also checks
322 // if TheTargetCodeGenInfo is NULL
323 std::unique_ptr
<CodeGenTypes
> Types
;
325 /// Holds information about C++ vtables.
326 CodeGenVTables VTables
;
328 std::unique_ptr
<CGObjCRuntime
> ObjCRuntime
;
329 std::unique_ptr
<CGOpenCLRuntime
> OpenCLRuntime
;
330 std::unique_ptr
<CGOpenMPRuntime
> OpenMPRuntime
;
331 std::unique_ptr
<CGCUDARuntime
> CUDARuntime
;
332 std::unique_ptr
<CGHLSLRuntime
> HLSLRuntime
;
333 std::unique_ptr
<CGDebugInfo
> DebugInfo
;
334 std::unique_ptr
<ObjCEntrypoints
> ObjCData
;
335 llvm::MDNode
*NoObjCARCExceptionsMetadata
= nullptr;
336 std::unique_ptr
<llvm::IndexedInstrProfReader
> PGOReader
;
337 InstrProfStats PGOStats
;
338 std::unique_ptr
<llvm::SanitizerStatReport
> SanStats
;
340 // A set of references that have only been seen via a weakref so far. This is
341 // used to remove the weak of the reference if we ever see a direct reference
343 llvm::SmallPtrSet
<llvm::GlobalValue
*, 10> WeakRefReferences
;
345 /// This contains all the decls which have definitions but/ which are deferred
346 /// for emission and therefore should only be output if they are actually
347 /// used. If a decl is in this, then it is known to have not been referenced
349 llvm::DenseMap
<StringRef
, GlobalDecl
> DeferredDecls
;
351 llvm::StringSet
<llvm::BumpPtrAllocator
> DeferredResolversToEmit
;
353 /// This is a list of deferred decls which we have seen that *are* actually
354 /// referenced. These get code generated when the module is done.
355 std::vector
<GlobalDecl
> DeferredDeclsToEmit
;
356 void addDeferredDeclToEmit(GlobalDecl GD
) {
357 DeferredDeclsToEmit
.emplace_back(GD
);
358 addEmittedDeferredDecl(GD
);
361 /// Decls that were DeferredDecls and have now been emitted.
362 llvm::DenseMap
<llvm::StringRef
, GlobalDecl
> EmittedDeferredDecls
;
364 void addEmittedDeferredDecl(GlobalDecl GD
) {
365 // Reemission is only needed in incremental mode.
366 if (!Context
.getLangOpts().IncrementalExtensions
)
369 // Assume a linkage by default that does not need reemission.
370 auto L
= llvm::GlobalValue::ExternalLinkage
;
371 if (llvm::isa
<FunctionDecl
>(GD
.getDecl()))
372 L
= getFunctionLinkage(GD
);
373 else if (auto *VD
= llvm::dyn_cast
<VarDecl
>(GD
.getDecl()))
374 L
= getLLVMLinkageVarDefinition(VD
);
376 if (llvm::GlobalValue::isInternalLinkage(L
) ||
377 llvm::GlobalValue::isLinkOnceLinkage(L
) ||
378 llvm::GlobalValue::isWeakLinkage(L
)) {
379 EmittedDeferredDecls
[getMangledName(GD
)] = GD
;
383 /// List of alias we have emitted. Used to make sure that what they point to
384 /// is defined once we get to the end of the of the translation unit.
385 std::vector
<GlobalDecl
> Aliases
;
387 /// List of multiversion functions to be emitted. This list is processed in
388 /// conjunction with other deferred symbols and is used to ensure that
389 /// multiversion function resolvers and ifuncs are defined and emitted.
390 std::vector
<GlobalDecl
> MultiVersionFuncs
;
392 llvm::MapVector
<StringRef
, llvm::TrackingVH
<llvm::Constant
>> Replacements
;
394 /// List of global values to be replaced with something else. Used when we
395 /// want to replace a GlobalValue but can't identify it by its mangled name
396 /// anymore (because the name is already taken).
397 llvm::SmallVector
<std::pair
<llvm::GlobalValue
*, llvm::Constant
*>, 8>
398 GlobalValReplacements
;
400 /// Variables for which we've emitted globals containing their constant
401 /// values along with the corresponding globals, for opportunistic reuse.
402 llvm::DenseMap
<const VarDecl
*, llvm::GlobalVariable
*> InitializerConstants
;
404 /// Set of global decls for which we already diagnosed mangled name conflict.
405 /// Required to not issue a warning (on a mangling conflict) multiple times
406 /// for the same decl.
407 llvm::DenseSet
<GlobalDecl
> DiagnosedConflictingDefinitions
;
409 /// A queue of (optional) vtables to consider emitting.
410 std::vector
<const CXXRecordDecl
*> DeferredVTables
;
412 /// A queue of (optional) vtables that may be emitted opportunistically.
413 std::vector
<const CXXRecordDecl
*> OpportunisticVTables
;
415 /// List of global values which are required to be present in the object file;
416 /// bitcast to i8*. This is used for forcing visibility of symbols which may
417 /// otherwise be optimized out.
418 std::vector
<llvm::WeakTrackingVH
> LLVMUsed
;
419 std::vector
<llvm::WeakTrackingVH
> LLVMCompilerUsed
;
421 /// Store the list of global constructors and their respective priorities to
422 /// be emitted when the translation unit is complete.
423 CtorList GlobalCtors
;
425 /// Store the list of global destructors and their respective priorities to be
426 /// emitted when the translation unit is complete.
427 CtorList GlobalDtors
;
429 /// An ordered map of canonical GlobalDecls to their mangled names.
430 llvm::MapVector
<GlobalDecl
, StringRef
> MangledDeclNames
;
431 llvm::StringMap
<GlobalDecl
, llvm::BumpPtrAllocator
> Manglings
;
433 /// Global annotations.
434 std::vector
<llvm::Constant
*> Annotations
;
436 // Store deferred function annotations so they can be emitted at the end with
437 // most up to date ValueDecl that will have all the inherited annotations.
438 llvm::MapVector
<StringRef
, const ValueDecl
*> DeferredAnnotations
;
440 /// Map used to get unique annotation strings.
441 llvm::StringMap
<llvm::Constant
*> AnnotationStrings
;
443 /// Used for uniquing of annotation arguments.
444 llvm::DenseMap
<unsigned, llvm::Constant
*> AnnotationArgs
;
446 llvm::StringMap
<llvm::GlobalVariable
*> CFConstantStringMap
;
448 llvm::DenseMap
<llvm::Constant
*, llvm::GlobalVariable
*> ConstantStringMap
;
449 llvm::DenseMap
<const UnnamedGlobalConstantDecl
*, llvm::GlobalVariable
*>
450 UnnamedGlobalConstantDeclMap
;
451 llvm::DenseMap
<const Decl
*, llvm::Constant
*> StaticLocalDeclMap
;
452 llvm::DenseMap
<const Decl
*, llvm::GlobalVariable
*> StaticLocalDeclGuardMap
;
453 llvm::DenseMap
<const Expr
*, llvm::Constant
*> MaterializedGlobalTemporaryMap
;
455 llvm::DenseMap
<QualType
, llvm::Constant
*> AtomicSetterHelperFnMap
;
456 llvm::DenseMap
<QualType
, llvm::Constant
*> AtomicGetterHelperFnMap
;
458 /// Map used to get unique type descriptor constants for sanitizers.
459 llvm::DenseMap
<QualType
, llvm::Constant
*> TypeDescriptorMap
;
461 /// Map used to track internal linkage functions declared within
462 /// extern "C" regions.
463 typedef llvm::MapVector
<IdentifierInfo
*,
464 llvm::GlobalValue
*> StaticExternCMap
;
465 StaticExternCMap StaticExternCValues
;
467 /// thread_local variables defined or used in this TU.
468 std::vector
<const VarDecl
*> CXXThreadLocals
;
470 /// thread_local variables with initializers that need to run
471 /// before any thread_local variable in this TU is odr-used.
472 std::vector
<llvm::Function
*> CXXThreadLocalInits
;
473 std::vector
<const VarDecl
*> CXXThreadLocalInitVars
;
475 /// Global variables with initializers that need to run before main.
476 std::vector
<llvm::Function
*> CXXGlobalInits
;
478 /// When a C++ decl with an initializer is deferred, null is
479 /// appended to CXXGlobalInits, and the index of that null is placed
480 /// here so that the initializer will be performed in the correct
481 /// order. Once the decl is emitted, the index is replaced with ~0U to ensure
482 /// that we don't re-emit the initializer.
483 llvm::DenseMap
<const Decl
*, unsigned> DelayedCXXInitPosition
;
485 typedef std::pair
<OrderGlobalInitsOrStermFinalizers
, llvm::Function
*>
488 // When a tail call is performed on an "undefined" symbol, on PPC without pc
489 // relative feature, the tail call is not allowed. In "EmitCall" for such
490 // tail calls, the "undefined" symbols may be forward declarations, their
491 // definitions are provided in the module after the callsites. For such tail
492 // calls, diagnose message should not be emitted.
493 llvm::SmallSetVector
<std::pair
<const FunctionDecl
*, SourceLocation
>, 4>
494 MustTailCallUndefinedGlobals
;
496 struct GlobalInitPriorityCmp
{
497 bool operator()(const GlobalInitData
&LHS
,
498 const GlobalInitData
&RHS
) const {
499 return LHS
.first
.priority
< RHS
.first
.priority
;
503 /// Global variables with initializers whose order of initialization is set by
504 /// init_priority attribute.
505 SmallVector
<GlobalInitData
, 8> PrioritizedCXXGlobalInits
;
507 /// Global destructor functions and arguments that need to run on termination.
508 /// When UseSinitAndSterm is set, it instead contains sterm finalizer
509 /// functions, which also run on unloading a shared library.
510 typedef std::tuple
<llvm::FunctionType
*, llvm::WeakTrackingVH
,
512 CXXGlobalDtorsOrStermFinalizer_t
;
513 SmallVector
<CXXGlobalDtorsOrStermFinalizer_t
, 8>
514 CXXGlobalDtorsOrStermFinalizers
;
516 typedef std::pair
<OrderGlobalInitsOrStermFinalizers
, llvm::Function
*>
519 struct StermFinalizerPriorityCmp
{
520 bool operator()(const StermFinalizerData
&LHS
,
521 const StermFinalizerData
&RHS
) const {
522 return LHS
.first
.priority
< RHS
.first
.priority
;
526 /// Global variables with sterm finalizers whose order of initialization is
527 /// set by init_priority attribute.
528 SmallVector
<StermFinalizerData
, 8> PrioritizedCXXStermFinalizers
;
530 /// The complete set of modules that has been imported.
531 llvm::SetVector
<clang::Module
*> ImportedModules
;
533 /// The set of modules for which the module initializers
534 /// have been emitted.
535 llvm::SmallPtrSet
<clang::Module
*, 16> EmittedModuleInitializers
;
537 /// A vector of metadata strings for linker options.
538 SmallVector
<llvm::MDNode
*, 16> LinkerOptionsMetadata
;
540 /// A vector of metadata strings for dependent libraries for ELF.
541 SmallVector
<llvm::MDNode
*, 16> ELFDependentLibraries
;
543 /// @name Cache for Objective-C runtime types
546 /// Cached reference to the class for constant strings. This value has type
547 /// int * but is actually an Obj-C class pointer.
548 llvm::WeakTrackingVH CFConstantStringClassRef
;
550 /// The type used to describe the state of a fast enumeration in
551 /// Objective-C's for..in loop.
552 QualType ObjCFastEnumerationStateType
;
556 /// Lazily create the Objective-C runtime
557 void createObjCRuntime();
559 void createOpenCLRuntime();
560 void createOpenMPRuntime();
561 void createCUDARuntime();
562 void createHLSLRuntime();
564 bool isTriviallyRecursive(const FunctionDecl
*F
);
565 bool shouldEmitFunction(GlobalDecl GD
);
566 // Whether a global variable should be emitted by CUDA/HIP host/device
567 // related attributes.
568 bool shouldEmitCUDAGlobalVar(const VarDecl
*VD
) const;
569 bool shouldOpportunisticallyEmitVTables();
570 /// Map used to be sure we don't emit the same CompoundLiteral twice.
571 llvm::DenseMap
<const CompoundLiteralExpr
*, llvm::GlobalVariable
*>
572 EmittedCompoundLiterals
;
574 /// Map of the global blocks we've emitted, so that we don't have to re-emit
575 /// them if the constexpr evaluator gets aggressive.
576 llvm::DenseMap
<const BlockExpr
*, llvm::Constant
*> EmittedGlobalBlocks
;
578 /// @name Cache for Blocks Runtime Globals
581 llvm::Constant
*NSConcreteGlobalBlock
= nullptr;
582 llvm::Constant
*NSConcreteStackBlock
= nullptr;
584 llvm::FunctionCallee BlockObjectAssign
= nullptr;
585 llvm::FunctionCallee BlockObjectDispose
= nullptr;
587 llvm::Type
*BlockDescriptorType
= nullptr;
588 llvm::Type
*GenericBlockLiteralType
= nullptr;
591 int GlobalUniqueCount
;
594 GlobalDecl initializedGlobalDecl
;
598 /// void @llvm.lifetime.start(i64 %size, i8* nocapture <ptr>)
599 llvm::Function
*LifetimeStartFn
= nullptr;
601 /// void @llvm.lifetime.end(i64 %size, i8* nocapture <ptr>)
602 llvm::Function
*LifetimeEndFn
= nullptr;
604 std::unique_ptr
<SanitizerMetadata
> SanitizerMD
;
606 llvm::MapVector
<const Decl
*, bool> DeferredEmptyCoverageMappingDecls
;
608 std::unique_ptr
<CoverageMappingModuleGen
> CoverageMapping
;
610 /// Mapping from canonical types to their metadata identifiers. We need to
611 /// maintain this mapping because identifiers may be formed from distinct
613 typedef llvm::DenseMap
<QualType
, llvm::Metadata
*> MetadataTypeMap
;
614 MetadataTypeMap MetadataIdMap
;
615 MetadataTypeMap VirtualMetadataIdMap
;
616 MetadataTypeMap GeneralizedMetadataIdMap
;
618 // Helps squashing blocks of TopLevelStmtDecl into a single llvm::Function
619 // when used with -fincremental-extensions.
620 std::pair
<std::unique_ptr
<CodeGenFunction
>, const TopLevelStmtDecl
*>
621 GlobalTopLevelStmtBlockInFlight
;
623 llvm::DenseMap
<GlobalDecl
, uint16_t> PtrAuthDiscriminatorHashes
;
625 llvm::DenseMap
<const CXXRecordDecl
*, std::optional
<PointerAuthQualifier
>>
627 std::optional
<PointerAuthQualifier
>
628 computeVTPointerAuthentication(const CXXRecordDecl
*ThisClass
);
631 CodeGenModule(ASTContext
&C
, IntrusiveRefCntPtr
<llvm::vfs::FileSystem
> FS
,
632 const HeaderSearchOptions
&headersearchopts
,
633 const PreprocessorOptions
&ppopts
,
634 const CodeGenOptions
&CodeGenOpts
, llvm::Module
&M
,
635 DiagnosticsEngine
&Diags
,
636 CoverageSourceInfo
*CoverageInfo
= nullptr);
642 /// Finalize LLVM code generation.
645 /// Return true if we should emit location information for expressions.
646 bool getExpressionLocationsEnabled() const;
648 /// Return a reference to the configured Objective-C runtime.
649 CGObjCRuntime
&getObjCRuntime() {
650 if (!ObjCRuntime
) createObjCRuntime();
654 /// Return true iff an Objective-C runtime has been configured.
655 bool hasObjCRuntime() { return !!ObjCRuntime
; }
657 const std::string
&getModuleNameHash() const { return ModuleNameHash
; }
659 /// Return a reference to the configured OpenCL runtime.
660 CGOpenCLRuntime
&getOpenCLRuntime() {
661 assert(OpenCLRuntime
!= nullptr);
662 return *OpenCLRuntime
;
665 /// Return a reference to the configured OpenMP runtime.
666 CGOpenMPRuntime
&getOpenMPRuntime() {
667 assert(OpenMPRuntime
!= nullptr);
668 return *OpenMPRuntime
;
671 /// Return a reference to the configured CUDA runtime.
672 CGCUDARuntime
&getCUDARuntime() {
673 assert(CUDARuntime
!= nullptr);
677 /// Return a reference to the configured HLSL runtime.
678 CGHLSLRuntime
&getHLSLRuntime() {
679 assert(HLSLRuntime
!= nullptr);
683 ObjCEntrypoints
&getObjCEntrypoints() const {
684 assert(ObjCData
!= nullptr);
688 // Version checking functions, used to implement ObjC's @available:
689 // i32 @__isOSVersionAtLeast(i32, i32, i32)
690 llvm::FunctionCallee IsOSVersionAtLeastFn
= nullptr;
691 // i32 @__isPlatformVersionAtLeast(i32, i32, i32, i32)
692 llvm::FunctionCallee IsPlatformVersionAtLeastFn
= nullptr;
694 InstrProfStats
&getPGOStats() { return PGOStats
; }
695 llvm::IndexedInstrProfReader
*getPGOReader() const { return PGOReader
.get(); }
697 CoverageMappingModuleGen
*getCoverageMapping() const {
698 return CoverageMapping
.get();
701 llvm::Constant
*getStaticLocalDeclAddress(const VarDecl
*D
) {
702 return StaticLocalDeclMap
[D
];
704 void setStaticLocalDeclAddress(const VarDecl
*D
,
706 StaticLocalDeclMap
[D
] = C
;
710 getOrCreateStaticVarDecl(const VarDecl
&D
,
711 llvm::GlobalValue::LinkageTypes Linkage
);
713 llvm::GlobalVariable
*getStaticLocalDeclGuardAddress(const VarDecl
*D
) {
714 return StaticLocalDeclGuardMap
[D
];
716 void setStaticLocalDeclGuardAddress(const VarDecl
*D
,
717 llvm::GlobalVariable
*C
) {
718 StaticLocalDeclGuardMap
[D
] = C
;
721 Address
createUnnamedGlobalFrom(const VarDecl
&D
, llvm::Constant
*Constant
,
724 bool lookupRepresentativeDecl(StringRef MangledName
,
725 GlobalDecl
&Result
) const;
727 llvm::Constant
*getAtomicSetterHelperFnMap(QualType Ty
) {
728 return AtomicSetterHelperFnMap
[Ty
];
730 void setAtomicSetterHelperFnMap(QualType Ty
,
731 llvm::Constant
*Fn
) {
732 AtomicSetterHelperFnMap
[Ty
] = Fn
;
735 llvm::Constant
*getAtomicGetterHelperFnMap(QualType Ty
) {
736 return AtomicGetterHelperFnMap
[Ty
];
738 void setAtomicGetterHelperFnMap(QualType Ty
,
739 llvm::Constant
*Fn
) {
740 AtomicGetterHelperFnMap
[Ty
] = Fn
;
743 llvm::Constant
*getTypeDescriptorFromMap(QualType Ty
) {
744 return TypeDescriptorMap
[Ty
];
746 void setTypeDescriptorInMap(QualType Ty
, llvm::Constant
*C
) {
747 TypeDescriptorMap
[Ty
] = C
;
750 CGDebugInfo
*getModuleDebugInfo() { return DebugInfo
.get(); }
752 llvm::MDNode
*getNoObjCARCExceptionsMetadata() {
753 if (!NoObjCARCExceptionsMetadata
)
754 NoObjCARCExceptionsMetadata
=
755 llvm::MDNode::get(getLLVMContext(), std::nullopt
);
756 return NoObjCARCExceptionsMetadata
;
759 ASTContext
&getContext() const { return Context
; }
760 const LangOptions
&getLangOpts() const { return LangOpts
; }
761 const IntrusiveRefCntPtr
<llvm::vfs::FileSystem
> &getFileSystem() const {
764 const HeaderSearchOptions
&getHeaderSearchOpts()
765 const { return HeaderSearchOpts
; }
766 const PreprocessorOptions
&getPreprocessorOpts()
767 const { return PreprocessorOpts
; }
768 const CodeGenOptions
&getCodeGenOpts() const { return CodeGenOpts
; }
769 llvm::Module
&getModule() const { return TheModule
; }
770 DiagnosticsEngine
&getDiags() const { return Diags
; }
771 const llvm::DataLayout
&getDataLayout() const {
772 return TheModule
.getDataLayout();
774 const TargetInfo
&getTarget() const { return Target
; }
775 const llvm::Triple
&getTriple() const { return Target
.getTriple(); }
776 bool supportsCOMDAT() const;
777 void maybeSetTrivialComdat(const Decl
&D
, llvm::GlobalObject
&GO
);
779 const ABIInfo
&getABIInfo();
780 CGCXXABI
&getCXXABI() const { return *ABI
; }
781 llvm::LLVMContext
&getLLVMContext() { return VMContext
; }
783 bool shouldUseTBAA() const { return TBAA
!= nullptr; }
785 const TargetCodeGenInfo
&getTargetCodeGenInfo();
787 CodeGenTypes
&getTypes() { return *Types
; }
789 CodeGenVTables
&getVTables() { return VTables
; }
791 ItaniumVTableContext
&getItaniumVTableContext() {
792 return VTables
.getItaniumVTableContext();
795 const ItaniumVTableContext
&getItaniumVTableContext() const {
796 return VTables
.getItaniumVTableContext();
799 MicrosoftVTableContext
&getMicrosoftVTableContext() {
800 return VTables
.getMicrosoftVTableContext();
803 CtorList
&getGlobalCtors() { return GlobalCtors
; }
804 CtorList
&getGlobalDtors() { return GlobalDtors
; }
806 /// getTBAATypeInfo - Get metadata used to describe accesses to objects of
808 llvm::MDNode
*getTBAATypeInfo(QualType QTy
);
810 /// getTBAAAccessInfo - Get TBAA information that describes an access to
811 /// an object of the given type.
812 TBAAAccessInfo
getTBAAAccessInfo(QualType AccessType
);
814 /// getTBAAVTablePtrAccessInfo - Get the TBAA information that describes an
815 /// access to a virtual table pointer.
816 TBAAAccessInfo
getTBAAVTablePtrAccessInfo(llvm::Type
*VTablePtrType
);
818 llvm::MDNode
*getTBAAStructInfo(QualType QTy
);
820 /// getTBAABaseTypeInfo - Get metadata that describes the given base access
821 /// type. Return null if the type is not suitable for use in TBAA access tags.
822 llvm::MDNode
*getTBAABaseTypeInfo(QualType QTy
);
824 /// getTBAAAccessTagInfo - Get TBAA tag for a given memory access.
825 llvm::MDNode
*getTBAAAccessTagInfo(TBAAAccessInfo Info
);
827 /// mergeTBAAInfoForCast - Get merged TBAA information for the purposes of
829 TBAAAccessInfo
mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo
,
830 TBAAAccessInfo TargetInfo
);
832 /// mergeTBAAInfoForConditionalOperator - Get merged TBAA information for the
833 /// purposes of conditional operator.
834 TBAAAccessInfo
mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA
,
835 TBAAAccessInfo InfoB
);
837 /// mergeTBAAInfoForMemoryTransfer - Get merged TBAA information for the
838 /// purposes of memory transfer calls.
839 TBAAAccessInfo
mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo
,
840 TBAAAccessInfo SrcInfo
);
842 /// getTBAAInfoForSubobject - Get TBAA information for an access with a given
844 TBAAAccessInfo
getTBAAInfoForSubobject(LValue Base
, QualType AccessType
) {
845 if (Base
.getTBAAInfo().isMayAlias())
846 return TBAAAccessInfo::getMayAliasInfo();
847 return getTBAAAccessInfo(AccessType
);
850 bool isPaddedAtomicType(QualType type
);
851 bool isPaddedAtomicType(const AtomicType
*type
);
853 /// DecorateInstructionWithTBAA - Decorate the instruction with a TBAA tag.
854 void DecorateInstructionWithTBAA(llvm::Instruction
*Inst
,
855 TBAAAccessInfo TBAAInfo
);
857 /// Adds !invariant.barrier !tag to instruction
858 void DecorateInstructionWithInvariantGroup(llvm::Instruction
*I
,
859 const CXXRecordDecl
*RD
);
861 /// Emit the given number of characters as a value of type size_t.
862 llvm::ConstantInt
*getSize(CharUnits numChars
);
864 /// Set the visibility for the given LLVM GlobalValue.
865 void setGlobalVisibility(llvm::GlobalValue
*GV
, const NamedDecl
*D
) const;
867 void setDSOLocal(llvm::GlobalValue
*GV
) const;
869 bool shouldMapVisibilityToDLLExport(const NamedDecl
*D
) const {
870 return getLangOpts().hasDefaultVisibilityExportMapping() && D
&&
871 (D
->getLinkageAndVisibility().getVisibility() ==
872 DefaultVisibility
) &&
873 (getLangOpts().isAllDefaultVisibilityExportMapping() ||
874 (getLangOpts().isExplicitDefaultVisibilityExportMapping() &&
875 D
->getLinkageAndVisibility().isVisibilityExplicit()));
877 void setDLLImportDLLExport(llvm::GlobalValue
*GV
, GlobalDecl D
) const;
878 void setDLLImportDLLExport(llvm::GlobalValue
*GV
, const NamedDecl
*D
) const;
879 /// Set visibility, dllimport/dllexport and dso_local.
880 /// This must be called after dllimport/dllexport is set.
881 void setGVProperties(llvm::GlobalValue
*GV
, GlobalDecl GD
) const;
882 void setGVProperties(llvm::GlobalValue
*GV
, const NamedDecl
*D
) const;
884 void setGVPropertiesAux(llvm::GlobalValue
*GV
, const NamedDecl
*D
) const;
886 /// Set the TLS mode for the given LLVM GlobalValue for the thread-local
887 /// variable declaration D.
888 void setTLSMode(llvm::GlobalValue
*GV
, const VarDecl
&D
) const;
890 /// Get LLVM TLS mode from CodeGenOptions.
891 llvm::GlobalVariable::ThreadLocalMode
GetDefaultLLVMTLSModel() const;
893 static llvm::GlobalValue::VisibilityTypes
GetLLVMVisibility(Visibility V
) {
895 case DefaultVisibility
: return llvm::GlobalValue::DefaultVisibility
;
896 case HiddenVisibility
: return llvm::GlobalValue::HiddenVisibility
;
897 case ProtectedVisibility
: return llvm::GlobalValue::ProtectedVisibility
;
899 llvm_unreachable("unknown visibility!");
902 llvm::Constant
*GetAddrOfGlobal(GlobalDecl GD
,
903 ForDefinition_t IsForDefinition
906 /// Will return a global variable of the given type. If a variable with a
907 /// different type already exists then a new variable with the right type
908 /// will be created and all uses of the old variable will be replaced with a
909 /// bitcast to the new variable.
910 llvm::GlobalVariable
*
911 CreateOrReplaceCXXRuntimeVariable(StringRef Name
, llvm::Type
*Ty
,
912 llvm::GlobalValue::LinkageTypes Linkage
,
913 llvm::Align Alignment
);
915 llvm::Function
*CreateGlobalInitOrCleanUpFunction(
916 llvm::FunctionType
*ty
, const Twine
&name
, const CGFunctionInfo
&FI
,
917 SourceLocation Loc
= SourceLocation(), bool TLS
= false,
918 llvm::GlobalVariable::LinkageTypes Linkage
=
919 llvm::GlobalVariable::InternalLinkage
);
921 /// Return the AST address space of the underlying global variable for D, as
922 /// determined by its declaration. Normally this is the same as the address
923 /// space of D's type, but in CUDA, address spaces are associated with
924 /// declarations, not types. If D is nullptr, return the default address
925 /// space for global variable.
927 /// For languages without explicit address spaces, if D has default address
928 /// space, target-specific global or constant address space may be returned.
929 LangAS
GetGlobalVarAddressSpace(const VarDecl
*D
);
931 /// Return the AST address space of constant literal, which is used to emit
932 /// the constant literal as global variable in LLVM IR.
933 /// Note: This is not necessarily the address space of the constant literal
934 /// in AST. For address space agnostic language, e.g. C++, constant literal
935 /// in AST is always in default address space.
936 LangAS
GetGlobalConstantAddressSpace() const;
938 /// Return the llvm::Constant for the address of the given global variable.
939 /// If Ty is non-null and if the global doesn't exist, then it will be created
940 /// with the specified type instead of whatever the normal requested type
941 /// would be. If IsForDefinition is true, it is guaranteed that an actual
942 /// global with type Ty will be returned, not conversion of a variable with
943 /// the same mangled name but some other type.
944 llvm::Constant
*GetAddrOfGlobalVar(const VarDecl
*D
,
945 llvm::Type
*Ty
= nullptr,
946 ForDefinition_t IsForDefinition
949 /// Return the address of the given function. If Ty is non-null, then this
950 /// function will use the specified type if it has to create it.
951 llvm::Constant
*GetAddrOfFunction(GlobalDecl GD
, llvm::Type
*Ty
= nullptr,
952 bool ForVTable
= false,
953 bool DontDefer
= false,
954 ForDefinition_t IsForDefinition
957 // Return the function body address of the given function.
958 llvm::Constant
*GetFunctionStart(const ValueDecl
*Decl
);
960 /// Return a function pointer for a reference to the given function.
961 /// This correctly handles weak references, but does not apply a
962 /// pointer signature.
963 llvm::Constant
*getRawFunctionPointer(GlobalDecl GD
,
964 llvm::Type
*Ty
= nullptr);
966 /// Return the ABI-correct function pointer value for a reference
967 /// to the given function. This will apply a pointer signature if
968 /// necessary, caching the result for the given function.
969 llvm::Constant
*getFunctionPointer(GlobalDecl GD
, llvm::Type
*Ty
= nullptr);
971 /// Return the ABI-correct function pointer value for a reference
972 /// to the given function. This will apply a pointer signature if
974 llvm::Constant
*getFunctionPointer(llvm::Constant
*Pointer
,
975 QualType FunctionType
);
977 llvm::Constant
*getMemberFunctionPointer(const FunctionDecl
*FD
,
978 llvm::Type
*Ty
= nullptr);
980 llvm::Constant
*getMemberFunctionPointer(llvm::Constant
*Pointer
,
983 CGPointerAuthInfo
getFunctionPointerAuthInfo(QualType T
);
985 CGPointerAuthInfo
getMemberFunctionPointerAuthInfo(QualType FT
);
987 CGPointerAuthInfo
getPointerAuthInfoForPointeeType(QualType type
);
989 CGPointerAuthInfo
getPointerAuthInfoForType(QualType type
);
991 bool shouldSignPointer(const PointerAuthSchema
&Schema
);
992 llvm::Constant
*getConstantSignedPointer(llvm::Constant
*Pointer
,
993 const PointerAuthSchema
&Schema
,
994 llvm::Constant
*StorageAddress
,
995 GlobalDecl SchemaDecl
,
996 QualType SchemaType
);
999 getConstantSignedPointer(llvm::Constant
*Pointer
, unsigned Key
,
1000 llvm::Constant
*StorageAddress
,
1001 llvm::ConstantInt
*OtherDiscriminator
);
1004 getPointerAuthOtherDiscriminator(const PointerAuthSchema
&Schema
,
1005 GlobalDecl SchemaDecl
, QualType SchemaType
);
1007 uint16_t getPointerAuthDeclDiscriminator(GlobalDecl GD
);
1008 std::optional
<CGPointerAuthInfo
>
1009 getVTablePointerAuthInfo(CodeGenFunction
*Context
,
1010 const CXXRecordDecl
*Record
,
1011 llvm::Value
*StorageAddress
);
1013 std::optional
<PointerAuthQualifier
>
1014 getVTablePointerAuthentication(const CXXRecordDecl
*thisClass
);
1016 CGPointerAuthInfo
EmitPointerAuthInfo(const RecordDecl
*RD
);
1018 // Return whether RTTI information should be emitted for this target.
1019 bool shouldEmitRTTI(bool ForEH
= false) {
1020 return (ForEH
|| getLangOpts().RTTI
) && !getLangOpts().CUDAIsDevice
&&
1021 !(getLangOpts().OpenMP
&& getLangOpts().OpenMPIsTargetDevice
&&
1022 (getTriple().isNVPTX() || getTriple().isAMDGPU()));
1025 /// Get the address of the RTTI descriptor for the given type.
1026 llvm::Constant
*GetAddrOfRTTIDescriptor(QualType Ty
, bool ForEH
= false);
1028 /// Get the address of a GUID.
1029 ConstantAddress
GetAddrOfMSGuidDecl(const MSGuidDecl
*GD
);
1031 /// Get the address of a UnnamedGlobalConstant
1033 GetAddrOfUnnamedGlobalConstantDecl(const UnnamedGlobalConstantDecl
*GCD
);
1035 /// Get the address of a template parameter object.
1037 GetAddrOfTemplateParamObject(const TemplateParamObjectDecl
*TPO
);
1039 /// Get the address of the thunk for the given global decl.
1040 llvm::Constant
*GetAddrOfThunk(StringRef Name
, llvm::Type
*FnTy
,
1043 /// Get a reference to the target of VD.
1044 ConstantAddress
GetWeakRefReference(const ValueDecl
*VD
);
1046 /// Returns the assumed alignment of an opaque pointer to the given class.
1047 CharUnits
getClassPointerAlignment(const CXXRecordDecl
*CD
);
1049 /// Returns the minimum object size for an object of the given class type
1050 /// (or a class derived from it).
1051 CharUnits
getMinimumClassObjectSize(const CXXRecordDecl
*CD
);
1053 /// Returns the minimum object size for an object of the given type.
1054 CharUnits
getMinimumObjectSize(QualType Ty
) {
1055 if (CXXRecordDecl
*RD
= Ty
->getAsCXXRecordDecl())
1056 return getMinimumClassObjectSize(RD
);
1057 return getContext().getTypeSizeInChars(Ty
);
1060 /// Returns the assumed alignment of a virtual base of a class.
1061 CharUnits
getVBaseAlignment(CharUnits DerivedAlign
,
1062 const CXXRecordDecl
*Derived
,
1063 const CXXRecordDecl
*VBase
);
1065 /// Given a class pointer with an actual known alignment, and the
1066 /// expected alignment of an object at a dynamic offset w.r.t that
1067 /// pointer, return the alignment to assume at the offset.
1068 CharUnits
getDynamicOffsetAlignment(CharUnits ActualAlign
,
1069 const CXXRecordDecl
*Class
,
1070 CharUnits ExpectedTargetAlign
);
1073 computeNonVirtualBaseClassOffset(const CXXRecordDecl
*DerivedClass
,
1074 CastExpr::path_const_iterator Start
,
1075 CastExpr::path_const_iterator End
);
1077 /// Returns the offset from a derived class to a class. Returns null if the
1080 GetNonVirtualBaseClassOffset(const CXXRecordDecl
*ClassDecl
,
1081 CastExpr::path_const_iterator PathBegin
,
1082 CastExpr::path_const_iterator PathEnd
);
1084 llvm::FoldingSet
<BlockByrefHelpers
> ByrefHelpersCache
;
1086 /// Fetches the global unique block count.
1087 int getUniqueBlockCount() { return ++Block
.GlobalUniqueCount
; }
1089 /// Fetches the type of a generic block descriptor.
1090 llvm::Type
*getBlockDescriptorType();
1092 /// The type of a generic block literal.
1093 llvm::Type
*getGenericBlockLiteralType();
1095 /// Gets the address of a block which requires no captures.
1096 llvm::Constant
*GetAddrOfGlobalBlock(const BlockExpr
*BE
, StringRef Name
);
1098 /// Returns the address of a block which requires no caputres, or null if
1099 /// we've yet to emit the block for BE.
1100 llvm::Constant
*getAddrOfGlobalBlockIfEmitted(const BlockExpr
*BE
) {
1101 return EmittedGlobalBlocks
.lookup(BE
);
1104 /// Notes that BE's global block is available via Addr. Asserts that BE
1105 /// isn't already emitted.
1106 void setAddrOfGlobalBlock(const BlockExpr
*BE
, llvm::Constant
*Addr
);
1108 /// Return a pointer to a constant CFString object for the given string.
1109 ConstantAddress
GetAddrOfConstantCFString(const StringLiteral
*Literal
);
1111 /// Return a constant array for the given string.
1112 llvm::Constant
*GetConstantArrayFromStringLiteral(const StringLiteral
*E
);
1114 /// Return a pointer to a constant array for the given string literal.
1116 GetAddrOfConstantStringFromLiteral(const StringLiteral
*S
,
1117 StringRef Name
= ".str");
1119 /// Return a pointer to a constant array for the given ObjCEncodeExpr node.
1121 GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr
*);
1123 /// Returns a pointer to a character array containing the literal and a
1124 /// terminating '\0' character. The result has pointer to array type.
1126 /// \param GlobalName If provided, the name to use for the global (if one is
1129 GetAddrOfConstantCString(const std::string
&Str
,
1130 const char *GlobalName
= nullptr);
1132 /// Returns a pointer to a constant global variable for the given file-scope
1133 /// compound literal expression.
1134 ConstantAddress
GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr
*E
);
1136 /// If it's been emitted already, returns the GlobalVariable corresponding to
1137 /// a compound literal. Otherwise, returns null.
1138 llvm::GlobalVariable
*
1139 getAddrOfConstantCompoundLiteralIfEmitted(const CompoundLiteralExpr
*E
);
1141 /// Notes that CLE's GlobalVariable is GV. Asserts that CLE isn't already
1143 void setAddrOfConstantCompoundLiteral(const CompoundLiteralExpr
*CLE
,
1144 llvm::GlobalVariable
*GV
);
1146 /// Returns a pointer to a global variable representing a temporary
1147 /// with static or thread storage duration.
1148 ConstantAddress
GetAddrOfGlobalTemporary(const MaterializeTemporaryExpr
*E
,
1151 /// Retrieve the record type that describes the state of an
1152 /// Objective-C fast enumeration loop (for..in).
1153 QualType
getObjCFastEnumerationStateType();
1155 // Produce code for this constructor/destructor. This method doesn't try
1156 // to apply any ABI rules about which other constructors/destructors
1157 // are needed or if they are alias to each other.
1158 llvm::Function
*codegenCXXStructor(GlobalDecl GD
);
1160 /// Return the address of the constructor/destructor of the given type.
1162 getAddrOfCXXStructor(GlobalDecl GD
, const CGFunctionInfo
*FnInfo
= nullptr,
1163 llvm::FunctionType
*FnType
= nullptr,
1164 bool DontDefer
= false,
1165 ForDefinition_t IsForDefinition
= NotForDefinition
) {
1166 return cast
<llvm::Constant
>(getAddrAndTypeOfCXXStructor(GD
, FnInfo
, FnType
,
1172 llvm::FunctionCallee
getAddrAndTypeOfCXXStructor(
1173 GlobalDecl GD
, const CGFunctionInfo
*FnInfo
= nullptr,
1174 llvm::FunctionType
*FnType
= nullptr, bool DontDefer
= false,
1175 ForDefinition_t IsForDefinition
= NotForDefinition
);
1177 /// Given a builtin id for a function like "__builtin_fabsf", return a
1178 /// Function* for "fabsf".
1179 llvm::Constant
*getBuiltinLibFunction(const FunctionDecl
*FD
,
1180 unsigned BuiltinID
);
1182 llvm::Function
*getIntrinsic(unsigned IID
,
1183 ArrayRef
<llvm::Type
*> Tys
= std::nullopt
);
1185 /// Emit code for a single top level declaration.
1186 void EmitTopLevelDecl(Decl
*D
);
1188 /// Stored a deferred empty coverage mapping for an unused
1189 /// and thus uninstrumented top level declaration.
1190 void AddDeferredUnusedCoverageMapping(Decl
*D
);
1192 /// Remove the deferred empty coverage mapping as this
1193 /// declaration is actually instrumented.
1194 void ClearUnusedCoverageMapping(const Decl
*D
);
1196 /// Emit all the deferred coverage mappings
1197 /// for the uninstrumented functions.
1198 void EmitDeferredUnusedCoverageMappings();
1200 /// Emit an alias for "main" if it has no arguments (needed for wasm).
1201 void EmitMainVoidAlias();
1203 /// Tell the consumer that this variable has been instantiated.
1204 void HandleCXXStaticMemberVarInstantiation(VarDecl
*VD
);
1206 /// If the declaration has internal linkage but is inside an
1207 /// extern "C" linkage specification, prepare to emit an alias for it
1208 /// to the expected name.
1209 template<typename SomeDecl
>
1210 void MaybeHandleStaticInExternC(const SomeDecl
*D
, llvm::GlobalValue
*GV
);
1212 /// Add a global to a list to be added to the llvm.used metadata.
1213 void addUsedGlobal(llvm::GlobalValue
*GV
);
1215 /// Add a global to a list to be added to the llvm.compiler.used metadata.
1216 void addCompilerUsedGlobal(llvm::GlobalValue
*GV
);
1218 /// Add a global to a list to be added to the llvm.compiler.used metadata.
1219 void addUsedOrCompilerUsedGlobal(llvm::GlobalValue
*GV
);
1221 /// Add a destructor and object to add to the C++ global destructor function.
1222 void AddCXXDtorEntry(llvm::FunctionCallee DtorFn
, llvm::Constant
*Object
) {
1223 CXXGlobalDtorsOrStermFinalizers
.emplace_back(DtorFn
.getFunctionType(),
1224 DtorFn
.getCallee(), Object
);
1227 /// Add an sterm finalizer to the C++ global cleanup function.
1228 void AddCXXStermFinalizerEntry(llvm::FunctionCallee DtorFn
) {
1229 CXXGlobalDtorsOrStermFinalizers
.emplace_back(DtorFn
.getFunctionType(),
1230 DtorFn
.getCallee(), nullptr);
1233 /// Add an sterm finalizer to its own llvm.global_dtors entry.
1234 void AddCXXStermFinalizerToGlobalDtor(llvm::Function
*StermFinalizer
,
1236 AddGlobalDtor(StermFinalizer
, Priority
);
1239 void AddCXXPrioritizedStermFinalizerEntry(llvm::Function
*StermFinalizer
,
1241 OrderGlobalInitsOrStermFinalizers
Key(Priority
,
1242 PrioritizedCXXStermFinalizers
.size());
1243 PrioritizedCXXStermFinalizers
.push_back(
1244 std::make_pair(Key
, StermFinalizer
));
1247 /// Create or return a runtime function declaration with the specified type
1248 /// and name. If \p AssumeConvergent is true, the call will have the
1249 /// convergent attribute added.
1250 llvm::FunctionCallee
1251 CreateRuntimeFunction(llvm::FunctionType
*Ty
, StringRef Name
,
1252 llvm::AttributeList ExtraAttrs
= llvm::AttributeList(),
1253 bool Local
= false, bool AssumeConvergent
= false);
1255 /// Create a new runtime global variable with the specified type and name.
1256 llvm::Constant
*CreateRuntimeVariable(llvm::Type
*Ty
,
1259 ///@name Custom Blocks Runtime Interfaces
1262 llvm::Constant
*getNSConcreteGlobalBlock();
1263 llvm::Constant
*getNSConcreteStackBlock();
1264 llvm::FunctionCallee
getBlockObjectAssign();
1265 llvm::FunctionCallee
getBlockObjectDispose();
1269 llvm::Function
*getLLVMLifetimeStartFn();
1270 llvm::Function
*getLLVMLifetimeEndFn();
1272 // Make sure that this type is translated.
1273 void UpdateCompletedType(const TagDecl
*TD
);
1275 llvm::Constant
*getMemberPointerConstant(const UnaryOperator
*e
);
1277 /// Emit type info if type of an expression is a variably modified
1278 /// type. Also emit proper debug info for cast types.
1279 void EmitExplicitCastExprType(const ExplicitCastExpr
*E
,
1280 CodeGenFunction
*CGF
= nullptr);
1282 /// Return the result of value-initializing the given type, i.e. a null
1283 /// expression of the given type. This is usually, but not always, an LLVM
1285 llvm::Constant
*EmitNullConstant(QualType T
);
1287 /// Return a null constant appropriate for zero-initializing a base class with
1288 /// the given type. This is usually, but not always, an LLVM null constant.
1289 llvm::Constant
*EmitNullConstantForBase(const CXXRecordDecl
*Record
);
1291 /// Emit a general error that something can't be done.
1292 void Error(SourceLocation loc
, StringRef error
);
1294 /// Print out an error that codegen doesn't support the specified stmt yet.
1295 void ErrorUnsupported(const Stmt
*S
, const char *Type
);
1297 /// Print out an error that codegen doesn't support the specified decl yet.
1298 void ErrorUnsupported(const Decl
*D
, const char *Type
);
1300 /// Set the attributes on the LLVM function for the given decl and function
1301 /// info. This applies attributes necessary for handling the ABI as well as
1302 /// user specified attributes like section.
1303 void SetInternalFunctionAttributes(GlobalDecl GD
, llvm::Function
*F
,
1304 const CGFunctionInfo
&FI
);
1306 /// Set the LLVM function attributes (sext, zext, etc).
1307 void SetLLVMFunctionAttributes(GlobalDecl GD
, const CGFunctionInfo
&Info
,
1308 llvm::Function
*F
, bool IsThunk
);
1310 /// Set the LLVM function attributes which only apply to a function
1312 void SetLLVMFunctionAttributesForDefinition(const Decl
*D
, llvm::Function
*F
);
1314 /// Set the LLVM function attributes that represent floating point
1316 void setLLVMFunctionFEnvAttributes(const FunctionDecl
*D
, llvm::Function
*F
);
1318 /// Return true iff the given type uses 'sret' when used as a return type.
1319 bool ReturnTypeUsesSRet(const CGFunctionInfo
&FI
);
1321 /// Return true iff the given type has `inreg` set.
1322 bool ReturnTypeHasInReg(const CGFunctionInfo
&FI
);
1324 /// Return true iff the given type uses an argument slot when 'sret' is used
1325 /// as a return type.
1326 bool ReturnSlotInterferesWithArgs(const CGFunctionInfo
&FI
);
1328 /// Return true iff the given type uses 'fpret' when used as a return type.
1329 bool ReturnTypeUsesFPRet(QualType ResultType
);
1331 /// Return true iff the given type uses 'fp2ret' when used as a return type.
1332 bool ReturnTypeUsesFP2Ret(QualType ResultType
);
1334 /// Get the LLVM attributes and calling convention to use for a particular
1337 /// \param Name - The function name.
1338 /// \param Info - The function type information.
1339 /// \param CalleeInfo - The callee information these attributes are being
1340 /// constructed for. If valid, the attributes applied to this decl may
1341 /// contribute to the function attributes and calling convention.
1342 /// \param Attrs [out] - On return, the attribute list to use.
1343 /// \param CallingConv [out] - On return, the LLVM calling convention to use.
1344 void ConstructAttributeList(StringRef Name
, const CGFunctionInfo
&Info
,
1345 CGCalleeInfo CalleeInfo
,
1346 llvm::AttributeList
&Attrs
, unsigned &CallingConv
,
1347 bool AttrOnCallSite
, bool IsThunk
);
1349 /// Adjust Memory attribute to ensure that the BE gets the right attribute
1350 // in order to generate the library call or the intrinsic for the function
1352 void AdjustMemoryAttribute(StringRef Name
, CGCalleeInfo CalleeInfo
,
1353 llvm::AttributeList
&Attrs
);
1355 /// Like the overload taking a `Function &`, but intended specifically
1356 /// for frontends that want to build on Clang's target-configuration logic.
1357 void addDefaultFunctionDefinitionAttributes(llvm::AttrBuilder
&attrs
);
1359 StringRef
getMangledName(GlobalDecl GD
);
1360 StringRef
getBlockMangledName(GlobalDecl GD
, const BlockDecl
*BD
);
1361 const GlobalDecl
getMangledNameDecl(StringRef
);
1363 void EmitTentativeDefinition(const VarDecl
*D
);
1365 void EmitExternalDeclaration(const DeclaratorDecl
*D
);
1367 void EmitVTable(CXXRecordDecl
*Class
);
1369 void RefreshTypeCacheForClass(const CXXRecordDecl
*Class
);
1371 /// Appends Opts to the "llvm.linker.options" metadata value.
1372 void AppendLinkerOptions(StringRef Opts
);
1374 /// Appends a detect mismatch command to the linker options.
1375 void AddDetectMismatch(StringRef Name
, StringRef Value
);
1377 /// Appends a dependent lib to the appropriate metadata value.
1378 void AddDependentLib(StringRef Lib
);
1381 llvm::GlobalVariable::LinkageTypes
getFunctionLinkage(GlobalDecl GD
);
1383 void setFunctionLinkage(GlobalDecl GD
, llvm::Function
*F
) {
1384 F
->setLinkage(getFunctionLinkage(GD
));
1387 /// Return the appropriate linkage for the vtable, VTT, and type information
1388 /// of the given class.
1389 llvm::GlobalVariable::LinkageTypes
getVTableLinkage(const CXXRecordDecl
*RD
);
1391 /// Return the store size, in character units, of the given LLVM type.
1392 CharUnits
GetTargetTypeStoreSize(llvm::Type
*Ty
) const;
1394 /// Returns LLVM linkage for a declarator.
1395 llvm::GlobalValue::LinkageTypes
1396 getLLVMLinkageForDeclarator(const DeclaratorDecl
*D
, GVALinkage Linkage
);
1398 /// Returns LLVM linkage for a declarator.
1399 llvm::GlobalValue::LinkageTypes
1400 getLLVMLinkageVarDefinition(const VarDecl
*VD
);
1402 /// Emit all the global annotations.
1403 void EmitGlobalAnnotations();
1405 /// Emit an annotation string.
1406 llvm::Constant
*EmitAnnotationString(StringRef Str
);
1408 /// Emit the annotation's translation unit.
1409 llvm::Constant
*EmitAnnotationUnit(SourceLocation Loc
);
1411 /// Emit the annotation line number.
1412 llvm::Constant
*EmitAnnotationLineNo(SourceLocation L
);
1414 /// Emit additional args of the annotation.
1415 llvm::Constant
*EmitAnnotationArgs(const AnnotateAttr
*Attr
);
1417 /// Generate the llvm::ConstantStruct which contains the annotation
1418 /// information for a given GlobalValue. The annotation struct is
1419 /// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the
1420 /// GlobalValue being annotated. The second field is the constant string
1421 /// created from the AnnotateAttr's annotation. The third field is a constant
1422 /// string containing the name of the translation unit. The fourth field is
1423 /// the line number in the file of the annotated value declaration.
1424 llvm::Constant
*EmitAnnotateAttr(llvm::GlobalValue
*GV
,
1425 const AnnotateAttr
*AA
,
1428 /// Add global annotations that are set on D, for the global GV. Those
1429 /// annotations are emitted during finalization of the LLVM code.
1430 void AddGlobalAnnotations(const ValueDecl
*D
, llvm::GlobalValue
*GV
);
1432 bool isInNoSanitizeList(SanitizerMask Kind
, llvm::Function
*Fn
,
1433 SourceLocation Loc
) const;
1435 bool isInNoSanitizeList(SanitizerMask Kind
, llvm::GlobalVariable
*GV
,
1436 SourceLocation Loc
, QualType Ty
,
1437 StringRef Category
= StringRef()) const;
1439 /// Imbue XRay attributes to a function, applying the always/never attribute
1440 /// lists in the process. Returns true if we did imbue attributes this way,
1441 /// false otherwise.
1442 bool imbueXRayAttrs(llvm::Function
*Fn
, SourceLocation Loc
,
1443 StringRef Category
= StringRef()) const;
1445 /// \returns true if \p Fn at \p Loc should be excluded from profile
1446 /// instrumentation by the SCL passed by \p -fprofile-list.
1447 ProfileList::ExclusionType
1448 isFunctionBlockedByProfileList(llvm::Function
*Fn
, SourceLocation Loc
) const;
1450 /// \returns true if \p Fn at \p Loc should be excluded from profile
1451 /// instrumentation.
1452 ProfileList::ExclusionType
1453 isFunctionBlockedFromProfileInstr(llvm::Function
*Fn
,
1454 SourceLocation Loc
) const;
1456 SanitizerMetadata
*getSanitizerMetadata() {
1457 return SanitizerMD
.get();
1460 void addDeferredVTable(const CXXRecordDecl
*RD
) {
1461 DeferredVTables
.push_back(RD
);
1464 /// Emit code for a single global function or var decl. Forward declarations
1465 /// are emitted lazily.
1466 void EmitGlobal(GlobalDecl D
);
1468 bool TryEmitBaseDestructorAsAlias(const CXXDestructorDecl
*D
);
1470 llvm::GlobalValue
*GetGlobalValue(StringRef Ref
);
1472 /// Set attributes which are common to any form of a global definition (alias,
1473 /// Objective-C method, function, global variable).
1475 /// NOTE: This should only be called for definitions.
1476 void SetCommonAttributes(GlobalDecl GD
, llvm::GlobalValue
*GV
);
1478 void addReplacement(StringRef Name
, llvm::Constant
*C
);
1480 void addGlobalValReplacement(llvm::GlobalValue
*GV
, llvm::Constant
*C
);
1482 /// Emit a code for threadprivate directive.
1483 /// \param D Threadprivate declaration.
1484 void EmitOMPThreadPrivateDecl(const OMPThreadPrivateDecl
*D
);
1486 /// Emit a code for declare reduction construct.
1487 void EmitOMPDeclareReduction(const OMPDeclareReductionDecl
*D
,
1488 CodeGenFunction
*CGF
= nullptr);
1490 /// Emit a code for declare mapper construct.
1491 void EmitOMPDeclareMapper(const OMPDeclareMapperDecl
*D
,
1492 CodeGenFunction
*CGF
= nullptr);
1494 /// Emit a code for requires directive.
1495 /// \param D Requires declaration
1496 void EmitOMPRequiresDecl(const OMPRequiresDecl
*D
);
1498 /// Emit a code for the allocate directive.
1499 /// \param D The allocate declaration
1500 void EmitOMPAllocateDecl(const OMPAllocateDecl
*D
);
1502 /// Return the alignment specified in an allocate directive, if present.
1503 std::optional
<CharUnits
> getOMPAllocateAlignment(const VarDecl
*VD
);
1505 /// Returns whether the given record has hidden LTO visibility and therefore
1506 /// may participate in (single-module) CFI and whole-program vtable
1508 bool HasHiddenLTOVisibility(const CXXRecordDecl
*RD
);
1510 /// Returns whether the given record has public LTO visibility (regardless of
1511 /// -lto-whole-program-visibility) and therefore may not participate in
1512 /// (single-module) CFI and whole-program vtable optimization.
1513 bool AlwaysHasLTOVisibilityPublic(const CXXRecordDecl
*RD
);
1515 /// Returns the vcall visibility of the given type. This is the scope in which
1516 /// a virtual function call could be made which ends up being dispatched to a
1517 /// member function of this class. This scope can be wider than the visibility
1518 /// of the class itself when the class has a more-visible dynamic base class.
1519 /// The client should pass in an empty Visited set, which is used to prevent
1520 /// redundant recursive processing.
1521 llvm::GlobalObject::VCallVisibility
1522 GetVCallVisibilityLevel(const CXXRecordDecl
*RD
,
1523 llvm::DenseSet
<const CXXRecordDecl
*> &Visited
);
1525 /// Emit type metadata for the given vtable using the given layout.
1526 void EmitVTableTypeMetadata(const CXXRecordDecl
*RD
,
1527 llvm::GlobalVariable
*VTable
,
1528 const VTableLayout
&VTLayout
);
1530 llvm::Type
*getVTableComponentType() const;
1532 /// Generate a cross-DSO type identifier for MD.
1533 llvm::ConstantInt
*CreateCrossDsoCfiTypeId(llvm::Metadata
*MD
);
1535 /// Generate a KCFI type identifier for T.
1536 llvm::ConstantInt
*CreateKCFITypeId(QualType T
);
1538 /// Create a metadata identifier for the given type. This may either be an
1539 /// MDString (for external identifiers) or a distinct unnamed MDNode (for
1540 /// internal identifiers).
1541 llvm::Metadata
*CreateMetadataIdentifierForType(QualType T
);
1543 /// Create a metadata identifier that is intended to be used to check virtual
1544 /// calls via a member function pointer.
1545 llvm::Metadata
*CreateMetadataIdentifierForVirtualMemPtrType(QualType T
);
1547 /// Create a metadata identifier for the generalization of the given type.
1548 /// This may either be an MDString (for external identifiers) or a distinct
1549 /// unnamed MDNode (for internal identifiers).
1550 llvm::Metadata
*CreateMetadataIdentifierGeneralized(QualType T
);
1552 /// Create and attach type metadata to the given function.
1553 void CreateFunctionTypeMetadataForIcall(const FunctionDecl
*FD
,
1556 /// Set type metadata to the given function.
1557 void setKCFIType(const FunctionDecl
*FD
, llvm::Function
*F
);
1559 /// Emit KCFI type identifier constants and remove unused identifiers.
1560 void finalizeKCFITypes();
1562 /// Whether this function's return type has no side effects, and thus may
1563 /// be trivially discarded if it is unused.
1564 bool MayDropFunctionReturn(const ASTContext
&Context
,
1565 QualType ReturnType
) const;
1567 /// Returns whether this module needs the "all-vtables" type identifier.
1568 bool NeedAllVtablesTypeId() const;
1570 /// Create and attach type metadata for the given vtable.
1571 void AddVTableTypeMetadata(llvm::GlobalVariable
*VTable
, CharUnits Offset
,
1572 const CXXRecordDecl
*RD
);
1574 /// Return a vector of most-base classes for RD. This is used to implement
1575 /// control flow integrity checks for member function pointers.
1577 /// A most-base class of a class C is defined as a recursive base class of C,
1578 /// including C itself, that does not have any bases.
1579 SmallVector
<const CXXRecordDecl
*, 0>
1580 getMostBaseClasses(const CXXRecordDecl
*RD
);
1582 /// Get the declaration of std::terminate for the platform.
1583 llvm::FunctionCallee
getTerminateFn();
1585 llvm::SanitizerStatReport
&getSanStats();
1588 createOpenCLIntToSamplerConversion(const Expr
*E
, CodeGenFunction
&CGF
);
1590 /// OpenCL v1.2 s5.6.4.6 allows the compiler to store kernel argument
1591 /// information in the program executable. The argument information stored
1592 /// includes the argument name, its type, the address and access qualifiers
1593 /// used. This helper can be used to generate metadata for source code kernel
1594 /// function as well as generated implicitly kernels. If a kernel is generated
1595 /// implicitly null value has to be passed to the last two parameters,
1596 /// otherwise all parameters must have valid non-null values.
1597 /// \param FN is a pointer to IR function being generated.
1598 /// \param FD is a pointer to function declaration if any.
1599 /// \param CGF is a pointer to CodeGenFunction that generates this function.
1600 void GenKernelArgMetadata(llvm::Function
*FN
,
1601 const FunctionDecl
*FD
= nullptr,
1602 CodeGenFunction
*CGF
= nullptr);
1604 /// Get target specific null pointer.
1605 /// \param T is the LLVM type of the null pointer.
1606 /// \param QT is the clang QualType of the null pointer.
1607 llvm::Constant
*getNullPointer(llvm::PointerType
*T
, QualType QT
);
1609 CharUnits
getNaturalTypeAlignment(QualType T
,
1610 LValueBaseInfo
*BaseInfo
= nullptr,
1611 TBAAAccessInfo
*TBAAInfo
= nullptr,
1612 bool forPointeeType
= false);
1613 CharUnits
getNaturalPointeeTypeAlignment(QualType T
,
1614 LValueBaseInfo
*BaseInfo
= nullptr,
1615 TBAAAccessInfo
*TBAAInfo
= nullptr);
1616 bool stopAutoInit();
1618 /// Print the postfix for externalized static variable or kernels for single
1619 /// source offloading languages CUDA and HIP. The unique postfix is created
1620 /// using either the CUID argument, or the file's UniqueID and active macros.
1621 /// The fallback method without a CUID requires that the offloading toolchain
1622 /// does not define separate macros via the -cc1 options.
1623 void printPostfixForExternalizedDecl(llvm::raw_ostream
&OS
,
1624 const Decl
*D
) const;
1626 /// Move some lazily-emitted states to the NewBuilder. This is especially
1627 /// essential for the incremental parsing environment like Clang Interpreter,
1628 /// because we'll lose all important information after each repl.
1629 void moveLazyEmissionStates(CodeGenModule
*NewBuilder
);
1631 /// Emit the IR encoding to attach the CUDA launch bounds attribute to \p F.
1632 /// If \p MaxThreadsVal is not nullptr, the max threads value is stored in it,
1633 /// if a valid one was found.
1634 void handleCUDALaunchBoundsAttr(llvm::Function
*F
,
1635 const CUDALaunchBoundsAttr
*A
,
1636 int32_t *MaxThreadsVal
= nullptr,
1637 int32_t *MinBlocksVal
= nullptr,
1638 int32_t *MaxClusterRankVal
= nullptr);
1640 /// Emit the IR encoding to attach the AMD GPU flat-work-group-size attribute
1641 /// to \p F. Alternatively, the work group size can be taken from a \p
1642 /// ReqdWGS. If \p MinThreadsVal is not nullptr, the min threads value is
1643 /// stored in it, if a valid one was found. If \p MaxThreadsVal is not
1644 /// nullptr, the max threads value is stored in it, if a valid one was found.
1645 void handleAMDGPUFlatWorkGroupSizeAttr(
1646 llvm::Function
*F
, const AMDGPUFlatWorkGroupSizeAttr
*A
,
1647 const ReqdWorkGroupSizeAttr
*ReqdWGS
= nullptr,
1648 int32_t *MinThreadsVal
= nullptr, int32_t *MaxThreadsVal
= nullptr);
1650 /// Emit the IR encoding to attach the AMD GPU waves-per-eu attribute to \p F.
1651 void handleAMDGPUWavesPerEUAttr(llvm::Function
*F
,
1652 const AMDGPUWavesPerEUAttr
*A
);
1655 GetOrCreateLLVMGlobal(StringRef MangledName
, llvm::Type
*Ty
, LangAS AddrSpace
,
1657 ForDefinition_t IsForDefinition
= NotForDefinition
);
1659 // FIXME: Hardcoding priority here is gross.
1660 void AddGlobalCtor(llvm::Function
*Ctor
, int Priority
= 65535,
1661 unsigned LexOrder
= ~0U,
1662 llvm::Constant
*AssociatedData
= nullptr);
1663 void AddGlobalDtor(llvm::Function
*Dtor
, int Priority
= 65535,
1664 bool IsDtorAttrFunc
= false);
1666 // Return whether structured convergence intrinsics should be generated for
1668 bool shouldEmitConvergenceTokens() const {
1669 // TODO: this should probably become unconditional once the controlled
1670 // convergence becomes the norm.
1671 return getTriple().isSPIRVLogical();
1674 void addUndefinedGlobalForTailCall(
1675 std::pair
<const FunctionDecl
*, SourceLocation
> Global
) {
1676 MustTailCallUndefinedGlobals
.insert(Global
);
1680 bool shouldDropDLLAttribute(const Decl
*D
, const llvm::GlobalValue
*GV
) const;
1682 llvm::Constant
*GetOrCreateLLVMFunction(
1683 StringRef MangledName
, llvm::Type
*Ty
, GlobalDecl D
, bool ForVTable
,
1684 bool DontDefer
= false, bool IsThunk
= false,
1685 llvm::AttributeList ExtraAttrs
= llvm::AttributeList(),
1686 ForDefinition_t IsForDefinition
= NotForDefinition
);
1688 // Adds a declaration to the list of multi version functions if not present.
1689 void AddDeferredMultiVersionResolverToEmit(GlobalDecl GD
);
1691 // References to multiversion functions are resolved through an implicitly
1692 // defined resolver function. This function is responsible for creating
1693 // the resolver symbol for the provided declaration. The value returned
1694 // will be for an ifunc (llvm::GlobalIFunc) if the current target supports
1695 // that feature and for a regular function (llvm::GlobalValue) otherwise.
1696 llvm::Constant
*GetOrCreateMultiVersionResolver(GlobalDecl GD
);
1698 // In scenarios where a function is not known to be a multiversion function
1699 // until a later declaration, it is sometimes necessary to change the
1700 // previously created mangled name to align with requirements of whatever
1701 // multiversion function kind the function is now known to be. This function
1702 // is responsible for performing such mangled name updates.
1703 void UpdateMultiVersionNames(GlobalDecl GD
, const FunctionDecl
*FD
,
1704 StringRef
&CurName
);
1706 bool GetCPUAndFeaturesAttributes(GlobalDecl GD
,
1707 llvm::AttrBuilder
&AttrBuilder
,
1708 bool SetTargetFeatures
= true);
1709 void setNonAliasAttributes(GlobalDecl GD
, llvm::GlobalObject
*GO
);
1711 /// Set function attributes for a function declaration.
1712 void SetFunctionAttributes(GlobalDecl GD
, llvm::Function
*F
,
1713 bool IsIncompleteFunction
, bool IsThunk
);
1715 void EmitGlobalDefinition(GlobalDecl D
, llvm::GlobalValue
*GV
= nullptr);
1717 void EmitGlobalFunctionDefinition(GlobalDecl GD
, llvm::GlobalValue
*GV
);
1718 void EmitMultiVersionFunctionDefinition(GlobalDecl GD
, llvm::GlobalValue
*GV
);
1720 void EmitGlobalVarDefinition(const VarDecl
*D
, bool IsTentative
= false);
1721 void EmitExternalVarDeclaration(const VarDecl
*D
);
1722 void EmitExternalFunctionDeclaration(const FunctionDecl
*D
);
1723 void EmitAliasDefinition(GlobalDecl GD
);
1724 void emitIFuncDefinition(GlobalDecl GD
);
1725 void emitCPUDispatchDefinition(GlobalDecl GD
);
1726 void EmitObjCPropertyImplementations(const ObjCImplementationDecl
*D
);
1727 void EmitObjCIvarInitializations(ObjCImplementationDecl
*D
);
1729 // C++ related functions.
1731 void EmitDeclContext(const DeclContext
*DC
);
1732 void EmitLinkageSpec(const LinkageSpecDecl
*D
);
1733 void EmitTopLevelStmt(const TopLevelStmtDecl
*D
);
1735 /// Emit the function that initializes C++ thread_local variables.
1736 void EmitCXXThreadLocalInitFunc();
1738 /// Emit the function that initializes global variables for a C++ Module.
1739 void EmitCXXModuleInitFunc(clang::Module
*Primary
);
1741 /// Emit the function that initializes C++ globals.
1742 void EmitCXXGlobalInitFunc();
1744 /// Emit the function that performs cleanup associated with C++ globals.
1745 void EmitCXXGlobalCleanUpFunc();
1747 /// Emit the function that initializes the specified global (if PerformInit is
1748 /// true) and registers its destructor.
1749 void EmitCXXGlobalVarDeclInitFunc(const VarDecl
*D
,
1750 llvm::GlobalVariable
*Addr
,
1753 void EmitPointerToInitFunc(const VarDecl
*VD
, llvm::GlobalVariable
*Addr
,
1754 llvm::Function
*InitFunc
, InitSegAttr
*ISA
);
1756 /// EmitCtorList - Generates a global array of functions and priorities using
1757 /// the given list and name. This array will have appending linkage and is
1758 /// suitable for use as a LLVM constructor or destructor array. Clears Fns.
1759 void EmitCtorList(CtorList
&Fns
, const char *GlobalName
);
1761 /// Emit any needed decls for which code generation was deferred.
1762 void EmitDeferred();
1764 /// Try to emit external vtables as available_externally if they have emitted
1765 /// all inlined virtual functions. It runs after EmitDeferred() and therefore
1766 /// is not allowed to create new references to things that need to be emitted
1768 void EmitVTablesOpportunistically();
1770 /// Call replaceAllUsesWith on all pairs in Replacements.
1771 void applyReplacements();
1773 /// Call replaceAllUsesWith on all pairs in GlobalValReplacements.
1774 void applyGlobalValReplacements();
1776 void checkAliases();
1778 std::map
<int, llvm::TinyPtrVector
<llvm::Function
*>> DtorsUsingAtExit
;
1780 /// Register functions annotated with __attribute__((destructor)) using
1781 /// __cxa_atexit, if it is available, or atexit otherwise.
1782 void registerGlobalDtorsWithAtExit();
1784 // When using sinit and sterm functions, unregister
1785 // __attribute__((destructor)) annotated functions which were previously
1786 // registered by the atexit subroutine using unatexit.
1787 void unregisterGlobalDtorsWithUnAtExit();
1789 /// Emit deferred multiversion function resolvers and associated variants.
1790 void emitMultiVersionFunctions();
1792 /// Emit any vtables which we deferred and still have a use for.
1793 void EmitDeferredVTables();
1795 /// Emit a dummy function that reference a CoreFoundation symbol when
1796 /// @available is used on Darwin.
1797 void emitAtAvailableLinkGuard();
1799 /// Emit the llvm.used and llvm.compiler.used metadata.
1800 void emitLLVMUsed();
1802 /// For C++20 Itanium ABI, emit the initializers for the module.
1803 void EmitModuleInitializers(clang::Module
*Primary
);
1805 /// Emit the link options introduced by imported modules.
1806 void EmitModuleLinkOptions();
1808 /// Helper function for EmitStaticExternCAliases() to redirect ifuncs that
1809 /// have a resolver name that matches 'Elem' to instead resolve to the name of
1810 /// 'CppFunc'. This redirection is necessary in cases where 'Elem' has a name
1811 /// that will be emitted as an alias of the name bound to 'CppFunc'; ifuncs
1812 /// may not reference aliases. Redirection is only performed if 'Elem' is only
1813 /// used by ifuncs in which case, 'Elem' is destroyed. 'true' is returned if
1814 /// redirection is successful, and 'false' is returned otherwise.
1815 bool CheckAndReplaceExternCIFuncs(llvm::GlobalValue
*Elem
,
1816 llvm::GlobalValue
*CppFunc
);
1818 /// Emit aliases for internal-linkage declarations inside "C" language
1819 /// linkage specifications, giving them the "expected" name where possible.
1820 void EmitStaticExternCAliases();
1822 void EmitDeclMetadata();
1824 /// Emit the Clang version as llvm.ident metadata.
1825 void EmitVersionIdentMetadata();
1827 /// Emit the Clang commandline as llvm.commandline metadata.
1828 void EmitCommandLineMetadata();
1830 /// Emit the module flag metadata used to pass options controlling the
1831 /// the backend to LLVM.
1832 void EmitBackendOptionsMetadata(const CodeGenOptions
&CodeGenOpts
);
1834 /// Emits OpenCL specific Metadata e.g. OpenCL version.
1835 void EmitOpenCLMetadata();
1837 /// Emit the llvm.gcov metadata used to tell LLVM where to emit the .gcno and
1838 /// .gcda files in a way that persists in .bc files.
1839 void EmitCoverageFile();
1841 /// Determine whether the definition must be emitted; if this returns \c
1842 /// false, the definition can be emitted lazily if it's used.
1843 bool MustBeEmitted(const ValueDecl
*D
);
1845 /// Determine whether the definition can be emitted eagerly, or should be
1846 /// delayed until the end of the translation unit. This is relevant for
1847 /// definitions whose linkage can change, e.g. implicit function instantions
1848 /// which may later be explicitly instantiated.
1849 bool MayBeEmittedEagerly(const ValueDecl
*D
);
1851 /// Check whether we can use a "simpler", more core exceptions personality
1853 void SimplifyPersonality();
1855 /// Helper function for getDefaultFunctionAttributes. Builds a set of function
1856 /// attributes which can be simply added to a function.
1857 void getTrivialDefaultFunctionAttributes(StringRef Name
, bool HasOptnone
,
1858 bool AttrOnCallSite
,
1859 llvm::AttrBuilder
&FuncAttrs
);
1861 /// Helper function for ConstructAttributeList and
1862 /// addDefaultFunctionDefinitionAttributes. Builds a set of function
1863 /// attributes to add to a function with the given properties.
1864 void getDefaultFunctionAttributes(StringRef Name
, bool HasOptnone
,
1865 bool AttrOnCallSite
,
1866 llvm::AttrBuilder
&FuncAttrs
);
1868 llvm::Metadata
*CreateMetadataIdentifierImpl(QualType T
, MetadataTypeMap
&Map
,
1872 } // end namespace CodeGen
1873 } // end namespace clang
1875 #endif // LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H