Revert r354244 "[DAGCombiner] Eliminate dead stores to stack."
[llvm-complete.git] / unittests / IR / InstructionsTest.cpp
blob94f37251e60bcfc6cae3c087298efe816e66f3ec
1 //===- llvm/unittest/IR/InstructionsTest.cpp - Instructions unit tests ----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/AsmParser/Parser.h"
10 #include "llvm/IR/Instructions.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/Analysis/ValueTracking.h"
13 #include "llvm/IR/BasicBlock.h"
14 #include "llvm/IR/Constants.h"
15 #include "llvm/IR/DataLayout.h"
16 #include "llvm/IR/DerivedTypes.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/IRBuilder.h"
19 #include "llvm/IR/LLVMContext.h"
20 #include "llvm/IR/MDBuilder.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/IR/NoFolder.h"
23 #include "llvm/IR/Operator.h"
24 #include "llvm/Support/SourceMgr.h"
25 #include "gmock/gmock-matchers.h"
26 #include "gtest/gtest.h"
27 #include <memory>
29 namespace llvm {
30 namespace {
32 static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) {
33 SMDiagnostic Err;
34 std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C);
35 if (!Mod)
36 Err.print("InstructionsTests", errs());
37 return Mod;
40 TEST(InstructionsTest, ReturnInst) {
41 LLVMContext C;
43 // test for PR6589
44 const ReturnInst* r0 = ReturnInst::Create(C);
45 EXPECT_EQ(r0->getNumOperands(), 0U);
46 EXPECT_EQ(r0->op_begin(), r0->op_end());
48 IntegerType* Int1 = IntegerType::get(C, 1);
49 Constant* One = ConstantInt::get(Int1, 1, true);
50 const ReturnInst* r1 = ReturnInst::Create(C, One);
51 EXPECT_EQ(1U, r1->getNumOperands());
52 User::const_op_iterator b(r1->op_begin());
53 EXPECT_NE(r1->op_end(), b);
54 EXPECT_EQ(One, *b);
55 EXPECT_EQ(One, r1->getOperand(0));
56 ++b;
57 EXPECT_EQ(r1->op_end(), b);
59 // clean up
60 delete r0;
61 delete r1;
64 // Test fixture that provides a module and a single function within it. Useful
65 // for tests that need to refer to the function in some way.
66 class ModuleWithFunctionTest : public testing::Test {
67 protected:
68 ModuleWithFunctionTest() : M(new Module("MyModule", Ctx)) {
69 FArgTypes.push_back(Type::getInt8Ty(Ctx));
70 FArgTypes.push_back(Type::getInt32Ty(Ctx));
71 FArgTypes.push_back(Type::getInt64Ty(Ctx));
72 FunctionType *FTy =
73 FunctionType::get(Type::getVoidTy(Ctx), FArgTypes, false);
74 F = Function::Create(FTy, Function::ExternalLinkage, "", M.get());
77 LLVMContext Ctx;
78 std::unique_ptr<Module> M;
79 SmallVector<Type *, 3> FArgTypes;
80 Function *F;
83 TEST_F(ModuleWithFunctionTest, CallInst) {
84 Value *Args[] = {ConstantInt::get(Type::getInt8Ty(Ctx), 20),
85 ConstantInt::get(Type::getInt32Ty(Ctx), 9999),
86 ConstantInt::get(Type::getInt64Ty(Ctx), 42)};
87 std::unique_ptr<CallInst> Call(CallInst::Create(F, Args));
89 // Make sure iteration over a call's arguments works as expected.
90 unsigned Idx = 0;
91 for (Value *Arg : Call->arg_operands()) {
92 EXPECT_EQ(FArgTypes[Idx], Arg->getType());
93 EXPECT_EQ(Call->getArgOperand(Idx)->getType(), Arg->getType());
94 Idx++;
98 TEST_F(ModuleWithFunctionTest, InvokeInst) {
99 BasicBlock *BB1 = BasicBlock::Create(Ctx, "", F);
100 BasicBlock *BB2 = BasicBlock::Create(Ctx, "", F);
102 Value *Args[] = {ConstantInt::get(Type::getInt8Ty(Ctx), 20),
103 ConstantInt::get(Type::getInt32Ty(Ctx), 9999),
104 ConstantInt::get(Type::getInt64Ty(Ctx), 42)};
105 std::unique_ptr<InvokeInst> Invoke(InvokeInst::Create(F, BB1, BB2, Args));
107 // Make sure iteration over invoke's arguments works as expected.
108 unsigned Idx = 0;
109 for (Value *Arg : Invoke->arg_operands()) {
110 EXPECT_EQ(FArgTypes[Idx], Arg->getType());
111 EXPECT_EQ(Invoke->getArgOperand(Idx)->getType(), Arg->getType());
112 Idx++;
116 TEST(InstructionsTest, BranchInst) {
117 LLVMContext C;
119 // Make a BasicBlocks
120 BasicBlock* bb0 = BasicBlock::Create(C);
121 BasicBlock* bb1 = BasicBlock::Create(C);
123 // Mandatory BranchInst
124 const BranchInst* b0 = BranchInst::Create(bb0);
126 EXPECT_TRUE(b0->isUnconditional());
127 EXPECT_FALSE(b0->isConditional());
128 EXPECT_EQ(1U, b0->getNumSuccessors());
130 // check num operands
131 EXPECT_EQ(1U, b0->getNumOperands());
133 EXPECT_NE(b0->op_begin(), b0->op_end());
134 EXPECT_EQ(b0->op_end(), std::next(b0->op_begin()));
136 EXPECT_EQ(b0->op_end(), std::next(b0->op_begin()));
138 IntegerType* Int1 = IntegerType::get(C, 1);
139 Constant* One = ConstantInt::get(Int1, 1, true);
141 // Conditional BranchInst
142 BranchInst* b1 = BranchInst::Create(bb0, bb1, One);
144 EXPECT_FALSE(b1->isUnconditional());
145 EXPECT_TRUE(b1->isConditional());
146 EXPECT_EQ(2U, b1->getNumSuccessors());
148 // check num operands
149 EXPECT_EQ(3U, b1->getNumOperands());
151 User::const_op_iterator b(b1->op_begin());
153 // check COND
154 EXPECT_NE(b, b1->op_end());
155 EXPECT_EQ(One, *b);
156 EXPECT_EQ(One, b1->getOperand(0));
157 EXPECT_EQ(One, b1->getCondition());
158 ++b;
160 // check ELSE
161 EXPECT_EQ(bb1, *b);
162 EXPECT_EQ(bb1, b1->getOperand(1));
163 EXPECT_EQ(bb1, b1->getSuccessor(1));
164 ++b;
166 // check THEN
167 EXPECT_EQ(bb0, *b);
168 EXPECT_EQ(bb0, b1->getOperand(2));
169 EXPECT_EQ(bb0, b1->getSuccessor(0));
170 ++b;
172 EXPECT_EQ(b1->op_end(), b);
174 // clean up
175 delete b0;
176 delete b1;
178 delete bb0;
179 delete bb1;
182 TEST(InstructionsTest, CastInst) {
183 LLVMContext C;
185 Type *Int8Ty = Type::getInt8Ty(C);
186 Type *Int16Ty = Type::getInt16Ty(C);
187 Type *Int32Ty = Type::getInt32Ty(C);
188 Type *Int64Ty = Type::getInt64Ty(C);
189 Type *V8x8Ty = VectorType::get(Int8Ty, 8);
190 Type *V8x64Ty = VectorType::get(Int64Ty, 8);
191 Type *X86MMXTy = Type::getX86_MMXTy(C);
193 Type *HalfTy = Type::getHalfTy(C);
194 Type *FloatTy = Type::getFloatTy(C);
195 Type *DoubleTy = Type::getDoubleTy(C);
197 Type *V2Int32Ty = VectorType::get(Int32Ty, 2);
198 Type *V2Int64Ty = VectorType::get(Int64Ty, 2);
199 Type *V4Int16Ty = VectorType::get(Int16Ty, 4);
201 Type *Int32PtrTy = PointerType::get(Int32Ty, 0);
202 Type *Int64PtrTy = PointerType::get(Int64Ty, 0);
204 Type *Int32PtrAS1Ty = PointerType::get(Int32Ty, 1);
205 Type *Int64PtrAS1Ty = PointerType::get(Int64Ty, 1);
207 Type *V2Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 2);
208 Type *V2Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 2);
209 Type *V4Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 4);
210 Type *V4Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 4);
212 Type *V2Int64PtrTy = VectorType::get(Int64PtrTy, 2);
213 Type *V2Int32PtrTy = VectorType::get(Int32PtrTy, 2);
214 Type *V4Int32PtrTy = VectorType::get(Int32PtrTy, 4);
216 const Constant* c8 = Constant::getNullValue(V8x8Ty);
217 const Constant* c64 = Constant::getNullValue(V8x64Ty);
219 const Constant *v2ptr32 = Constant::getNullValue(V2Int32PtrTy);
221 EXPECT_TRUE(CastInst::isCastable(V8x8Ty, X86MMXTy));
222 EXPECT_TRUE(CastInst::isCastable(X86MMXTy, V8x8Ty));
223 EXPECT_FALSE(CastInst::isCastable(Int64Ty, X86MMXTy));
224 EXPECT_TRUE(CastInst::isCastable(V8x64Ty, V8x8Ty));
225 EXPECT_TRUE(CastInst::isCastable(V8x8Ty, V8x64Ty));
226 EXPECT_EQ(CastInst::Trunc, CastInst::getCastOpcode(c64, true, V8x8Ty, true));
227 EXPECT_EQ(CastInst::SExt, CastInst::getCastOpcode(c8, true, V8x64Ty, true));
229 EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, X86MMXTy));
230 EXPECT_FALSE(CastInst::isBitCastable(X86MMXTy, V8x8Ty));
231 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, X86MMXTy));
232 EXPECT_FALSE(CastInst::isBitCastable(V8x64Ty, V8x8Ty));
233 EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, V8x64Ty));
235 // Check address space casts are rejected since we don't know the sizes here
236 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, Int32PtrAS1Ty));
237 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrAS1Ty, Int32PtrTy));
238 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, V2Int32PtrAS1Ty));
239 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int32PtrTy));
240 EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int64PtrAS1Ty));
241 EXPECT_TRUE(CastInst::isCastable(V2Int32PtrAS1Ty, V2Int32PtrTy));
242 EXPECT_EQ(CastInst::AddrSpaceCast, CastInst::getCastOpcode(v2ptr32, true,
243 V2Int32PtrAS1Ty,
244 true));
246 // Test mismatched number of elements for pointers
247 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int64PtrAS1Ty));
248 EXPECT_FALSE(CastInst::isBitCastable(V4Int64PtrAS1Ty, V2Int32PtrAS1Ty));
249 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int32PtrAS1Ty));
250 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, V2Int32PtrTy));
251 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int32PtrTy));
253 EXPECT_TRUE(CastInst::isBitCastable(Int32PtrTy, Int64PtrTy));
254 EXPECT_FALSE(CastInst::isBitCastable(DoubleTy, FloatTy));
255 EXPECT_FALSE(CastInst::isBitCastable(FloatTy, DoubleTy));
256 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy));
257 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy));
258 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, Int32Ty));
259 EXPECT_TRUE(CastInst::isBitCastable(Int16Ty, HalfTy));
260 EXPECT_TRUE(CastInst::isBitCastable(Int32Ty, FloatTy));
261 EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, Int64Ty));
263 EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, V4Int16Ty));
264 EXPECT_FALSE(CastInst::isBitCastable(Int32Ty, Int64Ty));
265 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, Int32Ty));
267 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int64Ty));
268 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, V2Int32PtrTy));
269 EXPECT_TRUE(CastInst::isBitCastable(V2Int64PtrTy, V2Int32PtrTy));
270 EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrTy, V2Int64PtrTy));
271 EXPECT_FALSE(CastInst::isBitCastable(V2Int32Ty, V2Int64Ty));
272 EXPECT_FALSE(CastInst::isBitCastable(V2Int64Ty, V2Int32Ty));
275 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
276 Constant::getNullValue(V4Int32PtrTy),
277 V2Int32PtrTy));
278 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
279 Constant::getNullValue(V2Int32PtrTy),
280 V4Int32PtrTy));
282 EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast,
283 Constant::getNullValue(V4Int32PtrAS1Ty),
284 V2Int32PtrTy));
285 EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast,
286 Constant::getNullValue(V2Int32PtrTy),
287 V4Int32PtrAS1Ty));
290 // Check that assertion is not hit when creating a cast with a vector of
291 // pointers
292 // First form
293 BasicBlock *BB = BasicBlock::Create(C);
294 Constant *NullV2I32Ptr = Constant::getNullValue(V2Int32PtrTy);
295 auto Inst1 = CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty, "foo", BB);
297 // Second form
298 auto Inst2 = CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty);
300 delete Inst2;
301 Inst1->eraseFromParent();
302 delete BB;
305 TEST(InstructionsTest, VectorGep) {
306 LLVMContext C;
308 // Type Definitions
309 Type *I8Ty = IntegerType::get(C, 8);
310 Type *I32Ty = IntegerType::get(C, 32);
311 PointerType *Ptri8Ty = PointerType::get(I8Ty, 0);
312 PointerType *Ptri32Ty = PointerType::get(I32Ty, 0);
314 VectorType *V2xi8PTy = VectorType::get(Ptri8Ty, 2);
315 VectorType *V2xi32PTy = VectorType::get(Ptri32Ty, 2);
317 // Test different aspects of the vector-of-pointers type
318 // and GEPs which use this type.
319 ConstantInt *Ci32a = ConstantInt::get(C, APInt(32, 1492));
320 ConstantInt *Ci32b = ConstantInt::get(C, APInt(32, 1948));
321 std::vector<Constant*> ConstVa(2, Ci32a);
322 std::vector<Constant*> ConstVb(2, Ci32b);
323 Constant *C2xi32a = ConstantVector::get(ConstVa);
324 Constant *C2xi32b = ConstantVector::get(ConstVb);
326 CastInst *PtrVecA = new IntToPtrInst(C2xi32a, V2xi32PTy);
327 CastInst *PtrVecB = new IntToPtrInst(C2xi32b, V2xi32PTy);
329 ICmpInst *ICmp0 = new ICmpInst(ICmpInst::ICMP_SGT, PtrVecA, PtrVecB);
330 ICmpInst *ICmp1 = new ICmpInst(ICmpInst::ICMP_ULT, PtrVecA, PtrVecB);
331 EXPECT_NE(ICmp0, ICmp1); // suppress warning.
333 BasicBlock* BB0 = BasicBlock::Create(C);
334 // Test InsertAtEnd ICmpInst constructor.
335 ICmpInst *ICmp2 = new ICmpInst(*BB0, ICmpInst::ICMP_SGE, PtrVecA, PtrVecB);
336 EXPECT_NE(ICmp0, ICmp2); // suppress warning.
338 GetElementPtrInst *Gep0 = GetElementPtrInst::Create(I32Ty, PtrVecA, C2xi32a);
339 GetElementPtrInst *Gep1 = GetElementPtrInst::Create(I32Ty, PtrVecA, C2xi32b);
340 GetElementPtrInst *Gep2 = GetElementPtrInst::Create(I32Ty, PtrVecB, C2xi32a);
341 GetElementPtrInst *Gep3 = GetElementPtrInst::Create(I32Ty, PtrVecB, C2xi32b);
343 CastInst *BTC0 = new BitCastInst(Gep0, V2xi8PTy);
344 CastInst *BTC1 = new BitCastInst(Gep1, V2xi8PTy);
345 CastInst *BTC2 = new BitCastInst(Gep2, V2xi8PTy);
346 CastInst *BTC3 = new BitCastInst(Gep3, V2xi8PTy);
348 Value *S0 = BTC0->stripPointerCasts();
349 Value *S1 = BTC1->stripPointerCasts();
350 Value *S2 = BTC2->stripPointerCasts();
351 Value *S3 = BTC3->stripPointerCasts();
353 EXPECT_NE(S0, Gep0);
354 EXPECT_NE(S1, Gep1);
355 EXPECT_NE(S2, Gep2);
356 EXPECT_NE(S3, Gep3);
358 int64_t Offset;
359 DataLayout TD("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f3"
360 "2:32:32-f64:64:64-v64:64:64-v128:128:128-a:0:64-s:64:64-f80"
361 ":128:128-n8:16:32:64-S128");
362 // Make sure we don't crash
363 GetPointerBaseWithConstantOffset(Gep0, Offset, TD);
364 GetPointerBaseWithConstantOffset(Gep1, Offset, TD);
365 GetPointerBaseWithConstantOffset(Gep2, Offset, TD);
366 GetPointerBaseWithConstantOffset(Gep3, Offset, TD);
368 // Gep of Geps
369 GetElementPtrInst *GepII0 = GetElementPtrInst::Create(I32Ty, Gep0, C2xi32b);
370 GetElementPtrInst *GepII1 = GetElementPtrInst::Create(I32Ty, Gep1, C2xi32a);
371 GetElementPtrInst *GepII2 = GetElementPtrInst::Create(I32Ty, Gep2, C2xi32b);
372 GetElementPtrInst *GepII3 = GetElementPtrInst::Create(I32Ty, Gep3, C2xi32a);
374 EXPECT_EQ(GepII0->getNumIndices(), 1u);
375 EXPECT_EQ(GepII1->getNumIndices(), 1u);
376 EXPECT_EQ(GepII2->getNumIndices(), 1u);
377 EXPECT_EQ(GepII3->getNumIndices(), 1u);
379 EXPECT_FALSE(GepII0->hasAllZeroIndices());
380 EXPECT_FALSE(GepII1->hasAllZeroIndices());
381 EXPECT_FALSE(GepII2->hasAllZeroIndices());
382 EXPECT_FALSE(GepII3->hasAllZeroIndices());
384 delete GepII0;
385 delete GepII1;
386 delete GepII2;
387 delete GepII3;
389 delete BTC0;
390 delete BTC1;
391 delete BTC2;
392 delete BTC3;
394 delete Gep0;
395 delete Gep1;
396 delete Gep2;
397 delete Gep3;
399 ICmp2->eraseFromParent();
400 delete BB0;
402 delete ICmp0;
403 delete ICmp1;
404 delete PtrVecA;
405 delete PtrVecB;
408 TEST(InstructionsTest, FPMathOperator) {
409 LLVMContext Context;
410 IRBuilder<> Builder(Context);
411 MDBuilder MDHelper(Context);
412 Instruction *I = Builder.CreatePHI(Builder.getDoubleTy(), 0);
413 MDNode *MD1 = MDHelper.createFPMath(1.0);
414 Value *V1 = Builder.CreateFAdd(I, I, "", MD1);
415 EXPECT_TRUE(isa<FPMathOperator>(V1));
416 FPMathOperator *O1 = cast<FPMathOperator>(V1);
417 EXPECT_EQ(O1->getFPAccuracy(), 1.0);
418 V1->deleteValue();
419 I->deleteValue();
423 TEST(InstructionsTest, isEliminableCastPair) {
424 LLVMContext C;
426 Type* Int16Ty = Type::getInt16Ty(C);
427 Type* Int32Ty = Type::getInt32Ty(C);
428 Type* Int64Ty = Type::getInt64Ty(C);
429 Type* Int64PtrTy = Type::getInt64PtrTy(C);
431 // Source and destination pointers have same size -> bitcast.
432 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
433 CastInst::IntToPtr,
434 Int64PtrTy, Int64Ty, Int64PtrTy,
435 Int32Ty, nullptr, Int32Ty),
436 CastInst::BitCast);
438 // Source and destination have unknown sizes, but the same address space and
439 // the intermediate int is the maximum pointer size -> bitcast
440 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
441 CastInst::IntToPtr,
442 Int64PtrTy, Int64Ty, Int64PtrTy,
443 nullptr, nullptr, nullptr),
444 CastInst::BitCast);
446 // Source and destination have unknown sizes, but the same address space and
447 // the intermediate int is not the maximum pointer size -> nothing
448 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
449 CastInst::IntToPtr,
450 Int64PtrTy, Int32Ty, Int64PtrTy,
451 nullptr, nullptr, nullptr),
452 0U);
454 // Middle pointer big enough -> bitcast.
455 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
456 CastInst::PtrToInt,
457 Int64Ty, Int64PtrTy, Int64Ty,
458 nullptr, Int64Ty, nullptr),
459 CastInst::BitCast);
461 // Middle pointer too small -> fail.
462 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
463 CastInst::PtrToInt,
464 Int64Ty, Int64PtrTy, Int64Ty,
465 nullptr, Int32Ty, nullptr),
466 0U);
468 // Test that we don't eliminate bitcasts between different address spaces,
469 // or if we don't have available pointer size information.
470 DataLayout DL("e-p:32:32:32-p1:16:16:16-p2:64:64:64-i1:8:8-i8:8:8-i16:16:16"
471 "-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64"
472 "-v128:128:128-a:0:64-s:64:64-f80:128:128-n8:16:32:64-S128");
474 Type* Int64PtrTyAS1 = Type::getInt64PtrTy(C, 1);
475 Type* Int64PtrTyAS2 = Type::getInt64PtrTy(C, 2);
477 IntegerType *Int16SizePtr = DL.getIntPtrType(C, 1);
478 IntegerType *Int64SizePtr = DL.getIntPtrType(C, 2);
480 // Cannot simplify inttoptr, addrspacecast
481 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
482 CastInst::AddrSpaceCast,
483 Int16Ty, Int64PtrTyAS1, Int64PtrTyAS2,
484 nullptr, Int16SizePtr, Int64SizePtr),
485 0U);
487 // Cannot simplify addrspacecast, ptrtoint
488 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::AddrSpaceCast,
489 CastInst::PtrToInt,
490 Int64PtrTyAS1, Int64PtrTyAS2, Int16Ty,
491 Int64SizePtr, Int16SizePtr, nullptr),
492 0U);
494 // Pass since the bitcast address spaces are the same
495 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
496 CastInst::BitCast,
497 Int16Ty, Int64PtrTyAS1, Int64PtrTyAS1,
498 nullptr, nullptr, nullptr),
499 CastInst::IntToPtr);
503 TEST(InstructionsTest, CloneCall) {
504 LLVMContext C;
505 Type *Int32Ty = Type::getInt32Ty(C);
506 Type *ArgTys[] = {Int32Ty, Int32Ty, Int32Ty};
507 FunctionType *FnTy = FunctionType::get(Int32Ty, ArgTys, /*isVarArg=*/false);
508 Value *Callee = Constant::getNullValue(FnTy->getPointerTo());
509 Value *Args[] = {
510 ConstantInt::get(Int32Ty, 1),
511 ConstantInt::get(Int32Ty, 2),
512 ConstantInt::get(Int32Ty, 3)
514 std::unique_ptr<CallInst> Call(
515 CallInst::Create(FnTy, 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.
529 AttrBuilder AB;
530 AB.addAttribute(Attribute::ReadOnly);
531 Call->setAttributes(
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) {
539 LLVMContext C;
540 Type *Int32Ty = Type::getInt32Ty(C);
541 FunctionType *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(FnTy, Callee, Args, OldBundle, "result"));
547 Call->setTailCallKind(CallInst::TailCallKind::TCK_NoTail);
548 AttrBuilder AB;
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) {
566 LLVMContext C;
567 Type *Int32Ty = Type::getInt32Ty(C);
568 FunctionType *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(
575 InvokeInst::Create(FnTy, Callee, NormalDest.get(), UnwindDest.get(), Args,
576 OldBundle, "result"));
577 AttrBuilder AB;
578 AB.addAttribute(Attribute::Cold);
579 Invoke->setAttributes(
580 AttributeList::get(C, AttributeList::FunctionIndex, AB));
581 Invoke->setDebugLoc(DebugLoc(MDNode::get(C, None)));
583 OperandBundleDef NewBundle("after", ConstantInt::get(Int32Ty, 7));
584 std::unique_ptr<InvokeInst> Clone(
585 InvokeInst::Create(Invoke.get(), NewBundle));
586 EXPECT_EQ(Invoke->getNormalDest(), Clone->getNormalDest());
587 EXPECT_EQ(Invoke->getUnwindDest(), Clone->getUnwindDest());
588 EXPECT_EQ(Invoke->getNumArgOperands(), Clone->getNumArgOperands());
589 EXPECT_EQ(Invoke->getArgOperand(0), Clone->getArgOperand(0));
590 EXPECT_EQ(Invoke->getCallingConv(), Clone->getCallingConv());
591 EXPECT_TRUE(Clone->hasFnAttr(Attribute::AttrKind::Cold));
592 EXPECT_EQ(Invoke->getDebugLoc(), Clone->getDebugLoc());
593 EXPECT_EQ(Clone->getNumOperandBundles(), 1U);
594 EXPECT_TRUE(Clone->getOperandBundle("after").hasValue());
597 TEST_F(ModuleWithFunctionTest, DropPoisonGeneratingFlags) {
598 auto *OnlyBB = BasicBlock::Create(Ctx, "bb", F);
599 auto *Arg0 = &*F->arg_begin();
601 IRBuilder<NoFolder> B(Ctx);
602 B.SetInsertPoint(OnlyBB);
605 auto *UI =
606 cast<Instruction>(B.CreateUDiv(Arg0, Arg0, "", /*isExact*/ true));
607 ASSERT_TRUE(UI->isExact());
608 UI->dropPoisonGeneratingFlags();
609 ASSERT_FALSE(UI->isExact());
613 auto *ShrI =
614 cast<Instruction>(B.CreateLShr(Arg0, Arg0, "", /*isExact*/ true));
615 ASSERT_TRUE(ShrI->isExact());
616 ShrI->dropPoisonGeneratingFlags();
617 ASSERT_FALSE(ShrI->isExact());
621 auto *AI = cast<Instruction>(
622 B.CreateAdd(Arg0, Arg0, "", /*HasNUW*/ true, /*HasNSW*/ false));
623 ASSERT_TRUE(AI->hasNoUnsignedWrap());
624 AI->dropPoisonGeneratingFlags();
625 ASSERT_FALSE(AI->hasNoUnsignedWrap());
626 ASSERT_FALSE(AI->hasNoSignedWrap());
630 auto *SI = cast<Instruction>(
631 B.CreateAdd(Arg0, Arg0, "", /*HasNUW*/ false, /*HasNSW*/ true));
632 ASSERT_TRUE(SI->hasNoSignedWrap());
633 SI->dropPoisonGeneratingFlags();
634 ASSERT_FALSE(SI->hasNoUnsignedWrap());
635 ASSERT_FALSE(SI->hasNoSignedWrap());
639 auto *ShlI = cast<Instruction>(
640 B.CreateShl(Arg0, Arg0, "", /*HasNUW*/ true, /*HasNSW*/ true));
641 ASSERT_TRUE(ShlI->hasNoSignedWrap());
642 ASSERT_TRUE(ShlI->hasNoUnsignedWrap());
643 ShlI->dropPoisonGeneratingFlags();
644 ASSERT_FALSE(ShlI->hasNoUnsignedWrap());
645 ASSERT_FALSE(ShlI->hasNoSignedWrap());
649 Value *GEPBase = Constant::getNullValue(B.getInt8PtrTy());
650 auto *GI = cast<GetElementPtrInst>(
651 B.CreateInBoundsGEP(B.getInt8Ty(), GEPBase, Arg0));
652 ASSERT_TRUE(GI->isInBounds());
653 GI->dropPoisonGeneratingFlags();
654 ASSERT_FALSE(GI->isInBounds());
658 TEST(InstructionsTest, GEPIndices) {
659 LLVMContext Context;
660 IRBuilder<NoFolder> Builder(Context);
661 Type *ElementTy = Builder.getInt8Ty();
662 Type *ArrTy = ArrayType::get(ArrayType::get(ElementTy, 64), 64);
663 Value *Indices[] = {
664 Builder.getInt32(0),
665 Builder.getInt32(13),
666 Builder.getInt32(42) };
668 Value *V = Builder.CreateGEP(ArrTy, UndefValue::get(PointerType::getUnqual(ArrTy)),
669 Indices);
670 ASSERT_TRUE(isa<GetElementPtrInst>(V));
672 auto *GEPI = cast<GetElementPtrInst>(V);
673 ASSERT_NE(GEPI->idx_begin(), GEPI->idx_end());
674 ASSERT_EQ(GEPI->idx_end(), std::next(GEPI->idx_begin(), 3));
675 EXPECT_EQ(Indices[0], GEPI->idx_begin()[0]);
676 EXPECT_EQ(Indices[1], GEPI->idx_begin()[1]);
677 EXPECT_EQ(Indices[2], GEPI->idx_begin()[2]);
678 EXPECT_EQ(GEPI->idx_begin(), GEPI->indices().begin());
679 EXPECT_EQ(GEPI->idx_end(), GEPI->indices().end());
681 const auto *CGEPI = GEPI;
682 ASSERT_NE(CGEPI->idx_begin(), CGEPI->idx_end());
683 ASSERT_EQ(CGEPI->idx_end(), std::next(CGEPI->idx_begin(), 3));
684 EXPECT_EQ(Indices[0], CGEPI->idx_begin()[0]);
685 EXPECT_EQ(Indices[1], CGEPI->idx_begin()[1]);
686 EXPECT_EQ(Indices[2], CGEPI->idx_begin()[2]);
687 EXPECT_EQ(CGEPI->idx_begin(), CGEPI->indices().begin());
688 EXPECT_EQ(CGEPI->idx_end(), CGEPI->indices().end());
690 delete GEPI;
693 TEST(InstructionsTest, SwitchInst) {
694 LLVMContext C;
696 std::unique_ptr<BasicBlock> BB1, BB2, BB3;
697 BB1.reset(BasicBlock::Create(C));
698 BB2.reset(BasicBlock::Create(C));
699 BB3.reset(BasicBlock::Create(C));
701 // We create block 0 after the others so that it gets destroyed first and
702 // clears the uses of the other basic blocks.
703 std::unique_ptr<BasicBlock> BB0(BasicBlock::Create(C));
705 auto *Int32Ty = Type::getInt32Ty(C);
707 SwitchInst *SI =
708 SwitchInst::Create(UndefValue::get(Int32Ty), BB0.get(), 3, BB0.get());
709 SI->addCase(ConstantInt::get(Int32Ty, 1), BB1.get());
710 SI->addCase(ConstantInt::get(Int32Ty, 2), BB2.get());
711 SI->addCase(ConstantInt::get(Int32Ty, 3), BB3.get());
713 auto CI = SI->case_begin();
714 ASSERT_NE(CI, SI->case_end());
715 EXPECT_EQ(1, CI->getCaseValue()->getSExtValue());
716 EXPECT_EQ(BB1.get(), CI->getCaseSuccessor());
717 EXPECT_EQ(2, (CI + 1)->getCaseValue()->getSExtValue());
718 EXPECT_EQ(BB2.get(), (CI + 1)->getCaseSuccessor());
719 EXPECT_EQ(3, (CI + 2)->getCaseValue()->getSExtValue());
720 EXPECT_EQ(BB3.get(), (CI + 2)->getCaseSuccessor());
721 EXPECT_EQ(CI + 1, std::next(CI));
722 EXPECT_EQ(CI + 2, std::next(CI, 2));
723 EXPECT_EQ(CI + 3, std::next(CI, 3));
724 EXPECT_EQ(SI->case_end(), CI + 3);
725 EXPECT_EQ(0, CI - CI);
726 EXPECT_EQ(1, (CI + 1) - CI);
727 EXPECT_EQ(2, (CI + 2) - CI);
728 EXPECT_EQ(3, SI->case_end() - CI);
729 EXPECT_EQ(3, std::distance(CI, SI->case_end()));
731 auto CCI = const_cast<const SwitchInst *>(SI)->case_begin();
732 SwitchInst::ConstCaseIt CCE = SI->case_end();
733 ASSERT_NE(CCI, SI->case_end());
734 EXPECT_EQ(1, CCI->getCaseValue()->getSExtValue());
735 EXPECT_EQ(BB1.get(), CCI->getCaseSuccessor());
736 EXPECT_EQ(2, (CCI + 1)->getCaseValue()->getSExtValue());
737 EXPECT_EQ(BB2.get(), (CCI + 1)->getCaseSuccessor());
738 EXPECT_EQ(3, (CCI + 2)->getCaseValue()->getSExtValue());
739 EXPECT_EQ(BB3.get(), (CCI + 2)->getCaseSuccessor());
740 EXPECT_EQ(CCI + 1, std::next(CCI));
741 EXPECT_EQ(CCI + 2, std::next(CCI, 2));
742 EXPECT_EQ(CCI + 3, std::next(CCI, 3));
743 EXPECT_EQ(CCE, CCI + 3);
744 EXPECT_EQ(0, CCI - CCI);
745 EXPECT_EQ(1, (CCI + 1) - CCI);
746 EXPECT_EQ(2, (CCI + 2) - CCI);
747 EXPECT_EQ(3, CCE - CCI);
748 EXPECT_EQ(3, std::distance(CCI, CCE));
750 // Make sure that the const iterator is compatible with a const auto ref.
751 const auto &Handle = *CCI;
752 EXPECT_EQ(1, Handle.getCaseValue()->getSExtValue());
753 EXPECT_EQ(BB1.get(), Handle.getCaseSuccessor());
756 TEST(InstructionsTest, CommuteShuffleMask) {
757 SmallVector<int, 16> Indices({-1, 0, 7});
758 ShuffleVectorInst::commuteShuffleMask(Indices, 4);
759 EXPECT_THAT(Indices, testing::ContainerEq(ArrayRef<int>({-1, 4, 3})));
762 TEST(InstructionsTest, ShuffleMaskQueries) {
763 // Create the elements for various constant vectors.
764 LLVMContext Ctx;
765 Type *Int32Ty = Type::getInt32Ty(Ctx);
766 Constant *CU = UndefValue::get(Int32Ty);
767 Constant *C0 = ConstantInt::get(Int32Ty, 0);
768 Constant *C1 = ConstantInt::get(Int32Ty, 1);
769 Constant *C2 = ConstantInt::get(Int32Ty, 2);
770 Constant *C3 = ConstantInt::get(Int32Ty, 3);
771 Constant *C4 = ConstantInt::get(Int32Ty, 4);
772 Constant *C5 = ConstantInt::get(Int32Ty, 5);
773 Constant *C6 = ConstantInt::get(Int32Ty, 6);
774 Constant *C7 = ConstantInt::get(Int32Ty, 7);
776 Constant *Identity = ConstantVector::get({C0, CU, C2, C3, C4});
777 EXPECT_TRUE(ShuffleVectorInst::isIdentityMask(Identity));
778 EXPECT_FALSE(ShuffleVectorInst::isSelectMask(Identity)); // identity is distinguished from select
779 EXPECT_FALSE(ShuffleVectorInst::isReverseMask(Identity));
780 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(Identity)); // identity is always single source
781 EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(Identity));
782 EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(Identity));
784 Constant *Select = ConstantVector::get({CU, C1, C5});
785 EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(Select));
786 EXPECT_TRUE(ShuffleVectorInst::isSelectMask(Select));
787 EXPECT_FALSE(ShuffleVectorInst::isReverseMask(Select));
788 EXPECT_FALSE(ShuffleVectorInst::isSingleSourceMask(Select));
789 EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(Select));
790 EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(Select));
792 Constant *Reverse = ConstantVector::get({C3, C2, C1, CU});
793 EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(Reverse));
794 EXPECT_FALSE(ShuffleVectorInst::isSelectMask(Reverse));
795 EXPECT_TRUE(ShuffleVectorInst::isReverseMask(Reverse));
796 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(Reverse)); // reverse is always single source
797 EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(Reverse));
798 EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(Reverse));
800 Constant *SingleSource = ConstantVector::get({C2, C2, C0, CU});
801 EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(SingleSource));
802 EXPECT_FALSE(ShuffleVectorInst::isSelectMask(SingleSource));
803 EXPECT_FALSE(ShuffleVectorInst::isReverseMask(SingleSource));
804 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(SingleSource));
805 EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(SingleSource));
806 EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(SingleSource));
808 Constant *ZeroEltSplat = ConstantVector::get({C0, C0, CU, C0});
809 EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(ZeroEltSplat));
810 EXPECT_FALSE(ShuffleVectorInst::isSelectMask(ZeroEltSplat));
811 EXPECT_FALSE(ShuffleVectorInst::isReverseMask(ZeroEltSplat));
812 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(ZeroEltSplat)); // 0-splat is always single source
813 EXPECT_TRUE(ShuffleVectorInst::isZeroEltSplatMask(ZeroEltSplat));
814 EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(ZeroEltSplat));
816 Constant *Transpose = ConstantVector::get({C0, C4, C2, C6});
817 EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(Transpose));
818 EXPECT_FALSE(ShuffleVectorInst::isSelectMask(Transpose));
819 EXPECT_FALSE(ShuffleVectorInst::isReverseMask(Transpose));
820 EXPECT_FALSE(ShuffleVectorInst::isSingleSourceMask(Transpose));
821 EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(Transpose));
822 EXPECT_TRUE(ShuffleVectorInst::isTransposeMask(Transpose));
824 // More tests to make sure the logic is/stays correct...
825 EXPECT_TRUE(ShuffleVectorInst::isIdentityMask(ConstantVector::get({CU, C1, CU, C3})));
826 EXPECT_TRUE(ShuffleVectorInst::isIdentityMask(ConstantVector::get({C4, CU, C6, CU})));
828 EXPECT_TRUE(ShuffleVectorInst::isSelectMask(ConstantVector::get({C4, C1, C6, CU})));
829 EXPECT_TRUE(ShuffleVectorInst::isSelectMask(ConstantVector::get({CU, C1, C6, C3})));
831 EXPECT_TRUE(ShuffleVectorInst::isReverseMask(ConstantVector::get({C7, C6, CU, C4})));
832 EXPECT_TRUE(ShuffleVectorInst::isReverseMask(ConstantVector::get({C3, CU, C1, CU})));
834 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(ConstantVector::get({C7, C5, CU, C7})));
835 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(ConstantVector::get({C3, C0, CU, C3})));
837 EXPECT_TRUE(ShuffleVectorInst::isZeroEltSplatMask(ConstantVector::get({C4, CU, CU, C4})));
838 EXPECT_TRUE(ShuffleVectorInst::isZeroEltSplatMask(ConstantVector::get({CU, C0, CU, C0})));
840 EXPECT_TRUE(ShuffleVectorInst::isTransposeMask(ConstantVector::get({C1, C5, C3, C7})));
841 EXPECT_TRUE(ShuffleVectorInst::isTransposeMask(ConstantVector::get({C1, C3})));
843 // Nothing special about the values here - just re-using inputs to reduce code.
844 Constant *V0 = ConstantVector::get({C0, C1, C2, C3});
845 Constant *V1 = ConstantVector::get({C3, C2, C1, C0});
847 // Identity with undef elts.
848 ShuffleVectorInst *Id1 = new ShuffleVectorInst(V0, V1,
849 ConstantVector::get({C0, C1, CU, CU}));
850 EXPECT_TRUE(Id1->isIdentity());
851 EXPECT_FALSE(Id1->isIdentityWithPadding());
852 EXPECT_FALSE(Id1->isIdentityWithExtract());
853 EXPECT_FALSE(Id1->isConcat());
854 delete Id1;
856 // Result has less elements than operands.
857 ShuffleVectorInst *Id2 = new ShuffleVectorInst(V0, V1,
858 ConstantVector::get({C0, C1, C2}));
859 EXPECT_FALSE(Id2->isIdentity());
860 EXPECT_FALSE(Id2->isIdentityWithPadding());
861 EXPECT_TRUE(Id2->isIdentityWithExtract());
862 EXPECT_FALSE(Id2->isConcat());
863 delete Id2;
865 // Result has less elements than operands; choose from Op1.
866 ShuffleVectorInst *Id3 = new ShuffleVectorInst(V0, V1,
867 ConstantVector::get({C4, CU, C6}));
868 EXPECT_FALSE(Id3->isIdentity());
869 EXPECT_FALSE(Id3->isIdentityWithPadding());
870 EXPECT_TRUE(Id3->isIdentityWithExtract());
871 EXPECT_FALSE(Id3->isConcat());
872 delete Id3;
874 // Result has less elements than operands; choose from Op0 and Op1 is not identity.
875 ShuffleVectorInst *Id4 = new ShuffleVectorInst(V0, V1,
876 ConstantVector::get({C4, C1, C6}));
877 EXPECT_FALSE(Id4->isIdentity());
878 EXPECT_FALSE(Id4->isIdentityWithPadding());
879 EXPECT_FALSE(Id4->isIdentityWithExtract());
880 EXPECT_FALSE(Id4->isConcat());
881 delete Id4;
883 // Result has more elements than operands, and extra elements are undef.
884 ShuffleVectorInst *Id5 = new ShuffleVectorInst(V0, V1,
885 ConstantVector::get({CU, C1, C2, C3, CU, CU}));
886 EXPECT_FALSE(Id5->isIdentity());
887 EXPECT_TRUE(Id5->isIdentityWithPadding());
888 EXPECT_FALSE(Id5->isIdentityWithExtract());
889 EXPECT_FALSE(Id5->isConcat());
890 delete Id5;
892 // Result has more elements than operands, and extra elements are undef; choose from Op1.
893 ShuffleVectorInst *Id6 = new ShuffleVectorInst(V0, V1,
894 ConstantVector::get({C4, C5, C6, CU, CU, CU}));
895 EXPECT_FALSE(Id6->isIdentity());
896 EXPECT_TRUE(Id6->isIdentityWithPadding());
897 EXPECT_FALSE(Id6->isIdentityWithExtract());
898 EXPECT_FALSE(Id6->isConcat());
899 delete Id6;
901 // Result has more elements than operands, but extra elements are not undef.
902 ShuffleVectorInst *Id7 = new ShuffleVectorInst(V0, V1,
903 ConstantVector::get({C0, C1, C2, C3, CU, C1}));
904 EXPECT_FALSE(Id7->isIdentity());
905 EXPECT_FALSE(Id7->isIdentityWithPadding());
906 EXPECT_FALSE(Id7->isIdentityWithExtract());
907 EXPECT_FALSE(Id7->isConcat());
908 delete Id7;
910 // Result has more elements than operands; choose from Op0 and Op1 is not identity.
911 ShuffleVectorInst *Id8 = new ShuffleVectorInst(V0, V1,
912 ConstantVector::get({C4, CU, C2, C3, CU, CU}));
913 EXPECT_FALSE(Id8->isIdentity());
914 EXPECT_FALSE(Id8->isIdentityWithPadding());
915 EXPECT_FALSE(Id8->isIdentityWithExtract());
916 EXPECT_FALSE(Id8->isConcat());
917 delete Id8;
919 // Result has twice as many elements as operands; choose consecutively from Op0 and Op1 is concat.
920 ShuffleVectorInst *Id9 = new ShuffleVectorInst(V0, V1,
921 ConstantVector::get({C0, CU, C2, C3, CU, CU, C6, C7}));
922 EXPECT_FALSE(Id9->isIdentity());
923 EXPECT_FALSE(Id9->isIdentityWithPadding());
924 EXPECT_FALSE(Id9->isIdentityWithExtract());
925 EXPECT_TRUE(Id9->isConcat());
926 delete Id9;
928 // Result has less than twice as many elements as operands, so not a concat.
929 ShuffleVectorInst *Id10 = new ShuffleVectorInst(V0, V1,
930 ConstantVector::get({C0, CU, C2, C3, CU, CU, C6}));
931 EXPECT_FALSE(Id10->isIdentity());
932 EXPECT_FALSE(Id10->isIdentityWithPadding());
933 EXPECT_FALSE(Id10->isIdentityWithExtract());
934 EXPECT_FALSE(Id10->isConcat());
935 delete Id10;
937 // Result has more than twice as many elements as operands, so not a concat.
938 ShuffleVectorInst *Id11 = new ShuffleVectorInst(V0, V1,
939 ConstantVector::get({C0, CU, C2, C3, CU, CU, C6, C7, CU}));
940 EXPECT_FALSE(Id11->isIdentity());
941 EXPECT_FALSE(Id11->isIdentityWithPadding());
942 EXPECT_FALSE(Id11->isIdentityWithExtract());
943 EXPECT_FALSE(Id11->isConcat());
944 delete Id11;
946 // If an input is undef, it's not a concat.
947 // TODO: IdentityWithPadding should be true here even though the high mask values are not undef.
948 ShuffleVectorInst *Id12 = new ShuffleVectorInst(V0, ConstantVector::get({CU, CU, CU, CU}),
949 ConstantVector::get({C0, CU, C2, C3, CU, CU, C6, C7}));
950 EXPECT_FALSE(Id12->isIdentity());
951 EXPECT_FALSE(Id12->isIdentityWithPadding());
952 EXPECT_FALSE(Id12->isIdentityWithExtract());
953 EXPECT_FALSE(Id12->isConcat());
954 delete Id12;
957 TEST(InstructionsTest, SkipDebug) {
958 LLVMContext C;
959 std::unique_ptr<Module> M = parseIR(C,
961 declare void @llvm.dbg.value(metadata, metadata, metadata)
963 define void @f() {
964 entry:
965 call void @llvm.dbg.value(metadata i32 0, metadata !11, metadata !DIExpression()), !dbg !13
966 ret void
969 !llvm.dbg.cu = !{!0}
970 !llvm.module.flags = !{!3, !4}
971 !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 6.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
972 !1 = !DIFile(filename: "t2.c", directory: "foo")
973 !2 = !{}
974 !3 = !{i32 2, !"Dwarf Version", i32 4}
975 !4 = !{i32 2, !"Debug Info Version", i32 3}
976 !8 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 1, type: !9, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: false, unit: !0, retainedNodes: !2)
977 !9 = !DISubroutineType(types: !10)
978 !10 = !{null}
979 !11 = !DILocalVariable(name: "x", scope: !8, file: !1, line: 2, type: !12)
980 !12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
981 !13 = !DILocation(line: 2, column: 7, scope: !8)
982 )");
983 ASSERT_TRUE(M);
984 Function *F = cast<Function>(M->getNamedValue("f"));
985 BasicBlock &BB = F->front();
987 // The first non-debug instruction is the terminator.
988 auto *Term = BB.getTerminator();
989 EXPECT_EQ(Term, BB.begin()->getNextNonDebugInstruction());
990 EXPECT_EQ(Term->getIterator(), skipDebugIntrinsics(BB.begin()));
992 // After the terminator, there are no non-debug instructions.
993 EXPECT_EQ(nullptr, Term->getNextNonDebugInstruction());
996 } // end anonymous namespace
997 } // end namespace llvm