Fix bot breakage in InstructionsTest.
[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/Function.h"
18 #include "llvm/IR/IRBuilder.h"
19 #include "llvm/IR/LLVMContext.h"
20 #include "llvm/IR/MDBuilder.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/IR/Operator.h"
23 #include "gtest/gtest.h"
24 #include <memory>
25
26 namespace llvm {
27 namespace {
28
29 TEST(InstructionsTest, ReturnInst) {
30   LLVMContext &C(getGlobalContext());
31
32   // test for PR6589
33   const ReturnInst* r0 = ReturnInst::Create(C);
34   EXPECT_EQ(r0->getNumOperands(), 0U);
35   EXPECT_EQ(r0->op_begin(), r0->op_end());
36
37   IntegerType* Int1 = IntegerType::get(C, 1);
38   Constant* One = ConstantInt::get(Int1, 1, true);
39   const ReturnInst* r1 = ReturnInst::Create(C, One);
40   EXPECT_EQ(1U, r1->getNumOperands());
41   User::const_op_iterator b(r1->op_begin());
42   EXPECT_NE(r1->op_end(), b);
43   EXPECT_EQ(One, *b);
44   EXPECT_EQ(One, r1->getOperand(0));
45   ++b;
46   EXPECT_EQ(r1->op_end(), b);
47
48   // clean up
49   delete r0;
50   delete r1;
51 }
52
53 TEST(InstructionsTest, CallInst) {
54   LLVMContext &C(getGlobalContext());
55   std::unique_ptr<Module> M(new Module("MyModule", C));
56
57   Type *ArgTypes[] = {Type::getInt8Ty(C), Type::getInt32Ty(C),
58                       Type::getInt64Ty(C)};
59   FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), ArgTypes, false);
60   Function *F = Function::Create(FTy, Function::ExternalLinkage, "", M.get());
61
62   Value *Args[] = {ConstantInt::get(Type::getInt8Ty(C), 20),
63                    ConstantInt::get(Type::getInt32Ty(C), 9999),
64                    ConstantInt::get(Type::getInt64Ty(C), 42)};
65   std::unique_ptr<CallInst> Call(CallInst::Create(F, Args));
66
67   // Make sure iteration over a call's arguments works as expected.
68   unsigned Idx = 0;
69   for (Value *Arg : Call->arg_operands()) {
70     EXPECT_EQ(ArgTypes[Idx], Arg->getType());
71     EXPECT_EQ(Call->getArgOperand(Idx)->getType(), Arg->getType());
72     Idx++;
73   }
74 }
75
76 TEST(InstructionsTest, BranchInst) {
77   LLVMContext &C(getGlobalContext());
78
79   // Make a BasicBlocks
80   BasicBlock* bb0 = BasicBlock::Create(C);
81   BasicBlock* bb1 = BasicBlock::Create(C);
82
83   // Mandatory BranchInst
84   const BranchInst* b0 = BranchInst::Create(bb0);
85
86   EXPECT_TRUE(b0->isUnconditional());
87   EXPECT_FALSE(b0->isConditional());
88   EXPECT_EQ(1U, b0->getNumSuccessors());
89
90   // check num operands
91   EXPECT_EQ(1U, b0->getNumOperands());
92
93   EXPECT_NE(b0->op_begin(), b0->op_end());
94   EXPECT_EQ(b0->op_end(), std::next(b0->op_begin()));
95
96   EXPECT_EQ(b0->op_end(), std::next(b0->op_begin()));
97
98   IntegerType* Int1 = IntegerType::get(C, 1);
99   Constant* One = ConstantInt::get(Int1, 1, true);
100
101   // Conditional BranchInst
102   BranchInst* b1 = BranchInst::Create(bb0, bb1, One);
103
104   EXPECT_FALSE(b1->isUnconditional());
105   EXPECT_TRUE(b1->isConditional());
106   EXPECT_EQ(2U, b1->getNumSuccessors());
107
108   // check num operands
109   EXPECT_EQ(3U, b1->getNumOperands());
110
111   User::const_op_iterator b(b1->op_begin());
112
113   // check COND
114   EXPECT_NE(b, b1->op_end());
115   EXPECT_EQ(One, *b);
116   EXPECT_EQ(One, b1->getOperand(0));
117   EXPECT_EQ(One, b1->getCondition());
118   ++b;
119
120   // check ELSE
121   EXPECT_EQ(bb1, *b);
122   EXPECT_EQ(bb1, b1->getOperand(1));
123   EXPECT_EQ(bb1, b1->getSuccessor(1));
124   ++b;
125
126   // check THEN
127   EXPECT_EQ(bb0, *b);
128   EXPECT_EQ(bb0, b1->getOperand(2));
129   EXPECT_EQ(bb0, b1->getSuccessor(0));
130   ++b;
131
132   EXPECT_EQ(b1->op_end(), b);
133
134   // clean up
135   delete b0;
136   delete b1;
137
138   delete bb0;
139   delete bb1;
140 }
141
142 TEST(InstructionsTest, CastInst) {
143   LLVMContext &C(getGlobalContext());
144
145   Type *Int8Ty = Type::getInt8Ty(C);
146   Type *Int16Ty = Type::getInt16Ty(C);
147   Type *Int32Ty = Type::getInt32Ty(C);
148   Type *Int64Ty = Type::getInt64Ty(C);
149   Type *V8x8Ty = VectorType::get(Int8Ty, 8);
150   Type *V8x64Ty = VectorType::get(Int64Ty, 8);
151   Type *X86MMXTy = Type::getX86_MMXTy(C);
152
153   Type *HalfTy = Type::getHalfTy(C);
154   Type *FloatTy = Type::getFloatTy(C);
155   Type *DoubleTy = Type::getDoubleTy(C);
156
157   Type *V2Int32Ty = VectorType::get(Int32Ty, 2);
158   Type *V2Int64Ty = VectorType::get(Int64Ty, 2);
159   Type *V4Int16Ty = VectorType::get(Int16Ty, 4);
160
161   Type *Int32PtrTy = PointerType::get(Int32Ty, 0);
162   Type *Int64PtrTy = PointerType::get(Int64Ty, 0);
163
164   Type *Int32PtrAS1Ty = PointerType::get(Int32Ty, 1);
165   Type *Int64PtrAS1Ty = PointerType::get(Int64Ty, 1);
166
167   Type *V2Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 2);
168   Type *V2Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 2);
169   Type *V4Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 4);
170   Type *V4Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 4);
171
172   Type *V2Int64PtrTy = VectorType::get(Int64PtrTy, 2);
173   Type *V2Int32PtrTy = VectorType::get(Int32PtrTy, 2);
174   Type *V4Int32PtrTy = VectorType::get(Int32PtrTy, 4);
175
176   const Constant* c8 = Constant::getNullValue(V8x8Ty);
177   const Constant* c64 = Constant::getNullValue(V8x64Ty);
178
179   const Constant *v2ptr32 = Constant::getNullValue(V2Int32PtrTy);
180
181   EXPECT_TRUE(CastInst::isCastable(V8x8Ty, X86MMXTy));
182   EXPECT_TRUE(CastInst::isCastable(X86MMXTy, V8x8Ty));
183   EXPECT_FALSE(CastInst::isCastable(Int64Ty, X86MMXTy));
184   EXPECT_TRUE(CastInst::isCastable(V8x64Ty, V8x8Ty));
185   EXPECT_TRUE(CastInst::isCastable(V8x8Ty, V8x64Ty));
186   EXPECT_EQ(CastInst::Trunc, CastInst::getCastOpcode(c64, true, V8x8Ty, true));
187   EXPECT_EQ(CastInst::SExt, CastInst::getCastOpcode(c8, true, V8x64Ty, true));
188
189   EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, X86MMXTy));
190   EXPECT_FALSE(CastInst::isBitCastable(X86MMXTy, V8x8Ty));
191   EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, X86MMXTy));
192   EXPECT_FALSE(CastInst::isBitCastable(V8x64Ty, V8x8Ty));
193   EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, V8x64Ty));
194
195   // Check address space casts are rejected since we don't know the sizes here
196   EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, Int32PtrAS1Ty));
197   EXPECT_FALSE(CastInst::isBitCastable(Int32PtrAS1Ty, Int32PtrTy));
198   EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, V2Int32PtrAS1Ty));
199   EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int32PtrTy));
200   EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int64PtrAS1Ty));
201   EXPECT_TRUE(CastInst::isCastable(V2Int32PtrAS1Ty, V2Int32PtrTy));
202   EXPECT_EQ(CastInst::AddrSpaceCast, CastInst::getCastOpcode(v2ptr32, true,
203                                                              V2Int32PtrAS1Ty,
204                                                              true));
205
206   // Test mismatched number of elements for pointers
207   EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int64PtrAS1Ty));
208   EXPECT_FALSE(CastInst::isBitCastable(V4Int64PtrAS1Ty, V2Int32PtrAS1Ty));
209   EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int32PtrAS1Ty));
210   EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, V2Int32PtrTy));
211   EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int32PtrTy));
212
213   EXPECT_TRUE(CastInst::isBitCastable(Int32PtrTy, Int64PtrTy));
214   EXPECT_FALSE(CastInst::isBitCastable(DoubleTy, FloatTy));
215   EXPECT_FALSE(CastInst::isBitCastable(FloatTy, DoubleTy));
216   EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy));
217   EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy));
218   EXPECT_TRUE(CastInst::isBitCastable(FloatTy, Int32Ty));
219   EXPECT_TRUE(CastInst::isBitCastable(Int16Ty, HalfTy));
220   EXPECT_TRUE(CastInst::isBitCastable(Int32Ty, FloatTy));
221   EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, Int64Ty));
222
223   EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, V4Int16Ty));
224   EXPECT_FALSE(CastInst::isBitCastable(Int32Ty, Int64Ty));
225   EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, Int32Ty));
226
227   EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int64Ty));
228   EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, V2Int32PtrTy));
229   EXPECT_TRUE(CastInst::isBitCastable(V2Int64PtrTy, V2Int32PtrTy));
230   EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrTy, V2Int64PtrTy));
231   EXPECT_FALSE(CastInst::isBitCastable(V2Int32Ty, V2Int64Ty));
232   EXPECT_FALSE(CastInst::isBitCastable(V2Int64Ty, V2Int32Ty));
233
234
235   EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
236                                      Constant::getNullValue(V4Int32PtrTy),
237                                      V2Int32PtrTy));
238   EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
239                                      Constant::getNullValue(V2Int32PtrTy),
240                                      V4Int32PtrTy));
241
242   EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast,
243                                      Constant::getNullValue(V4Int32PtrAS1Ty),
244                                      V2Int32PtrTy));
245   EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast,
246                                      Constant::getNullValue(V2Int32PtrTy),
247                                      V4Int32PtrAS1Ty));
248
249
250   // Check that assertion is not hit when creating a cast with a vector of
251   // pointers
252   // First form
253   BasicBlock *BB = BasicBlock::Create(C);
254   Constant *NullV2I32Ptr = Constant::getNullValue(V2Int32PtrTy);
255   CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty, "foo", BB);
256
257   // Second form
258   CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty);
259 }
260
261 TEST(InstructionsTest, VectorGep) {
262   LLVMContext &C(getGlobalContext());
263
264   // Type Definitions
265   PointerType *Ptri8Ty = PointerType::get(IntegerType::get(C, 8), 0);
266   PointerType *Ptri32Ty = PointerType::get(IntegerType::get(C, 32), 0);
267
268   VectorType *V2xi8PTy = VectorType::get(Ptri8Ty, 2);
269   VectorType *V2xi32PTy = VectorType::get(Ptri32Ty, 2);
270
271   // Test different aspects of the vector-of-pointers type
272   // and GEPs which use this type.
273   ConstantInt *Ci32a = ConstantInt::get(C, APInt(32, 1492));
274   ConstantInt *Ci32b = ConstantInt::get(C, APInt(32, 1948));
275   std::vector<Constant*> ConstVa(2, Ci32a);
276   std::vector<Constant*> ConstVb(2, Ci32b);
277   Constant *C2xi32a = ConstantVector::get(ConstVa);
278   Constant *C2xi32b = ConstantVector::get(ConstVb);
279
280   CastInst *PtrVecA = new IntToPtrInst(C2xi32a, V2xi32PTy);
281   CastInst *PtrVecB = new IntToPtrInst(C2xi32b, V2xi32PTy);
282
283   ICmpInst *ICmp0 = new ICmpInst(ICmpInst::ICMP_SGT, PtrVecA, PtrVecB);
284   ICmpInst *ICmp1 = new ICmpInst(ICmpInst::ICMP_ULT, PtrVecA, PtrVecB);
285   EXPECT_NE(ICmp0, ICmp1); // suppress warning.
286
287   BasicBlock* BB0 = BasicBlock::Create(C);
288   // Test InsertAtEnd ICmpInst constructor.
289   ICmpInst *ICmp2 = new ICmpInst(*BB0, ICmpInst::ICMP_SGE, PtrVecA, PtrVecB);
290   EXPECT_NE(ICmp0, ICmp2); // suppress warning.
291
292   GetElementPtrInst *Gep0 = GetElementPtrInst::Create(PtrVecA, C2xi32a);
293   GetElementPtrInst *Gep1 = GetElementPtrInst::Create(PtrVecA, C2xi32b);
294   GetElementPtrInst *Gep2 = GetElementPtrInst::Create(PtrVecB, C2xi32a);
295   GetElementPtrInst *Gep3 = GetElementPtrInst::Create(PtrVecB, C2xi32b);
296
297   CastInst *BTC0 = new BitCastInst(Gep0, V2xi8PTy);
298   CastInst *BTC1 = new BitCastInst(Gep1, V2xi8PTy);
299   CastInst *BTC2 = new BitCastInst(Gep2, V2xi8PTy);
300   CastInst *BTC3 = new BitCastInst(Gep3, V2xi8PTy);
301
302   Value *S0 = BTC0->stripPointerCasts();
303   Value *S1 = BTC1->stripPointerCasts();
304   Value *S2 = BTC2->stripPointerCasts();
305   Value *S3 = BTC3->stripPointerCasts();
306
307   EXPECT_NE(S0, Gep0);
308   EXPECT_NE(S1, Gep1);
309   EXPECT_NE(S2, Gep2);
310   EXPECT_NE(S3, Gep3);
311
312   int64_t Offset;
313   DataLayout TD("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f3"
314                 "2:32:32-f64:64:64-v64:64:64-v128:128:128-a:0:64-s:64:64-f80"
315                 ":128:128-n8:16:32:64-S128");
316   // Make sure we don't crash
317   GetPointerBaseWithConstantOffset(Gep0, Offset, &TD);
318   GetPointerBaseWithConstantOffset(Gep1, Offset, &TD);
319   GetPointerBaseWithConstantOffset(Gep2, Offset, &TD);
320   GetPointerBaseWithConstantOffset(Gep3, Offset, &TD);
321
322   // Gep of Geps
323   GetElementPtrInst *GepII0 = GetElementPtrInst::Create(Gep0, C2xi32b);
324   GetElementPtrInst *GepII1 = GetElementPtrInst::Create(Gep1, C2xi32a);
325   GetElementPtrInst *GepII2 = GetElementPtrInst::Create(Gep2, C2xi32b);
326   GetElementPtrInst *GepII3 = GetElementPtrInst::Create(Gep3, C2xi32a);
327
328   EXPECT_EQ(GepII0->getNumIndices(), 1u);
329   EXPECT_EQ(GepII1->getNumIndices(), 1u);
330   EXPECT_EQ(GepII2->getNumIndices(), 1u);
331   EXPECT_EQ(GepII3->getNumIndices(), 1u);
332
333   EXPECT_FALSE(GepII0->hasAllZeroIndices());
334   EXPECT_FALSE(GepII1->hasAllZeroIndices());
335   EXPECT_FALSE(GepII2->hasAllZeroIndices());
336   EXPECT_FALSE(GepII3->hasAllZeroIndices());
337
338   delete GepII0;
339   delete GepII1;
340   delete GepII2;
341   delete GepII3;
342
343   delete BTC0;
344   delete BTC1;
345   delete BTC2;
346   delete BTC3;
347
348   delete Gep0;
349   delete Gep1;
350   delete Gep2;
351   delete Gep3;
352
353   ICmp2->eraseFromParent();
354   delete BB0;
355
356   delete ICmp0;
357   delete ICmp1;
358   delete PtrVecA;
359   delete PtrVecB;
360 }
361
362 TEST(InstructionsTest, FPMathOperator) {
363   LLVMContext &Context = getGlobalContext();
364   IRBuilder<> Builder(Context);
365   MDBuilder MDHelper(Context);
366   Instruction *I = Builder.CreatePHI(Builder.getDoubleTy(), 0);
367   MDNode *MD1 = MDHelper.createFPMath(1.0);
368   Value *V1 = Builder.CreateFAdd(I, I, "", MD1);
369   EXPECT_TRUE(isa<FPMathOperator>(V1));
370   FPMathOperator *O1 = cast<FPMathOperator>(V1);
371   EXPECT_EQ(O1->getFPAccuracy(), 1.0);
372   delete V1;
373   delete I;
374 }
375
376
377 TEST(InstructionsTest, isEliminableCastPair) {
378   LLVMContext &C(getGlobalContext());
379
380   Type* Int16Ty = Type::getInt16Ty(C);
381   Type* Int32Ty = Type::getInt32Ty(C);
382   Type* Int64Ty = Type::getInt64Ty(C);
383   Type* Int64PtrTy = Type::getInt64PtrTy(C);
384
385   // Source and destination pointers have same size -> bitcast.
386   EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
387                                            CastInst::IntToPtr,
388                                            Int64PtrTy, Int64Ty, Int64PtrTy,
389                                            Int32Ty, 0, Int32Ty),
390             CastInst::BitCast);
391
392   // Source and destination have unknown sizes, but the same address space and
393   // the intermediate int is the maximum pointer size -> bitcast
394   EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
395                                            CastInst::IntToPtr,
396                                            Int64PtrTy, Int64Ty, Int64PtrTy,
397                                            0, 0, 0),
398             CastInst::BitCast);
399
400   // Source and destination have unknown sizes, but the same address space and
401   // the intermediate int is not the maximum pointer size -> nothing
402   EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
403                                            CastInst::IntToPtr,
404                                            Int64PtrTy, Int32Ty, Int64PtrTy,
405                                            0, 0, 0),
406             0U);
407
408   // Middle pointer big enough -> bitcast.
409   EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
410                                            CastInst::PtrToInt,
411                                            Int64Ty, Int64PtrTy, Int64Ty,
412                                            0, Int64Ty, 0),
413             CastInst::BitCast);
414
415   // Middle pointer too small -> fail.
416   EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
417                                            CastInst::PtrToInt,
418                                            Int64Ty, Int64PtrTy, Int64Ty,
419                                            0, Int32Ty, 0),
420             0U);
421
422   // Test that we don't eliminate bitcasts between different address spaces,
423   // or if we don't have available pointer size information.
424   DataLayout DL("e-p:32:32:32-p1:16:16:16-p2:64:64:64-i1:8:8-i8:8:8-i16:16:16"
425                 "-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64"
426                 "-v128:128:128-a:0:64-s:64:64-f80:128:128-n8:16:32:64-S128");
427
428   Type* Int64PtrTyAS1 = Type::getInt64PtrTy(C, 1);
429   Type* Int64PtrTyAS2 = Type::getInt64PtrTy(C, 2);
430
431   IntegerType *Int16SizePtr = DL.getIntPtrType(C, 1);
432   IntegerType *Int64SizePtr = DL.getIntPtrType(C, 2);
433
434   // Cannot simplify inttoptr, addrspacecast
435   EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
436                                            CastInst::AddrSpaceCast,
437                                            Int16Ty, Int64PtrTyAS1, Int64PtrTyAS2,
438                                            0, Int16SizePtr, Int64SizePtr),
439             0U);
440
441   // Cannot simplify addrspacecast, ptrtoint
442   EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::AddrSpaceCast,
443                                            CastInst::PtrToInt,
444                                            Int64PtrTyAS1, Int64PtrTyAS2, Int16Ty,
445                                            Int64SizePtr, Int16SizePtr, 0),
446             0U);
447
448   // Pass since the bitcast address spaces are the same
449   EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
450                                            CastInst::BitCast,
451                                            Int16Ty, Int64PtrTyAS1, Int64PtrTyAS1,
452                                            0, 0, 0),
453             CastInst::IntToPtr);
454
455 }
456
457 }  // end anonymous namespace
458 }  // end namespace llvm
459
460