blob: 7c75aaec1753982f458d374db94367bbff242b12 [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"
Gabor Greif15580382010-03-16 09:55:46 +000024#include "gtest/gtest.h"
Eli Bendersky84aa5e52014-03-26 20:41:15 +000025#include <memory>
Gabor Greif15580382010-03-16 09:55:46 +000026
27namespace llvm {
28namespace {
29
Gabor Greif35a9b8b2010-03-16 10:59:48 +000030TEST(InstructionsTest, ReturnInst) {
Mehdi Amini03b42e42016-04-14 21:59:01 +000031 LLVMContext C;
Gabor Greif15580382010-03-16 09:55:46 +000032
Gabor Greif35a9b8b2010-03-16 10:59:48 +000033 // test for PR6589
Gabor Greif15580382010-03-16 09:55:46 +000034 const ReturnInst* r0 = ReturnInst::Create(C);
Gabor Greifc377afc2010-03-16 15:26:09 +000035 EXPECT_EQ(r0->getNumOperands(), 0U);
Gabor Greif35a9b8b2010-03-16 10:59:48 +000036 EXPECT_EQ(r0->op_begin(), r0->op_end());
Gabor Greif86ca5492010-03-16 11:24:53 +000037
Chris Lattner229907c2011-07-18 04:54:35 +000038 IntegerType* Int1 = IntegerType::get(C, 1);
Gabor Greif86ca5492010-03-16 11:24:53 +000039 Constant* One = ConstantInt::get(Int1, 1, true);
40 const ReturnInst* r1 = ReturnInst::Create(C, One);
John McCalle83797c2011-08-27 19:23:22 +000041 EXPECT_EQ(1U, r1->getNumOperands());
Gabor Greif86ca5492010-03-16 11:24:53 +000042 User::const_op_iterator b(r1->op_begin());
John McCalle83797c2011-08-27 19:23:22 +000043 EXPECT_NE(r1->op_end(), b);
44 EXPECT_EQ(One, *b);
45 EXPECT_EQ(One, r1->getOperand(0));
Gabor Greif86ca5492010-03-16 11:24:53 +000046 ++b;
John McCalle83797c2011-08-27 19:23:22 +000047 EXPECT_EQ(r1->op_end(), b);
Gabor Greif421dd122010-03-16 12:32:03 +000048
49 // clean up
50 delete r0;
51 delete r1;
Gabor Greif15580382010-03-16 09:55:46 +000052}
53
Eli Bendersky84741622014-03-26 21:46:24 +000054// Test fixture that provides a module and a single function within it. Useful
55// for tests that need to refer to the function in some way.
56class ModuleWithFunctionTest : public testing::Test {
57protected:
NAKAMURA Takumibe8556d2014-03-27 11:32:41 +000058 ModuleWithFunctionTest() : M(new Module("MyModule", Ctx)) {
NAKAMURA Takumicb5ebf62014-03-27 11:38:28 +000059 FArgTypes.push_back(Type::getInt8Ty(Ctx));
60 FArgTypes.push_back(Type::getInt32Ty(Ctx));
61 FArgTypes.push_back(Type::getInt64Ty(Ctx));
Eli Bendersky84741622014-03-26 21:46:24 +000062 FunctionType *FTy =
63 FunctionType::get(Type::getVoidTy(Ctx), FArgTypes, false);
64 F = Function::Create(FTy, Function::ExternalLinkage, "", M.get());
65 }
Eli Bendersky84aa5e52014-03-26 20:41:15 +000066
Eli Bendersky84741622014-03-26 21:46:24 +000067 LLVMContext Ctx;
68 std::unique_ptr<Module> M;
NAKAMURA Takumicce8a582014-03-27 11:33:11 +000069 SmallVector<Type *, 3> FArgTypes;
Eli Bendersky84741622014-03-26 21:46:24 +000070 Function *F;
71};
Eli Bendersky84aa5e52014-03-26 20:41:15 +000072
Eli Bendersky84741622014-03-26 21:46:24 +000073TEST_F(ModuleWithFunctionTest, CallInst) {
74 Value *Args[] = {ConstantInt::get(Type::getInt8Ty(Ctx), 20),
75 ConstantInt::get(Type::getInt32Ty(Ctx), 9999),
76 ConstantInt::get(Type::getInt64Ty(Ctx), 42)};
Eli Benderskyc35c4b32014-03-26 21:11:34 +000077 std::unique_ptr<CallInst> Call(CallInst::Create(F, Args));
Eli Bendersky84aa5e52014-03-26 20:41:15 +000078
79 // Make sure iteration over a call's arguments works as expected.
80 unsigned Idx = 0;
81 for (Value *Arg : Call->arg_operands()) {
Eli Bendersky84741622014-03-26 21:46:24 +000082 EXPECT_EQ(FArgTypes[Idx], Arg->getType());
Eli Bendersky84aa5e52014-03-26 20:41:15 +000083 EXPECT_EQ(Call->getArgOperand(Idx)->getType(), Arg->getType());
84 Idx++;
85 }
86}
87
Eli Bendersky84741622014-03-26 21:46:24 +000088TEST_F(ModuleWithFunctionTest, InvokeInst) {
89 BasicBlock *BB1 = BasicBlock::Create(Ctx, "", F);
90 BasicBlock *BB2 = BasicBlock::Create(Ctx, "", F);
91
92 Value *Args[] = {ConstantInt::get(Type::getInt8Ty(Ctx), 20),
93 ConstantInt::get(Type::getInt32Ty(Ctx), 9999),
94 ConstantInt::get(Type::getInt64Ty(Ctx), 42)};
95 std::unique_ptr<InvokeInst> Invoke(InvokeInst::Create(F, BB1, BB2, Args));
96
97 // Make sure iteration over invoke's arguments works as expected.
98 unsigned Idx = 0;
99 for (Value *Arg : Invoke->arg_operands()) {
100 EXPECT_EQ(FArgTypes[Idx], Arg->getType());
101 EXPECT_EQ(Invoke->getArgOperand(Idx)->getType(), Arg->getType());
102 Idx++;
103 }
104}
105
Gabor Greifc377afc2010-03-16 15:26:09 +0000106TEST(InstructionsTest, BranchInst) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000107 LLVMContext C;
Gabor Greifc377afc2010-03-16 15:26:09 +0000108
109 // Make a BasicBlocks
110 BasicBlock* bb0 = BasicBlock::Create(C);
111 BasicBlock* bb1 = BasicBlock::Create(C);
112
113 // Mandatory BranchInst
114 const BranchInst* b0 = BranchInst::Create(bb0);
115
Gabor Greife52f3982010-03-16 15:53:58 +0000116 EXPECT_TRUE(b0->isUnconditional());
117 EXPECT_FALSE(b0->isConditional());
John McCalle83797c2011-08-27 19:23:22 +0000118 EXPECT_EQ(1U, b0->getNumSuccessors());
Gabor Greife52f3982010-03-16 15:53:58 +0000119
Gabor Greifc377afc2010-03-16 15:26:09 +0000120 // check num operands
John McCalle83797c2011-08-27 19:23:22 +0000121 EXPECT_EQ(1U, b0->getNumOperands());
Gabor Greifc377afc2010-03-16 15:26:09 +0000122
123 EXPECT_NE(b0->op_begin(), b0->op_end());
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000124 EXPECT_EQ(b0->op_end(), std::next(b0->op_begin()));
Gabor Greife52f3982010-03-16 15:53:58 +0000125
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000126 EXPECT_EQ(b0->op_end(), std::next(b0->op_begin()));
Gabor Greifc377afc2010-03-16 15:26:09 +0000127
Chris Lattner229907c2011-07-18 04:54:35 +0000128 IntegerType* Int1 = IntegerType::get(C, 1);
Gabor Greifc377afc2010-03-16 15:26:09 +0000129 Constant* One = ConstantInt::get(Int1, 1, true);
130
131 // Conditional BranchInst
132 BranchInst* b1 = BranchInst::Create(bb0, bb1, One);
133
Gabor Greife52f3982010-03-16 15:53:58 +0000134 EXPECT_FALSE(b1->isUnconditional());
135 EXPECT_TRUE(b1->isConditional());
John McCalle83797c2011-08-27 19:23:22 +0000136 EXPECT_EQ(2U, b1->getNumSuccessors());
Gabor Greife52f3982010-03-16 15:53:58 +0000137
Gabor Greifc377afc2010-03-16 15:26:09 +0000138 // check num operands
John McCalle83797c2011-08-27 19:23:22 +0000139 EXPECT_EQ(3U, b1->getNumOperands());
Gabor Greifc377afc2010-03-16 15:26:09 +0000140
141 User::const_op_iterator b(b1->op_begin());
142
143 // check COND
144 EXPECT_NE(b, b1->op_end());
John McCalle83797c2011-08-27 19:23:22 +0000145 EXPECT_EQ(One, *b);
146 EXPECT_EQ(One, b1->getOperand(0));
147 EXPECT_EQ(One, b1->getCondition());
Gabor Greifc377afc2010-03-16 15:26:09 +0000148 ++b;
149
150 // check ELSE
John McCalle83797c2011-08-27 19:23:22 +0000151 EXPECT_EQ(bb1, *b);
152 EXPECT_EQ(bb1, b1->getOperand(1));
153 EXPECT_EQ(bb1, b1->getSuccessor(1));
Gabor Greifc377afc2010-03-16 15:26:09 +0000154 ++b;
155
156 // check THEN
John McCalle83797c2011-08-27 19:23:22 +0000157 EXPECT_EQ(bb0, *b);
158 EXPECT_EQ(bb0, b1->getOperand(2));
159 EXPECT_EQ(bb0, b1->getSuccessor(0));
Gabor Greifc377afc2010-03-16 15:26:09 +0000160 ++b;
161
John McCalle83797c2011-08-27 19:23:22 +0000162 EXPECT_EQ(b1->op_end(), b);
Gabor Greifc377afc2010-03-16 15:26:09 +0000163
Gabor Greifc377afc2010-03-16 15:26:09 +0000164 // clean up
165 delete b0;
166 delete b1;
167
168 delete bb0;
169 delete bb1;
170}
171
Duncan Sands2d3cdd62011-04-01 03:34:54 +0000172TEST(InstructionsTest, CastInst) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000173 LLVMContext C;
Duncan Sands2d3cdd62011-04-01 03:34:54 +0000174
Matt Arsenaultcacbb232013-07-30 20:45:05 +0000175 Type *Int8Ty = Type::getInt8Ty(C);
176 Type *Int16Ty = Type::getInt16Ty(C);
177 Type *Int32Ty = Type::getInt32Ty(C);
178 Type *Int64Ty = Type::getInt64Ty(C);
179 Type *V8x8Ty = VectorType::get(Int8Ty, 8);
180 Type *V8x64Ty = VectorType::get(Int64Ty, 8);
181 Type *X86MMXTy = Type::getX86_MMXTy(C);
182
183 Type *HalfTy = Type::getHalfTy(C);
184 Type *FloatTy = Type::getFloatTy(C);
185 Type *DoubleTy = Type::getDoubleTy(C);
186
187 Type *V2Int32Ty = VectorType::get(Int32Ty, 2);
188 Type *V2Int64Ty = VectorType::get(Int64Ty, 2);
189 Type *V4Int16Ty = VectorType::get(Int16Ty, 4);
190
191 Type *Int32PtrTy = PointerType::get(Int32Ty, 0);
192 Type *Int64PtrTy = PointerType::get(Int64Ty, 0);
193
194 Type *Int32PtrAS1Ty = PointerType::get(Int32Ty, 1);
195 Type *Int64PtrAS1Ty = PointerType::get(Int64Ty, 1);
196
197 Type *V2Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 2);
198 Type *V2Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 2);
199 Type *V4Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 4);
200 Type *V4Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 4);
201
202 Type *V2Int64PtrTy = VectorType::get(Int64PtrTy, 2);
203 Type *V2Int32PtrTy = VectorType::get(Int32PtrTy, 2);
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +0000204 Type *V4Int32PtrTy = VectorType::get(Int32PtrTy, 4);
Duncan Sands2d3cdd62011-04-01 03:34:54 +0000205
Duncan Sandsa8514532011-05-18 07:13:41 +0000206 const Constant* c8 = Constant::getNullValue(V8x8Ty);
207 const Constant* c64 = Constant::getNullValue(V8x64Ty);
208
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +0000209 const Constant *v2ptr32 = Constant::getNullValue(V2Int32PtrTy);
210
Matt Arsenaultb4019ae2013-07-30 22:02:14 +0000211 EXPECT_TRUE(CastInst::isCastable(V8x8Ty, X86MMXTy));
212 EXPECT_TRUE(CastInst::isCastable(X86MMXTy, V8x8Ty));
213 EXPECT_FALSE(CastInst::isCastable(Int64Ty, X86MMXTy));
214 EXPECT_TRUE(CastInst::isCastable(V8x64Ty, V8x8Ty));
215 EXPECT_TRUE(CastInst::isCastable(V8x8Ty, V8x64Ty));
John McCalle83797c2011-08-27 19:23:22 +0000216 EXPECT_EQ(CastInst::Trunc, CastInst::getCastOpcode(c64, true, V8x8Ty, true));
217 EXPECT_EQ(CastInst::SExt, CastInst::getCastOpcode(c8, true, V8x64Ty, true));
Matt Arsenaultcacbb232013-07-30 20:45:05 +0000218
219 EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, X86MMXTy));
220 EXPECT_FALSE(CastInst::isBitCastable(X86MMXTy, V8x8Ty));
221 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, X86MMXTy));
222 EXPECT_FALSE(CastInst::isBitCastable(V8x64Ty, V8x8Ty));
223 EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, V8x64Ty));
224
225 // Check address space casts are rejected since we don't know the sizes here
226 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, Int32PtrAS1Ty));
227 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrAS1Ty, Int32PtrTy));
228 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, V2Int32PtrAS1Ty));
229 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int32PtrTy));
230 EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int64PtrAS1Ty));
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +0000231 EXPECT_TRUE(CastInst::isCastable(V2Int32PtrAS1Ty, V2Int32PtrTy));
232 EXPECT_EQ(CastInst::AddrSpaceCast, CastInst::getCastOpcode(v2ptr32, true,
233 V2Int32PtrAS1Ty,
234 true));
Matt Arsenaultcacbb232013-07-30 20:45:05 +0000235
236 // Test mismatched number of elements for pointers
237 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int64PtrAS1Ty));
238 EXPECT_FALSE(CastInst::isBitCastable(V4Int64PtrAS1Ty, V2Int32PtrAS1Ty));
239 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int32PtrAS1Ty));
240 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, V2Int32PtrTy));
241 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int32PtrTy));
242
243 EXPECT_TRUE(CastInst::isBitCastable(Int32PtrTy, Int64PtrTy));
244 EXPECT_FALSE(CastInst::isBitCastable(DoubleTy, FloatTy));
245 EXPECT_FALSE(CastInst::isBitCastable(FloatTy, DoubleTy));
246 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy));
247 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy));
248 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, Int32Ty));
249 EXPECT_TRUE(CastInst::isBitCastable(Int16Ty, HalfTy));
250 EXPECT_TRUE(CastInst::isBitCastable(Int32Ty, FloatTy));
251 EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, Int64Ty));
252
253 EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, V4Int16Ty));
254 EXPECT_FALSE(CastInst::isBitCastable(Int32Ty, Int64Ty));
255 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, Int32Ty));
256
257 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int64Ty));
258 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, V2Int32PtrTy));
259 EXPECT_TRUE(CastInst::isBitCastable(V2Int64PtrTy, V2Int32PtrTy));
260 EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrTy, V2Int64PtrTy));
261 EXPECT_FALSE(CastInst::isBitCastable(V2Int32Ty, V2Int64Ty));
262 EXPECT_FALSE(CastInst::isBitCastable(V2Int64Ty, V2Int32Ty));
Matt Arsenault065ced92013-07-31 00:17:33 +0000263
264
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +0000265 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
266 Constant::getNullValue(V4Int32PtrTy),
267 V2Int32PtrTy));
268 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
269 Constant::getNullValue(V2Int32PtrTy),
270 V4Int32PtrTy));
271
272 EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast,
273 Constant::getNullValue(V4Int32PtrAS1Ty),
274 V2Int32PtrTy));
275 EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast,
276 Constant::getNullValue(V2Int32PtrTy),
277 V4Int32PtrAS1Ty));
278
279
Matt Arsenault065ced92013-07-31 00:17:33 +0000280 // Check that assertion is not hit when creating a cast with a vector of
281 // pointers
282 // First form
283 BasicBlock *BB = BasicBlock::Create(C);
284 Constant *NullV2I32Ptr = Constant::getNullValue(V2Int32PtrTy);
Mehdi Amini03b42e42016-04-14 21:59:01 +0000285 auto Inst1 = CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty, "foo", BB);
Matt Arsenault065ced92013-07-31 00:17:33 +0000286
287 // Second form
Mehdi Amini03b42e42016-04-14 21:59:01 +0000288 auto Inst2 = CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty);
289
290 delete Inst2;
291 Inst1->eraseFromParent();
292 delete BB;
Duncan Sands2d3cdd62011-04-01 03:34:54 +0000293}
294
Nadav Rotem3924cb02011-12-05 06:29:09 +0000295TEST(InstructionsTest, VectorGep) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000296 LLVMContext C;
Nadav Rotem3924cb02011-12-05 06:29:09 +0000297
298 // Type Definitions
David Blaikieb3a39062015-03-14 21:40:10 +0000299 Type *I8Ty = IntegerType::get(C, 8);
300 Type *I32Ty = IntegerType::get(C, 32);
301 PointerType *Ptri8Ty = PointerType::get(I8Ty, 0);
302 PointerType *Ptri32Ty = PointerType::get(I32Ty, 0);
Nadav Rotem3924cb02011-12-05 06:29:09 +0000303
304 VectorType *V2xi8PTy = VectorType::get(Ptri8Ty, 2);
305 VectorType *V2xi32PTy = VectorType::get(Ptri32Ty, 2);
306
307 // Test different aspects of the vector-of-pointers type
308 // and GEPs which use this type.
309 ConstantInt *Ci32a = ConstantInt::get(C, APInt(32, 1492));
310 ConstantInt *Ci32b = ConstantInt::get(C, APInt(32, 1948));
311 std::vector<Constant*> ConstVa(2, Ci32a);
312 std::vector<Constant*> ConstVb(2, Ci32b);
313 Constant *C2xi32a = ConstantVector::get(ConstVa);
314 Constant *C2xi32b = ConstantVector::get(ConstVb);
315
316 CastInst *PtrVecA = new IntToPtrInst(C2xi32a, V2xi32PTy);
317 CastInst *PtrVecB = new IntToPtrInst(C2xi32b, V2xi32PTy);
318
319 ICmpInst *ICmp0 = new ICmpInst(ICmpInst::ICMP_SGT, PtrVecA, PtrVecB);
320 ICmpInst *ICmp1 = new ICmpInst(ICmpInst::ICMP_ULT, PtrVecA, PtrVecB);
321 EXPECT_NE(ICmp0, ICmp1); // suppress warning.
322
Evgeniy Stepanova259b262013-01-16 14:38:50 +0000323 BasicBlock* BB0 = BasicBlock::Create(C);
324 // Test InsertAtEnd ICmpInst constructor.
325 ICmpInst *ICmp2 = new ICmpInst(*BB0, ICmpInst::ICMP_SGE, PtrVecA, PtrVecB);
326 EXPECT_NE(ICmp0, ICmp2); // suppress warning.
327
David Blaikieb3a39062015-03-14 21:40:10 +0000328 GetElementPtrInst *Gep0 = GetElementPtrInst::Create(I32Ty, PtrVecA, C2xi32a);
329 GetElementPtrInst *Gep1 = GetElementPtrInst::Create(I32Ty, PtrVecA, C2xi32b);
330 GetElementPtrInst *Gep2 = GetElementPtrInst::Create(I32Ty, PtrVecB, C2xi32a);
331 GetElementPtrInst *Gep3 = GetElementPtrInst::Create(I32Ty, PtrVecB, C2xi32b);
Nadav Rotem3924cb02011-12-05 06:29:09 +0000332
333 CastInst *BTC0 = new BitCastInst(Gep0, V2xi8PTy);
334 CastInst *BTC1 = new BitCastInst(Gep1, V2xi8PTy);
335 CastInst *BTC2 = new BitCastInst(Gep2, V2xi8PTy);
336 CastInst *BTC3 = new BitCastInst(Gep3, V2xi8PTy);
337
338 Value *S0 = BTC0->stripPointerCasts();
339 Value *S1 = BTC1->stripPointerCasts();
340 Value *S2 = BTC2->stripPointerCasts();
341 Value *S3 = BTC3->stripPointerCasts();
342
343 EXPECT_NE(S0, Gep0);
344 EXPECT_NE(S1, Gep1);
345 EXPECT_NE(S2, Gep2);
346 EXPECT_NE(S3, Gep3);
347
348 int64_t Offset;
Micah Villmow9cfc13d2012-10-08 16:39:34 +0000349 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 +0000350 "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 +0000351 ":128:128-n8:16:32:64-S128");
352 // Make sure we don't crash
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000353 GetPointerBaseWithConstantOffset(Gep0, Offset, TD);
354 GetPointerBaseWithConstantOffset(Gep1, Offset, TD);
355 GetPointerBaseWithConstantOffset(Gep2, Offset, TD);
356 GetPointerBaseWithConstantOffset(Gep3, Offset, TD);
Nadav Rotem3924cb02011-12-05 06:29:09 +0000357
358 // Gep of Geps
David Blaikieb3a39062015-03-14 21:40:10 +0000359 GetElementPtrInst *GepII0 = GetElementPtrInst::Create(I32Ty, Gep0, C2xi32b);
360 GetElementPtrInst *GepII1 = GetElementPtrInst::Create(I32Ty, Gep1, C2xi32a);
361 GetElementPtrInst *GepII2 = GetElementPtrInst::Create(I32Ty, Gep2, C2xi32b);
362 GetElementPtrInst *GepII3 = GetElementPtrInst::Create(I32Ty, Gep3, C2xi32a);
Nadav Rotem3924cb02011-12-05 06:29:09 +0000363
364 EXPECT_EQ(GepII0->getNumIndices(), 1u);
365 EXPECT_EQ(GepII1->getNumIndices(), 1u);
366 EXPECT_EQ(GepII2->getNumIndices(), 1u);
367 EXPECT_EQ(GepII3->getNumIndices(), 1u);
368
369 EXPECT_FALSE(GepII0->hasAllZeroIndices());
370 EXPECT_FALSE(GepII1->hasAllZeroIndices());
371 EXPECT_FALSE(GepII2->hasAllZeroIndices());
372 EXPECT_FALSE(GepII3->hasAllZeroIndices());
373
374 delete GepII0;
375 delete GepII1;
376 delete GepII2;
377 delete GepII3;
378
379 delete BTC0;
380 delete BTC1;
381 delete BTC2;
382 delete BTC3;
383
384 delete Gep0;
385 delete Gep1;
386 delete Gep2;
387 delete Gep3;
388
Evgeniy Stepanova259b262013-01-16 14:38:50 +0000389 ICmp2->eraseFromParent();
390 delete BB0;
391
Nadav Rotem3924cb02011-12-05 06:29:09 +0000392 delete ICmp0;
393 delete ICmp1;
394 delete PtrVecA;
395 delete PtrVecB;
396}
397
Duncan Sands05f4df82012-04-16 16:28:59 +0000398TEST(InstructionsTest, FPMathOperator) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000399 LLVMContext Context;
Duncan Sands05f4df82012-04-16 16:28:59 +0000400 IRBuilder<> Builder(Context);
401 MDBuilder MDHelper(Context);
402 Instruction *I = Builder.CreatePHI(Builder.getDoubleTy(), 0);
403 MDNode *MD1 = MDHelper.createFPMath(1.0);
Duncan Sands05f4df82012-04-16 16:28:59 +0000404 Value *V1 = Builder.CreateFAdd(I, I, "", MD1);
Duncan Sands05f4df82012-04-16 16:28:59 +0000405 EXPECT_TRUE(isa<FPMathOperator>(V1));
Duncan Sands05f4df82012-04-16 16:28:59 +0000406 FPMathOperator *O1 = cast<FPMathOperator>(V1);
Duncan Sands05f4df82012-04-16 16:28:59 +0000407 EXPECT_EQ(O1->getFPAccuracy(), 1.0);
Duncan Sands05f4df82012-04-16 16:28:59 +0000408 delete V1;
Duncan Sands05f4df82012-04-16 16:28:59 +0000409 delete I;
410}
411
Duncan Sandse2395dc2012-10-30 16:03:32 +0000412
413TEST(InstructionsTest, isEliminableCastPair) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000414 LLVMContext C;
Duncan Sandse2395dc2012-10-30 16:03:32 +0000415
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000416 Type* Int16Ty = Type::getInt16Ty(C);
Duncan Sandse2395dc2012-10-30 16:03:32 +0000417 Type* Int32Ty = Type::getInt32Ty(C);
418 Type* Int64Ty = Type::getInt64Ty(C);
419 Type* Int64PtrTy = Type::getInt64PtrTy(C);
420
421 // Source and destination pointers have same size -> bitcast.
422 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
423 CastInst::IntToPtr,
424 Int64PtrTy, Int64Ty, Int64PtrTy,
Craig Topper66f09ad2014-06-08 22:29:17 +0000425 Int32Ty, nullptr, Int32Ty),
Duncan Sandse2395dc2012-10-30 16:03:32 +0000426 CastInst::BitCast);
427
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000428 // Source and destination have unknown sizes, but the same address space and
429 // the intermediate int is the maximum pointer size -> bitcast
Duncan Sandse2395dc2012-10-30 16:03:32 +0000430 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
431 CastInst::IntToPtr,
432 Int64PtrTy, Int64Ty, Int64PtrTy,
Craig Topper66f09ad2014-06-08 22:29:17 +0000433 nullptr, nullptr, nullptr),
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000434 CastInst::BitCast);
435
436 // Source and destination have unknown sizes, but the same address space and
437 // the intermediate int is not the maximum pointer size -> nothing
438 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
439 CastInst::IntToPtr,
440 Int64PtrTy, Int32Ty, Int64PtrTy,
Craig Topper66f09ad2014-06-08 22:29:17 +0000441 nullptr, nullptr, nullptr),
Duncan Sandse2395dc2012-10-30 16:03:32 +0000442 0U);
443
444 // Middle pointer big enough -> bitcast.
445 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
446 CastInst::PtrToInt,
447 Int64Ty, Int64PtrTy, Int64Ty,
Craig Topper66f09ad2014-06-08 22:29:17 +0000448 nullptr, Int64Ty, nullptr),
Duncan Sandse2395dc2012-10-30 16:03:32 +0000449 CastInst::BitCast);
450
451 // Middle pointer too small -> fail.
452 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
453 CastInst::PtrToInt,
454 Int64Ty, Int64PtrTy, Int64Ty,
Craig Topper66f09ad2014-06-08 22:29:17 +0000455 nullptr, Int32Ty, nullptr),
Duncan Sandse2395dc2012-10-30 16:03:32 +0000456 0U);
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000457
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000458 // Test that we don't eliminate bitcasts between different address spaces,
459 // or if we don't have available pointer size information.
460 DataLayout DL("e-p:32:32:32-p1:16:16:16-p2:64:64:64-i1:8:8-i8:8:8-i16:16:16"
461 "-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64"
Rafael Espindolac6751622013-12-13 18:56:34 +0000462 "-v128:128:128-a:0:64-s:64:64-f80:128:128-n8:16:32:64-S128");
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000463
464 Type* Int64PtrTyAS1 = Type::getInt64PtrTy(C, 1);
465 Type* Int64PtrTyAS2 = Type::getInt64PtrTy(C, 2);
466
467 IntegerType *Int16SizePtr = DL.getIntPtrType(C, 1);
468 IntegerType *Int64SizePtr = DL.getIntPtrType(C, 2);
469
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +0000470 // Cannot simplify inttoptr, addrspacecast
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000471 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +0000472 CastInst::AddrSpaceCast,
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000473 Int16Ty, Int64PtrTyAS1, Int64PtrTyAS2,
Craig Topper66f09ad2014-06-08 22:29:17 +0000474 nullptr, Int16SizePtr, Int64SizePtr),
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000475 0U);
476
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +0000477 // Cannot simplify addrspacecast, ptrtoint
478 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::AddrSpaceCast,
479 CastInst::PtrToInt,
480 Int64PtrTyAS1, Int64PtrTyAS2, Int16Ty,
Craig Topper66f09ad2014-06-08 22:29:17 +0000481 Int64SizePtr, Int16SizePtr, nullptr),
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +0000482 0U);
483
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000484 // Pass since the bitcast address spaces are the same
485 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
486 CastInst::BitCast,
487 Int16Ty, Int64PtrTyAS1, Int64PtrTyAS1,
Craig Topper66f09ad2014-06-08 22:29:17 +0000488 nullptr, nullptr, nullptr),
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000489 CastInst::IntToPtr);
490
Duncan Sandse2395dc2012-10-30 16:03:32 +0000491}
492
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000493TEST(InstructionsTest, CloneCall) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000494 LLVMContext C;
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000495 Type *Int32Ty = Type::getInt32Ty(C);
496 Type *ArgTys[] = {Int32Ty, Int32Ty, Int32Ty};
497 Type *FnTy = FunctionType::get(Int32Ty, ArgTys, /*isVarArg=*/false);
498 Value *Callee = Constant::getNullValue(FnTy->getPointerTo());
499 Value *Args[] = {
500 ConstantInt::get(Int32Ty, 1),
501 ConstantInt::get(Int32Ty, 2),
502 ConstantInt::get(Int32Ty, 3)
503 };
504 std::unique_ptr<CallInst> Call(CallInst::Create(Callee, Args, "result"));
505
506 // Test cloning the tail call kind.
507 CallInst::TailCallKind Kinds[] = {CallInst::TCK_None, CallInst::TCK_Tail,
508 CallInst::TCK_MustTail};
509 for (CallInst::TailCallKind TCK : Kinds) {
510 Call->setTailCallKind(TCK);
511 std::unique_ptr<CallInst> Clone(cast<CallInst>(Call->clone()));
512 EXPECT_EQ(Call->getTailCallKind(), Clone->getTailCallKind());
513 }
514 Call->setTailCallKind(CallInst::TCK_None);
515
516 // Test cloning an attribute.
517 {
518 AttrBuilder AB;
519 AB.addAttribute(Attribute::ReadOnly);
Reid Klecknerb5180542017-03-21 16:57:19 +0000520 Call->setAttributes(
521 AttributeList::get(C, AttributeList::FunctionIndex, AB));
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000522 std::unique_ptr<CallInst> Clone(cast<CallInst>(Call->clone()));
523 EXPECT_TRUE(Clone->onlyReadsMemory());
524 }
525}
526
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000527TEST(InstructionsTest, AlterCallBundles) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000528 LLVMContext C;
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000529 Type *Int32Ty = Type::getInt32Ty(C);
530 Type *FnTy = FunctionType::get(Int32Ty, Int32Ty, /*isVarArg=*/false);
531 Value *Callee = Constant::getNullValue(FnTy->getPointerTo());
532 Value *Args[] = {ConstantInt::get(Int32Ty, 42)};
533 OperandBundleDef OldBundle("before", UndefValue::get(Int32Ty));
534 std::unique_ptr<CallInst> Call(
535 CallInst::Create(Callee, Args, OldBundle, "result"));
536 Call->setTailCallKind(CallInst::TailCallKind::TCK_NoTail);
537 AttrBuilder AB;
538 AB.addAttribute(Attribute::Cold);
Reid Klecknerb5180542017-03-21 16:57:19 +0000539 Call->setAttributes(AttributeList::get(C, AttributeList::FunctionIndex, AB));
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000540 Call->setDebugLoc(DebugLoc(MDNode::get(C, None)));
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000541
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000542 OperandBundleDef NewBundle("after", ConstantInt::get(Int32Ty, 7));
543 std::unique_ptr<CallInst> Clone(CallInst::Create(Call.get(), NewBundle));
544 EXPECT_EQ(Call->getNumArgOperands(), Clone->getNumArgOperands());
545 EXPECT_EQ(Call->getArgOperand(0), Clone->getArgOperand(0));
546 EXPECT_EQ(Call->getCallingConv(), Clone->getCallingConv());
547 EXPECT_EQ(Call->getTailCallKind(), Clone->getTailCallKind());
548 EXPECT_TRUE(Clone->hasFnAttr(Attribute::AttrKind::Cold));
549 EXPECT_EQ(Call->getDebugLoc(), Clone->getDebugLoc());
Joseph Tremoulet56c99582016-01-14 06:30:19 +0000550 EXPECT_EQ(Clone->getNumOperandBundles(), 1U);
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000551 EXPECT_TRUE(Clone->getOperandBundle("after").hasValue());
552}
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000553
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000554TEST(InstructionsTest, AlterInvokeBundles) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000555 LLVMContext C;
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000556 Type *Int32Ty = Type::getInt32Ty(C);
557 Type *FnTy = FunctionType::get(Int32Ty, Int32Ty, /*isVarArg=*/false);
558 Value *Callee = Constant::getNullValue(FnTy->getPointerTo());
559 Value *Args[] = {ConstantInt::get(Int32Ty, 42)};
Joseph Tremouletf6cc7e62016-01-15 15:08:36 +0000560 std::unique_ptr<BasicBlock> NormalDest(BasicBlock::Create(C));
561 std::unique_ptr<BasicBlock> UnwindDest(BasicBlock::Create(C));
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000562 OperandBundleDef OldBundle("before", UndefValue::get(Int32Ty));
Joseph Tremouletf6cc7e62016-01-15 15:08:36 +0000563 std::unique_ptr<InvokeInst> Invoke(InvokeInst::Create(
564 Callee, NormalDest.get(), UnwindDest.get(), Args, OldBundle, "result"));
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000565 AttrBuilder AB;
566 AB.addAttribute(Attribute::Cold);
Reid Klecknerb5180542017-03-21 16:57:19 +0000567 Invoke->setAttributes(
568 AttributeList::get(C, AttributeList::FunctionIndex, AB));
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000569 Invoke->setDebugLoc(DebugLoc(MDNode::get(C, None)));
570
571 OperandBundleDef NewBundle("after", ConstantInt::get(Int32Ty, 7));
Joseph Tremouletf6cc7e62016-01-15 15:08:36 +0000572 std::unique_ptr<InvokeInst> Clone(
573 InvokeInst::Create(Invoke.get(), NewBundle));
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000574 EXPECT_EQ(Invoke->getNormalDest(), Clone->getNormalDest());
575 EXPECT_EQ(Invoke->getUnwindDest(), Clone->getUnwindDest());
576 EXPECT_EQ(Invoke->getNumArgOperands(), Clone->getNumArgOperands());
577 EXPECT_EQ(Invoke->getArgOperand(0), Clone->getArgOperand(0));
578 EXPECT_EQ(Invoke->getCallingConv(), Clone->getCallingConv());
579 EXPECT_TRUE(Clone->hasFnAttr(Attribute::AttrKind::Cold));
580 EXPECT_EQ(Invoke->getDebugLoc(), Clone->getDebugLoc());
NAKAMURA Takumi3557b882016-01-14 09:21:49 +0000581 EXPECT_EQ(Clone->getNumOperandBundles(), 1U);
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000582 EXPECT_TRUE(Clone->getOperandBundle("after").hasValue());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000583}
584
Sanjoy Dasaa722ae2017-02-23 22:50:52 +0000585TEST_F(ModuleWithFunctionTest, DropPoisonGeneratingFlags) {
586 auto *OnlyBB = BasicBlock::Create(Ctx, "bb", F);
587 auto *Arg0 = &*F->arg_begin();
588
589 IRBuilder<NoFolder> B(Ctx);
590 B.SetInsertPoint(OnlyBB);
591
592 {
593 auto *UI =
594 cast<Instruction>(B.CreateUDiv(Arg0, Arg0, "", /*isExact*/ true));
595 ASSERT_TRUE(UI->isExact());
596 UI->dropPoisonGeneratingFlags();
597 ASSERT_FALSE(UI->isExact());
598 }
599
600 {
601 auto *ShrI =
602 cast<Instruction>(B.CreateLShr(Arg0, Arg0, "", /*isExact*/ true));
603 ASSERT_TRUE(ShrI->isExact());
604 ShrI->dropPoisonGeneratingFlags();
605 ASSERT_FALSE(ShrI->isExact());
606 }
607
608 {
609 auto *AI = cast<Instruction>(
610 B.CreateAdd(Arg0, Arg0, "", /*HasNUW*/ true, /*HasNSW*/ false));
611 ASSERT_TRUE(AI->hasNoUnsignedWrap());
612 AI->dropPoisonGeneratingFlags();
613 ASSERT_FALSE(AI->hasNoUnsignedWrap());
614 ASSERT_FALSE(AI->hasNoSignedWrap());
615 }
616
617 {
618 auto *SI = cast<Instruction>(
619 B.CreateAdd(Arg0, Arg0, "", /*HasNUW*/ false, /*HasNSW*/ true));
620 ASSERT_TRUE(SI->hasNoSignedWrap());
621 SI->dropPoisonGeneratingFlags();
622 ASSERT_FALSE(SI->hasNoUnsignedWrap());
623 ASSERT_FALSE(SI->hasNoSignedWrap());
624 }
625
626 {
627 auto *ShlI = cast<Instruction>(
628 B.CreateShl(Arg0, Arg0, "", /*HasNUW*/ true, /*HasNSW*/ true));
629 ASSERT_TRUE(ShlI->hasNoSignedWrap());
630 ASSERT_TRUE(ShlI->hasNoUnsignedWrap());
631 ShlI->dropPoisonGeneratingFlags();
632 ASSERT_FALSE(ShlI->hasNoUnsignedWrap());
633 ASSERT_FALSE(ShlI->hasNoSignedWrap());
634 }
635
636 {
637 Value *GEPBase = Constant::getNullValue(B.getInt8PtrTy());
638 auto *GI = cast<GetElementPtrInst>(B.CreateInBoundsGEP(GEPBase, {Arg0}));
639 ASSERT_TRUE(GI->isInBounds());
640 GI->dropPoisonGeneratingFlags();
641 ASSERT_FALSE(GI->isInBounds());
642 }
643}
644
Chandler Carruthd1c95b62017-02-28 08:04:20 +0000645TEST(InstructionsTest, GEPIndices) {
646 LLVMContext Context;
647 IRBuilder<NoFolder> Builder(Context);
648 Type *ElementTy = Builder.getInt8Ty();
649 Type *ArrTy = ArrayType::get(ArrayType::get(ElementTy, 64), 64);
650 Value *Indices[] = {
651 Builder.getInt32(0),
652 Builder.getInt32(13),
653 Builder.getInt32(42) };
654
655 Value *V = Builder.CreateGEP(ArrTy, UndefValue::get(PointerType::getUnqual(ArrTy)),
656 Indices);
657 ASSERT_TRUE(isa<GetElementPtrInst>(V));
658
659 auto *GEPI = cast<GetElementPtrInst>(V);
660 ASSERT_NE(GEPI->idx_begin(), GEPI->idx_end());
661 ASSERT_EQ(GEPI->idx_end(), std::next(GEPI->idx_begin(), 3));
662 EXPECT_EQ(Indices[0], GEPI->idx_begin()[0]);
663 EXPECT_EQ(Indices[1], GEPI->idx_begin()[1]);
664 EXPECT_EQ(Indices[2], GEPI->idx_begin()[2]);
665 EXPECT_EQ(GEPI->idx_begin(), GEPI->indices().begin());
666 EXPECT_EQ(GEPI->idx_end(), GEPI->indices().end());
667
668 const auto *CGEPI = GEPI;
669 ASSERT_NE(CGEPI->idx_begin(), CGEPI->idx_end());
670 ASSERT_EQ(CGEPI->idx_end(), std::next(CGEPI->idx_begin(), 3));
671 EXPECT_EQ(Indices[0], CGEPI->idx_begin()[0]);
672 EXPECT_EQ(Indices[1], CGEPI->idx_begin()[1]);
673 EXPECT_EQ(Indices[2], CGEPI->idx_begin()[2]);
674 EXPECT_EQ(CGEPI->idx_begin(), CGEPI->indices().begin());
675 EXPECT_EQ(CGEPI->idx_end(), CGEPI->indices().end());
676
677 delete GEPI;
678}
679
Chandler Carruth927d8e62017-04-12 07:27:28 +0000680TEST(InstructionsTest, SwitchInst) {
681 LLVMContext C;
682
683 std::unique_ptr<BasicBlock> BB1, BB2, BB3;
684 BB1.reset(BasicBlock::Create(C));
685 BB2.reset(BasicBlock::Create(C));
686 BB3.reset(BasicBlock::Create(C));
687
688 // We create block 0 after the others so that it gets destroyed first and
689 // clears the uses of the other basic blocks.
690 std::unique_ptr<BasicBlock> BB0(BasicBlock::Create(C));
691
692 auto *Int32Ty = Type::getInt32Ty(C);
693
694 SwitchInst *SI =
695 SwitchInst::Create(UndefValue::get(Int32Ty), BB0.get(), 3, BB0.get());
696 SI->addCase(ConstantInt::get(Int32Ty, 1), BB1.get());
697 SI->addCase(ConstantInt::get(Int32Ty, 2), BB2.get());
698 SI->addCase(ConstantInt::get(Int32Ty, 3), BB3.get());
699
700 auto CI = SI->case_begin();
701 ASSERT_NE(CI, SI->case_end());
702 EXPECT_EQ(1, CI->getCaseValue()->getSExtValue());
703 EXPECT_EQ(BB1.get(), CI->getCaseSuccessor());
704 EXPECT_EQ(2, (CI + 1)->getCaseValue()->getSExtValue());
705 EXPECT_EQ(BB2.get(), (CI + 1)->getCaseSuccessor());
706 EXPECT_EQ(3, (CI + 2)->getCaseValue()->getSExtValue());
707 EXPECT_EQ(BB3.get(), (CI + 2)->getCaseSuccessor());
708 EXPECT_EQ(CI + 1, std::next(CI));
709 EXPECT_EQ(CI + 2, std::next(CI, 2));
710 EXPECT_EQ(CI + 3, std::next(CI, 3));
711 EXPECT_EQ(SI->case_end(), CI + 3);
712 EXPECT_EQ(0, CI - CI);
713 EXPECT_EQ(1, (CI + 1) - CI);
714 EXPECT_EQ(2, (CI + 2) - CI);
715 EXPECT_EQ(3, SI->case_end() - CI);
716 EXPECT_EQ(3, std::distance(CI, SI->case_end()));
717
718 auto CCI = const_cast<const SwitchInst *>(SI)->case_begin();
719 SwitchInst::ConstCaseIt CCE = SI->case_end();
720 ASSERT_NE(CCI, SI->case_end());
721 EXPECT_EQ(1, CCI->getCaseValue()->getSExtValue());
722 EXPECT_EQ(BB1.get(), CCI->getCaseSuccessor());
723 EXPECT_EQ(2, (CCI + 1)->getCaseValue()->getSExtValue());
724 EXPECT_EQ(BB2.get(), (CCI + 1)->getCaseSuccessor());
725 EXPECT_EQ(3, (CCI + 2)->getCaseValue()->getSExtValue());
726 EXPECT_EQ(BB3.get(), (CCI + 2)->getCaseSuccessor());
727 EXPECT_EQ(CCI + 1, std::next(CCI));
728 EXPECT_EQ(CCI + 2, std::next(CCI, 2));
729 EXPECT_EQ(CCI + 3, std::next(CCI, 3));
730 EXPECT_EQ(CCE, CCI + 3);
731 EXPECT_EQ(0, CCI - CCI);
732 EXPECT_EQ(1, (CCI + 1) - CCI);
733 EXPECT_EQ(2, (CCI + 2) - CCI);
734 EXPECT_EQ(3, CCE - CCI);
735 EXPECT_EQ(3, std::distance(CCI, CCE));
736
737 // Make sure that the const iterator is compatible with a const auto ref.
738 const auto &Handle = *CCI;
739 EXPECT_EQ(1, Handle.getCaseValue()->getSExtValue());
740 EXPECT_EQ(BB1.get(), Handle.getCaseSuccessor());
741}
742
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000743} // end anonymous namespace
744} // end namespace llvm