[PATCH] D51415: [SimpleLoopUnswitch] After unswitch delete dead blocks in parent loops

Fedor Sergeev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 29 12:11:57 PDT 2018


This revision was automatically updated to reflect the committed changes.
Closed by commit rL340955: [SimpleLoopUnswitch] After unswitch delete dead blocks in parent loops (authored by fedor.sergeev, committed by ).

Repository:
  rL LLVM

https://reviews.llvm.org/D51415

Files:
  llvm/trunk/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
  llvm/trunk/test/Transforms/SimpleLoopUnswitch/delete-dead-blocks.ll


Index: llvm/trunk/test/Transforms/SimpleLoopUnswitch/delete-dead-blocks.ll
===================================================================
--- llvm/trunk/test/Transforms/SimpleLoopUnswitch/delete-dead-blocks.ll
+++ llvm/trunk/test/Transforms/SimpleLoopUnswitch/delete-dead-blocks.ll
@@ -0,0 +1,45 @@
+; RUN: opt < %s -simple-loop-unswitch -enable-nontrivial-unswitch -S 2>&1 | FileCheck %s
+; RUN: opt < %s -passes=unswitch -enable-nontrivial-unswitch -S 2>&1 | FileCheck %s
+;
+; Checking that (dead) blocks from inner loop are deleted after unswitch.
+;
+declare void @foo()
+
+; CHECK-LABEL: @Test
+define void @Test(i32) {
+entry:
+  br label %outer
+outer:
+  %oi = phi i32 [ 0, %entry ], [ %oinc, %outer_continue]
+  br label %inner
+inner:
+  %ii = phi i32 [ 0, %outer ], [ %iinc, %continue]
+  call void @foo() 
+  switch i32 %0, label %get_out2 [
+    i32 0, label %continue
+    i32 1, label %case1
+    i32 2, label %get_out
+  ]
+;
+; since we unswitch on the above switch, %case1 and %continue blocks
+; become dead in the original loop
+;
+; CHECK-NOT: case1:
+case1:
+  br label %continue
+; CHECK-NOT: {{^}}continue:
+continue:
+  %iinc = add i32 %ii, 1
+  %icmp = icmp eq i32 %ii, 100
+  br i1 %icmp, label %inner, label %outer_continue
+
+outer_continue:
+  %oinc = add i32 %oi, 1
+  %ocmp = icmp eq i32 %oi, 100
+  br i1 %ocmp, label %outer, label %get_out
+
+get_out:
+  ret void
+get_out2:
+  unreachable
+}
Index: llvm/trunk/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
===================================================================
--- llvm/trunk/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
+++ llvm/trunk/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
@@ -1380,13 +1380,21 @@
                          DominatorTree &DT, LoopInfo &LI) {
   // Find all the dead blocks, and remove them from their successors.
   SmallVector<BasicBlock *, 16> DeadBlocks;
-  for (BasicBlock *BB : llvm::concat<BasicBlock *const>(L.blocks(), ExitBlocks))
+  for (BasicBlock *BB : ExitBlocks)
     if (!DT.isReachableFromEntry(BB)) {
       for (BasicBlock *SuccBB : successors(BB))
         SuccBB->removePredecessor(BB);
       DeadBlocks.push_back(BB);
     }
 
+  for (Loop *ParentL = &L; ParentL; ParentL = ParentL->getParentLoop())
+    for (BasicBlock *BB : ParentL->blocks())
+      if (!DT.isReachableFromEntry(BB)) {
+        for (BasicBlock *SuccBB : successors(BB))
+          SuccBB->removePredecessor(BB);
+        DeadBlocks.push_back(BB);
+      }
+
   SmallPtrSet<BasicBlock *, 16> DeadBlockSet(DeadBlocks.begin(),
                                              DeadBlocks.end());
 
@@ -1431,7 +1439,7 @@
 
   // Actually delete the blocks now that they've been fully unhooked from the
   // IR.
-  for (auto *BB : DeadBlocks)
+  for (auto *BB : DeadBlockSet)
     BB->eraseFromParent();
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D51415.163156.patch
Type: text/x-patch
Size: 2820 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180829/706489e9/attachment.bin>


More information about the llvm-commits mailing list