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