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 "llvm/Transforms/Utils/BreakCriticalEdges.h"
27 #include "gtest/gtest.h"
31 static std::unique_ptr
<Module
> parseIR(LLVMContext
&C
, const char *IR
) {
33 std::unique_ptr
<Module
> Mod
= parseAssemblyString(IR
, Err
, C
);
35 Err
.print("BasicBlockUtilsTests", errs());
39 static BasicBlock
*getBasicBlockByName(Function
&F
, StringRef Name
) {
40 for (BasicBlock
&BB
: F
)
41 if (BB
.getName() == Name
)
43 llvm_unreachable("Expected to find basic block!");
46 TEST(BasicBlockUtils
, EliminateUnreachableBlocks
) {
48 std::unique_ptr
<Module
> M
= parseIR(C
, R
"IR(
49 define i32 @has_unreachable(i1 %cond) {
51 br i1 %cond, label %bb0, label %bb1
55 %phi = phi i32 [ 0, %entry ], [ 1, %bb0 ]
61 Function
*F
= M
->getFunction("has_unreachable");
63 DomTreeUpdater
DTU(DT
, DomTreeUpdater::UpdateStrategy::Eager
);
65 EXPECT_EQ(F
->size(), (size_t)4);
66 bool Result
= EliminateUnreachableBlocks(*F
, &DTU
);
68 EXPECT_EQ(F
->size(), (size_t)3);
69 EXPECT_TRUE(DT
.verify());
72 TEST(BasicBlockUtils
, SplitEdge_ex1
) {
74 std::unique_ptr
<Module
> M
= parseIR(C
, R
"IR(
75 define void @foo(i1 %cond0) {
77 br i1 %cond0, label %bb0, label %bb1
87 Function
*F
= M
->getFunction("foo");
90 BasicBlock
*DestBlock
;
93 SrcBlock
= getBasicBlockByName(*F
, "entry");
94 DestBlock
= getBasicBlockByName(*F
, "bb0");
95 NewBB
= SplitEdge(SrcBlock
, DestBlock
, &DT
, nullptr, nullptr);
97 EXPECT_TRUE(DT
.verify());
98 EXPECT_EQ(NewBB
->getSinglePredecessor(), SrcBlock
);
99 EXPECT_EQ(NewBB
->getSingleSuccessor(), DestBlock
);
100 EXPECT_EQ(NewBB
->getParent(), F
);
103 for (BasicBlock
&BB
: *F
) {
104 if (BB
.getName() == NewBB
->getName()) {
111 TEST(BasicBlockUtils
, SplitEdge_ex2
) {
113 std::unique_ptr
<Module
> M
= parseIR(C
, R
"IR(
123 Function
*F
= M
->getFunction("foo");
124 DominatorTree
DT(*F
);
126 BasicBlock
*SrcBlock
;
127 BasicBlock
*DestBlock
;
130 SrcBlock
= getBasicBlockByName(*F
, "bb0");
131 DestBlock
= getBasicBlockByName(*F
, "bb2");
132 NewBB
= SplitEdge(SrcBlock
, DestBlock
, &DT
, nullptr, nullptr);
134 EXPECT_TRUE(DT
.verify());
135 EXPECT_EQ(NewBB
->getSinglePredecessor(), SrcBlock
);
136 EXPECT_EQ(NewBB
->getSingleSuccessor(), DestBlock
);
137 EXPECT_EQ(NewBB
->getParent(), F
);
140 for (BasicBlock
&BB
: *F
) {
141 if (BB
.getName() == NewBB
->getName()) {
148 TEST(BasicBlockUtils
, SplitEdge_ex3
) {
150 std::unique_ptr
<Module
> M
= parseIR(C
, R
"IR(
151 define i32 @foo(i32 %n) {
155 %sum.02 = phi i32 [ 0, %entry ], [ %sum.1, %bb3 ]
156 %0 = phi i32 [ 0, %entry ], [ %4, %bb3 ]
157 %1 = icmp slt i32 %0, %n
158 br i1 %1, label %bb0, label %bb1
160 %2 = add nsw i32 %sum.02, 2
163 %3 = add nsw i32 %sum.02, 1
166 %sum.1 = phi i32 [ %2, %bb0 ], [ %3, %bb1 ]
169 %4 = add nsw i32 %0, 1
170 %5 = icmp slt i32 %4, 100
171 br i1 %5, label %header, label %bb4
173 %sum.0.lcssa = phi i32 [ %sum.1, %bb3 ]
177 Function
*F
= M
->getFunction("foo");
178 DominatorTree
DT(*F
);
182 DataLayout
DL("e-i64:64-f80:128-n8:16:32:64-S128");
183 TargetLibraryInfoImpl TLII
;
184 TargetLibraryInfo
TLI(TLII
);
185 AssumptionCache
AC(*F
);
188 BasicAAResult
BAA(DL
, *F
, TLI
, AC
, &DT
);
191 MemorySSA
MSSA(*F
, &AA
, &DT
);
192 MemorySSAUpdater
Updater(&MSSA
);
194 BasicBlock
*SrcBlock
;
195 BasicBlock
*DestBlock
;
198 SrcBlock
= getBasicBlockByName(*F
, "header");
199 DestBlock
= getBasicBlockByName(*F
, "bb0");
200 NewBB
= SplitEdge(SrcBlock
, DestBlock
, &DT
, &LI
, &Updater
);
202 Updater
.getMemorySSA()->verifyMemorySSA();
203 EXPECT_TRUE(DT
.verify());
204 EXPECT_NE(LI
.getLoopFor(SrcBlock
), nullptr);
205 EXPECT_NE(LI
.getLoopFor(DestBlock
), nullptr);
206 EXPECT_NE(LI
.getLoopFor(NewBB
), nullptr);
207 EXPECT_EQ(NewBB
->getSinglePredecessor(), SrcBlock
);
208 EXPECT_EQ(NewBB
->getSingleSuccessor(), DestBlock
);
209 EXPECT_EQ(NewBB
->getParent(), F
);
212 for (BasicBlock
&BB
: *F
) {
213 if (BB
.getName() == NewBB
->getName()) {
220 TEST(BasicBlockUtils
, SplitEdge_ex4
) {
222 std::unique_ptr
<Module
> M
= parseIR(C
, R
"IR(
223 define void @bar(i32 %cond) personality i8 0 {
225 switch i32 %cond, label %exit [
226 i32 -1, label %continue
227 i32 0, label %continue
228 i32 1, label %continue_alt
229 i32 2, label %continue_alt
234 invoke void @sink() to label %normal unwind label %exception
236 invoke void @sink_alt() to label %normal unwind label %exception
238 %cleanup = landingpad i8 cleanup
239 br label %trivial-eh-handler
241 call void @sideeffect(i32 1)
244 call void @sideeffect(i32 0)
248 declare void @sideeffect(i32)
249 declare void @sink() cold
250 declare void @sink_alt() cold
252 Function
*F
= M
->getFunction("bar");
254 DominatorTree
DT(*F
);
258 TargetLibraryInfoImpl TLII
;
259 TargetLibraryInfo
TLI(TLII
);
263 MemorySSA
MSSA(*F
, &AA
, &DT
);
264 MemorySSAUpdater
MSSAU(&MSSA
);
266 BasicBlock
*SrcBlock
;
267 BasicBlock
*DestBlock
;
269 SrcBlock
= getBasicBlockByName(*F
, "continue");
270 DestBlock
= getBasicBlockByName(*F
, "exception");
272 unsigned SuccNum
= GetSuccessorNumber(SrcBlock
, DestBlock
);
273 Instruction
*LatchTerm
= SrcBlock
->getTerminator();
275 const CriticalEdgeSplittingOptions Options
=
276 CriticalEdgeSplittingOptions(&DT
, &LI
, &MSSAU
);
278 // Check that the following edge is both critical and the destination block is
279 // an exception block. These must be handled differently by SplitEdge
281 isCriticalEdge(LatchTerm
, SuccNum
, Options
.MergeIdenticalEdges
);
282 EXPECT_TRUE(CriticalEdge
);
284 bool Ehpad
= DestBlock
->isEHPad();
287 BasicBlock
*NewBB
= SplitEdge(SrcBlock
, DestBlock
, &DT
, &LI
, &MSSAU
, "");
289 MSSA
.verifyMemorySSA();
290 EXPECT_TRUE(DT
.verify());
291 EXPECT_NE(NewBB
, nullptr);
292 EXPECT_EQ(NewBB
->getSinglePredecessor(), SrcBlock
);
293 EXPECT_EQ(NewBB
, SrcBlock
->getTerminator()->getSuccessor(SuccNum
));
294 EXPECT_EQ(NewBB
->getParent(), F
);
297 for (BasicBlock
&BB
: *F
) {
298 if (BB
.getName() == NewBB
->getName()) {
306 TEST(BasicBlockUtils
, splitBasicBlockBefore_ex1
) {
308 std::unique_ptr
<Module
> M
= parseIR(C
, R
"IR(
316 %1 = phi i32 [ %0, %bb0 ]
322 Function
*F
= M
->getFunction("foo");
323 DominatorTree
DT(*F
);
325 BasicBlock
*DestBlock
;
328 DestBlock
= getBasicBlockByName(*F
, "bb2");
330 NewBB
= DestBlock
->splitBasicBlockBefore(DestBlock
->front().getIterator(),
333 PHINode
*PN
= dyn_cast
<PHINode
>(&(DestBlock
->front()));
334 EXPECT_EQ(PN
->getIncomingBlock(0), NewBB
);
335 EXPECT_EQ(NewBB
->getName(), "test");
336 EXPECT_EQ(NewBB
->getSingleSuccessor(), DestBlock
);
337 EXPECT_EQ(DestBlock
->getSinglePredecessor(), NewBB
);
341 TEST(BasicBlockUtils
, splitBasicBlockBefore_ex2
) {
343 std::unique_ptr
<Module
> M
= parseIR(C
, R
"IR(
351 %1 = phi i32 [ %0, %bb0 ], [ 1, %bb1 ]
357 Function
*F
= M
->getFunction("foo");
358 DominatorTree
DT(*F
);
360 BasicBlock
*DestBlock
= getBasicBlockByName(*F
, "bb2");
364 DestBlock
->splitBasicBlockBefore(DestBlock
->front().getIterator(),
367 "cannot split on multi incoming phis");
371 TEST(BasicBlockUtils
, NoUnreachableBlocksToEliminate
) {
373 std::unique_ptr
<Module
> M
= parseIR(C
, R
"IR(
374 define i32 @no_unreachable(i1 %cond) {
376 br i1 %cond, label %bb0, label %bb1
380 %phi = phi i32 [ 0, %entry ], [ 1, %bb0 ]
384 Function
*F
= M
->getFunction("no_unreachable");
385 DominatorTree
DT(*F
);
386 DomTreeUpdater
DTU(DT
, DomTreeUpdater::UpdateStrategy::Eager
);
388 EXPECT_EQ(F
->size(), (size_t)3);
389 bool Result
= EliminateUnreachableBlocks(*F
, &DTU
);
390 EXPECT_FALSE(Result
);
391 EXPECT_EQ(F
->size(), (size_t)3);
392 EXPECT_TRUE(DT
.verify());
395 TEST(BasicBlockUtils
, SplitBlockPredecessors
) {
397 std::unique_ptr
<Module
> M
= parseIR(C
, R
"IR(
398 define i32 @basic_func(i1 %cond) {
400 br i1 %cond, label %bb0, label %bb1
404 %phi = phi i32 [ 0, %entry ], [ 1, %bb0 ]
408 Function
*F
= M
->getFunction("basic_func");
409 DominatorTree
DT(*F
);
411 // Make sure the dominator tree is properly updated if calling this on the
413 SplitBlockPredecessors(&F
->getEntryBlock(), {}, "split.entry", &DT
);
414 EXPECT_TRUE(DT
.verify());
417 TEST(BasicBlockUtils
, SplitCriticalEdge
) {
419 std::unique_ptr
<Module
> M
= parseIR(C
, R
"IR(
420 define void @crit_edge(i1 %cond0, i1 %cond1) {
422 br i1 %cond0, label %bb0, label %bb1
431 Function
*F
= M
->getFunction("crit_edge");
432 DominatorTree
DT(*F
);
433 PostDominatorTree
PDT(*F
);
435 CriticalEdgeSplittingOptions
CESO(&DT
, nullptr, nullptr, &PDT
);
436 EXPECT_EQ(1u, SplitAllCriticalEdges(*F
, CESO
));
437 EXPECT_TRUE(DT
.verify());
438 EXPECT_TRUE(PDT
.verify());
441 TEST(BasicBlockUtils
, SplitIndirectBrCriticalEdgesIgnorePHIs
) {
443 std::unique_ptr
<Module
> M
= parseIR(C
, R
"IR(
444 define void @crit_edge(i8* %tgt, i1 %cond0, i1 %cond1) {
446 indirectbr i8* %tgt, [label %bb0, label %bb1, label %bb2]
448 br i1 %cond0, label %bb1, label %bb2
450 %p = phi i32 [0, %bb0], [0, %entry]
451 br i1 %cond1, label %bb3, label %bb4
460 Function
*F
= M
->getFunction("crit_edge");
461 DominatorTree
DT(*F
);
463 BranchProbabilityInfo
BPI(*F
, LI
);
464 BlockFrequencyInfo
BFI(*F
, BPI
, LI
);
466 ASSERT_TRUE(SplitIndirectBrCriticalEdges(*F
, /*IgnoreBlocksWithoutPHI=*/true,
469 // Check that successors of the split block get their probability correct.
470 BasicBlock
*BB1
= getBasicBlockByName(*F
, "bb1");
471 BasicBlock
*SplitBB
= BB1
->getTerminator()->getSuccessor(0);
472 ASSERT_EQ(2u, SplitBB
->getTerminator()->getNumSuccessors());
473 EXPECT_EQ(BranchProbability(1, 2), BPI
.getEdgeProbability(SplitBB
, 0u));
474 EXPECT_EQ(BranchProbability(1, 2), BPI
.getEdgeProbability(SplitBB
, 1u));
476 // bb2 has no PHI, so we shouldn't split bb0 -> bb2
477 BasicBlock
*BB0
= getBasicBlockByName(*F
, "bb0");
478 ASSERT_EQ(2u, BB0
->getTerminator()->getNumSuccessors());
479 EXPECT_EQ(BB0
->getTerminator()->getSuccessor(1),
480 getBasicBlockByName(*F
, "bb2"));
483 TEST(BasicBlockUtils
, SplitIndirectBrCriticalEdges
) {
485 std::unique_ptr
<Module
> M
= parseIR(C
, R
"IR(
486 define void @crit_edge(i8* %tgt, i1 %cond0, i1 %cond1) {
488 indirectbr i8* %tgt, [label %bb0, label %bb1, label %bb2]
490 br i1 %cond0, label %bb1, label %bb2
492 %p = phi i32 [0, %bb0], [0, %entry]
493 br i1 %cond1, label %bb3, label %bb4
502 Function
*F
= M
->getFunction("crit_edge");
503 DominatorTree
DT(*F
);
505 BranchProbabilityInfo
BPI(*F
, LI
);
506 BlockFrequencyInfo
BFI(*F
, BPI
, LI
);
508 ASSERT_TRUE(SplitIndirectBrCriticalEdges(*F
, /*IgnoreBlocksWithoutPHI=*/false,
511 // Check that successors of the split block get their probability correct.
512 BasicBlock
*BB1
= getBasicBlockByName(*F
, "bb1");
513 BasicBlock
*SplitBB
= BB1
->getTerminator()->getSuccessor(0);
514 ASSERT_EQ(2u, SplitBB
->getTerminator()->getNumSuccessors());
515 EXPECT_EQ(BranchProbability(1, 2), BPI
.getEdgeProbability(SplitBB
, 0u));
516 EXPECT_EQ(BranchProbability(1, 2), BPI
.getEdgeProbability(SplitBB
, 1u));
518 // Should split, resulting in:
519 // bb0 -> bb2.clone; bb2 -> split1; bb2.clone -> split,
520 BasicBlock
*BB0
= getBasicBlockByName(*F
, "bb0");
521 ASSERT_EQ(2u, BB0
->getTerminator()->getNumSuccessors());
522 BasicBlock
*BB2Clone
= BB0
->getTerminator()->getSuccessor(1);
523 BasicBlock
*BB2
= getBasicBlockByName(*F
, "bb2");
524 EXPECT_NE(BB2Clone
, BB2
);
525 ASSERT_EQ(1u, BB2
->getTerminator()->getNumSuccessors());
526 ASSERT_EQ(1u, BB2Clone
->getTerminator()->getNumSuccessors());
527 EXPECT_EQ(BB2
->getTerminator()->getSuccessor(0),
528 BB2Clone
->getTerminator()->getSuccessor(0));
531 TEST(BasicBlockUtils
, SetEdgeProbability
) {
533 std::unique_ptr
<Module
> M
= parseIR(C
, R
"IR(
534 define void @edge_probability(i32 %0) {
536 switch i32 %0, label %LD [
557 ], !prof !{!"branch_weights
", i32 1, i32 1, i32 1, i32 1, i32 1, i32 451, i32 1,
558 i32 12, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1,
559 i32 1, i32 1, i32 1, i32 1, i32 1}
604 Function
*F
= M
->getFunction("edge_probability");
605 DominatorTree
DT(*F
);
607 BranchProbabilityInfo
BPI(*F
, LI
);
609 // Check that the unreachable block has the minimal probability.
610 const BasicBlock
*EntryBB
= getBasicBlockByName(*F
, "entry");
611 const BasicBlock
*UnreachableBB
= getBasicBlockByName(*F
, "LD");
612 EXPECT_EQ(BranchProbability::getRaw(1),
613 BPI
.getEdgeProbability(EntryBB
, UnreachableBB
));