#include "llvm/Support/Debug.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/MathExtras.h"
+#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallVector.h"
#include <algorithm>
#include <map>
}
}
-
// Static constructor to create a '0' constant of arbitrary type...
Constant *Constant::getNullValue(const Type *Ty) {
switch (Ty->getTypeID()) {
- case Type::IntegerTyID: {
- const IntegerType *ITy = dyn_cast<IntegerType>(Ty);
- switch (ITy->getBitWidth()) {
- case 1: {
- static Constant *NullBool = ConstantInt::get(Ty, false);
- return NullBool;
- }
- case 8: {
- static Constant *NullInt8 = ConstantInt::get(Ty, 0);
- return NullInt8;
- }
- case 16: {
- static Constant *NullInt16 = ConstantInt::get(Ty, 0);
- return NullInt16;
- }
- case 32: {
- static Constant *NullInt32 = ConstantInt::get(Ty, 0);
- return NullInt32;
- }
- case 64: {
- static Constant *NullInt64 = ConstantInt::get(Ty, 0);
- return NullInt64;
- }
- default:
- return ConstantInt::get(Ty, 0);
- }
- }
- case Type::FloatTyID: {
- static Constant *NullFloat = ConstantFP::get(Type::FloatTy, 0);
- return NullFloat;
- }
- case Type::DoubleTyID: {
- static Constant *NullDouble = ConstantFP::get(Type::DoubleTy, 0);
- return NullDouble;
- }
+ case Type::IntegerTyID:
+ return ConstantInt::get(Ty, 0);
+ case Type::FloatTyID:
+ case Type::DoubleTyID:
+ return ConstantFP::get(Ty, 0.0);
case Type::PointerTyID:
return ConstantPointerNull::get(cast<PointerType>(Ty));
case Type::StructTyID:
//===----------------------------------------------------------------------===//
-// ConstantXXX Classes
+// ConstantInt
//===----------------------------------------------------------------------===//
-//===----------------------------------------------------------------------===//
-// Normal Constructors
+ConstantInt::ConstantInt(const IntegerType *Ty, uint64_t V)
+ : Constant(Ty, ConstantIntVal, 0, 0), Val(V) {
+}
+
+ConstantInt *ConstantInt::TheTrueVal = 0;
+ConstantInt *ConstantInt::TheFalseVal = 0;
+
+namespace llvm {
+ void CleanupTrueFalse(void *) {
+ ConstantInt::ResetTrueFalse();
+ }
+}
+
+static ManagedCleanup<llvm::CleanupTrueFalse> TrueFalseCleanup;
+
+ConstantInt *ConstantInt::CreateTrueFalseVals(bool WhichOne) {
+ assert(TheTrueVal == 0 && TheFalseVal == 0);
+ TheTrueVal = get(Type::Int1Ty, 1);
+ TheFalseVal = get(Type::Int1Ty, 0);
+
+ // Ensure that llvm_shutdown nulls out TheTrueVal/TheFalseVal.
+ TrueFalseCleanup.Register();
+
+ return WhichOne ? TheTrueVal : TheFalseVal;
+}
+
-ConstantInt::ConstantInt(bool V)
- : Constant(Type::Int1Ty, ConstantIntVal, 0, 0), Val(uint64_t(V)) {
+namespace {
+ struct DenseMapInt64KeyInfo {
+ typedef std::pair<uint64_t, const Type*> KeyTy;
+ static inline KeyTy getEmptyKey() { return KeyTy(0, 0); }
+ static inline KeyTy getTombstoneKey() { return KeyTy(1, 0); }
+ static unsigned getHashValue(const KeyTy &Key) {
+ return DenseMapKeyInfo<void*>::getHashValue(Key.second) ^ Key.first;
+ }
+ static bool isPod() { return true; }
+ };
}
-ConstantInt::ConstantInt(const Type *Ty, uint64_t V)
- : Constant(Ty, ConstantIntVal, 0, 0), Val(Ty == Type::Int1Ty ? bool(V) : V) {
+
+typedef DenseMap<DenseMapInt64KeyInfo::KeyTy, ConstantInt*,
+ DenseMapInt64KeyInfo> IntMapTy;
+static ManagedStatic<IntMapTy> IntConstants;
+
+// Get a ConstantInt from an int64_t. Note here that we canoncialize the value
+// to a uint64_t value that has been zero extended down to the size of the
+// integer type of the ConstantInt. This allows the getZExtValue method to
+// just return the stored value while getSExtValue has to convert back to sign
+// extended. getZExtValue is more common in LLVM than getSExtValue().
+ConstantInt *ConstantInt::get(const Type *Ty, int64_t V) {
+ const IntegerType *ITy = cast<IntegerType>(Ty);
+ V &= ITy->getBitMask();
+ ConstantInt *&Slot = (*IntConstants)[std::make_pair(uint64_t(V), Ty)];
+ if (Slot) return Slot;
+ return Slot = new ConstantInt(ITy, V);
}
+//===----------------------------------------------------------------------===//
+// ConstantFP
+//===----------------------------------------------------------------------===//
+
+
ConstantFP::ConstantFP(const Type *Ty, double V)
: Constant(Ty, ConstantFPVal, 0, 0) {
- assert(isValueValidForType(Ty, V) && "Value too large for type!");
Val = V;
}
+bool ConstantFP::isNullValue() const {
+ return DoubleToBits(Val) == 0;
+}
+
+bool ConstantFP::isExactlyValue(double V) const {
+ return DoubleToBits(V) == DoubleToBits(Val);
+}
+
+
+namespace {
+ struct DenseMapInt32KeyInfo {
+ typedef std::pair<uint32_t, const Type*> KeyTy;
+ static inline KeyTy getEmptyKey() { return KeyTy(0, 0); }
+ static inline KeyTy getTombstoneKey() { return KeyTy(1, 0); }
+ static unsigned getHashValue(const KeyTy &Key) {
+ return DenseMapKeyInfo<void*>::getHashValue(Key.second) ^ Key.first;
+ }
+ static bool isPod() { return true; }
+ };
+}
+
+//---- ConstantFP::get() implementation...
+//
+typedef DenseMap<DenseMapInt32KeyInfo::KeyTy, ConstantFP*,
+ DenseMapInt32KeyInfo> FloatMapTy;
+typedef DenseMap<DenseMapInt64KeyInfo::KeyTy, ConstantFP*,
+ DenseMapInt64KeyInfo> DoubleMapTy;
+
+static ManagedStatic<FloatMapTy> FloatConstants;
+static ManagedStatic<DoubleMapTy> DoubleConstants;
+
+ConstantFP *ConstantFP::get(const Type *Ty, double V) {
+ if (Ty == Type::FloatTy) {
+ uint32_t IntVal = FloatToBits((float)V);
+
+ ConstantFP *&Slot = (*FloatConstants)[std::make_pair(IntVal, Ty)];
+ if (Slot) return Slot;
+ return Slot = new ConstantFP(Ty, (float)V);
+ } else {
+ assert(Ty == Type::DoubleTy);
+ uint64_t IntVal = DoubleToBits(V);
+ ConstantFP *&Slot = (*DoubleConstants)[std::make_pair(IntVal, Ty)];
+ if (Slot) return Slot;
+ return Slot = new ConstantFP(Ty, V);
+ }
+}
+
+
+//===----------------------------------------------------------------------===//
+// ConstantXXX Classes
+//===----------------------------------------------------------------------===//
+
+
ConstantArray::ConstantArray(const ArrayType *T,
const std::vector<Constant*> &V)
: Constant(T, ConstantArrayVal, new Use[V.size()], V.size()) {
///
AbstractTypeMapTy AbstractTypeMap;
- private:
- void clear(std::vector<Constant *> &Constants) {
- for(typename MapTy::iterator I = Map.begin(); I != Map.end(); ++I)
- Constants.push_back(I->second);
- Map.clear();
- AbstractTypeMap.clear();
- InverseMap.clear();
- }
-
public:
typename MapTy::iterator map_end() { return Map.end(); }
}
-//---- ConstantInt::get() implementations...
-//
-static ManagedStatic<ValueMap<uint64_t, Type, ConstantInt> > IntConstants;
-
-// Get a ConstantInt from an int64_t. Note here that we canoncialize the value
-// to a uint64_t value that has been zero extended down to the size of the
-// integer type of the ConstantInt. This allows the getZExtValue method to
-// just return the stored value while getSExtValue has to convert back to sign
-// extended. getZExtValue is more common in LLVM than getSExtValue().
-ConstantInt *ConstantInt::get(const Type *Ty, int64_t V) {
- if (Ty == Type::Int1Ty)
- if (V & 1)
- return getTrue();
- else
- return getFalse();
- return IntConstants->getOrCreate(Ty, V & cast<IntegerType>(Ty)->getBitMask());
-}
-
-//---- ConstantFP::get() implementation...
-//
-namespace llvm {
- template<>
- struct ConstantCreator<ConstantFP, Type, uint64_t> {
- static ConstantFP *create(const Type *Ty, uint64_t V) {
- assert(Ty == Type::DoubleTy);
- return new ConstantFP(Ty, BitsToDouble(V));
- }
- };
- template<>
- struct ConstantCreator<ConstantFP, Type, uint32_t> {
- static ConstantFP *create(const Type *Ty, uint32_t V) {
- assert(Ty == Type::FloatTy);
- return new ConstantFP(Ty, BitsToFloat(V));
- }
- };
-}
-
-static ManagedStatic<ValueMap<uint64_t, Type, ConstantFP> > DoubleConstants;
-static ManagedStatic<ValueMap<uint32_t, Type, ConstantFP> > FloatConstants;
-
-bool ConstantFP::isNullValue() const {
- return DoubleToBits(Val) == 0;
-}
-
-bool ConstantFP::isExactlyValue(double V) const {
- return DoubleToBits(V) == DoubleToBits(Val);
-}
-
-
-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));
- } else {
- assert(Ty == Type::DoubleTy);
- return DoubleConstants->getOrCreate(Ty, DoubleToBits(V));
- }
-}
//---- ConstantAggregateZero::get() implementation...
//