25d61cc6caa07e7d1dffe0fc75c625be8d1dd6cb
[oota-llvm.git] / unittests / VMCore / ConstantsTest.cpp
1 //===- llvm/unittest/VMCore/ConstantsTest.cpp - Constants unit tests ------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/Constants.h"
11 #include "llvm/Instruction.h"
12 #include "llvm/InstrTypes.h"
13 #include "llvm/DerivedTypes.h"
14 #include "llvm/LLVMContext.h"
15 #include "llvm/Module.h"
16 #include "gtest/gtest.h"
17
18 namespace llvm {
19 namespace {
20
21 TEST(ConstantsTest, Integer_i1) {
22   IntegerType* Int1 = IntegerType::get(getGlobalContext(), 1);
23   Constant* One = ConstantInt::get(Int1, 1, true);
24   Constant* Zero = ConstantInt::get(Int1, 0);
25   Constant* NegOne = ConstantInt::get(Int1, static_cast<uint64_t>(-1), true);
26   EXPECT_EQ(NegOne, ConstantInt::getSigned(Int1, -1));
27   Constant* Undef = UndefValue::get(Int1);
28
29   // Input:  @b = constant i1 add(i1 1 , i1 1)
30   // Output: @b = constant i1 false
31   EXPECT_EQ(Zero, ConstantExpr::getAdd(One, One));
32
33   // @c = constant i1 add(i1 -1, i1 1)
34   // @c = constant i1 false
35   EXPECT_EQ(Zero, ConstantExpr::getAdd(NegOne, One));
36
37   // @d = constant i1 add(i1 -1, i1 -1)
38   // @d = constant i1 false
39   EXPECT_EQ(Zero, ConstantExpr::getAdd(NegOne, NegOne));
40
41   // @e = constant i1 sub(i1 -1, i1 1)
42   // @e = constant i1 false
43   EXPECT_EQ(Zero, ConstantExpr::getSub(NegOne, One));
44
45   // @f = constant i1 sub(i1 1 , i1 -1)
46   // @f = constant i1 false
47   EXPECT_EQ(Zero, ConstantExpr::getSub(One, NegOne));
48
49   // @g = constant i1 sub(i1 1 , i1 1)
50   // @g = constant i1 false
51   EXPECT_EQ(Zero, ConstantExpr::getSub(One, One));
52
53   // @h = constant i1 shl(i1 1 , i1 1)  ; undefined
54   // @h = constant i1 undef
55   EXPECT_EQ(Undef, ConstantExpr::getShl(One, One));
56
57   // @i = constant i1 shl(i1 1 , i1 0)
58   // @i = constant i1 true
59   EXPECT_EQ(One, ConstantExpr::getShl(One, Zero));
60
61   // @j = constant i1 lshr(i1 1, i1 1)  ; undefined
62   // @j = constant i1 undef
63   EXPECT_EQ(Undef, ConstantExpr::getLShr(One, One));
64
65   // @m = constant i1 ashr(i1 1, i1 1)  ; undefined
66   // @m = constant i1 undef
67   EXPECT_EQ(Undef, ConstantExpr::getAShr(One, One));
68
69   // @n = constant i1 mul(i1 -1, i1 1)
70   // @n = constant i1 true
71   EXPECT_EQ(One, ConstantExpr::getMul(NegOne, One));
72
73   // @o = constant i1 sdiv(i1 -1, i1 1) ; overflow
74   // @o = constant i1 true
75   EXPECT_EQ(One, ConstantExpr::getSDiv(NegOne, One));
76
77   // @p = constant i1 sdiv(i1 1 , i1 -1); overflow
78   // @p = constant i1 true
79   EXPECT_EQ(One, ConstantExpr::getSDiv(One, NegOne));
80
81   // @q = constant i1 udiv(i1 -1, i1 1)
82   // @q = constant i1 true
83   EXPECT_EQ(One, ConstantExpr::getUDiv(NegOne, One));
84
85   // @r = constant i1 udiv(i1 1, i1 -1)
86   // @r = constant i1 true
87   EXPECT_EQ(One, ConstantExpr::getUDiv(One, NegOne));
88
89   // @s = constant i1 srem(i1 -1, i1 1) ; overflow
90   // @s = constant i1 false
91   EXPECT_EQ(Zero, ConstantExpr::getSRem(NegOne, One));
92
93   // @t = constant i1 urem(i1 -1, i1 1)
94   // @t = constant i1 false
95   EXPECT_EQ(Zero, ConstantExpr::getURem(NegOne, One));
96
97   // @u = constant i1 srem(i1  1, i1 -1) ; overflow
98   // @u = constant i1 false
99   EXPECT_EQ(Zero, ConstantExpr::getSRem(One, NegOne));
100 }
101
102 TEST(ConstantsTest, IntSigns) {
103   IntegerType* Int8Ty = Type::getInt8Ty(getGlobalContext());
104   EXPECT_EQ(100, ConstantInt::get(Int8Ty, 100, false)->getSExtValue());
105   EXPECT_EQ(100, ConstantInt::get(Int8Ty, 100, true)->getSExtValue());
106   EXPECT_EQ(100, ConstantInt::getSigned(Int8Ty, 100)->getSExtValue());
107   EXPECT_EQ(-50, ConstantInt::get(Int8Ty, 206)->getSExtValue());
108   EXPECT_EQ(-50, ConstantInt::getSigned(Int8Ty, -50)->getSExtValue());
109   EXPECT_EQ(206U, ConstantInt::getSigned(Int8Ty, -50)->getZExtValue());
110
111   // Overflow is handled by truncation.
112   EXPECT_EQ(0x3b, ConstantInt::get(Int8Ty, 0x13b)->getSExtValue());
113 }
114
115 TEST(ConstantsTest, FP128Test) {
116   Type *FP128Ty = Type::getFP128Ty(getGlobalContext());
117
118   IntegerType *Int128Ty = Type::getIntNTy(getGlobalContext(), 128);
119   Constant *Zero128 = Constant::getNullValue(Int128Ty);
120   Constant *X = ConstantExpr::getUIToFP(Zero128, FP128Ty);
121   EXPECT_TRUE(isa<ConstantFP>(X));
122 }
123
124 #define CHECK(x, y) {                                           \
125     std::string __s;                                            \
126     raw_string_ostream __o(__s);                                \
127     cast<ConstantExpr>(x)->getAsInstruction()->print(__o);      \
128     __o.flush();                                                \
129     EXPECT_EQ(std::string("  <badref> = " y), __s);             \
130   }
131
132 TEST(ConstantsTest, AsInstructionsTest) {
133   Module *M = new Module("MyModule", getGlobalContext());
134
135   Type *Int64Ty = Type::getInt64Ty(getGlobalContext());
136   Type *Int32Ty = Type::getInt32Ty(getGlobalContext());
137   Type *Int16Ty = Type::getInt16Ty(getGlobalContext());
138   Type *Int1Ty = Type::getInt1Ty(getGlobalContext());
139   Type *FloatTy = Type::getFloatTy(getGlobalContext());
140   Type *DoubleTy = Type::getDoubleTy(getGlobalContext());
141
142   Constant *Global = M->getOrInsertGlobal("dummy",
143                                          PointerType::getUnqual(Int32Ty));
144   Constant *Global2 = M->getOrInsertGlobal("dummy2",
145                                          PointerType::getUnqual(Int32Ty));
146
147   Constant *P0 = ConstantExpr::getPtrToInt(Global, Int32Ty);
148   Constant *P1 = ConstantExpr::getUIToFP(P0, FloatTy);
149   Constant *P2 = ConstantExpr::getUIToFP(P0, DoubleTy);
150   Constant *P3 = ConstantExpr::getTrunc(P0, Int1Ty);
151   Constant *P4 = ConstantExpr::getPtrToInt(Global2, Int32Ty);
152   Constant *P5 = ConstantExpr::getUIToFP(P4, FloatTy);
153   Constant *P6 = ConstantExpr::getBitCast(P4, VectorType::get(Int16Ty, 2));
154
155   Constant *One = ConstantInt::get(Int32Ty, 1);
156
157   #define P0STR "ptrtoint (i32** @dummy to i32)"
158   #define P1STR "uitofp (i32 ptrtoint (i32** @dummy to i32) to float)"
159   #define P2STR "uitofp (i32 ptrtoint (i32** @dummy to i32) to double)"
160   #define P3STR "ptrtoint (i32** @dummy to i1)"
161   #define P4STR "ptrtoint (i32** @dummy2 to i32)"
162   #define P5STR "uitofp (i32 ptrtoint (i32** @dummy2 to i32) to float)"
163   #define P6STR "bitcast (i32 ptrtoint (i32** @dummy2 to i32) to <2 x i16>)"
164
165   CHECK(ConstantExpr::getNeg(P0), "sub i32 0, " P0STR);
166   CHECK(ConstantExpr::getFNeg(P1), "fsub float -0.000000e+00, " P1STR);
167   CHECK(ConstantExpr::getNot(P0), "xor i32 " P0STR ", -1");
168   CHECK(ConstantExpr::getAdd(P0, P0), "add i32 " P0STR ", " P0STR);
169   CHECK(ConstantExpr::getAdd(P0, P0, false, true), "add nsw i32 " P0STR ", "
170         P0STR);
171   CHECK(ConstantExpr::getAdd(P0, P0, true, true), "add nuw nsw i32 " P0STR ", "
172         P0STR);
173   CHECK(ConstantExpr::getFAdd(P1, P1), "fadd float " P1STR ", " P1STR);
174   CHECK(ConstantExpr::getSub(P0, P0), "sub i32 " P0STR ", " P0STR);
175   CHECK(ConstantExpr::getFSub(P1, P1), "fsub float " P1STR ", " P1STR);
176   CHECK(ConstantExpr::getMul(P0, P0), "mul i32 " P0STR ", " P0STR);
177   CHECK(ConstantExpr::getFMul(P1, P1), "fmul float " P1STR ", " P1STR);
178   CHECK(ConstantExpr::getUDiv(P0, P0), "udiv i32 " P0STR ", " P0STR);
179   CHECK(ConstantExpr::getSDiv(P0, P0), "sdiv i32 " P0STR ", " P0STR);
180   CHECK(ConstantExpr::getFDiv(P1, P1), "fdiv float " P1STR ", " P1STR);
181   CHECK(ConstantExpr::getURem(P0, P0), "urem i32 " P0STR ", " P0STR);
182   CHECK(ConstantExpr::getSRem(P0, P0), "srem i32 " P0STR ", " P0STR);
183   CHECK(ConstantExpr::getFRem(P1, P1), "frem float " P1STR ", " P1STR);
184   CHECK(ConstantExpr::getAnd(P0, P0), "and i32 " P0STR ", " P0STR);
185   CHECK(ConstantExpr::getOr(P0, P0), "or i32 " P0STR ", " P0STR);
186   CHECK(ConstantExpr::getXor(P0, P0), "xor i32 " P0STR ", " P0STR);
187   CHECK(ConstantExpr::getShl(P0, P0), "shl i32 " P0STR ", " P0STR);
188   CHECK(ConstantExpr::getShl(P0, P0, true), "shl nuw i32 " P0STR ", " P0STR);
189   CHECK(ConstantExpr::getShl(P0, P0, false, true), "shl nsw i32 " P0STR ", "
190         P0STR);
191   CHECK(ConstantExpr::getLShr(P0, P0, false), "lshr i32 " P0STR ", " P0STR);
192   CHECK(ConstantExpr::getLShr(P0, P0, true), "lshr exact i32 " P0STR ", " P0STR);
193   CHECK(ConstantExpr::getAShr(P0, P0, false), "ashr i32 " P0STR ", " P0STR);
194   CHECK(ConstantExpr::getAShr(P0, P0, true), "ashr exact i32 " P0STR ", " P0STR);
195
196   CHECK(ConstantExpr::getSExt(P0, Int64Ty), "sext i32 " P0STR " to i64");
197   CHECK(ConstantExpr::getZExt(P0, Int64Ty), "zext i32 " P0STR " to i64");
198   CHECK(ConstantExpr::getFPTrunc(P2, FloatTy), "fptrunc double " P2STR
199         " to float");
200   CHECK(ConstantExpr::getFPExtend(P1, DoubleTy), "fpext float " P1STR
201         " to double");
202
203   CHECK(ConstantExpr::getExactUDiv(P0, P0), "udiv exact i32 " P0STR ", " P0STR);
204
205   CHECK(ConstantExpr::getSelect(P3, P0, P4), "select i1 " P3STR ", i32 " P0STR
206         ", i32 " P4STR);
207   CHECK(ConstantExpr::getICmp(CmpInst::ICMP_EQ, P0, P4), "icmp eq i32 " P0STR
208         ", " P4STR);
209   CHECK(ConstantExpr::getFCmp(CmpInst::FCMP_ULT, P1, P5), "fcmp ult float "
210         P1STR ", " P5STR);
211
212   std::vector<Constant*> V;
213   V.push_back(One);
214   // FIXME: getGetElementPtr() actually creates an inbounds ConstantGEP,
215   //        not a normal one!
216   //CHECK(ConstantExpr::getGetElementPtr(Global, V, false),
217   //      "getelementptr i32** @dummy, i32 1");
218   CHECK(ConstantExpr::getInBoundsGetElementPtr(Global, V),
219         "getelementptr inbounds i32** @dummy, i32 1");
220
221   CHECK(ConstantExpr::getExtractElement(P6, One), "extractelement <2 x i16> "
222         P6STR ", i32 1");
223 }
224
225 #undef CHECK
226
227 }  // end anonymous namespace
228 }  // end namespace llvm