ResourcePriorityQueue.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. //===- ResourcePriorityQueue.cpp - A DFA-oriented priority queue -*- C++ -*-==//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the ResourcePriorityQueue class, which is a
  10. // SchedulingPriorityQueue that prioritizes instructions using DFA state to
  11. // reduce the length of the critical path through the basic block
  12. // on VLIW platforms.
  13. // The scheduler is basically a top-down adaptable list scheduler with DFA
  14. // resource tracking added to the cost function.
  15. // DFA is queried as a state machine to model "packets/bundles" during
  16. // schedule. Currently packets/bundles are discarded at the end of
  17. // scheduling, affecting only order of instructions.
  18. //
  19. //===----------------------------------------------------------------------===//
  20. #include "llvm/CodeGen/ResourcePriorityQueue.h"
  21. #include "llvm/CodeGen/DFAPacketizer.h"
  22. #include "llvm/CodeGen/MachineInstr.h"
  23. #include "llvm/CodeGen/SelectionDAGISel.h"
  24. #include "llvm/CodeGen/SelectionDAGNodes.h"
  25. #include "llvm/CodeGen/TargetInstrInfo.h"
  26. #include "llvm/CodeGen/TargetLowering.h"
  27. #include "llvm/CodeGen/TargetRegisterInfo.h"
  28. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  29. #include "llvm/Support/CommandLine.h"
  30. #include "llvm/Support/Debug.h"
  31. #include "llvm/Support/raw_ostream.h"
  32. #include "llvm/Target/TargetMachine.h"
  33. using namespace llvm;
  34. #define DEBUG_TYPE "scheduler"
  35. static cl::opt<bool> DisableDFASched("disable-dfa-sched", cl::Hidden,
  36. cl::ZeroOrMore, cl::init(false),
  37. cl::desc("Disable use of DFA during scheduling"));
  38. static cl::opt<int> RegPressureThreshold(
  39. "dfa-sched-reg-pressure-threshold", cl::Hidden, cl::ZeroOrMore, cl::init(5),
  40. cl::desc("Track reg pressure and switch priority to in-depth"));
  41. ResourcePriorityQueue::ResourcePriorityQueue(SelectionDAGISel *IS)
  42. : Picker(this), InstrItins(IS->MF->getSubtarget().getInstrItineraryData()) {
  43. const TargetSubtargetInfo &STI = IS->MF->getSubtarget();
  44. TRI = STI.getRegisterInfo();
  45. TLI = IS->TLI;
  46. TII = STI.getInstrInfo();
  47. ResourcesModel.reset(TII->CreateTargetScheduleState(STI));
  48. // This hard requirement could be relaxed, but for now
  49. // do not let it proceed.
  50. assert(ResourcesModel && "Unimplemented CreateTargetScheduleState.");
  51. unsigned NumRC = TRI->getNumRegClasses();
  52. RegLimit.resize(NumRC);
  53. RegPressure.resize(NumRC);
  54. std::fill(RegLimit.begin(), RegLimit.end(), 0);
  55. std::fill(RegPressure.begin(), RegPressure.end(), 0);
  56. for (const TargetRegisterClass *RC : TRI->regclasses())
  57. RegLimit[RC->getID()] = TRI->getRegPressureLimit(RC, *IS->MF);
  58. ParallelLiveRanges = 0;
  59. HorizontalVerticalBalance = 0;
  60. }
  61. unsigned
  62. ResourcePriorityQueue::numberRCValPredInSU(SUnit *SU, unsigned RCId) {
  63. unsigned NumberDeps = 0;
  64. for (SDep &Pred : SU->Preds) {
  65. if (Pred.isCtrl())
  66. continue;
  67. SUnit *PredSU = Pred.getSUnit();
  68. const SDNode *ScegN = PredSU->getNode();
  69. if (!ScegN)
  70. continue;
  71. // If value is passed to CopyToReg, it is probably
  72. // live outside BB.
  73. switch (ScegN->getOpcode()) {
  74. default: break;
  75. case ISD::TokenFactor: break;
  76. case ISD::CopyFromReg: NumberDeps++; break;
  77. case ISD::CopyToReg: break;
  78. case ISD::INLINEASM: break;
  79. case ISD::INLINEASM_BR: break;
  80. }
  81. if (!ScegN->isMachineOpcode())
  82. continue;
  83. for (unsigned i = 0, e = ScegN->getNumValues(); i != e; ++i) {
  84. MVT VT = ScegN->getSimpleValueType(i);
  85. if (TLI->isTypeLegal(VT)
  86. && (TLI->getRegClassFor(VT)->getID() == RCId)) {
  87. NumberDeps++;
  88. break;
  89. }
  90. }
  91. }
  92. return NumberDeps;
  93. }
  94. unsigned ResourcePriorityQueue::numberRCValSuccInSU(SUnit *SU,
  95. unsigned RCId) {
  96. unsigned NumberDeps = 0;
  97. for (const SDep &Succ : SU->Succs) {
  98. if (Succ.isCtrl())
  99. continue;
  100. SUnit *SuccSU = Succ.getSUnit();
  101. const SDNode *ScegN = SuccSU->getNode();
  102. if (!ScegN)
  103. continue;
  104. // If value is passed to CopyToReg, it is probably
  105. // live outside BB.
  106. switch (ScegN->getOpcode()) {
  107. default: break;
  108. case ISD::TokenFactor: break;
  109. case ISD::CopyFromReg: break;
  110. case ISD::CopyToReg: NumberDeps++; break;
  111. case ISD::INLINEASM: break;
  112. case ISD::INLINEASM_BR: break;
  113. }
  114. if (!ScegN->isMachineOpcode())
  115. continue;
  116. for (unsigned i = 0, e = ScegN->getNumOperands(); i != e; ++i) {
  117. const SDValue &Op = ScegN->getOperand(i);
  118. MVT VT = Op.getNode()->getSimpleValueType(Op.getResNo());
  119. if (TLI->isTypeLegal(VT)
  120. && (TLI->getRegClassFor(VT)->getID() == RCId)) {
  121. NumberDeps++;
  122. break;
  123. }
  124. }
  125. }
  126. return NumberDeps;
  127. }
  128. static unsigned numberCtrlDepsInSU(SUnit *SU) {
  129. unsigned NumberDeps = 0;
  130. for (const SDep &Succ : SU->Succs)
  131. if (Succ.isCtrl())
  132. NumberDeps++;
  133. return NumberDeps;
  134. }
  135. static unsigned numberCtrlPredInSU(SUnit *SU) {
  136. unsigned NumberDeps = 0;
  137. for (SDep &Pred : SU->Preds)
  138. if (Pred.isCtrl())
  139. NumberDeps++;
  140. return NumberDeps;
  141. }
  142. ///
  143. /// Initialize nodes.
  144. ///
  145. void ResourcePriorityQueue::initNodes(std::vector<SUnit> &sunits) {
  146. SUnits = &sunits;
  147. NumNodesSolelyBlocking.resize(SUnits->size(), 0);
  148. for (SUnit &SU : *SUnits) {
  149. initNumRegDefsLeft(&SU);
  150. SU.NodeQueueId = 0;
  151. }
  152. }
  153. /// This heuristic is used if DFA scheduling is not desired
  154. /// for some VLIW platform.
  155. bool resource_sort::operator()(const SUnit *LHS, const SUnit *RHS) const {
  156. // The isScheduleHigh flag allows nodes with wraparound dependencies that
  157. // cannot easily be modeled as edges with latencies to be scheduled as
  158. // soon as possible in a top-down schedule.
  159. if (LHS->isScheduleHigh && !RHS->isScheduleHigh)
  160. return false;
  161. if (!LHS->isScheduleHigh && RHS->isScheduleHigh)
  162. return true;
  163. unsigned LHSNum = LHS->NodeNum;
  164. unsigned RHSNum = RHS->NodeNum;
  165. // The most important heuristic is scheduling the critical path.
  166. unsigned LHSLatency = PQ->getLatency(LHSNum);
  167. unsigned RHSLatency = PQ->getLatency(RHSNum);
  168. if (LHSLatency < RHSLatency) return true;
  169. if (LHSLatency > RHSLatency) return false;
  170. // After that, if two nodes have identical latencies, look to see if one will
  171. // unblock more other nodes than the other.
  172. unsigned LHSBlocked = PQ->getNumSolelyBlockNodes(LHSNum);
  173. unsigned RHSBlocked = PQ->getNumSolelyBlockNodes(RHSNum);
  174. if (LHSBlocked < RHSBlocked) return true;
  175. if (LHSBlocked > RHSBlocked) return false;
  176. // Finally, just to provide a stable ordering, use the node number as a
  177. // deciding factor.
  178. return LHSNum < RHSNum;
  179. }
  180. /// getSingleUnscheduledPred - If there is exactly one unscheduled predecessor
  181. /// of SU, return it, otherwise return null.
  182. SUnit *ResourcePriorityQueue::getSingleUnscheduledPred(SUnit *SU) {
  183. SUnit *OnlyAvailablePred = nullptr;
  184. for (const SDep &Pred : SU->Preds) {
  185. SUnit &PredSU = *Pred.getSUnit();
  186. if (!PredSU.isScheduled) {
  187. // We found an available, but not scheduled, predecessor. If it's the
  188. // only one we have found, keep track of it... otherwise give up.
  189. if (OnlyAvailablePred && OnlyAvailablePred != &PredSU)
  190. return nullptr;
  191. OnlyAvailablePred = &PredSU;
  192. }
  193. }
  194. return OnlyAvailablePred;
  195. }
  196. void ResourcePriorityQueue::push(SUnit *SU) {
  197. // Look at all of the successors of this node. Count the number of nodes that
  198. // this node is the sole unscheduled node for.
  199. unsigned NumNodesBlocking = 0;
  200. for (const SDep &Succ : SU->Succs)
  201. if (getSingleUnscheduledPred(Succ.getSUnit()) == SU)
  202. ++NumNodesBlocking;
  203. NumNodesSolelyBlocking[SU->NodeNum] = NumNodesBlocking;
  204. Queue.push_back(SU);
  205. }
  206. /// Check if scheduling of this SU is possible
  207. /// in the current packet.
  208. bool ResourcePriorityQueue::isResourceAvailable(SUnit *SU) {
  209. if (!SU || !SU->getNode())
  210. return false;
  211. // If this is a compound instruction,
  212. // it is likely to be a call. Do not delay it.
  213. if (SU->getNode()->getGluedNode())
  214. return true;
  215. // First see if the pipeline could receive this instruction
  216. // in the current cycle.
  217. if (SU->getNode()->isMachineOpcode())
  218. switch (SU->getNode()->getMachineOpcode()) {
  219. default:
  220. if (!ResourcesModel->canReserveResources(&TII->get(
  221. SU->getNode()->getMachineOpcode())))
  222. return false;
  223. break;
  224. case TargetOpcode::EXTRACT_SUBREG:
  225. case TargetOpcode::INSERT_SUBREG:
  226. case TargetOpcode::SUBREG_TO_REG:
  227. case TargetOpcode::REG_SEQUENCE:
  228. case TargetOpcode::IMPLICIT_DEF:
  229. break;
  230. }
  231. // Now see if there are no other dependencies
  232. // to instructions already in the packet.
  233. for (const SUnit *S : Packet)
  234. for (const SDep &Succ : S->Succs) {
  235. // Since we do not add pseudos to packets, might as well
  236. // ignore order deps.
  237. if (Succ.isCtrl())
  238. continue;
  239. if (Succ.getSUnit() == SU)
  240. return false;
  241. }
  242. return true;
  243. }
  244. /// Keep track of available resources.
  245. void ResourcePriorityQueue::reserveResources(SUnit *SU) {
  246. // If this SU does not fit in the packet
  247. // start a new one.
  248. if (!isResourceAvailable(SU) || SU->getNode()->getGluedNode()) {
  249. ResourcesModel->clearResources();
  250. Packet.clear();
  251. }
  252. if (SU->getNode() && SU->getNode()->isMachineOpcode()) {
  253. switch (SU->getNode()->getMachineOpcode()) {
  254. default:
  255. ResourcesModel->reserveResources(&TII->get(
  256. SU->getNode()->getMachineOpcode()));
  257. break;
  258. case TargetOpcode::EXTRACT_SUBREG:
  259. case TargetOpcode::INSERT_SUBREG:
  260. case TargetOpcode::SUBREG_TO_REG:
  261. case TargetOpcode::REG_SEQUENCE:
  262. case TargetOpcode::IMPLICIT_DEF:
  263. break;
  264. }
  265. Packet.push_back(SU);
  266. }
  267. // Forcefully end packet for PseudoOps.
  268. else {
  269. ResourcesModel->clearResources();
  270. Packet.clear();
  271. }
  272. // If packet is now full, reset the state so in the next cycle
  273. // we start fresh.
  274. if (Packet.size() >= InstrItins->SchedModel.IssueWidth) {
  275. ResourcesModel->clearResources();
  276. Packet.clear();
  277. }
  278. }
  279. int ResourcePriorityQueue::rawRegPressureDelta(SUnit *SU, unsigned RCId) {
  280. int RegBalance = 0;
  281. if (!SU || !SU->getNode() || !SU->getNode()->isMachineOpcode())
  282. return RegBalance;
  283. // Gen estimate.
  284. for (unsigned i = 0, e = SU->getNode()->getNumValues(); i != e; ++i) {
  285. MVT VT = SU->getNode()->getSimpleValueType(i);
  286. if (TLI->isTypeLegal(VT)
  287. && TLI->getRegClassFor(VT)
  288. && TLI->getRegClassFor(VT)->getID() == RCId)
  289. RegBalance += numberRCValSuccInSU(SU, RCId);
  290. }
  291. // Kill estimate.
  292. for (unsigned i = 0, e = SU->getNode()->getNumOperands(); i != e; ++i) {
  293. const SDValue &Op = SU->getNode()->getOperand(i);
  294. MVT VT = Op.getNode()->getSimpleValueType(Op.getResNo());
  295. if (isa<ConstantSDNode>(Op.getNode()))
  296. continue;
  297. if (TLI->isTypeLegal(VT) && TLI->getRegClassFor(VT)
  298. && TLI->getRegClassFor(VT)->getID() == RCId)
  299. RegBalance -= numberRCValPredInSU(SU, RCId);
  300. }
  301. return RegBalance;
  302. }
  303. /// Estimates change in reg pressure from this SU.
  304. /// It is achieved by trivial tracking of defined
  305. /// and used vregs in dependent instructions.
  306. /// The RawPressure flag makes this function to ignore
  307. /// existing reg file sizes, and report raw def/use
  308. /// balance.
  309. int ResourcePriorityQueue::regPressureDelta(SUnit *SU, bool RawPressure) {
  310. int RegBalance = 0;
  311. if (!SU || !SU->getNode() || !SU->getNode()->isMachineOpcode())
  312. return RegBalance;
  313. if (RawPressure) {
  314. for (const TargetRegisterClass *RC : TRI->regclasses())
  315. RegBalance += rawRegPressureDelta(SU, RC->getID());
  316. }
  317. else {
  318. for (const TargetRegisterClass *RC : TRI->regclasses()) {
  319. if ((RegPressure[RC->getID()] +
  320. rawRegPressureDelta(SU, RC->getID()) > 0) &&
  321. (RegPressure[RC->getID()] +
  322. rawRegPressureDelta(SU, RC->getID()) >= RegLimit[RC->getID()]))
  323. RegBalance += rawRegPressureDelta(SU, RC->getID());
  324. }
  325. }
  326. return RegBalance;
  327. }
  328. // Constants used to denote relative importance of
  329. // heuristic components for cost computation.
  330. static const unsigned PriorityOne = 200;
  331. static const unsigned PriorityTwo = 50;
  332. static const unsigned PriorityThree = 15;
  333. static const unsigned PriorityFour = 5;
  334. static const unsigned ScaleOne = 20;
  335. static const unsigned ScaleTwo = 10;
  336. static const unsigned ScaleThree = 5;
  337. static const unsigned FactorOne = 2;
  338. /// Returns single number reflecting benefit of scheduling SU
  339. /// in the current cycle.
  340. int ResourcePriorityQueue::SUSchedulingCost(SUnit *SU) {
  341. // Initial trivial priority.
  342. int ResCount = 1;
  343. // Do not waste time on a node that is already scheduled.
  344. if (SU->isScheduled)
  345. return ResCount;
  346. // Forced priority is high.
  347. if (SU->isScheduleHigh)
  348. ResCount += PriorityOne;
  349. // Adaptable scheduling
  350. // A small, but very parallel
  351. // region, where reg pressure is an issue.
  352. if (HorizontalVerticalBalance > RegPressureThreshold) {
  353. // Critical path first
  354. ResCount += (SU->getHeight() * ScaleTwo);
  355. // If resources are available for it, multiply the
  356. // chance of scheduling.
  357. if (isResourceAvailable(SU))
  358. ResCount <<= FactorOne;
  359. // Consider change to reg pressure from scheduling
  360. // this SU.
  361. ResCount -= (regPressureDelta(SU,true) * ScaleOne);
  362. }
  363. // Default heuristic, greeady and
  364. // critical path driven.
  365. else {
  366. // Critical path first.
  367. ResCount += (SU->getHeight() * ScaleTwo);
  368. // Now see how many instructions is blocked by this SU.
  369. ResCount += (NumNodesSolelyBlocking[SU->NodeNum] * ScaleTwo);
  370. // If resources are available for it, multiply the
  371. // chance of scheduling.
  372. if (isResourceAvailable(SU))
  373. ResCount <<= FactorOne;
  374. ResCount -= (regPressureDelta(SU) * ScaleTwo);
  375. }
  376. // These are platform-specific things.
  377. // Will need to go into the back end
  378. // and accessed from here via a hook.
  379. for (SDNode *N = SU->getNode(); N; N = N->getGluedNode()) {
  380. if (N->isMachineOpcode()) {
  381. const MCInstrDesc &TID = TII->get(N->getMachineOpcode());
  382. if (TID.isCall())
  383. ResCount += (PriorityTwo + (ScaleThree*N->getNumValues()));
  384. }
  385. else
  386. switch (N->getOpcode()) {
  387. default: break;
  388. case ISD::TokenFactor:
  389. case ISD::CopyFromReg:
  390. case ISD::CopyToReg:
  391. ResCount += PriorityFour;
  392. break;
  393. case ISD::INLINEASM:
  394. case ISD::INLINEASM_BR:
  395. ResCount += PriorityThree;
  396. break;
  397. }
  398. }
  399. return ResCount;
  400. }
  401. /// Main resource tracking point.
  402. void ResourcePriorityQueue::scheduledNode(SUnit *SU) {
  403. // Use NULL entry as an event marker to reset
  404. // the DFA state.
  405. if (!SU) {
  406. ResourcesModel->clearResources();
  407. Packet.clear();
  408. return;
  409. }
  410. const SDNode *ScegN = SU->getNode();
  411. // Update reg pressure tracking.
  412. // First update current node.
  413. if (ScegN->isMachineOpcode()) {
  414. // Estimate generated regs.
  415. for (unsigned i = 0, e = ScegN->getNumValues(); i != e; ++i) {
  416. MVT VT = ScegN->getSimpleValueType(i);
  417. if (TLI->isTypeLegal(VT)) {
  418. const TargetRegisterClass *RC = TLI->getRegClassFor(VT);
  419. if (RC)
  420. RegPressure[RC->getID()] += numberRCValSuccInSU(SU, RC->getID());
  421. }
  422. }
  423. // Estimate killed regs.
  424. for (unsigned i = 0, e = ScegN->getNumOperands(); i != e; ++i) {
  425. const SDValue &Op = ScegN->getOperand(i);
  426. MVT VT = Op.getNode()->getSimpleValueType(Op.getResNo());
  427. if (TLI->isTypeLegal(VT)) {
  428. const TargetRegisterClass *RC = TLI->getRegClassFor(VT);
  429. if (RC) {
  430. if (RegPressure[RC->getID()] >
  431. (numberRCValPredInSU(SU, RC->getID())))
  432. RegPressure[RC->getID()] -= numberRCValPredInSU(SU, RC->getID());
  433. else RegPressure[RC->getID()] = 0;
  434. }
  435. }
  436. }
  437. for (SDep &Pred : SU->Preds) {
  438. if (Pred.isCtrl() || (Pred.getSUnit()->NumRegDefsLeft == 0))
  439. continue;
  440. --Pred.getSUnit()->NumRegDefsLeft;
  441. }
  442. }
  443. // Reserve resources for this SU.
  444. reserveResources(SU);
  445. // Adjust number of parallel live ranges.
  446. // Heuristic is simple - node with no data successors reduces
  447. // number of live ranges. All others, increase it.
  448. unsigned NumberNonControlDeps = 0;
  449. for (const SDep &Succ : SU->Succs) {
  450. adjustPriorityOfUnscheduledPreds(Succ.getSUnit());
  451. if (!Succ.isCtrl())
  452. NumberNonControlDeps++;
  453. }
  454. if (!NumberNonControlDeps) {
  455. if (ParallelLiveRanges >= SU->NumPreds)
  456. ParallelLiveRanges -= SU->NumPreds;
  457. else
  458. ParallelLiveRanges = 0;
  459. }
  460. else
  461. ParallelLiveRanges += SU->NumRegDefsLeft;
  462. // Track parallel live chains.
  463. HorizontalVerticalBalance += (SU->Succs.size() - numberCtrlDepsInSU(SU));
  464. HorizontalVerticalBalance -= (SU->Preds.size() - numberCtrlPredInSU(SU));
  465. }
  466. void ResourcePriorityQueue::initNumRegDefsLeft(SUnit *SU) {
  467. unsigned NodeNumDefs = 0;
  468. for (SDNode *N = SU->getNode(); N; N = N->getGluedNode())
  469. if (N->isMachineOpcode()) {
  470. const MCInstrDesc &TID = TII->get(N->getMachineOpcode());
  471. // No register need be allocated for this.
  472. if (N->getMachineOpcode() == TargetOpcode::IMPLICIT_DEF) {
  473. NodeNumDefs = 0;
  474. break;
  475. }
  476. NodeNumDefs = std::min(N->getNumValues(), TID.getNumDefs());
  477. }
  478. else
  479. switch(N->getOpcode()) {
  480. default: break;
  481. case ISD::CopyFromReg:
  482. NodeNumDefs++;
  483. break;
  484. case ISD::INLINEASM:
  485. case ISD::INLINEASM_BR:
  486. NodeNumDefs++;
  487. break;
  488. }
  489. SU->NumRegDefsLeft = NodeNumDefs;
  490. }
  491. /// adjustPriorityOfUnscheduledPreds - One of the predecessors of SU was just
  492. /// scheduled. If SU is not itself available, then there is at least one
  493. /// predecessor node that has not been scheduled yet. If SU has exactly ONE
  494. /// unscheduled predecessor, we want to increase its priority: it getting
  495. /// scheduled will make this node available, so it is better than some other
  496. /// node of the same priority that will not make a node available.
  497. void ResourcePriorityQueue::adjustPriorityOfUnscheduledPreds(SUnit *SU) {
  498. if (SU->isAvailable) return; // All preds scheduled.
  499. SUnit *OnlyAvailablePred = getSingleUnscheduledPred(SU);
  500. if (!OnlyAvailablePred || !OnlyAvailablePred->isAvailable)
  501. return;
  502. // Okay, we found a single predecessor that is available, but not scheduled.
  503. // Since it is available, it must be in the priority queue. First remove it.
  504. remove(OnlyAvailablePred);
  505. // Reinsert the node into the priority queue, which recomputes its
  506. // NumNodesSolelyBlocking value.
  507. push(OnlyAvailablePred);
  508. }
  509. /// Main access point - returns next instructions
  510. /// to be placed in scheduling sequence.
  511. SUnit *ResourcePriorityQueue::pop() {
  512. if (empty())
  513. return nullptr;
  514. std::vector<SUnit *>::iterator Best = Queue.begin();
  515. if (!DisableDFASched) {
  516. int BestCost = SUSchedulingCost(*Best);
  517. for (auto I = std::next(Queue.begin()), E = Queue.end(); I != E; ++I) {
  518. if (SUSchedulingCost(*I) > BestCost) {
  519. BestCost = SUSchedulingCost(*I);
  520. Best = I;
  521. }
  522. }
  523. }
  524. // Use default TD scheduling mechanism.
  525. else {
  526. for (auto I = std::next(Queue.begin()), E = Queue.end(); I != E; ++I)
  527. if (Picker(*Best, *I))
  528. Best = I;
  529. }
  530. SUnit *V = *Best;
  531. if (Best != std::prev(Queue.end()))
  532. std::swap(*Best, Queue.back());
  533. Queue.pop_back();
  534. return V;
  535. }
  536. void ResourcePriorityQueue::remove(SUnit *SU) {
  537. assert(!Queue.empty() && "Queue is empty!");
  538. std::vector<SUnit *>::iterator I = find(Queue, SU);
  539. if (I != std::prev(Queue.end()))
  540. std::swap(*I, Queue.back());
  541. Queue.pop_back();
  542. }