Use higher level methods, don't use TargetInstrDescriptors directly!
[oota-llvm.git] / lib / Target / SparcV9 / SparcV9PeepholeOpts.cpp
index ee749efe8eba781990cbb004d9b2a6adb7b2553c..21cc5d79e12d97c15f03c3754b83525128886381 100644 (file)
@@ -1,16 +1,12 @@
-// $Id$ -*-c++-*-
-//***************************************************************************
-// File:
-//     PeepholeOpts.h
+//===-- PeepholeOpts.cpp --------------------------------------------------===//
 // 
-// Purpose:
-//     Support for performing several peephole opts in one or a few passes
-//      over the machine code of a method.
-//**************************************************************************/
-
+// Support for performing several peephole opts in one or a few passes over the
+// machine code of a method.
+//
+//===----------------------------------------------------------------------===//
 
 #include "llvm/CodeGen/PeepholeOpts.h"
-#include "llvm/CodeGen/MachineCodeForBasicBlock.h"
+#include "llvm/CodeGen/MachineFunction.h"
 #include "llvm/CodeGen/MachineInstr.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/Target/MachineInstrInfo.h"
 #include "llvm/BasicBlock.h"
 #include "llvm/Pass.h"
 
-
-//********************* Internal Class Declarations ************************/
-
-
 //************************* Internal Functions *****************************/
 
 inline void
-DeleteInstruction(MachineCodeForBasicBlock& mvec,
-                  MachineCodeForBasicBlock::iterator& BBI,
+DeleteInstruction(MachineBasicBlock& mvec,
+                  MachineBasicBlock::iterator& BBI,
                   const TargetMachine& target)
 {
   // Check if this instruction is in a delay slot of its predecessor.
-  // If so, replace this instruction with a nop, else just delete it.
-  // By replacing in place, we save having to update the I-I maps.
   if (BBI != mvec.begin())
     {
       const MachineInstrInfo& mii = target.getInstrInfo();
       MachineInstr* predMI = *(BBI-1);
       if (unsigned ndelay = mii.getNumDelaySlots(predMI->getOpCode()))
         {
+          // This instruction is in a delay slot of its predecessor, so
+          // replace it with a nop. By replacing in place, we save having
+          // to update the I-I maps.
+          // 
           assert(ndelay == 1 && "Not yet handling multiple-delay-slot targets");
           (*BBI)->replace(mii.getNOPOpCode(), 0);
+          return;
         }
     }
-  else
-    {
-      mvec.erase(BBI);
-      BBI = mvec.end();
-    }
+  
+  // The instruction is not in a delay slot, so we can simply erase it.
+  mvec.erase(BBI);
+  BBI = mvec.end();
 }
 
 //******************* Individual Peephole Optimizations ********************/
 
 
 inline bool
-RemoveUselessCopies(MachineCodeForBasicBlock& mvec,
-                    MachineCodeForBasicBlock::iterator& BBI,
+RemoveUselessCopies(MachineBasicBlock& mvec,
+                    MachineBasicBlock::iterator& BBI,
                     const TargetMachine& target)
 {
   if (target.getOptInfo().IsUselessCopy(*BBI))
@@ -70,8 +64,8 @@ RemoveUselessCopies(MachineCodeForBasicBlock& mvec,
 
 class PeepholeOpts: public BasicBlockPass {
   const TargetMachine ⌖
-  bool visit(MachineCodeForBasicBlock& mvec,
-             MachineCodeForBasicBlock::iterator BBI) const;
+  bool visit(MachineBasicBlock& mvec,
+             MachineBasicBlock::iterator BBI) const;
 public:
   PeepholeOpts(const TargetMachine &T): target(T) { }
   bool runOnBasicBlock(BasicBlock &BB); // apply this pass to each BB
@@ -88,8 +82,8 @@ X("peephole", "Peephole Optimization", createPeepholeOptsPass);
  * instruction before MI, but not 
  */
 bool
-PeepholeOpts::visit(MachineCodeForBasicBlock& mvec,
-                    MachineCodeForBasicBlock::iterator BBI) const
+PeepholeOpts::visit(MachineBasicBlock& mvec,
+                    MachineBasicBlock::iterator BBI) const
 {
   bool changed = false;
 
@@ -106,16 +100,25 @@ bool
 PeepholeOpts::runOnBasicBlock(BasicBlock &BB)
 {
   // Get the machine instructions for this BB
-  MachineCodeForBasicBlock& mvec = MachineCodeForBasicBlock::get(&BB);
+  // FIXME: MachineBasicBlock::get() is deprecated, hence inlining the function
+  const Function *F = BB.getParent();
+  MachineFunction &MF = MachineFunction::get(F);
+  MachineBasicBlock *MBB = NULL;
+  for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
+    if (I->getBasicBlock() == &BB)
+      MBB = I;
+  }
+  assert(MBB && "MachineBasicBlock object not found for specified block!");
+  MachineBasicBlock &mvec = *MBB;
 
   // Iterate over all machine instructions in the BB
   // Use a reverse iterator to allow deletion of MI or any instruction after it.
   // Insertions or deletions *before* MI are not safe.
   // 
-  for (MachineCodeForBasicBlock::reverse_iterator RI=mvec.rbegin(),
+  for (MachineBasicBlock::reverse_iterator RI=mvec.rbegin(),
          RE=mvec.rend(); RI != RE; )
     {
-      MachineCodeForBasicBlock::iterator BBI = RI.base()-1; // save before incr
+      MachineBasicBlock::iterator BBI = RI.base()-1; // save before incr
       ++RI;             // pre-increment to delete MI or after it
       visit(mvec, BBI);
     }