ResourcePriorityQueue.cpp 19 KB

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