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 "llvm/LLVMContext.h"
13 #include "gtest/gtest.h"
18 TEST(ConstantsTest
, Integer_i1
) {
19 const IntegerType
* Int1
= IntegerType::get(getGlobalContext(), 1);
20 Constant
* One
= ConstantInt::get(Int1
, 1, true);
21 Constant
* Zero
= ConstantInt::get(Int1
, 0);
22 Constant
* NegOne
= ConstantInt::get(Int1
, static_cast<uint64_t>(-1), true);
23 EXPECT_EQ(NegOne
, ConstantInt::getSigned(Int1
, -1));
24 Constant
* Undef
= UndefValue::get(Int1
);
26 // Input: @b = constant i1 add(i1 1 , i1 1)
27 // Output: @b = constant i1 false
28 EXPECT_EQ(Zero
, ConstantExpr::getAdd(One
, One
));
30 // @c = constant i1 add(i1 -1, i1 1)
31 // @c = constant i1 false
32 EXPECT_EQ(Zero
, ConstantExpr::getAdd(NegOne
, One
));
34 // @d = constant i1 add(i1 -1, i1 -1)
35 // @d = constant i1 false
36 EXPECT_EQ(Zero
, ConstantExpr::getAdd(NegOne
, NegOne
));
38 // @e = constant i1 sub(i1 -1, i1 1)
39 // @e = constant i1 false
40 EXPECT_EQ(Zero
, ConstantExpr::getSub(NegOne
, One
));
42 // @f = constant i1 sub(i1 1 , i1 -1)
43 // @f = constant i1 false
44 EXPECT_EQ(Zero
, ConstantExpr::getSub(One
, NegOne
));
46 // @g = constant i1 sub(i1 1 , i1 1)
47 // @g = constant i1 false
48 EXPECT_EQ(Zero
, ConstantExpr::getSub(One
, One
));
50 // @h = constant i1 shl(i1 1 , i1 1) ; undefined
51 // @h = constant i1 undef
52 EXPECT_EQ(Undef
, ConstantExpr::getShl(One
, One
));
54 // @i = constant i1 shl(i1 1 , i1 0)
55 // @i = constant i1 true
56 EXPECT_EQ(One
, ConstantExpr::getShl(One
, Zero
));
58 // @j = constant i1 lshr(i1 1, i1 1) ; undefined
59 // @j = constant i1 undef
60 EXPECT_EQ(Undef
, ConstantExpr::getLShr(One
, One
));
62 // @m = constant i1 ashr(i1 1, i1 1) ; undefined
63 // @m = constant i1 undef
64 EXPECT_EQ(Undef
, ConstantExpr::getAShr(One
, One
));
66 // @n = constant i1 mul(i1 -1, i1 1)
67 // @n = constant i1 true
68 EXPECT_EQ(One
, ConstantExpr::getMul(NegOne
, One
));
70 // @o = constant i1 sdiv(i1 -1, i1 1) ; overflow
71 // @o = constant i1 true
72 EXPECT_EQ(One
, ConstantExpr::getSDiv(NegOne
, One
));
74 // @p = constant i1 sdiv(i1 1 , i1 -1); overflow
75 // @p = constant i1 true
76 EXPECT_EQ(One
, ConstantExpr::getSDiv(One
, NegOne
));
78 // @q = constant i1 udiv(i1 -1, i1 1)
79 // @q = constant i1 true
80 EXPECT_EQ(One
, ConstantExpr::getUDiv(NegOne
, One
));
82 // @r = constant i1 udiv(i1 1, i1 -1)
83 // @r = constant i1 true
84 EXPECT_EQ(One
, ConstantExpr::getUDiv(One
, NegOne
));
86 // @s = constant i1 srem(i1 -1, i1 1) ; overflow
87 // @s = constant i1 false
88 EXPECT_EQ(Zero
, ConstantExpr::getSRem(NegOne
, One
));
90 // @t = constant i1 urem(i1 -1, i1 1)
91 // @t = constant i1 false
92 EXPECT_EQ(Zero
, ConstantExpr::getURem(NegOne
, One
));
94 // @u = constant i1 srem(i1 1, i1 -1) ; overflow
95 // @u = constant i1 false
96 EXPECT_EQ(Zero
, ConstantExpr::getSRem(One
, NegOne
));
99 TEST(ConstantsTest
, IntSigns
) {
100 const IntegerType
* Int8Ty
= Type::getInt8Ty(getGlobalContext());
101 EXPECT_EQ(100, ConstantInt::get(Int8Ty
, 100, false)->getSExtValue());
102 EXPECT_EQ(100, ConstantInt::get(Int8Ty
, 100, true)->getSExtValue());
103 EXPECT_EQ(100, ConstantInt::getSigned(Int8Ty
, 100)->getSExtValue());
104 EXPECT_EQ(-50, ConstantInt::get(Int8Ty
, 206)->getSExtValue());
105 EXPECT_EQ(-50, ConstantInt::getSigned(Int8Ty
, -50)->getSExtValue());
106 EXPECT_EQ(206U, ConstantInt::getSigned(Int8Ty
, -50)->getZExtValue());
108 // Overflow is handled by truncation.
109 EXPECT_EQ(0x3b, ConstantInt::get(Int8Ty
, 0x13b)->getSExtValue());
112 TEST(ConstantsTest
, FP128Test
) {
113 const Type
*FP128Ty
= Type::getFP128Ty(getGlobalContext());
115 const IntegerType
*Int128Ty
= Type::getIntNTy(getGlobalContext(), 128);
116 Constant
*Zero128
= Constant::getNullValue(Int128Ty
);
117 Constant
*X
= ConstantExpr::getUIToFP(Zero128
, FP128Ty
);
118 EXPECT_TRUE(isa
<ConstantFP
>(X
));
121 } // end anonymous namespace
122 } // end namespace llvm