Change the MDNode uniquing to a ValueMap, at Devang's request.
authorOwen Anderson <resistor@mac.com>
Mon, 10 Aug 2009 18:16:08 +0000 (18:16 +0000)
committerOwen Anderson <resistor@mac.com>
Mon, 10 Aug 2009 18:16:08 +0000 (18:16 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78577 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Constants.h
include/llvm/Metadata.h
lib/VMCore/Constants.cpp
lib/VMCore/ConstantsContext.h
lib/VMCore/LLVMContextImpl.h
lib/VMCore/Metadata.cpp

index 32eac7b4410970174d19cf6cfaf153e75d1c83a8..a365949937304e44a92fe449c667be0bb278e5ef 100644 (file)
@@ -38,7 +38,7 @@ class VectorType;
 template<class ConstantClass, class TypeClass, class ValType>
 struct ConstantCreator;
 template<class ConstantClass, class TypeClass>
-struct ConvertConstantType;
+struct ConvertType;
 
 //===----------------------------------------------------------------------===//
 /// This is the shared class of boolean and integer constants. This class 
@@ -552,7 +552,7 @@ public:
 class ConstantExpr : public Constant {
   friend struct ConstantCreator<ConstantExpr,Type,
                             std::pair<unsigned, std::vector<Constant*> > >;
-  friend struct ConvertConstantType<ConstantExpr, Type>;
+  friend struct ConvertType<ConstantExpr, Type>;
 
 protected:
   ConstantExpr(const Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
index d383fc4357bdb80d89e14f566211bd7d89ab7ecd..ab2c073cad3f7b1b816a505a90d02dac4f83d05a 100644 (file)
@@ -28,6 +28,8 @@
 namespace llvm {
 class Constant;
 struct LLVMContext;
+template<class ConstantClass, class TypeClass, class ValType>
+struct ConstantCreator;
 
 //===----------------------------------------------------------------------===//
 // MetadataBase  - A base class for MDNode, MDString and NamedMDNode.
@@ -115,6 +117,8 @@ class MDNode : public MetadataBase, public FoldingSetNode {
   unsigned getNumOperands() { return User::getNumOperands();  }
 
   SmallVector<WeakVH, 4> Node;
+  
+  friend struct ConstantCreator<MDNode, Type, std::vector<Value*> >;
 protected:
   explicit MDNode(Value*const* Vals, unsigned NumVals);
 public:
index 161afe47e2ab5b6b57b422bed9a6fcc9a6f852ce..43cb39016f9de2980d058aa96d90762f81b3cbab 100644 (file)
@@ -1803,7 +1803,7 @@ void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
       pImpl->ArrayConstants.InsertOrGetItem(Lookup, Exists);
     
     if (Exists) {
-      Replacement = I->second;
+      Replacement = cast<Constant>(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
@@ -1890,7 +1890,7 @@ void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
       pImpl->StructConstants.InsertOrGetItem(Lookup, Exists);
     
     if (Exists) {
-      Replacement = I->second;
+      Replacement = cast<Constant>(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
index 90a2b6295a80caabeec9fa060872db92fc8a52ab..fc2d5af991c2662eb056fc768ecebea26ce3ed21 100644 (file)
@@ -16,6 +16,7 @@
 #define LLVM_CONSTANTSCONTEXT_H
 
 #include "llvm/Instructions.h"
+#include "llvm/Metadata.h"
 #include "llvm/Operator.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/ErrorHandling.h"
@@ -339,7 +340,7 @@ struct ConstantCreator {
 };
 
 template<class ConstantClass, class TypeClass>
-struct ConvertConstantType {
+struct ConvertType {
   static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
     llvm_unreachable("This type cannot be converted!");
   }
@@ -390,7 +391,7 @@ struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
 };
 
 template<>
-struct ConvertConstantType<ConstantExpr, Type> {
+struct ConvertType<ConstantExpr, Type> {
   static void convert(ConstantExpr *OldC, const Type *NewTy) {
     Constant *New;
     switch (OldC->getOpcode()) {
@@ -443,7 +444,14 @@ struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
 };
 
 template<>
-struct ConvertConstantType<ConstantVector, VectorType> {
+struct ConstantCreator<MDNode, Type, std::vector<Value*> > {
+  static MDNode *create(const Type* Ty, const std::vector<Value*> &V) {
+    return new MDNode(V.data(), V.size());
+  }
+};
+
+template<>
+struct ConvertType<ConstantVector, VectorType> {
   static void convert(ConstantVector *OldC, const VectorType *NewTy) {
     // Make everyone now use a constant of the new type...
     std::vector<Constant*> C;
@@ -457,7 +465,7 @@ struct ConvertConstantType<ConstantVector, VectorType> {
 };
 
 template<>
-struct ConvertConstantType<ConstantAggregateZero, Type> {
+struct ConvertType<ConstantAggregateZero, Type> {
   static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
     // Make everyone now use a constant of the new type...
     Constant *New = ConstantAggregateZero::get(NewTy);
@@ -468,7 +476,7 @@ struct ConvertConstantType<ConstantAggregateZero, Type> {
 };
 
 template<>
-struct ConvertConstantType<ConstantArray, ArrayType> {
+struct ConvertType<ConstantArray, ArrayType> {
   static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
     // Make everyone now use a constant of the new type...
     std::vector<Constant*> C;
@@ -482,7 +490,7 @@ struct ConvertConstantType<ConstantArray, ArrayType> {
 };
 
 template<>
-struct ConvertConstantType<ConstantStruct, StructType> {
+struct ConvertType<ConstantStruct, StructType> {
   static void convert(ConstantStruct *OldC, const StructType *NewTy) {
     // Make everyone now use a constant of the new type...
     std::vector<Constant*> C;
@@ -505,7 +513,7 @@ struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
 };
 
 template<>
-struct ConvertConstantType<ConstantPointerNull, PointerType> {
+struct ConvertType<ConstantPointerNull, PointerType> {
   static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
     // Make everyone now use a constant of the new type...
     Constant *New = ConstantPointerNull::get(NewTy);
@@ -524,7 +532,7 @@ struct ConstantCreator<UndefValue, Type, ValType> {
 };
 
 template<>
-struct ConvertConstantType<UndefValue, Type> {
+struct ConvertType<UndefValue, Type> {
   static void convert(UndefValue *OldC, const Type *NewTy) {
     // Make everyone now use a constant of the new type.
     Constant *New = UndefValue::get(NewTy);
@@ -539,8 +547,8 @@ template<class ValType, class TypeClass, class ConstantClass,
 class ValueMap : public AbstractTypeUser {
 public:
   typedef std::pair<const Type*, ValType> MapKey;
-  typedef std::map<MapKey, Constant *> MapTy;
-  typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
+  typedef std::map<MapKey, Value *> MapTy;
+  typedef std::map<Value*, typename MapTy::iterator> InverseMapTy;
   typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
 private:
   /// Map - This is the main map from the element descriptor to the Constants.
@@ -749,8 +757,7 @@ public:
     // leaving will remove() itself, causing the AbstractTypeMapEntry to be
     // eliminated eventually.
     do {
-      ConvertConstantType<ConstantClass,
-                          TypeClass>::convert(
+      ConvertType<ConstantClass, TypeClass>::convert(
                               static_cast<ConstantClass *>(I->second->second),
                                               cast<TypeClass>(NewTy));
 
index 966f54b3e222d23876a83b3ed1a1cdf02bbf8410..88489cbce93162769c748bbe2d2a18057879f4bf 100644 (file)
@@ -103,9 +103,9 @@ struct LLVMContextImpl {
   
   StringMap<MDString*> MDStringCache;
   
-  FoldingSet<MDNode> MDNodeSet;
-  
   ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
+
+  ValueMap<std::vector<Value*>, Type, MDNode> MDNodeSet;
   
   typedef ValueMap<std::vector<Constant*>, ArrayType, 
     ConstantArray, true /*largekey*/> ArrayConstantsTy;
index b44441fdb3f2a7667395cebd255b6493a52fe492..eb4544c98249d7eaa410845da9a52c8e890131cc 100644 (file)
@@ -83,26 +83,12 @@ void MDNode::Profile(FoldingSetNodeID &ID) const {
 
 MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) {
   LLVMContextImpl *pImpl = Context.pImpl;
-  FoldingSetNodeID ID;
-  for (unsigned i = 0; i != NumVals; ++i)
-    ID.AddPointer(Vals[i]);
-
-  pImpl->ConstantsLock.reader_acquire();
-  void *InsertPoint;
-  MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
-  pImpl->ConstantsLock.reader_release();
+  std::vector<Value*> V;
+  V.reserve(NumVals);
+  for (unsigned i = 0; i < NumVals; ++i)
+    V.push_back(Vals[i]);
   
-  if (!N) {
-    sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
-    N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
-    if (!N) {
-      // InsertPoint will have been set by the FindNodeOrInsertPos call.
-      N = new MDNode(Vals, NumVals);
-      pImpl->MDNodeSet.InsertNode(N, InsertPoint);
-    }
-  }
-
-  return N;
+  return pImpl->MDNodeSet.getOrCreate(Type::MetadataTy, V);
 }
 
 /// dropAllReferences - Remove all uses and clear node vector.