llvm.org GIT mirror llvm / b9fe533
[JumpThread] We want to fold (not thread) when all predecessor go to single BB's successor. . Summary: In case all predecessor go to a single successor of current BB. We want to fold (not thread). Reviewers: efriedma, sanjoy Reviewed By: sanjoy Subscribers: dberlin, majnemer, llvm-commits Differential Revision: https://reviews.llvm.org/D30869 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@300657 91177308-0d34-0410-b5e6-96231b3b80d8 Xin Tong 2 years ago
2 changed file(s) with 116 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
12871287 // If all edges were unthreadable, we fail.
12881288 if (PredToDestList.empty())
12891289 return false;
1290
1291 // If all the predecessors go to a single known successor, we want to fold,
1292 // not thread. By doing so, we do not need to duplicate the current block and
1293 // also miss potential opportunities in case we don't/can't duplicate.
1294 if (OnlyDest && OnlyDest != MultipleDestSentinel) {
1295 if (PredToDestList.size() ==
1296 (size_t)std::distance(pred_begin(BB), pred_end(BB))) {
1297 for (BasicBlock *SuccBB : successors(BB)) {
1298 if (SuccBB != OnlyDest)
1299 SuccBB->removePredecessor(BB, true); // This is unreachable successor.
1300 }
1301
1302 // Finally update the terminator.
1303 TerminatorInst *Term = BB->getTerminator();
1304 BranchInst::Create(OnlyDest, Term);
1305 Term->eraseFromParent();
1306
1307 // If the condition is now dead due to the removal of the old terminator,
1308 // erase it.
1309 auto *CondInst = dyn_cast(Cond);
1310 if (CondInst && CondInst->use_empty())
1311 CondInst->eraseFromParent();
1312 // FIXME: in case this instruction is defined in the current BB and it
1313 // resolves to a single value from all predecessors, we can do RAUW.
1314 return true;
1315 }
1316 }
12901317
12911318 // Determine which is the most common successor. If we have many inputs and
12921319 // this block is a switch, we want to start by threading the batch that goes
22 declare i32 @f1()
33 declare i32 @f2()
44 declare void @f3()
5
6 ; Make sure we can fold this branch ... We will not be able to thread it as
7 ; L0 is too big to duplicate. L2 is the unreachable block here.
8 ;
9 ; CHECK-LABEL: @test_br_folding_not_threading(
10 ; CHECK: L1:
11 ; CHECK: call i32 @f2()
12 ; CHECK: call void @f3()
13 ; CHECK-NEXT: ret void
14 ; CHECK-NOT: br
15 ; CHECK: L3:
16 define void @test_br_folding_not_threading(i1 %cond) nounwind {
17 entry:
18 br i1 %cond, label %L0, label %L3
19 L0:
20 call i32 @f2()
21 call i32 @f2()
22 call i32 @f2()
23 call i32 @f2()
24 call i32 @f2()
25 call i32 @f2()
26 call i32 @f2()
27 call i32 @f2()
28 call i32 @f2()
29 call i32 @f2()
30 call i32 @f2()
31 call i32 @f2()
32 call i32 @f2()
33 br i1 %cond, label %L1, label %L2
34
35 L1:
36 call void @f3()
37 ret void
38 L2:
39 call void @f3()
40 ret void
41 L3:
42 call void @f3()
43 ret void
44 }
45
46
47 ; Make sure we can fold this branch ... We will not be able to thread it as
48 ; L0 is too big to duplicate. L2 is the unreachable block here.
49 ; With more than 1 predecessors.
50 ;
51 ; CHECK-LABEL: @test_br_folding_not_threading_multiple_preds(
52 ; CHECK: L1:
53 ; CHECK: call i32 @f2()
54 ; CHECK: call void @f3()
55 ; CHECK-NEXT: ret void
56 ; CHECK-NOT: br
57 ; CHECK: L3:
58 define void @test_br_folding_not_threading_multiple_preds(i1 %condx, i1 %cond) nounwind {
59 entry:
60 br i1 %condx, label %X0, label %X1
61
62 X0:
63 br i1 %cond, label %L0, label %L3
64
65 X1:
66 br i1 %cond, label %L0, label %L3
67
68 L0:
69 call i32 @f2()
70 call i32 @f2()
71 call i32 @f2()
72 call i32 @f2()
73 call i32 @f2()
74 call i32 @f2()
75 call i32 @f2()
76 call i32 @f2()
77 call i32 @f2()
78 call i32 @f2()
79 call i32 @f2()
80 call i32 @f2()
81 call i32 @f2()
82 br i1 %cond, label %L1, label %L2
83
84 L1:
85 call void @f3()
86 ret void
87 L2:
88 call void @f3()
89 ret void
90 L3:
91 call void @f3()
92 ret void
93 }
594
695 define i32 @test1(i1 %cond) {
796 ; CHECK-LABEL: @test1(