+namespace {
+ class DefVal {
+ const ConstantInt * const Val;
+ const Type * const Ty;
+ protected:
+ inline DefVal(const ConstantInt *val, const Type *ty) : Val(val), Ty(ty) {}
+ public:
+ inline const Type *getType() const { return Ty; }
+ inline const ConstantInt *getVal() const { return Val; }
+ inline operator const ConstantInt * () const { return Val; }
+ inline const ConstantInt *operator->() const { return Val; }
+ };
+
+ struct DefZero : public DefVal {
+ inline DefZero(const ConstantInt *val, const Type *ty) : DefVal(val, ty) {}
+ inline DefZero(const ConstantInt *val) : DefVal(val, val->getType()) {}
+ };
+
+ struct DefOne : public DefVal {
+ inline DefOne(const ConstantInt *val, const Type *ty) : DefVal(val, ty) {}
+ };
+}
+
+
+// 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);
+ }