Chandler Carruth | c779e96 | 2013-01-07 15:35:46 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/IR/InstructionsTest.cpp - Instructions unit tests ----===// |
Gabor Greif | 5a7069a | 2010-03-16 09:55:46 +0000 | [diff] [blame] | 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 | |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 10 | #include "llvm/IR/Instructions.h" |
Chandler Carruth | 5a88dda | 2012-12-04 10:23:08 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/STLExtras.h" |
| 12 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 13 | #include "llvm/IR/BasicBlock.h" |
| 14 | #include "llvm/IR/Constants.h" |
| 15 | #include "llvm/IR/DataLayout.h" |
| 16 | #include "llvm/IR/DerivedTypes.h" |
| 17 | #include "llvm/IR/IRBuilder.h" |
| 18 | #include "llvm/IR/LLVMContext.h" |
| 19 | #include "llvm/IR/MDBuilder.h" |
| 20 | #include "llvm/IR/Operator.h" |
Gabor Greif | 5a7069a | 2010-03-16 09:55:46 +0000 | [diff] [blame] | 21 | #include "gtest/gtest.h" |
| 22 | |
| 23 | namespace llvm { |
| 24 | namespace { |
| 25 | |
Gabor Greif | 138acfe | 2010-03-16 10:59:48 +0000 | [diff] [blame] | 26 | TEST(InstructionsTest, ReturnInst) { |
Gabor Greif | 5a7069a | 2010-03-16 09:55:46 +0000 | [diff] [blame] | 27 | LLVMContext &C(getGlobalContext()); |
| 28 | |
Gabor Greif | 138acfe | 2010-03-16 10:59:48 +0000 | [diff] [blame] | 29 | // test for PR6589 |
Gabor Greif | 5a7069a | 2010-03-16 09:55:46 +0000 | [diff] [blame] | 30 | const ReturnInst* r0 = ReturnInst::Create(C); |
Gabor Greif | d165e1a | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 31 | EXPECT_EQ(r0->getNumOperands(), 0U); |
Gabor Greif | 138acfe | 2010-03-16 10:59:48 +0000 | [diff] [blame] | 32 | EXPECT_EQ(r0->op_begin(), r0->op_end()); |
Gabor Greif | 22385eb | 2010-03-16 11:24:53 +0000 | [diff] [blame] | 33 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 34 | IntegerType* Int1 = IntegerType::get(C, 1); |
Gabor Greif | 22385eb | 2010-03-16 11:24:53 +0000 | [diff] [blame] | 35 | Constant* One = ConstantInt::get(Int1, 1, true); |
| 36 | const ReturnInst* r1 = ReturnInst::Create(C, One); |
John McCall | f5ec9b5 | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 37 | EXPECT_EQ(1U, r1->getNumOperands()); |
Gabor Greif | 22385eb | 2010-03-16 11:24:53 +0000 | [diff] [blame] | 38 | User::const_op_iterator b(r1->op_begin()); |
John McCall | f5ec9b5 | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 39 | EXPECT_NE(r1->op_end(), b); |
| 40 | EXPECT_EQ(One, *b); |
| 41 | EXPECT_EQ(One, r1->getOperand(0)); |
Gabor Greif | 22385eb | 2010-03-16 11:24:53 +0000 | [diff] [blame] | 42 | ++b; |
John McCall | f5ec9b5 | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 43 | EXPECT_EQ(r1->op_end(), b); |
Gabor Greif | 642c066 | 2010-03-16 12:32:03 +0000 | [diff] [blame] | 44 | |
| 45 | // clean up |
| 46 | delete r0; |
| 47 | delete r1; |
Gabor Greif | 5a7069a | 2010-03-16 09:55:46 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Gabor Greif | d165e1a | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 50 | TEST(InstructionsTest, BranchInst) { |
| 51 | LLVMContext &C(getGlobalContext()); |
| 52 | |
| 53 | // Make a BasicBlocks |
| 54 | BasicBlock* bb0 = BasicBlock::Create(C); |
| 55 | BasicBlock* bb1 = BasicBlock::Create(C); |
| 56 | |
| 57 | // Mandatory BranchInst |
| 58 | const BranchInst* b0 = BranchInst::Create(bb0); |
| 59 | |
Gabor Greif | abf657f | 2010-03-16 15:53:58 +0000 | [diff] [blame] | 60 | EXPECT_TRUE(b0->isUnconditional()); |
| 61 | EXPECT_FALSE(b0->isConditional()); |
John McCall | f5ec9b5 | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 62 | EXPECT_EQ(1U, b0->getNumSuccessors()); |
Gabor Greif | abf657f | 2010-03-16 15:53:58 +0000 | [diff] [blame] | 63 | |
Gabor Greif | d165e1a | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 64 | // check num operands |
John McCall | f5ec9b5 | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 65 | EXPECT_EQ(1U, b0->getNumOperands()); |
Gabor Greif | d165e1a | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 66 | |
| 67 | EXPECT_NE(b0->op_begin(), b0->op_end()); |
John McCall | f5ec9b5 | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 68 | EXPECT_EQ(b0->op_end(), llvm::next(b0->op_begin())); |
Gabor Greif | abf657f | 2010-03-16 15:53:58 +0000 | [diff] [blame] | 69 | |
John McCall | f5ec9b5 | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 70 | EXPECT_EQ(b0->op_end(), llvm::next(b0->op_begin())); |
Gabor Greif | d165e1a | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 71 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 72 | IntegerType* Int1 = IntegerType::get(C, 1); |
Gabor Greif | d165e1a | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 73 | Constant* One = ConstantInt::get(Int1, 1, true); |
| 74 | |
| 75 | // Conditional BranchInst |
| 76 | BranchInst* b1 = BranchInst::Create(bb0, bb1, One); |
| 77 | |
Gabor Greif | abf657f | 2010-03-16 15:53:58 +0000 | [diff] [blame] | 78 | EXPECT_FALSE(b1->isUnconditional()); |
| 79 | EXPECT_TRUE(b1->isConditional()); |
John McCall | f5ec9b5 | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 80 | EXPECT_EQ(2U, b1->getNumSuccessors()); |
Gabor Greif | abf657f | 2010-03-16 15:53:58 +0000 | [diff] [blame] | 81 | |
Gabor Greif | d165e1a | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 82 | // check num operands |
John McCall | f5ec9b5 | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 83 | EXPECT_EQ(3U, b1->getNumOperands()); |
Gabor Greif | d165e1a | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 84 | |
| 85 | User::const_op_iterator b(b1->op_begin()); |
| 86 | |
| 87 | // check COND |
| 88 | EXPECT_NE(b, b1->op_end()); |
John McCall | f5ec9b5 | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 89 | EXPECT_EQ(One, *b); |
| 90 | EXPECT_EQ(One, b1->getOperand(0)); |
| 91 | EXPECT_EQ(One, b1->getCondition()); |
Gabor Greif | d165e1a | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 92 | ++b; |
| 93 | |
| 94 | // check ELSE |
John McCall | f5ec9b5 | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 95 | EXPECT_EQ(bb1, *b); |
| 96 | EXPECT_EQ(bb1, b1->getOperand(1)); |
| 97 | EXPECT_EQ(bb1, b1->getSuccessor(1)); |
Gabor Greif | d165e1a | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 98 | ++b; |
| 99 | |
| 100 | // check THEN |
John McCall | f5ec9b5 | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 101 | EXPECT_EQ(bb0, *b); |
| 102 | EXPECT_EQ(bb0, b1->getOperand(2)); |
| 103 | EXPECT_EQ(bb0, b1->getSuccessor(0)); |
Gabor Greif | d165e1a | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 104 | ++b; |
| 105 | |
John McCall | f5ec9b5 | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 106 | EXPECT_EQ(b1->op_end(), b); |
Gabor Greif | d165e1a | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 107 | |
Gabor Greif | d165e1a | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 108 | // clean up |
| 109 | delete b0; |
| 110 | delete b1; |
| 111 | |
| 112 | delete bb0; |
| 113 | delete bb1; |
| 114 | } |
| 115 | |
Duncan Sands | 6079465 | 2011-04-01 03:34:54 +0000 | [diff] [blame] | 116 | TEST(InstructionsTest, CastInst) { |
| 117 | LLVMContext &C(getGlobalContext()); |
| 118 | |
Matt Arsenault | f34dc42 | 2013-07-30 20:45:05 +0000 | [diff] [blame^] | 119 | Type *Int8Ty = Type::getInt8Ty(C); |
| 120 | Type *Int16Ty = Type::getInt16Ty(C); |
| 121 | Type *Int32Ty = Type::getInt32Ty(C); |
| 122 | Type *Int64Ty = Type::getInt64Ty(C); |
| 123 | Type *V8x8Ty = VectorType::get(Int8Ty, 8); |
| 124 | Type *V8x64Ty = VectorType::get(Int64Ty, 8); |
| 125 | Type *X86MMXTy = Type::getX86_MMXTy(C); |
| 126 | |
| 127 | Type *HalfTy = Type::getHalfTy(C); |
| 128 | Type *FloatTy = Type::getFloatTy(C); |
| 129 | Type *DoubleTy = Type::getDoubleTy(C); |
| 130 | |
| 131 | Type *V2Int32Ty = VectorType::get(Int32Ty, 2); |
| 132 | Type *V2Int64Ty = VectorType::get(Int64Ty, 2); |
| 133 | Type *V4Int16Ty = VectorType::get(Int16Ty, 4); |
| 134 | |
| 135 | Type *Int32PtrTy = PointerType::get(Int32Ty, 0); |
| 136 | Type *Int64PtrTy = PointerType::get(Int64Ty, 0); |
| 137 | |
| 138 | Type *Int32PtrAS1Ty = PointerType::get(Int32Ty, 1); |
| 139 | Type *Int64PtrAS1Ty = PointerType::get(Int64Ty, 1); |
| 140 | |
| 141 | Type *V2Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 2); |
| 142 | Type *V2Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 2); |
| 143 | Type *V4Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 4); |
| 144 | Type *V4Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 4); |
| 145 | |
| 146 | Type *V2Int64PtrTy = VectorType::get(Int64PtrTy, 2); |
| 147 | Type *V2Int32PtrTy = VectorType::get(Int32PtrTy, 2); |
Duncan Sands | 6079465 | 2011-04-01 03:34:54 +0000 | [diff] [blame] | 148 | |
Duncan Sands | 117feba | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 149 | const Constant* c8 = Constant::getNullValue(V8x8Ty); |
| 150 | const Constant* c64 = Constant::getNullValue(V8x64Ty); |
| 151 | |
Duncan Sands | 6079465 | 2011-04-01 03:34:54 +0000 | [diff] [blame] | 152 | EXPECT_TRUE(CastInst::isCastable(V8x8Ty, X86MMXTy)); |
| 153 | EXPECT_TRUE(CastInst::isCastable(X86MMXTy, V8x8Ty)); |
| 154 | EXPECT_FALSE(CastInst::isCastable(Int64Ty, X86MMXTy)); |
Duncan Sands | 117feba | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 155 | EXPECT_TRUE(CastInst::isCastable(V8x64Ty, V8x8Ty)); |
| 156 | EXPECT_TRUE(CastInst::isCastable(V8x8Ty, V8x64Ty)); |
John McCall | f5ec9b5 | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 157 | EXPECT_EQ(CastInst::Trunc, CastInst::getCastOpcode(c64, true, V8x8Ty, true)); |
| 158 | EXPECT_EQ(CastInst::SExt, CastInst::getCastOpcode(c8, true, V8x64Ty, true)); |
Matt Arsenault | f34dc42 | 2013-07-30 20:45:05 +0000 | [diff] [blame^] | 159 | |
| 160 | EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, X86MMXTy)); |
| 161 | EXPECT_FALSE(CastInst::isBitCastable(X86MMXTy, V8x8Ty)); |
| 162 | EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, X86MMXTy)); |
| 163 | EXPECT_FALSE(CastInst::isBitCastable(V8x64Ty, V8x8Ty)); |
| 164 | EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, V8x64Ty)); |
| 165 | |
| 166 | // Check address space casts are rejected since we don't know the sizes here |
| 167 | EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, Int32PtrAS1Ty)); |
| 168 | EXPECT_FALSE(CastInst::isBitCastable(Int32PtrAS1Ty, Int32PtrTy)); |
| 169 | EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, V2Int32PtrAS1Ty)); |
| 170 | EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int32PtrTy)); |
| 171 | EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int64PtrAS1Ty)); |
| 172 | |
| 173 | // Test mismatched number of elements for pointers |
| 174 | EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int64PtrAS1Ty)); |
| 175 | EXPECT_FALSE(CastInst::isBitCastable(V4Int64PtrAS1Ty, V2Int32PtrAS1Ty)); |
| 176 | EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int32PtrAS1Ty)); |
| 177 | EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, V2Int32PtrTy)); |
| 178 | EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int32PtrTy)); |
| 179 | |
| 180 | EXPECT_TRUE(CastInst::isBitCastable(Int32PtrTy, Int64PtrTy)); |
| 181 | EXPECT_FALSE(CastInst::isBitCastable(DoubleTy, FloatTy)); |
| 182 | EXPECT_FALSE(CastInst::isBitCastable(FloatTy, DoubleTy)); |
| 183 | EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy)); |
| 184 | EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy)); |
| 185 | EXPECT_TRUE(CastInst::isBitCastable(FloatTy, Int32Ty)); |
| 186 | EXPECT_TRUE(CastInst::isBitCastable(Int16Ty, HalfTy)); |
| 187 | EXPECT_TRUE(CastInst::isBitCastable(Int32Ty, FloatTy)); |
| 188 | EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, Int64Ty)); |
| 189 | |
| 190 | EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, V4Int16Ty)); |
| 191 | EXPECT_FALSE(CastInst::isBitCastable(Int32Ty, Int64Ty)); |
| 192 | EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, Int32Ty)); |
| 193 | |
| 194 | EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int64Ty)); |
| 195 | EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, V2Int32PtrTy)); |
| 196 | EXPECT_TRUE(CastInst::isBitCastable(V2Int64PtrTy, V2Int32PtrTy)); |
| 197 | EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrTy, V2Int64PtrTy)); |
| 198 | EXPECT_FALSE(CastInst::isBitCastable(V2Int32Ty, V2Int64Ty)); |
| 199 | EXPECT_FALSE(CastInst::isBitCastable(V2Int64Ty, V2Int32Ty)); |
Duncan Sands | 6079465 | 2011-04-01 03:34:54 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Nadav Rotem | 1608769 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 202 | TEST(InstructionsTest, VectorGep) { |
| 203 | LLVMContext &C(getGlobalContext()); |
| 204 | |
| 205 | // Type Definitions |
| 206 | PointerType *Ptri8Ty = PointerType::get(IntegerType::get(C, 8), 0); |
Matt Arsenault | 76bf61f | 2013-06-28 23:24:10 +0000 | [diff] [blame] | 207 | PointerType *Ptri32Ty = PointerType::get(IntegerType::get(C, 32), 0); |
Nadav Rotem | 1608769 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 208 | |
| 209 | VectorType *V2xi8PTy = VectorType::get(Ptri8Ty, 2); |
| 210 | VectorType *V2xi32PTy = VectorType::get(Ptri32Ty, 2); |
| 211 | |
| 212 | // Test different aspects of the vector-of-pointers type |
| 213 | // and GEPs which use this type. |
| 214 | ConstantInt *Ci32a = ConstantInt::get(C, APInt(32, 1492)); |
| 215 | ConstantInt *Ci32b = ConstantInt::get(C, APInt(32, 1948)); |
| 216 | std::vector<Constant*> ConstVa(2, Ci32a); |
| 217 | std::vector<Constant*> ConstVb(2, Ci32b); |
| 218 | Constant *C2xi32a = ConstantVector::get(ConstVa); |
| 219 | Constant *C2xi32b = ConstantVector::get(ConstVb); |
| 220 | |
| 221 | CastInst *PtrVecA = new IntToPtrInst(C2xi32a, V2xi32PTy); |
| 222 | CastInst *PtrVecB = new IntToPtrInst(C2xi32b, V2xi32PTy); |
| 223 | |
| 224 | ICmpInst *ICmp0 = new ICmpInst(ICmpInst::ICMP_SGT, PtrVecA, PtrVecB); |
| 225 | ICmpInst *ICmp1 = new ICmpInst(ICmpInst::ICMP_ULT, PtrVecA, PtrVecB); |
| 226 | EXPECT_NE(ICmp0, ICmp1); // suppress warning. |
| 227 | |
Evgeniy Stepanov | 4802b9d | 2013-01-16 14:38:50 +0000 | [diff] [blame] | 228 | BasicBlock* BB0 = BasicBlock::Create(C); |
| 229 | // Test InsertAtEnd ICmpInst constructor. |
| 230 | ICmpInst *ICmp2 = new ICmpInst(*BB0, ICmpInst::ICMP_SGE, PtrVecA, PtrVecB); |
| 231 | EXPECT_NE(ICmp0, ICmp2); // suppress warning. |
| 232 | |
Nadav Rotem | 1608769 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 233 | GetElementPtrInst *Gep0 = GetElementPtrInst::Create(PtrVecA, C2xi32a); |
| 234 | GetElementPtrInst *Gep1 = GetElementPtrInst::Create(PtrVecA, C2xi32b); |
| 235 | GetElementPtrInst *Gep2 = GetElementPtrInst::Create(PtrVecB, C2xi32a); |
| 236 | GetElementPtrInst *Gep3 = GetElementPtrInst::Create(PtrVecB, C2xi32b); |
| 237 | |
| 238 | CastInst *BTC0 = new BitCastInst(Gep0, V2xi8PTy); |
| 239 | CastInst *BTC1 = new BitCastInst(Gep1, V2xi8PTy); |
| 240 | CastInst *BTC2 = new BitCastInst(Gep2, V2xi8PTy); |
| 241 | CastInst *BTC3 = new BitCastInst(Gep3, V2xi8PTy); |
| 242 | |
| 243 | Value *S0 = BTC0->stripPointerCasts(); |
| 244 | Value *S1 = BTC1->stripPointerCasts(); |
| 245 | Value *S2 = BTC2->stripPointerCasts(); |
| 246 | Value *S3 = BTC3->stripPointerCasts(); |
| 247 | |
| 248 | EXPECT_NE(S0, Gep0); |
| 249 | EXPECT_NE(S1, Gep1); |
| 250 | EXPECT_NE(S2, Gep2); |
| 251 | EXPECT_NE(S3, Gep3); |
| 252 | |
| 253 | int64_t Offset; |
Micah Villmow | 791cfc2 | 2012-10-08 16:39:34 +0000 | [diff] [blame] | 254 | DataLayout TD("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f3" |
Nadav Rotem | 1608769 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 255 | "2:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80" |
| 256 | ":128:128-n8:16:32:64-S128"); |
| 257 | // Make sure we don't crash |
Dan Gohman | a070d2a | 2013-01-31 02:00:45 +0000 | [diff] [blame] | 258 | GetPointerBaseWithConstantOffset(Gep0, Offset, &TD); |
| 259 | GetPointerBaseWithConstantOffset(Gep1, Offset, &TD); |
| 260 | GetPointerBaseWithConstantOffset(Gep2, Offset, &TD); |
| 261 | GetPointerBaseWithConstantOffset(Gep3, Offset, &TD); |
Nadav Rotem | 1608769 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 262 | |
| 263 | // Gep of Geps |
| 264 | GetElementPtrInst *GepII0 = GetElementPtrInst::Create(Gep0, C2xi32b); |
| 265 | GetElementPtrInst *GepII1 = GetElementPtrInst::Create(Gep1, C2xi32a); |
| 266 | GetElementPtrInst *GepII2 = GetElementPtrInst::Create(Gep2, C2xi32b); |
| 267 | GetElementPtrInst *GepII3 = GetElementPtrInst::Create(Gep3, C2xi32a); |
| 268 | |
| 269 | EXPECT_EQ(GepII0->getNumIndices(), 1u); |
| 270 | EXPECT_EQ(GepII1->getNumIndices(), 1u); |
| 271 | EXPECT_EQ(GepII2->getNumIndices(), 1u); |
| 272 | EXPECT_EQ(GepII3->getNumIndices(), 1u); |
| 273 | |
| 274 | EXPECT_FALSE(GepII0->hasAllZeroIndices()); |
| 275 | EXPECT_FALSE(GepII1->hasAllZeroIndices()); |
| 276 | EXPECT_FALSE(GepII2->hasAllZeroIndices()); |
| 277 | EXPECT_FALSE(GepII3->hasAllZeroIndices()); |
| 278 | |
| 279 | delete GepII0; |
| 280 | delete GepII1; |
| 281 | delete GepII2; |
| 282 | delete GepII3; |
| 283 | |
| 284 | delete BTC0; |
| 285 | delete BTC1; |
| 286 | delete BTC2; |
| 287 | delete BTC3; |
| 288 | |
| 289 | delete Gep0; |
| 290 | delete Gep1; |
| 291 | delete Gep2; |
| 292 | delete Gep3; |
| 293 | |
Evgeniy Stepanov | 4802b9d | 2013-01-16 14:38:50 +0000 | [diff] [blame] | 294 | ICmp2->eraseFromParent(); |
| 295 | delete BB0; |
| 296 | |
Nadav Rotem | 1608769 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 297 | delete ICmp0; |
| 298 | delete ICmp1; |
| 299 | delete PtrVecA; |
| 300 | delete PtrVecB; |
| 301 | } |
| 302 | |
Duncan Sands | 8883c43 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 303 | TEST(InstructionsTest, FPMathOperator) { |
| 304 | LLVMContext &Context = getGlobalContext(); |
| 305 | IRBuilder<> Builder(Context); |
| 306 | MDBuilder MDHelper(Context); |
| 307 | Instruction *I = Builder.CreatePHI(Builder.getDoubleTy(), 0); |
| 308 | MDNode *MD1 = MDHelper.createFPMath(1.0); |
Duncan Sands | 8883c43 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 309 | Value *V1 = Builder.CreateFAdd(I, I, "", MD1); |
Duncan Sands | 8883c43 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 310 | EXPECT_TRUE(isa<FPMathOperator>(V1)); |
Duncan Sands | 8883c43 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 311 | FPMathOperator *O1 = cast<FPMathOperator>(V1); |
Duncan Sands | 8883c43 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 312 | EXPECT_EQ(O1->getFPAccuracy(), 1.0); |
Duncan Sands | 8883c43 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 313 | delete V1; |
Duncan Sands | 8883c43 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 314 | delete I; |
| 315 | } |
| 316 | |
Duncan Sands | 446cf94 | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 317 | |
| 318 | TEST(InstructionsTest, isEliminableCastPair) { |
| 319 | LLVMContext &C(getGlobalContext()); |
| 320 | |
| 321 | Type* Int32Ty = Type::getInt32Ty(C); |
| 322 | Type* Int64Ty = Type::getInt64Ty(C); |
| 323 | Type* Int64PtrTy = Type::getInt64PtrTy(C); |
| 324 | |
| 325 | // Source and destination pointers have same size -> bitcast. |
| 326 | EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt, |
| 327 | CastInst::IntToPtr, |
| 328 | Int64PtrTy, Int64Ty, Int64PtrTy, |
| 329 | Int32Ty, 0, Int32Ty), |
| 330 | CastInst::BitCast); |
| 331 | |
| 332 | // Source and destination pointers have different sizes -> fail. |
| 333 | EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt, |
| 334 | CastInst::IntToPtr, |
| 335 | Int64PtrTy, Int64Ty, Int64PtrTy, |
| 336 | Int32Ty, 0, Int64Ty), |
| 337 | 0U); |
| 338 | |
| 339 | // Middle pointer big enough -> bitcast. |
| 340 | EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr, |
| 341 | CastInst::PtrToInt, |
| 342 | Int64Ty, Int64PtrTy, Int64Ty, |
| 343 | 0, Int64Ty, 0), |
| 344 | CastInst::BitCast); |
| 345 | |
| 346 | // Middle pointer too small -> fail. |
| 347 | EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr, |
| 348 | CastInst::PtrToInt, |
| 349 | Int64Ty, Int64PtrTy, Int64Ty, |
| 350 | 0, Int32Ty, 0), |
| 351 | 0U); |
| 352 | } |
| 353 | |
Gabor Greif | 5a7069a | 2010-03-16 09:55:46 +0000 | [diff] [blame] | 354 | } // end anonymous namespace |
| 355 | } // end namespace llvm |