blob: 9624b816a8d11575aa0c3805857d5665d588a075 [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"
Jay Foad7c14a552011-04-11 09:35:34 +000012#include "llvm/Constants.h"
Gabor Greif86ca5492010-03-16 11:24:53 +000013#include "llvm/DerivedTypes.h"
Gabor Greif15580382010-03-16 09:55:46 +000014#include "llvm/LLVMContext.h"
Gabor Greif62217202010-03-17 19:27:31 +000015#include "llvm/ADT/STLExtras.h"
Gabor Greif15580382010-03-16 09:55:46 +000016#include "gtest/gtest.h"
17
18namespace llvm {
19namespace {
20
Gabor Greif35a9b8b2010-03-16 10:59:48 +000021TEST(InstructionsTest, ReturnInst) {
Gabor Greif15580382010-03-16 09:55:46 +000022 LLVMContext &C(getGlobalContext());
23
Gabor Greif35a9b8b2010-03-16 10:59:48 +000024 // test for PR6589
Gabor Greif15580382010-03-16 09:55:46 +000025 const ReturnInst* r0 = ReturnInst::Create(C);
Gabor Greifc377afc2010-03-16 15:26:09 +000026 EXPECT_EQ(r0->getNumOperands(), 0U);
Gabor Greif35a9b8b2010-03-16 10:59:48 +000027 EXPECT_EQ(r0->op_begin(), r0->op_end());
Gabor Greif86ca5492010-03-16 11:24:53 +000028
29 const IntegerType* Int1 = IntegerType::get(C, 1);
30 Constant* One = ConstantInt::get(Int1, 1, true);
31 const ReturnInst* r1 = ReturnInst::Create(C, One);
Gabor Greifc377afc2010-03-16 15:26:09 +000032 EXPECT_EQ(r1->getNumOperands(), 1U);
Gabor Greif86ca5492010-03-16 11:24:53 +000033 User::const_op_iterator b(r1->op_begin());
34 EXPECT_NE(b, r1->op_end());
35 EXPECT_EQ(*b, One);
36 EXPECT_EQ(r1->getOperand(0), One);
37 ++b;
38 EXPECT_EQ(b, r1->op_end());
Gabor Greif421dd122010-03-16 12:32:03 +000039
40 // clean up
41 delete r0;
42 delete r1;
Gabor Greif15580382010-03-16 09:55:46 +000043}
44
Gabor Greifc377afc2010-03-16 15:26:09 +000045TEST(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
Gabor Greife52f3982010-03-16 15:53:58 +000055 EXPECT_TRUE(b0->isUnconditional());
56 EXPECT_FALSE(b0->isConditional());
57 EXPECT_EQ(b0->getNumSuccessors(), 1U);
58
Gabor Greifc377afc2010-03-16 15:26:09 +000059 // check num operands
60 EXPECT_EQ(b0->getNumOperands(), 1U);
61
62 EXPECT_NE(b0->op_begin(), b0->op_end());
Oscar Fuentes40b31ad2010-08-02 06:00:15 +000063 EXPECT_EQ(llvm::next(b0->op_begin()), b0->op_end());
Gabor Greife52f3982010-03-16 15:53:58 +000064
Oscar Fuentes40b31ad2010-08-02 06:00:15 +000065 EXPECT_EQ(llvm::next(b0->op_begin()), b0->op_end());
Gabor Greifc377afc2010-03-16 15:26:09 +000066
67 const 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
Gabor Greife52f3982010-03-16 15:53:58 +000073 EXPECT_FALSE(b1->isUnconditional());
74 EXPECT_TRUE(b1->isConditional());
75 EXPECT_EQ(b1->getNumSuccessors(), 2U);
76
Gabor Greifc377afc2010-03-16 15:26:09 +000077 // check num operands
78 EXPECT_EQ(b1->getNumOperands(), 3U);
79
80 User::const_op_iterator b(b1->op_begin());
81
82 // check COND
83 EXPECT_NE(b, b1->op_end());
84 EXPECT_EQ(*b, One);
85 EXPECT_EQ(b1->getOperand(0), One);
Gabor Greife52f3982010-03-16 15:53:58 +000086 EXPECT_EQ(b1->getCondition(), One);
Gabor Greifc377afc2010-03-16 15:26:09 +000087 ++b;
88
89 // check ELSE
90 EXPECT_EQ(*b, bb1);
91 EXPECT_EQ(b1->getOperand(1), bb1);
Gabor Greife52f3982010-03-16 15:53:58 +000092 EXPECT_EQ(b1->getSuccessor(1), bb1);
Gabor Greifc377afc2010-03-16 15:26:09 +000093 ++b;
94
95 // check THEN
96 EXPECT_EQ(*b, bb0);
97 EXPECT_EQ(b1->getOperand(2), bb0);
Gabor Greife52f3982010-03-16 15:53:58 +000098 EXPECT_EQ(b1->getSuccessor(0), bb0);
Gabor Greifc377afc2010-03-16 15:26:09 +000099 ++b;
100
101 EXPECT_EQ(b, b1->op_end());
102
Gabor Greifc377afc2010-03-16 15:26:09 +0000103 // clean up
104 delete b0;
105 delete b1;
106
107 delete bb0;
108 delete bb1;
109}
110
Duncan Sands2d3cdd62011-04-01 03:34:54 +0000111TEST(InstructionsTest, CastInst) {
112 LLVMContext &C(getGlobalContext());
113
114 const Type* Int8Ty = Type::getInt8Ty(C);
115 const Type* Int64Ty = Type::getInt64Ty(C);
116 const Type* V8x8Ty = VectorType::get(Int8Ty, 8);
Duncan Sandsa8514532011-05-18 07:13:41 +0000117 const Type* V8x64Ty = VectorType::get(Int64Ty, 8);
Duncan Sands2d3cdd62011-04-01 03:34:54 +0000118 const Type* X86MMXTy = Type::getX86_MMXTy(C);
119
Duncan Sandsa8514532011-05-18 07:13:41 +0000120 const Constant* c8 = Constant::getNullValue(V8x8Ty);
121 const Constant* c64 = Constant::getNullValue(V8x64Ty);
122
Duncan Sands2d3cdd62011-04-01 03:34:54 +0000123 EXPECT_TRUE(CastInst::isCastable(V8x8Ty, X86MMXTy));
124 EXPECT_TRUE(CastInst::isCastable(X86MMXTy, V8x8Ty));
125 EXPECT_FALSE(CastInst::isCastable(Int64Ty, X86MMXTy));
Duncan Sandsa8514532011-05-18 07:13:41 +0000126 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);
Duncan Sands2d3cdd62011-04-01 03:34:54 +0000130}
131
Gabor Greif15580382010-03-16 09:55:46 +0000132} // end anonymous namespace
133} // end namespace llvm