2
0

ResourcePriorityQueue.cpp 20 KB

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