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

source code of clang/lib/CodeGen/CodeGenModule.h