From 9b4ee0c1bdbd46ea9b2bb5fd68edcb23ce0a4a8d Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Tue, 20 Feb 2007 07:17:17 +0000 Subject: [PATCH] switch ConstantFP's from ValueMap to DenseMap, which is much faster to query and is more memory efficient. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@34446 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/VMCore/Constants.cpp | 110 +++++++++++++++++++++------------------ 1 file changed, 59 insertions(+), 51 deletions(-) diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp index 816ffba4b55..cb61b0b5c5f 100644 --- a/lib/VMCore/Constants.cpp +++ b/lib/VMCore/Constants.cpp @@ -165,12 +165,9 @@ ConstantInt *ConstantInt::CreateTrueFalseVals(bool WhichOne) { } -//---- ConstantInt::get() implementations... -// -// Provide DenseMapKeyInfo for all pointers. namespace { - struct DenseMapIntegerKeyInfo { - typedef std::pair KeyTy; + struct DenseMapInt64KeyInfo { + typedef std::pair KeyTy; static inline KeyTy getEmptyKey() { return KeyTy(0, 0); } static inline KeyTy getTombstoneKey() { return KeyTy(1, 0); } static unsigned getHashValue(const KeyTy &Key) { @@ -181,8 +178,8 @@ namespace { } -typedef DenseMap IntMapTy; +typedef DenseMap IntMapTy; static ManagedStatic IntConstants; // Get a ConstantInt from an int64_t. Note here that we canoncialize the value @@ -193,22 +190,74 @@ static ManagedStatic IntConstants; ConstantInt *ConstantInt::get(const Type *Ty, int64_t V) { const IntegerType *ITy = cast(Ty); V &= ITy->getBitMask(); - ConstantInt *&Slot = (*IntConstants)[std::make_pair(uint64_t(V), ITy)]; + ConstantInt *&Slot = (*IntConstants)[std::make_pair(uint64_t(V), Ty)]; if (Slot) return Slot; return Slot = new ConstantInt(ITy, V); } //===----------------------------------------------------------------------===// -// ConstantXXX Classes +// ConstantFP //===----------------------------------------------------------------------===// ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty, ConstantFPVal, 0, 0) { - assert(isValueValidForType(Ty, V) && "Value too large for type!"); Val = V; } +bool ConstantFP::isNullValue() const { + return DoubleToBits(Val) == 0; +} + +bool ConstantFP::isExactlyValue(double V) const { + return DoubleToBits(V) == DoubleToBits(Val); +} + + +namespace { + struct DenseMapInt32KeyInfo { + typedef std::pair KeyTy; + static inline KeyTy getEmptyKey() { return KeyTy(0, 0); } + static inline KeyTy getTombstoneKey() { return KeyTy(1, 0); } + static unsigned getHashValue(const KeyTy &Key) { + return DenseMapKeyInfo::getHashValue(Key.second) ^ Key.first; + } + static bool isPod() { return true; } + }; +} + +//---- ConstantFP::get() implementation... +// +typedef DenseMap FloatMapTy; +typedef DenseMap DoubleMapTy; + +static ManagedStatic FloatConstants; +static ManagedStatic DoubleConstants; + +ConstantFP *ConstantFP::get(const Type *Ty, double V) { + if (Ty == Type::FloatTy) { + uint32_t IntVal = FloatToBits((float)V); + + ConstantFP *&Slot = (*FloatConstants)[std::make_pair(IntVal, Ty)]; + if (Slot) return Slot; + return Slot = new ConstantFP(Ty, (float)V); + } else { + assert(Ty == Type::DoubleTy); + uint64_t IntVal = DoubleToBits(V); + ConstantFP *&Slot = (*DoubleConstants)[std::make_pair(IntVal, Ty)]; + if (Slot) return Slot; + return Slot = new ConstantFP(Ty, (float)V); + } +} + + +//===----------------------------------------------------------------------===// +// ConstantXXX Classes +//===----------------------------------------------------------------------===// + + ConstantArray::ConstantArray(const ArrayType *T, const std::vector &V) : Constant(T, ConstantArrayVal, new Use[V.size()], V.size()) { @@ -847,47 +896,6 @@ public: -//---- ConstantFP::get() implementation... -// -namespace llvm { - template<> - struct ConstantCreator { - static ConstantFP *create(const Type *Ty, uint64_t V) { - assert(Ty == Type::DoubleTy); - return new ConstantFP(Ty, BitsToDouble(V)); - } - }; - template<> - struct ConstantCreator { - static ConstantFP *create(const Type *Ty, uint32_t V) { - assert(Ty == Type::FloatTy); - return new ConstantFP(Ty, BitsToFloat(V)); - } - }; -} - -static ManagedStatic > DoubleConstants; -static ManagedStatic > FloatConstants; - -bool ConstantFP::isNullValue() const { - return DoubleToBits(Val) == 0; -} - -bool ConstantFP::isExactlyValue(double V) const { - return DoubleToBits(V) == DoubleToBits(Val); -} - - -ConstantFP *ConstantFP::get(const Type *Ty, double V) { - if (Ty == Type::FloatTy) { - // Force the value through memory to normalize it. - return FloatConstants->getOrCreate(Ty, FloatToBits(V)); - } else { - assert(Ty == Type::DoubleTy); - return DoubleConstants->getOrCreate(Ty, DoubleToBits(V)); - } -} - //---- ConstantAggregateZero::get() implementation... // namespace llvm {