LLVM API Documentation

ScheduleDAG.cpp
Go to the documentation of this file.
00001 //===---- ScheduleDAG.cpp - Implement the ScheduleDAG class ---------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This implements the ScheduleDAG class, which is a base class used by
00011 // scheduling implementation classes.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "llvm/CodeGen/ScheduleDAG.h"
00016 #include "llvm/CodeGen/ScheduleHazardRecognizer.h"
00017 #include "llvm/CodeGen/SelectionDAGNodes.h"
00018 #include "llvm/Support/CommandLine.h"
00019 #include "llvm/Support/Debug.h"
00020 #include "llvm/Support/raw_ostream.h"
00021 #include "llvm/Target/TargetInstrInfo.h"
00022 #include "llvm/Target/TargetMachine.h"
00023 #include "llvm/Target/TargetRegisterInfo.h"
00024 #include "llvm/Target/TargetSubtargetInfo.h"
00025 #include <climits>
00026 using namespace llvm;
00027 
00028 #define DEBUG_TYPE "pre-RA-sched"
00029 
00030 #ifndef NDEBUG
00031 static cl::opt<bool> StressSchedOpt(
00032   "stress-sched", cl::Hidden, cl::init(false),
00033   cl::desc("Stress test instruction scheduling"));
00034 #endif
00035 
00036 void SchedulingPriorityQueue::anchor() { }
00037 
00038 ScheduleDAG::ScheduleDAG(MachineFunction &mf)
00039     : TM(mf.getTarget()), TII(TM.getSubtargetImpl()->getInstrInfo()),
00040       TRI(TM.getSubtargetImpl()->getRegisterInfo()), MF(mf),
00041       MRI(mf.getRegInfo()), EntrySU(), ExitSU() {
00042 #ifndef NDEBUG
00043   StressSched = StressSchedOpt;
00044 #endif
00045 }
00046 
00047 ScheduleDAG::~ScheduleDAG() {}
00048 
00049 /// Clear the DAG state (e.g. between scheduling regions).
00050 void ScheduleDAG::clearDAG() {
00051   SUnits.clear();
00052   EntrySU = SUnit();
00053   ExitSU = SUnit();
00054 }
00055 
00056 /// getInstrDesc helper to handle SDNodes.
00057 const MCInstrDesc *ScheduleDAG::getNodeDesc(const SDNode *Node) const {
00058   if (!Node || !Node->isMachineOpcode()) return nullptr;
00059   return &TII->get(Node->getMachineOpcode());
00060 }
00061 
00062 /// addPred - This adds the specified edge as a pred of the current node if
00063 /// not already.  It also adds the current node as a successor of the
00064 /// specified node.
00065 bool SUnit::addPred(const SDep &D, bool Required) {
00066   // If this node already has this dependence, don't add a redundant one.
00067   for (SmallVectorImpl<SDep>::iterator I = Preds.begin(), E = Preds.end();
00068          I != E; ++I) {
00069     // Zero-latency weak edges may be added purely for heuristic ordering. Don't
00070     // add them if another kind of edge already exists.
00071     if (!Required && I->getSUnit() == D.getSUnit())
00072       return false;
00073     if (I->overlaps(D)) {
00074       // Extend the latency if needed. Equivalent to removePred(I) + addPred(D).
00075       if (I->getLatency() < D.getLatency()) {
00076         SUnit *PredSU = I->getSUnit();
00077         // Find the corresponding successor in N.
00078         SDep ForwardD = *I;
00079         ForwardD.setSUnit(this);
00080         for (SmallVectorImpl<SDep>::iterator II = PredSU->Succs.begin(),
00081                EE = PredSU->Succs.end(); II != EE; ++II) {
00082           if (*II == ForwardD) {
00083             II->setLatency(D.getLatency());
00084             break;
00085           }
00086         }
00087         I->setLatency(D.getLatency());
00088       }
00089       return false;
00090     }
00091   }
00092   // Now add a corresponding succ to N.
00093   SDep P = D;
00094   P.setSUnit(this);
00095   SUnit *N = D.getSUnit();
00096   // Update the bookkeeping.
00097   if (D.getKind() == SDep::Data) {
00098     assert(NumPreds < UINT_MAX && "NumPreds will overflow!");
00099     assert(N->NumSuccs < UINT_MAX && "NumSuccs will overflow!");
00100     ++NumPreds;
00101     ++N->NumSuccs;
00102   }
00103   if (!N->isScheduled) {
00104     if (D.isWeak()) {
00105       ++WeakPredsLeft;
00106     }
00107     else {
00108       assert(NumPredsLeft < UINT_MAX && "NumPredsLeft will overflow!");
00109       ++NumPredsLeft;
00110     }
00111   }
00112   if (!isScheduled) {
00113     if (D.isWeak()) {
00114       ++N->WeakSuccsLeft;
00115     }
00116     else {
00117       assert(N->NumSuccsLeft < UINT_MAX && "NumSuccsLeft will overflow!");
00118       ++N->NumSuccsLeft;
00119     }
00120   }
00121   Preds.push_back(D);
00122   N->Succs.push_back(P);
00123   if (P.getLatency() != 0) {
00124     this->setDepthDirty();
00125     N->setHeightDirty();
00126   }
00127   return true;
00128 }
00129 
00130 /// removePred - This removes the specified edge as a pred of the current
00131 /// node if it exists.  It also removes the current node as a successor of
00132 /// the specified node.
00133 void SUnit::removePred(const SDep &D) {
00134   // Find the matching predecessor.
00135   for (SmallVectorImpl<SDep>::iterator I = Preds.begin(), E = Preds.end();
00136          I != E; ++I)
00137     if (*I == D) {
00138       // Find the corresponding successor in N.
00139       SDep P = D;
00140       P.setSUnit(this);
00141       SUnit *N = D.getSUnit();
00142       SmallVectorImpl<SDep>::iterator Succ = std::find(N->Succs.begin(),
00143                                                        N->Succs.end(), P);
00144       assert(Succ != N->Succs.end() && "Mismatching preds / succs lists!");
00145       N->Succs.erase(Succ);
00146       Preds.erase(I);
00147       // Update the bookkeeping.
00148       if (P.getKind() == SDep::Data) {
00149         assert(NumPreds > 0 && "NumPreds will underflow!");
00150         assert(N->NumSuccs > 0 && "NumSuccs will underflow!");
00151         --NumPreds;
00152         --N->NumSuccs;
00153       }
00154       if (!N->isScheduled) {
00155         if (D.isWeak())
00156           --WeakPredsLeft;
00157         else {
00158           assert(NumPredsLeft > 0 && "NumPredsLeft will underflow!");
00159           --NumPredsLeft;
00160         }
00161       }
00162       if (!isScheduled) {
00163         if (D.isWeak())
00164           --N->WeakSuccsLeft;
00165         else {
00166           assert(N->NumSuccsLeft > 0 && "NumSuccsLeft will underflow!");
00167           --N->NumSuccsLeft;
00168         }
00169       }
00170       if (P.getLatency() != 0) {
00171         this->setDepthDirty();
00172         N->setHeightDirty();
00173       }
00174       return;
00175     }
00176 }
00177 
00178 void SUnit::setDepthDirty() {
00179   if (!isDepthCurrent) return;
00180   SmallVector<SUnit*, 8> WorkList;
00181   WorkList.push_back(this);
00182   do {
00183     SUnit *SU = WorkList.pop_back_val();
00184     SU->isDepthCurrent = false;
00185     for (SUnit::const_succ_iterator I = SU->Succs.begin(),
00186          E = SU->Succs.end(); I != E; ++I) {
00187       SUnit *SuccSU = I->getSUnit();
00188       if (SuccSU->isDepthCurrent)
00189         WorkList.push_back(SuccSU);
00190     }
00191   } while (!WorkList.empty());
00192 }
00193 
00194 void SUnit::setHeightDirty() {
00195   if (!isHeightCurrent) return;
00196   SmallVector<SUnit*, 8> WorkList;
00197   WorkList.push_back(this);
00198   do {
00199     SUnit *SU = WorkList.pop_back_val();
00200     SU->isHeightCurrent = false;
00201     for (SUnit::const_pred_iterator I = SU->Preds.begin(),
00202          E = SU->Preds.end(); I != E; ++I) {
00203       SUnit *PredSU = I->getSUnit();
00204       if (PredSU->isHeightCurrent)
00205         WorkList.push_back(PredSU);
00206     }
00207   } while (!WorkList.empty());
00208 }
00209 
00210 /// setDepthToAtLeast - Update this node's successors to reflect the
00211 /// fact that this node's depth just increased.
00212 ///
00213 void SUnit::setDepthToAtLeast(unsigned NewDepth) {
00214   if (NewDepth <= getDepth())
00215     return;
00216   setDepthDirty();
00217   Depth = NewDepth;
00218   isDepthCurrent = true;
00219 }
00220 
00221 /// setHeightToAtLeast - Update this node's predecessors to reflect the
00222 /// fact that this node's height just increased.
00223 ///
00224 void SUnit::setHeightToAtLeast(unsigned NewHeight) {
00225   if (NewHeight <= getHeight())
00226     return;
00227   setHeightDirty();
00228   Height = NewHeight;
00229   isHeightCurrent = true;
00230 }
00231 
00232 /// ComputeDepth - Calculate the maximal path from the node to the exit.
00233 ///
00234 void SUnit::ComputeDepth() {
00235   SmallVector<SUnit*, 8> WorkList;
00236   WorkList.push_back(this);
00237   do {
00238     SUnit *Cur = WorkList.back();
00239 
00240     bool Done = true;
00241     unsigned MaxPredDepth = 0;
00242     for (SUnit::const_pred_iterator I = Cur->Preds.begin(),
00243          E = Cur->Preds.end(); I != E; ++I) {
00244       SUnit *PredSU = I->getSUnit();
00245       if (PredSU->isDepthCurrent)
00246         MaxPredDepth = std::max(MaxPredDepth,
00247                                 PredSU->Depth + I->getLatency());
00248       else {
00249         Done = false;
00250         WorkList.push_back(PredSU);
00251       }
00252     }
00253 
00254     if (Done) {
00255       WorkList.pop_back();
00256       if (MaxPredDepth != Cur->Depth) {
00257         Cur->setDepthDirty();
00258         Cur->Depth = MaxPredDepth;
00259       }
00260       Cur->isDepthCurrent = true;
00261     }
00262   } while (!WorkList.empty());
00263 }
00264 
00265 /// ComputeHeight - Calculate the maximal path from the node to the entry.
00266 ///
00267 void SUnit::ComputeHeight() {
00268   SmallVector<SUnit*, 8> WorkList;
00269   WorkList.push_back(this);
00270   do {
00271     SUnit *Cur = WorkList.back();
00272 
00273     bool Done = true;
00274     unsigned MaxSuccHeight = 0;
00275     for (SUnit::const_succ_iterator I = Cur->Succs.begin(),
00276          E = Cur->Succs.end(); I != E; ++I) {
00277       SUnit *SuccSU = I->getSUnit();
00278       if (SuccSU->isHeightCurrent)
00279         MaxSuccHeight = std::max(MaxSuccHeight,
00280                                  SuccSU->Height + I->getLatency());
00281       else {
00282         Done = false;
00283         WorkList.push_back(SuccSU);
00284       }
00285     }
00286 
00287     if (Done) {
00288       WorkList.pop_back();
00289       if (MaxSuccHeight != Cur->Height) {
00290         Cur->setHeightDirty();
00291         Cur->Height = MaxSuccHeight;
00292       }
00293       Cur->isHeightCurrent = true;
00294     }
00295   } while (!WorkList.empty());
00296 }
00297 
00298 void SUnit::biasCriticalPath() {
00299   if (NumPreds < 2)
00300     return;
00301 
00302   SUnit::pred_iterator BestI = Preds.begin();
00303   unsigned MaxDepth = BestI->getSUnit()->getDepth();
00304   for (SUnit::pred_iterator I = std::next(BestI), E = Preds.end(); I != E;
00305        ++I) {
00306     if (I->getKind() == SDep::Data && I->getSUnit()->getDepth() > MaxDepth)
00307       BestI = I;
00308   }
00309   if (BestI != Preds.begin())
00310     std::swap(*Preds.begin(), *BestI);
00311 }
00312 
00313 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
00314 /// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
00315 /// a group of nodes flagged together.
00316 void SUnit::dump(const ScheduleDAG *G) const {
00317   dbgs() << "SU(" << NodeNum << "): ";
00318   G->dumpNode(this);
00319 }
00320 
00321 void SUnit::dumpAll(const ScheduleDAG *G) const {
00322   dump(G);
00323 
00324   dbgs() << "  # preds left       : " << NumPredsLeft << "\n";
00325   dbgs() << "  # succs left       : " << NumSuccsLeft << "\n";
00326   if (WeakPredsLeft)
00327     dbgs() << "  # weak preds left  : " << WeakPredsLeft << "\n";
00328   if (WeakSuccsLeft)
00329     dbgs() << "  # weak succs left  : " << WeakSuccsLeft << "\n";
00330   dbgs() << "  # rdefs left       : " << NumRegDefsLeft << "\n";
00331   dbgs() << "  Latency            : " << Latency << "\n";
00332   dbgs() << "  Depth              : " << getDepth() << "\n";
00333   dbgs() << "  Height             : " << getHeight() << "\n";
00334 
00335   if (Preds.size() != 0) {
00336     dbgs() << "  Predecessors:\n";
00337     for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
00338          I != E; ++I) {
00339       dbgs() << "   ";
00340       switch (I->getKind()) {
00341       case SDep::Data:        dbgs() << "val "; break;
00342       case SDep::Anti:        dbgs() << "anti"; break;
00343       case SDep::Output:      dbgs() << "out "; break;
00344       case SDep::Order:       dbgs() << "ch  "; break;
00345       }
00346       dbgs() << "SU(" << I->getSUnit()->NodeNum << ")";
00347       if (I->isArtificial())
00348         dbgs() << " *";
00349       dbgs() << ": Latency=" << I->getLatency();
00350       if (I->isAssignedRegDep())
00351         dbgs() << " Reg=" << PrintReg(I->getReg(), G->TRI);
00352       dbgs() << "\n";
00353     }
00354   }
00355   if (Succs.size() != 0) {
00356     dbgs() << "  Successors:\n";
00357     for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
00358          I != E; ++I) {
00359       dbgs() << "   ";
00360       switch (I->getKind()) {
00361       case SDep::Data:        dbgs() << "val "; break;
00362       case SDep::Anti:        dbgs() << "anti"; break;
00363       case SDep::Output:      dbgs() << "out "; break;
00364       case SDep::Order:       dbgs() << "ch  "; break;
00365       }
00366       dbgs() << "SU(" << I->getSUnit()->NodeNum << ")";
00367       if (I->isArtificial())
00368         dbgs() << " *";
00369       dbgs() << ": Latency=" << I->getLatency();
00370       if (I->isAssignedRegDep())
00371         dbgs() << " Reg=" << PrintReg(I->getReg(), G->TRI);
00372       dbgs() << "\n";
00373     }
00374   }
00375   dbgs() << "\n";
00376 }
00377 #endif
00378 
00379 #ifndef NDEBUG
00380 /// VerifyScheduledDAG - Verify that all SUnits were scheduled and that
00381 /// their state is consistent. Return the number of scheduled nodes.
00382 ///
00383 unsigned ScheduleDAG::VerifyScheduledDAG(bool isBottomUp) {
00384   bool AnyNotSched = false;
00385   unsigned DeadNodes = 0;
00386   for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
00387     if (!SUnits[i].isScheduled) {
00388       if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) {
00389         ++DeadNodes;
00390         continue;
00391       }
00392       if (!AnyNotSched)
00393         dbgs() << "*** Scheduling failed! ***\n";
00394       SUnits[i].dump(this);
00395       dbgs() << "has not been scheduled!\n";
00396       AnyNotSched = true;
00397     }
00398     if (SUnits[i].isScheduled &&
00399         (isBottomUp ? SUnits[i].getHeight() : SUnits[i].getDepth()) >
00400           unsigned(INT_MAX)) {
00401       if (!AnyNotSched)
00402         dbgs() << "*** Scheduling failed! ***\n";
00403       SUnits[i].dump(this);
00404       dbgs() << "has an unexpected "
00405            << (isBottomUp ? "Height" : "Depth") << " value!\n";
00406       AnyNotSched = true;
00407     }
00408     if (isBottomUp) {
00409       if (SUnits[i].NumSuccsLeft != 0) {
00410         if (!AnyNotSched)
00411           dbgs() << "*** Scheduling failed! ***\n";
00412         SUnits[i].dump(this);
00413         dbgs() << "has successors left!\n";
00414         AnyNotSched = true;
00415       }
00416     } else {
00417       if (SUnits[i].NumPredsLeft != 0) {
00418         if (!AnyNotSched)
00419           dbgs() << "*** Scheduling failed! ***\n";
00420         SUnits[i].dump(this);
00421         dbgs() << "has predecessors left!\n";
00422         AnyNotSched = true;
00423       }
00424     }
00425   }
00426   assert(!AnyNotSched);
00427   return SUnits.size() - DeadNodes;
00428 }
00429 #endif
00430 
00431 /// InitDAGTopologicalSorting - create the initial topological
00432 /// ordering from the DAG to be scheduled.
00433 ///
00434 /// The idea of the algorithm is taken from
00435 /// "Online algorithms for managing the topological order of
00436 /// a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly
00437 /// This is the MNR algorithm, which was first introduced by
00438 /// A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in
00439 /// "Maintaining a topological order under edge insertions".
00440 ///
00441 /// Short description of the algorithm:
00442 ///
00443 /// Topological ordering, ord, of a DAG maps each node to a topological
00444 /// index so that for all edges X->Y it is the case that ord(X) < ord(Y).
00445 ///
00446 /// This means that if there is a path from the node X to the node Z,
00447 /// then ord(X) < ord(Z).
00448 ///
00449 /// This property can be used to check for reachability of nodes:
00450 /// if Z is reachable from X, then an insertion of the edge Z->X would
00451 /// create a cycle.
00452 ///
00453 /// The algorithm first computes a topological ordering for the DAG by
00454 /// initializing the Index2Node and Node2Index arrays and then tries to keep
00455 /// the ordering up-to-date after edge insertions by reordering the DAG.
00456 ///
00457 /// On insertion of the edge X->Y, the algorithm first marks by calling DFS
00458 /// the nodes reachable from Y, and then shifts them using Shift to lie
00459 /// immediately after X in Index2Node.
00460 void ScheduleDAGTopologicalSort::InitDAGTopologicalSorting() {
00461   unsigned DAGSize = SUnits.size();
00462   std::vector<SUnit*> WorkList;
00463   WorkList.reserve(DAGSize);
00464 
00465   Index2Node.resize(DAGSize);
00466   Node2Index.resize(DAGSize);
00467 
00468   // Initialize the data structures.
00469   if (ExitSU)
00470     WorkList.push_back(ExitSU);
00471   for (unsigned i = 0, e = DAGSize; i != e; ++i) {
00472     SUnit *SU = &SUnits[i];
00473     int NodeNum = SU->NodeNum;
00474     unsigned Degree = SU->Succs.size();
00475     // Temporarily use the Node2Index array as scratch space for degree counts.
00476     Node2Index[NodeNum] = Degree;
00477 
00478     // Is it a node without dependencies?
00479     if (Degree == 0) {
00480       assert(SU->Succs.empty() && "SUnit should have no successors");
00481       // Collect leaf nodes.
00482       WorkList.push_back(SU);
00483     }
00484   }
00485 
00486   int Id = DAGSize;
00487   while (!WorkList.empty()) {
00488     SUnit *SU = WorkList.back();
00489     WorkList.pop_back();
00490     if (SU->NodeNum < DAGSize)
00491       Allocate(SU->NodeNum, --Id);
00492     for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
00493          I != E; ++I) {
00494       SUnit *SU = I->getSUnit();
00495       if (SU->NodeNum < DAGSize && !--Node2Index[SU->NodeNum])
00496         // If all dependencies of the node are processed already,
00497         // then the node can be computed now.
00498         WorkList.push_back(SU);
00499     }
00500   }
00501 
00502   Visited.resize(DAGSize);
00503 
00504 #ifndef NDEBUG
00505   // Check correctness of the ordering
00506   for (unsigned i = 0, e = DAGSize; i != e; ++i) {
00507     SUnit *SU = &SUnits[i];
00508     for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
00509          I != E; ++I) {
00510       assert(Node2Index[SU->NodeNum] > Node2Index[I->getSUnit()->NodeNum] &&
00511       "Wrong topological sorting");
00512     }
00513   }
00514 #endif
00515 }
00516 
00517 /// AddPred - Updates the topological ordering to accommodate an edge
00518 /// to be added from SUnit X to SUnit Y.
00519 void ScheduleDAGTopologicalSort::AddPred(SUnit *Y, SUnit *X) {
00520   int UpperBound, LowerBound;
00521   LowerBound = Node2Index[Y->NodeNum];
00522   UpperBound = Node2Index[X->NodeNum];
00523   bool HasLoop = false;
00524   // Is Ord(X) < Ord(Y) ?
00525   if (LowerBound < UpperBound) {
00526     // Update the topological order.
00527     Visited.reset();
00528     DFS(Y, UpperBound, HasLoop);
00529     assert(!HasLoop && "Inserted edge creates a loop!");
00530     // Recompute topological indexes.
00531     Shift(Visited, LowerBound, UpperBound);
00532   }
00533 }
00534 
00535 /// RemovePred - Updates the topological ordering to accommodate an
00536 /// an edge to be removed from the specified node N from the predecessors
00537 /// of the current node M.
00538 void ScheduleDAGTopologicalSort::RemovePred(SUnit *M, SUnit *N) {
00539   // InitDAGTopologicalSorting();
00540 }
00541 
00542 /// DFS - Make a DFS traversal to mark all nodes reachable from SU and mark
00543 /// all nodes affected by the edge insertion. These nodes will later get new
00544 /// topological indexes by means of the Shift method.
00545 void ScheduleDAGTopologicalSort::DFS(const SUnit *SU, int UpperBound,
00546                                      bool &HasLoop) {
00547   std::vector<const SUnit*> WorkList;
00548   WorkList.reserve(SUnits.size());
00549 
00550   WorkList.push_back(SU);
00551   do {
00552     SU = WorkList.back();
00553     WorkList.pop_back();
00554     Visited.set(SU->NodeNum);
00555     for (int I = SU->Succs.size()-1; I >= 0; --I) {
00556       unsigned s = SU->Succs[I].getSUnit()->NodeNum;
00557       // Edges to non-SUnits are allowed but ignored (e.g. ExitSU).
00558       if (s >= Node2Index.size())
00559         continue;
00560       if (Node2Index[s] == UpperBound) {
00561         HasLoop = true;
00562         return;
00563       }
00564       // Visit successors if not already and in affected region.
00565       if (!Visited.test(s) && Node2Index[s] < UpperBound) {
00566         WorkList.push_back(SU->Succs[I].getSUnit());
00567       }
00568     }
00569   } while (!WorkList.empty());
00570 }
00571 
00572 /// Shift - Renumber the nodes so that the topological ordering is
00573 /// preserved.
00574 void ScheduleDAGTopologicalSort::Shift(BitVector& Visited, int LowerBound,
00575                                        int UpperBound) {
00576   std::vector<int> L;
00577   int shift = 0;
00578   int i;
00579 
00580   for (i = LowerBound; i <= UpperBound; ++i) {
00581     // w is node at topological index i.
00582     int w = Index2Node[i];
00583     if (Visited.test(w)) {
00584       // Unmark.
00585       Visited.reset(w);
00586       L.push_back(w);
00587       shift = shift + 1;
00588     } else {
00589       Allocate(w, i - shift);
00590     }
00591   }
00592 
00593   for (unsigned j = 0; j < L.size(); ++j) {
00594     Allocate(L[j], i - shift);
00595     i = i + 1;
00596   }
00597 }
00598 
00599 
00600 /// WillCreateCycle - Returns true if adding an edge to TargetSU from SU will
00601 /// create a cycle. If so, it is not safe to call AddPred(TargetSU, SU).
00602 bool ScheduleDAGTopologicalSort::WillCreateCycle(SUnit *TargetSU, SUnit *SU) {
00603   // Is SU reachable from TargetSU via successor edges?
00604   if (IsReachable(SU, TargetSU))
00605     return true;
00606   for (SUnit::pred_iterator
00607          I = TargetSU->Preds.begin(), E = TargetSU->Preds.end(); I != E; ++I)
00608     if (I->isAssignedRegDep() &&
00609         IsReachable(SU, I->getSUnit()))
00610       return true;
00611   return false;
00612 }
00613 
00614 /// IsReachable - Checks if SU is reachable from TargetSU.
00615 bool ScheduleDAGTopologicalSort::IsReachable(const SUnit *SU,
00616                                              const SUnit *TargetSU) {
00617   // If insertion of the edge SU->TargetSU would create a cycle
00618   // then there is a path from TargetSU to SU.
00619   int UpperBound, LowerBound;
00620   LowerBound = Node2Index[TargetSU->NodeNum];
00621   UpperBound = Node2Index[SU->NodeNum];
00622   bool HasLoop = false;
00623   // Is Ord(TargetSU) < Ord(SU) ?
00624   if (LowerBound < UpperBound) {
00625     Visited.reset();
00626     // There may be a path from TargetSU to SU. Check for it.
00627     DFS(TargetSU, UpperBound, HasLoop);
00628   }
00629   return HasLoop;
00630 }
00631 
00632 /// Allocate - assign the topological index to the node n.
00633 void ScheduleDAGTopologicalSort::Allocate(int n, int index) {
00634   Node2Index[n] = index;
00635   Index2Node[index] = n;
00636 }
00637 
00638 ScheduleDAGTopologicalSort::
00639 ScheduleDAGTopologicalSort(std::vector<SUnit> &sunits, SUnit *exitsu)
00640   : SUnits(sunits), ExitSU(exitsu) {}
00641 
00642 ScheduleHazardRecognizer::~ScheduleHazardRecognizer() {}