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