[PATCH] D47449: TailDuplicator: Fix tail duplicator to insert a branch when fallthrough is present.

Verena via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon May 28 03:54:12 PDT 2018


verena created this revision.
verena added reviewers: MatzeB, iteratee.
Herald added a subscriber: llvm-commits.

If a block with an implicit fallthrough is duplicated into another, we need to insert an explicit branch, since the target block could be anywhere in the function. Also remove a duplicated call to removeBranch.

Unfortunately, I work on a proprietary target, so cannot provide a test case.


Repository:
  rL LLVM

https://reviews.llvm.org/D47449

Files:
  lib/CodeGen/TailDuplicator.cpp


Index: lib/CodeGen/TailDuplicator.cpp
===================================================================
--- lib/CodeGen/TailDuplicator.cpp
+++ lib/CodeGen/TailDuplicator.cpp
@@ -895,6 +895,19 @@
       !TailBB->hasAddressTaken()) {
     LLVM_DEBUG(dbgs() << "\nMerging into block: " << *PrevBB
                       << "From MBB: " << *TailBB);
+    // If the BB to merge in has a fallthrough, insert an explicit branch,
+    // because these instructions will be moved into a different block now.
+    MachineBasicBlock *TailTBB, *TailFBB;
+    SmallVector<MachineOperand, 4> TailCond;
+    if (!TII->analyzeBranch(*TailBB, TailTBB, TailFBB, TailCond)) {
+      bool FallsThrough =
+          (TailTBB == nullptr) || (TailFBB == nullptr && !TailCond.empty());
+      MachineBasicBlock *FallThroughTarget = TailBB->getNextNode();
+      if (FallsThrough && FallThroughTarget != nullptr)
+        if (TailBB->isSuccessor(FallThroughTarget))
+          TII->insertUnconditionalBranch(*TailBB, FallThroughTarget,
+                                         DebugLoc());
+    }
     // There may be a branch to the layout successor. This is unlikely but it
     // happens. The correct thing to do is to remove the branch before
     // duplicating the instructions in all cases.
@@ -922,7 +935,6 @@
       }
       appendCopies(PrevBB, CopyInfos, Copies);
     } else {
-      TII->removeBranch(*PrevBB);
       // No PHIs to worry about, just splice the instructions over.
       PrevBB->splice(PrevBB->end(), TailBB, TailBB->begin(), TailBB->end());
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D47449.148803.patch
Type: text/x-patch
Size: 1557 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180528/1f32155b/attachment-0001.bin>


More information about the llvm-commits mailing list