blob: 080e35c743342f68595afcded98382dd6213d87e [file] [log] [blame]
Chandler Carruthc779e962013-01-07 15:35:46 +00001//===- llvm/unittest/IR/InstructionsTest.cpp - Instructions unit tests ----===//
Gabor Greif5a7069a2010-03-16 09:55:46 +00002//
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 Kumar83a64512018-06-19 23:42:17 +000010#include "llvm/AsmParser/Parser.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000011#include "llvm/IR/Instructions.h"
Chandler Carruth5a88dda2012-12-04 10:23:08 +000012#include "llvm/ADT/STLExtras.h"
13#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000014#include "llvm/IR/BasicBlock.h"
15#include "llvm/IR/Constants.h"
16#include "llvm/IR/DataLayout.h"
17#include "llvm/IR/DerivedTypes.h"
Eli Bendersky1003e8f2014-03-26 20:41:15 +000018#include "llvm/IR/Function.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000019#include "llvm/IR/IRBuilder.h"
20#include "llvm/IR/LLVMContext.h"
21#include "llvm/IR/MDBuilder.h"
Eli Bendersky1003e8f2014-03-26 20:41:15 +000022#include "llvm/IR/Module.h"
Sanjoy Das9e369752017-02-23 22:50:52 +000023#include "llvm/IR/NoFolder.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000024#include "llvm/IR/Operator.h"
Vedant Kumar83a64512018-06-19 23:42:17 +000025#include "llvm/Support/SourceMgr.h"
Zvi Rackoverf4d18ab2017-05-08 12:40:18 +000026#include "gmock/gmock-matchers.h"
Gabor Greif5a7069a2010-03-16 09:55:46 +000027#include "gtest/gtest.h"
Eli Bendersky1003e8f2014-03-26 20:41:15 +000028#include <memory>
Gabor Greif5a7069a2010-03-16 09:55:46 +000029
30namespace llvm {
31namespace {
32
Vedant Kumar83a64512018-06-19 23:42:17 +000033static 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 Greif138acfe2010-03-16 10:59:48 +000041TEST(InstructionsTest, ReturnInst) {
Mehdi Amini8be77072016-04-14 21:59:01 +000042 LLVMContext C;
Gabor Greif5a7069a2010-03-16 09:55:46 +000043
Gabor Greif138acfe2010-03-16 10:59:48 +000044 // test for PR6589
Gabor Greif5a7069a2010-03-16 09:55:46 +000045 const ReturnInst* r0 = ReturnInst::Create(C);
Gabor Greifd165e1a2010-03-16 15:26:09 +000046 EXPECT_EQ(r0->getNumOperands(), 0U);
Gabor Greif138acfe2010-03-16 10:59:48 +000047 EXPECT_EQ(r0->op_begin(), r0->op_end());
Gabor Greif22385eb2010-03-16 11:24:53 +000048
Chris Lattnerdb125cf2011-07-18 04:54:35 +000049 IntegerType* Int1 = IntegerType::get(C, 1);
Gabor Greif22385eb2010-03-16 11:24:53 +000050 Constant* One = ConstantInt::get(Int1, 1, true);
51 const ReturnInst* r1 = ReturnInst::Create(C, One);
John McCallf5ec9b52011-08-27 19:23:22 +000052 EXPECT_EQ(1U, r1->getNumOperands());
Gabor Greif22385eb2010-03-16 11:24:53 +000053 User::const_op_iterator b(r1->op_begin());
John McCallf5ec9b52011-08-27 19:23:22 +000054 EXPECT_NE(r1->op_end(), b);
55 EXPECT_EQ(One, *b);
56 EXPECT_EQ(One, r1->getOperand(0));
Gabor Greif22385eb2010-03-16 11:24:53 +000057 ++b;
John McCallf5ec9b52011-08-27 19:23:22 +000058 EXPECT_EQ(r1->op_end(), b);
Gabor Greif642c0662010-03-16 12:32:03 +000059
60 // clean up
61 delete r0;
62 delete r1;
Gabor Greif5a7069a2010-03-16 09:55:46 +000063}
64
Eli Benderskyd8f49932014-03-26 21:46:24 +000065// 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.
67class ModuleWithFunctionTest : public testing::Test {
68protected:
NAKAMURA Takumida56c392014-03-27 11:32:41 +000069 ModuleWithFunctionTest() : M(new Module("MyModule", Ctx)) {
NAKAMURA Takumic1129902014-03-27 11:38:28 +000070 FArgTypes.push_back(Type::getInt8Ty(Ctx));
71 FArgTypes.push_back(Type::getInt32Ty(Ctx));
72 FArgTypes.push_back(Type::getInt64Ty(Ctx));
Eli Benderskyd8f49932014-03-26 21:46:24 +000073 FunctionType *FTy =
74 FunctionType::get(Type::getVoidTy(Ctx), FArgTypes, false);
75 F = Function::Create(FTy, Function::ExternalLinkage, "", M.get());
76 }
Eli Bendersky1003e8f2014-03-26 20:41:15 +000077
Eli Benderskyd8f49932014-03-26 21:46:24 +000078 LLVMContext Ctx;
79 std::unique_ptr<Module> M;
NAKAMURA Takumi2b8b9032014-03-27 11:33:11 +000080 SmallVector<Type *, 3> FArgTypes;
Eli Benderskyd8f49932014-03-26 21:46:24 +000081 Function *F;
82};
Eli Bendersky1003e8f2014-03-26 20:41:15 +000083
Eli Benderskyd8f49932014-03-26 21:46:24 +000084TEST_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 Bendersky927eb312014-03-26 21:11:34 +000088 std::unique_ptr<CallInst> Call(CallInst::Create(F, Args));
Eli Bendersky1003e8f2014-03-26 20:41:15 +000089
90 // Make sure iteration over a call's arguments works as expected.
91 unsigned Idx = 0;
92 for (Value *Arg : Call->arg_operands()) {
Eli Benderskyd8f49932014-03-26 21:46:24 +000093 EXPECT_EQ(FArgTypes[Idx], Arg->getType());
Eli Bendersky1003e8f2014-03-26 20:41:15 +000094 EXPECT_EQ(Call->getArgOperand(Idx)->getType(), Arg->getType());
95 Idx++;
96 }
97}
98
Eli Benderskyd8f49932014-03-26 21:46:24 +000099TEST_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 Greifd165e1a2010-03-16 15:26:09 +0000117TEST(InstructionsTest, BranchInst) {
Mehdi Amini8be77072016-04-14 21:59:01 +0000118 LLVMContext C;
Gabor Greifd165e1a2010-03-16 15:26:09 +0000119
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 Greifabf657f2010-03-16 15:53:58 +0000127 EXPECT_TRUE(b0->isUnconditional());
128 EXPECT_FALSE(b0->isConditional());
John McCallf5ec9b52011-08-27 19:23:22 +0000129 EXPECT_EQ(1U, b0->getNumSuccessors());
Gabor Greifabf657f2010-03-16 15:53:58 +0000130
Gabor Greifd165e1a2010-03-16 15:26:09 +0000131 // check num operands
John McCallf5ec9b52011-08-27 19:23:22 +0000132 EXPECT_EQ(1U, b0->getNumOperands());
Gabor Greifd165e1a2010-03-16 15:26:09 +0000133
134 EXPECT_NE(b0->op_begin(), b0->op_end());
Benjamin Kramerd628f192014-03-02 12:27:27 +0000135 EXPECT_EQ(b0->op_end(), std::next(b0->op_begin()));
Gabor Greifabf657f2010-03-16 15:53:58 +0000136
Benjamin Kramerd628f192014-03-02 12:27:27 +0000137 EXPECT_EQ(b0->op_end(), std::next(b0->op_begin()));
Gabor Greifd165e1a2010-03-16 15:26:09 +0000138
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000139 IntegerType* Int1 = IntegerType::get(C, 1);
Gabor Greifd165e1a2010-03-16 15:26:09 +0000140 Constant* One = ConstantInt::get(Int1, 1, true);
141
142 // Conditional BranchInst
143 BranchInst* b1 = BranchInst::Create(bb0, bb1, One);
144
Gabor Greifabf657f2010-03-16 15:53:58 +0000145 EXPECT_FALSE(b1->isUnconditional());
146 EXPECT_TRUE(b1->isConditional());
John McCallf5ec9b52011-08-27 19:23:22 +0000147 EXPECT_EQ(2U, b1->getNumSuccessors());
Gabor Greifabf657f2010-03-16 15:53:58 +0000148
Gabor Greifd165e1a2010-03-16 15:26:09 +0000149 // check num operands
John McCallf5ec9b52011-08-27 19:23:22 +0000150 EXPECT_EQ(3U, b1->getNumOperands());
Gabor Greifd165e1a2010-03-16 15:26:09 +0000151
152 User::const_op_iterator b(b1->op_begin());
153
154 // check COND
155 EXPECT_NE(b, b1->op_end());
John McCallf5ec9b52011-08-27 19:23:22 +0000156 EXPECT_EQ(One, *b);
157 EXPECT_EQ(One, b1->getOperand(0));
158 EXPECT_EQ(One, b1->getCondition());
Gabor Greifd165e1a2010-03-16 15:26:09 +0000159 ++b;
160
161 // check ELSE
John McCallf5ec9b52011-08-27 19:23:22 +0000162 EXPECT_EQ(bb1, *b);
163 EXPECT_EQ(bb1, b1->getOperand(1));
164 EXPECT_EQ(bb1, b1->getSuccessor(1));
Gabor Greifd165e1a2010-03-16 15:26:09 +0000165 ++b;
166
167 // check THEN
John McCallf5ec9b52011-08-27 19:23:22 +0000168 EXPECT_EQ(bb0, *b);
169 EXPECT_EQ(bb0, b1->getOperand(2));
170 EXPECT_EQ(bb0, b1->getSuccessor(0));
Gabor Greifd165e1a2010-03-16 15:26:09 +0000171 ++b;
172
John McCallf5ec9b52011-08-27 19:23:22 +0000173 EXPECT_EQ(b1->op_end(), b);
Gabor Greifd165e1a2010-03-16 15:26:09 +0000174
Gabor Greifd165e1a2010-03-16 15:26:09 +0000175 // clean up
176 delete b0;
177 delete b1;
178
179 delete bb0;
180 delete bb1;
181}
182
Duncan Sands60794652011-04-01 03:34:54 +0000183TEST(InstructionsTest, CastInst) {
Mehdi Amini8be77072016-04-14 21:59:01 +0000184 LLVMContext C;
Duncan Sands60794652011-04-01 03:34:54 +0000185
Matt Arsenaultf34dc422013-07-30 20:45:05 +0000186 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 Arsenault79e3fb52014-01-22 19:21:33 +0000215 Type *V4Int32PtrTy = VectorType::get(Int32PtrTy, 4);
Duncan Sands60794652011-04-01 03:34:54 +0000216
Duncan Sands117feba2011-05-18 07:13:41 +0000217 const Constant* c8 = Constant::getNullValue(V8x8Ty);
218 const Constant* c64 = Constant::getNullValue(V8x64Ty);
219
Matt Arsenault59d3ae62013-11-15 01:34:59 +0000220 const Constant *v2ptr32 = Constant::getNullValue(V2Int32PtrTy);
221
Matt Arsenault485c7fd2013-07-30 22:02:14 +0000222 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 McCallf5ec9b52011-08-27 19:23:22 +0000227 EXPECT_EQ(CastInst::Trunc, CastInst::getCastOpcode(c64, true, V8x8Ty, true));
228 EXPECT_EQ(CastInst::SExt, CastInst::getCastOpcode(c8, true, V8x64Ty, true));
Matt Arsenaultf34dc422013-07-30 20:45:05 +0000229
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 Arsenault59d3ae62013-11-15 01:34:59 +0000242 EXPECT_TRUE(CastInst::isCastable(V2Int32PtrAS1Ty, V2Int32PtrTy));
243 EXPECT_EQ(CastInst::AddrSpaceCast, CastInst::getCastOpcode(v2ptr32, true,
244 V2Int32PtrAS1Ty,
245 true));
Matt Arsenaultf34dc422013-07-30 20:45:05 +0000246
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 Arsenault1bf0ec42013-07-31 00:17:33 +0000274
275
Matt Arsenault79e3fb52014-01-22 19:21:33 +0000276 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 Arsenault1bf0ec42013-07-31 00:17:33 +0000291 // 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 Amini8be77072016-04-14 21:59:01 +0000296 auto Inst1 = CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty, "foo", BB);
Matt Arsenault1bf0ec42013-07-31 00:17:33 +0000297
298 // Second form
Mehdi Amini8be77072016-04-14 21:59:01 +0000299 auto Inst2 = CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty);
300
301 delete Inst2;
302 Inst1->eraseFromParent();
303 delete BB;
Duncan Sands60794652011-04-01 03:34:54 +0000304}
305
Nadav Rotem16087692011-12-05 06:29:09 +0000306TEST(InstructionsTest, VectorGep) {
Mehdi Amini8be77072016-04-14 21:59:01 +0000307 LLVMContext C;
Nadav Rotem16087692011-12-05 06:29:09 +0000308
309 // Type Definitions
David Blaikief0295e42015-03-14 21:40:10 +0000310 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 Rotem16087692011-12-05 06:29:09 +0000314
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 Stepanov4802b9d2013-01-16 14:38:50 +0000334 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 Blaikief0295e42015-03-14 21:40:10 +0000339 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 Rotem16087692011-12-05 06:29:09 +0000343
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 Villmow791cfc22012-10-08 16:39:34 +0000360 DataLayout TD("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f3"
Rafael Espindolaf343bc92013-12-13 18:56:34 +0000361 "2:32:32-f64:64:64-v64:64:64-v128:128:128-a:0:64-s:64:64-f80"
Nadav Rotem16087692011-12-05 06:29:09 +0000362 ":128:128-n8:16:32:64-S128");
363 // Make sure we don't crash
Mehdi Amini529919f2015-03-10 02:37:25 +0000364 GetPointerBaseWithConstantOffset(Gep0, Offset, TD);
365 GetPointerBaseWithConstantOffset(Gep1, Offset, TD);
366 GetPointerBaseWithConstantOffset(Gep2, Offset, TD);
367 GetPointerBaseWithConstantOffset(Gep3, Offset, TD);
Nadav Rotem16087692011-12-05 06:29:09 +0000368
369 // Gep of Geps
David Blaikief0295e42015-03-14 21:40:10 +0000370 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 Rotem16087692011-12-05 06:29:09 +0000374
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 Stepanov4802b9d2013-01-16 14:38:50 +0000400 ICmp2->eraseFromParent();
401 delete BB0;
402
Nadav Rotem16087692011-12-05 06:29:09 +0000403 delete ICmp0;
404 delete ICmp1;
405 delete PtrVecA;
406 delete PtrVecB;
407}
408
Duncan Sands8883c432012-04-16 16:28:59 +0000409TEST(InstructionsTest, FPMathOperator) {
Mehdi Amini8be77072016-04-14 21:59:01 +0000410 LLVMContext Context;
Duncan Sands8883c432012-04-16 16:28:59 +0000411 IRBuilder<> Builder(Context);
412 MDBuilder MDHelper(Context);
413 Instruction *I = Builder.CreatePHI(Builder.getDoubleTy(), 0);
414 MDNode *MD1 = MDHelper.createFPMath(1.0);
Duncan Sands8883c432012-04-16 16:28:59 +0000415 Value *V1 = Builder.CreateFAdd(I, I, "", MD1);
Duncan Sands8883c432012-04-16 16:28:59 +0000416 EXPECT_TRUE(isa<FPMathOperator>(V1));
Duncan Sands8883c432012-04-16 16:28:59 +0000417 FPMathOperator *O1 = cast<FPMathOperator>(V1);
Duncan Sands8883c432012-04-16 16:28:59 +0000418 EXPECT_EQ(O1->getFPAccuracy(), 1.0);
Reid Kleckner816047d2017-05-18 17:24:10 +0000419 V1->deleteValue();
420 I->deleteValue();
Duncan Sands8883c432012-04-16 16:28:59 +0000421}
422
Duncan Sands446cf942012-10-30 16:03:32 +0000423
424TEST(InstructionsTest, isEliminableCastPair) {
Mehdi Amini8be77072016-04-14 21:59:01 +0000425 LLVMContext C;
Duncan Sands446cf942012-10-30 16:03:32 +0000426
Matt Arsenault3181f592013-07-30 22:27:10 +0000427 Type* Int16Ty = Type::getInt16Ty(C);
Duncan Sands446cf942012-10-30 16:03:32 +0000428 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 Topperb1770412014-06-08 22:29:17 +0000436 Int32Ty, nullptr, Int32Ty),
Duncan Sands446cf942012-10-30 16:03:32 +0000437 CastInst::BitCast);
438
Matt Arsenault3181f592013-07-30 22:27:10 +0000439 // Source and destination have unknown sizes, but the same address space and
440 // the intermediate int is the maximum pointer size -> bitcast
Duncan Sands446cf942012-10-30 16:03:32 +0000441 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
442 CastInst::IntToPtr,
443 Int64PtrTy, Int64Ty, Int64PtrTy,
Craig Topperb1770412014-06-08 22:29:17 +0000444 nullptr, nullptr, nullptr),
Matt Arsenault3181f592013-07-30 22:27:10 +0000445 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 Topperb1770412014-06-08 22:29:17 +0000452 nullptr, nullptr, nullptr),
Duncan Sands446cf942012-10-30 16:03:32 +0000453 0U);
454
455 // Middle pointer big enough -> bitcast.
456 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
457 CastInst::PtrToInt,
458 Int64Ty, Int64PtrTy, Int64Ty,
Craig Topperb1770412014-06-08 22:29:17 +0000459 nullptr, Int64Ty, nullptr),
Duncan Sands446cf942012-10-30 16:03:32 +0000460 CastInst::BitCast);
461
462 // Middle pointer too small -> fail.
463 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
464 CastInst::PtrToInt,
465 Int64Ty, Int64PtrTy, Int64Ty,
Craig Topperb1770412014-06-08 22:29:17 +0000466 nullptr, Int32Ty, nullptr),
Duncan Sands446cf942012-10-30 16:03:32 +0000467 0U);
Matt Arsenault3181f592013-07-30 22:27:10 +0000468
Matt Arsenault3181f592013-07-30 22:27:10 +0000469 // 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 Espindolaf343bc92013-12-13 18:56:34 +0000473 "-v128:128:128-a:0:64-s:64:64-f80:128:128-n8:16:32:64-S128");
Matt Arsenault3181f592013-07-30 22:27:10 +0000474
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 Arsenault59d3ae62013-11-15 01:34:59 +0000481 // Cannot simplify inttoptr, addrspacecast
Matt Arsenault3181f592013-07-30 22:27:10 +0000482 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
Matt Arsenault59d3ae62013-11-15 01:34:59 +0000483 CastInst::AddrSpaceCast,
Matt Arsenault3181f592013-07-30 22:27:10 +0000484 Int16Ty, Int64PtrTyAS1, Int64PtrTyAS2,
Craig Topperb1770412014-06-08 22:29:17 +0000485 nullptr, Int16SizePtr, Int64SizePtr),
Matt Arsenault3181f592013-07-30 22:27:10 +0000486 0U);
487
Matt Arsenault59d3ae62013-11-15 01:34:59 +0000488 // Cannot simplify addrspacecast, ptrtoint
489 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::AddrSpaceCast,
490 CastInst::PtrToInt,
491 Int64PtrTyAS1, Int64PtrTyAS2, Int16Ty,
Craig Topperb1770412014-06-08 22:29:17 +0000492 Int64SizePtr, Int16SizePtr, nullptr),
Matt Arsenault59d3ae62013-11-15 01:34:59 +0000493 0U);
494
Matt Arsenault3181f592013-07-30 22:27:10 +0000495 // Pass since the bitcast address spaces are the same
496 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
497 CastInst::BitCast,
498 Int16Ty, Int64PtrTyAS1, Int64PtrTyAS1,
Craig Topperb1770412014-06-08 22:29:17 +0000499 nullptr, nullptr, nullptr),
Matt Arsenault3181f592013-07-30 22:27:10 +0000500 CastInst::IntToPtr);
501
Duncan Sands446cf942012-10-30 16:03:32 +0000502}
503
Reid Kleckner39497492014-05-06 20:08:20 +0000504TEST(InstructionsTest, CloneCall) {
Mehdi Amini8be77072016-04-14 21:59:01 +0000505 LLVMContext C;
Reid Kleckner39497492014-05-06 20:08:20 +0000506 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 Kleckner67077702017-03-21 16:57:19 +0000531 Call->setAttributes(
532 AttributeList::get(C, AttributeList::FunctionIndex, AB));
Reid Kleckner39497492014-05-06 20:08:20 +0000533 std::unique_ptr<CallInst> Clone(cast<CallInst>(Call->clone()));
534 EXPECT_TRUE(Clone->onlyReadsMemory());
535 }
536}
537
Joseph Tremoulet0d05e6c2016-01-14 06:21:42 +0000538TEST(InstructionsTest, AlterCallBundles) {
Mehdi Amini8be77072016-04-14 21:59:01 +0000539 LLVMContext C;
Joseph Tremoulet0d05e6c2016-01-14 06:21:42 +0000540 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 Kleckner67077702017-03-21 16:57:19 +0000550 Call->setAttributes(AttributeList::get(C, AttributeList::FunctionIndex, AB));
Joseph Tremoulet0d05e6c2016-01-14 06:21:42 +0000551 Call->setDebugLoc(DebugLoc(MDNode::get(C, None)));
Matt Arsenault3181f592013-07-30 22:27:10 +0000552
Joseph Tremoulet0d05e6c2016-01-14 06:21:42 +0000553 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 Tremoulet6f916872016-01-14 06:30:19 +0000561 EXPECT_EQ(Clone->getNumOperandBundles(), 1U);
Joseph Tremoulet0d05e6c2016-01-14 06:21:42 +0000562 EXPECT_TRUE(Clone->getOperandBundle("after").hasValue());
563}
Matt Arsenault3181f592013-07-30 22:27:10 +0000564
Joseph Tremoulet0d05e6c2016-01-14 06:21:42 +0000565TEST(InstructionsTest, AlterInvokeBundles) {
Mehdi Amini8be77072016-04-14 21:59:01 +0000566 LLVMContext C;
Joseph Tremoulet0d05e6c2016-01-14 06:21:42 +0000567 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 Tremouletdf4beed2016-01-15 15:08:36 +0000571 std::unique_ptr<BasicBlock> NormalDest(BasicBlock::Create(C));
572 std::unique_ptr<BasicBlock> UnwindDest(BasicBlock::Create(C));
Joseph Tremoulet0d05e6c2016-01-14 06:21:42 +0000573 OperandBundleDef OldBundle("before", UndefValue::get(Int32Ty));
Joseph Tremouletdf4beed2016-01-15 15:08:36 +0000574 std::unique_ptr<InvokeInst> Invoke(InvokeInst::Create(
575 Callee, NormalDest.get(), UnwindDest.get(), Args, OldBundle, "result"));
Joseph Tremoulet0d05e6c2016-01-14 06:21:42 +0000576 AttrBuilder AB;
577 AB.addAttribute(Attribute::Cold);
Reid Kleckner67077702017-03-21 16:57:19 +0000578 Invoke->setAttributes(
579 AttributeList::get(C, AttributeList::FunctionIndex, AB));
Joseph Tremoulet0d05e6c2016-01-14 06:21:42 +0000580 Invoke->setDebugLoc(DebugLoc(MDNode::get(C, None)));
581
582 OperandBundleDef NewBundle("after", ConstantInt::get(Int32Ty, 7));
Joseph Tremouletdf4beed2016-01-15 15:08:36 +0000583 std::unique_ptr<InvokeInst> Clone(
584 InvokeInst::Create(Invoke.get(), NewBundle));
Joseph Tremoulet0d05e6c2016-01-14 06:21:42 +0000585 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 Takumi92d091b2016-01-14 09:21:49 +0000592 EXPECT_EQ(Clone->getNumOperandBundles(), 1U);
Joseph Tremoulet0d05e6c2016-01-14 06:21:42 +0000593 EXPECT_TRUE(Clone->getOperandBundle("after").hasValue());
Joseph Tremoulet0d05e6c2016-01-14 06:21:42 +0000594}
595
Sanjoy Das9e369752017-02-23 22:50:52 +0000596TEST_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 Carruthbff5a6e2017-02-28 08:04:20 +0000656TEST(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 Carruthddfada22017-04-12 07:27:28 +0000691TEST(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 Rackoverf4d18ab2017-05-08 12:40:18 +0000754TEST(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 Patel42f462a2018-06-19 18:44:00 +0000760TEST(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 Patel14ea0082018-08-30 15:05:38 +0000840
Sanjay Pateldff6e8d2018-09-20 14:36:09 +0000841 // 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 Patel14ea0082018-08-30 15:05:38 +0000845 // Identity with undef elts.
Sanjay Pateldff6e8d2018-09-20 14:36:09 +0000846 ShuffleVectorInst *Id1 = new ShuffleVectorInst(V0, V1,
Sanjay Patel14ea0082018-08-30 15:05:38 +0000847 ConstantVector::get({C0, C1, CU, CU}));
848 EXPECT_TRUE(Id1->isIdentity());
849 EXPECT_FALSE(Id1->isIdentityWithPadding());
850 EXPECT_FALSE(Id1->isIdentityWithExtract());
Sanjay Patel1c86ce82018-09-20 15:21:52 +0000851 EXPECT_FALSE(Id1->isConcat());
Sanjay Patel14ea0082018-08-30 15:05:38 +0000852 delete Id1;
853
854 // Result has less elements than operands.
Sanjay Pateldff6e8d2018-09-20 14:36:09 +0000855 ShuffleVectorInst *Id2 = new ShuffleVectorInst(V0, V1,
Sanjay Patel14ea0082018-08-30 15:05:38 +0000856 ConstantVector::get({C0, C1, C2}));
857 EXPECT_FALSE(Id2->isIdentity());
858 EXPECT_FALSE(Id2->isIdentityWithPadding());
859 EXPECT_TRUE(Id2->isIdentityWithExtract());
Sanjay Patel1c86ce82018-09-20 15:21:52 +0000860 EXPECT_FALSE(Id2->isConcat());
Sanjay Patel14ea0082018-08-30 15:05:38 +0000861 delete Id2;
862
863 // Result has less elements than operands; choose from Op1.
Sanjay Pateldff6e8d2018-09-20 14:36:09 +0000864 ShuffleVectorInst *Id3 = new ShuffleVectorInst(V0, V1,
Sanjay Patel14ea0082018-08-30 15:05:38 +0000865 ConstantVector::get({C4, CU, C6}));
866 EXPECT_FALSE(Id3->isIdentity());
867 EXPECT_FALSE(Id3->isIdentityWithPadding());
868 EXPECT_TRUE(Id3->isIdentityWithExtract());
Sanjay Patel1c86ce82018-09-20 15:21:52 +0000869 EXPECT_FALSE(Id3->isConcat());
Sanjay Patel14ea0082018-08-30 15:05:38 +0000870 delete Id3;
871
872 // Result has less elements than operands; choose from Op0 and Op1 is not identity.
Sanjay Pateldff6e8d2018-09-20 14:36:09 +0000873 ShuffleVectorInst *Id4 = new ShuffleVectorInst(V0, V1,
Sanjay Patel14ea0082018-08-30 15:05:38 +0000874 ConstantVector::get({C4, C1, C6}));
875 EXPECT_FALSE(Id4->isIdentity());
876 EXPECT_FALSE(Id4->isIdentityWithPadding());
877 EXPECT_FALSE(Id4->isIdentityWithExtract());
Sanjay Patel1c86ce82018-09-20 15:21:52 +0000878 EXPECT_FALSE(Id4->isConcat());
Sanjay Patel14ea0082018-08-30 15:05:38 +0000879 delete Id4;
880
881 // Result has more elements than operands, and extra elements are undef.
Sanjay Pateldff6e8d2018-09-20 14:36:09 +0000882 ShuffleVectorInst *Id5 = new ShuffleVectorInst(V0, V1,
Sanjay Patel14ea0082018-08-30 15:05:38 +0000883 ConstantVector::get({CU, C1, C2, C3, CU, CU}));
884 EXPECT_FALSE(Id5->isIdentity());
885 EXPECT_TRUE(Id5->isIdentityWithPadding());
886 EXPECT_FALSE(Id5->isIdentityWithExtract());
Sanjay Patel1c86ce82018-09-20 15:21:52 +0000887 EXPECT_FALSE(Id5->isConcat());
Sanjay Patel14ea0082018-08-30 15:05:38 +0000888 delete Id5;
889
890 // Result has more elements than operands, and extra elements are undef; choose from Op1.
Sanjay Pateldff6e8d2018-09-20 14:36:09 +0000891 ShuffleVectorInst *Id6 = new ShuffleVectorInst(V0, V1,
Sanjay Patel14ea0082018-08-30 15:05:38 +0000892 ConstantVector::get({C4, C5, C6, CU, CU, CU}));
893 EXPECT_FALSE(Id6->isIdentity());
894 EXPECT_TRUE(Id6->isIdentityWithPadding());
895 EXPECT_FALSE(Id6->isIdentityWithExtract());
Sanjay Patel1c86ce82018-09-20 15:21:52 +0000896 EXPECT_FALSE(Id6->isConcat());
Sanjay Patel14ea0082018-08-30 15:05:38 +0000897 delete Id6;
898
899 // Result has more elements than operands, but extra elements are not undef.
Sanjay Pateldff6e8d2018-09-20 14:36:09 +0000900 ShuffleVectorInst *Id7 = new ShuffleVectorInst(V0, V1,
Sanjay Patel14ea0082018-08-30 15:05:38 +0000901 ConstantVector::get({C0, C1, C2, C3, CU, C1}));
902 EXPECT_FALSE(Id7->isIdentity());
903 EXPECT_FALSE(Id7->isIdentityWithPadding());
904 EXPECT_FALSE(Id7->isIdentityWithExtract());
Sanjay Patel1c86ce82018-09-20 15:21:52 +0000905 EXPECT_FALSE(Id7->isConcat());
Sanjay Patel14ea0082018-08-30 15:05:38 +0000906 delete Id7;
907
908 // Result has more elements than operands; choose from Op0 and Op1 is not identity.
Sanjay Pateldff6e8d2018-09-20 14:36:09 +0000909 ShuffleVectorInst *Id8 = new ShuffleVectorInst(V0, V1,
Sanjay Patel14ea0082018-08-30 15:05:38 +0000910 ConstantVector::get({C4, CU, C2, C3, CU, CU}));
911 EXPECT_FALSE(Id8->isIdentity());
912 EXPECT_FALSE(Id8->isIdentityWithPadding());
913 EXPECT_FALSE(Id8->isIdentityWithExtract());
Sanjay Patel1c86ce82018-09-20 15:21:52 +0000914 EXPECT_FALSE(Id8->isConcat());
Sanjay Patel14ea0082018-08-30 15:05:38 +0000915 delete Id8;
Sanjay Patel1c86ce82018-09-20 15:21:52 +0000916
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 Patel42f462a2018-06-19 18:44:00 +0000953}
954
Vedant Kumar83a64512018-06-19 23:42:17 +0000955TEST(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 Kumarab0a33b2018-06-26 21:16:59 +0000988 EXPECT_EQ(Term->getIterator(), skipDebugIntrinsics(BB.begin()));
Vedant Kumar83a64512018-06-19 23:42:17 +0000989
990 // After the terminator, there are no non-debug instructions.
991 EXPECT_EQ(nullptr, Term->getNextNonDebugInstruction());
992}
993
Joseph Tremoulet0d05e6c2016-01-14 06:21:42 +0000994} // end anonymous namespace
995} // end namespace llvm