llvm.org GIT mirror llvm / ff4ee4d
[SimpleLoopUnswitch] remove a chain of dead blocks at once Recent change to deleteDeadBlocksFromLoop was not enough to fix all the problems related to dead blocks after nontrivial unswitching of switches. We need to delete all the dead blocks that were created during unswitching, otherwise we will keep having problems with phi's or dead blocks. This change removes all the dead blocks that are reachable from the loop, not trying to track whether these blocks are newly created by unswitching or not. While not completely correct, we are unlikely to get loose but reachable dead blocks that do not belong to our loop nest. It does fix all the failures currently known, in particular PR38778. Reviewed By: asbirlea Differential Revision: https://reviews.llvm.org/D51519 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@341398 91177308-0d34-0410-b5e6-96231b3b80d8 Fedor Sergeev 1 year, 18 days ago
2 changed file(s) with 75 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
13771377 deleteDeadBlocksFromLoop(Loop &L,
13781378 SmallVectorImpl &ExitBlocks,
13791379 DominatorTree &DT, LoopInfo &LI) {
1380 // Find all the dead blocks, and remove them from their successors.
1381 SmallVector DeadBlocks;
1382 for (BasicBlock *BB : ExitBlocks)
1383 if (!DT.isReachableFromEntry(BB)) {
1384 for (BasicBlock *SuccBB : successors(BB))
1380 // Find all the dead blocks tied to this loop, and remove them from their
1381 // successors.
1382 SmallPtrSet DeadBlockSet;
1383
1384 // Start with loop/exit blocks and get a transitive closure of reachable dead
1385 // blocks.
1386 SmallVector DeathCandidates(ExitBlocks.begin(),
1387 ExitBlocks.end());
1388 DeathCandidates.append(L.blocks().begin(), L.blocks().end());
1389 while (!DeathCandidates.empty()) {
1390 auto *BB = DeathCandidates.pop_back_val();
1391 if (!DeadBlockSet.count(BB) && !DT.isReachableFromEntry(BB)) {
1392 for (BasicBlock *SuccBB : successors(BB)) {
13851393 SuccBB->removePredecessor(BB);
1386 DeadBlocks.push_back(BB);
1387 }
1388
1389 for (Loop *ParentL = &L; ParentL; ParentL = ParentL->getParentLoop())
1390 for (BasicBlock *BB : ParentL->blocks())
1391 if (!DT.isReachableFromEntry(BB)) {
1392 for (BasicBlock *SuccBB : successors(BB))
1393 SuccBB->removePredecessor(BB);
1394 DeadBlocks.push_back(BB);
1394 DeathCandidates.push_back(SuccBB);
13951395 }
1396
1397 SmallPtrSet DeadBlockSet(DeadBlocks.begin(),
1398 DeadBlocks.end());
1396 DeadBlockSet.insert(BB);
1397 }
1398 }
13991399
14001400 // Filter out the dead blocks from the exit blocks list so that it can be
14011401 // used in the caller.
14041404
14051405 // Walk from this loop up through its parents removing all of the dead blocks.
14061406 for (Loop *ParentL = &L; ParentL; ParentL = ParentL->getParentLoop()) {
1407 for (auto *BB : DeadBlocks)
1407 for (auto *BB : DeadBlockSet)
14081408 ParentL->getBlocksSet().erase(BB);
14091409 llvm::erase_if(ParentL->getBlocksVector(),
14101410 [&](BasicBlock *BB) { return DeadBlockSet.count(BB); });
14291429 // Remove the loop mappings for the dead blocks and drop all the references
14301430 // from these blocks to others to handle cyclic references as we start
14311431 // deleting the blocks themselves.
1432 for (auto *BB : DeadBlocks) {
1432 for (auto *BB : DeadBlockSet) {
14331433 // Check that the dominator tree has already been updated.
14341434 assert(!DT.getNode(BB) && "Should already have cleared domtree!");
14351435 LI.changeLoopFor(BB, nullptr);
4242 get_out2:
4343 unreachable
4444 }
45
46 ;
47 ; This comes from PR38778
48 ; CHECK-LABEL: @Test2
49 define void @Test2(i32) {
50 header:
51 br label %loop
52 loop:
53 switch i32 %0, label %continue [
54 i32 -2147483648, label %check
55 i32 98, label %guarded1
56 i32 99, label %guarded2
57 ]
58 ; CHECK-NOT: {{^}}guarded1:
59 guarded1:
60 br i1 undef, label %continue, label %leave
61 guarded2:
62 br label %continue
63 check:
64 %val = add i32 0, 1
65 br i1 undef, label %continue, label %leave
66 continue:
67 br label %loop
68 leave:
69 %local = phi i32 [ 0, %guarded1 ], [ %val, %check ]
70 ret void
71 }
72
73 ;
74 ; Yet another test from PR38778
75 ;
76 ; CHECK-LABEL: @Test3
77 define void @Test3(i32) {
78 header:
79 br label %outer
80 outer:
81 %bad_input.i = icmp eq i32 %0, -2147483648
82 br label %inner
83 inner:
84 br i1 %bad_input.i, label %overflow, label %switchme
85 overflow:
86 br label %continue
87 switchme:
88 switch i32 %0, label %continue [
89 i32 88, label %go_out
90 i32 99, label %case2
91 ]
92 ; CHECK-NOT: {{^}}case2:
93 case2:
94 br label %continue
95 continue:
96 %local_11_92 = phi i32 [ 0, %switchme ], [ 18, %case2 ], [ 0, %overflow ]
97 br i1 undef, label %outer, label %inner
98 go_out:
99 unreachable
100 }