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