LLVM API Documentation
00001 //===- HexagonMachineScheduler.cpp - MI Scheduler for Hexagon -------------===// 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 // MachineScheduler schedules machine instructions after phi elimination. It 00011 // preserves LiveIntervals so it can be invoked before register allocation. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #define DEBUG_TYPE "misched" 00016 00017 #include "HexagonMachineScheduler.h" 00018 #include "llvm/CodeGen/MachineLoopInfo.h" 00019 #include "llvm/IR/Function.h" 00020 00021 using namespace llvm; 00022 00023 /// Platform specific modifications to DAG. 00024 void VLIWMachineScheduler::postprocessDAG() { 00025 SUnit* LastSequentialCall = NULL; 00026 // Currently we only catch the situation when compare gets scheduled 00027 // before preceding call. 00028 for (unsigned su = 0, e = SUnits.size(); su != e; ++su) { 00029 // Remember the call. 00030 if (SUnits[su].getInstr()->isCall()) 00031 LastSequentialCall = &(SUnits[su]); 00032 // Look for a compare that defines a predicate. 00033 else if (SUnits[su].getInstr()->isCompare() && LastSequentialCall) 00034 SUnits[su].addPred(SDep(LastSequentialCall, SDep::Barrier)); 00035 } 00036 } 00037 00038 /// Check if scheduling of this SU is possible 00039 /// in the current packet. 00040 /// It is _not_ precise (statefull), it is more like 00041 /// another heuristic. Many corner cases are figured 00042 /// empirically. 00043 bool VLIWResourceModel::isResourceAvailable(SUnit *SU) { 00044 if (!SU || !SU->getInstr()) 00045 return false; 00046 00047 // First see if the pipeline could receive this instruction 00048 // in the current cycle. 00049 switch (SU->getInstr()->getOpcode()) { 00050 default: 00051 if (!ResourcesModel->canReserveResources(SU->getInstr())) 00052 return false; 00053 case TargetOpcode::EXTRACT_SUBREG: 00054 case TargetOpcode::INSERT_SUBREG: 00055 case TargetOpcode::SUBREG_TO_REG: 00056 case TargetOpcode::REG_SEQUENCE: 00057 case TargetOpcode::IMPLICIT_DEF: 00058 case TargetOpcode::COPY: 00059 case TargetOpcode::INLINEASM: 00060 break; 00061 } 00062 00063 // Now see if there are no other dependencies to instructions already 00064 // in the packet. 00065 for (unsigned i = 0, e = Packet.size(); i != e; ++i) { 00066 if (Packet[i]->Succs.size() == 0) 00067 continue; 00068 for (SUnit::const_succ_iterator I = Packet[i]->Succs.begin(), 00069 E = Packet[i]->Succs.end(); I != E; ++I) { 00070 // Since we do not add pseudos to packets, might as well 00071 // ignore order dependencies. 00072 if (I->isCtrl()) 00073 continue; 00074 00075 if (I->getSUnit() == SU) 00076 return false; 00077 } 00078 } 00079 return true; 00080 } 00081 00082 /// Keep track of available resources. 00083 bool VLIWResourceModel::reserveResources(SUnit *SU) { 00084 bool startNewCycle = false; 00085 // Artificially reset state. 00086 if (!SU) { 00087 ResourcesModel->clearResources(); 00088 Packet.clear(); 00089 TotalPackets++; 00090 return false; 00091 } 00092 // If this SU does not fit in the packet 00093 // start a new one. 00094 if (!isResourceAvailable(SU)) { 00095 ResourcesModel->clearResources(); 00096 Packet.clear(); 00097 TotalPackets++; 00098 startNewCycle = true; 00099 } 00100 00101 switch (SU->getInstr()->getOpcode()) { 00102 default: 00103 ResourcesModel->reserveResources(SU->getInstr()); 00104 break; 00105 case TargetOpcode::EXTRACT_SUBREG: 00106 case TargetOpcode::INSERT_SUBREG: 00107 case TargetOpcode::SUBREG_TO_REG: 00108 case TargetOpcode::REG_SEQUENCE: 00109 case TargetOpcode::IMPLICIT_DEF: 00110 case TargetOpcode::KILL: 00111 case TargetOpcode::PROLOG_LABEL: 00112 case TargetOpcode::EH_LABEL: 00113 case TargetOpcode::COPY: 00114 case TargetOpcode::INLINEASM: 00115 break; 00116 } 00117 Packet.push_back(SU); 00118 00119 #ifndef NDEBUG 00120 DEBUG(dbgs() << "Packet[" << TotalPackets << "]:\n"); 00121 for (unsigned i = 0, e = Packet.size(); i != e; ++i) { 00122 DEBUG(dbgs() << "\t[" << i << "] SU("); 00123 DEBUG(dbgs() << Packet[i]->NodeNum << ")\t"); 00124 DEBUG(Packet[i]->getInstr()->dump()); 00125 } 00126 #endif 00127 00128 // If packet is now full, reset the state so in the next cycle 00129 // we start fresh. 00130 if (Packet.size() >= SchedModel->getIssueWidth()) { 00131 ResourcesModel->clearResources(); 00132 Packet.clear(); 00133 TotalPackets++; 00134 startNewCycle = true; 00135 } 00136 00137 return startNewCycle; 00138 } 00139 00140 /// schedule - Called back from MachineScheduler::runOnMachineFunction 00141 /// after setting up the current scheduling region. [RegionBegin, RegionEnd) 00142 /// only includes instructions that have DAG nodes, not scheduling boundaries. 00143 void VLIWMachineScheduler::schedule() { 00144 DEBUG(dbgs() 00145 << "********** MI Converging Scheduling VLIW BB#" << BB->getNumber() 00146 << " " << BB->getName() 00147 << " in_func " << BB->getParent()->getFunction()->getName() 00148 << " at loop depth " << MLI.getLoopDepth(BB) 00149 << " \n"); 00150 00151 buildDAGWithRegPressure(); 00152 00153 // Postprocess the DAG to add platform specific artificial dependencies. 00154 postprocessDAG(); 00155 00156 SmallVector<SUnit*, 8> TopRoots, BotRoots; 00157 findRootsAndBiasEdges(TopRoots, BotRoots); 00158 00159 // Initialize the strategy before modifying the DAG. 00160 SchedImpl->initialize(this); 00161 00162 // To view Height/Depth correctly, they should be accessed at least once. 00163 // 00164 // FIXME: SUnit::dumpAll always recompute depth and height now. The max 00165 // depth/height could be computed directly from the roots and leaves. 00166 DEBUG(unsigned maxH = 0; 00167 for (unsigned su = 0, e = SUnits.size(); su != e; ++su) 00168 if (SUnits[su].getHeight() > maxH) 00169 maxH = SUnits[su].getHeight(); 00170 dbgs() << "Max Height " << maxH << "\n";); 00171 DEBUG(unsigned maxD = 0; 00172 for (unsigned su = 0, e = SUnits.size(); su != e; ++su) 00173 if (SUnits[su].getDepth() > maxD) 00174 maxD = SUnits[su].getDepth(); 00175 dbgs() << "Max Depth " << maxD << "\n";); 00176 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su) 00177 SUnits[su].dumpAll(this)); 00178 00179 initQueues(TopRoots, BotRoots); 00180 00181 bool IsTopNode = false; 00182 while (SUnit *SU = SchedImpl->pickNode(IsTopNode)) { 00183 if (!checkSchedLimit()) 00184 break; 00185 00186 scheduleMI(SU, IsTopNode); 00187 00188 updateQueues(SU, IsTopNode); 00189 } 00190 assert(CurrentTop == CurrentBottom && "Nonempty unscheduled zone."); 00191 00192 placeDebugValues(); 00193 } 00194 00195 void ConvergingVLIWScheduler::initialize(ScheduleDAGMI *dag) { 00196 DAG = static_cast<VLIWMachineScheduler*>(dag); 00197 SchedModel = DAG->getSchedModel(); 00198 00199 Top.init(DAG, SchedModel); 00200 Bot.init(DAG, SchedModel); 00201 00202 // Initialize the HazardRecognizers. If itineraries don't exist, are empty, or 00203 // are disabled, then these HazardRecs will be disabled. 00204 const InstrItineraryData *Itin = DAG->getSchedModel()->getInstrItineraries(); 00205 const TargetMachine &TM = DAG->MF.getTarget(); 00206 delete Top.HazardRec; 00207 delete Bot.HazardRec; 00208 Top.HazardRec = TM.getInstrInfo()->CreateTargetMIHazardRecognizer(Itin, DAG); 00209 Bot.HazardRec = TM.getInstrInfo()->CreateTargetMIHazardRecognizer(Itin, DAG); 00210 00211 Top.ResourceModel = new VLIWResourceModel(TM, DAG->getSchedModel()); 00212 Bot.ResourceModel = new VLIWResourceModel(TM, DAG->getSchedModel()); 00213 00214 assert((!llvm::ForceTopDown || !llvm::ForceBottomUp) && 00215 "-misched-topdown incompatible with -misched-bottomup"); 00216 } 00217 00218 void ConvergingVLIWScheduler::releaseTopNode(SUnit *SU) { 00219 if (SU->isScheduled) 00220 return; 00221 00222 for (SUnit::succ_iterator I = SU->Preds.begin(), E = SU->Preds.end(); 00223 I != E; ++I) { 00224 unsigned PredReadyCycle = I->getSUnit()->TopReadyCycle; 00225 unsigned MinLatency = I->getLatency(); 00226 #ifndef NDEBUG 00227 Top.MaxMinLatency = std::max(MinLatency, Top.MaxMinLatency); 00228 #endif 00229 if (SU->TopReadyCycle < PredReadyCycle + MinLatency) 00230 SU->TopReadyCycle = PredReadyCycle + MinLatency; 00231 } 00232 Top.releaseNode(SU, SU->TopReadyCycle); 00233 } 00234 00235 void ConvergingVLIWScheduler::releaseBottomNode(SUnit *SU) { 00236 if (SU->isScheduled) 00237 return; 00238 00239 assert(SU->getInstr() && "Scheduled SUnit must have instr"); 00240 00241 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); 00242 I != E; ++I) { 00243 unsigned SuccReadyCycle = I->getSUnit()->BotReadyCycle; 00244 unsigned MinLatency = I->getLatency(); 00245 #ifndef NDEBUG 00246 Bot.MaxMinLatency = std::max(MinLatency, Bot.MaxMinLatency); 00247 #endif 00248 if (SU->BotReadyCycle < SuccReadyCycle + MinLatency) 00249 SU->BotReadyCycle = SuccReadyCycle + MinLatency; 00250 } 00251 Bot.releaseNode(SU, SU->BotReadyCycle); 00252 } 00253 00254 /// Does this SU have a hazard within the current instruction group. 00255 /// 00256 /// The scheduler supports two modes of hazard recognition. The first is the 00257 /// ScheduleHazardRecognizer API. It is a fully general hazard recognizer that 00258 /// supports highly complicated in-order reservation tables 00259 /// (ScoreboardHazardRecognizer) and arbitrary target-specific logic. 00260 /// 00261 /// The second is a streamlined mechanism that checks for hazards based on 00262 /// simple counters that the scheduler itself maintains. It explicitly checks 00263 /// for instruction dispatch limitations, including the number of micro-ops that 00264 /// can dispatch per cycle. 00265 /// 00266 /// TODO: Also check whether the SU must start a new group. 00267 bool ConvergingVLIWScheduler::SchedBoundary::checkHazard(SUnit *SU) { 00268 if (HazardRec->isEnabled()) 00269 return HazardRec->getHazardType(SU) != ScheduleHazardRecognizer::NoHazard; 00270 00271 unsigned uops = SchedModel->getNumMicroOps(SU->getInstr()); 00272 if (IssueCount + uops > SchedModel->getIssueWidth()) 00273 return true; 00274 00275 return false; 00276 } 00277 00278 void ConvergingVLIWScheduler::SchedBoundary::releaseNode(SUnit *SU, 00279 unsigned ReadyCycle) { 00280 if (ReadyCycle < MinReadyCycle) 00281 MinReadyCycle = ReadyCycle; 00282 00283 // Check for interlocks first. For the purpose of other heuristics, an 00284 // instruction that cannot issue appears as if it's not in the ReadyQueue. 00285 if (ReadyCycle > CurrCycle || checkHazard(SU)) 00286 00287 Pending.push(SU); 00288 else 00289 Available.push(SU); 00290 } 00291 00292 /// Move the boundary of scheduled code by one cycle. 00293 void ConvergingVLIWScheduler::SchedBoundary::bumpCycle() { 00294 unsigned Width = SchedModel->getIssueWidth(); 00295 IssueCount = (IssueCount <= Width) ? 0 : IssueCount - Width; 00296 00297 assert(MinReadyCycle < UINT_MAX && "MinReadyCycle uninitialized"); 00298 unsigned NextCycle = std::max(CurrCycle + 1, MinReadyCycle); 00299 00300 if (!HazardRec->isEnabled()) { 00301 // Bypass HazardRec virtual calls. 00302 CurrCycle = NextCycle; 00303 } else { 00304 // Bypass getHazardType calls in case of long latency. 00305 for (; CurrCycle != NextCycle; ++CurrCycle) { 00306 if (isTop()) 00307 HazardRec->AdvanceCycle(); 00308 else 00309 HazardRec->RecedeCycle(); 00310 } 00311 } 00312 CheckPending = true; 00313 00314 DEBUG(dbgs() << "*** " << Available.getName() << " cycle " 00315 << CurrCycle << '\n'); 00316 } 00317 00318 /// Move the boundary of scheduled code by one SUnit. 00319 void ConvergingVLIWScheduler::SchedBoundary::bumpNode(SUnit *SU) { 00320 bool startNewCycle = false; 00321 00322 // Update the reservation table. 00323 if (HazardRec->isEnabled()) { 00324 if (!isTop() && SU->isCall) { 00325 // Calls are scheduled with their preceding instructions. For bottom-up 00326 // scheduling, clear the pipeline state before emitting. 00327 HazardRec->Reset(); 00328 } 00329 HazardRec->EmitInstruction(SU); 00330 } 00331 00332 // Update DFA model. 00333 startNewCycle = ResourceModel->reserveResources(SU); 00334 00335 // Check the instruction group dispatch limit. 00336 // TODO: Check if this SU must end a dispatch group. 00337 IssueCount += SchedModel->getNumMicroOps(SU->getInstr()); 00338 if (startNewCycle) { 00339 DEBUG(dbgs() << "*** Max instrs at cycle " << CurrCycle << '\n'); 00340 bumpCycle(); 00341 } 00342 else 00343 DEBUG(dbgs() << "*** IssueCount " << IssueCount 00344 << " at cycle " << CurrCycle << '\n'); 00345 } 00346 00347 /// Release pending ready nodes in to the available queue. This makes them 00348 /// visible to heuristics. 00349 void ConvergingVLIWScheduler::SchedBoundary::releasePending() { 00350 // If the available queue is empty, it is safe to reset MinReadyCycle. 00351 if (Available.empty()) 00352 MinReadyCycle = UINT_MAX; 00353 00354 // Check to see if any of the pending instructions are ready to issue. If 00355 // so, add them to the available queue. 00356 for (unsigned i = 0, e = Pending.size(); i != e; ++i) { 00357 SUnit *SU = *(Pending.begin()+i); 00358 unsigned ReadyCycle = isTop() ? SU->TopReadyCycle : SU->BotReadyCycle; 00359 00360 if (ReadyCycle < MinReadyCycle) 00361 MinReadyCycle = ReadyCycle; 00362 00363 if (ReadyCycle > CurrCycle) 00364 continue; 00365 00366 if (checkHazard(SU)) 00367 continue; 00368 00369 Available.push(SU); 00370 Pending.remove(Pending.begin()+i); 00371 --i; --e; 00372 } 00373 CheckPending = false; 00374 } 00375 00376 /// Remove SU from the ready set for this boundary. 00377 void ConvergingVLIWScheduler::SchedBoundary::removeReady(SUnit *SU) { 00378 if (Available.isInQueue(SU)) 00379 Available.remove(Available.find(SU)); 00380 else { 00381 assert(Pending.isInQueue(SU) && "bad ready count"); 00382 Pending.remove(Pending.find(SU)); 00383 } 00384 } 00385 00386 /// If this queue only has one ready candidate, return it. As a side effect, 00387 /// advance the cycle until at least one node is ready. If multiple instructions 00388 /// are ready, return NULL. 00389 SUnit *ConvergingVLIWScheduler::SchedBoundary::pickOnlyChoice() { 00390 if (CheckPending) 00391 releasePending(); 00392 00393 for (unsigned i = 0; Available.empty(); ++i) { 00394 assert(i <= (HazardRec->getMaxLookAhead() + MaxMinLatency) && 00395 "permanent hazard"); (void)i; 00396 ResourceModel->reserveResources(0); 00397 bumpCycle(); 00398 releasePending(); 00399 } 00400 if (Available.size() == 1) 00401 return *Available.begin(); 00402 return NULL; 00403 } 00404 00405 #ifndef NDEBUG 00406 void ConvergingVLIWScheduler::traceCandidate(const char *Label, 00407 const ReadyQueue &Q, 00408 SUnit *SU, PressureElement P) { 00409 dbgs() << Label << " " << Q.getName() << " "; 00410 if (P.isValid()) 00411 dbgs() << DAG->TRI->getRegPressureSetName(P.PSetID) << ":" << P.UnitIncrease 00412 << " "; 00413 else 00414 dbgs() << " "; 00415 SU->dump(DAG); 00416 } 00417 #endif 00418 00419 /// getSingleUnscheduledPred - If there is exactly one unscheduled predecessor 00420 /// of SU, return it, otherwise return null. 00421 static SUnit *getSingleUnscheduledPred(SUnit *SU) { 00422 SUnit *OnlyAvailablePred = 0; 00423 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); 00424 I != E; ++I) { 00425 SUnit &Pred = *I->getSUnit(); 00426 if (!Pred.isScheduled) { 00427 // We found an available, but not scheduled, predecessor. If it's the 00428 // only one we have found, keep track of it... otherwise give up. 00429 if (OnlyAvailablePred && OnlyAvailablePred != &Pred) 00430 return 0; 00431 OnlyAvailablePred = &Pred; 00432 } 00433 } 00434 return OnlyAvailablePred; 00435 } 00436 00437 /// getSingleUnscheduledSucc - If there is exactly one unscheduled successor 00438 /// of SU, return it, otherwise return null. 00439 static SUnit *getSingleUnscheduledSucc(SUnit *SU) { 00440 SUnit *OnlyAvailableSucc = 0; 00441 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); 00442 I != E; ++I) { 00443 SUnit &Succ = *I->getSUnit(); 00444 if (!Succ.isScheduled) { 00445 // We found an available, but not scheduled, successor. If it's the 00446 // only one we have found, keep track of it... otherwise give up. 00447 if (OnlyAvailableSucc && OnlyAvailableSucc != &Succ) 00448 return 0; 00449 OnlyAvailableSucc = &Succ; 00450 } 00451 } 00452 return OnlyAvailableSucc; 00453 } 00454 00455 // Constants used to denote relative importance of 00456 // heuristic components for cost computation. 00457 static const unsigned PriorityOne = 200; 00458 static const unsigned PriorityTwo = 100; 00459 static const unsigned PriorityThree = 50; 00460 static const unsigned PriorityFour = 20; 00461 static const unsigned ScaleTwo = 10; 00462 static const unsigned FactorOne = 2; 00463 00464 /// Single point to compute overall scheduling cost. 00465 /// TODO: More heuristics will be used soon. 00466 int ConvergingVLIWScheduler::SchedulingCost(ReadyQueue &Q, SUnit *SU, 00467 SchedCandidate &Candidate, 00468 RegPressureDelta &Delta, 00469 bool verbose) { 00470 // Initial trivial priority. 00471 int ResCount = 1; 00472 00473 // Do not waste time on a node that is already scheduled. 00474 if (!SU || SU->isScheduled) 00475 return ResCount; 00476 00477 // Forced priority is high. 00478 if (SU->isScheduleHigh) 00479 ResCount += PriorityOne; 00480 00481 // Critical path first. 00482 if (Q.getID() == TopQID) { 00483 ResCount += (SU->getHeight() * ScaleTwo); 00484 00485 // If resources are available for it, multiply the 00486 // chance of scheduling. 00487 if (Top.ResourceModel->isResourceAvailable(SU)) 00488 ResCount <<= FactorOne; 00489 } else { 00490 ResCount += (SU->getDepth() * ScaleTwo); 00491 00492 // If resources are available for it, multiply the 00493 // chance of scheduling. 00494 if (Bot.ResourceModel->isResourceAvailable(SU)) 00495 ResCount <<= FactorOne; 00496 } 00497 00498 unsigned NumNodesBlocking = 0; 00499 if (Q.getID() == TopQID) { 00500 // How many SUs does it block from scheduling? 00501 // Look at all of the successors of this node. 00502 // Count the number of nodes that 00503 // this node is the sole unscheduled node for. 00504 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); 00505 I != E; ++I) 00506 if (getSingleUnscheduledPred(I->getSUnit()) == SU) 00507 ++NumNodesBlocking; 00508 } else { 00509 // How many unscheduled predecessors block this node? 00510 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); 00511 I != E; ++I) 00512 if (getSingleUnscheduledSucc(I->getSUnit()) == SU) 00513 ++NumNodesBlocking; 00514 } 00515 ResCount += (NumNodesBlocking * ScaleTwo); 00516 00517 // Factor in reg pressure as a heuristic. 00518 ResCount -= (Delta.Excess.UnitIncrease*PriorityThree); 00519 ResCount -= (Delta.CriticalMax.UnitIncrease*PriorityThree); 00520 00521 DEBUG(if (verbose) dbgs() << " Total(" << ResCount << ")"); 00522 00523 return ResCount; 00524 } 00525 00526 /// Pick the best candidate from the top queue. 00527 /// 00528 /// TODO: getMaxPressureDelta results can be mostly cached for each SUnit during 00529 /// DAG building. To adjust for the current scheduling location we need to 00530 /// maintain the number of vreg uses remaining to be top-scheduled. 00531 ConvergingVLIWScheduler::CandResult ConvergingVLIWScheduler:: 00532 pickNodeFromQueue(ReadyQueue &Q, const RegPressureTracker &RPTracker, 00533 SchedCandidate &Candidate) { 00534 DEBUG(Q.dump()); 00535 00536 // getMaxPressureDelta temporarily modifies the tracker. 00537 RegPressureTracker &TempTracker = const_cast<RegPressureTracker&>(RPTracker); 00538 00539 // BestSU remains NULL if no top candidates beat the best existing candidate. 00540 CandResult FoundCandidate = NoCand; 00541 for (ReadyQueue::iterator I = Q.begin(), E = Q.end(); I != E; ++I) { 00542 RegPressureDelta RPDelta; 00543 TempTracker.getMaxPressureDelta((*I)->getInstr(), RPDelta, 00544 DAG->getRegionCriticalPSets(), 00545 DAG->getRegPressure().MaxSetPressure); 00546 00547 int CurrentCost = SchedulingCost(Q, *I, Candidate, RPDelta, false); 00548 00549 // Initialize the candidate if needed. 00550 if (!Candidate.SU) { 00551 Candidate.SU = *I; 00552 Candidate.RPDelta = RPDelta; 00553 Candidate.SCost = CurrentCost; 00554 FoundCandidate = NodeOrder; 00555 continue; 00556 } 00557 00558 // Best cost. 00559 if (CurrentCost > Candidate.SCost) { 00560 DEBUG(traceCandidate("CCAND", Q, *I)); 00561 Candidate.SU = *I; 00562 Candidate.RPDelta = RPDelta; 00563 Candidate.SCost = CurrentCost; 00564 FoundCandidate = BestCost; 00565 continue; 00566 } 00567 00568 // Fall through to original instruction order. 00569 // Only consider node order if Candidate was chosen from this Q. 00570 if (FoundCandidate == NoCand) 00571 continue; 00572 } 00573 return FoundCandidate; 00574 } 00575 00576 /// Pick the best candidate node from either the top or bottom queue. 00577 SUnit *ConvergingVLIWScheduler::pickNodeBidrectional(bool &IsTopNode) { 00578 // Schedule as far as possible in the direction of no choice. This is most 00579 // efficient, but also provides the best heuristics for CriticalPSets. 00580 if (SUnit *SU = Bot.pickOnlyChoice()) { 00581 IsTopNode = false; 00582 return SU; 00583 } 00584 if (SUnit *SU = Top.pickOnlyChoice()) { 00585 IsTopNode = true; 00586 return SU; 00587 } 00588 SchedCandidate BotCand; 00589 // Prefer bottom scheduling when heuristics are silent. 00590 CandResult BotResult = pickNodeFromQueue(Bot.Available, 00591 DAG->getBotRPTracker(), BotCand); 00592 assert(BotResult != NoCand && "failed to find the first candidate"); 00593 00594 // If either Q has a single candidate that provides the least increase in 00595 // Excess pressure, we can immediately schedule from that Q. 00596 // 00597 // RegionCriticalPSets summarizes the pressure within the scheduled region and 00598 // affects picking from either Q. If scheduling in one direction must 00599 // increase pressure for one of the excess PSets, then schedule in that 00600 // direction first to provide more freedom in the other direction. 00601 if (BotResult == SingleExcess || BotResult == SingleCritical) { 00602 IsTopNode = false; 00603 return BotCand.SU; 00604 } 00605 // Check if the top Q has a better candidate. 00606 SchedCandidate TopCand; 00607 CandResult TopResult = pickNodeFromQueue(Top.Available, 00608 DAG->getTopRPTracker(), TopCand); 00609 assert(TopResult != NoCand && "failed to find the first candidate"); 00610 00611 if (TopResult == SingleExcess || TopResult == SingleCritical) { 00612 IsTopNode = true; 00613 return TopCand.SU; 00614 } 00615 // If either Q has a single candidate that minimizes pressure above the 00616 // original region's pressure pick it. 00617 if (BotResult == SingleMax) { 00618 IsTopNode = false; 00619 return BotCand.SU; 00620 } 00621 if (TopResult == SingleMax) { 00622 IsTopNode = true; 00623 return TopCand.SU; 00624 } 00625 if (TopCand.SCost > BotCand.SCost) { 00626 IsTopNode = true; 00627 return TopCand.SU; 00628 } 00629 // Otherwise prefer the bottom candidate in node order. 00630 IsTopNode = false; 00631 return BotCand.SU; 00632 } 00633 00634 /// Pick the best node to balance the schedule. Implements MachineSchedStrategy. 00635 SUnit *ConvergingVLIWScheduler::pickNode(bool &IsTopNode) { 00636 if (DAG->top() == DAG->bottom()) { 00637 assert(Top.Available.empty() && Top.Pending.empty() && 00638 Bot.Available.empty() && Bot.Pending.empty() && "ReadyQ garbage"); 00639 return NULL; 00640 } 00641 SUnit *SU; 00642 if (llvm::ForceTopDown) { 00643 SU = Top.pickOnlyChoice(); 00644 if (!SU) { 00645 SchedCandidate TopCand; 00646 CandResult TopResult = 00647 pickNodeFromQueue(Top.Available, DAG->getTopRPTracker(), TopCand); 00648 assert(TopResult != NoCand && "failed to find the first candidate"); 00649 (void)TopResult; 00650 SU = TopCand.SU; 00651 } 00652 IsTopNode = true; 00653 } else if (llvm::ForceBottomUp) { 00654 SU = Bot.pickOnlyChoice(); 00655 if (!SU) { 00656 SchedCandidate BotCand; 00657 CandResult BotResult = 00658 pickNodeFromQueue(Bot.Available, DAG->getBotRPTracker(), BotCand); 00659 assert(BotResult != NoCand && "failed to find the first candidate"); 00660 (void)BotResult; 00661 SU = BotCand.SU; 00662 } 00663 IsTopNode = false; 00664 } else { 00665 SU = pickNodeBidrectional(IsTopNode); 00666 } 00667 if (SU->isTopReady()) 00668 Top.removeReady(SU); 00669 if (SU->isBottomReady()) 00670 Bot.removeReady(SU); 00671 00672 DEBUG(dbgs() << "*** " << (IsTopNode ? "Top" : "Bottom") 00673 << " Scheduling Instruction in cycle " 00674 << (IsTopNode ? Top.CurrCycle : Bot.CurrCycle) << '\n'; 00675 SU->dump(DAG)); 00676 return SU; 00677 } 00678 00679 /// Update the scheduler's state after scheduling a node. This is the same node 00680 /// that was just returned by pickNode(). However, VLIWMachineScheduler needs 00681 /// to update it's state based on the current cycle before MachineSchedStrategy 00682 /// does. 00683 void ConvergingVLIWScheduler::schedNode(SUnit *SU, bool IsTopNode) { 00684 if (IsTopNode) { 00685 SU->TopReadyCycle = Top.CurrCycle; 00686 Top.bumpNode(SU); 00687 } else { 00688 SU->BotReadyCycle = Bot.CurrCycle; 00689 Bot.bumpNode(SU); 00690 } 00691 }