[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
Wed Sep 19 04:42:42 PDT 2018


verena updated this revision to Diff 166093.
verena added a comment.

I noticed this fix is needed in another place in this method as well.


https://reviews.llvm.org/D47449

Files:
  lib/CodeGen/TailDuplicator.cpp


Index: lib/CodeGen/TailDuplicator.cpp
===================================================================
--- lib/CodeGen/TailDuplicator.cpp
+++ lib/CodeGen/TailDuplicator.cpp
@@ -785,6 +785,21 @@
   return true;
 }
 
+/// If the block \p MBB contains an implicit fallthrough to another block add an
+/// explicit unconditional branch. If the block is unanalyzable do nothing.
+static void addBranchForFallthrough(const TargetInstrInfo *TII,
+                                    MachineBasicBlock &MBB) {
+  MachineBasicBlock *TBB, *FBB;
+  SmallVector<MachineOperand, 4> TailCond;
+  if (!TII->analyzeBranch(MBB, TBB, FBB, TailCond)) {
+    bool FallsThrough = !TBB || (!FBB && !TailCond.empty());
+    MachineBasicBlock *FallThroughTarget = MBB.getNextNode();
+    if (FallsThrough && FallThroughTarget)
+      if (MBB.isSuccessor(FallThroughTarget))
+        TII->insertUnconditionalBranch(MBB, FallThroughTarget, DebugLoc());
+  }
+}
+
 /// If it is profitable, duplicate TailBB's contents in each
 /// of its predecessors.
 /// \p IsSimple result of isSimpleBB
@@ -835,6 +850,10 @@
 
     TDBBs.push_back(PredBB);
 
+    // If the BB to merge in has a fallthrough, insert an explicit branch,
+    // because these instructions will be moved into a different block now.
+    addBranchForFallthrough(TII, *TailBB);
+
     // Remove PredBB's unconditional branch.
     TII->removeBranch(*PredBB);
 
@@ -895,6 +914,7 @@
       !TailBB->hasAddressTaken()) {
     LLVM_DEBUG(dbgs() << "\nMerging into block: " << *PrevBB
                       << "From MBB: " << *TailBB);
+    addBranchForFallthrough(TII, *TailBB);
     // 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.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D47449.166093.patch
Type: text/x-patch
Size: 1818 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180919/a27ee3bc/attachment.bin>


More information about the llvm-commits mailing list