Gabor Greif | 1558038 | 2010-03-16 09:55:46 +0000 | [diff] [blame] | 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" |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 11 | #include "llvm/BasicBlock.h" |
Jay Foad | 7c14a55 | 2011-04-11 09:35:34 +0000 | [diff] [blame] | 12 | #include "llvm/Constants.h" |
Gabor Greif | 86ca549 | 2010-03-16 11:24:53 +0000 | [diff] [blame] | 13 | #include "llvm/DerivedTypes.h" |
Gabor Greif | 1558038 | 2010-03-16 09:55:46 +0000 | [diff] [blame] | 14 | #include "llvm/LLVMContext.h" |
Gabor Greif | 6221720 | 2010-03-17 19:27:31 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/STLExtras.h" |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/ValueTracking.h" |
| 17 | #include "llvm/Target/TargetData.h" |
Gabor Greif | 1558038 | 2010-03-16 09:55:46 +0000 | [diff] [blame] | 18 | #include "gtest/gtest.h" |
| 19 | |
| 20 | namespace llvm { |
| 21 | namespace { |
| 22 | |
Gabor Greif | 35a9b8b | 2010-03-16 10:59:48 +0000 | [diff] [blame] | 23 | TEST(InstructionsTest, ReturnInst) { |
Gabor Greif | 1558038 | 2010-03-16 09:55:46 +0000 | [diff] [blame] | 24 | LLVMContext &C(getGlobalContext()); |
| 25 | |
Gabor Greif | 35a9b8b | 2010-03-16 10:59:48 +0000 | [diff] [blame] | 26 | // test for PR6589 |
Gabor Greif | 1558038 | 2010-03-16 09:55:46 +0000 | [diff] [blame] | 27 | const ReturnInst* r0 = ReturnInst::Create(C); |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 28 | EXPECT_EQ(r0->getNumOperands(), 0U); |
Gabor Greif | 35a9b8b | 2010-03-16 10:59:48 +0000 | [diff] [blame] | 29 | EXPECT_EQ(r0->op_begin(), r0->op_end()); |
Gabor Greif | 86ca549 | 2010-03-16 11:24:53 +0000 | [diff] [blame] | 30 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 31 | IntegerType* Int1 = IntegerType::get(C, 1); |
Gabor Greif | 86ca549 | 2010-03-16 11:24:53 +0000 | [diff] [blame] | 32 | Constant* One = ConstantInt::get(Int1, 1, true); |
| 33 | const ReturnInst* r1 = ReturnInst::Create(C, One); |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 34 | EXPECT_EQ(1U, r1->getNumOperands()); |
Gabor Greif | 86ca549 | 2010-03-16 11:24:53 +0000 | [diff] [blame] | 35 | User::const_op_iterator b(r1->op_begin()); |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 36 | EXPECT_NE(r1->op_end(), b); |
| 37 | EXPECT_EQ(One, *b); |
| 38 | EXPECT_EQ(One, r1->getOperand(0)); |
Gabor Greif | 86ca549 | 2010-03-16 11:24:53 +0000 | [diff] [blame] | 39 | ++b; |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 40 | EXPECT_EQ(r1->op_end(), b); |
Gabor Greif | 421dd12 | 2010-03-16 12:32:03 +0000 | [diff] [blame] | 41 | |
| 42 | // clean up |
| 43 | delete r0; |
| 44 | delete r1; |
Gabor Greif | 1558038 | 2010-03-16 09:55:46 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 47 | TEST(InstructionsTest, BranchInst) { |
| 48 | LLVMContext &C(getGlobalContext()); |
| 49 | |
| 50 | // Make a BasicBlocks |
| 51 | BasicBlock* bb0 = BasicBlock::Create(C); |
| 52 | BasicBlock* bb1 = BasicBlock::Create(C); |
| 53 | |
| 54 | // Mandatory BranchInst |
| 55 | const BranchInst* b0 = BranchInst::Create(bb0); |
| 56 | |
Gabor Greif | e52f398 | 2010-03-16 15:53:58 +0000 | [diff] [blame] | 57 | EXPECT_TRUE(b0->isUnconditional()); |
| 58 | EXPECT_FALSE(b0->isConditional()); |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 59 | EXPECT_EQ(1U, b0->getNumSuccessors()); |
Gabor Greif | e52f398 | 2010-03-16 15:53:58 +0000 | [diff] [blame] | 60 | |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 61 | // check num operands |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 62 | EXPECT_EQ(1U, b0->getNumOperands()); |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 63 | |
| 64 | EXPECT_NE(b0->op_begin(), b0->op_end()); |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 65 | EXPECT_EQ(b0->op_end(), llvm::next(b0->op_begin())); |
Gabor Greif | e52f398 | 2010-03-16 15:53:58 +0000 | [diff] [blame] | 66 | |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 67 | EXPECT_EQ(b0->op_end(), llvm::next(b0->op_begin())); |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 68 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 69 | IntegerType* Int1 = IntegerType::get(C, 1); |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 70 | Constant* One = ConstantInt::get(Int1, 1, true); |
| 71 | |
| 72 | // Conditional BranchInst |
| 73 | BranchInst* b1 = BranchInst::Create(bb0, bb1, One); |
| 74 | |
Gabor Greif | e52f398 | 2010-03-16 15:53:58 +0000 | [diff] [blame] | 75 | EXPECT_FALSE(b1->isUnconditional()); |
| 76 | EXPECT_TRUE(b1->isConditional()); |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 77 | EXPECT_EQ(2U, b1->getNumSuccessors()); |
Gabor Greif | e52f398 | 2010-03-16 15:53:58 +0000 | [diff] [blame] | 78 | |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 79 | // check num operands |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 80 | EXPECT_EQ(3U, b1->getNumOperands()); |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 81 | |
| 82 | User::const_op_iterator b(b1->op_begin()); |
| 83 | |
| 84 | // check COND |
| 85 | EXPECT_NE(b, b1->op_end()); |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 86 | EXPECT_EQ(One, *b); |
| 87 | EXPECT_EQ(One, b1->getOperand(0)); |
| 88 | EXPECT_EQ(One, b1->getCondition()); |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 89 | ++b; |
| 90 | |
| 91 | // check ELSE |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 92 | EXPECT_EQ(bb1, *b); |
| 93 | EXPECT_EQ(bb1, b1->getOperand(1)); |
| 94 | EXPECT_EQ(bb1, b1->getSuccessor(1)); |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 95 | ++b; |
| 96 | |
| 97 | // check THEN |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 98 | EXPECT_EQ(bb0, *b); |
| 99 | EXPECT_EQ(bb0, b1->getOperand(2)); |
| 100 | EXPECT_EQ(bb0, b1->getSuccessor(0)); |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 101 | ++b; |
| 102 | |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 103 | EXPECT_EQ(b1->op_end(), b); |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 104 | |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 105 | // clean up |
| 106 | delete b0; |
| 107 | delete b1; |
| 108 | |
| 109 | delete bb0; |
| 110 | delete bb1; |
| 111 | } |
| 112 | |
Duncan Sands | 2d3cdd6 | 2011-04-01 03:34:54 +0000 | [diff] [blame] | 113 | TEST(InstructionsTest, CastInst) { |
| 114 | LLVMContext &C(getGlobalContext()); |
| 115 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 116 | Type* Int8Ty = Type::getInt8Ty(C); |
| 117 | Type* Int64Ty = Type::getInt64Ty(C); |
| 118 | Type* V8x8Ty = VectorType::get(Int8Ty, 8); |
| 119 | Type* V8x64Ty = VectorType::get(Int64Ty, 8); |
| 120 | Type* X86MMXTy = Type::getX86_MMXTy(C); |
Duncan Sands | 2d3cdd6 | 2011-04-01 03:34:54 +0000 | [diff] [blame] | 121 | |
Duncan Sands | a851453 | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 122 | const Constant* c8 = Constant::getNullValue(V8x8Ty); |
| 123 | const Constant* c64 = Constant::getNullValue(V8x64Ty); |
| 124 | |
Duncan Sands | 2d3cdd6 | 2011-04-01 03:34:54 +0000 | [diff] [blame] | 125 | EXPECT_TRUE(CastInst::isCastable(V8x8Ty, X86MMXTy)); |
| 126 | EXPECT_TRUE(CastInst::isCastable(X86MMXTy, V8x8Ty)); |
| 127 | EXPECT_FALSE(CastInst::isCastable(Int64Ty, X86MMXTy)); |
Duncan Sands | a851453 | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 128 | EXPECT_TRUE(CastInst::isCastable(V8x64Ty, V8x8Ty)); |
| 129 | EXPECT_TRUE(CastInst::isCastable(V8x8Ty, V8x64Ty)); |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 130 | EXPECT_EQ(CastInst::Trunc, CastInst::getCastOpcode(c64, true, V8x8Ty, true)); |
| 131 | EXPECT_EQ(CastInst::SExt, CastInst::getCastOpcode(c8, true, V8x64Ty, true)); |
Duncan Sands | 2d3cdd6 | 2011-04-01 03:34:54 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 134 | |
| 135 | |
| 136 | TEST(InstructionsTest, VectorGep) { |
| 137 | LLVMContext &C(getGlobalContext()); |
| 138 | |
| 139 | // Type Definitions |
| 140 | PointerType *Ptri8Ty = PointerType::get(IntegerType::get(C, 8), 0); |
| 141 | PointerType *Ptri32Ty = PointerType::get(IntegerType::get(C, 8), 0); |
| 142 | |
| 143 | VectorType *V2xi8PTy = VectorType::get(Ptri8Ty, 2); |
| 144 | VectorType *V2xi32PTy = VectorType::get(Ptri32Ty, 2); |
| 145 | |
| 146 | // Test different aspects of the vector-of-pointers type |
| 147 | // and GEPs which use this type. |
| 148 | ConstantInt *Ci32a = ConstantInt::get(C, APInt(32, 1492)); |
| 149 | ConstantInt *Ci32b = ConstantInt::get(C, APInt(32, 1948)); |
| 150 | std::vector<Constant*> ConstVa(2, Ci32a); |
| 151 | std::vector<Constant*> ConstVb(2, Ci32b); |
| 152 | Constant *C2xi32a = ConstantVector::get(ConstVa); |
| 153 | Constant *C2xi32b = ConstantVector::get(ConstVb); |
| 154 | |
| 155 | CastInst *PtrVecA = new IntToPtrInst(C2xi32a, V2xi32PTy); |
| 156 | CastInst *PtrVecB = new IntToPtrInst(C2xi32b, V2xi32PTy); |
| 157 | |
| 158 | ICmpInst *ICmp0 = new ICmpInst(ICmpInst::ICMP_SGT, PtrVecA, PtrVecB); |
| 159 | ICmpInst *ICmp1 = new ICmpInst(ICmpInst::ICMP_ULT, PtrVecA, PtrVecB); |
| 160 | EXPECT_NE(ICmp0, ICmp1); // suppress warning. |
| 161 | |
| 162 | GetElementPtrInst *Gep0 = GetElementPtrInst::Create(PtrVecA, C2xi32a); |
| 163 | GetElementPtrInst *Gep1 = GetElementPtrInst::Create(PtrVecA, C2xi32b); |
| 164 | GetElementPtrInst *Gep2 = GetElementPtrInst::Create(PtrVecB, C2xi32a); |
| 165 | GetElementPtrInst *Gep3 = GetElementPtrInst::Create(PtrVecB, C2xi32b); |
| 166 | |
| 167 | CastInst *BTC0 = new BitCastInst(Gep0, V2xi8PTy); |
| 168 | CastInst *BTC1 = new BitCastInst(Gep1, V2xi8PTy); |
| 169 | CastInst *BTC2 = new BitCastInst(Gep2, V2xi8PTy); |
| 170 | CastInst *BTC3 = new BitCastInst(Gep3, V2xi8PTy); |
| 171 | |
| 172 | Value *S0 = BTC0->stripPointerCasts(); |
| 173 | Value *S1 = BTC1->stripPointerCasts(); |
| 174 | Value *S2 = BTC2->stripPointerCasts(); |
| 175 | Value *S3 = BTC3->stripPointerCasts(); |
| 176 | |
| 177 | EXPECT_NE(S0, Gep0); |
| 178 | EXPECT_NE(S1, Gep1); |
| 179 | EXPECT_NE(S2, Gep2); |
| 180 | EXPECT_NE(S3, Gep3); |
| 181 | |
| 182 | int64_t Offset; |
| 183 | TargetData TD("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f3" |
| 184 | "2:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80" |
| 185 | ":128:128-n8:16:32:64-S128"); |
| 186 | // Make sure we don't crash |
| 187 | GetPointerBaseWithConstantOffset(Gep0, Offset, TD); |
| 188 | GetPointerBaseWithConstantOffset(Gep1, Offset, TD); |
| 189 | GetPointerBaseWithConstantOffset(Gep2, Offset, TD); |
| 190 | GetPointerBaseWithConstantOffset(Gep3, Offset, TD); |
| 191 | |
| 192 | // Gep of Geps |
| 193 | GetElementPtrInst *GepII0 = GetElementPtrInst::Create(Gep0, C2xi32b); |
| 194 | GetElementPtrInst *GepII1 = GetElementPtrInst::Create(Gep1, C2xi32a); |
| 195 | GetElementPtrInst *GepII2 = GetElementPtrInst::Create(Gep2, C2xi32b); |
| 196 | GetElementPtrInst *GepII3 = GetElementPtrInst::Create(Gep3, C2xi32a); |
| 197 | |
| 198 | EXPECT_EQ(GepII0->getNumIndices(), 1u); |
| 199 | EXPECT_EQ(GepII1->getNumIndices(), 1u); |
| 200 | EXPECT_EQ(GepII2->getNumIndices(), 1u); |
| 201 | EXPECT_EQ(GepII3->getNumIndices(), 1u); |
| 202 | |
| 203 | EXPECT_FALSE(GepII0->hasAllZeroIndices()); |
| 204 | EXPECT_FALSE(GepII1->hasAllZeroIndices()); |
| 205 | EXPECT_FALSE(GepII2->hasAllZeroIndices()); |
| 206 | EXPECT_FALSE(GepII3->hasAllZeroIndices()); |
| 207 | |
| 208 | delete GepII0; |
| 209 | delete GepII1; |
| 210 | delete GepII2; |
| 211 | delete GepII3; |
| 212 | |
| 213 | delete BTC0; |
| 214 | delete BTC1; |
| 215 | delete BTC2; |
| 216 | delete BTC3; |
| 217 | |
| 218 | delete Gep0; |
| 219 | delete Gep1; |
| 220 | delete Gep2; |
| 221 | delete Gep3; |
| 222 | |
| 223 | delete ICmp0; |
| 224 | delete ICmp1; |
| 225 | delete PtrVecA; |
| 226 | delete PtrVecB; |
| 227 | } |
| 228 | |
Gabor Greif | 1558038 | 2010-03-16 09:55:46 +0000 | [diff] [blame] | 229 | } // end anonymous namespace |
| 230 | } // end namespace llvm |