1 //===- llvm/unittest/VMCore/ConstantsTest.cpp - Constants unit tests ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Constants.h"
11 #include "llvm/DerivedTypes.h"
12 #include "gtest/gtest.h"
17 TEST(ConstantsTest
, Integer_i1
) {
18 const IntegerType
* Int1
= IntegerType::get(1);
19 Constant
* One
= ConstantInt::get(Int1
, 1, true);
20 Constant
* Zero
= ConstantInt::get(Int1
, 0);
21 Constant
* NegOne
= ConstantInt::get(Int1
, static_cast<uint64_t>(-1), true);
22 EXPECT_EQ(NegOne
, ConstantInt::getSigned(Int1
, -1));
23 Constant
* Undef
= UndefValue::get(Int1
);
25 // Input: @b = constant i1 add(i1 1 , i1 1)
26 // Output: @b = constant i1 false
27 EXPECT_EQ(Zero
, ConstantExpr::getAdd(One
, One
));
29 // @c = constant i1 add(i1 -1, i1 1)
30 // @c = constant i1 false
31 EXPECT_EQ(Zero
, ConstantExpr::getAdd(NegOne
, One
));
33 // @d = constant i1 add(i1 -1, i1 -1)
34 // @d = constant i1 false
35 EXPECT_EQ(Zero
, ConstantExpr::getAdd(NegOne
, NegOne
));
37 // @e = constant i1 sub(i1 -1, i1 1)
38 // @e = constant i1 false
39 EXPECT_EQ(Zero
, ConstantExpr::getSub(NegOne
, One
));
41 // @f = constant i1 sub(i1 1 , i1 -1)
42 // @f = constant i1 false
43 EXPECT_EQ(Zero
, ConstantExpr::getSub(One
, NegOne
));
45 // @g = constant i1 sub(i1 1 , i1 1)
46 // @g = constant i1 false
47 EXPECT_EQ(Zero
, ConstantExpr::getSub(One
, One
));
49 // @h = constant i1 shl(i1 1 , i1 1) ; undefined
50 // @h = constant i1 undef
51 EXPECT_EQ(Undef
, ConstantExpr::getShl(One
, One
));
53 // @i = constant i1 shl(i1 1 , i1 0)
54 // @i = constant i1 true
55 EXPECT_EQ(One
, ConstantExpr::getShl(One
, Zero
));
57 // @j = constant i1 lshr(i1 1, i1 1) ; undefined
58 // @j = constant i1 undef
59 EXPECT_EQ(Undef
, ConstantExpr::getLShr(One
, One
));
61 // @m = constant i1 ashr(i1 1, i1 1) ; undefined
62 // @m = constant i1 undef
63 EXPECT_EQ(Undef
, ConstantExpr::getAShr(One
, One
));
65 // @n = constant i1 mul(i1 -1, i1 1)
66 // @n = constant i1 true
67 EXPECT_EQ(One
, ConstantExpr::getMul(NegOne
, One
));
69 // @o = constant i1 sdiv(i1 -1, i1 1) ; overflow
70 // @o = constant i1 true
71 EXPECT_EQ(One
, ConstantExpr::getSDiv(NegOne
, One
));
73 // @p = constant i1 sdiv(i1 1 , i1 -1); overflow
74 // @p = constant i1 true
75 EXPECT_EQ(One
, ConstantExpr::getSDiv(One
, NegOne
));
77 // @q = constant i1 udiv(i1 -1, i1 1)
78 // @q = constant i1 true
79 EXPECT_EQ(One
, ConstantExpr::getUDiv(NegOne
, One
));
81 // @r = constant i1 udiv(i1 1, i1 -1)
82 // @r = constant i1 true
83 EXPECT_EQ(One
, ConstantExpr::getUDiv(One
, NegOne
));
85 // @s = constant i1 srem(i1 -1, i1 1) ; overflow
86 // @s = constant i1 false
87 EXPECT_EQ(Zero
, ConstantExpr::getSRem(NegOne
, One
));
89 // @t = constant i1 urem(i1 -1, i1 1)
90 // @t = constant i1 false
91 EXPECT_EQ(Zero
, ConstantExpr::getURem(NegOne
, One
));
93 // @u = constant i1 srem(i1 1, i1 -1) ; overflow
94 // @u = constant i1 false
95 EXPECT_EQ(Zero
, ConstantExpr::getSRem(One
, NegOne
));
98 TEST(ConstantsTest
, IntSigns
) {
99 const IntegerType
* Int8Ty
= Type::Int8Ty
;
100 EXPECT_EQ(100, ConstantInt::get(Int8Ty
, 100, false)->getSExtValue());
101 EXPECT_EQ(100, ConstantInt::get(Int8Ty
, 100, true)->getSExtValue());
102 EXPECT_EQ(100, ConstantInt::getSigned(Int8Ty
, 100)->getSExtValue());
103 EXPECT_EQ(-50, ConstantInt::get(Int8Ty
, 206)->getSExtValue());
104 EXPECT_EQ(-50, ConstantInt::getSigned(Int8Ty
, -50)->getSExtValue());
105 EXPECT_EQ(206U, ConstantInt::getSigned(Int8Ty
, -50)->getZExtValue());
107 // Overflow is handled by truncation.
108 EXPECT_EQ(0x3b, ConstantInt::get(Int8Ty
, 0x13b)->getSExtValue());
111 } // end anonymous namespace
112 } // end namespace llvm