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

Chris Lattner sabre at nondot.org
Tue Oct 17 10:14:07 PDT 2006



Changes in directory llvm/lib/CodeGen:

BranchFolding.cpp updated: 1.11 -> 1.12
---
Log message:

Reenable this pass, fixing the bugs in it.
It now correctly deletes unreachable blocks and blocks that are empty.



---
Diffs of the changes:  (+32 -23)

 BranchFolding.cpp |   55 +++++++++++++++++++++++++++++++-----------------------
 1 files changed, 32 insertions(+), 23 deletions(-)


Index: llvm/lib/CodeGen/BranchFolding.cpp
diff -u llvm/lib/CodeGen/BranchFolding.cpp:1.11 llvm/lib/CodeGen/BranchFolding.cpp:1.12
--- llvm/lib/CodeGen/BranchFolding.cpp:1.11	Fri Oct 13 19:30:06 2006
+++ llvm/lib/CodeGen/BranchFolding.cpp	Tue Oct 17 12:13:52 2006
@@ -18,6 +18,7 @@
 
 #include "llvm/CodeGen/Passes.h"
 #include "llvm/CodeGen/MachineFunctionPass.h"
+#include "llvm/CodeGen/MachineJumpTableInfo.h"
 #include "llvm/Target/TargetInstrInfo.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/ADT/STLExtras.h"
@@ -36,39 +37,39 @@
 
 FunctionPass *llvm::createBranchFoldingPass() { return new BranchFolder(); }
 
+/// RemoveDeadBlock - Remove the specified dead machine basic block from the
+/// function, updating the CFG.
+static void RemoveDeadBlock(MachineBasicBlock *MBB) {
+  assert(MBB->pred_empty() && "MBB must be dead!");
+  MachineFunction *MF = MBB->getParent();
+  // drop all successors.
+  while (!MBB->succ_empty())
+    MBB->removeSuccessor(MBB->succ_end()-1);
+  // Remove the block.
+  MF->getBasicBlockList().erase(MBB);
+}
+
 bool BranchFolder::runOnMachineFunction(MachineFunction &MF) {
   TII = MF.getTarget().getInstrInfo();
   if (!TII) return false;
 
-  
-  // DISABLED FOR NOW.
-  return false;
-  
   //MF.dump();
   
   bool EverMadeChange = false;
   MadeChange = true;
   while (MadeChange) {
     MadeChange = false;
-    for (MachineFunction::iterator MBB = ++MF.begin(), E = MF.end(); MBB != E;
-         ++MBB)
-      OptimizeBlock(MBB);
     
-    // If branches were folded away somehow, do a quick scan and delete any dead
-    // blocks.
-    if (MadeChange) {
-      for (MachineFunction::iterator I = ++MF.begin(), E = MF.end(); I != E; ) {
-        MachineBasicBlock *MBB = I++;
-        // Is it dead?
-        if (MBB->pred_empty()) {
-          // drop all successors.
-          while (!MBB->succ_empty())
-            MBB->removeSuccessor(MBB->succ_end()-1);
-          MF.getBasicBlockList().erase(MBB);
-        }
+    for (MachineFunction::iterator I = ++MF.begin(), E = MF.end(); I != E; ) {
+      MachineBasicBlock *MBB = I++;
+      OptimizeBlock(MBB);
+      
+      // If it is dead, remove it.
+      if (MBB->pred_empty()) {
+        RemoveDeadBlock(MBB);
+        MadeChange = true;
       }
-    }
-
+    }      
     EverMadeChange |= MadeChange;
   }
 
@@ -115,14 +116,22 @@
     
     MachineFunction::iterator FallThrough = next(MBB);
     
-    if (FallThrough != MBB->getParent()->end()) {
+    if (FallThrough == MBB->getParent()->end()) {
+      // TODO: Simplify preds to not branch here if possible!
+    } else {
+      // Rewrite all predecessors of the old block to go to the fallthrough
+      // instead.
       while (!MBB->pred_empty()) {
         MachineBasicBlock *Pred = *(MBB->pred_end()-1);
         ReplaceUsesOfBlockWith(Pred, MBB, FallThrough, TII);
       }
+      
+      // If MBB was the target of a jump table, update jump tables to go to the
+      // fallthrough instead.
+      MBB->getParent()->getJumpTableInfo()->ReplaceMBBInJumpTables(MBB,
+                                                                   FallThrough);
       MadeChange = true;
     }
-    // TODO: CHANGE STUFF TO NOT BRANCH HERE!
     return;
   }
 






More information about the llvm-commits mailing list