Disable load width reduction xform of variant (zext (truncate load x)) for
[oota-llvm.git] / lib / CodeGen / BranchFolding.cpp
index 9145418cc391559752df8f421fedcf51f3177cb5..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;
 }
 
@@ -485,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);
@@ -855,28 +879,30 @@ void BranchFolder::OptimizeBlock(MachineBasicBlock *MBB) {
     bool CurFallsThru = CanFallThrough(MBB, CurUnAnalyzable, CurTBB, CurFBB, 
                                             CurCond);
 
-    // 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(),
-         E = MBB->pred_end(); PI != E; ++PI) {
-      // Analyze the branch at the end of the pred.
-      MachineBasicBlock *PredBB = *PI;
-      MachineFunction::iterator PredFallthrough = PredBB; ++PredFallthrough;
-      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);
+    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(),
+           E = MBB->pred_end(); PI != E; ++PI) {
+        // Analyze the branch at the end of the pred.
+        MachineBasicBlock *PredBB = *PI;
+        MachineFunction::iterator PredFallthrough = PredBB; ++PredFallthrough;
+        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);
         }
-        MBB->moveAfter(PredBB);
-        MadeChange = true;
-        return OptimizeBlock(MBB);
       }
     }