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" |
Gabor Greif | 86ca549 | 2010-03-16 11:24:53 +0000 | [diff] [blame] | 12 | #include "llvm/DerivedTypes.h" |
Gabor Greif | 1558038 | 2010-03-16 09:55:46 +0000 | [diff] [blame] | 13 | #include "llvm/LLVMContext.h" |
| 14 | #include "gtest/gtest.h" |
| 15 | |
| 16 | namespace llvm { |
| 17 | namespace { |
| 18 | |
Gabor Greif | 35a9b8b | 2010-03-16 10:59:48 +0000 | [diff] [blame] | 19 | TEST(InstructionsTest, ReturnInst) { |
Gabor Greif | 1558038 | 2010-03-16 09:55:46 +0000 | [diff] [blame] | 20 | LLVMContext &C(getGlobalContext()); |
| 21 | |
Gabor Greif | 35a9b8b | 2010-03-16 10:59:48 +0000 | [diff] [blame] | 22 | // test for PR6589 |
Gabor Greif | 1558038 | 2010-03-16 09:55:46 +0000 | [diff] [blame] | 23 | const ReturnInst* r0 = ReturnInst::Create(C); |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 24 | EXPECT_EQ(r0->getNumOperands(), 0U); |
Gabor Greif | 35a9b8b | 2010-03-16 10:59:48 +0000 | [diff] [blame] | 25 | EXPECT_EQ(r0->op_begin(), r0->op_end()); |
Gabor Greif | 86ca549 | 2010-03-16 11:24:53 +0000 | [diff] [blame] | 26 | |
| 27 | const IntegerType* Int1 = IntegerType::get(C, 1); |
| 28 | Constant* One = ConstantInt::get(Int1, 1, true); |
| 29 | const ReturnInst* r1 = ReturnInst::Create(C, One); |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 30 | EXPECT_EQ(r1->getNumOperands(), 1U); |
Gabor Greif | 86ca549 | 2010-03-16 11:24:53 +0000 | [diff] [blame] | 31 | User::const_op_iterator b(r1->op_begin()); |
| 32 | EXPECT_NE(b, r1->op_end()); |
| 33 | EXPECT_EQ(*b, One); |
| 34 | EXPECT_EQ(r1->getOperand(0), One); |
| 35 | ++b; |
| 36 | EXPECT_EQ(b, r1->op_end()); |
Gabor Greif | 421dd12 | 2010-03-16 12:32:03 +0000 | [diff] [blame] | 37 | |
| 38 | // clean up |
| 39 | delete r0; |
| 40 | delete r1; |
Gabor Greif | 1558038 | 2010-03-16 09:55:46 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 43 | TEST(InstructionsTest, BranchInst) { |
| 44 | LLVMContext &C(getGlobalContext()); |
| 45 | |
| 46 | // Make a BasicBlocks |
| 47 | BasicBlock* bb0 = BasicBlock::Create(C); |
| 48 | BasicBlock* bb1 = BasicBlock::Create(C); |
| 49 | |
| 50 | // Mandatory BranchInst |
| 51 | const BranchInst* b0 = BranchInst::Create(bb0); |
| 52 | |
| 53 | // check num operands |
| 54 | EXPECT_EQ(b0->getNumOperands(), 1U); |
| 55 | |
| 56 | EXPECT_NE(b0->op_begin(), b0->op_end()); |
| 57 | |
| 58 | const IntegerType* Int1 = IntegerType::get(C, 1); |
| 59 | Constant* One = ConstantInt::get(Int1, 1, true); |
| 60 | |
| 61 | // Conditional BranchInst |
| 62 | BranchInst* b1 = BranchInst::Create(bb0, bb1, One); |
| 63 | |
| 64 | // check num operands |
| 65 | EXPECT_EQ(b1->getNumOperands(), 3U); |
| 66 | |
| 67 | User::const_op_iterator b(b1->op_begin()); |
| 68 | |
| 69 | // check COND |
| 70 | EXPECT_NE(b, b1->op_end()); |
| 71 | EXPECT_EQ(*b, One); |
| 72 | EXPECT_EQ(b1->getOperand(0), One); |
| 73 | ++b; |
| 74 | |
| 75 | // check ELSE |
| 76 | EXPECT_EQ(*b, bb1); |
| 77 | EXPECT_EQ(b1->getOperand(1), bb1); |
| 78 | ++b; |
| 79 | |
| 80 | // check THEN |
| 81 | EXPECT_EQ(*b, bb0); |
| 82 | EXPECT_EQ(b1->getOperand(2), bb0); |
| 83 | ++b; |
| 84 | |
| 85 | EXPECT_EQ(b, b1->op_end()); |
| 86 | |
| 87 | // shrink it |
| 88 | b1->setUnconditionalDest(bb1); |
| 89 | |
| 90 | // check num operands |
| 91 | EXPECT_EQ(b1->getNumOperands(), 1U); |
| 92 | |
| 93 | User::const_op_iterator c(b1->op_begin()); |
| 94 | EXPECT_NE(c, b1->op_end()); |
| 95 | |
| 96 | // check THEN |
| 97 | EXPECT_EQ(*c, bb1); |
| 98 | EXPECT_EQ(b1->getOperand(0), bb1); |
| 99 | ++c; |
| 100 | |
| 101 | EXPECT_EQ(c, b1->op_end()); |
| 102 | |
| 103 | // clean up |
| 104 | delete b0; |
| 105 | delete b1; |
| 106 | |
| 107 | delete bb0; |
| 108 | delete bb1; |
| 109 | } |
| 110 | |
Gabor Greif | 1558038 | 2010-03-16 09:55:46 +0000 | [diff] [blame] | 111 | } // end anonymous namespace |
| 112 | } // end namespace llvm |