From fb717fe06dd0b52094be178ead0862efc2252797 Mon Sep 17 00:00:00 2001 From: Vasileios Porpodas Date: Tue, 7 Feb 2023 09:47:03 -0800 Subject: [PATCH] [NFC][IR] Make Module::getNamedMDList() private This patch adds several missing NamedMDList modifier functions, like removeNamedMDNode(), eraseNamedMDNode() and insertNamedMDNode(). There is no longer need to access the list directly so it also makes getNamedMDList() private. Differential Revision: https://reviews.llvm.org/D143969 --- llvm/include/llvm/IR/Module.h | 13 ++++++++++++- llvm/lib/IR/DebugInfo.cpp | 2 +- llvm/lib/IR/Module.cpp | 4 ++-- llvm/unittests/IR/ModuleTest.cpp | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 4 deletions(-) diff --git a/llvm/include/llvm/IR/Module.h b/llvm/include/llvm/IR/Module.h index 200f39b..dbd3eae 100644 --- a/llvm/include/llvm/IR/Module.h +++ b/llvm/include/llvm/IR/Module.h @@ -581,6 +581,17 @@ public: // Use ifunc_size() to get the number of functions in IFuncList. // Use ifuncs() to get the range of all IFuncs. + /// Detach \p MDNode from the list but don't delete it. + void removeNamedMDNode(NamedMDNode *MDNode) { NamedMDList.remove(MDNode); } + /// Remove \p MDNode from the list and delete it. + void eraseNamedMDNode(NamedMDNode *MDNode) { NamedMDList.erase(MDNode); } + /// Insert \p MDNode at the end of the alias list and take ownership. + void insertNamedMDNode(NamedMDNode *MDNode) { + NamedMDList.push_back(MDNode); + } + // Use named_metadata_size() to get the size of the named meatadata list. + // Use named_metadata() to get the range of all named metadata. + private: // Please use functions like insertAlias(), removeAlias() etc. /// Get the Module's list of aliases (constant). const AliasListType &getAliasList() const { return AliasList; } @@ -603,7 +614,6 @@ public: } friend class llvm::SymbolTableListTraits; -public: /// Get the Module's list of named metadata (constant). const NamedMDListType &getNamedMDList() const { return NamedMDList; } /// Get the Module's list of named metadata. @@ -613,6 +623,7 @@ public: return &Module::NamedMDList; } +public: /// Get the symbol table of global variable and function identifiers const ValueSymbolTable &getValueSymbolTable() const { return *ValSymTab; } /// Get the Module's symbol table of global variable and function identifiers. diff --git a/llvm/lib/IR/DebugInfo.cpp b/llvm/lib/IR/DebugInfo.cpp index 0a457bd..c601dbf 100644 --- a/llvm/lib/IR/DebugInfo.cpp +++ b/llvm/lib/IR/DebugInfo.cpp @@ -870,7 +870,7 @@ bool llvm::stripNonLineTableDebugInfo(Module &M) { // Create a new llvm.dbg.cu, which is equivalent to the one // -gline-tables-only would have created. - for (auto &NMD : M.getNamedMDList()) { + for (auto &NMD : M.named_metadata()) { SmallVector Ops; for (MDNode *Op : NMD.operands()) Ops.push_back(remap(Op)); diff --git a/llvm/lib/IR/Module.cpp b/llvm/lib/IR/Module.cpp index 3df1e7b..a1f8cd0 100644 --- a/llvm/lib/IR/Module.cpp +++ b/llvm/lib/IR/Module.cpp @@ -262,7 +262,7 @@ NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) { if (!NMD) { NMD = new NamedMDNode(Name); NMD->setParent(this); - NamedMDList.push_back(NMD); + insertNamedMDNode(NMD); } return NMD; } @@ -271,7 +271,7 @@ NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) { /// delete it. void Module::eraseNamedMetadata(NamedMDNode *NMD) { NamedMDSymTab.erase(NMD->getName()); - NamedMDList.erase(NMD->getIterator()); + eraseNamedMDNode(NMD); } bool Module::isValidModFlagBehavior(Metadata *MD, ModFlagBehavior &MFB) { diff --git a/llvm/unittests/IR/ModuleTest.cpp b/llvm/unittests/IR/ModuleTest.cpp index 8b675ba..af899c3 100644 --- a/llvm/unittests/IR/ModuleTest.cpp +++ b/llvm/unittests/IR/ModuleTest.cpp @@ -239,4 +239,38 @@ declare void @Foo() EXPECT_EQ(M->ifunc_size(), 1u); } +TEST(ModuleTest, NamedMDList) { + // This tests all Module's functions that interact with Module::NamedMDList. + LLVMContext C; + SMDiagnostic Err; + LLVMContext Context; + auto M = std::make_unique("M", C); + NamedMDNode *MDN1 = M->getOrInsertNamedMetadata("MDN1"); + EXPECT_EQ(M->named_metadata_size(), 1u); + NamedMDNode *MDN2 = M->getOrInsertNamedMetadata("MDN2"); + EXPECT_EQ(M->named_metadata_size(), 2u); + auto *NewMDN = M->getOrInsertNamedMetadata("NewMDN"); + EXPECT_EQ(M->named_metadata_size(), 3u); + + M->removeNamedMDNode(NewMDN); + EXPECT_EQ(M->named_metadata_size(), 2u); + + M->insertNamedMDNode(NewMDN); + EXPECT_EQ(&*std::prev(M->named_metadata().end()), NewMDN); + + M->removeNamedMDNode(NewMDN); + M->insertNamedMDNode(NewMDN); + EXPECT_EQ(M->named_metadata_size(), 3u); + EXPECT_EQ(&*std::prev(M->named_metadata().end()), NewMDN); + + auto Range = M->named_metadata(); + EXPECT_EQ(&*Range.begin(), MDN1); + EXPECT_EQ(&*std::next(Range.begin(), 1), MDN2); + EXPECT_EQ(&*std::next(Range.begin(), 2), NewMDN); + EXPECT_EQ(std::next(Range.begin(), 3), Range.end()); + + M->eraseNamedMDNode(NewMDN); + EXPECT_EQ(M->named_metadata_size(), 2u); +} + } // end namespace -- 2.7.4