diff --git a/llvm/include/llvm/CodeGen/MachineFunction.h b/llvm/include/llvm/CodeGen/MachineFunction.h --- a/llvm/include/llvm/CodeGen/MachineFunction.h +++ b/llvm/include/llvm/CodeGen/MachineFunction.h @@ -1030,7 +1030,8 @@ /// the function. MachineInstr::ExtraInfo *createMIExtraInfo( ArrayRef MMOs, MCSymbol *PreInstrSymbol = nullptr, - MCSymbol *PostInstrSymbol = nullptr, MDNode *HeapAllocMarker = nullptr); + MCSymbol *PostInstrSymbol = nullptr, MDNode *HeapAllocMarker = nullptr, + MDNode *PCSections = nullptr); /// Allocate a string and populate it with the given external symbol name. const char *createExternalSymbolName(StringRef Name); diff --git a/llvm/include/llvm/CodeGen/MachineInstr.h b/llvm/include/llvm/CodeGen/MachineInstr.h --- a/llvm/include/llvm/CodeGen/MachineInstr.h +++ b/llvm/include/llvm/CodeGen/MachineInstr.h @@ -151,17 +151,19 @@ ArrayRef MMOs, MCSymbol *PreInstrSymbol = nullptr, MCSymbol *PostInstrSymbol = nullptr, - MDNode *HeapAllocMarker = nullptr) { + MDNode *HeapAllocMarker = nullptr, + MDNode *PCSections = nullptr) { bool HasPreInstrSymbol = PreInstrSymbol != nullptr; bool HasPostInstrSymbol = PostInstrSymbol != nullptr; bool HasHeapAllocMarker = HeapAllocMarker != nullptr; + bool HasPCSections = PCSections != nullptr; auto *Result = new (Allocator.Allocate( totalSizeToAlloc( MMOs.size(), HasPreInstrSymbol + HasPostInstrSymbol, - HasHeapAllocMarker), + HasHeapAllocMarker + HasPCSections), alignof(ExtraInfo))) ExtraInfo(MMOs.size(), HasPreInstrSymbol, HasPostInstrSymbol, - HasHeapAllocMarker); + HasHeapAllocMarker, HasPCSections); // Copy the actual data into the trailing objects. std::copy(MMOs.begin(), MMOs.end(), @@ -174,6 +176,9 @@ PostInstrSymbol; if (HasHeapAllocMarker) Result->getTrailingObjects()[0] = HeapAllocMarker; + if (HasPCSections) + Result->getTrailingObjects()[HasHeapAllocMarker] = + PCSections; return Result; } @@ -196,6 +201,12 @@ return HasHeapAllocMarker ? getTrailingObjects()[0] : nullptr; } + MDNode *getPCSections() const { + return HasPCSections + ? getTrailingObjects()[HasHeapAllocMarker] + : nullptr; + } + private: friend TrailingObjects; @@ -208,6 +219,7 @@ const bool HasPreInstrSymbol; const bool HasPostInstrSymbol; const bool HasHeapAllocMarker; + const bool HasPCSections; // Implement the `TrailingObjects` internal API. size_t numTrailingObjects(OverloadToken) const { @@ -217,16 +229,17 @@ return HasPreInstrSymbol + HasPostInstrSymbol; } size_t numTrailingObjects(OverloadToken) const { - return HasHeapAllocMarker; + return HasHeapAllocMarker + HasPCSections; } // Just a boring constructor to allow us to initialize the sizes. Always use // the `create` routine above. ExtraInfo(int NumMMOs, bool HasPreInstrSymbol, bool HasPostInstrSymbol, - bool HasHeapAllocMarker) + bool HasHeapAllocMarker, bool HasPCSections) : NumMMOs(NumMMOs), HasPreInstrSymbol(HasPreInstrSymbol), HasPostInstrSymbol(HasPostInstrSymbol), - HasHeapAllocMarker(HasHeapAllocMarker) {} + HasHeapAllocMarker(HasHeapAllocMarker), + HasPCSections(HasPCSections) {} }; /// Enumeration of the kinds of inline extra info available. It is important @@ -757,6 +770,16 @@ return nullptr; } + /// Helper to extract PCSections metadata target sections. + MDNode *getPCSections() const { + if (!Info) + return nullptr; + if (ExtraInfo *EI = Info.get()) + return EI->getPCSections(); + + return nullptr; + } + /// API for querying MachineInstr properties. They are the same as MCInstrDesc /// queries but they are bundle aware. @@ -1788,6 +1811,10 @@ /// instruction is removed or duplicated. void setHeapAllocMarker(MachineFunction &MF, MDNode *MD); + // Set metadata on instructions that say which sections to emit instruction + // addresses into. + void setPCSections(MachineFunction &MF, MDNode *MD); + /// Return the MIFlags which represent both MachineInstrs. This /// should be used when merging two MachineInstrs into one. This routine does /// not modify the MIFlags of this MachineInstr. @@ -1866,7 +1893,7 @@ /// based on the number of pointers. void setExtraInfo(MachineFunction &MF, ArrayRef MMOs, MCSymbol *PreInstrSymbol, MCSymbol *PostInstrSymbol, - MDNode *HeapAllocMarker); + MDNode *HeapAllocMarker, MDNode *PCSections); }; /// Special DenseMapInfo traits to compare MachineInstr* by *value* of the diff --git a/llvm/lib/CodeGen/MachineFunction.cpp b/llvm/lib/CodeGen/MachineFunction.cpp --- a/llvm/lib/CodeGen/MachineFunction.cpp +++ b/llvm/lib/CodeGen/MachineFunction.cpp @@ -530,9 +530,10 @@ MachineInstr::ExtraInfo *MachineFunction::createMIExtraInfo( ArrayRef MMOs, MCSymbol *PreInstrSymbol, - MCSymbol *PostInstrSymbol, MDNode *HeapAllocMarker) { + MCSymbol *PostInstrSymbol, MDNode *HeapAllocMarker, MDNode *PCSections) { return MachineInstr::ExtraInfo::create(Allocator, MMOs, PreInstrSymbol, - PostInstrSymbol, HeapAllocMarker); + PostInstrSymbol, HeapAllocMarker, + PCSections); } const char *MachineFunction::createExternalSymbolName(StringRef Name) { diff --git a/llvm/lib/CodeGen/MachineInstr.cpp b/llvm/lib/CodeGen/MachineInstr.cpp --- a/llvm/lib/CodeGen/MachineInstr.cpp +++ b/llvm/lib/CodeGen/MachineInstr.cpp @@ -301,12 +301,14 @@ ArrayRef MMOs, MCSymbol *PreInstrSymbol, MCSymbol *PostInstrSymbol, - MDNode *HeapAllocMarker) { + MDNode *HeapAllocMarker, + MDNode *PCSections) { bool HasPreInstrSymbol = PreInstrSymbol != nullptr; bool HasPostInstrSymbol = PostInstrSymbol != nullptr; bool HasHeapAllocMarker = HeapAllocMarker != nullptr; - int NumPointers = - MMOs.size() + HasPreInstrSymbol + HasPostInstrSymbol + HasHeapAllocMarker; + bool HasPCSections = PCSections != nullptr; + int NumPointers = MMOs.size() + HasPreInstrSymbol + HasPostInstrSymbol + + HasHeapAllocMarker + HasPCSections; // Drop all extra info if there is none. if (NumPointers <= 0) { @@ -318,9 +320,10 @@ // out of line because PointerSumType cannot hold more than 4 tag types with // 32-bit pointers. // FIXME: Maybe we should make the symbols in the extra info mutable? - else if (NumPointers > 1 || HasHeapAllocMarker) { - Info.set(MF.createMIExtraInfo( - MMOs, PreInstrSymbol, PostInstrSymbol, HeapAllocMarker)); + else if (NumPointers > 1 || HasHeapAllocMarker || HasPCSections) { + Info.set( + MF.createMIExtraInfo(MMOs, PreInstrSymbol, PostInstrSymbol, + HeapAllocMarker, PCSections)); return; } @@ -338,7 +341,7 @@ return; setExtraInfo(MF, {}, getPreInstrSymbol(), getPostInstrSymbol(), - getHeapAllocMarker()); + getHeapAllocMarker(), getPCSections()); } void MachineInstr::setMemRefs(MachineFunction &MF, @@ -349,7 +352,7 @@ } setExtraInfo(MF, MMOs, getPreInstrSymbol(), getPostInstrSymbol(), - getHeapAllocMarker()); + getHeapAllocMarker(), getPCSections()); } void MachineInstr::addMemOperand(MachineFunction &MF, @@ -372,7 +375,8 @@ // are the same (including null). if (getPreInstrSymbol() == MI.getPreInstrSymbol() && getPostInstrSymbol() == MI.getPostInstrSymbol() && - getHeapAllocMarker() == MI.getHeapAllocMarker()) { + getHeapAllocMarker() == MI.getHeapAllocMarker() && + getPCSections() == MI.getPCSections()) { Info = MI.Info; return; } @@ -457,7 +461,7 @@ } setExtraInfo(MF, memoperands(), Symbol, getPostInstrSymbol(), - getHeapAllocMarker()); + getHeapAllocMarker(), getPCSections()); } void MachineInstr::setPostInstrSymbol(MachineFunction &MF, MCSymbol *Symbol) { @@ -472,7 +476,7 @@ } setExtraInfo(MF, memoperands(), getPreInstrSymbol(), Symbol, - getHeapAllocMarker()); + getHeapAllocMarker(), getPCSections()); } void MachineInstr::setHeapAllocMarker(MachineFunction &MF, MDNode *Marker) { @@ -481,7 +485,16 @@ return; setExtraInfo(MF, memoperands(), getPreInstrSymbol(), getPostInstrSymbol(), - Marker); + Marker, getPCSections()); +} + +void MachineInstr::setPCSections(MachineFunction &MF, MDNode *PCSections) { + // Do nothing if old and new symbols are the same. + if (PCSections == getPCSections()) + return; + + setExtraInfo(MF, memoperands(), getPreInstrSymbol(), getPostInstrSymbol(), + getHeapAllocMarker(), PCSections); } void MachineInstr::cloneInstrSymbols(MachineFunction &MF, @@ -496,6 +509,7 @@ setPreInstrSymbol(MF, MI.getPreInstrSymbol()); setPostInstrSymbol(MF, MI.getPostInstrSymbol()); setHeapAllocMarker(MF, MI.getHeapAllocMarker()); + setPCSections(MF, MI.getPCSections()); } uint16_t MachineInstr::mergeFlagsWith(const MachineInstr &Other) const { @@ -1753,6 +1767,14 @@ OS << " heap-alloc-marker "; HeapAllocMarker->printAsOperand(OS, MST); } + if (MDNode *PCSections = getPCSections()) { + if (!FirstOp) { + FirstOp = false; + OS << ','; + } + OS << " pcsections "; + PCSections->printAsOperand(OS, MST); + } if (DebugInstrNum) { if (!FirstOp) diff --git a/llvm/unittests/CodeGen/MachineInstrTest.cpp b/llvm/unittests/CodeGen/MachineInstrTest.cpp --- a/llvm/unittests/CodeGen/MachineInstrTest.cpp +++ b/llvm/unittests/CodeGen/MachineInstrTest.cpp @@ -269,36 +269,49 @@ MMOs.push_back(MMO); MCSymbol *Sym1 = MC->createTempSymbol("pre_label", false); MCSymbol *Sym2 = MC->createTempSymbol("post_label", false); - MDNode *MDN = MDNode::getDistinct(Ctx, None); + MDNode *HAM = MDNode::getDistinct(Ctx, None); + MDNode *PCS = MDNode::getDistinct(Ctx, None); ASSERT_TRUE(MI->memoperands_empty()); ASSERT_FALSE(MI->getPreInstrSymbol()); ASSERT_FALSE(MI->getPostInstrSymbol()); ASSERT_FALSE(MI->getHeapAllocMarker()); + ASSERT_FALSE(MI->getPCSections()); MI->setMemRefs(*MF, MMOs); ASSERT_TRUE(MI->memoperands().size() == 1); ASSERT_FALSE(MI->getPreInstrSymbol()); ASSERT_FALSE(MI->getPostInstrSymbol()); ASSERT_FALSE(MI->getHeapAllocMarker()); + ASSERT_FALSE(MI->getPCSections()); MI->setPreInstrSymbol(*MF, Sym1); ASSERT_TRUE(MI->memoperands().size() == 1); ASSERT_TRUE(MI->getPreInstrSymbol() == Sym1); ASSERT_FALSE(MI->getPostInstrSymbol()); ASSERT_FALSE(MI->getHeapAllocMarker()); + ASSERT_FALSE(MI->getPCSections()); MI->setPostInstrSymbol(*MF, Sym2); ASSERT_TRUE(MI->memoperands().size() == 1); ASSERT_TRUE(MI->getPreInstrSymbol() == Sym1); ASSERT_TRUE(MI->getPostInstrSymbol() == Sym2); ASSERT_FALSE(MI->getHeapAllocMarker()); + ASSERT_FALSE(MI->getPCSections()); - MI->setHeapAllocMarker(*MF, MDN); + MI->setHeapAllocMarker(*MF, HAM); ASSERT_TRUE(MI->memoperands().size() == 1); ASSERT_TRUE(MI->getPreInstrSymbol() == Sym1); ASSERT_TRUE(MI->getPostInstrSymbol() == Sym2); - ASSERT_TRUE(MI->getHeapAllocMarker() == MDN); + ASSERT_TRUE(MI->getHeapAllocMarker() == HAM); + ASSERT_FALSE(MI->getPCSections()); + + MI->setPCSections(*MF, PCS); + ASSERT_TRUE(MI->memoperands().size() == 1); + ASSERT_TRUE(MI->getPreInstrSymbol() == Sym1); + ASSERT_TRUE(MI->getPostInstrSymbol() == Sym2); + ASSERT_TRUE(MI->getHeapAllocMarker() == HAM); + ASSERT_TRUE(MI->getPCSections() == PCS); } TEST(MachineInstrExtraInfo, ChangeExtraInfo) { @@ -316,12 +329,14 @@ MMOs.push_back(MMO); MCSymbol *Sym1 = MC->createTempSymbol("pre_label", false); MCSymbol *Sym2 = MC->createTempSymbol("post_label", false); - MDNode *MDN = MDNode::getDistinct(Ctx, None); + MDNode *HAM = MDNode::getDistinct(Ctx, None); + MDNode *PCS = MDNode::getDistinct(Ctx, None); MI->setMemRefs(*MF, MMOs); MI->setPreInstrSymbol(*MF, Sym1); MI->setPostInstrSymbol(*MF, Sym2); - MI->setHeapAllocMarker(*MF, MDN); + MI->setHeapAllocMarker(*MF, HAM); + MI->setPCSections(*MF, PCS); MMOs.push_back(MMO); @@ -329,13 +344,15 @@ ASSERT_TRUE(MI->memoperands().size() == 2); ASSERT_TRUE(MI->getPreInstrSymbol() == Sym1); ASSERT_TRUE(MI->getPostInstrSymbol() == Sym2); - ASSERT_TRUE(MI->getHeapAllocMarker() == MDN); + ASSERT_TRUE(MI->getHeapAllocMarker() == HAM); + ASSERT_TRUE(MI->getPCSections() == PCS); MI->setPostInstrSymbol(*MF, Sym1); ASSERT_TRUE(MI->memoperands().size() == 2); ASSERT_TRUE(MI->getPreInstrSymbol() == Sym1); ASSERT_TRUE(MI->getPostInstrSymbol() == Sym1); - ASSERT_TRUE(MI->getHeapAllocMarker() == MDN); + ASSERT_TRUE(MI->getHeapAllocMarker() == HAM); + ASSERT_TRUE(MI->getPCSections() == PCS); } TEST(MachineInstrExtraInfo, RemoveExtraInfo) { @@ -354,36 +371,49 @@ MMOs.push_back(MMO); MCSymbol *Sym1 = MC->createTempSymbol("pre_label", false); MCSymbol *Sym2 = MC->createTempSymbol("post_label", false); - MDNode *MDN = MDNode::getDistinct(Ctx, None); + MDNode *HAM = MDNode::getDistinct(Ctx, None); + MDNode *PCS = MDNode::getDistinct(Ctx, None); MI->setMemRefs(*MF, MMOs); MI->setPreInstrSymbol(*MF, Sym1); MI->setPostInstrSymbol(*MF, Sym2); - MI->setHeapAllocMarker(*MF, MDN); + MI->setHeapAllocMarker(*MF, HAM); + MI->setPCSections(*MF, PCS); MI->setPostInstrSymbol(*MF, nullptr); ASSERT_TRUE(MI->memoperands().size() == 2); ASSERT_TRUE(MI->getPreInstrSymbol() == Sym1); ASSERT_FALSE(MI->getPostInstrSymbol()); - ASSERT_TRUE(MI->getHeapAllocMarker() == MDN); + ASSERT_TRUE(MI->getHeapAllocMarker() == HAM); + ASSERT_TRUE(MI->getPCSections() == PCS); MI->setHeapAllocMarker(*MF, nullptr); ASSERT_TRUE(MI->memoperands().size() == 2); ASSERT_TRUE(MI->getPreInstrSymbol() == Sym1); ASSERT_FALSE(MI->getPostInstrSymbol()); ASSERT_FALSE(MI->getHeapAllocMarker()); + ASSERT_TRUE(MI->getPCSections() == PCS); + + MI->setPCSections(*MF, nullptr); + ASSERT_TRUE(MI->memoperands().size() == 2); + ASSERT_TRUE(MI->getPreInstrSymbol() == Sym1); + ASSERT_FALSE(MI->getPostInstrSymbol()); + ASSERT_FALSE(MI->getHeapAllocMarker()); + ASSERT_FALSE(MI->getPCSections()); MI->setPreInstrSymbol(*MF, nullptr); ASSERT_TRUE(MI->memoperands().size() == 2); ASSERT_FALSE(MI->getPreInstrSymbol()); ASSERT_FALSE(MI->getPostInstrSymbol()); ASSERT_FALSE(MI->getHeapAllocMarker()); + ASSERT_FALSE(MI->getPCSections()); MI->setMemRefs(*MF, {}); ASSERT_TRUE(MI->memoperands_empty()); ASSERT_FALSE(MI->getPreInstrSymbol()); ASSERT_FALSE(MI->getPostInstrSymbol()); ASSERT_FALSE(MI->getHeapAllocMarker()); + ASSERT_FALSE(MI->getPCSections()); } TEST(MachineInstrDebugValue, AddDebugValueOperand) {