1 //===- llvm/unittest/IR/InstructionsTest.cpp - Instructions unit tests ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/AsmParser/Parser.h"
11 #include "llvm/IR/Instructions.h"
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/Analysis/ValueTracking.h"
14 #include "llvm/IR/BasicBlock.h"
15 #include "llvm/IR/Constants.h"
16 #include "llvm/IR/DataLayout.h"
17 #include "llvm/IR/DerivedTypes.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/IR/IRBuilder.h"
20 #include "llvm/IR/LLVMContext.h"
21 #include "llvm/IR/MDBuilder.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/IR/NoFolder.h"
24 #include "llvm/IR/Operator.h"
25 #include "llvm/Support/SourceMgr.h"
26 #include "gmock/gmock-matchers.h"
27 #include "gtest/gtest.h"
33 static std::unique_ptr
<Module
> parseIR(LLVMContext
&C
, const char *IR
) {
35 std::unique_ptr
<Module
> Mod
= parseAssemblyString(IR
, Err
, C
);
37 Err
.print("InstructionsTests", errs());
41 TEST(InstructionsTest
, ReturnInst
) {
45 const ReturnInst
* r0
= ReturnInst::Create(C
);
46 EXPECT_EQ(r0
->getNumOperands(), 0U);
47 EXPECT_EQ(r0
->op_begin(), r0
->op_end());
49 IntegerType
* Int1
= IntegerType::get(C
, 1);
50 Constant
* One
= ConstantInt::get(Int1
, 1, true);
51 const ReturnInst
* r1
= ReturnInst::Create(C
, One
);
52 EXPECT_EQ(1U, r1
->getNumOperands());
53 User::const_op_iterator
b(r1
->op_begin());
54 EXPECT_NE(r1
->op_end(), b
);
56 EXPECT_EQ(One
, r1
->getOperand(0));
58 EXPECT_EQ(r1
->op_end(), b
);
65 // Test fixture that provides a module and a single function within it. Useful
66 // for tests that need to refer to the function in some way.
67 class ModuleWithFunctionTest
: public testing::Test
{
69 ModuleWithFunctionTest() : M(new Module("MyModule", Ctx
)) {
70 FArgTypes
.push_back(Type::getInt8Ty(Ctx
));
71 FArgTypes
.push_back(Type::getInt32Ty(Ctx
));
72 FArgTypes
.push_back(Type::getInt64Ty(Ctx
));
74 FunctionType::get(Type::getVoidTy(Ctx
), FArgTypes
, false);
75 F
= Function::Create(FTy
, Function::ExternalLinkage
, "", M
.get());
79 std::unique_ptr
<Module
> M
;
80 SmallVector
<Type
*, 3> FArgTypes
;
84 TEST_F(ModuleWithFunctionTest
, CallInst
) {
85 Value
*Args
[] = {ConstantInt::get(Type::getInt8Ty(Ctx
), 20),
86 ConstantInt::get(Type::getInt32Ty(Ctx
), 9999),
87 ConstantInt::get(Type::getInt64Ty(Ctx
), 42)};
88 std::unique_ptr
<CallInst
> Call(CallInst::Create(F
, Args
));
90 // Make sure iteration over a call's arguments works as expected.
92 for (Value
*Arg
: Call
->arg_operands()) {
93 EXPECT_EQ(FArgTypes
[Idx
], Arg
->getType());
94 EXPECT_EQ(Call
->getArgOperand(Idx
)->getType(), Arg
->getType());
99 TEST_F(ModuleWithFunctionTest
, InvokeInst
) {
100 BasicBlock
*BB1
= BasicBlock::Create(Ctx
, "", F
);
101 BasicBlock
*BB2
= BasicBlock::Create(Ctx
, "", F
);
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
));
108 // Make sure iteration over invoke's arguments works as expected.
110 for (Value
*Arg
: Invoke
->arg_operands()) {
111 EXPECT_EQ(FArgTypes
[Idx
], Arg
->getType());
112 EXPECT_EQ(Invoke
->getArgOperand(Idx
)->getType(), Arg
->getType());
117 TEST(InstructionsTest
, BranchInst
) {
120 // Make a BasicBlocks
121 BasicBlock
* bb0
= BasicBlock::Create(C
);
122 BasicBlock
* bb1
= BasicBlock::Create(C
);
124 // Mandatory BranchInst
125 const BranchInst
* b0
= BranchInst::Create(bb0
);
127 EXPECT_TRUE(b0
->isUnconditional());
128 EXPECT_FALSE(b0
->isConditional());
129 EXPECT_EQ(1U, b0
->getNumSuccessors());
131 // check num operands
132 EXPECT_EQ(1U, b0
->getNumOperands());
134 EXPECT_NE(b0
->op_begin(), b0
->op_end());
135 EXPECT_EQ(b0
->op_end(), std::next(b0
->op_begin()));
137 EXPECT_EQ(b0
->op_end(), std::next(b0
->op_begin()));
139 IntegerType
* Int1
= IntegerType::get(C
, 1);
140 Constant
* One
= ConstantInt::get(Int1
, 1, true);
142 // Conditional BranchInst
143 BranchInst
* b1
= BranchInst::Create(bb0
, bb1
, One
);
145 EXPECT_FALSE(b1
->isUnconditional());
146 EXPECT_TRUE(b1
->isConditional());
147 EXPECT_EQ(2U, b1
->getNumSuccessors());
149 // check num operands
150 EXPECT_EQ(3U, b1
->getNumOperands());
152 User::const_op_iterator
b(b1
->op_begin());
155 EXPECT_NE(b
, b1
->op_end());
157 EXPECT_EQ(One
, b1
->getOperand(0));
158 EXPECT_EQ(One
, b1
->getCondition());
163 EXPECT_EQ(bb1
, b1
->getOperand(1));
164 EXPECT_EQ(bb1
, b1
->getSuccessor(1));
169 EXPECT_EQ(bb0
, b1
->getOperand(2));
170 EXPECT_EQ(bb0
, b1
->getSuccessor(0));
173 EXPECT_EQ(b1
->op_end(), b
);
183 TEST(InstructionsTest
, CastInst
) {
186 Type
*Int8Ty
= Type::getInt8Ty(C
);
187 Type
*Int16Ty
= Type::getInt16Ty(C
);
188 Type
*Int32Ty
= Type::getInt32Ty(C
);
189 Type
*Int64Ty
= Type::getInt64Ty(C
);
190 Type
*V8x8Ty
= VectorType::get(Int8Ty
, 8);
191 Type
*V8x64Ty
= VectorType::get(Int64Ty
, 8);
192 Type
*X86MMXTy
= Type::getX86_MMXTy(C
);
194 Type
*HalfTy
= Type::getHalfTy(C
);
195 Type
*FloatTy
= Type::getFloatTy(C
);
196 Type
*DoubleTy
= Type::getDoubleTy(C
);
198 Type
*V2Int32Ty
= VectorType::get(Int32Ty
, 2);
199 Type
*V2Int64Ty
= VectorType::get(Int64Ty
, 2);
200 Type
*V4Int16Ty
= VectorType::get(Int16Ty
, 4);
202 Type
*Int32PtrTy
= PointerType::get(Int32Ty
, 0);
203 Type
*Int64PtrTy
= PointerType::get(Int64Ty
, 0);
205 Type
*Int32PtrAS1Ty
= PointerType::get(Int32Ty
, 1);
206 Type
*Int64PtrAS1Ty
= PointerType::get(Int64Ty
, 1);
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);
213 Type
*V2Int64PtrTy
= VectorType::get(Int64PtrTy
, 2);
214 Type
*V2Int32PtrTy
= VectorType::get(Int32PtrTy
, 2);
215 Type
*V4Int32PtrTy
= VectorType::get(Int32PtrTy
, 4);
217 const Constant
* c8
= Constant::getNullValue(V8x8Ty
);
218 const Constant
* c64
= Constant::getNullValue(V8x64Ty
);
220 const Constant
*v2ptr32
= Constant::getNullValue(V2Int32PtrTy
);
222 EXPECT_TRUE(CastInst::isCastable(V8x8Ty
, X86MMXTy
));
223 EXPECT_TRUE(CastInst::isCastable(X86MMXTy
, V8x8Ty
));
224 EXPECT_FALSE(CastInst::isCastable(Int64Ty
, X86MMXTy
));
225 EXPECT_TRUE(CastInst::isCastable(V8x64Ty
, V8x8Ty
));
226 EXPECT_TRUE(CastInst::isCastable(V8x8Ty
, V8x64Ty
));
227 EXPECT_EQ(CastInst::Trunc
, CastInst::getCastOpcode(c64
, true, V8x8Ty
, true));
228 EXPECT_EQ(CastInst::SExt
, CastInst::getCastOpcode(c8
, true, V8x64Ty
, true));
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
));
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
));
242 EXPECT_TRUE(CastInst::isCastable(V2Int32PtrAS1Ty
, V2Int32PtrTy
));
243 EXPECT_EQ(CastInst::AddrSpaceCast
, CastInst::getCastOpcode(v2ptr32
, true,
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
));
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
));
264 EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty
, V4Int16Ty
));
265 EXPECT_FALSE(CastInst::isBitCastable(Int32Ty
, Int64Ty
));
266 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty
, Int32Ty
));
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
));
276 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast
,
277 Constant::getNullValue(V4Int32PtrTy
),
279 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast
,
280 Constant::getNullValue(V2Int32PtrTy
),
283 EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast
,
284 Constant::getNullValue(V4Int32PtrAS1Ty
),
286 EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast
,
287 Constant::getNullValue(V2Int32PtrTy
),
291 // Check that assertion is not hit when creating a cast with a vector of
294 BasicBlock
*BB
= BasicBlock::Create(C
);
295 Constant
*NullV2I32Ptr
= Constant::getNullValue(V2Int32PtrTy
);
296 auto Inst1
= CastInst::CreatePointerCast(NullV2I32Ptr
, V2Int32Ty
, "foo", BB
);
299 auto Inst2
= CastInst::CreatePointerCast(NullV2I32Ptr
, V2Int32Ty
);
302 Inst1
->eraseFromParent();
306 TEST(InstructionsTest
, VectorGep
) {
310 Type
*I8Ty
= IntegerType::get(C
, 8);
311 Type
*I32Ty
= IntegerType::get(C
, 32);
312 PointerType
*Ptri8Ty
= PointerType::get(I8Ty
, 0);
313 PointerType
*Ptri32Ty
= PointerType::get(I32Ty
, 0);
315 VectorType
*V2xi8PTy
= VectorType::get(Ptri8Ty
, 2);
316 VectorType
*V2xi32PTy
= VectorType::get(Ptri32Ty
, 2);
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
);
327 CastInst
*PtrVecA
= new IntToPtrInst(C2xi32a
, V2xi32PTy
);
328 CastInst
*PtrVecB
= new IntToPtrInst(C2xi32b
, V2xi32PTy
);
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.
334 BasicBlock
* BB0
= BasicBlock::Create(C
);
335 // Test InsertAtEnd ICmpInst constructor.
336 ICmpInst
*ICmp2
= new ICmpInst(*BB0
, ICmpInst::ICMP_SGE
, PtrVecA
, PtrVecB
);
337 EXPECT_NE(ICmp0
, ICmp2
); // suppress warning.
339 GetElementPtrInst
*Gep0
= GetElementPtrInst::Create(I32Ty
, PtrVecA
, C2xi32a
);
340 GetElementPtrInst
*Gep1
= GetElementPtrInst::Create(I32Ty
, PtrVecA
, C2xi32b
);
341 GetElementPtrInst
*Gep2
= GetElementPtrInst::Create(I32Ty
, PtrVecB
, C2xi32a
);
342 GetElementPtrInst
*Gep3
= GetElementPtrInst::Create(I32Ty
, PtrVecB
, C2xi32b
);
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
);
349 Value
*S0
= BTC0
->stripPointerCasts();
350 Value
*S1
= BTC1
->stripPointerCasts();
351 Value
*S2
= BTC2
->stripPointerCasts();
352 Value
*S3
= BTC3
->stripPointerCasts();
360 DataLayout
TD("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f3"
361 "2:32:32-f64:64:64-v64:64:64-v128:128:128-a:0:64-s:64:64-f80"
362 ":128:128-n8:16:32:64-S128");
363 // Make sure we don't crash
364 GetPointerBaseWithConstantOffset(Gep0
, Offset
, TD
);
365 GetPointerBaseWithConstantOffset(Gep1
, Offset
, TD
);
366 GetPointerBaseWithConstantOffset(Gep2
, Offset
, TD
);
367 GetPointerBaseWithConstantOffset(Gep3
, Offset
, TD
);
370 GetElementPtrInst
*GepII0
= GetElementPtrInst::Create(I32Ty
, Gep0
, C2xi32b
);
371 GetElementPtrInst
*GepII1
= GetElementPtrInst::Create(I32Ty
, Gep1
, C2xi32a
);
372 GetElementPtrInst
*GepII2
= GetElementPtrInst::Create(I32Ty
, Gep2
, C2xi32b
);
373 GetElementPtrInst
*GepII3
= GetElementPtrInst::Create(I32Ty
, Gep3
, C2xi32a
);
375 EXPECT_EQ(GepII0
->getNumIndices(), 1u);
376 EXPECT_EQ(GepII1
->getNumIndices(), 1u);
377 EXPECT_EQ(GepII2
->getNumIndices(), 1u);
378 EXPECT_EQ(GepII3
->getNumIndices(), 1u);
380 EXPECT_FALSE(GepII0
->hasAllZeroIndices());
381 EXPECT_FALSE(GepII1
->hasAllZeroIndices());
382 EXPECT_FALSE(GepII2
->hasAllZeroIndices());
383 EXPECT_FALSE(GepII3
->hasAllZeroIndices());
400 ICmp2
->eraseFromParent();
409 TEST(InstructionsTest
, FPMathOperator
) {
411 IRBuilder
<> Builder(Context
);
412 MDBuilder
MDHelper(Context
);
413 Instruction
*I
= Builder
.CreatePHI(Builder
.getDoubleTy(), 0);
414 MDNode
*MD1
= MDHelper
.createFPMath(1.0);
415 Value
*V1
= Builder
.CreateFAdd(I
, I
, "", MD1
);
416 EXPECT_TRUE(isa
<FPMathOperator
>(V1
));
417 FPMathOperator
*O1
= cast
<FPMathOperator
>(V1
);
418 EXPECT_EQ(O1
->getFPAccuracy(), 1.0);
424 TEST(InstructionsTest
, isEliminableCastPair
) {
427 Type
* Int16Ty
= Type::getInt16Ty(C
);
428 Type
* Int32Ty
= Type::getInt32Ty(C
);
429 Type
* Int64Ty
= Type::getInt64Ty(C
);
430 Type
* Int64PtrTy
= Type::getInt64PtrTy(C
);
432 // Source and destination pointers have same size -> bitcast.
433 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt
,
435 Int64PtrTy
, Int64Ty
, Int64PtrTy
,
436 Int32Ty
, nullptr, Int32Ty
),
439 // Source and destination have unknown sizes, but the same address space and
440 // the intermediate int is the maximum pointer size -> bitcast
441 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt
,
443 Int64PtrTy
, Int64Ty
, Int64PtrTy
,
444 nullptr, nullptr, nullptr),
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
,
451 Int64PtrTy
, Int32Ty
, Int64PtrTy
,
452 nullptr, nullptr, nullptr),
455 // Middle pointer big enough -> bitcast.
456 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr
,
458 Int64Ty
, Int64PtrTy
, Int64Ty
,
459 nullptr, Int64Ty
, nullptr),
462 // Middle pointer too small -> fail.
463 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr
,
465 Int64Ty
, Int64PtrTy
, Int64Ty
,
466 nullptr, Int32Ty
, nullptr),
469 // Test that we don't eliminate bitcasts between different address spaces,
470 // or if we don't have available pointer size information.
471 DataLayout
DL("e-p:32:32:32-p1:16:16:16-p2:64:64:64-i1:8:8-i8:8:8-i16:16:16"
472 "-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64"
473 "-v128:128:128-a:0:64-s:64:64-f80:128:128-n8:16:32:64-S128");
475 Type
* Int64PtrTyAS1
= Type::getInt64PtrTy(C
, 1);
476 Type
* Int64PtrTyAS2
= Type::getInt64PtrTy(C
, 2);
478 IntegerType
*Int16SizePtr
= DL
.getIntPtrType(C
, 1);
479 IntegerType
*Int64SizePtr
= DL
.getIntPtrType(C
, 2);
481 // Cannot simplify inttoptr, addrspacecast
482 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr
,
483 CastInst::AddrSpaceCast
,
484 Int16Ty
, Int64PtrTyAS1
, Int64PtrTyAS2
,
485 nullptr, Int16SizePtr
, Int64SizePtr
),
488 // Cannot simplify addrspacecast, ptrtoint
489 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::AddrSpaceCast
,
491 Int64PtrTyAS1
, Int64PtrTyAS2
, Int16Ty
,
492 Int64SizePtr
, Int16SizePtr
, nullptr),
495 // Pass since the bitcast address spaces are the same
496 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr
,
498 Int16Ty
, Int64PtrTyAS1
, Int64PtrTyAS1
,
499 nullptr, nullptr, nullptr),
504 TEST(InstructionsTest
, CloneCall
) {
506 Type
*Int32Ty
= Type::getInt32Ty(C
);
507 Type
*ArgTys
[] = {Int32Ty
, Int32Ty
, Int32Ty
};
508 Type
*FnTy
= FunctionType::get(Int32Ty
, ArgTys
, /*isVarArg=*/false);
509 Value
*Callee
= Constant::getNullValue(FnTy
->getPointerTo());
511 ConstantInt::get(Int32Ty
, 1),
512 ConstantInt::get(Int32Ty
, 2),
513 ConstantInt::get(Int32Ty
, 3)
515 std::unique_ptr
<CallInst
> Call(CallInst::Create(Callee
, Args
, "result"));
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());
525 Call
->setTailCallKind(CallInst::TCK_None
);
527 // Test cloning an attribute.
530 AB
.addAttribute(Attribute::ReadOnly
);
532 AttributeList::get(C
, AttributeList::FunctionIndex
, AB
));
533 std::unique_ptr
<CallInst
> Clone(cast
<CallInst
>(Call
->clone()));
534 EXPECT_TRUE(Clone
->onlyReadsMemory());
538 TEST(InstructionsTest
, AlterCallBundles
) {
540 Type
*Int32Ty
= Type::getInt32Ty(C
);
541 Type
*FnTy
= FunctionType::get(Int32Ty
, Int32Ty
, /*isVarArg=*/false);
542 Value
*Callee
= Constant::getNullValue(FnTy
->getPointerTo());
543 Value
*Args
[] = {ConstantInt::get(Int32Ty
, 42)};
544 OperandBundleDef
OldBundle("before", UndefValue::get(Int32Ty
));
545 std::unique_ptr
<CallInst
> Call(
546 CallInst::Create(Callee
, Args
, OldBundle
, "result"));
547 Call
->setTailCallKind(CallInst::TailCallKind::TCK_NoTail
);
549 AB
.addAttribute(Attribute::Cold
);
550 Call
->setAttributes(AttributeList::get(C
, AttributeList::FunctionIndex
, AB
));
551 Call
->setDebugLoc(DebugLoc(MDNode::get(C
, None
)));
553 OperandBundleDef
NewBundle("after", ConstantInt::get(Int32Ty
, 7));
554 std::unique_ptr
<CallInst
> Clone(CallInst::Create(Call
.get(), NewBundle
));
555 EXPECT_EQ(Call
->getNumArgOperands(), Clone
->getNumArgOperands());
556 EXPECT_EQ(Call
->getArgOperand(0), Clone
->getArgOperand(0));
557 EXPECT_EQ(Call
->getCallingConv(), Clone
->getCallingConv());
558 EXPECT_EQ(Call
->getTailCallKind(), Clone
->getTailCallKind());
559 EXPECT_TRUE(Clone
->hasFnAttr(Attribute::AttrKind::Cold
));
560 EXPECT_EQ(Call
->getDebugLoc(), Clone
->getDebugLoc());
561 EXPECT_EQ(Clone
->getNumOperandBundles(), 1U);
562 EXPECT_TRUE(Clone
->getOperandBundle("after").hasValue());
565 TEST(InstructionsTest
, AlterInvokeBundles
) {
567 Type
*Int32Ty
= Type::getInt32Ty(C
);
568 Type
*FnTy
= FunctionType::get(Int32Ty
, Int32Ty
, /*isVarArg=*/false);
569 Value
*Callee
= Constant::getNullValue(FnTy
->getPointerTo());
570 Value
*Args
[] = {ConstantInt::get(Int32Ty
, 42)};
571 std::unique_ptr
<BasicBlock
> NormalDest(BasicBlock::Create(C
));
572 std::unique_ptr
<BasicBlock
> UnwindDest(BasicBlock::Create(C
));
573 OperandBundleDef
OldBundle("before", UndefValue::get(Int32Ty
));
574 std::unique_ptr
<InvokeInst
> Invoke(InvokeInst::Create(
575 Callee
, NormalDest
.get(), UnwindDest
.get(), Args
, OldBundle
, "result"));
577 AB
.addAttribute(Attribute::Cold
);
578 Invoke
->setAttributes(
579 AttributeList::get(C
, AttributeList::FunctionIndex
, AB
));
580 Invoke
->setDebugLoc(DebugLoc(MDNode::get(C
, None
)));
582 OperandBundleDef
NewBundle("after", ConstantInt::get(Int32Ty
, 7));
583 std::unique_ptr
<InvokeInst
> Clone(
584 InvokeInst::Create(Invoke
.get(), NewBundle
));
585 EXPECT_EQ(Invoke
->getNormalDest(), Clone
->getNormalDest());
586 EXPECT_EQ(Invoke
->getUnwindDest(), Clone
->getUnwindDest());
587 EXPECT_EQ(Invoke
->getNumArgOperands(), Clone
->getNumArgOperands());
588 EXPECT_EQ(Invoke
->getArgOperand(0), Clone
->getArgOperand(0));
589 EXPECT_EQ(Invoke
->getCallingConv(), Clone
->getCallingConv());
590 EXPECT_TRUE(Clone
->hasFnAttr(Attribute::AttrKind::Cold
));
591 EXPECT_EQ(Invoke
->getDebugLoc(), Clone
->getDebugLoc());
592 EXPECT_EQ(Clone
->getNumOperandBundles(), 1U);
593 EXPECT_TRUE(Clone
->getOperandBundle("after").hasValue());
596 TEST_F(ModuleWithFunctionTest
, DropPoisonGeneratingFlags
) {
597 auto *OnlyBB
= BasicBlock::Create(Ctx
, "bb", F
);
598 auto *Arg0
= &*F
->arg_begin();
600 IRBuilder
<NoFolder
> B(Ctx
);
601 B
.SetInsertPoint(OnlyBB
);
605 cast
<Instruction
>(B
.CreateUDiv(Arg0
, Arg0
, "", /*isExact*/ true));
606 ASSERT_TRUE(UI
->isExact());
607 UI
->dropPoisonGeneratingFlags();
608 ASSERT_FALSE(UI
->isExact());
613 cast
<Instruction
>(B
.CreateLShr(Arg0
, Arg0
, "", /*isExact*/ true));
614 ASSERT_TRUE(ShrI
->isExact());
615 ShrI
->dropPoisonGeneratingFlags();
616 ASSERT_FALSE(ShrI
->isExact());
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());
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());
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());
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());
656 TEST(InstructionsTest
, GEPIndices
) {
658 IRBuilder
<NoFolder
> Builder(Context
);
659 Type
*ElementTy
= Builder
.getInt8Ty();
660 Type
*ArrTy
= ArrayType::get(ArrayType::get(ElementTy
, 64), 64);
663 Builder
.getInt32(13),
664 Builder
.getInt32(42) };
666 Value
*V
= Builder
.CreateGEP(ArrTy
, UndefValue::get(PointerType::getUnqual(ArrTy
)),
668 ASSERT_TRUE(isa
<GetElementPtrInst
>(V
));
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());
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());
691 TEST(InstructionsTest
, SwitchInst
) {
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
));
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
));
703 auto *Int32Ty
= Type::getInt32Ty(C
);
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());
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()));
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
));
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());
754 TEST(InstructionsTest
, CommuteShuffleMask
) {
755 SmallVector
<int, 16> Indices({-1, 0, 7});
756 ShuffleVectorInst::commuteShuffleMask(Indices
, 4);
757 EXPECT_THAT(Indices
, testing::ContainerEq(ArrayRef
<int>({-1, 4, 3})));
760 TEST(InstructionsTest
, ShuffleMaskQueries
) {
761 // Create the elements for various constant vectors.
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);
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
));
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
));
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
));
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
));
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
));
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
));
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
})));
826 EXPECT_TRUE(ShuffleVectorInst::isSelectMask(ConstantVector::get({C4
, C1
, C6
, CU
})));
827 EXPECT_TRUE(ShuffleVectorInst::isSelectMask(ConstantVector::get({CU
, C1
, C6
, C3
})));
829 EXPECT_TRUE(ShuffleVectorInst::isReverseMask(ConstantVector::get({C7
, C6
, CU
, C4
})));
830 EXPECT_TRUE(ShuffleVectorInst::isReverseMask(ConstantVector::get({C3
, CU
, C1
, CU
})));
832 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(ConstantVector::get({C7
, C5
, CU
, C7
})));
833 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(ConstantVector::get({C3
, C0
, CU
, C3
})));
835 EXPECT_TRUE(ShuffleVectorInst::isZeroEltSplatMask(ConstantVector::get({C4
, CU
, CU
, C4
})));
836 EXPECT_TRUE(ShuffleVectorInst::isZeroEltSplatMask(ConstantVector::get({CU
, C0
, CU
, C0
})));
838 EXPECT_TRUE(ShuffleVectorInst::isTransposeMask(ConstantVector::get({C1
, C5
, C3
, C7
})));
839 EXPECT_TRUE(ShuffleVectorInst::isTransposeMask(ConstantVector::get({C1
, C3
})));
841 // Nothing special about the values here - just re-using inputs to reduce code.
842 Constant
*V0
= ConstantVector::get({C0
, C1
, C2
, C3
});
843 Constant
*V1
= ConstantVector::get({C3
, C2
, C1
, C0
});
845 // Identity with undef elts.
846 ShuffleVectorInst
*Id1
= new ShuffleVectorInst(V0
, V1
,
847 ConstantVector::get({C0
, C1
, CU
, CU
}));
848 EXPECT_TRUE(Id1
->isIdentity());
849 EXPECT_FALSE(Id1
->isIdentityWithPadding());
850 EXPECT_FALSE(Id1
->isIdentityWithExtract());
851 EXPECT_FALSE(Id1
->isConcat());
854 // Result has less elements than operands.
855 ShuffleVectorInst
*Id2
= new ShuffleVectorInst(V0
, V1
,
856 ConstantVector::get({C0
, C1
, C2
}));
857 EXPECT_FALSE(Id2
->isIdentity());
858 EXPECT_FALSE(Id2
->isIdentityWithPadding());
859 EXPECT_TRUE(Id2
->isIdentityWithExtract());
860 EXPECT_FALSE(Id2
->isConcat());
863 // Result has less elements than operands; choose from Op1.
864 ShuffleVectorInst
*Id3
= new ShuffleVectorInst(V0
, V1
,
865 ConstantVector::get({C4
, CU
, C6
}));
866 EXPECT_FALSE(Id3
->isIdentity());
867 EXPECT_FALSE(Id3
->isIdentityWithPadding());
868 EXPECT_TRUE(Id3
->isIdentityWithExtract());
869 EXPECT_FALSE(Id3
->isConcat());
872 // Result has less elements than operands; choose from Op0 and Op1 is not identity.
873 ShuffleVectorInst
*Id4
= new ShuffleVectorInst(V0
, V1
,
874 ConstantVector::get({C4
, C1
, C6
}));
875 EXPECT_FALSE(Id4
->isIdentity());
876 EXPECT_FALSE(Id4
->isIdentityWithPadding());
877 EXPECT_FALSE(Id4
->isIdentityWithExtract());
878 EXPECT_FALSE(Id4
->isConcat());
881 // Result has more elements than operands, and extra elements are undef.
882 ShuffleVectorInst
*Id5
= new ShuffleVectorInst(V0
, V1
,
883 ConstantVector::get({CU
, C1
, C2
, C3
, CU
, CU
}));
884 EXPECT_FALSE(Id5
->isIdentity());
885 EXPECT_TRUE(Id5
->isIdentityWithPadding());
886 EXPECT_FALSE(Id5
->isIdentityWithExtract());
887 EXPECT_FALSE(Id5
->isConcat());
890 // Result has more elements than operands, and extra elements are undef; choose from Op1.
891 ShuffleVectorInst
*Id6
= new ShuffleVectorInst(V0
, V1
,
892 ConstantVector::get({C4
, C5
, C6
, CU
, CU
, CU
}));
893 EXPECT_FALSE(Id6
->isIdentity());
894 EXPECT_TRUE(Id6
->isIdentityWithPadding());
895 EXPECT_FALSE(Id6
->isIdentityWithExtract());
896 EXPECT_FALSE(Id6
->isConcat());
899 // Result has more elements than operands, but extra elements are not undef.
900 ShuffleVectorInst
*Id7
= new ShuffleVectorInst(V0
, V1
,
901 ConstantVector::get({C0
, C1
, C2
, C3
, CU
, C1
}));
902 EXPECT_FALSE(Id7
->isIdentity());
903 EXPECT_FALSE(Id7
->isIdentityWithPadding());
904 EXPECT_FALSE(Id7
->isIdentityWithExtract());
905 EXPECT_FALSE(Id7
->isConcat());
908 // Result has more elements than operands; choose from Op0 and Op1 is not identity.
909 ShuffleVectorInst
*Id8
= new ShuffleVectorInst(V0
, V1
,
910 ConstantVector::get({C4
, CU
, C2
, C3
, CU
, CU
}));
911 EXPECT_FALSE(Id8
->isIdentity());
912 EXPECT_FALSE(Id8
->isIdentityWithPadding());
913 EXPECT_FALSE(Id8
->isIdentityWithExtract());
914 EXPECT_FALSE(Id8
->isConcat());
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());
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());
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());
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());
955 TEST(InstructionsTest
, SkipDebug
) {
957 std::unique_ptr
<Module
> M
= parseIR(C
,
959 declare void @llvm.dbg.value(metadata, metadata, metadata)
963 call void @llvm.dbg.value(metadata i32 0, metadata !11, metadata !DIExpression()), !dbg !13
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
")
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)
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)
982 Function
*F
= cast
<Function
>(M
->getNamedValue("f"));
983 BasicBlock
&BB
= F
->front();
985 // The first non-debug instruction is the terminator.
986 auto *Term
= BB
.getTerminator();
987 EXPECT_EQ(Term
, BB
.begin()->getNextNonDebugInstruction());
988 EXPECT_EQ(Term
->getIterator(), skipDebugIntrinsics(BB
.begin()));
990 // After the terminator, there are no non-debug instructions.
991 EXPECT_EQ(nullptr, Term
->getNextNonDebugInstruction());
994 } // end anonymous namespace
995 } // end namespace llvm