[llvm-commits] [llvm] r148693 - in /llvm/trunk/lib/VMCore: Constants.cpp ConstantsContext.h LLVMContextImpl.cpp LLVMContextImpl.h

Chris Lattner sabre at nondot.org
Mon Jan 23 00:52:32 PST 2012


Author: lattner
Date: Mon Jan 23 02:52:32 2012
New Revision: 148693

URL: http://llvm.org/viewvc/llvm-project?rev=148693&view=rev
Log:
switch UndefValue and ConstantPointerNull over to DenseMap's for uniquing.

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

Modified: llvm/trunk/lib/VMCore/Constants.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Constants.cpp?rev=148693&r1=148692&r2=148693&view=diff
==============================================================================
--- llvm/trunk/lib/VMCore/Constants.cpp (original)
+++ llvm/trunk/lib/VMCore/Constants.cpp Mon Jan 23 02:52:32 2012
@@ -1127,13 +1127,29 @@
 //
 
 ConstantPointerNull *ConstantPointerNull::get(PointerType *Ty) {
-  return Ty->getContext().pImpl->NullPtrConstants.getOrCreate(Ty, 0);
+  OwningPtr<ConstantPointerNull> &Entry =
+    Ty->getContext().pImpl->CPNConstants[Ty];
+  if (Entry == 0)
+    Entry.reset(new ConstantPointerNull(Ty));
+  
+  return Entry.get();
 }
 
 // destroyConstant - Remove the constant from the constant table...
 //
 void ConstantPointerNull::destroyConstant() {
-  getType()->getContext().pImpl->NullPtrConstants.remove(this);
+  // Drop ownership of the CPN object before removing the entry so that it
+  // doesn't get double deleted.
+  LLVMContextImpl::CPNMapTy &CPNConstants = getContext().pImpl->CPNConstants;
+  LLVMContextImpl::CPNMapTy::iterator I = CPNConstants.find(getType());
+  assert(I != CPNConstants.end() && "CPN object not in uniquing map");
+  I->second.take();
+  
+  // Actually remove the entry from the DenseMap now, which won't free the
+  // constant.
+  CPNConstants.erase(I);
+  
+  // Free the constant and any dangling references to it.
   destroyConstantImpl();
 }
 
@@ -1142,13 +1158,28 @@
 //
 
 UndefValue *UndefValue::get(Type *Ty) {
-  return Ty->getContext().pImpl->UndefValueConstants.getOrCreate(Ty, 0);
+  OwningPtr<UndefValue> &Entry = Ty->getContext().pImpl->UVConstants[Ty];
+  if (Entry == 0)
+    Entry.reset(new UndefValue(Ty));
+  
+  return Entry.get();
 }
 
 // destroyConstant - Remove the constant from the constant table.
 //
 void UndefValue::destroyConstant() {
-  getType()->getContext().pImpl->UndefValueConstants.remove(this);
+  // Drop ownership of the object before removing the entry so that it
+  // doesn't get double deleted.
+  LLVMContextImpl::UVMapTy &UVConstants = getContext().pImpl->UVConstants;
+  LLVMContextImpl::UVMapTy::iterator I = UVConstants.find(getType());
+  assert(I != UVConstants.end() && "UV object not in uniquing map");
+  I->second.take();
+  
+  // Actually remove the entry from the DenseMap now, which won't free the
+  // constant.
+  UVConstants.erase(I);
+  
+  // Free the constant and any dangling references to it.
   destroyConstantImpl();
 }
 

Modified: llvm/trunk/lib/VMCore/ConstantsContext.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/ConstantsContext.h?rev=148693&r1=148692&r2=148693&view=diff
==============================================================================
--- llvm/trunk/lib/VMCore/ConstantsContext.h (original)
+++ llvm/trunk/lib/VMCore/ConstantsContext.h Mon Jan 23 02:52:32 2012
@@ -514,37 +514,6 @@
   }
 };
 
-// ConstantPointerNull does not take extra "value" argument...
-template<class ValType>
-struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
-  static ConstantPointerNull *create(PointerType *Ty, const ValType &V){
-    return new ConstantPointerNull(Ty);
-  }
-};
-
-template<>
-struct ConstantKeyData<ConstantPointerNull> {
-  typedef char ValType;
-  static ValType getValType(ConstantPointerNull *C) {
-    return 0;
-  }
-};
-
-// UndefValue does not take extra "value" argument...
-template<class ValType>
-struct ConstantCreator<UndefValue, Type, ValType> {
-  static UndefValue *create(Type *Ty, const ValType &V) {
-    return new UndefValue(Ty);
-  }
-};
-
-template<>
-struct ConstantKeyData<UndefValue> {
-  typedef char ValType;
-  static ValType getValType(UndefValue *C) {
-    return 0;
-  }
-};
 
 template<>
 struct ConstantCreator<InlineAsm, PointerType, InlineAsmKeyType> {

Modified: llvm/trunk/lib/VMCore/LLVMContextImpl.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/LLVMContextImpl.cpp?rev=148693&r1=148692&r2=148693&view=diff
==============================================================================
--- llvm/trunk/lib/VMCore/LLVMContextImpl.cpp (original)
+++ llvm/trunk/lib/VMCore/LLVMContextImpl.cpp Mon Jan 23 02:52:32 2012
@@ -58,6 +58,8 @@
   std::vector<Module*> 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(),
@@ -71,8 +73,8 @@
   StructConstants.freeConstants();
   VectorConstants.freeConstants();
   CAZConstants.clear();
-  NullPtrConstants.freeConstants();
-  UndefValueConstants.freeConstants();
+  CPNConstants.clear();
+  UVConstants.clear();
   InlineAsms.freeConstants();
   DeleteContainerSeconds(IntConstants);
   DeleteContainerSeconds(FPConstants);

Modified: llvm/trunk/lib/VMCore/LLVMContextImpl.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/LLVMContextImpl.h?rev=148693&r1=148692&r2=148693&view=diff
==============================================================================
--- llvm/trunk/lib/VMCore/LLVMContextImpl.h (original)
+++ llvm/trunk/lib/VMCore/LLVMContextImpl.h Mon Jan 23 02:52:32 2012
@@ -154,9 +154,11 @@
                             VectorType, ConstantVector> VectorConstantsTy;
   VectorConstantsTy VectorConstants;
   
-  ConstantUniqueMap<char, char, PointerType, ConstantPointerNull>
-    NullPtrConstants;
-  ConstantUniqueMap<char, char, Type, UndefValue> UndefValueConstants;
+  typedef DenseMap<PointerType*, OwningPtr<ConstantPointerNull> > CPNMapTy;
+  CPNMapTy CPNConstants;
+
+  typedef DenseMap<Type*, OwningPtr<UndefValue> > UVMapTy;
+  UVMapTy UVConstants;
   
   DenseMap<std::pair<Function*, BasicBlock*> , BlockAddress*> BlockAddresses;
   ConstantUniqueMap<ExprMapKeyType, const ExprMapKeyType&, Type, ConstantExpr>





More information about the llvm-commits mailing list