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