[llvm-commits] CVS: llvm/lib/VMCore/Constants.cpp

Chris Lattner sabre at nondot.org
Wed Sep 27 17:35:21 PDT 2006



Changes in directory llvm/lib/VMCore:

Constants.cpp updated: 1.160 -> 1.161
---
Log message:

Use the new ManagedStatic class to explicitly manage static variables, eliminating static ctors/dtors


---
Diffs of the changes:  (+47 -71)

 Constants.cpp |  118 +++++++++++++++++++++++-----------------------------------
 1 files changed, 47 insertions(+), 71 deletions(-)


Index: llvm/lib/VMCore/Constants.cpp
diff -u llvm/lib/VMCore/Constants.cpp:1.160 llvm/lib/VMCore/Constants.cpp:1.161
--- llvm/lib/VMCore/Constants.cpp:1.160	Sun Sep 17 14:14:47 2006
+++ llvm/lib/VMCore/Constants.cpp	Wed Sep 27 19:35:06 2006
@@ -21,6 +21,7 @@
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/Support/MathExtras.h"
 #include "llvm/Support/Compiler.h"
+#include "llvm/Support/ManagedStatic.h"
 #include <algorithm>
 #include <iostream>
 using namespace llvm;
@@ -879,15 +880,15 @@
 
 //---- ConstantUInt::get() and ConstantSInt::get() implementations...
 //
-static ValueMap< int64_t, Type, ConstantSInt> SIntConstants;
-static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants;
+static ManagedStatic<ValueMap< int64_t, Type, ConstantSInt> > SIntConstants;
+static ManagedStatic<ValueMap<uint64_t, Type, ConstantUInt> > UIntConstants;
 
 ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
-  return SIntConstants.getOrCreate(Ty, V);
+  return SIntConstants->getOrCreate(Ty, V);
 }
 
 ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
-  return UIntConstants.getOrCreate(Ty, V);
+  return UIntConstants->getOrCreate(Ty, V);
 }
 
 ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
@@ -915,8 +916,8 @@
   };
 }
 
-static ValueMap<uint64_t, Type, ConstantFP> DoubleConstants;
-static ValueMap<uint32_t, Type, ConstantFP> FloatConstants;
+static ManagedStatic<ValueMap<uint64_t, Type, ConstantFP> > DoubleConstants;
+static ManagedStatic<ValueMap<uint32_t, Type, ConstantFP> > FloatConstants;
 
 bool ConstantFP::isNullValue() const {
   return DoubleToBits(Val) == 0;
@@ -930,10 +931,10 @@
 ConstantFP *ConstantFP::get(const Type *Ty, double V) {
   if (Ty == Type::FloatTy) {
     // Force the value through memory to normalize it.
-    return FloatConstants.getOrCreate(Ty, FloatToBits(V));
+    return FloatConstants->getOrCreate(Ty, FloatToBits(V));
   } else {
     assert(Ty == Type::DoubleTy);
-    return DoubleConstants.getOrCreate(Ty, DoubleToBits(V));
+    return DoubleConstants->getOrCreate(Ty, DoubleToBits(V));
   }
 }
 
@@ -960,20 +961,21 @@
   };
 }
 
-static ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
+static ManagedStatic<ValueMap<char, Type, 
+                              ConstantAggregateZero> > AggZeroConstants;
 
 static char getValType(ConstantAggregateZero *CPZ) { return 0; }
 
 Constant *ConstantAggregateZero::get(const Type *Ty) {
   assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<PackedType>(Ty)) &&
          "Cannot create an aggregate zero of non-aggregate type!");
-  return AggZeroConstants.getOrCreate(Ty, 0);
+  return AggZeroConstants->getOrCreate(Ty, 0);
 }
 
 // destroyConstant - Remove the constant from the constant table...
 //
 void ConstantAggregateZero::destroyConstant() {
-  AggZeroConstants.remove(this);
+  AggZeroConstants->remove(this);
   destroyConstantImpl();
 }
 
@@ -1005,7 +1007,7 @@
 
 typedef ValueMap<std::vector<Constant*>, ArrayType, 
                  ConstantArray, true /*largekey*/> ArrayConstantsTy;
-static ArrayConstantsTy ArrayConstants;
+static ManagedStatic<ArrayConstantsTy> ArrayConstants;
 
 Constant *ConstantArray::get(const ArrayType *Ty,
                              const std::vector<Constant*> &V) {
@@ -1013,10 +1015,10 @@
   if (!V.empty()) {
     Constant *C = V[0];
     if (!C->isNullValue())
-      return ArrayConstants.getOrCreate(Ty, V);
+      return ArrayConstants->getOrCreate(Ty, V);
     for (unsigned i = 1, e = V.size(); i != e; ++i)
       if (V[i] != C)
-        return ArrayConstants.getOrCreate(Ty, V);
+        return ArrayConstants->getOrCreate(Ty, V);
   }
   return ConstantAggregateZero::get(Ty);
 }
@@ -1024,7 +1026,7 @@
 // destroyConstant - Remove the constant from the constant table...
 //
 void ConstantArray::destroyConstant() {
-  ArrayConstants.remove(this);
+  ArrayConstants->remove(this);
   destroyConstantImpl();
 }
 
@@ -1098,7 +1100,7 @@
 
 typedef ValueMap<std::vector<Constant*>, StructType,
                  ConstantStruct, true /*largekey*/> StructConstantsTy;
-static StructConstantsTy StructConstants;
+static ManagedStatic<StructConstantsTy> StructConstants;
 
 static std::vector<Constant*> getValType(ConstantStruct *CS) {
   std::vector<Constant*> Elements;
@@ -1113,7 +1115,7 @@
   // Create a ConstantAggregateZero value if all elements are zeros...
   for (unsigned i = 0, e = V.size(); i != e; ++i)
     if (!V[i]->isNullValue())
-      return StructConstants.getOrCreate(Ty, V);
+      return StructConstants->getOrCreate(Ty, V);
 
   return ConstantAggregateZero::get(Ty);
 }
@@ -1129,7 +1131,7 @@
 // destroyConstant - Remove the constant from the constant table...
 //
 void ConstantStruct::destroyConstant() {
-  StructConstants.remove(this);
+  StructConstants->remove(this);
   destroyConstantImpl();
 }
 
@@ -1159,8 +1161,8 @@
   return Elements;
 }
 
-static ValueMap<std::vector<Constant*>, PackedType,
-                ConstantPacked> PackedConstants;
+static ManagedStatic<ValueMap<std::vector<Constant*>, PackedType,
+                              ConstantPacked> > PackedConstants;
 
 Constant *ConstantPacked::get(const PackedType *Ty,
                               const std::vector<Constant*> &V) {
@@ -1168,10 +1170,10 @@
   if (!V.empty()) {
     Constant *C = V[0];
     if (!C->isNullValue())
-      return PackedConstants.getOrCreate(Ty, V);
+      return PackedConstants->getOrCreate(Ty, V);
     for (unsigned i = 1, e = V.size(); i != e; ++i)
       if (V[i] != C)
-        return PackedConstants.getOrCreate(Ty, V);
+        return PackedConstants->getOrCreate(Ty, V);
   }
   return ConstantAggregateZero::get(Ty);
 }
@@ -1184,7 +1186,7 @@
 // destroyConstant - Remove the constant from the constant table...
 //
 void ConstantPacked::destroyConstant() {
-  PackedConstants.remove(this);
+  PackedConstants->remove(this);
   destroyConstantImpl();
 }
 
@@ -1212,7 +1214,8 @@
   };
 }
 
-static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
+static ManagedStatic<ValueMap<char, PointerType, 
+                              ConstantPointerNull> > NullPtrConstants;
 
 static char getValType(ConstantPointerNull *) {
   return 0;
@@ -1220,13 +1223,13 @@
 
 
 ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
-  return NullPtrConstants.getOrCreate(Ty, 0);
+  return NullPtrConstants->getOrCreate(Ty, 0);
 }
 
 // destroyConstant - Remove the constant from the constant table...
 //
 void ConstantPointerNull::destroyConstant() {
-  NullPtrConstants.remove(this);
+  NullPtrConstants->remove(this);
   destroyConstantImpl();
 }
 
@@ -1255,7 +1258,7 @@
   };
 }
 
-static ValueMap<char, Type, UndefValue> UndefValueConstants;
+static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
 
 static char getValType(UndefValue *) {
   return 0;
@@ -1263,13 +1266,13 @@
 
 
 UndefValue *UndefValue::get(const Type *Ty) {
-  return UndefValueConstants.getOrCreate(Ty, 0);
+  return UndefValueConstants->getOrCreate(Ty, 0);
 }
 
 // destroyConstant - Remove the constant from the constant table.
 //
 void UndefValue::destroyConstant() {
-  UndefValueConstants.remove(this);
+  UndefValueConstants->remove(this);
   destroyConstantImpl();
 }
 
@@ -1355,7 +1358,8 @@
   return ExprMapKeyType(CE->getOpcode(), Operands);
 }
 
-static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
+static ManagedStatic<ValueMap<ExprMapKeyType, Type,
+                              ConstantExpr> > ExprConstants;
 
 Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
   assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
@@ -1366,7 +1370,7 @@
   // Look up the constant in the table first to ensure uniqueness
   std::vector<Constant*> argVec(1, C);
   ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
-  return ExprConstants.getOrCreate(Ty, Key);
+  return ExprConstants->getOrCreate(Ty, Key);
 }
 
 Constant *ConstantExpr::getSignExtend(Constant *C, const Type *Ty) {
@@ -1426,7 +1430,7 @@
 
   std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
   ExprMapKeyType Key = std::make_pair(Opcode, argVec);
-  return ExprConstants.getOrCreate(ReqTy, Key);
+  return ExprConstants->getOrCreate(ReqTy, Key);
 }
 
 Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
@@ -1482,7 +1486,7 @@
   argVec[1] = V1;
   argVec[2] = V2;
   ExprMapKeyType Key = std::make_pair(Instruction::Select, argVec);
-  return ExprConstants.getOrCreate(ReqTy, Key);
+  return ExprConstants->getOrCreate(ReqTy, Key);
 }
 
 /// getShiftTy - Return a shift left or shift right constant expr
@@ -1501,7 +1505,7 @@
   // Look up the constant in the table first to ensure uniqueness
   std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
   ExprMapKeyType Key = std::make_pair(Opcode, argVec);
-  return ExprConstants.getOrCreate(ReqTy, Key);
+  return ExprConstants->getOrCreate(ReqTy, Key);
 }
 
 
@@ -1522,7 +1526,7 @@
   for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
     ArgVec.push_back(cast<Constant>(IdxList[i]));
   const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,ArgVec);
-  return ExprConstants.getOrCreate(ReqTy, Key);
+  return ExprConstants->getOrCreate(ReqTy, Key);
 }
 
 Constant *ConstantExpr::getGetElementPtr(Constant *C,
@@ -1553,7 +1557,7 @@
   std::vector<Constant*> ArgVec(1, Val);
   ArgVec.push_back(Idx);
   const ExprMapKeyType &Key = std::make_pair(Instruction::ExtractElement,ArgVec);
-  return ExprConstants.getOrCreate(ReqTy, Key);
+  return ExprConstants->getOrCreate(ReqTy, Key);
 }
 
 Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
@@ -1574,7 +1578,7 @@
   ArgVec.push_back(Elt);
   ArgVec.push_back(Idx);
   const ExprMapKeyType &Key = std::make_pair(Instruction::InsertElement,ArgVec);
-  return ExprConstants.getOrCreate(ReqTy, Key);
+  return ExprConstants->getOrCreate(ReqTy, Key);
 }
 
 Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt, 
@@ -1598,7 +1602,7 @@
   ArgVec.push_back(V2);
   ArgVec.push_back(Mask);
   const ExprMapKeyType &Key = std::make_pair(Instruction::ShuffleVector,ArgVec);
-  return ExprConstants.getOrCreate(ReqTy, Key);
+  return ExprConstants->getOrCreate(ReqTy, Key);
 }
 
 Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2, 
@@ -1612,7 +1616,7 @@
 // destroyConstant - Remove the constant from the constant table...
 //
 void ConstantExpr::destroyConstant() {
-  ExprConstants.remove(this);
+  ExprConstants->remove(this);
   destroyConstantImpl();
 }
 
@@ -1661,7 +1665,7 @@
     // Check to see if we have this array type already.
     bool Exists;
     ArrayConstantsTy::MapTy::iterator I =
-      ArrayConstants.InsertOrGetItem(Lookup, Exists);
+      ArrayConstants->InsertOrGetItem(Lookup, Exists);
     
     if (Exists) {
       Replacement = I->second;
@@ -1670,7 +1674,7 @@
       // creating a new constant array, inserting it, replaceallusesof'ing the
       // old with the new, then deleting the old... just update the current one
       // in place!
-      ArrayConstants.MoveConstantToNewSlot(this, I);
+      ArrayConstants->MoveConstantToNewSlot(this, I);
       
       // Update to the new value.
       setOperand(OperandToUpdate, ToC);
@@ -1726,7 +1730,7 @@
     // Check to see if we have this array type already.
     bool Exists;
     StructConstantsTy::MapTy::iterator I =
-      StructConstants.InsertOrGetItem(Lookup, Exists);
+      StructConstants->InsertOrGetItem(Lookup, Exists);
     
     if (Exists) {
       Replacement = I->second;
@@ -1735,7 +1739,7 @@
       // creating a new constant struct, inserting it, replaceallusesof'ing the
       // old with the new, then deleting the old... just update the current one
       // in place!
-      StructConstants.MoveConstantToNewSlot(this, I);
+      StructConstants->MoveConstantToNewSlot(this, I);
       
       // Update to the new value.
       setOperand(OperandToUpdate, ToC);
@@ -1846,34 +1850,6 @@
 }
 
 
-
-/// clearAllValueMaps - This method frees all internal memory used by the
-/// constant subsystem, which can be used in environments where this memory
-/// is otherwise reported as a leak.
-void Constant::clearAllValueMaps() {
-  std::vector<Constant *> Constants;
-
-  DoubleConstants.clear(Constants);
-  FloatConstants.clear(Constants);
-  SIntConstants.clear(Constants);
-  UIntConstants.clear(Constants);
-  AggZeroConstants.clear(Constants);
-  ArrayConstants.clear(Constants);
-  StructConstants.clear(Constants);
-  PackedConstants.clear(Constants);
-  NullPtrConstants.clear(Constants);
-  UndefValueConstants.clear(Constants);
-  ExprConstants.clear(Constants);
-
-  for (std::vector<Constant *>::iterator I = Constants.begin(),
-       E = Constants.end(); I != E; ++I)
-    (*I)->dropAllReferences();
-  for (std::vector<Constant *>::iterator I = Constants.begin(),
-       E = Constants.end(); I != E; ++I)
-    (*I)->destroyConstantImpl();
-  Constants.clear();
-}
-
 /// getStringValue - Turn an LLVM constant pointer that eventually points to a
 /// global into a string value.  Return an empty string if we can't do it.
 /// Parameter Chop determines if the result is chopped at the first null






More information about the llvm-commits mailing list