summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/llvm/Constants.h9
-rw-r--r--unittests/VMCore/ConstantsTest.cpp14
2 files changed, 23 insertions, 0 deletions
diff --git a/include/llvm/Constants.h b/include/llvm/Constants.h
index 0f4c29a058..e04a6cf67d 100644
--- a/include/llvm/Constants.h
+++ b/include/llvm/Constants.h
@@ -110,6 +110,15 @@ public:
/// @brief Get a ConstantInt for a specific value.
static ConstantInt *get(const Type *Ty, uint64_t V, bool isSigned = false);
+ /// Return a ConstantInt with the specified value for the specified type. The
+ /// value V will be canonicalized to a an unsigned APInt. Accessing it with
+ /// either getSExtValue() or getZExtValue() will yield a correctly sized and
+ /// signed value for the type Ty.
+ /// @brief Get a ConstantInt for a specific signed value.
+ static ConstantInt *getSigned(const Type *Ty, int64_t V) {
+ return get(Ty, V, true);
+ }
+
/// Return a ConstantInt with the specified value and an implied Type. The
/// type is the integer type that corresponds to the bit width of the value.
static ConstantInt *get(const APInt &V);
diff --git a/unittests/VMCore/ConstantsTest.cpp b/unittests/VMCore/ConstantsTest.cpp
index c40dc7e471..519d928eac 100644
--- a/unittests/VMCore/ConstantsTest.cpp
+++ b/unittests/VMCore/ConstantsTest.cpp
@@ -19,6 +19,7 @@ TEST(ConstantsTest, Integer_i1) {
Constant* One = ConstantInt::get(Int1, 1, true);
Constant* Zero = ConstantInt::get(Int1, 0);
Constant* NegOne = ConstantInt::get(Int1, static_cast<uint64_t>(-1), true);
+ EXPECT_EQ(NegOne, ConstantInt::getSigned(Int1, -1));
Constant* Undef = UndefValue::get(Int1);
// Input: @b = constant i1 add(i1 1 , i1 1)
@@ -94,5 +95,18 @@ TEST(ConstantsTest, Integer_i1) {
EXPECT_EQ(Zero, ConstantExpr::getSRem(One, NegOne));
}
+TEST(ConstantsTest, IntSigns) {
+ const IntegerType* Int8Ty = Type::Int8Ty;
+ EXPECT_EQ(100, ConstantInt::get(Int8Ty, 100, false)->getSExtValue());
+ EXPECT_EQ(100, ConstantInt::get(Int8Ty, 100, true)->getSExtValue());
+ EXPECT_EQ(100, ConstantInt::getSigned(Int8Ty, 100)->getSExtValue());
+ EXPECT_EQ(-50, ConstantInt::get(Int8Ty, 206)->getSExtValue());
+ EXPECT_EQ(-50, ConstantInt::getSigned(Int8Ty, -50)->getSExtValue());
+ EXPECT_EQ(206U, ConstantInt::getSigned(Int8Ty, -50)->getZExtValue());
+
+ // Overflow is handled by truncation.
+ EXPECT_EQ(0x3b, ConstantInt::get(Int8Ty, 0x13b)->getSExtValue());
+}
+
} // end anonymous namespace
} // end namespace llvm