[llvm-commits] [llvm] r76912 - in /llvm/trunk: include/llvm/Constants.h include/llvm/LLVMContext.h lib/VMCore/Constants.cpp lib/VMCore/LLVMContext.cpp lib/VMCore/LLVMContextImpl.cpp lib/VMCore/LLVMContextImpl.h

Owen Anderson resistor at mac.com
Thu Jul 23 16:25:44 PDT 2009


Author: resistor
Date: Thu Jul 23 18:25:33 2009
New Revision: 76912

URL: http://llvm.org/viewvc/llvm-project?rev=76912&view=rev
Log:
Privatize the ConstantStruct table.

Modified:
    llvm/trunk/include/llvm/Constants.h
    llvm/trunk/include/llvm/LLVMContext.h
    llvm/trunk/lib/VMCore/Constants.cpp
    llvm/trunk/lib/VMCore/LLVMContext.cpp
    llvm/trunk/lib/VMCore/LLVMContextImpl.cpp
    llvm/trunk/lib/VMCore/LLVMContextImpl.h

Modified: llvm/trunk/include/llvm/Constants.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Constants.h?rev=76912&r1=76911&r2=76912&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Constants.h (original)
+++ llvm/trunk/include/llvm/Constants.h Thu Jul 23 18:25:33 2009
@@ -344,13 +344,10 @@
   friend struct ConstantCreator<ConstantStruct, StructType,
                                     std::vector<Constant*> >;
   ConstantStruct(const ConstantStruct &);      // DO NOT IMPLEMENT
+  friend class LLVMContextImpl;
 protected:
   ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
 public:
-  /// get() - Static factory methods - Return objects of the specified value
-  ///
-  static Constant *get(const StructType *T, const std::vector<Constant*> &V);
-  
   /// Transparently provide more efficient getOperand methods.
   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
 

Modified: llvm/trunk/include/llvm/LLVMContext.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/LLVMContext.h?rev=76912&r1=76911&r2=76912&view=diff

==============================================================================
--- llvm/trunk/include/llvm/LLVMContext.h (original)
+++ llvm/trunk/include/llvm/LLVMContext.h Thu Jul 23 18:25:33 2009
@@ -275,10 +275,13 @@
   void erase(MDNode *M);
   void erase(ConstantAggregateZero *Z);
   void erase(ConstantArray *Z);
+  void erase(ConstantStruct *S);
   
   // RAUW helpers
   Constant *replaceUsesOfWithOnConstant(ConstantArray *CA,
                                              Value *From, Value *To, Use *U);
+  Constant *replaceUsesOfWithOnConstant(ConstantStruct *CS, Value *From,
+                                        Value *To, Use *U);
 };
 
 /// FOR BACKWARDS COMPATIBILITY - Returns a global context.

Modified: llvm/trunk/lib/VMCore/Constants.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Constants.cpp?rev=76912&r1=76911&r2=76912&view=diff

==============================================================================
--- llvm/trunk/lib/VMCore/Constants.cpp (original)
+++ llvm/trunk/lib/VMCore/Constants.cpp Thu Jul 23 18:25:33 2009
@@ -1096,50 +1096,14 @@
 //
 
 namespace llvm {
-  template<>
-  struct ConvertConstantType<ConstantStruct, StructType> {
-    static void convert(ConstantStruct *OldC, const StructType *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 = ConstantStruct::get(NewTy, C);
-      assert(New != OldC && "Didn't replace constant??");
-
-      OldC->uncheckedReplaceAllUsesWith(New);
-      OldC->destroyConstant();    // This constant is now dead, destroy it.
-    }
-  };
-}
 
-typedef ValueMap<std::vector<Constant*>, StructType,
-                 ConstantStruct, true /*largekey*/> StructConstantsTy;
-static ManagedStatic<StructConstantsTy> StructConstants;
-
-static std::vector<Constant*> getValType(ConstantStruct *CS) {
-  std::vector<Constant*> Elements;
-  Elements.reserve(CS->getNumOperands());
-  for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
-    Elements.push_back(cast<Constant>(CS->getOperand(i)));
-  return Elements;
-}
-
-Constant *ConstantStruct::get(const StructType *Ty,
-                              const std::vector<Constant*> &V) {
-  // Create a ConstantAggregateZero value if all elements are zeros...
-  for (unsigned i = 0, e = V.size(); i != e; ++i)
-    if (!V[i]->isNullValue())
-      // Implicitly locked.
-      return StructConstants->getOrCreate(Ty, V);
-
-  return Ty->getContext().getConstantAggregateZero(Ty);
 }
 
 // destroyConstant - Remove the constant from the constant table...
 //
 void ConstantStruct::destroyConstant() {
   // Implicitly locked.
-  StructConstants->remove(this);
+  getType()->getContext().erase(this);
   destroyConstantImpl();
 }
 
@@ -2112,61 +2076,9 @@
 
 void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
                                                  Use *U) {
-  assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
-  Constant *ToC = cast<Constant>(To);
-
-  unsigned OperandToUpdate = U-OperandList;
-  assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
-
-  std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
-  Lookup.first.first = getType();
-  Lookup.second = this;
-  std::vector<Constant*> &Values = Lookup.first.second;
-  Values.reserve(getNumOperands());  // Build replacement struct.
-  
-  
-  // Fill values with the modified operands of the constant struct.  Also, 
-  // compute whether this turns into an all-zeros struct.
-  bool isAllZeros = false;
-  if (!ToC->isNullValue()) {
-    for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
-      Values.push_back(cast<Constant>(O->get()));
-  } else {
-    isAllZeros = true;
-    for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
-      Constant *Val = cast<Constant>(O->get());
-      Values.push_back(Val);
-      if (isAllZeros) isAllZeros = Val->isNullValue();
-    }
-  }
-  Values[OperandToUpdate] = ToC;
-  
-  Constant *Replacement = 0;
-  if (isAllZeros) {
-    Replacement = getType()->getContext().getConstantAggregateZero(getType());
-  } else {
-    // Check to see if we have this array type already.
-    sys::SmartScopedWriter<true> Writer(*ConstantsLock);
-    bool Exists;
-    StructConstantsTy::MapTy::iterator I =
-      StructConstants->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 struct, inserting it, replaceallusesof'ing the
-      // old with the new, then deleting the old... just update the current one
-      // in place!
-      StructConstants->MoveConstantToNewSlot(this, I);
-      
-      // Update to the new value.
-      setOperand(OperandToUpdate, ToC);
-      return;
-    }
-  }
-  
-  assert(Replacement != this && "I didn't contain From!");
+  Constant* Replacement =
+    getType()->getContext().replaceUsesOfWithOnConstant(this, From, To, U);
+  if (!Replacement) return;
   
   // Everyone using this now uses the replacement.
   uncheckedReplaceAllUsesWith(Replacement);

Modified: llvm/trunk/lib/VMCore/LLVMContext.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/LLVMContext.cpp?rev=76912&r1=76911&r2=76912&view=diff

==============================================================================
--- llvm/trunk/lib/VMCore/LLVMContext.cpp (original)
+++ llvm/trunk/lib/VMCore/LLVMContext.cpp Thu Jul 23 18:25:33 2009
@@ -146,7 +146,7 @@
 // ConstantStruct accessors.
 Constant* LLVMContext::getConstantStruct(const StructType* T,
                                          const std::vector<Constant*>& V) {
-  return ConstantStruct::get(T, V);
+  return pImpl->getConstantStruct(T, V);
 }
 
 Constant* LLVMContext::getConstantStruct(const std::vector<Constant*>& V,
@@ -658,7 +658,16 @@
   pImpl->erase(C);
 }
 
+void LLVMContext::erase(ConstantStruct *S) {
+  pImpl->erase(S);
+}
+
 Constant *LLVMContext::replaceUsesOfWithOnConstant(ConstantArray *CA,
                                                Value *From, Value *To, Use *U) {
   return pImpl->replaceUsesOfWithOnConstant(CA, From, To, U);
 }
+
+Constant *LLVMContext::replaceUsesOfWithOnConstant(ConstantStruct *CS,
+                                               Value *From, Value *To, Use *U) {
+  return pImpl->replaceUsesOfWithOnConstant(CS, From, To, U);
+}

Modified: llvm/trunk/lib/VMCore/LLVMContextImpl.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/LLVMContextImpl.cpp?rev=76912&r1=76911&r2=76912&view=diff

==============================================================================
--- llvm/trunk/lib/VMCore/LLVMContextImpl.cpp (original)
+++ llvm/trunk/lib/VMCore/LLVMContextImpl.cpp Thu Jul 23 18:25:33 2009
@@ -29,6 +29,14 @@
   return Elements;
 }
 
+static std::vector<Constant*> getValType(ConstantStruct *CS) {
+  std::vector<Constant*> Elements;
+  Elements.reserve(CS->getNumOperands());
+  for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
+    Elements.push_back(cast<Constant>(CS->getOperand(i)));
+  return Elements;
+}
+
 namespace llvm {
 template<typename T, typename Alloc>
 struct VISIBILITY_HIDDEN ConstantTraits< std::vector<T, Alloc> > {
@@ -83,6 +91,21 @@
     OldC->destroyConstant();    // This constant is now dead, destroy it.
   }
 };
+
+template<>
+struct ConvertConstantType<ConstantStruct, StructType> {
+  static void convert(ConstantStruct *OldC, const StructType *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().getConstantStruct(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,
@@ -324,11 +347,13 @@
     Context(C), TheTrueVal(0), TheFalseVal(0) {
   AggZeroConstants = new ValueMap<char, Type, ConstantAggregateZero>();
   ArrayConstants = new ArrayConstantsTy();
+  StructConstants = new StructConstantsTy();
 }
 
 LLVMContextImpl::~LLVMContextImpl() {
   delete AggZeroConstants;
   delete ArrayConstants;
+  delete StructConstants;
 }
 
 // Get a ConstantInt from an APInt. Note that the value stored in the DenseMap 
@@ -456,6 +481,17 @@
   return Context.getConstantAggregateZero(Ty);
 }
 
+Constant *LLVMContextImpl::getConstantStruct(const StructType *Ty,
+                              const std::vector<Constant*> &V) {
+  // Create a ConstantAggregateZero value if all elements are zeros...
+  for (unsigned i = 0, e = V.size(); i != e; ++i)
+    if (!V[i]->isNullValue())
+      // Implicitly locked.
+      return StructConstants->getOrCreate(Ty, V);
+
+  return Context.getConstantAggregateZero(Ty);
+}
+
 // *** erase methods ***
 
 void LLVMContextImpl::erase(MDString *M) {
@@ -477,7 +513,12 @@
   ArrayConstants->remove(C);
 }
 
+void LLVMContextImpl::erase(ConstantStruct *S) {
+  StructConstants->remove(S);
+}
+
 // *** RAUW helpers ***
+
 Constant *LLVMContextImpl::replaceUsesOfWithOnConstant(ConstantArray *CA,
                                                Value *From, Value *To, Use *U) {
   assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
@@ -556,3 +597,66 @@
   return Replacement;
 }
 
+Constant *LLVMContextImpl::replaceUsesOfWithOnConstant(ConstantStruct *CS,
+                                               Value *From, Value *To, Use *U) {
+  assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
+  Constant *ToC = cast<Constant>(To);
+
+  unsigned OperandToUpdate = U - CS->OperandList;
+  assert(CS->getOperand(OperandToUpdate) == From &&
+         "ReplaceAllUsesWith broken!");
+
+  std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
+  Lookup.first.first = CS->getType();
+  Lookup.second = CS;
+  std::vector<Constant*> &Values = Lookup.first.second;
+  Values.reserve(CS->getNumOperands());  // Build replacement struct.
+  
+  
+  // Fill values with the modified operands of the constant struct.  Also, 
+  // compute whether this turns into an all-zeros struct.
+  bool isAllZeros = false;
+  if (!ToC->isNullValue()) {
+    for (Use *O = CS->OperandList, *E = CS->OperandList + CS->getNumOperands(); 
+         O != E; ++O)
+      Values.push_back(cast<Constant>(O->get()));
+  } else {
+    isAllZeros = true;
+    for (Use *O = CS->OperandList, *E = CS->OperandList + CS->getNumOperands(); 
+         O != E; ++O) {
+      Constant *Val = cast<Constant>(O->get());
+      Values.push_back(Val);
+      if (isAllZeros) isAllZeros = Val->isNullValue();
+    }
+  }
+  Values[OperandToUpdate] = ToC;
+  
+  Constant *Replacement = 0;
+  if (isAllZeros) {
+    Replacement = Context.getConstantAggregateZero(CS->getType());
+  } else {
+    // Check to see if we have this array type already.
+    sys::SmartScopedWriter<true> Writer(ConstantsLock);
+    bool Exists;
+    StructConstantsTy::MapTy::iterator I =
+      StructConstants->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 struct, inserting it, replaceallusesof'ing the
+      // old with the new, then deleting the old... just update the current one
+      // in place!
+      StructConstants->MoveConstantToNewSlot(CS, I);
+      
+      // Update to the new value.
+      CS->setOperand(OperandToUpdate, ToC);
+      return 0;
+    }
+  }
+  
+  assert(Replacement != CS && "I didn't contain From!");
+  
+  return Replacement;
+}
\ No newline at end of file

Modified: llvm/trunk/lib/VMCore/LLVMContextImpl.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/LLVMContextImpl.h?rev=76912&r1=76911&r2=76912&view=diff

==============================================================================
--- llvm/trunk/lib/VMCore/LLVMContextImpl.h (original)
+++ llvm/trunk/lib/VMCore/LLVMContextImpl.h Thu Jul 23 18:25:33 2009
@@ -27,6 +27,7 @@
 #include "llvm/ADT/FoldingSet.h"
 #include "llvm/ADT/StringMap.h"
 #include <map>
+#include <vector>
 
 template<class ValType, class TypeClass, class ConstantClass,
          bool HasLargeKey = false  /*true for arrays and structs*/ >
@@ -117,6 +118,10 @@
     ConstantArray, true /*largekey*/> ArrayConstantsTy;
   ArrayConstantsTy *ArrayConstants;
   
+  typedef ValueMap<std::vector<Constant*>, StructType,
+                   ConstantStruct, true /*largekey*/> StructConstantsTy;
+  StructConstantsTy *StructConstants;
+  
   LLVMContext &Context;
   ConstantInt *TheTrueVal;
   ConstantInt *TheFalseVal;
@@ -142,6 +147,9 @@
   Constant *getConstantArray(const ArrayType *Ty,
                              const std::vector<Constant*> &V);
   
+  Constant *getConstantStruct(const StructType *Ty, 
+                              const std::vector<Constant*> &V);
+  
   ConstantInt *getTrue() {
     if (TheTrueVal)
       return TheTrueVal;
@@ -160,11 +168,14 @@
   void erase(MDNode *M);
   void erase(ConstantAggregateZero *Z);
   void erase(ConstantArray *C);
+  void erase(ConstantStruct *S);
   
   // RAUW helpers
   
   Constant *replaceUsesOfWithOnConstant(ConstantArray *CA, Value *From,
                                              Value *To, Use *U);
+  Constant *replaceUsesOfWithOnConstant(ConstantStruct *CS, Value *From,
+                                        Value *To, Use *U);
 };
 
 }





More information about the llvm-commits mailing list