blob: f0197bb671ab3569c793e3fc020adcdeb6b8375f [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
Chris Lattner229907c2011-07-18 04:54:35 +000029 IntegerType* Int1 = IntegerType::get(C, 1);
Gabor Greif86ca5492010-03-16 11:24:53 +000030 Constant* One = ConstantInt::get(Int1, 1, true);
31 const ReturnInst* r1 = ReturnInst::Create(C, One);
John McCalle83797c2011-08-27 19:23:22 +000032 EXPECT_EQ(1U, r1->getNumOperands());
Gabor Greif86ca5492010-03-16 11:24:53 +000033 User::const_op_iterator b(r1->op_begin());
John McCalle83797c2011-08-27 19:23:22 +000034 EXPECT_NE(r1->op_end(), b);
35 EXPECT_EQ(One, *b);
36 EXPECT_EQ(One, r1->getOperand(0));
Gabor Greif86ca5492010-03-16 11:24:53 +000037 ++b;
John McCalle83797c2011-08-27 19:23:22 +000038 EXPECT_EQ(r1->op_end(), b);
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());
John McCalle83797c2011-08-27 19:23:22 +000057 EXPECT_EQ(1U, b0->getNumSuccessors());
Gabor Greife52f3982010-03-16 15:53:58 +000058
Gabor Greifc377afc2010-03-16 15:26:09 +000059 // check num operands
John McCalle83797c2011-08-27 19:23:22 +000060 EXPECT_EQ(1U, b0->getNumOperands());
Gabor Greifc377afc2010-03-16 15:26:09 +000061
62 EXPECT_NE(b0->op_begin(), b0->op_end());
John McCalle83797c2011-08-27 19:23:22 +000063 EXPECT_EQ(b0->op_end(), llvm::next(b0->op_begin()));
Gabor Greife52f3982010-03-16 15:53:58 +000064
John McCalle83797c2011-08-27 19:23:22 +000065 EXPECT_EQ(b0->op_end(), llvm::next(b0->op_begin()));
Gabor Greifc377afc2010-03-16 15:26:09 +000066
Chris Lattner229907c2011-07-18 04:54:35 +000067 IntegerType* Int1 = IntegerType::get(C, 1);
Gabor Greifc377afc2010-03-16 15:26:09 +000068 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());
John McCalle83797c2011-08-27 19:23:22 +000075 EXPECT_EQ(2U, b1->getNumSuccessors());
Gabor Greife52f3982010-03-16 15:53:58 +000076
Gabor Greifc377afc2010-03-16 15:26:09 +000077 // check num operands
John McCalle83797c2011-08-27 19:23:22 +000078 EXPECT_EQ(3U, b1->getNumOperands());
Gabor Greifc377afc2010-03-16 15:26:09 +000079
80 User::const_op_iterator b(b1->op_begin());
81
82 // check COND
83 EXPECT_NE(b, b1->op_end());
John McCalle83797c2011-08-27 19:23:22 +000084 EXPECT_EQ(One, *b);
85 EXPECT_EQ(One, b1->getOperand(0));
86 EXPECT_EQ(One, b1->getCondition());
Gabor Greifc377afc2010-03-16 15:26:09 +000087 ++b;
88
89 // check ELSE
John McCalle83797c2011-08-27 19:23:22 +000090 EXPECT_EQ(bb1, *b);
91 EXPECT_EQ(bb1, b1->getOperand(1));
92 EXPECT_EQ(bb1, b1->getSuccessor(1));
Gabor Greifc377afc2010-03-16 15:26:09 +000093 ++b;
94
95 // check THEN
John McCalle83797c2011-08-27 19:23:22 +000096 EXPECT_EQ(bb0, *b);
97 EXPECT_EQ(bb0, b1->getOperand(2));
98 EXPECT_EQ(bb0, b1->getSuccessor(0));
Gabor Greifc377afc2010-03-16 15:26:09 +000099 ++b;
100
John McCalle83797c2011-08-27 19:23:22 +0000101 EXPECT_EQ(b1->op_end(), b);
Gabor Greifc377afc2010-03-16 15:26:09 +0000102
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
Chris Lattner229907c2011-07-18 04:54:35 +0000114 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);
Duncan Sands2d3cdd62011-04-01 03:34:54 +0000119
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));
John McCalle83797c2011-08-27 19:23:22 +0000128 EXPECT_EQ(CastInst::Trunc, CastInst::getCastOpcode(c64, true, V8x8Ty, true));
129 EXPECT_EQ(CastInst::SExt, CastInst::getCastOpcode(c8, true, V8x64Ty, true));
Duncan Sands2d3cdd62011-04-01 03:34:54 +0000130}
131
Gabor Greif15580382010-03-16 09:55:46 +0000132} // end anonymous namespace
133} // end namespace llvm