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 | |
Vedant Kumar | 83a6451 | 2018-06-19 23:42:17 +0000 | [diff] [blame] | 10 | #include "llvm/AsmParser/Parser.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 11 | #include "llvm/IR/Instructions.h" |
Chandler Carruth | 5a88dda | 2012-12-04 10:23:08 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/STLExtras.h" |
| 13 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 14 | #include "llvm/IR/BasicBlock.h" |
| 15 | #include "llvm/IR/Constants.h" |
| 16 | #include "llvm/IR/DataLayout.h" |
| 17 | #include "llvm/IR/DerivedTypes.h" |
Eli Bendersky | 1003e8f | 2014-03-26 20:41:15 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Function.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 19 | #include "llvm/IR/IRBuilder.h" |
| 20 | #include "llvm/IR/LLVMContext.h" |
| 21 | #include "llvm/IR/MDBuilder.h" |
Eli Bendersky | 1003e8f | 2014-03-26 20:41:15 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Module.h" |
Sanjoy Das | 9e36975 | 2017-02-23 22:50:52 +0000 | [diff] [blame] | 23 | #include "llvm/IR/NoFolder.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Operator.h" |
Vedant Kumar | 83a6451 | 2018-06-19 23:42:17 +0000 | [diff] [blame] | 25 | #include "llvm/Support/SourceMgr.h" |
Zvi Rackover | f4d18ab | 2017-05-08 12:40:18 +0000 | [diff] [blame] | 26 | #include "gmock/gmock-matchers.h" |
Gabor Greif | 5a7069a | 2010-03-16 09:55:46 +0000 | [diff] [blame] | 27 | #include "gtest/gtest.h" |
Eli Bendersky | 1003e8f | 2014-03-26 20:41:15 +0000 | [diff] [blame] | 28 | #include <memory> |
Gabor Greif | 5a7069a | 2010-03-16 09:55:46 +0000 | [diff] [blame] | 29 | |
| 30 | namespace llvm { |
| 31 | namespace { |
| 32 | |
Vedant Kumar | 83a6451 | 2018-06-19 23:42:17 +0000 | [diff] [blame] | 33 | static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) { |
| 34 | SMDiagnostic Err; |
| 35 | std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C); |
| 36 | if (!Mod) |
| 37 | Err.print("InstructionsTests", errs()); |
| 38 | return Mod; |
| 39 | } |
| 40 | |
Gabor Greif | 138acfe | 2010-03-16 10:59:48 +0000 | [diff] [blame] | 41 | TEST(InstructionsTest, ReturnInst) { |
Mehdi Amini | 8be7707 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 42 | LLVMContext C; |
Gabor Greif | 5a7069a | 2010-03-16 09:55:46 +0000 | [diff] [blame] | 43 | |
Gabor Greif | 138acfe | 2010-03-16 10:59:48 +0000 | [diff] [blame] | 44 | // test for PR6589 |
Gabor Greif | 5a7069a | 2010-03-16 09:55:46 +0000 | [diff] [blame] | 45 | const ReturnInst* r0 = ReturnInst::Create(C); |
Gabor Greif | d165e1a | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 46 | EXPECT_EQ(r0->getNumOperands(), 0U); |
Gabor Greif | 138acfe | 2010-03-16 10:59:48 +0000 | [diff] [blame] | 47 | EXPECT_EQ(r0->op_begin(), r0->op_end()); |
Gabor Greif | 22385eb | 2010-03-16 11:24:53 +0000 | [diff] [blame] | 48 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 49 | IntegerType* Int1 = IntegerType::get(C, 1); |
Gabor Greif | 22385eb | 2010-03-16 11:24:53 +0000 | [diff] [blame] | 50 | Constant* One = ConstantInt::get(Int1, 1, true); |
| 51 | const ReturnInst* r1 = ReturnInst::Create(C, One); |
John McCall | f5ec9b5 | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 52 | EXPECT_EQ(1U, r1->getNumOperands()); |
Gabor Greif | 22385eb | 2010-03-16 11:24:53 +0000 | [diff] [blame] | 53 | User::const_op_iterator b(r1->op_begin()); |
John McCall | f5ec9b5 | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 54 | EXPECT_NE(r1->op_end(), b); |
| 55 | EXPECT_EQ(One, *b); |
| 56 | EXPECT_EQ(One, r1->getOperand(0)); |
Gabor Greif | 22385eb | 2010-03-16 11:24:53 +0000 | [diff] [blame] | 57 | ++b; |
John McCall | f5ec9b5 | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 58 | EXPECT_EQ(r1->op_end(), b); |
Gabor Greif | 642c066 | 2010-03-16 12:32:03 +0000 | [diff] [blame] | 59 | |
| 60 | // clean up |
| 61 | delete r0; |
| 62 | delete r1; |
Gabor Greif | 5a7069a | 2010-03-16 09:55:46 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Eli Bendersky | d8f4993 | 2014-03-26 21:46:24 +0000 | [diff] [blame] | 65 | // Test fixture that provides a module and a single function within it. Useful |
| 66 | // for tests that need to refer to the function in some way. |
| 67 | class ModuleWithFunctionTest : public testing::Test { |
| 68 | protected: |
NAKAMURA Takumi | da56c39 | 2014-03-27 11:32:41 +0000 | [diff] [blame] | 69 | ModuleWithFunctionTest() : M(new Module("MyModule", Ctx)) { |
NAKAMURA Takumi | c112990 | 2014-03-27 11:38:28 +0000 | [diff] [blame] | 70 | FArgTypes.push_back(Type::getInt8Ty(Ctx)); |
| 71 | FArgTypes.push_back(Type::getInt32Ty(Ctx)); |
| 72 | FArgTypes.push_back(Type::getInt64Ty(Ctx)); |
Eli Bendersky | d8f4993 | 2014-03-26 21:46:24 +0000 | [diff] [blame] | 73 | FunctionType *FTy = |
| 74 | FunctionType::get(Type::getVoidTy(Ctx), FArgTypes, false); |
| 75 | F = Function::Create(FTy, Function::ExternalLinkage, "", M.get()); |
| 76 | } |
Eli Bendersky | 1003e8f | 2014-03-26 20:41:15 +0000 | [diff] [blame] | 77 | |
Eli Bendersky | d8f4993 | 2014-03-26 21:46:24 +0000 | [diff] [blame] | 78 | LLVMContext Ctx; |
| 79 | std::unique_ptr<Module> M; |
NAKAMURA Takumi | 2b8b903 | 2014-03-27 11:33:11 +0000 | [diff] [blame] | 80 | SmallVector<Type *, 3> FArgTypes; |
Eli Bendersky | d8f4993 | 2014-03-26 21:46:24 +0000 | [diff] [blame] | 81 | Function *F; |
| 82 | }; |
Eli Bendersky | 1003e8f | 2014-03-26 20:41:15 +0000 | [diff] [blame] | 83 | |
Eli Bendersky | d8f4993 | 2014-03-26 21:46:24 +0000 | [diff] [blame] | 84 | TEST_F(ModuleWithFunctionTest, CallInst) { |
| 85 | Value *Args[] = {ConstantInt::get(Type::getInt8Ty(Ctx), 20), |
| 86 | ConstantInt::get(Type::getInt32Ty(Ctx), 9999), |
| 87 | ConstantInt::get(Type::getInt64Ty(Ctx), 42)}; |
Eli Bendersky | 927eb31 | 2014-03-26 21:11:34 +0000 | [diff] [blame] | 88 | std::unique_ptr<CallInst> Call(CallInst::Create(F, Args)); |
Eli Bendersky | 1003e8f | 2014-03-26 20:41:15 +0000 | [diff] [blame] | 89 | |
| 90 | // Make sure iteration over a call's arguments works as expected. |
| 91 | unsigned Idx = 0; |
| 92 | for (Value *Arg : Call->arg_operands()) { |
Eli Bendersky | d8f4993 | 2014-03-26 21:46:24 +0000 | [diff] [blame] | 93 | EXPECT_EQ(FArgTypes[Idx], Arg->getType()); |
Eli Bendersky | 1003e8f | 2014-03-26 20:41:15 +0000 | [diff] [blame] | 94 | EXPECT_EQ(Call->getArgOperand(Idx)->getType(), Arg->getType()); |
| 95 | Idx++; |
| 96 | } |
| 97 | } |
| 98 | |
Eli Bendersky | d8f4993 | 2014-03-26 21:46:24 +0000 | [diff] [blame] | 99 | TEST_F(ModuleWithFunctionTest, InvokeInst) { |
| 100 | BasicBlock *BB1 = BasicBlock::Create(Ctx, "", F); |
| 101 | BasicBlock *BB2 = BasicBlock::Create(Ctx, "", F); |
| 102 | |
| 103 | Value *Args[] = {ConstantInt::get(Type::getInt8Ty(Ctx), 20), |
| 104 | ConstantInt::get(Type::getInt32Ty(Ctx), 9999), |
| 105 | ConstantInt::get(Type::getInt64Ty(Ctx), 42)}; |
| 106 | std::unique_ptr<InvokeInst> Invoke(InvokeInst::Create(F, BB1, BB2, Args)); |
| 107 | |
| 108 | // Make sure iteration over invoke's arguments works as expected. |
| 109 | unsigned Idx = 0; |
| 110 | for (Value *Arg : Invoke->arg_operands()) { |
| 111 | EXPECT_EQ(FArgTypes[Idx], Arg->getType()); |
| 112 | EXPECT_EQ(Invoke->getArgOperand(Idx)->getType(), Arg->getType()); |
| 113 | Idx++; |
| 114 | } |
| 115 | } |
| 116 | |
Gabor Greif | d165e1a | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 117 | TEST(InstructionsTest, BranchInst) { |
Mehdi Amini | 8be7707 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 118 | LLVMContext C; |
Gabor Greif | d165e1a | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 119 | |
| 120 | // Make a BasicBlocks |
| 121 | BasicBlock* bb0 = BasicBlock::Create(C); |
| 122 | BasicBlock* bb1 = BasicBlock::Create(C); |
| 123 | |
| 124 | // Mandatory BranchInst |
| 125 | const BranchInst* b0 = BranchInst::Create(bb0); |
| 126 | |
Gabor Greif | abf657f | 2010-03-16 15:53:58 +0000 | [diff] [blame] | 127 | EXPECT_TRUE(b0->isUnconditional()); |
| 128 | EXPECT_FALSE(b0->isConditional()); |
John McCall | f5ec9b5 | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 129 | EXPECT_EQ(1U, b0->getNumSuccessors()); |
Gabor Greif | abf657f | 2010-03-16 15:53:58 +0000 | [diff] [blame] | 130 | |
Gabor Greif | d165e1a | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 131 | // check num operands |
John McCall | f5ec9b5 | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 132 | EXPECT_EQ(1U, b0->getNumOperands()); |
Gabor Greif | d165e1a | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 133 | |
| 134 | EXPECT_NE(b0->op_begin(), b0->op_end()); |
Benjamin Kramer | d628f19 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 135 | EXPECT_EQ(b0->op_end(), std::next(b0->op_begin())); |
Gabor Greif | abf657f | 2010-03-16 15:53:58 +0000 | [diff] [blame] | 136 | |
Benjamin Kramer | d628f19 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 137 | EXPECT_EQ(b0->op_end(), std::next(b0->op_begin())); |
Gabor Greif | d165e1a | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 138 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 139 | IntegerType* Int1 = IntegerType::get(C, 1); |
Gabor Greif | d165e1a | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 140 | Constant* One = ConstantInt::get(Int1, 1, true); |
| 141 | |
| 142 | // Conditional BranchInst |
| 143 | BranchInst* b1 = BranchInst::Create(bb0, bb1, One); |
| 144 | |
Gabor Greif | abf657f | 2010-03-16 15:53:58 +0000 | [diff] [blame] | 145 | EXPECT_FALSE(b1->isUnconditional()); |
| 146 | EXPECT_TRUE(b1->isConditional()); |
John McCall | f5ec9b5 | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 147 | EXPECT_EQ(2U, b1->getNumSuccessors()); |
Gabor Greif | abf657f | 2010-03-16 15:53:58 +0000 | [diff] [blame] | 148 | |
Gabor Greif | d165e1a | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 149 | // check num operands |
John McCall | f5ec9b5 | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 150 | EXPECT_EQ(3U, b1->getNumOperands()); |
Gabor Greif | d165e1a | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 151 | |
| 152 | User::const_op_iterator b(b1->op_begin()); |
| 153 | |
| 154 | // check COND |
| 155 | EXPECT_NE(b, b1->op_end()); |
John McCall | f5ec9b5 | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 156 | EXPECT_EQ(One, *b); |
| 157 | EXPECT_EQ(One, b1->getOperand(0)); |
| 158 | EXPECT_EQ(One, b1->getCondition()); |
Gabor Greif | d165e1a | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 159 | ++b; |
| 160 | |
| 161 | // check ELSE |
John McCall | f5ec9b5 | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 162 | EXPECT_EQ(bb1, *b); |
| 163 | EXPECT_EQ(bb1, b1->getOperand(1)); |
| 164 | EXPECT_EQ(bb1, b1->getSuccessor(1)); |
Gabor Greif | d165e1a | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 165 | ++b; |
| 166 | |
| 167 | // check THEN |
John McCall | f5ec9b5 | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 168 | EXPECT_EQ(bb0, *b); |
| 169 | EXPECT_EQ(bb0, b1->getOperand(2)); |
| 170 | EXPECT_EQ(bb0, b1->getSuccessor(0)); |
Gabor Greif | d165e1a | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 171 | ++b; |
| 172 | |
John McCall | f5ec9b5 | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 173 | EXPECT_EQ(b1->op_end(), b); |
Gabor Greif | d165e1a | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 174 | |
Gabor Greif | d165e1a | 2010-03-16 15:26:09 +0000 | [diff] [blame] | 175 | // clean up |
| 176 | delete b0; |
| 177 | delete b1; |
| 178 | |
| 179 | delete bb0; |
| 180 | delete bb1; |
| 181 | } |
| 182 | |
Duncan Sands | 6079465 | 2011-04-01 03:34:54 +0000 | [diff] [blame] | 183 | TEST(InstructionsTest, CastInst) { |
Mehdi Amini | 8be7707 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 184 | LLVMContext C; |
Duncan Sands | 6079465 | 2011-04-01 03:34:54 +0000 | [diff] [blame] | 185 | |
Matt Arsenault | f34dc42 | 2013-07-30 20:45:05 +0000 | [diff] [blame] | 186 | Type *Int8Ty = Type::getInt8Ty(C); |
| 187 | Type *Int16Ty = Type::getInt16Ty(C); |
| 188 | Type *Int32Ty = Type::getInt32Ty(C); |
| 189 | Type *Int64Ty = Type::getInt64Ty(C); |
| 190 | Type *V8x8Ty = VectorType::get(Int8Ty, 8); |
| 191 | Type *V8x64Ty = VectorType::get(Int64Ty, 8); |
| 192 | Type *X86MMXTy = Type::getX86_MMXTy(C); |
| 193 | |
| 194 | Type *HalfTy = Type::getHalfTy(C); |
| 195 | Type *FloatTy = Type::getFloatTy(C); |
| 196 | Type *DoubleTy = Type::getDoubleTy(C); |
| 197 | |
| 198 | Type *V2Int32Ty = VectorType::get(Int32Ty, 2); |
| 199 | Type *V2Int64Ty = VectorType::get(Int64Ty, 2); |
| 200 | Type *V4Int16Ty = VectorType::get(Int16Ty, 4); |
| 201 | |
| 202 | Type *Int32PtrTy = PointerType::get(Int32Ty, 0); |
| 203 | Type *Int64PtrTy = PointerType::get(Int64Ty, 0); |
| 204 | |
| 205 | Type *Int32PtrAS1Ty = PointerType::get(Int32Ty, 1); |
| 206 | Type *Int64PtrAS1Ty = PointerType::get(Int64Ty, 1); |
| 207 | |
| 208 | Type *V2Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 2); |
| 209 | Type *V2Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 2); |
| 210 | Type *V4Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 4); |
| 211 | Type *V4Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 4); |
| 212 | |
| 213 | Type *V2Int64PtrTy = VectorType::get(Int64PtrTy, 2); |
| 214 | Type *V2Int32PtrTy = VectorType::get(Int32PtrTy, 2); |
Matt Arsenault | 79e3fb5 | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 215 | Type *V4Int32PtrTy = VectorType::get(Int32PtrTy, 4); |
Duncan Sands | 6079465 | 2011-04-01 03:34:54 +0000 | [diff] [blame] | 216 | |
Duncan Sands | 117feba | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 217 | const Constant* c8 = Constant::getNullValue(V8x8Ty); |
| 218 | const Constant* c64 = Constant::getNullValue(V8x64Ty); |
| 219 | |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 220 | const Constant *v2ptr32 = Constant::getNullValue(V2Int32PtrTy); |
| 221 | |
Matt Arsenault | 485c7fd | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 222 | EXPECT_TRUE(CastInst::isCastable(V8x8Ty, X86MMXTy)); |
| 223 | EXPECT_TRUE(CastInst::isCastable(X86MMXTy, V8x8Ty)); |
| 224 | EXPECT_FALSE(CastInst::isCastable(Int64Ty, X86MMXTy)); |
| 225 | EXPECT_TRUE(CastInst::isCastable(V8x64Ty, V8x8Ty)); |
| 226 | EXPECT_TRUE(CastInst::isCastable(V8x8Ty, V8x64Ty)); |
John McCall | f5ec9b5 | 2011-08-27 19:23:22 +0000 | [diff] [blame] | 227 | EXPECT_EQ(CastInst::Trunc, CastInst::getCastOpcode(c64, true, V8x8Ty, true)); |
| 228 | EXPECT_EQ(CastInst::SExt, CastInst::getCastOpcode(c8, true, V8x64Ty, true)); |
Matt Arsenault | f34dc42 | 2013-07-30 20:45:05 +0000 | [diff] [blame] | 229 | |
| 230 | EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, X86MMXTy)); |
| 231 | EXPECT_FALSE(CastInst::isBitCastable(X86MMXTy, V8x8Ty)); |
| 232 | EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, X86MMXTy)); |
| 233 | EXPECT_FALSE(CastInst::isBitCastable(V8x64Ty, V8x8Ty)); |
| 234 | EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, V8x64Ty)); |
| 235 | |
| 236 | // Check address space casts are rejected since we don't know the sizes here |
| 237 | EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, Int32PtrAS1Ty)); |
| 238 | EXPECT_FALSE(CastInst::isBitCastable(Int32PtrAS1Ty, Int32PtrTy)); |
| 239 | EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, V2Int32PtrAS1Ty)); |
| 240 | EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int32PtrTy)); |
| 241 | EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int64PtrAS1Ty)); |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 242 | EXPECT_TRUE(CastInst::isCastable(V2Int32PtrAS1Ty, V2Int32PtrTy)); |
| 243 | EXPECT_EQ(CastInst::AddrSpaceCast, CastInst::getCastOpcode(v2ptr32, true, |
| 244 | V2Int32PtrAS1Ty, |
| 245 | true)); |
Matt Arsenault | f34dc42 | 2013-07-30 20:45:05 +0000 | [diff] [blame] | 246 | |
| 247 | // Test mismatched number of elements for pointers |
| 248 | EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int64PtrAS1Ty)); |
| 249 | EXPECT_FALSE(CastInst::isBitCastable(V4Int64PtrAS1Ty, V2Int32PtrAS1Ty)); |
| 250 | EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int32PtrAS1Ty)); |
| 251 | EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, V2Int32PtrTy)); |
| 252 | EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int32PtrTy)); |
| 253 | |
| 254 | EXPECT_TRUE(CastInst::isBitCastable(Int32PtrTy, Int64PtrTy)); |
| 255 | EXPECT_FALSE(CastInst::isBitCastable(DoubleTy, FloatTy)); |
| 256 | EXPECT_FALSE(CastInst::isBitCastable(FloatTy, DoubleTy)); |
| 257 | EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy)); |
| 258 | EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy)); |
| 259 | EXPECT_TRUE(CastInst::isBitCastable(FloatTy, Int32Ty)); |
| 260 | EXPECT_TRUE(CastInst::isBitCastable(Int16Ty, HalfTy)); |
| 261 | EXPECT_TRUE(CastInst::isBitCastable(Int32Ty, FloatTy)); |
| 262 | EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, Int64Ty)); |
| 263 | |
| 264 | EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, V4Int16Ty)); |
| 265 | EXPECT_FALSE(CastInst::isBitCastable(Int32Ty, Int64Ty)); |
| 266 | EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, Int32Ty)); |
| 267 | |
| 268 | EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int64Ty)); |
| 269 | EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, V2Int32PtrTy)); |
| 270 | EXPECT_TRUE(CastInst::isBitCastable(V2Int64PtrTy, V2Int32PtrTy)); |
| 271 | EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrTy, V2Int64PtrTy)); |
| 272 | EXPECT_FALSE(CastInst::isBitCastable(V2Int32Ty, V2Int64Ty)); |
| 273 | EXPECT_FALSE(CastInst::isBitCastable(V2Int64Ty, V2Int32Ty)); |
Matt Arsenault | 1bf0ec4 | 2013-07-31 00:17:33 +0000 | [diff] [blame] | 274 | |
| 275 | |
Matt Arsenault | 79e3fb5 | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 276 | EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast, |
| 277 | Constant::getNullValue(V4Int32PtrTy), |
| 278 | V2Int32PtrTy)); |
| 279 | EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast, |
| 280 | Constant::getNullValue(V2Int32PtrTy), |
| 281 | V4Int32PtrTy)); |
| 282 | |
| 283 | EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast, |
| 284 | Constant::getNullValue(V4Int32PtrAS1Ty), |
| 285 | V2Int32PtrTy)); |
| 286 | EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast, |
| 287 | Constant::getNullValue(V2Int32PtrTy), |
| 288 | V4Int32PtrAS1Ty)); |
| 289 | |
| 290 | |
Matt Arsenault | 1bf0ec4 | 2013-07-31 00:17:33 +0000 | [diff] [blame] | 291 | // Check that assertion is not hit when creating a cast with a vector of |
| 292 | // pointers |
| 293 | // First form |
| 294 | BasicBlock *BB = BasicBlock::Create(C); |
| 295 | Constant *NullV2I32Ptr = Constant::getNullValue(V2Int32PtrTy); |
Mehdi Amini | 8be7707 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 296 | auto Inst1 = CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty, "foo", BB); |
Matt Arsenault | 1bf0ec4 | 2013-07-31 00:17:33 +0000 | [diff] [blame] | 297 | |
| 298 | // Second form |
Mehdi Amini | 8be7707 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 299 | auto Inst2 = CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty); |
| 300 | |
| 301 | delete Inst2; |
| 302 | Inst1->eraseFromParent(); |
| 303 | delete BB; |
Duncan Sands | 6079465 | 2011-04-01 03:34:54 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Nadav Rotem | 1608769 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 306 | TEST(InstructionsTest, VectorGep) { |
Mehdi Amini | 8be7707 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 307 | LLVMContext C; |
Nadav Rotem | 1608769 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 308 | |
| 309 | // Type Definitions |
David Blaikie | f0295e4 | 2015-03-14 21:40:10 +0000 | [diff] [blame] | 310 | Type *I8Ty = IntegerType::get(C, 8); |
| 311 | Type *I32Ty = IntegerType::get(C, 32); |
| 312 | PointerType *Ptri8Ty = PointerType::get(I8Ty, 0); |
| 313 | PointerType *Ptri32Ty = PointerType::get(I32Ty, 0); |
Nadav Rotem | 1608769 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 314 | |
| 315 | VectorType *V2xi8PTy = VectorType::get(Ptri8Ty, 2); |
| 316 | VectorType *V2xi32PTy = VectorType::get(Ptri32Ty, 2); |
| 317 | |
| 318 | // Test different aspects of the vector-of-pointers type |
| 319 | // and GEPs which use this type. |
| 320 | ConstantInt *Ci32a = ConstantInt::get(C, APInt(32, 1492)); |
| 321 | ConstantInt *Ci32b = ConstantInt::get(C, APInt(32, 1948)); |
| 322 | std::vector<Constant*> ConstVa(2, Ci32a); |
| 323 | std::vector<Constant*> ConstVb(2, Ci32b); |
| 324 | Constant *C2xi32a = ConstantVector::get(ConstVa); |
| 325 | Constant *C2xi32b = ConstantVector::get(ConstVb); |
| 326 | |
| 327 | CastInst *PtrVecA = new IntToPtrInst(C2xi32a, V2xi32PTy); |
| 328 | CastInst *PtrVecB = new IntToPtrInst(C2xi32b, V2xi32PTy); |
| 329 | |
| 330 | ICmpInst *ICmp0 = new ICmpInst(ICmpInst::ICMP_SGT, PtrVecA, PtrVecB); |
| 331 | ICmpInst *ICmp1 = new ICmpInst(ICmpInst::ICMP_ULT, PtrVecA, PtrVecB); |
| 332 | EXPECT_NE(ICmp0, ICmp1); // suppress warning. |
| 333 | |
Evgeniy Stepanov | 4802b9d | 2013-01-16 14:38:50 +0000 | [diff] [blame] | 334 | BasicBlock* BB0 = BasicBlock::Create(C); |
| 335 | // Test InsertAtEnd ICmpInst constructor. |
| 336 | ICmpInst *ICmp2 = new ICmpInst(*BB0, ICmpInst::ICMP_SGE, PtrVecA, PtrVecB); |
| 337 | EXPECT_NE(ICmp0, ICmp2); // suppress warning. |
| 338 | |
David Blaikie | f0295e4 | 2015-03-14 21:40:10 +0000 | [diff] [blame] | 339 | GetElementPtrInst *Gep0 = GetElementPtrInst::Create(I32Ty, PtrVecA, C2xi32a); |
| 340 | GetElementPtrInst *Gep1 = GetElementPtrInst::Create(I32Ty, PtrVecA, C2xi32b); |
| 341 | GetElementPtrInst *Gep2 = GetElementPtrInst::Create(I32Ty, PtrVecB, C2xi32a); |
| 342 | GetElementPtrInst *Gep3 = GetElementPtrInst::Create(I32Ty, PtrVecB, C2xi32b); |
Nadav Rotem | 1608769 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 343 | |
| 344 | CastInst *BTC0 = new BitCastInst(Gep0, V2xi8PTy); |
| 345 | CastInst *BTC1 = new BitCastInst(Gep1, V2xi8PTy); |
| 346 | CastInst *BTC2 = new BitCastInst(Gep2, V2xi8PTy); |
| 347 | CastInst *BTC3 = new BitCastInst(Gep3, V2xi8PTy); |
| 348 | |
| 349 | Value *S0 = BTC0->stripPointerCasts(); |
| 350 | Value *S1 = BTC1->stripPointerCasts(); |
| 351 | Value *S2 = BTC2->stripPointerCasts(); |
| 352 | Value *S3 = BTC3->stripPointerCasts(); |
| 353 | |
| 354 | EXPECT_NE(S0, Gep0); |
| 355 | EXPECT_NE(S1, Gep1); |
| 356 | EXPECT_NE(S2, Gep2); |
| 357 | EXPECT_NE(S3, Gep3); |
| 358 | |
| 359 | int64_t Offset; |
Micah Villmow | 791cfc2 | 2012-10-08 16:39:34 +0000 | [diff] [blame] | 360 | 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 | f343bc9 | 2013-12-13 18:56:34 +0000 | [diff] [blame] | 361 | "2:32:32-f64:64:64-v64:64:64-v128:128:128-a:0:64-s:64:64-f80" |
Nadav Rotem | 1608769 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 362 | ":128:128-n8:16:32:64-S128"); |
| 363 | // Make sure we don't crash |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 364 | GetPointerBaseWithConstantOffset(Gep0, Offset, TD); |
| 365 | GetPointerBaseWithConstantOffset(Gep1, Offset, TD); |
| 366 | GetPointerBaseWithConstantOffset(Gep2, Offset, TD); |
| 367 | GetPointerBaseWithConstantOffset(Gep3, Offset, TD); |
Nadav Rotem | 1608769 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 368 | |
| 369 | // Gep of Geps |
David Blaikie | f0295e4 | 2015-03-14 21:40:10 +0000 | [diff] [blame] | 370 | GetElementPtrInst *GepII0 = GetElementPtrInst::Create(I32Ty, Gep0, C2xi32b); |
| 371 | GetElementPtrInst *GepII1 = GetElementPtrInst::Create(I32Ty, Gep1, C2xi32a); |
| 372 | GetElementPtrInst *GepII2 = GetElementPtrInst::Create(I32Ty, Gep2, C2xi32b); |
| 373 | GetElementPtrInst *GepII3 = GetElementPtrInst::Create(I32Ty, Gep3, C2xi32a); |
Nadav Rotem | 1608769 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 374 | |
| 375 | EXPECT_EQ(GepII0->getNumIndices(), 1u); |
| 376 | EXPECT_EQ(GepII1->getNumIndices(), 1u); |
| 377 | EXPECT_EQ(GepII2->getNumIndices(), 1u); |
| 378 | EXPECT_EQ(GepII3->getNumIndices(), 1u); |
| 379 | |
| 380 | EXPECT_FALSE(GepII0->hasAllZeroIndices()); |
| 381 | EXPECT_FALSE(GepII1->hasAllZeroIndices()); |
| 382 | EXPECT_FALSE(GepII2->hasAllZeroIndices()); |
| 383 | EXPECT_FALSE(GepII3->hasAllZeroIndices()); |
| 384 | |
| 385 | delete GepII0; |
| 386 | delete GepII1; |
| 387 | delete GepII2; |
| 388 | delete GepII3; |
| 389 | |
| 390 | delete BTC0; |
| 391 | delete BTC1; |
| 392 | delete BTC2; |
| 393 | delete BTC3; |
| 394 | |
| 395 | delete Gep0; |
| 396 | delete Gep1; |
| 397 | delete Gep2; |
| 398 | delete Gep3; |
| 399 | |
Evgeniy Stepanov | 4802b9d | 2013-01-16 14:38:50 +0000 | [diff] [blame] | 400 | ICmp2->eraseFromParent(); |
| 401 | delete BB0; |
| 402 | |
Nadav Rotem | 1608769 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 403 | delete ICmp0; |
| 404 | delete ICmp1; |
| 405 | delete PtrVecA; |
| 406 | delete PtrVecB; |
| 407 | } |
| 408 | |
Duncan Sands | 8883c43 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 409 | TEST(InstructionsTest, FPMathOperator) { |
Mehdi Amini | 8be7707 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 410 | LLVMContext Context; |
Duncan Sands | 8883c43 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 411 | IRBuilder<> Builder(Context); |
| 412 | MDBuilder MDHelper(Context); |
| 413 | Instruction *I = Builder.CreatePHI(Builder.getDoubleTy(), 0); |
| 414 | MDNode *MD1 = MDHelper.createFPMath(1.0); |
Duncan Sands | 8883c43 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 415 | Value *V1 = Builder.CreateFAdd(I, I, "", MD1); |
Duncan Sands | 8883c43 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 416 | EXPECT_TRUE(isa<FPMathOperator>(V1)); |
Duncan Sands | 8883c43 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 417 | FPMathOperator *O1 = cast<FPMathOperator>(V1); |
Duncan Sands | 8883c43 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 418 | EXPECT_EQ(O1->getFPAccuracy(), 1.0); |
Reid Kleckner | 816047d | 2017-05-18 17:24:10 +0000 | [diff] [blame] | 419 | V1->deleteValue(); |
| 420 | I->deleteValue(); |
Duncan Sands | 8883c43 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Duncan Sands | 446cf94 | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 423 | |
| 424 | TEST(InstructionsTest, isEliminableCastPair) { |
Mehdi Amini | 8be7707 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 425 | LLVMContext C; |
Duncan Sands | 446cf94 | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 426 | |
Matt Arsenault | 3181f59 | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 427 | Type* Int16Ty = Type::getInt16Ty(C); |
Duncan Sands | 446cf94 | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 428 | Type* Int32Ty = Type::getInt32Ty(C); |
| 429 | Type* Int64Ty = Type::getInt64Ty(C); |
| 430 | Type* Int64PtrTy = Type::getInt64PtrTy(C); |
| 431 | |
| 432 | // Source and destination pointers have same size -> bitcast. |
| 433 | EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt, |
| 434 | CastInst::IntToPtr, |
| 435 | Int64PtrTy, Int64Ty, Int64PtrTy, |
Craig Topper | b177041 | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 436 | Int32Ty, nullptr, Int32Ty), |
Duncan Sands | 446cf94 | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 437 | CastInst::BitCast); |
| 438 | |
Matt Arsenault | 3181f59 | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 439 | // Source and destination have unknown sizes, but the same address space and |
| 440 | // the intermediate int is the maximum pointer size -> bitcast |
Duncan Sands | 446cf94 | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 441 | EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt, |
| 442 | CastInst::IntToPtr, |
| 443 | Int64PtrTy, Int64Ty, Int64PtrTy, |
Craig Topper | b177041 | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 444 | nullptr, nullptr, nullptr), |
Matt Arsenault | 3181f59 | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 445 | CastInst::BitCast); |
| 446 | |
| 447 | // Source and destination have unknown sizes, but the same address space and |
| 448 | // the intermediate int is not the maximum pointer size -> nothing |
| 449 | EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt, |
| 450 | CastInst::IntToPtr, |
| 451 | Int64PtrTy, Int32Ty, Int64PtrTy, |
Craig Topper | b177041 | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 452 | nullptr, nullptr, nullptr), |
Duncan Sands | 446cf94 | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 453 | 0U); |
| 454 | |
| 455 | // Middle pointer big enough -> bitcast. |
| 456 | EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr, |
| 457 | CastInst::PtrToInt, |
| 458 | Int64Ty, Int64PtrTy, Int64Ty, |
Craig Topper | b177041 | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 459 | nullptr, Int64Ty, nullptr), |
Duncan Sands | 446cf94 | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 460 | CastInst::BitCast); |
| 461 | |
| 462 | // Middle pointer too small -> fail. |
| 463 | EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr, |
| 464 | CastInst::PtrToInt, |
| 465 | Int64Ty, Int64PtrTy, Int64Ty, |
Craig Topper | b177041 | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 466 | nullptr, Int32Ty, nullptr), |
Duncan Sands | 446cf94 | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 467 | 0U); |
Matt Arsenault | 3181f59 | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 468 | |
Matt Arsenault | 3181f59 | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 469 | // Test that we don't eliminate bitcasts between different address spaces, |
| 470 | // or if we don't have available pointer size information. |
| 471 | DataLayout DL("e-p:32:32:32-p1:16:16:16-p2:64:64:64-i1:8:8-i8:8:8-i16:16:16" |
| 472 | "-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64" |
Rafael Espindola | f343bc9 | 2013-12-13 18:56:34 +0000 | [diff] [blame] | 473 | "-v128:128:128-a:0:64-s:64:64-f80:128:128-n8:16:32:64-S128"); |
Matt Arsenault | 3181f59 | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 474 | |
| 475 | Type* Int64PtrTyAS1 = Type::getInt64PtrTy(C, 1); |
| 476 | Type* Int64PtrTyAS2 = Type::getInt64PtrTy(C, 2); |
| 477 | |
| 478 | IntegerType *Int16SizePtr = DL.getIntPtrType(C, 1); |
| 479 | IntegerType *Int64SizePtr = DL.getIntPtrType(C, 2); |
| 480 | |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 481 | // Cannot simplify inttoptr, addrspacecast |
Matt Arsenault | 3181f59 | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 482 | EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr, |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 483 | CastInst::AddrSpaceCast, |
Matt Arsenault | 3181f59 | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 484 | Int16Ty, Int64PtrTyAS1, Int64PtrTyAS2, |
Craig Topper | b177041 | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 485 | nullptr, Int16SizePtr, Int64SizePtr), |
Matt Arsenault | 3181f59 | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 486 | 0U); |
| 487 | |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 488 | // Cannot simplify addrspacecast, ptrtoint |
| 489 | EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::AddrSpaceCast, |
| 490 | CastInst::PtrToInt, |
| 491 | Int64PtrTyAS1, Int64PtrTyAS2, Int16Ty, |
Craig Topper | b177041 | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 492 | Int64SizePtr, Int16SizePtr, nullptr), |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 493 | 0U); |
| 494 | |
Matt Arsenault | 3181f59 | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 495 | // Pass since the bitcast address spaces are the same |
| 496 | EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr, |
| 497 | CastInst::BitCast, |
| 498 | Int16Ty, Int64PtrTyAS1, Int64PtrTyAS1, |
Craig Topper | b177041 | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 499 | nullptr, nullptr, nullptr), |
Matt Arsenault | 3181f59 | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 500 | CastInst::IntToPtr); |
| 501 | |
Duncan Sands | 446cf94 | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Reid Kleckner | 3949749 | 2014-05-06 20:08:20 +0000 | [diff] [blame] | 504 | TEST(InstructionsTest, CloneCall) { |
Mehdi Amini | 8be7707 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 505 | LLVMContext C; |
Reid Kleckner | 3949749 | 2014-05-06 20:08:20 +0000 | [diff] [blame] | 506 | Type *Int32Ty = Type::getInt32Ty(C); |
| 507 | Type *ArgTys[] = {Int32Ty, Int32Ty, Int32Ty}; |
| 508 | Type *FnTy = FunctionType::get(Int32Ty, ArgTys, /*isVarArg=*/false); |
| 509 | Value *Callee = Constant::getNullValue(FnTy->getPointerTo()); |
| 510 | Value *Args[] = { |
| 511 | ConstantInt::get(Int32Ty, 1), |
| 512 | ConstantInt::get(Int32Ty, 2), |
| 513 | ConstantInt::get(Int32Ty, 3) |
| 514 | }; |
| 515 | std::unique_ptr<CallInst> Call(CallInst::Create(Callee, Args, "result")); |
| 516 | |
| 517 | // Test cloning the tail call kind. |
| 518 | CallInst::TailCallKind Kinds[] = {CallInst::TCK_None, CallInst::TCK_Tail, |
| 519 | CallInst::TCK_MustTail}; |
| 520 | for (CallInst::TailCallKind TCK : Kinds) { |
| 521 | Call->setTailCallKind(TCK); |
| 522 | std::unique_ptr<CallInst> Clone(cast<CallInst>(Call->clone())); |
| 523 | EXPECT_EQ(Call->getTailCallKind(), Clone->getTailCallKind()); |
| 524 | } |
| 525 | Call->setTailCallKind(CallInst::TCK_None); |
| 526 | |
| 527 | // Test cloning an attribute. |
| 528 | { |
| 529 | AttrBuilder AB; |
| 530 | AB.addAttribute(Attribute::ReadOnly); |
Reid Kleckner | 6707770 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 531 | Call->setAttributes( |
| 532 | AttributeList::get(C, AttributeList::FunctionIndex, AB)); |
Reid Kleckner | 3949749 | 2014-05-06 20:08:20 +0000 | [diff] [blame] | 533 | std::unique_ptr<CallInst> Clone(cast<CallInst>(Call->clone())); |
| 534 | EXPECT_TRUE(Clone->onlyReadsMemory()); |
| 535 | } |
| 536 | } |
| 537 | |
Joseph Tremoulet | 0d05e6c | 2016-01-14 06:21:42 +0000 | [diff] [blame] | 538 | TEST(InstructionsTest, AlterCallBundles) { |
Mehdi Amini | 8be7707 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 539 | LLVMContext C; |
Joseph Tremoulet | 0d05e6c | 2016-01-14 06:21:42 +0000 | [diff] [blame] | 540 | Type *Int32Ty = Type::getInt32Ty(C); |
| 541 | Type *FnTy = FunctionType::get(Int32Ty, Int32Ty, /*isVarArg=*/false); |
| 542 | Value *Callee = Constant::getNullValue(FnTy->getPointerTo()); |
| 543 | Value *Args[] = {ConstantInt::get(Int32Ty, 42)}; |
| 544 | OperandBundleDef OldBundle("before", UndefValue::get(Int32Ty)); |
| 545 | std::unique_ptr<CallInst> Call( |
| 546 | CallInst::Create(Callee, Args, OldBundle, "result")); |
| 547 | Call->setTailCallKind(CallInst::TailCallKind::TCK_NoTail); |
| 548 | AttrBuilder AB; |
| 549 | AB.addAttribute(Attribute::Cold); |
Reid Kleckner | 6707770 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 550 | Call->setAttributes(AttributeList::get(C, AttributeList::FunctionIndex, AB)); |
Joseph Tremoulet | 0d05e6c | 2016-01-14 06:21:42 +0000 | [diff] [blame] | 551 | Call->setDebugLoc(DebugLoc(MDNode::get(C, None))); |
Matt Arsenault | 3181f59 | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 552 | |
Joseph Tremoulet | 0d05e6c | 2016-01-14 06:21:42 +0000 | [diff] [blame] | 553 | OperandBundleDef NewBundle("after", ConstantInt::get(Int32Ty, 7)); |
| 554 | std::unique_ptr<CallInst> Clone(CallInst::Create(Call.get(), NewBundle)); |
| 555 | EXPECT_EQ(Call->getNumArgOperands(), Clone->getNumArgOperands()); |
| 556 | EXPECT_EQ(Call->getArgOperand(0), Clone->getArgOperand(0)); |
| 557 | EXPECT_EQ(Call->getCallingConv(), Clone->getCallingConv()); |
| 558 | EXPECT_EQ(Call->getTailCallKind(), Clone->getTailCallKind()); |
| 559 | EXPECT_TRUE(Clone->hasFnAttr(Attribute::AttrKind::Cold)); |
| 560 | EXPECT_EQ(Call->getDebugLoc(), Clone->getDebugLoc()); |
Joseph Tremoulet | 6f91687 | 2016-01-14 06:30:19 +0000 | [diff] [blame] | 561 | EXPECT_EQ(Clone->getNumOperandBundles(), 1U); |
Joseph Tremoulet | 0d05e6c | 2016-01-14 06:21:42 +0000 | [diff] [blame] | 562 | EXPECT_TRUE(Clone->getOperandBundle("after").hasValue()); |
| 563 | } |
Matt Arsenault | 3181f59 | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 564 | |
Joseph Tremoulet | 0d05e6c | 2016-01-14 06:21:42 +0000 | [diff] [blame] | 565 | TEST(InstructionsTest, AlterInvokeBundles) { |
Mehdi Amini | 8be7707 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 566 | LLVMContext C; |
Joseph Tremoulet | 0d05e6c | 2016-01-14 06:21:42 +0000 | [diff] [blame] | 567 | Type *Int32Ty = Type::getInt32Ty(C); |
| 568 | Type *FnTy = FunctionType::get(Int32Ty, Int32Ty, /*isVarArg=*/false); |
| 569 | Value *Callee = Constant::getNullValue(FnTy->getPointerTo()); |
| 570 | Value *Args[] = {ConstantInt::get(Int32Ty, 42)}; |
Joseph Tremoulet | df4beed | 2016-01-15 15:08:36 +0000 | [diff] [blame] | 571 | std::unique_ptr<BasicBlock> NormalDest(BasicBlock::Create(C)); |
| 572 | std::unique_ptr<BasicBlock> UnwindDest(BasicBlock::Create(C)); |
Joseph Tremoulet | 0d05e6c | 2016-01-14 06:21:42 +0000 | [diff] [blame] | 573 | OperandBundleDef OldBundle("before", UndefValue::get(Int32Ty)); |
Joseph Tremoulet | df4beed | 2016-01-15 15:08:36 +0000 | [diff] [blame] | 574 | std::unique_ptr<InvokeInst> Invoke(InvokeInst::Create( |
| 575 | Callee, NormalDest.get(), UnwindDest.get(), Args, OldBundle, "result")); |
Joseph Tremoulet | 0d05e6c | 2016-01-14 06:21:42 +0000 | [diff] [blame] | 576 | AttrBuilder AB; |
| 577 | AB.addAttribute(Attribute::Cold); |
Reid Kleckner | 6707770 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 578 | Invoke->setAttributes( |
| 579 | AttributeList::get(C, AttributeList::FunctionIndex, AB)); |
Joseph Tremoulet | 0d05e6c | 2016-01-14 06:21:42 +0000 | [diff] [blame] | 580 | Invoke->setDebugLoc(DebugLoc(MDNode::get(C, None))); |
| 581 | |
| 582 | OperandBundleDef NewBundle("after", ConstantInt::get(Int32Ty, 7)); |
Joseph Tremoulet | df4beed | 2016-01-15 15:08:36 +0000 | [diff] [blame] | 583 | std::unique_ptr<InvokeInst> Clone( |
| 584 | InvokeInst::Create(Invoke.get(), NewBundle)); |
Joseph Tremoulet | 0d05e6c | 2016-01-14 06:21:42 +0000 | [diff] [blame] | 585 | EXPECT_EQ(Invoke->getNormalDest(), Clone->getNormalDest()); |
| 586 | EXPECT_EQ(Invoke->getUnwindDest(), Clone->getUnwindDest()); |
| 587 | EXPECT_EQ(Invoke->getNumArgOperands(), Clone->getNumArgOperands()); |
| 588 | EXPECT_EQ(Invoke->getArgOperand(0), Clone->getArgOperand(0)); |
| 589 | EXPECT_EQ(Invoke->getCallingConv(), Clone->getCallingConv()); |
| 590 | EXPECT_TRUE(Clone->hasFnAttr(Attribute::AttrKind::Cold)); |
| 591 | EXPECT_EQ(Invoke->getDebugLoc(), Clone->getDebugLoc()); |
NAKAMURA Takumi | 92d091b | 2016-01-14 09:21:49 +0000 | [diff] [blame] | 592 | EXPECT_EQ(Clone->getNumOperandBundles(), 1U); |
Joseph Tremoulet | 0d05e6c | 2016-01-14 06:21:42 +0000 | [diff] [blame] | 593 | EXPECT_TRUE(Clone->getOperandBundle("after").hasValue()); |
Joseph Tremoulet | 0d05e6c | 2016-01-14 06:21:42 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Sanjoy Das | 9e36975 | 2017-02-23 22:50:52 +0000 | [diff] [blame] | 596 | TEST_F(ModuleWithFunctionTest, DropPoisonGeneratingFlags) { |
| 597 | auto *OnlyBB = BasicBlock::Create(Ctx, "bb", F); |
| 598 | auto *Arg0 = &*F->arg_begin(); |
| 599 | |
| 600 | IRBuilder<NoFolder> B(Ctx); |
| 601 | B.SetInsertPoint(OnlyBB); |
| 602 | |
| 603 | { |
| 604 | auto *UI = |
| 605 | cast<Instruction>(B.CreateUDiv(Arg0, Arg0, "", /*isExact*/ true)); |
| 606 | ASSERT_TRUE(UI->isExact()); |
| 607 | UI->dropPoisonGeneratingFlags(); |
| 608 | ASSERT_FALSE(UI->isExact()); |
| 609 | } |
| 610 | |
| 611 | { |
| 612 | auto *ShrI = |
| 613 | cast<Instruction>(B.CreateLShr(Arg0, Arg0, "", /*isExact*/ true)); |
| 614 | ASSERT_TRUE(ShrI->isExact()); |
| 615 | ShrI->dropPoisonGeneratingFlags(); |
| 616 | ASSERT_FALSE(ShrI->isExact()); |
| 617 | } |
| 618 | |
| 619 | { |
| 620 | auto *AI = cast<Instruction>( |
| 621 | B.CreateAdd(Arg0, Arg0, "", /*HasNUW*/ true, /*HasNSW*/ false)); |
| 622 | ASSERT_TRUE(AI->hasNoUnsignedWrap()); |
| 623 | AI->dropPoisonGeneratingFlags(); |
| 624 | ASSERT_FALSE(AI->hasNoUnsignedWrap()); |
| 625 | ASSERT_FALSE(AI->hasNoSignedWrap()); |
| 626 | } |
| 627 | |
| 628 | { |
| 629 | auto *SI = cast<Instruction>( |
| 630 | B.CreateAdd(Arg0, Arg0, "", /*HasNUW*/ false, /*HasNSW*/ true)); |
| 631 | ASSERT_TRUE(SI->hasNoSignedWrap()); |
| 632 | SI->dropPoisonGeneratingFlags(); |
| 633 | ASSERT_FALSE(SI->hasNoUnsignedWrap()); |
| 634 | ASSERT_FALSE(SI->hasNoSignedWrap()); |
| 635 | } |
| 636 | |
| 637 | { |
| 638 | auto *ShlI = cast<Instruction>( |
| 639 | B.CreateShl(Arg0, Arg0, "", /*HasNUW*/ true, /*HasNSW*/ true)); |
| 640 | ASSERT_TRUE(ShlI->hasNoSignedWrap()); |
| 641 | ASSERT_TRUE(ShlI->hasNoUnsignedWrap()); |
| 642 | ShlI->dropPoisonGeneratingFlags(); |
| 643 | ASSERT_FALSE(ShlI->hasNoUnsignedWrap()); |
| 644 | ASSERT_FALSE(ShlI->hasNoSignedWrap()); |
| 645 | } |
| 646 | |
| 647 | { |
| 648 | Value *GEPBase = Constant::getNullValue(B.getInt8PtrTy()); |
| 649 | auto *GI = cast<GetElementPtrInst>(B.CreateInBoundsGEP(GEPBase, {Arg0})); |
| 650 | ASSERT_TRUE(GI->isInBounds()); |
| 651 | GI->dropPoisonGeneratingFlags(); |
| 652 | ASSERT_FALSE(GI->isInBounds()); |
| 653 | } |
| 654 | } |
| 655 | |
Chandler Carruth | bff5a6e | 2017-02-28 08:04:20 +0000 | [diff] [blame] | 656 | TEST(InstructionsTest, GEPIndices) { |
| 657 | LLVMContext Context; |
| 658 | IRBuilder<NoFolder> Builder(Context); |
| 659 | Type *ElementTy = Builder.getInt8Ty(); |
| 660 | Type *ArrTy = ArrayType::get(ArrayType::get(ElementTy, 64), 64); |
| 661 | Value *Indices[] = { |
| 662 | Builder.getInt32(0), |
| 663 | Builder.getInt32(13), |
| 664 | Builder.getInt32(42) }; |
| 665 | |
| 666 | Value *V = Builder.CreateGEP(ArrTy, UndefValue::get(PointerType::getUnqual(ArrTy)), |
| 667 | Indices); |
| 668 | ASSERT_TRUE(isa<GetElementPtrInst>(V)); |
| 669 | |
| 670 | auto *GEPI = cast<GetElementPtrInst>(V); |
| 671 | ASSERT_NE(GEPI->idx_begin(), GEPI->idx_end()); |
| 672 | ASSERT_EQ(GEPI->idx_end(), std::next(GEPI->idx_begin(), 3)); |
| 673 | EXPECT_EQ(Indices[0], GEPI->idx_begin()[0]); |
| 674 | EXPECT_EQ(Indices[1], GEPI->idx_begin()[1]); |
| 675 | EXPECT_EQ(Indices[2], GEPI->idx_begin()[2]); |
| 676 | EXPECT_EQ(GEPI->idx_begin(), GEPI->indices().begin()); |
| 677 | EXPECT_EQ(GEPI->idx_end(), GEPI->indices().end()); |
| 678 | |
| 679 | const auto *CGEPI = GEPI; |
| 680 | ASSERT_NE(CGEPI->idx_begin(), CGEPI->idx_end()); |
| 681 | ASSERT_EQ(CGEPI->idx_end(), std::next(CGEPI->idx_begin(), 3)); |
| 682 | EXPECT_EQ(Indices[0], CGEPI->idx_begin()[0]); |
| 683 | EXPECT_EQ(Indices[1], CGEPI->idx_begin()[1]); |
| 684 | EXPECT_EQ(Indices[2], CGEPI->idx_begin()[2]); |
| 685 | EXPECT_EQ(CGEPI->idx_begin(), CGEPI->indices().begin()); |
| 686 | EXPECT_EQ(CGEPI->idx_end(), CGEPI->indices().end()); |
| 687 | |
| 688 | delete GEPI; |
| 689 | } |
| 690 | |
Chandler Carruth | ddfada2 | 2017-04-12 07:27:28 +0000 | [diff] [blame] | 691 | TEST(InstructionsTest, SwitchInst) { |
| 692 | LLVMContext C; |
| 693 | |
| 694 | std::unique_ptr<BasicBlock> BB1, BB2, BB3; |
| 695 | BB1.reset(BasicBlock::Create(C)); |
| 696 | BB2.reset(BasicBlock::Create(C)); |
| 697 | BB3.reset(BasicBlock::Create(C)); |
| 698 | |
| 699 | // We create block 0 after the others so that it gets destroyed first and |
| 700 | // clears the uses of the other basic blocks. |
| 701 | std::unique_ptr<BasicBlock> BB0(BasicBlock::Create(C)); |
| 702 | |
| 703 | auto *Int32Ty = Type::getInt32Ty(C); |
| 704 | |
| 705 | SwitchInst *SI = |
| 706 | SwitchInst::Create(UndefValue::get(Int32Ty), BB0.get(), 3, BB0.get()); |
| 707 | SI->addCase(ConstantInt::get(Int32Ty, 1), BB1.get()); |
| 708 | SI->addCase(ConstantInt::get(Int32Ty, 2), BB2.get()); |
| 709 | SI->addCase(ConstantInt::get(Int32Ty, 3), BB3.get()); |
| 710 | |
| 711 | auto CI = SI->case_begin(); |
| 712 | ASSERT_NE(CI, SI->case_end()); |
| 713 | EXPECT_EQ(1, CI->getCaseValue()->getSExtValue()); |
| 714 | EXPECT_EQ(BB1.get(), CI->getCaseSuccessor()); |
| 715 | EXPECT_EQ(2, (CI + 1)->getCaseValue()->getSExtValue()); |
| 716 | EXPECT_EQ(BB2.get(), (CI + 1)->getCaseSuccessor()); |
| 717 | EXPECT_EQ(3, (CI + 2)->getCaseValue()->getSExtValue()); |
| 718 | EXPECT_EQ(BB3.get(), (CI + 2)->getCaseSuccessor()); |
| 719 | EXPECT_EQ(CI + 1, std::next(CI)); |
| 720 | EXPECT_EQ(CI + 2, std::next(CI, 2)); |
| 721 | EXPECT_EQ(CI + 3, std::next(CI, 3)); |
| 722 | EXPECT_EQ(SI->case_end(), CI + 3); |
| 723 | EXPECT_EQ(0, CI - CI); |
| 724 | EXPECT_EQ(1, (CI + 1) - CI); |
| 725 | EXPECT_EQ(2, (CI + 2) - CI); |
| 726 | EXPECT_EQ(3, SI->case_end() - CI); |
| 727 | EXPECT_EQ(3, std::distance(CI, SI->case_end())); |
| 728 | |
| 729 | auto CCI = const_cast<const SwitchInst *>(SI)->case_begin(); |
| 730 | SwitchInst::ConstCaseIt CCE = SI->case_end(); |
| 731 | ASSERT_NE(CCI, SI->case_end()); |
| 732 | EXPECT_EQ(1, CCI->getCaseValue()->getSExtValue()); |
| 733 | EXPECT_EQ(BB1.get(), CCI->getCaseSuccessor()); |
| 734 | EXPECT_EQ(2, (CCI + 1)->getCaseValue()->getSExtValue()); |
| 735 | EXPECT_EQ(BB2.get(), (CCI + 1)->getCaseSuccessor()); |
| 736 | EXPECT_EQ(3, (CCI + 2)->getCaseValue()->getSExtValue()); |
| 737 | EXPECT_EQ(BB3.get(), (CCI + 2)->getCaseSuccessor()); |
| 738 | EXPECT_EQ(CCI + 1, std::next(CCI)); |
| 739 | EXPECT_EQ(CCI + 2, std::next(CCI, 2)); |
| 740 | EXPECT_EQ(CCI + 3, std::next(CCI, 3)); |
| 741 | EXPECT_EQ(CCE, CCI + 3); |
| 742 | EXPECT_EQ(0, CCI - CCI); |
| 743 | EXPECT_EQ(1, (CCI + 1) - CCI); |
| 744 | EXPECT_EQ(2, (CCI + 2) - CCI); |
| 745 | EXPECT_EQ(3, CCE - CCI); |
| 746 | EXPECT_EQ(3, std::distance(CCI, CCE)); |
| 747 | |
| 748 | // Make sure that the const iterator is compatible with a const auto ref. |
| 749 | const auto &Handle = *CCI; |
| 750 | EXPECT_EQ(1, Handle.getCaseValue()->getSExtValue()); |
| 751 | EXPECT_EQ(BB1.get(), Handle.getCaseSuccessor()); |
| 752 | } |
| 753 | |
Zvi Rackover | f4d18ab | 2017-05-08 12:40:18 +0000 | [diff] [blame] | 754 | TEST(InstructionsTest, CommuteShuffleMask) { |
| 755 | SmallVector<int, 16> Indices({-1, 0, 7}); |
| 756 | ShuffleVectorInst::commuteShuffleMask(Indices, 4); |
| 757 | EXPECT_THAT(Indices, testing::ContainerEq(ArrayRef<int>({-1, 4, 3}))); |
| 758 | } |
| 759 | |
Sanjay Patel | 42f462a | 2018-06-19 18:44:00 +0000 | [diff] [blame] | 760 | TEST(InstructionsTest, ShuffleMaskQueries) { |
| 761 | // Create the elements for various constant vectors. |
| 762 | LLVMContext Ctx; |
| 763 | Type *Int32Ty = Type::getInt32Ty(Ctx); |
| 764 | Constant *CU = UndefValue::get(Int32Ty); |
| 765 | Constant *C0 = ConstantInt::get(Int32Ty, 0); |
| 766 | Constant *C1 = ConstantInt::get(Int32Ty, 1); |
| 767 | Constant *C2 = ConstantInt::get(Int32Ty, 2); |
| 768 | Constant *C3 = ConstantInt::get(Int32Ty, 3); |
| 769 | Constant *C4 = ConstantInt::get(Int32Ty, 4); |
| 770 | Constant *C5 = ConstantInt::get(Int32Ty, 5); |
| 771 | Constant *C6 = ConstantInt::get(Int32Ty, 6); |
| 772 | Constant *C7 = ConstantInt::get(Int32Ty, 7); |
| 773 | |
| 774 | Constant *Identity = ConstantVector::get({C0, CU, C2, C3, C4}); |
| 775 | EXPECT_TRUE(ShuffleVectorInst::isIdentityMask(Identity)); |
| 776 | EXPECT_FALSE(ShuffleVectorInst::isSelectMask(Identity)); // identity is distinguished from select |
| 777 | EXPECT_FALSE(ShuffleVectorInst::isReverseMask(Identity)); |
| 778 | EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(Identity)); // identity is always single source |
| 779 | EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(Identity)); |
| 780 | EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(Identity)); |
| 781 | |
| 782 | Constant *Select = ConstantVector::get({CU, C1, C5}); |
| 783 | EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(Select)); |
| 784 | EXPECT_TRUE(ShuffleVectorInst::isSelectMask(Select)); |
| 785 | EXPECT_FALSE(ShuffleVectorInst::isReverseMask(Select)); |
| 786 | EXPECT_FALSE(ShuffleVectorInst::isSingleSourceMask(Select)); |
| 787 | EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(Select)); |
| 788 | EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(Select)); |
| 789 | |
| 790 | Constant *Reverse = ConstantVector::get({C3, C2, C1, CU}); |
| 791 | EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(Reverse)); |
| 792 | EXPECT_FALSE(ShuffleVectorInst::isSelectMask(Reverse)); |
| 793 | EXPECT_TRUE(ShuffleVectorInst::isReverseMask(Reverse)); |
| 794 | EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(Reverse)); // reverse is always single source |
| 795 | EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(Reverse)); |
| 796 | EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(Reverse)); |
| 797 | |
| 798 | Constant *SingleSource = ConstantVector::get({C2, C2, C0, CU}); |
| 799 | EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(SingleSource)); |
| 800 | EXPECT_FALSE(ShuffleVectorInst::isSelectMask(SingleSource)); |
| 801 | EXPECT_FALSE(ShuffleVectorInst::isReverseMask(SingleSource)); |
| 802 | EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(SingleSource)); |
| 803 | EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(SingleSource)); |
| 804 | EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(SingleSource)); |
| 805 | |
| 806 | Constant *ZeroEltSplat = ConstantVector::get({C0, C0, CU, C0}); |
| 807 | EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(ZeroEltSplat)); |
| 808 | EXPECT_FALSE(ShuffleVectorInst::isSelectMask(ZeroEltSplat)); |
| 809 | EXPECT_FALSE(ShuffleVectorInst::isReverseMask(ZeroEltSplat)); |
| 810 | EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(ZeroEltSplat)); // 0-splat is always single source |
| 811 | EXPECT_TRUE(ShuffleVectorInst::isZeroEltSplatMask(ZeroEltSplat)); |
| 812 | EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(ZeroEltSplat)); |
| 813 | |
| 814 | Constant *Transpose = ConstantVector::get({C0, C4, C2, C6}); |
| 815 | EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(Transpose)); |
| 816 | EXPECT_FALSE(ShuffleVectorInst::isSelectMask(Transpose)); |
| 817 | EXPECT_FALSE(ShuffleVectorInst::isReverseMask(Transpose)); |
| 818 | EXPECT_FALSE(ShuffleVectorInst::isSingleSourceMask(Transpose)); |
| 819 | EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(Transpose)); |
| 820 | EXPECT_TRUE(ShuffleVectorInst::isTransposeMask(Transpose)); |
| 821 | |
| 822 | // More tests to make sure the logic is/stays correct... |
| 823 | EXPECT_TRUE(ShuffleVectorInst::isIdentityMask(ConstantVector::get({CU, C1, CU, C3}))); |
| 824 | EXPECT_TRUE(ShuffleVectorInst::isIdentityMask(ConstantVector::get({C4, CU, C6, CU}))); |
| 825 | |
| 826 | EXPECT_TRUE(ShuffleVectorInst::isSelectMask(ConstantVector::get({C4, C1, C6, CU}))); |
| 827 | EXPECT_TRUE(ShuffleVectorInst::isSelectMask(ConstantVector::get({CU, C1, C6, C3}))); |
| 828 | |
| 829 | EXPECT_TRUE(ShuffleVectorInst::isReverseMask(ConstantVector::get({C7, C6, CU, C4}))); |
| 830 | EXPECT_TRUE(ShuffleVectorInst::isReverseMask(ConstantVector::get({C3, CU, C1, CU}))); |
| 831 | |
| 832 | EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(ConstantVector::get({C7, C5, CU, C7}))); |
| 833 | EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(ConstantVector::get({C3, C0, CU, C3}))); |
| 834 | |
| 835 | EXPECT_TRUE(ShuffleVectorInst::isZeroEltSplatMask(ConstantVector::get({C4, CU, CU, C4}))); |
| 836 | EXPECT_TRUE(ShuffleVectorInst::isZeroEltSplatMask(ConstantVector::get({CU, C0, CU, C0}))); |
| 837 | |
| 838 | EXPECT_TRUE(ShuffleVectorInst::isTransposeMask(ConstantVector::get({C1, C5, C3, C7}))); |
| 839 | EXPECT_TRUE(ShuffleVectorInst::isTransposeMask(ConstantVector::get({C1, C3}))); |
Sanjay Patel | 14ea008 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 840 | |
Sanjay Patel | dff6e8d | 2018-09-20 14:36:09 +0000 | [diff] [blame] | 841 | // Nothing special about the values here - just re-using inputs to reduce code. |
| 842 | Constant *V0 = ConstantVector::get({C0, C1, C2, C3}); |
| 843 | Constant *V1 = ConstantVector::get({C3, C2, C1, C0}); |
| 844 | |
Sanjay Patel | 14ea008 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 845 | // Identity with undef elts. |
Sanjay Patel | dff6e8d | 2018-09-20 14:36:09 +0000 | [diff] [blame] | 846 | ShuffleVectorInst *Id1 = new ShuffleVectorInst(V0, V1, |
Sanjay Patel | 14ea008 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 847 | ConstantVector::get({C0, C1, CU, CU})); |
| 848 | EXPECT_TRUE(Id1->isIdentity()); |
| 849 | EXPECT_FALSE(Id1->isIdentityWithPadding()); |
| 850 | EXPECT_FALSE(Id1->isIdentityWithExtract()); |
Sanjay Patel | 1c86ce8 | 2018-09-20 15:21:52 +0000 | [diff] [blame] | 851 | EXPECT_FALSE(Id1->isConcat()); |
Sanjay Patel | 14ea008 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 852 | delete Id1; |
| 853 | |
| 854 | // Result has less elements than operands. |
Sanjay Patel | dff6e8d | 2018-09-20 14:36:09 +0000 | [diff] [blame] | 855 | ShuffleVectorInst *Id2 = new ShuffleVectorInst(V0, V1, |
Sanjay Patel | 14ea008 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 856 | ConstantVector::get({C0, C1, C2})); |
| 857 | EXPECT_FALSE(Id2->isIdentity()); |
| 858 | EXPECT_FALSE(Id2->isIdentityWithPadding()); |
| 859 | EXPECT_TRUE(Id2->isIdentityWithExtract()); |
Sanjay Patel | 1c86ce8 | 2018-09-20 15:21:52 +0000 | [diff] [blame] | 860 | EXPECT_FALSE(Id2->isConcat()); |
Sanjay Patel | 14ea008 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 861 | delete Id2; |
| 862 | |
| 863 | // Result has less elements than operands; choose from Op1. |
Sanjay Patel | dff6e8d | 2018-09-20 14:36:09 +0000 | [diff] [blame] | 864 | ShuffleVectorInst *Id3 = new ShuffleVectorInst(V0, V1, |
Sanjay Patel | 14ea008 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 865 | ConstantVector::get({C4, CU, C6})); |
| 866 | EXPECT_FALSE(Id3->isIdentity()); |
| 867 | EXPECT_FALSE(Id3->isIdentityWithPadding()); |
| 868 | EXPECT_TRUE(Id3->isIdentityWithExtract()); |
Sanjay Patel | 1c86ce8 | 2018-09-20 15:21:52 +0000 | [diff] [blame] | 869 | EXPECT_FALSE(Id3->isConcat()); |
Sanjay Patel | 14ea008 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 870 | delete Id3; |
| 871 | |
| 872 | // Result has less elements than operands; choose from Op0 and Op1 is not identity. |
Sanjay Patel | dff6e8d | 2018-09-20 14:36:09 +0000 | [diff] [blame] | 873 | ShuffleVectorInst *Id4 = new ShuffleVectorInst(V0, V1, |
Sanjay Patel | 14ea008 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 874 | ConstantVector::get({C4, C1, C6})); |
| 875 | EXPECT_FALSE(Id4->isIdentity()); |
| 876 | EXPECT_FALSE(Id4->isIdentityWithPadding()); |
| 877 | EXPECT_FALSE(Id4->isIdentityWithExtract()); |
Sanjay Patel | 1c86ce8 | 2018-09-20 15:21:52 +0000 | [diff] [blame] | 878 | EXPECT_FALSE(Id4->isConcat()); |
Sanjay Patel | 14ea008 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 879 | delete Id4; |
| 880 | |
| 881 | // Result has more elements than operands, and extra elements are undef. |
Sanjay Patel | dff6e8d | 2018-09-20 14:36:09 +0000 | [diff] [blame] | 882 | ShuffleVectorInst *Id5 = new ShuffleVectorInst(V0, V1, |
Sanjay Patel | 14ea008 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 883 | ConstantVector::get({CU, C1, C2, C3, CU, CU})); |
| 884 | EXPECT_FALSE(Id5->isIdentity()); |
| 885 | EXPECT_TRUE(Id5->isIdentityWithPadding()); |
| 886 | EXPECT_FALSE(Id5->isIdentityWithExtract()); |
Sanjay Patel | 1c86ce8 | 2018-09-20 15:21:52 +0000 | [diff] [blame] | 887 | EXPECT_FALSE(Id5->isConcat()); |
Sanjay Patel | 14ea008 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 888 | delete Id5; |
| 889 | |
| 890 | // Result has more elements than operands, and extra elements are undef; choose from Op1. |
Sanjay Patel | dff6e8d | 2018-09-20 14:36:09 +0000 | [diff] [blame] | 891 | ShuffleVectorInst *Id6 = new ShuffleVectorInst(V0, V1, |
Sanjay Patel | 14ea008 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 892 | ConstantVector::get({C4, C5, C6, CU, CU, CU})); |
| 893 | EXPECT_FALSE(Id6->isIdentity()); |
| 894 | EXPECT_TRUE(Id6->isIdentityWithPadding()); |
| 895 | EXPECT_FALSE(Id6->isIdentityWithExtract()); |
Sanjay Patel | 1c86ce8 | 2018-09-20 15:21:52 +0000 | [diff] [blame] | 896 | EXPECT_FALSE(Id6->isConcat()); |
Sanjay Patel | 14ea008 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 897 | delete Id6; |
| 898 | |
| 899 | // Result has more elements than operands, but extra elements are not undef. |
Sanjay Patel | dff6e8d | 2018-09-20 14:36:09 +0000 | [diff] [blame] | 900 | ShuffleVectorInst *Id7 = new ShuffleVectorInst(V0, V1, |
Sanjay Patel | 14ea008 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 901 | ConstantVector::get({C0, C1, C2, C3, CU, C1})); |
| 902 | EXPECT_FALSE(Id7->isIdentity()); |
| 903 | EXPECT_FALSE(Id7->isIdentityWithPadding()); |
| 904 | EXPECT_FALSE(Id7->isIdentityWithExtract()); |
Sanjay Patel | 1c86ce8 | 2018-09-20 15:21:52 +0000 | [diff] [blame] | 905 | EXPECT_FALSE(Id7->isConcat()); |
Sanjay Patel | 14ea008 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 906 | delete Id7; |
| 907 | |
| 908 | // Result has more elements than operands; choose from Op0 and Op1 is not identity. |
Sanjay Patel | dff6e8d | 2018-09-20 14:36:09 +0000 | [diff] [blame] | 909 | ShuffleVectorInst *Id8 = new ShuffleVectorInst(V0, V1, |
Sanjay Patel | 14ea008 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 910 | ConstantVector::get({C4, CU, C2, C3, CU, CU})); |
| 911 | EXPECT_FALSE(Id8->isIdentity()); |
| 912 | EXPECT_FALSE(Id8->isIdentityWithPadding()); |
| 913 | EXPECT_FALSE(Id8->isIdentityWithExtract()); |
Sanjay Patel | 1c86ce8 | 2018-09-20 15:21:52 +0000 | [diff] [blame] | 914 | EXPECT_FALSE(Id8->isConcat()); |
Sanjay Patel | 14ea008 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 915 | delete Id8; |
Sanjay Patel | 1c86ce8 | 2018-09-20 15:21:52 +0000 | [diff] [blame] | 916 | |
| 917 | // Result has twice as many elements as operands; choose consecutively from Op0 and Op1 is concat. |
| 918 | ShuffleVectorInst *Id9 = new ShuffleVectorInst(V0, V1, |
| 919 | ConstantVector::get({C0, CU, C2, C3, CU, CU, C6, C7})); |
| 920 | EXPECT_FALSE(Id9->isIdentity()); |
| 921 | EXPECT_FALSE(Id9->isIdentityWithPadding()); |
| 922 | EXPECT_FALSE(Id9->isIdentityWithExtract()); |
| 923 | EXPECT_TRUE(Id9->isConcat()); |
| 924 | delete Id9; |
| 925 | |
| 926 | // Result has less than twice as many elements as operands, so not a concat. |
| 927 | ShuffleVectorInst *Id10 = new ShuffleVectorInst(V0, V1, |
| 928 | ConstantVector::get({C0, CU, C2, C3, CU, CU, C6})); |
| 929 | EXPECT_FALSE(Id10->isIdentity()); |
| 930 | EXPECT_FALSE(Id10->isIdentityWithPadding()); |
| 931 | EXPECT_FALSE(Id10->isIdentityWithExtract()); |
| 932 | EXPECT_FALSE(Id10->isConcat()); |
| 933 | delete Id10; |
| 934 | |
| 935 | // Result has more than twice as many elements as operands, so not a concat. |
| 936 | ShuffleVectorInst *Id11 = new ShuffleVectorInst(V0, V1, |
| 937 | ConstantVector::get({C0, CU, C2, C3, CU, CU, C6, C7, CU})); |
| 938 | EXPECT_FALSE(Id11->isIdentity()); |
| 939 | EXPECT_FALSE(Id11->isIdentityWithPadding()); |
| 940 | EXPECT_FALSE(Id11->isIdentityWithExtract()); |
| 941 | EXPECT_FALSE(Id11->isConcat()); |
| 942 | delete Id11; |
| 943 | |
| 944 | // If an input is undef, it's not a concat. |
| 945 | // TODO: IdentityWithPadding should be true here even though the high mask values are not undef. |
| 946 | ShuffleVectorInst *Id12 = new ShuffleVectorInst(V0, ConstantVector::get({CU, CU, CU, CU}), |
| 947 | ConstantVector::get({C0, CU, C2, C3, CU, CU, C6, C7})); |
| 948 | EXPECT_FALSE(Id12->isIdentity()); |
| 949 | EXPECT_FALSE(Id12->isIdentityWithPadding()); |
| 950 | EXPECT_FALSE(Id12->isIdentityWithExtract()); |
| 951 | EXPECT_FALSE(Id12->isConcat()); |
| 952 | delete Id12; |
Sanjay Patel | 42f462a | 2018-06-19 18:44:00 +0000 | [diff] [blame] | 953 | } |
| 954 | |
Vedant Kumar | 83a6451 | 2018-06-19 23:42:17 +0000 | [diff] [blame] | 955 | TEST(InstructionsTest, SkipDebug) { |
| 956 | LLVMContext C; |
| 957 | std::unique_ptr<Module> M = parseIR(C, |
| 958 | R"( |
| 959 | declare void @llvm.dbg.value(metadata, metadata, metadata) |
| 960 | |
| 961 | define void @f() { |
| 962 | entry: |
| 963 | call void @llvm.dbg.value(metadata i32 0, metadata !11, metadata !DIExpression()), !dbg !13 |
| 964 | ret void |
| 965 | } |
| 966 | |
| 967 | !llvm.dbg.cu = !{!0} |
| 968 | !llvm.module.flags = !{!3, !4} |
| 969 | !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 6.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2) |
| 970 | !1 = !DIFile(filename: "t2.c", directory: "foo") |
| 971 | !2 = !{} |
| 972 | !3 = !{i32 2, !"Dwarf Version", i32 4} |
| 973 | !4 = !{i32 2, !"Debug Info Version", i32 3} |
| 974 | !8 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 1, type: !9, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: false, unit: !0, retainedNodes: !2) |
| 975 | !9 = !DISubroutineType(types: !10) |
| 976 | !10 = !{null} |
| 977 | !11 = !DILocalVariable(name: "x", scope: !8, file: !1, line: 2, type: !12) |
| 978 | !12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) |
| 979 | !13 = !DILocation(line: 2, column: 7, scope: !8) |
| 980 | )"); |
| 981 | ASSERT_TRUE(M); |
| 982 | Function *F = cast<Function>(M->getNamedValue("f")); |
| 983 | BasicBlock &BB = F->front(); |
| 984 | |
| 985 | // The first non-debug instruction is the terminator. |
| 986 | auto *Term = BB.getTerminator(); |
| 987 | EXPECT_EQ(Term, BB.begin()->getNextNonDebugInstruction()); |
Vedant Kumar | ab0a33b | 2018-06-26 21:16:59 +0000 | [diff] [blame] | 988 | EXPECT_EQ(Term->getIterator(), skipDebugIntrinsics(BB.begin())); |
Vedant Kumar | 83a6451 | 2018-06-19 23:42:17 +0000 | [diff] [blame] | 989 | |
| 990 | // After the terminator, there are no non-debug instructions. |
| 991 | EXPECT_EQ(nullptr, Term->getNextNonDebugInstruction()); |
| 992 | } |
| 993 | |
Joseph Tremoulet | 0d05e6c | 2016-01-14 06:21:42 +0000 | [diff] [blame] | 994 | } // end anonymous namespace |
| 995 | } // end namespace llvm |