A test for r172535.
[oota-llvm.git] / unittests / IR / InstructionsTest.cpp
1 //===- llvm/unittest/IR/InstructionsTest.cpp - Instructions 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/Instructions.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/Analysis/ValueTracking.h"
13 #include "llvm/IR/BasicBlock.h"
14 #include "llvm/IR/Constants.h"
15 #include "llvm/IR/DataLayout.h"
16 #include "llvm/IR/DerivedTypes.h"
17 #include "llvm/IR/IRBuilder.h"
18 #include "llvm/IR/LLVMContext.h"
19 #include "llvm/IR/MDBuilder.h"
20 #include "llvm/IR/Operator.h"
21 #include "gtest/gtest.h"
22
23 namespace llvm {
24 namespace {
25
26 TEST(InstructionsTest, ReturnInst) {
27   LLVMContext &C(getGlobalContext());
28
29   // test for PR6589
30   const ReturnInst* r0 = ReturnInst::Create(C);
31   EXPECT_EQ(r0->getNumOperands(), 0U);
32   EXPECT_EQ(r0->op_begin(), r0->op_end());
33
34   IntegerType* Int1 = IntegerType::get(C, 1);
35   Constant* One = ConstantInt::get(Int1, 1, true);
36   const ReturnInst* r1 = ReturnInst::Create(C, One);
37   EXPECT_EQ(1U, r1->getNumOperands());
38   User::const_op_iterator b(r1->op_begin());
39   EXPECT_NE(r1->op_end(), b);
40   EXPECT_EQ(One, *b);
41   EXPECT_EQ(One, r1->getOperand(0));
42   ++b;
43   EXPECT_EQ(r1->op_end(), b);
44
45   // clean up
46   delete r0;
47   delete r1;
48 }
49
50 TEST(InstructionsTest, BranchInst) {
51   LLVMContext &C(getGlobalContext());
52
53   // Make a BasicBlocks
54   BasicBlock* bb0 = BasicBlock::Create(C);
55   BasicBlock* bb1 = BasicBlock::Create(C);
56
57   // Mandatory BranchInst
58   const BranchInst* b0 = BranchInst::Create(bb0);
59
60   EXPECT_TRUE(b0->isUnconditional());
61   EXPECT_FALSE(b0->isConditional());
62   EXPECT_EQ(1U, b0->getNumSuccessors());
63
64   // check num operands
65   EXPECT_EQ(1U, b0->getNumOperands());
66
67   EXPECT_NE(b0->op_begin(), b0->op_end());
68   EXPECT_EQ(b0->op_end(), llvm::next(b0->op_begin()));
69
70   EXPECT_EQ(b0->op_end(), llvm::next(b0->op_begin()));
71
72   IntegerType* Int1 = IntegerType::get(C, 1);
73   Constant* One = ConstantInt::get(Int1, 1, true);
74
75   // Conditional BranchInst
76   BranchInst* b1 = BranchInst::Create(bb0, bb1, One);
77
78   EXPECT_FALSE(b1->isUnconditional());
79   EXPECT_TRUE(b1->isConditional());
80   EXPECT_EQ(2U, b1->getNumSuccessors());
81
82   // check num operands
83   EXPECT_EQ(3U, b1->getNumOperands());
84
85   User::const_op_iterator b(b1->op_begin());
86
87   // check COND
88   EXPECT_NE(b, b1->op_end());
89   EXPECT_EQ(One, *b);
90   EXPECT_EQ(One, b1->getOperand(0));
91   EXPECT_EQ(One, b1->getCondition());
92   ++b;
93
94   // check ELSE
95   EXPECT_EQ(bb1, *b);
96   EXPECT_EQ(bb1, b1->getOperand(1));
97   EXPECT_EQ(bb1, b1->getSuccessor(1));
98   ++b;
99
100   // check THEN
101   EXPECT_EQ(bb0, *b);
102   EXPECT_EQ(bb0, b1->getOperand(2));
103   EXPECT_EQ(bb0, b1->getSuccessor(0));
104   ++b;
105
106   EXPECT_EQ(b1->op_end(), b);
107
108   // clean up
109   delete b0;
110   delete b1;
111
112   delete bb0;
113   delete bb1;
114 }
115
116 TEST(InstructionsTest, CastInst) {
117   LLVMContext &C(getGlobalContext());
118
119   Type* Int8Ty = Type::getInt8Ty(C);
120   Type* Int64Ty = Type::getInt64Ty(C);
121   Type* V8x8Ty = VectorType::get(Int8Ty, 8);
122   Type* V8x64Ty = VectorType::get(Int64Ty, 8);
123   Type* X86MMXTy = Type::getX86_MMXTy(C);
124
125   const Constant* c8 = Constant::getNullValue(V8x8Ty);
126   const Constant* c64 = Constant::getNullValue(V8x64Ty);
127
128   EXPECT_TRUE(CastInst::isCastable(V8x8Ty, X86MMXTy));
129   EXPECT_TRUE(CastInst::isCastable(X86MMXTy, V8x8Ty));
130   EXPECT_FALSE(CastInst::isCastable(Int64Ty, X86MMXTy));
131   EXPECT_TRUE(CastInst::isCastable(V8x64Ty, V8x8Ty));
132   EXPECT_TRUE(CastInst::isCastable(V8x8Ty, V8x64Ty));
133   EXPECT_EQ(CastInst::Trunc, CastInst::getCastOpcode(c64, true, V8x8Ty, true));
134   EXPECT_EQ(CastInst::SExt, CastInst::getCastOpcode(c8, true, V8x64Ty, true));
135 }
136
137
138
139 TEST(InstructionsTest, VectorGep) {
140   LLVMContext &C(getGlobalContext());
141
142   // Type Definitions
143   PointerType *Ptri8Ty = PointerType::get(IntegerType::get(C, 8), 0);
144   PointerType *Ptri32Ty = PointerType::get(IntegerType::get(C, 8), 0);
145
146   VectorType *V2xi8PTy = VectorType::get(Ptri8Ty, 2);
147   VectorType *V2xi32PTy = VectorType::get(Ptri32Ty, 2);
148
149   // Test different aspects of the vector-of-pointers type
150   // and GEPs which use this type.
151   ConstantInt *Ci32a = ConstantInt::get(C, APInt(32, 1492));
152   ConstantInt *Ci32b = ConstantInt::get(C, APInt(32, 1948));
153   std::vector<Constant*> ConstVa(2, Ci32a);
154   std::vector<Constant*> ConstVb(2, Ci32b);
155   Constant *C2xi32a = ConstantVector::get(ConstVa);
156   Constant *C2xi32b = ConstantVector::get(ConstVb);
157
158   CastInst *PtrVecA = new IntToPtrInst(C2xi32a, V2xi32PTy);
159   CastInst *PtrVecB = new IntToPtrInst(C2xi32b, V2xi32PTy);
160
161   ICmpInst *ICmp0 = new ICmpInst(ICmpInst::ICMP_SGT, PtrVecA, PtrVecB);
162   ICmpInst *ICmp1 = new ICmpInst(ICmpInst::ICMP_ULT, PtrVecA, PtrVecB);
163   EXPECT_NE(ICmp0, ICmp1); // suppress warning.
164
165   BasicBlock* BB0 = BasicBlock::Create(C);
166   // Test InsertAtEnd ICmpInst constructor.
167   ICmpInst *ICmp2 = new ICmpInst(*BB0, ICmpInst::ICMP_SGE, PtrVecA, PtrVecB);
168   EXPECT_NE(ICmp0, ICmp2); // suppress warning.
169
170   GetElementPtrInst *Gep0 = GetElementPtrInst::Create(PtrVecA, C2xi32a);
171   GetElementPtrInst *Gep1 = GetElementPtrInst::Create(PtrVecA, C2xi32b);
172   GetElementPtrInst *Gep2 = GetElementPtrInst::Create(PtrVecB, C2xi32a);
173   GetElementPtrInst *Gep3 = GetElementPtrInst::Create(PtrVecB, C2xi32b);
174
175   CastInst *BTC0 = new BitCastInst(Gep0, V2xi8PTy);
176   CastInst *BTC1 = new BitCastInst(Gep1, V2xi8PTy);
177   CastInst *BTC2 = new BitCastInst(Gep2, V2xi8PTy);
178   CastInst *BTC3 = new BitCastInst(Gep3, V2xi8PTy);
179
180   Value *S0 = BTC0->stripPointerCasts();
181   Value *S1 = BTC1->stripPointerCasts();
182   Value *S2 = BTC2->stripPointerCasts();
183   Value *S3 = BTC3->stripPointerCasts();
184
185   EXPECT_NE(S0, Gep0);
186   EXPECT_NE(S1, Gep1);
187   EXPECT_NE(S2, Gep2);
188   EXPECT_NE(S3, Gep3);
189
190   int64_t Offset;
191   DataLayout TD("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f3"
192                 "2:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80"
193                 ":128:128-n8:16:32:64-S128");
194   // Make sure we don't crash
195   GetPointerBaseWithConstantOffset(Gep0, Offset, TD);
196   GetPointerBaseWithConstantOffset(Gep1, Offset, TD);
197   GetPointerBaseWithConstantOffset(Gep2, Offset, TD);
198   GetPointerBaseWithConstantOffset(Gep3, Offset, TD);
199
200   // Gep of Geps
201   GetElementPtrInst *GepII0 = GetElementPtrInst::Create(Gep0, C2xi32b);
202   GetElementPtrInst *GepII1 = GetElementPtrInst::Create(Gep1, C2xi32a);
203   GetElementPtrInst *GepII2 = GetElementPtrInst::Create(Gep2, C2xi32b);
204   GetElementPtrInst *GepII3 = GetElementPtrInst::Create(Gep3, C2xi32a);
205
206   EXPECT_EQ(GepII0->getNumIndices(), 1u);
207   EXPECT_EQ(GepII1->getNumIndices(), 1u);
208   EXPECT_EQ(GepII2->getNumIndices(), 1u);
209   EXPECT_EQ(GepII3->getNumIndices(), 1u);
210
211   EXPECT_FALSE(GepII0->hasAllZeroIndices());
212   EXPECT_FALSE(GepII1->hasAllZeroIndices());
213   EXPECT_FALSE(GepII2->hasAllZeroIndices());
214   EXPECT_FALSE(GepII3->hasAllZeroIndices());
215
216   delete GepII0;
217   delete GepII1;
218   delete GepII2;
219   delete GepII3;
220
221   delete BTC0;
222   delete BTC1;
223   delete BTC2;
224   delete BTC3;
225
226   delete Gep0;
227   delete Gep1;
228   delete Gep2;
229   delete Gep3;
230
231   ICmp2->eraseFromParent();
232   delete BB0;
233
234   delete ICmp0;
235   delete ICmp1;
236   delete PtrVecA;
237   delete PtrVecB;
238 }
239
240 TEST(InstructionsTest, FPMathOperator) {
241   LLVMContext &Context = getGlobalContext();
242   IRBuilder<> Builder(Context);
243   MDBuilder MDHelper(Context);
244   Instruction *I = Builder.CreatePHI(Builder.getDoubleTy(), 0);
245   MDNode *MD1 = MDHelper.createFPMath(1.0);
246   Value *V1 = Builder.CreateFAdd(I, I, "", MD1);
247   EXPECT_TRUE(isa<FPMathOperator>(V1));
248   FPMathOperator *O1 = cast<FPMathOperator>(V1);
249   EXPECT_EQ(O1->getFPAccuracy(), 1.0);
250   delete V1;
251   delete I;
252 }
253
254
255 TEST(InstructionsTest, isEliminableCastPair) {
256   LLVMContext &C(getGlobalContext());
257
258   Type* Int32Ty = Type::getInt32Ty(C);
259   Type* Int64Ty = Type::getInt64Ty(C);
260   Type* Int64PtrTy = Type::getInt64PtrTy(C);
261
262   // Source and destination pointers have same size -> bitcast.
263   EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
264                                            CastInst::IntToPtr,
265                                            Int64PtrTy, Int64Ty, Int64PtrTy,
266                                            Int32Ty, 0, Int32Ty),
267             CastInst::BitCast);
268
269   // Source and destination pointers have different sizes -> fail.
270   EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
271                                            CastInst::IntToPtr,
272                                            Int64PtrTy, Int64Ty, Int64PtrTy,
273                                            Int32Ty, 0, Int64Ty),
274             0U);
275
276   // Middle pointer big enough -> bitcast.
277   EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
278                                            CastInst::PtrToInt,
279                                            Int64Ty, Int64PtrTy, Int64Ty,
280                                            0, Int64Ty, 0),
281             CastInst::BitCast);
282
283   // Middle pointer too small -> fail.
284   EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
285                                            CastInst::PtrToInt,
286                                            Int64Ty, Int64PtrTy, Int64Ty,
287                                            0, Int32Ty, 0),
288             0U);
289 }
290
291 }  // end anonymous namespace
292 }  // end namespace llvm