Add new optional getPassName() virtual function that a Pass can override
[oota-llvm.git] / lib / Target / SparcV9 / InstrSched / InstrScheduling.cpp
index e67df86c0369ad783c0442cb8f824112c802ba78..b0422797475193f48454333b4e5fb23dbe22d28e 100644 (file)
@@ -1,82 +1,41 @@
-// $Id$
-//***************************************************************************
-// File:
-//     InstrScheduling.cpp
-// 
-// Purpose:
-//     
-// History:
-//     7/23/01  -  Vikram Adve  -  Created
-//***************************************************************************
+//===- InstrScheduling.cpp - Generic Instruction Scheduling support -------===//
+//
+// This file implements the llvm/CodeGen/InstrScheduling.h interface, along with
+// generic support routines for instruction scheduling.
+//
+//===----------------------------------------------------------------------===//
 
 #include "llvm/CodeGen/InstrScheduling.h"
-#include "llvm/CodeGen/SchedPriorities.h"
-#include "llvm/Analysis/LiveVar/BBLiveVar.h"
-#include "llvm/CodeGen/TargetMachine.h"
 #include "llvm/CodeGen/MachineInstr.h"
-#include "llvm/Support/CommandLine.h"
+#include "llvm/CodeGen/MachineCodeForInstruction.h"
+#include "llvm/CodeGen/MachineCodeForMethod.h"
+#include "llvm/Analysis/LiveVar/FunctionLiveVarInfo.h" // FIXME: Remove when modularized better
+#include "llvm/Target/TargetMachine.h"
+#include "llvm/BasicBlock.h"
 #include "llvm/Instruction.h"
-#include <hash_set>
+#include "SchedPriorities.h"
+#include <ext/hash_set>
 #include <algorithm>
 #include <iterator>
+#include <iostream>
+using std::cerr;
+using std::vector;
+
+//************************* External Data Types *****************************/
 
 cl::Enum<enum SchedDebugLevel_t> SchedDebugLevel("dsched", cl::NoFlags,
   "enable instruction scheduling debugging information",
   clEnumValN(Sched_NoDebugInfo,      "n", "disable debug output"),
+  clEnumValN(Sched_Disable,        "off", "disable instruction scheduling"),
   clEnumValN(Sched_PrintMachineCode, "y", "print machine code after scheduling"),
   clEnumValN(Sched_PrintSchedTrace,  "t", "print trace of scheduling actions"),
   clEnumValN(Sched_PrintSchedGraphs, "g", "print scheduling graphs"), 0);
 
 
-//************************* Forward Declarations ***************************/
+//************************* Internal Data Types *****************************/
 
 class InstrSchedule;
 class SchedulingManager;
-class DelaySlotInfo;
-
-static void    ForwardListSchedule     (SchedulingManager& S);
-
-static void    RecordSchedule          (const BasicBlock* bb,
-                                        const SchedulingManager& S);
-
-static unsigned        ChooseOneGroup          (SchedulingManager& S);
-
-static void    MarkSuccessorsReady     (SchedulingManager& S,
-                                        const SchedGraphNode* node);
-
-static unsigned        FindSlotChoices         (SchedulingManager& S,
-                                        DelaySlotInfo*& getDelaySlotInfo);
-
-static void    AssignInstructionsToSlots(class SchedulingManager& S,
-                                         unsigned maxIssue);
-
-static void    ScheduleInstr           (class SchedulingManager& S,
-                                        const SchedGraphNode* node,
-                                        unsigned int slotNum,
-                                        cycles_t curTime);
-
-static bool    ViolatesMinimumGap      (const SchedulingManager& S,
-                                        MachineOpCode opCode,
-                                        const cycles_t inCycle);
-
-static bool    ConflictsWithChoices    (const SchedulingManager& S,
-                                        MachineOpCode opCode);
-
-static void    ChooseInstructionsForDelaySlots(SchedulingManager& S,
-                                        const BasicBlock* bb,
-                                        SchedGraph* graph);
-
-static bool    NodeCanFillDelaySlot    (const SchedulingManager& S,
-                                        const SchedGraphNode* node,
-                                        const SchedGraphNode* brNode,
-                                        bool nodeIsPredecessor);
-
-static void    MarkNodeForDelaySlot    (SchedulingManager& S,
-                                        SchedGraphNode* node,
-                                        const SchedGraphNode* brNode,
-                                        bool nodeIsPredecessor);
-
-//************************* Internal Data Types *****************************/
 
 
 //----------------------------------------------------------------------
@@ -198,7 +157,7 @@ public: // accessor functions to query chosen schedule
   }
   
   inline InstrGroup*   getIGroup       (cycles_t c) {
-    if (c >= groups.size())
+    if ((unsigned)c >= groups.size())
       groups.resize(c+1);
     if (groups[c] == NULL)
       groups[c] = new InstrGroup(nslots);
@@ -206,7 +165,7 @@ public: // accessor functions to query chosen schedule
   }
   
   inline const InstrGroup* getIGroup   (cycles_t c) const {
-    assert(c < groups.size());
+    assert((unsigned)c < groups.size());
     return groups[c];
   }
   
@@ -371,7 +330,7 @@ public:
     delayedNodeSlotNum = slotNum;
   }
   
-  void         scheduleDelayedNode     (SchedulingManager& S);
+  unsigned     scheduleDelayedNode     (SchedulingManager& S);
 };
 
 
@@ -392,18 +351,22 @@ private:
   unsigned int totalInstrCount;
   cycles_t curTime;
   cycles_t nextEarliestIssueTime;              // next cycle we can issue
-  vector<hash_set<const SchedGraphNode*> > choicesForSlot; // indexed by slot#
+  vector<std::hash_set<const SchedGraphNode*> > choicesForSlot; // indexed by slot#
   vector<const SchedGraphNode*> choiceVec;     // indexed by node ptr
   vector<int> numInClass;                      // indexed by sched class
   vector<cycles_t> nextEarliestStartTime;      // indexed by opCode
-  hash_map<const SchedGraphNode*, DelaySlotInfo*> delaySlotInfoForBranches;
+  std::hash_map<const SchedGraphNode*, DelaySlotInfo*> delaySlotInfoForBranches;
                                                // indexed by branch node ptr 
   
 public:
-  /*ctor*/     SchedulingManager       (const TargetMachine& _target,
-                                        const SchedGraph* graph,
-                                        SchedPriorities& schedPrio);
-  /*dtor*/     ~SchedulingManager      () {}
+  SchedulingManager(const TargetMachine& _target, const SchedGraph* graph,
+                    SchedPriorities& schedPrio);
+  ~SchedulingManager() {
+    for (std::hash_map<const SchedGraphNode*,
+           DelaySlotInfo*>::iterator I = delaySlotInfoForBranches.begin(),
+           E = delaySlotInfoForBranches.end(); I != E; ++I)
+      delete I->second;
+  }
   
   //----------------------------------------------------------------------
   // Simplify access to the machine instruction info
@@ -458,7 +421,7 @@ public:
     return choiceVec[i];
   }
   
-  inline hash_set<const SchedGraphNode*>& getChoicesForSlot(unsigned slotNum) {
+  inline std::hash_set<const SchedGraphNode*>& getChoicesForSlot(unsigned slotNum) {
     assert(slotNum < nslots);
     return choicesForSlot[slotNum];
   }
@@ -467,7 +430,7 @@ public:
     // Append the instruction to the vector of choices for current cycle.
     // Increment numInClass[c] for the sched class to which the instr belongs.
     choiceVec.push_back(node);
-    const InstrSchedClass& sc = schedInfo.getSchedClass(node->getMachineInstr()->getOpCode());
+    const InstrSchedClass& sc = schedInfo.getSchedClass(node->getOpCode());
     assert(sc < (int) numInClass.size());
     numInClass[sc]++;
   }
@@ -521,7 +484,7 @@ public:
       choicesForSlot[s].erase(node);
     
     // and decrement the instr count for the sched class to which it belongs
-    const InstrSchedClass& sc = schedInfo.getSchedClass(node->getMachineInstr()->getOpCode());
+    const InstrSchedClass& sc = schedInfo.getSchedClass(node->getOpCode());
     assert(sc < (int) numInClass.size());
     numInClass[sc]--;
   }
@@ -533,30 +496,21 @@ public:
   inline DelaySlotInfo* getDelaySlotInfoForInstr(const SchedGraphNode* bn,
                                                 bool createIfMissing=false)
   {
-    DelaySlotInfo* dinfo;
-    hash_map<const SchedGraphNode*, DelaySlotInfo* >::const_iterator
+    std::hash_map<const SchedGraphNode*, DelaySlotInfo*>::const_iterator
       I = delaySlotInfoForBranches.find(bn);
-    if (I == delaySlotInfoForBranches.end())
-      {
-       if (createIfMissing)
-         {
-           dinfo = new DelaySlotInfo(bn,
-                         getInstrInfo().getNumDelaySlots(bn->getMachineInstr()->getOpCode()));
-           delaySlotInfoForBranches[bn] = dinfo;
-         }
-       else
-         dinfo = NULL;
-      }
-    else
-      dinfo = (*I).second;
-    
-    return dinfo;
+    if (I != delaySlotInfoForBranches.end())
+      return I->second;
+
+    if (!createIfMissing) return 0;
+
+    DelaySlotInfo *dinfo =
+      new DelaySlotInfo(bn, getInstrInfo().getNumDelaySlots(bn->getOpCode()));
+    return delaySlotInfoForBranches[bn] = dinfo;
   }
   
 private:
-  /*ctor*/     SchedulingManager       ();     // Disable: DO NOT IMPLEMENT.
-  void         updateEarliestStartTimes(const SchedGraphNode* node,
-                                        cycles_t schedTime);
+  SchedulingManager();     // DISABLED: DO NOT IMPLEMENT
+  void updateEarliestStartTimes(const SchedGraphNode* node, cycles_t schedTime);
 };
 
 
@@ -589,20 +543,20 @@ void
 SchedulingManager::updateEarliestStartTimes(const SchedGraphNode* node,
                                            cycles_t schedTime)
 {
-  if (schedInfo.numBubblesAfter(node->getMachineInstr()->getOpCode()) > 0)
+  if (schedInfo.numBubblesAfter(node->getOpCode()) > 0)
     { // Update next earliest time before which *nothing* can issue.
-      nextEarliestIssueTime = max(nextEarliestIssueTime,
-                 curTime + 1 + schedInfo.numBubblesAfter(node->getMachineInstr()->getOpCode()));
+      nextEarliestIssueTime = std::max(nextEarliestIssueTime,
+                 curTime + 1 + schedInfo.numBubblesAfter(node->getOpCode()));
     }
   
   const vector<MachineOpCode>*
-    conflictVec = schedInfo.getConflictList(node->getMachineInstr()->getOpCode());
+    conflictVec = schedInfo.getConflictList(node->getOpCode());
   
   if (conflictVec != NULL)
     for (unsigned i=0; i < conflictVec->size(); i++)
       {
        MachineOpCode toOp = (*conflictVec)[i];
-       cycles_t est = schedTime + schedInfo.getMinIssueGap(node->getMachineInstr()->getOpCode(),
+       cycles_t est = schedTime + schedInfo.getMinIssueGap(node->getOpCode(),
                                                            toOp);
        assert(toOp < (int) nextEarliestStartTime.size());
        if (nextEarliestStartTime[toOp] < est)
@@ -610,158 +564,63 @@ SchedulingManager::updateEarliestStartTimes(const SchedGraphNode* node,
       }
 }
 
-//************************* External Functions *****************************/
-
+//************************* Internal Functions *****************************/
 
-//---------------------------------------------------------------------------
-// Function: ScheduleInstructionsWithSSA
-// 
-// Purpose:
-//   Entry point for instruction scheduling on SSA form.
-//   Schedules the machine instructions generated by instruction selection.
-//   Assumes that register allocation has not been done, i.e., operands
-//   are still in SSA form.
-//---------------------------------------------------------------------------
 
-bool
-ScheduleInstructionsWithSSA(Method* method,
-                           const TargetMachine &target)
+static void
+AssignInstructionsToSlots(class SchedulingManager& S, unsigned maxIssue)
 {
-  SchedGraphSet graphSet(method, target);      
-  
-  if (SchedDebugLevel >= Sched_PrintSchedGraphs)
-    {
-      cout << endl << "*** SCHEDULING GRAPHS FOR INSTRUCTION SCHEDULING"
-          << endl;
-      graphSet.dump();
-    }
-  
-  for (SchedGraphSet::const_iterator GI=graphSet.begin();
-       GI != graphSet.end(); ++GI)
-    {
-      SchedGraph* graph = (*GI).second;
-      const vector<const BasicBlock*>& bbvec = graph->getBasicBlocks();
-      assert(bbvec.size() == 1 && "Cannot schedule multiple basic blocks");
-      const BasicBlock* bb = bbvec[0];
-      
-      if (SchedDebugLevel >= Sched_PrintSchedTrace)
-       cout << endl << "*** TRACE OF INSTRUCTION SCHEDULING OPERATIONS\n\n";
-      
-      SchedPriorities schedPrio(method, graph);             // expensive!
-      SchedulingManager S(target, graph, schedPrio);
-      
-      ChooseInstructionsForDelaySlots(S, bb, graph); // modifies graph
-      
-      ForwardListSchedule(S);                       // computes schedule in S
-      
-      RecordSchedule((*GI).first, S);               // records schedule in BB
-    }
+  // find the slot to start from, in the current cycle
+  unsigned int startSlot = 0;
+  cycles_t curTime = S.getTime();
   
-  if (SchedDebugLevel >= Sched_PrintMachineCode)
-    {
-      cout << endl
-          << "*** Machine instructions after INSTRUCTION SCHEDULING" << endl;
-      PrintMachineInstructions(method);
-    }
+  assert(maxIssue > 0 && maxIssue <= S.nslots - startSlot);
   
-  return false;                                         // no reason to fail yet
-}
-
-
-// Check minimum gap requirements relative to instructions scheduled in
-// previous cycles.
-// Note that we do not need to consider `nextEarliestIssueTime' here because
-// that is also captured in the earliest start times for each opcode.
-// 
-static inline bool
-ViolatesMinimumGap(const SchedulingManager& S,
-                  MachineOpCode opCode,
-                  const cycles_t inCycle)
-{
-  return (inCycle < S.getEarliestStartTimeForOp(opCode));
-}
-
-
-// Check if the instruction would conflict with instructions already
-// chosen for the current cycle
-// 
-static inline bool
-ConflictsWithChoices(const SchedulingManager& S,
-                    MachineOpCode opCode)
-{
-  // Check if the instruction must issue by itself, and some feasible
-  // choices have already been made for this cycle
-  if (S.getNumChoices() > 0 && S.schedInfo.isSingleIssue(opCode))
-    return true;
+  // If only one instruction can be issued, do so.
+  if (maxIssue == 1)
+    for (unsigned s=startSlot; s < S.nslots; s++)
+      if (S.getChoicesForSlot(s).size() > 0)
+       {// found the one instruction
+         S.scheduleInstr(*S.getChoicesForSlot(s).begin(), s, curTime);
+         return;
+       }
   
-  // For each class that opCode belongs to, check if there are too many
-  // instructions of that class.
+  // Otherwise, choose from the choices for each slot
   // 
-  const InstrSchedClass sc = S.schedInfo.getSchedClass(opCode);
-  return (S.getNumChoicesInClass(sc) == S.schedInfo.getMaxIssueForClass(sc));
-}
-
-
-// Check if any issue restrictions would prevent the instruction from
-// being issued in the current cycle
-// 
-bool
-instrIsFeasible(const SchedulingManager& S,
-               MachineOpCode opCode)
-{
-  // skip the instruction if it cannot be issued due to issue restrictions
-  // caused by previously issued instructions
-  if (ViolatesMinimumGap(S, opCode, S.getTime()))
-    return false;
-  
-  // skip the instruction if it cannot be issued due to issue restrictions
-  // caused by previously chosen instructions for the current cycle
-  if (ConflictsWithChoices(S, opCode))
-    return false;
-  
-  return true;
-}
-
-//************************* Internal Functions *****************************/
-
-
-static void
-ForwardListSchedule(SchedulingManager& S)
-{
-  unsigned N;
-  const SchedGraphNode* node;
-  
-  S.schedPrio.initialize();
+  InstrGroup* igroup = S.isched.getIGroup(S.getTime());
+  assert(igroup != NULL && "Group creation failed?");
   
-  while ((N = S.schedPrio.getNumReady()) > 0)
+  // Find a slot that has only a single choice, and take it.
+  // If all slots have 0 or multiple choices, pick the first slot with
+  // choices and use its last instruction (just to avoid shifting the vector).
+  unsigned numIssued;
+  for (numIssued = 0; numIssued < maxIssue; numIssued++)
     {
-      // Choose one group of instructions for a cycle.  This will
-      // advance S.getTime() to the first cycle instructions can be issued.
-      // It may also schedule delay slot instructions in later cycles,
-      // but those are ignored here because they are outside the graph.
-      // 
-      unsigned numIssued = ChooseOneGroup(S);
-      assert(numIssued > 0 && "Deadlock in list scheduling algorithm?");
-      
-      // Notify the priority manager of scheduled instructions and mark
-      // any successors that may now be ready
-      // 
-      const InstrGroup* igroup = S.isched.getIGroup(S.getTime());
-      for (unsigned int s=0; s < S.nslots; s++)
-       if ((node = (*igroup)[s]) != NULL)
+      int chosenSlot = -1;
+      for (unsigned s=startSlot; s < S.nslots; s++)
+       if ((*igroup)[s] == NULL && S.getChoicesForSlot(s).size() == 1)
          {
-           S.schedPrio.issuedReadyNodeAt(S.getTime(), node);
-           MarkSuccessorsReady(S, node);
+           chosenSlot = (int) s;
+           break;
          }
       
-      // Move to the next the next earliest cycle for which
-      // an instruction can be issued, or the next earliest in which
-      // one will be ready, or to the next cycle, whichever is latest.
-      // 
-      S.updateTime(max(S.getTime() + 1,
-                      max(S.getEarliestIssueTime(),
-                          S.schedPrio.getEarliestReadyTime())));
+      if (chosenSlot == -1)
+       for (unsigned s=startSlot; s < S.nslots; s++)
+         if ((*igroup)[s] == NULL && S.getChoicesForSlot(s).size() > 0)
+           {
+             chosenSlot = (int) s;
+             break;
+           }
+      
+      if (chosenSlot != -1)
+       { // Insert the chosen instr in the chosen slot and
+         // erase it from all slots.
+         const SchedGraphNode* node= *S.getChoicesForSlot(chosenSlot).begin();
+         S.scheduleInstr(node, chosenSlot, curTime);
+       }
     }
+  
+  assert(numIssued > 0 && "Should not happen when maxIssue > 0!");
 }
 
 
@@ -775,70 +634,40 @@ ForwardListSchedule(SchedulingManager& S)
 static void
 RecordSchedule(const BasicBlock* bb, const SchedulingManager& S)
 {
+  MachineCodeForBasicBlock& mvec = bb->getMachineInstrVec();
+  const MachineInstrInfo& mii = S.schedInfo.getInstrInfo();
+  
+#ifndef NDEBUG
+  // Lets make sure we didn't lose any instructions, except possibly
+  // some NOPs from delay slots.  Also, PHIs are not included in the schedule.
+  unsigned numInstr = 0;
+  for (MachineCodeForBasicBlock::iterator I=mvec.begin(); I != mvec.end(); ++I)
+    if (! mii.isNop((*I)->getOpCode()) &&
+       ! mii.isDummyPhiInstr((*I)->getOpCode()))
+      ++numInstr;
+  assert(S.isched.getNumInstructions() >= numInstr &&
+        "Lost some non-NOP instructions during scheduling!");
+#endif
+  
   if (S.isched.getNumInstructions() == 0)
     return;                            // empty basic block!
   
-  MachineCodeForBasicBlock& mvec = bb->getMachineInstrVec();
-  unsigned int oldSize = mvec.size(); 
-  
   // First find the dummy instructions at the start of the basic block
-  const MachineInstrInfo& mii = S.schedInfo.getInstrInfo();
   MachineCodeForBasicBlock::iterator I = mvec.begin();
   for ( ; I != mvec.end(); ++I)
     if (! mii.isDummyPhiInstr((*I)->getOpCode()))
       break;
   
   // Erase all except the dummy PHI instructions from mvec, and
-  // pre-allocate create space for the ones we will be put back in.
+  // pre-allocate create space for the ones we will put back in.
   mvec.erase(I, mvec.end());
-  mvec.reserve(mvec.size() + S.isched.getNumInstructions());
   
   InstrSchedule::const_iterator NIend = S.isched.end();
   for (InstrSchedule::const_iterator NI = S.isched.begin(); NI != NIend; ++NI)
-    mvec.push_back((*NI)->getMachineInstr());
+    mvec.push_back(const_cast<MachineInstr*>((*NI)->getMachineInstr()));
 }
 
 
-static unsigned
-ChooseOneGroup(SchedulingManager& S)
-{
-  assert(S.schedPrio.getNumReady() > 0
-        && "Don't get here without ready instructions.");
-  
-  DelaySlotInfo* getDelaySlotInfo;
-  
-  // Choose up to `nslots' feasible instructions and their possible slots.
-  unsigned numIssued = FindSlotChoices(S, getDelaySlotInfo);
-  
-  while (numIssued == 0)
-    {
-      S.updateTime(S.getTime()+1);
-      numIssued = FindSlotChoices(S, getDelaySlotInfo);
-    }
-  
-  AssignInstructionsToSlots(S, numIssued);
-  
-  if (getDelaySlotInfo != NULL)
-    getDelaySlotInfo->scheduleDelayedNode(S); 
-  
-  // Print trace of scheduled instructions before newly ready ones
-  if (SchedDebugLevel >= Sched_PrintSchedTrace)
-    {
-      cout << "    Cycle " << S.getTime() << " : Scheduled instructions:\n";
-      const InstrGroup* igroup = S.isched.getIGroup(S.getTime());
-      for (unsigned int s=0; s < S.nslots; s++)
-       {
-         cout << "        ";
-         if ((*igroup)[s] != NULL)
-           cout << * ((*igroup)[s])->getMachineInstr() << endl;
-         else
-           cout << "<none>" << endl;
-       }
-    }
-  
-  return numIssued;
-}
-
 
 static void
 MarkSuccessorsReady(SchedulingManager& S, const SchedGraphNode* node)
@@ -912,7 +741,7 @@ FindSlotChoices(SchedulingManager& S,
       if (nextNode == NULL)
        break;                  // no more instructions for this cycle
       
-      if (S.getInstrInfo().getNumDelaySlots(nextNode->getMachineInstr()->getOpCode()) > 0)
+      if (S.getInstrInfo().getNumDelaySlots(nextNode->getOpCode()) > 0)
        {
          delaySlotInfo = S.getDelaySlotInfoForInstr(nextNode);
          if (delaySlotInfo != NULL)
@@ -925,7 +754,7 @@ FindSlotChoices(SchedulingManager& S,
                indexForDelayedInstr = S.getNumChoices();
            }
        }
-      else if (S.schedInfo.breaksIssueGroup(nextNode->getMachineInstr()->getOpCode()))
+      else if (S.schedInfo.breaksIssueGroup(nextNode->getOpCode()))
        {
          if (indexForBreakingNode < S.nslots)
            // have a breaking instruction already so throw this one away
@@ -935,15 +764,17 @@ FindSlotChoices(SchedulingManager& S,
        }
       
       if (nextNode != NULL)
-       S.addChoice(nextNode);
-      
-      if (S.schedInfo.isSingleIssue(nextNode->getMachineInstr()->getOpCode()))
-       {
-         assert(S.getNumChoices() == 1 &&
-                "Prioritizer returned invalid instr for this cycle!");
-         break;
-       }
+        {
+          S.addChoice(nextNode);
       
+          if (S.schedInfo.isSingleIssue(nextNode->getOpCode()))
+            {
+              assert(S.getNumChoices() == 1 &&
+                     "Prioritizer returned invalid instr for this cycle!");
+              break;
+            }
+        }
+          
       if (indexForDelayedInstr < S.nslots)
        break;                  // leave the rest for delay slots
     }
@@ -963,7 +794,7 @@ FindSlotChoices(SchedulingManager& S,
       
       if (S.getNumChoices() == 1)
        {
-         MachineOpCode opCode = S.getChoice(0)->getMachineInstr()->getOpCode();
+         MachineOpCode opCode = S.getChoice(0)->getOpCode();
          unsigned int s;
          for (s=startSlot; s < S.nslots; s++)
            if (S.schedInfo.instrCanUseSlot(opCode, s))
@@ -975,7 +806,7 @@ FindSlotChoices(SchedulingManager& S,
        {
          for (unsigned i=0; i < S.getNumChoices(); i++)
            {
-             MachineOpCode opCode = S.getChoice(i)->getMachineInstr()->getOpCode();
+             MachineOpCode opCode = S.getChoice(i)->getOpCode();
              for (unsigned int s=startSlot; s < S.nslots; s++)
                if (S.schedInfo.instrCanUseSlot(opCode, s))
                  S.addChoiceToSlot(s, S.getChoice(i));
@@ -995,7 +826,7 @@ FindSlotChoices(SchedulingManager& S,
       assert(delaySlotInfo != NULL && "No delay slot info for instr?");
       
       const SchedGraphNode* delayedNode = S.getChoice(indexForDelayedInstr);
-      MachineOpCode delayOpCode = delayedNode->getMachineInstr()->getOpCode();
+      MachineOpCode delayOpCode = delayedNode->getOpCode();
       unsigned ndelays= S.getInstrInfo().getNumDelaySlots(delayOpCode);
       
       unsigned delayedNodeSlot = S.nslots;
@@ -1015,7 +846,7 @@ FindSlotChoices(SchedulingManager& S,
        {
          // Try to assign every other instruction to a lower numbered
          // slot than delayedNodeSlot.
-         MachineOpCode opCode = S.getChoice(i)->getMachineInstr()->getOpCode();
+         MachineOpCode opCode =S.getChoice(i)->getOpCode();
          bool noSlotFound = true;
          unsigned int s;
          for (s=startSlot; s < delayedNodeSlot; s++)
@@ -1038,7 +869,7 @@ FindSlotChoices(SchedulingManager& S,
          
          assert(s < S.nslots && "No feasible slot for instruction?");
          
-         highestSlotUsed = max(highestSlotUsed, (int) s);
+         highestSlotUsed = std::max(highestSlotUsed, (int) s);
        }
       
       assert(highestSlotUsed <= (int) S.nslots-1 && "Invalid slot used?");
@@ -1069,7 +900,7 @@ FindSlotChoices(SchedulingManager& S,
          
       // Find the last possible slot for this instruction.
       for (int s = S.nslots-1; s >= (int) startSlot; s--)
-       if (S.schedInfo.instrCanUseSlot(breakingNode->getMachineInstr()->getOpCode(), s))
+       if (S.schedInfo.instrCanUseSlot(breakingNode->getOpCode(), s))
          {
            breakingSlot = s;
            break;
@@ -1083,7 +914,7 @@ FindSlotChoices(SchedulingManager& S,
       for (unsigned i=0;
           i < S.getNumChoices() && i < indexForBreakingNode; i++)
        {
-         MachineOpCode opCode = S.getChoice(i)->getMachineInstr()->getOpCode();
+         MachineOpCode opCode =S.getChoice(i)->getOpCode();
          
          // If a higher priority instruction cannot be assigned to
          // any earlier slots, don't schedule the breaking instruction.
@@ -1122,8 +953,7 @@ FindSlotChoices(SchedulingManager& S,
       // Otherwise, just ignore the instruction.
       for (unsigned i=indexForBreakingNode+1; i < S.getNumChoices(); i++)
        {
-         bool foundLowerSlot = false;
-         MachineOpCode opCode = S.getChoice(i)->getMachineInstr()->getOpCode();
+         MachineOpCode opCode = S.getChoice(i)->getOpCode();
          for (unsigned int s=startSlot; s < nslotsToUse; s++)
            if (S.schedInfo.instrCanUseSlot(opCode, s))
              S.addChoiceToSlot(s, S.getChoice(i));
@@ -1134,63 +964,95 @@ FindSlotChoices(SchedulingManager& S,
 }
 
 
-static void
-AssignInstructionsToSlots(class SchedulingManager& S, unsigned maxIssue)
+static unsigned
+ChooseOneGroup(SchedulingManager& S)
 {
-  // find the slot to start from, in the current cycle
-  unsigned int startSlot = 0;
-  cycles_t curTime = S.getTime();
-  
-  assert(maxIssue > 0 && maxIssue <= S.nslots - startSlot);
+  assert(S.schedPrio.getNumReady() > 0
+        && "Don't get here without ready instructions.");
   
-  // If only one instruction can be issued, do so.
-  if (maxIssue == 1)
-    for (unsigned s=startSlot; s < S.nslots; s++)
-      if (S.getChoicesForSlot(s).size() > 0)
-       {// found the one instruction
-         S.scheduleInstr(*S.getChoicesForSlot(s).begin(), s, curTime);
-         return;
-       }
+  cycles_t firstCycle = S.getTime();
+  DelaySlotInfo* getDelaySlotInfo = NULL;
   
-  // Otherwise, choose from the choices for each slot
-  // 
-  InstrGroup* igroup = S.isched.getIGroup(S.getTime());
-  assert(igroup != NULL && "Group creation failed?");
+  // Choose up to `nslots' feasible instructions and their possible slots.
+  unsigned numIssued = FindSlotChoices(S, getDelaySlotInfo);
   
-  // Find a slot that has only a single choice, and take it.
-  // If all slots have 0 or multiple choices, pick the first slot with
-  // choices and use its last instruction (just to avoid shifting the vector).
-  unsigned numIssued;
-  for (numIssued = 0; numIssued < maxIssue; numIssued++)
+  while (numIssued == 0)
     {
-      int chosenSlot = -1, chosenNodeIndex = -1;
-      for (unsigned s=startSlot; s < S.nslots; s++)
-       if ((*igroup)[s] == NULL && S.getChoicesForSlot(s).size() == 1)
-         {
-           chosenSlot = (int) s;
-           break;
-         }
-      
-      if (chosenSlot == -1)
-       for (unsigned s=startSlot; s < S.nslots; s++)
-         if ((*igroup)[s] == NULL && S.getChoicesForSlot(s).size() > 0)
-           {
-             chosenSlot = (int) s;
-             break;
-           }
-      
-      if (chosenSlot != -1)
-       { // Insert the chosen instr in the chosen slot and
-         // erase it from all slots.
-         const SchedGraphNode* node= *S.getChoicesForSlot(chosenSlot).begin();
-         S.scheduleInstr(node, chosenSlot, curTime);
-       }
+      S.updateTime(S.getTime()+1);
+      numIssued = FindSlotChoices(S, getDelaySlotInfo);
     }
   
-  assert(numIssued > 0 && "Should not happen when maxIssue > 0!");
+  AssignInstructionsToSlots(S, numIssued);
+  
+  if (getDelaySlotInfo != NULL)
+    numIssued += getDelaySlotInfo->scheduleDelayedNode(S); 
+  
+  // Print trace of scheduled instructions before newly ready ones
+  if (SchedDebugLevel >= Sched_PrintSchedTrace)
+    {
+      for (cycles_t c = firstCycle; c <= S.getTime(); c++)
+        {
+          cerr << "    Cycle " << (long)c << " : Scheduled instructions:\n";
+          const InstrGroup* igroup = S.isched.getIGroup(c);
+          for (unsigned int s=0; s < S.nslots; s++)
+            {
+              cerr << "        ";
+              if ((*igroup)[s] != NULL)
+                cerr << * ((*igroup)[s])->getMachineInstr() << "\n";
+              else
+                cerr << "<none>\n";
+            }
+        }
+    }
+  
+  return numIssued;
 }
 
 
+static void
+ForwardListSchedule(SchedulingManager& S)
+{
+  unsigned N;
+  const SchedGraphNode* node;
+  
+  S.schedPrio.initialize();
+  
+  while ((N = S.schedPrio.getNumReady()) > 0)
+    {
+      cycles_t nextCycle = S.getTime();
+      
+      // Choose one group of instructions for a cycle, plus any delay slot
+      // instructions (which may overflow into successive cycles).
+      // This will advance S.getTime() to the last cycle in which
+      // instructions are actually issued.
+      // 
+      unsigned numIssued = ChooseOneGroup(S);
+      assert(numIssued > 0 && "Deadlock in list scheduling algorithm?");
+      
+      // Notify the priority manager of scheduled instructions and mark
+      // any successors that may now be ready
+      // 
+      for (cycles_t c = nextCycle; c <= S.getTime(); c++)
+        {
+          const InstrGroup* igroup = S.isched.getIGroup(c);
+          for (unsigned int s=0; s < S.nslots; s++)
+            if ((node = (*igroup)[s]) != NULL)
+              {
+                S.schedPrio.issuedReadyNodeAt(S.getTime(), node);
+                MarkSuccessorsReady(S, node);
+              }
+        }
+      
+      // Move to the next the next earliest cycle for which
+      // an instruction can be issued, or the next earliest in which
+      // one will be ready, or to the next cycle, whichever is latest.
+      // 
+      S.updateTime(std::max(S.getTime() + 1,
+                            std::max(S.getEarliestIssueTime(),
+                                     S.schedPrio.getEarliestReadyTime())));
+    }
+}
+
 
 //---------------------------------------------------------------------
 // Code for filling delay slots for delayed terminator instructions
@@ -1201,107 +1063,7 @@ AssignInstructionsToSlots(class SchedulingManager& S, unsigned maxIssue)
 // when we cannot find single-cycle instructions that can be reordered.
 //----------------------------------------------------------------------
 
-static void
-ChooseInstructionsForDelaySlots(SchedulingManager& S,
-                               const BasicBlock* bb,
-                               SchedGraph* graph)
-{
-  // Look for instructions that can be used for delay slots.
-  // Remove them from the graph, and mark them to be used for delay slots.
-  const MachineInstrInfo& mii = S.getInstrInfo();
-  const TerminatorInst* term = bb->getTerminator();
-  MachineCodeForVMInstr& termMvec = term->getMachineInstrVec();
-  
-  // Find the first branch instr in the sequence of machine instrs for term
-  // 
-  unsigned first = 0;
-  while (! mii.isBranch(termMvec[first]->getOpCode()))
-    ++first;
-  assert(first < termMvec.size() &&
-        "No branch instructions for BR?  Ok, but weird!  Delete assertion.");
-  if (first == termMvec.size())
-    return;
-  
-  SchedGraphNode* brNode = graph->getGraphNodeForInstr(termMvec[first]);
-  assert(! mii.isCall(brNode->getMachineInstr()->getOpCode()) && "Call used as terminator?");
-  
-  unsigned ndelays = mii.getNumDelaySlots(brNode->getMachineInstr()->getOpCode());
-  if (ndelays == 0)
-    return;
-  
-  // Use vectors to remember the nodes chosen for delay slots, and the
-  // NOPs that will be unused.  We cannot remove them from the graph while
-  // walking through the preds and succs of the brNode here, so we
-  // remember the nodes in the vectors and remove them later.
-  // We use separate vectors for the single-cycle and multi-cycle nodes,
-  // so that we can give preference to single-cycle nodes.
-  // 
-  vector<SchedGraphNode*> sdelayNodeVec;
-  vector<SchedGraphNode*> mdelayNodeVec;
-  vector<SchedGraphNode*> nopNodeVec;
-  unsigned numUseful = 0;
-  
-  sdelayNodeVec.reserve(ndelays);
-  
-  for (sg_pred_iterator P = pred_begin(brNode);
-       P != pred_end(brNode) && sdelayNodeVec.size() < ndelays; ++P)
-    if (! (*P)->isDummyNode() &&
-       ! mii.isNop((*P)->getMachineInstr()->getOpCode()) &&
-       NodeCanFillDelaySlot(S, *P, brNode, /*pred*/ true))
-      {
-       ++numUseful;
-       if (mii.maxLatency((*P)->getMachineInstr()->getOpCode()) > 1)
-         mdelayNodeVec.push_back(*P);
-       else
-         sdelayNodeVec.push_back(*P);
-      }
-  
-  // If not enough single-cycle instructions were found, select the
-  // lowest-latency multi-cycle instructions and use them.
-  // Note that this is the most efficient code when only 1 (or even 2)
-  // values need to be selected.
-  // 
-  while (sdelayNodeVec.size() < ndelays && mdelayNodeVec.size() > 0)
-    {
-      unsigned latency;
-      unsigned minLatency = mii.maxLatency(mdelayNodeVec[0]->getMachineInstr()->getOpCode());
-      unsigned minIndex   = 0;
-      for (unsigned i=1; i < mdelayNodeVec.size(); i++)
-       if (minLatency >=
-           (latency = mii.maxLatency(mdelayNodeVec[i]->getMachineInstr()->getOpCode())))
-         {
-           minLatency = latency;
-           minIndex   = i;
-         }
-      sdelayNodeVec.push_back(mdelayNodeVec[minIndex]);
-      if (sdelayNodeVec.size() < ndelays) // avoid the last erase!
-       mdelayNodeVec.erase(mdelayNodeVec.begin() + minIndex);
-    }
-  
-  // Now, remove the NOPs currently in delay slots from the graph.
-  // If not enough useful instructions were found, use the NOPs to
-  // fill delay slots, otherwise, just discard them.
-  for (sg_succ_iterator I=succ_begin(brNode); I != succ_end(brNode); ++I)
-    if (! (*I)->isDummyNode()
-       && mii.isNop((*I)->getMachineInstr()->getOpCode()))
-      {
-       if (sdelayNodeVec.size() < ndelays)
-         sdelayNodeVec.push_back(*I);
-       else
-         nopNodeVec.push_back(*I);
-      }
-  
-  // Mark the nodes chosen for delay slots.  This removes them from the graph.
-  for (unsigned i=0; i < sdelayNodeVec.size(); i++)
-    MarkNodeForDelaySlot(S, sdelayNodeVec[i], brNode, true);
-  
-  // And remove the unused NOPs the graph.
-  for (unsigned i=0; i < nopNodeVec.size(); i++)
-    nopNodeVec[i]->eraseAllEdges();
-}
-
-
-bool
+static bool
 NodeCanFillDelaySlot(const SchedulingManager& S,
                     const SchedGraphNode* node,
                     const SchedGraphNode* brNode,
@@ -1310,11 +1072,11 @@ NodeCanFillDelaySlot(const SchedulingManager& S,
   assert(! node->isDummyNode());
   
   // don't put a branch in the delay slot of another branch
-  if (S.getInstrInfo().isBranch(node->getMachineInstr()->getOpCode()))
+  if (S.getInstrInfo().isBranch(node->getOpCode()))
     return false;
   
   // don't put a single-issue instruction in the delay slot of a branch
-  if (S.schedInfo.isSingleIssue(node->getMachineInstr()->getOpCode()))
+  if (S.schedInfo.isSingleIssue(node->getOpCode()))
     return false;
   
   // don't put a load-use dependence in the delay slot of a branch
@@ -1323,13 +1085,13 @@ NodeCanFillDelaySlot(const SchedulingManager& S,
   for (SchedGraphNode::const_iterator EI = node->beginInEdges();
        EI != node->endInEdges(); ++EI)
     if (! (*EI)->getSrc()->isDummyNode()
-       && mii.isLoad((*EI)->getSrc()->getMachineInstr()->getOpCode())
+       && mii.isLoad((*EI)->getSrc()->getOpCode())
        && (*EI)->getDepType() == SchedGraphEdge::CtrlDep)
       return false;
   
   // for now, don't put an instruction that does not have operand
   // interlocks in the delay slot of a branch
-  if (! S.getInstrInfo().hasOperandInterlock(node->getMachineInstr()->getOpCode()))
+  if (! S.getInstrInfo().hasOperandInterlock(node->getOpCode()))
     return false;
   
   // Finally, if the instruction preceeds the branch, we make sure the
@@ -1357,16 +1119,18 @@ NodeCanFillDelaySlot(const SchedulingManager& S,
 }
 
 
-void
+static void
 MarkNodeForDelaySlot(SchedulingManager& S,
+                    SchedGraph* graph,
                     SchedGraphNode* node,
                     const SchedGraphNode* brNode,
                     bool nodeIsPredecessor)
 {
   if (nodeIsPredecessor)
     { // If node is in the same basic block (i.e., preceeds brNode),
-      // remove it and all its incident edges from the graph.
-      node->eraseAllEdges();
+      // remove it and all its incident edges from the graph.  Make sure we
+      // add dummy edges for pred/succ nodes that become entry/exit nodes.
+      graph->eraseIncidentEdges(node, /*addDummyEdges*/ true);
     }
   else
     { // If the node was from a target block, add the node to the graph
@@ -1379,10 +1143,194 @@ MarkNodeForDelaySlot(SchedulingManager& S,
 }
 
 
+void
+FindUsefulInstructionsForDelaySlots(SchedulingManager& S,
+                                    SchedGraphNode* brNode,
+                                    vector<SchedGraphNode*>& sdelayNodeVec)
+{
+  const MachineInstrInfo& mii = S.getInstrInfo();
+  unsigned ndelays =
+    mii.getNumDelaySlots(brNode->getOpCode());
+  
+  if (ndelays == 0)
+    return;
+  
+  sdelayNodeVec.reserve(ndelays);
+  
+  // Use a separate vector to hold the feasible multi-cycle nodes.
+  // These will be used if not enough single-cycle nodes are found.
+  // 
+  vector<SchedGraphNode*> mdelayNodeVec;
+  
+  for (sg_pred_iterator P = pred_begin(brNode);
+       P != pred_end(brNode) && sdelayNodeVec.size() < ndelays; ++P)
+    if (! (*P)->isDummyNode() &&
+       ! mii.isNop((*P)->getOpCode()) &&
+       NodeCanFillDelaySlot(S, *P, brNode, /*pred*/ true))
+      {
+       if (mii.maxLatency((*P)->getOpCode()) > 1)
+         mdelayNodeVec.push_back(*P);
+       else
+         sdelayNodeVec.push_back(*P);
+      }
+  
+  // If not enough single-cycle instructions were found, select the
+  // lowest-latency multi-cycle instructions and use them.
+  // Note that this is the most efficient code when only 1 (or even 2)
+  // values need to be selected.
+  // 
+  while (sdelayNodeVec.size() < ndelays && mdelayNodeVec.size() > 0)
+    {
+      unsigned lmin =
+        mii.maxLatency(mdelayNodeVec[0]->getOpCode());
+      unsigned minIndex   = 0;
+      for (unsigned i=1; i < mdelayNodeVec.size(); i++)
+        {
+          unsigned li = 
+            mii.maxLatency(mdelayNodeVec[i]->getOpCode());
+          if (lmin >= li)
+            {
+              lmin = li;
+              minIndex = i;
+            }
+        }
+      sdelayNodeVec.push_back(mdelayNodeVec[minIndex]);
+      if (sdelayNodeVec.size() < ndelays) // avoid the last erase!
+       mdelayNodeVec.erase(mdelayNodeVec.begin() + minIndex);
+    }
+}
+
+
+// Remove the NOPs currently in delay slots from the graph.
+// Mark instructions specified in sdelayNodeVec to replace them.
+// If not enough useful instructions were found, mark the NOPs to be used
+// for filling delay slots, otherwise, otherwise just discard them.
+// 
+void
+ReplaceNopsWithUsefulInstr(SchedulingManager& S,
+                           SchedGraphNode* node,
+                           vector<SchedGraphNode*> sdelayNodeVec,
+                           SchedGraph* graph)
+{
+  vector<SchedGraphNode*> nopNodeVec;   // this will hold unused NOPs
+  const MachineInstrInfo& mii = S.getInstrInfo();
+  const MachineInstr* brInstr = node->getMachineInstr();
+  unsigned ndelays= mii.getNumDelaySlots(brInstr->getOpCode());
+  assert(ndelays > 0 && "Unnecessary call to replace NOPs");
+  
+  // Remove the NOPs currently in delay slots from the graph.
+  // If not enough useful instructions were found, use the NOPs to
+  // fill delay slots, otherwise, just discard them.
+  //  
+  unsigned int firstDelaySlotIdx = node->getOrigIndexInBB() + 1;
+  MachineCodeForBasicBlock& bbMvec  = node->getBB()->getMachineInstrVec();
+  assert(bbMvec[firstDelaySlotIdx - 1] == brInstr &&
+         "Incorrect instr. index in basic block for brInstr");
+  
+  // First find all useful instructions already in the delay slots
+  // and USE THEM.  We'll throw away the unused alternatives below
+  // 
+  for (unsigned i=firstDelaySlotIdx; i < firstDelaySlotIdx + ndelays; ++i)
+    if (! mii.isNop(bbMvec[i]->getOpCode()))
+      sdelayNodeVec.insert(sdelayNodeVec.begin(),
+                           graph->getGraphNodeForInstr(bbMvec[i]));
+  
+  // Then find the NOPs and keep only as many as are needed.
+  // Put the rest in nopNodeVec to be deleted.
+  for (unsigned i=firstDelaySlotIdx; i < firstDelaySlotIdx + ndelays; ++i)
+    if (mii.isNop(bbMvec[i]->getOpCode()))
+      if (sdelayNodeVec.size() < ndelays)
+        sdelayNodeVec.push_back(graph->getGraphNodeForInstr(bbMvec[i]));
+      else
+        nopNodeVec.push_back(graph->getGraphNodeForInstr(bbMvec[i]));
+  
+  assert(sdelayNodeVec.size() >= ndelays);
+  
+  // If some delay slots were already filled, throw away that many new choices
+  if (sdelayNodeVec.size() > ndelays)
+    sdelayNodeVec.resize(ndelays);
+  
+  // Mark the nodes chosen for delay slots.  This removes them from the graph.
+  for (unsigned i=0; i < sdelayNodeVec.size(); i++)
+    MarkNodeForDelaySlot(S, graph, sdelayNodeVec[i], node, true);
+  
+  // And remove the unused NOPs from the graph.
+  for (unsigned i=0; i < nopNodeVec.size(); i++)
+    graph->eraseIncidentEdges(nopNodeVec[i], /*addDummyEdges*/ true);
+}
+
+
+// For all delayed instructions, choose instructions to put in the delay
+// slots and pull those out of the graph.  Mark them for the delay slots
+// in the DelaySlotInfo object for that graph node.  If no useful work
+// is found for a delay slot, use the NOP that is currently in that slot.
+// 
+// We try to fill the delay slots with useful work for all instructions
+// EXCEPT CALLS AND RETURNS.
+// For CALLs and RETURNs, it is nearly always possible to use one of the
+// call sequence instrs and putting anything else in the delay slot could be
+// suboptimal.  Also, it complicates generating the calling sequence code in
+// regalloc.
+// 
+static void
+ChooseInstructionsForDelaySlots(SchedulingManager& S,
+                               const BasicBlock *bb,
+                               SchedGraph *graph)
+{
+  const MachineInstrInfo& mii = S.getInstrInfo();
+  const Instruction *termInstr = (Instruction*)bb->getTerminator();
+  MachineCodeForInstruction &termMvec=MachineCodeForInstruction::get(termInstr);
+  vector<SchedGraphNode*> delayNodeVec;
+  const MachineInstr* brInstr = NULL;
+  
+  if (termInstr->getOpcode() != Instruction::Ret)
+    {
+      // To find instructions that need delay slots without searching the full
+      // machine code, we assume that the only delayed instructions are CALLs
+      // or instructions generated for the terminator inst.
+      // Find the first branch instr in the sequence of machine instrs for term
+      // 
+      unsigned first = 0;
+      while (first < termMvec.size() &&
+             ! mii.isBranch(termMvec[first]->getOpCode()))
+        {
+          ++first;
+        }
+      assert(first < termMvec.size() &&
+         "No branch instructions for BR?  Ok, but weird!  Delete assertion.");
+      
+      brInstr = (first < termMvec.size())? termMvec[first] : NULL;
+      
+      // Compute a vector of the nodes chosen for delay slots and then
+      // mark delay slots to replace NOPs with these useful instructions.
+      // 
+      if (brInstr != NULL)
+        {
+          SchedGraphNode* brNode = graph->getGraphNodeForInstr(brInstr);
+          FindUsefulInstructionsForDelaySlots(S, brNode, delayNodeVec);
+          ReplaceNopsWithUsefulInstr(S, brNode, delayNodeVec, graph);
+        }
+    }
+  
+  // Also mark delay slots for other delayed instructions to hold NOPs. 
+  // Simply passing in an empty delayNodeVec will have this effect.
+  // 
+  delayNodeVec.clear();
+  const MachineCodeForBasicBlock& bbMvec = bb->getMachineInstrVec();
+  for (unsigned i=0; i < bbMvec.size(); i++)
+    if (bbMvec[i] != brInstr &&
+        mii.getNumDelaySlots(bbMvec[i]->getOpCode()) > 0)
+      {
+        SchedGraphNode* node = graph->getGraphNodeForInstr(bbMvec[i]);
+        ReplaceNopsWithUsefulInstr(S, node, delayNodeVec, graph);
+      }
+}
+
+
 // 
 // Schedule the delayed branch and its delay slots
 // 
-void
+unsigned
 DelaySlotInfo::scheduleDelayedNode(SchedulingManager& S)
 {
   assert(delayedNodeSlotNum < S.nslots && "Illegal slot for branch");
@@ -1412,10 +1360,10 @@ DelaySlotInfo::scheduleDelayedNode(SchedulingManager& S)
        {
          const SchedGraphNode* dnode = delayNodeVec[i];
          if ( ! S.isScheduled(dnode)
-              && S.schedInfo.instrCanUseSlot(dnode->getMachineInstr()->getOpCode(), nextSlot)
-              && instrIsFeasible(S, dnode->getMachineInstr()->getOpCode()))
+              && S.schedInfo.instrCanUseSlot(dnode->getOpCode(), nextSlot)
+              && instrIsFeasible(S, dnode->getOpCode()))
            {
-             assert(S.getInstrInfo().hasOperandInterlock(dnode->getMachineInstr()->getOpCode())
+             assert(S.getInstrInfo().hasOperandInterlock(dnode->getOpCode())
                     && "Instructions without interlocks not yet supported "
                     "when filling branch delay slots");
              S.scheduleInstr(dnode, nextSlot, nextTime);
@@ -1449,5 +1397,152 @@ DelaySlotInfo::scheduleDelayedNode(SchedulingManager& S)
        S.scheduleInstr(delayNodeVec[i], nextSlot, nextTime);
        break;
       }
+
+  return 1 + ndelays;
+}
+
+
+// Check if the instruction would conflict with instructions already
+// chosen for the current cycle
+// 
+static inline bool
+ConflictsWithChoices(const SchedulingManager& S,
+                    MachineOpCode opCode)
+{
+  // Check if the instruction must issue by itself, and some feasible
+  // choices have already been made for this cycle
+  if (S.getNumChoices() > 0 && S.schedInfo.isSingleIssue(opCode))
+    return true;
+  
+  // For each class that opCode belongs to, check if there are too many
+  // instructions of that class.
+  // 
+  const InstrSchedClass sc = S.schedInfo.getSchedClass(opCode);
+  return (S.getNumChoicesInClass(sc) == S.schedInfo.getMaxIssueForClass(sc));
+}
+
+
+//************************* External Functions *****************************/
+
+
+//---------------------------------------------------------------------------
+// Function: ViolatesMinimumGap
+// 
+// Purpose:
+//   Check minimum gap requirements relative to instructions scheduled in
+//   previous cycles.
+//   Note that we do not need to consider `nextEarliestIssueTime' here because
+//   that is also captured in the earliest start times for each opcode.
+//---------------------------------------------------------------------------
+
+static inline bool
+ViolatesMinimumGap(const SchedulingManager& S,
+                  MachineOpCode opCode,
+                  const cycles_t inCycle)
+{
+  return (inCycle < S.getEarliestStartTimeForOp(opCode));
+}
+
+
+//---------------------------------------------------------------------------
+// Function: instrIsFeasible
+// 
+// Purpose:
+//   Check if any issue restrictions would prevent the instruction from
+//   being issued in the current cycle
+//---------------------------------------------------------------------------
+
+bool
+instrIsFeasible(const SchedulingManager& S,
+               MachineOpCode opCode)
+{
+  // skip the instruction if it cannot be issued due to issue restrictions
+  // caused by previously issued instructions
+  if (ViolatesMinimumGap(S, opCode, S.getTime()))
+    return false;
+  
+  // skip the instruction if it cannot be issued due to issue restrictions
+  // caused by previously chosen instructions for the current cycle
+  if (ConflictsWithChoices(S, opCode))
+    return false;
+  
+  return true;
+}
+
+//---------------------------------------------------------------------------
+// Function: ScheduleInstructionsWithSSA
+// 
+// Purpose:
+//   Entry point for instruction scheduling on SSA form.
+//   Schedules the machine instructions generated by instruction selection.
+//   Assumes that register allocation has not been done, i.e., operands
+//   are still in SSA form.
+//---------------------------------------------------------------------------
+
+namespace {
+  class InstructionSchedulingWithSSA : public FunctionPass {
+    const TargetMachine &target;
+  public:
+    inline InstructionSchedulingWithSSA(const TargetMachine &T) : target(T) {}
+
+    const char *getPassName() const { return "Instruction Scheduling"; }
+  
+    // getAnalysisUsage - We use LiveVarInfo...
+    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
+      AU.addRequired(FunctionLiveVarInfo::ID);
+    }
+    
+    bool runOnFunction(Function *F);
+  };
+} // end anonymous namespace
+
+
+bool
+InstructionSchedulingWithSSA::runOnFunction(Function *M)
+{
+  if (SchedDebugLevel == Sched_Disable)
+    return false;
+  
+  SchedGraphSet graphSet(M, target);   
+  
+  if (SchedDebugLevel >= Sched_PrintSchedGraphs)
+    {
+      cerr << "\n*** SCHEDULING GRAPHS FOR INSTRUCTION SCHEDULING\n";
+      graphSet.dump();
+    }
+  
+  for (SchedGraphSet::const_iterator GI=graphSet.begin(), GE=graphSet.end();
+       GI != GE; ++GI)
+    {
+      SchedGraph* graph = (*GI);
+      const vector<const BasicBlock*> &bbvec = graph->getBasicBlocks();
+      assert(bbvec.size() == 1 && "Cannot schedule multiple basic blocks");
+      const BasicBlock* bb = bbvec[0];
+      
+      if (SchedDebugLevel >= Sched_PrintSchedTrace)
+        cerr << "\n*** TRACE OF INSTRUCTION SCHEDULING OPERATIONS\n\n";
+      
+      // expensive!
+      SchedPriorities schedPrio(M, graph,getAnalysis<FunctionLiveVarInfo>());
+      SchedulingManager S(target, graph, schedPrio);
+          
+      ChooseInstructionsForDelaySlots(S, bb, graph); // modifies graph
+      
+      ForwardListSchedule(S);               // computes schedule in S
+      
+      RecordSchedule(bb, S);                // records schedule in BB
+    }
+  
+  if (SchedDebugLevel >= Sched_PrintMachineCode)
+    {
+      cerr << "\n*** Machine instructions after INSTRUCTION SCHEDULING\n";
+      MachineCodeForMethod::get(M).dump();
+    }
+  
+  return false;
 }
 
+
+Pass *createInstructionSchedulingWithSSAPass(const TargetMachine &tgt) {
+  return new InstructionSchedulingWithSSA(tgt);
+}