Home | History | Annotate | Download | only in CodeGen
      1 //===---- ScheduleDAG.cpp - Implement the ScheduleDAG class ---------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This implements the ScheduleDAG class, which is a base class used by
     11 // scheduling implementation classes.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #include "llvm/CodeGen/ScheduleDAG.h"
     16 #include "llvm/CodeGen/ScheduleHazardRecognizer.h"
     17 #include "llvm/CodeGen/SelectionDAGNodes.h"
     18 #include "llvm/Support/CommandLine.h"
     19 #include "llvm/Support/Debug.h"
     20 #include "llvm/Support/raw_ostream.h"
     21 #include "llvm/Target/TargetInstrInfo.h"
     22 #include "llvm/Target/TargetMachine.h"
     23 #include "llvm/Target/TargetRegisterInfo.h"
     24 #include <climits>
     25 using namespace llvm;
     26 
     27 #define DEBUG_TYPE "pre-RA-sched"
     28 
     29 #ifndef NDEBUG
     30 static cl::opt<bool> StressSchedOpt(
     31   "stress-sched", cl::Hidden, cl::init(false),
     32   cl::desc("Stress test instruction scheduling"));
     33 #endif
     34 
     35 void SchedulingPriorityQueue::anchor() { }
     36 
     37 ScheduleDAG::ScheduleDAG(MachineFunction &mf)
     38   : TM(mf.getTarget()),
     39     TII(TM.getInstrInfo()),
     40     TRI(TM.getRegisterInfo()),
     41     MF(mf), MRI(mf.getRegInfo()),
     42     EntrySU(), ExitSU() {
     43 #ifndef NDEBUG
     44   StressSched = StressSchedOpt;
     45 #endif
     46 }
     47 
     48 ScheduleDAG::~ScheduleDAG() {}
     49 
     50 /// Clear the DAG state (e.g. between scheduling regions).
     51 void ScheduleDAG::clearDAG() {
     52   SUnits.clear();
     53   EntrySU = SUnit();
     54   ExitSU = SUnit();
     55 }
     56 
     57 /// getInstrDesc helper to handle SDNodes.
     58 const MCInstrDesc *ScheduleDAG::getNodeDesc(const SDNode *Node) const {
     59   if (!Node || !Node->isMachineOpcode()) return nullptr;
     60   return &TII->get(Node->getMachineOpcode());
     61 }
     62 
     63 /// addPred - This adds the specified edge as a pred of the current node if
     64 /// not already.  It also adds the current node as a successor of the
     65 /// specified node.
     66 bool SUnit::addPred(const SDep &D, bool Required) {
     67   // If this node already has this dependence, don't add a redundant one.
     68   for (SmallVectorImpl<SDep>::iterator I = Preds.begin(), E = Preds.end();
     69          I != E; ++I) {
     70     // Zero-latency weak edges may be added purely for heuristic ordering. Don't
     71     // add them if another kind of edge already exists.
     72     if (!Required && I->getSUnit() == D.getSUnit())
     73       return false;
     74     if (I->overlaps(D)) {
     75       // Extend the latency if needed. Equivalent to removePred(I) + addPred(D).
     76       if (I->getLatency() < D.getLatency()) {
     77         SUnit *PredSU = I->getSUnit();
     78         // Find the corresponding successor in N.
     79         SDep ForwardD = *I;
     80         ForwardD.setSUnit(this);
     81         for (SmallVectorImpl<SDep>::iterator II = PredSU->Succs.begin(),
     82                EE = PredSU->Succs.end(); II != EE; ++II) {
     83           if (*II == ForwardD) {
     84             II->setLatency(D.getLatency());
     85             break;
     86           }
     87         }
     88         I->setLatency(D.getLatency());
     89       }
     90       return false;
     91     }
     92   }
     93   // Now add a corresponding succ to N.
     94   SDep P = D;
     95   P.setSUnit(this);
     96   SUnit *N = D.getSUnit();
     97   // Update the bookkeeping.
     98   if (D.getKind() == SDep::Data) {
     99     assert(NumPreds < UINT_MAX && "NumPreds will overflow!");
    100     assert(N->NumSuccs < UINT_MAX && "NumSuccs will overflow!");
    101     ++NumPreds;
    102     ++N->NumSuccs;
    103   }
    104   if (!N->isScheduled) {
    105     if (D.isWeak()) {
    106       ++WeakPredsLeft;
    107     }
    108     else {
    109       assert(NumPredsLeft < UINT_MAX && "NumPredsLeft will overflow!");
    110       ++NumPredsLeft;
    111     }
    112   }
    113   if (!isScheduled) {
    114     if (D.isWeak()) {
    115       ++N->WeakSuccsLeft;
    116     }
    117     else {
    118       assert(N->NumSuccsLeft < UINT_MAX && "NumSuccsLeft will overflow!");
    119       ++N->NumSuccsLeft;
    120     }
    121   }
    122   Preds.push_back(D);
    123   N->Succs.push_back(P);
    124   if (P.getLatency() != 0) {
    125     this->setDepthDirty();
    126     N->setHeightDirty();
    127   }
    128   return true;
    129 }
    130 
    131 /// removePred - This removes the specified edge as a pred of the current
    132 /// node if it exists.  It also removes the current node as a successor of
    133 /// the specified node.
    134 void SUnit::removePred(const SDep &D) {
    135   // Find the matching predecessor.
    136   for (SmallVectorImpl<SDep>::iterator I = Preds.begin(), E = Preds.end();
    137          I != E; ++I)
    138     if (*I == D) {
    139       // Find the corresponding successor in N.
    140       SDep P = D;
    141       P.setSUnit(this);
    142       SUnit *N = D.getSUnit();
    143       SmallVectorImpl<SDep>::iterator Succ = std::find(N->Succs.begin(),
    144                                                        N->Succs.end(), P);
    145       assert(Succ != N->Succs.end() && "Mismatching preds / succs lists!");
    146       N->Succs.erase(Succ);
    147       Preds.erase(I);
    148       // Update the bookkeeping.
    149       if (P.getKind() == SDep::Data) {
    150         assert(NumPreds > 0 && "NumPreds will underflow!");
    151         assert(N->NumSuccs > 0 && "NumSuccs will underflow!");
    152         --NumPreds;
    153         --N->NumSuccs;
    154       }
    155       if (!N->isScheduled) {
    156         if (D.isWeak())
    157           --WeakPredsLeft;
    158         else {
    159           assert(NumPredsLeft > 0 && "NumPredsLeft will underflow!");
    160           --NumPredsLeft;
    161         }
    162       }
    163       if (!isScheduled) {
    164         if (D.isWeak())
    165           --N->WeakSuccsLeft;
    166         else {
    167           assert(N->NumSuccsLeft > 0 && "NumSuccsLeft will underflow!");
    168           --N->NumSuccsLeft;
    169         }
    170       }
    171       if (P.getLatency() != 0) {
    172         this->setDepthDirty();
    173         N->setHeightDirty();
    174       }
    175       return;
    176     }
    177 }
    178 
    179 void SUnit::setDepthDirty() {
    180   if (!isDepthCurrent) return;
    181   SmallVector<SUnit*, 8> WorkList;
    182   WorkList.push_back(this);
    183   do {
    184     SUnit *SU = WorkList.pop_back_val();
    185     SU->isDepthCurrent = false;
    186     for (SUnit::const_succ_iterator I = SU->Succs.begin(),
    187          E = SU->Succs.end(); I != E; ++I) {
    188       SUnit *SuccSU = I->getSUnit();
    189       if (SuccSU->isDepthCurrent)
    190         WorkList.push_back(SuccSU);
    191     }
    192   } while (!WorkList.empty());
    193 }
    194 
    195 void SUnit::setHeightDirty() {
    196   if (!isHeightCurrent) return;
    197   SmallVector<SUnit*, 8> WorkList;
    198   WorkList.push_back(this);
    199   do {
    200     SUnit *SU = WorkList.pop_back_val();
    201     SU->isHeightCurrent = false;
    202     for (SUnit::const_pred_iterator I = SU->Preds.begin(),
    203          E = SU->Preds.end(); I != E; ++I) {
    204       SUnit *PredSU = I->getSUnit();
    205       if (PredSU->isHeightCurrent)
    206         WorkList.push_back(PredSU);
    207     }
    208   } while (!WorkList.empty());
    209 }
    210 
    211 /// setDepthToAtLeast - Update this node's successors to reflect the
    212 /// fact that this node's depth just increased.
    213 ///
    214 void SUnit::setDepthToAtLeast(unsigned NewDepth) {
    215   if (NewDepth <= getDepth())
    216     return;
    217   setDepthDirty();
    218   Depth = NewDepth;
    219   isDepthCurrent = true;
    220 }
    221 
    222 /// setHeightToAtLeast - Update this node's predecessors to reflect the
    223 /// fact that this node's height just increased.
    224 ///
    225 void SUnit::setHeightToAtLeast(unsigned NewHeight) {
    226   if (NewHeight <= getHeight())
    227     return;
    228   setHeightDirty();
    229   Height = NewHeight;
    230   isHeightCurrent = true;
    231 }
    232 
    233 /// ComputeDepth - Calculate the maximal path from the node to the exit.
    234 ///
    235 void SUnit::ComputeDepth() {
    236   SmallVector<SUnit*, 8> WorkList;
    237   WorkList.push_back(this);
    238   do {
    239     SUnit *Cur = WorkList.back();
    240 
    241     bool Done = true;
    242     unsigned MaxPredDepth = 0;
    243     for (SUnit::const_pred_iterator I = Cur->Preds.begin(),
    244          E = Cur->Preds.end(); I != E; ++I) {
    245       SUnit *PredSU = I->getSUnit();
    246       if (PredSU->isDepthCurrent)
    247         MaxPredDepth = std::max(MaxPredDepth,
    248                                 PredSU->Depth + I->getLatency());
    249       else {
    250         Done = false;
    251         WorkList.push_back(PredSU);
    252       }
    253     }
    254 
    255     if (Done) {
    256       WorkList.pop_back();
    257       if (MaxPredDepth != Cur->Depth) {
    258         Cur->setDepthDirty();
    259         Cur->Depth = MaxPredDepth;
    260       }
    261       Cur->isDepthCurrent = true;
    262     }
    263   } while (!WorkList.empty());
    264 }
    265 
    266 /// ComputeHeight - Calculate the maximal path from the node to the entry.
    267 ///
    268 void SUnit::ComputeHeight() {
    269   SmallVector<SUnit*, 8> WorkList;
    270   WorkList.push_back(this);
    271   do {
    272     SUnit *Cur = WorkList.back();
    273 
    274     bool Done = true;
    275     unsigned MaxSuccHeight = 0;
    276     for (SUnit::const_succ_iterator I = Cur->Succs.begin(),
    277          E = Cur->Succs.end(); I != E; ++I) {
    278       SUnit *SuccSU = I->getSUnit();
    279       if (SuccSU->isHeightCurrent)
    280         MaxSuccHeight = std::max(MaxSuccHeight,
    281                                  SuccSU->Height + I->getLatency());
    282       else {
    283         Done = false;
    284         WorkList.push_back(SuccSU);
    285       }
    286     }
    287 
    288     if (Done) {
    289       WorkList.pop_back();
    290       if (MaxSuccHeight != Cur->Height) {
    291         Cur->setHeightDirty();
    292         Cur->Height = MaxSuccHeight;
    293       }
    294       Cur->isHeightCurrent = true;
    295     }
    296   } while (!WorkList.empty());
    297 }
    298 
    299 void SUnit::biasCriticalPath() {
    300   if (NumPreds < 2)
    301     return;
    302 
    303   SUnit::pred_iterator BestI = Preds.begin();
    304   unsigned MaxDepth = BestI->getSUnit()->getDepth();
    305   for (SUnit::pred_iterator I = std::next(BestI), E = Preds.end(); I != E;
    306        ++I) {
    307     if (I->getKind() == SDep::Data && I->getSUnit()->getDepth() > MaxDepth)
    308       BestI = I;
    309   }
    310   if (BestI != Preds.begin())
    311     std::swap(*Preds.begin(), *BestI);
    312 }
    313 
    314 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
    315 /// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
    316 /// a group of nodes flagged together.
    317 void SUnit::dump(const ScheduleDAG *G) const {
    318   dbgs() << "SU(" << NodeNum << "): ";
    319   G->dumpNode(this);
    320 }
    321 
    322 void SUnit::dumpAll(const ScheduleDAG *G) const {
    323   dump(G);
    324 
    325   dbgs() << "  # preds left       : " << NumPredsLeft << "\n";
    326   dbgs() << "  # succs left       : " << NumSuccsLeft << "\n";
    327   if (WeakPredsLeft)
    328     dbgs() << "  # weak preds left  : " << WeakPredsLeft << "\n";
    329   if (WeakSuccsLeft)
    330     dbgs() << "  # weak succs left  : " << WeakSuccsLeft << "\n";
    331   dbgs() << "  # rdefs left       : " << NumRegDefsLeft << "\n";
    332   dbgs() << "  Latency            : " << Latency << "\n";
    333   dbgs() << "  Depth              : " << getDepth() << "\n";
    334   dbgs() << "  Height             : " << getHeight() << "\n";
    335 
    336   if (Preds.size() != 0) {
    337     dbgs() << "  Predecessors:\n";
    338     for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
    339          I != E; ++I) {
    340       dbgs() << "   ";
    341       switch (I->getKind()) {
    342       case SDep::Data:        dbgs() << "val "; break;
    343       case SDep::Anti:        dbgs() << "anti"; break;
    344       case SDep::Output:      dbgs() << "out "; break;
    345       case SDep::Order:       dbgs() << "ch  "; break;
    346       }
    347       dbgs() << "SU(" << I->getSUnit()->NodeNum << ")";
    348       if (I->isArtificial())
    349         dbgs() << " *";
    350       dbgs() << ": Latency=" << I->getLatency();
    351       if (I->isAssignedRegDep())
    352         dbgs() << " Reg=" << PrintReg(I->getReg(), G->TRI);
    353       dbgs() << "\n";
    354     }
    355   }
    356   if (Succs.size() != 0) {
    357     dbgs() << "  Successors:\n";
    358     for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
    359          I != E; ++I) {
    360       dbgs() << "   ";
    361       switch (I->getKind()) {
    362       case SDep::Data:        dbgs() << "val "; break;
    363       case SDep::Anti:        dbgs() << "anti"; break;
    364       case SDep::Output:      dbgs() << "out "; break;
    365       case SDep::Order:       dbgs() << "ch  "; break;
    366       }
    367       dbgs() << "SU(" << I->getSUnit()->NodeNum << ")";
    368       if (I->isArtificial())
    369         dbgs() << " *";
    370       dbgs() << ": Latency=" << I->getLatency();
    371       if (I->isAssignedRegDep())
    372         dbgs() << " Reg=" << PrintReg(I->getReg(), G->TRI);
    373       dbgs() << "\n";
    374     }
    375   }
    376   dbgs() << "\n";
    377 }
    378 #endif
    379 
    380 #ifndef NDEBUG
    381 /// VerifyScheduledDAG - Verify that all SUnits were scheduled and that
    382 /// their state is consistent. Return the number of scheduled nodes.
    383 ///
    384 unsigned ScheduleDAG::VerifyScheduledDAG(bool isBottomUp) {
    385   bool AnyNotSched = false;
    386   unsigned DeadNodes = 0;
    387   for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
    388     if (!SUnits[i].isScheduled) {
    389       if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) {
    390         ++DeadNodes;
    391         continue;
    392       }
    393       if (!AnyNotSched)
    394         dbgs() << "*** Scheduling failed! ***\n";
    395       SUnits[i].dump(this);
    396       dbgs() << "has not been scheduled!\n";
    397       AnyNotSched = true;
    398     }
    399     if (SUnits[i].isScheduled &&
    400         (isBottomUp ? SUnits[i].getHeight() : SUnits[i].getDepth()) >
    401           unsigned(INT_MAX)) {
    402       if (!AnyNotSched)
    403         dbgs() << "*** Scheduling failed! ***\n";
    404       SUnits[i].dump(this);
    405       dbgs() << "has an unexpected "
    406            << (isBottomUp ? "Height" : "Depth") << " value!\n";
    407       AnyNotSched = true;
    408     }
    409     if (isBottomUp) {
    410       if (SUnits[i].NumSuccsLeft != 0) {
    411         if (!AnyNotSched)
    412           dbgs() << "*** Scheduling failed! ***\n";
    413         SUnits[i].dump(this);
    414         dbgs() << "has successors left!\n";
    415         AnyNotSched = true;
    416       }
    417     } else {
    418       if (SUnits[i].NumPredsLeft != 0) {
    419         if (!AnyNotSched)
    420           dbgs() << "*** Scheduling failed! ***\n";
    421         SUnits[i].dump(this);
    422         dbgs() << "has predecessors left!\n";
    423         AnyNotSched = true;
    424       }
    425     }
    426   }
    427   assert(!AnyNotSched);
    428   return SUnits.size() - DeadNodes;
    429 }
    430 #endif
    431 
    432 /// InitDAGTopologicalSorting - create the initial topological
    433 /// ordering from the DAG to be scheduled.
    434 ///
    435 /// The idea of the algorithm is taken from
    436 /// "Online algorithms for managing the topological order of
    437 /// a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly
    438 /// This is the MNR algorithm, which was first introduced by
    439 /// A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in
    440 /// "Maintaining a topological order under edge insertions".
    441 ///
    442 /// Short description of the algorithm:
    443 ///
    444 /// Topological ordering, ord, of a DAG maps each node to a topological
    445 /// index so that for all edges X->Y it is the case that ord(X) < ord(Y).
    446 ///
    447 /// This means that if there is a path from the node X to the node Z,
    448 /// then ord(X) < ord(Z).
    449 ///
    450 /// This property can be used to check for reachability of nodes:
    451 /// if Z is reachable from X, then an insertion of the edge Z->X would
    452 /// create a cycle.
    453 ///
    454 /// The algorithm first computes a topological ordering for the DAG by
    455 /// initializing the Index2Node and Node2Index arrays and then tries to keep
    456 /// the ordering up-to-date after edge insertions by reordering the DAG.
    457 ///
    458 /// On insertion of the edge X->Y, the algorithm first marks by calling DFS
    459 /// the nodes reachable from Y, and then shifts them using Shift to lie
    460 /// immediately after X in Index2Node.
    461 void ScheduleDAGTopologicalSort::InitDAGTopologicalSorting() {
    462   unsigned DAGSize = SUnits.size();
    463   std::vector<SUnit*> WorkList;
    464   WorkList.reserve(DAGSize);
    465 
    466   Index2Node.resize(DAGSize);
    467   Node2Index.resize(DAGSize);
    468 
    469   // Initialize the data structures.
    470   if (ExitSU)
    471     WorkList.push_back(ExitSU);
    472   for (unsigned i = 0, e = DAGSize; i != e; ++i) {
    473     SUnit *SU = &SUnits[i];
    474     int NodeNum = SU->NodeNum;
    475     unsigned Degree = SU->Succs.size();
    476     // Temporarily use the Node2Index array as scratch space for degree counts.
    477     Node2Index[NodeNum] = Degree;
    478 
    479     // Is it a node without dependencies?
    480     if (Degree == 0) {
    481       assert(SU->Succs.empty() && "SUnit should have no successors");
    482       // Collect leaf nodes.
    483       WorkList.push_back(SU);
    484     }
    485   }
    486 
    487   int Id = DAGSize;
    488   while (!WorkList.empty()) {
    489     SUnit *SU = WorkList.back();
    490     WorkList.pop_back();
    491     if (SU->NodeNum < DAGSize)
    492       Allocate(SU->NodeNum, --Id);
    493     for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
    494          I != E; ++I) {
    495       SUnit *SU = I->getSUnit();
    496       if (SU->NodeNum < DAGSize && !--Node2Index[SU->NodeNum])
    497         // If all dependencies of the node are processed already,
    498         // then the node can be computed now.
    499         WorkList.push_back(SU);
    500     }
    501   }
    502 
    503   Visited.resize(DAGSize);
    504 
    505 #ifndef NDEBUG
    506   // Check correctness of the ordering
    507   for (unsigned i = 0, e = DAGSize; i != e; ++i) {
    508     SUnit *SU = &SUnits[i];
    509     for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
    510          I != E; ++I) {
    511       assert(Node2Index[SU->NodeNum] > Node2Index[I->getSUnit()->NodeNum] &&
    512       "Wrong topological sorting");
    513     }
    514   }
    515 #endif
    516 }
    517 
    518 /// AddPred - Updates the topological ordering to accommodate an edge
    519 /// to be added from SUnit X to SUnit Y.
    520 void ScheduleDAGTopologicalSort::AddPred(SUnit *Y, SUnit *X) {
    521   int UpperBound, LowerBound;
    522   LowerBound = Node2Index[Y->NodeNum];
    523   UpperBound = Node2Index[X->NodeNum];
    524   bool HasLoop = false;
    525   // Is Ord(X) < Ord(Y) ?
    526   if (LowerBound < UpperBound) {
    527     // Update the topological order.
    528     Visited.reset();
    529     DFS(Y, UpperBound, HasLoop);
    530     assert(!HasLoop && "Inserted edge creates a loop!");
    531     // Recompute topological indexes.
    532     Shift(Visited, LowerBound, UpperBound);
    533   }
    534 }
    535 
    536 /// RemovePred - Updates the topological ordering to accommodate an
    537 /// an edge to be removed from the specified node N from the predecessors
    538 /// of the current node M.
    539 void ScheduleDAGTopologicalSort::RemovePred(SUnit *M, SUnit *N) {
    540   // InitDAGTopologicalSorting();
    541 }
    542 
    543 /// DFS - Make a DFS traversal to mark all nodes reachable from SU and mark
    544 /// all nodes affected by the edge insertion. These nodes will later get new
    545 /// topological indexes by means of the Shift method.
    546 void ScheduleDAGTopologicalSort::DFS(const SUnit *SU, int UpperBound,
    547                                      bool &HasLoop) {
    548   std::vector<const SUnit*> WorkList;
    549   WorkList.reserve(SUnits.size());
    550 
    551   WorkList.push_back(SU);
    552   do {
    553     SU = WorkList.back();
    554     WorkList.pop_back();
    555     Visited.set(SU->NodeNum);
    556     for (int I = SU->Succs.size()-1; I >= 0; --I) {
    557       unsigned s = SU->Succs[I].getSUnit()->NodeNum;
    558       // Edges to non-SUnits are allowed but ignored (e.g. ExitSU).
    559       if (s >= Node2Index.size())
    560         continue;
    561       if (Node2Index[s] == UpperBound) {
    562         HasLoop = true;
    563         return;
    564       }
    565       // Visit successors if not already and in affected region.
    566       if (!Visited.test(s) && Node2Index[s] < UpperBound) {
    567         WorkList.push_back(SU->Succs[I].getSUnit());
    568       }
    569     }
    570   } while (!WorkList.empty());
    571 }
    572 
    573 /// Shift - Renumber the nodes so that the topological ordering is
    574 /// preserved.
    575 void ScheduleDAGTopologicalSort::Shift(BitVector& Visited, int LowerBound,
    576                                        int UpperBound) {
    577   std::vector<int> L;
    578   int shift = 0;
    579   int i;
    580 
    581   for (i = LowerBound; i <= UpperBound; ++i) {
    582     // w is node at topological index i.
    583     int w = Index2Node[i];
    584     if (Visited.test(w)) {
    585       // Unmark.
    586       Visited.reset(w);
    587       L.push_back(w);
    588       shift = shift + 1;
    589     } else {
    590       Allocate(w, i - shift);
    591     }
    592   }
    593 
    594   for (unsigned j = 0; j < L.size(); ++j) {
    595     Allocate(L[j], i - shift);
    596     i = i + 1;
    597   }
    598 }
    599 
    600 
    601 /// WillCreateCycle - Returns true if adding an edge to TargetSU from SU will
    602 /// create a cycle. If so, it is not safe to call AddPred(TargetSU, SU).
    603 bool ScheduleDAGTopologicalSort::WillCreateCycle(SUnit *TargetSU, SUnit *SU) {
    604   // Is SU reachable from TargetSU via successor edges?
    605   if (IsReachable(SU, TargetSU))
    606     return true;
    607   for (SUnit::pred_iterator
    608          I = TargetSU->Preds.begin(), E = TargetSU->Preds.end(); I != E; ++I)
    609     if (I->isAssignedRegDep() &&
    610         IsReachable(SU, I->getSUnit()))
    611       return true;
    612   return false;
    613 }
    614 
    615 /// IsReachable - Checks if SU is reachable from TargetSU.
    616 bool ScheduleDAGTopologicalSort::IsReachable(const SUnit *SU,
    617                                              const SUnit *TargetSU) {
    618   // If insertion of the edge SU->TargetSU would create a cycle
    619   // then there is a path from TargetSU to SU.
    620   int UpperBound, LowerBound;
    621   LowerBound = Node2Index[TargetSU->NodeNum];
    622   UpperBound = Node2Index[SU->NodeNum];
    623   bool HasLoop = false;
    624   // Is Ord(TargetSU) < Ord(SU) ?
    625   if (LowerBound < UpperBound) {
    626     Visited.reset();
    627     // There may be a path from TargetSU to SU. Check for it.
    628     DFS(TargetSU, UpperBound, HasLoop);
    629   }
    630   return HasLoop;
    631 }
    632 
    633 /// Allocate - assign the topological index to the node n.
    634 void ScheduleDAGTopologicalSort::Allocate(int n, int index) {
    635   Node2Index[n] = index;
    636   Index2Node[index] = n;
    637 }
    638 
    639 ScheduleDAGTopologicalSort::
    640 ScheduleDAGTopologicalSort(std::vector<SUnit> &sunits, SUnit *exitsu)
    641   : SUnits(sunits), ExitSU(exitsu) {}
    642 
    643 ScheduleHazardRecognizer::~ScheduleHazardRecognizer() {}
    644