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