ResourcePriorityQueue.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. //===- ResourcePriorityQueue.cpp - A DFA-oriented priority queue -*- C++ -*-==//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the ResourcePriorityQueue class, which is a
  11. // SchedulingPriorityQueue that prioritizes instructions using DFA state to
  12. // reduce the length of the critical path through the basic block
  13. // on VLIW platforms.
  14. // The scheduler is basically a top-down adaptable list scheduler with DFA
  15. // resource tracking added to the cost function.
  16. // DFA is queried as a state machine to model "packets/bundles" during
  17. // schedule. Currently packets/bundles are discarded at the end of
  18. // scheduling, affecting only order of instructions.
  19. //
  20. //===----------------------------------------------------------------------===//
  21. #include "llvm/CodeGen/ResourcePriorityQueue.h"
  22. #include "llvm/CodeGen/MachineInstr.h"
  23. #include "llvm/CodeGen/SelectionDAGNodes.h"
  24. #include "llvm/CodeGen/TargetLowering.h"
  25. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  26. #include "llvm/Support/CommandLine.h"
  27. #include "llvm/Support/Debug.h"
  28. #include "llvm/Support/raw_ostream.h"
  29. #include "llvm/Target/TargetMachine.h"
  30. using namespace llvm;
  31. #define DEBUG_TYPE "scheduler"
  32. static cl::opt<bool> DisableDFASched("disable-dfa-sched", cl::Hidden,
  33. cl::ZeroOrMore, cl::init(false),
  34. cl::desc("Disable use of DFA during scheduling"));
  35. static cl::opt<int> RegPressureThreshold(
  36. "dfa-sched-reg-pressure-threshold", cl::Hidden, cl::ZeroOrMore, cl::init(5),
  37. cl::desc("Track reg pressure and switch priority to in-depth"));
  38. ResourcePriorityQueue::ResourcePriorityQueue(SelectionDAGISel *IS)
  39. : Picker(this), InstrItins(IS->MF->getSubtarget().getInstrItineraryData()) {
  40. const TargetSubtargetInfo &STI = IS->MF->getSubtarget();
  41. TRI = STI.getRegisterInfo();
  42. TLI = IS->TLI;
  43. TII = STI.getInstrInfo();
  44. ResourcesModel.reset(TII->CreateTargetScheduleState(STI));
  45. // This hard requirement could be relaxed, but for now
  46. // do not let it proceed.
  47. assert(ResourcesModel && "Unimplemented CreateTargetScheduleState.");
  48. unsigned NumRC = TRI->getNumRegClasses();
  49. RegLimit.resize(NumRC);
  50. RegPressure.resize(NumRC);
  51. std::fill(RegLimit.begin(), RegLimit.end(), 0);
  52. std::fill(RegPressure.begin(), RegPressure.end(), 0);
  53. for (const TargetRegisterClass *RC : TRI->regclasses())
  54. RegLimit[RC->getID()] = TRI->getRegPressureLimit(RC, *IS->MF);
  55. ParallelLiveRanges = 0;
  56. HorizontalVerticalBalance = 0;
  57. }
  58. unsigned
  59. ResourcePriorityQueue::numberRCValPredInSU(SUnit *SU, unsigned RCId) {
  60. unsigned NumberDeps = 0;
  61. for (SDep &Pred : SU->Preds) {
  62. if (Pred.isCtrl())
  63. continue;
  64. SUnit *PredSU = Pred.getSUnit();
  65. const SDNode *ScegN = PredSU->getNode();
  66. if (!ScegN)
  67. continue;
  68. // If value is passed to CopyToReg, it is probably
  69. // live outside BB.
  70. switch (ScegN->getOpcode()) {
  71. default: break;
  72. case ISD::TokenFactor: break;
  73. case ISD::CopyFromReg: NumberDeps++; break;
  74. case ISD::CopyToReg: break;
  75. case ISD::INLINEASM: 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. }
  109. if (!ScegN->isMachineOpcode())
  110. continue;
  111. for (unsigned i = 0, e = ScegN->getNumOperands(); i != e; ++i) {
  112. const SDValue &Op = ScegN->getOperand(i);
  113. MVT VT = Op.getNode()->getSimpleValueType(Op.getResNo());
  114. if (TLI->isTypeLegal(VT)
  115. && (TLI->getRegClassFor(VT)->getID() == RCId)) {
  116. NumberDeps++;
  117. break;
  118. }
  119. }
  120. }
  121. return NumberDeps;
  122. }
  123. static unsigned numberCtrlDepsInSU(SUnit *SU) {
  124. unsigned NumberDeps = 0;
  125. for (const SDep &Succ : SU->Succs)
  126. if (Succ.isCtrl())
  127. NumberDeps++;
  128. return NumberDeps;
  129. }
  130. static unsigned numberCtrlPredInSU(SUnit *SU) {
  131. unsigned NumberDeps = 0;
  132. for (SDep &Pred : SU->Preds)
  133. if (Pred.isCtrl())
  134. NumberDeps++;
  135. return NumberDeps;
  136. }
  137. ///
  138. /// Initialize nodes.
  139. ///
  140. void ResourcePriorityQueue::initNodes(std::vector<SUnit> &sunits) {
  141. SUnits = &sunits;
  142. NumNodesSolelyBlocking.resize(SUnits->size(), 0);
  143. for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
  144. SUnit *SU = &(*SUnits)[i];
  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 (unsigned i = 0, e = Packet.size(); i != e; ++i)
  230. for (const SDep &Succ : Packet[i]->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. ResCount += PriorityThree;
  391. break;
  392. }
  393. }
  394. return ResCount;
  395. }
  396. /// Main resource tracking point.
  397. void ResourcePriorityQueue::scheduledNode(SUnit *SU) {
  398. // Use NULL entry as an event marker to reset
  399. // the DFA state.
  400. if (!SU) {
  401. ResourcesModel->clearResources();
  402. Packet.clear();
  403. return;
  404. }
  405. const SDNode *ScegN = SU->getNode();
  406. // Update reg pressure tracking.
  407. // First update current node.
  408. if (ScegN->isMachineOpcode()) {
  409. // Estimate generated regs.
  410. for (unsigned i = 0, e = ScegN->getNumValues(); i != e; ++i) {
  411. MVT VT = ScegN->getSimpleValueType(i);
  412. if (TLI->isTypeLegal(VT)) {
  413. const TargetRegisterClass *RC = TLI->getRegClassFor(VT);
  414. if (RC)
  415. RegPressure[RC->getID()] += numberRCValSuccInSU(SU, RC->getID());
  416. }
  417. }
  418. // Estimate killed regs.
  419. for (unsigned i = 0, e = ScegN->getNumOperands(); i != e; ++i) {
  420. const SDValue &Op = ScegN->getOperand(i);
  421. MVT VT = Op.getNode()->getSimpleValueType(Op.getResNo());
  422. if (TLI->isTypeLegal(VT)) {
  423. const TargetRegisterClass *RC = TLI->getRegClassFor(VT);
  424. if (RC) {
  425. if (RegPressure[RC->getID()] >
  426. (numberRCValPredInSU(SU, RC->getID())))
  427. RegPressure[RC->getID()] -= numberRCValPredInSU(SU, RC->getID());
  428. else RegPressure[RC->getID()] = 0;
  429. }
  430. }
  431. }
  432. for (SDep &Pred : SU->Preds) {
  433. if (Pred.isCtrl() || (Pred.getSUnit()->NumRegDefsLeft == 0))
  434. continue;
  435. --Pred.getSUnit()->NumRegDefsLeft;
  436. }
  437. }
  438. // Reserve resources for this SU.
  439. reserveResources(SU);
  440. // Adjust number of parallel live ranges.
  441. // Heuristic is simple - node with no data successors reduces
  442. // number of live ranges. All others, increase it.
  443. unsigned NumberNonControlDeps = 0;
  444. for (const SDep &Succ : SU->Succs) {
  445. adjustPriorityOfUnscheduledPreds(Succ.getSUnit());
  446. if (!Succ.isCtrl())
  447. NumberNonControlDeps++;
  448. }
  449. if (!NumberNonControlDeps) {
  450. if (ParallelLiveRanges >= SU->NumPreds)
  451. ParallelLiveRanges -= SU->NumPreds;
  452. else
  453. ParallelLiveRanges = 0;
  454. }
  455. else
  456. ParallelLiveRanges += SU->NumRegDefsLeft;
  457. // Track parallel live chains.
  458. HorizontalVerticalBalance += (SU->Succs.size() - numberCtrlDepsInSU(SU));
  459. HorizontalVerticalBalance -= (SU->Preds.size() - numberCtrlPredInSU(SU));
  460. }
  461. void ResourcePriorityQueue::initNumRegDefsLeft(SUnit *SU) {
  462. unsigned NodeNumDefs = 0;
  463. for (SDNode *N = SU->getNode(); N; N = N->getGluedNode())
  464. if (N->isMachineOpcode()) {
  465. const MCInstrDesc &TID = TII->get(N->getMachineOpcode());
  466. // No register need be allocated for this.
  467. if (N->getMachineOpcode() == TargetOpcode::IMPLICIT_DEF) {
  468. NodeNumDefs = 0;
  469. break;
  470. }
  471. NodeNumDefs = std::min(N->getNumValues(), TID.getNumDefs());
  472. }
  473. else
  474. switch(N->getOpcode()) {
  475. default: break;
  476. case ISD::CopyFromReg:
  477. NodeNumDefs++;
  478. break;
  479. case ISD::INLINEASM:
  480. NodeNumDefs++;
  481. break;
  482. }
  483. SU->NumRegDefsLeft = NodeNumDefs;
  484. }
  485. /// adjustPriorityOfUnscheduledPreds - One of the predecessors of SU was just
  486. /// scheduled. If SU is not itself available, then there is at least one
  487. /// predecessor node that has not been scheduled yet. If SU has exactly ONE
  488. /// unscheduled predecessor, we want to increase its priority: it getting
  489. /// scheduled will make this node available, so it is better than some other
  490. /// node of the same priority that will not make a node available.
  491. void ResourcePriorityQueue::adjustPriorityOfUnscheduledPreds(SUnit *SU) {
  492. if (SU->isAvailable) return; // All preds scheduled.
  493. SUnit *OnlyAvailablePred = getSingleUnscheduledPred(SU);
  494. if (!OnlyAvailablePred || !OnlyAvailablePred->isAvailable)
  495. return;
  496. // Okay, we found a single predecessor that is available, but not scheduled.
  497. // Since it is available, it must be in the priority queue. First remove it.
  498. remove(OnlyAvailablePred);
  499. // Reinsert the node into the priority queue, which recomputes its
  500. // NumNodesSolelyBlocking value.
  501. push(OnlyAvailablePred);
  502. }
  503. /// Main access point - returns next instructions
  504. /// to be placed in scheduling sequence.
  505. SUnit *ResourcePriorityQueue::pop() {
  506. if (empty())
  507. return nullptr;
  508. std::vector<SUnit *>::iterator Best = Queue.begin();
  509. if (!DisableDFASched) {
  510. int BestCost = SUSchedulingCost(*Best);
  511. for (auto I = std::next(Queue.begin()), E = Queue.end(); I != E; ++I) {
  512. if (SUSchedulingCost(*I) > BestCost) {
  513. BestCost = SUSchedulingCost(*I);
  514. Best = I;
  515. }
  516. }
  517. }
  518. // Use default TD scheduling mechanism.
  519. else {
  520. for (auto I = std::next(Queue.begin()), E = Queue.end(); I != E; ++I)
  521. if (Picker(*Best, *I))
  522. Best = I;
  523. }
  524. SUnit *V = *Best;
  525. if (Best != std::prev(Queue.end()))
  526. std::swap(*Best, Queue.back());
  527. Queue.pop_back();
  528. return V;
  529. }
  530. void ResourcePriorityQueue::remove(SUnit *SU) {
  531. assert(!Queue.empty() && "Queue is empty!");
  532. std::vector<SUnit *>::iterator I = find(Queue, SU);
  533. if (I != std::prev(Queue.end()))
  534. std::swap(*I, Queue.back());
  535. Queue.pop_back();
  536. }