Disable load width reduction xform of variant (zext (truncate load x)) for
[oota-llvm.git] / lib / CodeGen / BranchFolding.cpp
index 6c0434180e26be7c511837155e901b93021f0df9..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);
@@ -67,7 +71,7 @@ FunctionPass *llvm::createBranchFoldingPass() { return new BranchFolder(); }
 /// RemoveDeadBlock - Remove the specified dead machine basic block from the
 /// function, updating the CFG.
 void BranchFolder::RemoveDeadBlock(MachineBasicBlock *MBB) {
-  assert(!MBB->isAccessable() && "MBB must be dead!");
+  assert(MBB->pred_empty() && "MBB must be dead!");
   DOUT << "\nRemoving MBB: " << *MBB;
   
   MachineFunction *MF = MBB->getParent();
@@ -95,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;
@@ -153,6 +160,7 @@ bool BranchFolder::runOnMachineFunction(MachineFunction &MF) {
       }
   }
   
+  delete RS;
   return EverMadeChange;
 }
 
@@ -280,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;
 }
 
@@ -440,7 +461,7 @@ bool BranchFolder::OptimizeBranches(MachineFunction &MF) {
     OptimizeBlock(MBB);
     
     // If it is dead, remove it.
-    if (!MBB->isAccessable()) {
+    if (MBB->pred_empty()) {
       RemoveDeadBlock(MBB);
       MadeChange = true;
       ++NumDeadBlocks;
@@ -618,14 +639,14 @@ void BranchFolder::OptimizeBlock(MachineBasicBlock *MBB) {
   // explicitly.
   if (MBB->empty()) {
     // Dead block?  Leave for cleanup later.
-    if (!MBB->isAccessable()) return;
+    if (MBB->pred_empty()) return;
     
     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->isAccessable()) {
+      while (!MBB->pred_empty()) {
         MachineBasicBlock *Pred = *(MBB->pred_end()-1);
         ReplaceUsesOfBlockWith(Pred, MBB, FallThrough, TII);
       }