Privatize the ConstantArray table.
authorOwen Anderson <resistor@mac.com>
Tue, 21 Jul 2009 20:55:28 +0000 (20:55 +0000)
committerOwen Anderson <resistor@mac.com>
Tue, 21 Jul 2009 20:55:28 +0000 (20:55 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@76639 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 4ef680cc03b7c73e1ab68f65f502c4c2355b0ff8..3bf8b142abf3072892b8f72e80e6f9aa31cceeeb 100644 (file)
@@ -286,12 +286,10 @@ class ConstantArray : public Constant {
   friend struct ConstantCreator<ConstantArray, ArrayType,
                                     std::vector<Constant*> >;
   ConstantArray(const ConstantArray &);      // DO NOT IMPLEMENT
+  friend class LLVMContextImpl;
 protected:
   ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
 public:
-  /// get() - Static factory methods - Return objects of the specified value
-  static Constant *get(const ArrayType *T, const std::vector<Constant*> &);
-
   /// Transparently provide more efficient getOperand methods.
   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
 
index 0d0b6624dc1527708b6e9efd24bf48e04a0c36d5..134311e98afe750b2f1775dc262b80a3c80bf68c 100644 (file)
@@ -44,6 +44,7 @@ class Type;
 class APInt;
 class APFloat;
 class Value;
+class Use;
 
 /// This is an important class for using LLVM in a threaded context.  It
 /// (opaquely) owns and manages the core "global" data of LLVM's core 
@@ -273,6 +274,11 @@ public:
   void erase(MDString *M);
   void erase(MDNode *M);
   void erase(ConstantAggregateZero *Z);
+  void erase(ConstantArray *Z);
+  
+  // RAUW helpers
+  Constant *replaceUsesOfWithOnConstant(ConstantArray *CA,
+                                             Value *From, Value *To, Use *U);
 };
 
 /// FOR BACKWARDS COMPATIBILITY - Returns a global context.
index 8bcf383adeb3012aec32cece701ce8cc78561e1a..e64b0c4ff50b71c48925e3d662bd1d1021f2e855 100644 (file)
@@ -1033,60 +1033,11 @@ void ConstantAggregateZero::destroyConstant() {
   destroyConstantImpl();
 }
 
-//---- ConstantArray::get() implementation...
-//
-namespace llvm {
-  template<>
-  struct ConvertConstantType<ConstantArray, ArrayType> {
-    static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
-      // Make everyone now use a constant of the new type...
-      std::vector<Constant*> C;
-      for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
-        C.push_back(cast<Constant>(OldC->getOperand(i)));
-      Constant *New = ConstantArray::get(NewTy, C);
-      assert(New != OldC && "Didn't replace constant??");
-      OldC->uncheckedReplaceAllUsesWith(New);
-      OldC->destroyConstant();    // This constant is now dead, destroy it.
-    }
-  };
-}
-
-static std::vector<Constant*> getValType(ConstantArray *CA) {
-  std::vector<Constant*> Elements;
-  Elements.reserve(CA->getNumOperands());
-  for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
-    Elements.push_back(cast<Constant>(CA->getOperand(i)));
-  return Elements;
-}
-
-typedef ValueMap<std::vector<Constant*>, ArrayType, 
-                 ConstantArray, true /*largekey*/> ArrayConstantsTy;
-static ManagedStatic<ArrayConstantsTy> ArrayConstants;
-
-Constant *ConstantArray::get(const ArrayType *Ty,
-                             const std::vector<Constant*> &V) {
-  // If this is an all-zero array, return a ConstantAggregateZero object
-  if (!V.empty()) {
-    Constant *C = V[0];
-    if (!C->isNullValue()) {
-      // Implicitly locked.
-      return ArrayConstants->getOrCreate(Ty, V);
-    }
-    for (unsigned i = 1, e = V.size(); i != e; ++i)
-      if (V[i] != C) {
-        // Implicitly locked.
-        return ArrayConstants->getOrCreate(Ty, V);
-      }
-  }
-  
-  return Ty->getContext().getConstantAggregateZero(Ty);
-}
-
 /// destroyConstant - Remove the constant from the constant table...
 ///
 void ConstantArray::destroyConstant() {
   // Implicitly locked.
-  ArrayConstants->remove(this);
+  getType()->getContext().erase(this);
   destroyConstantImpl();
 }
 
@@ -2160,77 +2111,10 @@ const char *ConstantExpr::getOpcodeName() const {
 /// array instance.
 void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
                                                 Use *U) {
-  assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
-  Constant *ToC = cast<Constant>(To);
-
-  std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup;
-  Lookup.first.first = getType();
-  Lookup.second = this;
-
-  std::vector<Constant*> &Values = Lookup.first.second;
-  Values.reserve(getNumOperands());  // Build replacement array.
-
-  // Fill values with the modified operands of the constant array.  Also, 
-  // compute whether this turns into an all-zeros array.
-  bool isAllZeros = false;
-  unsigned NumUpdated = 0;
-  if (!ToC->isNullValue()) {
-    for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
-      Constant *Val = cast<Constant>(O->get());
-      if (Val == From) {
-        Val = ToC;
-        ++NumUpdated;
-      }
-      Values.push_back(Val);
-    }
-  } else {
-    isAllZeros = true;
-    for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
-      Constant *Val = cast<Constant>(O->get());
-      if (Val == From) {
-        Val = ToC;
-        ++NumUpdated;
-      }
-      Values.push_back(Val);
-      if (isAllZeros) isAllZeros = Val->isNullValue();
-    }
-  }
-  
-  Constant *Replacement = 0;
-  if (isAllZeros) {
-    Replacement =
-              From->getType()->getContext().getConstantAggregateZero(getType());
-  } else {
-    // Check to see if we have this array type already.
-    sys::SmartScopedWriter<true> Writer(*ConstantsLock);
-    bool Exists;
-    ArrayConstantsTy::MapTy::iterator I =
-      ArrayConstants->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 array, inserting it, replaceallusesof'ing the
-      // old with the new, then deleting the old... just update the current one
-      // in place!
-      ArrayConstants->MoveConstantToNewSlot(this, I);
-      
-      // Update to the new value.  Optimize for the case when we have a single
-      // operand that we're changing, but handle bulk updates efficiently.
-      if (NumUpdated == 1) {
-        unsigned OperandToUpdate = U-OperandList;
-        assert(getOperand(OperandToUpdate) == From &&
-               "ReplaceAllUsesWith broken!");
-        setOperand(OperandToUpdate, ToC);
-      } else {
-        for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
-          if (getOperand(i) == From)
-            setOperand(i, ToC);
-      }
-      return;
-    }
-  }
+  Constant *Replacement =
+    getType()->getContext().replaceUsesOfWithOnConstant(this, From, To, U);
+  if (!Replacement) return;
  
   // Otherwise, I do need to replace this with an existing value.
   assert(Replacement != this && "I didn't contain From!");
index 782a87b04b0da402428c3f3b9bd5a1d4914588d8..7194c2e15854e843487c508cd1575fa47f1feaae 100644 (file)
@@ -174,7 +174,7 @@ ConstantAggregateZero* LLVMContext::getConstantAggregateZero(const Type* Ty) {
 // ConstantArray accessors.
 Constant* LLVMContext::getConstantArray(const ArrayType* T,
                                         const std::vector<Constant*>& V) {
-  return ConstantArray::get(T, V);
+  return pImpl->getConstantArray(T, V);
 }
 
 Constant* LLVMContext::getConstantArray(const ArrayType* T,
@@ -652,4 +652,13 @@ void LLVMContext::erase(MDNode *M) {
 
 void LLVMContext::erase(ConstantAggregateZero *Z) {
   pImpl->erase(Z);
+}
+
+void LLVMContext::erase(ConstantArray *C) {
+  pImpl->erase(C);
+}
+
+Constant *LLVMContext::replaceUsesOfWithOnConstant(ConstantArray *CA,
+                                               Value *From, Value *To, Use *U) {
+  return pImpl->replaceUsesOfWithOnConstant(CA, From, To, U);
 }
\ No newline at end of file
index c72e3cbf5df0e6ce601d6bc00aff4244e81d04cd..5441f1ab88b57273405fe50b80fd54013ed88281 100644 (file)
@@ -21,6 +21,14 @@ using namespace llvm;
 
 static char getValType(ConstantAggregateZero *CPZ) { return 0; }
 
+static std::vector<Constant*> getValType(ConstantArray *CA) {
+  std::vector<Constant*> Elements;
+  Elements.reserve(CA->getNumOperands());
+  for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
+    Elements.push_back(cast<Constant>(CA->getOperand(i)));
+  return Elements;
+}
+
 namespace llvm {
 template<typename T, typename Alloc>
 struct VISIBILITY_HIDDEN ConstantTraits< std::vector<T, Alloc> > {
@@ -61,11 +69,25 @@ struct ConvertConstantType<ConstantAggregateZero, Type> {
     OldC->destroyConstant();     // This constant is now dead, destroy it.
   }
 };
+
+template<>
+struct ConvertConstantType<ConstantArray, ArrayType> {
+  static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
+    // Make everyone now use a constant of the new type...
+    std::vector<Constant*> C;
+    for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
+      C.push_back(cast<Constant>(OldC->getOperand(i)));
+    Constant *New = NewTy->getContext().getConstantArray(NewTy, C);
+    assert(New != OldC && "Didn't replace constant??");
+    OldC->uncheckedReplaceAllUsesWith(New);
+    OldC->destroyConstant();    // This constant is now dead, destroy it.
+  }
+};
 }
   
 template<class ValType, class TypeClass, class ConstantClass,
          bool HasLargeKey  /*true for arrays and structs*/ >
-class VISIBILITY_HIDDEN ContextValueMap : public AbstractTypeUser {
+class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser {
 public:
   typedef std::pair<const Type*, ValType> MapKey;
   typedef std::map<MapKey, Constant *> MapTy;
@@ -300,11 +322,13 @@ public:
 
 LLVMContextImpl::LLVMContextImpl(LLVMContext &C) :
     Context(C), TheTrueVal(0), TheFalseVal(0) {
-  AggZeroConstants = new ContextValueMap<char, Type, ConstantAggregateZero>();
+  AggZeroConstants = new ValueMap<char, Type, ConstantAggregateZero>();
+  ArrayConstants = new ArrayConstantsTy();
 }
 
 LLVMContextImpl::~LLVMContextImpl() {
   delete AggZeroConstants;
+  delete ArrayConstants;
 }
 
 // Get a ConstantInt from an APInt. Note that the value stored in the DenseMap 
@@ -413,6 +437,25 @@ LLVMContextImpl::getConstantAggregateZero(const Type *Ty) {
   return AggZeroConstants->getOrCreate(Ty, 0);
 }
 
+Constant *LLVMContextImpl::getConstantArray(const ArrayType *Ty,
+                             const std::vector<Constant*> &V) {
+  // If this is an all-zero array, return a ConstantAggregateZero object
+  if (!V.empty()) {
+    Constant *C = V[0];
+    if (!C->isNullValue()) {
+      // Implicitly locked.
+      return ArrayConstants->getOrCreate(Ty, V);
+    }
+    for (unsigned i = 1, e = V.size(); i != e; ++i)
+      if (V[i] != C) {
+        // Implicitly locked.
+        return ArrayConstants->getOrCreate(Ty, V);
+      }
+  }
+  
+  return Context.getConstantAggregateZero(Ty);
+}
+
 // *** erase methods ***
 
 void LLVMContextImpl::erase(MDString *M) {
@@ -428,3 +471,87 @@ void LLVMContextImpl::erase(MDNode *M) {
 void LLVMContextImpl::erase(ConstantAggregateZero *Z) {
   AggZeroConstants->remove(Z);
 }
+
+void LLVMContextImpl::erase(ConstantArray *C) {
+  ArrayConstants->remove(C);
+}
+
+// *** RAUW helpers ***
+Constant *LLVMContextImpl::replaceUsesOfWithOnConstant(ConstantArray *CA,
+                                               Value *From, Value *To, Use *U) {
+  assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
+  Constant *ToC = cast<Constant>(To);
+
+  std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup;
+  Lookup.first.first = CA->getType();
+  Lookup.second = CA;
+
+  std::vector<Constant*> &Values = Lookup.first.second;
+  Values.reserve(CA->getNumOperands());  // Build replacement array.
+
+  // Fill values with the modified operands of the constant array.  Also, 
+  // compute whether this turns into an all-zeros array.
+  bool isAllZeros = false;
+  unsigned NumUpdated = 0;
+  if (!ToC->isNullValue()) {
+    for (Use *O = CA->OperandList, *E = CA->OperandList + CA->getNumOperands();
+         O != E; ++O) {
+      Constant *Val = cast<Constant>(O->get());
+      if (Val == From) {
+        Val = ToC;
+        ++NumUpdated;
+      }
+      Values.push_back(Val);
+    }
+  } else {
+    isAllZeros = true;
+    for (Use *O = CA->OperandList, *E = CA->OperandList + CA->getNumOperands();
+         O != E; ++O) {
+      Constant *Val = cast<Constant>(O->get());
+      if (Val == From) {
+        Val = ToC;
+        ++NumUpdated;
+      }
+      Values.push_back(Val);
+      if (isAllZeros) isAllZeros = Val->isNullValue();
+    }
+  }
+  
+  Constant *Replacement = 0;
+  if (isAllZeros) {
+    Replacement = Context.getConstantAggregateZero(CA->getType());
+  } else {
+    // Check to see if we have this array type already.
+    sys::SmartScopedWriter<true> Writer(ConstantsLock);
+    bool Exists;
+    ArrayConstantsTy::MapTy::iterator I =
+      ArrayConstants->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 array, inserting it, replaceallusesof'ing the
+      // old with the new, then deleting the old... just update the current one
+      // in place!
+      ArrayConstants->MoveConstantToNewSlot(CA, I);
+      
+      // Update to the new value.  Optimize for the case when we have a single
+      // operand that we're changing, but handle bulk updates efficiently.
+      if (NumUpdated == 1) {
+        unsigned OperandToUpdate = U - CA->OperandList;
+        assert(CA->getOperand(OperandToUpdate) == From &&
+               "ReplaceAllUsesWith broken!");
+        CA->setOperand(OperandToUpdate, ToC);
+      } else {
+        for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
+          if (CA->getOperand(i) == From)
+            CA->setOperand(i, ToC);
+      }
+      return 0;
+    }
+  }
+  
+  return Replacement;
+}
+
index e2c2537deb17770d2569c67fd2ccbf545397cbf8..03f0e7b1414bfd667fab0796902f5d107a09c5dd 100644 (file)
@@ -30,7 +30,7 @@
 
 template<class ValType, class TypeClass, class ConstantClass,
          bool HasLargeKey = false  /*true for arrays and structs*/ >
-class ContextValueMap;
+class ValueMap;
 
 namespace llvm {
 template<class ValType>
@@ -111,7 +111,11 @@ class LLVMContextImpl {
   
   FoldingSet<MDNode> MDNodeSet;
   
-  ContextValueMap<char, Type, ConstantAggregateZero> *AggZeroConstants;
+  ValueMap<char, Type, ConstantAggregateZero> *AggZeroConstants;
+  
+  typedef ValueMap<std::vector<Constant*>, ArrayType, 
+    ConstantArray, true /*largekey*/> ArrayConstantsTy;
+  ArrayConstantsTy *ArrayConstants;
   
   LLVMContext &Context;
   ConstantInt *TheTrueVal;
@@ -135,6 +139,9 @@ public:
   
   ConstantAggregateZero *getConstantAggregateZero(const Type *Ty);
   
+  Constant *getConstantArray(const ArrayType *Ty,
+                             const std::vector<Constant*> &V);
+  
   ConstantInt *getTrue() {
     if (TheTrueVal)
       return TheTrueVal;
@@ -152,6 +159,12 @@ public:
   void erase(MDString *M);
   void erase(MDNode *M);
   void erase(ConstantAggregateZero *Z);
+  void erase(ConstantArray *C);
+  
+  // RAUW helpers
+  
+  Constant *replaceUsesOfWithOnConstant(ConstantArray *CA, Value *From,
+                                             Value *To, Use *U);
 };
 
 }