comparison llvm/lib/IR/Module.cpp @ 221:79ff65ed7e25

LLVM12 Original
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 15 Jun 2021 19:15:29 +0900
parents 0572611fdcc8
children 5f17cb93ff66
comparison
equal deleted inserted replaced
220:42394fc6a535 221:79ff65ed7e25
31 #include "llvm/IR/GlobalIFunc.h" 31 #include "llvm/IR/GlobalIFunc.h"
32 #include "llvm/IR/GlobalValue.h" 32 #include "llvm/IR/GlobalValue.h"
33 #include "llvm/IR/GlobalVariable.h" 33 #include "llvm/IR/GlobalVariable.h"
34 #include "llvm/IR/LLVMContext.h" 34 #include "llvm/IR/LLVMContext.h"
35 #include "llvm/IR/Metadata.h" 35 #include "llvm/IR/Metadata.h"
36 #include "llvm/IR/ModuleSummaryIndex.h"
36 #include "llvm/IR/SymbolTableListTraits.h" 37 #include "llvm/IR/SymbolTableListTraits.h"
37 #include "llvm/IR/Type.h" 38 #include "llvm/IR/Type.h"
38 #include "llvm/IR/TypeFinder.h" 39 #include "llvm/IR/TypeFinder.h"
39 #include "llvm/IR/Value.h" 40 #include "llvm/IR/Value.h"
40 #include "llvm/IR/ValueSymbolTable.h" 41 #include "llvm/IR/ValueSymbolTable.h"
69 //===----------------------------------------------------------------------===// 70 //===----------------------------------------------------------------------===//
70 // Primitive Module methods. 71 // Primitive Module methods.
71 // 72 //
72 73
73 Module::Module(StringRef MID, LLVMContext &C) 74 Module::Module(StringRef MID, LLVMContext &C)
74 : Context(C), ValSymTab(std::make_unique<ValueSymbolTable>()), 75 : Context(C), ValSymTab(std::make_unique<ValueSymbolTable>(-1)),
75 Materializer(), ModuleID(std::string(MID)), 76 Materializer(), ModuleID(std::string(MID)),
76 SourceFileName(std::string(MID)), DL("") { 77 SourceFileName(std::string(MID)), DL("") {
77 Context.addModule(this); 78 Context.addModule(this);
78 } 79 }
79 80
470 SrcStructTypes.run(*this, true); 471 SrcStructTypes.run(*this, true);
471 Ret.assign(SrcStructTypes.begin(), SrcStructTypes.end()); 472 Ret.assign(SrcStructTypes.begin(), SrcStructTypes.end());
472 return Ret; 473 return Ret;
473 } 474 }
474 475
476 std::string Module::getUniqueIntrinsicName(StringRef BaseName, Intrinsic::ID Id,
477 const FunctionType *Proto) {
478 auto Encode = [&BaseName](unsigned Suffix) {
479 return (Twine(BaseName) + "." + Twine(Suffix)).str();
480 };
481
482 {
483 // fast path - the prototype is already known
484 auto UinItInserted = UniquedIntrinsicNames.insert({{Id, Proto}, 0});
485 if (!UinItInserted.second)
486 return Encode(UinItInserted.first->second);
487 }
488
489 // Not known yet. A new entry was created with index 0. Check if there already
490 // exists a matching declaration, or select a new entry.
491
492 // Start looking for names with the current known maximum count (or 0).
493 auto NiidItInserted = CurrentIntrinsicIds.insert({BaseName, 0});
494 unsigned Count = NiidItInserted.first->second;
495
496 // This might be slow if a whole population of intrinsics already existed, but
497 // we cache the values for later usage.
498 std::string NewName;
499 while (true) {
500 NewName = Encode(Count);
501 GlobalValue *F = getNamedValue(NewName);
502 if (!F) {
503 // Reserve this entry for the new proto
504 UniquedIntrinsicNames[{Id, Proto}] = Count;
505 break;
506 }
507
508 // A declaration with this name already exists. Remember it.
509 FunctionType *FT = dyn_cast<FunctionType>(F->getType()->getElementType());
510 auto UinItInserted = UniquedIntrinsicNames.insert({{Id, FT}, Count});
511 if (FT == Proto) {
512 // It was a declaration for our prototype. This entry was allocated in the
513 // beginning. Update the count to match the existing declaration.
514 UinItInserted.first->second = Count;
515 break;
516 }
517
518 ++Count;
519 }
520
521 NiidItInserted.first->second = Count + 1;
522
523 return NewName;
524 }
525
475 // dropAllReferences() - This function causes all the subelements to "let go" 526 // dropAllReferences() - This function causes all the subelements to "let go"
476 // of all references that they are maintaining. This allows one to 'delete' a 527 // of all references that they are maintaining. This allows one to 'delete' a
477 // whole module at a time, even though there may be circular references... first 528 // whole module at a time, even though there may be circular references... first
478 // all references are dropped, and all use counts go to zero. Then everything 529 // all references are dropped, and all use counts go to zero. Then everything
479 // is deleted for real. Note that no operations are valid on an object that 530 // is deleted for real. Note that no operations are valid on an object that
506 if (!Val) 557 if (!Val)
507 return 0; 558 return 0;
508 return cast<ConstantInt>(Val->getValue())->getZExtValue(); 559 return cast<ConstantInt>(Val->getValue())->getZExtValue();
509 } 560 }
510 561
562 bool Module::isDwarf64() const {
563 auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("DWARF64"));
564 return Val && cast<ConstantInt>(Val->getValue())->isOne();
565 }
566
511 unsigned Module::getCodeViewFlag() const { 567 unsigned Module::getCodeViewFlag() const {
512 auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("CodeView")); 568 auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("CodeView"));
513 if (!Val) 569 if (!Val)
514 return 0; 570 return 0;
515 return cast<ConstantInt>(Val->getValue())->getZExtValue(); 571 return cast<ConstantInt>(Val->getValue())->getZExtValue();
516 } 572 }
517 573
518 unsigned Module::getInstructionCount() { 574 unsigned Module::getInstructionCount() const {
519 unsigned NumInstrs = 0; 575 unsigned NumInstrs = 0;
520 for (Function &F : FunctionList) 576 for (const Function &F : FunctionList)
521 NumInstrs += F.getInstructionCount(); 577 NumInstrs += F.getInstructionCount();
522 return NumInstrs; 578 return NumInstrs;
523 } 579 }
524 580
525 Comdat *Module::getOrInsertComdat(StringRef Name) { 581 Comdat *Module::getOrInsertComdat(StringRef Name) {
579 setModuleFlag(ModFlagBehavior::Error, "CSProfileSummary", M); 635 setModuleFlag(ModFlagBehavior::Error, "CSProfileSummary", M);
580 else 636 else
581 setModuleFlag(ModFlagBehavior::Error, "ProfileSummary", M); 637 setModuleFlag(ModFlagBehavior::Error, "ProfileSummary", M);
582 } 638 }
583 639
584 Metadata *Module::getProfileSummary(bool IsCS) { 640 Metadata *Module::getProfileSummary(bool IsCS) const {
585 return (IsCS ? getModuleFlag("CSProfileSummary") 641 return (IsCS ? getModuleFlag("CSProfileSummary")
586 : getModuleFlag("ProfileSummary")); 642 : getModuleFlag("ProfileSummary"));
587 } 643 }
588 644
589 bool Module::getSemanticInterposition() const { 645 bool Module::getSemanticInterposition() const {
609 return Val && (cast<ConstantInt>(Val->getValue())->getZExtValue() > 0); 665 return Val && (cast<ConstantInt>(Val->getValue())->getZExtValue() > 0);
610 } 666 }
611 667
612 void Module::setRtLibUseGOT() { 668 void Module::setRtLibUseGOT() {
613 addModuleFlag(ModFlagBehavior::Max, "RtLibUseGOT", 1); 669 addModuleFlag(ModFlagBehavior::Max, "RtLibUseGOT", 1);
670 }
671
672 bool Module::getUwtable() const {
673 auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("uwtable"));
674 return Val && (cast<ConstantInt>(Val->getValue())->getZExtValue() > 0);
675 }
676
677 void Module::setUwtable() { addModuleFlag(ModFlagBehavior::Max, "uwtable", 1); }
678
679 FramePointerKind Module::getFramePointer() const {
680 auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("frame-pointer"));
681 return static_cast<FramePointerKind>(
682 Val ? cast<ConstantInt>(Val->getValue())->getZExtValue() : 0);
683 }
684
685 void Module::setFramePointer(FramePointerKind Kind) {
686 addModuleFlag(ModFlagBehavior::Max, "frame-pointer", static_cast<int>(Kind));
687 }
688
689 StringRef Module::getStackProtectorGuard() const {
690 Metadata *MD = getModuleFlag("stack-protector-guard");
691 if (auto *MDS = dyn_cast_or_null<MDString>(MD))
692 return MDS->getString();
693 return {};
694 }
695
696 void Module::setStackProtectorGuard(StringRef Kind) {
697 MDString *ID = MDString::get(getContext(), Kind);
698 addModuleFlag(ModFlagBehavior::Error, "stack-protector-guard", ID);
699 }
700
701 StringRef Module::getStackProtectorGuardReg() const {
702 Metadata *MD = getModuleFlag("stack-protector-guard-reg");
703 if (auto *MDS = dyn_cast_or_null<MDString>(MD))
704 return MDS->getString();
705 return {};
706 }
707
708 void Module::setStackProtectorGuardReg(StringRef Reg) {
709 MDString *ID = MDString::get(getContext(), Reg);
710 addModuleFlag(ModFlagBehavior::Error, "stack-protector-guard-reg", ID);
711 }
712
713 int Module::getStackProtectorGuardOffset() const {
714 Metadata *MD = getModuleFlag("stack-protector-guard-offset");
715 if (auto *CI = mdconst::dyn_extract_or_null<ConstantInt>(MD))
716 return CI->getSExtValue();
717 return INT_MAX;
718 }
719
720 void Module::setStackProtectorGuardOffset(int Offset) {
721 addModuleFlag(ModFlagBehavior::Error, "stack-protector-guard-offset", Offset);
614 } 722 }
615 723
616 void Module::setSDKVersion(const VersionTuple &V) { 724 void Module::setSDKVersion(const VersionTuple &V) {
617 SmallVector<unsigned, 3> Entries; 725 SmallVector<unsigned, 3> Entries;
618 Entries.push_back(V.getMajor()); 726 Entries.push_back(V.getMajor());
651 } 759 }
652 return Result; 760 return Result;
653 } 761 }
654 762
655 GlobalVariable *llvm::collectUsedGlobalVariables( 763 GlobalVariable *llvm::collectUsedGlobalVariables(
656 const Module &M, SmallPtrSetImpl<GlobalValue *> &Set, bool CompilerUsed) { 764 const Module &M, SmallVectorImpl<GlobalValue *> &Vec, bool CompilerUsed) {
657 const char *Name = CompilerUsed ? "llvm.compiler.used" : "llvm.used"; 765 const char *Name = CompilerUsed ? "llvm.compiler.used" : "llvm.used";
658 GlobalVariable *GV = M.getGlobalVariable(Name); 766 GlobalVariable *GV = M.getGlobalVariable(Name);
659 if (!GV || !GV->hasInitializer()) 767 if (!GV || !GV->hasInitializer())
660 return GV; 768 return GV;
661 769
662 const ConstantArray *Init = cast<ConstantArray>(GV->getInitializer()); 770 const ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
663 for (Value *Op : Init->operands()) { 771 for (Value *Op : Init->operands()) {
664 GlobalValue *G = cast<GlobalValue>(Op->stripPointerCasts()); 772 GlobalValue *G = cast<GlobalValue>(Op->stripPointerCasts());
665 Set.insert(G); 773 Vec.push_back(G);
666 } 774 }
667 return GV; 775 return GV;
668 } 776 }
777
778 void Module::setPartialSampleProfileRatio(const ModuleSummaryIndex &Index) {
779 if (auto *SummaryMD = getProfileSummary(/*IsCS*/ false)) {
780 std::unique_ptr<ProfileSummary> ProfileSummary(
781 ProfileSummary::getFromMD(SummaryMD));
782 if (ProfileSummary) {
783 if (ProfileSummary->getKind() != ProfileSummary::PSK_Sample ||
784 !ProfileSummary->isPartialProfile())
785 return;
786 uint64_t BlockCount = Index.getBlockCount();
787 uint32_t NumCounts = ProfileSummary->getNumCounts();
788 if (!NumCounts)
789 return;
790 double Ratio = (double)BlockCount / NumCounts;
791 ProfileSummary->setPartialProfileRatio(Ratio);
792 setProfileSummary(ProfileSummary->getMD(getContext()),
793 ProfileSummary::PSK_Sample);
794 }
795 }
796 }