blob: 054193f31ee7bf4f63d06a0ca72825b19e548627 [file] [log] [blame]
Gabor Greif15580382010-03-16 09:55:46 +00001//===- 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 Greifc377afc2010-03-16 15:26:09 +000011#include "llvm/BasicBlock.h"
Gabor Greif86ca5492010-03-16 11:24:53 +000012#include "llvm/DerivedTypes.h"
Gabor Greif15580382010-03-16 09:55:46 +000013#include "llvm/LLVMContext.h"
14#include "gtest/gtest.h"
15
16namespace llvm {
17namespace {
18
Gabor Greif35a9b8b2010-03-16 10:59:48 +000019TEST(InstructionsTest, ReturnInst) {
Gabor Greif15580382010-03-16 09:55:46 +000020 LLVMContext &C(getGlobalContext());
21
Gabor Greif35a9b8b2010-03-16 10:59:48 +000022 // test for PR6589
Gabor Greif15580382010-03-16 09:55:46 +000023 const ReturnInst* r0 = ReturnInst::Create(C);
Gabor Greifc377afc2010-03-16 15:26:09 +000024 EXPECT_EQ(r0->getNumOperands(), 0U);
Gabor Greif35a9b8b2010-03-16 10:59:48 +000025 EXPECT_EQ(r0->op_begin(), r0->op_end());
Gabor Greif86ca5492010-03-16 11:24:53 +000026
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 Greifc377afc2010-03-16 15:26:09 +000030 EXPECT_EQ(r1->getNumOperands(), 1U);
Gabor Greif86ca5492010-03-16 11:24:53 +000031 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 Greif421dd122010-03-16 12:32:03 +000037
38 // clean up
39 delete r0;
40 delete r1;
Gabor Greif15580382010-03-16 09:55:46 +000041}
42
Gabor Greifc377afc2010-03-16 15:26:09 +000043TEST(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
Gabor Greife52f3982010-03-16 15:53:58 +000053 EXPECT_TRUE(b0->isUnconditional());
54 EXPECT_FALSE(b0->isConditional());
55 EXPECT_EQ(b0->getNumSuccessors(), 1U);
56
Gabor Greifc377afc2010-03-16 15:26:09 +000057 // check num operands
58 EXPECT_EQ(b0->getNumOperands(), 1U);
59
60 EXPECT_NE(b0->op_begin(), b0->op_end());
Gabor Greife52f3982010-03-16 15:53:58 +000061 EXPECT_EQ(b0->op_begin() + 1, b0->op_end());
62
63 EXPECT_EQ(b0->op_begin() + 1, b0->op_end());
Gabor Greifc377afc2010-03-16 15:26:09 +000064
65 const IntegerType* Int1 = IntegerType::get(C, 1);
66 Constant* One = ConstantInt::get(Int1, 1, true);
67
68 // Conditional BranchInst
69 BranchInst* b1 = BranchInst::Create(bb0, bb1, One);
70
Gabor Greife52f3982010-03-16 15:53:58 +000071 EXPECT_FALSE(b1->isUnconditional());
72 EXPECT_TRUE(b1->isConditional());
73 EXPECT_EQ(b1->getNumSuccessors(), 2U);
74
Gabor Greifc377afc2010-03-16 15:26:09 +000075 // check num operands
76 EXPECT_EQ(b1->getNumOperands(), 3U);
77
78 User::const_op_iterator b(b1->op_begin());
79
80 // check COND
81 EXPECT_NE(b, b1->op_end());
82 EXPECT_EQ(*b, One);
83 EXPECT_EQ(b1->getOperand(0), One);
Gabor Greife52f3982010-03-16 15:53:58 +000084 EXPECT_EQ(b1->getCondition(), One);
Gabor Greifc377afc2010-03-16 15:26:09 +000085 ++b;
86
87 // check ELSE
88 EXPECT_EQ(*b, bb1);
89 EXPECT_EQ(b1->getOperand(1), bb1);
Gabor Greife52f3982010-03-16 15:53:58 +000090 EXPECT_EQ(b1->getSuccessor(1), bb1);
Gabor Greifc377afc2010-03-16 15:26:09 +000091 ++b;
92
93 // check THEN
94 EXPECT_EQ(*b, bb0);
95 EXPECT_EQ(b1->getOperand(2), bb0);
Gabor Greife52f3982010-03-16 15:53:58 +000096 EXPECT_EQ(b1->getSuccessor(0), bb0);
Gabor Greifc377afc2010-03-16 15:26:09 +000097 ++b;
98
99 EXPECT_EQ(b, b1->op_end());
100
101 // shrink it
102 b1->setUnconditionalDest(bb1);
103
104 // check num operands
105 EXPECT_EQ(b1->getNumOperands(), 1U);
106
107 User::const_op_iterator c(b1->op_begin());
108 EXPECT_NE(c, b1->op_end());
109
110 // check THEN
111 EXPECT_EQ(*c, bb1);
112 EXPECT_EQ(b1->getOperand(0), bb1);
Gabor Greife52f3982010-03-16 15:53:58 +0000113 EXPECT_EQ(b1->getSuccessor(0), bb1);
Gabor Greifc377afc2010-03-16 15:26:09 +0000114 ++c;
115
116 EXPECT_EQ(c, b1->op_end());
117
118 // clean up
119 delete b0;
120 delete b1;
121
122 delete bb0;
123 delete bb1;
124}
125
Gabor Greif15580382010-03-16 09:55:46 +0000126} // end anonymous namespace
127} // end namespace llvm