fee38b891de427b05d3327a635ff245ce6b36048
[oota-llvm.git] / unittests / IR / ConstantsTest.cpp
1 //===- llvm/unittest/IR/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/IR/Constants.h"
11 #include "llvm/IR/DerivedTypes.h"
12 #include "llvm/IR/InstrTypes.h"
13 #include "llvm/IR/Instruction.h"
14 #include "llvm/IR/LLVMContext.h"
15 #include "llvm/IR/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 TEST(ConstantsTest, PointerCast) {
125   LLVMContext &C(getGlobalContext());
126   Type *Int8PtrTy = Type::getInt8PtrTy(C);
127   Type *Int32PtrTy = Type::getInt32PtrTy(C);
128   Type *Int64Ty = Type::getInt64Ty(C);
129   VectorType *Int8PtrVecTy = VectorType::get(Int8PtrTy, 4);
130   VectorType *Int32PtrVecTy = VectorType::get(Int32PtrTy, 4);
131   VectorType *Int64VecTy = VectorType::get(Int64Ty, 4);
132
133   // ptrtoint i8* to i64
134   EXPECT_EQ(Constant::getNullValue(Int64Ty),
135             ConstantExpr::getPointerCast(
136               Constant::getNullValue(Int8PtrTy), Int64Ty));
137
138   // bitcast i8* to i32*
139   EXPECT_EQ(Constant::getNullValue(Int32PtrTy),
140             ConstantExpr::getPointerCast(
141               Constant::getNullValue(Int8PtrTy), Int32PtrTy));
142
143   // ptrtoint <4 x i8*> to <4 x i64>
144   EXPECT_EQ(Constant::getNullValue(Int64VecTy),
145             ConstantExpr::getPointerCast(
146               Constant::getNullValue(Int8PtrVecTy), Int64VecTy));
147
148   // bitcast <4 x i8*> to <4 x i32*>
149   EXPECT_EQ(Constant::getNullValue(Int32PtrVecTy),
150             ConstantExpr::getPointerCast(
151               Constant::getNullValue(Int8PtrVecTy), Int32PtrVecTy));
152 }
153
154 #define CHECK(x, y) {                                                   \
155     std::string __s;                                                    \
156     raw_string_ostream __o(__s);                                        \
157     Instruction *__I = cast<ConstantExpr>(x)->getAsInstruction();       \
158     __I->print(__o);                                                    \
159     delete __I;                                                         \
160     __o.flush();                                                        \
161     EXPECT_EQ(std::string("  <badref> = " y), __s);                     \
162   }
163
164 TEST(ConstantsTest, AsInstructionsTest) {
165   OwningPtr<Module> M(new Module("MyModule", getGlobalContext()));
166
167   Type *Int64Ty = Type::getInt64Ty(getGlobalContext());
168   Type *Int32Ty = Type::getInt32Ty(getGlobalContext());
169   Type *Int16Ty = Type::getInt16Ty(getGlobalContext());
170   Type *Int1Ty = Type::getInt1Ty(getGlobalContext());
171   Type *FloatTy = Type::getFloatTy(getGlobalContext());
172   Type *DoubleTy = Type::getDoubleTy(getGlobalContext());
173
174   Constant *Global = M->getOrInsertGlobal("dummy",
175                                          PointerType::getUnqual(Int32Ty));
176   Constant *Global2 = M->getOrInsertGlobal("dummy2",
177                                          PointerType::getUnqual(Int32Ty));
178
179   Constant *P0 = ConstantExpr::getPtrToInt(Global, Int32Ty);
180   Constant *P1 = ConstantExpr::getUIToFP(P0, FloatTy);
181   Constant *P2 = ConstantExpr::getUIToFP(P0, DoubleTy);
182   Constant *P3 = ConstantExpr::getTrunc(P0, Int1Ty);
183   Constant *P4 = ConstantExpr::getPtrToInt(Global2, Int32Ty);
184   Constant *P5 = ConstantExpr::getUIToFP(P4, FloatTy);
185   Constant *P6 = ConstantExpr::getBitCast(P4, VectorType::get(Int16Ty, 2));
186
187   Constant *One = ConstantInt::get(Int32Ty, 1);
188
189   #define P0STR "ptrtoint (i32** @dummy to i32)"
190   #define P1STR "uitofp (i32 ptrtoint (i32** @dummy to i32) to float)"
191   #define P2STR "uitofp (i32 ptrtoint (i32** @dummy to i32) to double)"
192   #define P3STR "ptrtoint (i32** @dummy to i1)"
193   #define P4STR "ptrtoint (i32** @dummy2 to i32)"
194   #define P5STR "uitofp (i32 ptrtoint (i32** @dummy2 to i32) to float)"
195   #define P6STR "bitcast (i32 ptrtoint (i32** @dummy2 to i32) to <2 x i16>)"
196
197   CHECK(ConstantExpr::getNeg(P0), "sub i32 0, " P0STR);
198   CHECK(ConstantExpr::getFNeg(P1), "fsub float -0.000000e+00, " P1STR);
199   CHECK(ConstantExpr::getNot(P0), "xor i32 " P0STR ", -1");
200   CHECK(ConstantExpr::getAdd(P0, P0), "add i32 " P0STR ", " P0STR);
201   CHECK(ConstantExpr::getAdd(P0, P0, false, true), "add nsw i32 " P0STR ", "
202         P0STR);
203   CHECK(ConstantExpr::getAdd(P0, P0, true, true), "add nuw nsw i32 " P0STR ", "
204         P0STR);
205   CHECK(ConstantExpr::getFAdd(P1, P1), "fadd float " P1STR ", " P1STR);
206   CHECK(ConstantExpr::getSub(P0, P0), "sub i32 " P0STR ", " P0STR);
207   CHECK(ConstantExpr::getFSub(P1, P1), "fsub float " P1STR ", " P1STR);
208   CHECK(ConstantExpr::getMul(P0, P0), "mul i32 " P0STR ", " P0STR);
209   CHECK(ConstantExpr::getFMul(P1, P1), "fmul float " P1STR ", " P1STR);
210   CHECK(ConstantExpr::getUDiv(P0, P0), "udiv i32 " P0STR ", " P0STR);
211   CHECK(ConstantExpr::getSDiv(P0, P0), "sdiv i32 " P0STR ", " P0STR);
212   CHECK(ConstantExpr::getFDiv(P1, P1), "fdiv float " P1STR ", " P1STR);
213   CHECK(ConstantExpr::getURem(P0, P0), "urem i32 " P0STR ", " P0STR);
214   CHECK(ConstantExpr::getSRem(P0, P0), "srem i32 " P0STR ", " P0STR);
215   CHECK(ConstantExpr::getFRem(P1, P1), "frem float " P1STR ", " P1STR);
216   CHECK(ConstantExpr::getAnd(P0, P0), "and i32 " P0STR ", " P0STR);
217   CHECK(ConstantExpr::getOr(P0, P0), "or i32 " P0STR ", " P0STR);
218   CHECK(ConstantExpr::getXor(P0, P0), "xor i32 " P0STR ", " P0STR);
219   CHECK(ConstantExpr::getShl(P0, P0), "shl i32 " P0STR ", " P0STR);
220   CHECK(ConstantExpr::getShl(P0, P0, true), "shl nuw i32 " P0STR ", " P0STR);
221   CHECK(ConstantExpr::getShl(P0, P0, false, true), "shl nsw i32 " P0STR ", "
222         P0STR);
223   CHECK(ConstantExpr::getLShr(P0, P0, false), "lshr i32 " P0STR ", " P0STR);
224   CHECK(ConstantExpr::getLShr(P0, P0, true), "lshr exact i32 " P0STR ", " P0STR);
225   CHECK(ConstantExpr::getAShr(P0, P0, false), "ashr i32 " P0STR ", " P0STR);
226   CHECK(ConstantExpr::getAShr(P0, P0, true), "ashr exact i32 " P0STR ", " P0STR);
227
228   CHECK(ConstantExpr::getSExt(P0, Int64Ty), "sext i32 " P0STR " to i64");
229   CHECK(ConstantExpr::getZExt(P0, Int64Ty), "zext i32 " P0STR " to i64");
230   CHECK(ConstantExpr::getFPTrunc(P2, FloatTy), "fptrunc double " P2STR
231         " to float");
232   CHECK(ConstantExpr::getFPExtend(P1, DoubleTy), "fpext float " P1STR
233         " to double");
234
235   CHECK(ConstantExpr::getExactUDiv(P0, P0), "udiv exact i32 " P0STR ", " P0STR);
236
237   CHECK(ConstantExpr::getSelect(P3, P0, P4), "select i1 " P3STR ", i32 " P0STR
238         ", i32 " P4STR);
239   CHECK(ConstantExpr::getICmp(CmpInst::ICMP_EQ, P0, P4), "icmp eq i32 " P0STR
240         ", " P4STR);
241   CHECK(ConstantExpr::getFCmp(CmpInst::FCMP_ULT, P1, P5), "fcmp ult float "
242         P1STR ", " P5STR);
243
244   std::vector<Constant*> V;
245   V.push_back(One);
246   // FIXME: getGetElementPtr() actually creates an inbounds ConstantGEP,
247   //        not a normal one!
248   //CHECK(ConstantExpr::getGetElementPtr(Global, V, false),
249   //      "getelementptr i32** @dummy, i32 1");
250   CHECK(ConstantExpr::getInBoundsGetElementPtr(Global, V),
251         "getelementptr inbounds i32** @dummy, i32 1");
252
253   CHECK(ConstantExpr::getExtractElement(P6, One), "extractelement <2 x i16> "
254         P6STR ", i32 1");
255 }
256
257 #undef CHECK
258
259 }  // end anonymous namespace
260 }  // end namespace llvm