-static ConstPoolInt *getUnsignedConstant(uint64_t V, const Type *Ty) {
- if (Ty->isPointerType()) Ty = Type::ULongTy;
- return Ty->isSigned() ? ConstPoolSInt::get(Ty, V) : ConstPoolUInt::get(Ty, V);
+// getUnsignedConstant - Return a constant value of the specified type. If the
+// constant value is not valid for the specified type, return null. This cannot
+// happen for values in the range of 0 to 127.
+//
+static ConstantInt *getUnsignedConstant(uint64_t V, const Type *Ty) {
+ if (isa<PointerType>(Ty)) Ty = Type::ULongTy;
+ if (Ty->isSigned()) {
+ // If this value is not a valid unsigned value for this type, return null!
+ if (V > 127 && ((int64_t)V < 0 ||
+ !ConstantSInt::isValueValidForType(Ty, (int64_t)V)))
+ return 0;
+ return ConstantSInt::get(Ty, V);
+ } else {
+ // If this value is not a valid unsigned value for this type, return null!
+ if (V > 255 && !ConstantUInt::isValueValidForType(Ty, V))
+ return 0;
+ return ConstantUInt::get(Ty, V);
+ }