1 //===- llvm/unittest/VMCore/InstructionsTest.cpp - Instructions unit tests ===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
21 TEST(InstructionsTest, ReturnInst) {
22 LLVMContext &C(getGlobalContext());
25 const ReturnInst* r0 = ReturnInst::Create(C);
26 EXPECT_EQ(r0->getNumOperands(), 0U);
27 EXPECT_EQ(r0->op_begin(), r0->op_end());
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(r1->getNumOperands(), 1U);
33 User::const_op_iterator b(r1->op_begin());
34 EXPECT_NE(b, r1->op_end());
36 EXPECT_EQ(r1->getOperand(0), One);
38 EXPECT_EQ(b, r1->op_end());
45 TEST(InstructionsTest, BranchInst) {
46 LLVMContext &C(getGlobalContext());
49 BasicBlock* bb0 = BasicBlock::Create(C);
50 BasicBlock* bb1 = BasicBlock::Create(C);
52 // Mandatory BranchInst
53 const BranchInst* b0 = BranchInst::Create(bb0);
55 EXPECT_TRUE(b0->isUnconditional());
56 EXPECT_FALSE(b0->isConditional());
57 EXPECT_EQ(b0->getNumSuccessors(), 1U);
60 EXPECT_EQ(b0->getNumOperands(), 1U);
62 EXPECT_NE(b0->op_begin(), b0->op_end());
63 EXPECT_EQ(llvm::next(b0->op_begin()), b0->op_end());
65 EXPECT_EQ(llvm::next(b0->op_begin()), b0->op_end());
67 IntegerType* Int1 = IntegerType::get(C, 1);
68 Constant* One = ConstantInt::get(Int1, 1, true);
70 // Conditional BranchInst
71 BranchInst* b1 = BranchInst::Create(bb0, bb1, One);
73 EXPECT_FALSE(b1->isUnconditional());
74 EXPECT_TRUE(b1->isConditional());
75 EXPECT_EQ(b1->getNumSuccessors(), 2U);
78 EXPECT_EQ(b1->getNumOperands(), 3U);
80 User::const_op_iterator b(b1->op_begin());
83 EXPECT_NE(b, b1->op_end());
85 EXPECT_EQ(b1->getOperand(0), One);
86 EXPECT_EQ(b1->getCondition(), One);
91 EXPECT_EQ(b1->getOperand(1), bb1);
92 EXPECT_EQ(b1->getSuccessor(1), bb1);
97 EXPECT_EQ(b1->getOperand(2), bb0);
98 EXPECT_EQ(b1->getSuccessor(0), bb0);
101 EXPECT_EQ(b, b1->op_end());
111 TEST(InstructionsTest, CastInst) {
112 LLVMContext &C(getGlobalContext());
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);
120 const Constant* c8 = Constant::getNullValue(V8x8Ty);
121 const Constant* c64 = Constant::getNullValue(V8x64Ty);
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::getCastOpcode(c64, true, V8x8Ty, true), CastInst::Trunc);
129 EXPECT_EQ(CastInst::getCastOpcode(c8, true, V8x64Ty, true), CastInst::SExt);
132 } // end anonymous namespace
133 } // end namespace llvm