blob: c1baa74487aa75d7679c701a636979433f026f52 [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"
Gabor Greif62217202010-03-17 19:27:31 +000014#include "llvm/ADT/STLExtras.h"
Gabor Greif15580382010-03-16 09:55:46 +000015#include "gtest/gtest.h"
16
17namespace llvm {
18namespace {
19
Gabor Greif35a9b8b2010-03-16 10:59:48 +000020TEST(InstructionsTest, ReturnInst) {
Gabor Greif15580382010-03-16 09:55:46 +000021 LLVMContext &C(getGlobalContext());
22
Gabor Greif35a9b8b2010-03-16 10:59:48 +000023 // test for PR6589
Gabor Greif15580382010-03-16 09:55:46 +000024 const ReturnInst* r0 = ReturnInst::Create(C);
Gabor Greifc377afc2010-03-16 15:26:09 +000025 EXPECT_EQ(r0->getNumOperands(), 0U);
Gabor Greif35a9b8b2010-03-16 10:59:48 +000026 EXPECT_EQ(r0->op_begin(), r0->op_end());
Gabor Greif86ca5492010-03-16 11:24:53 +000027
28 const IntegerType* Int1 = IntegerType::get(C, 1);
29 Constant* One = ConstantInt::get(Int1, 1, true);
30 const ReturnInst* r1 = ReturnInst::Create(C, One);
Gabor Greifc377afc2010-03-16 15:26:09 +000031 EXPECT_EQ(r1->getNumOperands(), 1U);
Gabor Greif86ca5492010-03-16 11:24:53 +000032 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());
Gabor Greif421dd122010-03-16 12:32:03 +000038
39 // clean up
40 delete r0;
41 delete r1;
Gabor Greif15580382010-03-16 09:55:46 +000042}
43
Gabor Greifc377afc2010-03-16 15:26:09 +000044TEST(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
Gabor Greife52f3982010-03-16 15:53:58 +000054 EXPECT_TRUE(b0->isUnconditional());
55 EXPECT_FALSE(b0->isConditional());
56 EXPECT_EQ(b0->getNumSuccessors(), 1U);
57
Gabor Greifc377afc2010-03-16 15:26:09 +000058 // check num operands
59 EXPECT_EQ(b0->getNumOperands(), 1U);
60
61 EXPECT_NE(b0->op_begin(), b0->op_end());
Gabor Greifa9d0ef12010-03-18 18:59:08 +000062 EXPECT_EQ(next(b0->op_begin()), b0->op_end());
Gabor Greife52f3982010-03-16 15:53:58 +000063
Gabor Greif62217202010-03-17 19:27:31 +000064 EXPECT_EQ(next(b0->op_begin()), b0->op_end());
Gabor Greifc377afc2010-03-16 15:26:09 +000065
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
Gabor Greife52f3982010-03-16 15:53:58 +000072 EXPECT_FALSE(b1->isUnconditional());
73 EXPECT_TRUE(b1->isConditional());
74 EXPECT_EQ(b1->getNumSuccessors(), 2U);
75
Gabor Greifc377afc2010-03-16 15:26:09 +000076 // 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);
Gabor Greife52f3982010-03-16 15:53:58 +000085 EXPECT_EQ(b1->getCondition(), One);
Gabor Greifc377afc2010-03-16 15:26:09 +000086 ++b;
87
88 // check ELSE
89 EXPECT_EQ(*b, bb1);
90 EXPECT_EQ(b1->getOperand(1), bb1);
Gabor Greife52f3982010-03-16 15:53:58 +000091 EXPECT_EQ(b1->getSuccessor(1), bb1);
Gabor Greifc377afc2010-03-16 15:26:09 +000092 ++b;
93
94 // check THEN
95 EXPECT_EQ(*b, bb0);
96 EXPECT_EQ(b1->getOperand(2), bb0);
Gabor Greife52f3982010-03-16 15:53:58 +000097 EXPECT_EQ(b1->getSuccessor(0), bb0);
Gabor Greifc377afc2010-03-16 15:26:09 +000098 ++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);
Gabor Greife52f3982010-03-16 15:53:58 +0000114 EXPECT_EQ(b1->getSuccessor(0), bb1);
Gabor Greifc377afc2010-03-16 15:26:09 +0000115 ++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
Gabor Greif15580382010-03-16 09:55:46 +0000127} // end anonymous namespace
128} // end namespace llvm