Apply patch review feedback.
[oota-llvm.git] / lib / CodeGen / PreAllocSplitting.cpp
index 581320a3961a4382c7d26fffe234579e3a17e63f..97d4728348e561c15d99503f933b20e2c35b2b8c 100644 (file)
@@ -15,6 +15,7 @@
 //===----------------------------------------------------------------------===//
 
 #define DEBUG_TYPE "pre-alloc-split"
+#include "VirtRegMap.h"
 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
 #include "llvm/CodeGen/LiveStackAnalysis.h"
 #include "llvm/CodeGen/MachineDominators.h"
@@ -57,6 +58,7 @@ namespace {
     MachineRegisterInfo   *MRI;
     LiveIntervals         *LIs;
     LiveStacks            *LSs;
+    VirtRegMap            *VRM;
 
     // Barrier - Current barrier being processed.
     MachineInstr          *Barrier;
@@ -100,8 +102,10 @@ namespace {
         AU.addPreservedID(PHIEliminationID);
       AU.addRequired<MachineDominatorTree>();
       AU.addRequired<MachineLoopInfo>();
+      AU.addRequired<VirtRegMap>();
       AU.addPreserved<MachineDominatorTree>();
       AU.addPreserved<MachineLoopInfo>();
+      AU.addPreserved<VirtRegMap>();
       MachineFunctionPass::getAnalysisUsage(AU);
     }
     
@@ -228,68 +232,38 @@ PreAllocSplitting::findSpillPoint(MachineBasicBlock *MBB, MachineInstr *MI,
                                   unsigned &SpillIndex) {
   MachineBasicBlock::iterator Pt = MBB->begin();
 
-  // Go top down if RefsInMBB is empty.
-  if (RefsInMBB.empty() && !DefMI) {
-    MachineBasicBlock::iterator MII = MBB->begin();
-    MachineBasicBlock::iterator EndPt = MI;
+  MachineBasicBlock::iterator MII = MI;
+  MachineBasicBlock::iterator EndPt = DefMI
+    ? MachineBasicBlock::iterator(DefMI) : MBB->begin();
     
-    if (MII == EndPt) return Pt;
+  while (MII != EndPt && !RefsInMBB.count(MII) &&
+         MII->getOpcode() != TRI->getCallFrameSetupOpcode())
+    --MII;
+  if (MII == EndPt || RefsInMBB.count(MII)) return Pt;
     
-    do {
-      ++MII;
-      unsigned Index = LIs->getInstructionIndex(MII);
-      unsigned Gap = LIs->findGapBeforeInstr(Index);
-      
-      // We can't insert the spill between the barrier (a call), and its
-      // corresponding call frame setup/teardown.
-      if (prior(MII)->getOpcode() == TRI->getCallFrameSetupOpcode()) {
-        bool reachedBarrier = false;
-        do {
-          if (MII == EndPt) {
-            reachedBarrier = true;
-            break;
-          }
-          ++MII;
-        } while (MII->getOpcode() != TRI->getCallFrameDestroyOpcode());
-        
-        if (reachedBarrier) break;
-      } else if (Gap) {
-        Pt = MII;
-        SpillIndex = Gap;
-        break;
-      }
-    } while (MII != EndPt);
-  } else {
-    MachineBasicBlock::iterator MII = MI;
-    MachineBasicBlock::iterator EndPt = DefMI
-      ? MachineBasicBlock::iterator(DefMI) : MBB->begin();
+  while (MII != EndPt && !RefsInMBB.count(MII)) {
+    unsigned Index = LIs->getInstructionIndex(MII);
     
-    while (MII != EndPt && !RefsInMBB.count(MII)) {
-      unsigned Index = LIs->getInstructionIndex(MII);
-      
-      // We can't insert the spill between the barrier (a call), and its
-      // corresponding call frame setup.
-      if (prior(MII)->getOpcode() == TRI->getCallFrameSetupOpcode()) {
+    // We can't insert the spill between the barrier (a call), and its
+    // corresponding call frame setup.
+    if (MII->getOpcode() == TRI->getCallFrameDestroyOpcode()) {
+      while (MII->getOpcode() != TRI->getCallFrameSetupOpcode()) {
         --MII;
-        continue;
-      } if (MII->getOpcode() == TRI->getCallFrameDestroyOpcode()) {
-        bool reachedBarrier = false;
-        while (MII->getOpcode() != TRI->getCallFrameSetupOpcode()) {
-          --MII;
-          if (MII == EndPt) {
-            reachedBarrier = true;
-            break;
-          }
+        if (MII == EndPt) {
+          return Pt;
         }
-        
-        if (reachedBarrier) break;
-        else continue;
-      } else if (LIs->hasGapBeforeInstr(Index)) {
-        Pt = MII;
-        SpillIndex = LIs->findGapBeforeInstr(Index, true);
       }
-      --MII;
+      continue;
+    } else if (LIs->hasGapBeforeInstr(Index)) {
+      Pt = MII;
+      SpillIndex = LIs->findGapBeforeInstr(Index, true);
     }
+    
+    if (RefsInMBB.count(MII))
+      return Pt;
+    
+    
+    --MII;
   }
 
   return Pt;
@@ -307,81 +281,44 @@ PreAllocSplitting::findRestorePoint(MachineBasicBlock *MBB, MachineInstr *MI,
   // FIXME: Allow spill to be inserted to the beginning of the mbb. Update mbb
   // begin index accordingly.
   MachineBasicBlock::iterator Pt = MBB->end();
-  unsigned EndIdx = LIs->getMBBEndIdx(MBB);
+  MachineBasicBlock::iterator EndPt = MBB->getFirstTerminator();
 
-  // Go bottom up if RefsInMBB is empty and the end of the mbb isn't beyond
-  // the last index in the live range.
-  if (RefsInMBB.empty() && LastIdx >= EndIdx) {
-    MachineBasicBlock::iterator MII = MBB->getFirstTerminator();
-    MachineBasicBlock::iterator EndPt = MI;
-    
-    if (MII == EndPt) return Pt;
-    
-    --MII;
-    do {
-      unsigned Index = LIs->getInstructionIndex(MII);
-      unsigned Gap = LIs->findGapBeforeInstr(Index);
-      
-      // We can't insert a restore between the barrier (a call) and its 
-      // corresponding call frame teardown.
-      if (MII->getOpcode() == TRI->getCallFrameDestroyOpcode()) {
-        bool reachedBarrier = false;
-        while (MII->getOpcode() != TRI->getCallFrameSetupOpcode()) {
-          --MII;
-          if (MII == EndPt) {
-            reachedBarrier = true;
-            break;
-          }
-        }
-        
-        if (reachedBarrier) break;
-        else continue;
-      } else if (Gap) {
-        Pt = MII;
-        RestoreIndex = Gap;
-        break;
-      }
-      
-      --MII;
-    } while (MII != EndPt);
-  } else {
-    MachineBasicBlock::iterator MII = MI;
-    MII = ++MII;
-    
-    // FIXME: Limit the number of instructions to examine to reduce
-    // compile time?
-    while (MII != MBB->getFirstTerminator()) {
-      unsigned Index = LIs->getInstructionIndex(MII);
-      if (Index > LastIdx)
-        break;
-      unsigned Gap = LIs->findGapBeforeInstr(Index);
+  // We start at the call, so walk forward until we find the call frame teardown
+  // since we can't insert restores before that.  Bail if we encounter a use
+  // during this time.
+  MachineBasicBlock::iterator MII = MI;
+  if (MII == EndPt) return Pt;
+  
+  while (MII != EndPt && !RefsInMBB.count(MII) &&
+         MII->getOpcode() != TRI->getCallFrameDestroyOpcode())
+    ++MII;
+  if (MII == EndPt || RefsInMBB.count(MII)) return Pt;
+  ++MII;
+  
+  // FIXME: Limit the number of instructions to examine to reduce
+  // compile time?
+  while (MII != EndPt) {
+    unsigned Index = LIs->getInstructionIndex(MII);
+    if (Index > LastIdx)
+      break;
+    unsigned Gap = LIs->findGapBeforeInstr(Index);
       
-      // We can't insert a restore between the barrier (a call) and its 
-      // corresponding call frame teardown.
-      if (MII->getOpcode() == TRI->getCallFrameDestroyOpcode()) {
+    // We can't insert a restore between the barrier (a call) and its 
+    // corresponding call frame teardown.
+    if (MII->getOpcode() == TRI->getCallFrameSetupOpcode()) {
+      do {
+        if (MII == EndPt || RefsInMBB.count(MII)) return Pt;
         ++MII;
-        continue;
-      } else if (prior(MII)->getOpcode() == TRI->getCallFrameSetupOpcode()) {
-        bool reachedBarrier = false;
-        do {
-          if (MII == MBB->getFirstTerminator() || RefsInMBB.count(MII)) {
-            reachedBarrier = true;
-            break;
-          }
-          
-          ++MII;
-        } while (MII->getOpcode() != TRI->getCallFrameDestroyOpcode());
-        
-        if (reachedBarrier) break;
-      } else if (Gap) {
-        Pt = MII;
-        RestoreIndex = Gap;
-      }
-      
-      if (RefsInMBB.count(MII))
-        break;
-      ++MII;
+      } while (MII->getOpcode() != TRI->getCallFrameDestroyOpcode());
+    } else if (Gap) {
+      Pt = MII;
+      RestoreIndex = Gap;
     }
+    
+    if (RefsInMBB.count(MII))
+      return Pt;
+    
+    ++MII;
   }
 
   return Pt;
@@ -402,7 +339,7 @@ int PreAllocSplitting::CreateSpillStackSlot(unsigned Reg,
   }
 
   // Create live interval for stack slot.
-  CurrSLI = &LSs->getOrCreateInterval(SS);
+  CurrSLI = &LSs->getOrCreateInterval(SS, RC);
   if (CurrSLI->hasAtLeastOneValue())
     CurrSValNo = CurrSLI->getValNumInfo(0);
   else
@@ -866,7 +803,7 @@ void PreAllocSplitting::RenumberValno(VNInfo* VN) {
       MachineInstr* MI = LIs->getInstructionFromIndex(*KI);
       unsigned DefIdx = MI->findRegisterDefOperandIdx(CurrLI->reg);
       if (DefIdx == ~0U) continue;
-      if (MI->isRegReDefinedByTwoAddr(DefIdx)) {
+      if (MI->isRegTiedToUseOperand(DefIdx)) {
         VNInfo* NextVN =
                      CurrLI->findDefinedVNInfo(LiveIntervals::getDefIndex(*KI));
         if (NextVN == OldVN) continue;
@@ -917,6 +854,9 @@ void PreAllocSplitting::RenumberValno(VNInfo* VN) {
     MO.setReg(NewVReg);
   }
   
+  // Grow the VirtRegMap, since we've created a new vreg.
+  VRM->grow();
+  
   // The renumbered vreg shares a stack slot with the old register.
   if (IntervalSSMap.count(CurrLI->reg))
     IntervalSSMap[NewVReg] = IntervalSSMap[CurrLI->reg];
@@ -986,8 +926,7 @@ MachineInstr* PreAllocSplitting::FoldSpill(unsigned vreg,
   if (I != IntervalSSMap.end()) {
     SS = I->second;
   } else {
-    SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment());
-    
+    SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment());    
   }
   
   MachineInstr* FMI = TII->foldMemoryOperand(*MBB->getParent(),
@@ -999,7 +938,7 @@ MachineInstr* PreAllocSplitting::FoldSpill(unsigned vreg,
     ++NumFolds;
     
     IntervalSSMap[vreg] = SS;
-    CurrSLI = &LSs->getOrCreateInterval(SS);
+    CurrSLI = &LSs->getOrCreateInterval(SS, RC);
     if (CurrSLI->hasAtLeastOneValue())
       CurrSValNo = CurrSLI->getValNumInfo(0);
     else
@@ -1274,7 +1213,7 @@ unsigned PreAllocSplitting::getNumberOfNonSpills(
       NonSpills++;
     
     int DefIdx = (*UI)->findRegisterDefOperandIdx(Reg);
-    if (DefIdx != -1 && (*UI)->isRegReDefinedByTwoAddr(DefIdx))
+    if (DefIdx != -1 && (*UI)->isRegTiedToUseOperand(DefIdx))
       FeedsTwoAddr = true;
   }
   
@@ -1512,6 +1451,7 @@ bool PreAllocSplitting::runOnMachineFunction(MachineFunction &MF) {
   MRI    = &MF.getRegInfo();
   LIs    = &getAnalysis<LiveIntervals>();
   LSs    = &getAnalysis<LiveStacks>();
+  VRM    = &getAnalysis<VirtRegMap>();
 
   bool MadeChange = false;