cleanup ConstantInt to use a single DenseMap for uniquing instead of the
authorChris Lattner <sabre@nondot.org>
Tue, 20 Feb 2007 06:39:57 +0000 (06:39 +0000)
committerChris Lattner <sabre@nondot.org>
Tue, 20 Feb 2007 06:39:57 +0000 (06:39 +0000)
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

include/llvm/Constants.h
lib/VMCore/Constants.cpp

index 11605eaacf2fa15e85f6b9cada5af7c043c65dce..53a5811a3f80079fce46e49f44c80a2db7a63f33 100644 (file)
@@ -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.
index ec02d30bd289b69b6571375ae01c4990ddbb7c15..816ffba4b55aacf64ff6030c32cd870ed0749692 100644 (file)
@@ -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...
 //