1 //===- llvm/unittest/IR/InstructionsTest.cpp - Instructions unit tests ----===//
2 //
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
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"
28 #include <memory>
29
30 namespace llvm {
31 namespace {
32
parseIR(LLVMContext & C,const char * IR)33 static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) {
34 SMDiagnostic Err;
35 std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C);
36 if (!Mod)
37 Err.print("InstructionsTests", errs());
38 return Mod;
39 }
40
TEST(InstructionsTest,ReturnInst)41 TEST(InstructionsTest, ReturnInst) {
42 LLVMContext C;
43
44 // test for PR6589
45 const ReturnInst* r0 = ReturnInst::Create(C);
46 EXPECT_EQ(r0->getNumOperands(), 0U);
47 EXPECT_EQ(r0->op_begin(), r0->op_end());
48
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);
55 EXPECT_EQ(One, *b);
56 EXPECT_EQ(One, r1->getOperand(0));
57 ++b;
58 EXPECT_EQ(r1->op_end(), b);
59
60 // clean up
61 delete r0;
62 delete r1;
63 }
64
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 {
68 protected:
ModuleWithFunctionTest()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));
73 FunctionType *FTy =
74 FunctionType::get(Type::getVoidTy(Ctx), FArgTypes, false);
75 F = Function::Create(FTy, Function::ExternalLinkage, "", M.get());
76 }
77
78 LLVMContext Ctx;
79 std::unique_ptr<Module> M;
80 SmallVector<Type *, 3> FArgTypes;
81 Function *F;
82 };
83
TEST_F(ModuleWithFunctionTest,CallInst)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));
89
90 // Make sure iteration over a call's arguments works as expected.
91 unsigned Idx = 0;
92 for (Value *Arg : Call->arg_operands()) {
93 EXPECT_EQ(FArgTypes[Idx], Arg->getType());
94 EXPECT_EQ(Call->getArgOperand(Idx)->getType(), Arg->getType());
95 Idx++;
96 }
97 }
98
TEST_F(ModuleWithFunctionTest,InvokeInst)99 TEST_F(ModuleWithFunctionTest, InvokeInst) {
100 BasicBlock *BB1 = BasicBlock::Create(Ctx, "", F);
101 BasicBlock *BB2 = BasicBlock::Create(Ctx, "", F);
102
103 Value *Args[] = {ConstantInt::get(Type::getInt8Ty(Ctx), 20),
104 ConstantInt::get(Type::getInt32Ty(Ctx), 9999),
105 ConstantInt::get(Type::getInt64Ty(Ctx), 42)};
106 std::unique_ptr<InvokeInst> Invoke(InvokeInst::Create(F, BB1, BB2, Args));
107
108 // Make sure iteration over invoke's arguments works as expected.
109 unsigned Idx = 0;
110 for (Value *Arg : Invoke->arg_operands()) {
111 EXPECT_EQ(FArgTypes[Idx], Arg->getType());
112 EXPECT_EQ(Invoke->getArgOperand(Idx)->getType(), Arg->getType());
113 Idx++;
114 }
115 }
116
TEST(InstructionsTest,BranchInst)117 TEST(InstructionsTest, BranchInst) {
118 LLVMContext C;
119
120 // Make a BasicBlocks
121 BasicBlock* bb0 = BasicBlock::Create(C);
122 BasicBlock* bb1 = BasicBlock::Create(C);
123
124 // Mandatory BranchInst
125 const BranchInst* b0 = BranchInst::Create(bb0);
126
127 EXPECT_TRUE(b0->isUnconditional());
128 EXPECT_FALSE(b0->isConditional());
129 EXPECT_EQ(1U, b0->getNumSuccessors());
130
131 // check num operands
132 EXPECT_EQ(1U, b0->getNumOperands());
133
134 EXPECT_NE(b0->op_begin(), b0->op_end());
135 EXPECT_EQ(b0->op_end(), std::next(b0->op_begin()));
136
137 EXPECT_EQ(b0->op_end(), std::next(b0->op_begin()));
138
139 IntegerType* Int1 = IntegerType::get(C, 1);
140 Constant* One = ConstantInt::get(Int1, 1, true);
141
142 // Conditional BranchInst
143 BranchInst* b1 = BranchInst::Create(bb0, bb1, One);
144
145 EXPECT_FALSE(b1->isUnconditional());
146 EXPECT_TRUE(b1->isConditional());
147 EXPECT_EQ(2U, b1->getNumSuccessors());
148
149 // check num operands
150 EXPECT_EQ(3U, b1->getNumOperands());
151
152 User::const_op_iterator b(b1->op_begin());
153
154 // check COND
155 EXPECT_NE(b, b1->op_end());
156 EXPECT_EQ(One, *b);
157 EXPECT_EQ(One, b1->getOperand(0));
158 EXPECT_EQ(One, b1->getCondition());
159 ++b;
160
161 // check ELSE
162 EXPECT_EQ(bb1, *b);
163 EXPECT_EQ(bb1, b1->getOperand(1));
164 EXPECT_EQ(bb1, b1->getSuccessor(1));
165 ++b;
166
167 // check THEN
168 EXPECT_EQ(bb0, *b);
169 EXPECT_EQ(bb0, b1->getOperand(2));
170 EXPECT_EQ(bb0, b1->getSuccessor(0));
171 ++b;
172
173 EXPECT_EQ(b1->op_end(), b);
174
175 // clean up
176 delete b0;
177 delete b1;
178
179 delete bb0;
180 delete bb1;
181 }
182
TEST(InstructionsTest,CastInst)183 TEST(InstructionsTest, CastInst) {
184 LLVMContext C;
185
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);
193
194 Type *HalfTy = Type::getHalfTy(C);
195 Type *FloatTy = Type::getFloatTy(C);
196 Type *DoubleTy = Type::getDoubleTy(C);
197
198 Type *V2Int32Ty = VectorType::get(Int32Ty, 2);
199 Type *V2Int64Ty = VectorType::get(Int64Ty, 2);
200 Type *V4Int16Ty = VectorType::get(Int16Ty, 4);
201
202 Type *Int32PtrTy = PointerType::get(Int32Ty, 0);
203 Type *Int64PtrTy = PointerType::get(Int64Ty, 0);
204
205 Type *Int32PtrAS1Ty = PointerType::get(Int32Ty, 1);
206 Type *Int64PtrAS1Ty = PointerType::get(Int64Ty, 1);
207
208 Type *V2Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 2);
209 Type *V2Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 2);
210 Type *V4Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 4);
211 Type *V4Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 4);
212
213 Type *V2Int64PtrTy = VectorType::get(Int64PtrTy, 2);
214 Type *V2Int32PtrTy = VectorType::get(Int32PtrTy, 2);
215 Type *V4Int32PtrTy = VectorType::get(Int32PtrTy, 4);
216
217 const Constant* c8 = Constant::getNullValue(V8x8Ty);
218 const Constant* c64 = Constant::getNullValue(V8x64Ty);
219
220 const Constant *v2ptr32 = Constant::getNullValue(V2Int32PtrTy);
221
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));
229
230 EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, X86MMXTy));
231 EXPECT_FALSE(CastInst::isBitCastable(X86MMXTy, V8x8Ty));
232 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, X86MMXTy));
233 EXPECT_FALSE(CastInst::isBitCastable(V8x64Ty, V8x8Ty));
234 EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, V8x64Ty));
235
236 // Check address space casts are rejected since we don't know the sizes here
237 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, Int32PtrAS1Ty));
238 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrAS1Ty, Int32PtrTy));
239 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, V2Int32PtrAS1Ty));
240 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int32PtrTy));
241 EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int64PtrAS1Ty));
242 EXPECT_TRUE(CastInst::isCastable(V2Int32PtrAS1Ty, V2Int32PtrTy));
243 EXPECT_EQ(CastInst::AddrSpaceCast, CastInst::getCastOpcode(v2ptr32, true,
244 V2Int32PtrAS1Ty,
245 true));
246
247 // Test mismatched number of elements for pointers
248 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int64PtrAS1Ty));
249 EXPECT_FALSE(CastInst::isBitCastable(V4Int64PtrAS1Ty, V2Int32PtrAS1Ty));
250 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int32PtrAS1Ty));
251 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, V2Int32PtrTy));
252 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int32PtrTy));
253
254 EXPECT_TRUE(CastInst::isBitCastable(Int32PtrTy, Int64PtrTy));
255 EXPECT_FALSE(CastInst::isBitCastable(DoubleTy, FloatTy));
256 EXPECT_FALSE(CastInst::isBitCastable(FloatTy, DoubleTy));
257 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy));
258 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy));
259 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, Int32Ty));
260 EXPECT_TRUE(CastInst::isBitCastable(Int16Ty, HalfTy));
261 EXPECT_TRUE(CastInst::isBitCastable(Int32Ty, FloatTy));
262 EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, Int64Ty));
263
264 EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, V4Int16Ty));
265 EXPECT_FALSE(CastInst::isBitCastable(Int32Ty, Int64Ty));
266 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, Int32Ty));
267
268 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int64Ty));
269 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, V2Int32PtrTy));
270 EXPECT_TRUE(CastInst::isBitCastable(V2Int64PtrTy, V2Int32PtrTy));
271 EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrTy, V2Int64PtrTy));
272 EXPECT_FALSE(CastInst::isBitCastable(V2Int32Ty, V2Int64Ty));
273 EXPECT_FALSE(CastInst::isBitCastable(V2Int64Ty, V2Int32Ty));
274
275
276 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
277 Constant::getNullValue(V4Int32PtrTy),
278 V2Int32PtrTy));
279 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
280 Constant::getNullValue(V2Int32PtrTy),
281 V4Int32PtrTy));
282
283 EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast,
284 Constant::getNullValue(V4Int32PtrAS1Ty),
285 V2Int32PtrTy));
286 EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast,
287 Constant::getNullValue(V2Int32PtrTy),
288 V4Int32PtrAS1Ty));
289
290
291 // Check that assertion is not hit when creating a cast with a vector of
292 // pointers
293 // First form
294 BasicBlock *BB = BasicBlock::Create(C);
295 Constant *NullV2I32Ptr = Constant::getNullValue(V2Int32PtrTy);
296 auto Inst1 = CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty, "foo", BB);
297
298 // Second form
299 auto Inst2 = CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty);
300
301 delete Inst2;
302 Inst1->eraseFromParent();
303 delete BB;
304 }
305
TEST(InstructionsTest,VectorGep)306 TEST(InstructionsTest, VectorGep) {
307 LLVMContext C;
308
309 // Type Definitions
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);
314
315 VectorType *V2xi8PTy = VectorType::get(Ptri8Ty, 2);
316 VectorType *V2xi32PTy = VectorType::get(Ptri32Ty, 2);
317
318 // Test different aspects of the vector-of-pointers type
319 // and GEPs which use this type.
320 ConstantInt *Ci32a = ConstantInt::get(C, APInt(32, 1492));
321 ConstantInt *Ci32b = ConstantInt::get(C, APInt(32, 1948));
322 std::vector<Constant*> ConstVa(2, Ci32a);
323 std::vector<Constant*> ConstVb(2, Ci32b);
324 Constant *C2xi32a = ConstantVector::get(ConstVa);
325 Constant *C2xi32b = ConstantVector::get(ConstVb);
326
327 CastInst *PtrVecA = new IntToPtrInst(C2xi32a, V2xi32PTy);
328 CastInst *PtrVecB = new IntToPtrInst(C2xi32b, V2xi32PTy);
329
330 ICmpInst *ICmp0 = new ICmpInst(ICmpInst::ICMP_SGT, PtrVecA, PtrVecB);
331 ICmpInst *ICmp1 = new ICmpInst(ICmpInst::ICMP_ULT, PtrVecA, PtrVecB);
332 EXPECT_NE(ICmp0, ICmp1); // suppress warning.
333
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.
338
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);
343
344 CastInst *BTC0 = new BitCastInst(Gep0, V2xi8PTy);
345 CastInst *BTC1 = new BitCastInst(Gep1, V2xi8PTy);
346 CastInst *BTC2 = new BitCastInst(Gep2, V2xi8PTy);
347 CastInst *BTC3 = new BitCastInst(Gep3, V2xi8PTy);
348
349 Value *S0 = BTC0->stripPointerCasts();
350 Value *S1 = BTC1->stripPointerCasts();
351 Value *S2 = BTC2->stripPointerCasts();
352 Value *S3 = BTC3->stripPointerCasts();
353
354 EXPECT_NE(S0, Gep0);
355 EXPECT_NE(S1, Gep1);
356 EXPECT_NE(S2, Gep2);
357 EXPECT_NE(S3, Gep3);
358
359 int64_t Offset;
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);
368
369 // Gep of Geps
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);
374
375 EXPECT_EQ(GepII0->getNumIndices(), 1u);
376 EXPECT_EQ(GepII1->getNumIndices(), 1u);
377 EXPECT_EQ(GepII2->getNumIndices(), 1u);
378 EXPECT_EQ(GepII3->getNumIndices(), 1u);
379
380 EXPECT_FALSE(GepII0->hasAllZeroIndices());
381 EXPECT_FALSE(GepII1->hasAllZeroIndices());
382 EXPECT_FALSE(GepII2->hasAllZeroIndices());
383 EXPECT_FALSE(GepII3->hasAllZeroIndices());
384
385 delete GepII0;
386 delete GepII1;
387 delete GepII2;
388 delete GepII3;
389
390 delete BTC0;
391 delete BTC1;
392 delete BTC2;
393 delete BTC3;
394
395 delete Gep0;
396 delete Gep1;
397 delete Gep2;
398 delete Gep3;
399
400 ICmp2->eraseFromParent();
401 delete BB0;
402
403 delete ICmp0;
404 delete ICmp1;
405 delete PtrVecA;
406 delete PtrVecB;
407 }
408
TEST(InstructionsTest,FPMathOperator)409 TEST(InstructionsTest, FPMathOperator) {
410 LLVMContext Context;
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);
419 V1->deleteValue();
420 I->deleteValue();
421 }
422
423
TEST(InstructionsTest,isEliminableCastPair)424 TEST(InstructionsTest, isEliminableCastPair) {
425 LLVMContext C;
426
427 Type* Int16Ty = Type::getInt16Ty(C);
428 Type* Int32Ty = Type::getInt32Ty(C);
429 Type* Int64Ty = Type::getInt64Ty(C);
430 Type* Int64PtrTy = Type::getInt64PtrTy(C);
431
432 // Source and destination pointers have same size -> bitcast.
433 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
434 CastInst::IntToPtr,
435 Int64PtrTy, Int64Ty, Int64PtrTy,
436 Int32Ty, nullptr, Int32Ty),
437 CastInst::BitCast);
438
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,
442 CastInst::IntToPtr,
443 Int64PtrTy, Int64Ty, Int64PtrTy,
444 nullptr, nullptr, nullptr),
445 CastInst::BitCast);
446
447 // Source and destination have unknown sizes, but the same address space and
448 // the intermediate int is not the maximum pointer size -> nothing
449 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
450 CastInst::IntToPtr,
451 Int64PtrTy, Int32Ty, Int64PtrTy,
452 nullptr, nullptr, nullptr),
453 0U);
454
455 // Middle pointer big enough -> bitcast.
456 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
457 CastInst::PtrToInt,
458 Int64Ty, Int64PtrTy, Int64Ty,
459 nullptr, Int64Ty, nullptr),
460 CastInst::BitCast);
461
462 // Middle pointer too small -> fail.
463 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
464 CastInst::PtrToInt,
465 Int64Ty, Int64PtrTy, Int64Ty,
466 nullptr, Int32Ty, nullptr),
467 0U);
468
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");
474
475 Type* Int64PtrTyAS1 = Type::getInt64PtrTy(C, 1);
476 Type* Int64PtrTyAS2 = Type::getInt64PtrTy(C, 2);
477
478 IntegerType *Int16SizePtr = DL.getIntPtrType(C, 1);
479 IntegerType *Int64SizePtr = DL.getIntPtrType(C, 2);
480
481 // Cannot simplify inttoptr, addrspacecast
482 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
483 CastInst::AddrSpaceCast,
484 Int16Ty, Int64PtrTyAS1, Int64PtrTyAS2,
485 nullptr, Int16SizePtr, Int64SizePtr),
486 0U);
487
488 // Cannot simplify addrspacecast, ptrtoint
489 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::AddrSpaceCast,
490 CastInst::PtrToInt,
491 Int64PtrTyAS1, Int64PtrTyAS2, Int16Ty,
492 Int64SizePtr, Int16SizePtr, nullptr),
493 0U);
494
495 // Pass since the bitcast address spaces are the same
496 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
497 CastInst::BitCast,
498 Int16Ty, Int64PtrTyAS1, Int64PtrTyAS1,
499 nullptr, nullptr, nullptr),
500 CastInst::IntToPtr);
501
502 }
503
TEST(InstructionsTest,CloneCall)504 TEST(InstructionsTest, CloneCall) {
505 LLVMContext C;
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());
510 Value *Args[] = {
511 ConstantInt::get(Int32Ty, 1),
512 ConstantInt::get(Int32Ty, 2),
513 ConstantInt::get(Int32Ty, 3)
514 };
515 std::unique_ptr<CallInst> Call(CallInst::Create(Callee, Args, "result"));
516
517 // Test cloning the tail call kind.
518 CallInst::TailCallKind Kinds[] = {CallInst::TCK_None, CallInst::TCK_Tail,
519 CallInst::TCK_MustTail};
520 for (CallInst::TailCallKind TCK : Kinds) {
521 Call->setTailCallKind(TCK);
522 std::unique_ptr<CallInst> Clone(cast<CallInst>(Call->clone()));
523 EXPECT_EQ(Call->getTailCallKind(), Clone->getTailCallKind());
524 }
525 Call->setTailCallKind(CallInst::TCK_None);
526
527 // Test cloning an attribute.
528 {
529 AttrBuilder AB;
530 AB.addAttribute(Attribute::ReadOnly);
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());
535 }
536 }
537
TEST(InstructionsTest,AlterCallBundles)538 TEST(InstructionsTest, AlterCallBundles) {
539 LLVMContext C;
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);
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)));
552
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());
563 }
564
TEST(InstructionsTest,AlterInvokeBundles)565 TEST(InstructionsTest, AlterInvokeBundles) {
566 LLVMContext C;
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"));
576 AttrBuilder AB;
577 AB.addAttribute(Attribute::Cold);
578 Invoke->setAttributes(
579 AttributeList::get(C, AttributeList::FunctionIndex, AB));
580 Invoke->setDebugLoc(DebugLoc(MDNode::get(C, None)));
581
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());
594 }
595
TEST_F(ModuleWithFunctionTest,DropPoisonGeneratingFlags)596 TEST_F(ModuleWithFunctionTest, DropPoisonGeneratingFlags) {
597 auto *OnlyBB = BasicBlock::Create(Ctx, "bb", F);
598 auto *Arg0 = &*F->arg_begin();
599
600 IRBuilder<NoFolder> B(Ctx);
601 B.SetInsertPoint(OnlyBB);
602
603 {
604 auto *UI =
605 cast<Instruction>(B.CreateUDiv(Arg0, Arg0, "", /*isExact*/ true));
606 ASSERT_TRUE(UI->isExact());
607 UI->dropPoisonGeneratingFlags();
608 ASSERT_FALSE(UI->isExact());
609 }
610
611 {
612 auto *ShrI =
613 cast<Instruction>(B.CreateLShr(Arg0, Arg0, "", /*isExact*/ true));
614 ASSERT_TRUE(ShrI->isExact());
615 ShrI->dropPoisonGeneratingFlags();
616 ASSERT_FALSE(ShrI->isExact());
617 }
618
619 {
620 auto *AI = cast<Instruction>(
621 B.CreateAdd(Arg0, Arg0, "", /*HasNUW*/ true, /*HasNSW*/ false));
622 ASSERT_TRUE(AI->hasNoUnsignedWrap());
623 AI->dropPoisonGeneratingFlags();
624 ASSERT_FALSE(AI->hasNoUnsignedWrap());
625 ASSERT_FALSE(AI->hasNoSignedWrap());
626 }
627
628 {
629 auto *SI = cast<Instruction>(
630 B.CreateAdd(Arg0, Arg0, "", /*HasNUW*/ false, /*HasNSW*/ true));
631 ASSERT_TRUE(SI->hasNoSignedWrap());
632 SI->dropPoisonGeneratingFlags();
633 ASSERT_FALSE(SI->hasNoUnsignedWrap());
634 ASSERT_FALSE(SI->hasNoSignedWrap());
635 }
636
637 {
638 auto *ShlI = cast<Instruction>(
639 B.CreateShl(Arg0, Arg0, "", /*HasNUW*/ true, /*HasNSW*/ true));
640 ASSERT_TRUE(ShlI->hasNoSignedWrap());
641 ASSERT_TRUE(ShlI->hasNoUnsignedWrap());
642 ShlI->dropPoisonGeneratingFlags();
643 ASSERT_FALSE(ShlI->hasNoUnsignedWrap());
644 ASSERT_FALSE(ShlI->hasNoSignedWrap());
645 }
646
647 {
648 Value *GEPBase = Constant::getNullValue(B.getInt8PtrTy());
649 auto *GI = cast<GetElementPtrInst>(B.CreateInBoundsGEP(GEPBase, {Arg0}));
650 ASSERT_TRUE(GI->isInBounds());
651 GI->dropPoisonGeneratingFlags();
652 ASSERT_FALSE(GI->isInBounds());
653 }
654 }
655
TEST(InstructionsTest,GEPIndices)656 TEST(InstructionsTest, GEPIndices) {
657 LLVMContext Context;
658 IRBuilder<NoFolder> Builder(Context);
659 Type *ElementTy = Builder.getInt8Ty();
660 Type *ArrTy = ArrayType::get(ArrayType::get(ElementTy, 64), 64);
661 Value *Indices[] = {
662 Builder.getInt32(0),
663 Builder.getInt32(13),
664 Builder.getInt32(42) };
665
666 Value *V = Builder.CreateGEP(ArrTy, UndefValue::get(PointerType::getUnqual(ArrTy)),
667 Indices);
668 ASSERT_TRUE(isa<GetElementPtrInst>(V));
669
670 auto *GEPI = cast<GetElementPtrInst>(V);
671 ASSERT_NE(GEPI->idx_begin(), GEPI->idx_end());
672 ASSERT_EQ(GEPI->idx_end(), std::next(GEPI->idx_begin(), 3));
673 EXPECT_EQ(Indices[0], GEPI->idx_begin()[0]);
674 EXPECT_EQ(Indices[1], GEPI->idx_begin()[1]);
675 EXPECT_EQ(Indices[2], GEPI->idx_begin()[2]);
676 EXPECT_EQ(GEPI->idx_begin(), GEPI->indices().begin());
677 EXPECT_EQ(GEPI->idx_end(), GEPI->indices().end());
678
679 const auto *CGEPI = GEPI;
680 ASSERT_NE(CGEPI->idx_begin(), CGEPI->idx_end());
681 ASSERT_EQ(CGEPI->idx_end(), std::next(CGEPI->idx_begin(), 3));
682 EXPECT_EQ(Indices[0], CGEPI->idx_begin()[0]);
683 EXPECT_EQ(Indices[1], CGEPI->idx_begin()[1]);
684 EXPECT_EQ(Indices[2], CGEPI->idx_begin()[2]);
685 EXPECT_EQ(CGEPI->idx_begin(), CGEPI->indices().begin());
686 EXPECT_EQ(CGEPI->idx_end(), CGEPI->indices().end());
687
688 delete GEPI;
689 }
690
TEST(InstructionsTest,SwitchInst)691 TEST(InstructionsTest, SwitchInst) {
692 LLVMContext C;
693
694 std::unique_ptr<BasicBlock> BB1, BB2, BB3;
695 BB1.reset(BasicBlock::Create(C));
696 BB2.reset(BasicBlock::Create(C));
697 BB3.reset(BasicBlock::Create(C));
698
699 // We create block 0 after the others so that it gets destroyed first and
700 // clears the uses of the other basic blocks.
701 std::unique_ptr<BasicBlock> BB0(BasicBlock::Create(C));
702
703 auto *Int32Ty = Type::getInt32Ty(C);
704
705 SwitchInst *SI =
706 SwitchInst::Create(UndefValue::get(Int32Ty), BB0.get(), 3, BB0.get());
707 SI->addCase(ConstantInt::get(Int32Ty, 1), BB1.get());
708 SI->addCase(ConstantInt::get(Int32Ty, 2), BB2.get());
709 SI->addCase(ConstantInt::get(Int32Ty, 3), BB3.get());
710
711 auto CI = SI->case_begin();
712 ASSERT_NE(CI, SI->case_end());
713 EXPECT_EQ(1, CI->getCaseValue()->getSExtValue());
714 EXPECT_EQ(BB1.get(), CI->getCaseSuccessor());
715 EXPECT_EQ(2, (CI + 1)->getCaseValue()->getSExtValue());
716 EXPECT_EQ(BB2.get(), (CI + 1)->getCaseSuccessor());
717 EXPECT_EQ(3, (CI + 2)->getCaseValue()->getSExtValue());
718 EXPECT_EQ(BB3.get(), (CI + 2)->getCaseSuccessor());
719 EXPECT_EQ(CI + 1, std::next(CI));
720 EXPECT_EQ(CI + 2, std::next(CI, 2));
721 EXPECT_EQ(CI + 3, std::next(CI, 3));
722 EXPECT_EQ(SI->case_end(), CI + 3);
723 EXPECT_EQ(0, CI - CI);
724 EXPECT_EQ(1, (CI + 1) - CI);
725 EXPECT_EQ(2, (CI + 2) - CI);
726 EXPECT_EQ(3, SI->case_end() - CI);
727 EXPECT_EQ(3, std::distance(CI, SI->case_end()));
728
729 auto CCI = const_cast<const SwitchInst *>(SI)->case_begin();
730 SwitchInst::ConstCaseIt CCE = SI->case_end();
731 ASSERT_NE(CCI, SI->case_end());
732 EXPECT_EQ(1, CCI->getCaseValue()->getSExtValue());
733 EXPECT_EQ(BB1.get(), CCI->getCaseSuccessor());
734 EXPECT_EQ(2, (CCI + 1)->getCaseValue()->getSExtValue());
735 EXPECT_EQ(BB2.get(), (CCI + 1)->getCaseSuccessor());
736 EXPECT_EQ(3, (CCI + 2)->getCaseValue()->getSExtValue());
737 EXPECT_EQ(BB3.get(), (CCI + 2)->getCaseSuccessor());
738 EXPECT_EQ(CCI + 1, std::next(CCI));
739 EXPECT_EQ(CCI + 2, std::next(CCI, 2));
740 EXPECT_EQ(CCI + 3, std::next(CCI, 3));
741 EXPECT_EQ(CCE, CCI + 3);
742 EXPECT_EQ(0, CCI - CCI);
743 EXPECT_EQ(1, (CCI + 1) - CCI);
744 EXPECT_EQ(2, (CCI + 2) - CCI);
745 EXPECT_EQ(3, CCE - CCI);
746 EXPECT_EQ(3, std::distance(CCI, CCE));
747
748 // Make sure that the const iterator is compatible with a const auto ref.
749 const auto &Handle = *CCI;
750 EXPECT_EQ(1, Handle.getCaseValue()->getSExtValue());
751 EXPECT_EQ(BB1.get(), Handle.getCaseSuccessor());
752 }
753
TEST(InstructionsTest,CommuteShuffleMask)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})));
758 }
759
TEST(InstructionsTest,ShuffleMaskQueries)760 TEST(InstructionsTest, ShuffleMaskQueries) {
761 // Create the elements for various constant vectors.
762 LLVMContext Ctx;
763 Type *Int32Ty = Type::getInt32Ty(Ctx);
764 Constant *CU = UndefValue::get(Int32Ty);
765 Constant *C0 = ConstantInt::get(Int32Ty, 0);
766 Constant *C1 = ConstantInt::get(Int32Ty, 1);
767 Constant *C2 = ConstantInt::get(Int32Ty, 2);
768 Constant *C3 = ConstantInt::get(Int32Ty, 3);
769 Constant *C4 = ConstantInt::get(Int32Ty, 4);
770 Constant *C5 = ConstantInt::get(Int32Ty, 5);
771 Constant *C6 = ConstantInt::get(Int32Ty, 6);
772 Constant *C7 = ConstantInt::get(Int32Ty, 7);
773
774 Constant *Identity = ConstantVector::get({C0, CU, C2, C3, C4});
775 EXPECT_TRUE(ShuffleVectorInst::isIdentityMask(Identity));
776 EXPECT_FALSE(ShuffleVectorInst::isSelectMask(Identity)); // identity is distinguished from select
777 EXPECT_FALSE(ShuffleVectorInst::isReverseMask(Identity));
778 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(Identity)); // identity is always single source
779 EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(Identity));
780 EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(Identity));
781
782 Constant *Select = ConstantVector::get({CU, C1, C5});
783 EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(Select));
784 EXPECT_TRUE(ShuffleVectorInst::isSelectMask(Select));
785 EXPECT_FALSE(ShuffleVectorInst::isReverseMask(Select));
786 EXPECT_FALSE(ShuffleVectorInst::isSingleSourceMask(Select));
787 EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(Select));
788 EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(Select));
789
790 Constant *Reverse = ConstantVector::get({C3, C2, C1, CU});
791 EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(Reverse));
792 EXPECT_FALSE(ShuffleVectorInst::isSelectMask(Reverse));
793 EXPECT_TRUE(ShuffleVectorInst::isReverseMask(Reverse));
794 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(Reverse)); // reverse is always single source
795 EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(Reverse));
796 EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(Reverse));
797
798 Constant *SingleSource = ConstantVector::get({C2, C2, C0, CU});
799 EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(SingleSource));
800 EXPECT_FALSE(ShuffleVectorInst::isSelectMask(SingleSource));
801 EXPECT_FALSE(ShuffleVectorInst::isReverseMask(SingleSource));
802 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(SingleSource));
803 EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(SingleSource));
804 EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(SingleSource));
805
806 Constant *ZeroEltSplat = ConstantVector::get({C0, C0, CU, C0});
807 EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(ZeroEltSplat));
808 EXPECT_FALSE(ShuffleVectorInst::isSelectMask(ZeroEltSplat));
809 EXPECT_FALSE(ShuffleVectorInst::isReverseMask(ZeroEltSplat));
810 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(ZeroEltSplat)); // 0-splat is always single source
811 EXPECT_TRUE(ShuffleVectorInst::isZeroEltSplatMask(ZeroEltSplat));
812 EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(ZeroEltSplat));
813
814 Constant *Transpose = ConstantVector::get({C0, C4, C2, C6});
815 EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(Transpose));
816 EXPECT_FALSE(ShuffleVectorInst::isSelectMask(Transpose));
817 EXPECT_FALSE(ShuffleVectorInst::isReverseMask(Transpose));
818 EXPECT_FALSE(ShuffleVectorInst::isSingleSourceMask(Transpose));
819 EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(Transpose));
820 EXPECT_TRUE(ShuffleVectorInst::isTransposeMask(Transpose));
821
822 // More tests to make sure the logic is/stays correct...
823 EXPECT_TRUE(ShuffleVectorInst::isIdentityMask(ConstantVector::get({CU, C1, CU, C3})));
824 EXPECT_TRUE(ShuffleVectorInst::isIdentityMask(ConstantVector::get({C4, CU, C6, CU})));
825
826 EXPECT_TRUE(ShuffleVectorInst::isSelectMask(ConstantVector::get({C4, C1, C6, CU})));
827 EXPECT_TRUE(ShuffleVectorInst::isSelectMask(ConstantVector::get({CU, C1, C6, C3})));
828
829 EXPECT_TRUE(ShuffleVectorInst::isReverseMask(ConstantVector::get({C7, C6, CU, C4})));
830 EXPECT_TRUE(ShuffleVectorInst::isReverseMask(ConstantVector::get({C3, CU, C1, CU})));
831
832 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(ConstantVector::get({C7, C5, CU, C7})));
833 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(ConstantVector::get({C3, C0, CU, C3})));
834
835 EXPECT_TRUE(ShuffleVectorInst::isZeroEltSplatMask(ConstantVector::get({C4, CU, CU, C4})));
836 EXPECT_TRUE(ShuffleVectorInst::isZeroEltSplatMask(ConstantVector::get({CU, C0, CU, C0})));
837
838 EXPECT_TRUE(ShuffleVectorInst::isTransposeMask(ConstantVector::get({C1, C5, C3, C7})));
839 EXPECT_TRUE(ShuffleVectorInst::isTransposeMask(ConstantVector::get({C1, C3})));
840 }
841
TEST(InstructionsTest,SkipDebug)842 TEST(InstructionsTest, SkipDebug) {
843 LLVMContext C;
844 std::unique_ptr<Module> M = parseIR(C,
845 R"(
846 declare void @llvm.dbg.value(metadata, metadata, metadata)
847
848 define void @f() {
849 entry:
850 call void @llvm.dbg.value(metadata i32 0, metadata !11, metadata !DIExpression()), !dbg !13
851 ret void
852 }
853
854 !llvm.dbg.cu = !{!0}
855 !llvm.module.flags = !{!3, !4}
856 !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 6.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
857 !1 = !DIFile(filename: "t2.c", directory: "foo")
858 !2 = !{}
859 !3 = !{i32 2, !"Dwarf Version", i32 4}
860 !4 = !{i32 2, !"Debug Info Version", i32 3}
861 !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)
862 !9 = !DISubroutineType(types: !10)
863 !10 = !{null}
864 !11 = !DILocalVariable(name: "x", scope: !8, file: !1, line: 2, type: !12)
865 !12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
866 !13 = !DILocation(line: 2, column: 7, scope: !8)
867 )");
868 ASSERT_TRUE(M);
869 Function *F = cast<Function>(M->getNamedValue("f"));
870 BasicBlock &BB = F->front();
871
872 // The first non-debug instruction is the terminator.
873 auto *Term = BB.getTerminator();
874 EXPECT_EQ(Term, BB.begin()->getNextNonDebugInstruction());
875 EXPECT_EQ(Term->getIterator(), skipDebugIntrinsics(BB.begin()));
876
877 // After the terminator, there are no non-debug instructions.
878 EXPECT_EQ(nullptr, Term->getNextNonDebugInstruction());
879 }
880
881 } // end anonymous namespace
882 } // end namespace llvm
883