ResourcePriorityQueue.cpp 19 KB

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