- V &= ITy->getBitMask();
- ConstantInt *&Slot = (*IntConstants)[std::make_pair(uint64_t(V), Ty)];
- if (Slot) return Slot;
+ APInt Tmp(ITy->getBitWidth(), V);
+ return get(Ty, Tmp);
+}
+
+// Get a ConstantInt from a Type and APInt. Note that the value stored in
+// the DenseMap as the key is a DensMapAPIntKeyInfo::KeyTy which has provided
+// 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 Type *Ty, const APInt& V) {
+ const IntegerType *ITy = cast<IntegerType>(Ty);
+ assert(ITy->getBitWidth() == V.getBitWidth() && "Invalid type for constant");
+ // get an existing value or the insertion position
+ DenseMapAPIntKeyInfo::KeyTy Key(V, Ty);
+ ConstantInt *&Slot = (*IntConstants)[Key];
+ // if it exists, return it.
+ if (Slot)
+ return Slot;
+ // otherwise create a new one, insert it, and return it.