MachineScheduler.cpp 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  1. //===- MachineScheduler.cpp - Machine Instruction 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. // MachineScheduler schedules machine instructions after phi elimination. It
  11. // preserves LiveIntervals so it can be invoked before register allocation.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #define DEBUG_TYPE "misched"
  15. #include "llvm/CodeGen/LiveIntervalAnalysis.h"
  16. #include "llvm/CodeGen/MachineScheduler.h"
  17. #include "llvm/CodeGen/Passes.h"
  18. #include "llvm/CodeGen/RegisterClassInfo.h"
  19. #include "llvm/CodeGen/RegisterPressure.h"
  20. #include "llvm/CodeGen/ScheduleDAGInstrs.h"
  21. #include "llvm/CodeGen/ScheduleHazardRecognizer.h"
  22. #include "llvm/Target/TargetInstrInfo.h"
  23. #include "llvm/MC/MCInstrItineraries.h"
  24. #include "llvm/Analysis/AliasAnalysis.h"
  25. #include "llvm/Support/CommandLine.h"
  26. #include "llvm/Support/Debug.h"
  27. #include "llvm/Support/ErrorHandling.h"
  28. #include "llvm/Support/raw_ostream.h"
  29. #include "llvm/ADT/OwningPtr.h"
  30. #include "llvm/ADT/PriorityQueue.h"
  31. #include <queue>
  32. using namespace llvm;
  33. static cl::opt<bool> ForceTopDown("misched-topdown", cl::Hidden,
  34. cl::desc("Force top-down list scheduling"));
  35. static cl::opt<bool> ForceBottomUp("misched-bottomup", cl::Hidden,
  36. cl::desc("Force bottom-up list scheduling"));
  37. #ifndef NDEBUG
  38. static cl::opt<bool> ViewMISchedDAGs("view-misched-dags", cl::Hidden,
  39. cl::desc("Pop up a window to show MISched dags after they are processed"));
  40. static cl::opt<unsigned> MISchedCutoff("misched-cutoff", cl::Hidden,
  41. cl::desc("Stop scheduling after N instructions"), cl::init(~0U));
  42. #else
  43. static bool ViewMISchedDAGs = false;
  44. #endif // NDEBUG
  45. //===----------------------------------------------------------------------===//
  46. // Machine Instruction Scheduling Pass and Registry
  47. //===----------------------------------------------------------------------===//
  48. MachineSchedContext::MachineSchedContext():
  49. MF(0), MLI(0), MDT(0), PassConfig(0), AA(0), LIS(0) {
  50. RegClassInfo = new RegisterClassInfo();
  51. }
  52. MachineSchedContext::~MachineSchedContext() {
  53. delete RegClassInfo;
  54. }
  55. namespace {
  56. /// MachineScheduler runs after coalescing and before register allocation.
  57. class MachineScheduler : public MachineSchedContext,
  58. public MachineFunctionPass {
  59. public:
  60. MachineScheduler();
  61. virtual void getAnalysisUsage(AnalysisUsage &AU) const;
  62. virtual void releaseMemory() {}
  63. virtual bool runOnMachineFunction(MachineFunction&);
  64. virtual void print(raw_ostream &O, const Module* = 0) const;
  65. static char ID; // Class identification, replacement for typeinfo
  66. };
  67. } // namespace
  68. char MachineScheduler::ID = 0;
  69. char &llvm::MachineSchedulerID = MachineScheduler::ID;
  70. INITIALIZE_PASS_BEGIN(MachineScheduler, "misched",
  71. "Machine Instruction Scheduler", false, false)
  72. INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
  73. INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
  74. INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
  75. INITIALIZE_PASS_END(MachineScheduler, "misched",
  76. "Machine Instruction Scheduler", false, false)
  77. MachineScheduler::MachineScheduler()
  78. : MachineFunctionPass(ID) {
  79. initializeMachineSchedulerPass(*PassRegistry::getPassRegistry());
  80. }
  81. void MachineScheduler::getAnalysisUsage(AnalysisUsage &AU) const {
  82. AU.setPreservesCFG();
  83. AU.addRequiredID(MachineDominatorsID);
  84. AU.addRequired<MachineLoopInfo>();
  85. AU.addRequired<AliasAnalysis>();
  86. AU.addRequired<TargetPassConfig>();
  87. AU.addRequired<SlotIndexes>();
  88. AU.addPreserved<SlotIndexes>();
  89. AU.addRequired<LiveIntervals>();
  90. AU.addPreserved<LiveIntervals>();
  91. MachineFunctionPass::getAnalysisUsage(AU);
  92. }
  93. MachinePassRegistry MachineSchedRegistry::Registry;
  94. /// A dummy default scheduler factory indicates whether the scheduler
  95. /// is overridden on the command line.
  96. static ScheduleDAGInstrs *useDefaultMachineSched(MachineSchedContext *C) {
  97. return 0;
  98. }
  99. /// MachineSchedOpt allows command line selection of the scheduler.
  100. static cl::opt<MachineSchedRegistry::ScheduleDAGCtor, false,
  101. RegisterPassParser<MachineSchedRegistry> >
  102. MachineSchedOpt("misched",
  103. cl::init(&useDefaultMachineSched), cl::Hidden,
  104. cl::desc("Machine instruction scheduler to use"));
  105. static MachineSchedRegistry
  106. DefaultSchedRegistry("default", "Use the target's default scheduler choice.",
  107. useDefaultMachineSched);
  108. /// Forward declare the standard machine scheduler. This will be used as the
  109. /// default scheduler if the target does not set a default.
  110. static ScheduleDAGInstrs *createConvergingSched(MachineSchedContext *C);
  111. /// Decrement this iterator until reaching the top or a non-debug instr.
  112. static MachineBasicBlock::iterator
  113. priorNonDebug(MachineBasicBlock::iterator I, MachineBasicBlock::iterator Beg) {
  114. assert(I != Beg && "reached the top of the region, cannot decrement");
  115. while (--I != Beg) {
  116. if (!I->isDebugValue())
  117. break;
  118. }
  119. return I;
  120. }
  121. /// If this iterator is a debug value, increment until reaching the End or a
  122. /// non-debug instruction.
  123. static MachineBasicBlock::iterator
  124. nextIfDebug(MachineBasicBlock::iterator I, MachineBasicBlock::iterator End) {
  125. for(; I != End; ++I) {
  126. if (!I->isDebugValue())
  127. break;
  128. }
  129. return I;
  130. }
  131. /// Top-level MachineScheduler pass driver.
  132. ///
  133. /// Visit blocks in function order. Divide each block into scheduling regions
  134. /// and visit them bottom-up. Visiting regions bottom-up is not required, but is
  135. /// consistent with the DAG builder, which traverses the interior of the
  136. /// scheduling regions bottom-up.
  137. ///
  138. /// This design avoids exposing scheduling boundaries to the DAG builder,
  139. /// simplifying the DAG builder's support for "special" target instructions.
  140. /// At the same time the design allows target schedulers to operate across
  141. /// scheduling boundaries, for example to bundle the boudary instructions
  142. /// without reordering them. This creates complexity, because the target
  143. /// scheduler must update the RegionBegin and RegionEnd positions cached by
  144. /// ScheduleDAGInstrs whenever adding or removing instructions. A much simpler
  145. /// design would be to split blocks at scheduling boundaries, but LLVM has a
  146. /// general bias against block splitting purely for implementation simplicity.
  147. bool MachineScheduler::runOnMachineFunction(MachineFunction &mf) {
  148. DEBUG(dbgs() << "Before MISsched:\n"; mf.print(dbgs()));
  149. // Initialize the context of the pass.
  150. MF = &mf;
  151. MLI = &getAnalysis<MachineLoopInfo>();
  152. MDT = &getAnalysis<MachineDominatorTree>();
  153. PassConfig = &getAnalysis<TargetPassConfig>();
  154. AA = &getAnalysis<AliasAnalysis>();
  155. LIS = &getAnalysis<LiveIntervals>();
  156. const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
  157. RegClassInfo->runOnMachineFunction(*MF);
  158. // Select the scheduler, or set the default.
  159. MachineSchedRegistry::ScheduleDAGCtor Ctor = MachineSchedOpt;
  160. if (Ctor == useDefaultMachineSched) {
  161. // Get the default scheduler set by the target.
  162. Ctor = MachineSchedRegistry::getDefault();
  163. if (!Ctor) {
  164. Ctor = createConvergingSched;
  165. MachineSchedRegistry::setDefault(Ctor);
  166. }
  167. }
  168. // Instantiate the selected scheduler.
  169. OwningPtr<ScheduleDAGInstrs> Scheduler(Ctor(this));
  170. // Visit all machine basic blocks.
  171. //
  172. // TODO: Visit blocks in global postorder or postorder within the bottom-up
  173. // loop tree. Then we can optionally compute global RegPressure.
  174. for (MachineFunction::iterator MBB = MF->begin(), MBBEnd = MF->end();
  175. MBB != MBBEnd; ++MBB) {
  176. Scheduler->startBlock(MBB);
  177. // Break the block into scheduling regions [I, RegionEnd), and schedule each
  178. // region as soon as it is discovered. RegionEnd points the the scheduling
  179. // boundary at the bottom of the region. The DAG does not include RegionEnd,
  180. // but the region does (i.e. the next RegionEnd is above the previous
  181. // RegionBegin). If the current block has no terminator then RegionEnd ==
  182. // MBB->end() for the bottom region.
  183. //
  184. // The Scheduler may insert instructions during either schedule() or
  185. // exitRegion(), even for empty regions. So the local iterators 'I' and
  186. // 'RegionEnd' are invalid across these calls.
  187. unsigned RemainingCount = MBB->size();
  188. for(MachineBasicBlock::iterator RegionEnd = MBB->end();
  189. RegionEnd != MBB->begin(); RegionEnd = Scheduler->begin()) {
  190. // Avoid decrementing RegionEnd for blocks with no terminator.
  191. if (RegionEnd != MBB->end()
  192. || TII->isSchedulingBoundary(llvm::prior(RegionEnd), MBB, *MF)) {
  193. --RegionEnd;
  194. // Count the boundary instruction.
  195. --RemainingCount;
  196. }
  197. // The next region starts above the previous region. Look backward in the
  198. // instruction stream until we find the nearest boundary.
  199. MachineBasicBlock::iterator I = RegionEnd;
  200. for(;I != MBB->begin(); --I, --RemainingCount) {
  201. if (TII->isSchedulingBoundary(llvm::prior(I), MBB, *MF))
  202. break;
  203. }
  204. // Notify the scheduler of the region, even if we may skip scheduling
  205. // it. Perhaps it still needs to be bundled.
  206. Scheduler->enterRegion(MBB, I, RegionEnd, RemainingCount);
  207. // Skip empty scheduling regions (0 or 1 schedulable instructions).
  208. if (I == RegionEnd || I == llvm::prior(RegionEnd)) {
  209. // Close the current region. Bundle the terminator if needed.
  210. // This invalidates 'RegionEnd' and 'I'.
  211. Scheduler->exitRegion();
  212. continue;
  213. }
  214. DEBUG(dbgs() << "********** MI Scheduling **********\n");
  215. DEBUG(dbgs() << MF->getFunction()->getName()
  216. << ":BB#" << MBB->getNumber() << "\n From: " << *I << " To: ";
  217. if (RegionEnd != MBB->end()) dbgs() << *RegionEnd;
  218. else dbgs() << "End";
  219. dbgs() << " Remaining: " << RemainingCount << "\n");
  220. // Schedule a region: possibly reorder instructions.
  221. // This invalidates 'RegionEnd' and 'I'.
  222. Scheduler->schedule();
  223. // Close the current region.
  224. Scheduler->exitRegion();
  225. // Scheduling has invalidated the current iterator 'I'. Ask the
  226. // scheduler for the top of it's scheduled region.
  227. RegionEnd = Scheduler->begin();
  228. }
  229. assert(RemainingCount == 0 && "Instruction count mismatch!");
  230. Scheduler->finishBlock();
  231. }
  232. Scheduler->finalizeSchedule();
  233. DEBUG(LIS->print(dbgs()));
  234. return true;
  235. }
  236. void MachineScheduler::print(raw_ostream &O, const Module* m) const {
  237. // unimplemented
  238. }
  239. //===----------------------------------------------------------------------===//
  240. // MachineSchedStrategy - Interface to a machine scheduling algorithm.
  241. //===----------------------------------------------------------------------===//
  242. namespace {
  243. class ScheduleDAGMI;
  244. /// MachineSchedStrategy - Interface used by ScheduleDAGMI to drive the selected
  245. /// scheduling algorithm.
  246. ///
  247. /// If this works well and targets wish to reuse ScheduleDAGMI, we may expose it
  248. /// in ScheduleDAGInstrs.h
  249. class MachineSchedStrategy {
  250. public:
  251. virtual ~MachineSchedStrategy() {}
  252. /// Initialize the strategy after building the DAG for a new region.
  253. virtual void initialize(ScheduleDAGMI *DAG) = 0;
  254. /// Pick the next node to schedule, or return NULL. Set IsTopNode to true to
  255. /// schedule the node at the top of the unscheduled region. Otherwise it will
  256. /// be scheduled at the bottom.
  257. virtual SUnit *pickNode(bool &IsTopNode) = 0;
  258. /// Notify MachineSchedStrategy that ScheduleDAGMI has scheduled a node.
  259. virtual void schedNode(SUnit *SU, bool IsTopNode) = 0;
  260. /// When all predecessor dependencies have been resolved, free this node for
  261. /// top-down scheduling.
  262. virtual void releaseTopNode(SUnit *SU) = 0;
  263. /// When all successor dependencies have been resolved, free this node for
  264. /// bottom-up scheduling.
  265. virtual void releaseBottomNode(SUnit *SU) = 0;
  266. };
  267. } // namespace
  268. //===----------------------------------------------------------------------===//
  269. // ScheduleDAGMI - Base class for MachineInstr scheduling with LiveIntervals
  270. // preservation.
  271. //===----------------------------------------------------------------------===//
  272. namespace {
  273. /// ScheduleDAGMI is an implementation of ScheduleDAGInstrs that schedules
  274. /// machine instructions while updating LiveIntervals.
  275. class ScheduleDAGMI : public ScheduleDAGInstrs {
  276. AliasAnalysis *AA;
  277. RegisterClassInfo *RegClassInfo;
  278. MachineSchedStrategy *SchedImpl;
  279. MachineBasicBlock::iterator LiveRegionEnd;
  280. /// Register pressure in this region computed by buildSchedGraph.
  281. IntervalPressure RegPressure;
  282. RegPressureTracker RPTracker;
  283. /// List of pressure sets that exceed the target's pressure limit before
  284. /// scheduling, listed in increasing set ID order. Each pressure set is paired
  285. /// with its max pressure in the currently scheduled regions.
  286. std::vector<PressureElement> RegionCriticalPSets;
  287. /// The top of the unscheduled zone.
  288. MachineBasicBlock::iterator CurrentTop;
  289. IntervalPressure TopPressure;
  290. RegPressureTracker TopRPTracker;
  291. /// The bottom of the unscheduled zone.
  292. MachineBasicBlock::iterator CurrentBottom;
  293. IntervalPressure BotPressure;
  294. RegPressureTracker BotRPTracker;
  295. #ifndef NDEBUG
  296. /// The number of instructions scheduled so far. Used to cut off the
  297. /// scheduler at the point determined by misched-cutoff.
  298. unsigned NumInstrsScheduled;
  299. #endif
  300. public:
  301. ScheduleDAGMI(MachineSchedContext *C, MachineSchedStrategy *S):
  302. ScheduleDAGInstrs(*C->MF, *C->MLI, *C->MDT, /*IsPostRA=*/false, C->LIS),
  303. AA(C->AA), RegClassInfo(C->RegClassInfo), SchedImpl(S),
  304. RPTracker(RegPressure), CurrentTop(), TopRPTracker(TopPressure),
  305. CurrentBottom(), BotRPTracker(BotPressure) {
  306. #ifndef NDEBUG
  307. NumInstrsScheduled = 0;
  308. #endif
  309. }
  310. ~ScheduleDAGMI() {
  311. delete SchedImpl;
  312. }
  313. MachineBasicBlock::iterator top() const { return CurrentTop; }
  314. MachineBasicBlock::iterator bottom() const { return CurrentBottom; }
  315. /// Implement the ScheduleDAGInstrs interface for handling the next scheduling
  316. /// region. This covers all instructions in a block, while schedule() may only
  317. /// cover a subset.
  318. void enterRegion(MachineBasicBlock *bb,
  319. MachineBasicBlock::iterator begin,
  320. MachineBasicBlock::iterator end,
  321. unsigned endcount);
  322. /// Implement ScheduleDAGInstrs interface for scheduling a sequence of
  323. /// reorderable instructions.
  324. void schedule();
  325. /// Get current register pressure for the top scheduled instructions.
  326. const IntervalPressure &getTopPressure() const { return TopPressure; }
  327. const RegPressureTracker &getTopRPTracker() const { return TopRPTracker; }
  328. /// Get current register pressure for the bottom scheduled instructions.
  329. const IntervalPressure &getBotPressure() const { return BotPressure; }
  330. const RegPressureTracker &getBotRPTracker() const { return BotRPTracker; }
  331. /// Get register pressure for the entire scheduling region before scheduling.
  332. const IntervalPressure &getRegPressure() const { return RegPressure; }
  333. const std::vector<PressureElement> &getRegionCriticalPSets() const {
  334. return RegionCriticalPSets;
  335. }
  336. /// getIssueWidth - Return the max instructions per scheduling group.
  337. unsigned getIssueWidth() const {
  338. return (InstrItins && InstrItins->SchedModel)
  339. ? InstrItins->SchedModel->IssueWidth : 1;
  340. }
  341. /// getNumMicroOps - Return the number of issue slots required for this MI.
  342. unsigned getNumMicroOps(MachineInstr *MI) const {
  343. if (!InstrItins) return 1;
  344. int UOps = InstrItins->getNumMicroOps(MI->getDesc().getSchedClass());
  345. return (UOps >= 0) ? UOps : TII->getNumMicroOps(InstrItins, MI);
  346. }
  347. protected:
  348. void initRegPressure();
  349. void updateScheduledPressure(std::vector<unsigned> NewMaxPressure);
  350. void moveInstruction(MachineInstr *MI, MachineBasicBlock::iterator InsertPos);
  351. bool checkSchedLimit();
  352. void releaseRoots();
  353. void releaseSucc(SUnit *SU, SDep *SuccEdge);
  354. void releaseSuccessors(SUnit *SU);
  355. void releasePred(SUnit *SU, SDep *PredEdge);
  356. void releasePredecessors(SUnit *SU);
  357. void placeDebugValues();
  358. };
  359. } // namespace
  360. /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. When
  361. /// NumPredsLeft reaches zero, release the successor node.
  362. ///
  363. /// FIXME: Adjust SuccSU height based on MinLatency.
  364. void ScheduleDAGMI::releaseSucc(SUnit *SU, SDep *SuccEdge) {
  365. SUnit *SuccSU = SuccEdge->getSUnit();
  366. #ifndef NDEBUG
  367. if (SuccSU->NumPredsLeft == 0) {
  368. dbgs() << "*** Scheduling failed! ***\n";
  369. SuccSU->dump(this);
  370. dbgs() << " has been released too many times!\n";
  371. llvm_unreachable(0);
  372. }
  373. #endif
  374. --SuccSU->NumPredsLeft;
  375. if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
  376. SchedImpl->releaseTopNode(SuccSU);
  377. }
  378. /// releaseSuccessors - Call releaseSucc on each of SU's successors.
  379. void ScheduleDAGMI::releaseSuccessors(SUnit *SU) {
  380. for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
  381. I != E; ++I) {
  382. releaseSucc(SU, &*I);
  383. }
  384. }
  385. /// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. When
  386. /// NumSuccsLeft reaches zero, release the predecessor node.
  387. ///
  388. /// FIXME: Adjust PredSU height based on MinLatency.
  389. void ScheduleDAGMI::releasePred(SUnit *SU, SDep *PredEdge) {
  390. SUnit *PredSU = PredEdge->getSUnit();
  391. #ifndef NDEBUG
  392. if (PredSU->NumSuccsLeft == 0) {
  393. dbgs() << "*** Scheduling failed! ***\n";
  394. PredSU->dump(this);
  395. dbgs() << " has been released too many times!\n";
  396. llvm_unreachable(0);
  397. }
  398. #endif
  399. --PredSU->NumSuccsLeft;
  400. if (PredSU->NumSuccsLeft == 0 && PredSU != &EntrySU)
  401. SchedImpl->releaseBottomNode(PredSU);
  402. }
  403. /// releasePredecessors - Call releasePred on each of SU's predecessors.
  404. void ScheduleDAGMI::releasePredecessors(SUnit *SU) {
  405. for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
  406. I != E; ++I) {
  407. releasePred(SU, &*I);
  408. }
  409. }
  410. void ScheduleDAGMI::moveInstruction(MachineInstr *MI,
  411. MachineBasicBlock::iterator InsertPos) {
  412. // Advance RegionBegin if the first instruction moves down.
  413. if (&*RegionBegin == MI)
  414. ++RegionBegin;
  415. // Update the instruction stream.
  416. BB->splice(InsertPos, BB, MI);
  417. // Update LiveIntervals
  418. LIS->handleMove(MI);
  419. // Recede RegionBegin if an instruction moves above the first.
  420. if (RegionBegin == InsertPos)
  421. RegionBegin = MI;
  422. }
  423. bool ScheduleDAGMI::checkSchedLimit() {
  424. #ifndef NDEBUG
  425. if (NumInstrsScheduled == MISchedCutoff && MISchedCutoff != ~0U) {
  426. CurrentTop = CurrentBottom;
  427. return false;
  428. }
  429. ++NumInstrsScheduled;
  430. #endif
  431. return true;
  432. }
  433. /// enterRegion - Called back from MachineScheduler::runOnMachineFunction after
  434. /// crossing a scheduling boundary. [begin, end) includes all instructions in
  435. /// the region, including the boundary itself and single-instruction regions
  436. /// that don't get scheduled.
  437. void ScheduleDAGMI::enterRegion(MachineBasicBlock *bb,
  438. MachineBasicBlock::iterator begin,
  439. MachineBasicBlock::iterator end,
  440. unsigned endcount)
  441. {
  442. ScheduleDAGInstrs::enterRegion(bb, begin, end, endcount);
  443. // For convenience remember the end of the liveness region.
  444. LiveRegionEnd =
  445. (RegionEnd == bb->end()) ? RegionEnd : llvm::next(RegionEnd);
  446. }
  447. // Setup the register pressure trackers for the top scheduled top and bottom
  448. // scheduled regions.
  449. void ScheduleDAGMI::initRegPressure() {
  450. TopRPTracker.init(&MF, RegClassInfo, LIS, BB, RegionBegin);
  451. BotRPTracker.init(&MF, RegClassInfo, LIS, BB, LiveRegionEnd);
  452. // Close the RPTracker to finalize live ins.
  453. RPTracker.closeRegion();
  454. DEBUG(RPTracker.getPressure().dump(TRI));
  455. // Initialize the live ins and live outs.
  456. TopRPTracker.addLiveRegs(RPTracker.getPressure().LiveInRegs);
  457. BotRPTracker.addLiveRegs(RPTracker.getPressure().LiveOutRegs);
  458. // Close one end of the tracker so we can call
  459. // getMaxUpward/DownwardPressureDelta before advancing across any
  460. // instructions. This converts currently live regs into live ins/outs.
  461. TopRPTracker.closeTop();
  462. BotRPTracker.closeBottom();
  463. // Account for liveness generated by the region boundary.
  464. if (LiveRegionEnd != RegionEnd)
  465. BotRPTracker.recede();
  466. assert(BotRPTracker.getPos() == RegionEnd && "Can't find the region bottom");
  467. // Cache the list of excess pressure sets in this region. This will also track
  468. // the max pressure in the scheduled code for these sets.
  469. RegionCriticalPSets.clear();
  470. std::vector<unsigned> RegionPressure = RPTracker.getPressure().MaxSetPressure;
  471. for (unsigned i = 0, e = RegionPressure.size(); i < e; ++i) {
  472. unsigned Limit = TRI->getRegPressureSetLimit(i);
  473. if (RegionPressure[i] > Limit)
  474. RegionCriticalPSets.push_back(PressureElement(i, 0));
  475. }
  476. DEBUG(dbgs() << "Excess PSets: ";
  477. for (unsigned i = 0, e = RegionCriticalPSets.size(); i != e; ++i)
  478. dbgs() << TRI->getRegPressureSetName(
  479. RegionCriticalPSets[i].PSetID) << " ";
  480. dbgs() << "\n");
  481. }
  482. // FIXME: When the pressure tracker deals in pressure differences then we won't
  483. // iterate over all RegionCriticalPSets[i].
  484. void ScheduleDAGMI::
  485. updateScheduledPressure(std::vector<unsigned> NewMaxPressure) {
  486. for (unsigned i = 0, e = RegionCriticalPSets.size(); i < e; ++i) {
  487. unsigned ID = RegionCriticalPSets[i].PSetID;
  488. int &MaxUnits = RegionCriticalPSets[i].UnitIncrease;
  489. if ((int)NewMaxPressure[ID] > MaxUnits)
  490. MaxUnits = NewMaxPressure[ID];
  491. }
  492. }
  493. // Release all DAG roots for scheduling.
  494. void ScheduleDAGMI::releaseRoots() {
  495. SmallVector<SUnit*, 16> BotRoots;
  496. for (std::vector<SUnit>::iterator
  497. I = SUnits.begin(), E = SUnits.end(); I != E; ++I) {
  498. // A SUnit is ready to top schedule if it has no predecessors.
  499. if (I->Preds.empty())
  500. SchedImpl->releaseTopNode(&(*I));
  501. // A SUnit is ready to bottom schedule if it has no successors.
  502. if (I->Succs.empty())
  503. BotRoots.push_back(&(*I));
  504. }
  505. // Release bottom roots in reverse order so the higher priority nodes appear
  506. // first. This is more natural and slightly more efficient.
  507. for (SmallVectorImpl<SUnit*>::const_reverse_iterator
  508. I = BotRoots.rbegin(), E = BotRoots.rend(); I != E; ++I)
  509. SchedImpl->releaseBottomNode(*I);
  510. }
  511. /// schedule - Called back from MachineScheduler::runOnMachineFunction
  512. /// after setting up the current scheduling region. [RegionBegin, RegionEnd)
  513. /// only includes instructions that have DAG nodes, not scheduling boundaries.
  514. void ScheduleDAGMI::schedule() {
  515. // Initialize the register pressure tracker used by buildSchedGraph.
  516. RPTracker.init(&MF, RegClassInfo, LIS, BB, LiveRegionEnd);
  517. // Account for liveness generate by the region boundary.
  518. if (LiveRegionEnd != RegionEnd)
  519. RPTracker.recede();
  520. // Build the DAG, and compute current register pressure.
  521. buildSchedGraph(AA, &RPTracker);
  522. // Initialize top/bottom trackers after computing region pressure.
  523. initRegPressure();
  524. DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
  525. SUnits[su].dumpAll(this));
  526. if (ViewMISchedDAGs) viewGraph();
  527. SchedImpl->initialize(this);
  528. // Release edges from the special Entry node or to the special Exit node.
  529. releaseSuccessors(&EntrySU);
  530. releasePredecessors(&ExitSU);
  531. // Release all DAG roots for scheduling.
  532. releaseRoots();
  533. CurrentTop = nextIfDebug(RegionBegin, RegionEnd);
  534. CurrentBottom = RegionEnd;
  535. bool IsTopNode = false;
  536. while (SUnit *SU = SchedImpl->pickNode(IsTopNode)) {
  537. if (!checkSchedLimit())
  538. break;
  539. // Move the instruction to its new location in the instruction stream.
  540. MachineInstr *MI = SU->getInstr();
  541. if (IsTopNode) {
  542. assert(SU->isTopReady() && "node still has unscheduled dependencies");
  543. if (&*CurrentTop == MI)
  544. CurrentTop = nextIfDebug(++CurrentTop, CurrentBottom);
  545. else {
  546. moveInstruction(MI, CurrentTop);
  547. TopRPTracker.setPos(MI);
  548. }
  549. // Update top scheduled pressure.
  550. TopRPTracker.advance();
  551. assert(TopRPTracker.getPos() == CurrentTop && "out of sync");
  552. updateScheduledPressure(TopRPTracker.getPressure().MaxSetPressure);
  553. // Release dependent instructions for scheduling.
  554. releaseSuccessors(SU);
  555. }
  556. else {
  557. assert(SU->isBottomReady() && "node still has unscheduled dependencies");
  558. MachineBasicBlock::iterator priorII =
  559. priorNonDebug(CurrentBottom, CurrentTop);
  560. if (&*priorII == MI)
  561. CurrentBottom = priorII;
  562. else {
  563. if (&*CurrentTop == MI) {
  564. CurrentTop = nextIfDebug(++CurrentTop, priorII);
  565. TopRPTracker.setPos(CurrentTop);
  566. }
  567. moveInstruction(MI, CurrentBottom);
  568. CurrentBottom = MI;
  569. }
  570. // Update bottom scheduled pressure.
  571. BotRPTracker.recede();
  572. assert(BotRPTracker.getPos() == CurrentBottom && "out of sync");
  573. updateScheduledPressure(BotRPTracker.getPressure().MaxSetPressure);
  574. // Release dependent instructions for scheduling.
  575. releasePredecessors(SU);
  576. }
  577. SU->isScheduled = true;
  578. SchedImpl->schedNode(SU, IsTopNode);
  579. }
  580. assert(CurrentTop == CurrentBottom && "Nonempty unscheduled zone.");
  581. placeDebugValues();
  582. }
  583. /// Reinsert any remaining debug_values, just like the PostRA scheduler.
  584. void ScheduleDAGMI::placeDebugValues() {
  585. // If first instruction was a DBG_VALUE then put it back.
  586. if (FirstDbgValue) {
  587. BB->splice(RegionBegin, BB, FirstDbgValue);
  588. RegionBegin = FirstDbgValue;
  589. }
  590. for (std::vector<std::pair<MachineInstr *, MachineInstr *> >::iterator
  591. DI = DbgValues.end(), DE = DbgValues.begin(); DI != DE; --DI) {
  592. std::pair<MachineInstr *, MachineInstr *> P = *prior(DI);
  593. MachineInstr *DbgValue = P.first;
  594. MachineBasicBlock::iterator OrigPrevMI = P.second;
  595. BB->splice(++OrigPrevMI, BB, DbgValue);
  596. if (OrigPrevMI == llvm::prior(RegionEnd))
  597. RegionEnd = DbgValue;
  598. }
  599. DbgValues.clear();
  600. FirstDbgValue = NULL;
  601. }
  602. //===----------------------------------------------------------------------===//
  603. // ConvergingScheduler - Implementation of the standard MachineSchedStrategy.
  604. //===----------------------------------------------------------------------===//
  605. namespace {
  606. /// ReadyQueue encapsulates vector of "ready" SUnits with basic convenience
  607. /// methods for pushing and removing nodes. ReadyQueue's are uniquely identified
  608. /// by an ID. SUnit::NodeQueueId is a mask of the ReadyQueues the SUnit is in.
  609. class ReadyQueue {
  610. unsigned ID;
  611. std::string Name;
  612. std::vector<SUnit*> Queue;
  613. public:
  614. ReadyQueue(unsigned id, const Twine &name): ID(id), Name(name.str()) {}
  615. unsigned getID() const { return ID; }
  616. StringRef getName() const { return Name; }
  617. // SU is in this queue if it's NodeQueueID is a superset of this ID.
  618. bool isInQueue(SUnit *SU) const { return (SU->NodeQueueId & ID); }
  619. bool empty() const { return Queue.empty(); }
  620. unsigned size() const { return Queue.size(); }
  621. typedef std::vector<SUnit*>::iterator iterator;
  622. iterator begin() { return Queue.begin(); }
  623. iterator end() { return Queue.end(); }
  624. iterator find(SUnit *SU) {
  625. return std::find(Queue.begin(), Queue.end(), SU);
  626. }
  627. void push(SUnit *SU) {
  628. Queue.push_back(SU);
  629. SU->NodeQueueId |= ID;
  630. }
  631. void remove(iterator I) {
  632. (*I)->NodeQueueId &= ~ID;
  633. *I = Queue.back();
  634. Queue.pop_back();
  635. }
  636. void dump() {
  637. dbgs() << Name << ": ";
  638. for (unsigned i = 0, e = Queue.size(); i < e; ++i)
  639. dbgs() << Queue[i]->NodeNum << " ";
  640. dbgs() << "\n";
  641. }
  642. };
  643. /// ConvergingScheduler shrinks the unscheduled zone using heuristics to balance
  644. /// the schedule.
  645. class ConvergingScheduler : public MachineSchedStrategy {
  646. /// Store the state used by ConvergingScheduler heuristics, required for the
  647. /// lifetime of one invocation of pickNode().
  648. struct SchedCandidate {
  649. // The best SUnit candidate.
  650. SUnit *SU;
  651. // Register pressure values for the best candidate.
  652. RegPressureDelta RPDelta;
  653. SchedCandidate(): SU(NULL) {}
  654. };
  655. /// Represent the type of SchedCandidate found within a single queue.
  656. enum CandResult {
  657. NoCand, NodeOrder, SingleExcess, SingleCritical, SingleMax, MultiPressure };
  658. /// Each Scheduling boundary is associated with ready queues. It tracks the
  659. /// current cycle in whichever direction at has moved, and maintains the state
  660. /// of "hazards" and other interlocks at the current cycle.
  661. struct SchedBoundary {
  662. ScheduleDAGMI *DAG;
  663. ReadyQueue Available;
  664. ReadyQueue Pending;
  665. bool CheckPending;
  666. ScheduleHazardRecognizer *HazardRec;
  667. unsigned CurrCycle;
  668. unsigned IssueCount;
  669. /// MinReadyCycle - Cycle of the soonest available instruction.
  670. unsigned MinReadyCycle;
  671. // Remember the greatest min operand latency.
  672. unsigned MaxMinLatency;
  673. /// Pending queues extend the ready queues with the same ID and the
  674. /// PendingFlag set.
  675. SchedBoundary(unsigned ID, const Twine &Name):
  676. DAG(0), Available(ID, Name+".A"),
  677. Pending(ID << ConvergingScheduler::LogMaxQID, Name+".P"),
  678. CheckPending(false), HazardRec(0), CurrCycle(0), IssueCount(0),
  679. MinReadyCycle(UINT_MAX), MaxMinLatency(0) {}
  680. ~SchedBoundary() { delete HazardRec; }
  681. bool isTop() const {
  682. return Available.getID() == ConvergingScheduler::TopQID;
  683. }
  684. bool checkHazard(SUnit *SU);
  685. void releaseNode(SUnit *SU, unsigned ReadyCycle);
  686. void bumpCycle();
  687. void bumpNode(SUnit *SU);
  688. void releasePending();
  689. void removeReady(SUnit *SU);
  690. SUnit *pickOnlyChoice();
  691. };
  692. ScheduleDAGMI *DAG;
  693. const TargetRegisterInfo *TRI;
  694. // State of the top and bottom scheduled instruction boundaries.
  695. SchedBoundary Top;
  696. SchedBoundary Bot;
  697. public:
  698. /// SUnit::NodeQueueId: 0 (none), 1 (top), 2 (bot), 3 (both)
  699. enum {
  700. TopQID = 1,
  701. BotQID = 2,
  702. LogMaxQID = 2
  703. };
  704. ConvergingScheduler():
  705. DAG(0), TRI(0), Top(TopQID, "TopQ"), Bot(BotQID, "BotQ") {}
  706. virtual void initialize(ScheduleDAGMI *dag);
  707. virtual SUnit *pickNode(bool &IsTopNode);
  708. virtual void schedNode(SUnit *SU, bool IsTopNode);
  709. virtual void releaseTopNode(SUnit *SU);
  710. virtual void releaseBottomNode(SUnit *SU);
  711. protected:
  712. SUnit *pickNodeBidrectional(bool &IsTopNode);
  713. CandResult pickNodeFromQueue(ReadyQueue &Q,
  714. const RegPressureTracker &RPTracker,
  715. SchedCandidate &Candidate);
  716. #ifndef NDEBUG
  717. void traceCandidate(const char *Label, const ReadyQueue &Q, SUnit *SU,
  718. PressureElement P = PressureElement());
  719. #endif
  720. };
  721. } // namespace
  722. void ConvergingScheduler::initialize(ScheduleDAGMI *dag) {
  723. DAG = dag;
  724. TRI = DAG->TRI;
  725. Top.DAG = dag;
  726. Bot.DAG = dag;
  727. // Initialize the HazardRecognizers.
  728. const TargetMachine &TM = DAG->MF.getTarget();
  729. const InstrItineraryData *Itin = TM.getInstrItineraryData();
  730. Top.HazardRec = TM.getInstrInfo()->CreateTargetMIHazardRecognizer(Itin, DAG);
  731. Bot.HazardRec = TM.getInstrInfo()->CreateTargetMIHazardRecognizer(Itin, DAG);
  732. assert((!ForceTopDown || !ForceBottomUp) &&
  733. "-misched-topdown incompatible with -misched-bottomup");
  734. }
  735. void ConvergingScheduler::releaseTopNode(SUnit *SU) {
  736. if (SU->isScheduled)
  737. return;
  738. for (SUnit::succ_iterator I = SU->Preds.begin(), E = SU->Preds.end();
  739. I != E; ++I) {
  740. unsigned PredReadyCycle = I->getSUnit()->TopReadyCycle;
  741. unsigned Latency =
  742. DAG->computeOperandLatency(I->getSUnit(), SU, *I, /*FindMin=*/true);
  743. #ifndef NDEBUG
  744. Top.MaxMinLatency = std::max(Latency, Top.MaxMinLatency);
  745. #endif
  746. if (SU->TopReadyCycle < PredReadyCycle + Latency)
  747. SU->TopReadyCycle = PredReadyCycle + Latency;
  748. }
  749. Top.releaseNode(SU, SU->TopReadyCycle);
  750. }
  751. void ConvergingScheduler::releaseBottomNode(SUnit *SU) {
  752. if (SU->isScheduled)
  753. return;
  754. assert(SU->getInstr() && "Scheduled SUnit must have instr");
  755. for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
  756. I != E; ++I) {
  757. unsigned SuccReadyCycle = I->getSUnit()->BotReadyCycle;
  758. unsigned Latency =
  759. DAG->computeOperandLatency(SU, I->getSUnit(), *I, /*FindMin=*/true);
  760. #ifndef NDEBUG
  761. Bot.MaxMinLatency = std::max(Latency, Bot.MaxMinLatency);
  762. #endif
  763. if (SU->BotReadyCycle < SuccReadyCycle + Latency)
  764. SU->BotReadyCycle = SuccReadyCycle + Latency;
  765. }
  766. Bot.releaseNode(SU, SU->BotReadyCycle);
  767. }
  768. /// Does this SU have a hazard within the current instruction group.
  769. ///
  770. /// The scheduler supports two modes of hazard recognition. The first is the
  771. /// ScheduleHazardRecognizer API. It is a fully general hazard recognizer that
  772. /// supports highly complicated in-order reservation tables
  773. /// (ScoreboardHazardRecognizer) and arbitraty target-specific logic.
  774. ///
  775. /// The second is a streamlined mechanism that checks for hazards based on
  776. /// simple counters that the scheduler itself maintains. It explicitly checks
  777. /// for instruction dispatch limitations, including the number of micro-ops that
  778. /// can dispatch per cycle.
  779. ///
  780. /// TODO: Also check whether the SU must start a new group.
  781. bool ConvergingScheduler::SchedBoundary::checkHazard(SUnit *SU) {
  782. if (HazardRec->isEnabled())
  783. return HazardRec->getHazardType(SU) != ScheduleHazardRecognizer::NoHazard;
  784. if (IssueCount + DAG->getNumMicroOps(SU->getInstr()) > DAG->getIssueWidth())
  785. return true;
  786. return false;
  787. }
  788. void ConvergingScheduler::SchedBoundary::releaseNode(SUnit *SU,
  789. unsigned ReadyCycle) {
  790. if (ReadyCycle < MinReadyCycle)
  791. MinReadyCycle = ReadyCycle;
  792. // Check for interlocks first. For the purpose of other heuristics, an
  793. // instruction that cannot issue appears as if it's not in the ReadyQueue.
  794. if (ReadyCycle > CurrCycle || checkHazard(SU))
  795. Pending.push(SU);
  796. else
  797. Available.push(SU);
  798. }
  799. /// Move the boundary of scheduled code by one cycle.
  800. void ConvergingScheduler::SchedBoundary::bumpCycle() {
  801. unsigned Width = DAG->getIssueWidth();
  802. IssueCount = (IssueCount <= Width) ? 0 : IssueCount - Width;
  803. assert(MinReadyCycle < UINT_MAX && "MinReadyCycle uninitialized");
  804. unsigned NextCycle = std::max(CurrCycle + 1, MinReadyCycle);
  805. if (!HazardRec->isEnabled()) {
  806. // Bypass HazardRec virtual calls.
  807. CurrCycle = NextCycle;
  808. }
  809. else {
  810. // Bypass getHazardType calls in case of long latency.
  811. for (; CurrCycle != NextCycle; ++CurrCycle) {
  812. if (isTop())
  813. HazardRec->AdvanceCycle();
  814. else
  815. HazardRec->RecedeCycle();
  816. }
  817. }
  818. CheckPending = true;
  819. DEBUG(dbgs() << "*** " << Available.getName() << " cycle "
  820. << CurrCycle << '\n');
  821. }
  822. /// Move the boundary of scheduled code by one SUnit.
  823. void ConvergingScheduler::SchedBoundary::bumpNode(SUnit *SU) {
  824. // Update the reservation table.
  825. if (HazardRec->isEnabled()) {
  826. if (!isTop() && SU->isCall) {
  827. // Calls are scheduled with their preceding instructions. For bottom-up
  828. // scheduling, clear the pipeline state before emitting.
  829. HazardRec->Reset();
  830. }
  831. HazardRec->EmitInstruction(SU);
  832. }
  833. // Check the instruction group dispatch limit.
  834. // TODO: Check if this SU must end a dispatch group.
  835. IssueCount += DAG->getNumMicroOps(SU->getInstr());
  836. if (IssueCount >= DAG->getIssueWidth()) {
  837. DEBUG(dbgs() << "*** Max instrs at cycle " << CurrCycle << '\n');
  838. bumpCycle();
  839. }
  840. }
  841. /// Release pending ready nodes in to the available queue. This makes them
  842. /// visible to heuristics.
  843. void ConvergingScheduler::SchedBoundary::releasePending() {
  844. // If the available queue is empty, it is safe to reset MinReadyCycle.
  845. if (Available.empty())
  846. MinReadyCycle = UINT_MAX;
  847. // Check to see if any of the pending instructions are ready to issue. If
  848. // so, add them to the available queue.
  849. for (unsigned i = 0, e = Pending.size(); i != e; ++i) {
  850. SUnit *SU = *(Pending.begin()+i);
  851. unsigned ReadyCycle = isTop() ? SU->TopReadyCycle : SU->BotReadyCycle;
  852. if (ReadyCycle < MinReadyCycle)
  853. MinReadyCycle = ReadyCycle;
  854. if (ReadyCycle > CurrCycle)
  855. continue;
  856. if (checkHazard(SU))
  857. continue;
  858. Available.push(SU);
  859. Pending.remove(Pending.begin()+i);
  860. --i; --e;
  861. }
  862. CheckPending = false;
  863. }
  864. /// Remove SU from the ready set for this boundary.
  865. void ConvergingScheduler::SchedBoundary::removeReady(SUnit *SU) {
  866. if (Available.isInQueue(SU))
  867. Available.remove(Available.find(SU));
  868. else {
  869. assert(Pending.isInQueue(SU) && "bad ready count");
  870. Pending.remove(Pending.find(SU));
  871. }
  872. }
  873. /// If this queue only has one ready candidate, return it. As a side effect,
  874. /// advance the cycle until at least one node is ready. If multiple instructions
  875. /// are ready, return NULL.
  876. SUnit *ConvergingScheduler::SchedBoundary::pickOnlyChoice() {
  877. if (CheckPending)
  878. releasePending();
  879. for (unsigned i = 0; Available.empty(); ++i) {
  880. assert(i <= (HazardRec->getMaxLookAhead() + MaxMinLatency) &&
  881. "permanent hazard"); (void)i;
  882. bumpCycle();
  883. releasePending();
  884. }
  885. if (Available.size() == 1)
  886. return *Available.begin();
  887. return NULL;
  888. }
  889. #ifndef NDEBUG
  890. void ConvergingScheduler::traceCandidate(const char *Label, const ReadyQueue &Q,
  891. SUnit *SU, PressureElement P) {
  892. dbgs() << Label << " " << Q.getName() << " ";
  893. if (P.isValid())
  894. dbgs() << TRI->getRegPressureSetName(P.PSetID) << ":" << P.UnitIncrease
  895. << " ";
  896. else
  897. dbgs() << " ";
  898. SU->dump(DAG);
  899. }
  900. #endif
  901. /// pickNodeFromQueue helper that returns true if the LHS reg pressure effect is
  902. /// more desirable than RHS from scheduling standpoint.
  903. static bool compareRPDelta(const RegPressureDelta &LHS,
  904. const RegPressureDelta &RHS) {
  905. // Compare each component of pressure in decreasing order of importance
  906. // without checking if any are valid. Invalid PressureElements are assumed to
  907. // have UnitIncrease==0, so are neutral.
  908. // Avoid increasing the max critical pressure in the scheduled region.
  909. if (LHS.Excess.UnitIncrease != RHS.Excess.UnitIncrease)
  910. return LHS.Excess.UnitIncrease < RHS.Excess.UnitIncrease;
  911. // Avoid increasing the max critical pressure in the scheduled region.
  912. if (LHS.CriticalMax.UnitIncrease != RHS.CriticalMax.UnitIncrease)
  913. return LHS.CriticalMax.UnitIncrease < RHS.CriticalMax.UnitIncrease;
  914. // Avoid increasing the max pressure of the entire region.
  915. if (LHS.CurrentMax.UnitIncrease != RHS.CurrentMax.UnitIncrease)
  916. return LHS.CurrentMax.UnitIncrease < RHS.CurrentMax.UnitIncrease;
  917. return false;
  918. }
  919. /// Pick the best candidate from the top queue.
  920. ///
  921. /// TODO: getMaxPressureDelta results can be mostly cached for each SUnit during
  922. /// DAG building. To adjust for the current scheduling location we need to
  923. /// maintain the number of vreg uses remaining to be top-scheduled.
  924. ConvergingScheduler::CandResult ConvergingScheduler::
  925. pickNodeFromQueue(ReadyQueue &Q, const RegPressureTracker &RPTracker,
  926. SchedCandidate &Candidate) {
  927. DEBUG(Q.dump());
  928. // getMaxPressureDelta temporarily modifies the tracker.
  929. RegPressureTracker &TempTracker = const_cast<RegPressureTracker&>(RPTracker);
  930. // BestSU remains NULL if no top candidates beat the best existing candidate.
  931. CandResult FoundCandidate = NoCand;
  932. for (ReadyQueue::iterator I = Q.begin(), E = Q.end(); I != E; ++I) {
  933. RegPressureDelta RPDelta;
  934. TempTracker.getMaxPressureDelta((*I)->getInstr(), RPDelta,
  935. DAG->getRegionCriticalPSets(),
  936. DAG->getRegPressure().MaxSetPressure);
  937. // Initialize the candidate if needed.
  938. if (!Candidate.SU) {
  939. Candidate.SU = *I;
  940. Candidate.RPDelta = RPDelta;
  941. FoundCandidate = NodeOrder;
  942. continue;
  943. }
  944. // Avoid exceeding the target's limit.
  945. if (RPDelta.Excess.UnitIncrease < Candidate.RPDelta.Excess.UnitIncrease) {
  946. DEBUG(traceCandidate("ECAND", Q, *I, RPDelta.Excess));
  947. Candidate.SU = *I;
  948. Candidate.RPDelta = RPDelta;
  949. FoundCandidate = SingleExcess;
  950. continue;
  951. }
  952. if (RPDelta.Excess.UnitIncrease > Candidate.RPDelta.Excess.UnitIncrease)
  953. continue;
  954. if (FoundCandidate == SingleExcess)
  955. FoundCandidate = MultiPressure;
  956. // Avoid increasing the max critical pressure in the scheduled region.
  957. if (RPDelta.CriticalMax.UnitIncrease
  958. < Candidate.RPDelta.CriticalMax.UnitIncrease) {
  959. DEBUG(traceCandidate("PCAND", Q, *I, RPDelta.CriticalMax));
  960. Candidate.SU = *I;
  961. Candidate.RPDelta = RPDelta;
  962. FoundCandidate = SingleCritical;
  963. continue;
  964. }
  965. if (RPDelta.CriticalMax.UnitIncrease
  966. > Candidate.RPDelta.CriticalMax.UnitIncrease)
  967. continue;
  968. if (FoundCandidate == SingleCritical)
  969. FoundCandidate = MultiPressure;
  970. // Avoid increasing the max pressure of the entire region.
  971. if (RPDelta.CurrentMax.UnitIncrease
  972. < Candidate.RPDelta.CurrentMax.UnitIncrease) {
  973. DEBUG(traceCandidate("MCAND", Q, *I, RPDelta.CurrentMax));
  974. Candidate.SU = *I;
  975. Candidate.RPDelta = RPDelta;
  976. FoundCandidate = SingleMax;
  977. continue;
  978. }
  979. if (RPDelta.CurrentMax.UnitIncrease
  980. > Candidate.RPDelta.CurrentMax.UnitIncrease)
  981. continue;
  982. if (FoundCandidate == SingleMax)
  983. FoundCandidate = MultiPressure;
  984. // Fall through to original instruction order.
  985. // Only consider node order if Candidate was chosen from this Q.
  986. if (FoundCandidate == NoCand)
  987. continue;
  988. if ((Q.getID() == TopQID && (*I)->NodeNum < Candidate.SU->NodeNum)
  989. || (Q.getID() == BotQID && (*I)->NodeNum > Candidate.SU->NodeNum)) {
  990. DEBUG(traceCandidate("NCAND", Q, *I));
  991. Candidate.SU = *I;
  992. Candidate.RPDelta = RPDelta;
  993. FoundCandidate = NodeOrder;
  994. }
  995. }
  996. return FoundCandidate;
  997. }
  998. /// Pick the best candidate node from either the top or bottom queue.
  999. SUnit *ConvergingScheduler::pickNodeBidrectional(bool &IsTopNode) {
  1000. // Schedule as far as possible in the direction of no choice. This is most
  1001. // efficient, but also provides the best heuristics for CriticalPSets.
  1002. if (SUnit *SU = Bot.pickOnlyChoice()) {
  1003. IsTopNode = false;
  1004. return SU;
  1005. }
  1006. if (SUnit *SU = Top.pickOnlyChoice()) {
  1007. IsTopNode = true;
  1008. return SU;
  1009. }
  1010. SchedCandidate BotCand;
  1011. // Prefer bottom scheduling when heuristics are silent.
  1012. CandResult BotResult = pickNodeFromQueue(Bot.Available,
  1013. DAG->getBotRPTracker(), BotCand);
  1014. assert(BotResult != NoCand && "failed to find the first candidate");
  1015. // If either Q has a single candidate that provides the least increase in
  1016. // Excess pressure, we can immediately schedule from that Q.
  1017. //
  1018. // RegionCriticalPSets summarizes the pressure within the scheduled region and
  1019. // affects picking from either Q. If scheduling in one direction must
  1020. // increase pressure for one of the excess PSets, then schedule in that
  1021. // direction first to provide more freedom in the other direction.
  1022. if (BotResult == SingleExcess || BotResult == SingleCritical) {
  1023. IsTopNode = false;
  1024. return BotCand.SU;
  1025. }
  1026. // Check if the top Q has a better candidate.
  1027. SchedCandidate TopCand;
  1028. CandResult TopResult = pickNodeFromQueue(Top.Available,
  1029. DAG->getTopRPTracker(), TopCand);
  1030. assert(TopResult != NoCand && "failed to find the first candidate");
  1031. if (TopResult == SingleExcess || TopResult == SingleCritical) {
  1032. IsTopNode = true;
  1033. return TopCand.SU;
  1034. }
  1035. // If either Q has a single candidate that minimizes pressure above the
  1036. // original region's pressure pick it.
  1037. if (BotResult == SingleMax) {
  1038. IsTopNode = false;
  1039. return BotCand.SU;
  1040. }
  1041. if (TopResult == SingleMax) {
  1042. IsTopNode = true;
  1043. return TopCand.SU;
  1044. }
  1045. // Check for a salient pressure difference and pick the best from either side.
  1046. if (compareRPDelta(TopCand.RPDelta, BotCand.RPDelta)) {
  1047. IsTopNode = true;
  1048. return TopCand.SU;
  1049. }
  1050. // Otherwise prefer the bottom candidate in node order.
  1051. IsTopNode = false;
  1052. return BotCand.SU;
  1053. }
  1054. /// Pick the best node to balance the schedule. Implements MachineSchedStrategy.
  1055. SUnit *ConvergingScheduler::pickNode(bool &IsTopNode) {
  1056. if (DAG->top() == DAG->bottom()) {
  1057. assert(Top.Available.empty() && Top.Pending.empty() &&
  1058. Bot.Available.empty() && Bot.Pending.empty() && "ReadyQ garbage");
  1059. return NULL;
  1060. }
  1061. SUnit *SU;
  1062. if (ForceTopDown) {
  1063. SU = Top.pickOnlyChoice();
  1064. if (!SU) {
  1065. SchedCandidate TopCand;
  1066. CandResult TopResult =
  1067. pickNodeFromQueue(Top.Available, DAG->getTopRPTracker(), TopCand);
  1068. assert(TopResult != NoCand && "failed to find the first candidate");
  1069. (void)TopResult;
  1070. SU = TopCand.SU;
  1071. }
  1072. IsTopNode = true;
  1073. }
  1074. else if (ForceBottomUp) {
  1075. SU = Bot.pickOnlyChoice();
  1076. if (!SU) {
  1077. SchedCandidate BotCand;
  1078. CandResult BotResult =
  1079. pickNodeFromQueue(Bot.Available, DAG->getBotRPTracker(), BotCand);
  1080. assert(BotResult != NoCand && "failed to find the first candidate");
  1081. (void)BotResult;
  1082. SU = BotCand.SU;
  1083. }
  1084. IsTopNode = false;
  1085. }
  1086. else {
  1087. SU = pickNodeBidrectional(IsTopNode);
  1088. }
  1089. if (SU->isTopReady())
  1090. Top.removeReady(SU);
  1091. if (SU->isBottomReady())
  1092. Bot.removeReady(SU);
  1093. DEBUG(dbgs() << "*** " << (IsTopNode ? "Top" : "Bottom")
  1094. << " Scheduling Instruction in cycle "
  1095. << (IsTopNode ? Top.CurrCycle : Bot.CurrCycle) << '\n';
  1096. SU->dump(DAG));
  1097. return SU;
  1098. }
  1099. /// Update the scheduler's state after scheduling a node. This is the same node
  1100. /// that was just returned by pickNode(). However, ScheduleDAGMI needs to update
  1101. /// it's state based on the current cycle before MachineSchedStrategy does.
  1102. void ConvergingScheduler::schedNode(SUnit *SU, bool IsTopNode) {
  1103. if (IsTopNode) {
  1104. SU->TopReadyCycle = Top.CurrCycle;
  1105. Top.bumpNode(SU);
  1106. }
  1107. else {
  1108. SU->BotReadyCycle = Bot.CurrCycle;
  1109. Bot.bumpNode(SU);
  1110. }
  1111. }
  1112. /// Create the standard converging machine scheduler. This will be used as the
  1113. /// default scheduler if the target does not set a default.
  1114. static ScheduleDAGInstrs *createConvergingSched(MachineSchedContext *C) {
  1115. assert((!ForceTopDown || !ForceBottomUp) &&
  1116. "-misched-topdown incompatible with -misched-bottomup");
  1117. return new ScheduleDAGMI(C, new ConvergingScheduler());
  1118. }
  1119. static MachineSchedRegistry
  1120. ConvergingSchedRegistry("converge", "Standard converging scheduler.",
  1121. createConvergingSched);
  1122. //===----------------------------------------------------------------------===//
  1123. // Machine Instruction Shuffler for Correctness Testing
  1124. //===----------------------------------------------------------------------===//
  1125. #ifndef NDEBUG
  1126. namespace {
  1127. /// Apply a less-than relation on the node order, which corresponds to the
  1128. /// instruction order prior to scheduling. IsReverse implements greater-than.
  1129. template<bool IsReverse>
  1130. struct SUnitOrder {
  1131. bool operator()(SUnit *A, SUnit *B) const {
  1132. if (IsReverse)
  1133. return A->NodeNum > B->NodeNum;
  1134. else
  1135. return A->NodeNum < B->NodeNum;
  1136. }
  1137. };
  1138. /// Reorder instructions as much as possible.
  1139. class InstructionShuffler : public MachineSchedStrategy {
  1140. bool IsAlternating;
  1141. bool IsTopDown;
  1142. // Using a less-than relation (SUnitOrder<false>) for the TopQ priority
  1143. // gives nodes with a higher number higher priority causing the latest
  1144. // instructions to be scheduled first.
  1145. PriorityQueue<SUnit*, std::vector<SUnit*>, SUnitOrder<false> >
  1146. TopQ;
  1147. // When scheduling bottom-up, use greater-than as the queue priority.
  1148. PriorityQueue<SUnit*, std::vector<SUnit*>, SUnitOrder<true> >
  1149. BottomQ;
  1150. public:
  1151. InstructionShuffler(bool alternate, bool topdown)
  1152. : IsAlternating(alternate), IsTopDown(topdown) {}
  1153. virtual void initialize(ScheduleDAGMI *) {
  1154. TopQ.clear();
  1155. BottomQ.clear();
  1156. }
  1157. /// Implement MachineSchedStrategy interface.
  1158. /// -----------------------------------------
  1159. virtual SUnit *pickNode(bool &IsTopNode) {
  1160. SUnit *SU;
  1161. if (IsTopDown) {
  1162. do {
  1163. if (TopQ.empty()) return NULL;
  1164. SU = TopQ.top();
  1165. TopQ.pop();
  1166. } while (SU->isScheduled);
  1167. IsTopNode = true;
  1168. }
  1169. else {
  1170. do {
  1171. if (BottomQ.empty()) return NULL;
  1172. SU = BottomQ.top();
  1173. BottomQ.pop();
  1174. } while (SU->isScheduled);
  1175. IsTopNode = false;
  1176. }
  1177. if (IsAlternating)
  1178. IsTopDown = !IsTopDown;
  1179. return SU;
  1180. }
  1181. virtual void schedNode(SUnit *SU, bool IsTopNode) {}
  1182. virtual void releaseTopNode(SUnit *SU) {
  1183. TopQ.push(SU);
  1184. }
  1185. virtual void releaseBottomNode(SUnit *SU) {
  1186. BottomQ.push(SU);
  1187. }
  1188. };
  1189. } // namespace
  1190. static ScheduleDAGInstrs *createInstructionShuffler(MachineSchedContext *C) {
  1191. bool Alternate = !ForceTopDown && !ForceBottomUp;
  1192. bool TopDown = !ForceBottomUp;
  1193. assert((TopDown || !ForceTopDown) &&
  1194. "-misched-topdown incompatible with -misched-bottomup");
  1195. return new ScheduleDAGMI(C, new InstructionShuffler(Alternate, TopDown));
  1196. }
  1197. static MachineSchedRegistry ShufflerRegistry(
  1198. "shuffle", "Shuffle machine instructions alternating directions",
  1199. createInstructionShuffler);
  1200. #endif // !NDEBUG