Chandler Carruth | 74b6a77 | 2013-01-07 15:35:46 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/IR/InstructionsTest.cpp - Instructions unit tests ----===// |
Gabor Greif | 1558038 | 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 | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 10 | #include "llvm/IR/Instructions.h" |
Chandler Carruth | 130cec2 | 2012-12-04 10:23:08 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/STLExtras.h" |
| 12 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 9fb823b | 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" |
Eli Bendersky | 84aa5e5 | 2014-03-26 20:41:15 +0000 | [diff] [blame] | 17 | #include "llvm/IR/Function.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 18 | #include "llvm/IR/IRBuilder.h" |
| 19 | #include "llvm/IR/LLVMContext.h" |
| 20 | #include "llvm/IR/MDBuilder.h" |
Eli Bendersky | 84aa5e5 | 2014-03-26 20:41:15 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Module.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Operator.h" |
Gabor Greif | 1558038 | 2010-03-16 09:55:46 +0000 | [diff] [blame] | 23 | #include "gtest/gtest.h" |
Eli Bendersky | 84aa5e5 | 2014-03-26 20:41:15 +0000 | [diff] [blame] | 24 | #include <memory> |
Gabor Greif | 1558038 | 2010-03-16 09:55:46 +0000 | [diff] [blame] | 25 | |
| 26 | namespace llvm { |
| 27 | namespace { |
| 28 | |
Gabor Greif | 35a9b8b | 2010-03-16 10:59:48 +0000 | [diff] [blame] | 29 | TEST(InstructionsTest, ReturnInst) { |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 30 | LLVMContext C; |
Gabor Greif | 1558038 | 2010-03-16 09:55:46 +0000 | [diff] [blame] | 31 | |
Gabor Greif | 35a9b8b | 2010-03-16 10:59:48 +0000 | [diff] [blame] | 32 | // test for PR6589 |
Gabor Greif | 1558038 | 2010-03-16 09:55:46 +0000 | [diff] [blame] | 33 | const ReturnInst* r0 = ReturnInst::Create(C); |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 34 | EXPECT_EQ(r0->getNumOperands(), 0U); |
Gabor Greif | 35a9b8b | 2010-03-16 10:59:48 +0000 | [diff] [blame] | 35 | EXPECT_EQ(r0->op_begin(), r0->op_end()); |
Gabor Greif | 86ca549 | 2010-03-16 11:24:53 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 37 | IntegerType* Int1 = IntegerType::get(C, 1); |
Gabor Greif | 86ca549 | 2010-03-16 11:24:53 +0000 | [diff] [blame] | 38 | Constant* One = ConstantInt::get(Int1, 1, true); |
| 39 | const ReturnInst* r1 = ReturnInst::Create(C, One); |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 40 | EXPECT_EQ(1U, r1->getNumOperands()); |
Gabor Greif | 86ca549 | 2010-03-16 11:24:53 +0000 | [diff] [blame] | 41 | User::const_op_iterator b(r1->op_begin()); |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 42 | EXPECT_NE(r1->op_end(), b); |
| 43 | EXPECT_EQ(One, *b); |
| 44 | EXPECT_EQ(One, r1->getOperand(0)); |
Gabor Greif | 86ca549 | 2010-03-16 11:24:53 +0000 | [diff] [blame] | 45 | ++b; |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 46 | EXPECT_EQ(r1->op_end(), b); |
Gabor Greif | 421dd12 | 2010-03-16 12:32:03 +0000 | [diff] [blame] | 47 | |
| 48 | // clean up |
| 49 | delete r0; |
| 50 | delete r1; |
Gabor Greif | 1558038 | 2010-03-16 09:55:46 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Eli Bendersky | 8474162 | 2014-03-26 21:46:24 +0000 | [diff] [blame] | 53 | // Test fixture that provides a module and a single function within it. Useful |
| 54 | // for tests that need to refer to the function in some way. |
| 55 | class ModuleWithFunctionTest : public testing::Test { |
| 56 | protected: |
NAKAMURA Takumi | be8556d | 2014-03-27 11:32:41 +0000 | [diff] [blame] | 57 | ModuleWithFunctionTest() : M(new Module("MyModule", Ctx)) { |
NAKAMURA Takumi | cb5ebf6 | 2014-03-27 11:38:28 +0000 | [diff] [blame] | 58 | FArgTypes.push_back(Type::getInt8Ty(Ctx)); |
| 59 | FArgTypes.push_back(Type::getInt32Ty(Ctx)); |
| 60 | FArgTypes.push_back(Type::getInt64Ty(Ctx)); |
Eli Bendersky | 8474162 | 2014-03-26 21:46:24 +0000 | [diff] [blame] | 61 | FunctionType *FTy = |
| 62 | FunctionType::get(Type::getVoidTy(Ctx), FArgTypes, false); |
| 63 | F = Function::Create(FTy, Function::ExternalLinkage, "", M.get()); |
| 64 | } |
Eli Bendersky | 84aa5e5 | 2014-03-26 20:41:15 +0000 | [diff] [blame] | 65 | |
Eli Bendersky | 8474162 | 2014-03-26 21:46:24 +0000 | [diff] [blame] | 66 | LLVMContext Ctx; |
| 67 | std::unique_ptr<Module> M; |
NAKAMURA Takumi | cce8a58 | 2014-03-27 11:33:11 +0000 | [diff] [blame] | 68 | SmallVector<Type *, 3> FArgTypes; |
Eli Bendersky | 8474162 | 2014-03-26 21:46:24 +0000 | [diff] [blame] | 69 | Function *F; |
| 70 | }; |
Eli Bendersky | 84aa5e5 | 2014-03-26 20:41:15 +0000 | [diff] [blame] | 71 | |
Eli Bendersky | 8474162 | 2014-03-26 21:46:24 +0000 | [diff] [blame] | 72 | TEST_F(ModuleWithFunctionTest, CallInst) { |
| 73 | Value *Args[] = {ConstantInt::get(Type::getInt8Ty(Ctx), 20), |
| 74 | ConstantInt::get(Type::getInt32Ty(Ctx), 9999), |
| 75 | ConstantInt::get(Type::getInt64Ty(Ctx), 42)}; |
Eli Bendersky | c35c4b3 | 2014-03-26 21:11:34 +0000 | [diff] [blame] | 76 | std::unique_ptr<CallInst> Call(CallInst::Create(F, Args)); |
Eli Bendersky | 84aa5e5 | 2014-03-26 20:41:15 +0000 | [diff] [blame] | 77 | |
| 78 | // Make sure iteration over a call's arguments works as expected. |
| 79 | unsigned Idx = 0; |
| 80 | for (Value *Arg : Call->arg_operands()) { |
Eli Bendersky | 8474162 | 2014-03-26 21:46:24 +0000 | [diff] [blame] | 81 | EXPECT_EQ(FArgTypes[Idx], Arg->getType()); |
Eli Bendersky | 84aa5e5 | 2014-03-26 20:41:15 +0000 | [diff] [blame] | 82 | EXPECT_EQ(Call->getArgOperand(Idx)->getType(), Arg->getType()); |
| 83 | Idx++; |
| 84 | } |
| 85 | } |
| 86 | |
Eli Bendersky | 8474162 | 2014-03-26 21:46:24 +0000 | [diff] [blame] | 87 | TEST_F(ModuleWithFunctionTest, InvokeInst) { |
| 88 | BasicBlock *BB1 = BasicBlock::Create(Ctx, "", F); |
| 89 | BasicBlock *BB2 = BasicBlock::Create(Ctx, "", F); |
| 90 | |
| 91 | Value *Args[] = {ConstantInt::get(Type::getInt8Ty(Ctx), 20), |
| 92 | ConstantInt::get(Type::getInt32Ty(Ctx), 9999), |
| 93 | ConstantInt::get(Type::getInt64Ty(Ctx), 42)}; |
| 94 | std::unique_ptr<InvokeInst> Invoke(InvokeInst::Create(F, BB1, BB2, Args)); |
| 95 | |
| 96 | // Make sure iteration over invoke's arguments works as expected. |
| 97 | unsigned Idx = 0; |
| 98 | for (Value *Arg : Invoke->arg_operands()) { |
| 99 | EXPECT_EQ(FArgTypes[Idx], Arg->getType()); |
| 100 | EXPECT_EQ(Invoke->getArgOperand(Idx)->getType(), Arg->getType()); |
| 101 | Idx++; |
| 102 | } |
| 103 | } |
| 104 | |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 105 | TEST(InstructionsTest, BranchInst) { |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 106 | LLVMContext C; |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 107 | |
| 108 | // Make a BasicBlocks |
| 109 | BasicBlock* bb0 = BasicBlock::Create(C); |
| 110 | BasicBlock* bb1 = BasicBlock::Create(C); |
| 111 | |
| 112 | // Mandatory BranchInst |
| 113 | const BranchInst* b0 = BranchInst::Create(bb0); |
| 114 | |
Gabor Greif | e52f398 | 2010-03-16 15:53:58 +0000 | [diff] [blame] | 115 | EXPECT_TRUE(b0->isUnconditional()); |
| 116 | EXPECT_FALSE(b0->isConditional()); |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 117 | EXPECT_EQ(1U, b0->getNumSuccessors()); |
Gabor Greif | e52f398 | 2010-03-16 15:53:58 +0000 | [diff] [blame] | 118 | |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 119 | // check num operands |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 120 | EXPECT_EQ(1U, b0->getNumOperands()); |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 121 | |
| 122 | EXPECT_NE(b0->op_begin(), b0->op_end()); |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 123 | EXPECT_EQ(b0->op_end(), std::next(b0->op_begin())); |
Gabor Greif | e52f398 | 2010-03-16 15:53:58 +0000 | [diff] [blame] | 124 | |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 125 | EXPECT_EQ(b0->op_end(), std::next(b0->op_begin())); |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 126 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 127 | IntegerType* Int1 = IntegerType::get(C, 1); |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 128 | Constant* One = ConstantInt::get(Int1, 1, true); |
| 129 | |
| 130 | // Conditional BranchInst |
| 131 | BranchInst* b1 = BranchInst::Create(bb0, bb1, One); |
| 132 | |
Gabor Greif | e52f398 | 2010-03-16 15:53:58 +0000 | [diff] [blame] | 133 | EXPECT_FALSE(b1->isUnconditional()); |
| 134 | EXPECT_TRUE(b1->isConditional()); |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 135 | EXPECT_EQ(2U, b1->getNumSuccessors()); |
Gabor Greif | e52f398 | 2010-03-16 15:53:58 +0000 | [diff] [blame] | 136 | |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 137 | // check num operands |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 138 | EXPECT_EQ(3U, b1->getNumOperands()); |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 139 | |
| 140 | User::const_op_iterator b(b1->op_begin()); |
| 141 | |
| 142 | // check COND |
| 143 | EXPECT_NE(b, b1->op_end()); |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 144 | EXPECT_EQ(One, *b); |
| 145 | EXPECT_EQ(One, b1->getOperand(0)); |
| 146 | EXPECT_EQ(One, b1->getCondition()); |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 147 | ++b; |
| 148 | |
| 149 | // check ELSE |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 150 | EXPECT_EQ(bb1, *b); |
| 151 | EXPECT_EQ(bb1, b1->getOperand(1)); |
| 152 | EXPECT_EQ(bb1, b1->getSuccessor(1)); |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 153 | ++b; |
| 154 | |
| 155 | // check THEN |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 156 | EXPECT_EQ(bb0, *b); |
| 157 | EXPECT_EQ(bb0, b1->getOperand(2)); |
| 158 | EXPECT_EQ(bb0, b1->getSuccessor(0)); |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 159 | ++b; |
| 160 | |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 161 | EXPECT_EQ(b1->op_end(), b); |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 162 | |
Gabor Greif | c377afc | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 163 | // clean up |
| 164 | delete b0; |
| 165 | delete b1; |
| 166 | |
| 167 | delete bb0; |
| 168 | delete bb1; |
| 169 | } |
| 170 | |
Duncan Sands | 2d3cdd6 | 2011-04-01 03:34:54 +0000 | [diff] [blame] | 171 | TEST(InstructionsTest, CastInst) { |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 172 | LLVMContext C; |
Duncan Sands | 2d3cdd6 | 2011-04-01 03:34:54 +0000 | [diff] [blame] | 173 | |
Matt Arsenault | cacbb23 | 2013-07-30 20:45:05 +0000 | [diff] [blame] | 174 | Type *Int8Ty = Type::getInt8Ty(C); |
| 175 | Type *Int16Ty = Type::getInt16Ty(C); |
| 176 | Type *Int32Ty = Type::getInt32Ty(C); |
| 177 | Type *Int64Ty = Type::getInt64Ty(C); |
| 178 | Type *V8x8Ty = VectorType::get(Int8Ty, 8); |
| 179 | Type *V8x64Ty = VectorType::get(Int64Ty, 8); |
| 180 | Type *X86MMXTy = Type::getX86_MMXTy(C); |
| 181 | |
| 182 | Type *HalfTy = Type::getHalfTy(C); |
| 183 | Type *FloatTy = Type::getFloatTy(C); |
| 184 | Type *DoubleTy = Type::getDoubleTy(C); |
| 185 | |
| 186 | Type *V2Int32Ty = VectorType::get(Int32Ty, 2); |
| 187 | Type *V2Int64Ty = VectorType::get(Int64Ty, 2); |
| 188 | Type *V4Int16Ty = VectorType::get(Int16Ty, 4); |
| 189 | |
| 190 | Type *Int32PtrTy = PointerType::get(Int32Ty, 0); |
| 191 | Type *Int64PtrTy = PointerType::get(Int64Ty, 0); |
| 192 | |
| 193 | Type *Int32PtrAS1Ty = PointerType::get(Int32Ty, 1); |
| 194 | Type *Int64PtrAS1Ty = PointerType::get(Int64Ty, 1); |
| 195 | |
| 196 | Type *V2Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 2); |
| 197 | Type *V2Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 2); |
| 198 | Type *V4Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 4); |
| 199 | Type *V4Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 4); |
| 200 | |
| 201 | Type *V2Int64PtrTy = VectorType::get(Int64PtrTy, 2); |
| 202 | Type *V2Int32PtrTy = VectorType::get(Int32PtrTy, 2); |
Matt Arsenault | fc3c91d | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 203 | Type *V4Int32PtrTy = VectorType::get(Int32PtrTy, 4); |
Duncan Sands | 2d3cdd6 | 2011-04-01 03:34:54 +0000 | [diff] [blame] | 204 | |
Duncan Sands | a851453 | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 205 | const Constant* c8 = Constant::getNullValue(V8x8Ty); |
| 206 | const Constant* c64 = Constant::getNullValue(V8x64Ty); |
| 207 | |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 208 | const Constant *v2ptr32 = Constant::getNullValue(V2Int32PtrTy); |
| 209 | |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 210 | EXPECT_TRUE(CastInst::isCastable(V8x8Ty, X86MMXTy)); |
| 211 | EXPECT_TRUE(CastInst::isCastable(X86MMXTy, V8x8Ty)); |
| 212 | EXPECT_FALSE(CastInst::isCastable(Int64Ty, X86MMXTy)); |
| 213 | EXPECT_TRUE(CastInst::isCastable(V8x64Ty, V8x8Ty)); |
| 214 | EXPECT_TRUE(CastInst::isCastable(V8x8Ty, V8x64Ty)); |
John McCall | e83797c | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 215 | EXPECT_EQ(CastInst::Trunc, CastInst::getCastOpcode(c64, true, V8x8Ty, true)); |
| 216 | EXPECT_EQ(CastInst::SExt, CastInst::getCastOpcode(c8, true, V8x64Ty, true)); |
Matt Arsenault | cacbb23 | 2013-07-30 20:45:05 +0000 | [diff] [blame] | 217 | |
| 218 | EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, X86MMXTy)); |
| 219 | EXPECT_FALSE(CastInst::isBitCastable(X86MMXTy, V8x8Ty)); |
| 220 | EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, X86MMXTy)); |
| 221 | EXPECT_FALSE(CastInst::isBitCastable(V8x64Ty, V8x8Ty)); |
| 222 | EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, V8x64Ty)); |
| 223 | |
| 224 | // Check address space casts are rejected since we don't know the sizes here |
| 225 | EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, Int32PtrAS1Ty)); |
| 226 | EXPECT_FALSE(CastInst::isBitCastable(Int32PtrAS1Ty, Int32PtrTy)); |
| 227 | EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, V2Int32PtrAS1Ty)); |
| 228 | EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int32PtrTy)); |
| 229 | EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int64PtrAS1Ty)); |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 230 | EXPECT_TRUE(CastInst::isCastable(V2Int32PtrAS1Ty, V2Int32PtrTy)); |
| 231 | EXPECT_EQ(CastInst::AddrSpaceCast, CastInst::getCastOpcode(v2ptr32, true, |
| 232 | V2Int32PtrAS1Ty, |
| 233 | true)); |
Matt Arsenault | cacbb23 | 2013-07-30 20:45:05 +0000 | [diff] [blame] | 234 | |
| 235 | // Test mismatched number of elements for pointers |
| 236 | EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int64PtrAS1Ty)); |
| 237 | EXPECT_FALSE(CastInst::isBitCastable(V4Int64PtrAS1Ty, V2Int32PtrAS1Ty)); |
| 238 | EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int32PtrAS1Ty)); |
| 239 | EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, V2Int32PtrTy)); |
| 240 | EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int32PtrTy)); |
| 241 | |
| 242 | EXPECT_TRUE(CastInst::isBitCastable(Int32PtrTy, Int64PtrTy)); |
| 243 | EXPECT_FALSE(CastInst::isBitCastable(DoubleTy, FloatTy)); |
| 244 | EXPECT_FALSE(CastInst::isBitCastable(FloatTy, DoubleTy)); |
| 245 | EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy)); |
| 246 | EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy)); |
| 247 | EXPECT_TRUE(CastInst::isBitCastable(FloatTy, Int32Ty)); |
| 248 | EXPECT_TRUE(CastInst::isBitCastable(Int16Ty, HalfTy)); |
| 249 | EXPECT_TRUE(CastInst::isBitCastable(Int32Ty, FloatTy)); |
| 250 | EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, Int64Ty)); |
| 251 | |
| 252 | EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, V4Int16Ty)); |
| 253 | EXPECT_FALSE(CastInst::isBitCastable(Int32Ty, Int64Ty)); |
| 254 | EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, Int32Ty)); |
| 255 | |
| 256 | EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int64Ty)); |
| 257 | EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, V2Int32PtrTy)); |
| 258 | EXPECT_TRUE(CastInst::isBitCastable(V2Int64PtrTy, V2Int32PtrTy)); |
| 259 | EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrTy, V2Int64PtrTy)); |
| 260 | EXPECT_FALSE(CastInst::isBitCastable(V2Int32Ty, V2Int64Ty)); |
| 261 | EXPECT_FALSE(CastInst::isBitCastable(V2Int64Ty, V2Int32Ty)); |
Matt Arsenault | 065ced9 | 2013-07-31 00:17:33 +0000 | [diff] [blame] | 262 | |
| 263 | |
Matt Arsenault | fc3c91d | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 264 | EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast, |
| 265 | Constant::getNullValue(V4Int32PtrTy), |
| 266 | V2Int32PtrTy)); |
| 267 | EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast, |
| 268 | Constant::getNullValue(V2Int32PtrTy), |
| 269 | V4Int32PtrTy)); |
| 270 | |
| 271 | EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast, |
| 272 | Constant::getNullValue(V4Int32PtrAS1Ty), |
| 273 | V2Int32PtrTy)); |
| 274 | EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast, |
| 275 | Constant::getNullValue(V2Int32PtrTy), |
| 276 | V4Int32PtrAS1Ty)); |
| 277 | |
| 278 | |
Matt Arsenault | 065ced9 | 2013-07-31 00:17:33 +0000 | [diff] [blame] | 279 | // Check that assertion is not hit when creating a cast with a vector of |
| 280 | // pointers |
| 281 | // First form |
| 282 | BasicBlock *BB = BasicBlock::Create(C); |
| 283 | Constant *NullV2I32Ptr = Constant::getNullValue(V2Int32PtrTy); |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 284 | auto Inst1 = CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty, "foo", BB); |
Matt Arsenault | 065ced9 | 2013-07-31 00:17:33 +0000 | [diff] [blame] | 285 | |
| 286 | // Second form |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 287 | auto Inst2 = CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty); |
| 288 | |
| 289 | delete Inst2; |
| 290 | Inst1->eraseFromParent(); |
| 291 | delete BB; |
Duncan Sands | 2d3cdd6 | 2011-04-01 03:34:54 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 294 | TEST(InstructionsTest, VectorGep) { |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 295 | LLVMContext C; |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 296 | |
| 297 | // Type Definitions |
David Blaikie | b3a3906 | 2015-03-14 21:40:10 +0000 | [diff] [blame] | 298 | Type *I8Ty = IntegerType::get(C, 8); |
| 299 | Type *I32Ty = IntegerType::get(C, 32); |
| 300 | PointerType *Ptri8Ty = PointerType::get(I8Ty, 0); |
| 301 | PointerType *Ptri32Ty = PointerType::get(I32Ty, 0); |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 302 | |
| 303 | VectorType *V2xi8PTy = VectorType::get(Ptri8Ty, 2); |
| 304 | VectorType *V2xi32PTy = VectorType::get(Ptri32Ty, 2); |
| 305 | |
| 306 | // Test different aspects of the vector-of-pointers type |
| 307 | // and GEPs which use this type. |
| 308 | ConstantInt *Ci32a = ConstantInt::get(C, APInt(32, 1492)); |
| 309 | ConstantInt *Ci32b = ConstantInt::get(C, APInt(32, 1948)); |
| 310 | std::vector<Constant*> ConstVa(2, Ci32a); |
| 311 | std::vector<Constant*> ConstVb(2, Ci32b); |
| 312 | Constant *C2xi32a = ConstantVector::get(ConstVa); |
| 313 | Constant *C2xi32b = ConstantVector::get(ConstVb); |
| 314 | |
| 315 | CastInst *PtrVecA = new IntToPtrInst(C2xi32a, V2xi32PTy); |
| 316 | CastInst *PtrVecB = new IntToPtrInst(C2xi32b, V2xi32PTy); |
| 317 | |
| 318 | ICmpInst *ICmp0 = new ICmpInst(ICmpInst::ICMP_SGT, PtrVecA, PtrVecB); |
| 319 | ICmpInst *ICmp1 = new ICmpInst(ICmpInst::ICMP_ULT, PtrVecA, PtrVecB); |
| 320 | EXPECT_NE(ICmp0, ICmp1); // suppress warning. |
| 321 | |
Evgeniy Stepanov | a259b26 | 2013-01-16 14:38:50 +0000 | [diff] [blame] | 322 | BasicBlock* BB0 = BasicBlock::Create(C); |
| 323 | // Test InsertAtEnd ICmpInst constructor. |
| 324 | ICmpInst *ICmp2 = new ICmpInst(*BB0, ICmpInst::ICMP_SGE, PtrVecA, PtrVecB); |
| 325 | EXPECT_NE(ICmp0, ICmp2); // suppress warning. |
| 326 | |
David Blaikie | b3a3906 | 2015-03-14 21:40:10 +0000 | [diff] [blame] | 327 | GetElementPtrInst *Gep0 = GetElementPtrInst::Create(I32Ty, PtrVecA, C2xi32a); |
| 328 | GetElementPtrInst *Gep1 = GetElementPtrInst::Create(I32Ty, PtrVecA, C2xi32b); |
| 329 | GetElementPtrInst *Gep2 = GetElementPtrInst::Create(I32Ty, PtrVecB, C2xi32a); |
| 330 | GetElementPtrInst *Gep3 = GetElementPtrInst::Create(I32Ty, PtrVecB, C2xi32b); |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 331 | |
| 332 | CastInst *BTC0 = new BitCastInst(Gep0, V2xi8PTy); |
| 333 | CastInst *BTC1 = new BitCastInst(Gep1, V2xi8PTy); |
| 334 | CastInst *BTC2 = new BitCastInst(Gep2, V2xi8PTy); |
| 335 | CastInst *BTC3 = new BitCastInst(Gep3, V2xi8PTy); |
| 336 | |
| 337 | Value *S0 = BTC0->stripPointerCasts(); |
| 338 | Value *S1 = BTC1->stripPointerCasts(); |
| 339 | Value *S2 = BTC2->stripPointerCasts(); |
| 340 | Value *S3 = BTC3->stripPointerCasts(); |
| 341 | |
| 342 | EXPECT_NE(S0, Gep0); |
| 343 | EXPECT_NE(S1, Gep1); |
| 344 | EXPECT_NE(S2, Gep2); |
| 345 | EXPECT_NE(S3, Gep3); |
| 346 | |
| 347 | int64_t Offset; |
Micah Villmow | 9cfc13d | 2012-10-08 16:39:34 +0000 | [diff] [blame] | 348 | DataLayout TD("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f3" |
Rafael Espindola | c675162 | 2013-12-13 18:56:34 +0000 | [diff] [blame] | 349 | "2:32:32-f64:64:64-v64:64:64-v128:128:128-a:0:64-s:64:64-f80" |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 350 | ":128:128-n8:16:32:64-S128"); |
| 351 | // Make sure we don't crash |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 352 | GetPointerBaseWithConstantOffset(Gep0, Offset, TD); |
| 353 | GetPointerBaseWithConstantOffset(Gep1, Offset, TD); |
| 354 | GetPointerBaseWithConstantOffset(Gep2, Offset, TD); |
| 355 | GetPointerBaseWithConstantOffset(Gep3, Offset, TD); |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 356 | |
| 357 | // Gep of Geps |
David Blaikie | b3a3906 | 2015-03-14 21:40:10 +0000 | [diff] [blame] | 358 | GetElementPtrInst *GepII0 = GetElementPtrInst::Create(I32Ty, Gep0, C2xi32b); |
| 359 | GetElementPtrInst *GepII1 = GetElementPtrInst::Create(I32Ty, Gep1, C2xi32a); |
| 360 | GetElementPtrInst *GepII2 = GetElementPtrInst::Create(I32Ty, Gep2, C2xi32b); |
| 361 | GetElementPtrInst *GepII3 = GetElementPtrInst::Create(I32Ty, Gep3, C2xi32a); |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 362 | |
| 363 | EXPECT_EQ(GepII0->getNumIndices(), 1u); |
| 364 | EXPECT_EQ(GepII1->getNumIndices(), 1u); |
| 365 | EXPECT_EQ(GepII2->getNumIndices(), 1u); |
| 366 | EXPECT_EQ(GepII3->getNumIndices(), 1u); |
| 367 | |
| 368 | EXPECT_FALSE(GepII0->hasAllZeroIndices()); |
| 369 | EXPECT_FALSE(GepII1->hasAllZeroIndices()); |
| 370 | EXPECT_FALSE(GepII2->hasAllZeroIndices()); |
| 371 | EXPECT_FALSE(GepII3->hasAllZeroIndices()); |
| 372 | |
| 373 | delete GepII0; |
| 374 | delete GepII1; |
| 375 | delete GepII2; |
| 376 | delete GepII3; |
| 377 | |
| 378 | delete BTC0; |
| 379 | delete BTC1; |
| 380 | delete BTC2; |
| 381 | delete BTC3; |
| 382 | |
| 383 | delete Gep0; |
| 384 | delete Gep1; |
| 385 | delete Gep2; |
| 386 | delete Gep3; |
| 387 | |
Evgeniy Stepanov | a259b26 | 2013-01-16 14:38:50 +0000 | [diff] [blame] | 388 | ICmp2->eraseFromParent(); |
| 389 | delete BB0; |
| 390 | |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 391 | delete ICmp0; |
| 392 | delete ICmp1; |
| 393 | delete PtrVecA; |
| 394 | delete PtrVecB; |
| 395 | } |
| 396 | |
Duncan Sands | 05f4df8 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 397 | TEST(InstructionsTest, FPMathOperator) { |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 398 | LLVMContext Context; |
Duncan Sands | 05f4df8 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 399 | IRBuilder<> Builder(Context); |
| 400 | MDBuilder MDHelper(Context); |
| 401 | Instruction *I = Builder.CreatePHI(Builder.getDoubleTy(), 0); |
| 402 | MDNode *MD1 = MDHelper.createFPMath(1.0); |
Duncan Sands | 05f4df8 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 403 | Value *V1 = Builder.CreateFAdd(I, I, "", MD1); |
Duncan Sands | 05f4df8 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 404 | EXPECT_TRUE(isa<FPMathOperator>(V1)); |
Duncan Sands | 05f4df8 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 405 | FPMathOperator *O1 = cast<FPMathOperator>(V1); |
Duncan Sands | 05f4df8 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 406 | EXPECT_EQ(O1->getFPAccuracy(), 1.0); |
Duncan Sands | 05f4df8 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 407 | delete V1; |
Duncan Sands | 05f4df8 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 408 | delete I; |
| 409 | } |
| 410 | |
Duncan Sands | e2395dc | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 411 | |
| 412 | TEST(InstructionsTest, isEliminableCastPair) { |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 413 | LLVMContext C; |
Duncan Sands | e2395dc | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 414 | |
Matt Arsenault | 130e0ef | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 415 | Type* Int16Ty = Type::getInt16Ty(C); |
Duncan Sands | e2395dc | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 416 | Type* Int32Ty = Type::getInt32Ty(C); |
| 417 | Type* Int64Ty = Type::getInt64Ty(C); |
| 418 | Type* Int64PtrTy = Type::getInt64PtrTy(C); |
| 419 | |
| 420 | // Source and destination pointers have same size -> bitcast. |
| 421 | EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt, |
| 422 | CastInst::IntToPtr, |
| 423 | Int64PtrTy, Int64Ty, Int64PtrTy, |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 424 | Int32Ty, nullptr, Int32Ty), |
Duncan Sands | e2395dc | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 425 | CastInst::BitCast); |
| 426 | |
Matt Arsenault | 130e0ef | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 427 | // Source and destination have unknown sizes, but the same address space and |
| 428 | // the intermediate int is the maximum pointer size -> bitcast |
Duncan Sands | e2395dc | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 429 | EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt, |
| 430 | CastInst::IntToPtr, |
| 431 | Int64PtrTy, Int64Ty, Int64PtrTy, |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 432 | nullptr, nullptr, nullptr), |
Matt Arsenault | 130e0ef | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 433 | CastInst::BitCast); |
| 434 | |
| 435 | // Source and destination have unknown sizes, but the same address space and |
| 436 | // the intermediate int is not the maximum pointer size -> nothing |
| 437 | EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt, |
| 438 | CastInst::IntToPtr, |
| 439 | Int64PtrTy, Int32Ty, Int64PtrTy, |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 440 | nullptr, nullptr, nullptr), |
Duncan Sands | e2395dc | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 441 | 0U); |
| 442 | |
| 443 | // Middle pointer big enough -> bitcast. |
| 444 | EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr, |
| 445 | CastInst::PtrToInt, |
| 446 | Int64Ty, Int64PtrTy, Int64Ty, |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 447 | nullptr, Int64Ty, nullptr), |
Duncan Sands | e2395dc | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 448 | CastInst::BitCast); |
| 449 | |
| 450 | // Middle pointer too small -> fail. |
| 451 | EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr, |
| 452 | CastInst::PtrToInt, |
| 453 | Int64Ty, Int64PtrTy, Int64Ty, |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 454 | nullptr, Int32Ty, nullptr), |
Duncan Sands | e2395dc | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 455 | 0U); |
Matt Arsenault | 130e0ef | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 456 | |
Matt Arsenault | 130e0ef | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 457 | // Test that we don't eliminate bitcasts between different address spaces, |
| 458 | // or if we don't have available pointer size information. |
| 459 | DataLayout DL("e-p:32:32:32-p1:16:16:16-p2:64:64:64-i1:8:8-i8:8:8-i16:16:16" |
| 460 | "-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64" |
Rafael Espindola | c675162 | 2013-12-13 18:56:34 +0000 | [diff] [blame] | 461 | "-v128:128:128-a:0:64-s:64:64-f80:128:128-n8:16:32:64-S128"); |
Matt Arsenault | 130e0ef | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 462 | |
| 463 | Type* Int64PtrTyAS1 = Type::getInt64PtrTy(C, 1); |
| 464 | Type* Int64PtrTyAS2 = Type::getInt64PtrTy(C, 2); |
| 465 | |
| 466 | IntegerType *Int16SizePtr = DL.getIntPtrType(C, 1); |
| 467 | IntegerType *Int64SizePtr = DL.getIntPtrType(C, 2); |
| 468 | |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 469 | // Cannot simplify inttoptr, addrspacecast |
Matt Arsenault | 130e0ef | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 470 | EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr, |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 471 | CastInst::AddrSpaceCast, |
Matt Arsenault | 130e0ef | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 472 | Int16Ty, Int64PtrTyAS1, Int64PtrTyAS2, |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 473 | nullptr, Int16SizePtr, Int64SizePtr), |
Matt Arsenault | 130e0ef | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 474 | 0U); |
| 475 | |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 476 | // Cannot simplify addrspacecast, ptrtoint |
| 477 | EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::AddrSpaceCast, |
| 478 | CastInst::PtrToInt, |
| 479 | Int64PtrTyAS1, Int64PtrTyAS2, Int16Ty, |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 480 | Int64SizePtr, Int16SizePtr, nullptr), |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 481 | 0U); |
| 482 | |
Matt Arsenault | 130e0ef | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 483 | // Pass since the bitcast address spaces are the same |
| 484 | EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr, |
| 485 | CastInst::BitCast, |
| 486 | Int16Ty, Int64PtrTyAS1, Int64PtrTyAS1, |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 487 | nullptr, nullptr, nullptr), |
Matt Arsenault | 130e0ef | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 488 | CastInst::IntToPtr); |
| 489 | |
Duncan Sands | e2395dc | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Reid Kleckner | 118e1bf | 2014-05-06 20:08:20 +0000 | [diff] [blame] | 492 | TEST(InstructionsTest, CloneCall) { |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 493 | LLVMContext C; |
Reid Kleckner | 118e1bf | 2014-05-06 20:08:20 +0000 | [diff] [blame] | 494 | Type *Int32Ty = Type::getInt32Ty(C); |
| 495 | Type *ArgTys[] = {Int32Ty, Int32Ty, Int32Ty}; |
| 496 | Type *FnTy = FunctionType::get(Int32Ty, ArgTys, /*isVarArg=*/false); |
| 497 | Value *Callee = Constant::getNullValue(FnTy->getPointerTo()); |
| 498 | Value *Args[] = { |
| 499 | ConstantInt::get(Int32Ty, 1), |
| 500 | ConstantInt::get(Int32Ty, 2), |
| 501 | ConstantInt::get(Int32Ty, 3) |
| 502 | }; |
| 503 | std::unique_ptr<CallInst> Call(CallInst::Create(Callee, Args, "result")); |
| 504 | |
| 505 | // Test cloning the tail call kind. |
| 506 | CallInst::TailCallKind Kinds[] = {CallInst::TCK_None, CallInst::TCK_Tail, |
| 507 | CallInst::TCK_MustTail}; |
| 508 | for (CallInst::TailCallKind TCK : Kinds) { |
| 509 | Call->setTailCallKind(TCK); |
| 510 | std::unique_ptr<CallInst> Clone(cast<CallInst>(Call->clone())); |
| 511 | EXPECT_EQ(Call->getTailCallKind(), Clone->getTailCallKind()); |
| 512 | } |
| 513 | Call->setTailCallKind(CallInst::TCK_None); |
| 514 | |
| 515 | // Test cloning an attribute. |
| 516 | { |
| 517 | AttrBuilder AB; |
| 518 | AB.addAttribute(Attribute::ReadOnly); |
| 519 | Call->setAttributes(AttributeSet::get(C, AttributeSet::FunctionIndex, AB)); |
| 520 | std::unique_ptr<CallInst> Clone(cast<CallInst>(Call->clone())); |
| 521 | EXPECT_TRUE(Clone->onlyReadsMemory()); |
| 522 | } |
| 523 | } |
| 524 | |
Joseph Tremoulet | bba70e4 | 2016-01-14 06:21:42 +0000 | [diff] [blame] | 525 | TEST(InstructionsTest, AlterCallBundles) { |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 526 | LLVMContext C; |
Joseph Tremoulet | bba70e4 | 2016-01-14 06:21:42 +0000 | [diff] [blame] | 527 | Type *Int32Ty = Type::getInt32Ty(C); |
| 528 | Type *FnTy = FunctionType::get(Int32Ty, Int32Ty, /*isVarArg=*/false); |
| 529 | Value *Callee = Constant::getNullValue(FnTy->getPointerTo()); |
| 530 | Value *Args[] = {ConstantInt::get(Int32Ty, 42)}; |
| 531 | OperandBundleDef OldBundle("before", UndefValue::get(Int32Ty)); |
| 532 | std::unique_ptr<CallInst> Call( |
| 533 | CallInst::Create(Callee, Args, OldBundle, "result")); |
| 534 | Call->setTailCallKind(CallInst::TailCallKind::TCK_NoTail); |
| 535 | AttrBuilder AB; |
| 536 | AB.addAttribute(Attribute::Cold); |
| 537 | Call->setAttributes(AttributeSet::get(C, AttributeSet::FunctionIndex, AB)); |
| 538 | Call->setDebugLoc(DebugLoc(MDNode::get(C, None))); |
Matt Arsenault | 130e0ef | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 539 | |
Joseph Tremoulet | bba70e4 | 2016-01-14 06:21:42 +0000 | [diff] [blame] | 540 | OperandBundleDef NewBundle("after", ConstantInt::get(Int32Ty, 7)); |
| 541 | std::unique_ptr<CallInst> Clone(CallInst::Create(Call.get(), NewBundle)); |
| 542 | EXPECT_EQ(Call->getNumArgOperands(), Clone->getNumArgOperands()); |
| 543 | EXPECT_EQ(Call->getArgOperand(0), Clone->getArgOperand(0)); |
| 544 | EXPECT_EQ(Call->getCallingConv(), Clone->getCallingConv()); |
| 545 | EXPECT_EQ(Call->getTailCallKind(), Clone->getTailCallKind()); |
| 546 | EXPECT_TRUE(Clone->hasFnAttr(Attribute::AttrKind::Cold)); |
| 547 | EXPECT_EQ(Call->getDebugLoc(), Clone->getDebugLoc()); |
Joseph Tremoulet | 56c9958 | 2016-01-14 06:30:19 +0000 | [diff] [blame] | 548 | EXPECT_EQ(Clone->getNumOperandBundles(), 1U); |
Joseph Tremoulet | bba70e4 | 2016-01-14 06:21:42 +0000 | [diff] [blame] | 549 | EXPECT_TRUE(Clone->getOperandBundle("after").hasValue()); |
| 550 | } |
Matt Arsenault | 130e0ef | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 551 | |
Joseph Tremoulet | bba70e4 | 2016-01-14 06:21:42 +0000 | [diff] [blame] | 552 | TEST(InstructionsTest, AlterInvokeBundles) { |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 553 | LLVMContext C; |
Joseph Tremoulet | bba70e4 | 2016-01-14 06:21:42 +0000 | [diff] [blame] | 554 | Type *Int32Ty = Type::getInt32Ty(C); |
| 555 | Type *FnTy = FunctionType::get(Int32Ty, Int32Ty, /*isVarArg=*/false); |
| 556 | Value *Callee = Constant::getNullValue(FnTy->getPointerTo()); |
| 557 | Value *Args[] = {ConstantInt::get(Int32Ty, 42)}; |
Joseph Tremoulet | f6cc7e6 | 2016-01-15 15:08:36 +0000 | [diff] [blame] | 558 | std::unique_ptr<BasicBlock> NormalDest(BasicBlock::Create(C)); |
| 559 | std::unique_ptr<BasicBlock> UnwindDest(BasicBlock::Create(C)); |
Joseph Tremoulet | bba70e4 | 2016-01-14 06:21:42 +0000 | [diff] [blame] | 560 | OperandBundleDef OldBundle("before", UndefValue::get(Int32Ty)); |
Joseph Tremoulet | f6cc7e6 | 2016-01-15 15:08:36 +0000 | [diff] [blame] | 561 | std::unique_ptr<InvokeInst> Invoke(InvokeInst::Create( |
| 562 | Callee, NormalDest.get(), UnwindDest.get(), Args, OldBundle, "result")); |
Joseph Tremoulet | bba70e4 | 2016-01-14 06:21:42 +0000 | [diff] [blame] | 563 | AttrBuilder AB; |
| 564 | AB.addAttribute(Attribute::Cold); |
| 565 | Invoke->setAttributes(AttributeSet::get(C, AttributeSet::FunctionIndex, AB)); |
| 566 | Invoke->setDebugLoc(DebugLoc(MDNode::get(C, None))); |
| 567 | |
| 568 | OperandBundleDef NewBundle("after", ConstantInt::get(Int32Ty, 7)); |
Joseph Tremoulet | f6cc7e6 | 2016-01-15 15:08:36 +0000 | [diff] [blame] | 569 | std::unique_ptr<InvokeInst> Clone( |
| 570 | InvokeInst::Create(Invoke.get(), NewBundle)); |
Joseph Tremoulet | bba70e4 | 2016-01-14 06:21:42 +0000 | [diff] [blame] | 571 | EXPECT_EQ(Invoke->getNormalDest(), Clone->getNormalDest()); |
| 572 | EXPECT_EQ(Invoke->getUnwindDest(), Clone->getUnwindDest()); |
| 573 | EXPECT_EQ(Invoke->getNumArgOperands(), Clone->getNumArgOperands()); |
| 574 | EXPECT_EQ(Invoke->getArgOperand(0), Clone->getArgOperand(0)); |
| 575 | EXPECT_EQ(Invoke->getCallingConv(), Clone->getCallingConv()); |
| 576 | EXPECT_TRUE(Clone->hasFnAttr(Attribute::AttrKind::Cold)); |
| 577 | EXPECT_EQ(Invoke->getDebugLoc(), Clone->getDebugLoc()); |
NAKAMURA Takumi | 3557b88 | 2016-01-14 09:21:49 +0000 | [diff] [blame] | 578 | EXPECT_EQ(Clone->getNumOperandBundles(), 1U); |
Joseph Tremoulet | bba70e4 | 2016-01-14 06:21:42 +0000 | [diff] [blame] | 579 | EXPECT_TRUE(Clone->getOperandBundle("after").hasValue()); |
Joseph Tremoulet | bba70e4 | 2016-01-14 06:21:42 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | } // end anonymous namespace |
| 583 | } // end namespace llvm |