Now that ConstantBool::True/False are gone, we can modify Type.cpp to

eliminate its static dtors, without having code that depends on order of
initialization.  Eliminate static ctors/dtors from Type.cpp.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@30667 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2006-09-28 23:38:07 +00:00
parent 003cbf35f4
commit de65fb3a8b
2 changed files with 71 additions and 94 deletions

View File

@ -347,11 +347,6 @@ public:
/// ///
void removeAbstractTypeUser(AbstractTypeUser *U) const; void removeAbstractTypeUser(AbstractTypeUser *U) const;
/// clearAllTypeMaps - This method frees all internal memory used by the
/// type subsystem, which can be used in environments where this memory is
/// otherwise reported as a leak.
static void clearAllTypeMaps();
private: private:
/// isSizedDerivedType - Derived types like structures and arrays are sized /// isSizedDerivedType - Derived types like structures and arrays are sized
/// iff all of the members of the type are sized as well. Since asking for /// iff all of the members of the type are sized as well. Since asking for

View File

@ -21,6 +21,7 @@
#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/STLExtras.h"
#include "llvm/Support/MathExtras.h" #include "llvm/Support/MathExtras.h"
#include "llvm/Support/Compiler.h" #include "llvm/Support/Compiler.h"
#include "llvm/Support/ManagedStatic.h"
#include <algorithm> #include <algorithm>
#include <iostream> #include <iostream>
using namespace llvm; using namespace llvm;
@ -57,13 +58,15 @@ Type* PATypeHolder::get() const {
// for types as they are needed. Because resolution of types must invalidate // for types as they are needed. Because resolution of types must invalidate
// all of the abstract type descriptions, we keep them in a seperate map to make // all of the abstract type descriptions, we keep them in a seperate map to make
// this easy. // this easy.
static std::map<const Type*, std::string> ConcreteTypeDescriptions; static ManagedStatic<std::map<const Type*,
static std::map<const Type*, std::string> AbstractTypeDescriptions; std::string> > ConcreteTypeDescriptions;
static ManagedStatic<std::map<const Type*,
std::string> > AbstractTypeDescriptions;
Type::Type(const char *Name, TypeID id) Type::Type(const char *Name, TypeID id)
: ID(id), Abstract(false), RefCount(0), ForwardType(0) { : ID(id), Abstract(false), RefCount(0), ForwardType(0) {
assert(Name && Name[0] && "Should use other ctor if no name!"); assert(Name && Name[0] && "Should use other ctor if no name!");
ConcreteTypeDescriptions[this] = Name; (*ConcreteTypeDescriptions)[this] = Name;
} }
@ -250,18 +253,18 @@ static std::string getTypeDescription(const Type *Ty,
std::vector<const Type *> &TypeStack) { std::vector<const Type *> &TypeStack) {
if (isa<OpaqueType>(Ty)) { // Base case for the recursion if (isa<OpaqueType>(Ty)) { // Base case for the recursion
std::map<const Type*, std::string>::iterator I = std::map<const Type*, std::string>::iterator I =
AbstractTypeDescriptions.lower_bound(Ty); AbstractTypeDescriptions->lower_bound(Ty);
if (I != AbstractTypeDescriptions.end() && I->first == Ty) if (I != AbstractTypeDescriptions->end() && I->first == Ty)
return I->second; return I->second;
std::string Desc = "opaque"; std::string Desc = "opaque";
AbstractTypeDescriptions.insert(std::make_pair(Ty, Desc)); AbstractTypeDescriptions->insert(std::make_pair(Ty, Desc));
return Desc; return Desc;
} }
if (!Ty->isAbstract()) { // Base case for the recursion if (!Ty->isAbstract()) { // Base case for the recursion
std::map<const Type*, std::string>::iterator I = std::map<const Type*, std::string>::iterator I =
ConcreteTypeDescriptions.find(Ty); ConcreteTypeDescriptions->find(Ty);
if (I != ConcreteTypeDescriptions.end()) return I->second; if (I != ConcreteTypeDescriptions->end()) return I->second;
} }
// Check to see if the Type is already on the stack... // Check to see if the Type is already on the stack...
@ -354,9 +357,9 @@ static const std::string &getOrCreateDesc(std::map<const Type*,std::string>&Map,
const std::string &Type::getDescription() const { const std::string &Type::getDescription() const {
if (isAbstract()) if (isAbstract())
return getOrCreateDesc(AbstractTypeDescriptions, this); return getOrCreateDesc(*AbstractTypeDescriptions, this);
else else
return getOrCreateDesc(ConcreteTypeDescriptions, this); return getOrCreateDesc(*ConcreteTypeDescriptions, this);
} }
@ -382,39 +385,41 @@ const Type *StructType::getTypeAtIndex(const Value *V) const {
// Static 'Type' data // Static 'Type' data
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#define DeclarePrimType(TY, Str) \
struct VISIBILITY_HIDDEN TY##Type : public Type { \
TY##Type() : Type(Str, Type::TY##TyID) {} \
}; \
static ManagedStatic<TY##Type> The##TY##Ty
namespace { namespace {
struct VISIBILITY_HIDDEN PrimType : public Type { DeclarePrimType(Void, "void");
PrimType(const char *S, TypeID ID) : Type(S, ID) {} DeclarePrimType(Bool, "bool");
}; DeclarePrimType(SByte, "sbyte");
DeclarePrimType(UByte, "ubyte");
DeclarePrimType(Short, "short");
DeclarePrimType(UShort, "ushort");
DeclarePrimType(Int, "int");
DeclarePrimType(UInt, "uint");
DeclarePrimType(Long, "long");
DeclarePrimType(ULong, "ulong");
DeclarePrimType(Float, "float");
DeclarePrimType(Double, "double");
DeclarePrimType(Label, "label");
} }
static PrimType TheVoidTy ("void" , Type::VoidTyID); Type *Type::VoidTy = &*TheVoidTy;
static PrimType TheBoolTy ("bool" , Type::BoolTyID); Type *Type::BoolTy = &*TheBoolTy;
static PrimType TheSByteTy ("sbyte" , Type::SByteTyID); Type *Type::SByteTy = &*TheSByteTy;
static PrimType TheUByteTy ("ubyte" , Type::UByteTyID); Type *Type::UByteTy = &*TheUByteTy;
static PrimType TheShortTy ("short" , Type::ShortTyID); Type *Type::ShortTy = &*TheShortTy;
static PrimType TheUShortTy("ushort", Type::UShortTyID); Type *Type::UShortTy = &*TheUShortTy;
static PrimType TheIntTy ("int" , Type::IntTyID); Type *Type::IntTy = &*TheIntTy;
static PrimType TheUIntTy ("uint" , Type::UIntTyID); Type *Type::UIntTy = &*TheUIntTy;
static PrimType TheLongTy ("long" , Type::LongTyID); Type *Type::LongTy = &*TheLongTy;
static PrimType TheULongTy ("ulong" , Type::ULongTyID); Type *Type::ULongTy = &*TheULongTy;
static PrimType TheFloatTy ("float" , Type::FloatTyID); Type *Type::FloatTy = &*TheFloatTy;
static PrimType TheDoubleTy("double", Type::DoubleTyID); Type *Type::DoubleTy = &*TheDoubleTy;
static PrimType TheLabelTy ("label" , Type::LabelTyID); Type *Type::LabelTy = &*TheLabelTy;
Type *Type::VoidTy = &TheVoidTy;
Type *Type::BoolTy = &TheBoolTy;
Type *Type::SByteTy = &TheSByteTy;
Type *Type::UByteTy = &TheUByteTy;
Type *Type::ShortTy = &TheShortTy;
Type *Type::UShortTy = &TheUShortTy;
Type *Type::IntTy = &TheIntTy;
Type *Type::UIntTy = &TheUIntTy;
Type *Type::LongTy = &TheLongTy;
Type *Type::ULongTy = &TheULongTy;
Type *Type::FloatTy = &TheFloatTy;
Type *Type::DoubleTy = &TheDoubleTy;
Type *Type::LabelTy = &TheLabelTy;
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
@ -990,7 +995,7 @@ public:
} }
// Define the actual map itself now... // Define the actual map itself now...
static TypeMap<FunctionValType, FunctionType> FunctionTypes; static ManagedStatic<TypeMap<FunctionValType, FunctionType> > FunctionTypes;
FunctionValType FunctionValType::get(const FunctionType *FT) { FunctionValType FunctionValType::get(const FunctionType *FT) {
// Build up a FunctionValType // Build up a FunctionValType
@ -1007,10 +1012,10 @@ FunctionType *FunctionType::get(const Type *ReturnType,
const std::vector<const Type*> &Params, const std::vector<const Type*> &Params,
bool isVarArg) { bool isVarArg) {
FunctionValType VT(ReturnType, Params, isVarArg); FunctionValType VT(ReturnType, Params, isVarArg);
FunctionType *MT = FunctionTypes.get(VT); FunctionType *MT = FunctionTypes->get(VT);
if (MT) return MT; if (MT) return MT;
FunctionTypes.add(VT, MT = new FunctionType(ReturnType, Params, isVarArg)); FunctionTypes->add(VT, MT = new FunctionType(ReturnType, Params, isVarArg));
#ifdef DEBUG_MERGE_TYPES #ifdef DEBUG_MERGE_TYPES
std::cerr << "Derived new type: " << MT << "\n"; std::cerr << "Derived new type: " << MT << "\n";
@ -1048,18 +1053,18 @@ public:
} }
}; };
} }
static TypeMap<ArrayValType, ArrayType> ArrayTypes; static ManagedStatic<TypeMap<ArrayValType, ArrayType> > ArrayTypes;
ArrayType *ArrayType::get(const Type *ElementType, uint64_t NumElements) { ArrayType *ArrayType::get(const Type *ElementType, uint64_t NumElements) {
assert(ElementType && "Can't get array of null types!"); assert(ElementType && "Can't get array of null types!");
ArrayValType AVT(ElementType, NumElements); ArrayValType AVT(ElementType, NumElements);
ArrayType *AT = ArrayTypes.get(AVT); ArrayType *AT = ArrayTypes->get(AVT);
if (AT) return AT; // Found a match, return it! if (AT) return AT; // Found a match, return it!
// Value not found. Derive a new type! // Value not found. Derive a new type!
ArrayTypes.add(AVT, AT = new ArrayType(ElementType, NumElements)); ArrayTypes->add(AVT, AT = new ArrayType(ElementType, NumElements));
#ifdef DEBUG_MERGE_TYPES #ifdef DEBUG_MERGE_TYPES
std::cerr << "Derived new type: " << *AT << "\n"; std::cerr << "Derived new type: " << *AT << "\n";
@ -1098,7 +1103,7 @@ public:
} }
}; };
} }
static TypeMap<PackedValType, PackedType> PackedTypes; static ManagedStatic<TypeMap<PackedValType, PackedType> > PackedTypes;
PackedType *PackedType::get(const Type *ElementType, unsigned NumElements) { PackedType *PackedType::get(const Type *ElementType, unsigned NumElements) {
@ -1106,11 +1111,11 @@ PackedType *PackedType::get(const Type *ElementType, unsigned NumElements) {
assert(isPowerOf2_32(NumElements) && "Vector length should be a power of 2!"); assert(isPowerOf2_32(NumElements) && "Vector length should be a power of 2!");
PackedValType PVT(ElementType, NumElements); PackedValType PVT(ElementType, NumElements);
PackedType *PT = PackedTypes.get(PVT); PackedType *PT = PackedTypes->get(PVT);
if (PT) return PT; // Found a match, return it! if (PT) return PT; // Found a match, return it!
// Value not found. Derive a new type! // Value not found. Derive a new type!
PackedTypes.add(PVT, PT = new PackedType(ElementType, NumElements)); PackedTypes->add(PVT, PT = new PackedType(ElementType, NumElements));
#ifdef DEBUG_MERGE_TYPES #ifdef DEBUG_MERGE_TYPES
std::cerr << "Derived new type: " << *PT << "\n"; std::cerr << "Derived new type: " << *PT << "\n";
@ -1155,15 +1160,15 @@ public:
}; };
} }
static TypeMap<StructValType, StructType> StructTypes; static ManagedStatic<TypeMap<StructValType, StructType> > StructTypes;
StructType *StructType::get(const std::vector<const Type*> &ETypes) { StructType *StructType::get(const std::vector<const Type*> &ETypes) {
StructValType STV(ETypes); StructValType STV(ETypes);
StructType *ST = StructTypes.get(STV); StructType *ST = StructTypes->get(STV);
if (ST) return ST; if (ST) return ST;
// Value not found. Derive a new type! // Value not found. Derive a new type!
StructTypes.add(STV, ST = new StructType(ETypes)); StructTypes->add(STV, ST = new StructType(ETypes));
#ifdef DEBUG_MERGE_TYPES #ifdef DEBUG_MERGE_TYPES
std::cerr << "Derived new type: " << *ST << "\n"; std::cerr << "Derived new type: " << *ST << "\n";
@ -1205,7 +1210,7 @@ public:
}; };
} }
static TypeMap<PointerValType, PointerType> PointerTypes; static ManagedStatic<TypeMap<PointerValType, PointerType> > PointerTypes;
PointerType *PointerType::get(const Type *ValueType) { PointerType *PointerType::get(const Type *ValueType) {
assert(ValueType && "Can't get a pointer to <null> type!"); assert(ValueType && "Can't get a pointer to <null> type!");
@ -1213,11 +1218,11 @@ PointerType *PointerType::get(const Type *ValueType) {
"Pointer to void is not valid, use sbyte* instead!"); "Pointer to void is not valid, use sbyte* instead!");
PointerValType PVT(ValueType); PointerValType PVT(ValueType);
PointerType *PT = PointerTypes.get(PVT); PointerType *PT = PointerTypes->get(PVT);
if (PT) return PT; if (PT) return PT;
// Value not found. Derive a new type! // Value not found. Derive a new type!
PointerTypes.add(PVT, PT = new PointerType(ValueType)); PointerTypes->add(PVT, PT = new PointerType(ValueType));
#ifdef DEBUG_MERGE_TYPES #ifdef DEBUG_MERGE_TYPES
std::cerr << "Derived new type: " << *PT << "\n"; std::cerr << "Derived new type: " << *PT << "\n";
@ -1274,7 +1279,7 @@ void DerivedType::refineAbstractTypeTo(const Type *NewType) {
assert(ForwardType == 0 && "This type has already been refined!"); assert(ForwardType == 0 && "This type has already been refined!");
// The descriptions may be out of date. Conservatively clear them all! // The descriptions may be out of date. Conservatively clear them all!
AbstractTypeDescriptions.clear(); AbstractTypeDescriptions->clear();
#ifdef DEBUG_MERGE_TYPES #ifdef DEBUG_MERGE_TYPES
std::cerr << "REFINING abstract type [" << (void*)this << " " std::cerr << "REFINING abstract type [" << (void*)this << " "
@ -1356,11 +1361,11 @@ void DerivedType::notifyUsesThatTypeBecameConcrete() {
// //
void FunctionType::refineAbstractType(const DerivedType *OldType, void FunctionType::refineAbstractType(const DerivedType *OldType,
const Type *NewType) { const Type *NewType) {
FunctionTypes.RefineAbstractType(this, OldType, NewType); FunctionTypes->RefineAbstractType(this, OldType, NewType);
} }
void FunctionType::typeBecameConcrete(const DerivedType *AbsTy) { void FunctionType::typeBecameConcrete(const DerivedType *AbsTy) {
FunctionTypes.TypeBecameConcrete(this, AbsTy); FunctionTypes->TypeBecameConcrete(this, AbsTy);
} }
@ -1370,11 +1375,11 @@ void FunctionType::typeBecameConcrete(const DerivedType *AbsTy) {
// //
void ArrayType::refineAbstractType(const DerivedType *OldType, void ArrayType::refineAbstractType(const DerivedType *OldType,
const Type *NewType) { const Type *NewType) {
ArrayTypes.RefineAbstractType(this, OldType, NewType); ArrayTypes->RefineAbstractType(this, OldType, NewType);
} }
void ArrayType::typeBecameConcrete(const DerivedType *AbsTy) { void ArrayType::typeBecameConcrete(const DerivedType *AbsTy) {
ArrayTypes.TypeBecameConcrete(this, AbsTy); ArrayTypes->TypeBecameConcrete(this, AbsTy);
} }
// refineAbstractType - Called when a contained type is found to be more // refineAbstractType - Called when a contained type is found to be more
@ -1383,11 +1388,11 @@ void ArrayType::typeBecameConcrete(const DerivedType *AbsTy) {
// //
void PackedType::refineAbstractType(const DerivedType *OldType, void PackedType::refineAbstractType(const DerivedType *OldType,
const Type *NewType) { const Type *NewType) {
PackedTypes.RefineAbstractType(this, OldType, NewType); PackedTypes->RefineAbstractType(this, OldType, NewType);
} }
void PackedType::typeBecameConcrete(const DerivedType *AbsTy) { void PackedType::typeBecameConcrete(const DerivedType *AbsTy) {
PackedTypes.TypeBecameConcrete(this, AbsTy); PackedTypes->TypeBecameConcrete(this, AbsTy);
} }
// refineAbstractType - Called when a contained type is found to be more // refineAbstractType - Called when a contained type is found to be more
@ -1396,11 +1401,11 @@ void PackedType::typeBecameConcrete(const DerivedType *AbsTy) {
// //
void StructType::refineAbstractType(const DerivedType *OldType, void StructType::refineAbstractType(const DerivedType *OldType,
const Type *NewType) { const Type *NewType) {
StructTypes.RefineAbstractType(this, OldType, NewType); StructTypes->RefineAbstractType(this, OldType, NewType);
} }
void StructType::typeBecameConcrete(const DerivedType *AbsTy) { void StructType::typeBecameConcrete(const DerivedType *AbsTy) {
StructTypes.TypeBecameConcrete(this, AbsTy); StructTypes->TypeBecameConcrete(this, AbsTy);
} }
// refineAbstractType - Called when a contained type is found to be more // refineAbstractType - Called when a contained type is found to be more
@ -1409,11 +1414,11 @@ void StructType::typeBecameConcrete(const DerivedType *AbsTy) {
// //
void PointerType::refineAbstractType(const DerivedType *OldType, void PointerType::refineAbstractType(const DerivedType *OldType,
const Type *NewType) { const Type *NewType) {
PointerTypes.RefineAbstractType(this, OldType, NewType); PointerTypes->RefineAbstractType(this, OldType, NewType);
} }
void PointerType::typeBecameConcrete(const DerivedType *AbsTy) { void PointerType::typeBecameConcrete(const DerivedType *AbsTy) {
PointerTypes.TypeBecameConcrete(this, AbsTy); PointerTypes->TypeBecameConcrete(this, AbsTy);
} }
bool SequentialType::indexValid(const Value *V) const { bool SequentialType::indexValid(const Value *V) const {
@ -1443,26 +1448,3 @@ std::ostream &operator<<(std::ostream &OS, const Type &T) {
return OS; return OS;
} }
} }
/// clearAllTypeMaps - This method frees all internal memory used by the
/// type subsystem, which can be used in environments where this memory is
/// otherwise reported as a leak.
void Type::clearAllTypeMaps() {
std::vector<Type *> DerivedTypes;
FunctionTypes.clear(DerivedTypes);
PointerTypes.clear(DerivedTypes);
StructTypes.clear(DerivedTypes);
ArrayTypes.clear(DerivedTypes);
PackedTypes.clear(DerivedTypes);
for(std::vector<Type *>::iterator I = DerivedTypes.begin(),
E = DerivedTypes.end(); I != E; ++I)
(*I)->ContainedTys.clear();
for(std::vector<Type *>::iterator I = DerivedTypes.begin(),
E = DerivedTypes.end(); I != E; ++I)
delete *I;
DerivedTypes.clear();
}
// vim: sw=2