LLVM API Documentation

ResourcePriorityQueue.cpp
Go to the documentation of this file.
00001 //===- ResourcePriorityQueue.cpp - A DFA-oriented priority queue -*- C++ -*-==//
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 file implements the ResourcePriorityQueue class, which is a
00011 // SchedulingPriorityQueue that prioritizes instructions using DFA state to
00012 // reduce the length of the critical path through the basic block
00013 // on VLIW platforms.
00014 // The scheduler is basically a top-down adaptable list scheduler with DFA
00015 // resource tracking added to the cost function.
00016 // DFA is queried as a state machine to model "packets/bundles" during
00017 // schedule. Currently packets/bundles are discarded at the end of
00018 // scheduling, affecting only order of instructions.
00019 //
00020 //===----------------------------------------------------------------------===//
00021 
00022 #include "llvm/CodeGen/ResourcePriorityQueue.h"
00023 #include "llvm/CodeGen/MachineInstr.h"
00024 #include "llvm/CodeGen/SelectionDAGNodes.h"
00025 #include "llvm/Support/CommandLine.h"
00026 #include "llvm/Support/Debug.h"
00027 #include "llvm/Support/raw_ostream.h"
00028 #include "llvm/Target/TargetLowering.h"
00029 #include "llvm/Target/TargetMachine.h"
00030 #include "llvm/Target/TargetSubtargetInfo.h"
00031 
00032 using namespace llvm;
00033 
00034 #define DEBUG_TYPE "scheduler"
00035 
00036 static cl::opt<bool> DisableDFASched("disable-dfa-sched", cl::Hidden,
00037   cl::ZeroOrMore, cl::init(false),
00038   cl::desc("Disable use of DFA during scheduling"));
00039 
00040 static cl::opt<signed> RegPressureThreshold(
00041   "dfa-sched-reg-pressure-threshold", cl::Hidden, cl::ZeroOrMore, cl::init(5),
00042   cl::desc("Track reg pressure and switch priority to in-depth"));
00043 
00044 ResourcePriorityQueue::ResourcePriorityQueue(SelectionDAGISel *IS)
00045     : Picker(this), InstrItins(IS->getTargetLowering()
00046                                    ->getTargetMachine()
00047                                    .getSubtargetImpl()
00048                                    ->getInstrItineraryData()) {
00049   const TargetMachine &TM = (*IS->MF).getTarget();
00050   TRI = TM.getSubtargetImpl()->getRegisterInfo();
00051   TLI = IS->getTargetLowering();
00052   TII = TM.getSubtargetImpl()->getInstrInfo();
00053   ResourcesModel = TII->CreateTargetScheduleState(&TM, nullptr);
00054   // This hard requirement could be relaxed, but for now
00055   // do not let it procede.
00056   assert(ResourcesModel && "Unimplemented CreateTargetScheduleState.");
00057 
00058   unsigned NumRC = TRI->getNumRegClasses();
00059   RegLimit.resize(NumRC);
00060   RegPressure.resize(NumRC);
00061   std::fill(RegLimit.begin(), RegLimit.end(), 0);
00062   std::fill(RegPressure.begin(), RegPressure.end(), 0);
00063   for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(),
00064                                              E = TRI->regclass_end();
00065        I != E; ++I)
00066     RegLimit[(*I)->getID()] = TRI->getRegPressureLimit(*I, *IS->MF);
00067 
00068   ParallelLiveRanges = 0;
00069   HorizontalVerticalBalance = 0;
00070 }
00071 
00072 unsigned
00073 ResourcePriorityQueue::numberRCValPredInSU(SUnit *SU, unsigned RCId) {
00074   unsigned NumberDeps = 0;
00075   for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
00076        I != E; ++I) {
00077     if (I->isCtrl())
00078       continue;
00079 
00080     SUnit *PredSU = I->getSUnit();
00081     const SDNode *ScegN = PredSU->getNode();
00082 
00083     if (!ScegN)
00084       continue;
00085 
00086     // If value is passed to CopyToReg, it is probably
00087     // live outside BB.
00088     switch (ScegN->getOpcode()) {
00089       default:  break;
00090       case ISD::TokenFactor:    break;
00091       case ISD::CopyFromReg:    NumberDeps++;  break;
00092       case ISD::CopyToReg:      break;
00093       case ISD::INLINEASM:      break;
00094     }
00095     if (!ScegN->isMachineOpcode())
00096       continue;
00097 
00098     for (unsigned i = 0, e = ScegN->getNumValues(); i != e; ++i) {
00099       MVT VT = ScegN->getSimpleValueType(i);
00100       if (TLI->isTypeLegal(VT)
00101           && (TLI->getRegClassFor(VT)->getID() == RCId)) {
00102         NumberDeps++;
00103         break;
00104       }
00105     }
00106   }
00107   return NumberDeps;
00108 }
00109 
00110 unsigned ResourcePriorityQueue::numberRCValSuccInSU(SUnit *SU,
00111                                                     unsigned RCId) {
00112   unsigned NumberDeps = 0;
00113   for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
00114        I != E; ++I) {
00115     if (I->isCtrl())
00116       continue;
00117 
00118     SUnit *SuccSU = I->getSUnit();
00119     const SDNode *ScegN = SuccSU->getNode();
00120     if (!ScegN)
00121       continue;
00122 
00123     // If value is passed to CopyToReg, it is probably
00124     // live outside BB.
00125     switch (ScegN->getOpcode()) {
00126       default:  break;
00127       case ISD::TokenFactor:    break;
00128       case ISD::CopyFromReg:    break;
00129       case ISD::CopyToReg:      NumberDeps++;  break;
00130       case ISD::INLINEASM:      break;
00131     }
00132     if (!ScegN->isMachineOpcode())
00133       continue;
00134 
00135     for (unsigned i = 0, e = ScegN->getNumOperands(); i != e; ++i) {
00136       const SDValue &Op = ScegN->getOperand(i);
00137       MVT VT = Op.getNode()->getSimpleValueType(Op.getResNo());
00138       if (TLI->isTypeLegal(VT)
00139           && (TLI->getRegClassFor(VT)->getID() == RCId)) {
00140         NumberDeps++;
00141         break;
00142       }
00143     }
00144   }
00145   return NumberDeps;
00146 }
00147 
00148 static unsigned numberCtrlDepsInSU(SUnit *SU) {
00149   unsigned NumberDeps = 0;
00150   for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
00151        I != E; ++I)
00152     if (I->isCtrl())
00153       NumberDeps++;
00154 
00155   return NumberDeps;
00156 }
00157 
00158 static unsigned numberCtrlPredInSU(SUnit *SU) {
00159   unsigned NumberDeps = 0;
00160   for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
00161        I != E; ++I)
00162     if (I->isCtrl())
00163       NumberDeps++;
00164 
00165   return NumberDeps;
00166 }
00167 
00168 ///
00169 /// Initialize nodes.
00170 ///
00171 void ResourcePriorityQueue::initNodes(std::vector<SUnit> &sunits) {
00172   SUnits = &sunits;
00173   NumNodesSolelyBlocking.resize(SUnits->size(), 0);
00174 
00175   for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
00176     SUnit *SU = &(*SUnits)[i];
00177     initNumRegDefsLeft(SU);
00178     SU->NodeQueueId = 0;
00179   }
00180 }
00181 
00182 /// This heuristic is used if DFA scheduling is not desired
00183 /// for some VLIW platform.
00184 bool resource_sort::operator()(const SUnit *LHS, const SUnit *RHS) const {
00185   // The isScheduleHigh flag allows nodes with wraparound dependencies that
00186   // cannot easily be modeled as edges with latencies to be scheduled as
00187   // soon as possible in a top-down schedule.
00188   if (LHS->isScheduleHigh && !RHS->isScheduleHigh)
00189     return false;
00190 
00191   if (!LHS->isScheduleHigh && RHS->isScheduleHigh)
00192     return true;
00193 
00194   unsigned LHSNum = LHS->NodeNum;
00195   unsigned RHSNum = RHS->NodeNum;
00196 
00197   // The most important heuristic is scheduling the critical path.
00198   unsigned LHSLatency = PQ->getLatency(LHSNum);
00199   unsigned RHSLatency = PQ->getLatency(RHSNum);
00200   if (LHSLatency < RHSLatency) return true;
00201   if (LHSLatency > RHSLatency) return false;
00202 
00203   // After that, if two nodes have identical latencies, look to see if one will
00204   // unblock more other nodes than the other.
00205   unsigned LHSBlocked = PQ->getNumSolelyBlockNodes(LHSNum);
00206   unsigned RHSBlocked = PQ->getNumSolelyBlockNodes(RHSNum);
00207   if (LHSBlocked < RHSBlocked) return true;
00208   if (LHSBlocked > RHSBlocked) return false;
00209 
00210   // Finally, just to provide a stable ordering, use the node number as a
00211   // deciding factor.
00212   return LHSNum < RHSNum;
00213 }
00214 
00215 
00216 /// getSingleUnscheduledPred - If there is exactly one unscheduled predecessor
00217 /// of SU, return it, otherwise return null.
00218 SUnit *ResourcePriorityQueue::getSingleUnscheduledPred(SUnit *SU) {
00219   SUnit *OnlyAvailablePred = nullptr;
00220   for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
00221        I != E; ++I) {
00222     SUnit &Pred = *I->getSUnit();
00223     if (!Pred.isScheduled) {
00224       // We found an available, but not scheduled, predecessor.  If it's the
00225       // only one we have found, keep track of it... otherwise give up.
00226       if (OnlyAvailablePred && OnlyAvailablePred != &Pred)
00227         return nullptr;
00228       OnlyAvailablePred = &Pred;
00229     }
00230   }
00231   return OnlyAvailablePred;
00232 }
00233 
00234 void ResourcePriorityQueue::push(SUnit *SU) {
00235   // Look at all of the successors of this node.  Count the number of nodes that
00236   // this node is the sole unscheduled node for.
00237   unsigned NumNodesBlocking = 0;
00238   for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
00239        I != E; ++I)
00240     if (getSingleUnscheduledPred(I->getSUnit()) == SU)
00241       ++NumNodesBlocking;
00242 
00243   NumNodesSolelyBlocking[SU->NodeNum] = NumNodesBlocking;
00244   Queue.push_back(SU);
00245 }
00246 
00247 /// Check if scheduling of this SU is possible
00248 /// in the current packet.
00249 bool ResourcePriorityQueue::isResourceAvailable(SUnit *SU) {
00250   if (!SU || !SU->getNode())
00251     return false;
00252 
00253   // If this is a compound instruction,
00254   // it is likely to be a call. Do not delay it.
00255   if (SU->getNode()->getGluedNode())
00256     return true;
00257 
00258   // First see if the pipeline could receive this instruction
00259   // in the current cycle.
00260   if (SU->getNode()->isMachineOpcode())
00261     switch (SU->getNode()->getMachineOpcode()) {
00262     default:
00263       if (!ResourcesModel->canReserveResources(&TII->get(
00264           SU->getNode()->getMachineOpcode())))
00265            return false;
00266     case TargetOpcode::EXTRACT_SUBREG:
00267     case TargetOpcode::INSERT_SUBREG:
00268     case TargetOpcode::SUBREG_TO_REG:
00269     case TargetOpcode::REG_SEQUENCE:
00270     case TargetOpcode::IMPLICIT_DEF:
00271         break;
00272     }
00273 
00274   // Now see if there are no other dependencies
00275   // to instructions alredy in the packet.
00276   for (unsigned i = 0, e = Packet.size(); i != e; ++i)
00277     for (SUnit::const_succ_iterator I = Packet[i]->Succs.begin(),
00278          E = Packet[i]->Succs.end(); I != E; ++I) {
00279       // Since we do not add pseudos to packets, might as well
00280       // ignor order deps.
00281       if (I->isCtrl())
00282         continue;
00283 
00284       if (I->getSUnit() == SU)
00285         return false;
00286     }
00287 
00288   return true;
00289 }
00290 
00291 /// Keep track of available resources.
00292 void ResourcePriorityQueue::reserveResources(SUnit *SU) {
00293   // If this SU does not fit in the packet
00294   // start a new one.
00295   if (!isResourceAvailable(SU) || SU->getNode()->getGluedNode()) {
00296     ResourcesModel->clearResources();
00297     Packet.clear();
00298   }
00299 
00300   if (SU->getNode() && SU->getNode()->isMachineOpcode()) {
00301     switch (SU->getNode()->getMachineOpcode()) {
00302     default:
00303       ResourcesModel->reserveResources(&TII->get(
00304         SU->getNode()->getMachineOpcode()));
00305       break;
00306     case TargetOpcode::EXTRACT_SUBREG:
00307     case TargetOpcode::INSERT_SUBREG:
00308     case TargetOpcode::SUBREG_TO_REG:
00309     case TargetOpcode::REG_SEQUENCE:
00310     case TargetOpcode::IMPLICIT_DEF:
00311       break;
00312     }
00313     Packet.push_back(SU);
00314   }
00315   // Forcefully end packet for PseudoOps.
00316   else {
00317     ResourcesModel->clearResources();
00318     Packet.clear();
00319   }
00320 
00321   // If packet is now full, reset the state so in the next cycle
00322   // we start fresh.
00323   if (Packet.size() >= InstrItins->SchedModel.IssueWidth) {
00324     ResourcesModel->clearResources();
00325     Packet.clear();
00326   }
00327 }
00328 
00329 signed ResourcePriorityQueue::rawRegPressureDelta(SUnit *SU, unsigned RCId) {
00330   signed RegBalance    = 0;
00331 
00332   if (!SU || !SU->getNode() || !SU->getNode()->isMachineOpcode())
00333     return RegBalance;
00334 
00335   // Gen estimate.
00336   for (unsigned i = 0, e = SU->getNode()->getNumValues(); i != e; ++i) {
00337       MVT VT = SU->getNode()->getSimpleValueType(i);
00338       if (TLI->isTypeLegal(VT)
00339           && TLI->getRegClassFor(VT)
00340           && TLI->getRegClassFor(VT)->getID() == RCId)
00341         RegBalance += numberRCValSuccInSU(SU, RCId);
00342   }
00343   // Kill estimate.
00344   for (unsigned i = 0, e = SU->getNode()->getNumOperands(); i != e; ++i) {
00345       const SDValue &Op = SU->getNode()->getOperand(i);
00346       MVT VT = Op.getNode()->getSimpleValueType(Op.getResNo());
00347       if (isa<ConstantSDNode>(Op.getNode()))
00348         continue;
00349 
00350       if (TLI->isTypeLegal(VT) && TLI->getRegClassFor(VT)
00351           && TLI->getRegClassFor(VT)->getID() == RCId)
00352         RegBalance -= numberRCValPredInSU(SU, RCId);
00353   }
00354   return RegBalance;
00355 }
00356 
00357 /// Estimates change in reg pressure from this SU.
00358 /// It is achieved by trivial tracking of defined
00359 /// and used vregs in dependent instructions.
00360 /// The RawPressure flag makes this function to ignore
00361 /// existing reg file sizes, and report raw def/use
00362 /// balance.
00363 signed ResourcePriorityQueue::regPressureDelta(SUnit *SU, bool RawPressure) {
00364   signed RegBalance    = 0;
00365 
00366   if (!SU || !SU->getNode() || !SU->getNode()->isMachineOpcode())
00367     return RegBalance;
00368 
00369   if (RawPressure) {
00370     for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(),
00371              E = TRI->regclass_end(); I != E; ++I) {
00372       const TargetRegisterClass *RC = *I;
00373       RegBalance += rawRegPressureDelta(SU, RC->getID());
00374     }
00375   }
00376   else {
00377     for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(),
00378          E = TRI->regclass_end(); I != E; ++I) {
00379       const TargetRegisterClass *RC = *I;
00380       if ((RegPressure[RC->getID()] +
00381            rawRegPressureDelta(SU, RC->getID()) > 0) &&
00382           (RegPressure[RC->getID()] +
00383            rawRegPressureDelta(SU, RC->getID())  >= RegLimit[RC->getID()]))
00384         RegBalance += rawRegPressureDelta(SU, RC->getID());
00385     }
00386   }
00387 
00388   return RegBalance;
00389 }
00390 
00391 // Constants used to denote relative importance of
00392 // heuristic components for cost computation.
00393 static const unsigned PriorityOne = 200;
00394 static const unsigned PriorityTwo = 50;
00395 static const unsigned PriorityThree = 15;
00396 static const unsigned PriorityFour = 5;
00397 static const unsigned ScaleOne = 20;
00398 static const unsigned ScaleTwo = 10;
00399 static const unsigned ScaleThree = 5;
00400 static const unsigned FactorOne = 2;
00401 
00402 /// Returns single number reflecting benefit of scheduling SU
00403 /// in the current cycle.
00404 signed ResourcePriorityQueue::SUSchedulingCost(SUnit *SU) {
00405   // Initial trivial priority.
00406   signed ResCount = 1;
00407 
00408   // Do not waste time on a node that is already scheduled.
00409   if (SU->isScheduled)
00410     return ResCount;
00411 
00412   // Forced priority is high.
00413   if (SU->isScheduleHigh)
00414     ResCount += PriorityOne;
00415 
00416   // Adaptable scheduling
00417   // A small, but very parallel
00418   // region, where reg pressure is an issue.
00419   if (HorizontalVerticalBalance > RegPressureThreshold) {
00420     // Critical path first
00421     ResCount += (SU->getHeight() * ScaleTwo);
00422     // If resources are available for it, multiply the
00423     // chance of scheduling.
00424     if (isResourceAvailable(SU))
00425       ResCount <<= FactorOne;
00426 
00427     // Consider change to reg pressure from scheduling
00428     // this SU.
00429     ResCount -= (regPressureDelta(SU,true) * ScaleOne);
00430   }
00431   // Default heuristic, greeady and
00432   // critical path driven.
00433   else {
00434     // Critical path first.
00435     ResCount += (SU->getHeight() * ScaleTwo);
00436     // Now see how many instructions is blocked by this SU.
00437     ResCount += (NumNodesSolelyBlocking[SU->NodeNum] * ScaleTwo);
00438     // If resources are available for it, multiply the
00439     // chance of scheduling.
00440     if (isResourceAvailable(SU))
00441       ResCount <<= FactorOne;
00442 
00443     ResCount -= (regPressureDelta(SU) * ScaleTwo);
00444   }
00445 
00446   // These are platform-specific things.
00447   // Will need to go into the back end
00448   // and accessed from here via a hook.
00449   for (SDNode *N = SU->getNode(); N; N = N->getGluedNode()) {
00450     if (N->isMachineOpcode()) {
00451       const MCInstrDesc &TID = TII->get(N->getMachineOpcode());
00452       if (TID.isCall())
00453         ResCount += (PriorityTwo + (ScaleThree*N->getNumValues()));
00454     }
00455     else
00456       switch (N->getOpcode()) {
00457       default:  break;
00458       case ISD::TokenFactor:
00459       case ISD::CopyFromReg:
00460       case ISD::CopyToReg:
00461         ResCount += PriorityFour;
00462         break;
00463 
00464       case ISD::INLINEASM:
00465         ResCount += PriorityThree;
00466         break;
00467       }
00468   }
00469   return ResCount;
00470 }
00471 
00472 
00473 /// Main resource tracking point.
00474 void ResourcePriorityQueue::scheduledNode(SUnit *SU) {
00475   // Use NULL entry as an event marker to reset
00476   // the DFA state.
00477   if (!SU) {
00478     ResourcesModel->clearResources();
00479     Packet.clear();
00480     return;
00481   }
00482 
00483   const SDNode *ScegN = SU->getNode();
00484   // Update reg pressure tracking.
00485   // First update current node.
00486   if (ScegN->isMachineOpcode()) {
00487     // Estimate generated regs.
00488     for (unsigned i = 0, e = ScegN->getNumValues(); i != e; ++i) {
00489       MVT VT = ScegN->getSimpleValueType(i);
00490 
00491       if (TLI->isTypeLegal(VT)) {
00492         const TargetRegisterClass *RC = TLI->getRegClassFor(VT);
00493         if (RC)
00494           RegPressure[RC->getID()] += numberRCValSuccInSU(SU, RC->getID());
00495       }
00496     }
00497     // Estimate killed regs.
00498     for (unsigned i = 0, e = ScegN->getNumOperands(); i != e; ++i) {
00499       const SDValue &Op = ScegN->getOperand(i);
00500       MVT VT = Op.getNode()->getSimpleValueType(Op.getResNo());
00501 
00502       if (TLI->isTypeLegal(VT)) {
00503         const TargetRegisterClass *RC = TLI->getRegClassFor(VT);
00504         if (RC) {
00505           if (RegPressure[RC->getID()] >
00506             (numberRCValPredInSU(SU, RC->getID())))
00507             RegPressure[RC->getID()] -= numberRCValPredInSU(SU, RC->getID());
00508           else RegPressure[RC->getID()] = 0;
00509         }
00510       }
00511     }
00512     for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
00513                               I != E; ++I) {
00514       if (I->isCtrl() || (I->getSUnit()->NumRegDefsLeft == 0))
00515         continue;
00516       --I->getSUnit()->NumRegDefsLeft;
00517     }
00518   }
00519 
00520   // Reserve resources for this SU.
00521   reserveResources(SU);
00522 
00523   // Adjust number of parallel live ranges.
00524   // Heuristic is simple - node with no data successors reduces
00525   // number of live ranges. All others, increase it.
00526   unsigned NumberNonControlDeps = 0;
00527 
00528   for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
00529                                   I != E; ++I) {
00530     adjustPriorityOfUnscheduledPreds(I->getSUnit());
00531     if (!I->isCtrl())
00532       NumberNonControlDeps++;
00533   }
00534 
00535   if (!NumberNonControlDeps) {
00536     if (ParallelLiveRanges >= SU->NumPreds)
00537       ParallelLiveRanges -= SU->NumPreds;
00538     else
00539       ParallelLiveRanges = 0;
00540 
00541   }
00542   else
00543     ParallelLiveRanges += SU->NumRegDefsLeft;
00544 
00545   // Track parallel live chains.
00546   HorizontalVerticalBalance += (SU->Succs.size() - numberCtrlDepsInSU(SU));
00547   HorizontalVerticalBalance -= (SU->Preds.size() - numberCtrlPredInSU(SU));
00548 }
00549 
00550 void ResourcePriorityQueue::initNumRegDefsLeft(SUnit *SU) {
00551   unsigned  NodeNumDefs = 0;
00552   for (SDNode *N = SU->getNode(); N; N = N->getGluedNode())
00553     if (N->isMachineOpcode()) {
00554       const MCInstrDesc &TID = TII->get(N->getMachineOpcode());
00555       // No register need be allocated for this.
00556       if (N->getMachineOpcode() == TargetOpcode::IMPLICIT_DEF) {
00557         NodeNumDefs = 0;
00558         break;
00559       }
00560       NodeNumDefs = std::min(N->getNumValues(), TID.getNumDefs());
00561     }
00562     else
00563       switch(N->getOpcode()) {
00564         default:     break;
00565         case ISD::CopyFromReg:
00566           NodeNumDefs++;
00567           break;
00568         case ISD::INLINEASM:
00569           NodeNumDefs++;
00570           break;
00571       }
00572 
00573   SU->NumRegDefsLeft = NodeNumDefs;
00574 }
00575 
00576 /// adjustPriorityOfUnscheduledPreds - One of the predecessors of SU was just
00577 /// scheduled.  If SU is not itself available, then there is at least one
00578 /// predecessor node that has not been scheduled yet.  If SU has exactly ONE
00579 /// unscheduled predecessor, we want to increase its priority: it getting
00580 /// scheduled will make this node available, so it is better than some other
00581 /// node of the same priority that will not make a node available.
00582 void ResourcePriorityQueue::adjustPriorityOfUnscheduledPreds(SUnit *SU) {
00583   if (SU->isAvailable) return;  // All preds scheduled.
00584 
00585   SUnit *OnlyAvailablePred = getSingleUnscheduledPred(SU);
00586   if (!OnlyAvailablePred || !OnlyAvailablePred->isAvailable)
00587     return;
00588 
00589   // Okay, we found a single predecessor that is available, but not scheduled.
00590   // Since it is available, it must be in the priority queue.  First remove it.
00591   remove(OnlyAvailablePred);
00592 
00593   // Reinsert the node into the priority queue, which recomputes its
00594   // NumNodesSolelyBlocking value.
00595   push(OnlyAvailablePred);
00596 }
00597 
00598 
00599 /// Main access point - returns next instructions
00600 /// to be placed in scheduling sequence.
00601 SUnit *ResourcePriorityQueue::pop() {
00602   if (empty())
00603     return nullptr;
00604 
00605   std::vector<SUnit *>::iterator Best = Queue.begin();
00606   if (!DisableDFASched) {
00607     signed BestCost = SUSchedulingCost(*Best);
00608     for (std::vector<SUnit *>::iterator I = std::next(Queue.begin()),
00609            E = Queue.end(); I != E; ++I) {
00610 
00611       if (SUSchedulingCost(*I) > BestCost) {
00612         BestCost = SUSchedulingCost(*I);
00613         Best = I;
00614       }
00615     }
00616   }
00617   // Use default TD scheduling mechanism.
00618   else {
00619     for (std::vector<SUnit *>::iterator I = std::next(Queue.begin()),
00620        E = Queue.end(); I != E; ++I)
00621       if (Picker(*Best, *I))
00622         Best = I;
00623   }
00624 
00625   SUnit *V = *Best;
00626   if (Best != std::prev(Queue.end()))
00627     std::swap(*Best, Queue.back());
00628 
00629   Queue.pop_back();
00630 
00631   return V;
00632 }
00633 
00634 
00635 void ResourcePriorityQueue::remove(SUnit *SU) {
00636   assert(!Queue.empty() && "Queue is empty!");
00637   std::vector<SUnit *>::iterator I = std::find(Queue.begin(), Queue.end(), SU);
00638   if (I != std::prev(Queue.end()))
00639     std::swap(*I, Queue.back());
00640 
00641   Queue.pop_back();
00642 }
00643 
00644 
00645 #ifdef NDEBUG
00646 void ResourcePriorityQueue::dump(ScheduleDAG *DAG) const {}
00647 #else
00648 void ResourcePriorityQueue::dump(ScheduleDAG *DAG) const {
00649   ResourcePriorityQueue q = *this;
00650   while (!q.empty()) {
00651     SUnit *su = q.pop();
00652     dbgs() << "Height " << su->getHeight() << ": ";
00653     su->dump(DAG);
00654   }
00655 }
00656 #endif