Relax alignment fragments.
[oota-llvm.git] / unittests / VMCore / InstructionsTest.cpp
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"
11 #include "llvm/BasicBlock.h"
12 #include "llvm/DerivedTypes.h"
13 #include "llvm/LLVMContext.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "gtest/gtest.h"
16
17 namespace llvm {
18 namespace {
19
20 TEST(InstructionsTest, ReturnInst) {
21   LLVMContext &C(getGlobalContext());
22
23   // test for PR6589
24   const ReturnInst* r0 = ReturnInst::Create(C);
25   EXPECT_EQ(r0->getNumOperands(), 0U);
26   EXPECT_EQ(r0->op_begin(), r0->op_end());
27
28   const IntegerType* Int1 = IntegerType::get(C, 1);
29   Constant* One = ConstantInt::get(Int1, 1, true);
30   const ReturnInst* r1 = ReturnInst::Create(C, One);
31   EXPECT_EQ(r1->getNumOperands(), 1U);
32   User::const_op_iterator b(r1->op_begin());
33   EXPECT_NE(b, r1->op_end());
34   EXPECT_EQ(*b, One);
35   EXPECT_EQ(r1->getOperand(0), One);
36   ++b;
37   EXPECT_EQ(b, r1->op_end());
38
39   // clean up
40   delete r0;
41   delete r1;
42 }
43
44 TEST(InstructionsTest, BranchInst) {
45   LLVMContext &C(getGlobalContext());
46
47   // Make a BasicBlocks
48   BasicBlock* bb0 = BasicBlock::Create(C);
49   BasicBlock* bb1 = BasicBlock::Create(C);
50
51   // Mandatory BranchInst
52   const BranchInst* b0 = BranchInst::Create(bb0);
53
54   EXPECT_TRUE(b0->isUnconditional());
55   EXPECT_FALSE(b0->isConditional());
56   EXPECT_EQ(b0->getNumSuccessors(), 1U);
57
58   // check num operands
59   EXPECT_EQ(b0->getNumOperands(), 1U);
60
61   EXPECT_NE(b0->op_begin(), b0->op_end());
62   EXPECT_EQ(llvm::next(b0->op_begin()), b0->op_end());
63
64   EXPECT_EQ(llvm::next(b0->op_begin()), b0->op_end());
65
66   const IntegerType* Int1 = IntegerType::get(C, 1);
67   Constant* One = ConstantInt::get(Int1, 1, true);
68
69   // Conditional BranchInst
70   BranchInst* b1 = BranchInst::Create(bb0, bb1, One);
71
72   EXPECT_FALSE(b1->isUnconditional());
73   EXPECT_TRUE(b1->isConditional());
74   EXPECT_EQ(b1->getNumSuccessors(), 2U);
75
76   // check num operands
77   EXPECT_EQ(b1->getNumOperands(), 3U);
78
79   User::const_op_iterator b(b1->op_begin());
80
81   // check COND
82   EXPECT_NE(b, b1->op_end());
83   EXPECT_EQ(*b, One);
84   EXPECT_EQ(b1->getOperand(0), One);
85   EXPECT_EQ(b1->getCondition(), One);
86   ++b;
87
88   // check ELSE
89   EXPECT_EQ(*b, bb1);
90   EXPECT_EQ(b1->getOperand(1), bb1);
91   EXPECT_EQ(b1->getSuccessor(1), bb1);
92   ++b;
93
94   // check THEN
95   EXPECT_EQ(*b, bb0);
96   EXPECT_EQ(b1->getOperand(2), bb0);
97   EXPECT_EQ(b1->getSuccessor(0), bb0);
98   ++b;
99
100   EXPECT_EQ(b, b1->op_end());
101
102   // shrink it
103   b1->setUnconditionalDest(bb1);
104
105   // check num operands
106   EXPECT_EQ(b1->getNumOperands(), 1U);
107
108   User::const_op_iterator c(b1->op_begin());
109   EXPECT_NE(c, b1->op_end());
110
111   // check THEN
112   EXPECT_EQ(*c, bb1);
113   EXPECT_EQ(b1->getOperand(0), bb1);
114   EXPECT_EQ(b1->getSuccessor(0), bb1);
115   ++c;
116
117   EXPECT_EQ(c, b1->op_end());
118
119   // clean up
120   delete b0;
121   delete b1;
122
123   delete bb0;
124   delete bb1;
125 }
126
127 }  // end anonymous namespace
128 }  // end namespace llvm