From 5d68585bb1337be1b257458370a3d350461182a3 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Tue, 4 Oct 2005 01:17:50 +0000 Subject: [PATCH] implement the struct version of the array speedup, speeding up the testcase a bit more from 1:48 -> 1.40. llvm-svn: 23619 --- lib/VMCore/Constants.cpp | 58 +++++++++++++++++++++++++++++++++------- 1 file changed, 49 insertions(+), 9 deletions(-) diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp index 6728fc4080f..64ac4c1e29e 100644 --- a/lib/VMCore/Constants.cpp +++ b/lib/VMCore/Constants.cpp @@ -889,8 +889,9 @@ namespace llvm { }; } -static ValueMap, StructType, - ConstantStruct> StructConstants; +typedef ValueMap, StructType, + ConstantStruct> StructConstantsTy; +static StructConstantsTy StructConstants; static std::vector getValType(ConstantStruct *CS) { std::vector Elements; @@ -1346,6 +1347,7 @@ const char *ConstantExpr::getOpcodeName() const { void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To, bool DisableChecking) { assert(isa(To) && "Cannot make Constant refer to non-constant!"); + Constant *ToC = cast(To); std::pair Lookup; Lookup.first.first = getType(); @@ -1353,10 +1355,12 @@ void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To, std::vector &Values = Lookup.first.second; Values.reserve(getNumOperands()); // Build replacement array. - bool isAllZeros = false; // Does this turn into an all-zeros 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 = cast(To); + if (Val == From) Val = ToC; Values.push_back(Val); if (isAllZeros) isAllZeros = Val->isNullValue(); } @@ -1384,7 +1388,7 @@ void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To, // Update to the new values. for (unsigned i = 0, e = getNumOperands(); i != e; ++i) if (getOperand(i) == From) - setOperand(i, cast(To)); + setOperand(i, ToC); return; } } @@ -1405,16 +1409,52 @@ void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To, void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To, bool DisableChecking) { assert(isa(To) && "Cannot make Constant refer to non-constant!"); + Constant *ToC = cast(To); + + std::pair Lookup; + Lookup.first.first = getType(); + Lookup.second = this; + std::vector &Values = Lookup.first.second; + Values.reserve(getNumOperands()); // Build replacement struct. - std::vector Values; - Values.reserve(getNumOperands()); // Build replacement array... + // 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 = cast(To); + if (Val == From) Val = ToC; Values.push_back(Val); + if (isAllZeros) isAllZeros = Val->isNullValue(); + } + + Constant *Replacement = 0; + if (isAllZeros) { + Replacement = ConstantAggregateZero::get(getType()); + } else { + // Check to see if we have this array type already. + bool Exists; + StructConstantsTy::MapIterator I = + StructConstants.InsertOrGetItem(Lookup, Exists); + + if (Exists) { + Replacement = I->second; + } else { + // Okay, the new shape doesn't exist in the system yet. Instead of + // creating a new constant struct, inserting it, replaceallusesof'ing the + // old with the new, then deleting the old... just update the current one + // in place! + if (I != StructConstants.map_end() && I->second == this) + ++I; // Do not invalidate iterator! + StructConstants.remove(this); // Remove old shape from the map. + + // Update to the new values. + for (unsigned i = 0, e = getNumOperands(); i != e; ++i) + if (getOperand(i) == From) + setOperand(i, ToC); + return; + } } - Constant *Replacement = ConstantStruct::get(getType(), Values); assert(Replacement != this && "I didn't contain From!"); // Everyone using this now uses the replacement...