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(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 = getGlobalContext().getUndef(Int1);
26 // Input: @b = constant i1 add(i1 1 , i1 1)
27 // Output: @b = constant i1 false
28 EXPECT_EQ(Zero, getGlobalContext().getConstantExprAdd(One, One));
30 // @c = constant i1 add(i1 -1, i1 1)
31 // @c = constant i1 false
32 EXPECT_EQ(Zero, getGlobalContext().getConstantExprAdd(NegOne, One));
34 // @d = constant i1 add(i1 -1, i1 -1)
35 // @d = constant i1 false
36 EXPECT_EQ(Zero, getGlobalContext().getConstantExprAdd(NegOne, NegOne));
38 // @e = constant i1 sub(i1 -1, i1 1)
39 // @e = constant i1 false
40 EXPECT_EQ(Zero, getGlobalContext().getConstantExprSub(NegOne, One));
42 // @f = constant i1 sub(i1 1 , i1 -1)
43 // @f = constant i1 false
44 EXPECT_EQ(Zero, getGlobalContext().getConstantExprSub(One, NegOne));
46 // @g = constant i1 sub(i1 1 , i1 1)
47 // @g = constant i1 false
48 EXPECT_EQ(Zero, getGlobalContext().getConstantExprSub(One, One));
50 // @h = constant i1 shl(i1 1 , i1 1) ; undefined
51 // @h = constant i1 undef
52 EXPECT_EQ(Undef, getGlobalContext().getConstantExprShl(One, One));
54 // @i = constant i1 shl(i1 1 , i1 0)
55 // @i = constant i1 true
56 EXPECT_EQ(One, getGlobalContext().getConstantExprShl(One, Zero));
58 // @j = constant i1 lshr(i1 1, i1 1) ; undefined
59 // @j = constant i1 undef
60 EXPECT_EQ(Undef, getGlobalContext().getConstantExprLShr(One, One));
62 // @m = constant i1 ashr(i1 1, i1 1) ; undefined
63 // @m = constant i1 undef
64 EXPECT_EQ(Undef, getGlobalContext().getConstantExprAShr(One, One));
66 // @n = constant i1 mul(i1 -1, i1 1)
67 // @n = constant i1 true
68 EXPECT_EQ(One, getGlobalContext().getConstantExprMul(NegOne, One));
70 // @o = constant i1 sdiv(i1 -1, i1 1) ; overflow
71 // @o = constant i1 true
72 EXPECT_EQ(One, getGlobalContext().getConstantExprSDiv(NegOne, One));
74 // @p = constant i1 sdiv(i1 1 , i1 -1); overflow
75 // @p = constant i1 true
76 EXPECT_EQ(One, getGlobalContext().getConstantExprSDiv(One, NegOne));
78 // @q = constant i1 udiv(i1 -1, i1 1)
79 // @q = constant i1 true
80 EXPECT_EQ(One, getGlobalContext().getConstantExprUDiv(NegOne, One));
82 // @r = constant i1 udiv(i1 1, i1 -1)
83 // @r = constant i1 true
84 EXPECT_EQ(One, getGlobalContext().getConstantExprUDiv(One, NegOne));
86 // @s = constant i1 srem(i1 -1, i1 1) ; overflow
87 // @s = constant i1 false
88 EXPECT_EQ(Zero, getGlobalContext().getConstantExprSRem(NegOne, One));
90 // @t = constant i1 urem(i1 -1, i1 1)
91 // @t = constant i1 false
92 EXPECT_EQ(Zero, getGlobalContext().getConstantExprURem(NegOne, One));
94 // @u = constant i1 srem(i1 1, i1 -1) ; overflow
95 // @u = constant i1 false
96 EXPECT_EQ(Zero, getGlobalContext().getConstantExprSRem(One, NegOne));
99 TEST(ConstantsTest, IntSigns) {
100 const IntegerType* Int8Ty = Type::Int8Ty;
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 } // end anonymous namespace
113 } // end namespace llvm