diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp index 9657cd28c1f..e9b433b66c3 100644 --- a/lib/VMCore/Constants.cpp +++ b/lib/VMCore/Constants.cpp @@ -993,18 +993,21 @@ 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); + ConstantAggregateZero *&Entry = Ty->getContext().pImpl->CAZConstants[Ty]; + if (Entry == 0) + Entry = new ConstantAggregateZero(Ty); + + return Entry; } /// destroyConstant - Remove the constant from the constant table... /// void ConstantAggregateZero::destroyConstant() { - getType()->getContext().pImpl->AggZeroConstants.remove(this); + getContext().pImpl->CAZConstants.erase(getType()); destroyConstantImpl(); } @@ -1112,13 +1115,18 @@ Constant *ConstantVector::getSplatValue() const { // ConstantPointerNull *ConstantPointerNull::get(PointerType *Ty) { - return Ty->getContext().pImpl->NullPtrConstants.getOrCreate(Ty, 0); + ConstantPointerNull *&Entry = Ty->getContext().pImpl->CPNConstants[Ty]; + if (Entry == 0) + Entry = new ConstantPointerNull(Ty); + + return Entry; } // destroyConstant - Remove the constant from the constant table... // void ConstantPointerNull::destroyConstant() { - getType()->getContext().pImpl->NullPtrConstants.remove(this); + getContext().pImpl->CPNConstants.erase(getType()); + // Free the constant and any dangling references to it. destroyConstantImpl(); } @@ -1127,13 +1135,18 @@ void ConstantPointerNull::destroyConstant() { // UndefValue *UndefValue::get(Type *Ty) { - return Ty->getContext().pImpl->UndefValueConstants.getOrCreate(Ty, 0); + UndefValue *&Entry = Ty->getContext().pImpl->UVConstants[Ty]; + if (Entry == 0) + Entry = new UndefValue(Ty); + + return Entry; } // destroyConstant - Remove the constant from the constant table. // void UndefValue::destroyConstant() { - getType()->getContext().pImpl->UndefValueConstants.remove(this); + // Free the constant and any dangling references to it. + getContext().pImpl->UVConstants.erase(getType()); destroyConstantImpl(); } diff --git a/lib/VMCore/ConstantsContext.h b/lib/VMCore/ConstantsContext.h index 4ee4296d448..4bdeaa78302 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; @@ -529,37 +514,6 @@ struct ConstantKeyData { } }; -// ConstantPointerNull does not take extra "value" argument... -template -struct ConstantCreator { - static ConstantPointerNull *create(PointerType *Ty, const ValType &V){ - return new ConstantPointerNull(Ty); - } -}; - -template<> -struct ConstantKeyData { - typedef char ValType; - static ValType getValType(ConstantPointerNull *C) { - return 0; - } -}; - -// UndefValue does not take extra "value" argument... -template -struct ConstantCreator { - static UndefValue *create(Type *Ty, const ValType &V) { - return new UndefValue(Ty); - } -}; - -template<> -struct ConstantKeyData { - typedef char ValType; - static ValType getValType(UndefValue *C) { - return 0; - } -}; template<> struct ConstantCreator { diff --git a/lib/VMCore/LLVMContextImpl.cpp b/lib/VMCore/LLVMContextImpl.cpp index b0dd680926d..2eaebaceb1d 100644 --- a/lib/VMCore/LLVMContextImpl.cpp +++ b/lib/VMCore/LLVMContextImpl.cpp @@ -58,6 +58,8 @@ LLVMContextImpl::~LLVMContextImpl() { std::vector Modules(OwnedModules.begin(), OwnedModules.end()); DeleteContainerPointers(Modules); + // Free the constants. This is important to do here to ensure that they are + // freed before the LeakDetector is torn down. std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(), DropReferences()); std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(), @@ -70,9 +72,9 @@ LLVMContextImpl::~LLVMContextImpl() { ArrayConstants.freeConstants(); StructConstants.freeConstants(); VectorConstants.freeConstants(); - AggZeroConstants.freeConstants(); - NullPtrConstants.freeConstants(); - UndefValueConstants.freeConstants(); + DeleteContainerSeconds(CAZConstants); + DeleteContainerSeconds(CPNConstants); + DeleteContainerSeconds(UVConstants); InlineAsms.freeConstants(); DeleteContainerSeconds(IntConstants); DeleteContainerSeconds(FPConstants); diff --git a/lib/VMCore/LLVMContextImpl.h b/lib/VMCore/LLVMContextImpl.h index 30f9d469878..f8abdf17e8a 100644 --- a/lib/VMCore/LLVMContextImpl.h +++ b/lib/VMCore/LLVMContextImpl.h @@ -138,7 +138,7 @@ public: // on Context destruction. SmallPtrSet NonUniquedMDNodes; - ConstantUniqueMap AggZeroConstants; + DenseMap CAZConstants; typedef ConstantUniqueMap, ArrayRef, ArrayType, ConstantArray, true /*largekey*/> ArrayConstantsTy; @@ -152,9 +152,9 @@ public: VectorType, ConstantVector> VectorConstantsTy; VectorConstantsTy VectorConstants; - ConstantUniqueMap - NullPtrConstants; - ConstantUniqueMap UndefValueConstants; + DenseMap CPNConstants; + + DenseMap UVConstants; DenseMap , BlockAddress*> BlockAddresses; ConstantUniqueMap