1 //===- llvm/unittest/IR/BasicBlockTest.cpp - BasicBlock unit tests --------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/IR/BasicBlock.h"
10 #include "llvm/ADT/STLExtras.h"
11 #include "llvm/AsmParser/Parser.h"
12 #include "llvm/IR/Function.h"
13 #include "llvm/IR/IRBuilder.h"
14 #include "llvm/IR/Instructions.h"
15 #include "llvm/IR/LLVMContext.h"
16 #include "llvm/IR/Module.h"
17 #include "llvm/IR/NoFolder.h"
18 #include "llvm/IR/Verifier.h"
19 #include "llvm/Support/SourceMgr.h"
20 #include "gmock/gmock-matchers.h"
21 #include "gtest/gtest.h"
27 TEST(BasicBlockTest
, PhiRange
) {
30 // Create the main block.
31 std::unique_ptr
<BasicBlock
> BB(BasicBlock::Create(Context
));
33 // Create some predecessors of it.
34 std::unique_ptr
<BasicBlock
> BB1(BasicBlock::Create(Context
));
35 BranchInst::Create(BB
.get(), BB1
.get());
36 std::unique_ptr
<BasicBlock
> BB2(BasicBlock::Create(Context
));
37 BranchInst::Create(BB
.get(), BB2
.get());
39 // Make sure this doesn't crash if there are no phis.
41 for (auto &PN
: BB
->phis()) {
45 ASSERT_EQ(PhiCount
, 0) << "empty block should have no phis";
48 auto *BI
= BranchInst::Create(BB
.get(), BB
.get());
50 // Now insert some PHI nodes.
51 auto *Int32Ty
= Type::getInt32Ty(Context
);
52 auto *P1
= PHINode::Create(Int32Ty
, /*NumReservedValues*/ 3, "phi.1", BI
);
53 auto *P2
= PHINode::Create(Int32Ty
, /*NumReservedValues*/ 3, "phi.2", BI
);
54 auto *P3
= PHINode::Create(Int32Ty
, /*NumReservedValues*/ 3, "phi.3", BI
);
56 // Some non-PHI nodes.
57 auto *Sum
= BinaryOperator::CreateAdd(P1
, P2
, "sum", BI
);
59 // Now wire up the incoming values that are interesting.
60 P1
->addIncoming(P2
, BB
.get());
61 P2
->addIncoming(P1
, BB
.get());
62 P3
->addIncoming(Sum
, BB
.get());
64 // Finally, let's iterate them, which is the thing we're trying to test.
65 // We'll use this to wire up the rest of the incoming values.
66 for (auto &PN
: BB
->phis()) {
67 PN
.addIncoming(UndefValue::get(Int32Ty
), BB1
.get());
68 PN
.addIncoming(UndefValue::get(Int32Ty
), BB2
.get());
71 // Test that we can use const iterators and generally that the iterators
72 // behave like iterators.
73 BasicBlock::const_phi_iterator CI
;
74 CI
= BB
->phis().begin();
75 EXPECT_NE(CI
, BB
->phis().end());
77 // Test that filtering iterators work with basic blocks.
78 auto isPhi
= [](Instruction
&I
) { return isa
<PHINode
>(&I
); };
79 auto Phis
= make_filter_range(*BB
, isPhi
);
80 auto ReversedPhis
= reverse(make_filter_range(*BB
, isPhi
));
81 EXPECT_EQ(std::distance(Phis
.begin(), Phis
.end()), 3);
82 EXPECT_EQ(&*Phis
.begin(), P1
);
83 EXPECT_EQ(std::distance(ReversedPhis
.begin(), ReversedPhis
.end()), 3);
84 EXPECT_EQ(&*ReversedPhis
.begin(), P3
);
86 // And iterate a const range.
87 for (const auto &PN
: const_cast<const BasicBlock
*>(BB
.get())->phis()) {
88 EXPECT_EQ(BB
.get(), PN
.getIncomingBlock(0));
89 EXPECT_EQ(BB1
.get(), PN
.getIncomingBlock(1));
90 EXPECT_EQ(BB2
.get(), PN
.getIncomingBlock(2));
94 #define CHECK_ITERATORS(Range1, Range2) \
95 EXPECT_EQ(std::distance(Range1.begin(), Range1.end()), \
96 std::distance(Range2.begin(), Range2.end())); \
97 for (auto Pair : zip(Range1, Range2)) \
98 EXPECT_EQ(&std::get<0>(Pair), std::get<1>(Pair));
100 TEST(BasicBlockTest
, TestInstructionsWithoutDebug
) {
103 Module
*M
= new Module("MyModule", Ctx
);
104 Type
*ArgTy1
[] = {PointerType::getUnqual(Ctx
)};
105 FunctionType
*FT
= FunctionType::get(Type::getVoidTy(Ctx
), ArgTy1
, false);
106 Argument
*V
= new Argument(Type::getInt32Ty(Ctx
));
107 Function
*F
= Function::Create(FT
, Function::ExternalLinkage
, "", M
);
109 Function
*DbgDeclare
= Intrinsic::getDeclaration(M
, Intrinsic::dbg_declare
);
110 Function
*DbgValue
= Intrinsic::getDeclaration(M
, Intrinsic::dbg_value
);
111 Value
*DIV
= MetadataAsValue::get(Ctx
, (Metadata
*)nullptr);
112 SmallVector
<Value
*, 3> Args
= {DIV
, DIV
, DIV
};
114 BasicBlock
*BB1
= BasicBlock::Create(Ctx
, "", F
);
115 const BasicBlock
*BBConst
= BB1
;
116 IRBuilder
<> Builder1(BB1
);
118 AllocaInst
*Var
= Builder1
.CreateAlloca(Builder1
.getInt8Ty());
119 Builder1
.CreateCall(DbgValue
, Args
);
120 Instruction
*AddInst
= cast
<Instruction
>(Builder1
.CreateAdd(V
, V
));
121 Instruction
*MulInst
= cast
<Instruction
>(Builder1
.CreateMul(AddInst
, V
));
122 Builder1
.CreateCall(DbgDeclare
, Args
);
123 Instruction
*SubInst
= cast
<Instruction
>(Builder1
.CreateSub(MulInst
, V
));
125 SmallVector
<Instruction
*, 4> Exp
= {Var
, AddInst
, MulInst
, SubInst
};
126 CHECK_ITERATORS(BB1
->instructionsWithoutDebug(), Exp
);
127 CHECK_ITERATORS(BBConst
->instructionsWithoutDebug(), Exp
);
129 EXPECT_EQ(static_cast<size_t>(BB1
->sizeWithoutDebug()), Exp
.size());
130 EXPECT_EQ(static_cast<size_t>(BBConst
->sizeWithoutDebug()), Exp
.size());
136 TEST(BasicBlockTest
, ComesBefore
) {
137 const char *ModuleString
= R
"(define i32 @f(i32 %x) {
138 %add = add i32 %x, 42
143 auto M
= parseAssemblyString(ModuleString
, Err
, Ctx
);
144 ASSERT_TRUE(M
.get());
146 Function
*F
= M
->getFunction("f");
147 BasicBlock
&BB
= F
->front();
148 BasicBlock::iterator I
= BB
.begin();
149 Instruction
*Add
= &*I
++;
150 Instruction
*Ret
= &*I
++;
152 // Intentionally duplicated to verify cached and uncached are the same.
153 EXPECT_FALSE(BB
.isInstrOrderValid());
154 EXPECT_FALSE(Add
->comesBefore(Add
));
155 EXPECT_TRUE(BB
.isInstrOrderValid());
156 EXPECT_FALSE(Add
->comesBefore(Add
));
157 BB
.invalidateOrders();
158 EXPECT_FALSE(BB
.isInstrOrderValid());
159 EXPECT_TRUE(Add
->comesBefore(Ret
));
160 EXPECT_TRUE(BB
.isInstrOrderValid());
161 EXPECT_TRUE(Add
->comesBefore(Ret
));
162 BB
.invalidateOrders();
163 EXPECT_FALSE(Ret
->comesBefore(Add
));
164 EXPECT_FALSE(Ret
->comesBefore(Add
));
165 BB
.invalidateOrders();
166 EXPECT_FALSE(Ret
->comesBefore(Ret
));
167 EXPECT_FALSE(Ret
->comesBefore(Ret
));
170 class InstrOrderInvalidationTest
: public ::testing::Test
{
172 void SetUp() override
{
173 M
.reset(new Module("MyModule", Ctx
));
174 Nop
= Intrinsic::getDeclaration(M
.get(), Intrinsic::donothing
);
175 FunctionType
*FT
= FunctionType::get(Type::getVoidTy(Ctx
), {}, false);
176 Function
*F
= Function::Create(FT
, Function::ExternalLinkage
, "foo", *M
);
177 BB
= BasicBlock::Create(Ctx
, "entry", F
);
179 IRBuilder
<> Builder(BB
);
180 I1
= Builder
.CreateCall(Nop
);
181 I2
= Builder
.CreateCall(Nop
);
182 I3
= Builder
.CreateCall(Nop
);
183 Ret
= Builder
.CreateRetVoid();
187 std::unique_ptr
<Module
> M
;
188 Function
*Nop
= nullptr;
189 BasicBlock
*BB
= nullptr;
190 Instruction
*I1
= nullptr;
191 Instruction
*I2
= nullptr;
192 Instruction
*I3
= nullptr;
193 Instruction
*Ret
= nullptr;
196 TEST_F(InstrOrderInvalidationTest
, InsertInvalidation
) {
197 EXPECT_FALSE(BB
->isInstrOrderValid());
198 EXPECT_TRUE(I1
->comesBefore(I2
));
199 EXPECT_TRUE(BB
->isInstrOrderValid());
200 EXPECT_TRUE(I2
->comesBefore(I3
));
201 EXPECT_TRUE(I3
->comesBefore(Ret
));
202 EXPECT_TRUE(BB
->isInstrOrderValid());
204 // Invalidate orders.
205 IRBuilder
<> Builder(BB
, I2
->getIterator());
206 Instruction
*I1a
= Builder
.CreateCall(Nop
);
207 EXPECT_FALSE(BB
->isInstrOrderValid());
208 EXPECT_TRUE(I1
->comesBefore(I1a
));
209 EXPECT_TRUE(BB
->isInstrOrderValid());
210 EXPECT_TRUE(I1a
->comesBefore(I2
));
211 EXPECT_TRUE(I2
->comesBefore(I3
));
212 EXPECT_TRUE(I3
->comesBefore(Ret
));
213 EXPECT_TRUE(BB
->isInstrOrderValid());
216 TEST_F(InstrOrderInvalidationTest
, SpliceInvalidation
) {
217 EXPECT_TRUE(I1
->comesBefore(I2
));
218 EXPECT_TRUE(I2
->comesBefore(I3
));
219 EXPECT_TRUE(I3
->comesBefore(Ret
));
220 EXPECT_TRUE(BB
->isInstrOrderValid());
222 // Use Instruction::moveBefore, which uses splice.
224 EXPECT_FALSE(BB
->isInstrOrderValid());
226 EXPECT_TRUE(I2
->comesBefore(I1
));
227 EXPECT_TRUE(I1
->comesBefore(I3
));
228 EXPECT_TRUE(I3
->comesBefore(Ret
));
229 EXPECT_TRUE(BB
->isInstrOrderValid());
232 TEST_F(InstrOrderInvalidationTest
, RemoveNoInvalidation
) {
233 // Cache the instruction order.
234 EXPECT_FALSE(BB
->isInstrOrderValid());
235 EXPECT_TRUE(I1
->comesBefore(I2
));
236 EXPECT_TRUE(BB
->isInstrOrderValid());
238 // Removing does not invalidate instruction order.
239 I2
->removeFromParent();
242 EXPECT_TRUE(BB
->isInstrOrderValid());
243 EXPECT_TRUE(I1
->comesBefore(I3
));
244 EXPECT_EQ(std::next(I1
->getIterator()), I3
->getIterator());
247 TEST_F(InstrOrderInvalidationTest
, EraseNoInvalidation
) {
248 // Cache the instruction order.
249 EXPECT_FALSE(BB
->isInstrOrderValid());
250 EXPECT_TRUE(I1
->comesBefore(I2
));
251 EXPECT_TRUE(BB
->isInstrOrderValid());
253 // Removing does not invalidate instruction order.
254 I2
->eraseFromParent();
256 EXPECT_TRUE(BB
->isInstrOrderValid());
257 EXPECT_TRUE(I1
->comesBefore(I3
));
258 EXPECT_EQ(std::next(I1
->getIterator()), I3
->getIterator());
261 static std::unique_ptr
<Module
> parseIR(LLVMContext
&C
, const char *IR
) {
263 std::unique_ptr
<Module
> Mod
= parseAssemblyString(IR
, Err
, C
);
265 Err
.print(__FILE__
, errs());
269 TEST(BasicBlockTest
, SpliceFromBB
) {
271 std::unique_ptr
<Module
> M
= parseIR(Ctx
, R
"(
272 define void @f(i32 %a) {
274 %fromInstr1 = add i32 %a, %a
275 %fromInstr2 = sub i32 %a, %a
279 %toInstr1 = mul i32 %a, %a
280 %toInstr2 = sdiv i32 %a, %a
284 Function
*F
= &*M
->begin();
285 auto BBIt
= F
->begin();
286 BasicBlock
*FromBB
= &*BBIt
++;
287 BasicBlock
*ToBB
= &*BBIt
++;
289 auto FromBBIt
= FromBB
->begin();
290 Instruction
*FromI1
= &*FromBBIt
++;
291 Instruction
*FromI2
= &*FromBBIt
++;
292 Instruction
*FromBr
= &*FromBBIt
++;
294 auto ToBBIt
= ToBB
->begin();
295 Instruction
*ToI1
= &*ToBBIt
++;
296 Instruction
*ToI2
= &*ToBBIt
++;
297 Instruction
*ToRet
= &*ToBBIt
++;
298 ToBB
->splice(ToI1
->getIterator(), FromBB
);
300 EXPECT_TRUE(FromBB
->empty());
302 auto It
= ToBB
->begin();
303 EXPECT_EQ(&*It
++, FromI1
);
304 EXPECT_EQ(&*It
++, FromI2
);
305 EXPECT_EQ(&*It
++, FromBr
);
306 EXPECT_EQ(&*It
++, ToI1
);
307 EXPECT_EQ(&*It
++, ToI2
);
308 EXPECT_EQ(&*It
++, ToRet
);
311 TEST(BasicBlockTest
, SpliceOneInstr
) {
313 std::unique_ptr
<Module
> M
= parseIR(Ctx
, R
"(
314 define void @f(i32 %a) {
316 %fromInstr1 = add i32 %a, %a
317 %fromInstr2 = sub i32 %a, %a
321 %toInstr1 = mul i32 %a, %a
322 %toInstr2 = sdiv i32 %a, %a
326 Function
*F
= &*M
->begin();
327 auto BBIt
= F
->begin();
328 BasicBlock
*FromBB
= &*BBIt
++;
329 BasicBlock
*ToBB
= &*BBIt
++;
331 auto FromBBIt
= FromBB
->begin();
332 Instruction
*FromI1
= &*FromBBIt
++;
333 Instruction
*FromI2
= &*FromBBIt
++;
334 Instruction
*FromBr
= &*FromBBIt
++;
336 auto ToBBIt
= ToBB
->begin();
337 Instruction
*ToI1
= &*ToBBIt
++;
338 Instruction
*ToI2
= &*ToBBIt
++;
339 Instruction
*ToRet
= &*ToBBIt
++;
340 ToBB
->splice(ToI1
->getIterator(), FromBB
, FromI2
->getIterator());
342 EXPECT_EQ(FromBB
->size(), 2u);
343 EXPECT_EQ(ToBB
->size(), 4u);
345 auto It
= FromBB
->begin();
346 EXPECT_EQ(&*It
++, FromI1
);
347 EXPECT_EQ(&*It
++, FromBr
);
350 EXPECT_EQ(&*It
++, FromI2
);
351 EXPECT_EQ(&*It
++, ToI1
);
352 EXPECT_EQ(&*It
++, ToI2
);
353 EXPECT_EQ(&*It
++, ToRet
);
356 TEST(BasicBlockTest
, SpliceOneInstrWhenFromIsSameAsTo
) {
358 std::unique_ptr
<Module
> M
= parseIR(Ctx
, R
"(
359 define void @f(i32 %a) {
361 %instr1 = add i32 %a, %a
362 %instr2 = sub i32 %a, %a
366 Function
*F
= &*M
->begin();
367 auto BBIt
= F
->begin();
368 BasicBlock
*BB
= &*BBIt
++;
370 auto It
= BB
->begin();
371 Instruction
*Instr1
= &*It
++;
372 Instruction
*Instr2
= &*It
++;
373 Instruction
*Ret
= &*It
++;
375 // According to ilist's splice() a single-element splice where dst == src
377 BB
->splice(Instr1
->getIterator(), BB
, Instr1
->getIterator());
380 EXPECT_EQ(&*It
++, Instr1
);
381 EXPECT_EQ(&*It
++, Instr2
);
382 EXPECT_EQ(&*It
++, Ret
);
383 EXPECT_EQ(BB
->size(), 3u);
386 TEST(BasicBlockTest
, SpliceLastInstr
) {
388 std::unique_ptr
<Module
> M
= parseIR(Ctx
, R
"(
389 define void @f(i32 %a) {
391 %fromInstr1 = add i32 %a, %a
392 %fromInstr2 = sub i32 %a, %a
396 %toInstr1 = mul i32 %a, %a
397 %toInstr2 = sdiv i32 %a, %a
401 Function
*F
= &*M
->begin();
402 auto BBIt
= F
->begin();
403 BasicBlock
*FromBB
= &*BBIt
++;
404 BasicBlock
*ToBB
= &*BBIt
++;
406 auto FromBBIt
= FromBB
->begin();
407 Instruction
*FromI1
= &*FromBBIt
++;
408 Instruction
*FromI2
= &*FromBBIt
++;
409 Instruction
*FromBr
= &*FromBBIt
++;
411 auto ToBBIt
= ToBB
->begin();
412 Instruction
*ToI1
= &*ToBBIt
++;
413 Instruction
*ToI2
= &*ToBBIt
++;
414 Instruction
*ToRet
= &*ToBBIt
++;
415 ToBB
->splice(ToI1
->getIterator(), FromBB
, FromI2
->getIterator(),
416 FromBr
->getIterator());
418 EXPECT_EQ(FromBB
->size(), 2u);
419 auto It
= FromBB
->begin();
420 EXPECT_EQ(&*It
++, FromI1
);
421 EXPECT_EQ(&*It
++, FromBr
);
423 EXPECT_EQ(ToBB
->size(), 4u);
425 EXPECT_EQ(&*It
++, FromI2
);
426 EXPECT_EQ(&*It
++, ToI1
);
427 EXPECT_EQ(&*It
++, ToI2
);
428 EXPECT_EQ(&*It
++, ToRet
);
431 TEST(BasicBlockTest
, SpliceInstrRange
) {
433 std::unique_ptr
<Module
> M
= parseIR(Ctx
, R
"(
434 define void @f(i32 %a) {
436 %fromInstr1 = add i32 %a, %a
437 %fromInstr2 = sub i32 %a, %a
441 %toInstr1 = mul i32 %a, %a
442 %toInstr2 = sdiv i32 %a, %a
446 Function
*F
= &*M
->begin();
447 auto BBIt
= F
->begin();
448 BasicBlock
*FromBB
= &*BBIt
++;
449 BasicBlock
*ToBB
= &*BBIt
++;
451 auto FromBBIt
= FromBB
->begin();
452 Instruction
*FromI1
= &*FromBBIt
++;
453 Instruction
*FromI2
= &*FromBBIt
++;
454 Instruction
*FromBr
= &*FromBBIt
++;
456 auto ToBBIt
= ToBB
->begin();
457 Instruction
*ToI1
= &*ToBBIt
++;
458 Instruction
*ToI2
= &*ToBBIt
++;
459 Instruction
*ToRet
= &*ToBBIt
++;
460 ToBB
->splice(ToI2
->getIterator(), FromBB
, FromBB
->begin(), FromBB
->end());
462 EXPECT_EQ(FromBB
->size(), 0u);
464 EXPECT_EQ(ToBB
->size(), 6u);
465 auto It
= ToBB
->begin();
466 EXPECT_EQ(&*It
++, ToI1
);
467 EXPECT_EQ(&*It
++, FromI1
);
468 EXPECT_EQ(&*It
++, FromI2
);
469 EXPECT_EQ(&*It
++, FromBr
);
470 EXPECT_EQ(&*It
++, ToI2
);
471 EXPECT_EQ(&*It
++, ToRet
);
474 #ifdef EXPENSIVE_CHECKS
475 TEST(BasicBlockTest
, SpliceEndBeforeBegin
) {
477 std::unique_ptr
<Module
> M
= parseIR(Ctx
, R
"(
478 define void @f(i32 %a) {
480 %fromInstr1 = add i32 %a, %a
481 %fromInstr2 = sub i32 %a, %a
485 %toInstr1 = mul i32 %a, %a
486 %toInstr2 = sdiv i32 %a, %a
490 Function
*F
= &*M
->begin();
491 auto BBIt
= F
->begin();
492 BasicBlock
*FromBB
= &*BBIt
++;
493 BasicBlock
*ToBB
= &*BBIt
++;
495 auto FromBBIt
= FromBB
->begin();
496 Instruction
*FromI1
= &*FromBBIt
++;
497 Instruction
*FromI2
= &*FromBBIt
++;
499 auto ToBBIt
= ToBB
->begin();
500 Instruction
*ToI2
= &*ToBBIt
++;
502 EXPECT_DEATH(ToBB
->splice(ToI2
->getIterator(), FromBB
, FromI2
->getIterator(),
503 FromI1
->getIterator()),
504 "FromBeginIt not before FromEndIt!");
506 #endif //EXPENSIVE_CHECKS
508 TEST(BasicBlockTest
, EraseRange
) {
510 std::unique_ptr
<Module
> M
= parseIR(Ctx
, R
"(
511 define void @f(i32 %a) {
513 %instr1 = add i32 %a, %a
514 %instr2 = sub i32 %a, %a
518 Function
*F
= &*M
->begin();
520 auto BB0It
= F
->begin();
521 BasicBlock
*BB0
= &*BB0It
;
523 auto It
= BB0
->begin();
524 Instruction
*Instr1
= &*It
++;
525 Instruction
*Instr2
= &*It
++;
527 EXPECT_EQ(BB0
->size(), 3u);
529 // Erase no instruction
530 BB0
->erase(Instr1
->getIterator(), Instr1
->getIterator());
531 EXPECT_EQ(BB0
->size(), 3u);
534 BB0
->erase(Instr1
->getIterator(), Instr2
->getIterator());
535 EXPECT_EQ(BB0
->size(), 2u);
536 EXPECT_EQ(&*BB0
->begin(), Instr2
);
538 // Erase all instructions
539 BB0
->erase(BB0
->begin(), BB0
->end());
540 EXPECT_TRUE(BB0
->empty());
543 TEST(BasicBlockTest
, DiscardValueNames
) {
544 const char *ModuleString
= "declare void @f(i32 %dangling)";
548 auto M
= parseAssemblyString(ModuleString
, Err
, Ctx
);
549 ASSERT_TRUE(M
.get());
550 EXPECT_FALSE(Ctx
.shouldDiscardValueNames());
553 auto M
= parseAssemblyString(ModuleString
, Err
, Ctx
);
554 ASSERT_TRUE(M
.get());
555 Ctx
.setDiscardValueNames(true);
559 TEST(BasicBlockTest
, DiscardValueNames2
) {
562 Module
M("Mod", Ctx
);
563 auto FTy
= FunctionType::get(Type::getVoidTy(M
.getContext()),
564 {Type::getInt32Ty(Ctx
)}, /*isVarArg=*/false);
566 Function
*F
= Function::Create(FTy
, Function::ExternalLinkage
, "f", &M
);
567 F
->getArg(0)->setName("dangling");
568 F
->removeFromParent();
569 EXPECT_FALSE(Ctx
.shouldDiscardValueNames());
573 Function
*F
= Function::Create(FTy
, Function::ExternalLinkage
, "f", &M
);
574 F
->getArg(0)->setName("dangling");
575 F
->removeFromParent();
576 Ctx
.setDiscardValueNames(true);
581 } // End anonymous namespace.
582 } // End llvm namespace.