From 496425774e8adbc526e298f91f4c6980177c1d7f Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Mon, 28 Dec 2009 09:24:53 +0000 Subject: [PATCH] Eliminate two bits of ugliness in MDNode::replaceElement: eliminate the temporary smallvector, and only do FindNodeOrInsertPos twice if the first one succeeds and we delete a node. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@92220 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/VMCore/Metadata.cpp | 39 +++++++++++++-------------------------- 1 file changed, 13 insertions(+), 26 deletions(-) diff --git a/lib/VMCore/Metadata.cpp b/lib/VMCore/Metadata.cpp index dd6a0f61348..eddd8b070a6 100644 --- a/lib/VMCore/Metadata.cpp +++ b/lib/VMCore/Metadata.cpp @@ -145,31 +145,21 @@ Value *MDNode::getElement(unsigned i) const { void MDNode::replaceElement(Value *From, Value *To) { if (From == To || !getType()) return; - LLVMContext &Context = getType()->getContext(); - LLVMContextImpl *pImpl = Context.pImpl; + LLVMContextImpl *pImpl = getType()->getContext().pImpl; + + // 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. - SmallVector Indexes; - unsigned Index = 0; - for (unsigned i = 0, e = getNumElements(); i != e; ++i, ++Index) { - Value *V = getElement(i); - if (V && V == From) - Indexes.push_back(Index); + for (unsigned i = 0, e = getNumElements(); i != e; ++i) { + if (Operands[i] == From) + Operands[i].set(To, this); } - if (Indexes.empty()) - return; - - // Remove "this" from the context map. - pImpl->MDNodeSet.RemoveNode(this); - - // Replace From element(s) in place. - for (SmallVector::iterator I = Indexes.begin(), E = Indexes.end(); - I != E; ++I) - Operands[*I].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 // updated "this" into the folding node set, replace all uses of existing @@ -182,15 +172,12 @@ void MDNode::replaceElement(Value *From, Value *To) { if (N) { N->replaceAllUsesWith(this); delete N; - N = 0; + N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint); + assert(N == 0 && "shouldn't be in the map now!"); (void)N; } - N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint); - if (!N) { - // InsertPoint will have been set by the FindNodeOrInsertPos call. - N = this; - pImpl->MDNodeSet.InsertNode(N, InsertPoint); - } + // InsertPoint will have been set by the FindNodeOrInsertPos call. + pImpl->MDNodeSet.InsertNode(this, InsertPoint); } //===----------------------------------------------------------------------===// -- 2.34.1