ResourcePriorityQueue.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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. #define DEBUG_TYPE "scheduler"
  22. #include "llvm/CodeGen/ResourcePriorityQueue.h"
  23. #include "llvm/Support/CommandLine.h"
  24. #include "llvm/Support/Debug.h"
  25. #include "llvm/Support/raw_ostream.h"
  26. #include "llvm/CodeGen/MachineInstr.h"
  27. #include "llvm/CodeGen/SelectionDAGNodes.h"
  28. #include "llvm/Target/TargetMachine.h"
  29. #include "llvm/Target/TargetLowering.h"
  30. using namespace llvm;
  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<signed> 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),
  39. InstrItins(IS->getTargetLowering().getTargetMachine().getInstrItineraryData())
  40. {
  41. TII = IS->getTargetLowering().getTargetMachine().getInstrInfo();
  42. TRI = IS->getTargetLowering().getTargetMachine().getRegisterInfo();
  43. TLI = &IS->getTargetLowering();
  44. const TargetMachine &tm = (*IS->MF).getTarget();
  45. ResourcesModel = tm.getInstrInfo()->CreateTargetScheduleState(&tm,NULL);
  46. // This hard requirement could be relaxed, but for now
  47. // do not let it procede.
  48. assert (ResourcesModel && "Unimplemented CreateTargetScheduleState.");
  49. unsigned NumRC = TRI->getNumRegClasses();
  50. RegLimit.resize(NumRC);
  51. RegPressure.resize(NumRC);
  52. std::fill(RegLimit.begin(), RegLimit.end(), 0);
  53. std::fill(RegPressure.begin(), RegPressure.end(), 0);
  54. for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(),
  55. E = TRI->regclass_end(); 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. EVT VT = ScegN->getValueType(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. EVT VT = Op.getNode()->getValueType(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 = 0;
  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 0;
  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. EVT VT = SU->getNode()->getValueType(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. EVT VT = Op.getNode()->getValueType(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 = 100;
  341. static const unsigned PriorityThree = 50;
  342. static const unsigned PriorityFour = 15;
  343. static const unsigned PriorityFive = 5;
  344. static const unsigned ScaleOne = 20;
  345. static const unsigned ScaleTwo = 10;
  346. static const unsigned ScaleThree = 5;
  347. static const unsigned FactorOne = 2;
  348. /// Returns single number reflecting benefit of scheduling SU
  349. /// in the current cycle.
  350. signed ResourcePriorityQueue::SUSchedulingCost(SUnit *SU) {
  351. // Initial trivial priority.
  352. signed ResCount = 1;
  353. // Do not waste time on a node that is already scheduled.
  354. if (SU->isScheduled)
  355. return ResCount;
  356. // Forced priority is high.
  357. if (SU->isScheduleHigh)
  358. ResCount += PriorityOne;
  359. // Adaptable scheduling
  360. // A small, but very parallel
  361. // region, where reg pressure is an issue.
  362. if (HorizontalVerticalBalance > RegPressureThreshold) {
  363. // Critical path first
  364. ResCount += (SU->getHeight() * ScaleTwo);
  365. // If resources are available for it, multiply the
  366. // chance of scheduling.
  367. if (isResourceAvailable(SU))
  368. ResCount <<= FactorOne;
  369. // Consider change to reg pressure from scheduling
  370. // this SU.
  371. ResCount -= (regPressureDelta(SU,true) * ScaleOne);
  372. }
  373. // Default heuristic, greeady and
  374. // critical path driven.
  375. else {
  376. // Critical path first.
  377. ResCount += (SU->getHeight() * ScaleTwo);
  378. // Now see how many instructions is blocked by this SU.
  379. ResCount += (NumNodesSolelyBlocking[SU->NodeNum] * ScaleTwo);
  380. // If resources are available for it, multiply the
  381. // chance of scheduling.
  382. if (isResourceAvailable(SU))
  383. ResCount <<= FactorOne;
  384. ResCount -= (regPressureDelta(SU) * ScaleTwo);
  385. }
  386. // These are platform specific things.
  387. // Will need to go into the back end
  388. // and accessed from here via a hook.
  389. for (SDNode *N = SU->getNode(); N; N = N->getGluedNode()) {
  390. if (N->isMachineOpcode()) {
  391. const MCInstrDesc &TID = TII->get(N->getMachineOpcode());
  392. if (TID.isCall())
  393. ResCount += (PriorityThree + (ScaleThree*N->getNumValues()));
  394. }
  395. else
  396. switch (N->getOpcode()) {
  397. default: break;
  398. case ISD::TokenFactor:
  399. case ISD::CopyFromReg:
  400. case ISD::CopyToReg:
  401. ResCount += PriorityFive;
  402. break;
  403. case ISD::INLINEASM:
  404. ResCount += PriorityFour;
  405. break;
  406. }
  407. }
  408. return ResCount;
  409. }
  410. /// Main resource tracking point.
  411. void ResourcePriorityQueue::scheduledNode(SUnit *SU) {
  412. // Use NULL entry as an event marker to reset
  413. // the DFA state.
  414. if (!SU) {
  415. ResourcesModel->clearResources();
  416. Packet.clear();
  417. return;
  418. }
  419. const SDNode *ScegN = SU->getNode();
  420. // Update reg pressure tracking.
  421. // First update current node.
  422. if (ScegN->isMachineOpcode()) {
  423. // Estimate generated regs.
  424. for (unsigned i = 0, e = ScegN->getNumValues(); i != e; ++i) {
  425. EVT VT = ScegN->getValueType(i);
  426. if (TLI->isTypeLegal(VT)) {
  427. const TargetRegisterClass *RC = TLI->getRegClassFor(VT);
  428. if (RC)
  429. RegPressure[RC->getID()] += numberRCValSuccInSU(SU, RC->getID());
  430. }
  431. }
  432. // Estimate killed regs.
  433. for (unsigned i = 0, e = ScegN->getNumOperands(); i != e; ++i) {
  434. const SDValue &Op = ScegN->getOperand(i);
  435. EVT VT = Op.getNode()->getValueType(Op.getResNo());
  436. if (TLI->isTypeLegal(VT)) {
  437. const TargetRegisterClass *RC = TLI->getRegClassFor(VT);
  438. if (RC) {
  439. if (RegPressure[RC->getID()] >
  440. (numberRCValPredInSU(SU, RC->getID())))
  441. RegPressure[RC->getID()] -= numberRCValPredInSU(SU, RC->getID());
  442. else RegPressure[RC->getID()] = 0;
  443. }
  444. }
  445. }
  446. for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
  447. I != E; ++I) {
  448. if (I->isCtrl() || (I->getSUnit()->NumRegDefsLeft == 0))
  449. continue;
  450. --I->getSUnit()->NumRegDefsLeft;
  451. }
  452. }
  453. // Reserve resources for this SU.
  454. reserveResources(SU);
  455. // Adjust number of parallel live ranges.
  456. // Heuristic is simple - node with no data successors reduces
  457. // number of live ranges. All others, increase it.
  458. unsigned NumberNonControlDeps = 0;
  459. for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
  460. I != E; ++I) {
  461. adjustPriorityOfUnscheduledPreds(I->getSUnit());
  462. if (!I->isCtrl())
  463. NumberNonControlDeps++;
  464. }
  465. if (!NumberNonControlDeps) {
  466. if (ParallelLiveRanges >= SU->NumPreds)
  467. ParallelLiveRanges -= SU->NumPreds;
  468. else
  469. ParallelLiveRanges = 0;
  470. }
  471. else
  472. ParallelLiveRanges += SU->NumRegDefsLeft;
  473. // Track parallel live chains.
  474. HorizontalVerticalBalance += (SU->Succs.size() - numberCtrlDepsInSU(SU));
  475. HorizontalVerticalBalance -= (SU->Preds.size() - numberCtrlPredInSU(SU));
  476. }
  477. void ResourcePriorityQueue::initNumRegDefsLeft(SUnit *SU) {
  478. unsigned NodeNumDefs = 0;
  479. for (SDNode *N = SU->getNode(); N; N = N->getGluedNode())
  480. if (N->isMachineOpcode()) {
  481. const MCInstrDesc &TID = TII->get(N->getMachineOpcode());
  482. // No register need be allocated for this.
  483. if (N->getMachineOpcode() == TargetOpcode::IMPLICIT_DEF) {
  484. NodeNumDefs = 0;
  485. break;
  486. }
  487. NodeNumDefs = std::min(N->getNumValues(), TID.getNumDefs());
  488. }
  489. else
  490. switch(N->getOpcode()) {
  491. default: break;
  492. case ISD::CopyFromReg:
  493. NodeNumDefs++;
  494. break;
  495. case ISD::INLINEASM:
  496. NodeNumDefs++;
  497. break;
  498. }
  499. SU->NumRegDefsLeft = NodeNumDefs;
  500. }
  501. /// adjustPriorityOfUnscheduledPreds - One of the predecessors of SU was just
  502. /// scheduled. If SU is not itself available, then there is at least one
  503. /// predecessor node that has not been scheduled yet. If SU has exactly ONE
  504. /// unscheduled predecessor, we want to increase its priority: it getting
  505. /// scheduled will make this node available, so it is better than some other
  506. /// node of the same priority that will not make a node available.
  507. void ResourcePriorityQueue::adjustPriorityOfUnscheduledPreds(SUnit *SU) {
  508. if (SU->isAvailable) return; // All preds scheduled.
  509. SUnit *OnlyAvailablePred = getSingleUnscheduledPred(SU);
  510. if (OnlyAvailablePred == 0 || !OnlyAvailablePred->isAvailable)
  511. return;
  512. // Okay, we found a single predecessor that is available, but not scheduled.
  513. // Since it is available, it must be in the priority queue. First remove it.
  514. remove(OnlyAvailablePred);
  515. // Reinsert the node into the priority queue, which recomputes its
  516. // NumNodesSolelyBlocking value.
  517. push(OnlyAvailablePred);
  518. }
  519. /// Main access point - returns next instructions
  520. /// to be placed in scheduling sequence.
  521. SUnit *ResourcePriorityQueue::pop() {
  522. if (empty())
  523. return 0;
  524. std::vector<SUnit *>::iterator Best = Queue.begin();
  525. if (!DisableDFASched) {
  526. signed BestCost = SUSchedulingCost(*Best);
  527. for (std::vector<SUnit *>::iterator I = Queue.begin(),
  528. E = Queue.end(); I != E; ++I) {
  529. if (*I == *Best)
  530. continue;
  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 = llvm::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 != prior(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 != prior(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