From 23ec01fcc32368801033558b7764dac356a51e5a Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Tue, 4 Oct 2005 18:47:09 +0000 Subject: [PATCH] Minor speedup to avoid array searches given a Use*. This speeds up bc reading of the python test from 1:00 to 54s. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23627 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/VMCore/Constants.cpp | 62 +++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp index 0f0894bc53e..374aad52a18 100644 --- a/lib/VMCore/Constants.cpp +++ b/lib/VMCore/Constants.cpp @@ -1390,22 +1390,32 @@ void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) { assert(isa(To) && "Cannot make Constant refer to non-constant!"); Constant *ToC = cast(To); - + + unsigned OperandToUpdate = U-OperandList; + assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!"); + std::pair Lookup; Lookup.first.first = getType(); Lookup.second = this; + std::vector &Values = Lookup.first.second; Values.reserve(getNumOperands()); // Build replacement array. - + // Fill values with the modified operands of the constant array. Also, // compute whether this turns into an all-zeros array. - bool isAllZeros = ToC->isNullValue(); - for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { - Constant *Val = getOperand(i); - if (Val == From) Val = ToC; - Values.push_back(Val); - if (isAllZeros) isAllZeros = Val->isNullValue(); + bool isAllZeros = false; + if (!ToC->isNullValue()) { + for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) + Values.push_back(cast(O->get())); + } else { + isAllZeros = true; + for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) { + Constant *Val = cast(O->get()); + Values.push_back(Val); + if (isAllZeros) isAllZeros = Val->isNullValue(); + } } + Values[OperandToUpdate] = ToC; Constant *Replacement = 0; if (isAllZeros) { @@ -1429,10 +1439,8 @@ void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To, // located at descriptor I. ArrayConstants.UpdateInverseMap(this, I); - // Update to the new values. - for (unsigned i = 0, e = getNumOperands(); i != e; ++i) - if (getOperand(i) == From) - setOperand(i, ToC); + // Update to the new value. + setOperand(OperandToUpdate, ToC); return; } } @@ -1452,22 +1460,32 @@ void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To, assert(isa(To) && "Cannot make Constant refer to non-constant!"); Constant *ToC = cast(To); + unsigned OperandToUpdate = U-OperandList; + assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!"); + std::pair Lookup; Lookup.first.first = getType(); Lookup.second = this; std::vector &Values = Lookup.first.second; Values.reserve(getNumOperands()); // Build replacement struct. + // Fill values with the modified operands of the constant struct. Also, // compute whether this turns into an all-zeros struct. - bool isAllZeros = ToC->isNullValue(); - for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { - Constant *Val = getOperand(i); - if (Val == From) Val = ToC; - Values.push_back(Val); - if (isAllZeros) isAllZeros = Val->isNullValue(); + bool isAllZeros = false; + if (!ToC->isNullValue()) { + for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) + Values.push_back(cast(O->get())); + } else { + isAllZeros = true; + for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) { + Constant *Val = cast(O->get()); + Values.push_back(Val); + if (isAllZeros) isAllZeros = Val->isNullValue(); + } } - + Values[OperandToUpdate] = ToC; + Constant *Replacement = 0; if (isAllZeros) { Replacement = ConstantAggregateZero::get(getType()); @@ -1490,10 +1508,8 @@ void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To, // located at descriptor I. StructConstants.UpdateInverseMap(this, I); - // Update to the new values. - for (unsigned i = 0, e = getNumOperands(); i != e; ++i) - if (getOperand(i) == From) - setOperand(i, ToC); + // Update to the new value. + setOperand(OperandToUpdate, ToC); return; } } -- 2.34.1