From 2a82d82936729b02fe1bbdcbfe764a61b8999be1 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Mon, 23 Jan 2012 08:42:38 +0000 Subject: [PATCH] Replace a use of ConstantUniqueMap for CAZ constants with a simple DenseMap. Now that the type system rewrite has landed, there is no need for its complexity and std::map'ness. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@148691 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/VMCore/Constants.cpp | 23 +++++++++++++++++++---- lib/VMCore/ConstantsContext.h | 15 --------------- lib/VMCore/LLVMContextImpl.cpp | 2 +- lib/VMCore/LLVMContextImpl.h | 4 +++- 4 files changed, 23 insertions(+), 21 deletions(-) diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp index 9657cd28c1f..d04298f1496 100644 --- a/lib/VMCore/Constants.cpp +++ b/lib/VMCore/Constants.cpp @@ -993,18 +993,33 @@ bool ConstantFP::isValueValidForType(Type *Ty, const APFloat& Val) { //===----------------------------------------------------------------------===// // Factory Function Implementation -ConstantAggregateZero* ConstantAggregateZero::get(Type* Ty) { +ConstantAggregateZero *ConstantAggregateZero::get(Type *Ty) { assert((Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy()) && "Cannot create an aggregate zero of non-aggregate type!"); - LLVMContextImpl *pImpl = Ty->getContext().pImpl; - return pImpl->AggZeroConstants.getOrCreate(Ty, 0); + OwningPtr &Entry = + Ty->getContext().pImpl->CAZConstants[Ty]; + if (Entry == 0) + Entry.reset(new ConstantAggregateZero(Ty)); + + return Entry.get(); } /// destroyConstant - Remove the constant from the constant table... /// void ConstantAggregateZero::destroyConstant() { - getType()->getContext().pImpl->AggZeroConstants.remove(this); + // Drop ownership of the CAZ object before removing the entry so that it + // doesn't get double deleted. + LLVMContextImpl::CAZMapTy &CAZConstants = getContext().pImpl->CAZConstants; + LLVMContextImpl::CAZMapTy::iterator I = CAZConstants.find(getType()); + assert(I != CAZConstants.end() && "CAZ object not in uniquing map"); + I->second.take(); + + // Actually remove the entry from the DenseMap now, which won't free the + // constant. + CAZConstants.erase(I); + + // Free the constant and any dangling references to it. destroyConstantImpl(); } diff --git a/lib/VMCore/ConstantsContext.h b/lib/VMCore/ConstantsContext.h index 4ee4296d448..fec2be58cfc 100644 --- a/lib/VMCore/ConstantsContext.h +++ b/lib/VMCore/ConstantsContext.h @@ -477,13 +477,6 @@ struct ConstantKeyData { } }; -// ConstantAggregateZero does not take extra "value" argument... -template -struct ConstantCreator { - static ConstantAggregateZero *create(Type *Ty, const ValType &V){ - return new ConstantAggregateZero(Ty); - } -}; template<> struct ConstantKeyData { @@ -497,14 +490,6 @@ struct ConstantKeyData { } }; -template<> -struct ConstantKeyData { - typedef char ValType; - static ValType getValType(ConstantAggregateZero *C) { - return 0; - } -}; - template<> struct ConstantKeyData { typedef std::vector ValType; diff --git a/lib/VMCore/LLVMContextImpl.cpp b/lib/VMCore/LLVMContextImpl.cpp index b0dd680926d..7ab3ccec62f 100644 --- a/lib/VMCore/LLVMContextImpl.cpp +++ b/lib/VMCore/LLVMContextImpl.cpp @@ -70,7 +70,7 @@ LLVMContextImpl::~LLVMContextImpl() { ArrayConstants.freeConstants(); StructConstants.freeConstants(); VectorConstants.freeConstants(); - AggZeroConstants.freeConstants(); + CAZConstants.clear(); NullPtrConstants.freeConstants(); UndefValueConstants.freeConstants(); InlineAsms.freeConstants(); diff --git a/lib/VMCore/LLVMContextImpl.h b/lib/VMCore/LLVMContextImpl.h index 30f9d469878..f963f639211 100644 --- a/lib/VMCore/LLVMContextImpl.h +++ b/lib/VMCore/LLVMContextImpl.h @@ -27,6 +27,7 @@ #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/FoldingSet.h" +#include "llvm/ADT/OwningPtr.h" #include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/StringMap.h" #include @@ -138,7 +139,8 @@ public: // on Context destruction. SmallPtrSet NonUniquedMDNodes; - ConstantUniqueMap AggZeroConstants; + typedef DenseMap > CAZMapTy; + CAZMapTy CAZConstants; typedef ConstantUniqueMap, ArrayRef, ArrayType, ConstantArray, true /*largekey*/> ArrayConstantsTy;