[PATCH] D26299: Improving the efficiency of the Loop Unswitching pass

Abhilash Bhandari via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 6 09:32:46 PST 2016


Abhilash updated the summary for this revision.
Abhilash updated this revision to Diff 76988.
Abhilash added a comment.

The following changes have been incorporated in the updated diff:
As suggested by Michael Zolothukin:

1. Context added.
2. Clang-format has been applied.

As suggested by Weiming Zhao:

1. Removed the redundant conditional break on 'isUnreachable'.


Repository:
  rL LLVM

https://reviews.llvm.org/D26299

Files:
  lib/Transforms/Scalar/LoopUnswitch.cpp


Index: lib/Transforms/Scalar/LoopUnswitch.cpp
===================================================================
--- lib/Transforms/Scalar/LoopUnswitch.cpp
+++ lib/Transforms/Scalar/LoopUnswitch.cpp
@@ -593,6 +593,45 @@
       continue;
 
     if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
+      /*
+       * Some branches may be rendered unreachable because of previous
+       * unswitching.
+       * Unswitch only those branches that are reachable.
+       */
+      auto *Node = DT->getNode(*I)->getIDom();
+      BasicBlock *DomBB = Node->getBlock();
+      bool isUnreachable = false;
+      while (currentLoop->contains(DomBB)) {
+        BranchInst *BI = dyn_cast<BranchInst>(DomBB->getTerminator());
+
+        if (BI && BI->isConditional()) {
+          Value *Cond = BI->getCondition();
+          BasicBlock *Succ = NULL;
+          if (Cond == ConstantInt::getTrue(Cond->getContext())) {
+            Succ = BI->getSuccessor(1);
+          } else if (Cond == ConstantInt::getFalse(Cond->getContext())) {
+            Succ = BI->getSuccessor(0);
+          }
+
+          if (Succ) {
+            BasicBlockEdge UnreachableEdge(BI->getParent(), Succ);
+            if (DT->dominates(UnreachableEdge, *I)) {
+              isUnreachable = true;
+              break;
+            }
+          }
+        }
+
+        Node = DT->getNode(DomBB)->getIDom();
+        DomBB = Node->getBlock();
+      }
+
+      if (isUnreachable) {
+        DEBUG(dbgs() << "Unreachable branch: " << *TI << " in Function "
+                     << (*I)->getParent()->getName() << "\n");
+        continue;
+      }
+
       // If this isn't branching on an invariant condition, we can't unswitch
       // it.
       if (BI->isConditional()) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D26299.76988.patch
Type: text/x-patch
Size: 1741 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161106/ab5d21b6/attachment.bin>


More information about the llvm-commits mailing list