[llvm-commits] CVS: llvm/lib/CodeGen/BranchFolding.cpp

Chris Lattner sabre at nondot.org
Sat Nov 18 13:56:54 PST 2006



Changes in directory llvm/lib/CodeGen:

BranchFolding.cpp updated: 1.35 -> 1.36
---
Log message:

Fix another case we *don't* want to do this xform.


---
Diffs of the changes:  (+26 -2)

 BranchFolding.cpp |   28 ++++++++++++++++++++++++++--
 1 files changed, 26 insertions(+), 2 deletions(-)


Index: llvm/lib/CodeGen/BranchFolding.cpp
diff -u llvm/lib/CodeGen/BranchFolding.cpp:1.35 llvm/lib/CodeGen/BranchFolding.cpp:1.36
--- llvm/lib/CodeGen/BranchFolding.cpp:1.35	Sat Nov 18 15:30:35 2006
+++ llvm/lib/CodeGen/BranchFolding.cpp	Sat Nov 18 15:56:39 2006
@@ -16,6 +16,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#define DEBUG_TYPE "branchfolding"
 #include "llvm/CodeGen/Passes.h"
 #include "llvm/CodeGen/MachineDebugInfo.h"
 #include "llvm/CodeGen/MachineFunctionPass.h"
@@ -23,6 +24,7 @@
 #include "llvm/Target/TargetInstrInfo.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/Debug.h"
 #include "llvm/ADT/Statistic.h"
 #include "llvm/ADT/STLExtras.h"
 #include <algorithm>
@@ -705,17 +707,39 @@
     if (!PriorCond.empty() && PriorFBB == 0 &&
         MachineFunction::iterator(PriorTBB) == FallThrough &&
         !CanFallThrough(MBB)) {
+      bool DoTransform = true;
+      
       // We have to be careful that the succs of PredBB aren't both no-successor
       // blocks.  If neither have successors and if PredBB is the second from
       // last block in the function, we'd just keep swapping the two blocks for
       // last.  Only do the swap if one is clearly better to fall through than
       // the other.
-      if (FallThrough != --MBB->getParent()->end() ||
-          IsBetterFallthrough(PriorTBB, MBB, *TII)) {
+      if (FallThrough == --MBB->getParent()->end() &&
+          !IsBetterFallthrough(PriorTBB, MBB, *TII))
+        DoTransform = false;
+
+      // We don't want to do this transformation if we have control flow like:
+      //   br cond BB2
+      // BB1:
+      //   ..
+      //   jmp BBX
+      // BB2:
+      //   ..
+      //   ret
+      //
+      // In this case, we could actually be moving the return block *into* a
+      // loop!
+      if (DoTransform && !MBB->succ_empty() && !CanFallThrough(PriorTBB))
+        DoTransform = false;
+      
       
+      if (DoTransform) {
         // Reverse the branch so we will fall through on the previous true cond.
         std::vector<MachineOperand> NewPriorCond(PriorCond);
         if (!TII->ReverseBranchCondition(NewPriorCond)) {
+          DOUT << "\nMoving MBB: " << *MBB;
+          DOUT << "To make fallthrough to: " << *PriorTBB << "\n";
+          
           TII->RemoveBranch(PrevBB);
           TII->InsertBranch(PrevBB, MBB, 0, NewPriorCond);
 






More information about the llvm-commits mailing list