[X86][SSE] Bitcast assertion in XFormVExtractWithShuffleIntoLoad
[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   std::unique_ptr<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 #ifdef GTEST_HAS_DEATH_TEST
258 #ifndef NDEBUG
259 TEST(ConstantsTest, ReplaceWithConstantTest) {
260   std::unique_ptr<Module> M(new Module("MyModule", getGlobalContext()));
261
262   Type *Int32Ty = Type::getInt32Ty(getGlobalContext());
263   Constant *One = ConstantInt::get(Int32Ty, 1);
264
265   Constant *Global =
266       M->getOrInsertGlobal("dummy", PointerType::getUnqual(Int32Ty));
267   Constant *GEP = ConstantExpr::getGetElementPtr(Global, One);
268   EXPECT_DEATH(Global->replaceAllUsesWith(GEP),
269                "this->replaceAllUsesWith\\(expr\\(this\\)\\) is NOT valid!");
270 }
271
272 #endif
273 #endif
274
275 #undef CHECK
276
277 TEST(ConstantsTest, ConstantArrayReplaceWithConstant) {
278   LLVMContext Context;
279   std::unique_ptr<Module> M(new Module("MyModule", Context));
280
281   Type *IntTy = Type::getInt8Ty(Context);
282   ArrayType *ArrayTy = ArrayType::get(IntTy, 2);
283   Constant *A01Vals[2] = {ConstantInt::get(IntTy, 0),
284                           ConstantInt::get(IntTy, 1)};
285   Constant *A01 = ConstantArray::get(ArrayTy, A01Vals);
286
287   Constant *Global = new GlobalVariable(*M, IntTy, false,
288                                         GlobalValue::ExternalLinkage, nullptr);
289   Constant *GlobalInt = ConstantExpr::getPtrToInt(Global, IntTy);
290   Constant *A0GVals[2] = {ConstantInt::get(IntTy, 0), GlobalInt};
291   Constant *A0G = ConstantArray::get(ArrayTy, A0GVals);
292   ASSERT_NE(A01, A0G);
293
294   GlobalVariable *RefArray =
295       new GlobalVariable(*M, ArrayTy, false, GlobalValue::ExternalLinkage, A0G);
296   ASSERT_EQ(A0G, RefArray->getInitializer());
297
298   GlobalInt->replaceAllUsesWith(ConstantInt::get(IntTy, 1));
299   ASSERT_EQ(A01, RefArray->getInitializer());
300 }
301
302 TEST(ConstantsTest, ConstantExprReplaceWithConstant) {
303   LLVMContext Context;
304   std::unique_ptr<Module> M(new Module("MyModule", Context));
305
306   Type *IntTy = Type::getInt8Ty(Context);
307   Constant *G1 = new GlobalVariable(*M, IntTy, false,
308                                     GlobalValue::ExternalLinkage, nullptr);
309   Constant *G2 = new GlobalVariable(*M, IntTy, false,
310                                     GlobalValue::ExternalLinkage, nullptr);
311   ASSERT_NE(G1, G2);
312
313   Constant *Int1 = ConstantExpr::getPtrToInt(G1, IntTy);
314   Constant *Int2 = ConstantExpr::getPtrToInt(G2, IntTy);
315   ASSERT_NE(Int1, Int2);
316
317   GlobalVariable *Ref =
318       new GlobalVariable(*M, IntTy, false, GlobalValue::ExternalLinkage, Int1);
319   ASSERT_EQ(Int1, Ref->getInitializer());
320
321   G1->replaceAllUsesWith(G2);
322   ASSERT_EQ(Int2, Ref->getInitializer());
323 }
324
325 TEST(ConstantsTest, GEPReplaceWithConstant) {
326   LLVMContext Context;
327   std::unique_ptr<Module> M(new Module("MyModule", Context));
328
329   Type *IntTy = Type::getInt32Ty(Context);
330   Type *PtrTy = PointerType::get(IntTy, 0);
331   auto *C1 = ConstantInt::get(IntTy, 1);
332   auto *Placeholder = new GlobalVariable(
333       *M, IntTy, false, GlobalValue::ExternalWeakLinkage, nullptr);
334   auto *GEP = ConstantExpr::getGetElementPtr(Placeholder, C1);
335   ASSERT_EQ(GEP->getOperand(0), Placeholder);
336
337   auto *Ref =
338       new GlobalVariable(*M, PtrTy, false, GlobalValue::ExternalLinkage, GEP);
339   ASSERT_EQ(GEP, Ref->getInitializer());
340
341   auto *Global = new GlobalVariable(*M, PtrTy, false,
342                                     GlobalValue::ExternalLinkage, nullptr);
343   auto *Alias = GlobalAlias::create(IntTy, 0, GlobalValue::ExternalLinkage,
344                                     "alias", Global, M.get());
345   Placeholder->replaceAllUsesWith(Alias);
346   ASSERT_EQ(GEP, Ref->getInitializer());
347   ASSERT_EQ(GEP->getOperand(0), Alias);
348 }
349
350 }  // end anonymous namespace
351 }  // end namespace llvm