diff --git a/include/llvm/Constants.h b/include/llvm/Constants.h index 36d8299f48f..5e463443ba8 100644 --- a/include/llvm/Constants.h +++ b/include/llvm/Constants.h @@ -181,9 +181,12 @@ public: /// This static method returns true if the type Ty is big enough to /// represent the value V. This can be used to avoid having the get method - /// assert when V is larger than Ty can represent. + /// assert when V is larger than Ty can represent. Note that values are + /// always treated as unsigned so if the intention is to represent a signed + /// type, you must do the conversion first. /// @returns true if V is a valid value for type Ty /// @brief Determine if the value is in range for the given type. + static bool isValueValidForType(const Type *Ty, uint64_t V); static bool isValueValidForType(const Type *Ty, int64_t V); /// @returns true if this is the null integer value. @@ -205,7 +208,7 @@ public: int64_t V = getSExtValue(); if (V < 0) return false; // Be careful about wrap-around on 'long's ++V; - return !isValueValidForType(getType()->getSignedVersion(), V) || V < 0; + return !isValueValidForType(getType(), V) || V < 0; } return isAllOnesValue(); } @@ -219,7 +222,7 @@ public: int64_t V = getSExtValue(); if (V > 0) return false; // Be careful about wrap-around on 'long's --V; - return !isValueValidForType(getType()->getSignedVersion(), V) || V > 0; + return !isValueValidForType(getType(), V) || V > 0; } return getZExtValue() == 0; } diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp index c2709182878..e7953a90e70 100644 --- a/lib/VMCore/Constants.cpp +++ b/lib/VMCore/Constants.cpp @@ -592,26 +592,31 @@ getWithOperands(const std::vector &Ops) const { //===----------------------------------------------------------------------===// // isValueValidForType implementations +bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) { + switch (Ty->getTypeID()) { + default: return false; // These can't be represented as integers! + case Type::SByteTyID: + case Type::UByteTyID: return Val <= UINT8_MAX; + case Type::ShortTyID: + case Type::UShortTyID:return Val <= UINT16_MAX; + case Type::IntTyID: + case Type::UIntTyID: return Val <= UINT32_MAX; + case Type::LongTyID: + case Type::ULongTyID: return true; // always true, has to fit in largest type + } +} + bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) { switch (Ty->getTypeID()) { - default: - return false; // These can't be represented as integers!!! - // Signed types... + default: return false; // These can't be represented as integers! case Type::SByteTyID: - return (Val <= INT8_MAX && Val >= INT8_MIN); - case Type::UByteTyID: - return (Val >= 0) && (Val <= UINT8_MAX); + case Type::UByteTyID: return (Val >= INT8_MIN && Val <= INT8_MAX); case Type::ShortTyID: - return (Val <= INT16_MAX && Val >= INT16_MIN); - case Type::UShortTyID: - return (Val >= 0) && (Val <= UINT16_MAX); + case Type::UShortTyID:return (Val >= INT16_MIN && Val <= UINT16_MAX); case Type::IntTyID: - return (Val <= int(INT32_MAX) && Val >= int(INT32_MIN)); - case Type::UIntTyID: - return (Val >= 0) && (Val <= UINT32_MAX); + case Type::UIntTyID: return (Val >= INT32_MIN && Val <= UINT32_MAX); case Type::LongTyID: - case Type::ULongTyID: - return true; // always true, has to fit in largest type + case Type::ULongTyID: return true; // always true, has to fit in largest type } }