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 #include "HexagonMachineScheduler.h" 00016 #include "llvm/CodeGen/MachineLoopInfo.h" 00017 #include "llvm/IR/Function.h" 00018 00019 using namespace llvm; 00020 00021 #define DEBUG_TYPE "misched" 00022 00023 /// Platform-specific modifications to DAG. 00024 void VLIWMachineScheduler::postprocessDAG() { 00025 SUnit* LastSequentialCall = nullptr; 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::CFI_INSTRUCTION: 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 // Notify the scheduling strategy after updating the DAG. 00191 SchedImpl->schedNode(SU, IsTopNode); 00192 } 00193 assert(CurrentTop == CurrentBottom && "Nonempty unscheduled zone."); 00194 00195 placeDebugValues(); 00196 } 00197 00198 void ConvergingVLIWScheduler::initialize(ScheduleDAGMI *dag) { 00199 DAG = static_cast<VLIWMachineScheduler*>(dag); 00200 SchedModel = DAG->getSchedModel(); 00201 00202 Top.init(DAG, SchedModel); 00203 Bot.init(DAG, SchedModel); 00204 00205 // Initialize the HazardRecognizers. If itineraries don't exist, are empty, or 00206 // are disabled, then these HazardRecs will be disabled. 00207 const InstrItineraryData *Itin = DAG->getSchedModel()->getInstrItineraries(); 00208 const TargetMachine &TM = DAG->MF.getTarget(); 00209 delete Top.HazardRec; 00210 delete Bot.HazardRec; 00211 Top.HazardRec = 00212 TM.getSubtargetImpl()->getInstrInfo()->CreateTargetMIHazardRecognizer( 00213 Itin, DAG); 00214 Bot.HazardRec = 00215 TM.getSubtargetImpl()->getInstrInfo()->CreateTargetMIHazardRecognizer( 00216 Itin, DAG); 00217 00218 delete Top.ResourceModel; 00219 delete Bot.ResourceModel; 00220 Top.ResourceModel = new VLIWResourceModel(TM, DAG->getSchedModel()); 00221 Bot.ResourceModel = new VLIWResourceModel(TM, DAG->getSchedModel()); 00222 00223 assert((!llvm::ForceTopDown || !llvm::ForceBottomUp) && 00224 "-misched-topdown incompatible with -misched-bottomup"); 00225 } 00226 00227 void ConvergingVLIWScheduler::releaseTopNode(SUnit *SU) { 00228 if (SU->isScheduled) 00229 return; 00230 00231 for (SUnit::succ_iterator I = SU->Preds.begin(), E = SU->Preds.end(); 00232 I != E; ++I) { 00233 unsigned PredReadyCycle = I->getSUnit()->TopReadyCycle; 00234 unsigned MinLatency = I->getLatency(); 00235 #ifndef NDEBUG 00236 Top.MaxMinLatency = std::max(MinLatency, Top.MaxMinLatency); 00237 #endif 00238 if (SU->TopReadyCycle < PredReadyCycle + MinLatency) 00239 SU->TopReadyCycle = PredReadyCycle + MinLatency; 00240 } 00241 Top.releaseNode(SU, SU->TopReadyCycle); 00242 } 00243 00244 void ConvergingVLIWScheduler::releaseBottomNode(SUnit *SU) { 00245 if (SU->isScheduled) 00246 return; 00247 00248 assert(SU->getInstr() && "Scheduled SUnit must have instr"); 00249 00250 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); 00251 I != E; ++I) { 00252 unsigned SuccReadyCycle = I->getSUnit()->BotReadyCycle; 00253 unsigned MinLatency = I->getLatency(); 00254 #ifndef NDEBUG 00255 Bot.MaxMinLatency = std::max(MinLatency, Bot.MaxMinLatency); 00256 #endif 00257 if (SU->BotReadyCycle < SuccReadyCycle + MinLatency) 00258 SU->BotReadyCycle = SuccReadyCycle + MinLatency; 00259 } 00260 Bot.releaseNode(SU, SU->BotReadyCycle); 00261 } 00262 00263 /// Does this SU have a hazard within the current instruction group. 00264 /// 00265 /// The scheduler supports two modes of hazard recognition. The first is the 00266 /// ScheduleHazardRecognizer API. It is a fully general hazard recognizer that 00267 /// supports highly complicated in-order reservation tables 00268 /// (ScoreboardHazardRecognizer) and arbitrary target-specific logic. 00269 /// 00270 /// The second is a streamlined mechanism that checks for hazards based on 00271 /// simple counters that the scheduler itself maintains. It explicitly checks 00272 /// for instruction dispatch limitations, including the number of micro-ops that 00273 /// can dispatch per cycle. 00274 /// 00275 /// TODO: Also check whether the SU must start a new group. 00276 bool ConvergingVLIWScheduler::VLIWSchedBoundary::checkHazard(SUnit *SU) { 00277 if (HazardRec->isEnabled()) 00278 return HazardRec->getHazardType(SU) != ScheduleHazardRecognizer::NoHazard; 00279 00280 unsigned uops = SchedModel->getNumMicroOps(SU->getInstr()); 00281 if (IssueCount + uops > SchedModel->getIssueWidth()) 00282 return true; 00283 00284 return false; 00285 } 00286 00287 void ConvergingVLIWScheduler::VLIWSchedBoundary::releaseNode(SUnit *SU, 00288 unsigned ReadyCycle) { 00289 if (ReadyCycle < MinReadyCycle) 00290 MinReadyCycle = ReadyCycle; 00291 00292 // Check for interlocks first. For the purpose of other heuristics, an 00293 // instruction that cannot issue appears as if it's not in the ReadyQueue. 00294 if (ReadyCycle > CurrCycle || checkHazard(SU)) 00295 00296 Pending.push(SU); 00297 else 00298 Available.push(SU); 00299 } 00300 00301 /// Move the boundary of scheduled code by one cycle. 00302 void ConvergingVLIWScheduler::VLIWSchedBoundary::bumpCycle() { 00303 unsigned Width = SchedModel->getIssueWidth(); 00304 IssueCount = (IssueCount <= Width) ? 0 : IssueCount - Width; 00305 00306 assert(MinReadyCycle < UINT_MAX && "MinReadyCycle uninitialized"); 00307 unsigned NextCycle = std::max(CurrCycle + 1, MinReadyCycle); 00308 00309 if (!HazardRec->isEnabled()) { 00310 // Bypass HazardRec virtual calls. 00311 CurrCycle = NextCycle; 00312 } else { 00313 // Bypass getHazardType calls in case of long latency. 00314 for (; CurrCycle != NextCycle; ++CurrCycle) { 00315 if (isTop()) 00316 HazardRec->AdvanceCycle(); 00317 else 00318 HazardRec->RecedeCycle(); 00319 } 00320 } 00321 CheckPending = true; 00322 00323 DEBUG(dbgs() << "*** " << Available.getName() << " cycle " 00324 << CurrCycle << '\n'); 00325 } 00326 00327 /// Move the boundary of scheduled code by one SUnit. 00328 void ConvergingVLIWScheduler::VLIWSchedBoundary::bumpNode(SUnit *SU) { 00329 bool startNewCycle = false; 00330 00331 // Update the reservation table. 00332 if (HazardRec->isEnabled()) { 00333 if (!isTop() && SU->isCall) { 00334 // Calls are scheduled with their preceding instructions. For bottom-up 00335 // scheduling, clear the pipeline state before emitting. 00336 HazardRec->Reset(); 00337 } 00338 HazardRec->EmitInstruction(SU); 00339 } 00340 00341 // Update DFA model. 00342 startNewCycle = ResourceModel->reserveResources(SU); 00343 00344 // Check the instruction group dispatch limit. 00345 // TODO: Check if this SU must end a dispatch group. 00346 IssueCount += SchedModel->getNumMicroOps(SU->getInstr()); 00347 if (startNewCycle) { 00348 DEBUG(dbgs() << "*** Max instrs at cycle " << CurrCycle << '\n'); 00349 bumpCycle(); 00350 } 00351 else 00352 DEBUG(dbgs() << "*** IssueCount " << IssueCount 00353 << " at cycle " << CurrCycle << '\n'); 00354 } 00355 00356 /// Release pending ready nodes in to the available queue. This makes them 00357 /// visible to heuristics. 00358 void ConvergingVLIWScheduler::VLIWSchedBoundary::releasePending() { 00359 // If the available queue is empty, it is safe to reset MinReadyCycle. 00360 if (Available.empty()) 00361 MinReadyCycle = UINT_MAX; 00362 00363 // Check to see if any of the pending instructions are ready to issue. If 00364 // so, add them to the available queue. 00365 for (unsigned i = 0, e = Pending.size(); i != e; ++i) { 00366 SUnit *SU = *(Pending.begin()+i); 00367 unsigned ReadyCycle = isTop() ? SU->TopReadyCycle : SU->BotReadyCycle; 00368 00369 if (ReadyCycle < MinReadyCycle) 00370 MinReadyCycle = ReadyCycle; 00371 00372 if (ReadyCycle > CurrCycle) 00373 continue; 00374 00375 if (checkHazard(SU)) 00376 continue; 00377 00378 Available.push(SU); 00379 Pending.remove(Pending.begin()+i); 00380 --i; --e; 00381 } 00382 CheckPending = false; 00383 } 00384 00385 /// Remove SU from the ready set for this boundary. 00386 void ConvergingVLIWScheduler::VLIWSchedBoundary::removeReady(SUnit *SU) { 00387 if (Available.isInQueue(SU)) 00388 Available.remove(Available.find(SU)); 00389 else { 00390 assert(Pending.isInQueue(SU) && "bad ready count"); 00391 Pending.remove(Pending.find(SU)); 00392 } 00393 } 00394 00395 /// If this queue only has one ready candidate, return it. As a side effect, 00396 /// advance the cycle until at least one node is ready. If multiple instructions 00397 /// are ready, return NULL. 00398 SUnit *ConvergingVLIWScheduler::VLIWSchedBoundary::pickOnlyChoice() { 00399 if (CheckPending) 00400 releasePending(); 00401 00402 for (unsigned i = 0; Available.empty(); ++i) { 00403 assert(i <= (HazardRec->getMaxLookAhead() + MaxMinLatency) && 00404 "permanent hazard"); (void)i; 00405 ResourceModel->reserveResources(nullptr); 00406 bumpCycle(); 00407 releasePending(); 00408 } 00409 if (Available.size() == 1) 00410 return *Available.begin(); 00411 return nullptr; 00412 } 00413 00414 #ifndef NDEBUG 00415 void ConvergingVLIWScheduler::traceCandidate(const char *Label, 00416 const ReadyQueue &Q, 00417 SUnit *SU, PressureChange P) { 00418 dbgs() << Label << " " << Q.getName() << " "; 00419 if (P.isValid()) 00420 dbgs() << DAG->TRI->getRegPressureSetName(P.getPSet()) << ":" 00421 << P.getUnitInc() << " "; 00422 else 00423 dbgs() << " "; 00424 SU->dump(DAG); 00425 } 00426 #endif 00427 00428 /// getSingleUnscheduledPred - If there is exactly one unscheduled predecessor 00429 /// of SU, return it, otherwise return null. 00430 static SUnit *getSingleUnscheduledPred(SUnit *SU) { 00431 SUnit *OnlyAvailablePred = nullptr; 00432 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); 00433 I != E; ++I) { 00434 SUnit &Pred = *I->getSUnit(); 00435 if (!Pred.isScheduled) { 00436 // We found an available, but not scheduled, predecessor. If it's the 00437 // only one we have found, keep track of it... otherwise give up. 00438 if (OnlyAvailablePred && OnlyAvailablePred != &Pred) 00439 return nullptr; 00440 OnlyAvailablePred = &Pred; 00441 } 00442 } 00443 return OnlyAvailablePred; 00444 } 00445 00446 /// getSingleUnscheduledSucc - If there is exactly one unscheduled successor 00447 /// of SU, return it, otherwise return null. 00448 static SUnit *getSingleUnscheduledSucc(SUnit *SU) { 00449 SUnit *OnlyAvailableSucc = nullptr; 00450 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); 00451 I != E; ++I) { 00452 SUnit &Succ = *I->getSUnit(); 00453 if (!Succ.isScheduled) { 00454 // We found an available, but not scheduled, successor. If it's the 00455 // only one we have found, keep track of it... otherwise give up. 00456 if (OnlyAvailableSucc && OnlyAvailableSucc != &Succ) 00457 return nullptr; 00458 OnlyAvailableSucc = &Succ; 00459 } 00460 } 00461 return OnlyAvailableSucc; 00462 } 00463 00464 // Constants used to denote relative importance of 00465 // heuristic components for cost computation. 00466 static const unsigned PriorityOne = 200; 00467 static const unsigned PriorityTwo = 50; 00468 static const unsigned ScaleTwo = 10; 00469 static const unsigned FactorOne = 2; 00470 00471 /// Single point to compute overall scheduling cost. 00472 /// TODO: More heuristics will be used soon. 00473 int ConvergingVLIWScheduler::SchedulingCost(ReadyQueue &Q, SUnit *SU, 00474 SchedCandidate &Candidate, 00475 RegPressureDelta &Delta, 00476 bool verbose) { 00477 // Initial trivial priority. 00478 int ResCount = 1; 00479 00480 // Do not waste time on a node that is already scheduled. 00481 if (!SU || SU->isScheduled) 00482 return ResCount; 00483 00484 // Forced priority is high. 00485 if (SU->isScheduleHigh) 00486 ResCount += PriorityOne; 00487 00488 // Critical path first. 00489 if (Q.getID() == TopQID) { 00490 ResCount += (SU->getHeight() * ScaleTwo); 00491 00492 // If resources are available for it, multiply the 00493 // chance of scheduling. 00494 if (Top.ResourceModel->isResourceAvailable(SU)) 00495 ResCount <<= FactorOne; 00496 } else { 00497 ResCount += (SU->getDepth() * ScaleTwo); 00498 00499 // If resources are available for it, multiply the 00500 // chance of scheduling. 00501 if (Bot.ResourceModel->isResourceAvailable(SU)) 00502 ResCount <<= FactorOne; 00503 } 00504 00505 unsigned NumNodesBlocking = 0; 00506 if (Q.getID() == TopQID) { 00507 // How many SUs does it block from scheduling? 00508 // Look at all of the successors of this node. 00509 // Count the number of nodes that 00510 // this node is the sole unscheduled node for. 00511 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); 00512 I != E; ++I) 00513 if (getSingleUnscheduledPred(I->getSUnit()) == SU) 00514 ++NumNodesBlocking; 00515 } else { 00516 // How many unscheduled predecessors block this node? 00517 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); 00518 I != E; ++I) 00519 if (getSingleUnscheduledSucc(I->getSUnit()) == SU) 00520 ++NumNodesBlocking; 00521 } 00522 ResCount += (NumNodesBlocking * ScaleTwo); 00523 00524 // Factor in reg pressure as a heuristic. 00525 ResCount -= (Delta.Excess.getUnitInc()*PriorityTwo); 00526 ResCount -= (Delta.CriticalMax.getUnitInc()*PriorityTwo); 00527 00528 DEBUG(if (verbose) dbgs() << " Total(" << ResCount << ")"); 00529 00530 return ResCount; 00531 } 00532 00533 /// Pick the best candidate from the top queue. 00534 /// 00535 /// TODO: getMaxPressureDelta results can be mostly cached for each SUnit during 00536 /// DAG building. To adjust for the current scheduling location we need to 00537 /// maintain the number of vreg uses remaining to be top-scheduled. 00538 ConvergingVLIWScheduler::CandResult ConvergingVLIWScheduler:: 00539 pickNodeFromQueue(ReadyQueue &Q, const RegPressureTracker &RPTracker, 00540 SchedCandidate &Candidate) { 00541 DEBUG(Q.dump()); 00542 00543 // getMaxPressureDelta temporarily modifies the tracker. 00544 RegPressureTracker &TempTracker = const_cast<RegPressureTracker&>(RPTracker); 00545 00546 // BestSU remains NULL if no top candidates beat the best existing candidate. 00547 CandResult FoundCandidate = NoCand; 00548 for (ReadyQueue::iterator I = Q.begin(), E = Q.end(); I != E; ++I) { 00549 RegPressureDelta RPDelta; 00550 TempTracker.getMaxPressureDelta((*I)->getInstr(), RPDelta, 00551 DAG->getRegionCriticalPSets(), 00552 DAG->getRegPressure().MaxSetPressure); 00553 00554 int CurrentCost = SchedulingCost(Q, *I, Candidate, RPDelta, false); 00555 00556 // Initialize the candidate if needed. 00557 if (!Candidate.SU) { 00558 Candidate.SU = *I; 00559 Candidate.RPDelta = RPDelta; 00560 Candidate.SCost = CurrentCost; 00561 FoundCandidate = NodeOrder; 00562 continue; 00563 } 00564 00565 // Best cost. 00566 if (CurrentCost > Candidate.SCost) { 00567 DEBUG(traceCandidate("CCAND", Q, *I)); 00568 Candidate.SU = *I; 00569 Candidate.RPDelta = RPDelta; 00570 Candidate.SCost = CurrentCost; 00571 FoundCandidate = BestCost; 00572 continue; 00573 } 00574 00575 // Fall through to original instruction order. 00576 // Only consider node order if Candidate was chosen from this Q. 00577 if (FoundCandidate == NoCand) 00578 continue; 00579 } 00580 return FoundCandidate; 00581 } 00582 00583 /// Pick the best candidate node from either the top or bottom queue. 00584 SUnit *ConvergingVLIWScheduler::pickNodeBidrectional(bool &IsTopNode) { 00585 // Schedule as far as possible in the direction of no choice. This is most 00586 // efficient, but also provides the best heuristics for CriticalPSets. 00587 if (SUnit *SU = Bot.pickOnlyChoice()) { 00588 IsTopNode = false; 00589 return SU; 00590 } 00591 if (SUnit *SU = Top.pickOnlyChoice()) { 00592 IsTopNode = true; 00593 return SU; 00594 } 00595 SchedCandidate BotCand; 00596 // Prefer bottom scheduling when heuristics are silent. 00597 CandResult BotResult = pickNodeFromQueue(Bot.Available, 00598 DAG->getBotRPTracker(), BotCand); 00599 assert(BotResult != NoCand && "failed to find the first candidate"); 00600 00601 // If either Q has a single candidate that provides the least increase in 00602 // Excess pressure, we can immediately schedule from that Q. 00603 // 00604 // RegionCriticalPSets summarizes the pressure within the scheduled region and 00605 // affects picking from either Q. If scheduling in one direction must 00606 // increase pressure for one of the excess PSets, then schedule in that 00607 // direction first to provide more freedom in the other direction. 00608 if (BotResult == SingleExcess || BotResult == SingleCritical) { 00609 IsTopNode = false; 00610 return BotCand.SU; 00611 } 00612 // Check if the top Q has a better candidate. 00613 SchedCandidate TopCand; 00614 CandResult TopResult = pickNodeFromQueue(Top.Available, 00615 DAG->getTopRPTracker(), TopCand); 00616 assert(TopResult != NoCand && "failed to find the first candidate"); 00617 00618 if (TopResult == SingleExcess || TopResult == SingleCritical) { 00619 IsTopNode = true; 00620 return TopCand.SU; 00621 } 00622 // If either Q has a single candidate that minimizes pressure above the 00623 // original region's pressure pick it. 00624 if (BotResult == SingleMax) { 00625 IsTopNode = false; 00626 return BotCand.SU; 00627 } 00628 if (TopResult == SingleMax) { 00629 IsTopNode = true; 00630 return TopCand.SU; 00631 } 00632 if (TopCand.SCost > BotCand.SCost) { 00633 IsTopNode = true; 00634 return TopCand.SU; 00635 } 00636 // Otherwise prefer the bottom candidate in node order. 00637 IsTopNode = false; 00638 return BotCand.SU; 00639 } 00640 00641 /// Pick the best node to balance the schedule. Implements MachineSchedStrategy. 00642 SUnit *ConvergingVLIWScheduler::pickNode(bool &IsTopNode) { 00643 if (DAG->top() == DAG->bottom()) { 00644 assert(Top.Available.empty() && Top.Pending.empty() && 00645 Bot.Available.empty() && Bot.Pending.empty() && "ReadyQ garbage"); 00646 return nullptr; 00647 } 00648 SUnit *SU; 00649 if (llvm::ForceTopDown) { 00650 SU = Top.pickOnlyChoice(); 00651 if (!SU) { 00652 SchedCandidate TopCand; 00653 CandResult TopResult = 00654 pickNodeFromQueue(Top.Available, DAG->getTopRPTracker(), TopCand); 00655 assert(TopResult != NoCand && "failed to find the first candidate"); 00656 (void)TopResult; 00657 SU = TopCand.SU; 00658 } 00659 IsTopNode = true; 00660 } else if (llvm::ForceBottomUp) { 00661 SU = Bot.pickOnlyChoice(); 00662 if (!SU) { 00663 SchedCandidate BotCand; 00664 CandResult BotResult = 00665 pickNodeFromQueue(Bot.Available, DAG->getBotRPTracker(), BotCand); 00666 assert(BotResult != NoCand && "failed to find the first candidate"); 00667 (void)BotResult; 00668 SU = BotCand.SU; 00669 } 00670 IsTopNode = false; 00671 } else { 00672 SU = pickNodeBidrectional(IsTopNode); 00673 } 00674 if (SU->isTopReady()) 00675 Top.removeReady(SU); 00676 if (SU->isBottomReady()) 00677 Bot.removeReady(SU); 00678 00679 DEBUG(dbgs() << "*** " << (IsTopNode ? "Top" : "Bottom") 00680 << " Scheduling Instruction in cycle " 00681 << (IsTopNode ? Top.CurrCycle : Bot.CurrCycle) << '\n'; 00682 SU->dump(DAG)); 00683 return SU; 00684 } 00685 00686 /// Update the scheduler's state after scheduling a node. This is the same node 00687 /// that was just returned by pickNode(). However, VLIWMachineScheduler needs 00688 /// to update it's state based on the current cycle before MachineSchedStrategy 00689 /// does. 00690 void ConvergingVLIWScheduler::schedNode(SUnit *SU, bool IsTopNode) { 00691 if (IsTopNode) { 00692 SU->TopReadyCycle = Top.CurrCycle; 00693 Top.bumpNode(SU); 00694 } else { 00695 SU->BotReadyCycle = Bot.CurrCycle; 00696 Bot.bumpNode(SU); 00697 } 00698 }