#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;
//---- 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) {
};
}
-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;
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));
}
}
};
}
-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();
}
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) {
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);
}
// destroyConstant - Remove the constant from the constant table...
//
void ConstantArray::destroyConstant() {
- ArrayConstants.remove(this);
+ ArrayConstants->remove(this);
destroyConstantImpl();
}
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;
// 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);
}
// destroyConstant - Remove the constant from the constant table...
//
void ConstantStruct::destroyConstant() {
- StructConstants.remove(this);
+ StructConstants->remove(this);
destroyConstantImpl();
}
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) {
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);
}
// destroyConstant - Remove the constant from the constant table...
//
void ConstantPacked::destroyConstant() {
- PackedConstants.remove(this);
+ PackedConstants->remove(this);
destroyConstantImpl();
}
};
}
-static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
+static ManagedStatic<ValueMap<char, PointerType,
+ ConstantPointerNull> > NullPtrConstants;
static char getValType(ConstantPointerNull *) {
return 0;
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();
}
};
}
-static ValueMap<char, Type, UndefValue> UndefValueConstants;
+static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
static char getValType(UndefValue *) {
return 0;
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();
}
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!");
// 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) {
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) {
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
// 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);
}
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,
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) {
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,
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,
// destroyConstant - Remove the constant from the constant table...
//
void ConstantExpr::destroyConstant() {
- ExprConstants.remove(this);
+ ExprConstants->remove(this);
destroyConstantImpl();
}
// 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;
// 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);
// 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;
// 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);
}
-
-/// 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