1 //===- BasicBlockUtils.cpp - Unit tests for BasicBlockUtils ---------------===//
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/Transforms/Utils/BasicBlockUtils.h"
10 #include "llvm/Analysis/AssumptionCache.h"
11 #include "llvm/Analysis/BasicAliasAnalysis.h"
12 #include "llvm/Analysis/BlockFrequencyInfo.h"
13 #include "llvm/Analysis/BranchProbabilityInfo.h"
14 #include "llvm/Analysis/CFG.h"
15 #include "llvm/Analysis/DomTreeUpdater.h"
16 #include "llvm/Analysis/LoopInfo.h"
17 #include "llvm/Analysis/MemorySSA.h"
18 #include "llvm/Analysis/MemorySSAUpdater.h"
19 #include "llvm/Analysis/PostDominators.h"
20 #include "llvm/Analysis/TargetLibraryInfo.h"
21 #include "llvm/AsmParser/Parser.h"
22 #include "llvm/IR/BasicBlock.h"
23 #include "llvm/IR/Dominators.h"
24 #include "llvm/IR/LLVMContext.h"
25 #include "llvm/Support/SourceMgr.h"
26 #include "gtest/gtest.h"
30 static std::unique_ptr
<Module
> parseIR(LLVMContext
&C
, const char *IR
) {
32 std::unique_ptr
<Module
> Mod
= parseAssemblyString(IR
, Err
, C
);
34 Err
.print("BasicBlockUtilsTests", errs());
38 static BasicBlock
*getBasicBlockByName(Function
&F
, StringRef Name
) {
39 for (BasicBlock
&BB
: F
)
40 if (BB
.getName() == Name
)
42 llvm_unreachable("Expected to find basic block!");
45 TEST(BasicBlockUtils
, EliminateUnreachableBlocks
) {
47 std::unique_ptr
<Module
> M
= parseIR(C
, R
"IR(
48 define i32 @has_unreachable(i1 %cond) {
50 br i1 %cond, label %bb0, label %bb1
54 %phi = phi i32 [ 0, %entry ], [ 1, %bb0 ]
60 Function
*F
= M
->getFunction("has_unreachable");
62 DomTreeUpdater
DTU(DT
, DomTreeUpdater::UpdateStrategy::Eager
);
64 EXPECT_EQ(F
->size(), (size_t)4);
65 bool Result
= EliminateUnreachableBlocks(*F
, &DTU
);
67 EXPECT_EQ(F
->size(), (size_t)3);
68 EXPECT_TRUE(DT
.verify());
71 TEST(BasicBlockUtils
, SplitEdge_ex1
) {
73 std::unique_ptr
<Module
> M
= parseIR(C
, R
"IR(
74 define void @foo(i1 %cond0) {
76 br i1 %cond0, label %bb0, label %bb1
86 Function
*F
= M
->getFunction("foo");
89 BasicBlock
*DestBlock
;
92 SrcBlock
= getBasicBlockByName(*F
, "entry");
93 DestBlock
= getBasicBlockByName(*F
, "bb0");
94 NewBB
= SplitEdge(SrcBlock
, DestBlock
, &DT
, nullptr, nullptr);
96 EXPECT_TRUE(DT
.verify());
97 EXPECT_EQ(NewBB
->getSinglePredecessor(), SrcBlock
);
98 EXPECT_EQ(NewBB
->getSingleSuccessor(), DestBlock
);
99 EXPECT_EQ(NewBB
->getParent(), F
);
102 for (BasicBlock
&BB
: *F
) {
103 if (BB
.getName() == NewBB
->getName()) {
110 TEST(BasicBlockUtils
, SplitEdge_ex2
) {
112 std::unique_ptr
<Module
> M
= parseIR(C
, R
"IR(
122 Function
*F
= M
->getFunction("foo");
123 DominatorTree
DT(*F
);
125 BasicBlock
*SrcBlock
;
126 BasicBlock
*DestBlock
;
129 SrcBlock
= getBasicBlockByName(*F
, "bb0");
130 DestBlock
= getBasicBlockByName(*F
, "bb2");
131 NewBB
= SplitEdge(SrcBlock
, DestBlock
, &DT
, nullptr, nullptr);
133 EXPECT_TRUE(DT
.verify());
134 EXPECT_EQ(NewBB
->getSinglePredecessor(), SrcBlock
);
135 EXPECT_EQ(NewBB
->getSingleSuccessor(), DestBlock
);
136 EXPECT_EQ(NewBB
->getParent(), F
);
139 for (BasicBlock
&BB
: *F
) {
140 if (BB
.getName() == NewBB
->getName()) {
147 TEST(BasicBlockUtils
, SplitEdge_ex3
) {
149 std::unique_ptr
<Module
> M
= parseIR(C
, R
"IR(
150 define i32 @foo(i32 %n) {
154 %sum.02 = phi i32 [ 0, %entry ], [ %sum.1, %bb3 ]
155 %0 = phi i32 [ 0, %entry ], [ %4, %bb3 ]
156 %1 = icmp slt i32 %0, %n
157 br i1 %1, label %bb0, label %bb1
159 %2 = add nsw i32 %sum.02, 2
162 %3 = add nsw i32 %sum.02, 1
165 %sum.1 = phi i32 [ %2, %bb0 ], [ %3, %bb1 ]
168 %4 = add nsw i32 %0, 1
169 %5 = icmp slt i32 %4, 100
170 br i1 %5, label %header, label %bb4
172 %sum.0.lcssa = phi i32 [ %sum.1, %bb3 ]
176 Function
*F
= M
->getFunction("foo");
177 DominatorTree
DT(*F
);
181 DataLayout
DL("e-i64:64-f80:128-n8:16:32:64-S128");
182 TargetLibraryInfoImpl TLII
;
183 TargetLibraryInfo
TLI(TLII
);
184 AssumptionCache
AC(*F
);
187 BasicAAResult
BAA(DL
, *F
, TLI
, AC
, &DT
);
190 MemorySSA
MSSA(*F
, &AA
, &DT
);
191 MemorySSAUpdater
Updater(&MSSA
);
193 BasicBlock
*SrcBlock
;
194 BasicBlock
*DestBlock
;
197 SrcBlock
= getBasicBlockByName(*F
, "header");
198 DestBlock
= getBasicBlockByName(*F
, "bb0");
199 NewBB
= SplitEdge(SrcBlock
, DestBlock
, &DT
, &LI
, &Updater
);
201 Updater
.getMemorySSA()->verifyMemorySSA();
202 EXPECT_TRUE(DT
.verify());
203 EXPECT_NE(LI
.getLoopFor(SrcBlock
), nullptr);
204 EXPECT_NE(LI
.getLoopFor(DestBlock
), nullptr);
205 EXPECT_NE(LI
.getLoopFor(NewBB
), nullptr);
206 EXPECT_EQ(NewBB
->getSinglePredecessor(), SrcBlock
);
207 EXPECT_EQ(NewBB
->getSingleSuccessor(), DestBlock
);
208 EXPECT_EQ(NewBB
->getParent(), F
);
211 for (BasicBlock
&BB
: *F
) {
212 if (BB
.getName() == NewBB
->getName()) {
219 TEST(BasicBlockUtils
, SplitEdge_ex4
) {
221 std::unique_ptr
<Module
> M
= parseIR(C
, R
"IR(
222 define void @bar(i32 %cond) personality i8 0 {
224 switch i32 %cond, label %exit [
225 i32 -1, label %continue
226 i32 0, label %continue
227 i32 1, label %continue_alt
228 i32 2, label %continue_alt
233 invoke void @sink() to label %normal unwind label %exception
235 invoke void @sink_alt() to label %normal unwind label %exception
237 %cleanup = landingpad i8 cleanup
238 br label %trivial-eh-handler
240 call void @sideeffect(i32 1)
243 call void @sideeffect(i32 0)
247 declare void @sideeffect(i32)
248 declare void @sink() cold
249 declare void @sink_alt() cold
251 Function
*F
= M
->getFunction("bar");
253 DominatorTree
DT(*F
);
257 TargetLibraryInfoImpl TLII
;
258 TargetLibraryInfo
TLI(TLII
);
262 MemorySSA
MSSA(*F
, &AA
, &DT
);
263 MemorySSAUpdater
MSSAU(&MSSA
);
265 BasicBlock
*SrcBlock
;
266 BasicBlock
*DestBlock
;
268 SrcBlock
= getBasicBlockByName(*F
, "continue");
269 DestBlock
= getBasicBlockByName(*F
, "exception");
271 unsigned SuccNum
= GetSuccessorNumber(SrcBlock
, DestBlock
);
272 Instruction
*LatchTerm
= SrcBlock
->getTerminator();
274 const CriticalEdgeSplittingOptions Options
=
275 CriticalEdgeSplittingOptions(&DT
, &LI
, &MSSAU
);
277 // Check that the following edge is both critical and the destination block is
278 // an exception block. These must be handled differently by SplitEdge
280 isCriticalEdge(LatchTerm
, SuccNum
, Options
.MergeIdenticalEdges
);
281 EXPECT_TRUE(CriticalEdge
);
283 bool Ehpad
= DestBlock
->isEHPad();
286 BasicBlock
*NewBB
= SplitEdge(SrcBlock
, DestBlock
, &DT
, &LI
, &MSSAU
, "");
288 MSSA
.verifyMemorySSA();
289 EXPECT_TRUE(DT
.verify());
290 EXPECT_NE(NewBB
, nullptr);
291 EXPECT_EQ(NewBB
->getSinglePredecessor(), SrcBlock
);
292 EXPECT_EQ(NewBB
, SrcBlock
->getTerminator()->getSuccessor(SuccNum
));
293 EXPECT_EQ(NewBB
->getParent(), F
);
296 for (BasicBlock
&BB
: *F
) {
297 if (BB
.getName() == NewBB
->getName()) {
305 TEST(BasicBlockUtils
, splitBasicBlockBefore_ex1
) {
307 std::unique_ptr
<Module
> M
= parseIR(C
, R
"IR(
315 %1 = phi i32 [ %0, %bb0 ]
321 Function
*F
= M
->getFunction("foo");
322 DominatorTree
DT(*F
);
324 BasicBlock
*DestBlock
;
327 DestBlock
= getBasicBlockByName(*F
, "bb2");
329 NewBB
= DestBlock
->splitBasicBlockBefore(DestBlock
->front().getIterator(),
332 PHINode
*PN
= dyn_cast
<PHINode
>(&(DestBlock
->front()));
333 EXPECT_EQ(PN
->getIncomingBlock(0), NewBB
);
334 EXPECT_EQ(NewBB
->getName(), "test");
335 EXPECT_EQ(NewBB
->getSingleSuccessor(), DestBlock
);
336 EXPECT_EQ(DestBlock
->getSinglePredecessor(), NewBB
);
340 TEST(BasicBlockUtils
, splitBasicBlockBefore_ex2
) {
342 std::unique_ptr
<Module
> M
= parseIR(C
, R
"IR(
350 %1 = phi i32 [ %0, %bb0 ], [ 1, %bb1 ]
356 Function
*F
= M
->getFunction("foo");
357 DominatorTree
DT(*F
);
359 BasicBlock
*DestBlock
= getBasicBlockByName(*F
, "bb2");
363 DestBlock
->splitBasicBlockBefore(DestBlock
->front().getIterator(),
366 "cannot split on multi incoming phis");
370 TEST(BasicBlockUtils
, NoUnreachableBlocksToEliminate
) {
372 std::unique_ptr
<Module
> M
= parseIR(C
, R
"IR(
373 define i32 @no_unreachable(i1 %cond) {
375 br i1 %cond, label %bb0, label %bb1
379 %phi = phi i32 [ 0, %entry ], [ 1, %bb0 ]
383 Function
*F
= M
->getFunction("no_unreachable");
384 DominatorTree
DT(*F
);
385 DomTreeUpdater
DTU(DT
, DomTreeUpdater::UpdateStrategy::Eager
);
387 EXPECT_EQ(F
->size(), (size_t)3);
388 bool Result
= EliminateUnreachableBlocks(*F
, &DTU
);
389 EXPECT_FALSE(Result
);
390 EXPECT_EQ(F
->size(), (size_t)3);
391 EXPECT_TRUE(DT
.verify());
394 TEST(BasicBlockUtils
, SplitBlockPredecessors
) {
396 std::unique_ptr
<Module
> M
= parseIR(C
, R
"IR(
397 define i32 @basic_func(i1 %cond) {
399 br i1 %cond, label %bb0, label %bb1
403 %phi = phi i32 [ 0, %entry ], [ 1, %bb0 ]
407 Function
*F
= M
->getFunction("basic_func");
408 DominatorTree
DT(*F
);
410 // Make sure the dominator tree is properly updated if calling this on the
412 SplitBlockPredecessors(&F
->getEntryBlock(), {}, "split.entry", &DT
);
413 EXPECT_TRUE(DT
.verify());
416 TEST(BasicBlockUtils
, SplitCriticalEdge
) {
418 std::unique_ptr
<Module
> M
= parseIR(C
, R
"IR(
419 define void @crit_edge(i1 %cond0, i1 %cond1) {
421 br i1 %cond0, label %bb0, label %bb1
430 Function
*F
= M
->getFunction("crit_edge");
431 DominatorTree
DT(*F
);
432 PostDominatorTree
PDT(*F
);
434 CriticalEdgeSplittingOptions
CESO(&DT
, nullptr, nullptr, &PDT
);
435 EXPECT_EQ(1u, SplitAllCriticalEdges(*F
, CESO
));
436 EXPECT_TRUE(DT
.verify());
437 EXPECT_TRUE(PDT
.verify());
440 TEST(BasicBlockUtils
, SplitIndirectBrCriticalEdgesIgnorePHIs
) {
442 std::unique_ptr
<Module
> M
= parseIR(C
, R
"IR(
443 define void @crit_edge(i8* %tgt, i1 %cond0, i1 %cond1) {
445 indirectbr i8* %tgt, [label %bb0, label %bb1, label %bb2]
447 br i1 %cond0, label %bb1, label %bb2
449 %p = phi i32 [0, %bb0], [0, %entry]
450 br i1 %cond1, label %bb3, label %bb4
459 Function
*F
= M
->getFunction("crit_edge");
460 DominatorTree
DT(*F
);
462 BranchProbabilityInfo
BPI(*F
, LI
);
463 BlockFrequencyInfo
BFI(*F
, BPI
, LI
);
465 ASSERT_TRUE(SplitIndirectBrCriticalEdges(*F
, /*IgnoreBlocksWithoutPHI=*/true,
468 // Check that successors of the split block get their probability correct.
469 BasicBlock
*BB1
= getBasicBlockByName(*F
, "bb1");
470 BasicBlock
*SplitBB
= BB1
->getTerminator()->getSuccessor(0);
471 ASSERT_EQ(2u, SplitBB
->getTerminator()->getNumSuccessors());
472 EXPECT_EQ(BranchProbability(1, 2), BPI
.getEdgeProbability(SplitBB
, 0u));
473 EXPECT_EQ(BranchProbability(1, 2), BPI
.getEdgeProbability(SplitBB
, 1u));
475 // bb2 has no PHI, so we shouldn't split bb0 -> bb2
476 BasicBlock
*BB0
= getBasicBlockByName(*F
, "bb0");
477 ASSERT_EQ(2u, BB0
->getTerminator()->getNumSuccessors());
478 EXPECT_EQ(BB0
->getTerminator()->getSuccessor(1),
479 getBasicBlockByName(*F
, "bb2"));
482 TEST(BasicBlockUtils
, SplitIndirectBrCriticalEdges
) {
484 std::unique_ptr
<Module
> M
= parseIR(C
, R
"IR(
485 define void @crit_edge(i8* %tgt, i1 %cond0, i1 %cond1) {
487 indirectbr i8* %tgt, [label %bb0, label %bb1, label %bb2]
489 br i1 %cond0, label %bb1, label %bb2
491 %p = phi i32 [0, %bb0], [0, %entry]
492 br i1 %cond1, label %bb3, label %bb4
501 Function
*F
= M
->getFunction("crit_edge");
502 DominatorTree
DT(*F
);
504 BranchProbabilityInfo
BPI(*F
, LI
);
505 BlockFrequencyInfo
BFI(*F
, BPI
, LI
);
507 ASSERT_TRUE(SplitIndirectBrCriticalEdges(*F
, /*IgnoreBlocksWithoutPHI=*/false,
510 // Check that successors of the split block get their probability correct.
511 BasicBlock
*BB1
= getBasicBlockByName(*F
, "bb1");
512 BasicBlock
*SplitBB
= BB1
->getTerminator()->getSuccessor(0);
513 ASSERT_EQ(2u, SplitBB
->getTerminator()->getNumSuccessors());
514 EXPECT_EQ(BranchProbability(1, 2), BPI
.getEdgeProbability(SplitBB
, 0u));
515 EXPECT_EQ(BranchProbability(1, 2), BPI
.getEdgeProbability(SplitBB
, 1u));
517 // Should split, resulting in:
518 // bb0 -> bb2.clone; bb2 -> split1; bb2.clone -> split,
519 BasicBlock
*BB0
= getBasicBlockByName(*F
, "bb0");
520 ASSERT_EQ(2u, BB0
->getTerminator()->getNumSuccessors());
521 BasicBlock
*BB2Clone
= BB0
->getTerminator()->getSuccessor(1);
522 BasicBlock
*BB2
= getBasicBlockByName(*F
, "bb2");
523 EXPECT_NE(BB2Clone
, BB2
);
524 ASSERT_EQ(1u, BB2
->getTerminator()->getNumSuccessors());
525 ASSERT_EQ(1u, BB2Clone
->getTerminator()->getNumSuccessors());
526 EXPECT_EQ(BB2
->getTerminator()->getSuccessor(0),
527 BB2Clone
->getTerminator()->getSuccessor(0));
530 TEST(BasicBlockUtils
, SetEdgeProbability
) {
532 std::unique_ptr
<Module
> M
= parseIR(C
, R
"IR(
533 define void @edge_probability(i32 %0) {
535 switch i32 %0, label %LD [
556 ], !prof !{!"branch_weights
", i32 1, i32 1, i32 1, i32 1, i32 1, i32 451, i32 1,
557 i32 12, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1,
558 i32 1, i32 1, i32 1, i32 1, i32 1}
603 Function
*F
= M
->getFunction("edge_probability");
604 DominatorTree
DT(*F
);
606 BranchProbabilityInfo
BPI(*F
, LI
);
608 // Check that the unreachable block has the minimal probability.
609 const BasicBlock
*EntryBB
= getBasicBlockByName(*F
, "entry");
610 const BasicBlock
*UnreachableBB
= getBasicBlockByName(*F
, "LD");
611 EXPECT_EQ(BranchProbability::getRaw(1),
612 BPI
.getEdgeProbability(EntryBB
, UnreachableBB
));