+ConstantInt::ConstantInt(const IntegerType *Ty, const APInt& V)
+ : Constant(Ty, ConstantIntVal, 0, 0), Val(V) {
+ assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
+}
+
+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;
+}
+
+
+namespace {
+ struct DenseMapAPIntKeyInfo {
+ struct KeyTy {
+ APInt val;
+ const Type* type;
+ KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
+ KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
+ bool operator==(const KeyTy& that) const {
+ return type == that.type && this->val == that.val;
+ }
+ bool operator!=(const KeyTy& that) const {
+ return !this->operator==(that);
+ }
+ };
+ static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
+ static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
+ static unsigned getHashValue(const KeyTy &Key) {
+ return DenseMapInfo<void*>::getHashValue(Key.type) ^
+ Key.val.getHashValue();
+ }
+ static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
+ return LHS == RHS;
+ }
+ static bool isPod() { return false; }
+ };
+}
+
+
+typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
+ DenseMapAPIntKeyInfo> IntMapTy;
+static ManagedStatic<IntMapTy> IntConstants;
+
+ConstantInt *ConstantInt::get(const Type *Ty, uint64_t V, bool isSigned) {
+ const IntegerType *ITy = cast<IntegerType>(Ty);
+ return get(APInt(ITy->getBitWidth(), V, isSigned));
+}
+
+// Get a ConstantInt from an APInt. Note that the value stored in the DenseMap
+// as the key, is a DensMapAPIntKeyInfo::KeyTy which has provided the
+// operator== and operator!= to ensure that the DenseMap doesn't attempt to
+// compare APInt's of different widths, which would violate an APInt class
+// invariant which generates an assertion.
+ConstantInt *ConstantInt::get(const APInt& V) {
+ // Get the corresponding integer type for the bit width of the value.
+ const IntegerType *ITy = IntegerType::get(V.getBitWidth());
+ // get an existing value or the insertion position
+ DenseMapAPIntKeyInfo::KeyTy Key(V, ITy);
+ ConstantInt *&Slot = (*IntConstants)[Key];
+ // if it exists, return it.
+ if (Slot)
+ return Slot;
+ // otherwise create a new one, insert it, and return it.
+ return Slot = new ConstantInt(ITy, V);
+}
+