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/Constants.h"
13#include "llvm/DerivedTypes.h"
14#include "llvm/LLVMContext.h"
15#include "llvm/ADT/STLExtras.h"
16#include "gtest/gtest.h"
17
18namespace llvm {
19namespace {
20
21TEST(InstructionsTest, ReturnInst) {
22  LLVMContext &C(getGlobalContext());
23
24  // test for PR6589
25  const ReturnInst* r0 = ReturnInst::Create(C);
26  EXPECT_EQ(r0->getNumOperands(), 0U);
27  EXPECT_EQ(r0->op_begin(), r0->op_end());
28
29  IntegerType* Int1 = IntegerType::get(C, 1);
30  Constant* One = ConstantInt::get(Int1, 1, true);
31  const ReturnInst* r1 = ReturnInst::Create(C, One);
32  EXPECT_EQ(1U, r1->getNumOperands());
33  User::const_op_iterator b(r1->op_begin());
34  EXPECT_NE(r1->op_end(), b);
35  EXPECT_EQ(One, *b);
36  EXPECT_EQ(One, r1->getOperand(0));
37  ++b;
38  EXPECT_EQ(r1->op_end(), b);
39
40  // clean up
41  delete r0;
42  delete r1;
43}
44
45TEST(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
55  EXPECT_TRUE(b0->isUnconditional());
56  EXPECT_FALSE(b0->isConditional());
57  EXPECT_EQ(1U, b0->getNumSuccessors());
58
59  // check num operands
60  EXPECT_EQ(1U, b0->getNumOperands());
61
62  EXPECT_NE(b0->op_begin(), b0->op_end());
63  EXPECT_EQ(b0->op_end(), llvm::next(b0->op_begin()));
64
65  EXPECT_EQ(b0->op_end(), llvm::next(b0->op_begin()));
66
67  IntegerType* Int1 = IntegerType::get(C, 1);
68  Constant* One = ConstantInt::get(Int1, 1, true);
69
70  // Conditional BranchInst
71  BranchInst* b1 = BranchInst::Create(bb0, bb1, One);
72
73  EXPECT_FALSE(b1->isUnconditional());
74  EXPECT_TRUE(b1->isConditional());
75  EXPECT_EQ(2U, b1->getNumSuccessors());
76
77  // check num operands
78  EXPECT_EQ(3U, b1->getNumOperands());
79
80  User::const_op_iterator b(b1->op_begin());
81
82  // check COND
83  EXPECT_NE(b, b1->op_end());
84  EXPECT_EQ(One, *b);
85  EXPECT_EQ(One, b1->getOperand(0));
86  EXPECT_EQ(One, b1->getCondition());
87  ++b;
88
89  // check ELSE
90  EXPECT_EQ(bb1, *b);
91  EXPECT_EQ(bb1, b1->getOperand(1));
92  EXPECT_EQ(bb1, b1->getSuccessor(1));
93  ++b;
94
95  // check THEN
96  EXPECT_EQ(bb0, *b);
97  EXPECT_EQ(bb0, b1->getOperand(2));
98  EXPECT_EQ(bb0, b1->getSuccessor(0));
99  ++b;
100
101  EXPECT_EQ(b1->op_end(), b);
102
103  // clean up
104  delete b0;
105  delete b1;
106
107  delete bb0;
108  delete bb1;
109}
110
111TEST(InstructionsTest, CastInst) {
112  LLVMContext &C(getGlobalContext());
113
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);
119
120  const Constant* c8 = Constant::getNullValue(V8x8Ty);
121  const Constant* c64 = Constant::getNullValue(V8x64Ty);
122
123  EXPECT_TRUE(CastInst::isCastable(V8x8Ty, X86MMXTy));
124  EXPECT_TRUE(CastInst::isCastable(X86MMXTy, V8x8Ty));
125  EXPECT_FALSE(CastInst::isCastable(Int64Ty, X86MMXTy));
126  EXPECT_TRUE(CastInst::isCastable(V8x64Ty, V8x8Ty));
127  EXPECT_TRUE(CastInst::isCastable(V8x8Ty, V8x64Ty));
128  EXPECT_EQ(CastInst::Trunc, CastInst::getCastOpcode(c64, true, V8x8Ty, true));
129  EXPECT_EQ(CastInst::SExt, CastInst::getCastOpcode(c8, true, V8x64Ty, true));
130}
131
132}  // end anonymous namespace
133}  // end namespace llvm
134