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