From: Chris Lattner Date: Tue, 20 Feb 2007 06:39:57 +0000 (+0000) Subject: cleanup ConstantInt to use a single DenseMap for uniquing instead of the X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=6b6f6ba66c656b205a6db0751685911032fe710e;p=oota-llvm.git cleanup ConstantInt to use a single DenseMap for uniquing instead of the heavy-weight ValueMap class. This reduces mem usage bc reading kc++ by 29K, even though it only creates 2955 constant ints! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@34445 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/Constants.h b/include/llvm/Constants.h index 11605eaacf2..53a5811a3f8 100644 --- a/include/llvm/Constants.h +++ b/include/llvm/Constants.h @@ -41,12 +41,9 @@ struct ConvertConstantType; /// @brief Class for constant integers. class ConstantInt : public Constant { static ConstantInt *TheTrueVal, *TheFalseVal; -protected: - uint64_t Val; -protected: ConstantInt(const ConstantInt &); // DO NOT IMPLEMENT ConstantInt(const IntegerType *Ty, uint64_t V); - friend struct ConstantCreator; + uint64_t Val; public: /// Return the constant as a 64-bit unsigned integer value after it /// has been zero extended as appropriate for the type of this constant. diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp index ec02d30bd28..816ffba4b55 100644 --- a/lib/VMCore/Constants.cpp +++ b/lib/VMCore/Constants.cpp @@ -22,6 +22,7 @@ #include "llvm/Support/Debug.h" #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/MathExtras.h" +#include "llvm/ADT/DenseMap.h" #include "llvm/ADT/SmallVector.h" #include #include @@ -134,16 +135,74 @@ ConstantVector *ConstantVector::getAllOnesValue(const VectorType *Ty) { //===----------------------------------------------------------------------===// -// ConstantXXX Classes -//===----------------------------------------------------------------------===// - +// ConstantInt //===----------------------------------------------------------------------===// -// Normal Constructors ConstantInt::ConstantInt(const IntegerType *Ty, uint64_t V) : Constant(Ty, ConstantIntVal, 0, 0), Val(V) { } +ConstantInt *ConstantInt::TheTrueVal = 0; +ConstantInt *ConstantInt::TheFalseVal = 0; + +namespace llvm { + void CleanupTrueFalse(void *) { + ConstantInt::ResetTrueFalse(); + } +} + +static ManagedCleanup TrueFalseCleanup; + +ConstantInt *ConstantInt::CreateTrueFalseVals(bool WhichOne) { + assert(TheTrueVal == 0 && TheFalseVal == 0); + TheTrueVal = get(Type::Int1Ty, 1); + TheFalseVal = get(Type::Int1Ty, 0); + + // Ensure that llvm_shutdown nulls out TheTrueVal/TheFalseVal. + TrueFalseCleanup.Register(); + + return WhichOne ? TheTrueVal : TheFalseVal; +} + + +//---- ConstantInt::get() implementations... +// +// Provide DenseMapKeyInfo for all pointers. +namespace { + struct DenseMapIntegerKeyInfo { + 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; } + }; +} + + +typedef DenseMap IntMapTy; +static ManagedStatic IntConstants; + +// Get a ConstantInt from an int64_t. Note here that we canoncialize the value +// to a uint64_t value that has been zero extended down to the size of the +// integer type of the ConstantInt. This allows the getZExtValue method to +// just return the stored value while getSExtValue has to convert back to sign +// extended. getZExtValue is more common in LLVM than getSExtValue(). +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)]; + if (Slot) return Slot; + return Slot = new ConstantInt(ITy, V); +} + +//===----------------------------------------------------------------------===// +// ConstantXXX Classes +//===----------------------------------------------------------------------===// + + ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty, ConstantFPVal, 0, 0) { assert(isValueValidForType(Ty, V) && "Value too large for type!"); @@ -598,15 +657,6 @@ namespace llvm { /// AbstractTypeMapTy AbstractTypeMap; - private: - void clear(std::vector &Constants) { - for(typename MapTy::iterator I = Map.begin(); I != Map.end(); ++I) - Constants.push_back(I->second); - Map.clear(); - AbstractTypeMap.clear(); - InverseMap.clear(); - } - public: typename MapTy::iterator map_end() { return Map.end(); } @@ -796,43 +846,6 @@ public: } -//---- ConstantInt::get() implementations... -// -static ManagedStatic >IntConstants; - - -// Get a ConstantInt from an int64_t. Note here that we canoncialize the value -// to a uint64_t value that has been zero extended down to the size of the -// integer type of the ConstantInt. This allows the getZExtValue method to -// just return the stored value while getSExtValue has to convert back to sign -// extended. getZExtValue is more common in LLVM than getSExtValue(). -ConstantInt *ConstantInt::get(const Type *Ty, int64_t V) { - const IntegerType *ITy = cast(Ty); - return IntConstants->getOrCreate(ITy, V & ITy->getBitMask()); -} - -ConstantInt *ConstantInt::TheTrueVal = 0; -ConstantInt *ConstantInt::TheFalseVal = 0; - -void CleanupTrueFalse(void *) { - ConstantInt::ResetTrueFalse(); -} - -static ManagedCleanup TrueFalseCleanup; - -ConstantInt *ConstantInt::CreateTrueFalseVals(bool WhichOne) { - assert(TheTrueVal == 0 && TheFalseVal == 0); - TheTrueVal = get(Type::Int1Ty, 1); - TheFalseVal = get(Type::Int1Ty, 0); - - // Ensure that llvm_shutdown nulls out TheTrueVal/TheFalseVal. - TrueFalseCleanup.Register(); - - return WhichOne ? TheTrueVal : TheFalseVal; -} - - - //---- ConstantFP::get() implementation... //