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