diff options
author | Chris Lattner <sabre@nondot.org> | 2007-02-20 06:39:57 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2007-02-20 06:39:57 +0000 |
commit | 6b6f6ba66c656b205a6db0751685911032fe710e (patch) | |
tree | 0fa0a49755a74dc84b56e681e7f9e8f811360cf0 | |
parent | d283566319a5bec8bfa2223580d92c86e01af13b (diff) |
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
-rw-r--r-- | include/llvm/Constants.h | 5 | ||||
-rw-r--r-- | lib/VMCore/Constants.cpp | 113 |
2 files changed, 64 insertions, 54 deletions
diff --git a/include/llvm/Constants.h b/include/llvm/Constants.h index 11605eaacf..53a5811a3f 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<ConstantInt, IntegerType, uint64_t>; + 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 ec02d30bd2..816ffba4b5 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 <algorithm> #include <map> @@ -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<llvm::CleanupTrueFalse> 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<uint64_t, const IntegerType*> 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<void*>::getHashValue(Key.second) ^ Key.first; + } + static bool isPod() { return true; } + }; +} + + +typedef DenseMap<DenseMapIntegerKeyInfo::KeyTy, ConstantInt*, +DenseMapIntegerKeyInfo> IntMapTy; +static ManagedStatic<IntMapTy> 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<IntegerType>(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<Constant *> &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<ValueMap<uint64_t, IntegerType, ConstantInt> >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<IntegerType>(Ty); - return IntConstants->getOrCreate(ITy, V & ITy->getBitMask()); -} - -ConstantInt *ConstantInt::TheTrueVal = 0; -ConstantInt *ConstantInt::TheFalseVal = 0; - -void CleanupTrueFalse(void *) { - ConstantInt::ResetTrueFalse(); -} - -static ManagedCleanup<CleanupTrueFalse> 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... // |