Fix CodeGen/Generic/2007-04-08-MultipleFrameIndices.ll and PR1308:
[oota-llvm.git] / lib / CodeGen / BranchFolding.cpp
index 10262f285446639628b545b9fc0f91eb82524960..1f5d6752b659a11ebc69de984d79205d4108e8be 100644 (file)
 #include "llvm/CodeGen/MachineModuleInfo.h"
 #include "llvm/CodeGen/MachineFunctionPass.h"
 #include "llvm/CodeGen/MachineJumpTableInfo.h"
+#include "llvm/CodeGen/RegisterScavenging.h"
 #include "llvm/Target/TargetInstrInfo.h"
 #include "llvm/Target/TargetMachine.h"
+#include "llvm/Target/MRegisterInfo.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/ADT/Statistic.h"
@@ -49,7 +51,9 @@ namespace {
                                  MachineBasicBlock *NewDest);
     MachineBasicBlock *SplitMBBAt(MachineBasicBlock &CurMBB,
                                   MachineBasicBlock::iterator BBI1);
-        
+
+    const MRegisterInfo *RegInfo;
+    RegScavenger *RS;
     // Branch optzn.
     bool OptimizeBranches(MachineFunction &MF);
     void OptimizeBlock(MachineBasicBlock *MBB);
@@ -68,6 +72,7 @@ FunctionPass *llvm::createBranchFoldingPass() { return new BranchFolder(); }
 /// function, updating the CFG.
 void BranchFolder::RemoveDeadBlock(MachineBasicBlock *MBB) {
   assert(MBB->pred_empty() && "MBB must be dead!");
+  DOUT << "\nRemoving MBB: " << *MBB;
   
   MachineFunction *MF = MBB->getParent();
   // drop all successors.
@@ -94,6 +99,9 @@ bool BranchFolder::runOnMachineFunction(MachineFunction &MF) {
   TII = MF.getTarget().getInstrInfo();
   if (!TII) return false;
 
+  RegInfo = MF.getTarget().getRegisterInfo();
+  RS = RegInfo->requiresRegisterScavenging(MF) ? new RegScavenger() : NULL;
+
   MMI = getAnalysisToUpdate<MachineModuleInfo>();
   
   bool EverMadeChange = false;
@@ -152,6 +160,7 @@ bool BranchFolder::runOnMachineFunction(MachineFunction &MF) {
       }
   }
   
+  delete RS;
   return EverMadeChange;
 }
 
@@ -279,6 +288,19 @@ MachineBasicBlock *BranchFolder::SplitMBBAt(MachineBasicBlock &CurMBB,
   
   // Splice the code over.
   NewMBB->splice(NewMBB->end(), &CurMBB, BBI1, CurMBB.end());
+
+  // For targets that use the register scavenger, we must maintain LiveIns.
+  if (RS) {
+    RS->enterBasicBlock(&CurMBB);
+    if (!CurMBB.empty())
+      RS->forward(prior(CurMBB.end()));
+    BitVector RegsLiveAtExit(RegInfo->getNumRegs());
+    RS->getRegsUsed(RegsLiveAtExit, false);
+    for (unsigned int i=0, e=RegInfo->getNumRegs(); i!=e; i++)
+      if (RegsLiveAtExit[i])
+        NewMBB->addLiveIn(i);
+  }
+
   return NewMBB;
 }
 
@@ -431,6 +453,9 @@ bool BranchFolder::TailMergeBlocks(MachineFunction &MF) {
 bool BranchFolder::OptimizeBranches(MachineFunction &MF) {
   MadeChange = false;
   
+  // Make sure blocks are numbered in order
+  MF.RenumberBlocks();
+
   for (MachineFunction::iterator I = ++MF.begin(), E = MF.end(); I != E; ) {
     MachineBasicBlock *MBB = I++;
     OptimizeBlock(MBB);
@@ -482,6 +507,8 @@ static bool CorrectExtraCFGEdges(MachineBasicBlock &MBB,
     } else if (*SI == DestB) {
       DestB = 0;
       ++SI;
+    } else if ((*SI)->isLandingPad()) {
+      ++SI;
     } else {
       // Otherwise, this is a superfluous edge, remove it.
       MBB.removeSuccessor(SI);
@@ -849,7 +876,10 @@ void BranchFolder::OptimizeBlock(MachineBasicBlock *MBB) {
                       PriorTBB, PriorFBB, PriorCond)) {
     // Now we know that there was no fall-through into this block, check to
     // see if it has a fall-through into its successor.
-    if (!CanFallThrough(MBB, CurUnAnalyzable, CurTBB, CurFBB, CurCond)) {
+    bool CurFallsThru = CanFallThrough(MBB, CurUnAnalyzable, CurTBB, CurFBB, 
+                                            CurCond);
+
+    if (!MBB->isLandingPad()) {
       // Check all the predecessors of this block.  If one of them has no fall
       // throughs, move this block right after it.
       for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
@@ -857,14 +887,26 @@ void BranchFolder::OptimizeBlock(MachineBasicBlock *MBB) {
         // Analyze the branch at the end of the pred.
         MachineBasicBlock *PredBB = *PI;
         MachineFunction::iterator PredFallthrough = PredBB; ++PredFallthrough;
-        std::vector<MachineOperand> PredCond;
-        if (PredBB != MBB && !CanFallThrough(PredBB)) {
+        if (PredBB != MBB && !CanFallThrough(PredBB)
+            && (!CurFallsThru || MBB->getNumber() >= PredBB->getNumber())) {
+          // If the current block doesn't fall through, just move it.
+          // If the current block can fall through and does not end with a
+          // conditional branch, we need to append an unconditional jump to 
+          // the (current) next block.  To avoid a possible compile-time
+          // infinite loop, move blocks only backward in this case.
+          if (CurFallsThru) {
+            MachineBasicBlock *NextBB = next(MachineFunction::iterator(MBB));
+            CurCond.clear();
+            TII->InsertBranch(*MBB, NextBB, 0, CurCond);
+          }
           MBB->moveAfter(PredBB);
           MadeChange = true;
           return OptimizeBlock(MBB);
         }
       }
+    }
         
+    if (!CurFallsThru) {
       // Check all successors to see if we can move this block before it.
       for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
            E = MBB->succ_end(); SI != E; ++SI) {