1 //===--- CodeGenModule.h - Per-Module state for LLVM CodeGen ----*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This is the internal per-translation-unit state used for llvm translation. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H 15 #define LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H 16 17 #include "CGVTables.h" 18 #include "CodeGenTypeCache.h" 19 #include "CodeGenTypes.h" 20 #include "SanitizerMetadata.h" 21 #include "clang/AST/Attr.h" 22 #include "clang/AST/DeclCXX.h" 23 #include "clang/AST/DeclObjC.h" 24 #include "clang/AST/GlobalDecl.h" 25 #include "clang/AST/Mangle.h" 26 #include "clang/Basic/ABI.h" 27 #include "clang/Basic/LangOptions.h" 28 #include "clang/Basic/Module.h" 29 #include "clang/Basic/SanitizerBlacklist.h" 30 #include "llvm/ADT/DenseMap.h" 31 #include "llvm/ADT/SetVector.h" 32 #include "llvm/ADT/SmallPtrSet.h" 33 #include "llvm/ADT/StringMap.h" 34 #include "llvm/IR/Module.h" 35 #include "llvm/IR/ValueHandle.h" 36 37 namespace llvm { 38 class Module; 39 class Constant; 40 class ConstantInt; 41 class Function; 42 class GlobalValue; 43 class DataLayout; 44 class FunctionType; 45 class LLVMContext; 46 class IndexedInstrProfReader; 47 } 48 49 namespace clang { 50 class TargetCodeGenInfo; 51 class ASTContext; 52 class AtomicType; 53 class FunctionDecl; 54 class IdentifierInfo; 55 class ObjCMethodDecl; 56 class ObjCImplementationDecl; 57 class ObjCCategoryImplDecl; 58 class ObjCProtocolDecl; 59 class ObjCEncodeExpr; 60 class BlockExpr; 61 class CharUnits; 62 class Decl; 63 class Expr; 64 class Stmt; 65 class InitListExpr; 66 class StringLiteral; 67 class NamedDecl; 68 class ValueDecl; 69 class VarDecl; 70 class LangOptions; 71 class CodeGenOptions; 72 class HeaderSearchOptions; 73 class PreprocessorOptions; 74 class DiagnosticsEngine; 75 class AnnotateAttr; 76 class CXXDestructorDecl; 77 class Module; 78 class CoverageSourceInfo; 79 80 namespace CodeGen { 81 82 class CallArgList; 83 class CodeGenFunction; 84 class CodeGenTBAA; 85 class CGCXXABI; 86 class CGDebugInfo; 87 class CGObjCRuntime; 88 class CGOpenCLRuntime; 89 class CGOpenMPRuntime; 90 class CGCUDARuntime; 91 class BlockFieldFlags; 92 class FunctionArgList; 93 class CoverageMappingModuleGen; 94 95 struct OrderGlobalInits { 96 unsigned int priority; 97 unsigned int lex_order; 98 OrderGlobalInits(unsigned int p, unsigned int l) 99 : priority(p), lex_order(l) {} 100 101 bool operator==(const OrderGlobalInits &RHS) const { 102 return priority == RHS.priority && lex_order == RHS.lex_order; 103 } 104 105 bool operator<(const OrderGlobalInits &RHS) const { 106 return std::tie(priority, lex_order) < 107 std::tie(RHS.priority, RHS.lex_order); 108 } 109 }; 110 111 struct RREntrypoints { 112 RREntrypoints() { memset(this, 0, sizeof(*this)); } 113 /// void objc_autoreleasePoolPop(void*); 114 llvm::Constant *objc_autoreleasePoolPop; 115 116 /// void *objc_autoreleasePoolPush(void); 117 llvm::Constant *objc_autoreleasePoolPush; 118 }; 119 120 struct ARCEntrypoints { 121 ARCEntrypoints() { memset(this, 0, sizeof(*this)); } 122 123 /// id objc_autorelease(id); 124 llvm::Constant *objc_autorelease; 125 126 /// id objc_autoreleaseReturnValue(id); 127 llvm::Constant *objc_autoreleaseReturnValue; 128 129 /// void objc_copyWeak(id *dest, id *src); 130 llvm::Constant *objc_copyWeak; 131 132 /// void objc_destroyWeak(id*); 133 llvm::Constant *objc_destroyWeak; 134 135 /// id objc_initWeak(id*, id); 136 llvm::Constant *objc_initWeak; 137 138 /// id objc_loadWeak(id*); 139 llvm::Constant *objc_loadWeak; 140 141 /// id objc_loadWeakRetained(id*); 142 llvm::Constant *objc_loadWeakRetained; 143 144 /// void objc_moveWeak(id *dest, id *src); 145 llvm::Constant *objc_moveWeak; 146 147 /// id objc_retain(id); 148 llvm::Constant *objc_retain; 149 150 /// id objc_retainAutorelease(id); 151 llvm::Constant *objc_retainAutorelease; 152 153 /// id objc_retainAutoreleaseReturnValue(id); 154 llvm::Constant *objc_retainAutoreleaseReturnValue; 155 156 /// id objc_retainAutoreleasedReturnValue(id); 157 llvm::Constant *objc_retainAutoreleasedReturnValue; 158 159 /// id objc_retainBlock(id); 160 llvm::Constant *objc_retainBlock; 161 162 /// void objc_release(id); 163 llvm::Constant *objc_release; 164 165 /// id objc_storeStrong(id*, id); 166 llvm::Constant *objc_storeStrong; 167 168 /// id objc_storeWeak(id*, id); 169 llvm::Constant *objc_storeWeak; 170 171 /// A void(void) inline asm to use to mark that the return value of 172 /// a call will be immediately retain. 173 llvm::InlineAsm *retainAutoreleasedReturnValueMarker; 174 175 /// void clang.arc.use(...); 176 llvm::Constant *clang_arc_use; 177 }; 178 179 /// This class records statistics on instrumentation based profiling. 180 class InstrProfStats { 181 uint32_t VisitedInMainFile; 182 uint32_t MissingInMainFile; 183 uint32_t Visited; 184 uint32_t Missing; 185 uint32_t Mismatched; 186 187 public: 188 InstrProfStats() 189 : VisitedInMainFile(0), MissingInMainFile(0), Visited(0), Missing(0), 190 Mismatched(0) {} 191 /// Record that we've visited a function and whether or not that function was 192 /// in the main source file. 193 void addVisited(bool MainFile) { 194 if (MainFile) 195 ++VisitedInMainFile; 196 ++Visited; 197 } 198 /// Record that a function we've visited has no profile data. 199 void addMissing(bool MainFile) { 200 if (MainFile) 201 ++MissingInMainFile; 202 ++Missing; 203 } 204 /// Record that a function we've visited has mismatched profile data. 205 void addMismatched(bool MainFile) { ++Mismatched; } 206 /// Whether or not the stats we've gathered indicate any potential problems. 207 bool hasDiagnostics() { return Missing || Mismatched; } 208 /// Report potential problems we've found to \c Diags. 209 void reportDiagnostics(DiagnosticsEngine &Diags, StringRef MainFile); 210 }; 211 212 /// A pair of helper functions for a __block variable. 213 class BlockByrefHelpers : public llvm::FoldingSetNode { 214 // MSVC requires this type to be complete in order to process this 215 // header. 216 public: 217 llvm::Constant *CopyHelper; 218 llvm::Constant *DisposeHelper; 219 220 /// The alignment of the field. This is important because 221 /// different offsets to the field within the byref struct need to 222 /// have different helper functions. 223 CharUnits Alignment; 224 225 BlockByrefHelpers(CharUnits alignment) : Alignment(alignment) {} 226 BlockByrefHelpers(const BlockByrefHelpers &) = default; 227 virtual ~BlockByrefHelpers(); 228 229 void Profile(llvm::FoldingSetNodeID &id) const { 230 id.AddInteger(Alignment.getQuantity()); 231 profileImpl(id); 232 } 233 virtual void profileImpl(llvm::FoldingSetNodeID &id) const = 0; 234 235 virtual bool needsCopy() const { return true; } 236 virtual void emitCopy(CodeGenFunction &CGF, Address dest, Address src) = 0; 237 238 virtual bool needsDispose() const { return true; } 239 virtual void emitDispose(CodeGenFunction &CGF, Address field) = 0; 240 }; 241 242 /// This class organizes the cross-function state that is used while generating 243 /// LLVM code. 244 class CodeGenModule : public CodeGenTypeCache { 245 CodeGenModule(const CodeGenModule &) = delete; 246 void operator=(const CodeGenModule &) = delete; 247 248 public: 249 struct Structor { 250 Structor() : Priority(0), Initializer(nullptr), AssociatedData(nullptr) {} 251 Structor(int Priority, llvm::Constant *Initializer, 252 llvm::Constant *AssociatedData) 253 : Priority(Priority), Initializer(Initializer), 254 AssociatedData(AssociatedData) {} 255 int Priority; 256 llvm::Constant *Initializer; 257 llvm::Constant *AssociatedData; 258 }; 259 260 typedef std::vector<Structor> CtorList; 261 262 private: 263 ASTContext &Context; 264 const LangOptions &LangOpts; 265 const HeaderSearchOptions &HeaderSearchOpts; // Only used for debug info. 266 const PreprocessorOptions &PreprocessorOpts; // Only used for debug info. 267 const CodeGenOptions &CodeGenOpts; 268 llvm::Module &TheModule; 269 DiagnosticsEngine &Diags; 270 const TargetInfo &Target; 271 std::unique_ptr<CGCXXABI> ABI; 272 llvm::LLVMContext &VMContext; 273 274 CodeGenTBAA *TBAA; 275 276 mutable const TargetCodeGenInfo *TheTargetCodeGenInfo; 277 278 // This should not be moved earlier, since its initialization depends on some 279 // of the previous reference members being already initialized and also checks 280 // if TheTargetCodeGenInfo is NULL 281 CodeGenTypes Types; 282 283 /// Holds information about C++ vtables. 284 CodeGenVTables VTables; 285 286 CGObjCRuntime* ObjCRuntime; 287 CGOpenCLRuntime* OpenCLRuntime; 288 CGOpenMPRuntime* OpenMPRuntime; 289 CGCUDARuntime* CUDARuntime; 290 CGDebugInfo* DebugInfo; 291 ARCEntrypoints *ARCData; 292 llvm::MDNode *NoObjCARCExceptionsMetadata; 293 RREntrypoints *RRData; 294 std::unique_ptr<llvm::IndexedInstrProfReader> PGOReader; 295 InstrProfStats PGOStats; 296 297 // A set of references that have only been seen via a weakref so far. This is 298 // used to remove the weak of the reference if we ever see a direct reference 299 // or a definition. 300 llvm::SmallPtrSet<llvm::GlobalValue*, 10> WeakRefReferences; 301 302 /// This contains all the decls which have definitions but/ which are deferred 303 /// for emission and therefore should only be output if they are actually 304 /// used. If a decl is in this, then it is known to have not been referenced 305 /// yet. 306 std::map<StringRef, GlobalDecl> DeferredDecls; 307 308 /// This is a list of deferred decls which we have seen that *are* actually 309 /// referenced. These get code generated when the module is done. 310 struct DeferredGlobal { 311 DeferredGlobal(llvm::GlobalValue *GV, GlobalDecl GD) : GV(GV), GD(GD) {} 312 llvm::TrackingVH<llvm::GlobalValue> GV; 313 GlobalDecl GD; 314 }; 315 std::vector<DeferredGlobal> DeferredDeclsToEmit; 316 void addDeferredDeclToEmit(llvm::GlobalValue *GV, GlobalDecl GD) { 317 DeferredDeclsToEmit.emplace_back(GV, GD); 318 } 319 320 /// List of alias we have emitted. Used to make sure that what they point to 321 /// is defined once we get to the end of the of the translation unit. 322 std::vector<GlobalDecl> Aliases; 323 324 typedef llvm::StringMap<llvm::TrackingVH<llvm::Constant> > ReplacementsTy; 325 ReplacementsTy Replacements; 326 327 /// List of global values to be replaced with something else. Used when we 328 /// want to replace a GlobalValue but can't identify it by its mangled name 329 /// anymore (because the name is already taken). 330 llvm::SmallVector<std::pair<llvm::GlobalValue *, llvm::Constant *>, 8> 331 GlobalValReplacements; 332 333 /// Set of global decls for which we already diagnosed mangled name conflict. 334 /// Required to not issue a warning (on a mangling conflict) multiple times 335 /// for the same decl. 336 llvm::DenseSet<GlobalDecl> DiagnosedConflictingDefinitions; 337 338 /// A queue of (optional) vtables to consider emitting. 339 std::vector<const CXXRecordDecl*> DeferredVTables; 340 341 /// List of global values which are required to be present in the object file; 342 /// bitcast to i8*. This is used for forcing visibility of symbols which may 343 /// otherwise be optimized out. 344 std::vector<llvm::WeakVH> LLVMUsed; 345 std::vector<llvm::WeakVH> LLVMCompilerUsed; 346 347 /// Store the list of global constructors and their respective priorities to 348 /// be emitted when the translation unit is complete. 349 CtorList GlobalCtors; 350 351 /// Store the list of global destructors and their respective priorities to be 352 /// emitted when the translation unit is complete. 353 CtorList GlobalDtors; 354 355 /// An ordered map of canonical GlobalDecls to their mangled names. 356 llvm::MapVector<GlobalDecl, StringRef> MangledDeclNames; 357 llvm::StringMap<GlobalDecl, llvm::BumpPtrAllocator> Manglings; 358 359 /// Global annotations. 360 std::vector<llvm::Constant*> Annotations; 361 362 /// Map used to get unique annotation strings. 363 llvm::StringMap<llvm::Constant*> AnnotationStrings; 364 365 llvm::StringMap<llvm::GlobalVariable *> CFConstantStringMap; 366 367 llvm::DenseMap<llvm::Constant *, llvm::GlobalVariable *> ConstantStringMap; 368 llvm::DenseMap<const Decl*, llvm::Constant *> StaticLocalDeclMap; 369 llvm::DenseMap<const Decl*, llvm::GlobalVariable*> StaticLocalDeclGuardMap; 370 llvm::DenseMap<const Expr*, llvm::Constant *> MaterializedGlobalTemporaryMap; 371 372 llvm::DenseMap<QualType, llvm::Constant *> AtomicSetterHelperFnMap; 373 llvm::DenseMap<QualType, llvm::Constant *> AtomicGetterHelperFnMap; 374 375 /// Map used to get unique type descriptor constants for sanitizers. 376 llvm::DenseMap<QualType, llvm::Constant *> TypeDescriptorMap; 377 378 /// Map used to track internal linkage functions declared within 379 /// extern "C" regions. 380 typedef llvm::MapVector<IdentifierInfo *, 381 llvm::GlobalValue *> StaticExternCMap; 382 StaticExternCMap StaticExternCValues; 383 384 /// \brief thread_local variables defined or used in this TU. 385 std::vector<std::pair<const VarDecl *, llvm::GlobalVariable *> > 386 CXXThreadLocals; 387 388 /// \brief thread_local variables with initializers that need to run 389 /// before any thread_local variable in this TU is odr-used. 390 std::vector<llvm::Function *> CXXThreadLocalInits; 391 std::vector<llvm::GlobalVariable *> CXXThreadLocalInitVars; 392 393 /// Global variables with initializers that need to run before main. 394 std::vector<llvm::Function *> CXXGlobalInits; 395 396 /// When a C++ decl with an initializer is deferred, null is 397 /// appended to CXXGlobalInits, and the index of that null is placed 398 /// here so that the initializer will be performed in the correct 399 /// order. Once the decl is emitted, the index is replaced with ~0U to ensure 400 /// that we don't re-emit the initializer. 401 llvm::DenseMap<const Decl*, unsigned> DelayedCXXInitPosition; 402 403 typedef std::pair<OrderGlobalInits, llvm::Function*> GlobalInitData; 404 405 struct GlobalInitPriorityCmp { 406 bool operator()(const GlobalInitData &LHS, 407 const GlobalInitData &RHS) const { 408 return LHS.first.priority < RHS.first.priority; 409 } 410 }; 411 412 /// Global variables with initializers whose order of initialization is set by 413 /// init_priority attribute. 414 SmallVector<GlobalInitData, 8> PrioritizedCXXGlobalInits; 415 416 /// Global destructor functions and arguments that need to run on termination. 417 std::vector<std::pair<llvm::WeakVH,llvm::Constant*> > CXXGlobalDtors; 418 419 /// \brief The complete set of modules that has been imported. 420 llvm::SetVector<clang::Module *> ImportedModules; 421 422 /// \brief A vector of metadata strings. 423 SmallVector<llvm::Metadata *, 16> LinkerOptionsMetadata; 424 425 /// @name Cache for Objective-C runtime types 426 /// @{ 427 428 /// Cached reference to the class for constant strings. This value has type 429 /// int * but is actually an Obj-C class pointer. 430 llvm::WeakVH CFConstantStringClassRef; 431 432 /// Cached reference to the class for constant strings. This value has type 433 /// int * but is actually an Obj-C class pointer. 434 llvm::WeakVH ConstantStringClassRef; 435 436 /// \brief The LLVM type corresponding to NSConstantString. 437 llvm::StructType *NSConstantStringType; 438 439 /// \brief The type used to describe the state of a fast enumeration in 440 /// Objective-C's for..in loop. 441 QualType ObjCFastEnumerationStateType; 442 443 /// @} 444 445 /// Lazily create the Objective-C runtime 446 void createObjCRuntime(); 447 448 void createOpenCLRuntime(); 449 void createOpenMPRuntime(); 450 void createCUDARuntime(); 451 452 bool isTriviallyRecursive(const FunctionDecl *F); 453 bool shouldEmitFunction(GlobalDecl GD); 454 455 /// @name Cache for Blocks Runtime Globals 456 /// @{ 457 458 llvm::Constant *NSConcreteGlobalBlock; 459 llvm::Constant *NSConcreteStackBlock; 460 461 llvm::Constant *BlockObjectAssign; 462 llvm::Constant *BlockObjectDispose; 463 464 llvm::Type *BlockDescriptorType; 465 llvm::Type *GenericBlockLiteralType; 466 467 struct { 468 int GlobalUniqueCount; 469 } Block; 470 471 /// void @llvm.lifetime.start(i64 %size, i8* nocapture <ptr>) 472 llvm::Constant *LifetimeStartFn; 473 474 /// void @llvm.lifetime.end(i64 %size, i8* nocapture <ptr>) 475 llvm::Constant *LifetimeEndFn; 476 477 GlobalDecl initializedGlobalDecl; 478 479 std::unique_ptr<SanitizerMetadata> SanitizerMD; 480 481 /// @} 482 483 llvm::DenseMap<const Decl *, bool> DeferredEmptyCoverageMappingDecls; 484 485 std::unique_ptr<CoverageMappingModuleGen> CoverageMapping; 486 487 /// Mapping from canonical types to their metadata identifiers. We need to 488 /// maintain this mapping because identifiers may be formed from distinct 489 /// MDNodes. 490 llvm::DenseMap<QualType, llvm::Metadata *> MetadataIdMap; 491 492 public: 493 CodeGenModule(ASTContext &C, const HeaderSearchOptions &headersearchopts, 494 const PreprocessorOptions &ppopts, 495 const CodeGenOptions &CodeGenOpts, llvm::Module &M, 496 DiagnosticsEngine &Diags, 497 CoverageSourceInfo *CoverageInfo = nullptr); 498 499 ~CodeGenModule(); 500 501 void clear(); 502 503 /// Finalize LLVM code generation. 504 void Release(); 505 506 /// Return a reference to the configured Objective-C runtime. 507 CGObjCRuntime &getObjCRuntime() { 508 if (!ObjCRuntime) createObjCRuntime(); 509 return *ObjCRuntime; 510 } 511 512 /// Return true iff an Objective-C runtime has been configured. 513 bool hasObjCRuntime() { return !!ObjCRuntime; } 514 515 /// Return a reference to the configured OpenCL runtime. 516 CGOpenCLRuntime &getOpenCLRuntime() { 517 assert(OpenCLRuntime != nullptr); 518 return *OpenCLRuntime; 519 } 520 521 /// Return a reference to the configured OpenMP runtime. 522 CGOpenMPRuntime &getOpenMPRuntime() { 523 assert(OpenMPRuntime != nullptr); 524 return *OpenMPRuntime; 525 } 526 527 /// Return a reference to the configured CUDA runtime. 528 CGCUDARuntime &getCUDARuntime() { 529 assert(CUDARuntime != nullptr); 530 return *CUDARuntime; 531 } 532 533 ARCEntrypoints &getARCEntrypoints() const { 534 assert(getLangOpts().ObjCAutoRefCount && ARCData != nullptr); 535 return *ARCData; 536 } 537 538 RREntrypoints &getRREntrypoints() const { 539 assert(RRData != nullptr); 540 return *RRData; 541 } 542 543 InstrProfStats &getPGOStats() { return PGOStats; } 544 llvm::IndexedInstrProfReader *getPGOReader() const { return PGOReader.get(); } 545 546 CoverageMappingModuleGen *getCoverageMapping() const { 547 return CoverageMapping.get(); 548 } 549 550 llvm::Constant *getStaticLocalDeclAddress(const VarDecl *D) { 551 return StaticLocalDeclMap[D]; 552 } 553 void setStaticLocalDeclAddress(const VarDecl *D, 554 llvm::Constant *C) { 555 StaticLocalDeclMap[D] = C; 556 } 557 558 llvm::Constant * 559 getOrCreateStaticVarDecl(const VarDecl &D, 560 llvm::GlobalValue::LinkageTypes Linkage); 561 562 llvm::GlobalVariable *getStaticLocalDeclGuardAddress(const VarDecl *D) { 563 return StaticLocalDeclGuardMap[D]; 564 } 565 void setStaticLocalDeclGuardAddress(const VarDecl *D, 566 llvm::GlobalVariable *C) { 567 StaticLocalDeclGuardMap[D] = C; 568 } 569 570 bool lookupRepresentativeDecl(StringRef MangledName, 571 GlobalDecl &Result) const; 572 573 llvm::Constant *getAtomicSetterHelperFnMap(QualType Ty) { 574 return AtomicSetterHelperFnMap[Ty]; 575 } 576 void setAtomicSetterHelperFnMap(QualType Ty, 577 llvm::Constant *Fn) { 578 AtomicSetterHelperFnMap[Ty] = Fn; 579 } 580 581 llvm::Constant *getAtomicGetterHelperFnMap(QualType Ty) { 582 return AtomicGetterHelperFnMap[Ty]; 583 } 584 void setAtomicGetterHelperFnMap(QualType Ty, 585 llvm::Constant *Fn) { 586 AtomicGetterHelperFnMap[Ty] = Fn; 587 } 588 589 llvm::Constant *getTypeDescriptorFromMap(QualType Ty) { 590 return TypeDescriptorMap[Ty]; 591 } 592 void setTypeDescriptorInMap(QualType Ty, llvm::Constant *C) { 593 TypeDescriptorMap[Ty] = C; 594 } 595 596 CGDebugInfo *getModuleDebugInfo() { return DebugInfo; } 597 598 llvm::MDNode *getNoObjCARCExceptionsMetadata() { 599 if (!NoObjCARCExceptionsMetadata) 600 NoObjCARCExceptionsMetadata = llvm::MDNode::get(getLLVMContext(), None); 601 return NoObjCARCExceptionsMetadata; 602 } 603 604 ASTContext &getContext() const { return Context; } 605 const LangOptions &getLangOpts() const { return LangOpts; } 606 const HeaderSearchOptions &getHeaderSearchOpts() 607 const { return HeaderSearchOpts; } 608 const PreprocessorOptions &getPreprocessorOpts() 609 const { return PreprocessorOpts; } 610 const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; } 611 llvm::Module &getModule() const { return TheModule; } 612 DiagnosticsEngine &getDiags() const { return Diags; } 613 const llvm::DataLayout &getDataLayout() const { 614 return TheModule.getDataLayout(); 615 } 616 const TargetInfo &getTarget() const { return Target; } 617 const llvm::Triple &getTriple() const; 618 bool supportsCOMDAT() const; 619 void maybeSetTrivialComdat(const Decl &D, llvm::GlobalObject &GO); 620 621 CGCXXABI &getCXXABI() const { return *ABI; } 622 llvm::LLVMContext &getLLVMContext() { return VMContext; } 623 624 bool shouldUseTBAA() const { return TBAA != nullptr; } 625 626 const TargetCodeGenInfo &getTargetCodeGenInfo(); 627 628 CodeGenTypes &getTypes() { return Types; } 629 630 CodeGenVTables &getVTables() { return VTables; } 631 632 ItaniumVTableContext &getItaniumVTableContext() { 633 return VTables.getItaniumVTableContext(); 634 } 635 636 MicrosoftVTableContext &getMicrosoftVTableContext() { 637 return VTables.getMicrosoftVTableContext(); 638 } 639 640 CtorList &getGlobalCtors() { return GlobalCtors; } 641 CtorList &getGlobalDtors() { return GlobalDtors; } 642 643 llvm::MDNode *getTBAAInfo(QualType QTy); 644 llvm::MDNode *getTBAAInfoForVTablePtr(); 645 llvm::MDNode *getTBAAStructInfo(QualType QTy); 646 /// Return the MDNode in the type DAG for the given struct type. 647 llvm::MDNode *getTBAAStructTypeInfo(QualType QTy); 648 /// Return the path-aware tag for given base type, access node and offset. 649 llvm::MDNode *getTBAAStructTagInfo(QualType BaseTy, llvm::MDNode *AccessN, 650 uint64_t O); 651 652 bool isTypeConstant(QualType QTy, bool ExcludeCtorDtor); 653 654 bool isPaddedAtomicType(QualType type); 655 bool isPaddedAtomicType(const AtomicType *type); 656 657 /// Decorate the instruction with a TBAA tag. For scalar TBAA, the tag 658 /// is the same as the type. For struct-path aware TBAA, the tag 659 /// is different from the type: base type, access type and offset. 660 /// When ConvertTypeToTag is true, we create a tag based on the scalar type. 661 void DecorateInstructionWithTBAA(llvm::Instruction *Inst, 662 llvm::MDNode *TBAAInfo, 663 bool ConvertTypeToTag = true); 664 665 /// Adds !invariant.barrier !tag to instruction 666 void DecorateInstructionWithInvariantGroup(llvm::Instruction *I, 667 const CXXRecordDecl *RD); 668 669 /// Emit the given number of characters as a value of type size_t. 670 llvm::ConstantInt *getSize(CharUnits numChars); 671 672 /// Set the visibility for the given LLVM GlobalValue. 673 void setGlobalVisibility(llvm::GlobalValue *GV, const NamedDecl *D) const; 674 675 /// Set the TLS mode for the given LLVM GlobalValue for the thread-local 676 /// variable declaration D. 677 void setTLSMode(llvm::GlobalValue *GV, const VarDecl &D) const; 678 679 static llvm::GlobalValue::VisibilityTypes GetLLVMVisibility(Visibility V) { 680 switch (V) { 681 case DefaultVisibility: return llvm::GlobalValue::DefaultVisibility; 682 case HiddenVisibility: return llvm::GlobalValue::HiddenVisibility; 683 case ProtectedVisibility: return llvm::GlobalValue::ProtectedVisibility; 684 } 685 llvm_unreachable("unknown visibility!"); 686 } 687 688 llvm::Constant *GetAddrOfGlobal(GlobalDecl GD, bool IsForDefinition = false); 689 690 /// Will return a global variable of the given type. If a variable with a 691 /// different type already exists then a new variable with the right type 692 /// will be created and all uses of the old variable will be replaced with a 693 /// bitcast to the new variable. 694 llvm::GlobalVariable * 695 CreateOrReplaceCXXRuntimeVariable(StringRef Name, llvm::Type *Ty, 696 llvm::GlobalValue::LinkageTypes Linkage); 697 698 llvm::Function * 699 CreateGlobalInitOrDestructFunction(llvm::FunctionType *ty, const Twine &name, 700 SourceLocation Loc = SourceLocation(), 701 bool TLS = false); 702 703 /// Return the address space of the underlying global variable for D, as 704 /// determined by its declaration. Normally this is the same as the address 705 /// space of D's type, but in CUDA, address spaces are associated with 706 /// declarations, not types. 707 unsigned GetGlobalVarAddressSpace(const VarDecl *D, unsigned AddrSpace); 708 709 /// Return the llvm::Constant for the address of the given global variable. 710 /// If Ty is non-null and if the global doesn't exist, then it will be greated 711 /// with the specified type instead of whatever the normal requested type 712 /// would be. 713 llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D, 714 llvm::Type *Ty = nullptr); 715 716 /// Return the address of the given function. If Ty is non-null, then this 717 /// function will use the specified type if it has to create it. 718 llvm::Constant *GetAddrOfFunction(GlobalDecl GD, llvm::Type *Ty = nullptr, 719 bool ForVTable = false, 720 bool DontDefer = false, 721 bool IsForDefinition = false); 722 723 /// Get the address of the RTTI descriptor for the given type. 724 llvm::Constant *GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH = false); 725 726 /// Get the address of a uuid descriptor . 727 ConstantAddress GetAddrOfUuidDescriptor(const CXXUuidofExpr* E); 728 729 /// Get the address of the thunk for the given global decl. 730 llvm::Constant *GetAddrOfThunk(GlobalDecl GD, const ThunkInfo &Thunk); 731 732 /// Get a reference to the target of VD. 733 ConstantAddress GetWeakRefReference(const ValueDecl *VD); 734 735 /// Returns the assumed alignment of an opaque pointer to the given class. 736 CharUnits getClassPointerAlignment(const CXXRecordDecl *CD); 737 738 /// Returns the assumed alignment of a virtual base of a class. 739 CharUnits getVBaseAlignment(CharUnits DerivedAlign, 740 const CXXRecordDecl *Derived, 741 const CXXRecordDecl *VBase); 742 743 /// Given a class pointer with an actual known alignment, and the 744 /// expected alignment of an object at a dynamic offset w.r.t that 745 /// pointer, return the alignment to assume at the offset. 746 CharUnits getDynamicOffsetAlignment(CharUnits ActualAlign, 747 const CXXRecordDecl *Class, 748 CharUnits ExpectedTargetAlign); 749 750 CharUnits 751 computeNonVirtualBaseClassOffset(const CXXRecordDecl *DerivedClass, 752 CastExpr::path_const_iterator Start, 753 CastExpr::path_const_iterator End); 754 755 /// Returns the offset from a derived class to a class. Returns null if the 756 /// offset is 0. 757 llvm::Constant * 758 GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl, 759 CastExpr::path_const_iterator PathBegin, 760 CastExpr::path_const_iterator PathEnd); 761 762 llvm::FoldingSet<BlockByrefHelpers> ByrefHelpersCache; 763 764 /// Fetches the global unique block count. 765 int getUniqueBlockCount() { return ++Block.GlobalUniqueCount; } 766 767 /// Fetches the type of a generic block descriptor. 768 llvm::Type *getBlockDescriptorType(); 769 770 /// The type of a generic block literal. 771 llvm::Type *getGenericBlockLiteralType(); 772 773 /// Gets the address of a block which requires no captures. 774 llvm::Constant *GetAddrOfGlobalBlock(const BlockExpr *BE, const char *); 775 776 /// Return a pointer to a constant CFString object for the given string. 777 ConstantAddress GetAddrOfConstantCFString(const StringLiteral *Literal); 778 779 /// Return a pointer to a constant NSString object for the given string. Or a 780 /// user defined String object as defined via 781 /// -fconstant-string-class=class_name option. 782 ConstantAddress GetAddrOfConstantString(const StringLiteral *Literal); 783 784 /// Return a constant array for the given string. 785 llvm::Constant *GetConstantArrayFromStringLiteral(const StringLiteral *E); 786 787 /// Return a pointer to a constant array for the given string literal. 788 ConstantAddress 789 GetAddrOfConstantStringFromLiteral(const StringLiteral *S, 790 StringRef Name = ".str"); 791 792 /// Return a pointer to a constant array for the given ObjCEncodeExpr node. 793 ConstantAddress 794 GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *); 795 796 /// Returns a pointer to a character array containing the literal and a 797 /// terminating '\0' character. The result has pointer to array type. 798 /// 799 /// \param GlobalName If provided, the name to use for the global (if one is 800 /// created). 801 ConstantAddress 802 GetAddrOfConstantCString(const std::string &Str, 803 const char *GlobalName = nullptr); 804 805 /// Returns a pointer to a constant global variable for the given file-scope 806 /// compound literal expression. 807 ConstantAddress GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr*E); 808 809 /// \brief Returns a pointer to a global variable representing a temporary 810 /// with static or thread storage duration. 811 ConstantAddress GetAddrOfGlobalTemporary(const MaterializeTemporaryExpr *E, 812 const Expr *Inner); 813 814 /// \brief Retrieve the record type that describes the state of an 815 /// Objective-C fast enumeration loop (for..in). 816 QualType getObjCFastEnumerationStateType(); 817 818 // Produce code for this constructor/destructor. This method doesn't try 819 // to apply any ABI rules about which other constructors/destructors 820 // are needed or if they are alias to each other. 821 llvm::Function *codegenCXXStructor(const CXXMethodDecl *MD, 822 StructorType Type); 823 824 /// Return the address of the constructor/destructor of the given type. 825 llvm::Constant * 826 getAddrOfCXXStructor(const CXXMethodDecl *MD, StructorType Type, 827 const CGFunctionInfo *FnInfo = nullptr, 828 llvm::FunctionType *FnType = nullptr, 829 bool DontDefer = false, bool IsForDefinition = false); 830 831 /// Given a builtin id for a function like "__builtin_fabsf", return a 832 /// Function* for "fabsf". 833 llvm::Value *getBuiltinLibFunction(const FunctionDecl *FD, 834 unsigned BuiltinID); 835 836 llvm::Function *getIntrinsic(unsigned IID, ArrayRef<llvm::Type*> Tys = None); 837 838 /// Emit code for a single top level declaration. 839 void EmitTopLevelDecl(Decl *D); 840 841 /// \brief Stored a deferred empty coverage mapping for an unused 842 /// and thus uninstrumented top level declaration. 843 void AddDeferredUnusedCoverageMapping(Decl *D); 844 845 /// \brief Remove the deferred empty coverage mapping as this 846 /// declaration is actually instrumented. 847 void ClearUnusedCoverageMapping(const Decl *D); 848 849 /// \brief Emit all the deferred coverage mappings 850 /// for the uninstrumented functions. 851 void EmitDeferredUnusedCoverageMappings(); 852 853 /// Tell the consumer that this variable has been instantiated. 854 void HandleCXXStaticMemberVarInstantiation(VarDecl *VD); 855 856 /// \brief If the declaration has internal linkage but is inside an 857 /// extern "C" linkage specification, prepare to emit an alias for it 858 /// to the expected name. 859 template<typename SomeDecl> 860 void MaybeHandleStaticInExternC(const SomeDecl *D, llvm::GlobalValue *GV); 861 862 /// Add a global to a list to be added to the llvm.used metadata. 863 void addUsedGlobal(llvm::GlobalValue *GV); 864 865 /// Add a global to a list to be added to the llvm.compiler.used metadata. 866 void addCompilerUsedGlobal(llvm::GlobalValue *GV); 867 868 /// Add a destructor and object to add to the C++ global destructor function. 869 void AddCXXDtorEntry(llvm::Constant *DtorFn, llvm::Constant *Object) { 870 CXXGlobalDtors.emplace_back(DtorFn, Object); 871 } 872 873 /// Create a new runtime function with the specified type and name. 874 llvm::Constant *CreateRuntimeFunction(llvm::FunctionType *Ty, 875 StringRef Name, 876 llvm::AttributeSet ExtraAttrs = 877 llvm::AttributeSet()); 878 /// Create a new compiler builtin function with the specified type and name. 879 llvm::Constant *CreateBuiltinFunction(llvm::FunctionType *Ty, 880 StringRef Name, 881 llvm::AttributeSet ExtraAttrs = 882 llvm::AttributeSet()); 883 /// Create a new runtime global variable with the specified type and name. 884 llvm::Constant *CreateRuntimeVariable(llvm::Type *Ty, 885 StringRef Name); 886 887 ///@name Custom Blocks Runtime Interfaces 888 ///@{ 889 890 llvm::Constant *getNSConcreteGlobalBlock(); 891 llvm::Constant *getNSConcreteStackBlock(); 892 llvm::Constant *getBlockObjectAssign(); 893 llvm::Constant *getBlockObjectDispose(); 894 895 ///@} 896 897 llvm::Constant *getLLVMLifetimeStartFn(); 898 llvm::Constant *getLLVMLifetimeEndFn(); 899 900 // Make sure that this type is translated. 901 void UpdateCompletedType(const TagDecl *TD); 902 903 llvm::Constant *getMemberPointerConstant(const UnaryOperator *e); 904 905 /// Try to emit the initializer for the given declaration as a constant; 906 /// returns 0 if the expression cannot be emitted as a constant. 907 llvm::Constant *EmitConstantInit(const VarDecl &D, 908 CodeGenFunction *CGF = nullptr); 909 910 /// Try to emit the given expression as a constant; returns 0 if the 911 /// expression cannot be emitted as a constant. 912 llvm::Constant *EmitConstantExpr(const Expr *E, QualType DestType, 913 CodeGenFunction *CGF = nullptr); 914 915 /// Emit the given constant value as a constant, in the type's scalar 916 /// representation. 917 llvm::Constant *EmitConstantValue(const APValue &Value, QualType DestType, 918 CodeGenFunction *CGF = nullptr); 919 920 /// Emit the given constant value as a constant, in the type's memory 921 /// representation. 922 llvm::Constant *EmitConstantValueForMemory(const APValue &Value, 923 QualType DestType, 924 CodeGenFunction *CGF = nullptr); 925 926 /// Return the result of value-initializing the given type, i.e. a null 927 /// expression of the given type. This is usually, but not always, an LLVM 928 /// null constant. 929 llvm::Constant *EmitNullConstant(QualType T); 930 931 /// Return a null constant appropriate for zero-initializing a base class with 932 /// the given type. This is usually, but not always, an LLVM null constant. 933 llvm::Constant *EmitNullConstantForBase(const CXXRecordDecl *Record); 934 935 /// Emit a general error that something can't be done. 936 void Error(SourceLocation loc, StringRef error); 937 938 /// Print out an error that codegen doesn't support the specified stmt yet. 939 void ErrorUnsupported(const Stmt *S, const char *Type); 940 941 /// Print out an error that codegen doesn't support the specified decl yet. 942 void ErrorUnsupported(const Decl *D, const char *Type); 943 944 /// Set the attributes on the LLVM function for the given decl and function 945 /// info. This applies attributes necessary for handling the ABI as well as 946 /// user specified attributes like section. 947 void SetInternalFunctionAttributes(const Decl *D, llvm::Function *F, 948 const CGFunctionInfo &FI); 949 950 /// Set the LLVM function attributes (sext, zext, etc). 951 void SetLLVMFunctionAttributes(const Decl *D, 952 const CGFunctionInfo &Info, 953 llvm::Function *F); 954 955 /// Set the LLVM function attributes which only apply to a function 956 /// definition. 957 void SetLLVMFunctionAttributesForDefinition(const Decl *D, llvm::Function *F); 958 959 /// Return true iff the given type uses 'sret' when used as a return type. 960 bool ReturnTypeUsesSRet(const CGFunctionInfo &FI); 961 962 /// Return true iff the given type uses an argument slot when 'sret' is used 963 /// as a return type. 964 bool ReturnSlotInterferesWithArgs(const CGFunctionInfo &FI); 965 966 /// Return true iff the given type uses 'fpret' when used as a return type. 967 bool ReturnTypeUsesFPRet(QualType ResultType); 968 969 /// Return true iff the given type uses 'fp2ret' when used as a return type. 970 bool ReturnTypeUsesFP2Ret(QualType ResultType); 971 972 /// Get the LLVM attributes and calling convention to use for a particular 973 /// function type. 974 /// 975 /// \param Info - The function type information. 976 /// \param TargetDecl - The decl these attributes are being constructed 977 /// for. If supplied the attributes applied to this decl may contribute to the 978 /// function attributes and calling convention. 979 /// \param PAL [out] - On return, the attribute list to use. 980 /// \param CallingConv [out] - On return, the LLVM calling convention to use. 981 void ConstructAttributeList(const CGFunctionInfo &Info, 982 const Decl *TargetDecl, 983 AttributeListType &PAL, 984 unsigned &CallingConv, 985 bool AttrOnCallSite); 986 987 StringRef getMangledName(GlobalDecl GD); 988 StringRef getBlockMangledName(GlobalDecl GD, const BlockDecl *BD); 989 990 void EmitTentativeDefinition(const VarDecl *D); 991 992 void EmitVTable(CXXRecordDecl *Class); 993 994 /// Emit the RTTI descriptors for the builtin types. 995 void EmitFundamentalRTTIDescriptors(); 996 997 /// \brief Appends Opts to the "Linker Options" metadata value. 998 void AppendLinkerOptions(StringRef Opts); 999 1000 /// \brief Appends a detect mismatch command to the linker options. 1001 void AddDetectMismatch(StringRef Name, StringRef Value); 1002 1003 /// \brief Appends a dependent lib to the "Linker Options" metadata value. 1004 void AddDependentLib(StringRef Lib); 1005 1006 llvm::GlobalVariable::LinkageTypes getFunctionLinkage(GlobalDecl GD); 1007 1008 void setFunctionLinkage(GlobalDecl GD, llvm::Function *F) { 1009 F->setLinkage(getFunctionLinkage(GD)); 1010 } 1011 1012 /// Set the DLL storage class on F. 1013 void setFunctionDLLStorageClass(GlobalDecl GD, llvm::Function *F); 1014 1015 /// Return the appropriate linkage for the vtable, VTT, and type information 1016 /// of the given class. 1017 llvm::GlobalVariable::LinkageTypes getVTableLinkage(const CXXRecordDecl *RD); 1018 1019 /// Return the store size, in character units, of the given LLVM type. 1020 CharUnits GetTargetTypeStoreSize(llvm::Type *Ty) const; 1021 1022 /// Returns LLVM linkage for a declarator. 1023 llvm::GlobalValue::LinkageTypes 1024 getLLVMLinkageForDeclarator(const DeclaratorDecl *D, GVALinkage Linkage, 1025 bool IsConstantVariable); 1026 1027 /// Returns LLVM linkage for a declarator. 1028 llvm::GlobalValue::LinkageTypes 1029 getLLVMLinkageVarDefinition(const VarDecl *VD, bool IsConstant); 1030 1031 /// Emit all the global annotations. 1032 void EmitGlobalAnnotations(); 1033 1034 /// Emit an annotation string. 1035 llvm::Constant *EmitAnnotationString(StringRef Str); 1036 1037 /// Emit the annotation's translation unit. 1038 llvm::Constant *EmitAnnotationUnit(SourceLocation Loc); 1039 1040 /// Emit the annotation line number. 1041 llvm::Constant *EmitAnnotationLineNo(SourceLocation L); 1042 1043 /// Generate the llvm::ConstantStruct which contains the annotation 1044 /// information for a given GlobalValue. The annotation struct is 1045 /// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the 1046 /// GlobalValue being annotated. The second field is the constant string 1047 /// created from the AnnotateAttr's annotation. The third field is a constant 1048 /// string containing the name of the translation unit. The fourth field is 1049 /// the line number in the file of the annotated value declaration. 1050 llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV, 1051 const AnnotateAttr *AA, 1052 SourceLocation L); 1053 1054 /// Add global annotations that are set on D, for the global GV. Those 1055 /// annotations are emitted during finalization of the LLVM code. 1056 void AddGlobalAnnotations(const ValueDecl *D, llvm::GlobalValue *GV); 1057 1058 bool isInSanitizerBlacklist(llvm::Function *Fn, SourceLocation Loc) const; 1059 1060 bool isInSanitizerBlacklist(llvm::GlobalVariable *GV, SourceLocation Loc, 1061 QualType Ty, 1062 StringRef Category = StringRef()) const; 1063 1064 SanitizerMetadata *getSanitizerMetadata() { 1065 return SanitizerMD.get(); 1066 } 1067 1068 void addDeferredVTable(const CXXRecordDecl *RD) { 1069 DeferredVTables.push_back(RD); 1070 } 1071 1072 /// Emit code for a singal global function or var decl. Forward declarations 1073 /// are emitted lazily. 1074 void EmitGlobal(GlobalDecl D); 1075 1076 bool 1077 HasTrivialDestructorBody(ASTContext &Context, 1078 const CXXRecordDecl *BaseClassDecl, 1079 const CXXRecordDecl *MostDerivedClassDecl); 1080 bool 1081 FieldHasTrivialDestructorBody(ASTContext &Context, const FieldDecl *Field); 1082 1083 bool TryEmitDefinitionAsAlias(GlobalDecl Alias, GlobalDecl Target, 1084 bool InEveryTU); 1085 bool TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D); 1086 1087 /// Set attributes for a global definition. 1088 void setFunctionDefinitionAttributes(const FunctionDecl *D, 1089 llvm::Function *F); 1090 1091 llvm::GlobalValue *GetGlobalValue(StringRef Ref); 1092 1093 /// Set attributes which are common to any form of a global definition (alias, 1094 /// Objective-C method, function, global variable). 1095 /// 1096 /// NOTE: This should only be called for definitions. 1097 void SetCommonAttributes(const Decl *D, llvm::GlobalValue *GV); 1098 1099 /// Set attributes which must be preserved by an alias. This includes common 1100 /// attributes (i.e. it includes a call to SetCommonAttributes). 1101 /// 1102 /// NOTE: This should only be called for definitions. 1103 void setAliasAttributes(const Decl *D, llvm::GlobalValue *GV); 1104 1105 void addReplacement(StringRef Name, llvm::Constant *C); 1106 1107 void addGlobalValReplacement(llvm::GlobalValue *GV, llvm::Constant *C); 1108 1109 /// \brief Emit a code for threadprivate directive. 1110 /// \param D Threadprivate declaration. 1111 void EmitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D); 1112 1113 /// Returns whether the given record is blacklisted from control flow 1114 /// integrity checks. 1115 bool IsCFIBlacklistedRecord(const CXXRecordDecl *RD); 1116 1117 /// Emit bit set entries for the given vtable using the given layout if 1118 /// vptr CFI is enabled. 1119 void EmitVTableBitSetEntries(llvm::GlobalVariable *VTable, 1120 const VTableLayout &VTLayout); 1121 1122 /// Create a metadata identifier for the given type. This may either be an 1123 /// MDString (for external identifiers) or a distinct unnamed MDNode (for 1124 /// internal identifiers). 1125 llvm::Metadata *CreateMetadataIdentifierForType(QualType T); 1126 1127 /// Create a bitset entry for the given vtable. 1128 llvm::MDTuple *CreateVTableBitSetEntry(llvm::GlobalVariable *VTable, 1129 CharUnits Offset, 1130 const CXXRecordDecl *RD); 1131 1132 /// \breif Get the declaration of std::terminate for the platform. 1133 llvm::Constant *getTerminateFn(); 1134 1135 private: 1136 llvm::Constant * 1137 GetOrCreateLLVMFunction(StringRef MangledName, llvm::Type *Ty, GlobalDecl D, 1138 bool ForVTable, bool DontDefer = false, 1139 bool IsThunk = false, 1140 llvm::AttributeSet ExtraAttrs = llvm::AttributeSet(), 1141 bool IsForDefinition = false); 1142 1143 llvm::Constant *GetOrCreateLLVMGlobal(StringRef MangledName, 1144 llvm::PointerType *PTy, 1145 const VarDecl *D); 1146 1147 void setNonAliasAttributes(const Decl *D, llvm::GlobalObject *GO); 1148 1149 /// Set function attributes for a function declaration. 1150 void SetFunctionAttributes(GlobalDecl GD, llvm::Function *F, 1151 bool IsIncompleteFunction, bool IsThunk); 1152 1153 void EmitGlobalDefinition(GlobalDecl D, llvm::GlobalValue *GV = nullptr); 1154 1155 void EmitGlobalFunctionDefinition(GlobalDecl GD, llvm::GlobalValue *GV); 1156 void EmitGlobalVarDefinition(const VarDecl *D); 1157 void EmitAliasDefinition(GlobalDecl GD); 1158 void EmitObjCPropertyImplementations(const ObjCImplementationDecl *D); 1159 void EmitObjCIvarInitializations(ObjCImplementationDecl *D); 1160 1161 // C++ related functions. 1162 1163 void EmitNamespace(const NamespaceDecl *D); 1164 void EmitLinkageSpec(const LinkageSpecDecl *D); 1165 void CompleteDIClassType(const CXXMethodDecl* D); 1166 1167 /// \brief Emit the function that initializes C++ thread_local variables. 1168 void EmitCXXThreadLocalInitFunc(); 1169 1170 /// Emit the function that initializes C++ globals. 1171 void EmitCXXGlobalInitFunc(); 1172 1173 /// Emit the function that destroys C++ globals. 1174 void EmitCXXGlobalDtorFunc(); 1175 1176 /// Emit the function that initializes the specified global (if PerformInit is 1177 /// true) and registers its destructor. 1178 void EmitCXXGlobalVarDeclInitFunc(const VarDecl *D, 1179 llvm::GlobalVariable *Addr, 1180 bool PerformInit); 1181 1182 void EmitPointerToInitFunc(const VarDecl *VD, llvm::GlobalVariable *Addr, 1183 llvm::Function *InitFunc, InitSegAttr *ISA); 1184 1185 // FIXME: Hardcoding priority here is gross. 1186 void AddGlobalCtor(llvm::Function *Ctor, int Priority = 65535, 1187 llvm::Constant *AssociatedData = nullptr); 1188 void AddGlobalDtor(llvm::Function *Dtor, int Priority = 65535); 1189 1190 /// Generates a global array of functions and priorities using the given list 1191 /// and name. This array will have appending linkage and is suitable for use 1192 /// as a LLVM constructor or destructor array. 1193 void EmitCtorList(const CtorList &Fns, const char *GlobalName); 1194 1195 /// Emit the RTTI descriptors for the given type. 1196 void EmitFundamentalRTTIDescriptor(QualType Type); 1197 1198 /// Emit any needed decls for which code generation was deferred. 1199 void EmitDeferred(); 1200 1201 /// Call replaceAllUsesWith on all pairs in Replacements. 1202 void applyReplacements(); 1203 1204 /// Call replaceAllUsesWith on all pairs in GlobalValReplacements. 1205 void applyGlobalValReplacements(); 1206 1207 void checkAliases(); 1208 1209 /// Emit any vtables which we deferred and still have a use for. 1210 void EmitDeferredVTables(); 1211 1212 /// Emit the llvm.used and llvm.compiler.used metadata. 1213 void emitLLVMUsed(); 1214 1215 /// \brief Emit the link options introduced by imported modules. 1216 void EmitModuleLinkOptions(); 1217 1218 /// \brief Emit aliases for internal-linkage declarations inside "C" language 1219 /// linkage specifications, giving them the "expected" name where possible. 1220 void EmitStaticExternCAliases(); 1221 1222 void EmitDeclMetadata(); 1223 1224 /// \brief Emit the Clang version as llvm.ident metadata. 1225 void EmitVersionIdentMetadata(); 1226 1227 /// Emits target specific Metadata for global declarations. 1228 void EmitTargetMetadata(); 1229 1230 /// Emit the llvm.gcov metadata used to tell LLVM where to emit the .gcno and 1231 /// .gcda files in a way that persists in .bc files. 1232 void EmitCoverageFile(); 1233 1234 /// Emits the initializer for a uuidof string. 1235 llvm::Constant *EmitUuidofInitializer(StringRef uuidstr); 1236 1237 /// Determine whether the definition must be emitted; if this returns \c 1238 /// false, the definition can be emitted lazily if it's used. 1239 bool MustBeEmitted(const ValueDecl *D); 1240 1241 /// Determine whether the definition can be emitted eagerly, or should be 1242 /// delayed until the end of the translation unit. This is relevant for 1243 /// definitions whose linkage can change, e.g. implicit function instantions 1244 /// which may later be explicitly instantiated. 1245 bool MayBeEmittedEagerly(const ValueDecl *D); 1246 1247 /// Check whether we can use a "simpler", more core exceptions personality 1248 /// function. 1249 void SimplifyPersonality(); 1250 }; 1251 } // end namespace CodeGen 1252 } // end namespace clang 1253 1254 #endif // LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H 1255