From df58389ef1fb8373d900975a6a66e4fbe0344f83 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Mon, 28 Dec 2009 09:32:10 +0000 Subject: [PATCH] avoid a completely unneeded linear walk. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@92221 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Metadata.h | 2 +- lib/VMCore/Metadata.cpp | 21 +++++++++------------ 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/include/llvm/Metadata.h b/include/llvm/Metadata.h index 7cfaeafa8f1..1ece55935f9 100644 --- a/include/llvm/Metadata.h +++ b/include/llvm/Metadata.h @@ -102,7 +102,7 @@ class MDNode : public MetadataBase, public FoldingSetNode { }; // Replace each instance of F from the element list of this node with T. - void replaceElement(Value *F, Value *T); + void replaceElement(MDNodeElement *Op, Value *NewVal); protected: explicit MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals, diff --git a/lib/VMCore/Metadata.cpp b/lib/VMCore/Metadata.cpp index eddd8b070a6..0a3ddcbbe1b 100644 --- a/lib/VMCore/Metadata.cpp +++ b/lib/VMCore/Metadata.cpp @@ -76,11 +76,11 @@ public: void MDNodeElement::deleted() { - Parent->replaceElement(this->operator Value*(), 0); + Parent->replaceElement(this, 0); } void MDNodeElement::allUsesReplacedWith(Value *NV) { - Parent->replaceElement(this->operator Value*(), NV); + Parent->replaceElement(this, NV); } @@ -142,8 +142,10 @@ Value *MDNode::getElement(unsigned i) const { // Replace value from this node's element list. -void MDNode::replaceElement(Value *From, Value *To) { - if (From == To || !getType()) +void MDNode::replaceElement(MDNodeElement *Op, Value *To) { + Value *From = *Op; + + if (From == To) return; LLVMContextImpl *pImpl = getType()->getContext().pImpl; @@ -151,14 +153,9 @@ void MDNode::replaceElement(Value *From, Value *To) { // Remove "this" from the context map. FoldingSet doesn't have to reprofile // this node to remove it, so we don't care what state the operands are in. pImpl->MDNodeSet.RemoveNode(this); - - // Find value. This is a linear search, do something if it consumes - // lot of time. It is possible that to have multiple instances of - // From in this MDNode's element list. - for (unsigned i = 0, e = getNumElements(); i != e; ++i) { - if (Operands[i] == From) - Operands[i].set(To, this); - } + + // Update the operand. + Op->set(To, this); // Insert updated "this" into the context's folding node set. // If a node with same element list already exist then before inserting -- 2.34.1