[PATCH] D25511: BranchRelaxation: Expand unconditional branches first

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 12 04:41:58 PDT 2016


arsenm created this revision.
arsenm added a subscriber: llvm-commits.
Herald added a subscriber: wdng.

It's likely if a conditional branch needs to be expanded, the following
unconditional branch will also need expansion. By expanding the
unconditional branch first, the conditional branch can be simply
inverted to jump over the inserted indirect branch block. If the
conditional branch is expanded first, it results in an additional
branch.

      

This avoids test regressions in future commits.


https://reviews.llvm.org/D25511

Files:
  lib/CodeGen/BranchRelaxation.cpp


Index: lib/CodeGen/BranchRelaxation.cpp
===================================================================
--- lib/CodeGen/BranchRelaxation.cpp
+++ lib/CodeGen/BranchRelaxation.cpp
@@ -415,6 +415,28 @@
   for (MachineFunction::iterator I = MF->begin(); I != MF->end(); ++I) {
     MachineBasicBlock &MBB = *I;
 
+    auto Last = MBB.rbegin();
+    if (Last == MBB.rend()) // Empty block.
+      continue;
+
+    // Expand the unconditional branch first if necessary. If there is a
+    // conditional branch, this will end up changing the branch destination of
+    // it to be over the newly inserted indirect branch block, which may avoid
+    // the need to try expanding the conditional branch first, saving an extra
+    // jump.
+    if (Last->isUnconditionalBranch()) {
+      // Unconditional branch destination might be unanalyzable, assume these
+      // are OK.
+      if (MachineBasicBlock *DestBB = TII->getBranchDestBlock(*Last)) {
+        if (!isBlockInRange(*Last, *DestBB)) {
+          fixupUnconditionalBranch(*Last);
+          ++NumUnconditionalRelaxed;
+          Changed = true;
+        }
+      }
+    }
+
+    // Loop over the conditional branches.
     MachineBasicBlock::iterator Next;
     for (MachineBasicBlock::iterator J = MBB.getFirstTerminator();
          J != MBB.end(); J = Next) {
@@ -441,21 +463,6 @@
           Next = MBB.getFirstTerminator();
         }
       }
-
-      if (MI.isUnconditionalBranch()) {
-        // Unconditional branch destination might be unanalyzable, assume these
-        // are OK.
-        if (MachineBasicBlock *DestBB = TII->getBranchDestBlock(MI)) {
-          if (!isBlockInRange(MI, *DestBB)) {
-            fixupUnconditionalBranch(MI);
-            ++NumUnconditionalRelaxed;
-            Changed = true;
-          }
-        }
-
-        // Unconditional branch is the last terminator.
-        break;
-      }
     }
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D25511.74361.patch
Type: text/x-patch
Size: 1905 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161012/359f4c1b/attachment.bin>


More information about the llvm-commits mailing list