Gabor Greif | 1558038 | 2010-03-16 09:55:46 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/VMCore/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/Instructions.h" |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 11 | #include "llvm/BasicBlock.h" |
Jay Foad | 7c14a55 | 2011-04-11 09:35:34 +0000 | [diff] [blame] | 12 | #include "llvm/Constants.h" |
Gabor Greif | 86ca549 | 2010-03-16 11:24:53 +0000 | [diff] [blame] | 13 | #include "llvm/DerivedTypes.h" |
Gabor Greif | 1558038 | 2010-03-16 09:55:46 +0000 | [diff] [blame] | 14 | #include "llvm/LLVMContext.h" |
Gabor Greif | 6221720 | 2010-03-17 19:27:31 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/STLExtras.h" |
Gabor Greif | 1558038 | 2010-03-16 09:55:46 +0000 | [diff] [blame] | 16 | #include "gtest/gtest.h" |
| 17 | |
| 18 | namespace llvm { |
| 19 | namespace { |
| 20 | |
Gabor Greif | 35a9b8b | 2010-03-16 10:59:48 +0000 | [diff] [blame] | 21 | TEST(InstructionsTest, ReturnInst) { |
Gabor Greif | 1558038 | 2010-03-16 09:55:46 +0000 | [diff] [blame] | 22 | LLVMContext &C(getGlobalContext()); |
| 23 | |
Gabor Greif | 35a9b8b | 2010-03-16 10:59:48 +0000 | [diff] [blame] | 24 | // test for PR6589 |
Gabor Greif | 1558038 | 2010-03-16 09:55:46 +0000 | [diff] [blame] | 25 | const ReturnInst* r0 = ReturnInst::Create(C); |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 26 | EXPECT_EQ(r0->getNumOperands(), 0U); |
Gabor Greif | 35a9b8b | 2010-03-16 10:59:48 +0000 | [diff] [blame] | 27 | EXPECT_EQ(r0->op_begin(), r0->op_end()); |
Gabor Greif | 86ca549 | 2010-03-16 11:24:53 +0000 | [diff] [blame] | 28 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 29 | IntegerType* Int1 = IntegerType::get(C, 1); |
Gabor Greif | 86ca549 | 2010-03-16 11:24:53 +0000 | [diff] [blame] | 30 | Constant* One = ConstantInt::get(Int1, 1, true); |
| 31 | const ReturnInst* r1 = ReturnInst::Create(C, One); |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame^] | 32 | EXPECT_EQ(1U, r1->getNumOperands()); |
Gabor Greif | 86ca549 | 2010-03-16 11:24:53 +0000 | [diff] [blame] | 33 | User::const_op_iterator b(r1->op_begin()); |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame^] | 34 | EXPECT_NE(r1->op_end(), b); |
| 35 | EXPECT_EQ(One, *b); |
| 36 | EXPECT_EQ(One, r1->getOperand(0)); |
Gabor Greif | 86ca549 | 2010-03-16 11:24:53 +0000 | [diff] [blame] | 37 | ++b; |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame^] | 38 | EXPECT_EQ(r1->op_end(), b); |
Gabor Greif | 421dd12 | 2010-03-16 12:32:03 +0000 | [diff] [blame] | 39 | |
| 40 | // clean up |
| 41 | delete r0; |
| 42 | delete r1; |
Gabor Greif | 1558038 | 2010-03-16 09:55:46 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 45 | TEST(InstructionsTest, BranchInst) { |
| 46 | LLVMContext &C(getGlobalContext()); |
| 47 | |
| 48 | // Make a BasicBlocks |
| 49 | BasicBlock* bb0 = BasicBlock::Create(C); |
| 50 | BasicBlock* bb1 = BasicBlock::Create(C); |
| 51 | |
| 52 | // Mandatory BranchInst |
| 53 | const BranchInst* b0 = BranchInst::Create(bb0); |
| 54 | |
Gabor Greif | e52f398 | 2010-03-16 15:53:58 +0000 | [diff] [blame] | 55 | EXPECT_TRUE(b0->isUnconditional()); |
| 56 | EXPECT_FALSE(b0->isConditional()); |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame^] | 57 | EXPECT_EQ(1U, b0->getNumSuccessors()); |
Gabor Greif | e52f398 | 2010-03-16 15:53:58 +0000 | [diff] [blame] | 58 | |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 59 | // check num operands |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame^] | 60 | EXPECT_EQ(1U, b0->getNumOperands()); |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 61 | |
| 62 | EXPECT_NE(b0->op_begin(), b0->op_end()); |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame^] | 63 | EXPECT_EQ(b0->op_end(), llvm::next(b0->op_begin())); |
Gabor Greif | e52f398 | 2010-03-16 15:53:58 +0000 | [diff] [blame] | 64 | |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame^] | 65 | EXPECT_EQ(b0->op_end(), llvm::next(b0->op_begin())); |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 66 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 67 | IntegerType* Int1 = IntegerType::get(C, 1); |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 68 | Constant* One = ConstantInt::get(Int1, 1, true); |
| 69 | |
| 70 | // Conditional BranchInst |
| 71 | BranchInst* b1 = BranchInst::Create(bb0, bb1, One); |
| 72 | |
Gabor Greif | e52f398 | 2010-03-16 15:53:58 +0000 | [diff] [blame] | 73 | EXPECT_FALSE(b1->isUnconditional()); |
| 74 | EXPECT_TRUE(b1->isConditional()); |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame^] | 75 | EXPECT_EQ(2U, b1->getNumSuccessors()); |
Gabor Greif | e52f398 | 2010-03-16 15:53:58 +0000 | [diff] [blame] | 76 | |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 77 | // check num operands |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame^] | 78 | EXPECT_EQ(3U, b1->getNumOperands()); |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 79 | |
| 80 | User::const_op_iterator b(b1->op_begin()); |
| 81 | |
| 82 | // check COND |
| 83 | EXPECT_NE(b, b1->op_end()); |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame^] | 84 | EXPECT_EQ(One, *b); |
| 85 | EXPECT_EQ(One, b1->getOperand(0)); |
| 86 | EXPECT_EQ(One, b1->getCondition()); |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 87 | ++b; |
| 88 | |
| 89 | // check ELSE |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame^] | 90 | EXPECT_EQ(bb1, *b); |
| 91 | EXPECT_EQ(bb1, b1->getOperand(1)); |
| 92 | EXPECT_EQ(bb1, b1->getSuccessor(1)); |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 93 | ++b; |
| 94 | |
| 95 | // check THEN |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame^] | 96 | EXPECT_EQ(bb0, *b); |
| 97 | EXPECT_EQ(bb0, b1->getOperand(2)); |
| 98 | EXPECT_EQ(bb0, b1->getSuccessor(0)); |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 99 | ++b; |
| 100 | |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame^] | 101 | EXPECT_EQ(b1->op_end(), b); |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 102 | |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 103 | // clean up |
| 104 | delete b0; |
| 105 | delete b1; |
| 106 | |
| 107 | delete bb0; |
| 108 | delete bb1; |
| 109 | } |
| 110 | |
Duncan Sands | 2d3cdd6 | 2011-04-01 03:34:54 +0000 | [diff] [blame] | 111 | TEST(InstructionsTest, CastInst) { |
| 112 | LLVMContext &C(getGlobalContext()); |
| 113 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 114 | Type* Int8Ty = Type::getInt8Ty(C); |
| 115 | Type* Int64Ty = Type::getInt64Ty(C); |
| 116 | Type* V8x8Ty = VectorType::get(Int8Ty, 8); |
| 117 | Type* V8x64Ty = VectorType::get(Int64Ty, 8); |
| 118 | Type* X86MMXTy = Type::getX86_MMXTy(C); |
Duncan Sands | 2d3cdd6 | 2011-04-01 03:34:54 +0000 | [diff] [blame] | 119 | |
Duncan Sands | a851453 | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 120 | const Constant* c8 = Constant::getNullValue(V8x8Ty); |
| 121 | const Constant* c64 = Constant::getNullValue(V8x64Ty); |
| 122 | |
Duncan Sands | 2d3cdd6 | 2011-04-01 03:34:54 +0000 | [diff] [blame] | 123 | EXPECT_TRUE(CastInst::isCastable(V8x8Ty, X86MMXTy)); |
| 124 | EXPECT_TRUE(CastInst::isCastable(X86MMXTy, V8x8Ty)); |
| 125 | EXPECT_FALSE(CastInst::isCastable(Int64Ty, X86MMXTy)); |
Duncan Sands | a851453 | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 126 | EXPECT_TRUE(CastInst::isCastable(V8x64Ty, V8x8Ty)); |
| 127 | EXPECT_TRUE(CastInst::isCastable(V8x8Ty, V8x64Ty)); |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame^] | 128 | EXPECT_EQ(CastInst::Trunc, CastInst::getCastOpcode(c64, true, V8x8Ty, true)); |
| 129 | EXPECT_EQ(CastInst::SExt, CastInst::getCastOpcode(c8, true, V8x64Ty, true)); |
Duncan Sands | 2d3cdd6 | 2011-04-01 03:34:54 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Gabor Greif | 1558038 | 2010-03-16 09:55:46 +0000 | [diff] [blame] | 132 | } // end anonymous namespace |
| 133 | } // end namespace llvm |