LLVM  mainline
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(mf.getSubtarget().getInstrInfo()),
00040       TRI(mf.getSubtarget().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 }
00376 #endif
00377 
00378 #ifndef NDEBUG
00379 /// VerifyScheduledDAG - Verify that all SUnits were scheduled and that
00380 /// their state is consistent. Return the number of scheduled nodes.
00381 ///
00382 unsigned ScheduleDAG::VerifyScheduledDAG(bool isBottomUp) {
00383   bool AnyNotSched = false;
00384   unsigned DeadNodes = 0;
00385   for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
00386     if (!SUnits[i].isScheduled) {
00387       if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) {
00388         ++DeadNodes;
00389         continue;
00390       }
00391       if (!AnyNotSched)
00392         dbgs() << "*** Scheduling failed! ***\n";
00393       SUnits[i].dump(this);
00394       dbgs() << "has not been scheduled!\n";
00395       AnyNotSched = true;
00396     }
00397     if (SUnits[i].isScheduled &&
00398         (isBottomUp ? SUnits[i].getHeight() : SUnits[i].getDepth()) >
00399           unsigned(INT_MAX)) {
00400       if (!AnyNotSched)
00401         dbgs() << "*** Scheduling failed! ***\n";
00402       SUnits[i].dump(this);
00403       dbgs() << "has an unexpected "
00404            << (isBottomUp ? "Height" : "Depth") << " value!\n";
00405       AnyNotSched = true;
00406     }
00407     if (isBottomUp) {
00408       if (SUnits[i].NumSuccsLeft != 0) {
00409         if (!AnyNotSched)
00410           dbgs() << "*** Scheduling failed! ***\n";
00411         SUnits[i].dump(this);
00412         dbgs() << "has successors left!\n";
00413         AnyNotSched = true;
00414       }
00415     } else {
00416       if (SUnits[i].NumPredsLeft != 0) {
00417         if (!AnyNotSched)
00418           dbgs() << "*** Scheduling failed! ***\n";
00419         SUnits[i].dump(this);
00420         dbgs() << "has predecessors left!\n";
00421         AnyNotSched = true;
00422       }
00423     }
00424   }
00425   assert(!AnyNotSched);
00426   return SUnits.size() - DeadNodes;
00427 }
00428 #endif
00429 
00430 /// InitDAGTopologicalSorting - create the initial topological
00431 /// ordering from the DAG to be scheduled.
00432 ///
00433 /// The idea of the algorithm is taken from
00434 /// "Online algorithms for managing the topological order of
00435 /// a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly
00436 /// This is the MNR algorithm, which was first introduced by
00437 /// A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in
00438 /// "Maintaining a topological order under edge insertions".
00439 ///
00440 /// Short description of the algorithm:
00441 ///
00442 /// Topological ordering, ord, of a DAG maps each node to a topological
00443 /// index so that for all edges X->Y it is the case that ord(X) < ord(Y).
00444 ///
00445 /// This means that if there is a path from the node X to the node Z,
00446 /// then ord(X) < ord(Z).
00447 ///
00448 /// This property can be used to check for reachability of nodes:
00449 /// if Z is reachable from X, then an insertion of the edge Z->X would
00450 /// create a cycle.
00451 ///
00452 /// The algorithm first computes a topological ordering for the DAG by
00453 /// initializing the Index2Node and Node2Index arrays and then tries to keep
00454 /// the ordering up-to-date after edge insertions by reordering the DAG.
00455 ///
00456 /// On insertion of the edge X->Y, the algorithm first marks by calling DFS
00457 /// the nodes reachable from Y, and then shifts them using Shift to lie
00458 /// immediately after X in Index2Node.
00459 void ScheduleDAGTopologicalSort::InitDAGTopologicalSorting() {
00460   unsigned DAGSize = SUnits.size();
00461   std::vector<SUnit*> WorkList;
00462   WorkList.reserve(DAGSize);
00463 
00464   Index2Node.resize(DAGSize);
00465   Node2Index.resize(DAGSize);
00466 
00467   // Initialize the data structures.
00468   if (ExitSU)
00469     WorkList.push_back(ExitSU);
00470   for (unsigned i = 0, e = DAGSize; i != e; ++i) {
00471     SUnit *SU = &SUnits[i];
00472     int NodeNum = SU->NodeNum;
00473     unsigned Degree = SU->Succs.size();
00474     // Temporarily use the Node2Index array as scratch space for degree counts.
00475     Node2Index[NodeNum] = Degree;
00476 
00477     // Is it a node without dependencies?
00478     if (Degree == 0) {
00479       assert(SU->Succs.empty() && "SUnit should have no successors");
00480       // Collect leaf nodes.
00481       WorkList.push_back(SU);
00482     }
00483   }
00484 
00485   int Id = DAGSize;
00486   while (!WorkList.empty()) {
00487     SUnit *SU = WorkList.back();
00488     WorkList.pop_back();
00489     if (SU->NodeNum < DAGSize)
00490       Allocate(SU->NodeNum, --Id);
00491     for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
00492          I != E; ++I) {
00493       SUnit *SU = I->getSUnit();
00494       if (SU->NodeNum < DAGSize && !--Node2Index[SU->NodeNum])
00495         // If all dependencies of the node are processed already,
00496         // then the node can be computed now.
00497         WorkList.push_back(SU);
00498     }
00499   }
00500 
00501   Visited.resize(DAGSize);
00502 
00503 #ifndef NDEBUG
00504   // Check correctness of the ordering
00505   for (unsigned i = 0, e = DAGSize; i != e; ++i) {
00506     SUnit *SU = &SUnits[i];
00507     for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
00508          I != E; ++I) {
00509       assert(Node2Index[SU->NodeNum] > Node2Index[I->getSUnit()->NodeNum] &&
00510       "Wrong topological sorting");
00511     }
00512   }
00513 #endif
00514 }
00515 
00516 /// AddPred - Updates the topological ordering to accommodate an edge
00517 /// to be added from SUnit X to SUnit Y.
00518 void ScheduleDAGTopologicalSort::AddPred(SUnit *Y, SUnit *X) {
00519   int UpperBound, LowerBound;
00520   LowerBound = Node2Index[Y->NodeNum];
00521   UpperBound = Node2Index[X->NodeNum];
00522   bool HasLoop = false;
00523   // Is Ord(X) < Ord(Y) ?
00524   if (LowerBound < UpperBound) {
00525     // Update the topological order.
00526     Visited.reset();
00527     DFS(Y, UpperBound, HasLoop);
00528     assert(!HasLoop && "Inserted edge creates a loop!");
00529     // Recompute topological indexes.
00530     Shift(Visited, LowerBound, UpperBound);
00531   }
00532 }
00533 
00534 /// RemovePred - Updates the topological ordering to accommodate an
00535 /// an edge to be removed from the specified node N from the predecessors
00536 /// of the current node M.
00537 void ScheduleDAGTopologicalSort::RemovePred(SUnit *M, SUnit *N) {
00538   // InitDAGTopologicalSorting();
00539 }
00540 
00541 /// DFS - Make a DFS traversal to mark all nodes reachable from SU and mark
00542 /// all nodes affected by the edge insertion. These nodes will later get new
00543 /// topological indexes by means of the Shift method.
00544 void ScheduleDAGTopologicalSort::DFS(const SUnit *SU, int UpperBound,
00545                                      bool &HasLoop) {
00546   std::vector<const SUnit*> WorkList;
00547   WorkList.reserve(SUnits.size());
00548 
00549   WorkList.push_back(SU);
00550   do {
00551     SU = WorkList.back();
00552     WorkList.pop_back();
00553     Visited.set(SU->NodeNum);
00554     for (int I = SU->Succs.size()-1; I >= 0; --I) {
00555       unsigned s = SU->Succs[I].getSUnit()->NodeNum;
00556       // Edges to non-SUnits are allowed but ignored (e.g. ExitSU).
00557       if (s >= Node2Index.size())
00558         continue;
00559       if (Node2Index[s] == UpperBound) {
00560         HasLoop = true;
00561         return;
00562       }
00563       // Visit successors if not already and in affected region.
00564       if (!Visited.test(s) && Node2Index[s] < UpperBound) {
00565         WorkList.push_back(SU->Succs[I].getSUnit());
00566       }
00567     }
00568   } while (!WorkList.empty());
00569 }
00570 
00571 /// Shift - Renumber the nodes so that the topological ordering is
00572 /// preserved.
00573 void ScheduleDAGTopologicalSort::Shift(BitVector& Visited, int LowerBound,
00574                                        int UpperBound) {
00575   std::vector<int> L;
00576   int shift = 0;
00577   int i;
00578 
00579   for (i = LowerBound; i <= UpperBound; ++i) {
00580     // w is node at topological index i.
00581     int w = Index2Node[i];
00582     if (Visited.test(w)) {
00583       // Unmark.
00584       Visited.reset(w);
00585       L.push_back(w);
00586       shift = shift + 1;
00587     } else {
00588       Allocate(w, i - shift);
00589     }
00590   }
00591 
00592   for (unsigned j = 0; j < L.size(); ++j) {
00593     Allocate(L[j], i - shift);
00594     i = i + 1;
00595   }
00596 }
00597 
00598 
00599 /// WillCreateCycle - Returns true if adding an edge to TargetSU from SU will
00600 /// create a cycle. If so, it is not safe to call AddPred(TargetSU, SU).
00601 bool ScheduleDAGTopologicalSort::WillCreateCycle(SUnit *TargetSU, SUnit *SU) {
00602   // Is SU reachable from TargetSU via successor edges?
00603   if (IsReachable(SU, TargetSU))
00604     return true;
00605   for (SUnit::pred_iterator
00606          I = TargetSU->Preds.begin(), E = TargetSU->Preds.end(); I != E; ++I)
00607     if (I->isAssignedRegDep() &&
00608         IsReachable(SU, I->getSUnit()))
00609       return true;
00610   return false;
00611 }
00612 
00613 /// IsReachable - Checks if SU is reachable from TargetSU.
00614 bool ScheduleDAGTopologicalSort::IsReachable(const SUnit *SU,
00615                                              const SUnit *TargetSU) {
00616   // If insertion of the edge SU->TargetSU would create a cycle
00617   // then there is a path from TargetSU to SU.
00618   int UpperBound, LowerBound;
00619   LowerBound = Node2Index[TargetSU->NodeNum];
00620   UpperBound = Node2Index[SU->NodeNum];
00621   bool HasLoop = false;
00622   // Is Ord(TargetSU) < Ord(SU) ?
00623   if (LowerBound < UpperBound) {
00624     Visited.reset();
00625     // There may be a path from TargetSU to SU. Check for it.
00626     DFS(TargetSU, UpperBound, HasLoop);
00627   }
00628   return HasLoop;
00629 }
00630 
00631 /// Allocate - assign the topological index to the node n.
00632 void ScheduleDAGTopologicalSort::Allocate(int n, int index) {
00633   Node2Index[n] = index;
00634   Index2Node[index] = n;
00635 }
00636 
00637 ScheduleDAGTopologicalSort::
00638 ScheduleDAGTopologicalSort(std::vector<SUnit> &sunits, SUnit *exitsu)
00639   : SUnits(sunits), ExitSU(exitsu) {}
00640 
00641 ScheduleHazardRecognizer::~ScheduleHazardRecognizer() {}