From: Owen Anderson Date: Mon, 10 Aug 2009 18:16:08 +0000 (+0000) Subject: Change the MDNode uniquing to a ValueMap, at Devang's request. X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=0631fce85050f8f633e5fd032875d9151e0db4da;p=oota-llvm.git Change the MDNode uniquing to a ValueMap, at Devang's request. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78577 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/Constants.h b/include/llvm/Constants.h index 32eac7b4410..a3659499373 100644 --- a/include/llvm/Constants.h +++ b/include/llvm/Constants.h @@ -38,7 +38,7 @@ class VectorType; template struct ConstantCreator; template -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 > >; - friend struct ConvertConstantType; + friend struct ConvertType; protected: ConstantExpr(const Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps) diff --git a/include/llvm/Metadata.h b/include/llvm/Metadata.h index d383fc4357b..ab2c073cad3 100644 --- a/include/llvm/Metadata.h +++ b/include/llvm/Metadata.h @@ -28,6 +28,8 @@ namespace llvm { class Constant; struct LLVMContext; +template +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 Node; + + friend struct ConstantCreator >; protected: explicit MDNode(Value*const* Vals, unsigned NumVals); public: diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp index 161afe47e2a..43cb39016f9 100644 --- a/lib/VMCore/Constants.cpp +++ b/lib/VMCore/Constants.cpp @@ -1803,7 +1803,7 @@ void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To, pImpl->ArrayConstants.InsertOrGetItem(Lookup, Exists); if (Exists) { - Replacement = I->second; + Replacement = cast(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(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 diff --git a/lib/VMCore/ConstantsContext.h b/lib/VMCore/ConstantsContext.h index 90a2b6295a8..fc2d5af991c 100644 --- a/lib/VMCore/ConstantsContext.h +++ b/lib/VMCore/ConstantsContext.h @@ -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 -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 { }; template<> -struct ConvertConstantType { +struct ConvertType { static void convert(ConstantExpr *OldC, const Type *NewTy) { Constant *New; switch (OldC->getOpcode()) { @@ -443,7 +444,14 @@ struct ConstantCreator { }; template<> -struct ConvertConstantType { +struct ConstantCreator > { + static MDNode *create(const Type* Ty, const std::vector &V) { + return new MDNode(V.data(), V.size()); + } +}; + +template<> +struct ConvertType { static void convert(ConstantVector *OldC, const VectorType *NewTy) { // Make everyone now use a constant of the new type... std::vector C; @@ -457,7 +465,7 @@ struct ConvertConstantType { }; template<> -struct ConvertConstantType { +struct ConvertType { 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 { }; template<> -struct ConvertConstantType { +struct ConvertType { static void convert(ConstantArray *OldC, const ArrayType *NewTy) { // Make everyone now use a constant of the new type... std::vector C; @@ -482,7 +490,7 @@ struct ConvertConstantType { }; template<> -struct ConvertConstantType { +struct ConvertType { static void convert(ConstantStruct *OldC, const StructType *NewTy) { // Make everyone now use a constant of the new type... std::vector C; @@ -505,7 +513,7 @@ struct ConstantCreator { }; template<> -struct ConvertConstantType { +struct ConvertType { 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 { }; template<> -struct ConvertConstantType { +struct ConvertType { 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 MapKey; - typedef std::map MapTy; - typedef std::map InverseMapTy; + typedef std::map MapTy; + typedef std::map InverseMapTy; typedef std::map 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::convert( + ConvertType::convert( static_cast(I->second->second), cast(NewTy)); diff --git a/lib/VMCore/LLVMContextImpl.h b/lib/VMCore/LLVMContextImpl.h index 966f54b3e22..88489cbce93 100644 --- a/lib/VMCore/LLVMContextImpl.h +++ b/lib/VMCore/LLVMContextImpl.h @@ -103,9 +103,9 @@ struct LLVMContextImpl { StringMap MDStringCache; - FoldingSet MDNodeSet; - ValueMap AggZeroConstants; + + ValueMap, Type, MDNode> MDNodeSet; typedef ValueMap, ArrayType, ConstantArray, true /*largekey*/> ArrayConstantsTy; diff --git a/lib/VMCore/Metadata.cpp b/lib/VMCore/Metadata.cpp index b44441fdb3f..eb4544c9824 100644 --- a/lib/VMCore/Metadata.cpp +++ b/lib/VMCore/Metadata.cpp @@ -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 V; + V.reserve(NumVals); + for (unsigned i = 0; i < NumVals; ++i) + V.push_back(Vals[i]); - if (!N) { - sys::SmartScopedWriter 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.