blob: 619ddc5413df04cb9593ab2dcbee496664b31016 [file] [log] [blame]
Chandler Carruth74b6a772013-01-07 15:35:46 +00001//===- llvm/unittest/IR/InstructionsTest.cpp - Instructions unit tests ----===//
Gabor Greif15580382010-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
Chandler Carruth9fb823b2013-01-02 11:36:10 +000010#include "llvm/IR/Instructions.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000011#include "llvm/ADT/STLExtras.h"
12#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000013#include "llvm/IR/BasicBlock.h"
14#include "llvm/IR/Constants.h"
15#include "llvm/IR/DataLayout.h"
16#include "llvm/IR/DerivedTypes.h"
Eli Bendersky84aa5e52014-03-26 20:41:15 +000017#include "llvm/IR/Function.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/IRBuilder.h"
19#include "llvm/IR/LLVMContext.h"
20#include "llvm/IR/MDBuilder.h"
Eli Bendersky84aa5e52014-03-26 20:41:15 +000021#include "llvm/IR/Module.h"
Sanjoy Dasaa722ae2017-02-23 22:50:52 +000022#include "llvm/IR/NoFolder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Operator.h"
Zvi Rackoverdfbd3d72017-05-08 12:40:18 +000024#include "gmock/gmock-matchers.h"
Gabor Greif15580382010-03-16 09:55:46 +000025#include "gtest/gtest.h"
Eli Bendersky84aa5e52014-03-26 20:41:15 +000026#include <memory>
Gabor Greif15580382010-03-16 09:55:46 +000027
28namespace llvm {
29namespace {
30
Gabor Greif35a9b8b2010-03-16 10:59:48 +000031TEST(InstructionsTest, ReturnInst) {
Mehdi Amini03b42e42016-04-14 21:59:01 +000032 LLVMContext C;
Gabor Greif15580382010-03-16 09:55:46 +000033
Gabor Greif35a9b8b2010-03-16 10:59:48 +000034 // test for PR6589
Gabor Greif15580382010-03-16 09:55:46 +000035 const ReturnInst* r0 = ReturnInst::Create(C);
Gabor Greifc377afc2010-03-16 15:26:09 +000036 EXPECT_EQ(r0->getNumOperands(), 0U);
Gabor Greif35a9b8b2010-03-16 10:59:48 +000037 EXPECT_EQ(r0->op_begin(), r0->op_end());
Gabor Greif86ca5492010-03-16 11:24:53 +000038
Chris Lattner229907c2011-07-18 04:54:35 +000039 IntegerType* Int1 = IntegerType::get(C, 1);
Gabor Greif86ca5492010-03-16 11:24:53 +000040 Constant* One = ConstantInt::get(Int1, 1, true);
41 const ReturnInst* r1 = ReturnInst::Create(C, One);
John McCalle83797c2011-08-27 19:23:22 +000042 EXPECT_EQ(1U, r1->getNumOperands());
Gabor Greif86ca5492010-03-16 11:24:53 +000043 User::const_op_iterator b(r1->op_begin());
John McCalle83797c2011-08-27 19:23:22 +000044 EXPECT_NE(r1->op_end(), b);
45 EXPECT_EQ(One, *b);
46 EXPECT_EQ(One, r1->getOperand(0));
Gabor Greif86ca5492010-03-16 11:24:53 +000047 ++b;
John McCalle83797c2011-08-27 19:23:22 +000048 EXPECT_EQ(r1->op_end(), b);
Gabor Greif421dd122010-03-16 12:32:03 +000049
50 // clean up
51 delete r0;
52 delete r1;
Gabor Greif15580382010-03-16 09:55:46 +000053}
54
Eli Bendersky84741622014-03-26 21:46:24 +000055// Test fixture that provides a module and a single function within it. Useful
56// for tests that need to refer to the function in some way.
57class ModuleWithFunctionTest : public testing::Test {
58protected:
NAKAMURA Takumibe8556d2014-03-27 11:32:41 +000059 ModuleWithFunctionTest() : M(new Module("MyModule", Ctx)) {
NAKAMURA Takumicb5ebf62014-03-27 11:38:28 +000060 FArgTypes.push_back(Type::getInt8Ty(Ctx));
61 FArgTypes.push_back(Type::getInt32Ty(Ctx));
62 FArgTypes.push_back(Type::getInt64Ty(Ctx));
Eli Bendersky84741622014-03-26 21:46:24 +000063 FunctionType *FTy =
64 FunctionType::get(Type::getVoidTy(Ctx), FArgTypes, false);
65 F = Function::Create(FTy, Function::ExternalLinkage, "", M.get());
66 }
Eli Bendersky84aa5e52014-03-26 20:41:15 +000067
Eli Bendersky84741622014-03-26 21:46:24 +000068 LLVMContext Ctx;
69 std::unique_ptr<Module> M;
NAKAMURA Takumicce8a582014-03-27 11:33:11 +000070 SmallVector<Type *, 3> FArgTypes;
Eli Bendersky84741622014-03-26 21:46:24 +000071 Function *F;
72};
Eli Bendersky84aa5e52014-03-26 20:41:15 +000073
Eli Bendersky84741622014-03-26 21:46:24 +000074TEST_F(ModuleWithFunctionTest, CallInst) {
75 Value *Args[] = {ConstantInt::get(Type::getInt8Ty(Ctx), 20),
76 ConstantInt::get(Type::getInt32Ty(Ctx), 9999),
77 ConstantInt::get(Type::getInt64Ty(Ctx), 42)};
Eli Benderskyc35c4b32014-03-26 21:11:34 +000078 std::unique_ptr<CallInst> Call(CallInst::Create(F, Args));
Eli Bendersky84aa5e52014-03-26 20:41:15 +000079
80 // Make sure iteration over a call's arguments works as expected.
81 unsigned Idx = 0;
82 for (Value *Arg : Call->arg_operands()) {
Eli Bendersky84741622014-03-26 21:46:24 +000083 EXPECT_EQ(FArgTypes[Idx], Arg->getType());
Eli Bendersky84aa5e52014-03-26 20:41:15 +000084 EXPECT_EQ(Call->getArgOperand(Idx)->getType(), Arg->getType());
85 Idx++;
86 }
87}
88
Eli Bendersky84741622014-03-26 21:46:24 +000089TEST_F(ModuleWithFunctionTest, InvokeInst) {
90 BasicBlock *BB1 = BasicBlock::Create(Ctx, "", F);
91 BasicBlock *BB2 = BasicBlock::Create(Ctx, "", F);
92
93 Value *Args[] = {ConstantInt::get(Type::getInt8Ty(Ctx), 20),
94 ConstantInt::get(Type::getInt32Ty(Ctx), 9999),
95 ConstantInt::get(Type::getInt64Ty(Ctx), 42)};
96 std::unique_ptr<InvokeInst> Invoke(InvokeInst::Create(F, BB1, BB2, Args));
97
98 // Make sure iteration over invoke's arguments works as expected.
99 unsigned Idx = 0;
100 for (Value *Arg : Invoke->arg_operands()) {
101 EXPECT_EQ(FArgTypes[Idx], Arg->getType());
102 EXPECT_EQ(Invoke->getArgOperand(Idx)->getType(), Arg->getType());
103 Idx++;
104 }
105}
106
Gabor Greifc377afc2010-03-16 15:26:09 +0000107TEST(InstructionsTest, BranchInst) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000108 LLVMContext C;
Gabor Greifc377afc2010-03-16 15:26:09 +0000109
110 // Make a BasicBlocks
111 BasicBlock* bb0 = BasicBlock::Create(C);
112 BasicBlock* bb1 = BasicBlock::Create(C);
113
114 // Mandatory BranchInst
115 const BranchInst* b0 = BranchInst::Create(bb0);
116
Gabor Greife52f3982010-03-16 15:53:58 +0000117 EXPECT_TRUE(b0->isUnconditional());
118 EXPECT_FALSE(b0->isConditional());
John McCalle83797c2011-08-27 19:23:22 +0000119 EXPECT_EQ(1U, b0->getNumSuccessors());
Gabor Greife52f3982010-03-16 15:53:58 +0000120
Gabor Greifc377afc2010-03-16 15:26:09 +0000121 // check num operands
John McCalle83797c2011-08-27 19:23:22 +0000122 EXPECT_EQ(1U, b0->getNumOperands());
Gabor Greifc377afc2010-03-16 15:26:09 +0000123
124 EXPECT_NE(b0->op_begin(), b0->op_end());
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000125 EXPECT_EQ(b0->op_end(), std::next(b0->op_begin()));
Gabor Greife52f3982010-03-16 15:53:58 +0000126
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000127 EXPECT_EQ(b0->op_end(), std::next(b0->op_begin()));
Gabor Greifc377afc2010-03-16 15:26:09 +0000128
Chris Lattner229907c2011-07-18 04:54:35 +0000129 IntegerType* Int1 = IntegerType::get(C, 1);
Gabor Greifc377afc2010-03-16 15:26:09 +0000130 Constant* One = ConstantInt::get(Int1, 1, true);
131
132 // Conditional BranchInst
133 BranchInst* b1 = BranchInst::Create(bb0, bb1, One);
134
Gabor Greife52f3982010-03-16 15:53:58 +0000135 EXPECT_FALSE(b1->isUnconditional());
136 EXPECT_TRUE(b1->isConditional());
John McCalle83797c2011-08-27 19:23:22 +0000137 EXPECT_EQ(2U, b1->getNumSuccessors());
Gabor Greife52f3982010-03-16 15:53:58 +0000138
Gabor Greifc377afc2010-03-16 15:26:09 +0000139 // check num operands
John McCalle83797c2011-08-27 19:23:22 +0000140 EXPECT_EQ(3U, b1->getNumOperands());
Gabor Greifc377afc2010-03-16 15:26:09 +0000141
142 User::const_op_iterator b(b1->op_begin());
143
144 // check COND
145 EXPECT_NE(b, b1->op_end());
John McCalle83797c2011-08-27 19:23:22 +0000146 EXPECT_EQ(One, *b);
147 EXPECT_EQ(One, b1->getOperand(0));
148 EXPECT_EQ(One, b1->getCondition());
Gabor Greifc377afc2010-03-16 15:26:09 +0000149 ++b;
150
151 // check ELSE
John McCalle83797c2011-08-27 19:23:22 +0000152 EXPECT_EQ(bb1, *b);
153 EXPECT_EQ(bb1, b1->getOperand(1));
154 EXPECT_EQ(bb1, b1->getSuccessor(1));
Gabor Greifc377afc2010-03-16 15:26:09 +0000155 ++b;
156
157 // check THEN
John McCalle83797c2011-08-27 19:23:22 +0000158 EXPECT_EQ(bb0, *b);
159 EXPECT_EQ(bb0, b1->getOperand(2));
160 EXPECT_EQ(bb0, b1->getSuccessor(0));
Gabor Greifc377afc2010-03-16 15:26:09 +0000161 ++b;
162
John McCalle83797c2011-08-27 19:23:22 +0000163 EXPECT_EQ(b1->op_end(), b);
Gabor Greifc377afc2010-03-16 15:26:09 +0000164
Gabor Greifc377afc2010-03-16 15:26:09 +0000165 // clean up
166 delete b0;
167 delete b1;
168
169 delete bb0;
170 delete bb1;
171}
172
Duncan Sands2d3cdd62011-04-01 03:34:54 +0000173TEST(InstructionsTest, CastInst) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000174 LLVMContext C;
Duncan Sands2d3cdd62011-04-01 03:34:54 +0000175
Matt Arsenaultcacbb232013-07-30 20:45:05 +0000176 Type *Int8Ty = Type::getInt8Ty(C);
177 Type *Int16Ty = Type::getInt16Ty(C);
178 Type *Int32Ty = Type::getInt32Ty(C);
179 Type *Int64Ty = Type::getInt64Ty(C);
180 Type *V8x8Ty = VectorType::get(Int8Ty, 8);
181 Type *V8x64Ty = VectorType::get(Int64Ty, 8);
182 Type *X86MMXTy = Type::getX86_MMXTy(C);
183
184 Type *HalfTy = Type::getHalfTy(C);
185 Type *FloatTy = Type::getFloatTy(C);
186 Type *DoubleTy = Type::getDoubleTy(C);
187
188 Type *V2Int32Ty = VectorType::get(Int32Ty, 2);
189 Type *V2Int64Ty = VectorType::get(Int64Ty, 2);
190 Type *V4Int16Ty = VectorType::get(Int16Ty, 4);
191
192 Type *Int32PtrTy = PointerType::get(Int32Ty, 0);
193 Type *Int64PtrTy = PointerType::get(Int64Ty, 0);
194
195 Type *Int32PtrAS1Ty = PointerType::get(Int32Ty, 1);
196 Type *Int64PtrAS1Ty = PointerType::get(Int64Ty, 1);
197
198 Type *V2Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 2);
199 Type *V2Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 2);
200 Type *V4Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 4);
201 Type *V4Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 4);
202
203 Type *V2Int64PtrTy = VectorType::get(Int64PtrTy, 2);
204 Type *V2Int32PtrTy = VectorType::get(Int32PtrTy, 2);
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +0000205 Type *V4Int32PtrTy = VectorType::get(Int32PtrTy, 4);
Duncan Sands2d3cdd62011-04-01 03:34:54 +0000206
Duncan Sandsa8514532011-05-18 07:13:41 +0000207 const Constant* c8 = Constant::getNullValue(V8x8Ty);
208 const Constant* c64 = Constant::getNullValue(V8x64Ty);
209
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +0000210 const Constant *v2ptr32 = Constant::getNullValue(V2Int32PtrTy);
211
Matt Arsenaultb4019ae2013-07-30 22:02:14 +0000212 EXPECT_TRUE(CastInst::isCastable(V8x8Ty, X86MMXTy));
213 EXPECT_TRUE(CastInst::isCastable(X86MMXTy, V8x8Ty));
214 EXPECT_FALSE(CastInst::isCastable(Int64Ty, X86MMXTy));
215 EXPECT_TRUE(CastInst::isCastable(V8x64Ty, V8x8Ty));
216 EXPECT_TRUE(CastInst::isCastable(V8x8Ty, V8x64Ty));
John McCalle83797c2011-08-27 19:23:22 +0000217 EXPECT_EQ(CastInst::Trunc, CastInst::getCastOpcode(c64, true, V8x8Ty, true));
218 EXPECT_EQ(CastInst::SExt, CastInst::getCastOpcode(c8, true, V8x64Ty, true));
Matt Arsenaultcacbb232013-07-30 20:45:05 +0000219
220 EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, X86MMXTy));
221 EXPECT_FALSE(CastInst::isBitCastable(X86MMXTy, V8x8Ty));
222 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, X86MMXTy));
223 EXPECT_FALSE(CastInst::isBitCastable(V8x64Ty, V8x8Ty));
224 EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, V8x64Ty));
225
226 // Check address space casts are rejected since we don't know the sizes here
227 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, Int32PtrAS1Ty));
228 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrAS1Ty, Int32PtrTy));
229 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, V2Int32PtrAS1Ty));
230 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int32PtrTy));
231 EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int64PtrAS1Ty));
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +0000232 EXPECT_TRUE(CastInst::isCastable(V2Int32PtrAS1Ty, V2Int32PtrTy));
233 EXPECT_EQ(CastInst::AddrSpaceCast, CastInst::getCastOpcode(v2ptr32, true,
234 V2Int32PtrAS1Ty,
235 true));
Matt Arsenaultcacbb232013-07-30 20:45:05 +0000236
237 // Test mismatched number of elements for pointers
238 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int64PtrAS1Ty));
239 EXPECT_FALSE(CastInst::isBitCastable(V4Int64PtrAS1Ty, V2Int32PtrAS1Ty));
240 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int32PtrAS1Ty));
241 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, V2Int32PtrTy));
242 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int32PtrTy));
243
244 EXPECT_TRUE(CastInst::isBitCastable(Int32PtrTy, Int64PtrTy));
245 EXPECT_FALSE(CastInst::isBitCastable(DoubleTy, FloatTy));
246 EXPECT_FALSE(CastInst::isBitCastable(FloatTy, DoubleTy));
247 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy));
248 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy));
249 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, Int32Ty));
250 EXPECT_TRUE(CastInst::isBitCastable(Int16Ty, HalfTy));
251 EXPECT_TRUE(CastInst::isBitCastable(Int32Ty, FloatTy));
252 EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, Int64Ty));
253
254 EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, V4Int16Ty));
255 EXPECT_FALSE(CastInst::isBitCastable(Int32Ty, Int64Ty));
256 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, Int32Ty));
257
258 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int64Ty));
259 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, V2Int32PtrTy));
260 EXPECT_TRUE(CastInst::isBitCastable(V2Int64PtrTy, V2Int32PtrTy));
261 EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrTy, V2Int64PtrTy));
262 EXPECT_FALSE(CastInst::isBitCastable(V2Int32Ty, V2Int64Ty));
263 EXPECT_FALSE(CastInst::isBitCastable(V2Int64Ty, V2Int32Ty));
Matt Arsenault065ced92013-07-31 00:17:33 +0000264
265
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +0000266 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
267 Constant::getNullValue(V4Int32PtrTy),
268 V2Int32PtrTy));
269 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
270 Constant::getNullValue(V2Int32PtrTy),
271 V4Int32PtrTy));
272
273 EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast,
274 Constant::getNullValue(V4Int32PtrAS1Ty),
275 V2Int32PtrTy));
276 EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast,
277 Constant::getNullValue(V2Int32PtrTy),
278 V4Int32PtrAS1Ty));
279
280
Matt Arsenault065ced92013-07-31 00:17:33 +0000281 // Check that assertion is not hit when creating a cast with a vector of
282 // pointers
283 // First form
284 BasicBlock *BB = BasicBlock::Create(C);
285 Constant *NullV2I32Ptr = Constant::getNullValue(V2Int32PtrTy);
Mehdi Amini03b42e42016-04-14 21:59:01 +0000286 auto Inst1 = CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty, "foo", BB);
Matt Arsenault065ced92013-07-31 00:17:33 +0000287
288 // Second form
Mehdi Amini03b42e42016-04-14 21:59:01 +0000289 auto Inst2 = CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty);
290
291 delete Inst2;
292 Inst1->eraseFromParent();
293 delete BB;
Duncan Sands2d3cdd62011-04-01 03:34:54 +0000294}
295
Nadav Rotem3924cb02011-12-05 06:29:09 +0000296TEST(InstructionsTest, VectorGep) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000297 LLVMContext C;
Nadav Rotem3924cb02011-12-05 06:29:09 +0000298
299 // Type Definitions
David Blaikieb3a39062015-03-14 21:40:10 +0000300 Type *I8Ty = IntegerType::get(C, 8);
301 Type *I32Ty = IntegerType::get(C, 32);
302 PointerType *Ptri8Ty = PointerType::get(I8Ty, 0);
303 PointerType *Ptri32Ty = PointerType::get(I32Ty, 0);
Nadav Rotem3924cb02011-12-05 06:29:09 +0000304
305 VectorType *V2xi8PTy = VectorType::get(Ptri8Ty, 2);
306 VectorType *V2xi32PTy = VectorType::get(Ptri32Ty, 2);
307
308 // Test different aspects of the vector-of-pointers type
309 // and GEPs which use this type.
310 ConstantInt *Ci32a = ConstantInt::get(C, APInt(32, 1492));
311 ConstantInt *Ci32b = ConstantInt::get(C, APInt(32, 1948));
312 std::vector<Constant*> ConstVa(2, Ci32a);
313 std::vector<Constant*> ConstVb(2, Ci32b);
314 Constant *C2xi32a = ConstantVector::get(ConstVa);
315 Constant *C2xi32b = ConstantVector::get(ConstVb);
316
317 CastInst *PtrVecA = new IntToPtrInst(C2xi32a, V2xi32PTy);
318 CastInst *PtrVecB = new IntToPtrInst(C2xi32b, V2xi32PTy);
319
320 ICmpInst *ICmp0 = new ICmpInst(ICmpInst::ICMP_SGT, PtrVecA, PtrVecB);
321 ICmpInst *ICmp1 = new ICmpInst(ICmpInst::ICMP_ULT, PtrVecA, PtrVecB);
322 EXPECT_NE(ICmp0, ICmp1); // suppress warning.
323
Evgeniy Stepanova259b262013-01-16 14:38:50 +0000324 BasicBlock* BB0 = BasicBlock::Create(C);
325 // Test InsertAtEnd ICmpInst constructor.
326 ICmpInst *ICmp2 = new ICmpInst(*BB0, ICmpInst::ICMP_SGE, PtrVecA, PtrVecB);
327 EXPECT_NE(ICmp0, ICmp2); // suppress warning.
328
David Blaikieb3a39062015-03-14 21:40:10 +0000329 GetElementPtrInst *Gep0 = GetElementPtrInst::Create(I32Ty, PtrVecA, C2xi32a);
330 GetElementPtrInst *Gep1 = GetElementPtrInst::Create(I32Ty, PtrVecA, C2xi32b);
331 GetElementPtrInst *Gep2 = GetElementPtrInst::Create(I32Ty, PtrVecB, C2xi32a);
332 GetElementPtrInst *Gep3 = GetElementPtrInst::Create(I32Ty, PtrVecB, C2xi32b);
Nadav Rotem3924cb02011-12-05 06:29:09 +0000333
334 CastInst *BTC0 = new BitCastInst(Gep0, V2xi8PTy);
335 CastInst *BTC1 = new BitCastInst(Gep1, V2xi8PTy);
336 CastInst *BTC2 = new BitCastInst(Gep2, V2xi8PTy);
337 CastInst *BTC3 = new BitCastInst(Gep3, V2xi8PTy);
338
339 Value *S0 = BTC0->stripPointerCasts();
340 Value *S1 = BTC1->stripPointerCasts();
341 Value *S2 = BTC2->stripPointerCasts();
342 Value *S3 = BTC3->stripPointerCasts();
343
344 EXPECT_NE(S0, Gep0);
345 EXPECT_NE(S1, Gep1);
346 EXPECT_NE(S2, Gep2);
347 EXPECT_NE(S3, Gep3);
348
349 int64_t Offset;
Micah Villmow9cfc13d2012-10-08 16:39:34 +0000350 DataLayout TD("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f3"
Rafael Espindolac6751622013-12-13 18:56:34 +0000351 "2:32:32-f64:64:64-v64:64:64-v128:128:128-a:0:64-s:64:64-f80"
Nadav Rotem3924cb02011-12-05 06:29:09 +0000352 ":128:128-n8:16:32:64-S128");
353 // Make sure we don't crash
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000354 GetPointerBaseWithConstantOffset(Gep0, Offset, TD);
355 GetPointerBaseWithConstantOffset(Gep1, Offset, TD);
356 GetPointerBaseWithConstantOffset(Gep2, Offset, TD);
357 GetPointerBaseWithConstantOffset(Gep3, Offset, TD);
Nadav Rotem3924cb02011-12-05 06:29:09 +0000358
359 // Gep of Geps
David Blaikieb3a39062015-03-14 21:40:10 +0000360 GetElementPtrInst *GepII0 = GetElementPtrInst::Create(I32Ty, Gep0, C2xi32b);
361 GetElementPtrInst *GepII1 = GetElementPtrInst::Create(I32Ty, Gep1, C2xi32a);
362 GetElementPtrInst *GepII2 = GetElementPtrInst::Create(I32Ty, Gep2, C2xi32b);
363 GetElementPtrInst *GepII3 = GetElementPtrInst::Create(I32Ty, Gep3, C2xi32a);
Nadav Rotem3924cb02011-12-05 06:29:09 +0000364
365 EXPECT_EQ(GepII0->getNumIndices(), 1u);
366 EXPECT_EQ(GepII1->getNumIndices(), 1u);
367 EXPECT_EQ(GepII2->getNumIndices(), 1u);
368 EXPECT_EQ(GepII3->getNumIndices(), 1u);
369
370 EXPECT_FALSE(GepII0->hasAllZeroIndices());
371 EXPECT_FALSE(GepII1->hasAllZeroIndices());
372 EXPECT_FALSE(GepII2->hasAllZeroIndices());
373 EXPECT_FALSE(GepII3->hasAllZeroIndices());
374
375 delete GepII0;
376 delete GepII1;
377 delete GepII2;
378 delete GepII3;
379
380 delete BTC0;
381 delete BTC1;
382 delete BTC2;
383 delete BTC3;
384
385 delete Gep0;
386 delete Gep1;
387 delete Gep2;
388 delete Gep3;
389
Evgeniy Stepanova259b262013-01-16 14:38:50 +0000390 ICmp2->eraseFromParent();
391 delete BB0;
392
Nadav Rotem3924cb02011-12-05 06:29:09 +0000393 delete ICmp0;
394 delete ICmp1;
395 delete PtrVecA;
396 delete PtrVecB;
397}
398
Duncan Sands05f4df82012-04-16 16:28:59 +0000399TEST(InstructionsTest, FPMathOperator) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000400 LLVMContext Context;
Duncan Sands05f4df82012-04-16 16:28:59 +0000401 IRBuilder<> Builder(Context);
402 MDBuilder MDHelper(Context);
403 Instruction *I = Builder.CreatePHI(Builder.getDoubleTy(), 0);
404 MDNode *MD1 = MDHelper.createFPMath(1.0);
Duncan Sands05f4df82012-04-16 16:28:59 +0000405 Value *V1 = Builder.CreateFAdd(I, I, "", MD1);
Duncan Sands05f4df82012-04-16 16:28:59 +0000406 EXPECT_TRUE(isa<FPMathOperator>(V1));
Duncan Sands05f4df82012-04-16 16:28:59 +0000407 FPMathOperator *O1 = cast<FPMathOperator>(V1);
Duncan Sands05f4df82012-04-16 16:28:59 +0000408 EXPECT_EQ(O1->getFPAccuracy(), 1.0);
Reid Kleckner96ab8722017-05-18 17:24:10 +0000409 V1->deleteValue();
410 I->deleteValue();
Duncan Sands05f4df82012-04-16 16:28:59 +0000411}
412
Duncan Sandse2395dc2012-10-30 16:03:32 +0000413
414TEST(InstructionsTest, isEliminableCastPair) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000415 LLVMContext C;
Duncan Sandse2395dc2012-10-30 16:03:32 +0000416
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000417 Type* Int16Ty = Type::getInt16Ty(C);
Duncan Sandse2395dc2012-10-30 16:03:32 +0000418 Type* Int32Ty = Type::getInt32Ty(C);
419 Type* Int64Ty = Type::getInt64Ty(C);
420 Type* Int64PtrTy = Type::getInt64PtrTy(C);
421
422 // Source and destination pointers have same size -> bitcast.
423 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
424 CastInst::IntToPtr,
425 Int64PtrTy, Int64Ty, Int64PtrTy,
Craig Topper66f09ad2014-06-08 22:29:17 +0000426 Int32Ty, nullptr, Int32Ty),
Duncan Sandse2395dc2012-10-30 16:03:32 +0000427 CastInst::BitCast);
428
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000429 // Source and destination have unknown sizes, but the same address space and
430 // the intermediate int is the maximum pointer size -> bitcast
Duncan Sandse2395dc2012-10-30 16:03:32 +0000431 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
432 CastInst::IntToPtr,
433 Int64PtrTy, Int64Ty, Int64PtrTy,
Craig Topper66f09ad2014-06-08 22:29:17 +0000434 nullptr, nullptr, nullptr),
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000435 CastInst::BitCast);
436
437 // Source and destination have unknown sizes, but the same address space and
438 // the intermediate int is not the maximum pointer size -> nothing
439 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
440 CastInst::IntToPtr,
441 Int64PtrTy, Int32Ty, Int64PtrTy,
Craig Topper66f09ad2014-06-08 22:29:17 +0000442 nullptr, nullptr, nullptr),
Duncan Sandse2395dc2012-10-30 16:03:32 +0000443 0U);
444
445 // Middle pointer big enough -> bitcast.
446 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
447 CastInst::PtrToInt,
448 Int64Ty, Int64PtrTy, Int64Ty,
Craig Topper66f09ad2014-06-08 22:29:17 +0000449 nullptr, Int64Ty, nullptr),
Duncan Sandse2395dc2012-10-30 16:03:32 +0000450 CastInst::BitCast);
451
452 // Middle pointer too small -> fail.
453 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
454 CastInst::PtrToInt,
455 Int64Ty, Int64PtrTy, Int64Ty,
Craig Topper66f09ad2014-06-08 22:29:17 +0000456 nullptr, Int32Ty, nullptr),
Duncan Sandse2395dc2012-10-30 16:03:32 +0000457 0U);
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000458
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000459 // Test that we don't eliminate bitcasts between different address spaces,
460 // or if we don't have available pointer size information.
461 DataLayout DL("e-p:32:32:32-p1:16:16:16-p2:64:64:64-i1:8:8-i8:8:8-i16:16:16"
462 "-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64"
Rafael Espindolac6751622013-12-13 18:56:34 +0000463 "-v128:128:128-a:0:64-s:64:64-f80:128:128-n8:16:32:64-S128");
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000464
465 Type* Int64PtrTyAS1 = Type::getInt64PtrTy(C, 1);
466 Type* Int64PtrTyAS2 = Type::getInt64PtrTy(C, 2);
467
468 IntegerType *Int16SizePtr = DL.getIntPtrType(C, 1);
469 IntegerType *Int64SizePtr = DL.getIntPtrType(C, 2);
470
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +0000471 // Cannot simplify inttoptr, addrspacecast
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000472 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +0000473 CastInst::AddrSpaceCast,
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000474 Int16Ty, Int64PtrTyAS1, Int64PtrTyAS2,
Craig Topper66f09ad2014-06-08 22:29:17 +0000475 nullptr, Int16SizePtr, Int64SizePtr),
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000476 0U);
477
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +0000478 // Cannot simplify addrspacecast, ptrtoint
479 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::AddrSpaceCast,
480 CastInst::PtrToInt,
481 Int64PtrTyAS1, Int64PtrTyAS2, Int16Ty,
Craig Topper66f09ad2014-06-08 22:29:17 +0000482 Int64SizePtr, Int16SizePtr, nullptr),
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +0000483 0U);
484
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000485 // Pass since the bitcast address spaces are the same
486 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
487 CastInst::BitCast,
488 Int16Ty, Int64PtrTyAS1, Int64PtrTyAS1,
Craig Topper66f09ad2014-06-08 22:29:17 +0000489 nullptr, nullptr, nullptr),
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000490 CastInst::IntToPtr);
491
Duncan Sandse2395dc2012-10-30 16:03:32 +0000492}
493
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000494TEST(InstructionsTest, CloneCall) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000495 LLVMContext C;
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000496 Type *Int32Ty = Type::getInt32Ty(C);
497 Type *ArgTys[] = {Int32Ty, Int32Ty, Int32Ty};
498 Type *FnTy = FunctionType::get(Int32Ty, ArgTys, /*isVarArg=*/false);
499 Value *Callee = Constant::getNullValue(FnTy->getPointerTo());
500 Value *Args[] = {
501 ConstantInt::get(Int32Ty, 1),
502 ConstantInt::get(Int32Ty, 2),
503 ConstantInt::get(Int32Ty, 3)
504 };
505 std::unique_ptr<CallInst> Call(CallInst::Create(Callee, Args, "result"));
506
507 // Test cloning the tail call kind.
508 CallInst::TailCallKind Kinds[] = {CallInst::TCK_None, CallInst::TCK_Tail,
509 CallInst::TCK_MustTail};
510 for (CallInst::TailCallKind TCK : Kinds) {
511 Call->setTailCallKind(TCK);
512 std::unique_ptr<CallInst> Clone(cast<CallInst>(Call->clone()));
513 EXPECT_EQ(Call->getTailCallKind(), Clone->getTailCallKind());
514 }
515 Call->setTailCallKind(CallInst::TCK_None);
516
517 // Test cloning an attribute.
518 {
519 AttrBuilder AB;
520 AB.addAttribute(Attribute::ReadOnly);
Reid Klecknerb5180542017-03-21 16:57:19 +0000521 Call->setAttributes(
522 AttributeList::get(C, AttributeList::FunctionIndex, AB));
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000523 std::unique_ptr<CallInst> Clone(cast<CallInst>(Call->clone()));
524 EXPECT_TRUE(Clone->onlyReadsMemory());
525 }
526}
527
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000528TEST(InstructionsTest, AlterCallBundles) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000529 LLVMContext C;
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000530 Type *Int32Ty = Type::getInt32Ty(C);
531 Type *FnTy = FunctionType::get(Int32Ty, Int32Ty, /*isVarArg=*/false);
532 Value *Callee = Constant::getNullValue(FnTy->getPointerTo());
533 Value *Args[] = {ConstantInt::get(Int32Ty, 42)};
534 OperandBundleDef OldBundle("before", UndefValue::get(Int32Ty));
535 std::unique_ptr<CallInst> Call(
536 CallInst::Create(Callee, Args, OldBundle, "result"));
537 Call->setTailCallKind(CallInst::TailCallKind::TCK_NoTail);
538 AttrBuilder AB;
539 AB.addAttribute(Attribute::Cold);
Reid Klecknerb5180542017-03-21 16:57:19 +0000540 Call->setAttributes(AttributeList::get(C, AttributeList::FunctionIndex, AB));
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000541 Call->setDebugLoc(DebugLoc(MDNode::get(C, None)));
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000542
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000543 OperandBundleDef NewBundle("after", ConstantInt::get(Int32Ty, 7));
544 std::unique_ptr<CallInst> Clone(CallInst::Create(Call.get(), NewBundle));
545 EXPECT_EQ(Call->getNumArgOperands(), Clone->getNumArgOperands());
546 EXPECT_EQ(Call->getArgOperand(0), Clone->getArgOperand(0));
547 EXPECT_EQ(Call->getCallingConv(), Clone->getCallingConv());
548 EXPECT_EQ(Call->getTailCallKind(), Clone->getTailCallKind());
549 EXPECT_TRUE(Clone->hasFnAttr(Attribute::AttrKind::Cold));
550 EXPECT_EQ(Call->getDebugLoc(), Clone->getDebugLoc());
Joseph Tremoulet56c99582016-01-14 06:30:19 +0000551 EXPECT_EQ(Clone->getNumOperandBundles(), 1U);
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000552 EXPECT_TRUE(Clone->getOperandBundle("after").hasValue());
553}
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000554
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000555TEST(InstructionsTest, AlterInvokeBundles) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000556 LLVMContext C;
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000557 Type *Int32Ty = Type::getInt32Ty(C);
558 Type *FnTy = FunctionType::get(Int32Ty, Int32Ty, /*isVarArg=*/false);
559 Value *Callee = Constant::getNullValue(FnTy->getPointerTo());
560 Value *Args[] = {ConstantInt::get(Int32Ty, 42)};
Joseph Tremouletf6cc7e62016-01-15 15:08:36 +0000561 std::unique_ptr<BasicBlock> NormalDest(BasicBlock::Create(C));
562 std::unique_ptr<BasicBlock> UnwindDest(BasicBlock::Create(C));
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000563 OperandBundleDef OldBundle("before", UndefValue::get(Int32Ty));
Joseph Tremouletf6cc7e62016-01-15 15:08:36 +0000564 std::unique_ptr<InvokeInst> Invoke(InvokeInst::Create(
565 Callee, NormalDest.get(), UnwindDest.get(), Args, OldBundle, "result"));
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000566 AttrBuilder AB;
567 AB.addAttribute(Attribute::Cold);
Reid Klecknerb5180542017-03-21 16:57:19 +0000568 Invoke->setAttributes(
569 AttributeList::get(C, AttributeList::FunctionIndex, AB));
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000570 Invoke->setDebugLoc(DebugLoc(MDNode::get(C, None)));
571
572 OperandBundleDef NewBundle("after", ConstantInt::get(Int32Ty, 7));
Joseph Tremouletf6cc7e62016-01-15 15:08:36 +0000573 std::unique_ptr<InvokeInst> Clone(
574 InvokeInst::Create(Invoke.get(), NewBundle));
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000575 EXPECT_EQ(Invoke->getNormalDest(), Clone->getNormalDest());
576 EXPECT_EQ(Invoke->getUnwindDest(), Clone->getUnwindDest());
577 EXPECT_EQ(Invoke->getNumArgOperands(), Clone->getNumArgOperands());
578 EXPECT_EQ(Invoke->getArgOperand(0), Clone->getArgOperand(0));
579 EXPECT_EQ(Invoke->getCallingConv(), Clone->getCallingConv());
580 EXPECT_TRUE(Clone->hasFnAttr(Attribute::AttrKind::Cold));
581 EXPECT_EQ(Invoke->getDebugLoc(), Clone->getDebugLoc());
NAKAMURA Takumi3557b882016-01-14 09:21:49 +0000582 EXPECT_EQ(Clone->getNumOperandBundles(), 1U);
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000583 EXPECT_TRUE(Clone->getOperandBundle("after").hasValue());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000584}
585
Sanjoy Dasaa722ae2017-02-23 22:50:52 +0000586TEST_F(ModuleWithFunctionTest, DropPoisonGeneratingFlags) {
587 auto *OnlyBB = BasicBlock::Create(Ctx, "bb", F);
588 auto *Arg0 = &*F->arg_begin();
589
590 IRBuilder<NoFolder> B(Ctx);
591 B.SetInsertPoint(OnlyBB);
592
593 {
594 auto *UI =
595 cast<Instruction>(B.CreateUDiv(Arg0, Arg0, "", /*isExact*/ true));
596 ASSERT_TRUE(UI->isExact());
597 UI->dropPoisonGeneratingFlags();
598 ASSERT_FALSE(UI->isExact());
599 }
600
601 {
602 auto *ShrI =
603 cast<Instruction>(B.CreateLShr(Arg0, Arg0, "", /*isExact*/ true));
604 ASSERT_TRUE(ShrI->isExact());
605 ShrI->dropPoisonGeneratingFlags();
606 ASSERT_FALSE(ShrI->isExact());
607 }
608
609 {
610 auto *AI = cast<Instruction>(
611 B.CreateAdd(Arg0, Arg0, "", /*HasNUW*/ true, /*HasNSW*/ false));
612 ASSERT_TRUE(AI->hasNoUnsignedWrap());
613 AI->dropPoisonGeneratingFlags();
614 ASSERT_FALSE(AI->hasNoUnsignedWrap());
615 ASSERT_FALSE(AI->hasNoSignedWrap());
616 }
617
618 {
619 auto *SI = cast<Instruction>(
620 B.CreateAdd(Arg0, Arg0, "", /*HasNUW*/ false, /*HasNSW*/ true));
621 ASSERT_TRUE(SI->hasNoSignedWrap());
622 SI->dropPoisonGeneratingFlags();
623 ASSERT_FALSE(SI->hasNoUnsignedWrap());
624 ASSERT_FALSE(SI->hasNoSignedWrap());
625 }
626
627 {
628 auto *ShlI = cast<Instruction>(
629 B.CreateShl(Arg0, Arg0, "", /*HasNUW*/ true, /*HasNSW*/ true));
630 ASSERT_TRUE(ShlI->hasNoSignedWrap());
631 ASSERT_TRUE(ShlI->hasNoUnsignedWrap());
632 ShlI->dropPoisonGeneratingFlags();
633 ASSERT_FALSE(ShlI->hasNoUnsignedWrap());
634 ASSERT_FALSE(ShlI->hasNoSignedWrap());
635 }
636
637 {
638 Value *GEPBase = Constant::getNullValue(B.getInt8PtrTy());
639 auto *GI = cast<GetElementPtrInst>(B.CreateInBoundsGEP(GEPBase, {Arg0}));
640 ASSERT_TRUE(GI->isInBounds());
641 GI->dropPoisonGeneratingFlags();
642 ASSERT_FALSE(GI->isInBounds());
643 }
644}
645
Chandler Carruthd1c95b62017-02-28 08:04:20 +0000646TEST(InstructionsTest, GEPIndices) {
647 LLVMContext Context;
648 IRBuilder<NoFolder> Builder(Context);
649 Type *ElementTy = Builder.getInt8Ty();
650 Type *ArrTy = ArrayType::get(ArrayType::get(ElementTy, 64), 64);
651 Value *Indices[] = {
652 Builder.getInt32(0),
653 Builder.getInt32(13),
654 Builder.getInt32(42) };
655
656 Value *V = Builder.CreateGEP(ArrTy, UndefValue::get(PointerType::getUnqual(ArrTy)),
657 Indices);
658 ASSERT_TRUE(isa<GetElementPtrInst>(V));
659
660 auto *GEPI = cast<GetElementPtrInst>(V);
661 ASSERT_NE(GEPI->idx_begin(), GEPI->idx_end());
662 ASSERT_EQ(GEPI->idx_end(), std::next(GEPI->idx_begin(), 3));
663 EXPECT_EQ(Indices[0], GEPI->idx_begin()[0]);
664 EXPECT_EQ(Indices[1], GEPI->idx_begin()[1]);
665 EXPECT_EQ(Indices[2], GEPI->idx_begin()[2]);
666 EXPECT_EQ(GEPI->idx_begin(), GEPI->indices().begin());
667 EXPECT_EQ(GEPI->idx_end(), GEPI->indices().end());
668
669 const auto *CGEPI = GEPI;
670 ASSERT_NE(CGEPI->idx_begin(), CGEPI->idx_end());
671 ASSERT_EQ(CGEPI->idx_end(), std::next(CGEPI->idx_begin(), 3));
672 EXPECT_EQ(Indices[0], CGEPI->idx_begin()[0]);
673 EXPECT_EQ(Indices[1], CGEPI->idx_begin()[1]);
674 EXPECT_EQ(Indices[2], CGEPI->idx_begin()[2]);
675 EXPECT_EQ(CGEPI->idx_begin(), CGEPI->indices().begin());
676 EXPECT_EQ(CGEPI->idx_end(), CGEPI->indices().end());
677
678 delete GEPI;
679}
680
Chandler Carruth927d8e62017-04-12 07:27:28 +0000681TEST(InstructionsTest, SwitchInst) {
682 LLVMContext C;
683
684 std::unique_ptr<BasicBlock> BB1, BB2, BB3;
685 BB1.reset(BasicBlock::Create(C));
686 BB2.reset(BasicBlock::Create(C));
687 BB3.reset(BasicBlock::Create(C));
688
689 // We create block 0 after the others so that it gets destroyed first and
690 // clears the uses of the other basic blocks.
691 std::unique_ptr<BasicBlock> BB0(BasicBlock::Create(C));
692
693 auto *Int32Ty = Type::getInt32Ty(C);
694
695 SwitchInst *SI =
696 SwitchInst::Create(UndefValue::get(Int32Ty), BB0.get(), 3, BB0.get());
697 SI->addCase(ConstantInt::get(Int32Ty, 1), BB1.get());
698 SI->addCase(ConstantInt::get(Int32Ty, 2), BB2.get());
699 SI->addCase(ConstantInt::get(Int32Ty, 3), BB3.get());
700
701 auto CI = SI->case_begin();
702 ASSERT_NE(CI, SI->case_end());
703 EXPECT_EQ(1, CI->getCaseValue()->getSExtValue());
704 EXPECT_EQ(BB1.get(), CI->getCaseSuccessor());
705 EXPECT_EQ(2, (CI + 1)->getCaseValue()->getSExtValue());
706 EXPECT_EQ(BB2.get(), (CI + 1)->getCaseSuccessor());
707 EXPECT_EQ(3, (CI + 2)->getCaseValue()->getSExtValue());
708 EXPECT_EQ(BB3.get(), (CI + 2)->getCaseSuccessor());
709 EXPECT_EQ(CI + 1, std::next(CI));
710 EXPECT_EQ(CI + 2, std::next(CI, 2));
711 EXPECT_EQ(CI + 3, std::next(CI, 3));
712 EXPECT_EQ(SI->case_end(), CI + 3);
713 EXPECT_EQ(0, CI - CI);
714 EXPECT_EQ(1, (CI + 1) - CI);
715 EXPECT_EQ(2, (CI + 2) - CI);
716 EXPECT_EQ(3, SI->case_end() - CI);
717 EXPECT_EQ(3, std::distance(CI, SI->case_end()));
718
719 auto CCI = const_cast<const SwitchInst *>(SI)->case_begin();
720 SwitchInst::ConstCaseIt CCE = SI->case_end();
721 ASSERT_NE(CCI, SI->case_end());
722 EXPECT_EQ(1, CCI->getCaseValue()->getSExtValue());
723 EXPECT_EQ(BB1.get(), CCI->getCaseSuccessor());
724 EXPECT_EQ(2, (CCI + 1)->getCaseValue()->getSExtValue());
725 EXPECT_EQ(BB2.get(), (CCI + 1)->getCaseSuccessor());
726 EXPECT_EQ(3, (CCI + 2)->getCaseValue()->getSExtValue());
727 EXPECT_EQ(BB3.get(), (CCI + 2)->getCaseSuccessor());
728 EXPECT_EQ(CCI + 1, std::next(CCI));
729 EXPECT_EQ(CCI + 2, std::next(CCI, 2));
730 EXPECT_EQ(CCI + 3, std::next(CCI, 3));
731 EXPECT_EQ(CCE, CCI + 3);
732 EXPECT_EQ(0, CCI - CCI);
733 EXPECT_EQ(1, (CCI + 1) - CCI);
734 EXPECT_EQ(2, (CCI + 2) - CCI);
735 EXPECT_EQ(3, CCE - CCI);
736 EXPECT_EQ(3, std::distance(CCI, CCE));
737
738 // Make sure that the const iterator is compatible with a const auto ref.
739 const auto &Handle = *CCI;
740 EXPECT_EQ(1, Handle.getCaseValue()->getSExtValue());
741 EXPECT_EQ(BB1.get(), Handle.getCaseSuccessor());
742}
743
Zvi Rackoverdfbd3d72017-05-08 12:40:18 +0000744TEST(InstructionsTest, CommuteShuffleMask) {
745 SmallVector<int, 16> Indices({-1, 0, 7});
746 ShuffleVectorInst::commuteShuffleMask(Indices, 4);
747 EXPECT_THAT(Indices, testing::ContainerEq(ArrayRef<int>({-1, 4, 3})));
748}
749
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000750} // end anonymous namespace
751} // end namespace llvm