Now that we use an ilist of machine instructions, iterators are more robust
[oota-llvm.git] / lib / CodeGen / PHIElimination.cpp
index 324f4c4e853c44b6b4852fad2e60b41a3952481d..ffde4beaf0a221c12a867f78ff15cc65aa92a75b 100644 (file)
@@ -20,7 +20,6 @@
 #include "llvm/CodeGen/LiveVariables.h"
 #include "llvm/Target/TargetInstrInfo.h"
 #include "llvm/Target/TargetMachine.h"
-#include "llvm/Support/CFG.h"
 #include "Support/STLExtras.h"
 using namespace llvm;
 
@@ -69,6 +68,13 @@ bool PNE::EliminatePHINodes(MachineFunction &MF, MachineBasicBlock &MBB) {
   const TargetInstrInfo &MII = MF.getTarget().getInstrInfo();
   const MRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
 
+  // Get an iterator to the first instruction after the last PHI node (this may
+  // allso be the end of the basic block).
+  MachineBasicBlock::iterator AfterPHIsIt = MBB.begin();
+  while (AfterPHIsIt != MBB.end() &&
+         AfterPHIsIt->getOpcode() == TargetInstrInfo::PHI)
+    ++AfterPHIsIt;    // Skip over all of the PHI nodes...
+
   while (MBB.front().getOpcode() == TargetInstrInfo::PHI) {
     // Unlink the PHI node from the basic block... but don't delete the PHI yet
     MachineInstr *MI = MBB.remove(MBB.begin());
@@ -86,15 +92,11 @@ bool PNE::EliminatePHINodes(MachineFunction &MF, MachineBasicBlock &MBB) {
     // after any remaining phi nodes) which copies the new incoming register
     // into the phi node destination.
     //
-    MachineBasicBlock::iterator AfterPHIsIt = MBB.begin();
-    while (AfterPHIsIt != MBB.end() &&
-           AfterPHIsIt->getOpcode() == TargetInstrInfo::PHI)
-      ++AfterPHIsIt;    // Skip over all of the PHI nodes...
     RegInfo->copyRegToReg(MBB, AfterPHIsIt, DestReg, IncomingReg, RC);
     
     // Update live variable information if there is any...
     if (LV) {
-      MachineInstr *PHICopy = --AfterPHIsIt;
+      MachineInstr *PHICopy = prior(AfterPHIsIt);
 
       // Add information to LiveVariables to know that the incoming value is
       // killed.  Note that because the value is defined in several places (once
@@ -195,14 +197,12 @@ bool PNE::EliminatePHINodes(MachineFunction &MF, MachineBasicBlock &MBB) {
           // at an appropriate point later.
           //
           bool ValueIsLive = false;
-          const BasicBlock *BB = opBlock.getBasicBlock();
-          for (succ_const_iterator SI = succ_begin(BB), E = succ_end(BB);
-               SI != E && !ValueIsLive; ++SI) {
-            const std::pair<MachineBasicBlock*, unsigned> &
-              SuccInfo = LV->getBasicBlockInfo(*SI);
+          for (MachineBasicBlock::succ_iterator SI = opBlock.succ_begin(),
+                 E = opBlock.succ_end(); SI != E && !ValueIsLive; ++SI) {
+            MachineBasicBlock *MBB = *SI;
             
             // Is it alive in this successor?
-            unsigned SuccIdx = SuccInfo.second;
+            unsigned SuccIdx = LV->getMachineBasicBlockIndex(MBB);
             if (SuccIdx < InRegVI.AliveBlocks.size() &&
                 InRegVI.AliveBlocks[SuccIdx]) {
               ValueIsLive = true;
@@ -210,7 +210,6 @@ bool PNE::EliminatePHINodes(MachineFunction &MF, MachineBasicBlock &MBB) {
             }
             
             // Is it killed in this successor?
-            MachineBasicBlock *MBB = SuccInfo.first;
             for (unsigned i = 0, e = InRegVI.Kills.size(); i != e; ++i)
               if (InRegVI.Kills[i].first == MBB) {
                 ValueIsLive = true;