PostRASchedulerList.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. //===----- SchedulePostRAList.cpp - list scheduler ------------------------===//
  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 implements a top-down list scheduler, using standard algorithms.
  11. // The basic approach uses a priority queue of available nodes to schedule.
  12. // One at a time, nodes are taken from the priority queue (thus in priority
  13. // order), checked for legality to schedule, and emitted if legal.
  14. //
  15. // Nodes may not be legal to schedule either due to structural hazards (e.g.
  16. // pipeline or resource constraints) or because an input to the instruction has
  17. // not completed execution.
  18. //
  19. //===----------------------------------------------------------------------===//
  20. #define DEBUG_TYPE "post-RA-sched"
  21. #include "llvm/CodeGen/Passes.h"
  22. #include "llvm/CodeGen/ScheduleDAGInstrs.h"
  23. #include "llvm/CodeGen/LatencyPriorityQueue.h"
  24. #include "llvm/CodeGen/SchedulerRegistry.h"
  25. #include "llvm/CodeGen/MachineFunctionPass.h"
  26. #include "llvm/CodeGen/MachineRegisterInfo.h"
  27. #include "llvm/Target/TargetInstrInfo.h"
  28. #include "llvm/Target/TargetRegisterInfo.h"
  29. #include "llvm/Support/Compiler.h"
  30. #include "llvm/Support/Debug.h"
  31. #include "llvm/ADT/Statistic.h"
  32. #include "llvm/ADT/DenseSet.h"
  33. #include "llvm/ADT/SmallVector.h"
  34. #include <map>
  35. #include <climits>
  36. using namespace llvm;
  37. STATISTIC(NumStalls, "Number of pipeline stalls");
  38. static cl::opt<bool>
  39. EnableAntiDepBreaking("break-anti-dependencies",
  40. cl::desc("Break scheduling anti-dependencies"),
  41. cl::init(false));
  42. namespace {
  43. class VISIBILITY_HIDDEN PostRAScheduler : public MachineFunctionPass {
  44. public:
  45. static char ID;
  46. PostRAScheduler() : MachineFunctionPass(&ID) {}
  47. const char *getPassName() const {
  48. return "Post RA top-down list latency scheduler";
  49. }
  50. bool runOnMachineFunction(MachineFunction &Fn);
  51. };
  52. char PostRAScheduler::ID = 0;
  53. class VISIBILITY_HIDDEN SchedulePostRATDList : public ScheduleDAGInstrs {
  54. /// AvailableQueue - The priority queue to use for the available SUnits.
  55. ///
  56. LatencyPriorityQueue AvailableQueue;
  57. /// PendingQueue - This contains all of the instructions whose operands have
  58. /// been issued, but their results are not ready yet (due to the latency of
  59. /// the operation). Once the operands becomes available, the instruction is
  60. /// added to the AvailableQueue.
  61. std::vector<SUnit*> PendingQueue;
  62. /// Topo - A topological ordering for SUnits.
  63. ScheduleDAGTopologicalSort Topo;
  64. public:
  65. SchedulePostRATDList(MachineBasicBlock *mbb, const TargetMachine &tm)
  66. : ScheduleDAGInstrs(mbb, tm), Topo(SUnits) {}
  67. void Schedule();
  68. private:
  69. void ReleaseSucc(SUnit *SU, SDep *SuccEdge);
  70. void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
  71. void ListScheduleTopDown();
  72. bool BreakAntiDependencies();
  73. };
  74. }
  75. bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) {
  76. DOUT << "PostRAScheduler\n";
  77. // Loop over all of the basic blocks
  78. for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
  79. MBB != MBBe; ++MBB) {
  80. SchedulePostRATDList Scheduler(MBB, Fn.getTarget());
  81. Scheduler.Run();
  82. Scheduler.EmitSchedule();
  83. }
  84. return true;
  85. }
  86. /// Schedule - Schedule the DAG using list scheduling.
  87. void SchedulePostRATDList::Schedule() {
  88. DOUT << "********** List Scheduling **********\n";
  89. // Build scheduling units.
  90. BuildSchedUnits();
  91. if (EnableAntiDepBreaking) {
  92. if (BreakAntiDependencies()) {
  93. // We made changes. Update the dependency graph.
  94. // Theoretically we could update the graph in place:
  95. // When a live range is changed to use a different register, remove
  96. // the def's anti-dependence *and* output-dependence edges due to
  97. // that register, and add new anti-dependence and output-dependence
  98. // edges based on the next live range of the register.
  99. SUnits.clear();
  100. BuildSchedUnits();
  101. }
  102. }
  103. AvailableQueue.initNodes(SUnits);
  104. ListScheduleTopDown();
  105. AvailableQueue.releaseState();
  106. }
  107. /// getInstrOperandRegClass - Return register class of the operand of an
  108. /// instruction of the specified TargetInstrDesc.
  109. static const TargetRegisterClass*
  110. getInstrOperandRegClass(const TargetRegisterInfo *TRI,
  111. const TargetInstrInfo *TII, const TargetInstrDesc &II,
  112. unsigned Op) {
  113. if (Op >= II.getNumOperands())
  114. return NULL;
  115. if (II.OpInfo[Op].isLookupPtrRegClass())
  116. return TII->getPointerRegClass();
  117. return TRI->getRegClass(II.OpInfo[Op].RegClass);
  118. }
  119. /// BreakAntiDependencies - Identifiy anti-dependencies along the critical path
  120. /// of the ScheduleDAG and break them by renaming registers.
  121. ///
  122. bool SchedulePostRATDList::BreakAntiDependencies() {
  123. // The code below assumes that there is at least one instruction,
  124. // so just duck out immediately if the block is empty.
  125. if (BB->empty()) return false;
  126. Topo.InitDAGTopologicalSorting();
  127. // Compute a critical path for the DAG.
  128. SUnit *Max = 0;
  129. std::vector<SDep *> CriticalPath(SUnits.size());
  130. for (ScheduleDAGTopologicalSort::const_iterator I = Topo.begin(),
  131. E = Topo.end(); I != E; ++I) {
  132. SUnit *SU = &SUnits[*I];
  133. for (SUnit::pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
  134. P != PE; ++P) {
  135. SUnit *PredSU = P->getSUnit();
  136. // This assumes that there's no delay for reusing registers.
  137. unsigned PredLatency = P->getLatency();
  138. unsigned PredTotalLatency = PredSU->CycleBound + PredLatency;
  139. if (SU->CycleBound < PredTotalLatency ||
  140. (SU->CycleBound == PredTotalLatency &&
  141. P->getKind() == SDep::Anti)) {
  142. SU->CycleBound = PredTotalLatency;
  143. CriticalPath[*I] = &*P;
  144. }
  145. }
  146. // Keep track of the node at the end of the critical path.
  147. if (!Max || SU->CycleBound + SU->Latency > Max->CycleBound + Max->Latency)
  148. Max = SU;
  149. }
  150. DOUT << "Critical path has total latency "
  151. << (Max ? Max->CycleBound + Max->Latency : 0) << "\n";
  152. // Walk the critical path from the bottom up. Collect all anti-dependence
  153. // edges on the critical path. Skip anti-dependencies between SUnits that
  154. // are connected with other edges, since such units won't be able to be
  155. // scheduled past each other anyway.
  156. //
  157. // The heuristic is that edges on the critical path are more important to
  158. // break than other edges. And since there are a limited number of
  159. // registers, we don't want to waste them breaking edges that aren't
  160. // important.
  161. //
  162. // TODO: Instructions with multiple defs could have multiple
  163. // anti-dependencies. The current code here only knows how to break one
  164. // edge per instruction. Note that we'd have to be able to break all of
  165. // the anti-dependencies in an instruction in order to be effective.
  166. BitVector AllocatableSet = TRI->getAllocatableSet(*MF);
  167. DenseMap<MachineInstr *, unsigned> CriticalAntiDeps;
  168. for (SUnit *SU = Max; CriticalPath[SU->NodeNum];
  169. SU = CriticalPath[SU->NodeNum]->getSUnit()) {
  170. SDep *Edge = CriticalPath[SU->NodeNum];
  171. SUnit *NextSU = Edge->getSUnit();
  172. // Only consider anti-dependence edges.
  173. if (Edge->getKind() != SDep::Anti)
  174. continue;
  175. unsigned AntiDepReg = Edge->getReg();
  176. assert(AntiDepReg != 0 && "Anti-dependence on reg0?");
  177. // Don't break anti-dependencies on non-allocatable registers.
  178. if (!AllocatableSet.test(AntiDepReg))
  179. continue;
  180. // If the SUnit has other dependencies on the SUnit that it
  181. // anti-depends on, don't bother breaking the anti-dependency.
  182. // Also, if there are dependencies on other SUnits with the
  183. // same register as the anti-dependency, don't attempt to
  184. // break it.
  185. for (SUnit::pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
  186. P != PE; ++P)
  187. if (P->getSUnit() == NextSU ?
  188. (P->getKind() != SDep::Anti || P->getReg() != AntiDepReg) :
  189. (P->getKind() == SDep::Data && P->getReg() == AntiDepReg)) {
  190. AntiDepReg = 0;
  191. break;
  192. }
  193. if (AntiDepReg != 0)
  194. CriticalAntiDeps[SU->getInstr()] = AntiDepReg;
  195. }
  196. // For live regs that are only used in one register class in a live range,
  197. // the register class. If the register is not live, the corresponding value
  198. // is null. If the register is live but used in multiple register classes,
  199. // the corresponding value is -1 casted to a pointer.
  200. const TargetRegisterClass *
  201. Classes[TargetRegisterInfo::FirstVirtualRegister] = {};
  202. // Map registers to all their references within a live range.
  203. std::multimap<unsigned, MachineOperand *> RegRefs;
  204. // The index of the most recent kill (proceding bottom-up), or -1 if
  205. // the register is not live.
  206. unsigned KillIndices[TargetRegisterInfo::FirstVirtualRegister];
  207. std::fill(KillIndices, array_endof(KillIndices), -1);
  208. // The index of the most recent def (proceding bottom up), or -1 if
  209. // the register is live.
  210. unsigned DefIndices[TargetRegisterInfo::FirstVirtualRegister];
  211. std::fill(DefIndices, array_endof(DefIndices), BB->size());
  212. // Determine the live-out physregs for this block.
  213. if (!BB->empty() && BB->back().getDesc().isReturn())
  214. // In a return block, examine the function live-out regs.
  215. for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(),
  216. E = MRI.liveout_end(); I != E; ++I) {
  217. unsigned Reg = *I;
  218. Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
  219. KillIndices[Reg] = BB->size();
  220. DefIndices[Reg] = -1;
  221. // Repeat, for all aliases.
  222. for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
  223. unsigned AliasReg = *Alias;
  224. Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
  225. KillIndices[AliasReg] = BB->size();
  226. DefIndices[AliasReg] = -1;
  227. }
  228. }
  229. else
  230. // In a non-return block, examine the live-in regs of all successors.
  231. for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
  232. SE = BB->succ_end(); SI != SE; ++SI)
  233. for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
  234. E = (*SI)->livein_end(); I != E; ++I) {
  235. unsigned Reg = *I;
  236. Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
  237. KillIndices[Reg] = BB->size();
  238. DefIndices[Reg] = -1;
  239. // Repeat, for all aliases.
  240. for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
  241. unsigned AliasReg = *Alias;
  242. Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
  243. KillIndices[AliasReg] = BB->size();
  244. DefIndices[AliasReg] = -1;
  245. }
  246. }
  247. // Consider callee-saved registers as live-out, since we're running after
  248. // prologue/epilogue insertion so there's no way to add additional
  249. // saved registers.
  250. //
  251. // TODO: If the callee saves and restores these, then we can potentially
  252. // use them between the save and the restore. To do that, we could scan
  253. // the exit blocks to see which of these registers are defined.
  254. // Alternatively, calle-saved registers that aren't saved and restored
  255. // could be marked live-in in every block.
  256. for (const unsigned *I = TRI->getCalleeSavedRegs(); *I; ++I) {
  257. unsigned Reg = *I;
  258. Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
  259. KillIndices[Reg] = BB->size();
  260. DefIndices[Reg] = -1;
  261. // Repeat, for all aliases.
  262. for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
  263. unsigned AliasReg = *Alias;
  264. Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
  265. KillIndices[AliasReg] = BB->size();
  266. DefIndices[AliasReg] = -1;
  267. }
  268. }
  269. // Consider this pattern:
  270. // A = ...
  271. // ... = A
  272. // A = ...
  273. // ... = A
  274. // A = ...
  275. // ... = A
  276. // A = ...
  277. // ... = A
  278. // There are three anti-dependencies here, and without special care,
  279. // we'd break all of them using the same register:
  280. // A = ...
  281. // ... = A
  282. // B = ...
  283. // ... = B
  284. // B = ...
  285. // ... = B
  286. // B = ...
  287. // ... = B
  288. // because at each anti-dependence, B is the first register that
  289. // isn't A which is free. This re-introduces anti-dependencies
  290. // at all but one of the original anti-dependencies that we were
  291. // trying to break. To avoid this, keep track of the most recent
  292. // register that each register was replaced with, avoid avoid
  293. // using it to repair an anti-dependence on the same register.
  294. // This lets us produce this:
  295. // A = ...
  296. // ... = A
  297. // B = ...
  298. // ... = B
  299. // C = ...
  300. // ... = C
  301. // B = ...
  302. // ... = B
  303. // This still has an anti-dependence on B, but at least it isn't on the
  304. // original critical path.
  305. //
  306. // TODO: If we tracked more than one register here, we could potentially
  307. // fix that remaining critical edge too. This is a little more involved,
  308. // because unlike the most recent register, less recent registers should
  309. // still be considered, though only if no other registers are available.
  310. unsigned LastNewReg[TargetRegisterInfo::FirstVirtualRegister] = {};
  311. // Attempt to break anti-dependence edges on the critical path. Walk the
  312. // instructions from the bottom up, tracking information about liveness
  313. // as we go to help determine which registers are available.
  314. bool Changed = false;
  315. unsigned Count = BB->size() - 1;
  316. for (MachineBasicBlock::reverse_iterator I = BB->rbegin(), E = BB->rend();
  317. I != E; ++I, --Count) {
  318. MachineInstr *MI = &*I;
  319. // After regalloc, IMPLICIT_DEF instructions aren't safe to treat as
  320. // dependence-breaking. In the case of an INSERT_SUBREG, the IMPLICIT_DEF
  321. // is left behind appearing to clobber the super-register, while the
  322. // subregister needs to remain live. So we just ignore them.
  323. if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF)
  324. continue;
  325. // Check if this instruction has an anti-dependence that we're
  326. // interested in.
  327. DenseMap<MachineInstr *, unsigned>::iterator C = CriticalAntiDeps.find(MI);
  328. unsigned AntiDepReg = C != CriticalAntiDeps.end() ?
  329. C->second : 0;
  330. // Scan the register operands for this instruction and update
  331. // Classes and RegRefs.
  332. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  333. MachineOperand &MO = MI->getOperand(i);
  334. if (!MO.isReg()) continue;
  335. unsigned Reg = MO.getReg();
  336. if (Reg == 0) continue;
  337. const TargetRegisterClass *NewRC =
  338. getInstrOperandRegClass(TRI, TII, MI->getDesc(), i);
  339. // If this instruction has a use of AntiDepReg, breaking it
  340. // is invalid.
  341. if (MO.isUse() && AntiDepReg == Reg)
  342. AntiDepReg = 0;
  343. // For now, only allow the register to be changed if its register
  344. // class is consistent across all uses.
  345. if (!Classes[Reg] && NewRC)
  346. Classes[Reg] = NewRC;
  347. else if (!NewRC || Classes[Reg] != NewRC)
  348. Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
  349. // Now check for aliases.
  350. for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
  351. // If an alias of the reg is used during the live range, give up.
  352. // Note that this allows us to skip checking if AntiDepReg
  353. // overlaps with any of the aliases, among other things.
  354. unsigned AliasReg = *Alias;
  355. if (Classes[AliasReg]) {
  356. Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
  357. Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
  358. }
  359. }
  360. // If we're still willing to consider this register, note the reference.
  361. if (Classes[Reg] != reinterpret_cast<TargetRegisterClass *>(-1))
  362. RegRefs.insert(std::make_pair(Reg, &MO));
  363. }
  364. // Determine AntiDepReg's register class, if it is live and is
  365. // consistently used within a single class.
  366. const TargetRegisterClass *RC = AntiDepReg != 0 ? Classes[AntiDepReg] : 0;
  367. assert((AntiDepReg == 0 || RC != NULL) &&
  368. "Register should be live if it's causing an anti-dependence!");
  369. if (RC == reinterpret_cast<TargetRegisterClass *>(-1))
  370. AntiDepReg = 0;
  371. // Look for a suitable register to use to break the anti-depenence.
  372. //
  373. // TODO: Instead of picking the first free register, consider which might
  374. // be the best.
  375. if (AntiDepReg != 0) {
  376. for (TargetRegisterClass::iterator R = RC->allocation_order_begin(*MF),
  377. RE = RC->allocation_order_end(*MF); R != RE; ++R) {
  378. unsigned NewReg = *R;
  379. // Don't replace a register with itself.
  380. if (NewReg == AntiDepReg) continue;
  381. // Don't replace a register with one that was recently used to repair
  382. // an anti-dependence with this AntiDepReg, because that would
  383. // re-introduce that anti-dependence.
  384. if (NewReg == LastNewReg[AntiDepReg]) continue;
  385. // If NewReg is dead and NewReg's most recent def is not before
  386. // AntiDepReg's kill, it's safe to replace AntiDepReg with NewReg.
  387. assert(((KillIndices[AntiDepReg] == -1u) != (DefIndices[AntiDepReg] == -1u)) &&
  388. "Kill and Def maps aren't consistent for AntiDepReg!");
  389. assert(((KillIndices[NewReg] == -1u) != (DefIndices[NewReg] == -1u)) &&
  390. "Kill and Def maps aren't consistent for NewReg!");
  391. if (KillIndices[NewReg] == -1u &&
  392. KillIndices[AntiDepReg] <= DefIndices[NewReg]) {
  393. DOUT << "Breaking anti-dependence edge on "
  394. << TRI->getName(AntiDepReg)
  395. << " with " << RegRefs.count(AntiDepReg) << " references"
  396. << " using " << TRI->getName(NewReg) << "!\n";
  397. // Update the references to the old register to refer to the new
  398. // register.
  399. std::pair<std::multimap<unsigned, MachineOperand *>::iterator,
  400. std::multimap<unsigned, MachineOperand *>::iterator>
  401. Range = RegRefs.equal_range(AntiDepReg);
  402. for (std::multimap<unsigned, MachineOperand *>::iterator
  403. Q = Range.first, QE = Range.second; Q != QE; ++Q)
  404. Q->second->setReg(NewReg);
  405. // We just went back in time and modified history; the
  406. // liveness information for the anti-depenence reg is now
  407. // inconsistent. Set the state as if it were dead.
  408. Classes[NewReg] = Classes[AntiDepReg];
  409. DefIndices[NewReg] = DefIndices[AntiDepReg];
  410. KillIndices[NewReg] = KillIndices[AntiDepReg];
  411. Classes[AntiDepReg] = 0;
  412. DefIndices[AntiDepReg] = KillIndices[AntiDepReg];
  413. KillIndices[AntiDepReg] = -1;
  414. RegRefs.erase(AntiDepReg);
  415. Changed = true;
  416. LastNewReg[AntiDepReg] = NewReg;
  417. break;
  418. }
  419. }
  420. }
  421. // Update liveness.
  422. // Proceding upwards, registers that are defed but not used in this
  423. // instruction are now dead.
  424. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  425. MachineOperand &MO = MI->getOperand(i);
  426. if (!MO.isReg()) continue;
  427. unsigned Reg = MO.getReg();
  428. if (Reg == 0) continue;
  429. if (!MO.isDef()) continue;
  430. // Ignore two-addr defs.
  431. if (MI->isRegReDefinedByTwoAddr(i)) continue;
  432. DefIndices[Reg] = Count;
  433. KillIndices[Reg] = -1;
  434. Classes[Reg] = 0;
  435. RegRefs.erase(Reg);
  436. // Repeat, for all subregs.
  437. for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
  438. *Subreg; ++Subreg) {
  439. unsigned SubregReg = *Subreg;
  440. DefIndices[SubregReg] = Count;
  441. KillIndices[SubregReg] = -1;
  442. Classes[SubregReg] = 0;
  443. RegRefs.erase(SubregReg);
  444. }
  445. }
  446. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  447. MachineOperand &MO = MI->getOperand(i);
  448. if (!MO.isReg()) continue;
  449. unsigned Reg = MO.getReg();
  450. if (Reg == 0) continue;
  451. if (!MO.isUse()) continue;
  452. const TargetRegisterClass *NewRC =
  453. getInstrOperandRegClass(TRI, TII, MI->getDesc(), i);
  454. // For now, only allow the register to be changed if its register
  455. // class is consistent across all uses.
  456. if (!Classes[Reg] && NewRC)
  457. Classes[Reg] = NewRC;
  458. else if (!NewRC || Classes[Reg] != NewRC)
  459. Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
  460. RegRefs.insert(std::make_pair(Reg, &MO));
  461. // It wasn't previously live but now it is, this is a kill.
  462. if (KillIndices[Reg] == -1u) {
  463. KillIndices[Reg] = Count;
  464. DefIndices[Reg] = -1u;
  465. }
  466. // Repeat, for all aliases.
  467. for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
  468. unsigned AliasReg = *Alias;
  469. if (KillIndices[AliasReg] == -1u) {
  470. KillIndices[AliasReg] = Count;
  471. DefIndices[AliasReg] = -1u;
  472. }
  473. }
  474. }
  475. }
  476. assert(Count == -1u && "Count mismatch!");
  477. return Changed;
  478. }
  479. //===----------------------------------------------------------------------===//
  480. // Top-Down Scheduling
  481. //===----------------------------------------------------------------------===//
  482. /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
  483. /// the PendingQueue if the count reaches zero. Also update its cycle bound.
  484. void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) {
  485. SUnit *SuccSU = SuccEdge->getSUnit();
  486. --SuccSU->NumPredsLeft;
  487. #ifndef NDEBUG
  488. if (SuccSU->NumPredsLeft < 0) {
  489. cerr << "*** Scheduling failed! ***\n";
  490. SuccSU->dump(this);
  491. cerr << " has been released too many times!\n";
  492. assert(0);
  493. }
  494. #endif
  495. // Compute how many cycles it will be before this actually becomes
  496. // available. This is the max of the start time of all predecessors plus
  497. // their latencies.
  498. unsigned PredDoneCycle = SU->Cycle + SuccEdge->getLatency();
  499. SuccSU->CycleBound = std::max(SuccSU->CycleBound, PredDoneCycle);
  500. if (SuccSU->NumPredsLeft == 0) {
  501. PendingQueue.push_back(SuccSU);
  502. }
  503. }
  504. /// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
  505. /// count of its successors. If a successor pending count is zero, add it to
  506. /// the Available queue.
  507. void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
  508. DOUT << "*** Scheduling [" << CurCycle << "]: ";
  509. DEBUG(SU->dump(this));
  510. Sequence.push_back(SU);
  511. SU->Cycle = CurCycle;
  512. // Top down: release successors.
  513. for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
  514. I != E; ++I)
  515. ReleaseSucc(SU, &*I);
  516. SU->isScheduled = true;
  517. AvailableQueue.ScheduledNode(SU);
  518. }
  519. /// ListScheduleTopDown - The main loop of list scheduling for top-down
  520. /// schedulers.
  521. void SchedulePostRATDList::ListScheduleTopDown() {
  522. unsigned CurCycle = 0;
  523. // All leaves to Available queue.
  524. for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
  525. // It is available if it has no predecessors.
  526. if (SUnits[i].Preds.empty()) {
  527. AvailableQueue.push(&SUnits[i]);
  528. SUnits[i].isAvailable = true;
  529. }
  530. }
  531. // While Available queue is not empty, grab the node with the highest
  532. // priority. If it is not ready put it back. Schedule the node.
  533. Sequence.reserve(SUnits.size());
  534. while (!AvailableQueue.empty() || !PendingQueue.empty()) {
  535. // Check to see if any of the pending instructions are ready to issue. If
  536. // so, add them to the available queue.
  537. for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
  538. if (PendingQueue[i]->CycleBound == CurCycle) {
  539. AvailableQueue.push(PendingQueue[i]);
  540. PendingQueue[i]->isAvailable = true;
  541. PendingQueue[i] = PendingQueue.back();
  542. PendingQueue.pop_back();
  543. --i; --e;
  544. } else {
  545. assert(PendingQueue[i]->CycleBound > CurCycle && "Non-positive latency?");
  546. }
  547. }
  548. // If there are no instructions available, don't try to issue anything.
  549. if (AvailableQueue.empty()) {
  550. ++CurCycle;
  551. continue;
  552. }
  553. SUnit *FoundSUnit = AvailableQueue.pop();
  554. // If we found a node to schedule, do it now.
  555. if (FoundSUnit) {
  556. ScheduleNodeTopDown(FoundSUnit, CurCycle);
  557. // If this is a pseudo-op node, we don't want to increment the current
  558. // cycle.
  559. if (FoundSUnit->Latency) // Don't increment CurCycle for pseudo-ops!
  560. ++CurCycle;
  561. } else {
  562. // Otherwise, we have a pipeline stall, but no other problem, just advance
  563. // the current cycle and try again.
  564. DOUT << "*** Advancing cycle, no work to do\n";
  565. ++NumStalls;
  566. ++CurCycle;
  567. }
  568. }
  569. #ifndef NDEBUG
  570. VerifySchedule(/*isBottomUp=*/false);
  571. #endif
  572. }
  573. //===----------------------------------------------------------------------===//
  574. // Public Constructor Functions
  575. //===----------------------------------------------------------------------===//
  576. FunctionPass *llvm::createPostRAScheduler() {
  577. return new PostRAScheduler();
  578. }