ResourcePriorityQueue.cpp 21 KB

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